xref: /freebsd/sys/fs/msdosfs/msdosfs_vnops.c (revision 687fce03619ba355d08a6a09d9cbd33a794b21d4)
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/namei.h>
54 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
55 #include <sys/kernel.h>
56 #include <sys/stat.h>
57 #include <sys/buf.h>
58 #include <sys/proc.h>
59 #include <sys/mount.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
62 #include <sys/malloc.h>
63 #include <sys/dirent.h>
64 #include <sys/signalvar.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_zone.h>
69 #include <vm/vnode_pager.h>
70 
71 #include <msdosfs/bpb.h>
72 #include <msdosfs/direntry.h>
73 #include <msdosfs/denode.h>
74 #include <msdosfs/msdosfsmount.h>
75 #include <msdosfs/fat.h>
76 
77 /*
78  * Prototypes for MSDOSFS vnode operations
79  */
80 static int msdosfs_create __P((struct vop_create_args *));
81 static int msdosfs_mknod __P((struct vop_mknod_args *));
82 static int msdosfs_close __P((struct vop_close_args *));
83 static int msdosfs_access __P((struct vop_access_args *));
84 static int msdosfs_getattr __P((struct vop_getattr_args *));
85 static int msdosfs_setattr __P((struct vop_setattr_args *));
86 static int msdosfs_read __P((struct vop_read_args *));
87 static int msdosfs_write __P((struct vop_write_args *));
88 static int msdosfs_fsync __P((struct vop_fsync_args *));
89 static int msdosfs_remove __P((struct vop_remove_args *));
90 static int msdosfs_link __P((struct vop_link_args *));
91 static int msdosfs_rename __P((struct vop_rename_args *));
92 static int msdosfs_mkdir __P((struct vop_mkdir_args *));
93 static int msdosfs_rmdir __P((struct vop_rmdir_args *));
94 static int msdosfs_symlink __P((struct vop_symlink_args *));
95 static int msdosfs_readdir __P((struct vop_readdir_args *));
96 static int msdosfs_bmap __P((struct vop_bmap_args *));
97 static int msdosfs_strategy __P((struct vop_strategy_args *));
98 static int msdosfs_print __P((struct vop_print_args *));
99 static int msdosfs_pathconf __P((struct vop_pathconf_args *ap));
100 static int msdosfs_getpages __P((struct vop_getpages_args *));
101 static int msdosfs_putpages __P((struct vop_putpages_args *));
102 
103 /*
104  * Some general notes:
105  *
106  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
107  * read/written using the vnode for the filesystem. Blocks that represent
108  * the contents of a file are read/written using the vnode for the file
109  * (including directories when they are read/written as files). This
110  * presents problems for the dos filesystem because data that should be in
111  * an inode (if dos had them) resides in the directory itself.  Since we
112  * must update directory entries without the benefit of having the vnode
113  * for the directory we must use the vnode for the filesystem.  This means
114  * that when a directory is actually read/written (via read, write, or
115  * readdir, or seek) we must use the vnode for the filesystem instead of
116  * the vnode for the directory as would happen in ufs. This is to insure we
117  * retreive the correct block from the buffer cache since the hash value is
118  * based upon the vnode address and the desired block number.
119  */
120 
121 /*
122  * Create a regular file. On entry the directory to contain the file being
123  * created is locked.  We must release before we return. We must also free
124  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
125  * only if the SAVESTART bit in cn_flags is clear on success.
126  */
127 static int
128 msdosfs_create(ap)
129 	struct vop_create_args /* {
130 		struct vnode *a_dvp;
131 		struct vnode **a_vpp;
132 		struct componentname *a_cnp;
133 		struct vattr *a_vap;
134 	} */ *ap;
135 {
136 	struct componentname *cnp = ap->a_cnp;
137 	struct denode ndirent;
138 	struct denode *dep;
139 	struct denode *pdep = VTODE(ap->a_dvp);
140 	struct timespec ts;
141 	int error;
142 
143 #ifdef MSDOSFS_DEBUG
144 	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
145 #endif
146 
147 	/*
148 	 * If this is the root directory and there is no space left we
149 	 * can't do anything.  This is because the root directory can not
150 	 * change size.
151 	 */
152 	if (pdep->de_StartCluster == MSDOSFSROOT
153 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
154 		error = ENOSPC;
155 		goto bad;
156 	}
157 
158 	/*
159 	 * Create a directory entry for the file, then call createde() to
160 	 * have it installed. NOTE: DOS files are always executable.  We
161 	 * use the absence of the owner write bit to make the file
162 	 * readonly.
163 	 */
164 #ifdef DIAGNOSTIC
165 	if ((cnp->cn_flags & HASBUF) == 0)
166 		panic("msdosfs_create: no name");
167 #endif
168 	bzero(&ndirent, sizeof(ndirent));
169 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
170 	if (error)
171 		goto bad;
172 
173 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
174 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
175 	ndirent.de_LowerCase = 0;
176 	ndirent.de_StartCluster = 0;
177 	ndirent.de_FileSize = 0;
178 	ndirent.de_dev = pdep->de_dev;
179 	ndirent.de_devvp = pdep->de_devvp;
180 	ndirent.de_pmp = pdep->de_pmp;
181 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
182 	getnanotime(&ts);
183 	DETIMES(&ndirent, &ts, &ts, &ts);
184 	error = createde(&ndirent, pdep, &dep, cnp);
185 	if (error)
186 		goto bad;
187 	*ap->a_vpp = DETOV(dep);
188 	return (0);
189 
190 bad:
191 	return (error);
192 }
193 
194 static int
195 msdosfs_mknod(ap)
196 	struct vop_mknod_args /* {
197 		struct vnode *a_dvp;
198 		struct vnode **a_vpp;
199 		struct componentname *a_cnp;
200 		struct vattr *a_vap;
201 	} */ *ap;
202 {
203 
204 	switch (ap->a_vap->va_type) {
205 	case VDIR:
206 		return (msdosfs_mkdir((struct vop_mkdir_args *)ap));
207 		break;
208 
209 	case VREG:
210 		return (msdosfs_create((struct vop_create_args *)ap));
211 		break;
212 
213 	default:
214 		return (EINVAL);
215 	}
216 	/* NOTREACHED */
217 }
218 
219 static int
220 msdosfs_close(ap)
221 	struct vop_close_args /* {
222 		struct vnode *a_vp;
223 		int a_fflag;
224 		struct ucred *a_cred;
225 		struct proc *a_p;
226 	} */ *ap;
227 {
228 	struct vnode *vp = ap->a_vp;
229 	struct denode *dep = VTODE(vp);
230 	struct timespec ts;
231 
232 	simple_lock(&vp->v_interlock);
233 	if (vp->v_usecount > 1) {
234 		getnanotime(&ts);
235 		DETIMES(dep, &ts, &ts, &ts);
236 	}
237 	simple_unlock(&vp->v_interlock);
238 	return 0;
239 }
240 
241 static int
242 msdosfs_access(ap)
243 	struct vop_access_args /* {
244 		struct vnode *a_vp;
245 		int a_mode;
246 		struct ucred *a_cred;
247 		struct proc *a_p;
248 	} */ *ap;
249 {
250 	struct vnode *vp = ap->a_vp;
251 	struct denode *dep = VTODE(ap->a_vp);
252 	struct msdosfsmount *pmp = dep->de_pmp;
253 	struct ucred *cred = ap->a_cred;
254 	mode_t mask, file_mode, mode = ap->a_mode;
255 	register gid_t *gp;
256 	int i;
257 
258 	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
259 	    ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
260 	file_mode &= pmp->pm_mask;
261 
262 	/*
263 	 * Disallow write attempts on read-only file systems;
264 	 * unless the file is a socket, fifo, or a block or
265 	 * character device resident on the file system.
266 	 */
267 	if (mode & VWRITE) {
268 		switch (vp->v_type) {
269 		case VDIR:
270 		case VLNK:
271 		case VREG:
272 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
273 				return (EROFS);
274 			break;
275 		default:
276 			break;
277 		}
278 	}
279 
280 	/* User id 0 always gets access. */
281 	if (cred->cr_uid == 0)
282 		return 0;
283 
284 	mask = 0;
285 
286 	/* Otherwise, check the owner. */
287 	if (cred->cr_uid == pmp->pm_uid) {
288 		if (mode & VEXEC)
289 			mask |= S_IXUSR;
290 		if (mode & VREAD)
291 			mask |= S_IRUSR;
292 		if (mode & VWRITE)
293 			mask |= S_IWUSR;
294 		return (file_mode & mask) == mask ? 0 : EACCES;
295 	}
296 
297 	/* Otherwise, check the groups. */
298 	for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
299 		if (pmp->pm_gid == *gp) {
300 			if (mode & VEXEC)
301 				mask |= S_IXGRP;
302 			if (mode & VREAD)
303 				mask |= S_IRGRP;
304 			if (mode & VWRITE)
305 				mask |= S_IWGRP;
306 			return (file_mode & mask) == mask ? 0 : EACCES;
307 		}
308 
309 	/* Otherwise, check everyone else. */
310 	if (mode & VEXEC)
311 		mask |= S_IXOTH;
312 	if (mode & VREAD)
313 		mask |= S_IROTH;
314 	if (mode & VWRITE)
315 		mask |= S_IWOTH;
316 	return (file_mode & mask) == mask ? 0 : EACCES;
317 }
318 
319 static int
320 msdosfs_getattr(ap)
321 	struct vop_getattr_args /* {
322 		struct vnode *a_vp;
323 		struct vattr *a_vap;
324 		struct ucred *a_cred;
325 		struct proc *a_p;
326 	} */ *ap;
327 {
328 	struct denode *dep = VTODE(ap->a_vp);
329 	struct msdosfsmount *pmp = dep->de_pmp;
330 	struct vattr *vap = ap->a_vap;
331 	mode_t mode;
332 	struct timespec ts;
333 	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
334 	u_long fileid;
335 
336 	getnanotime(&ts);
337 	DETIMES(dep, &ts, &ts, &ts);
338 	vap->va_fsid = dev2udev(dep->de_dev);
339 	/*
340 	 * The following computation of the fileid must be the same as that
341 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
342 	 * doesn't work.
343 	 */
344 	if (dep->de_Attributes & ATTR_DIRECTORY) {
345 		fileid = cntobn(pmp, dep->de_StartCluster) * dirsperblk;
346 		if (dep->de_StartCluster == MSDOSFSROOT)
347 			fileid = 1;
348 	} else {
349 		fileid = cntobn(pmp, dep->de_dirclust) * dirsperblk;
350 		if (dep->de_dirclust == MSDOSFSROOT)
351 			fileid = roottobn(pmp, 0) * dirsperblk;
352 		fileid += dep->de_diroffset / sizeof(struct direntry);
353 	}
354 	vap->va_fileid = fileid;
355 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
356 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
357 	else
358 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
359 	vap->va_mode = mode & pmp->pm_mask;
360 	vap->va_uid = pmp->pm_uid;
361 	vap->va_gid = pmp->pm_gid;
362 	vap->va_nlink = 1;
363 	vap->va_rdev = 0;
364 	vap->va_size = dep->de_FileSize;
365 	dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
366 	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
367 		dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
368 		dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
369 	} else {
370 		vap->va_atime = vap->va_mtime;
371 		vap->va_ctime = vap->va_mtime;
372 	}
373 	vap->va_flags = 0;
374 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
375 		vap->va_flags |= SF_ARCHIVED;
376 	vap->va_gen = 0;
377 	vap->va_blocksize = pmp->pm_bpcluster;
378 	vap->va_bytes =
379 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
380 	vap->va_type = ap->a_vp->v_type;
381 	vap->va_filerev = dep->de_modrev;
382 	return (0);
383 }
384 
385 static int
386 msdosfs_setattr(ap)
387 	struct vop_setattr_args /* {
388 		struct vnode *a_vp;
389 		struct vattr *a_vap;
390 		struct ucred *a_cred;
391 		struct proc *a_p;
392 	} */ *ap;
393 {
394 	struct vnode *vp = ap->a_vp;
395 	struct denode *dep = VTODE(ap->a_vp);
396 	struct msdosfsmount *pmp = dep->de_pmp;
397 	struct vattr *vap = ap->a_vap;
398 	struct ucred *cred = ap->a_cred;
399 	int error = 0;
400 
401 #ifdef MSDOSFS_DEBUG
402 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
403 	    ap->a_vp, vap, cred, ap->a_p);
404 #endif
405 
406 	/*
407 	 * Check for unsettable attributes.
408 	 */
409 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
410 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
411 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
412 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
413 #ifdef MSDOSFS_DEBUG
414 		printf("msdosfs_setattr(): returning EINVAL\n");
415 		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
416 		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
417 		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
418 		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
419 		printf("    va_uid %x, va_gid %x\n",
420 		    vap->va_uid, vap->va_gid);
421 #endif
422 		return (EINVAL);
423 	}
424 	if (vap->va_flags != VNOVAL) {
425 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 			return (EROFS);
427 		if (cred->cr_uid != pmp->pm_uid &&
428 		    (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
429 			return (error);
430 		/*
431 		 * We are very inconsistent about handling unsupported
432 		 * attributes.  We ignored the access time and the
433 		 * read and execute bits.  We were strict for the other
434 		 * attributes.
435 		 *
436 		 * Here we are strict, stricter than ufs in not allowing
437 		 * users to attempt to set SF_SETTABLE bits or anyone to
438 		 * set unsupported bits.  However, we ignore attempts to
439 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
440 		 * sensible file system attempts it a lot.
441 		 */
442 		if (cred->cr_uid != 0) {
443 			if (vap->va_flags & SF_SETTABLE)
444 				return EPERM;
445 		}
446 		if (vap->va_flags & ~SF_ARCHIVED)
447 			return EOPNOTSUPP;
448 		if (vap->va_flags & SF_ARCHIVED)
449 			dep->de_Attributes &= ~ATTR_ARCHIVE;
450 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
451 			dep->de_Attributes |= ATTR_ARCHIVE;
452 		dep->de_flag |= DE_MODIFIED;
453 	}
454 
455 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
456 		uid_t uid;
457 		gid_t gid;
458 
459 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
460 			return (EROFS);
461 		uid = vap->va_uid;
462 		if (uid == (uid_t)VNOVAL)
463 			uid = pmp->pm_uid;
464 		gid = vap->va_gid;
465 		if (gid == (gid_t)VNOVAL)
466 			gid = pmp->pm_gid;
467 		if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
468 		    (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
469 		    (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
470 			return error;
471 		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
472 			return EINVAL;
473 	}
474 
475 	if (vap->va_size != VNOVAL) {
476 		/*
477 		 * Disallow write attempts on read-only file systems;
478 		 * unless the file is a socket, fifo, or a block or
479 		 * character device resident on the file system.
480 		 */
481 		switch (vp->v_type) {
482 		case VDIR:
483 			return (EISDIR);
484 			/* NOT REACHED */
485 		case VLNK:
486 		case VREG:
487 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 				return (EROFS);
489 			break;
490 		default:
491 			break;
492 		}
493 		error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
494 		if (error)
495 			return error;
496 	}
497 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
498 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
499 			return (EROFS);
500 		if (cred->cr_uid != pmp->pm_uid &&
501 		    (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)) &&
502 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
503 		    (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_p))))
504 			return (error);
505 		if (vp->v_type != VDIR) {
506 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
507 			    vap->va_atime.tv_sec != VNOVAL)
508 				unix2dostime(&vap->va_atime, &dep->de_ADate, NULL, NULL);
509 			if (vap->va_mtime.tv_sec != VNOVAL)
510 				unix2dostime(&vap->va_mtime, &dep->de_MDate, &dep->de_MTime, NULL);
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 = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
525 			return (error);
526 		if (vp->v_type != VDIR) {
527 			/* We ignore the read and execute bits. */
528 			if (vap->va_mode & VWRITE)
529 				dep->de_Attributes &= ~ATTR_READONLY;
530 			else
531 				dep->de_Attributes |= ATTR_READONLY;
532 			dep->de_flag |= DE_MODIFIED;
533 		}
534 	}
535 	return (deupdat(dep, 1));
536 }
537 
538 static int
539 msdosfs_read(ap)
540 	struct vop_read_args /* {
541 		struct vnode *a_vp;
542 		struct uio *a_uio;
543 		int a_ioflag;
544 		struct ucred *a_cred;
545 	} */ *ap;
546 {
547 	int error = 0;
548 	off_t diff;
549 	int blsize;
550 	int isadir;
551 	int orig_resid;
552 	long n;
553 	long on;
554 	daddr_t lbn;
555 	daddr_t rablock;
556 	int rasize;
557 	int seqcount;
558 	struct buf *bp;
559 	struct vnode *vp = ap->a_vp;
560 	struct denode *dep = VTODE(vp);
561 	struct msdosfsmount *pmp = dep->de_pmp;
562 	struct uio *uio = ap->a_uio;
563 
564 	if (uio->uio_offset < 0)
565 		return (EINVAL);
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 	seqcount = ap->a_ioflag >> 16;
575 
576 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
577 	do {
578 		if (uio->uio_offset >= dep->de_FileSize)
579 			break;
580 		lbn = de_cluster(pmp, uio->uio_offset);
581 		on = uio->uio_offset & pmp->pm_crbomask;
582 		n = min((u_long) (pmp->pm_bpcluster - on), uio->uio_resid);
583 		diff = dep->de_FileSize - uio->uio_offset;
584 		if (diff < n)
585 			n = diff;
586 		/* convert cluster # to block # if a directory */
587 		if (isadir) {
588 			error = pcbmap(dep, lbn, &lbn, 0, &blsize);
589 			if (error)
590 				break;
591 		}
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 			error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
599 		} else {
600 			rablock = lbn + 1;
601 			if (seqcount > 1 &&
602 			    de_cn2off(pmp, rablock) < dep->de_FileSize) {
603 				rasize = pmp->pm_bpcluster;
604 				error = breadn(vp, lbn, pmp->pm_bpcluster,
605 				    &rablock, &rasize, 1, NOCRED, &bp);
606 			} else {
607 				error = bread(vp, lbn, pmp->pm_bpcluster,
608 				    NOCRED, &bp);
609 			}
610 		}
611 		n = min(n, pmp->pm_bpcluster - bp->b_resid);
612 		if (error) {
613 			brelse(bp);
614 			break;
615 		}
616 		error = uiomove(bp->b_data + on, (int) n, uio);
617 		brelse(bp);
618 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
619 	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
620 	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
621 		dep->de_flag |= DE_ACCESS;
622 	return (error);
623 }
624 
625 /*
626  * Write data to a file or directory.
627  */
628 static int
629 msdosfs_write(ap)
630 	struct vop_write_args /* {
631 		struct vnode *a_vp;
632 		struct uio *a_uio;
633 		int a_ioflag;
634 		struct ucred *a_cred;
635 	} */ *ap;
636 {
637 	int n;
638 	int croffset;
639 	int resid;
640 	u_long osize;
641 	int error = 0;
642 	u_long count;
643 	daddr_t bn, lastcn;
644 	struct buf *bp;
645 	int ioflag = ap->a_ioflag;
646 	struct uio *uio = ap->a_uio;
647 	struct proc *p = uio->uio_procp;
648 	struct vnode *vp = ap->a_vp;
649 	struct vnode *thisvp;
650 	struct denode *dep = VTODE(vp);
651 	struct msdosfsmount *pmp = dep->de_pmp;
652 	struct ucred *cred = ap->a_cred;
653 
654 #ifdef MSDOSFS_DEBUG
655 	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
656 	    vp, uio, ioflag, cred);
657 	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
658 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
659 #endif
660 
661 	switch (vp->v_type) {
662 	case VREG:
663 		if (ioflag & IO_APPEND)
664 			uio->uio_offset = dep->de_FileSize;
665 		thisvp = vp;
666 		break;
667 	case VDIR:
668 		return EISDIR;
669 	default:
670 		panic("msdosfs_write(): bad file type");
671 	}
672 
673 	if (uio->uio_offset < 0)
674 		return (EINVAL);
675 
676 	if (uio->uio_resid == 0)
677 		return (0);
678 
679 	/*
680 	 * If they've exceeded their filesize limit, tell them about it.
681 	 */
682 	if (p &&
683 	    ((uio->uio_offset + uio->uio_resid) >
684 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
685 		psignal(p, SIGXFSZ);
686 		return (EFBIG);
687 	}
688 
689 	/*
690 	 * If the offset we are starting the write at is beyond the end of
691 	 * the file, then they've done a seek.  Unix filesystems allow
692 	 * files with holes in them, DOS doesn't so we must fill the hole
693 	 * with zeroed blocks.
694 	 */
695 	if (uio->uio_offset > dep->de_FileSize) {
696 		error = deextend(dep, uio->uio_offset, cred);
697 		if (error)
698 			return (error);
699 	}
700 
701 	/*
702 	 * Remember some values in case the write fails.
703 	 */
704 	resid = uio->uio_resid;
705 	osize = dep->de_FileSize;
706 
707 	/*
708 	 * If we write beyond the end of the file, extend it to its ultimate
709 	 * size ahead of the time to hopefully get a contiguous area.
710 	 */
711 	if (uio->uio_offset + resid > osize) {
712 		count = de_clcount(pmp, uio->uio_offset + resid) -
713 			de_clcount(pmp, osize);
714 		error = extendfile(dep, count, NULL, NULL, 0);
715 		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
716 			goto errexit;
717 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
718 	} else
719 		lastcn = de_clcount(pmp, osize) - 1;
720 
721 	do {
722 		if (de_cluster(pmp, uio->uio_offset) > lastcn) {
723 			error = ENOSPC;
724 			break;
725 		}
726 
727 		croffset = uio->uio_offset & pmp->pm_crbomask;
728 		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
729 		if (uio->uio_offset + n > dep->de_FileSize) {
730 			dep->de_FileSize = uio->uio_offset + n;
731 			/* The object size needs to be set before buffer is allocated */
732 			vnode_pager_setsize(vp, dep->de_FileSize);
733 		}
734 
735 		bn = de_cluster(pmp, uio->uio_offset);
736 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
737 		    && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
738 		        > de_cluster(pmp, uio->uio_offset)
739 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
740 			/*
741 			 * If either the whole cluster gets written,
742 			 * or we write the cluster from its start beyond EOF,
743 			 * then no need to read data from disk.
744 			 */
745 			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
746 			clrbuf(bp);
747 			/*
748 			 * Do the bmap now, since pcbmap needs buffers
749 			 * for the fat table. (see msdosfs_strategy)
750 			 */
751 			if (bp->b_blkno == bp->b_lblkno) {
752 				error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno,
753 				     0, 0);
754 				if (error)
755 					bp->b_blkno = -1;
756 			}
757 			if (bp->b_blkno == -1) {
758 				brelse(bp);
759 				if (!error)
760 					error = EIO;		/* XXX */
761 				break;
762 			}
763 		} else {
764 			/*
765 			 * The block we need to write into exists, so read it in.
766 			 */
767 			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
768 			if (error) {
769 				brelse(bp);
770 				break;
771 			}
772 		}
773 
774 		/*
775 		 * Should these vnode_pager_* functions be done on dir
776 		 * files?
777 		 */
778 
779 		/*
780 		 * Copy the data from user space into the buf header.
781 		 */
782 		error = uiomove(bp->b_data + croffset, n, uio);
783 		if (error) {
784 			brelse(bp);
785 			break;
786 		}
787 
788 		/*
789 		 * If they want this synchronous then write it and wait for
790 		 * it.  Otherwise, if on a cluster boundary write it
791 		 * asynchronously so we can move on to the next block
792 		 * without delay.  Otherwise do a delayed write because we
793 		 * may want to write somemore into the block later.
794 		 */
795 		if (ioflag & IO_SYNC)
796 			(void) bwrite(bp);
797 		else if (n + croffset == pmp->pm_bpcluster)
798 			bawrite(bp);
799 		else
800 			bdwrite(bp);
801 		dep->de_flag |= DE_UPDATE;
802 	} while (error == 0 && uio->uio_resid > 0);
803 
804 	/*
805 	 * If the write failed and they want us to, truncate the file back
806 	 * to the size it was before the write was attempted.
807 	 */
808 errexit:
809 	if (error) {
810 		if (ioflag & IO_UNIT) {
811 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
812 			uio->uio_offset -= resid - uio->uio_resid;
813 			uio->uio_resid = resid;
814 		} else {
815 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
816 			if (uio->uio_resid != resid)
817 				error = 0;
818 		}
819 	} else if (ioflag & IO_SYNC)
820 		error = deupdat(dep, 1);
821 	return (error);
822 }
823 
824 /*
825  * Flush the blocks of a file to disk.
826  *
827  * This function is worthless for vnodes that represent directories. Maybe we
828  * could just do a sync if they try an fsync on a directory file.
829  */
830 static int
831 msdosfs_fsync(ap)
832 	struct vop_fsync_args /* {
833 		struct vnode *a_vp;
834 		struct ucred *a_cred;
835 		int a_waitfor;
836 		struct proc *a_p;
837 	} */ *ap;
838 {
839 	struct vnode *vp = ap->a_vp;
840 	int s;
841 	struct buf *bp, *nbp;
842 
843 	/*
844 	 * Flush all dirty buffers associated with a vnode.
845 	 */
846 loop:
847 	s = splbio();
848 	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
849 		nbp = TAILQ_NEXT(bp, b_vnbufs);
850 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
851 			continue;
852 		if ((bp->b_flags & B_DELWRI) == 0)
853 			panic("msdosfs_fsync: not dirty");
854 		bremfree(bp);
855 		splx(s);
856 		(void) bwrite(bp);
857 		goto loop;
858 	}
859 	while (vp->v_numoutput) {
860 		vp->v_flag |= VBWAIT;
861 		(void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
862 	}
863 #ifdef DIAGNOSTIC
864 	if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
865 		vprint("msdosfs_fsync: dirty", vp);
866 		goto loop;
867 	}
868 #endif
869 	splx(s);
870 	return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
871 }
872 
873 static int
874 msdosfs_remove(ap)
875 	struct vop_remove_args /* {
876 		struct vnode *a_dvp;
877 		struct vnode *a_vp;
878 		struct componentname *a_cnp;
879 	} */ *ap;
880 {
881 	struct denode *dep = VTODE(ap->a_vp);
882 	struct denode *ddep = VTODE(ap->a_dvp);
883 	int error;
884 
885 	if (ap->a_vp->v_type == VDIR)
886 		error = EPERM;
887 	else
888 		error = removede(ddep, dep);
889 #ifdef MSDOSFS_DEBUG
890 	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
891 #endif
892 	return (error);
893 }
894 
895 /*
896  * DOS filesystems don't know what links are. But since we already called
897  * msdosfs_lookup() with create and lockparent, the parent is locked so we
898  * have to free it before we return the error.
899  */
900 static int
901 msdosfs_link(ap)
902 	struct vop_link_args /* {
903 		struct vnode *a_tdvp;
904 		struct vnode *a_vp;
905 		struct componentname *a_cnp;
906 	} */ *ap;
907 {
908 	return (EOPNOTSUPP);
909 }
910 
911 /*
912  * Renames on files require moving the denode to a new hash queue since the
913  * denode's location is used to compute which hash queue to put the file
914  * in. Unless it is a rename in place.  For example "mv a b".
915  *
916  * What follows is the basic algorithm:
917  *
918  * if (file move) {
919  *	if (dest file exists) {
920  *		remove dest file
921  *	}
922  *	if (dest and src in same directory) {
923  *		rewrite name in existing directory slot
924  *	} else {
925  *		write new entry in dest directory
926  *		update offset and dirclust in denode
927  *		move denode to new hash chain
928  *		clear old directory entry
929  *	}
930  * } else {
931  *	directory move
932  *	if (dest directory exists) {
933  *		if (dest is not empty) {
934  *			return ENOTEMPTY
935  *		}
936  *		remove dest directory
937  *	}
938  *	if (dest and src in same directory) {
939  *		rewrite name in existing entry
940  *	} else {
941  *		be sure dest is not a child of src directory
942  *		write entry in dest directory
943  *		update "." and ".." in moved directory
944  *		clear old directory entry for moved directory
945  *	}
946  * }
947  *
948  * On entry:
949  *	source's parent directory is unlocked
950  *	source file or directory is unlocked
951  *	destination's parent directory is locked
952  *	destination file or directory is locked if it exists
953  *
954  * On exit:
955  *	all denodes should be released
956  *
957  * Notes:
958  * I'm not sure how the memory containing the pathnames pointed at by the
959  * componentname structures is freed, there may be some memory bleeding
960  * for each rename done.
961  */
962 static int
963 msdosfs_rename(ap)
964 	struct vop_rename_args /* {
965 		struct vnode *a_fdvp;
966 		struct vnode *a_fvp;
967 		struct componentname *a_fcnp;
968 		struct vnode *a_tdvp;
969 		struct vnode *a_tvp;
970 		struct componentname *a_tcnp;
971 	} */ *ap;
972 {
973 	struct vnode *tdvp = ap->a_tdvp;
974 	struct vnode *fvp = ap->a_fvp;
975 	struct vnode *fdvp = ap->a_fdvp;
976 	struct vnode *tvp = ap->a_tvp;
977 	struct componentname *tcnp = ap->a_tcnp;
978 	struct componentname *fcnp = ap->a_fcnp;
979 	struct proc *p = fcnp->cn_proc;
980 	struct denode *ip, *xp, *dp, *zp;
981 	u_char toname[11], oldname[11];
982 	u_long from_diroffset, to_diroffset;
983 	u_char to_count;
984 	int doingdirectory = 0, newparent = 0;
985 	int error;
986 	u_long cn;
987 	daddr_t bn;
988 	struct denode *fddep;	/* from file's parent directory	 */
989 	struct denode *fdep;	/* from file or directory	 */
990 	struct denode *tddep;	/* to file's parent directory	 */
991 	struct denode *tdep;	/* to file or directory		 */
992 	struct msdosfsmount *pmp;
993 	struct direntry *dotdotp;
994 	struct buf *bp;
995 
996 	fddep = VTODE(ap->a_fdvp);
997 	fdep = VTODE(ap->a_fvp);
998 	tddep = VTODE(ap->a_tdvp);
999 	tdep = tvp ? VTODE(tvp) : NULL;
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, p);
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, p);
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_proc);
1087 	VOP_UNLOCK(fvp, 0, p);
1088 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1089 		newparent = 1;
1090 	vrele(fdvp);
1091 	if (doingdirectory && newparent) {
1092 		if (error)	/* write access check above */
1093 			goto bad;
1094 		if (xp != NULL)
1095 			vput(tvp);
1096 		/*
1097 		 * doscheckpath() vput()'s dp,
1098 		 * so we have to do a relookup afterwards
1099 		 */
1100 		error = doscheckpath(ip, dp);
1101 		if (error)
1102 			goto out;
1103 		if ((tcnp->cn_flags & SAVESTART) == 0)
1104 			panic("msdosfs_rename: lost to startdir");
1105 		error = relookup(tdvp, &tvp, tcnp);
1106 		if (error)
1107 			goto out;
1108 		dp = VTODE(tdvp);
1109 		xp = tvp ? VTODE(tvp) : NULL;
1110 	}
1111 
1112 	if (xp != NULL) {
1113 		/*
1114 		 * Target must be empty if a directory and have no links
1115 		 * to it. Also, ensure source and target are compatible
1116 		 * (both directories, or both not directories).
1117 		 */
1118 		if (xp->de_Attributes & ATTR_DIRECTORY) {
1119 			if (!dosdirempty(xp)) {
1120 				error = ENOTEMPTY;
1121 				goto bad;
1122 			}
1123 			if (!doingdirectory) {
1124 				error = ENOTDIR;
1125 				goto bad;
1126 			}
1127 			cache_purge(tdvp);
1128 		} else if (doingdirectory) {
1129 			error = EISDIR;
1130 			goto bad;
1131 		}
1132 		error = removede(dp, xp);
1133 		if (error)
1134 			goto bad;
1135 		vput(tvp);
1136 		xp = NULL;
1137 	}
1138 
1139 	/*
1140 	 * Convert the filename in tcnp into a dos filename. We copy this
1141 	 * into the denode and directory entry for the destination
1142 	 * file/directory.
1143 	 */
1144 	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1145 	if (error)
1146 		goto abortit;
1147 
1148 	/*
1149 	 * Since from wasn't locked at various places above,
1150 	 * have to do a relookup here.
1151 	 */
1152 	fcnp->cn_flags &= ~MODMASK;
1153 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1154 	if ((fcnp->cn_flags & SAVESTART) == 0)
1155 		panic("msdosfs_rename: lost from startdir");
1156 	if (!newparent)
1157 		VOP_UNLOCK(tdvp, 0, p);
1158 	(void) relookup(fdvp, &fvp, fcnp);
1159 	if (fvp == NULL) {
1160 		/*
1161 		 * From name has disappeared.
1162 		 */
1163 		if (doingdirectory)
1164 			panic("rename: lost dir entry");
1165 		vrele(ap->a_fvp);
1166 		if (newparent)
1167 			VOP_UNLOCK(tdvp, 0, p);
1168 		vrele(tdvp);
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 		vrele(ap->a_fvp);
1187 		VOP_UNLOCK(fvp, 0, p);
1188 		if (newparent)
1189 			VOP_UNLOCK(fdvp, 0, p);
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, p);
1212 			VOP_UNLOCK(fvp, 0, p);
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 really panic here, fs is corrupt */
1220 			if (newparent)
1221 				VOP_UNLOCK(fdvp, 0, p);
1222 			VOP_UNLOCK(fvp, 0, p);
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 really panic here, fs is corrupt */
1230 				if (newparent)
1231 					VOP_UNLOCK(fdvp, 0, p);
1232 				VOP_UNLOCK(fvp, 0, p);
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, p);
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 really panic here, fs is corrupt */
1260 			brelse(bp);
1261 			VOP_UNLOCK(fvp, 0, p);
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 		error = bwrite(bp);
1269 		if (error) {
1270 			/* XXX should really panic here, fs is corrupt */
1271 			VOP_UNLOCK(fvp, 0, p);
1272 			goto bad;
1273 		}
1274 	}
1275 
1276 	VOP_UNLOCK(fvp, 0, p);
1277 bad:
1278 	if (xp)
1279 		vput(tvp);
1280 	vput(tdvp);
1281 out:
1282 	ip->de_flag &= ~DE_RENAME;
1283 	vrele(fdvp);
1284 	vrele(fvp);
1285 	return (error);
1286 
1287 }
1288 
1289 static struct {
1290 	struct direntry dot;
1291 	struct direntry dotdot;
1292 } dosdirtemplate = {
1293 	{	".       ", "   ",			/* the . entry */
1294 		ATTR_DIRECTORY,				/* file attribute */
1295 		0,	 				/* reserved */
1296 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1297 		{ 0, 0 },				/* access date */
1298 		{ 0, 0 },				/* high bits of start cluster */
1299 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1300 		{ 0, 0 },				/* startcluster */
1301 		{ 0, 0, 0, 0 } 				/* filesize */
1302 	},
1303 	{	"..      ", "   ",			/* the .. entry */
1304 		ATTR_DIRECTORY,				/* file attribute */
1305 		0,	 				/* reserved */
1306 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1307 		{ 0, 0 },				/* access date */
1308 		{ 0, 0 },				/* high bits of start cluster */
1309 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1310 		{ 0, 0 },				/* startcluster */
1311 		{ 0, 0, 0, 0 }				/* filesize */
1312 	}
1313 };
1314 
1315 static int
1316 msdosfs_mkdir(ap)
1317 	struct vop_mkdir_args /* {
1318 		struct vnode *a_dvp;
1319 		struvt vnode **a_vpp;
1320 		struvt componentname *a_cnp;
1321 		struct vattr *a_vap;
1322 	} */ *ap;
1323 {
1324 	struct componentname *cnp = ap->a_cnp;
1325 	struct denode *dep;
1326 	struct denode *pdep = VTODE(ap->a_dvp);
1327 	struct direntry *denp;
1328 	struct msdosfsmount *pmp = pdep->de_pmp;
1329 	struct buf *bp;
1330 	u_long newcluster, pcl;
1331 	int bn;
1332 	int error;
1333 	struct denode ndirent;
1334 	struct timespec ts;
1335 
1336 	/*
1337 	 * If this is the root directory and there is no space left we
1338 	 * can't do anything.  This is because the root directory can not
1339 	 * change size.
1340 	 */
1341 	if (pdep->de_StartCluster == MSDOSFSROOT
1342 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1343 		error = ENOSPC;
1344 		goto bad2;
1345 	}
1346 
1347 	/*
1348 	 * Allocate a cluster to hold the about to be created directory.
1349 	 */
1350 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1351 	if (error)
1352 		goto bad2;
1353 
1354 	bzero(&ndirent, sizeof(ndirent));
1355 	ndirent.de_pmp = pmp;
1356 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1357 	getnanotime(&ts);
1358 	DETIMES(&ndirent, &ts, &ts, &ts);
1359 
1360 	/*
1361 	 * Now fill the cluster with the "." and ".." entries. And write
1362 	 * the cluster to disk.  This way it is there for the parent
1363 	 * directory to be pointing at if there were a crash.
1364 	 */
1365 	bn = cntobn(pmp, newcluster);
1366 	/* always succeeds */
1367 	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1368 	bzero(bp->b_data, pmp->pm_bpcluster);
1369 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1370 	denp = (struct direntry *)bp->b_data;
1371 	putushort(denp[0].deStartCluster, newcluster);
1372 	putushort(denp[0].deCDate, ndirent.de_CDate);
1373 	putushort(denp[0].deCTime, ndirent.de_CTime);
1374 	denp[0].deCHundredth = ndirent.de_CHun;
1375 	putushort(denp[0].deADate, ndirent.de_ADate);
1376 	putushort(denp[0].deMDate, ndirent.de_MDate);
1377 	putushort(denp[0].deMTime, ndirent.de_MTime);
1378 	pcl = pdep->de_StartCluster;
1379 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1380 		pcl = 0;
1381 	putushort(denp[1].deStartCluster, pcl);
1382 	putushort(denp[1].deCDate, ndirent.de_CDate);
1383 	putushort(denp[1].deCTime, ndirent.de_CTime);
1384 	denp[1].deCHundredth = ndirent.de_CHun;
1385 	putushort(denp[1].deADate, ndirent.de_ADate);
1386 	putushort(denp[1].deMDate, ndirent.de_MDate);
1387 	putushort(denp[1].deMTime, ndirent.de_MTime);
1388 	if (FAT32(pmp)) {
1389 		putushort(denp[0].deHighClust, newcluster >> 16);
1390 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1391 	}
1392 
1393 	error = bwrite(bp);
1394 	if (error)
1395 		goto bad;
1396 
1397 	/*
1398 	 * Now build up a directory entry pointing to the newly allocated
1399 	 * cluster.  This will be written to an empty slot in the parent
1400 	 * directory.
1401 	 */
1402 #ifdef DIAGNOSTIC
1403 	if ((cnp->cn_flags & HASBUF) == 0)
1404 		panic("msdosfs_mkdir: no name");
1405 #endif
1406 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1407 	if (error)
1408 		goto bad;
1409 
1410 	ndirent.de_Attributes = ATTR_DIRECTORY;
1411 	ndirent.de_LowerCase = 0;
1412 	ndirent.de_StartCluster = newcluster;
1413 	ndirent.de_FileSize = 0;
1414 	ndirent.de_dev = pdep->de_dev;
1415 	ndirent.de_devvp = pdep->de_devvp;
1416 	error = createde(&ndirent, pdep, &dep, cnp);
1417 	if (error)
1418 		goto bad;
1419 	*ap->a_vpp = DETOV(dep);
1420 	return (0);
1421 
1422 bad:
1423 	clusterfree(pmp, newcluster, NULL);
1424 bad2:
1425 	return (error);
1426 }
1427 
1428 static int
1429 msdosfs_rmdir(ap)
1430 	struct vop_rmdir_args /* {
1431 		struct vnode *a_dvp;
1432 		struct vnode *a_vp;
1433 		struct componentname *a_cnp;
1434 	} */ *ap;
1435 {
1436 	register struct vnode *vp = ap->a_vp;
1437 	register struct vnode *dvp = ap->a_dvp;
1438 	register struct componentname *cnp = ap->a_cnp;
1439 	register struct denode *ip, *dp;
1440 	struct proc *p = cnp->cn_proc;
1441 	int error;
1442 
1443 	ip = VTODE(vp);
1444 	dp = VTODE(dvp);
1445 
1446 	/*
1447 	 * Verify the directory is empty (and valid).
1448 	 * (Rmdir ".." won't be valid since
1449 	 *  ".." will contain a reference to
1450 	 *  the current directory and thus be
1451 	 *  non-empty.)
1452 	 */
1453 	error = 0;
1454 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1455 		error = ENOTEMPTY;
1456 		goto out;
1457 	}
1458 	/*
1459 	 * Delete the entry from the directory.  For dos filesystems this
1460 	 * gets rid of the directory entry on disk, the in memory copy
1461 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1462 	 * being found by deget().  When the vput() on dep is done we give
1463 	 * up access and eventually msdosfs_reclaim() will be called which
1464 	 * will remove it from the denode cache.
1465 	 */
1466 	error = removede(dp, ip);
1467 	if (error)
1468 		goto out;
1469 	/*
1470 	 * This is where we decrement the link count in the parent
1471 	 * directory.  Since dos filesystems don't do this we just purge
1472 	 * the name cache.
1473 	 */
1474 	cache_purge(dvp);
1475 	VOP_UNLOCK(dvp, 0, p);
1476 	/*
1477 	 * Truncate the directory that is being deleted.
1478 	 */
1479 	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, p);
1480 	cache_purge(vp);
1481 
1482 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
1483 out:
1484 	return (error);
1485 }
1486 
1487 /*
1488  * DOS filesystems don't know what symlinks are.
1489  */
1490 static int
1491 msdosfs_symlink(ap)
1492 	struct vop_symlink_args /* {
1493 		struct vnode *a_dvp;
1494 		struct vnode **a_vpp;
1495 		struct componentname *a_cnp;
1496 		struct vattr *a_vap;
1497 		char *a_target;
1498 	} */ *ap;
1499 {
1500 	return (EOPNOTSUPP);
1501 }
1502 
1503 static int
1504 msdosfs_readdir(ap)
1505 	struct vop_readdir_args /* {
1506 		struct vnode *a_vp;
1507 		struct uio *a_uio;
1508 		struct ucred *a_cred;
1509 		int *a_eofflag;
1510 		int *a_ncookies;
1511 		u_long **a_cookies;
1512 	} */ *ap;
1513 {
1514 	int error = 0;
1515 	int diff;
1516 	long n;
1517 	int blsize;
1518 	long on;
1519 	u_long cn;
1520 	u_long fileno;
1521 	u_long dirsperblk;
1522 	long bias = 0;
1523 	daddr_t bn, lbn;
1524 	struct buf *bp;
1525 	struct denode *dep = VTODE(ap->a_vp);
1526 	struct msdosfsmount *pmp = dep->de_pmp;
1527 	struct direntry *dentp;
1528 	struct dirent dirbuf;
1529 	struct uio *uio = ap->a_uio;
1530 	u_long *cookies = NULL;
1531 	int ncookies = 0;
1532 	off_t offset, off;
1533 	int chksum = -1;
1534 
1535 #ifdef MSDOSFS_DEBUG
1536 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1537 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1538 #endif
1539 
1540 	/*
1541 	 * msdosfs_readdir() won't operate properly on regular files since
1542 	 * it does i/o only with the the filesystem vnode, and hence can
1543 	 * retrieve the wrong block from the buffer cache for a plain file.
1544 	 * So, fail attempts to readdir() on a plain file.
1545 	 */
1546 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1547 		return (ENOTDIR);
1548 
1549 	/*
1550 	 * To be safe, initialize dirbuf
1551 	 */
1552 	bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1553 
1554 	/*
1555 	 * If the user buffer is smaller than the size of one dos directory
1556 	 * entry or the file offset is not a multiple of the size of a
1557 	 * directory entry, then we fail the read.
1558 	 */
1559 	off = offset = uio->uio_offset;
1560 	if (uio->uio_resid < sizeof(struct direntry) ||
1561 	    (offset & (sizeof(struct direntry) - 1)))
1562 		return (EINVAL);
1563 
1564 	if (ap->a_ncookies) {
1565 		ncookies = uio->uio_resid / 16;
1566 		MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1567 		       M_WAITOK);
1568 		*ap->a_cookies = cookies;
1569 		*ap->a_ncookies = ncookies;
1570 	}
1571 
1572 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1573 
1574 	/*
1575 	 * If they are reading from the root directory then, we simulate
1576 	 * the . and .. entries since these don't exist in the root
1577 	 * directory.  We also set the offset bias to make up for having to
1578 	 * simulate these entries. By this I mean that at file offset 64 we
1579 	 * read the first entry in the root directory that lives on disk.
1580 	 */
1581 	if (dep->de_StartCluster == MSDOSFSROOT
1582 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1583 #if 0
1584 		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1585 		    offset);
1586 #endif
1587 		bias = 2 * sizeof(struct direntry);
1588 		if (offset < bias) {
1589 			for (n = (int)offset / sizeof(struct direntry);
1590 			     n < 2; n++) {
1591 				if (FAT32(pmp))
1592 					dirbuf.d_fileno = cntobn(pmp,
1593 								 pmp->pm_rootdirblk)
1594 							  * dirsperblk;
1595 				else
1596 					dirbuf.d_fileno = 1;
1597 				dirbuf.d_type = DT_DIR;
1598 				switch (n) {
1599 				case 0:
1600 					dirbuf.d_namlen = 1;
1601 					strcpy(dirbuf.d_name, ".");
1602 					break;
1603 				case 1:
1604 					dirbuf.d_namlen = 2;
1605 					strcpy(dirbuf.d_name, "..");
1606 					break;
1607 				}
1608 				dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1609 				if (uio->uio_resid < dirbuf.d_reclen)
1610 					goto out;
1611 				error = uiomove((caddr_t) &dirbuf,
1612 						dirbuf.d_reclen, uio);
1613 				if (error)
1614 					goto out;
1615 				offset += sizeof(struct direntry);
1616 				off = offset;
1617 				if (cookies) {
1618 					*cookies++ = offset;
1619 					if (--ncookies <= 0)
1620 						goto out;
1621 				}
1622 			}
1623 		}
1624 	}
1625 
1626 	off = offset;
1627 	while (uio->uio_resid > 0) {
1628 		lbn = de_cluster(pmp, offset - bias);
1629 		on = (offset - bias) & pmp->pm_crbomask;
1630 		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1631 		diff = dep->de_FileSize - (offset - bias);
1632 		if (diff <= 0)
1633 			break;
1634 		n = min(n, diff);
1635 		error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1636 		if (error)
1637 			break;
1638 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1639 		if (error) {
1640 			brelse(bp);
1641 			return (error);
1642 		}
1643 		n = min(n, blsize - bp->b_resid);
1644 
1645 		/*
1646 		 * Convert from dos directory entries to fs-independent
1647 		 * directory entries.
1648 		 */
1649 		for (dentp = (struct direntry *)(bp->b_data + on);
1650 		     (char *)dentp < bp->b_data + on + n;
1651 		     dentp++, offset += sizeof(struct direntry)) {
1652 #if 0
1653 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1654 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1655 #endif
1656 			/*
1657 			 * If this is an unused entry, we can stop.
1658 			 */
1659 			if (dentp->deName[0] == SLOT_EMPTY) {
1660 				brelse(bp);
1661 				goto out;
1662 			}
1663 			/*
1664 			 * Skip deleted entries.
1665 			 */
1666 			if (dentp->deName[0] == SLOT_DELETED) {
1667 				chksum = -1;
1668 				continue;
1669 			}
1670 
1671 			/*
1672 			 * Handle Win95 long directory entries
1673 			 */
1674 			if (dentp->deAttributes == ATTR_WIN95) {
1675 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1676 					continue;
1677 				chksum = win2unixfn((struct winentry *)dentp,
1678 					&dirbuf, chksum,
1679 					pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1680 					pmp->pm_u2w);
1681 				continue;
1682 			}
1683 
1684 			/*
1685 			 * Skip volume labels
1686 			 */
1687 			if (dentp->deAttributes & ATTR_VOLUME) {
1688 				chksum = -1;
1689 				continue;
1690 			}
1691 			/*
1692 			 * This computation of d_fileno must match
1693 			 * the computation of va_fileid in
1694 			 * msdosfs_getattr.
1695 			 */
1696 			if (dentp->deAttributes & ATTR_DIRECTORY) {
1697 				fileno = getushort(dentp->deStartCluster);
1698 				if (FAT32(pmp))
1699 					fileno |= getushort(dentp->deHighClust) << 16;
1700 				/* if this is the root directory */
1701 				if (fileno == MSDOSFSROOT)
1702 					if (FAT32(pmp))
1703 						fileno = cntobn(pmp,
1704 								pmp->pm_rootdirblk)
1705 							 * dirsperblk;
1706 					else
1707 						fileno = 1;
1708 				else
1709 					fileno = cntobn(pmp, fileno) * dirsperblk;
1710 				dirbuf.d_fileno = fileno;
1711 				dirbuf.d_type = DT_DIR;
1712 			} else {
1713 				dirbuf.d_fileno = offset / sizeof(struct direntry);
1714 				dirbuf.d_type = DT_REG;
1715 			}
1716 			if (chksum != winChksum(dentp->deName))
1717 				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1718 				    (u_char *)dirbuf.d_name,
1719 				    dentp->deLowerCase |
1720 					((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1721 					(LCASE_BASE | LCASE_EXT) : 0),
1722 				    pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1723 				    pmp->pm_d2u,
1724 				    pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1725 				    pmp->pm_ul);
1726 			else
1727 				dirbuf.d_name[dirbuf.d_namlen] = 0;
1728 			chksum = -1;
1729 			dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1730 			if (uio->uio_resid < dirbuf.d_reclen) {
1731 				brelse(bp);
1732 				goto out;
1733 			}
1734 			error = uiomove((caddr_t) &dirbuf,
1735 					dirbuf.d_reclen, uio);
1736 			if (error) {
1737 				brelse(bp);
1738 				goto out;
1739 			}
1740 			if (cookies) {
1741 				*cookies++ = offset + sizeof(struct direntry);
1742 				if (--ncookies <= 0) {
1743 					brelse(bp);
1744 					goto out;
1745 				}
1746 			}
1747 			off = offset + sizeof(struct direntry);
1748 		}
1749 		brelse(bp);
1750 	}
1751 out:
1752 	/* Subtract unused cookies */
1753 	if (ap->a_ncookies)
1754 		*ap->a_ncookies -= ncookies;
1755 
1756 	uio->uio_offset = off;
1757 
1758 	/*
1759 	 * Set the eofflag (NFS uses it)
1760 	 */
1761 	if (ap->a_eofflag) {
1762 		if (dep->de_FileSize - (offset - bias) <= 0)
1763 			*ap->a_eofflag = 1;
1764 		else
1765 			*ap->a_eofflag = 0;
1766 	}
1767 	return (error);
1768 }
1769 
1770 /*
1771  * vp  - address of vnode file the file
1772  * bn  - which cluster we are interested in mapping to a filesystem block number.
1773  * vpp - returns the vnode for the block special file holding the filesystem
1774  *	 containing the file of interest
1775  * bnp - address of where to return the filesystem relative block number
1776  */
1777 static int
1778 msdosfs_bmap(ap)
1779 	struct vop_bmap_args /* {
1780 		struct vnode *a_vp;
1781 		daddr_t a_bn;
1782 		struct vnode **a_vpp;
1783 		daddr_t *a_bnp;
1784 		int *a_runp;
1785 		int *a_runb;
1786 	} */ *ap;
1787 {
1788 	struct denode *dep = VTODE(ap->a_vp);
1789 
1790 	if (ap->a_vpp != NULL)
1791 		*ap->a_vpp = dep->de_devvp;
1792 	if (ap->a_bnp == NULL)
1793 		return (0);
1794 	if (ap->a_runp) {
1795 		/*
1796 		 * Sequential clusters should be counted here.
1797 		 */
1798 		*ap->a_runp = 0;
1799 	}
1800 	if (ap->a_runb) {
1801 		*ap->a_runb = 0;
1802 	}
1803 	return (pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0));
1804 }
1805 
1806 static int
1807 msdosfs_strategy(ap)
1808 	struct vop_strategy_args /* {
1809 		struct vnode *a_vp;
1810 		struct buf *a_bp;
1811 	} */ *ap;
1812 {
1813 	struct buf *bp = ap->a_bp;
1814 	struct denode *dep = VTODE(bp->b_vp);
1815 	struct vnode *vp;
1816 	int error = 0;
1817 
1818 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1819 		panic("msdosfs_strategy: spec");
1820 	/*
1821 	 * If we don't already know the filesystem relative block number
1822 	 * then get it using pcbmap().  If pcbmap() returns the block
1823 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1824 	 * don't allow files with holes, so we shouldn't ever see this.
1825 	 */
1826 	if (bp->b_blkno == bp->b_lblkno) {
1827 		error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1828 		if (error) {
1829 			bp->b_error = error;
1830 			bp->b_flags |= B_ERROR;
1831 			biodone(bp);
1832 			return (error);
1833 		}
1834 		if ((long)bp->b_blkno == -1)
1835 			vfs_bio_clrbuf(bp);
1836 	}
1837 	if (bp->b_blkno == -1) {
1838 		biodone(bp);
1839 		return (0);
1840 	}
1841 	/*
1842 	 * Read/write the block from/to the disk that contains the desired
1843 	 * file block.
1844 	 */
1845 	vp = dep->de_devvp;
1846 	bp->b_dev = vp->v_rdev;
1847 	VOP_STRATEGY(vp, bp);
1848 	return (0);
1849 }
1850 
1851 static int
1852 msdosfs_print(ap)
1853 	struct vop_print_args /* {
1854 		struct vnode *vp;
1855 	} */ *ap;
1856 {
1857 	struct denode *dep = VTODE(ap->a_vp);
1858 
1859 	printf(
1860 	    "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1861 	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1862 	printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1863 	lockmgr_printinfo(&dep->de_lock);
1864 	printf("\n");
1865 	return (0);
1866 }
1867 
1868 static int
1869 msdosfs_pathconf(ap)
1870 	struct vop_pathconf_args /* {
1871 		struct vnode *a_vp;
1872 		int a_name;
1873 		int *a_retval;
1874 	} */ *ap;
1875 {
1876 	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1877 
1878 	switch (ap->a_name) {
1879 	case _PC_LINK_MAX:
1880 		*ap->a_retval = 1;
1881 		return (0);
1882 	case _PC_NAME_MAX:
1883 		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1884 		return (0);
1885 	case _PC_PATH_MAX:
1886 		*ap->a_retval = PATH_MAX;
1887 		return (0);
1888 	case _PC_CHOWN_RESTRICTED:
1889 		*ap->a_retval = 1;
1890 		return (0);
1891 	case _PC_NO_TRUNC:
1892 		*ap->a_retval = 0;
1893 		return (0);
1894 	default:
1895 		return (EINVAL);
1896 	}
1897 	/* NOTREACHED */
1898 }
1899 
1900 /*
1901  * get page routine
1902  *
1903  * XXX By default, wimp out... note that a_offset is ignored (and always
1904  * XXX has been).
1905  */
1906 int
1907 msdosfs_getpages(ap)
1908 	struct vop_getpages_args *ap;
1909 {
1910 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1911 		ap->a_reqpage);
1912 }
1913 
1914 /*
1915  * put page routine
1916  *
1917  * XXX By default, wimp out... note that a_offset is ignored (and always
1918  * XXX has been).
1919  */
1920 int
1921 msdosfs_putpages(ap)
1922 	struct vop_putpages_args *ap;
1923 {
1924 	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1925 		ap->a_sync, ap->a_rtvals);
1926 }
1927 
1928 /* Global vfs data structures for msdosfs */
1929 vop_t **msdosfs_vnodeop_p;
1930 static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1931 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1932 	{ &vop_access_desc,		(vop_t *) msdosfs_access },
1933 	{ &vop_bmap_desc,		(vop_t *) msdosfs_bmap },
1934 	{ &vop_cachedlookup_desc,	(vop_t *) msdosfs_lookup },
1935 	{ &vop_close_desc,		(vop_t *) msdosfs_close },
1936 	{ &vop_create_desc,		(vop_t *) msdosfs_create },
1937 	{ &vop_fsync_desc,		(vop_t *) msdosfs_fsync },
1938 	{ &vop_getattr_desc,		(vop_t *) msdosfs_getattr },
1939 	{ &vop_inactive_desc,		(vop_t *) msdosfs_inactive },
1940 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
1941 	{ &vop_link_desc,		(vop_t *) msdosfs_link },
1942 	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
1943 	{ &vop_lookup_desc,		(vop_t *) vfs_cache_lookup },
1944 	{ &vop_mkdir_desc,		(vop_t *) msdosfs_mkdir },
1945 	{ &vop_mknod_desc,		(vop_t *) msdosfs_mknod },
1946 	{ &vop_pathconf_desc,		(vop_t *) msdosfs_pathconf },
1947 	{ &vop_print_desc,		(vop_t *) msdosfs_print },
1948 	{ &vop_read_desc,		(vop_t *) msdosfs_read },
1949 	{ &vop_readdir_desc,		(vop_t *) msdosfs_readdir },
1950 	{ &vop_reclaim_desc,		(vop_t *) msdosfs_reclaim },
1951 	{ &vop_remove_desc,		(vop_t *) msdosfs_remove },
1952 	{ &vop_rename_desc,		(vop_t *) msdosfs_rename },
1953 	{ &vop_rmdir_desc,		(vop_t *) msdosfs_rmdir },
1954 	{ &vop_setattr_desc,		(vop_t *) msdosfs_setattr },
1955 	{ &vop_strategy_desc,		(vop_t *) msdosfs_strategy },
1956 	{ &vop_symlink_desc,		(vop_t *) msdosfs_symlink },
1957 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
1958 	{ &vop_write_desc,		(vop_t *) msdosfs_write },
1959 	{ &vop_getpages_desc,		(vop_t *) msdosfs_getpages },
1960 	{ &vop_putpages_desc,		(vop_t *) msdosfs_putpages },
1961 	{ NULL, NULL }
1962 };
1963 static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1964 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1965 
1966 VNODEOP_SET(msdosfs_vnodeop_opv_desc);
1967