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