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