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