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