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