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