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