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