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