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