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