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