xref: /openbsd/sys/msdosfs/msdosfs_vnops.c (revision 5a0ec814)
1 /*	$OpenBSD: msdosfs_vnops.c,v 1.143 2024/10/18 05:52:32 miod Exp $	*/
2 /*	$NetBSD: msdosfs_vnops.c,v 1.63 1997/10/17 11:24:19 ws Exp $	*/
3 
4 /*-
5  * Copyright (C) 2005 Thomas Wang.
6  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8  * All rights reserved.
9  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by TooLs GmbH.
22  * 4. The name of TooLs GmbH may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*
37  * Written by Paul Popelka (paulp@uts.amdahl.com)
38  *
39  * You can do anything you want with this software, just don't say you wrote
40  * it, and don't remove this notice.
41  *
42  * This software is provided "as is".
43  *
44  * The author supplies this software to be publicly redistributed on the
45  * understanding that the author is not responsible for the correct
46  * functioning of this software in any circumstances and is not liable for
47  * any damages caused by this software.
48  *
49  * October 1992
50  */
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/namei.h>
55 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
56 #include <sys/fcntl.h>		/* define FWRITE ... */
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #include <sys/buf.h>
60 #include <sys/proc.h>
61 #include <sys/mount.h>
62 #include <sys/vnode.h>
63 #include <sys/lock.h>
64 #include <sys/signalvar.h>
65 #include <sys/specdev.h> /* XXX */	/* defines v_rdev */
66 #include <sys/malloc.h>
67 #include <sys/pool.h>
68 #include <sys/dirent.h>		/* defines dirent structure */
69 #include <sys/lockf.h>
70 #include <sys/unistd.h>
71 
72 #include <msdosfs/bpb.h>
73 #include <msdosfs/direntry.h>
74 #include <msdosfs/denode.h>
75 #include <msdosfs/msdosfsmount.h>
76 #include <msdosfs/fat.h>
77 
78 static uint32_t fileidhash(uint64_t);
79 
80 int msdosfs_bmaparray(struct vnode *, uint32_t, daddr_t *, int *);
81 int msdosfs_kqfilter(void *);
82 int filt_msdosfsread(struct knote *, long);
83 int filt_msdosfswrite(struct knote *, long);
84 int filt_msdosfsvnode(struct knote *, long);
85 void filt_msdosfsdetach(struct knote *);
86 
87 /*
88  * Some general notes:
89  *
90  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
91  * read/written using the vnode for the filesystem. Blocks that represent
92  * the contents of a file are read/written using the vnode for the file
93  * (including directories when they are read/written as files). This
94  * presents problems for the dos filesystem because data that should be in
95  * an inode (if dos had them) resides in the directory itself.  Since we
96  * must update directory entries without the benefit of having the vnode
97  * for the directory we must use the vnode for the filesystem.  This means
98  * that when a directory is actually read/written (via read, write, or
99  * readdir, or seek) we must use the vnode for the filesystem instead of
100  * the vnode for the directory as would happen in ufs. This is to insure we
101  * retrieve the correct block from the buffer cache since the hash value is
102  * based upon the vnode address and the desired block number.
103  */
104 
105 /*
106  * Create a regular file. On entry the directory to contain the file being
107  * created is locked.  We must release before we return. We must also free
108  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
109  * only if the SAVESTART bit in cn_flags is clear on success.
110  */
111 int
msdosfs_create(void * v)112 msdosfs_create(void *v)
113 {
114 	struct vop_create_args *ap = v;
115 	struct componentname *cnp = ap->a_cnp;
116 	struct denode ndirent;
117 	struct denode *dep;
118 	struct denode *pdep = VTODE(ap->a_dvp);
119 	int error;
120 	struct timespec ts;
121 
122 #ifdef MSDOSFS_DEBUG
123 	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
124 #endif
125 
126 	/*
127 	 * If this is the root directory and there is no space left we
128 	 * can't do anything.  This is because the root directory can not
129 	 * change size.
130 	 */
131 	if (pdep->de_StartCluster == MSDOSFSROOT
132 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
133 		error = ENOSPC;
134 		goto bad;
135 	}
136 
137 	/*
138 	 * Create a directory entry for the file, then call createde() to
139 	 * have it installed. NOTE: DOS files are always executable.  We
140 	 * use the absence of the owner write bit to make the file
141 	 * readonly.
142 	 */
143 #ifdef DIAGNOSTIC
144 	if ((cnp->cn_flags & HASBUF) == 0)
145 		panic("msdosfs_create: no name");
146 #endif
147 	bzero(&ndirent, sizeof(ndirent));
148 	if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
149 		goto bad;
150 
151 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
152 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
153 	ndirent.de_StartCluster = 0;
154 	ndirent.de_FileSize = 0;
155 	ndirent.de_dev = pdep->de_dev;
156 	ndirent.de_devvp = pdep->de_devvp;
157 	ndirent.de_pmp = pdep->de_pmp;
158 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
159 	getnanotime(&ts);
160 	DETIMES(&ndirent, &ts, &ts, &ts);
161 	if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
162 		goto bad;
163 	if ((cnp->cn_flags & SAVESTART) == 0)
164 		pool_put(&namei_pool, cnp->cn_pnbuf);
165 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
166 	*ap->a_vpp = DETOV(dep);
167 	return (0);
168 
169 bad:
170 	pool_put(&namei_pool, cnp->cn_pnbuf);
171 	return (error);
172 }
173 
174 int
msdosfs_mknod(void * v)175 msdosfs_mknod(void *v)
176 {
177 	struct vop_mknod_args *ap = v;
178 
179 	pool_put(&namei_pool, ap->a_cnp->cn_pnbuf);
180 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
181 	return (EINVAL);
182 }
183 
184 int
msdosfs_open(void * v)185 msdosfs_open(void *v)
186 {
187 	return (0);
188 }
189 
190 int
msdosfs_close(void * v)191 msdosfs_close(void *v)
192 {
193 	struct vop_close_args *ap = v;
194 	struct vnode *vp = ap->a_vp;
195 	struct denode *dep = VTODE(vp);
196 	struct timespec ts;
197 
198 	if (vp->v_usecount > 1 && !VOP_ISLOCKED(vp)) {
199 		getnanotime(&ts);
200 		DETIMES(dep, &ts, &ts, &ts);
201 	}
202 	return (0);
203 }
204 
205 int
msdosfs_access(void * v)206 msdosfs_access(void *v)
207 {
208 	struct vop_access_args *ap = v;
209 	struct denode *dep = VTODE(ap->a_vp);
210 	struct msdosfsmount *pmp = dep->de_pmp;
211 	mode_t dosmode;
212 
213 	dosmode = (S_IRUSR | S_IRGRP | S_IROTH);
214 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
215 		dosmode |= (S_IWUSR | S_IWGRP | S_IWOTH);
216 	if (dep->de_Attributes & ATTR_DIRECTORY)
217 		dosmode |= (S_IXUSR | S_IXGRP | S_IXOTH);
218 	dosmode &= pmp->pm_mask;
219 
220 	return (vaccess(ap->a_vp->v_type, dosmode, pmp->pm_uid, pmp->pm_gid,
221 	    ap->a_mode, ap->a_cred));
222 }
223 
224 int
msdosfs_getattr(void * v)225 msdosfs_getattr(void *v)
226 {
227 	struct vop_getattr_args *ap = v;
228 	struct denode *dep = VTODE(ap->a_vp);
229 	struct msdosfsmount *pmp = dep->de_pmp;
230 	struct vattr *vap = ap->a_vap;
231 	struct timespec ts;
232 	uint32_t fileid;
233 
234 	getnanotime(&ts);
235 	DETIMES(dep, &ts, &ts, &ts);
236 	vap->va_fsid = dep->de_dev;
237 
238 	/*
239 	 * The following computation of the fileid must be the same as
240 	 * that used in msdosfs_readdir() to compute d_fileno. If not,
241 	 * pwd doesn't work.
242 	 *
243 	 * We now use the starting cluster number as the fileid/fileno.
244 	 * This works for both files and directories (including the root
245 	 * directory, on FAT32).  Even on FAT32, this will at most be a
246 	 * 28-bit number, as the high 4 bits of FAT32 cluster numbers
247 	 * are reserved.
248 	 *
249 	 * However, we do need to do something for 0-length files, which
250 	 * will not have a starting cluster number.
251 	 *
252 	 * These files cannot be directories, since (except for /, which
253 	 * is special-cased anyway) directories contain entries for . and
254 	 * .., so must have non-zero length.
255 	 *
256 	 * In this case, we just create a non-cryptographic hash of the
257 	 * original fileid calculation, and set the top bit.
258 	 *
259 	 * This algorithm has the benefit that all directories, and all
260 	 * non-zero-length files, will have fileids that are persistent
261 	 * across mounts and reboots, and that cannot collide (as long
262 	 * as the filesystem is not corrupt).  Zero-length files will
263 	 * have fileids that are persistent, but that may collide.  We
264 	 * will just have to live with that.
265 	 */
266 	fileid = dep->de_StartCluster;
267 
268 	if (dep->de_Attributes & ATTR_DIRECTORY) {
269 		/* Special-case root */
270 		if (dep->de_StartCluster == MSDOSFSROOT)
271 			fileid = FAT32(pmp) ? pmp->pm_rootdirblk : 1;
272 	} else {
273 		if (dep->de_FileSize == 0) {
274 			uint32_t dirsperblk;
275 			uint64_t fileid64;
276 
277 			dirsperblk = pmp->pm_BytesPerSec /
278 			    sizeof(struct direntry);
279 
280 			fileid64 = (dep->de_dirclust == MSDOSFSROOT) ?
281 			    roottobn(pmp, 0) : cntobn(pmp, dep->de_dirclust);
282 			fileid64 *= dirsperblk;
283 			fileid64 += dep->de_diroffset / sizeof(struct direntry);
284 
285 			fileid = fileidhash(fileid64);
286 		}
287 	}
288 
289 	vap->va_fileid = fileid;
290 	vap->va_mode = (S_IRUSR|S_IRGRP|S_IROTH);
291 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
292 		vap->va_mode |= (S_IWUSR|S_IWGRP|S_IWOTH);
293 	if (dep->de_Attributes & ATTR_DIRECTORY) {
294 		vap->va_mode |= S_IFDIR;
295 		vap->va_mode |= (vap->va_mode & S_IRUSR) ? S_IXUSR : 0;
296 		vap->va_mode |= (vap->va_mode & S_IRGRP) ? S_IXGRP : 0;
297 		vap->va_mode |= (vap->va_mode & S_IROTH) ? S_IXOTH : 0;
298 	}
299 	vap->va_mode &= dep->de_pmp->pm_mask;
300 	vap->va_nlink = 1;
301 	vap->va_gid = dep->de_pmp->pm_gid;
302 	vap->va_uid = dep->de_pmp->pm_uid;
303 	vap->va_rdev = 0;
304 	vap->va_size = dep->de_FileSize;
305 	dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
306 	if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
307 		dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
308 		dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CTimeHundredth, &vap->va_ctime);
309 	} else {
310 		vap->va_atime = vap->va_mtime;
311 		vap->va_ctime = vap->va_mtime;
312 	}
313 	vap->va_flags = 0;
314 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
315 		vap->va_flags |= SF_ARCHIVED;
316 	vap->va_gen = 0;
317 	vap->va_blocksize = dep->de_pmp->pm_bpcluster;
318 	vap->va_bytes = (dep->de_FileSize + dep->de_pmp->pm_crbomask) &
319 				~(dep->de_pmp->pm_crbomask);
320 	vap->va_type = ap->a_vp->v_type;
321 	return (0);
322 }
323 
324 int
msdosfs_setattr(void * v)325 msdosfs_setattr(void *v)
326 {
327 	struct vop_setattr_args *ap = v;
328 	struct vnode *vp = ap->a_vp;
329 	struct denode *dep = VTODE(ap->a_vp);
330 	struct msdosfsmount *pmp = dep->de_pmp;
331 	struct vattr *vap = ap->a_vap;
332 	struct ucred *cred = ap->a_cred;
333 	int error = 0;
334 
335 #ifdef MSDOSFS_DEBUG
336 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
337 	    ap->a_vp, vap, cred, ap->a_p);
338 #endif
339 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
340 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
341 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
342 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
343 #ifdef MSDOSFS_DEBUG
344 		printf("msdosfs_setattr(): returning EINVAL\n");
345 		printf("    va_type %d, va_nlink %x, va_fsid %ld, "
346 		    "va_fileid %llx\n", vap->va_type, vap->va_nlink,
347 		    vap->va_fsid, (unsigned long long)vap->va_fileid);
348 		printf("    va_blocksize %lx, va_rdev %x, va_bytes %llx, "
349 		    "va_gen %lx\n", vap->va_blocksize,
350 		    (unsigned int)vap->va_rdev,
351 		    (unsigned long long)vap->va_bytes, vap->va_gen);
352 #endif
353 		return (EINVAL);
354 	}
355 	if (vap->va_flags != VNOVAL) {
356 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
357 			return (EINVAL);
358 		if (cred->cr_uid != pmp->pm_uid) {
359 			error = suser_ucred(cred);
360 			if (error)
361 				return (error);
362 		}
363 		/*
364 		 * We are very inconsistent about handling unsupported
365 		 * attributes.  We ignored the access time and the
366 		 * read and execute bits.  We were strict for the other
367 		 * attributes.
368 		 *
369 		 * Here we are strict, stricter than ufs in not allowing
370 		 * users to attempt to set SF_SETTABLE bits or anyone to
371 		 * set unsupported bits.  However, we ignore attempts to
372 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
373 		 * sensible filesystem attempts it a lot.
374 		 */
375 		if (vap->va_flags & SF_SETTABLE) {
376 			error = suser_ucred(cred);
377 			if (error)
378 				return (error);
379 		}
380 		if (vap->va_flags & ~SF_ARCHIVED)
381 			return EOPNOTSUPP;
382 		if (vap->va_flags & SF_ARCHIVED)
383 			dep->de_Attributes &= ~ATTR_ARCHIVE;
384 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
385 			dep->de_Attributes |= ATTR_ARCHIVE;
386 		dep->de_flag |= DE_MODIFIED;
387 	}
388 
389 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
390 		uid_t uid;
391 		gid_t gid;
392 
393 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
394 			return (EINVAL);
395 		uid = vap->va_uid;
396 		if (uid == (uid_t)VNOVAL)
397 			uid = pmp->pm_uid;
398 		gid = vap->va_gid;
399 		if (gid == (gid_t)VNOVAL)
400 			gid = pmp->pm_gid;
401 		if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
402 		    (gid != pmp->pm_gid && !groupmember(gid, cred))) {
403 			error = suser_ucred(cred);
404 			if (error)
405 				return (error);
406 		}
407 		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
408 			return EINVAL;
409 	}
410 
411 	if (vap->va_size != VNOVAL) {
412 		switch (vp->v_type) {
413 		case VDIR:
414 			return (EISDIR);
415 		case VREG:
416 			/*
417 			 * Truncation is only supported for regular files,
418 			 * Disallow it if the filesystem is read-only.
419 			 */
420 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
421 				return (EINVAL);
422 			break;
423 		default:
424 			/*
425 			 * According to POSIX, the result is unspecified
426 			 * for file types other than regular files,
427 			 * directories and shared memory objects.  We
428 			 * don't support any file types except regular
429 			 * files and directories in this file system, so
430 			 * this (default) case is unreachable and can do
431 			 * anything.  Keep falling through to detrunc()
432 			 * for now.
433 			 */
434 			break;
435 		}
436 		error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
437 		if (error)
438 			return error;
439 	}
440 	if ((vap->va_vaflags & VA_UTIMES_CHANGE) ||
441 	    vap->va_atime.tv_nsec != VNOVAL ||
442 	    vap->va_mtime.tv_nsec != VNOVAL) {
443 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
444 			return (EINVAL);
445 		if (cred->cr_uid != pmp->pm_uid &&
446 		    (error = suser_ucred(cred)) &&
447 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
448 		    (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_p))))
449 			return (error);
450 		if (vp->v_type != VDIR) {
451 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
452 			    vap->va_atime.tv_nsec != VNOVAL) {
453 				dep->de_flag &= ~DE_ACCESS;
454 				unix2dostime(&vap->va_atime, &dep->de_ADate,
455 				    NULL, NULL);
456 			}
457 			if (vap->va_mtime.tv_nsec != VNOVAL) {
458 				dep->de_flag &= ~DE_UPDATE;
459 				unix2dostime(&vap->va_mtime, &dep->de_MDate,
460 				    &dep->de_MTime, NULL);
461 			}
462 			dep->de_Attributes |= ATTR_ARCHIVE;
463 			dep->de_flag |= DE_MODIFIED;
464 		}
465 	}
466 	/*
467 	 * DOS files only have the ability to have their writability
468 	 * attribute set, so we use the owner write bit to set the readonly
469 	 * attribute.
470 	 */
471 	if (vap->va_mode != (mode_t)VNOVAL) {
472 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
473 			return (EINVAL);
474 		if (cred->cr_uid != pmp->pm_uid) {
475 			error = suser_ucred(cred);
476 			if (error)
477 				return (error);
478 		}
479 		if (vp->v_type != VDIR) {
480 			/* We ignore the read and execute bits. */
481 			if (vap->va_mode & VWRITE)
482 				dep->de_Attributes &= ~ATTR_READONLY;
483 			else
484 				dep->de_Attributes |= ATTR_READONLY;
485 			dep->de_Attributes |= ATTR_ARCHIVE;
486 			dep->de_flag |= DE_MODIFIED;
487 		}
488 	}
489 	VN_KNOTE(ap->a_vp, NOTE_ATTRIB);
490 	return (deupdat(dep, 1));
491 }
492 
493 int
msdosfs_read(void * v)494 msdosfs_read(void *v)
495 {
496 	struct vop_read_args *ap = v;
497 	struct vnode *vp = ap->a_vp;
498 	struct denode *dep = VTODE(vp);
499 	struct msdosfsmount *pmp = dep->de_pmp;
500 	struct uio *uio = ap->a_uio;
501 	int isadir, error = 0;
502 	uint32_t n, diff, size, on;
503 	struct buf *bp;
504 	uint32_t cn;
505 	daddr_t bn;
506 
507 	/*
508 	 * If they didn't ask for any data, then we are done.
509 	 */
510 	if (uio->uio_resid == 0)
511 		return (0);
512 	if (uio->uio_offset < 0)
513 		return (EINVAL);
514 
515 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
516 	do {
517 		if (uio->uio_offset >= dep->de_FileSize)
518 			return (0);
519 
520 		cn = de_cluster(pmp, uio->uio_offset);
521 		size = pmp->pm_bpcluster;
522 		on = uio->uio_offset & pmp->pm_crbomask;
523 		n = ulmin(pmp->pm_bpcluster - on, uio->uio_resid);
524 
525 		/*
526 		 * de_FileSize is uint32_t, and we know that uio_offset <
527 		 * de_FileSize, so uio->uio_offset < 2^32.  Therefore
528 		 * the cast to uint32_t on the next line is safe.
529 		 */
530 		diff = dep->de_FileSize - (uint32_t)uio->uio_offset;
531 		if (diff < n)
532 			n = diff;
533 
534 		/*
535 		 * If we are operating on a directory file then be sure to
536 		 * do i/o with the vnode for the filesystem instead of the
537 		 * vnode for the directory.
538 		 */
539 		if (isadir) {
540 			/* convert cluster # to block # */
541 			error = pcbmap(dep, cn, &bn, 0, &size);
542 			if (error)
543 				return (error);
544 			error = bread(pmp->pm_devvp, bn, size, &bp);
545 		} else {
546 			if (de_cn2off(pmp, cn + 1) >= dep->de_FileSize)
547 				error = bread(vp, cn, size, &bp);
548 			else
549 				error = bread_cluster(vp, cn, size, &bp);
550 		}
551 		n = min(n, pmp->pm_bpcluster - bp->b_resid);
552 		if (error) {
553 			brelse(bp);
554 			return (error);
555 		}
556 		error = uiomove(bp->b_data + on, n, uio);
557 		brelse(bp);
558 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
559 	if (!isadir && !(vp->v_mount->mnt_flag & MNT_NOATIME))
560 		dep->de_flag |= DE_ACCESS;
561 	return (error);
562 }
563 
564 /*
565  * Write data to a file or directory.
566  */
567 int
msdosfs_write(void * v)568 msdosfs_write(void *v)
569 {
570 	struct vop_write_args *ap = v;
571 	uint32_t n, croffset;
572 	size_t resid;
573 	ssize_t overrun;
574 	int extended = 0;
575 	uint32_t osize;
576 	int error = 0;
577 	uint32_t count, lastcn, cn;
578 	struct buf *bp;
579 	int ioflag = ap->a_ioflag;
580 	struct uio *uio = ap->a_uio;
581 	struct vnode *vp = ap->a_vp;
582 	struct vnode *thisvp;
583 	struct denode *dep = VTODE(vp);
584 	struct msdosfsmount *pmp = dep->de_pmp;
585 	struct ucred *cred = ap->a_cred;
586 
587 #ifdef MSDOSFS_DEBUG
588 	printf("msdosfs_write(vp %p, uio %p, ioflag %08x, cred %p\n",
589 	    vp, uio, ioflag, cred);
590 	printf("msdosfs_write(): diroff %d, dirclust %d, startcluster %d\n",
591 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
592 #endif
593 
594 	switch (vp->v_type) {
595 	case VREG:
596 		if (ioflag & IO_APPEND)
597 			uio->uio_offset = dep->de_FileSize;
598 		thisvp = vp;
599 		break;
600 	case VDIR:
601 		return EISDIR;
602 	default:
603 		panic("msdosfs_write(): bad file type");
604 	}
605 
606 	if (uio->uio_offset < 0)
607 		return (EINVAL);
608 
609 	if (uio->uio_resid == 0)
610 		return (0);
611 
612 	/* Don't bother to try to write files larger than the f/s limit */
613 	if (uio->uio_offset > MSDOSFS_FILESIZE_MAX ||
614 	    uio->uio_resid > (MSDOSFS_FILESIZE_MAX - uio->uio_offset))
615 		return (EFBIG);
616 
617 	/* do the filesize rlimit check */
618 	if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
619 		return (error);
620 
621 	/*
622 	 * If the offset we are starting the write at is beyond the end of
623 	 * the file, then they've done a seek.  Unix filesystems allow
624 	 * files with holes in them, DOS doesn't so we must fill the hole
625 	 * with zeroed blocks.
626 	 */
627 	if (uio->uio_offset > dep->de_FileSize) {
628 		if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
629 			goto out;
630 	}
631 
632 	/*
633 	 * Remember some values in case the write fails.
634 	 */
635 	resid = uio->uio_resid;
636 	osize = dep->de_FileSize;
637 
638 	/*
639 	 * If we write beyond the end of the file, extend it to its ultimate
640 	 * size ahead of the time to hopefully get a contiguous area.
641 	 */
642 	if (uio->uio_offset + resid > osize) {
643 		extended = 1;
644 		count = de_clcount(pmp, uio->uio_offset + resid) -
645 			de_clcount(pmp, osize);
646 		if ((error = extendfile(dep, count, NULL, NULL, 0)) &&
647 		    (error != ENOSPC || (ioflag & IO_UNIT)))
648 			goto errexit;
649 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
650 	} else
651 		lastcn = de_clcount(pmp, osize) - 1;
652 
653 	do {
654 		croffset = uio->uio_offset & pmp->pm_crbomask;
655 		cn = de_cluster(pmp, uio->uio_offset);
656 
657 		if (cn > lastcn) {
658 			error = ENOSPC;
659 			break;
660 		}
661 
662 		if (croffset == 0 &&
663 		    (de_cluster(pmp, uio->uio_offset + uio->uio_resid) > cn ||
664 		     (uio->uio_offset + uio->uio_resid) >= dep->de_FileSize)) {
665 			/*
666 			 * If either the whole cluster gets written,
667 			 * or we write the cluster from its start beyond EOF,
668 			 * then no need to read data from disk.
669 			 */
670 			bp = getblk(thisvp, cn, pmp->pm_bpcluster, 0, INFSLP);
671 			clrbuf(bp);
672 			/*
673 			 * Do the bmap now, since pcbmap needs buffers
674 			 * for the fat table. (see msdosfs_strategy)
675 			 */
676 			if (bp->b_blkno == bp->b_lblkno) {
677 				error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
678 				if (error)
679 					bp->b_blkno = -1;
680 			}
681 			if (bp->b_blkno == -1) {
682 				brelse(bp);
683 				if (!error)
684 					error = EIO;		/* XXX */
685 				break;
686 			}
687 		} else {
688 			/*
689 			 * The block we need to write into exists, so
690 			 * read it in.
691 			 */
692 			error = bread(thisvp, cn, pmp->pm_bpcluster, &bp);
693 			if (error) {
694 				brelse(bp);
695 				break;
696 			}
697 		}
698 
699 		n = ulmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
700 		if (uio->uio_offset + n > dep->de_FileSize) {
701 			dep->de_FileSize = uio->uio_offset + n;
702 			uvm_vnp_setsize(vp, dep->de_FileSize);
703 		}
704 		uvm_vnp_uncache(vp);
705 		/*
706 		 * Should these vnode_pager_* functions be done on dir
707 		 * files?
708 		 */
709 
710 		/*
711 		 * Copy the data from user space into the buf header.
712 		 */
713 		error = uiomove(bp->b_data + croffset, n, uio);
714 
715 		/*
716 		 * If they want this synchronous then write it and wait for
717 		 * it.  Otherwise, if on a cluster boundary write it
718 		 * asynchronously so we can move on to the next block
719 		 * without delay.  Otherwise do a delayed write because we
720 		 * may want to write some more into the block later.
721 		 */
722 #if 0
723 		if (ioflag & IO_NOCACHE)
724 			bp->b_flags |= B_NOCACHE;
725 #endif
726 		if (ioflag & IO_SYNC)
727 			(void) bwrite(bp);
728 		else if (n + croffset == pmp->pm_bpcluster)
729 			bawrite(bp);
730 		else
731 			bdwrite(bp);
732 		dep->de_flag |= DE_UPDATE;
733 	} while (error == 0 && uio->uio_resid > 0);
734 
735 	if (resid > uio->uio_resid)
736 		 VN_KNOTE(ap->a_vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
737 
738 	if (dep->de_FileSize < osize)
739 		VN_KNOTE(ap->a_vp, NOTE_TRUNCATE);
740 
741 	/*
742 	 * If the write failed and they want us to, truncate the file back
743 	 * to the size it was before the write was attempted.
744 	 */
745 errexit:
746 	if (error) {
747 		if (ioflag & IO_UNIT) {
748 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, curproc);
749 			uio->uio_offset -= resid - uio->uio_resid;
750 			uio->uio_resid = resid;
751 		} else {
752 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, curproc);
753 			if (uio->uio_resid != resid)
754 				error = 0;
755 		}
756 	} else if (ioflag & IO_SYNC)
757 		error = deupdat(dep, 1);
758 
759 out:
760 	/* correct the result for writes clamped by vn_fsizechk() */
761 	uio->uio_resid += overrun;
762 	return (error);
763 }
764 
765 int
msdosfs_ioctl(void * v)766 msdosfs_ioctl(void *v)
767 {
768 	return (ENOTTY);
769 }
770 
771 /*
772  * Flush the blocks of a file to disk.
773  *
774  * This function is worthless for vnodes that represent directories. Maybe we
775  * could just do a sync if they try an fsync on a directory file.
776  */
777 int
msdosfs_fsync(void * v)778 msdosfs_fsync(void *v)
779 {
780 	struct vop_fsync_args *ap = v;
781 	struct vnode *vp = ap->a_vp;
782 
783 	vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
784 	return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
785 }
786 
787 /*
788  * Flush the blocks of a file to disk.
789  *
790  * This function is worthless for vnodes that represent directories. Maybe we
791  * could just do a sync if they try an fsync on a directory file.
792  */
793 int
msdosfs_remove(void * v)794 msdosfs_remove(void *v)
795 {
796 	struct vop_remove_args *ap = v;
797 	struct denode *dep = VTODE(ap->a_vp);
798 	struct denode *ddep = VTODE(ap->a_dvp);
799 	int error;
800 
801 	if (ap->a_vp->v_type == VDIR)
802 		error = EPERM;
803 	else
804 		error = removede(ddep, dep);
805 
806 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
807 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
808 
809 #ifdef MSDOSFS_DEBUG
810 	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep,
811 	    ap->a_vp->v_usecount);
812 #endif
813 	return (error);
814 }
815 
816 /*
817  * DOS filesystems don't know what links are. But since we already called
818  * msdosfs_lookup() with create and lockparent, the parent is locked so we
819  * have to free it before we return the error.
820  */
821 int
msdosfs_link(void * v)822 msdosfs_link(void *v)
823 {
824 	struct vop_link_args *ap = v;
825 
826 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
827 	vput(ap->a_dvp);
828 	return (EOPNOTSUPP);
829 }
830 
831 /*
832  * Renames on files require moving the denode to a new hash queue since the
833  * denode's location is used to compute which hash queue to put the file
834  * in. Unless it is a rename in place.  For example "mv a b".
835  *
836  * What follows is the basic algorithm:
837  *
838  * if (file move) {
839  *	if (dest file exists) {
840  *		remove dest file
841  *	}
842  *	if (dest and src in same directory) {
843  *		rewrite name in existing directory slot
844  *	} else {
845  *		write new entry in dest directory
846  *		update offset and dirclust in denode
847  *		move denode to new hash chain
848  *		clear old directory entry
849  *	}
850  * } else {
851  *	directory move
852  *	if (dest directory exists) {
853  *		if (dest is not empty) {
854  *			return ENOTEMPTY
855  *		}
856  *		remove dest directory
857  *	}
858  *	if (dest and src in same directory) {
859  *		rewrite name in existing entry
860  *	} else {
861  *		be sure dest is not a child of src directory
862  *		write entry in dest directory
863  *		update "." and ".." in moved directory
864  *		update offset and dirclust in denode
865  *		move denode to new hash chain
866  *		clear old directory entry for moved directory
867  *	}
868  * }
869  *
870  * On entry:
871  *	source's parent directory is unlocked
872  *	source file or directory is unlocked
873  *	destination's parent directory is locked
874  *	destination file or directory is locked if it exists
875  *
876  * On exit:
877  *	all denodes should be released
878  *
879  * Notes:
880  * I'm not sure how the memory containing the pathnames pointed at by the
881  * componentname structures is freed, there may be some memory bleeding
882  * for each rename done.
883  */
884 int
msdosfs_rename(void * v)885 msdosfs_rename(void *v)
886 {
887 	struct vop_rename_args *ap = v;
888 	struct vnode *tvp = ap->a_tvp;
889 	struct vnode *tdvp = ap->a_tdvp;
890 	struct vnode *fvp = ap->a_fvp;
891 	struct vnode *fdvp = ap->a_fdvp;
892 	struct componentname *tcnp = ap->a_tcnp;
893 	struct componentname *fcnp = ap->a_fcnp;
894 	struct denode *ip, *xp, *dp, *zp;
895 	u_char toname[11], oldname[11];
896 	uint32_t from_diroffset, to_diroffset;
897 	u_char to_count;
898 	int doingdirectory = 0, newparent = 0;
899 	int error;
900 	uint32_t cn, pcl;
901 	daddr_t bn;
902 	struct msdosfsmount *pmp;
903 	struct direntry *dotdotp;
904 	struct buf *bp;
905 
906 	pmp = VFSTOMSDOSFS(fdvp->v_mount);
907 
908 #ifdef DIAGNOSTIC
909 	if ((tcnp->cn_flags & HASBUF) == 0 ||
910 	    (fcnp->cn_flags & HASBUF) == 0)
911 		panic("msdosfs_rename: no name");
912 #endif
913 	/*
914 	 * Check for cross-device rename.
915 	 */
916 	if ((fvp->v_mount != tdvp->v_mount) ||
917 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
918 		error = EXDEV;
919 abortit:
920 		VOP_ABORTOP(tdvp, tcnp);
921 		if (tdvp == tvp)
922 			vrele(tdvp);
923 		else
924 			vput(tdvp);
925 		if (tvp)
926 			vput(tvp);
927 		VOP_ABORTOP(fdvp, fcnp);
928 		vrele(fdvp);
929 		vrele(fvp);
930 		return (error);
931 	}
932 
933 	/*
934 	 * If source and dest are the same, do nothing.
935 	 */
936 	if (tvp == fvp) {
937 		error = 0;
938 		goto abortit;
939 	}
940 
941 	/* */
942 	if ((error = vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY)) != 0)
943 		goto abortit;
944 	dp = VTODE(fdvp);
945 	ip = VTODE(fvp);
946 
947 	/*
948 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
949 	 * leads to a crippled directory tree.  It's pretty tough to do a
950 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
951 	 * doesn't work if the ".." entry is missing.
952 	 */
953 	if (ip->de_Attributes & ATTR_DIRECTORY) {
954 		/*
955 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
956 		 */
957 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
958 		    dp == ip ||
959 		    (fcnp->cn_flags & ISDOTDOT) ||
960 		    (tcnp->cn_flags & ISDOTDOT) ||
961 		    (ip->de_flag & DE_RENAME)) {
962 			VOP_UNLOCK(fvp);
963 			error = EINVAL;
964 			goto abortit;
965 		}
966 		ip->de_flag |= DE_RENAME;
967 		doingdirectory++;
968 	}
969 	VN_KNOTE(fdvp, NOTE_WRITE);	/* XXX right place? */
970 
971 	/*
972 	 * When the target exists, both the directory
973 	 * and target vnodes are returned locked.
974 	 */
975 	dp = VTODE(tdvp);
976 	xp = tvp ? VTODE(tvp) : NULL;
977 	/*
978 	 * Remember direntry place to use for destination
979 	 */
980 	to_diroffset = dp->de_fndoffset;
981 	to_count = dp->de_fndcnt;
982 
983 	/*
984 	 * If ".." must be changed (ie the directory gets a new
985 	 * parent) then the source directory must not be in the
986 	 * directory hierarchy above the target, as this would
987 	 * orphan everything below the source directory. Also
988 	 * the user must have write permission in the source so
989 	 * as to be able to change "..". We must repeat the call
990 	 * to namei, as the parent directory is unlocked by the
991 	 * call to doscheckpath().
992 	 */
993 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
994 	VOP_UNLOCK(fvp);
995 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
996 		newparent = 1;
997 	vrele(fdvp);
998 	if (doingdirectory && newparent) {
999 		if (error)	/* write access check above */
1000 			goto bad1;
1001 		if (xp != NULL)
1002 			vput(tvp);
1003 		/*
1004 		 * doscheckpath() vput()'s dp,
1005 		 * so we have to do a relookup afterwards
1006 		 */
1007 		if ((error = doscheckpath(ip, dp)) != 0)
1008 			goto out;
1009 		if ((tcnp->cn_flags & SAVESTART) == 0)
1010 			panic("msdosfs_rename: lost to startdir");
1011 		if ((error = vfs_relookup(tdvp, &tvp, tcnp)) != 0)
1012 			goto out;
1013 		dp = VTODE(tdvp);
1014 		xp = tvp ? VTODE(tvp) : NULL;
1015 	}
1016 
1017 	VN_KNOTE(tdvp, NOTE_WRITE);
1018 
1019 	if (xp != NULL) {
1020 		/*
1021 		 * Target must be empty if a directory and have no links
1022 		 * to it. Also, ensure source and target are compatible
1023 		 * (both directories, or both not directories).
1024 		 */
1025 		if (xp->de_Attributes & ATTR_DIRECTORY) {
1026 			if (!dosdirempty(xp)) {
1027 				error = ENOTEMPTY;
1028 				goto bad1;
1029 			}
1030 			if (!doingdirectory) {
1031 				error = ENOTDIR;
1032 				goto bad1;
1033 			}
1034 			cache_purge(tdvp);
1035 		} else if (doingdirectory) {
1036 			error = EISDIR;
1037 			goto bad1;
1038 		}
1039 		if ((error = removede(dp, xp)) != 0)
1040 			goto bad1;
1041 		VN_KNOTE(tvp, NOTE_DELETE);
1042 		vput(tvp);
1043 		xp = NULL;
1044 	}
1045 
1046 	/*
1047 	 * Convert the filename in tcnp into a dos filename. We copy this
1048 	 * into the denode and directory entry for the destination
1049 	 * file/directory.
1050 	 */
1051 	if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0)
1052 		goto bad1;
1053 
1054 	/*
1055 	 * Since from wasn't locked at various places above,
1056 	 * have to do a relookup here.
1057 	 */
1058 	fcnp->cn_flags &= ~MODMASK;
1059 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1060 	if ((fcnp->cn_flags & SAVESTART) == 0)
1061 		panic("msdosfs_rename: lost from startdir");
1062 	if (!newparent)
1063 		VOP_UNLOCK(tdvp);
1064 	(void) vfs_relookup(fdvp, &fvp, fcnp);
1065 	if (fvp == NULL) {
1066 		/*
1067 		 * From name has disappeared.
1068 		 */
1069 		if (doingdirectory)
1070 			panic("rename: lost dir entry");
1071 		vrele(ap->a_fvp);
1072 		if (newparent)
1073 			VOP_UNLOCK(tdvp);
1074 		vrele(tdvp);
1075 		return 0;
1076 	}
1077 	xp = VTODE(fvp);
1078 	zp = VTODE(fdvp);
1079 	from_diroffset = zp->de_fndoffset;
1080 
1081 	/*
1082 	 * Ensure that the directory entry still exists and has not
1083 	 * changed till now. If the source is a file the entry may
1084 	 * have been unlinked or renamed. In either case there is
1085 	 * no further work to be done. If the source is a directory
1086 	 * then it cannot have been rmdir'ed or renamed; this is
1087 	 * prohibited by the DE_RENAME flag.
1088 	 */
1089 	if (xp != ip) {
1090 		if (doingdirectory)
1091 			panic("rename: lost dir entry");
1092 		vrele(ap->a_fvp);
1093 		if (newparent)
1094 			VOP_UNLOCK(fdvp);
1095 		xp = NULL;
1096 	} else {
1097 		vrele(fvp);
1098 		xp = NULL;
1099 
1100 		/*
1101 		 * First write a new entry in the destination
1102 		 * directory and mark the entry in the source directory
1103 		 * as deleted.  Then move the denode to the correct hash
1104 		 * chain for its new location in the filesystem.  And, if
1105 		 * we moved a directory, then update its .. entry to point
1106 		 * to the new parent directory.
1107 		 */
1108 		bcopy(ip->de_Name, oldname, 11);
1109 		bcopy(toname, ip->de_Name, 11);	/* update denode */
1110 		dp->de_fndoffset = to_diroffset;
1111 		dp->de_fndcnt = to_count;
1112 		error = createde(ip, dp, NULL, tcnp);
1113 		if (error) {
1114 			bcopy(oldname, ip->de_Name, 11);
1115 			if (newparent)
1116 				VOP_UNLOCK(fdvp);
1117 			goto bad;
1118 		}
1119 		ip->de_refcnt++;
1120 		zp->de_fndoffset = from_diroffset;
1121 		if ((error = removede(zp, ip)) != 0) {
1122 			/* XXX should really panic here, fs is corrupt */
1123 			if (newparent)
1124 				VOP_UNLOCK(fdvp);
1125 			goto bad;
1126 		}
1127 
1128 		cache_purge(fvp);
1129 
1130 		if (!doingdirectory) {
1131 			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1132 				       &ip->de_dirclust, 0);
1133 			if (error) {
1134 				/* XXX should really panic here, fs is corrupt */
1135 				if (newparent)
1136 					VOP_UNLOCK(fdvp);
1137 				goto bad;
1138 			}
1139 			ip->de_diroffset = to_diroffset;
1140 			if (ip->de_dirclust != MSDOSFSROOT)
1141 				ip->de_diroffset &= pmp->pm_crbomask;
1142 		}
1143 		reinsert(ip);
1144 		if (newparent)
1145 			VOP_UNLOCK(fdvp);
1146 	}
1147 
1148 	/*
1149 	 * If we moved a directory to a new parent directory, then we must
1150 	 * fixup the ".." entry in the moved directory.
1151 	 */
1152 	if (doingdirectory && newparent) {
1153 		cn = ip->de_StartCluster;
1154 		if (cn == MSDOSFSROOT) {
1155 			/* this should never happen */
1156 			panic("msdosfs_rename: updating .. in root directory?");
1157 		} else
1158 			bn = cntobn(pmp, cn);
1159 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
1160 		if (error) {
1161 			/* XXX should really panic here, fs is corrupt */
1162 			brelse(bp);
1163 			goto bad;
1164 		}
1165 		dotdotp = (struct direntry *)bp->b_data;
1166 		putushort(dotdotp[0].deStartCluster, cn);
1167 		pcl = dp->de_StartCluster;
1168 		if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1169 			pcl = 0;
1170 		putushort(dotdotp[1].deStartCluster, pcl);
1171 		if (FAT32(pmp)) {
1172 			putushort(dotdotp[0].deHighClust, cn >> 16);
1173 			putushort(dotdotp[1].deHighClust, pcl >> 16);
1174 		}
1175 		if ((error = bwrite(bp)) != 0) {
1176 			/* XXX should really panic here, fs is corrupt */
1177 			goto bad;
1178 		}
1179 	}
1180 
1181 	VN_KNOTE(fvp, NOTE_RENAME);
1182 
1183 bad:
1184 	VOP_UNLOCK(fvp);
1185 	vrele(fdvp);
1186 bad1:
1187 	if (xp)
1188 		vput(tvp);
1189 	vput(tdvp);
1190 out:
1191 	ip->de_flag &= ~DE_RENAME;
1192 	vrele(fvp);
1193 	return (error);
1194 
1195 }
1196 
1197 struct {
1198 	struct direntry dot;
1199 	struct direntry dotdot;
1200 } dosdirtemplate = {
1201 	{	".       ", "   ",			/* the . entry */
1202 		ATTR_DIRECTORY,				/* file attribute */
1203 		CASE_LOWER_BASE | CASE_LOWER_EXT,	/* lower case */
1204 		0,					/* create time 100ths */
1205 		{ 0, 0 }, { 0, 0 },			/* create time & date */
1206 		{ 0, 0 },				/* access date */
1207 		{ 0, 0 },				/* high bits of start cluster */
1208 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1209 		{ 0, 0 },				/* startcluster */
1210 		{ 0, 0, 0, 0 }				/* filesize */
1211 	},
1212 	{	"..      ", "   ",			/* the .. entry */
1213 		ATTR_DIRECTORY,				/* file attribute */
1214 		CASE_LOWER_BASE | CASE_LOWER_EXT,	/* lower case */
1215 		0,					/* create time 100ths */
1216 		{ 0, 0 }, { 0, 0 },			/* create time & date */
1217 		{ 0, 0 },				/* access date */
1218 		{ 0, 0 },				/* high bits of start cluster */
1219 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1220 		{ 0, 0 },				/* startcluster */
1221 		{ 0, 0, 0, 0 }				/* filesize */
1222 	}
1223 };
1224 
1225 int
msdosfs_mkdir(void * v)1226 msdosfs_mkdir(void *v)
1227 {
1228 	struct vop_mkdir_args *ap = v;
1229 	struct componentname *cnp = ap->a_cnp;
1230 	struct denode ndirent;
1231 	struct denode *dep;
1232 	struct denode *pdep = VTODE(ap->a_dvp);
1233 	int error;
1234 	daddr_t bn;
1235 	uint32_t newcluster, pcl;
1236 	struct direntry *denp;
1237 	struct msdosfsmount *pmp = pdep->de_pmp;
1238 	struct buf *bp;
1239 	struct timespec ts;
1240 
1241 	/*
1242 	 * If this is the root directory and there is no space left we
1243 	 * can't do anything.  This is because the root directory can not
1244 	 * change size.
1245 	 */
1246 	if (pdep->de_StartCluster == MSDOSFSROOT
1247 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1248 		error = ENOSPC;
1249 		goto bad2;
1250 	}
1251 
1252 	/*
1253 	 * Allocate a cluster to hold the about to be created directory.
1254 	 */
1255 	error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
1256 	if (error)
1257 		goto bad2;
1258 
1259 	bzero(&ndirent, sizeof(ndirent));
1260 	ndirent.de_pmp = pmp;
1261 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1262 	getnanotime(&ts);
1263 	DETIMES(&ndirent, &ts, &ts, &ts);
1264 
1265 	/*
1266 	 * Now fill the cluster with the "." and ".." entries. And write
1267 	 * the cluster to disk.  This way it is there for the parent
1268 	 * directory to be pointing at if there were a crash.
1269 	 */
1270 	bn = cntobn(pmp, newcluster);
1271 	/* always succeeds */
1272 	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, INFSLP);
1273 	bzero(bp->b_data, pmp->pm_bpcluster);
1274 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1275 	denp = (struct direntry *)bp->b_data;
1276 	putushort(denp[0].deStartCluster, newcluster);
1277 	putushort(denp[0].deCDate, ndirent.de_CDate);
1278 	putushort(denp[0].deCTime, ndirent.de_CTime);
1279 	denp[0].deCTimeHundredth = ndirent.de_CTimeHundredth;
1280 	putushort(denp[0].deADate, ndirent.de_ADate);
1281 	putushort(denp[0].deMDate, ndirent.de_MDate);
1282 	putushort(denp[0].deMTime, ndirent.de_MTime);
1283 	pcl = pdep->de_StartCluster;
1284 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1285 		pcl = 0;
1286 	putushort(denp[1].deStartCluster, pcl);
1287 	putushort(denp[1].deCDate, ndirent.de_CDate);
1288 	putushort(denp[1].deCTime, ndirent.de_CTime);
1289 	denp[1].deCTimeHundredth = ndirent.de_CTimeHundredth;
1290 	putushort(denp[1].deADate, ndirent.de_ADate);
1291 	putushort(denp[1].deMDate, ndirent.de_MDate);
1292 	putushort(denp[1].deMTime, ndirent.de_MTime);
1293 	if (FAT32(pmp)) {
1294 		putushort(denp[0].deHighClust, newcluster >> 16);
1295 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1296 	}
1297 
1298 	if ((error = bwrite(bp)) != 0)
1299 		goto bad;
1300 
1301 	/*
1302 	 * Now build up a directory entry pointing to the newly allocated
1303 	 * cluster.  This will be written to an empty slot in the parent
1304 	 * directory.
1305 	 */
1306 #ifdef DIAGNOSTIC
1307 	if ((cnp->cn_flags & HASBUF) == 0)
1308 		panic("msdosfs_mkdir: no name");
1309 #endif
1310 	if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1311 		goto bad;
1312 
1313 	ndirent.de_Attributes = ATTR_DIRECTORY;
1314 	ndirent.de_StartCluster = newcluster;
1315 	ndirent.de_FileSize = 0;
1316 	ndirent.de_dev = pdep->de_dev;
1317 	ndirent.de_devvp = pdep->de_devvp;
1318 	if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
1319 		goto bad;
1320 	if ((cnp->cn_flags & SAVESTART) == 0)
1321 		pool_put(&namei_pool, cnp->cn_pnbuf);
1322 	VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1323 	vput(ap->a_dvp);
1324 	*ap->a_vpp = DETOV(dep);
1325 	return (0);
1326 
1327 bad:
1328 	clusterfree(pmp, newcluster, NULL);
1329 bad2:
1330 	pool_put(&namei_pool, cnp->cn_pnbuf);
1331 	vput(ap->a_dvp);
1332 	return (error);
1333 }
1334 
1335 int
msdosfs_rmdir(void * v)1336 msdosfs_rmdir(void *v)
1337 {
1338 	struct vop_rmdir_args *ap = v;
1339 	struct vnode *vp = ap->a_vp;
1340 	struct vnode *dvp = ap->a_dvp;
1341 	struct componentname *cnp = ap->a_cnp;
1342 	struct denode *ip, *dp;
1343 	int error;
1344 
1345 	ip = VTODE(vp);
1346 	dp = VTODE(dvp);
1347 	/*
1348 	 * Verify the directory is empty (and valid).
1349 	 * (Rmdir ".." won't be valid since
1350 	 *  ".." will contain a reference to
1351 	 *  the current directory and thus be
1352 	 *  non-empty.)
1353 	 */
1354 	error = 0;
1355 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1356 		error = ENOTEMPTY;
1357 		goto out;
1358 	}
1359 
1360 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1361 
1362 	/*
1363 	 * Delete the entry from the directory.  For dos filesystems this
1364 	 * gets rid of the directory entry on disk, the in memory copy
1365 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1366 	 * being found by deget().  When the vput() on dep is done we give
1367 	 * up access and eventually msdosfs_reclaim() will be called which
1368 	 * will remove it from the denode cache.
1369 	 */
1370 	if ((error = removede(dp, ip)) != 0)
1371 		goto out;
1372 	/*
1373 	 * This is where we decrement the link count in the parent
1374 	 * directory.  Since dos filesystems don't do this we just purge
1375 	 * the name cache and let go of the parent directory denode.
1376 	 */
1377 	cache_purge(dvp);
1378 	vput(dvp);
1379 	dvp = NULL;
1380 	/*
1381 	 * Truncate the directory that is being deleted.
1382 	 */
1383 	error = detrunc(ip, (uint32_t)0, IO_SYNC, cnp->cn_cred, cnp->cn_proc);
1384 	cache_purge(vp);
1385 out:
1386 	if (dvp)
1387 		vput(dvp);
1388 	VN_KNOTE(vp, NOTE_DELETE);
1389 	vput(vp);
1390 	return (error);
1391 }
1392 
1393 /*
1394  * DOS filesystems don't know what symlinks are.
1395  */
1396 int
msdosfs_symlink(void * v)1397 msdosfs_symlink(void *v)
1398 {
1399 	struct vop_symlink_args *ap = v;
1400 
1401 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1402 	vput(ap->a_dvp);
1403 	return (EOPNOTSUPP);
1404 }
1405 
1406 int
msdosfs_readdir(void * v)1407 msdosfs_readdir(void *v)
1408 {
1409 	struct vop_readdir_args *ap = v;
1410 	int error = 0;
1411 	int diff;
1412 	long n;
1413 	int blsize;
1414 	long on;
1415 	long lost;
1416 	long count;
1417 	uint32_t dirsperblk;
1418 	uint32_t cn, lbn;
1419 	uint32_t fileno;
1420 	long bias = 0;
1421 	daddr_t bn;
1422 	struct buf *bp;
1423 	struct denode *dep = VTODE(ap->a_vp);
1424 	struct msdosfsmount *pmp = dep->de_pmp;
1425 	struct direntry *dentp;
1426 	struct dirent dirbuf;
1427 	struct uio *uio = ap->a_uio;
1428 	off_t offset, wlast = -1;
1429 	int chksum = -1;
1430 
1431 #ifdef MSDOSFS_DEBUG
1432 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1433 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1434 #endif
1435 
1436 	/*
1437 	 * msdosfs_readdir() won't operate properly on regular files since
1438 	 * it does i/o only with the filesystem vnode, and hence can
1439 	 * retrieve the wrong block from the buffer cache for a plain file.
1440 	 * So, fail attempts to readdir() on a plain file.
1441 	 */
1442 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1443 		return (ENOTDIR);
1444 
1445 	/*
1446 	 * To be safe, initialize dirbuf
1447 	 */
1448 	bzero(&dirbuf, sizeof(dirbuf));
1449 
1450 	/*
1451 	 * If the user buffer is smaller than the size of one dos directory
1452 	 * entry or the file offset is not a multiple of the size of a
1453 	 * directory entry, then we fail the read.
1454 	 */
1455 	count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1456 	offset = uio->uio_offset;
1457 	if (count < sizeof(struct direntry) ||
1458 	    (offset & (sizeof(struct direntry) - 1)))
1459 		return (EINVAL);
1460 	lost = uio->uio_resid - count;
1461 	uio->uio_resid = count;
1462 
1463 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1464 
1465 	/*
1466 	 * If they are reading from the root directory then, we simulate
1467 	 * the . and .. entries since these don't exist in the root
1468 	 * directory.  We also set the offset bias to make up for having to
1469 	 * simulate these entries. By this I mean that at file offset 64 we
1470 	 * read the first entry in the root directory that lives on disk.
1471 	 */
1472 	if (dep->de_StartCluster == MSDOSFSROOT
1473 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1474 #if 0
1475 		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1476 		    offset);
1477 #endif
1478 		bias = 2 * sizeof(struct direntry);
1479 		if (offset < bias) {
1480 			for (n = (int)offset / sizeof(struct direntry);
1481 			     n < 2; n++) {
1482 			        if (FAT32(pmp))
1483 				        dirbuf.d_fileno = pmp->pm_rootdirblk;
1484 				else
1485 				        dirbuf.d_fileno = 1;
1486 				dirbuf.d_type = DT_DIR;
1487 				switch (n) {
1488 				case 0:
1489 					dirbuf.d_namlen = 1;
1490 					strlcpy(dirbuf.d_name, ".",
1491 					    sizeof dirbuf.d_name);
1492 					break;
1493 				case 1:
1494 					dirbuf.d_namlen = 2;
1495 					strlcpy(dirbuf.d_name, "..",
1496 					    sizeof dirbuf.d_name);
1497 					break;
1498 				}
1499 				dirbuf.d_reclen = DIRENT_SIZE(&dirbuf);
1500 				dirbuf.d_off = offset +
1501 				    sizeof(struct direntry);
1502 				if (uio->uio_resid < dirbuf.d_reclen)
1503 					goto out;
1504 				error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1505 				if (error)
1506 					goto out;
1507 				offset = dirbuf.d_off;
1508 			}
1509 		}
1510 	}
1511 
1512 	while (uio->uio_resid > 0) {
1513 		lbn = de_cluster(pmp, offset - bias);
1514 		on = (offset - bias) & pmp->pm_crbomask;
1515 		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1516 		diff = dep->de_FileSize - (offset - bias);
1517 		if (diff <= 0)
1518 			break;
1519 		n = min(n, diff);
1520 		if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1521 			break;
1522 		error = bread(pmp->pm_devvp, bn, blsize, &bp);
1523 		if (error) {
1524 			brelse(bp);
1525 			return (error);
1526 		}
1527 		n = min(n, blsize - bp->b_resid);
1528 
1529 		/*
1530 		 * Convert from dos directory entries to fs-independent
1531 		 * directory entries.
1532 		 */
1533 		for (dentp = (struct direntry *)(bp->b_data + on);
1534 		     (char *)dentp < bp->b_data + on + n;
1535 		     dentp++, offset += sizeof(struct direntry)) {
1536 #if 0
1537 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1538 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1539 #endif
1540 			/*
1541 			 * If this is an unused entry, we can stop.
1542 			 */
1543 			if (dentp->deName[0] == SLOT_EMPTY) {
1544 				brelse(bp);
1545 				goto out;
1546 			}
1547 			/*
1548 			 * Skip deleted entries.
1549 			 */
1550 			if (dentp->deName[0] == SLOT_DELETED) {
1551 				chksum = -1;
1552 				wlast = -1;
1553 				continue;
1554 			}
1555 
1556 			/*
1557 			 * Handle Win95 long directory entries
1558 			 */
1559 			if (dentp->deAttributes == ATTR_WIN95) {
1560 				struct winentry *wep;
1561 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1562 					continue;
1563 				wep = (struct winentry *)dentp;
1564 				chksum = win2unixfn(wep, &dirbuf, chksum);
1565 				if (wep->weCnt & WIN_LAST)
1566 					wlast = offset;
1567 				continue;
1568 			}
1569 
1570 			/*
1571 			 * Skip volume labels
1572 			 */
1573 			if (dentp->deAttributes & ATTR_VOLUME) {
1574 				chksum = -1;
1575 				wlast = -1;
1576 				continue;
1577 			}
1578 
1579 			/*
1580 			 * This computation of d_fileno must match
1581 			 * the computation of va_fileid in
1582 			 * msdosfs_getattr.
1583 			 */
1584 			fileno = getushort(dentp->deStartCluster);
1585 			if (FAT32(pmp))
1586 			    fileno |= getushort(dentp->deHighClust) << 16;
1587 
1588 			if (dentp->deAttributes & ATTR_DIRECTORY) {
1589 				/* Special-case root */
1590 				if (fileno == MSDOSFSROOT)  {
1591 					fileno = FAT32(pmp) ?
1592 					    pmp->pm_rootdirblk : 1;
1593 				}
1594 
1595 				dirbuf.d_fileno = fileno;
1596 				dirbuf.d_type = DT_DIR;
1597 			} else {
1598 				if (getulong(dentp->deFileSize) == 0) {
1599 					uint64_t fileno64;
1600 
1601 					fileno64 = (cn == MSDOSFSROOT) ?
1602 					    roottobn(pmp, 0) : cntobn(pmp, cn);
1603 
1604 					fileno64 *= dirsperblk;
1605 					fileno64 += dentp -
1606 					    (struct direntry *)bp->b_data;
1607 
1608 					fileno = fileidhash(fileno64);
1609 				}
1610 
1611 				dirbuf.d_fileno = fileno;
1612 				dirbuf.d_type = DT_REG;
1613 			}
1614 
1615 			if (chksum != winChksum(dentp->deName))
1616 				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1617 				    (u_char *)dirbuf.d_name,
1618 				    pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
1619 			else
1620 				dirbuf.d_name[dirbuf.d_namlen] = 0;
1621 			chksum = -1;
1622 			dirbuf.d_reclen = DIRENT_SIZE(&dirbuf);
1623 			dirbuf.d_off = offset + sizeof(struct direntry);
1624 			if (uio->uio_resid < dirbuf.d_reclen) {
1625 				brelse(bp);
1626 				/* Remember long-name offset. */
1627 				if (wlast != -1)
1628 					offset = wlast;
1629 				goto out;
1630 			}
1631 			wlast = -1;
1632 			error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1633 			if (error) {
1634 				brelse(bp);
1635 				goto out;
1636 			}
1637 		}
1638 		brelse(bp);
1639 	}
1640 
1641 out:
1642 	uio->uio_offset = offset;
1643 	uio->uio_resid += lost;
1644 	if (dep->de_FileSize - (offset - bias) <= 0)
1645 		*ap->a_eofflag = 1;
1646 	else
1647 		*ap->a_eofflag = 0;
1648 	return (error);
1649 }
1650 
1651 /*
1652  * DOS filesystems don't know what symlinks are.
1653  */
1654 int
msdosfs_readlink(void * v)1655 msdosfs_readlink(void *v)
1656 {
1657 #if 0
1658 	struct vop_readlink_args /* {
1659 		struct vnode *a_vp;
1660 		struct uio *a_uio;
1661 		struct ucred *a_cred;
1662 	} */ *ap;
1663 #endif
1664 
1665 	return (EINVAL);
1666 }
1667 
1668 int
msdosfs_lock(void * v)1669 msdosfs_lock(void *v)
1670 {
1671 	struct vop_lock_args *ap = v;
1672 	struct vnode *vp = ap->a_vp;
1673 
1674 	return rrw_enter(&VTODE(vp)->de_lock, ap->a_flags & LK_RWFLAGS);
1675 }
1676 
1677 int
msdosfs_unlock(void * v)1678 msdosfs_unlock(void *v)
1679 {
1680 	struct vop_unlock_args *ap = v;
1681 	struct vnode *vp = ap->a_vp;
1682 
1683 	rrw_exit(&VTODE(vp)->de_lock);
1684 	return 0;
1685 }
1686 
1687 int
msdosfs_islocked(void * v)1688 msdosfs_islocked(void *v)
1689 {
1690 	struct vop_islocked_args *ap = v;
1691 
1692 	return rrw_status(&VTODE(ap->a_vp)->de_lock);
1693 }
1694 
1695 /*
1696  * vp  - address of vnode file the file
1697  * bn  - which cluster we are interested in mapping to a filesystem block number
1698  * vpp - returns the vnode for the block special file holding the filesystem
1699  *	 containing the file of interest
1700  * bnp - address of where to return the filesystem relative block number
1701  */
1702 int
msdosfs_bmap(void * v)1703 msdosfs_bmap(void *v)
1704 {
1705 	struct vop_bmap_args *ap = v;
1706 	struct denode *dep = VTODE(ap->a_vp);
1707 	uint32_t cn;
1708 
1709 	if (ap->a_vpp != NULL)
1710 		*ap->a_vpp = dep->de_devvp;
1711 	if (ap->a_bnp == NULL)
1712 		return (0);
1713 
1714 	cn = ap->a_bn;
1715 	if (cn != ap->a_bn)
1716 		return (EFBIG);
1717 
1718 	return (msdosfs_bmaparray(ap->a_vp, cn, ap->a_bnp, ap->a_runp));
1719 }
1720 
1721 int
msdosfs_bmaparray(struct vnode * vp,uint32_t cn,daddr_t * bnp,int * runp)1722 msdosfs_bmaparray(struct vnode *vp, uint32_t cn, daddr_t *bnp, int *runp)
1723 {
1724 	struct denode *dep = VTODE(vp);
1725 	struct msdosfsmount *pmp = dep->de_pmp;
1726 	struct mount *mp;
1727 	int error, maxrun = 0, run;
1728 	daddr_t runbn;
1729 
1730 	mp = vp->v_mount;
1731 
1732 	if (runp) {
1733 		/*
1734 		 * XXX
1735 		 * If MAXBSIZE is the largest transfer the disks can handle,
1736 		 * we probably want maxrun to be 1 block less so that we
1737 		 * don't create a block larger than the device can handle.
1738 		 */
1739 		*runp = 0;
1740 		maxrun = min(MAXBSIZE / mp->mnt_stat.f_iosize - 1,
1741 		    pmp->pm_maxcluster - cn);
1742 	}
1743 
1744 	if ((error = pcbmap(dep, cn, bnp, 0, 0)) != 0)
1745 		return (error);
1746 
1747 	for (run = 1; run <= maxrun; run++) {
1748 		error = pcbmap(dep, cn + run, &runbn, 0, 0);
1749 		if (error != 0 || (runbn != *bnp + de_cn2bn(pmp, run)))
1750 			break;
1751 	}
1752 
1753 	if (runp)
1754 		*runp = run - 1;
1755 
1756 	return (0);
1757 }
1758 
1759 int
msdosfs_strategy(void * v)1760 msdosfs_strategy(void *v)
1761 {
1762 	struct vop_strategy_args *ap = v;
1763 	struct buf *bp = ap->a_bp;
1764 	struct denode *dep = VTODE(bp->b_vp);
1765 	struct vnode *vp;
1766 	int error = 0;
1767 	int s;
1768 
1769 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1770 		panic("msdosfs_strategy: spec");
1771 	/*
1772 	 * If we don't already know the filesystem relative block number
1773 	 * then get it using pcbmap().  If pcbmap() returns the block
1774 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1775 	 * don't allow files with holes, so we shouldn't ever see this.
1776 	 */
1777 	if (bp->b_blkno == bp->b_lblkno) {
1778 		error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1779 		if (error)
1780 			bp->b_blkno = -1;
1781 		if (bp->b_blkno == -1)
1782 			clrbuf(bp);
1783 	}
1784 	if (bp->b_blkno == -1) {
1785 		s = splbio();
1786 		biodone(bp);
1787 		splx(s);
1788 		return (error);
1789 	}
1790 
1791 	/*
1792 	 * Read/write the block from/to the disk that contains the desired
1793 	 * file block.
1794 	 */
1795 
1796 	vp = dep->de_devvp;
1797 	bp->b_dev = vp->v_rdev;
1798 	VOP_STRATEGY(vp, bp);
1799 	return (0);
1800 }
1801 
1802 int
msdosfs_print(void * v)1803 msdosfs_print(void *v)
1804 {
1805 #if defined(DEBUG) || defined(DIAGNOSTIC) || defined(VFSLCKDEBUG)
1806 	struct vop_print_args *ap = v;
1807 	struct denode *dep = VTODE(ap->a_vp);
1808 
1809 	printf(
1810 	    "tag VT_MSDOSFS, startcluster %u, dircluster %u, diroffset %u ",
1811 	    dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1812 	printf(" dev %d, %d, %s\n",
1813 	    major(dep->de_dev), minor(dep->de_dev),
1814 	    VOP_ISLOCKED(ap->a_vp) ? "(LOCKED)" : "");
1815 #ifdef DIAGNOSTIC
1816 	printf("\n");
1817 #endif
1818 #endif
1819 
1820 	return (0);
1821 }
1822 
1823 int
msdosfs_advlock(void * v)1824 msdosfs_advlock(void *v)
1825 {
1826 	struct vop_advlock_args *ap = v;
1827 	struct denode *dep = VTODE(ap->a_vp);
1828 
1829 	return (lf_advlock(&dep->de_lockf, dep->de_FileSize, ap->a_id, ap->a_op,
1830 	    ap->a_fl, ap->a_flags));
1831 }
1832 
1833 int
msdosfs_pathconf(void * v)1834 msdosfs_pathconf(void *v)
1835 {
1836 	struct vop_pathconf_args *ap = v;
1837 	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1838 	int error = 0;
1839 
1840 	switch (ap->a_name) {
1841 	case _PC_LINK_MAX:
1842 		*ap->a_retval = 1;
1843 		break;
1844 	case _PC_NAME_MAX:
1845 		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1846 		break;
1847 	case _PC_CHOWN_RESTRICTED:
1848 		*ap->a_retval = 1;
1849 		break;
1850 	case _PC_NO_TRUNC:
1851 		*ap->a_retval = 0;
1852 		break;
1853 	case _PC_TIMESTAMP_RESOLUTION:
1854 		*ap->a_retval = 2000000000;	/* 2 billion nanoseconds */
1855 		break;
1856 	default:
1857 		error = EINVAL;
1858 		break;
1859 	}
1860 
1861 	return (error);
1862 }
1863 
1864 /*
1865  * Thomas Wang's hash function, severely hacked to always set the high
1866  * bit on the number it returns (so no longer a proper hash function).
1867  */
1868 static uint32_t
fileidhash(uint64_t fileid)1869 fileidhash(uint64_t fileid)
1870 {
1871 	uint64_t c1 = 0x6e5ea73858134343LL;
1872 	uint64_t c2 = 0xb34e8f99a2ec9ef5LL;
1873 
1874 	/*
1875 	 * We now have the original fileid value, as 64-bit value.
1876 	 * We need to reduce it to 32-bits, with the top bit set.
1877 	 */
1878 	fileid ^= ((c1 ^ fileid) >> 32);
1879 	fileid *= c1;
1880 	fileid ^= ((c2 ^ fileid) >> 31);
1881 	fileid *= c2;
1882 	fileid ^= ((c1 ^ fileid) >> 32);
1883 
1884 	return (uint32_t)(fileid | 0x80000000);
1885 }
1886 
1887 /* Global vfs data structures for msdosfs */
1888 const struct vops msdosfs_vops = {
1889 	.vop_lookup	= msdosfs_lookup,
1890 	.vop_create	= msdosfs_create,
1891 	.vop_mknod	= msdosfs_mknod,
1892 	.vop_open	= msdosfs_open,
1893 	.vop_close	= msdosfs_close,
1894 	.vop_access	= msdosfs_access,
1895 	.vop_getattr	= msdosfs_getattr,
1896 	.vop_setattr	= msdosfs_setattr,
1897 	.vop_read	= msdosfs_read,
1898 	.vop_write	= msdosfs_write,
1899 	.vop_ioctl	= msdosfs_ioctl,
1900 	.vop_kqfilter	= msdosfs_kqfilter,
1901 	.vop_fsync	= msdosfs_fsync,
1902 	.vop_remove	= msdosfs_remove,
1903 	.vop_link	= msdosfs_link,
1904 	.vop_rename	= msdosfs_rename,
1905 	.vop_mkdir	= msdosfs_mkdir,
1906 	.vop_rmdir	= msdosfs_rmdir,
1907 	.vop_symlink	= msdosfs_symlink,
1908 	.vop_readdir	= msdosfs_readdir,
1909 	.vop_readlink	= msdosfs_readlink,
1910 	.vop_abortop	= vop_generic_abortop,
1911 	.vop_inactive	= msdosfs_inactive,
1912 	.vop_reclaim	= msdosfs_reclaim,
1913 	.vop_lock	= msdosfs_lock,
1914 	.vop_unlock	= msdosfs_unlock,
1915 	.vop_bmap	= msdosfs_bmap,
1916 	.vop_strategy	= msdosfs_strategy,
1917 	.vop_print	= msdosfs_print,
1918 	.vop_islocked	= msdosfs_islocked,
1919 	.vop_pathconf	= msdosfs_pathconf,
1920 	.vop_advlock	= msdosfs_advlock,
1921 	.vop_bwrite	= vop_generic_bwrite,
1922 	.vop_revoke	= vop_generic_revoke,
1923 };
1924 
1925 const struct filterops msdosfsread_filtops = {
1926 	.f_flags	= FILTEROP_ISFD,
1927 	.f_attach	= NULL,
1928 	.f_detach	= filt_msdosfsdetach,
1929 	.f_event	= filt_msdosfsread,
1930 };
1931 
1932 const struct filterops msdosfswrite_filtops = {
1933 	.f_flags	= FILTEROP_ISFD,
1934 	.f_attach	= NULL,
1935 	.f_detach	= filt_msdosfsdetach,
1936 	.f_event	= filt_msdosfswrite,
1937 };
1938 
1939 const struct filterops msdosfsvnode_filtops = {
1940 	.f_flags	= FILTEROP_ISFD,
1941 	.f_attach	= NULL,
1942 	.f_detach	= filt_msdosfsdetach,
1943 	.f_event	= filt_msdosfsvnode,
1944 };
1945 
1946 int
msdosfs_kqfilter(void * v)1947 msdosfs_kqfilter(void *v)
1948 {
1949 	struct vop_kqfilter_args *ap = v;
1950 	struct vnode *vp = ap->a_vp;
1951 	struct knote *kn = ap->a_kn;
1952 
1953 	switch (kn->kn_filter) {
1954 	case EVFILT_READ:
1955 		kn->kn_fop = &msdosfsread_filtops;
1956 		break;
1957 	case EVFILT_WRITE:
1958 		kn->kn_fop = &msdosfswrite_filtops;
1959 		break;
1960 	case EVFILT_VNODE:
1961 		kn->kn_fop = &msdosfsvnode_filtops;
1962 		break;
1963 	default:
1964 		return (EINVAL);
1965 	}
1966 
1967 	kn->kn_hook = (caddr_t)vp;
1968 
1969 	klist_insert_locked(&vp->v_klist, kn);
1970 
1971 	return (0);
1972 }
1973 
1974 void
filt_msdosfsdetach(struct knote * kn)1975 filt_msdosfsdetach(struct knote *kn)
1976 {
1977 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1978 
1979 	klist_remove_locked(&vp->v_klist, kn);
1980 }
1981 
1982 int
filt_msdosfsread(struct knote * kn,long hint)1983 filt_msdosfsread(struct knote *kn, long hint)
1984 {
1985 	struct vnode *vp = (struct vnode *)kn->kn_hook;
1986 	struct denode *dep = VTODE(vp);
1987 
1988 	/*
1989 	 * filesystem is gone, so set the EOF flag and schedule
1990 	 * the knote for deletion.
1991 	 */
1992 	if (hint == NOTE_REVOKE) {
1993 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1994 		return (1);
1995 	}
1996 
1997 	kn->kn_data = dep->de_FileSize - foffset(kn->kn_fp);
1998 	if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
1999 		kn->kn_fflags |= NOTE_EOF;
2000 		return (1);
2001 	}
2002 
2003 	if (kn->kn_flags & (__EV_POLL | __EV_SELECT))
2004 		return (1);
2005 
2006 	return (kn->kn_data != 0);
2007 }
2008 
2009 int
filt_msdosfswrite(struct knote * kn,long hint)2010 filt_msdosfswrite(struct knote *kn, long hint)
2011 {
2012 	/*
2013 	 * filesystem is gone, so set the EOF flag and schedule
2014 	 * the knote for deletion.
2015 	 */
2016 	if (hint == NOTE_REVOKE) {
2017 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2018 		return (1);
2019 	}
2020 
2021 	kn->kn_data = 0;
2022 	return (1);
2023 }
2024 
2025 int
filt_msdosfsvnode(struct knote * kn,long hint)2026 filt_msdosfsvnode(struct knote *kn, long hint)
2027 {
2028 	if (kn->kn_sfflags & hint)
2029 		kn->kn_fflags |= hint;
2030 	if (hint == NOTE_REVOKE) {
2031 		kn->kn_flags |= EV_EOF;
2032 		return (1);
2033 	}
2034 	return (kn->kn_fflags != 0);
2035 }
2036