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