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