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