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