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