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