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