xref: /freebsd/sys/fs/msdosfs/msdosfs_vnops.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*	$Id: msdosfs_vnops.c,v 1.33 1996/09/19 18:20:47 nate Exp $ */
2 /*	$NetBSD: msdosfs_vnops.c,v 1.20 1994/08/21 18:44:13 ws Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994 Wolfgang Solfrank.
6  * Copyright (C) 1994 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/dir.h>		/* defines dirent structure */
65 #include <sys/signalvar.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_extern.h>
70 
71 #include <msdosfs/bpb.h>
72 #include <msdosfs/direntry.h>
73 #include <msdosfs/denode.h>
74 #include <msdosfs/msdosfsmount.h>
75 #include <msdosfs/fat.h>
76 
77 /*
78  * Prototypes for MSDOSFS vnode operations
79  */
80 static int msdosfs_create __P((struct vop_create_args *));
81 static int msdosfs_mknod __P((struct vop_mknod_args *));
82 static int msdosfs_open __P((struct vop_open_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_ioctl __P((struct vop_ioctl_args *));
90 static int msdosfs_select __P((struct vop_select_args *));
91 static int msdosfs_mmap __P((struct vop_mmap_args *));
92 static int msdosfs_fsync __P((struct vop_fsync_args *));
93 static int msdosfs_seek __P((struct vop_seek_args *));
94 static int msdosfs_remove __P((struct vop_remove_args *));
95 static int msdosfs_link __P((struct vop_link_args *));
96 static int msdosfs_rename __P((struct vop_rename_args *));
97 static int msdosfs_mkdir __P((struct vop_mkdir_args *));
98 static int msdosfs_rmdir __P((struct vop_rmdir_args *));
99 static int msdosfs_symlink __P((struct vop_symlink_args *));
100 static int msdosfs_readdir __P((struct vop_readdir_args *));
101 static int msdosfs_readlink __P((struct vop_readlink_args *));
102 static int msdosfs_abortop __P((struct vop_abortop_args *));
103 static int msdosfs_lock __P((struct vop_lock_args *));
104 static int msdosfs_unlock __P((struct vop_unlock_args *));
105 static int msdosfs_bmap __P((struct vop_bmap_args *));
106 static int msdosfs_strategy __P((struct vop_strategy_args *));
107 static int msdosfs_print __P((struct vop_print_args *));
108 static int msdosfs_islocked __P((struct vop_islocked_args *));
109 static int msdosfs_advlock __P((struct vop_advlock_args *));
110 static int msdosfs_pathconf __P((struct vop_pathconf_args *ap));
111 static int msdosfs_reallocblks __P((struct vop_reallocblks_args *));
112 
113 /*
114  * Some general notes:
115  *
116  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
117  * read/written using the vnode for the filesystem. Blocks that represent
118  * the contents of a file are read/written using the vnode for the file
119  * (including directories when they are read/written as files). This
120  * presents problems for the dos filesystem because data that should be in
121  * an inode (if dos had them) resides in the directory itself.  Since we
122  * must update directory entries without the benefit of having the vnode
123  * for the directory we must use the vnode for the filesystem.  This means
124  * that when a directory is actually read/written (via read, write, or
125  * readdir, or seek) we must use the vnode for the filesystem instead of
126  * the vnode for the directory as would happen in ufs. This is to insure we
127  * retreive the correct block from the buffer cache since the hash value is
128  * based upon the vnode address and the desired block number.
129  */
130 
131 /*
132  * Create a regular file. On entry the directory to contain the file being
133  * created is locked.  We must release before we return. We must also free
134  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
135  * only if the SAVESTART bit in cn_flags is clear on success.
136  */
137 static int
138 msdosfs_create(ap)
139 	struct vop_create_args /* {
140 		struct vnode *a_dvp;
141 		struct vnode **a_vpp;
142 		struct componentname *a_cnp;
143 		struct vattr *a_vap;
144 	} */ *ap;
145 {
146 	struct componentname *cnp = ap->a_cnp;
147 	struct denode ndirent;
148 	struct denode *dep;
149 	struct denode *pdep = VTODE(ap->a_dvp);
150 	struct timespec ts;
151 	int error;
152 
153 #ifdef MSDOSFS_DEBUG
154 	printf("msdosfs_create(cnp %08x, vap %08x\n", cnp, ap->a_vap);
155 #endif
156 
157 	/*
158 	 * Create a directory entry for the file, then call createde() to
159 	 * have it installed. NOTE: DOS files are always executable.  We
160 	 * use the absence of the owner write bit to make the file
161 	 * readonly.
162 	 */
163 #ifdef DIAGNOSTIC
164 	if ((cnp->cn_flags & SAVENAME) == 0)
165 		panic("msdosfs_create: no name");
166 #endif
167 	bzero(&ndirent, sizeof(ndirent));
168 	TIMEVAL_TO_TIMESPEC(&time, &ts);
169 	unix2dostime(&ts, &ndirent.de_Date, &ndirent.de_Time);
170 	unix2dosfn((u_char *)cnp->cn_nameptr, ndirent.de_Name, cnp->cn_namelen);
171 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE)
172 				? ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
173 	ndirent.de_StartCluster = 0;
174 	ndirent.de_FileSize = 0;
175 	ndirent.de_dev = pdep->de_dev;
176 	ndirent.de_devvp = pdep->de_devvp;
177 	if ((error = createde(&ndirent, pdep, &dep)) == 0) {
178 		*ap->a_vpp = DETOV(dep);
179 		if ((cnp->cn_flags & SAVESTART) == 0)
180 			free(cnp->cn_pnbuf, M_NAMEI);
181 	} else {
182 		free(cnp->cn_pnbuf, M_NAMEI);
183 	}
184 	vput(ap->a_dvp);		/* release parent dir */
185 	return error;
186 }
187 
188 static int
189 msdosfs_mknod(ap)
190 	struct vop_mknod_args /* {
191 		struct vnode *a_dvp;
192 		struct vnode **a_vpp;
193 		struct componentname *a_cnp;
194 		struct vattr *a_vap;
195 	} */ *ap;
196 {
197 	int error;
198 
199 	switch (ap->a_vap->va_type) {
200 	case VDIR:
201 		error = msdosfs_mkdir((struct vop_mkdir_args *)ap);
202 		break;
203 
204 	case VREG:
205 		error = msdosfs_create((struct vop_create_args *)ap);
206 		break;
207 
208 	default:
209 		error = EINVAL;
210 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
211 		vput(ap->a_dvp);
212 		break;
213 	}
214 	return error;
215 }
216 
217 static int
218 msdosfs_open(ap)
219 	struct vop_open_args /* {
220 		struct vnode *a_vp;
221 		int a_mode;
222 		struct ucred *a_cred;
223 		struct proc *a_p;
224 	} */ *ap;
225 {
226 	return 0;
227 }
228 
229 static int
230 msdosfs_close(ap)
231 	struct vop_close_args /* {
232 		struct vnode *a_vp;
233 		int a_fflag;
234 		struct ucred *a_cred;
235 		struct proc *a_p;
236 	} */ *ap;
237 {
238 	struct vnode *vp = ap->a_vp;
239 	struct denode *dep = VTODE(vp);
240 
241 	if (vp->v_usecount > 1 && !(dep->de_flag & DE_LOCKED))
242 		DE_TIMES(dep, &time);
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 vattr *vap = ap->a_vap;
334 
335 	DE_TIMES(dep, &time);
336 	vap->va_fsid = dep->de_dev;
337 	/*
338 	 * The following computation of the fileid must be the same as that
339 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
340 	 * doesn't work.
341 	 */
342 	if (dep->de_Attributes & ATTR_DIRECTORY) {
343 		if ((cn = dep->de_StartCluster) == MSDOSFSROOT)
344 			cn = 1;
345 	} else {
346 		if ((cn = dep->de_dirclust) == MSDOSFSROOT)
347 			cn = 1;
348 		cn = (cn << 16) | (dep->de_diroffset & 0xffff);
349 	}
350 	vap->va_fileid = cn;
351 	vap->va_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
352 		((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
353 	vap->va_mode &= dep->de_pmp->pm_mask;
354 	if (dep->de_Attributes & ATTR_DIRECTORY)
355 		vap->va_mode |= S_IFDIR;
356 	vap->va_nlink = 1;
357 	vap->va_gid = dep->de_pmp->pm_gid;
358 	vap->va_uid = dep->de_pmp->pm_uid;
359 	vap->va_rdev = 0;
360 	vap->va_size = dep->de_FileSize;
361 	dos2unixtime(dep->de_Date, dep->de_Time, &vap->va_atime);
362 	vap->va_mtime = vap->va_atime;
363 #if 0
364 #ifndef MSDOSFS_NODIRMOD
365 	if (vap->va_mode & S_IFDIR)
366 		TIMEVAL_TO_TIMESPEC(&time, &vap->va_mtime);
367 #endif
368 #endif
369 	vap->va_ctime = vap->va_atime;
370 	vap->va_flags = (dep->de_Attributes & ATTR_ARCHIVE) ? 0 : SF_ARCHIVED;
371 	vap->va_gen = 0;
372 	vap->va_blocksize = dep->de_pmp->pm_bpcluster;
373 	vap->va_bytes = (dep->de_FileSize + dep->de_pmp->pm_crbomask) &
374 	    			~(dep->de_pmp->pm_crbomask);
375 	vap->va_type = ap->a_vp->v_type;
376 	vap->va_filerev = dep->de_modrev;
377 	return 0;
378 }
379 
380 static int
381 msdosfs_setattr(ap)
382 	struct vop_setattr_args /* {
383 		struct vnode *a_vp;
384 		struct vattr *a_vap;
385 		struct ucred *a_cred;
386 		struct proc *a_p;
387 	} */ *ap;
388 {
389 	struct vnode *vp = ap->a_vp;
390 	struct denode *dep = VTODE(ap->a_vp);
391 	struct vattr *vap = ap->a_vap;
392 	struct ucred *cred = ap->a_cred;
393 	int error = 0;
394 
395 	/*
396 	 * Check for unsettable attributes.
397 	 */
398 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
399 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
400 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
401 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
402 		return (EINVAL);
403 	}
404 	if (vap->va_flags != VNOVAL) {
405 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
406 			return (EROFS);
407 		if (cred->cr_uid != dep->de_pmp->pm_uid &&
408 		    (error = suser(cred, &ap->a_p->p_acflag)))
409 			return (error);
410 		/*
411 		 * We are very inconsistent about handling unsupported
412 		 * attributes.  We ignored the the access time and the
413 		 * read and execute bits.  We were strict for the other
414 		 * attributes.
415 		 *
416 		 * Here we are strict, stricter than ufs in not allowing
417 		 * users to attempt to set SF_SETTABLE bits or anyone to
418 		 * set unsupported bits.  However, we ignore attempts to
419 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
420 		 * sensible file system attempts it a lot.
421 		 */
422 		if (cred->cr_uid != 0) {
423 			if (vap->va_flags & SF_SETTABLE)
424 				return EPERM;
425 		}
426 		if (vap->va_flags & ~SF_ARCHIVED)
427 			return EINVAL;
428 		if (vap->va_flags & SF_ARCHIVED)
429 			dep->de_Attributes &= ~ATTR_ARCHIVE;
430 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
431 			dep->de_Attributes |= ATTR_ARCHIVE;
432 		dep->de_flag |= DE_MODIFIED;
433 	}
434 
435 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (uid_t)VNOVAL) {
436 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
437 			return (EROFS);
438 		if ((cred->cr_uid != dep->de_pmp->pm_uid ||
439 		     vap->va_uid != dep->de_pmp->pm_uid ||
440 		     (vap->va_gid != dep->de_pmp->pm_gid &&
441 		      !groupmember(vap->va_gid, cred))) &&
442 		    (error = suser(cred, &ap->a_p->p_acflag)))
443 			return error;
444 		if (vap->va_uid != dep->de_pmp->pm_uid ||
445 		    vap->va_gid != dep->de_pmp->pm_gid)
446 			return EINVAL;
447 	}
448 	if (vap->va_size != VNOVAL) {
449 		/*
450 		 * Disallow write attempts on read-only file systems;
451 		 * unless the file is a socket, fifo, or a block or
452 		 * character device resident on the file system.
453 		 */
454 		switch (vp->v_type) {
455 		case VDIR:
456 			return (EISDIR);
457 		case VLNK:
458 		case VREG:
459 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
460 				return (EROFS);
461 			break;
462 		}
463 		error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
464 		if (error)
465 			return error;
466 	}
467 	if (vap->va_mtime.tv_sec != VNOVAL) {
468 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
469 			return (EROFS);
470 		if (cred->cr_uid != dep->de_pmp->pm_uid &&
471 		    (error = suser(cred, &ap->a_p->p_acflag)) &&
472 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
473 		    (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_p))))
474 			return error;
475 		dep->de_flag |= DE_UPDATE;
476 		error = deupdat(dep, &vap->va_mtime, 1);
477 		if (error)
478 			return error;
479 	}
480 
481 	/*
482 	 * DOS files only have the ability to have their writability
483 	 * attribute set, so we use the owner write bit to set the readonly
484 	 * attribute.
485 	 */
486 	error = 0;
487 	if (vap->va_mode != (u_short) VNOVAL) {
488 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
489 			return (EROFS);
490 		if (cred->cr_uid != dep->de_pmp->pm_uid &&
491 		    (error = suser(cred, &ap->a_p->p_acflag)))
492 			return error;
493 
494 		/* We ignore the read and execute bits */
495 		if (vap->va_mode & VWRITE)
496 			dep->de_Attributes &= ~ATTR_READONLY;
497 		else
498 			dep->de_Attributes |= ATTR_READONLY;
499 		dep->de_flag |= DE_MODIFIED;
500 	}
501 	return error;
502 }
503 
504 static int
505 msdosfs_read(ap)
506 	struct vop_read_args /* {
507 		struct vnode *a_vp;
508 		struct uio *a_uio;
509 		int a_ioflag;
510 		struct ucred *a_cred;
511 	} */ *ap;
512 {
513 	int error = 0;
514 	int diff;
515 	int isadir;
516 	long n;
517 	long on;
518 	daddr_t lbn;
519 	daddr_t rablock;
520 	int rasize;
521 	struct buf *bp;
522 	struct vnode *vp = ap->a_vp;
523 	struct denode *dep = VTODE(vp);
524 	struct msdosfsmount *pmp = dep->de_pmp;
525 	struct uio *uio = ap->a_uio;
526 
527 	/*
528 	 * If they didn't ask for any data, then we are done.
529 	 */
530 	if (uio->uio_resid == 0)
531 		return 0;
532 	if (uio->uio_offset < 0)
533 		return EINVAL;
534 
535 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
536 	do {
537 		lbn = uio->uio_offset >> pmp->pm_cnshift;
538 		on = uio->uio_offset & pmp->pm_crbomask;
539 		n = min((u_long) (pmp->pm_bpcluster - on), uio->uio_resid);
540 		diff = dep->de_FileSize - uio->uio_offset;
541 		if (diff <= 0)
542 			return 0;
543 		/* convert cluster # to block # if a directory */
544 		if (isadir) {
545 			error = pcbmap(dep, lbn, &lbn, 0);
546 			if (error)
547 				return error;
548 		}
549 		if (diff < n)
550 			n = diff;
551 		/*
552 		 * If we are operating on a directory file then be sure to
553 		 * do i/o with the vnode for the filesystem instead of the
554 		 * vnode for the directory.
555 		 */
556 		if (isadir) {
557 			error = bread(pmp->pm_devvp, lbn, pmp->pm_bpcluster,
558 			    NOCRED, &bp);
559 		} else {
560 			rablock = lbn + 1;
561 #ifdef	PC98
562 			/*
563 			 * 1024byte/sector support
564 			 */
565 			if (pmp->pm_BytesPerSec == 1024)
566 					vp->v_flag |= 0x10000;
567 #endif
568 			if (vp->v_lastr + 1 == lbn &&
569 			    rablock * pmp->pm_bpcluster < dep->de_FileSize) {
570 				rasize = pmp->pm_bpcluster;
571 				error = breadn(vp, lbn, pmp->pm_bpcluster,
572 					       &rablock, &rasize, 1,
573 					       NOCRED, &bp);
574 			} else {
575 				error = bread(vp, lbn, pmp->pm_bpcluster, NOCRED,
576 					      &bp);
577 			}
578 			vp->v_lastr = lbn;
579 		}
580 		n = min(n, pmp->pm_bpcluster - bp->b_resid);
581 		if (error) {
582 			brelse(bp);
583 			return error;
584 		}
585 		error = uiomove(bp->b_data + on, (int) n, uio);
586 		/*
587 		 * If we have read everything from this block or have read
588 		 * to end of file then we are done with this block.  Mark
589 		 * it to say the buffer can be reused if need be.
590 		 */
591 #if 0
592 		if (n + on == pmp->pm_bpcluster ||
593 		    uio->uio_offset == dep->de_FileSize)
594 			bp->b_flags |= B_AGE;
595 #endif
596 		brelse(bp);
597 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
598 	return error;
599 }
600 
601 /*
602  * Write data to a file or directory.
603  */
604 static int
605 msdosfs_write(ap)
606 	struct vop_write_args /* {
607 		struct vnode *a_vp;
608 		struct uio *a_uio;
609 		int a_ioflag;
610 		struct ucred *a_cred;
611 	} */ *ap;
612 {
613 	int n;
614 	int isadir;
615 	int croffset;
616 	int resid;
617 	int osize;
618 	int error = 0;
619 	u_long count;
620 	daddr_t bn, lastcn;
621 	struct buf *bp;
622 	int ioflag = ap->a_ioflag;
623 	struct uio *uio = ap->a_uio;
624 	struct proc *p = uio->uio_procp;
625 	struct vnode *vp = ap->a_vp;
626 	struct vnode *thisvp;
627 	struct denode *dep = VTODE(vp);
628 	struct msdosfsmount *pmp = dep->de_pmp;
629 	struct ucred *cred = ap->a_cred;
630 	struct timespec ts;
631 
632 #ifdef MSDOSFS_DEBUG
633 	printf("msdosfs_write(vp %08x, uio %08x, ioflag %08x, cred %08x\n",
634 	       vp, uio, ioflag, cred);
635 	printf("msdosfs_write(): diroff %d, dirclust %d, startcluster %d\n",
636 	       dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
637 #endif
638 
639 	switch (vp->v_type) {
640 	case VREG:
641 		if (ioflag & IO_APPEND)
642 			uio->uio_offset = dep->de_FileSize;
643 		isadir = 0;
644 		thisvp = vp;
645 		break;
646 
647 	case VDIR:
648 		if ((ioflag & IO_SYNC) == 0)
649 			panic("msdosfs_write(): non-sync directory update");
650 		isadir = 1;
651 		thisvp = pmp->pm_devvp;
652 		break;
653 
654 	default:
655 		panic("msdosfs_write(): bad file type");
656 		break;
657 	}
658 
659 	if (uio->uio_offset < 0)
660 		return EINVAL;
661 
662 	if (uio->uio_resid == 0)
663 		return 0;
664 
665 	/*
666 	 * If they've exceeded their filesize limit, tell them about it.
667 	 */
668 	if (vp->v_type == VREG && p &&
669 	    ((uio->uio_offset + uio->uio_resid) >
670 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
671 		psignal(p, SIGXFSZ);
672 		return EFBIG;
673 	}
674 
675 	/*
676 	 * If attempting to write beyond the end of the root directory we
677 	 * stop that here because the root directory can not grow.
678 	 */
679 	if ((dep->de_Attributes & ATTR_DIRECTORY) &&
680 	    dep->de_StartCluster == MSDOSFSROOT &&
681 	    (uio->uio_offset + uio->uio_resid) > dep->de_FileSize)
682 		return ENOSPC;
683 
684 	/*
685 	 * If the offset we are starting the write at is beyond the end of
686 	 * the file, then they've done a seek.  Unix filesystems allow
687 	 * files with holes in them, DOS doesn't so we must fill the hole
688 	 * with zeroed blocks.
689 	 */
690 	if (uio->uio_offset > dep->de_FileSize) {
691 		error = deextend(dep, uio->uio_offset, cred);
692 		if (error)
693 			return error;
694 	}
695 
696 	/*
697 	 * Remember some values in case the write fails.
698 	 */
699 	resid = uio->uio_resid;
700 	osize = dep->de_FileSize;
701 
702 
703 #ifdef	PC98
704 	/*
705 	 * 1024byte/sector support
706 	 */
707 	if (pmp->pm_BytesPerSec == 1024)
708 		thisvp->v_flag |= 0x10000;
709 #endif
710 	/*
711 	 * If we write beyond the end of the file, extend it to its ultimate
712 	 * size ahead of the time to hopefully get a contiguous area.
713 	 */
714 	if (uio->uio_offset + resid > osize) {
715 		count = de_clcount(pmp, uio->uio_offset + resid) - de_clcount(pmp, osize);
716 		if ((error = extendfile(dep, count, NULL, NULL, 0))
717 		    && (error != ENOSPC || (ioflag & IO_UNIT)))
718 			goto errexit;
719 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
720 	} else
721 		lastcn = de_clcount(pmp, osize) - 1;
722 
723 	do {
724 		bn = de_blk(pmp, uio->uio_offset);
725 		if (isadir) {
726 			error = pcbmap(dep, bn, &bn, 0);
727 			if (error)
728 				break;
729 		} else if (bn > lastcn) {
730 			error = ENOSPC;
731 			break;
732 		}
733 
734 		croffset = uio->uio_offset & pmp->pm_crbomask;
735 		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
736 		if (uio->uio_offset + n > dep->de_FileSize) {
737 			dep->de_FileSize = uio->uio_offset + n;
738 			/* The object size needs to be set before buffer is allocated */
739 			vnode_pager_setsize(vp, dep->de_FileSize);
740 		}
741 
742 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
743 		    && (de_blk(pmp, uio->uio_offset + uio->uio_resid) > de_blk(pmp, uio->uio_offset)
744 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
745 			/*
746 			 * If either the whole cluster gets written,
747 			 * or we write the cluster from its start beyond EOF,
748 			 * then no need to read data from disk.
749 			 */
750 			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
751 			clrbuf(bp);
752 			/*
753 			 * Do the bmap now, since pcbmap needs buffers
754 			 * for the fat table. (see msdosfs_strategy)
755 			 */
756 			if (!isadir) {
757 				if (bp->b_blkno == bp->b_lblkno) {
758 					error = pcbmap(dep, bp->b_lblkno,
759 							   &bp->b_blkno, 0);
760 					if (error)
761 						bp->b_blkno = -1;
762 				}
763 				if (bp->b_blkno == -1) {
764 					brelse(bp);
765 					if (!error)
766 						error = EIO;		/* XXX */
767 					break;
768 				}
769 			}
770 		} else {
771 			/*
772 			 * The block we need to write into exists, so read it in.
773 			 */
774 			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
775 			if (error)
776 				break;
777 		}
778 
779 		/*
780 		 * Should these vnode_pager_* functions be done on dir
781 		 * files?
782 		 */
783 
784 		/*
785 		 * Copy the data from user space into the buf header.
786 		 */
787 		error = uiomove(bp->b_data + croffset, n, uio);
788 
789 		/*
790 		 * If they want this synchronous then write it and wait for
791 		 * it.  Otherwise, if on a cluster boundary write it
792 		 * asynchronously so we can move on to the next block
793 		 * without delay.  Otherwise do a delayed write because we
794 		 * may want to write somemore into the block later.
795 		 */
796 		if (ioflag & IO_SYNC)
797 			(void) bwrite(bp);
798 		else if (n + croffset == pmp->pm_bpcluster) {
799 			bawrite(bp);
800 		} else
801 			bdwrite(bp);
802 		dep->de_flag |= DE_UPDATE;
803 	} while (error == 0 && uio->uio_resid > 0);
804 
805 	/*
806 	 * If the write failed and they want us to, truncate the file back
807 	 * to the size it was before the write was attempted.
808 	 */
809 errexit:
810 	if (error) {
811 		if (ioflag & IO_UNIT) {
812 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
813 			uio->uio_offset -= resid - uio->uio_resid;
814 			uio->uio_resid = resid;
815 		} else {
816 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
817 			if (uio->uio_resid != resid)
818 				error = 0;
819 		}
820 	} else {
821 		TIMEVAL_TO_TIMESPEC(&time, &ts);
822 		error = deupdat(dep, &ts, 1);
823 	}
824 	return error;
825 }
826 
827 static int
828 msdosfs_ioctl(ap)
829 	struct vop_ioctl_args /* {
830 		struct vnode *a_vp;
831 		int a_command;
832 		caddr_t a_data;
833 		int a_fflag;
834 		struct ucred *a_cred;
835 		struct proc *a_p;
836 	} */ *ap;
837 {
838 	return ENOTTY;
839 }
840 
841 static int
842 msdosfs_select(ap)
843 	struct vop_select_args /* {
844 		struct vnode *a_vp;
845 		int a_which;
846 		int a_fflags;
847 		struct ucred *a_cred;
848 		struct proc *a_p;
849 	} */ *ap;
850 {
851 	return 1;		/* DOS filesystems never block? */
852 }
853 
854 static int
855 msdosfs_mmap(ap)
856 	struct vop_mmap_args /* {
857 		struct vnode *a_vp;
858 		int a_fflags;
859 		struct ucred *a_cred;
860 		struct proc *a_p;
861 	} */ *ap;
862 {
863 	return EINVAL;
864 }
865 
866 /*
867  * Flush the blocks of a file to disk.
868  *
869  * This function is worthless for vnodes that represent directories. Maybe we
870  * could just do a sync if they try an fsync on a directory file.
871  */
872 static int
873 msdosfs_fsync(ap)
874 	struct vop_fsync_args /* {
875 		struct vnode *a_vp;
876 		struct ucred *a_cred;
877 		int a_waitfor;
878 		struct proc *a_p;
879 	} */ *ap;
880 {
881 	register struct vnode *vp = ap->a_vp;
882 	register struct buf *bp;
883 	int wait = ap->a_waitfor == MNT_WAIT;
884 	struct timespec ts;
885 	struct buf *nbp;
886 	int s;
887 
888 	/*
889 	 * Flush all dirty buffers associated with a vnode.
890 	 */
891 loop:
892 	s = splbio();
893 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
894 		nbp = bp->b_vnbufs.le_next;
895 		if ((bp->b_flags & B_BUSY))
896 			continue;
897 		if ((bp->b_flags & B_DELWRI) == 0)
898 			panic("msdosfs_fsync: not dirty");
899 		bremfree(bp);
900 		bp->b_flags |= B_BUSY;
901 		splx(s);
902 		(void) bwrite(bp);
903 		goto loop;
904 	}
905 	while (vp->v_numoutput) {
906 		vp->v_flag |= VBWAIT;
907 		(void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
908 	}
909 #ifdef DIAGNOSTIC
910 	if (vp->v_dirtyblkhd.lh_first) {
911 		vprint("msdosfs_fsync: dirty", vp);
912 		goto loop;
913 	}
914 #endif
915 	splx(s);
916 	TIMEVAL_TO_TIMESPEC(&time, &ts);
917 	return deupdat(VTODE(vp), &ts, wait);
918 }
919 
920 /*
921  * Now the whole work of extending a file is done in the write function.
922  * So nothing to do here.
923  */
924 static int
925 msdosfs_seek(ap)
926 	struct vop_seek_args /* {
927 		struct vnode *a_vp;
928 		off_t a_oldoff;
929 		off_t a_newoff;
930 		struct ucred *a_cred;
931 	} */ *ap;
932 {
933 	return 0;
934 }
935 
936 static int
937 msdosfs_remove(ap)
938 	struct vop_remove_args /* {
939 		struct vnode *a_dvp;
940 		struct vnode *a_vp;
941 		struct componentname *a_cnp;
942 	} */ *ap;
943 {
944 	int error;
945 	struct denode *dep = VTODE(ap->a_vp);
946 	struct denode *ddep = VTODE(ap->a_dvp);
947 
948 	error = removede(ddep,dep);
949 #ifdef MSDOSFS_DEBUG
950 	printf("msdosfs_remove(), dep %08x, v_usecount %d\n", dep, ap->a_vp->v_usecount);
951 #endif
952 	if (ddep == dep)
953 		vrele(ap->a_vp);
954 	else
955 		vput(ap->a_vp);	/* causes msdosfs_inactive() to be called
956 				 * via vrele() */
957 	vput(ap->a_dvp);
958 	return error;
959 }
960 
961 /*
962  * DOS filesystems don't know what links are. But since we already called
963  * msdosfs_lookup() with create and lockparent, the parent is locked so we
964  * have to free it before we return the error.
965  */
966 static int
967 msdosfs_link(ap)
968 	struct vop_link_args /* {
969 		struct vnode *a_tdvp;
970 		struct vnode *a_vp;
971 		struct componentname *a_cnp;
972 	} */ *ap;
973 {
974 	VOP_ABORTOP(ap->a_tdvp, ap->a_cnp);
975 	vput(ap->a_tdvp);
976 	return EOPNOTSUPP;
977 }
978 
979 /*
980  * Renames on files require moving the denode to a new hash queue since the
981  * denode's location is used to compute which hash queue to put the file
982  * in. Unless it is a rename in place.  For example "mv a b".
983  *
984  * What follows is the basic algorithm:
985  *
986  * if (file move) {
987  *	if (dest file exists) {
988  *		remove dest file
989  *	}
990  *	if (dest and src in same directory) {
991  *		rewrite name in existing directory slot
992  *	} else {
993  *		write new entry in dest directory
994  *		update offset and dirclust in denode
995  *		move denode to new hash chain
996  *		clear old directory entry
997  *	}
998  * } else {
999  *	directory move
1000  *	if (dest directory exists) {
1001  *		if (dest is not empty) {
1002  *			return ENOTEMPTY
1003  *		}
1004  *		remove dest directory
1005  *	}
1006  *	if (dest and src in same directory) {
1007  *		rewrite name in existing entry
1008  *	} else {
1009  *		be sure dest is not a child of src directory
1010  *		write entry in dest directory
1011  *		update "." and ".." in moved directory
1012  *		clear old directory entry for moved directory
1013  *	}
1014  * }
1015  *
1016  * On entry:
1017  *	source's parent directory is unlocked
1018  *	source file or directory is unlocked
1019  *	destination's parent directory is locked
1020  *	destination file or directory is locked if it exists
1021  *
1022  * On exit:
1023  *	all denodes should be released
1024  *
1025  * Notes:
1026  * I'm not sure how the memory containing the pathnames pointed at by the
1027  * componentname structures is freed, there may be some memory bleeding
1028  * for each rename done.
1029  */
1030 static int
1031 msdosfs_rename(ap)
1032 	struct vop_rename_args /* {
1033 		struct vnode *a_fdvp;
1034 		struct vnode *a_fvp;
1035 		struct componentname *a_fcnp;
1036 		struct vnode *a_tdvp;
1037 		struct vnode *a_tvp;
1038 		struct componentname *a_tcnp;
1039 	} */ *ap;
1040 {
1041 	u_char toname[11];
1042 	int error;
1043 	int newparent = 0;
1044 	int sourceisadirectory = 0;
1045 	u_long cn;
1046 	daddr_t bn;
1047 	struct vnode *tvp = ap->a_tvp;
1048 	struct denode *fddep;	/* from file's parent directory	 */
1049 	struct denode *fdep;	/* from file or directory	 */
1050 	struct denode *tddep;	/* to file's parent directory	 */
1051 	struct denode *tdep;	/* to file or directory		 */
1052 	struct msdosfsmount *pmp;
1053 	struct direntry *dotdotp;
1054 	struct direntry *ep;
1055 	struct buf *bp;
1056 
1057 	fddep = VTODE(ap->a_fdvp);
1058 	fdep = VTODE(ap->a_fvp);
1059 	tddep = VTODE(ap->a_tdvp);
1060 	tdep = tvp ? VTODE(tvp) : NULL;
1061 	pmp = fddep->de_pmp;
1062 
1063 	/* Check for cross-device rename */
1064 	if ((ap->a_fvp->v_mount != ap->a_tdvp->v_mount) ||
1065 	    (tvp && (ap->a_fvp->v_mount != tvp->v_mount))) {
1066 		error = EXDEV;
1067 		goto bad;
1068 	}
1069 
1070 	/*
1071 	 * Convert the filename in tcnp into a dos filename. We copy this
1072 	 * into the denode and directory entry for the destination
1073 	 * file/directory.
1074 	 */
1075 	unix2dosfn((u_char *) ap->a_tcnp->cn_nameptr,
1076 		   toname, ap->a_tcnp->cn_namelen);
1077 
1078 	/*
1079 	 * At this point this is the lock state of the denodes:
1080 	 *	fddep referenced
1081 	 *	fdep  referenced
1082 	 *	tddep locked
1083 	 *	tdep  locked if it exists
1084 	 */
1085 
1086 	/*
1087 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
1088 	 * leads to a crippled directory tree.  It's pretty tough to do a
1089 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1090 	 * doesn't work if the ".." entry is missing.
1091 	 */
1092 	if (fdep->de_Attributes & ATTR_DIRECTORY) {
1093 		if ((ap->a_fcnp->cn_namelen == 1
1094 		     && ap->a_fcnp->cn_nameptr[0] == '.')
1095 		    || fddep == fdep
1096 		    || (ap->a_fcnp->cn_flags | ap->a_tcnp->cn_flags)
1097 		       & ISDOTDOT) {
1098 			VOP_ABORTOP(ap->a_tdvp, ap->a_tcnp);
1099 			vput(ap->a_tdvp);
1100 			if (tvp)
1101 				vput(tvp);
1102 			VOP_ABORTOP(ap->a_fdvp, ap->a_fcnp);
1103 			vrele(ap->a_fdvp);
1104 			vrele(ap->a_fvp);
1105 			return EINVAL;
1106 		}
1107 		sourceisadirectory = 1;
1108 	}
1109 
1110 	/*
1111 	 * If we are renaming a directory, and the directory is being moved
1112 	 * to another directory, then we must be sure the destination
1113 	 * directory is not in the subtree of the source directory.  This
1114 	 * could orphan everything under the source directory.
1115 	 * doscheckpath() unlocks the destination's parent directory so we
1116 	 * must look it up again to relock it.
1117 	 */
1118 	if (fddep->de_StartCluster != tddep->de_StartCluster)
1119 		newparent = 1;
1120 	if (sourceisadirectory && newparent) {
1121 		if (tdep) {
1122 			vput(ap->a_tvp);
1123 			tdep = NULL;
1124 		}
1125 		/* doscheckpath() vput()'s tddep */
1126 		error = doscheckpath(fdep, tddep);
1127 		tddep = NULL;
1128 		if (error)
1129 			goto bad;
1130 		if ((ap->a_tcnp->cn_flags & SAVESTART) == 0)
1131 			panic("msdosfs_rename(): lost to startdir");
1132 		error = relookup(ap->a_tdvp, &tvp, ap->a_tcnp);
1133 		if (error)
1134 			goto bad;
1135 		tddep = VTODE(ap->a_tdvp);
1136 		tdep = tvp ? VTODE(tvp) : NULL;
1137 	}
1138 
1139 	/*
1140 	 * If the destination exists, then be sure its type (file or dir)
1141 	 * matches that of the source.  And, if it is a directory make sure
1142 	 * it is empty.  Then delete the destination.
1143 	 */
1144 	if (tdep) {
1145 		if (tdep->de_Attributes & ATTR_DIRECTORY) {
1146 			if (!sourceisadirectory) {
1147 				error = ENOTDIR;
1148 				goto bad;
1149 			}
1150 			if (!dosdirempty(tdep)) {
1151 				error = ENOTEMPTY;
1152 				goto bad;
1153 			}
1154 			cache_purge(DETOV(tddep));
1155 		} else {		/* destination is file */
1156 			if (sourceisadirectory) {
1157 				error = EISDIR;
1158 				goto bad;
1159 			}
1160 		}
1161 		error = removede(tddep,tdep);
1162 		if (error)
1163 			goto bad;
1164 		vput(ap->a_tvp);
1165 		tdep = NULL;
1166 	}
1167 
1168 	/*
1169 	 * If the source and destination are in the same directory then
1170 	 * just read in the directory entry, change the name in the
1171 	 * directory entry and write it back to disk.
1172 	 */
1173 	if (newparent == 0) {
1174 		/* tddep and fddep point to the same denode here */
1175 		VOP_LOCK(ap->a_fvp);	/* ap->a_fdvp is already locked */
1176 		error = readep(fddep->de_pmp, fdep->de_dirclust,
1177 				   fdep->de_diroffset, &bp, &ep);
1178 		if (error) {
1179 			VOP_UNLOCK(ap->a_fvp);
1180 			goto bad;
1181 		}
1182 		bcopy(toname, ep->deName, 11);
1183 		error = bwrite(bp);
1184 		if (error) {
1185 			VOP_UNLOCK(ap->a_fvp);
1186 			goto bad;
1187 		}
1188 		bcopy(toname, fdep->de_Name, 11);	/* update denode */
1189 		/*
1190 		 * fdep locked fddep and tddep point to the same denode
1191 		 * which is locked tdep is NULL
1192 		 */
1193 	} else {
1194 		u_long dirsize = 0L;
1195 
1196 		/*
1197 		 * If the source and destination are in different
1198 		 * directories, then mark the entry in the source directory
1199 		 * as deleted and write a new entry in the destination
1200 		 * directory.  Then move the denode to the correct hash
1201 		 * chain for its new location in the filesystem.  And, if
1202 		 * we moved a directory, then update its .. entry to point
1203 		 * to the new parent directory. If we moved a directory
1204 		 * will also insure that the directory entry on disk has a
1205 		 * filesize of zero.
1206 		 */
1207 		VOP_LOCK(ap->a_fvp);
1208 		bcopy(toname, fdep->de_Name, 11);	/* update denode */
1209 		if (fdep->de_Attributes & ATTR_DIRECTORY) {
1210 			dirsize = fdep->de_FileSize;
1211 			fdep->de_FileSize = 0;
1212 		}
1213 		error = createde(fdep, tddep, (struct denode **) 0);
1214 		if (fdep->de_Attributes & ATTR_DIRECTORY) {
1215 			fdep->de_FileSize = dirsize;
1216 		}
1217 		if (error) {
1218 			/* should put back filename */
1219 			VOP_UNLOCK(ap->a_fvp);
1220 			goto bad;
1221 		}
1222 		VOP_LOCK(ap->a_fdvp);
1223 		error = readep(fddep->de_pmp, fddep->de_fndclust,
1224 				   fddep->de_fndoffset, &bp, &ep);
1225 		if (error) {
1226 			VOP_UNLOCK(ap->a_fvp);
1227 			VOP_UNLOCK(ap->a_fdvp);
1228 			goto bad;
1229 		}
1230 		ep->deName[0] = SLOT_DELETED;
1231 		error = bwrite(bp);
1232 		if (error) {
1233 			VOP_UNLOCK(ap->a_fvp);
1234 			VOP_UNLOCK(ap->a_fdvp);
1235 			goto bad;
1236 		}
1237 		if (!sourceisadirectory) {
1238 			fdep->de_dirclust = tddep->de_fndclust;
1239 			fdep->de_diroffset = tddep->de_fndoffset;
1240 			reinsert(fdep);
1241 		}
1242 		VOP_UNLOCK(ap->a_fdvp);
1243 	}
1244 	/* fdep is still locked here */
1245 
1246 	/*
1247 	 * If we moved a directory to a new parent directory, then we must
1248 	 * fixup the ".." entry in the moved directory.
1249 	 */
1250 	if (sourceisadirectory && newparent) {
1251 		cn = fdep->de_StartCluster;
1252 		if (cn == MSDOSFSROOT) {
1253 			/* this should never happen */
1254 			panic("msdosfs_rename(): updating .. in root directory?");
1255 		} else {
1256 			bn = cntobn(pmp, cn);
1257 		}
1258 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1259 			      NOCRED, &bp);
1260 		if (error) {
1261 			/* should really panic here, fs is corrupt */
1262 			VOP_UNLOCK(ap->a_fvp);
1263 			goto bad;
1264 		}
1265 		dotdotp = (struct direntry *) bp->b_data + 1;
1266 		putushort(dotdotp->deStartCluster, tddep->de_StartCluster);
1267 		error = bwrite(bp);
1268 		VOP_UNLOCK(ap->a_fvp);
1269 		if (error) {
1270 			/* should really panic here, fs is corrupt */
1271 			goto bad;
1272 		}
1273 	} else
1274 		VOP_UNLOCK(ap->a_fvp);
1275 bad:	;
1276 	vrele(DETOV(fdep));
1277 	vrele(DETOV(fddep));
1278 	if (tdep)
1279 		vput(DETOV(tdep));
1280 	if (tddep)
1281 		vput(DETOV(tddep));
1282 	return error;
1283 }
1284 
1285 static struct {
1286 	struct direntry dot;
1287 	struct direntry dotdot;
1288 }      dosdirtemplate = {
1289     {
1290 	".       ", "   ",		/* the . entry */
1291 	ATTR_DIRECTORY,			/* file attribute */
1292 	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},	/* resevered */
1293 	{210, 4}, {210, 4},		/* time and date */
1294 	{0, 0},				/* startcluster */
1295 	{0, 0, 0, 0},			/* filesize */
1296     },{
1297 	"..      ", "   ",		/* the .. entry */
1298 	ATTR_DIRECTORY,			/* file attribute */
1299 	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},	/* resevered */
1300 	{210, 4}, {210, 4},		/* time and date */
1301 	{0, 0},				/* startcluster */
1302 	{0, 0, 0, 0},			/* filesize */
1303     }
1304 };
1305 
1306 static int
1307 msdosfs_mkdir(ap)
1308 	struct vop_mkdir_args /* {
1309 		struct vnode *a_dvp;
1310 		struvt vnode **a_vpp;
1311 		struvt componentname *a_cnp;
1312 		struct vattr *a_vap;
1313 	} */ *ap;
1314 {
1315 	int bn;
1316 	int error;
1317 	u_long newcluster;
1318 	struct denode *pdep;
1319 	struct denode *ndep;
1320 	struct direntry *denp;
1321 	struct denode ndirent;
1322 	struct msdosfsmount *pmp;
1323 	struct buf *bp;
1324 	struct timespec ts;
1325 	u_short dDate, dTime;
1326 
1327 	pdep = VTODE(ap->a_dvp);
1328 
1329 	/*
1330 	 * If this is the root directory and there is no space left we
1331 	 * can't do anything.  This is because the root directory can not
1332 	 * change size.
1333 	 */
1334 	if (pdep->de_StartCluster == MSDOSFSROOT && pdep->de_fndclust == (u_long)-1) {
1335 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1336 		vput(ap->a_dvp);
1337 		return ENOSPC;
1338 	}
1339 
1340 	pmp = pdep->de_pmp;
1341 
1342 	/*
1343 	 * Allocate a cluster to hold the about to be created directory.
1344 	 */
1345 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1346 	if (error) {
1347 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1348 		vput(ap->a_dvp);
1349 		return error;
1350 	}
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->deStartCluster, newcluster);
1364 	TIMEVAL_TO_TIMESPEC(&time, &ts);
1365 	unix2dostime(&ts, &dDate, &dTime);
1366 	putushort(denp->deDate, dDate);
1367 	putushort(denp->deTime, dTime);
1368 	denp++;
1369 	putushort(denp->deStartCluster, pdep->de_StartCluster);
1370 	putushort(denp->deDate, dDate);
1371 	putushort(denp->deTime, dTime);
1372 	error = bwrite(bp);
1373 	if (error) {
1374 		clusterfree(pmp, newcluster, NULL);
1375 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1376 		vput(ap->a_dvp);
1377 		return error;
1378 	}
1379 
1380 	/*
1381 	 * Now build up a directory entry pointing to the newly allocated
1382 	 * cluster.  This will be written to an empty slot in the parent
1383 	 * directory.
1384 	 */
1385 	ndep = &ndirent;
1386 	bzero(ndep, sizeof(*ndep));
1387 	unix2dosfn((u_char *)ap->a_cnp->cn_nameptr,
1388 		   ndep->de_Name, ap->a_cnp->cn_namelen);
1389 	TIMEVAL_TO_TIMESPEC(&time, &ts);
1390 	unix2dostime(&ts, &ndep->de_Date, &ndep->de_Time);
1391 	ndep->de_StartCluster = newcluster;
1392 	ndep->de_Attributes = ATTR_DIRECTORY;
1393 
1394 	error = createde(ndep, pdep, &ndep);
1395 	if (error) {
1396 		clusterfree(pmp, newcluster, NULL);
1397 	} else {
1398 		*ap->a_vpp = DETOV(ndep);
1399 	}
1400 	free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1401 #ifdef MSDOSFS_DEBUG
1402 	printf("msdosfs_mkdir(): vput(%08x)\n", ap->a_dvp);
1403 #endif
1404 	vput(ap->a_dvp);
1405 	return error;
1406 }
1407 
1408 static int
1409 msdosfs_rmdir(ap)
1410 	struct vop_rmdir_args /* {
1411 		struct vnode *a_dvp;
1412 		struct vnode *a_vp;
1413 		struct componentname *a_cnp;
1414 	} */ *ap;
1415 {
1416 	struct denode *ddep;
1417 	struct denode *dep;
1418 	int error = 0;
1419 
1420 	ddep = VTODE(ap->a_dvp);	/* parent dir of dir to delete	 */
1421 	dep = VTODE(ap->a_vp);/* directory to delete	 */
1422 
1423 	/*
1424 	 * Don't let "rmdir ." go thru.
1425 	 */
1426 	if (ddep == dep) {
1427 		vrele(ap->a_vp);
1428 		vput(ap->a_vp);
1429 		return EINVAL;
1430 	}
1431 
1432 	/*
1433 	 * Be sure the directory being deleted is empty.
1434 	 */
1435 	if (dosdirempty(dep) == 0) {
1436 		error = ENOTEMPTY;
1437 		goto out;
1438 	}
1439 
1440 	/*
1441 	 * Delete the entry from the directory.  For dos filesystems this
1442 	 * gets rid of the directory entry on disk, the in memory copy
1443 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1444 	 * being found by deget().  When the vput() on dep is done we give
1445 	 * up access and eventually msdosfs_reclaim() will be called which
1446 	 * will remove it from the denode cache.
1447 	 */
1448 	error = removede(ddep,dep);
1449 	if (error)
1450 		goto out;
1451 
1452 	/*
1453 	 * This is where we decrement the link count in the parent
1454 	 * directory.  Since dos filesystems don't do this we just purge
1455 	 * the name cache and let go of the parent directory denode.
1456 	 */
1457 	cache_purge(DETOV(ddep));
1458 	vput(ap->a_dvp);
1459 	ap->a_dvp = NULL;
1460 
1461 	/*
1462 	 * Truncate the directory that is being deleted.
1463 	 */
1464 	error = detrunc(dep, (u_long) 0, IO_SYNC, NOCRED, NULL);
1465 	cache_purge(DETOV(dep));
1466 
1467 out:	;
1468 	if (ap->a_dvp)
1469 		vput(ap->a_dvp);
1470 	vput(ap->a_vp);
1471 	return error;
1472 }
1473 
1474 /*
1475  * DOS filesystems don't know what symlinks are.
1476  */
1477 static int
1478 msdosfs_symlink(ap)
1479 	struct vop_symlink_args /* {
1480 		struct vnode *a_dvp;
1481 		struct vnode **a_vpp;
1482 		struct componentname *a_cnp;
1483 		struct vattr *a_vap;
1484 		char *a_target;
1485 	} */ *ap;
1486 {
1487 	free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1488 	vput(ap->a_dvp);
1489 	return EINVAL;
1490 }
1491 
1492 /*
1493  * Dummy dirents to simulate the "." and ".." entries of the root directory
1494  * in a dos filesystem.  Dos doesn't provide these. Note that each entry
1495  * must be the same size as a dos directory entry (32 bytes).
1496  */
1497 static struct dos_dirent {
1498 	u_long d_fileno;
1499 	u_short d_reclen;
1500 	u_char d_type;
1501 	u_char d_namlen;
1502 	u_char d_name[24];
1503 }          rootdots[2] = {
1504 
1505 	{
1506 		1,		/* d_fileno			 */
1507 		sizeof(struct direntry),	/* d_reclen			 */
1508 		DT_DIR,		/* d_type			 */
1509 		1,		/* d_namlen			 */
1510 		"."		/* d_name			 */
1511 	},
1512 	{
1513 		1,		/* d_fileno			 */
1514 		sizeof(struct direntry),	/* d_reclen			 */
1515 		DT_DIR,		/* d_type			 */
1516 		2,		/* d_namlen			 */
1517 		".."		/* d_name			 */
1518 	}
1519 };
1520 
1521 static int
1522 msdosfs_readdir(ap)
1523 	struct vop_readdir_args /* {
1524 		struct vnode *a_vp;
1525 		struct uio *a_uio;
1526 		struct ucred *a_cred;
1527 		int *a_eofflag;
1528 		u_int *a_cookies;
1529 		int a_ncookies;
1530 	} */ *ap;
1531 {
1532 	int error = 0;
1533 	int diff;
1534 	char pushout;
1535 	long n;
1536 	long on;
1537 	long lost;
1538 	long count;
1539 	u_long cn;
1540 	u_long fileno;
1541 	long bias = 0;
1542 	daddr_t bn;
1543 	daddr_t lbn;
1544 	struct buf *bp;
1545 	struct denode *dep = VTODE(ap->a_vp);
1546 	struct msdosfsmount *pmp = dep->de_pmp;
1547 	struct direntry *dentp;
1548 	struct dirent *prev;
1549 	struct dirent *crnt;
1550 	u_char dirbuf[512];	/* holds converted dos directories */
1551 	struct uio *uio = ap->a_uio;
1552 	off_t off;
1553 	int ncookies = 0;
1554 
1555 #ifdef MSDOSFS_DEBUG
1556 	printf("msdosfs_readdir(): vp %08x, uio %08x, cred %08x, eofflagp %08x\n",
1557 	       ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1558 #endif
1559 
1560 	/*
1561 	 * msdosfs_readdir() won't operate properly on regular files since
1562 	 * it does i/o only with the the filesystem vnode, and hence can
1563 	 * retrieve the wrong block from the buffer cache for a plain file.
1564 	 * So, fail attempts to readdir() on a plain file.
1565 	 */
1566 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1567 		return ENOTDIR;
1568 
1569 	/*
1570 	 * If the user buffer is smaller than the size of one dos directory
1571 	 * entry or the file offset is not a multiple of the size of a
1572 	 * directory entry, then we fail the read.
1573 	 */
1574 	count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1575 	lost = uio->uio_resid - count;
1576 	if (count < sizeof(struct direntry) ||
1577 	    (uio->uio_offset & (sizeof(struct direntry) - 1)))
1578 		return EINVAL;
1579 	uio->uio_resid = count;
1580 	uio->uio_iov->iov_len = count;
1581 	off = uio->uio_offset;
1582 
1583 	/*
1584 	 * If they are reading from the root directory then, we simulate
1585 	 * the . and .. entries since these don't exist in the root
1586 	 * directory.  We also set the offset bias to make up for having to
1587 	 * simulate these entries. By this I mean that at file offset 64 we
1588 	 * read the first entry in the root directory that lives on disk.
1589 	 */
1590 	if (dep->de_StartCluster == MSDOSFSROOT) {
1591 		/*
1592 		 * printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1593 		 *	  uio->uio_offset);
1594 		 */
1595 		bias = 2 * sizeof(struct direntry);
1596 		if (uio->uio_offset < 2 * sizeof(struct direntry)) {
1597 			if (uio->uio_offset
1598 			    && uio->uio_offset != sizeof(struct direntry)) {
1599 				error = EINVAL;
1600 				goto out;
1601 			}
1602 			n = 1;
1603 			if (!uio->uio_offset) {
1604 				n = 2;
1605 				ncookies++;
1606 			}
1607 			ncookies++;
1608 			error = uiomove((char *) rootdots + uio->uio_offset,
1609 					n * sizeof(struct direntry), uio);
1610 		}
1611 	}
1612 	while (!error && uio->uio_resid > 0) {
1613 		lbn = (uio->uio_offset - bias) >> pmp->pm_cnshift;
1614 		on = (uio->uio_offset - bias) & pmp->pm_crbomask;
1615 		n = min((u_long) (pmp->pm_bpcluster - on), uio->uio_resid);
1616 		diff = dep->de_FileSize - (uio->uio_offset - bias);
1617 		if (diff <= 0)
1618 			break;
1619 		if (diff < n)
1620 			n = diff;
1621 		error = pcbmap(dep, lbn, &bn, &cn);
1622 		if (error)
1623 			break;
1624 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, NOCRED, &bp);
1625 		n = min(n, pmp->pm_bpcluster - bp->b_resid);
1626 		if (error) {
1627 			brelse(bp);
1628 			return error;
1629 		}
1630 
1631 		/*
1632 		 * code to convert from dos directory entries to ufs
1633 		 * directory entries
1634 		 */
1635 		pushout = 0;
1636 		dentp = (struct direntry *)(bp->b_data + on);
1637 		prev = 0;
1638 		crnt = (struct dirent *) dirbuf;
1639 		while ((char *) dentp < bp->b_data + on + n) {
1640 			/*
1641 			 * printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1642 			 *	  dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1643 			 */
1644 			/*
1645 			 * If we have an empty entry or a slot from a
1646 			 * deleted file, or a volume label entry just
1647 			 * concatenate its space onto the end of the
1648 			 * previous entry or, manufacture an empty entry if
1649 			 * there is no previous entry.
1650 			 */
1651 			if (dentp->deName[0] == SLOT_EMPTY ||
1652 			    dentp->deName[0] == SLOT_DELETED ||
1653 			    (dentp->deAttributes & ATTR_VOLUME)) {
1654 				if (prev) {
1655 					prev->d_reclen += sizeof(struct direntry);
1656 				} else {
1657 					prev = crnt;
1658 					prev->d_fileno = 0;
1659 					prev->d_reclen = sizeof(struct direntry);
1660 					prev->d_type = DT_UNKNOWN;
1661 					prev->d_namlen = 0;
1662 					prev->d_name[0] = 0;
1663 					ncookies++;
1664 				}
1665 			} else {
1666 				/*
1667 				 * this computation of d_fileno must match
1668 				 * the computation of va_fileid in
1669 				 * msdosfs_getattr
1670 				 */
1671 				if (dentp->deAttributes & ATTR_DIRECTORY) {
1672 					/* if this is the root directory */
1673 					fileno = getushort(dentp->deStartCluster);
1674 					if (fileno == MSDOSFSROOT)
1675 						fileno = 1;
1676 				} else {
1677 					/*
1678 					 * if the file's dirent lives in
1679 					 * root dir
1680 					 */
1681 					if ((fileno = cn) == MSDOSFSROOT)
1682 						fileno = 1;
1683 					fileno = (fileno << 16) |
1684 					    ((dentp - (struct direntry *) bp->b_data) & 0xffff);
1685 				}
1686 				crnt->d_fileno = fileno;
1687 				crnt->d_reclen = sizeof(struct direntry);
1688 				crnt->d_type = (dentp->deAttributes & ATTR_DIRECTORY)
1689 					         ? DT_DIR : DT_REG;
1690 				crnt->d_namlen = dos2unixfn(dentp->deName,
1691 							    (u_char *)crnt->d_name);
1692 				/*
1693 				 * printf("readdir: file %s, fileno %08x, attr %02x, start %08x\n",
1694 				 *	  crnt->d_name, crnt->d_fileno, dentp->deAttributes,
1695 				 *	  dentp->deStartCluster);
1696 				 */
1697 				prev = crnt;
1698 				ncookies++;
1699 			}
1700 			dentp++;
1701 
1702 			crnt = (struct dirent *) ((char *) crnt + sizeof(struct direntry));
1703 			pushout = 1;
1704 
1705 			/*
1706 			 * If our intermediate buffer is full then copy its
1707 			 * contents to user space.  I would just use the
1708 			 * buffer the buf header points to but, I'm afraid
1709 			 * that when we brelse() it someone else might find
1710 			 * it in the cache and think its contents are
1711 			 * valid.  Maybe there is a way to invalidate the
1712 			 * buffer before brelse()'ing it.
1713 			 */
1714 			if ((u_char *) crnt >= &dirbuf[sizeof dirbuf]) {
1715 				pushout = 0;
1716 				error = uiomove(dirbuf, sizeof(dirbuf), uio);
1717 				if (error)
1718 					break;
1719 				prev = 0;
1720 				crnt = (struct dirent *) dirbuf;
1721 			}
1722 		}
1723 		if (pushout) {
1724 			pushout = 0;
1725 			error = uiomove(dirbuf, (char *) crnt - (char *) dirbuf,
1726 			    uio);
1727 		}
1728 
1729 #if 0
1730 		/*
1731 		 * If we have read everything from this block or have read
1732 		 * to end of file then we are done with this block.  Mark
1733 		 * it to say the buffer can be reused if need be.
1734 		 */
1735 		if (n + on == pmp->pm_bpcluster ||
1736 		    (uio->uio_offset - bias) == dep->de_FileSize)
1737 			bp->b_flags |= B_AGE;
1738 #endif /* if 0 */
1739 		brelse(bp);
1740 		if (n == 0)
1741 			break;
1742 	}
1743 out:	;
1744 	uio->uio_resid += lost;
1745 	if (!error && ap->a_ncookies != NULL) {
1746 		struct dirent* dpStart;
1747 		struct dirent* dpEnd;
1748 		struct dirent* dp;
1749 		u_int *cookies;
1750 		u_int *cookiep;
1751 
1752 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1753 			panic("msdosfs_readdir: unexpected uio from NFS server");
1754 		dpStart = (struct dirent *)
1755 		     (uio->uio_iov->iov_base - (uio->uio_offset - off));
1756 		dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1757 		MALLOC(cookies, u_int *, ncookies * sizeof(u_int),
1758 		       M_TEMP, M_WAITOK);
1759 		for (dp = dpStart, cookiep = cookies;
1760 		     dp < dpEnd;
1761 		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1762 			off += dp->d_reclen;
1763 			*cookiep++ = (u_int) off;
1764 		}
1765 		*ap->a_ncookies = ncookies;
1766 		*ap->a_cookies = cookies;
1767 	}
1768 
1769 	/*
1770 	 * Set the eofflag (NFS uses it)
1771 	 */
1772 	if (ap->a_eofflag)
1773 		if (dep->de_FileSize - (uio->uio_offset - bias) <= 0)
1774 			*ap->a_eofflag = 1;
1775 		else
1776 			*ap->a_eofflag = 0;
1777 
1778 	return error;
1779 }
1780 
1781 /*
1782  * DOS filesystems don't know what symlinks are.
1783  */
1784 static int
1785 msdosfs_readlink(ap)
1786 	struct vop_readlink_args /* {
1787 		struct vnode *a_vp;
1788 		struct uio *a_uio;
1789 		struct ucred *a_cred;
1790 	} */ *ap;
1791 {
1792 	return EINVAL;
1793 }
1794 
1795 static int
1796 msdosfs_abortop(ap)
1797 	struct vop_abortop_args /* {
1798 		struct vnode *a_dvp;
1799 		struct componentname *a_cnp;
1800 	} */ *ap;
1801 {
1802 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1803 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
1804 	return 0;
1805 }
1806 
1807 static int
1808 msdosfs_lock(ap)
1809 	struct vop_lock_args /* {
1810 		struct vnode *a_vp;
1811 	} */ *ap;
1812 {
1813 	struct denode *dep = VTODE(ap->a_vp);
1814 
1815 	while (dep->de_flag & DE_LOCKED) {
1816 		dep->de_flag |= DE_WANTED;
1817 		if (dep->de_lockholder == curproc->p_pid)
1818 			panic("msdosfs_lock: locking against myself");
1819 		dep->de_lockwaiter = curproc->p_pid;
1820 		(void) tsleep((caddr_t) dep, PINOD, "msdlck", 0);
1821 	}
1822 	dep->de_lockwaiter = 0;
1823 	dep->de_lockholder = curproc->p_pid;
1824 	dep->de_flag |= DE_LOCKED;
1825 	return 0;
1826 }
1827 
1828 static int
1829 msdosfs_unlock(ap)
1830 	struct vop_unlock_args /* {
1831 		struct vnode *vp;
1832 	} */ *ap;
1833 {
1834 	struct denode *dep = VTODE(ap->a_vp);
1835 
1836 	if (!(dep->de_flag & DE_LOCKED))
1837 		panic("msdosfs_unlock: denode not locked");
1838 	dep->de_lockholder = 0;
1839 	dep->de_flag &= ~DE_LOCKED;
1840 	if (dep->de_flag & DE_WANTED) {
1841 		dep->de_flag &= ~DE_WANTED;
1842 		wakeup((caddr_t) dep);
1843 	}
1844 	return 0;
1845 }
1846 
1847 static int
1848 msdosfs_islocked(ap)
1849 	struct vop_islocked_args /* {
1850 		struct vnode *a_vp;
1851 	} */ *ap;
1852 {
1853 	return VTODE(ap->a_vp)->de_flag & DE_LOCKED ? 1 : 0;
1854 }
1855 
1856 /*
1857  * vp  - address of vnode file the file
1858  * bn  - which cluster we are interested in mapping to a filesystem block number.
1859  * vpp - returns the vnode for the block special file holding the filesystem
1860  *	 containing the file of interest
1861  * bnp - address of where to return the filesystem relative block number
1862  */
1863 static int
1864 msdosfs_bmap(ap)
1865 	struct vop_bmap_args /* {
1866 		struct vnode *a_vp;
1867 		daddr_t a_bn;
1868 		struct vnode **a_vpp;
1869 		daddr_t *a_bnp;
1870 		int *a_runp;
1871 		int *a_runb;
1872 	} */ *ap;
1873 {
1874 	struct denode *dep = VTODE(ap->a_vp);
1875 
1876 	if (ap->a_vpp != NULL)
1877 		*ap->a_vpp = dep->de_devvp;
1878 	if (ap->a_bnp == NULL)
1879 		return 0;
1880 	if (ap->a_runp) {
1881 		/*
1882 		 * Sequential clusters should be counted here.
1883 		 */
1884 		*ap->a_runp = 0;
1885 	}
1886 	if (ap->a_runb) {
1887 		*ap->a_runb = 0;
1888 	}
1889 	return pcbmap(dep, ap->a_bn, ap->a_bnp, 0);
1890 }
1891 
1892 static int
1893 msdosfs_reallocblks(ap)
1894 	struct vop_reallocblks_args /* {
1895 		struct vnode *a_vp;
1896 		struct cluster_save *a_buflist;
1897 	} */ *ap;
1898 {
1899 	/* Currently no support for clustering */		/* XXX */
1900 	return ENOSPC;
1901 }
1902 
1903 static int
1904 msdosfs_strategy(ap)
1905 	struct vop_strategy_args /* {
1906 		struct buf *a_bp;
1907 	} */ *ap;
1908 {
1909 	struct buf *bp = ap->a_bp;
1910 	struct denode *dep = VTODE(bp->b_vp);
1911 	struct vnode *vp;
1912 	int error = 0;
1913 
1914 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1915 		panic("msdosfs_strategy: spec");
1916 	/*
1917 	 * If we don't already know the filesystem relative block number
1918 	 * then get it using pcbmap().  If pcbmap() returns the block
1919 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1920 	 * don't allow files with holes, so we shouldn't ever see this.
1921 	 */
1922 	if (bp->b_blkno == bp->b_lblkno) {
1923 		error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0);
1924 		if (error)
1925 			bp->b_blkno = -1;
1926 		if (bp->b_blkno == -1)
1927 			clrbuf(bp);
1928 	}
1929 	if (bp->b_blkno == -1) {
1930 		biodone(bp);
1931 		return error;
1932 	}
1933 #ifdef DIAGNOSTIC
1934 #endif
1935 	/*
1936 	 * Read/write the block from/to the disk that contains the desired
1937 	 * file block.
1938 	 */
1939 	vp = dep->de_devvp;
1940 	bp->b_dev = vp->v_rdev;
1941 	VOCALL(vp->v_op, VOFFSET(vop_strategy), ap);
1942 	return 0;
1943 }
1944 
1945 static int
1946 msdosfs_print(ap)
1947 	struct vop_print_args /* {
1948 		struct vnode *vp;
1949 	} */ *ap;
1950 {
1951 	struct denode *dep = VTODE(ap->a_vp);
1952 
1953 	printf(
1954 	    "tag VT_MSDOSFS, startcluster %d, dircluster %ld, diroffset %ld ",
1955 	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1956 	printf(" dev %d, %d, %s\n",
1957 	       major(dep->de_dev), minor(dep->de_dev),
1958 	       dep->de_flag & DE_LOCKED ? "(LOCKED)" : "");
1959 	if (dep->de_lockholder) {
1960 		printf("    owner pid %d", (int)dep->de_lockholder);
1961 		if (dep->de_lockwaiter)
1962 			printf(" waiting pid %d", (int)dep->de_lockwaiter);
1963 		printf("\n");
1964 	}
1965 	return 0;
1966 }
1967 
1968 static int
1969 msdosfs_advlock(ap)
1970 	struct vop_advlock_args /* {
1971 		struct vnode *a_vp;
1972 		caddr_t a_id;
1973 		int a_op;
1974 		struct flock *a_fl;
1975 		int a_flags;
1976 	} */ *ap;
1977 {
1978 	return EINVAL;		/* we don't do locking yet		 */
1979 }
1980 
1981 static int
1982 msdosfs_pathconf(ap)
1983 	struct vop_pathconf_args /* {
1984 		struct vnode *a_vp;
1985 		int a_name;
1986 		int *a_retval;
1987 	} */ *ap;
1988 {
1989 	switch (ap->a_name) {
1990 	case _PC_LINK_MAX:
1991 		*ap->a_retval = 1;
1992 		return 0;
1993 	case _PC_NAME_MAX:
1994 		*ap->a_retval = 12;
1995 		return 0;
1996 	case _PC_PATH_MAX:
1997 		*ap->a_retval = PATH_MAX; /* 255? */
1998 		return 0;
1999 	case _PC_CHOWN_RESTRICTED:
2000 		*ap->a_retval = 1;
2001 		return 0;
2002 	case _PC_NO_TRUNC:
2003 		*ap->a_retval = 0;
2004 		return 0;
2005 	default:
2006 		return EINVAL;
2007 	}
2008 }
2009 
2010 /* Global vfs data structures for msdosfs */
2011 vop_t **msdosfs_vnodeop_p;
2012 static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
2013 	{ &vop_default_desc, (vop_t *)vn_default_error },
2014 	{ &vop_lookup_desc, (vop_t *)msdosfs_lookup },		/* lookup */
2015 	{ &vop_create_desc, (vop_t *)msdosfs_create },		/* create */
2016 	{ &vop_mknod_desc, (vop_t *)msdosfs_mknod },		/* mknod */
2017 	{ &vop_open_desc, (vop_t *)msdosfs_open },		/* open */
2018 	{ &vop_close_desc, (vop_t *)msdosfs_close },		/* close */
2019 	{ &vop_access_desc, (vop_t *)msdosfs_access },		/* access */
2020 	{ &vop_getattr_desc, (vop_t *)msdosfs_getattr },	/* getattr */
2021 	{ &vop_setattr_desc, (vop_t *)msdosfs_setattr },	/* setattr */
2022 	{ &vop_read_desc, (vop_t *)msdosfs_read },		/* read */
2023 	{ &vop_write_desc, (vop_t *)msdosfs_write },		/* write */
2024 	{ &vop_ioctl_desc, (vop_t *)msdosfs_ioctl },		/* ioctl */
2025 	{ &vop_select_desc, (vop_t *)msdosfs_select },		/* select */
2026 	{ &vop_mmap_desc, (vop_t *)msdosfs_mmap },		/* mmap */
2027 	{ &vop_fsync_desc, (vop_t *)msdosfs_fsync },		/* fsync */
2028 	{ &vop_seek_desc, (vop_t *)msdosfs_seek },		/* seek */
2029 	{ &vop_remove_desc, (vop_t *)msdosfs_remove },		/* remove */
2030 	{ &vop_link_desc, (vop_t *)msdosfs_link },		/* link */
2031 	{ &vop_rename_desc, (vop_t *)msdosfs_rename },		/* rename */
2032 	{ &vop_mkdir_desc, (vop_t *)msdosfs_mkdir },		/* mkdir */
2033 	{ &vop_rmdir_desc, (vop_t *)msdosfs_rmdir },		/* rmdir */
2034 	{ &vop_symlink_desc, (vop_t *)msdosfs_symlink },	/* symlink */
2035 	{ &vop_readdir_desc, (vop_t *)msdosfs_readdir },	/* readdir */
2036 	{ &vop_readlink_desc, (vop_t *)msdosfs_readlink },	/* readlink */
2037 	{ &vop_abortop_desc, (vop_t *)msdosfs_abortop },	/* abortop */
2038 	{ &vop_inactive_desc, (vop_t *)msdosfs_inactive },	/* inactive */
2039 	{ &vop_reclaim_desc, (vop_t *)msdosfs_reclaim },	/* reclaim */
2040 	{ &vop_lock_desc, (vop_t *)msdosfs_lock },		/* lock */
2041 	{ &vop_unlock_desc, (vop_t *)msdosfs_unlock },		/* unlock */
2042 	{ &vop_bmap_desc, (vop_t *)msdosfs_bmap },		/* bmap */
2043 	{ &vop_strategy_desc, (vop_t *)msdosfs_strategy },	/* strategy */
2044 	{ &vop_print_desc, (vop_t *)msdosfs_print },		/* print */
2045 	{ &vop_islocked_desc, (vop_t *)msdosfs_islocked },	/* islocked */
2046 	{ &vop_pathconf_desc, (vop_t *)msdosfs_pathconf },	/* pathconf */
2047 	{ &vop_advlock_desc, (vop_t *)msdosfs_advlock },	/* advlock */
2048 	{ &vop_reallocblks_desc, (vop_t *)msdosfs_reallocblks },	/* reallocblks */
2049 	{ &vop_bwrite_desc, (vop_t *)vn_bwrite },		/* bwrite */
2050 	{ NULL, NULL }
2051 };
2052 static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
2053 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
2054 
2055 VNODEOP_SET(msdosfs_vnodeop_opv_desc);
2056