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