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