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