xref: /dragonfly/sys/vfs/msdosfs/msdosfs_vnops.c (revision b4f25088)
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vnops.c,v 1.95.2.4 2003/06/13 15:05:47 trhodes Exp $ */
2 /*	$NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
54 #include <sys/kernel.h>
55 #include <sys/stat.h>
56 #include <sys/buf.h>
57 #include <sys/proc.h>
58 #include <sys/priv.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 <machine/inttypes.h>
75 
76 #include "bpb.h"
77 #include "direntry.h"
78 #include "denode.h"
79 #include "msdosfsmount.h"
80 #include "fat.h"
81 
82 #define	DOS_FILESIZE_MAX	0xffffffff
83 
84 /*
85  * Prototypes for MSDOSFS vnode operations
86  */
87 static int msdosfs_create (struct vop_old_create_args *);
88 static int msdosfs_mknod (struct vop_old_mknod_args *);
89 static int msdosfs_open (struct vop_open_args *);
90 static int msdosfs_close (struct vop_close_args *);
91 static int msdosfs_access (struct vop_access_args *);
92 static int msdosfs_getattr (struct vop_getattr_args *);
93 static int msdosfs_setattr (struct vop_setattr_args *);
94 static int msdosfs_read (struct vop_read_args *);
95 static int msdosfs_write (struct vop_write_args *);
96 static int msdosfs_fsync (struct vop_fsync_args *);
97 static int msdosfs_remove (struct vop_old_remove_args *);
98 static int msdosfs_link (struct vop_old_link_args *);
99 static int msdosfs_rename (struct vop_old_rename_args *);
100 static int msdosfs_mkdir (struct vop_old_mkdir_args *);
101 static int msdosfs_rmdir (struct vop_old_rmdir_args *);
102 static int msdosfs_symlink (struct vop_old_symlink_args *);
103 static int msdosfs_readdir (struct vop_readdir_args *);
104 static int msdosfs_bmap (struct vop_bmap_args *);
105 static int msdosfs_strategy (struct vop_strategy_args *);
106 static int msdosfs_print (struct vop_print_args *);
107 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
108 
109 /*
110  * Some general notes:
111  *
112  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
113  * read/written using the vnode for the filesystem. Blocks that represent
114  * the contents of a file are read/written using the vnode for the file
115  * (including directories when they are read/written as files). This
116  * presents problems for the dos filesystem because data that should be in
117  * an inode (if dos had them) resides in the directory itself.  Since we
118  * must update directory entries without the benefit of having the vnode
119  * for the directory we must use the vnode for the filesystem.  This means
120  * that when a directory is actually read/written (via read, write, or
121  * readdir, or seek) we must use the vnode for the filesystem instead of
122  * the vnode for the directory as would happen in ufs. This is to insure we
123  * retreive the correct block from the buffer cache since the hash value is
124  * based upon the vnode address and the desired block number.
125  */
126 
127 /*
128  * Create a regular file. On entry the directory to contain the file being
129  * created is locked.  We must release before we return.
130  *
131  * msdosfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
132  *		  struct componentname *a_cnp, struct vattr *a_vap)
133  */
134 static int
135 msdosfs_create(struct vop_old_create_args *ap)
136 {
137 	struct componentname *cnp = ap->a_cnp;
138 	struct denode ndirent;
139 	struct denode *dep;
140 	struct denode *pdep = VTODE(ap->a_dvp);
141 	struct timespec ts;
142 	int error;
143 
144 #ifdef MSDOSFS_DEBUG
145 	kprintf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
146 #endif
147 
148 	/*
149 	 * If this is the root directory and there is no space left we
150 	 * can't do anything.  This is because the root directory can not
151 	 * change size.
152 	 */
153 	if (pdep->de_StartCluster == MSDOSFSROOT
154 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
155 		error = ENOSPC;
156 		goto bad;
157 	}
158 
159 	/*
160 	 * Create a directory entry for the file, then call createde() to
161 	 * have it installed. NOTE: DOS files are always executable.  We
162 	 * use the absence of the owner write bit to make the file
163 	 * readonly.
164 	 */
165 	bzero(&ndirent, sizeof(ndirent));
166 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
167 	if (error)
168 		goto bad;
169 
170 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
171 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
172 	ndirent.de_LowerCase = 0;
173 	ndirent.de_StartCluster = 0;
174 	ndirent.de_FileSize = 0;
175 	ndirent.de_dev = pdep->de_dev;
176 	ndirent.de_devvp = pdep->de_devvp;
177 	ndirent.de_pmp = pdep->de_pmp;
178 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
179 	getnanotime(&ts);
180 	DETIMES(&ndirent, &ts, &ts, &ts);
181 	error = createde(&ndirent, pdep, &dep, cnp);
182 	if (error)
183 		goto bad;
184 	*ap->a_vpp = DETOV(dep);
185 	return (0);
186 
187 bad:
188 	return (error);
189 }
190 
191 /*
192  * msdosfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
193  *		 struct componentname *a_cnp, struct vattr *a_vap)
194  */
195 static int
196 msdosfs_mknod(struct vop_old_mknod_args *ap)
197 {
198 	switch (ap->a_vap->va_type) {
199 	case VDIR:
200 		return (msdosfs_mkdir((struct vop_old_mkdir_args *)ap));
201 		break;
202 
203 	case VREG:
204 		return (msdosfs_create((struct vop_old_create_args *)ap));
205 		break;
206 
207 	default:
208 		return (EINVAL);
209 	}
210 	/* NOTREACHED */
211 }
212 
213 /*
214  * msdosfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
215  *		struct file *a_fp)
216  */
217 static int
218 msdosfs_open(struct vop_open_args *ap)
219 {
220 	return(vop_stdopen(ap));
221 }
222 
223 /*
224  * msdosfs_close(struct vnode *a_vp, int a_fflag)
225  */
226 static int
227 msdosfs_close(struct vop_close_args *ap)
228 {
229 	struct vnode *vp = ap->a_vp;
230 	struct denode *dep = VTODE(vp);
231 	struct timespec ts;
232 
233 	if (vp->v_sysref.refcnt > 1) {
234 		getnanotime(&ts);
235 		DETIMES(dep, &ts, &ts, &ts);
236 	}
237 	return (vop_stdclose(ap));
238 }
239 
240 /*
241  * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
242  */
243 static int
244 msdosfs_access(struct vop_access_args *ap)
245 {
246 	struct denode *dep = VTODE(ap->a_vp);
247 	struct msdosfsmount *pmp = dep->de_pmp;
248 	mode_t file_mode;
249 
250 	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
251 	    ((dep->de_Attributes & ATTR_READONLY) ?
252 	     	0 : (S_IWUSR|S_IWGRP|S_IWOTH));
253 	file_mode &= pmp->pm_mask;
254 
255 	return (vop_helper_access(ap, pmp->pm_uid, pmp->pm_gid, file_mode, 0));
256 }
257 
258 /*
259  * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
260  */
261 static int
262 msdosfs_getattr(struct vop_getattr_args *ap)
263 {
264 	struct denode *dep = VTODE(ap->a_vp);
265 	struct msdosfsmount *pmp = dep->de_pmp;
266 	struct vattr *vap = ap->a_vap;
267 	mode_t mode;
268 	struct timespec ts;
269 	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
270 	u_long fileid;
271 
272 	getnanotime(&ts);
273 	DETIMES(dep, &ts, &ts, &ts);
274 	vap->va_fsid = dev2udev(dep->de_dev);
275 	/*
276 	 * The following computation of the fileid must be the same as that
277 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
278 	 * doesn't work.
279 	 */
280 	if (dep->de_Attributes & ATTR_DIRECTORY) {
281 		fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
282 		if (dep->de_StartCluster == MSDOSFSROOT)
283 			fileid = 1;
284 	} else {
285 		fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
286 		if (dep->de_dirclust == MSDOSFSROOT)
287 			fileid = roottobn(pmp, 0) * dirsperblk;
288 		fileid += dep->de_diroffset / sizeof(struct direntry);
289 	}
290 	vap->va_fileid = fileid;
291 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
292 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
293 	else
294 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
295 	vap->va_mode = mode & pmp->pm_mask;
296 	vap->va_uid = pmp->pm_uid;
297 	vap->va_gid = pmp->pm_gid;
298 	vap->va_nlink = 1;
299 	vap->va_rmajor = VNOVAL;
300 	vap->va_rminor = VNOVAL;
301 	vap->va_size = dep->de_FileSize;
302 	dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
303 	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
304 		dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
305 		dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
306 	} else {
307 		vap->va_atime = vap->va_mtime;
308 		vap->va_ctime = vap->va_mtime;
309 	}
310 	vap->va_flags = 0;
311 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
312 		vap->va_flags |= SF_ARCHIVED;
313 	vap->va_gen = 0;
314 	vap->va_blocksize = pmp->pm_bpcluster;
315 	vap->va_bytes =
316 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
317 	vap->va_type = ap->a_vp->v_type;
318 	vap->va_filerev = dep->de_modrev;
319 	return (0);
320 }
321 
322 /*
323  * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
324  *		   struct ucred *a_cred)
325  */
326 static int
327 msdosfs_setattr(struct vop_setattr_args *ap)
328 {
329 	struct vnode *vp = ap->a_vp;
330 	struct denode *dep = VTODE(ap->a_vp);
331 	struct msdosfsmount *pmp = dep->de_pmp;
332 	struct vattr *vap = ap->a_vap;
333 	struct ucred *cred = ap->a_cred;
334 	int error = 0;
335 
336 #ifdef MSDOSFS_DEBUG
337 	kprintf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
338 	    ap->a_vp, vap, cred);
339 #endif
340 
341 	/*
342 	 * Check for unsettable attributes.
343 	 */
344 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
345 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
346 	    (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
347 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
348 #ifdef MSDOSFS_DEBUG
349 		kprintf("msdosfs_setattr(): returning EINVAL\n");
350 		kprintf("    va_type %u, va_nlink %"PRIx64", va_fsid %x, va_fileid %"PRIx64"\n",
351 		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
352 		kprintf("    va_blocksize %lx, va_rmajor %x, va_bytes %"PRIx64", va_gen %"PRIx64"\n",
353 		    vap->va_blocksize, vap->va_rmajor, vap->va_bytes, vap->va_gen);
354 		kprintf("    va_uid %x, va_gid %x\n",
355 		    vap->va_uid, vap->va_gid);
356 #endif
357 		return (EINVAL);
358 	}
359 	if (vap->va_flags != VNOVAL) {
360 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
361 			return (EROFS);
362 		if (cred->cr_uid != pmp->pm_uid &&
363 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
364 			return (error);
365 		/*
366 		 * We are very inconsistent about handling unsupported
367 		 * attributes.  We ignored the access time and the
368 		 * read and execute bits.  We were strict for the other
369 		 * attributes.
370 		 *
371 		 * Here we are strict, stricter than ufs in not allowing
372 		 * users to attempt to set SF_SETTABLE bits or anyone to
373 		 * set unsupported bits.  However, we ignore attempts to
374 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
375 		 * sensible file system attempts it a lot.
376 		 */
377 		if (cred->cr_uid != 0) {
378 			if (vap->va_flags & SF_SETTABLE)
379 				return EPERM;
380 		}
381 		if (vap->va_flags & ~SF_ARCHIVED)
382 			return EOPNOTSUPP;
383 		if (vap->va_flags & SF_ARCHIVED)
384 			dep->de_Attributes &= ~ATTR_ARCHIVE;
385 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
386 			dep->de_Attributes |= ATTR_ARCHIVE;
387 		dep->de_flag |= DE_MODIFIED;
388 	}
389 
390 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
391 		uid_t uid;
392 		gid_t gid;
393 
394 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
395 			return (EROFS);
396 		uid = vap->va_uid;
397 		if (uid == (uid_t)VNOVAL)
398 			uid = pmp->pm_uid;
399 		gid = vap->va_gid;
400 		if (gid == (gid_t)VNOVAL)
401 			gid = pmp->pm_gid;
402 		if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
403 		    (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
404 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
405 			return error;
406 		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
407 			return EINVAL;
408 	}
409 
410 	if (vap->va_size != VNOVAL) {
411 		/*
412 		 * Disallow write attempts on read-only file systems;
413 		 * unless the file is a socket, fifo, or a block or
414 		 * character device resident on the file system.
415 		 */
416 		switch (vp->v_type) {
417 		case VDIR:
418 			return (EISDIR);
419 			/* NOT REACHED */
420 		case VLNK:
421 		case VREG:
422 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
423 				return (EROFS);
424 			break;
425 		default:
426 			break;
427 		}
428 		error = detrunc(dep, vap->va_size, 0);
429 		if (error)
430 			return error;
431 	}
432 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
433 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
434 			return (EROFS);
435 		if (cred->cr_uid != pmp->pm_uid &&
436 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
437 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
438 		    (error = VOP_EACCESS(ap->a_vp, VWRITE, cred))))
439 			return (error);
440 		if (vp->v_type != VDIR) {
441 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
442 			    vap->va_atime.tv_sec != VNOVAL) {
443 				dep->de_flag &= ~DE_ACCESS;
444 				unix2dostime(&vap->va_atime, &dep->de_ADate,
445 				    NULL, NULL);
446 			}
447 			if (vap->va_mtime.tv_sec != VNOVAL) {
448 				dep->de_flag &= ~DE_UPDATE;
449 				unix2dostime(&vap->va_mtime, &dep->de_MDate,
450 				    &dep->de_MTime, NULL);
451 			}
452 			dep->de_Attributes |= ATTR_ARCHIVE;
453 			dep->de_flag |= DE_MODIFIED;
454 		}
455 	}
456 	/*
457 	 * DOS files only have the ability to have their writability
458 	 * attribute set, so we use the owner write bit to set the readonly
459 	 * attribute.
460 	 */
461 	if (vap->va_mode != (mode_t)VNOVAL) {
462 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
463 			return (EROFS);
464 		if (cred->cr_uid != pmp->pm_uid &&
465 		    (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
466 			return (error);
467 		if (vp->v_type != VDIR) {
468 			/* We ignore the read and execute bits. */
469 			if (vap->va_mode & VWRITE)
470 				dep->de_Attributes &= ~ATTR_READONLY;
471 			else
472 				dep->de_Attributes |= ATTR_READONLY;
473 			dep->de_Attributes |= ATTR_ARCHIVE;
474 			dep->de_flag |= DE_MODIFIED;
475 		}
476 	}
477 	return (deupdat(dep, 1));
478 }
479 
480 /*
481  * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
482  *		struct ucred *a_cred)
483  */
484 static int
485 msdosfs_read(struct vop_read_args *ap)
486 {
487 	int error = 0;
488 	int blsize;
489 	int isadir;
490 	size_t orig_resid;
491 	u_int n;
492 	u_long diff;
493 	u_long on;
494 	daddr_t lbn;
495 	daddr_t rablock;
496 	off_t raoffset;
497 	off_t loffset;
498 	int rasize;
499 	int seqcount;
500 	struct buf *bp;
501 	struct vnode *vp = ap->a_vp;
502 	struct denode *dep = VTODE(vp);
503 	struct msdosfsmount *pmp = dep->de_pmp;
504 	struct uio *uio = ap->a_uio;
505 
506 	if (uio->uio_offset < 0)
507 		return (EINVAL);
508 
509 	if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
510                 return (0);
511 	/*
512 	 * If they didn't ask for any data, then we are done.
513 	 */
514 	orig_resid = uio->uio_resid;
515 	if (orig_resid == 0)
516 		return (0);
517 
518 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
519 
520 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
521 	do {
522 		if (uio->uio_offset >= dep->de_FileSize)
523 			break;
524 
525 		/*
526 		 * note: lbn is a cluster number, not a device block number.
527 		 */
528 		lbn = de_off2cn(pmp, uio->uio_offset);
529 		loffset = de_cn2doff(pmp, lbn);
530 
531 		/*
532 		 * If we are operating on a directory file then be sure to
533 		 * do i/o with the vnode for the filesystem instead of the
534 		 * vnode for the directory.
535 		 */
536 		if (isadir) {
537 			/*
538 			 * convert cluster # to block #.  lbn is a
539 			 * device block number after this.
540 			 */
541 			error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
542 			loffset = de_bntodoff(pmp, lbn);
543 			if (error == E2BIG) {
544 				error = EINVAL;
545 				break;
546 			} else if (error)
547 				break;
548 			error = bread(pmp->pm_devvp, loffset, blsize, &bp);
549 		} else {
550 			blsize = pmp->pm_bpcluster;
551 			rablock = lbn + 1;
552 			raoffset = de_cn2doff(pmp, rablock);
553 			if (seqcount > 1 &&
554 			    raoffset < dep->de_FileSize) {
555 				rasize = pmp->pm_bpcluster;
556 				error = breadn(vp, loffset, blsize,
557 						&raoffset, &rasize, 1, &bp);
558 			} else {
559 				error = bread(vp, loffset, blsize, &bp);
560 			}
561 		}
562 		if (error) {
563 			brelse(bp);
564 			break;
565 		}
566 		on = uio->uio_offset & pmp->pm_crbomask;
567 		diff = pmp->pm_bpcluster - on;
568 		n = szmin(uio->uio_resid, diff);
569 		diff = dep->de_FileSize - uio->uio_offset;
570 		if (diff < n)
571 			n = diff;
572 		diff = blsize - bp->b_resid;
573 		if (diff < n)
574 			n = diff;
575 		error = uiomovebp(bp, bp->b_data + on, (size_t)n, uio);
576 		brelse(bp);
577 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
578 	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
579 	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
580 		dep->de_flag |= DE_ACCESS;
581 	return (error);
582 }
583 
584 /*
585  * Write data to a file or directory.
586  *
587  * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
588  *		 struct ucred *a_cred)
589  */
590 static int
591 msdosfs_write(struct vop_write_args *ap)
592 {
593 	int n;
594 	int croffset;
595 	size_t resid;
596 	u_long osize;
597 	int error = 0;
598 	u_long count;
599 	daddr_t cn, lastcn;
600 	struct buf *bp;
601 	int ioflag = ap->a_ioflag;
602 	struct uio *uio = ap->a_uio;
603 	struct thread *td = uio->uio_td;
604 	struct vnode *vp = ap->a_vp;
605 	struct vnode *thisvp;
606 	struct denode *dep = VTODE(vp);
607 	struct msdosfsmount *pmp = dep->de_pmp;
608 	struct proc *p = (td ? td->td_proc : NULL);
609 	struct lwp *lp = (td ? td->td_lwp : NULL);
610 
611 #ifdef MSDOSFS_DEBUG
612 	kprintf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
613 	    vp, uio, ioflag, ap->a_cred);
614 	kprintf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
615 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
616 #endif
617 
618 	switch (vp->v_type) {
619 	case VREG:
620 		if (ioflag & IO_APPEND)
621 			uio->uio_offset = dep->de_FileSize;
622 		thisvp = vp;
623 		break;
624 	case VDIR:
625 		return EISDIR;
626 	default:
627 		panic("msdosfs_write(): bad file type");
628 	}
629 
630 	if (uio->uio_offset < 0)
631 		return (EFBIG);
632 
633 	if (uio->uio_resid == 0)
634 		return (0);
635 
636 	/*
637 	 * If they've exceeded their filesize limit, tell them about it.
638 	 */
639 	if (p &&
640 	    ((uoff_t)uio->uio_offset + uio->uio_resid >
641 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
642 		lwpsignal(p, lp, SIGXFSZ);
643 		return (EFBIG);
644 	}
645 
646 	if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
647                 return (EFBIG);
648 	if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
649                 return (EFBIG);
650 
651 	/*
652 	 * If the offset we are starting the write at is beyond the end of
653 	 * the file, then they've done a seek.  Unix filesystems allow
654 	 * files with holes in them, DOS doesn't so we must fill the hole
655 	 * with zeroed blocks.
656 	 */
657 	if (uio->uio_offset > dep->de_FileSize) {
658 		error = deextend(dep, uio->uio_offset);
659 		if (error)
660 			return (error);
661 	}
662 
663 	/*
664 	 * Remember some values in case the write fails.
665 	 */
666 	resid = uio->uio_resid;
667 	osize = dep->de_FileSize;
668 
669 	/*
670 	 * If we write beyond the end of the file, extend it to its ultimate
671 	 * size ahead of the time to hopefully get a contiguous area.
672 	 */
673 	if (uio->uio_offset + resid > osize) {
674 		count = de_clcount(pmp, uio->uio_offset + resid) -
675 			de_clcount(pmp, osize);
676 		error = extendfile(dep, count, NULL, NULL, 0);
677 		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
678 			goto errexit;
679 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
680 	} else
681 		lastcn = de_clcount(pmp, osize) - 1;
682 
683 	do {
684 		if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
685 			error = ENOSPC;
686 			break;
687 		}
688 
689 		croffset = uio->uio_offset & pmp->pm_crbomask;
690 		n = (int)szmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
691 		if (uio->uio_offset + n > dep->de_FileSize) {
692 			dep->de_FileSize = uio->uio_offset + n;
693 			/* The object size needs to be set before buffer is allocated */
694 			vnode_pager_setsize(vp, dep->de_FileSize);
695 		}
696 
697 		/*
698 		 * If either the whole cluster gets written, or we write
699 		 * the cluster from its start beyond EOF, then no need to
700 		 * read data from disk.
701 		 *
702 		 * If UIO_NOCOPY is set we have to do a read-before-write
703 		 * to fill in any missing pieces of the buffer since no
704 		 * actual overwrite will occur.
705 		 */
706 		cn = de_off2cn(pmp, uio->uio_offset);
707 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
708 		    && uio->uio_segflg != UIO_NOCOPY
709 		    && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid)
710 		        > de_off2cn(pmp, uio->uio_offset)
711 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
712 			bp = getblk(thisvp, de_cn2doff(pmp, cn),
713 				    pmp->pm_bpcluster, 0, 0);
714 			clrbuf(bp);
715 			/*
716 			 * Do the bmap now, since pcbmap needs buffers
717 			 * for the fat table. (see msdosfs_strategy)
718 			 */
719 			if (bp->b_bio2.bio_offset == NOOFFSET) {
720 				daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
721 				daddr_t dblkno;
722 
723 				error = pcbmap(dep, lblkno,
724 					       &dblkno, NULL, NULL);
725 				if (error || dblkno == (daddr_t)-1) {
726 					bp->b_bio2.bio_offset = NOOFFSET;
727 				} else {
728 					bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
729 				}
730 			}
731 			if (bp->b_bio2.bio_offset == NOOFFSET) {
732 				brelse(bp);
733 				if (!error)
734 					error = EIO;		/* XXX */
735 				break;
736 			}
737 		} else {
738 			/*
739 			 * The block we need to write into exists, so read
740 			 * it in.
741 			 */
742 			error = bread(thisvp, de_cn2doff(pmp, cn),
743 				      pmp->pm_bpcluster, &bp);
744 			if (error) {
745 				brelse(bp);
746 				break;
747 			}
748 		}
749 
750 		/*
751 		 * Should these vnode_pager_* functions be done on dir
752 		 * files?
753 		 */
754 
755 		/*
756 		 * Copy the data from user space into the buf header.
757 		 */
758 		error = uiomovebp(bp, bp->b_data + croffset, (size_t)n, uio);
759 		if (error) {
760 			brelse(bp);
761 			break;
762 		}
763 
764 		/*
765 		 * If they want this synchronous then write it and wait for
766 		 * it.  Otherwise, if on a cluster boundary write it
767 		 * asynchronously so we can move on to the next block
768 		 * without delay.  Otherwise do a delayed write because we
769 		 * may want to write somemore into the block later.
770 		 */
771 		if (ioflag & IO_SYNC)
772 			bwrite(bp);
773 		else if (n + croffset == pmp->pm_bpcluster)
774 			bawrite(bp);
775 		else
776 			bdwrite(bp);
777 		dep->de_flag |= DE_UPDATE;
778 	} while (error == 0 && uio->uio_resid > 0);
779 
780 	/*
781 	 * If the write failed and they want us to, truncate the file back
782 	 * to the size it was before the write was attempted.
783 	 */
784 errexit:
785 	if (error) {
786 		if (ioflag & IO_UNIT) {
787 			detrunc(dep, osize, ioflag & IO_SYNC);
788 			uio->uio_offset -= resid - uio->uio_resid;
789 			uio->uio_resid = resid;
790 		} else {
791 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC);
792 			if (uio->uio_resid != resid)
793 				error = 0;
794 		}
795 	} else if (ioflag & IO_SYNC)
796 		error = deupdat(dep, 1);
797 	return (error);
798 }
799 
800 /*
801  * Flush the blocks of a file to disk.
802  *
803  * This function is worthless for vnodes that represent directories. Maybe we
804  * could just do a sync if they try an fsync on a directory file.
805  *
806  * msdosfs_fsync(struct vnode *a_vp, int a_waitfor)
807  */
808 static int
809 msdosfs_fsync(struct vop_fsync_args *ap)
810 {
811 	struct vnode *vp = ap->a_vp;
812 
813 	/*
814 	 * Flush all dirty buffers associated with a vnode.
815 	 */
816 #ifdef DIAGNOSTIC
817 loop:
818 #endif
819 	vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
820 #ifdef DIAGNOSTIC
821 	if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
822 		vprint("msdosfs_fsync: dirty", vp);
823 		goto loop;
824 	}
825 #endif
826 	return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
827 }
828 
829 /*
830  * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
831  *		  struct componentname *a_cnp)
832  */
833 static int
834 msdosfs_remove(struct vop_old_remove_args *ap)
835 {
836 	struct denode *dep = VTODE(ap->a_vp);
837 	struct denode *ddep = VTODE(ap->a_dvp);
838 	int error;
839 
840 	if (ap->a_vp->v_type == VDIR)
841 		error = EPERM;
842 	else
843 		error = removede(ddep, dep);
844 #ifdef MSDOSFS_DEBUG
845 	kprintf("msdosfs_remove(), dep %p, v_sysrefs %d\n",
846 		dep, ap->a_vp->v_sysref.refcnt);
847 #endif
848 	return (error);
849 }
850 
851 /*
852  * DOS filesystems don't know what links are. But since we already called
853  * msdosfs_lookup() with create and lockparent, the parent is locked so we
854  * have to free it before we return the error.
855  *
856  * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
857  *		struct componentname *a_cnp)
858  */
859 static int
860 msdosfs_link(struct vop_old_link_args *ap)
861 {
862 	return (EOPNOTSUPP);
863 }
864 
865 /*
866  * Renames on files require moving the denode to a new hash queue since the
867  * denode's location is used to compute which hash queue to put the file
868  * in. Unless it is a rename in place.  For example "mv a b".
869  *
870  * What follows is the basic algorithm:
871  *
872  * if (file move) {
873  *	if (dest file exists) {
874  *		remove dest file
875  *	}
876  *	if (dest and src in same directory) {
877  *		rewrite name in existing directory slot
878  *	} else {
879  *		write new entry in dest directory
880  *		update offset and dirclust in denode
881  *		move denode to new hash chain
882  *		clear old directory entry
883  *	}
884  * } else {
885  *	directory move
886  *	if (dest directory exists) {
887  *		if (dest is not empty) {
888  *			return ENOTEMPTY
889  *		}
890  *		remove dest directory
891  *	}
892  *	if (dest and src in same directory) {
893  *		rewrite name in existing entry
894  *	} else {
895  *		be sure dest is not a child of src directory
896  *		write entry in dest directory
897  *		update "." and ".." in moved directory
898  *		clear old directory entry for moved directory
899  *	}
900  * }
901  *
902  * On entry:
903  *	source's parent directory is unlocked
904  *	source file or directory is unlocked
905  *	destination's parent directory is locked
906  *	destination file or directory is locked if it exists
907  *
908  * On exit:
909  *	all denodes should be released
910  *
911  * Notes:
912  * I'm not sure how the memory containing the pathnames pointed at by the
913  * componentname structures is freed, there may be some memory bleeding
914  * for each rename done.
915  *
916  * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
917  *		  struct componentname *a_fcnp, struct vnode *a_tdvp,
918  *		  struct vnode *a_tvp, struct componentname *a_tcnp)
919  */
920 static int
921 msdosfs_rename(struct vop_old_rename_args *ap)
922 {
923 	struct vnode *tdvp = ap->a_tdvp;
924 	struct vnode *fvp = ap->a_fvp;
925 	struct vnode *fdvp = ap->a_fdvp;
926 	struct vnode *tvp = ap->a_tvp;
927 	struct componentname *tcnp = ap->a_tcnp;
928 	struct componentname *fcnp = ap->a_fcnp;
929 	struct denode *ip, *xp, *dp, *zp;
930 	u_char toname[11], oldname[11];
931 	u_long from_diroffset, to_diroffset;
932 	u_char to_count;
933 	int doingdirectory = 0, newparent = 0;
934 	int error;
935 	u_long cn;
936 	daddr_t bn;
937 	struct msdosfsmount *pmp;
938 	struct direntry *dotdotp;
939 	struct buf *bp;
940 
941 	pmp = VFSTOMSDOSFS(fdvp->v_mount);
942 
943 	/*
944 	 * Check for cross-device rename.
945 	 */
946 	if ((fvp->v_mount != tdvp->v_mount) ||
947 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
948 		error = EXDEV;
949 abortit:
950 		if (tdvp == tvp)
951 			vrele(tdvp);
952 		else
953 			vput(tdvp);
954 		if (tvp)
955 			vput(tvp);
956 		vrele(fdvp);
957 		vrele(fvp);
958 		return (error);
959 	}
960 
961 	/*
962 	 * If source and dest are the same, do nothing.
963 	 */
964 	if (tvp == fvp) {
965 		error = 0;
966 		goto abortit;
967 	}
968 
969 	/*
970 	 * fvp, fdvp are unlocked, tvp, tdvp are locked.  Lock fvp and note
971 	 * that we have to unlock it to use the abortit target.
972 	 */
973 	error = vn_lock(fvp, LK_EXCLUSIVE);
974 	if (error)
975 		goto abortit;
976 	dp = VTODE(fdvp);
977 	ip = VTODE(fvp);
978 
979 	/*
980 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
981 	 * leads to a crippled directory tree.  It's pretty tough to do a
982 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
983 	 * doesn't work if the ".." entry is missing.
984 	 */
985 	if (ip->de_Attributes & ATTR_DIRECTORY) {
986 		/*
987 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
988 		 */
989 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
990 		    dp == ip ||
991 		    (fcnp->cn_flags & CNP_ISDOTDOT) ||
992 		    (tcnp->cn_flags & CNP_ISDOTDOT) ||
993 		    (ip->de_flag & DE_RENAME)) {
994 			vn_unlock(fvp);
995 			error = EINVAL;
996 			goto abortit;
997 		}
998 		ip->de_flag |= DE_RENAME;
999 		doingdirectory++;
1000 	}
1001 
1002 	/*
1003 	 * fvp locked, fdvp unlocked, tvp, tdvp locked.  DE_RENAME only
1004 	 * set if doingdirectory.  We will get fvp unlocked in fairly
1005 	 * short order.  dp and xp must be setup and fvp must be unlocked
1006 	 * for the out and bad targets to work properly.
1007 	 */
1008 	dp = VTODE(tdvp);
1009 	xp = tvp ? VTODE(tvp) : NULL;
1010 
1011 	/*
1012 	 * Remember direntry place to use for destination
1013 	 */
1014 	to_diroffset = dp->de_fndoffset;
1015 	to_count = dp->de_fndcnt;
1016 
1017 	/*
1018 	 * If ".." must be changed (ie the directory gets a new
1019 	 * parent) then the source directory must not be in the
1020 	 * directory heirarchy above the target, as this would
1021 	 * orphan everything below the source directory. Also
1022 	 * the user must have write permission in the source so
1023 	 * as to be able to change "..". We must repeat the call
1024 	 * to namei, as the parent directory is unlocked by the
1025 	 * call to doscheckpath().
1026 	 */
1027 	error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
1028 	vn_unlock(fvp);
1029 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1030 		newparent = 1;
1031 
1032 	/*
1033 	 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1034 	 * DE_RENAME only set if doingdirectory.
1035 	 */
1036 	if (doingdirectory && newparent) {
1037 		if (error)	/* write access check above */
1038 			goto bad;
1039 		if (xp != NULL) {
1040 			vput(tvp);
1041 			xp = NULL;
1042 		}
1043 		/*
1044 		 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1045 		 * get an extra ref so we wind up with just an unlocked, ref'd
1046 		 * tdvp.  The 'out' target skips tvp and tdvp cleanups (tdvp
1047 		 * isn't locked so we can't use the out target).
1048 		 */
1049 		vref(tdvp);
1050 		error = doscheckpath(ip, dp);
1051 		tcnp->cn_flags |= CNP_PDIRUNLOCK;
1052 		if (error) {
1053 			vrele(tdvp);
1054 			goto out;
1055 		}
1056 		/*
1057 		 * relookup no longer messes with the ref count.  tdvp must
1058 		 * be unlocked on entry and on success will be locked on
1059 		 * return.
1060 		 */
1061 		error = relookup(tdvp, &tvp, tcnp);
1062 		if (error) {
1063 			if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1064 				vrele(tdvp);
1065 			else
1066 				vput(tdvp);
1067 			goto out;
1068 		}
1069 
1070 		/*
1071 		 * tvp and tdvp are now locked again.
1072 		 */
1073 		dp = VTODE(tdvp);
1074 		xp = tvp ? VTODE(tvp) : NULL;
1075 	}
1076 
1077 	/*
1078 	 * tvp and tdvp are now locked again, the 'bad' target can be used
1079 	 * to clean them up again.  Delete an existant target and clean
1080 	 * up tvp.  Set xp to NULL to indicate that tvp has been cleaned up.
1081 	 */
1082 	if (xp != NULL) {
1083 		/*
1084 		 * Target must be empty if a directory and have no links
1085 		 * to it. Also, ensure source and target are compatible
1086 		 * (both directories, or both not directories).
1087 		 */
1088 		if (xp->de_Attributes & ATTR_DIRECTORY) {
1089 			if (!dosdirempty(xp)) {
1090 				error = ENOTEMPTY;
1091 				goto bad;
1092 			}
1093 			if (!doingdirectory) {
1094 				error = ENOTDIR;
1095 				goto bad;
1096 			}
1097 		} else if (doingdirectory) {
1098 			error = EISDIR;
1099 			goto bad;
1100 		}
1101 		error = removede(dp, xp);
1102 		if (error)
1103 			goto bad;
1104 		vput(tvp);
1105 		xp = NULL;
1106 		tvp = NULL;
1107 	}
1108 
1109 	/*
1110 	 * Convert the filename in tcnp into a dos filename. We copy this
1111 	 * into the denode and directory entry for the destination
1112 	 * file/directory.
1113 	 */
1114 	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1115 	if (error)
1116 		goto bad;
1117 
1118 	/*
1119 	 * Since from wasn't locked at various places above, we have to do
1120 	 * a relookup here.  If the target and source are the same directory
1121 	 * we have to unlock the target directory in order to safely relookup
1122 	 * the source, because relookup expects its directory to be unlocked.
1123 	 *
1124 	 * Note that ap->a_fvp is still valid and ref'd.  Any cleanup must
1125 	 * now take that into account.
1126 	 *
1127 	 * The tdvp locking issues make this a real mess.
1128 	 */
1129 	fcnp->cn_flags &= ~CNP_MODMASK;
1130 	fcnp->cn_flags |= CNP_LOCKPARENT;
1131 	if (newparent == 0)
1132 		vn_unlock(tdvp);
1133 	error = relookup(fdvp, &fvp, fcnp);
1134 	if (error || fvp == NULL) {
1135 		/*
1136 		 * From name has disappeared.  Note: fdvp might == tdvp.
1137 		 *
1138 		 * DE_RENAME is only set if doingdirectory.
1139 		 */
1140 		if (doingdirectory)
1141 			panic("rename: lost dir entry");
1142 		if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1143 			vrele(fdvp);
1144 		else
1145 			vput(fdvp);
1146 		if (newparent == 0)
1147 			vrele(tdvp);
1148 		else
1149 			vput(tdvp);
1150 		vrele(ap->a_fvp);
1151 		return(0);
1152 	}
1153 
1154 	/*
1155 	 * No error occured.  tdvp, fdvp and fvp are all locked.  If
1156 	 * newparent was 0 be aware that fdvp == tdvp.  tvp has been cleaned
1157 	 * up.  ap->a_fvp is still refd.
1158 	 */
1159 	xp = VTODE(fvp);
1160 	zp = VTODE(fdvp);
1161 	from_diroffset = zp->de_fndoffset;
1162 
1163 	/*
1164 	 * Ensure that the directory entry still exists and has not
1165 	 * changed till now. If the source is a file the entry may
1166 	 * have been unlinked or renamed. In either case there is
1167 	 * no further work to be done. If the source is a directory
1168 	 * then it cannot have been rmdir'ed or renamed; this is
1169 	 * prohibited by the DE_RENAME flag.
1170 	 *
1171 	 * DE_RENAME is only set if doingdirectory.
1172 	 */
1173 	if (xp != ip) {
1174 		if (doingdirectory)
1175 			panic("rename: lost dir entry");
1176 		goto done;
1177 	} else {
1178 		u_long new_dirclust;
1179 		u_long new_diroffset;
1180 
1181 		/*
1182 		 * First write a new entry in the destination
1183 		 * directory and mark the entry in the source directory
1184 		 * as deleted.  Then move the denode to the correct hash
1185 		 * chain for its new location in the filesystem.  And, if
1186 		 * we moved a directory, then update its .. entry to point
1187 		 * to the new parent directory.
1188 		 */
1189 		bcopy(ip->de_Name, oldname, 11);
1190 		bcopy(toname, ip->de_Name, 11);	/* update denode */
1191 		dp->de_fndoffset = to_diroffset;
1192 		dp->de_fndcnt = to_count;
1193 		error = createde(ip, dp, NULL, tcnp);
1194 		if (error) {
1195 			bcopy(oldname, ip->de_Name, 11);
1196 			goto done;
1197 		}
1198 		ip->de_refcnt++;
1199 		zp->de_fndoffset = from_diroffset;
1200 		error = removede(zp, ip);
1201 		if (error) {
1202 			/* XXX should really panic here, fs is corrupt */
1203 			goto done;
1204 		}
1205 		if (!doingdirectory) {
1206 			error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1207 				       NULL, &new_dirclust, NULL);
1208 			if (error) {
1209 				/* XXX should really panic here, fs is corrupt */
1210 				goto done;
1211 			}
1212 			if (new_dirclust == MSDOSFSROOT)
1213 				new_diroffset = to_diroffset;
1214 			else
1215 				new_diroffset = to_diroffset & pmp->pm_crbomask;
1216 			msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1217 		}
1218 	}
1219 
1220 	/*
1221 	 * If we moved a directory to a new parent directory, then we must
1222 	 * fixup the ".." entry in the moved directory.
1223 	 */
1224 	if (doingdirectory && newparent) {
1225 		cn = ip->de_StartCluster;
1226 		if (cn == MSDOSFSROOT) {
1227 			/* this should never happen */
1228 			panic("msdosfs_rename(): updating .. in root directory?");
1229 		} else {
1230 			bn = xcntobn(pmp, cn);
1231 		}
1232 		error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
1233 		if (error) {
1234 			/* XXX should really panic here, fs is corrupt */
1235 			brelse(bp);
1236 			goto done;
1237 		}
1238 		dotdotp = (struct direntry *)bp->b_data + 1;
1239 		putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1240 		if (FAT32(pmp))
1241 			putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1242 		error = bwrite(bp);
1243 		if (error) {
1244 			/* XXX should really panic here, fs is corrupt */
1245 			goto done;
1246 		}
1247 	}
1248 
1249 	/*
1250 	 * done case fvp, fdvp, tdvp are locked.  ap->a_fvp is refd
1251 	 */
1252 done:
1253 	if (doingdirectory)
1254 		ip->de_flag &= ~DE_RENAME;	/* XXX fvp not locked */
1255 	vput(fvp);
1256 	if (newparent)
1257 		vput(fdvp);
1258 	else
1259 		vrele(fdvp);
1260 	vput(tdvp);
1261 	vrele(ap->a_fvp);
1262 	return (error);
1263 
1264 	/*
1265 	 * 'bad' target: xp governs tvp.  tvp and tdvp arel ocked, fdvp and fvp
1266 	 * are not locked.  ip points to fvp's inode which may have DE_RENAME
1267 	 * set.
1268 	 */
1269 bad:
1270 	if (xp)
1271 		vput(tvp);
1272 	vput(tdvp);
1273 out:
1274 	/*
1275 	 * 'out' target: tvp and tdvp have already been cleaned up.
1276 	 */
1277 	if (doingdirectory)
1278 		ip->de_flag &= ~DE_RENAME;
1279 	vrele(fdvp);
1280 	vrele(fvp);
1281 	return (error);
1282 
1283 }
1284 
1285 static struct {
1286 	struct direntry dot;
1287 	struct direntry dotdot;
1288 } dosdirtemplate = {
1289 	{	".       ", "   ",			/* the . entry */
1290 		ATTR_DIRECTORY,				/* file attribute */
1291 		0,	 				/* reserved */
1292 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1293 		{ 0, 0 },				/* access date */
1294 		{ 0, 0 },				/* high bits of start cluster */
1295 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1296 		{ 0, 0 },				/* startcluster */
1297 		{ 0, 0, 0, 0 } 				/* filesize */
1298 	},
1299 	{	"..      ", "   ",			/* the .. entry */
1300 		ATTR_DIRECTORY,				/* file attribute */
1301 		0,	 				/* reserved */
1302 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1303 		{ 0, 0 },				/* access date */
1304 		{ 0, 0 },				/* high bits of start cluster */
1305 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1306 		{ 0, 0 },				/* startcluster */
1307 		{ 0, 0, 0, 0 }				/* filesize */
1308 	}
1309 };
1310 
1311 /*
1312  * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1313  *		 struct componentname *a_cnp, struct vattr *a_vap)
1314  */
1315 static int
1316 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1317 {
1318 	struct componentname *cnp = ap->a_cnp;
1319 	struct denode *dep;
1320 	struct denode *pdep = VTODE(ap->a_dvp);
1321 	struct direntry *denp;
1322 	struct msdosfsmount *pmp = pdep->de_pmp;
1323 	struct buf *bp;
1324 	u_long newcluster, pcl;
1325 	int bn;
1326 	int error;
1327 	struct denode ndirent;
1328 	struct timespec ts;
1329 
1330 	/*
1331 	 * If this is the root directory and there is no space left we
1332 	 * can't do anything.  This is because the root directory can not
1333 	 * change size.
1334 	 */
1335 	if (pdep->de_StartCluster == MSDOSFSROOT
1336 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1337 		error = ENOSPC;
1338 		goto bad2;
1339 	}
1340 
1341 	/*
1342 	 * Allocate a cluster to hold the about to be created directory.
1343 	 */
1344 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1345 	if (error)
1346 		goto bad2;
1347 
1348 	bzero(&ndirent, sizeof(ndirent));
1349 	ndirent.de_pmp = pmp;
1350 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1351 	getnanotime(&ts);
1352 	DETIMES(&ndirent, &ts, &ts, &ts);
1353 
1354 	/*
1355 	 * Now fill the cluster with the "." and ".." entries. And write
1356 	 * the cluster to disk.  This way it is there for the parent
1357 	 * directory to be pointing at if there were a crash.
1358 	 */
1359 	bn = xcntobn(pmp, newcluster);
1360 	/* always succeeds */
1361 	bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1362 		    pmp->pm_bpcluster, 0, 0);
1363 	bzero(bp->b_data, pmp->pm_bpcluster);
1364 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1365 	denp = (struct direntry *)bp->b_data;
1366 	putushort(denp[0].deStartCluster, newcluster);
1367 	putushort(denp[0].deCDate, ndirent.de_CDate);
1368 	putushort(denp[0].deCTime, ndirent.de_CTime);
1369 	denp[0].deCHundredth = ndirent.de_CHun;
1370 	putushort(denp[0].deADate, ndirent.de_ADate);
1371 	putushort(denp[0].deMDate, ndirent.de_MDate);
1372 	putushort(denp[0].deMTime, ndirent.de_MTime);
1373 	pcl = pdep->de_StartCluster;
1374 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1375 		pcl = 0;
1376 	putushort(denp[1].deStartCluster, pcl);
1377 	putushort(denp[1].deCDate, ndirent.de_CDate);
1378 	putushort(denp[1].deCTime, ndirent.de_CTime);
1379 	denp[1].deCHundredth = ndirent.de_CHun;
1380 	putushort(denp[1].deADate, ndirent.de_ADate);
1381 	putushort(denp[1].deMDate, ndirent.de_MDate);
1382 	putushort(denp[1].deMTime, ndirent.de_MTime);
1383 	if (FAT32(pmp)) {
1384 		putushort(denp[0].deHighClust, newcluster >> 16);
1385 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1386 	}
1387 
1388 	error = bwrite(bp);
1389 	if (error)
1390 		goto bad;
1391 
1392 	/*
1393 	 * Now build up a directory entry pointing to the newly allocated
1394 	 * cluster.  This will be written to an empty slot in the parent
1395 	 * directory.
1396 	 */
1397 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1398 	if (error)
1399 		goto bad;
1400 
1401 	ndirent.de_Attributes = ATTR_DIRECTORY;
1402 	ndirent.de_LowerCase = 0;
1403 	ndirent.de_StartCluster = newcluster;
1404 	ndirent.de_FileSize = 0;
1405 	ndirent.de_dev = pdep->de_dev;
1406 	ndirent.de_devvp = pdep->de_devvp;
1407 	error = createde(&ndirent, pdep, &dep, cnp);
1408 	if (error)
1409 		goto bad;
1410 	*ap->a_vpp = DETOV(dep);
1411 	return (0);
1412 
1413 bad:
1414 	clusterfree(pmp, newcluster, NULL);
1415 bad2:
1416 	return (error);
1417 }
1418 
1419 /*
1420  * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1421  *		 struct componentname *a_cnp)
1422  */
1423 static int
1424 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1425 {
1426 	struct vnode *vp = ap->a_vp;
1427 	struct vnode *dvp = ap->a_dvp;
1428 	struct denode *ip, *dp;
1429 	int error;
1430 
1431 	ip = VTODE(vp);
1432 	dp = VTODE(dvp);
1433 
1434 	/*
1435 	 * Verify the directory is empty (and valid).
1436 	 * (Rmdir ".." won't be valid since
1437 	 *  ".." will contain a reference to
1438 	 *  the current directory and thus be
1439 	 *  non-empty.)
1440 	 */
1441 	error = 0;
1442 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1443 		error = ENOTEMPTY;
1444 		goto out;
1445 	}
1446 	/*
1447 	 * Delete the entry from the directory.  For dos filesystems this
1448 	 * gets rid of the directory entry on disk, the in memory copy
1449 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1450 	 * being found by deget().  When the vput() on dep is done we give
1451 	 * up access and eventually msdosfs_reclaim() will be called which
1452 	 * will remove it from the denode cache.
1453 	 */
1454 	error = removede(dp, ip);
1455 	if (error)
1456 		goto out;
1457 	/*
1458 	 * This is where we decrement the link count in the parent
1459 	 * directory.  Since dos filesystems don't do this we just purge
1460 	 * the name cache.
1461 	 */
1462 	vn_unlock(dvp);
1463 	/*
1464 	 * Truncate the directory that is being deleted.
1465 	 */
1466 	error = detrunc(ip, (u_long)0, IO_SYNC);
1467 
1468 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1469 out:
1470 	return (error);
1471 }
1472 
1473 /*
1474  * DOS filesystems don't know what symlinks are.
1475  *
1476  * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1477  *		   struct componentname *a_cnp, struct vattr *a_vap,
1478  *		   char *a_target)
1479  */
1480 static int
1481 msdosfs_symlink(struct vop_old_symlink_args *ap)
1482 {
1483 	return (EOPNOTSUPP);
1484 }
1485 
1486 /*
1487  * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1488  *		   struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1489  *		   off_t **a_cookies)
1490  */
1491 static int
1492 msdosfs_readdir(struct vop_readdir_args *ap)
1493 {
1494 	struct mbnambuf nb;
1495 	int error = 0;
1496 	int diff;
1497 	long n;
1498 	int blsize;
1499 	long on;
1500 	u_long cn;
1501 	u_long dirsperblk;
1502 	long bias = 0;
1503 	daddr_t bn, lbn;
1504 	struct buf *bp;
1505 	struct denode *dep;
1506 	struct msdosfsmount *pmp;
1507 	struct direntry *dentp;
1508 	struct uio *uio = ap->a_uio;
1509 	off_t *cookies = NULL;
1510 	int ncookies = 0;
1511 	off_t offset, off;
1512 	int chksum = -1;
1513 	ino_t d_ino;
1514 	uint16_t d_namlen;
1515 	uint8_t d_type;
1516 	char *d_name_storage = NULL;
1517 	char *d_name = NULL;
1518 
1519 	if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
1520 		return (error);
1521 
1522 	dep = VTODE(ap->a_vp);
1523 	pmp = dep->de_pmp;
1524 
1525 #ifdef MSDOSFS_DEBUG
1526 	kprintf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1527 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1528 #endif
1529 
1530 	/*
1531 	 * msdosfs_readdir() won't operate properly on regular files since
1532 	 * it does i/o only with the the filesystem vnode, and hence can
1533 	 * retrieve the wrong block from the buffer cache for a plain file.
1534 	 * So, fail attempts to readdir() on a plain file.
1535 	 */
1536 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
1537 		error = ENOTDIR;
1538 		goto done;
1539 	}
1540 
1541 	/*
1542 	 * If the user buffer is smaller than the size of one dos directory
1543 	 * entry or the file offset is not a multiple of the size of a
1544 	 * directory entry, then we fail the read.
1545 	 */
1546 	off = offset = uio->uio_offset;
1547 	if (uio->uio_resid < sizeof(struct direntry) ||
1548 	    (offset & (sizeof(struct direntry) - 1))) {
1549 		error = EINVAL;
1550 		goto done;
1551 	}
1552 
1553 	if (ap->a_ncookies) {
1554 		ncookies = uio->uio_resid / 16 + 1;
1555 		if (ncookies > 1024)
1556 			ncookies = 1024;
1557 		cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, 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