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