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