xref: /freebsd/sys/fs/msdosfs/msdosfs_vnops.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*-
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/clock.h>
56 #include <sys/dirent.h>
57 #include <sys/lock.h>
58 #include <sys/lockf.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/stat.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
70 
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73 
74 #include <fs/msdosfs/bpb.h>
75 #include <fs/msdosfs/direntry.h>
76 #include <fs/msdosfs/denode.h>
77 #include <fs/msdosfs/fat.h>
78 #include <fs/msdosfs/msdosfsmount.h>
79 
80 #define	DOS_FILESIZE_MAX	0xffffffff
81 
82 /*
83  * Prototypes for MSDOSFS vnode operations
84  */
85 static vop_create_t	msdosfs_create;
86 static vop_mknod_t	msdosfs_mknod;
87 static vop_open_t	msdosfs_open;
88 static vop_close_t	msdosfs_close;
89 static vop_access_t	msdosfs_access;
90 static vop_getattr_t	msdosfs_getattr;
91 static vop_setattr_t	msdosfs_setattr;
92 static vop_read_t	msdosfs_read;
93 static vop_write_t	msdosfs_write;
94 static vop_fsync_t	msdosfs_fsync;
95 static vop_remove_t	msdosfs_remove;
96 static vop_link_t	msdosfs_link;
97 static vop_rename_t	msdosfs_rename;
98 static vop_mkdir_t	msdosfs_mkdir;
99 static vop_rmdir_t	msdosfs_rmdir;
100 static vop_symlink_t	msdosfs_symlink;
101 static vop_readdir_t	msdosfs_readdir;
102 static vop_bmap_t	msdosfs_bmap;
103 static vop_strategy_t	msdosfs_strategy;
104 static vop_print_t	msdosfs_print;
105 static vop_pathconf_t	msdosfs_pathconf;
106 static vop_vptofh_t	msdosfs_vptofh;
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. We must also free
129  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
130  * only if the SAVESTART bit in cn_flags is clear on success.
131  */
132 static int
133 msdosfs_create(ap)
134 	struct vop_create_args /* {
135 		struct vnode *a_dvp;
136 		struct vnode **a_vpp;
137 		struct componentname *a_cnp;
138 		struct vattr *a_vap;
139 	} */ *ap;
140 {
141 	struct componentname *cnp = ap->a_cnp;
142 	struct denode ndirent;
143 	struct denode *dep;
144 	struct denode *pdep = VTODE(ap->a_dvp);
145 	struct timespec ts;
146 	int error;
147 
148 #ifdef MSDOSFS_DEBUG
149 	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
150 #endif
151 
152 	/*
153 	 * If this is the root directory and there is no space left we
154 	 * can't do anything.  This is because the root directory can not
155 	 * change size.
156 	 */
157 	if (pdep->de_StartCluster == MSDOSFSROOT
158 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
159 		error = ENOSPC;
160 		goto bad;
161 	}
162 
163 	/*
164 	 * Create a directory entry for the file, then call createde() to
165 	 * have it installed. NOTE: DOS files are always executable.  We
166 	 * use the absence of the owner write bit to make the file
167 	 * readonly.
168 	 */
169 #ifdef DIAGNOSTIC
170 	if ((cnp->cn_flags & HASBUF) == 0)
171 		panic("msdosfs_create: no name");
172 #endif
173 	bzero(&ndirent, sizeof(ndirent));
174 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
175 	if (error)
176 		goto bad;
177 
178 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
179 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
180 	ndirent.de_LowerCase = 0;
181 	ndirent.de_StartCluster = 0;
182 	ndirent.de_FileSize = 0;
183 	ndirent.de_dev = pdep->de_dev;
184 	ndirent.de_pmp = pdep->de_pmp;
185 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
186 	getnanotime(&ts);
187 	DETIMES(&ndirent, &ts, &ts, &ts);
188 	error = createde(&ndirent, pdep, &dep, cnp);
189 	if (error)
190 		goto bad;
191 	*ap->a_vpp = DETOV(dep);
192 	return (0);
193 
194 bad:
195 	return (error);
196 }
197 
198 static int
199 msdosfs_mknod(ap)
200 	struct vop_mknod_args /* {
201 		struct vnode *a_dvp;
202 		struct vnode **a_vpp;
203 		struct componentname *a_cnp;
204 		struct vattr *a_vap;
205 	} */ *ap;
206 {
207 
208     return (EINVAL);
209 }
210 
211 static int
212 msdosfs_open(ap)
213 	struct vop_open_args /* {
214 		struct vnode *a_vp;
215 		int a_mode;
216 		struct ucred *a_cred;
217 		struct thread *a_td;
218 		int a_fdidx;
219 	} */ *ap;
220 {
221 	struct denode *dep = VTODE(ap->a_vp);
222 	vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
223 	return 0;
224 }
225 
226 static int
227 msdosfs_close(ap)
228 	struct vop_close_args /* {
229 		struct vnode *a_vp;
230 		int a_fflag;
231 		struct ucred *a_cred;
232 		struct thread *a_td;
233 	} */ *ap;
234 {
235 	struct vnode *vp = ap->a_vp;
236 	struct denode *dep = VTODE(vp);
237 	struct timespec ts;
238 
239 	VI_LOCK(vp);
240 	if (vp->v_usecount > 1) {
241 		getnanotime(&ts);
242 		DETIMES(dep, &ts, &ts, &ts);
243 	}
244 	VI_UNLOCK(vp);
245 	return 0;
246 }
247 
248 static int
249 msdosfs_access(ap)
250 	struct vop_access_args /* {
251 		struct vnode *a_vp;
252 		accmode_t a_accmode;
253 		struct ucred *a_cred;
254 		struct thread *a_td;
255 	} */ *ap;
256 {
257 	struct vnode *vp = ap->a_vp;
258 	struct denode *dep = VTODE(ap->a_vp);
259 	struct msdosfsmount *pmp = dep->de_pmp;
260 	mode_t file_mode;
261 	accmode_t accmode = ap->a_accmode;
262 
263 	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
264 	    ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
265 	file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
266 
267 	/*
268 	 * Disallow writing to directories and regular files if the
269 	 * filesystem is read-only.
270 	 */
271 	if (accmode & VWRITE) {
272 		switch (vp->v_type) {
273 		case VDIR:
274 		case VREG:
275 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
276 				return (EROFS);
277 			break;
278 		default:
279 			break;
280 		}
281 	}
282 
283 	return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
284 	    ap->a_accmode, ap->a_cred, NULL));
285 }
286 
287 static int
288 msdosfs_getattr(ap)
289 	struct vop_getattr_args /* {
290 		struct vnode *a_vp;
291 		struct vattr *a_vap;
292 		struct ucred *a_cred;
293 	} */ *ap;
294 {
295 	struct denode *dep = VTODE(ap->a_vp);
296 	struct msdosfsmount *pmp = dep->de_pmp;
297 	struct vattr *vap = ap->a_vap;
298 	mode_t mode;
299 	struct timespec ts;
300 	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
301 	uint64_t fileid;
302 
303 	getnanotime(&ts);
304 	DETIMES(dep, &ts, &ts, &ts);
305 	vap->va_fsid = dev2udev(dep->de_dev);
306 	/*
307 	 * The following computation of the fileid must be the same as that
308 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
309 	 * doesn't work.
310 	 */
311 	if (dep->de_Attributes & ATTR_DIRECTORY) {
312 		fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
313 		    dirsperblk;
314 		if (dep->de_StartCluster == MSDOSFSROOT)
315 			fileid = 1;
316 	} else {
317 		fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
318 		    dirsperblk;
319 		if (dep->de_dirclust == MSDOSFSROOT)
320 			fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
321 		fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
322 	}
323 
324 	if (pmp->pm_flags & MSDOSFS_LARGEFS)
325 		vap->va_fileid = msdosfs_fileno_map(pmp->pm_mountp, fileid);
326 	else
327 		vap->va_fileid = (long)fileid;
328 
329 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
330 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
331 	else
332 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
333 	vap->va_mode = mode &
334 	    (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
335 	vap->va_uid = pmp->pm_uid;
336 	vap->va_gid = pmp->pm_gid;
337 	vap->va_nlink = 1;
338 	vap->va_rdev = NODEV;
339 	vap->va_size = dep->de_FileSize;
340 	fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
341 	vap->va_ctime = vap->va_mtime;
342 	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
343 		fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
344 		fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
345 		    0, &vap->va_birthtime);
346 	} else {
347 		vap->va_atime = vap->va_mtime;
348 		vap->va_birthtime.tv_sec = -1;
349 		vap->va_birthtime.tv_nsec = 0;
350 	}
351 	vap->va_flags = 0;
352 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
353 		vap->va_flags |= SF_ARCHIVED;
354 	vap->va_gen = 0;
355 	vap->va_blocksize = pmp->pm_bpcluster;
356 	vap->va_bytes =
357 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
358 	vap->va_type = ap->a_vp->v_type;
359 	vap->va_filerev = dep->de_modrev;
360 	return (0);
361 }
362 
363 static int
364 msdosfs_setattr(ap)
365 	struct vop_setattr_args /* {
366 		struct vnode *a_vp;
367 		struct vattr *a_vap;
368 		struct ucred *a_cred;
369 	} */ *ap;
370 {
371 	struct vnode *vp = ap->a_vp;
372 	struct denode *dep = VTODE(ap->a_vp);
373 	struct msdosfsmount *pmp = dep->de_pmp;
374 	struct vattr *vap = ap->a_vap;
375 	struct ucred *cred = ap->a_cred;
376 	struct thread *td = curthread;
377 	int error = 0;
378 
379 #ifdef MSDOSFS_DEBUG
380 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
381 	    ap->a_vp, vap, cred);
382 #endif
383 
384 	/*
385 	 * Check for unsettable attributes.
386 	 */
387 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
388 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
389 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
390 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
391 #ifdef MSDOSFS_DEBUG
392 		printf("msdosfs_setattr(): returning EINVAL\n");
393 		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
394 		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
395 		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
396 		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
397 		printf("    va_uid %x, va_gid %x\n",
398 		    vap->va_uid, vap->va_gid);
399 #endif
400 		return (EINVAL);
401 	}
402 	if (vap->va_flags != VNOVAL) {
403 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
404 			return (EROFS);
405 		if (cred->cr_uid != pmp->pm_uid) {
406 			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
407 			if (error)
408 				return (error);
409 		}
410 		/*
411 		 * We are very inconsistent about handling unsupported
412 		 * attributes.  We ignored the access time and the
413 		 * read and execute bits.  We were strict for the other
414 		 * attributes.
415 		 *
416 		 * Here we are strict, stricter than ufs in not allowing
417 		 * users to attempt to set SF_SETTABLE bits or anyone to
418 		 * set unsupported bits.  However, we ignore attempts to
419 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
420 		 * sensible filesystem attempts it a lot.
421 		 */
422 		if (vap->va_flags & SF_SETTABLE) {
423 			error = priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0);
424 			if (error)
425 				return (error);
426 		}
427 		if (vap->va_flags & ~SF_ARCHIVED)
428 			return EOPNOTSUPP;
429 		if (vap->va_flags & SF_ARCHIVED)
430 			dep->de_Attributes &= ~ATTR_ARCHIVE;
431 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
432 			dep->de_Attributes |= ATTR_ARCHIVE;
433 		dep->de_flag |= DE_MODIFIED;
434 	}
435 
436 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
437 		uid_t uid;
438 		gid_t gid;
439 
440 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
441 			return (EROFS);
442 		uid = vap->va_uid;
443 		if (uid == (uid_t)VNOVAL)
444 			uid = pmp->pm_uid;
445 		gid = vap->va_gid;
446 		if (gid == (gid_t)VNOVAL)
447 			gid = pmp->pm_gid;
448 		if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
449 		    (gid != pmp->pm_gid && !groupmember(gid, cred))) {
450 			error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
451 			if (error)
452 				return (error);
453 		}
454 		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
455 			return EINVAL;
456 	}
457 
458 	if (vap->va_size != VNOVAL) {
459 		switch (vp->v_type) {
460 		case VDIR:
461 			return (EISDIR);
462 		case VREG:
463 			/*
464 			 * Truncation is only supported for regular files,
465 			 * Disallow it if the filesystem is read-only.
466 			 */
467 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 				return (EROFS);
469 			break;
470 		default:
471 			/*
472 			 * According to POSIX, the result is unspecified
473 			 * for file types other than regular files,
474 			 * directories and shared memory objects.  We
475 			 * don't support any file types except regular
476 			 * files and directories in this file system, so
477 			 * this (default) case is unreachable and can do
478 			 * anything.  Keep falling through to detrunc()
479 			 * for now.
480 			 */
481 			break;
482 		}
483 		error = detrunc(dep, vap->va_size, 0, cred, td);
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 (vap->va_vaflags & VA_UTIMES_NULL) {
491 			error = VOP_ACCESS(vp, VADMIN, cred, td);
492 			if (error)
493 				error = VOP_ACCESS(vp, VWRITE, cred, td);
494 		} else
495 			error = VOP_ACCESS(vp, VADMIN, cred, td);
496 		if (vp->v_type != VDIR) {
497 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
498 			    vap->va_atime.tv_sec != VNOVAL) {
499 				dep->de_flag &= ~DE_ACCESS;
500 				timespec2fattime(&vap->va_atime, 0,
501 				    &dep->de_ADate, NULL, NULL);
502 			}
503 			if (vap->va_mtime.tv_sec != VNOVAL) {
504 				dep->de_flag &= ~DE_UPDATE;
505 				timespec2fattime(&vap->va_mtime, 0,
506 				    &dep->de_MDate, &dep->de_MTime, NULL);
507 			}
508 			dep->de_Attributes |= ATTR_ARCHIVE;
509 			dep->de_flag |= DE_MODIFIED;
510 		}
511 	}
512 	/*
513 	 * DOS files only have the ability to have their writability
514 	 * attribute set, so we use the owner write bit to set the readonly
515 	 * attribute.
516 	 */
517 	if (vap->va_mode != (mode_t)VNOVAL) {
518 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
519 			return (EROFS);
520 		if (cred->cr_uid != pmp->pm_uid) {
521 			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
522 			if (error)
523 				return (error);
524 		}
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, 0));
536 }
537 
538 static int
539 msdosfs_read(ap)
540 	struct vop_read_args /* {
541 		struct vnode *a_vp;
542 		struct uio *a_uio;
543 		int a_ioflag;
544 		struct ucred *a_cred;
545 	} */ *ap;
546 {
547 	int error = 0;
548 	int blsize;
549 	int isadir;
550 	int orig_resid;
551 	u_int n;
552 	u_long diff;
553 	u_long on;
554 	daddr_t lbn;
555 	daddr_t rablock;
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 	/*
565 	 * If they didn't ask for any data, then we are done.
566 	 */
567 	orig_resid = uio->uio_resid;
568 	if (orig_resid == 0)
569 		return (0);
570 
571 	/*
572 	 * The caller is supposed to ensure that
573 	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
574 	 * We don't need to check for large offsets as in ffs because
575 	 * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
576 	 * offsets cannot cause overflow even in theory.
577 	 */
578 
579 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
580 
581 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
582 	do {
583 		if (uio->uio_offset >= dep->de_FileSize)
584 			break;
585 		lbn = de_cluster(pmp, uio->uio_offset);
586 		rablock = lbn + 1;
587 		blsize = pmp->pm_bpcluster;
588 		on = uio->uio_offset & pmp->pm_crbomask;
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 			/* convert cluster # to block # */
596 			error = pcbmap(dep, lbn, &lbn, 0, &blsize);
597 			if (error == E2BIG) {
598 				error = EINVAL;
599 				break;
600 			} else if (error)
601 				break;
602 			error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
603 		} else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
604 			error = bread(vp, lbn, blsize, NOCRED, &bp);
605 		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
606 			error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
607 			    NOCRED, on + uio->uio_resid, seqcount, &bp);
608 		} else if (seqcount > 1) {
609 			rasize = blsize;
610 			error = breadn(vp, lbn,
611 			    blsize, &rablock, &rasize, 1, NOCRED, &bp);
612 		} else {
613 			error = bread(vp, lbn, blsize, NOCRED, &bp);
614 		}
615 		if (error) {
616 			brelse(bp);
617 			break;
618 		}
619 		diff = pmp->pm_bpcluster - on;
620 		n = diff > uio->uio_resid ? uio->uio_resid : diff;
621 		diff = dep->de_FileSize - uio->uio_offset;
622 		if (diff < n)
623 			n = diff;
624 		diff = blsize - bp->b_resid;
625 		if (diff < n)
626 			n = diff;
627 		error = uiomove(bp->b_data + on, (int) n, uio);
628 		brelse(bp);
629 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
630 	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
631 	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
632 		dep->de_flag |= DE_ACCESS;
633 	return (error);
634 }
635 
636 /*
637  * Write data to a file or directory.
638  */
639 static int
640 msdosfs_write(ap)
641 	struct vop_write_args /* {
642 		struct vnode *a_vp;
643 		struct uio *a_uio;
644 		int a_ioflag;
645 		struct ucred *a_cred;
646 	} */ *ap;
647 {
648 	int n;
649 	int croffset;
650 	int resid;
651 	u_long osize;
652 	int error = 0;
653 	u_long count;
654 	int seqcount;
655 	daddr_t bn, lastcn;
656 	struct buf *bp;
657 	int ioflag = ap->a_ioflag;
658 	struct uio *uio = ap->a_uio;
659 	struct thread *td = uio->uio_td;
660 	struct vnode *vp = ap->a_vp;
661 	struct vnode *thisvp;
662 	struct denode *dep = VTODE(vp);
663 	struct msdosfsmount *pmp = dep->de_pmp;
664 	struct ucred *cred = ap->a_cred;
665 
666 #ifdef MSDOSFS_DEBUG
667 	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
668 	    vp, uio, ioflag, cred);
669 	printf("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 	/*
686 	 * This is needed (unlike in ffs_write()) because we extend the
687 	 * file outside of the loop but we don't want to extend the file
688 	 * for writes of 0 bytes.
689 	 */
690 	if (uio->uio_resid == 0)
691 		return (0);
692 
693 	/*
694 	 * The caller is supposed to ensure that
695 	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
696 	 */
697 	if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
698 		return (EFBIG);
699 
700 	/*
701 	 * If they've exceeded their filesize limit, tell them about it.
702 	 */
703 	if (td != NULL) {
704 		PROC_LOCK(td->td_proc);
705 		if ((uoff_t)uio->uio_offset + uio->uio_resid >
706 		    lim_cur(td->td_proc, RLIMIT_FSIZE)) {
707 			psignal(td->td_proc, SIGXFSZ);
708 			PROC_UNLOCK(td->td_proc);
709 			return (EFBIG);
710 		}
711 		PROC_UNLOCK(td->td_proc);
712 	}
713 
714 	/*
715 	 * If the offset we are starting the write at is beyond the end of
716 	 * the file, then they've done a seek.  Unix filesystems allow
717 	 * files with holes in them, DOS doesn't so we must fill the hole
718 	 * with zeroed blocks.
719 	 */
720 	if (uio->uio_offset > dep->de_FileSize) {
721 		error = deextend(dep, uio->uio_offset, cred);
722 		if (error)
723 			return (error);
724 	}
725 
726 	/*
727 	 * Remember some values in case the write fails.
728 	 */
729 	resid = uio->uio_resid;
730 	osize = dep->de_FileSize;
731 
732 	/*
733 	 * If we write beyond the end of the file, extend it to its ultimate
734 	 * size ahead of the time to hopefully get a contiguous area.
735 	 */
736 	if (uio->uio_offset + resid > osize) {
737 		count = de_clcount(pmp, uio->uio_offset + resid) -
738 			de_clcount(pmp, osize);
739 		error = extendfile(dep, count, NULL, NULL, 0);
740 		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
741 			goto errexit;
742 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
743 	} else
744 		lastcn = de_clcount(pmp, osize) - 1;
745 
746 	seqcount = ioflag >> IO_SEQSHIFT;
747 	do {
748 		if (de_cluster(pmp, uio->uio_offset) > lastcn) {
749 			error = ENOSPC;
750 			break;
751 		}
752 
753 		croffset = uio->uio_offset & pmp->pm_crbomask;
754 		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
755 		if (uio->uio_offset + n > dep->de_FileSize) {
756 			dep->de_FileSize = uio->uio_offset + n;
757 			/* The object size needs to be set before buffer is allocated */
758 			vnode_pager_setsize(vp, dep->de_FileSize);
759 		}
760 
761 		bn = de_cluster(pmp, uio->uio_offset);
762 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
763 		    && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
764 			> de_cluster(pmp, uio->uio_offset)
765 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
766 			/*
767 			 * If either the whole cluster gets written,
768 			 * or we write the cluster from its start beyond EOF,
769 			 * then no need to read data from disk.
770 			 */
771 			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
772 			vfs_bio_clrbuf(bp);
773 			/*
774 			 * Do the bmap now, since pcbmap needs buffers
775 			 * for the fat table. (see msdosfs_strategy)
776 			 */
777 			if (bp->b_blkno == bp->b_lblkno) {
778 				error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
779 				if (error)
780 					bp->b_blkno = -1;
781 				else
782 					bp->b_blkno = bn;
783 			}
784 			if (bp->b_blkno == -1) {
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 it in.
793 			 */
794 			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
795 			if (error) {
796 				brelse(bp);
797 				break;
798 			}
799 		}
800 
801 		/*
802 		 * Should these vnode_pager_* functions be done on dir
803 		 * files?
804 		 */
805 
806 		/*
807 		 * Copy the data from user space into the buf header.
808 		 */
809 		error = uiomove(bp->b_data + croffset, n, uio);
810 		if (error) {
811 			brelse(bp);
812 			break;
813 		}
814 
815 		/* Prepare for clustered writes in some else clauses. */
816 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
817 			bp->b_flags |= B_CLUSTEROK;
818 
819 		/*
820 		 * If IO_SYNC, then each buffer is written synchronously.
821 		 * Otherwise, if we have a severe page deficiency then
822 		 * write the buffer asynchronously.  Otherwise, if on a
823 		 * cluster boundary then write the buffer asynchronously,
824 		 * combining it with contiguous clusters if permitted and
825 		 * possible, since we don't expect more writes into this
826 		 * buffer soon.  Otherwise, do a delayed write because we
827 		 * expect more writes into this buffer soon.
828 		 */
829 		if (ioflag & IO_SYNC)
830 			(void)bwrite(bp);
831 		else if (vm_page_count_severe() || buf_dirty_count_severe())
832 			bawrite(bp);
833 		else if (n + croffset == pmp->pm_bpcluster) {
834 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
835 				cluster_write(vp, bp, dep->de_FileSize,
836 				    seqcount);
837 			else
838 				bawrite(bp);
839 		} else
840 			bdwrite(bp);
841 		dep->de_flag |= DE_UPDATE;
842 	} while (error == 0 && uio->uio_resid > 0);
843 
844 	/*
845 	 * If the write failed and they want us to, truncate the file back
846 	 * to the size it was before the write was attempted.
847 	 */
848 errexit:
849 	if (error) {
850 		if (ioflag & IO_UNIT) {
851 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
852 			uio->uio_offset -= resid - uio->uio_resid;
853 			uio->uio_resid = resid;
854 		} else {
855 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
856 			if (uio->uio_resid != resid)
857 				error = 0;
858 		}
859 	} else if (ioflag & IO_SYNC)
860 		error = deupdat(dep, 1);
861 	return (error);
862 }
863 
864 /*
865  * Flush the blocks of a file to disk.
866  *
867  * This function is worthless for vnodes that represent directories. Maybe we
868  * could just do a sync if they try an fsync on a directory file.
869  */
870 static int
871 msdosfs_fsync(ap)
872 	struct vop_fsync_args /* {
873 		struct vnode *a_vp;
874 		struct ucred *a_cred;
875 		int a_waitfor;
876 		struct thread *a_td;
877 	} */ *ap;
878 {
879 
880 	vop_stdfsync(ap);
881 	return (deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT));
882 }
883 
884 static int
885 msdosfs_remove(ap)
886 	struct vop_remove_args /* {
887 		struct vnode *a_dvp;
888 		struct vnode *a_vp;
889 		struct componentname *a_cnp;
890 	} */ *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 	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
902 #endif
903 	return (error);
904 }
905 
906 /*
907  * DOS filesystems don't know what links are.
908  */
909 static int
910 msdosfs_link(ap)
911 	struct vop_link_args /* {
912 		struct vnode *a_tdvp;
913 		struct vnode *a_vp;
914 		struct componentname *a_cnp;
915 	} */ *ap;
916 {
917 	return (EOPNOTSUPP);
918 }
919 
920 /*
921  * Renames on files require moving the denode to a new hash queue since the
922  * denode's location is used to compute which hash queue to put the file
923  * in. Unless it is a rename in place.  For example "mv a b".
924  *
925  * What follows is the basic algorithm:
926  *
927  * if (file move) {
928  *	if (dest file exists) {
929  *		remove dest file
930  *	}
931  *	if (dest and src in same directory) {
932  *		rewrite name in existing directory slot
933  *	} else {
934  *		write new entry in dest directory
935  *		update offset and dirclust in denode
936  *		move denode to new hash chain
937  *		clear old directory entry
938  *	}
939  * } else {
940  *	directory move
941  *	if (dest directory exists) {
942  *		if (dest is not empty) {
943  *			return ENOTEMPTY
944  *		}
945  *		remove dest directory
946  *	}
947  *	if (dest and src in same directory) {
948  *		rewrite name in existing entry
949  *	} else {
950  *		be sure dest is not a child of src directory
951  *		write entry in dest directory
952  *		update "." and ".." in moved directory
953  *		clear old directory entry for moved directory
954  *	}
955  * }
956  *
957  * On entry:
958  *	source's parent directory is unlocked
959  *	source file or directory is unlocked
960  *	destination's parent directory is locked
961  *	destination file or directory is locked if it exists
962  *
963  * On exit:
964  *	all denodes should be released
965  */
966 static int
967 msdosfs_rename(ap)
968 	struct vop_rename_args /* {
969 		struct vnode *a_fdvp;
970 		struct vnode *a_fvp;
971 		struct componentname *a_fcnp;
972 		struct vnode *a_tdvp;
973 		struct vnode *a_tvp;
974 		struct componentname *a_tcnp;
975 	} */ *ap;
976 {
977 	struct vnode *tdvp = ap->a_tdvp;
978 	struct vnode *fvp = ap->a_fvp;
979 	struct vnode *fdvp = ap->a_fdvp;
980 	struct vnode *tvp = ap->a_tvp;
981 	struct componentname *tcnp = ap->a_tcnp;
982 	struct componentname *fcnp = ap->a_fcnp;
983 	struct denode *ip, *xp, *dp, *zp;
984 	u_char toname[12], oldname[11];
985 	u_long from_diroffset, to_diroffset;
986 	u_char to_count;
987 	int doingdirectory = 0, newparent = 0;
988 	int error;
989 	u_long cn;
990 	daddr_t bn;
991 	struct denode *fddep;	/* from file's parent directory	 */
992 	struct msdosfsmount *pmp;
993 	struct direntry *dotdotp;
994 	struct buf *bp;
995 
996 	fddep = VTODE(ap->a_fdvp);
997 	pmp = fddep->de_pmp;
998 
999 	pmp = VFSTOMSDOSFS(fdvp->v_mount);
1000 
1001 #ifdef DIAGNOSTIC
1002 	if ((tcnp->cn_flags & HASBUF) == 0 ||
1003 	    (fcnp->cn_flags & HASBUF) == 0)
1004 		panic("msdosfs_rename: no name");
1005 #endif
1006 	/*
1007 	 * Check for cross-device rename.
1008 	 */
1009 	if (fvp->v_mount != tdvp->v_mount ||
1010 	    (tvp && fvp->v_mount != tvp->v_mount)) {
1011 		error = EXDEV;
1012 abortit:
1013 		if (tdvp == tvp)
1014 			vrele(tdvp);
1015 		else
1016 			vput(tdvp);
1017 		if (tvp)
1018 			vput(tvp);
1019 		vrele(fdvp);
1020 		vrele(fvp);
1021 		return (error);
1022 	}
1023 
1024 	/*
1025 	 * If source and dest are the same, do nothing.
1026 	 */
1027 	if (tvp == fvp) {
1028 		error = 0;
1029 		goto abortit;
1030 	}
1031 
1032 	error = vn_lock(fvp, LK_EXCLUSIVE);
1033 	if (error)
1034 		goto abortit;
1035 	dp = VTODE(fdvp);
1036 	ip = VTODE(fvp);
1037 
1038 	/*
1039 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
1040 	 * leads to a crippled directory tree.  It's pretty tough to do a
1041 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1042 	 * doesn't work if the ".." entry is missing.
1043 	 */
1044 	if (ip->de_Attributes & ATTR_DIRECTORY) {
1045 		/*
1046 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1047 		 */
1048 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1049 		    dp == ip ||
1050 		    (fcnp->cn_flags & ISDOTDOT) ||
1051 		    (tcnp->cn_flags & ISDOTDOT) ||
1052 		    (ip->de_flag & DE_RENAME)) {
1053 			VOP_UNLOCK(fvp, 0);
1054 			error = EINVAL;
1055 			goto abortit;
1056 		}
1057 		ip->de_flag |= DE_RENAME;
1058 		doingdirectory++;
1059 	}
1060 
1061 	/*
1062 	 * When the target exists, both the directory
1063 	 * and target vnodes are returned locked.
1064 	 */
1065 	dp = VTODE(tdvp);
1066 	xp = tvp ? VTODE(tvp) : NULL;
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, tcnp->cn_thread);
1084 	VOP_UNLOCK(fvp, 0);
1085 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1086 		newparent = 1;
1087 	if (doingdirectory && newparent) {
1088 		if (error)	/* write access check above */
1089 			goto bad;
1090 		if (xp != NULL)
1091 			vput(tvp);
1092 		/*
1093 		 * doscheckpath() vput()'s dp,
1094 		 * so we have to do a relookup afterwards
1095 		 */
1096 		error = doscheckpath(ip, dp);
1097 		if (error)
1098 			goto out;
1099 		if ((tcnp->cn_flags & SAVESTART) == 0)
1100 			panic("msdosfs_rename: lost to startdir");
1101 		error = relookup(tdvp, &tvp, tcnp);
1102 		if (error)
1103 			goto out;
1104 		dp = VTODE(tdvp);
1105 		xp = tvp ? VTODE(tvp) : NULL;
1106 	}
1107 
1108 	if (xp != NULL) {
1109 		/*
1110 		 * Target must be empty if a directory and have no links
1111 		 * to it. Also, ensure source and target are compatible
1112 		 * (both directories, or both not directories).
1113 		 */
1114 		if (xp->de_Attributes & ATTR_DIRECTORY) {
1115 			if (!dosdirempty(xp)) {
1116 				error = ENOTEMPTY;
1117 				goto bad;
1118 			}
1119 			if (!doingdirectory) {
1120 				error = ENOTDIR;
1121 				goto bad;
1122 			}
1123 			cache_purge(tdvp);
1124 		} else if (doingdirectory) {
1125 			error = EISDIR;
1126 			goto bad;
1127 		}
1128 		error = removede(dp, xp);
1129 		if (error)
1130 			goto bad;
1131 		vput(tvp);
1132 		xp = NULL;
1133 	}
1134 
1135 	/*
1136 	 * Convert the filename in tcnp into a dos filename. We copy this
1137 	 * into the denode and directory entry for the destination
1138 	 * file/directory.
1139 	 */
1140 	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1141 	if (error)
1142 		goto abortit;
1143 
1144 	/*
1145 	 * Since from wasn't locked at various places above,
1146 	 * have to do a relookup here.
1147 	 */
1148 	fcnp->cn_flags &= ~MODMASK;
1149 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1150 	if ((fcnp->cn_flags & SAVESTART) == 0)
1151 		panic("msdosfs_rename: lost from startdir");
1152 	if (!newparent)
1153 		VOP_UNLOCK(tdvp, 0);
1154 	if (relookup(fdvp, &fvp, fcnp) == 0)
1155 		vrele(fdvp);
1156 	if (fvp == NULL) {
1157 		/*
1158 		 * From name has disappeared.
1159 		 */
1160 		if (doingdirectory)
1161 			panic("rename: lost dir entry");
1162 		if (newparent)
1163 			VOP_UNLOCK(tdvp, 0);
1164 		vrele(tdvp);
1165 		vrele(ap->a_fvp);
1166 		return 0;
1167 	}
1168 	xp = VTODE(fvp);
1169 	zp = VTODE(fdvp);
1170 	from_diroffset = zp->de_fndoffset;
1171 
1172 	/*
1173 	 * Ensure that the directory entry still exists and has not
1174 	 * changed till now. If the source is a file the entry may
1175 	 * have been unlinked or renamed. In either case there is
1176 	 * no further work to be done. If the source is a directory
1177 	 * then it cannot have been rmdir'ed or renamed; this is
1178 	 * prohibited by the DE_RENAME flag.
1179 	 */
1180 	if (xp != ip) {
1181 		if (doingdirectory)
1182 			panic("rename: lost dir entry");
1183 		VOP_UNLOCK(fvp, 0);
1184 		if (newparent)
1185 			VOP_UNLOCK(fdvp, 0);
1186 		vrele(ap->a_fvp);
1187 		xp = NULL;
1188 	} else {
1189 		vrele(fvp);
1190 		xp = NULL;
1191 
1192 		/*
1193 		 * First write a new entry in the destination
1194 		 * directory and mark the entry in the source directory
1195 		 * as deleted.  Then move the denode to the correct hash
1196 		 * chain for its new location in the filesystem.  And, if
1197 		 * we moved a directory, then update its .. entry to point
1198 		 * to the new parent directory.
1199 		 */
1200 		bcopy(ip->de_Name, oldname, 11);
1201 		bcopy(toname, ip->de_Name, 11);	/* update denode */
1202 		dp->de_fndoffset = to_diroffset;
1203 		dp->de_fndcnt = to_count;
1204 		error = createde(ip, dp, (struct denode **)0, tcnp);
1205 		if (error) {
1206 			bcopy(oldname, ip->de_Name, 11);
1207 			if (newparent)
1208 				VOP_UNLOCK(fdvp, 0);
1209 			VOP_UNLOCK(fvp, 0);
1210 			goto bad;
1211 		}
1212 		ip->de_refcnt++;
1213 		zp->de_fndoffset = from_diroffset;
1214 		error = removede(zp, ip);
1215 		if (error) {
1216 			/* XXX should downgrade to ro here, fs is corrupt */
1217 			if (newparent)
1218 				VOP_UNLOCK(fdvp, 0);
1219 			VOP_UNLOCK(fvp, 0);
1220 			goto bad;
1221 		}
1222 		if (!doingdirectory) {
1223 			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1224 				       &ip->de_dirclust, 0);
1225 			if (error) {
1226 				/* XXX should downgrade to ro here, fs is corrupt */
1227 				if (newparent)
1228 					VOP_UNLOCK(fdvp, 0);
1229 				VOP_UNLOCK(fvp, 0);
1230 				goto bad;
1231 			}
1232 			if (ip->de_dirclust == MSDOSFSROOT)
1233 				ip->de_diroffset = to_diroffset;
1234 			else
1235 				ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1236 		}
1237 		reinsert(ip);
1238 		if (newparent)
1239 			VOP_UNLOCK(fdvp, 0);
1240 	}
1241 
1242 	/*
1243 	 * If we moved a directory to a new parent directory, then we must
1244 	 * fixup the ".." entry in the moved directory.
1245 	 */
1246 	if (doingdirectory && newparent) {
1247 		cn = ip->de_StartCluster;
1248 		if (cn == MSDOSFSROOT) {
1249 			/* this should never happen */
1250 			panic("msdosfs_rename(): updating .. in root directory?");
1251 		} else
1252 			bn = cntobn(pmp, cn);
1253 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1254 			      NOCRED, &bp);
1255 		if (error) {
1256 			/* XXX should downgrade to ro here, fs is corrupt */
1257 			brelse(bp);
1258 			VOP_UNLOCK(fvp, 0);
1259 			goto bad;
1260 		}
1261 		dotdotp = (struct direntry *)bp->b_data + 1;
1262 		putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1263 		if (FAT32(pmp))
1264 			putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1265 		if (fvp->v_mount->mnt_flag & MNT_ASYNC)
1266 			bdwrite(bp);
1267 		else if ((error = bwrite(bp)) != 0) {
1268 			/* XXX should downgrade to ro here, fs is corrupt */
1269 			VOP_UNLOCK(fvp, 0);
1270 			goto bad;
1271 		}
1272 	}
1273 
1274 	VOP_UNLOCK(fvp, 0);
1275 bad:
1276 	if (xp)
1277 		vput(tvp);
1278 	vput(tdvp);
1279 out:
1280 	ip->de_flag &= ~DE_RENAME;
1281 	vrele(fdvp);
1282 	vrele(fvp);
1283 	return (error);
1284 
1285 }
1286 
1287 static struct {
1288 	struct direntry dot;
1289 	struct direntry dotdot;
1290 } dosdirtemplate = {
1291 	{	".       ", "   ",			/* the . entry */
1292 		ATTR_DIRECTORY,				/* file attribute */
1293 		0,					/* reserved */
1294 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1295 		{ 0, 0 },				/* access date */
1296 		{ 0, 0 },				/* high bits of start cluster */
1297 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1298 		{ 0, 0 },				/* startcluster */
1299 		{ 0, 0, 0, 0 }				/* filesize */
1300 	},
1301 	{	"..      ", "   ",			/* the .. entry */
1302 		ATTR_DIRECTORY,				/* file attribute */
1303 		0,					/* reserved */
1304 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1305 		{ 0, 0 },				/* access date */
1306 		{ 0, 0 },				/* high bits of start cluster */
1307 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1308 		{ 0, 0 },				/* startcluster */
1309 		{ 0, 0, 0, 0 }				/* filesize */
1310 	}
1311 };
1312 
1313 static int
1314 msdosfs_mkdir(ap)
1315 	struct vop_mkdir_args /* {
1316 		struct vnode *a_dvp;
1317 		struct vnode **a_vpp;
1318 		struvt componentname *a_cnp;
1319 		struct vattr *a_vap;
1320 	} */ *ap;
1321 {
1322 	struct componentname *cnp = ap->a_cnp;
1323 	struct denode *dep;
1324 	struct denode *pdep = VTODE(ap->a_dvp);
1325 	struct direntry *denp;
1326 	struct msdosfsmount *pmp = pdep->de_pmp;
1327 	struct buf *bp;
1328 	u_long newcluster, pcl;
1329 	int bn;
1330 	int error;
1331 	struct denode ndirent;
1332 	struct timespec ts;
1333 
1334 	/*
1335 	 * If this is the root directory and there is no space left we
1336 	 * can't do anything.  This is because the root directory can not
1337 	 * change size.
1338 	 */
1339 	if (pdep->de_StartCluster == MSDOSFSROOT
1340 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1341 		error = ENOSPC;
1342 		goto bad2;
1343 	}
1344 
1345 	/*
1346 	 * Allocate a cluster to hold the about to be created directory.
1347 	 */
1348 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1349 	if (error)
1350 		goto bad2;
1351 
1352 	bzero(&ndirent, sizeof(ndirent));
1353 	ndirent.de_pmp = pmp;
1354 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1355 	getnanotime(&ts);
1356 	DETIMES(&ndirent, &ts, &ts, &ts);
1357 
1358 	/*
1359 	 * Now fill the cluster with the "." and ".." entries. And write
1360 	 * the cluster to disk.  This way it is there for the parent
1361 	 * directory to be pointing at if there were a crash.
1362 	 */
1363 	bn = cntobn(pmp, newcluster);
1364 	/* always succeeds */
1365 	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1366 	bzero(bp->b_data, pmp->pm_bpcluster);
1367 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1368 	denp = (struct direntry *)bp->b_data;
1369 	putushort(denp[0].deStartCluster, newcluster);
1370 	putushort(denp[0].deCDate, ndirent.de_CDate);
1371 	putushort(denp[0].deCTime, ndirent.de_CTime);
1372 	denp[0].deCHundredth = ndirent.de_CHun;
1373 	putushort(denp[0].deADate, ndirent.de_ADate);
1374 	putushort(denp[0].deMDate, ndirent.de_MDate);
1375 	putushort(denp[0].deMTime, ndirent.de_MTime);
1376 	pcl = pdep->de_StartCluster;
1377 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1378 		pcl = 0;
1379 	putushort(denp[1].deStartCluster, pcl);
1380 	putushort(denp[1].deCDate, ndirent.de_CDate);
1381 	putushort(denp[1].deCTime, ndirent.de_CTime);
1382 	denp[1].deCHundredth = ndirent.de_CHun;
1383 	putushort(denp[1].deADate, ndirent.de_ADate);
1384 	putushort(denp[1].deMDate, ndirent.de_MDate);
1385 	putushort(denp[1].deMTime, ndirent.de_MTime);
1386 	if (FAT32(pmp)) {
1387 		putushort(denp[0].deHighClust, newcluster >> 16);
1388 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1389 	}
1390 
1391 	if (ap->a_dvp->v_mount->mnt_flag & MNT_ASYNC)
1392 		bdwrite(bp);
1393 	else if ((error = bwrite(bp)) != 0)
1394 		goto bad;
1395 
1396 	/*
1397 	 * Now build up a directory entry pointing to the newly allocated
1398 	 * cluster.  This will be written to an empty slot in the parent
1399 	 * directory.
1400 	 */
1401 #ifdef DIAGNOSTIC
1402 	if ((cnp->cn_flags & HASBUF) == 0)
1403 		panic("msdosfs_mkdir: no name");
1404 #endif
1405 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1406 	if (error)
1407 		goto bad;
1408 
1409 	ndirent.de_Attributes = ATTR_DIRECTORY;
1410 	ndirent.de_LowerCase = 0;
1411 	ndirent.de_StartCluster = newcluster;
1412 	ndirent.de_FileSize = 0;
1413 	ndirent.de_dev = pdep->de_dev;
1414 	error = createde(&ndirent, pdep, &dep, cnp);
1415 	if (error)
1416 		goto bad;
1417 	*ap->a_vpp = DETOV(dep);
1418 	return (0);
1419 
1420 bad:
1421 	clusterfree(pmp, newcluster, NULL);
1422 bad2:
1423 	return (error);
1424 }
1425 
1426 static int
1427 msdosfs_rmdir(ap)
1428 	struct vop_rmdir_args /* {
1429 		struct vnode *a_dvp;
1430 		struct vnode *a_vp;
1431 		struct componentname *a_cnp;
1432 	} */ *ap;
1433 {
1434 	struct vnode *vp = ap->a_vp;
1435 	struct vnode *dvp = ap->a_dvp;
1436 	struct componentname *cnp = ap->a_cnp;
1437 	struct denode *ip, *dp;
1438 	struct thread *td = cnp->cn_thread;
1439 	int error;
1440 
1441 	ip = VTODE(vp);
1442 	dp = VTODE(dvp);
1443 
1444 	/*
1445 	 * Verify the directory is empty (and valid).
1446 	 * (Rmdir ".." won't be valid since
1447 	 *  ".." will contain a reference to
1448 	 *  the current directory and thus be
1449 	 *  non-empty.)
1450 	 */
1451 	error = 0;
1452 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1453 		error = ENOTEMPTY;
1454 		goto out;
1455 	}
1456 	/*
1457 	 * Delete the entry from the directory.  For dos filesystems this
1458 	 * gets rid of the directory entry on disk, the in memory copy
1459 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1460 	 * being found by deget().  When the vput() on dep is done we give
1461 	 * up access and eventually msdosfs_reclaim() will be called which
1462 	 * will remove it from the denode cache.
1463 	 */
1464 	error = removede(dp, ip);
1465 	if (error)
1466 		goto out;
1467 	/*
1468 	 * This is where we decrement the link count in the parent
1469 	 * directory.  Since dos filesystems don't do this we just purge
1470 	 * the name cache.
1471 	 */
1472 	cache_purge(dvp);
1473 	VOP_UNLOCK(dvp, 0);
1474 	/*
1475 	 * Truncate the directory that is being deleted.
1476 	 */
1477 	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, td);
1478 	cache_purge(vp);
1479 
1480 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1481 out:
1482 	return (error);
1483 }
1484 
1485 /*
1486  * DOS filesystems don't know what symlinks are.
1487  */
1488 static int
1489 msdosfs_symlink(ap)
1490 	struct vop_symlink_args /* {
1491 		struct vnode *a_dvp;
1492 		struct vnode **a_vpp;
1493 		struct componentname *a_cnp;
1494 		struct vattr *a_vap;
1495 		char *a_target;
1496 	} */ *ap;
1497 {
1498 	return (EOPNOTSUPP);
1499 }
1500 
1501 static int
1502 msdosfs_readdir(ap)
1503 	struct vop_readdir_args /* {
1504 		struct vnode *a_vp;
1505 		struct uio *a_uio;
1506 		struct ucred *a_cred;
1507 		int *a_eofflag;
1508 		int *a_ncookies;
1509 		u_long **a_cookies;
1510 	} */ *ap;
1511 {
1512 	struct mbnambuf nb;
1513 	int error = 0;
1514 	int diff;
1515 	long n;
1516 	int blsize;
1517 	long on;
1518 	u_long cn;
1519 	uint64_t fileno;
1520 	u_long dirsperblk;
1521 	long bias = 0;
1522 	daddr_t bn, lbn;
1523 	struct buf *bp;
1524 	struct denode *dep = VTODE(ap->a_vp);
1525 	struct msdosfsmount *pmp = dep->de_pmp;
1526 	struct direntry *dentp;
1527 	struct dirent dirbuf;
1528 	struct uio *uio = ap->a_uio;
1529 	u_long *cookies = NULL;
1530 	int ncookies = 0;
1531 	off_t offset, off;
1532 	int chksum = -1;
1533 
1534 #ifdef MSDOSFS_DEBUG
1535 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1536 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1537 #endif
1538 
1539 	/*
1540 	 * msdosfs_readdir() won't operate properly on regular files since
1541 	 * it does i/o only with the the filesystem vnode, and hence can
1542 	 * retrieve the wrong block from the buffer cache for a plain file.
1543 	 * So, fail attempts to readdir() on a plain file.
1544 	 */
1545 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1546 		return (ENOTDIR);
1547 
1548 	/*
1549 	 * To be safe, initialize dirbuf
1550 	 */
1551 	bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1552 
1553 	/*
1554 	 * If the user buffer is smaller than the size of one dos directory
1555 	 * entry or the file offset is not a multiple of the size of a
1556 	 * directory entry, then we fail the read.
1557 	 */
1558 	off = offset = uio->uio_offset;
1559 	if (uio->uio_resid < sizeof(struct direntry) ||
1560 	    (offset & (sizeof(struct direntry) - 1)))
1561 		return (EINVAL);
1562 
1563 	if (ap->a_ncookies) {
1564 		ncookies = uio->uio_resid / 16;
1565 		cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1566 		       M_WAITOK);
1567 		*ap->a_cookies = cookies;
1568 		*ap->a_ncookies = ncookies;
1569 	}
1570 
1571 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1572 
1573 	/*
1574 	 * If they are reading from the root directory then, we simulate
1575 	 * the . and .. entries since these don't exist in the root
1576 	 * directory.  We also set the offset bias to make up for having to
1577 	 * simulate these entries. By this I mean that at file offset 64 we
1578 	 * read the first entry in the root directory that lives on disk.
1579 	 */
1580 	if (dep->de_StartCluster == MSDOSFSROOT
1581 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1582 #if 0
1583 		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1584 		    offset);
1585 #endif
1586 		bias = 2 * sizeof(struct direntry);
1587 		if (offset < bias) {
1588 			for (n = (int)offset / sizeof(struct direntry);
1589 			     n < 2; n++) {
1590 				if (FAT32(pmp))
1591 					fileno = (uint64_t)cntobn(pmp,
1592 								 pmp->pm_rootdirblk)
1593 							  * dirsperblk;
1594 				else
1595 					fileno = 1;
1596 				if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1597 					dirbuf.d_fileno =
1598 					    msdosfs_fileno_map(pmp->pm_mountp,
1599 					    fileno);
1600 				} else {
1601 
1602 					dirbuf.d_fileno = (uint32_t)fileno;
1603 				}
1604 				dirbuf.d_type = DT_DIR;
1605 				switch (n) {
1606 				case 0:
1607 					dirbuf.d_namlen = 1;
1608 					strcpy(dirbuf.d_name, ".");
1609 					break;
1610 				case 1:
1611 					dirbuf.d_namlen = 2;
1612 					strcpy(dirbuf.d_name, "..");
1613 					break;
1614 				}
1615 				dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1616 				if (uio->uio_resid < dirbuf.d_reclen)
1617 					goto out;
1618 				error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1619 				if (error)
1620 					goto out;
1621 				offset += sizeof(struct direntry);
1622 				off = offset;
1623 				if (cookies) {
1624 					*cookies++ = offset;
1625 					if (--ncookies <= 0)
1626 						goto out;
1627 				}
1628 			}
1629 		}
1630 	}
1631 
1632 	mbnambuf_init(&nb);
1633 	off = offset;
1634 	while (uio->uio_resid > 0) {
1635 		lbn = de_cluster(pmp, offset - bias);
1636 		on = (offset - bias) & pmp->pm_crbomask;
1637 		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1638 		diff = dep->de_FileSize - (offset - bias);
1639 		if (diff <= 0)
1640 			break;
1641 		n = min(n, diff);
1642 		error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1643 		if (error)
1644 			break;
1645 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1646 		if (error) {
1647 			brelse(bp);
1648 			return (error);
1649 		}
1650 		n = min(n, blsize - bp->b_resid);
1651 		if (n == 0) {
1652 			brelse(bp);
1653 			return (EIO);
1654 		}
1655 
1656 		/*
1657 		 * Convert from dos directory entries to fs-independent
1658 		 * directory entries.
1659 		 */
1660 		for (dentp = (struct direntry *)(bp->b_data + on);
1661 		     (char *)dentp < bp->b_data + on + n;
1662 		     dentp++, offset += sizeof(struct direntry)) {
1663 #if 0
1664 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1665 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1666 #endif
1667 			/*
1668 			 * If this is an unused entry, we can stop.
1669 			 */
1670 			if (dentp->deName[0] == SLOT_EMPTY) {
1671 				brelse(bp);
1672 				goto out;
1673 			}
1674 			/*
1675 			 * Skip deleted entries.
1676 			 */
1677 			if (dentp->deName[0] == SLOT_DELETED) {
1678 				chksum = -1;
1679 				mbnambuf_init(&nb);
1680 				continue;
1681 			}
1682 
1683 			/*
1684 			 * Handle Win95 long directory entries
1685 			 */
1686 			if (dentp->deAttributes == ATTR_WIN95) {
1687 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1688 					continue;
1689 				chksum = win2unixfn(&nb,
1690 				    (struct winentry *)dentp, chksum, pmp);
1691 				continue;
1692 			}
1693 
1694 			/*
1695 			 * Skip volume labels
1696 			 */
1697 			if (dentp->deAttributes & ATTR_VOLUME) {
1698 				chksum = -1;
1699 				mbnambuf_init(&nb);
1700 				continue;
1701 			}
1702 			/*
1703 			 * This computation of d_fileno must match
1704 			 * the computation of va_fileid in
1705 			 * msdosfs_getattr.
1706 			 */
1707 			if (dentp->deAttributes & ATTR_DIRECTORY) {
1708 				fileno = getushort(dentp->deStartCluster);
1709 				if (FAT32(pmp))
1710 					fileno |= getushort(dentp->deHighClust) << 16;
1711 				/* if this is the root directory */
1712 				if (fileno == MSDOSFSROOT)
1713 					if (FAT32(pmp))
1714 						fileno = (uint64_t)cntobn(pmp,
1715 								pmp->pm_rootdirblk)
1716 							 * dirsperblk;
1717 					else
1718 						fileno = 1;
1719 				else
1720 					fileno = (uint64_t)cntobn(pmp, fileno) *
1721 					    dirsperblk;
1722 				dirbuf.d_type = DT_DIR;
1723 			} else {
1724 				fileno = (uoff_t)offset /
1725 				    sizeof(struct direntry);
1726 				dirbuf.d_type = DT_REG;
1727 			}
1728 			if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1729 				dirbuf.d_fileno =
1730 				    msdosfs_fileno_map(pmp->pm_mountp, fileno);
1731 			} else
1732 				dirbuf.d_fileno = (uint32_t)fileno;
1733 
1734 			if (chksum != winChksum(dentp)) {
1735 				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1736 				    (u_char *)dirbuf.d_name,
1737 				    dentp->deLowerCase |
1738 					((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1739 					(LCASE_BASE | LCASE_EXT) : 0),
1740 				    pmp);
1741 				mbnambuf_init(&nb);
1742 			} else
1743 				mbnambuf_flush(&nb, &dirbuf);
1744 			chksum = -1;
1745 			dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1746 			if (uio->uio_resid < dirbuf.d_reclen) {
1747 				brelse(bp);
1748 				goto out;
1749 			}
1750 			error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1751 			if (error) {
1752 				brelse(bp);
1753 				goto out;
1754 			}
1755 			if (cookies) {
1756 				*cookies++ = offset + sizeof(struct direntry);
1757 				if (--ncookies <= 0) {
1758 					brelse(bp);
1759 					goto out;
1760 				}
1761 			}
1762 			off = offset + sizeof(struct direntry);
1763 		}
1764 		brelse(bp);
1765 	}
1766 out:
1767 	/* Subtract unused cookies */
1768 	if (ap->a_ncookies)
1769 		*ap->a_ncookies -= ncookies;
1770 
1771 	uio->uio_offset = off;
1772 
1773 	/*
1774 	 * Set the eofflag (NFS uses it)
1775 	 */
1776 	if (ap->a_eofflag) {
1777 		if (dep->de_FileSize - (offset - bias) <= 0)
1778 			*ap->a_eofflag = 1;
1779 		else
1780 			*ap->a_eofflag = 0;
1781 	}
1782 	return (error);
1783 }
1784 
1785 /*-
1786  * a_vp   - pointer to the file's vnode
1787  * a_bn   - logical block number within the file (cluster number for us)
1788  * a_bop  - where to return the bufobj of the special file containing the fs
1789  * a_bnp  - where to return the "physical" block number corresponding to a_bn
1790  *          (relative to the special file; units are blocks of size DEV_BSIZE)
1791  * a_runp - where to return the "run past" a_bn.  This is the count of logical
1792  *          blocks whose physical blocks (together with a_bn's physical block)
1793  *          are contiguous.
1794  * a_runb - where to return the "run before" a_bn.
1795  */
1796 static int
1797 msdosfs_bmap(ap)
1798 	struct vop_bmap_args /* {
1799 		struct vnode *a_vp;
1800 		daddr_t a_bn;
1801 		struct bufobj **a_bop;
1802 		daddr_t *a_bnp;
1803 		int *a_runp;
1804 		int *a_runb;
1805 	} */ *ap;
1806 {
1807 	struct denode *dep;
1808 	struct mount *mp;
1809 	struct msdosfsmount *pmp;
1810 	struct vnode *vp;
1811 	daddr_t runbn;
1812 	u_long cn;
1813 	int bnpercn, error, maxio, maxrun, run;
1814 
1815 	vp = ap->a_vp;
1816 	dep = VTODE(vp);
1817 	pmp = dep->de_pmp;
1818 	if (ap->a_bop != NULL)
1819 		*ap->a_bop = &pmp->pm_devvp->v_bufobj;
1820 	if (ap->a_bnp == NULL)
1821 		return (0);
1822 	if (ap->a_runp != NULL)
1823 		*ap->a_runp = 0;
1824 	if (ap->a_runb != NULL)
1825 		*ap->a_runb = 0;
1826 	cn = ap->a_bn;
1827 	if (cn != ap->a_bn)
1828 		return (EFBIG);
1829 	error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1830 	if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1831 		return (error);
1832 
1833 	mp = vp->v_mount;
1834 	maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1835 	bnpercn = de_cn2bn(pmp, 1);
1836 	if (ap->a_runp != NULL) {
1837 		maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1838 		for (run = 1; run <= maxrun; run++) {
1839 			if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1840 			    runbn != *ap->a_bnp + run * bnpercn)
1841 				break;
1842 		}
1843 		*ap->a_runp = run - 1;
1844 	}
1845 	if (ap->a_runb != NULL) {
1846 		maxrun = ulmin(maxio - 1, cn);
1847 		for (run = 1; run < maxrun; run++) {
1848 			if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1849 			    runbn != *ap->a_bnp - run * bnpercn)
1850 				break;
1851 		}
1852 		*ap->a_runb = run - 1;
1853 	}
1854 	return (0);
1855 }
1856 
1857 static int
1858 msdosfs_strategy(ap)
1859 	struct vop_strategy_args /* {
1860 		struct vnode *a_vp;
1861 		struct buf *a_bp;
1862 	} */ *ap;
1863 {
1864 	struct buf *bp = ap->a_bp;
1865 	struct denode *dep = VTODE(ap->a_vp);
1866 	struct bufobj *bo;
1867 	int error = 0;
1868 	daddr_t blkno;
1869 
1870 	/*
1871 	 * If we don't already know the filesystem relative block number
1872 	 * then get it using pcbmap().  If pcbmap() returns the block
1873 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1874 	 * don't allow files with holes, so we shouldn't ever see this.
1875 	 */
1876 	if (bp->b_blkno == bp->b_lblkno) {
1877 		error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1878 		bp->b_blkno = blkno;
1879 		if (error) {
1880 			bp->b_error = error;
1881 			bp->b_ioflags |= BIO_ERROR;
1882 			bufdone(bp);
1883 			return (0);
1884 		}
1885 		if ((long)bp->b_blkno == -1)
1886 			vfs_bio_clrbuf(bp);
1887 	}
1888 	if (bp->b_blkno == -1) {
1889 		bufdone(bp);
1890 		return (0);
1891 	}
1892 	/*
1893 	 * Read/write the block from/to the disk that contains the desired
1894 	 * file block.
1895 	 */
1896 	bp->b_iooffset = dbtob(bp->b_blkno);
1897 	bo = dep->de_pmp->pm_bo;
1898 	BO_STRATEGY(bo, bp);
1899 	return (0);
1900 }
1901 
1902 static int
1903 msdosfs_print(ap)
1904 	struct vop_print_args /* {
1905 		struct vnode *vp;
1906 	} */ *ap;
1907 {
1908 	struct denode *dep = VTODE(ap->a_vp);
1909 
1910 	printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1911 	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1912 	printf("on dev %s\n", devtoname(dep->de_dev));
1913 	return (0);
1914 }
1915 
1916 static int
1917 msdosfs_pathconf(ap)
1918 	struct vop_pathconf_args /* {
1919 		struct vnode *a_vp;
1920 		int a_name;
1921 		int *a_retval;
1922 	} */ *ap;
1923 {
1924 	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1925 
1926 	switch (ap->a_name) {
1927 	case _PC_LINK_MAX:
1928 		*ap->a_retval = 1;
1929 		return (0);
1930 	case _PC_NAME_MAX:
1931 		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1932 		return (0);
1933 	case _PC_PATH_MAX:
1934 		*ap->a_retval = PATH_MAX;
1935 		return (0);
1936 	case _PC_CHOWN_RESTRICTED:
1937 		*ap->a_retval = 1;
1938 		return (0);
1939 	case _PC_NO_TRUNC:
1940 		*ap->a_retval = 0;
1941 		return (0);
1942 	default:
1943 		return (EINVAL);
1944 	}
1945 	/* NOTREACHED */
1946 }
1947 
1948 static int
1949 msdosfs_vptofh(ap)
1950 	struct vop_vptofh_args /* {
1951 		struct vnode *a_vp;
1952 		struct fid *a_fhp;
1953 	} */ *ap;
1954 {
1955 	struct denode *dep;
1956 	struct defid *defhp;
1957 
1958 	dep = VTODE(ap->a_vp);
1959 	defhp = (struct defid *)ap->a_fhp;
1960 	defhp->defid_len = sizeof(struct defid);
1961 	defhp->defid_dirclust = dep->de_dirclust;
1962 	defhp->defid_dirofs = dep->de_diroffset;
1963 	/* defhp->defid_gen = dep->de_gen; */
1964 	return (0);
1965 }
1966 
1967 /* Global vfs data structures for msdosfs */
1968 struct vop_vector msdosfs_vnodeops = {
1969 	.vop_default =		&default_vnodeops,
1970 
1971 	.vop_access =		msdosfs_access,
1972 	.vop_bmap =		msdosfs_bmap,
1973 	.vop_cachedlookup =	msdosfs_lookup,
1974 	.vop_open =		msdosfs_open,
1975 	.vop_close =		msdosfs_close,
1976 	.vop_create =		msdosfs_create,
1977 	.vop_fsync =		msdosfs_fsync,
1978 	.vop_getattr =		msdosfs_getattr,
1979 	.vop_inactive =		msdosfs_inactive,
1980 	.vop_link =		msdosfs_link,
1981 	.vop_lookup =		vfs_cache_lookup,
1982 	.vop_mkdir =		msdosfs_mkdir,
1983 	.vop_mknod =		msdosfs_mknod,
1984 	.vop_pathconf =		msdosfs_pathconf,
1985 	.vop_print =		msdosfs_print,
1986 	.vop_read =		msdosfs_read,
1987 	.vop_readdir =		msdosfs_readdir,
1988 	.vop_reclaim =		msdosfs_reclaim,
1989 	.vop_remove =		msdosfs_remove,
1990 	.vop_rename =		msdosfs_rename,
1991 	.vop_rmdir =		msdosfs_rmdir,
1992 	.vop_setattr =		msdosfs_setattr,
1993 	.vop_strategy =		msdosfs_strategy,
1994 	.vop_symlink =		msdosfs_symlink,
1995 	.vop_write =		msdosfs_write,
1996 	.vop_vptofh =		msdosfs_vptofh,
1997 };
1998