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