xref: /freebsd/sys/fs/msdosfs/msdosfs_vnops.c (revision b71fec07dbcbbd6b5665aef535e57fa10a3e910c)
1 /*	$Id: msdosfs_vnops.c,v 1.31 1996/06/14 11:01:41 asami 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.ts_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 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
735 		    && (de_blk(pmp, uio->uio_offset + uio->uio_resid) > de_blk(pmp, uio->uio_offset)
736 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
737 			/*
738 			 * If either the whole cluster gets written,
739 			 * or we write the cluster from its start beyond EOF,
740 			 * then no need to read data from disk.
741 			 */
742 			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
743 			clrbuf(bp);
744 			/*
745 			 * Do the bmap now, since pcbmap needs buffers
746 			 * for the fat table. (see msdosfs_strategy)
747 			 */
748 			if (!isadir) {
749 				if (bp->b_blkno == bp->b_lblkno) {
750 					error = pcbmap(dep, bp->b_lblkno,
751 							   &bp->b_blkno, 0);
752 					if (error)
753 						bp->b_blkno = -1;
754 				}
755 				if (bp->b_blkno == -1) {
756 					brelse(bp);
757 					if (!error)
758 						error = EIO;		/* XXX */
759 					break;
760 				}
761 			}
762 		} else {
763 			/*
764 			 * The block we need to write into exists, so read it in.
765 			 */
766 			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
767 			if (error)
768 				break;
769 		}
770 
771 		croffset = uio->uio_offset & pmp->pm_crbomask;
772 		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
773 		if (uio->uio_offset + n > dep->de_FileSize) {
774 			dep->de_FileSize = uio->uio_offset + n;
775 			vnode_pager_setsize(vp, dep->de_FileSize);	/* why? */
776 		}
777 		/*
778 		 * Should these vnode_pager_* functions be done on dir
779 		 * files?
780 		 */
781 
782 		/*
783 		 * Copy the data from user space into the buf header.
784 		 */
785 		error = uiomove(bp->b_data + croffset, n, uio);
786 
787 		/*
788 		 * If they want this synchronous then write it and wait for
789 		 * it.  Otherwise, if on a cluster boundary write it
790 		 * asynchronously so we can move on to the next block
791 		 * without delay.  Otherwise do a delayed write because we
792 		 * may want to write somemore into the block later.
793 		 */
794 		if (ioflag & IO_SYNC)
795 			(void) bwrite(bp);
796 		else if (n + croffset == pmp->pm_bpcluster) {
797 			bawrite(bp);
798 		} else
799 			bdwrite(bp);
800 		dep->de_flag |= DE_UPDATE;
801 	} while (error == 0 && uio->uio_resid > 0);
802 
803 	/*
804 	 * If the write failed and they want us to, truncate the file back
805 	 * to the size it was before the write was attempted.
806 	 */
807 errexit:
808 	if (error) {
809 		if (ioflag & IO_UNIT) {
810 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
811 			uio->uio_offset -= resid - uio->uio_resid;
812 			uio->uio_resid = resid;
813 		} else {
814 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
815 			if (uio->uio_resid != resid)
816 				error = 0;
817 		}
818 	} else {
819 		TIMEVAL_TO_TIMESPEC(&time, &ts);
820 		error = deupdat(dep, &ts, 1);
821 	}
822 	return error;
823 }
824 
825 static int
826 msdosfs_ioctl(ap)
827 	struct vop_ioctl_args /* {
828 		struct vnode *a_vp;
829 		int a_command;
830 		caddr_t a_data;
831 		int a_fflag;
832 		struct ucred *a_cred;
833 		struct proc *a_p;
834 	} */ *ap;
835 {
836 	return ENOTTY;
837 }
838 
839 static int
840 msdosfs_select(ap)
841 	struct vop_select_args /* {
842 		struct vnode *a_vp;
843 		int a_which;
844 		int a_fflags;
845 		struct ucred *a_cred;
846 		struct proc *a_p;
847 	} */ *ap;
848 {
849 	return 1;		/* DOS filesystems never block? */
850 }
851 
852 static int
853 msdosfs_mmap(ap)
854 	struct vop_mmap_args /* {
855 		struct vnode *a_vp;
856 		int a_fflags;
857 		struct ucred *a_cred;
858 		struct proc *a_p;
859 	} */ *ap;
860 {
861 	return EINVAL;
862 }
863 
864 /*
865  * Flush the blocks of a file to disk.
866  *
867  * This function is worthless for vnodes that represent directories. Maybe we
868  * could just do a sync if they try an fsync on a directory file.
869  */
870 static int
871 msdosfs_fsync(ap)
872 	struct vop_fsync_args /* {
873 		struct vnode *a_vp;
874 		struct ucred *a_cred;
875 		int a_waitfor;
876 		struct proc *a_p;
877 	} */ *ap;
878 {
879 	register struct vnode *vp = ap->a_vp;
880 	register struct buf *bp;
881 	int wait = ap->a_waitfor == MNT_WAIT;
882 	struct timespec ts;
883 	struct buf *nbp;
884 	int s;
885 
886 	/*
887 	 * Flush all dirty buffers associated with a vnode.
888 	 */
889 loop:
890 	s = splbio();
891 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
892 		nbp = bp->b_vnbufs.le_next;
893 		if ((bp->b_flags & B_BUSY))
894 			continue;
895 		if ((bp->b_flags & B_DELWRI) == 0)
896 			panic("msdosfs_fsync: not dirty");
897 		bremfree(bp);
898 		bp->b_flags |= B_BUSY;
899 		splx(s);
900 		(void) bwrite(bp);
901 		goto loop;
902 	}
903 	while (vp->v_numoutput) {
904 		vp->v_flag |= VBWAIT;
905 		(void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
906 	}
907 #ifdef DIAGNOSTIC
908 	if (vp->v_dirtyblkhd.lh_first) {
909 		vprint("msdosfs_fsync: dirty", vp);
910 		goto loop;
911 	}
912 #endif
913 	splx(s);
914 	TIMEVAL_TO_TIMESPEC(&time, &ts);
915 	return deupdat(VTODE(vp), &ts, wait);
916 }
917 
918 /*
919  * Now the whole work of extending a file is done in the write function.
920  * So nothing to do here.
921  */
922 static int
923 msdosfs_seek(ap)
924 	struct vop_seek_args /* {
925 		struct vnode *a_vp;
926 		off_t a_oldoff;
927 		off_t a_newoff;
928 		struct ucred *a_cred;
929 	} */ *ap;
930 {
931 	return 0;
932 }
933 
934 static int
935 msdosfs_remove(ap)
936 	struct vop_remove_args /* {
937 		struct vnode *a_dvp;
938 		struct vnode *a_vp;
939 		struct componentname *a_cnp;
940 	} */ *ap;
941 {
942 	int error;
943 	struct denode *dep = VTODE(ap->a_vp);
944 	struct denode *ddep = VTODE(ap->a_dvp);
945 
946 	error = removede(ddep,dep);
947 #ifdef MSDOSFS_DEBUG
948 	printf("msdosfs_remove(), dep %08x, v_usecount %d\n", dep, ap->a_vp->v_usecount);
949 #endif
950 	if (ddep == dep)
951 		vrele(ap->a_vp);
952 	else
953 		vput(ap->a_vp);	/* causes msdosfs_inactive() to be called
954 				 * via vrele() */
955 	vput(ap->a_dvp);
956 	return error;
957 }
958 
959 /*
960  * DOS filesystems don't know what links are. But since we already called
961  * msdosfs_lookup() with create and lockparent, the parent is locked so we
962  * have to free it before we return the error.
963  */
964 static int
965 msdosfs_link(ap)
966 	struct vop_link_args /* {
967 		struct vnode *a_tdvp;
968 		struct vnode *a_vp;
969 		struct componentname *a_cnp;
970 	} */ *ap;
971 {
972 	VOP_ABORTOP(ap->a_tdvp, ap->a_cnp);
973 	vput(ap->a_tdvp);
974 	return EOPNOTSUPP;
975 }
976 
977 /*
978  * Renames on files require moving the denode to a new hash queue since the
979  * denode's location is used to compute which hash queue to put the file
980  * in. Unless it is a rename in place.  For example "mv a b".
981  *
982  * What follows is the basic algorithm:
983  *
984  * if (file move) {
985  *	if (dest file exists) {
986  *		remove dest file
987  *	}
988  *	if (dest and src in same directory) {
989  *		rewrite name in existing directory slot
990  *	} else {
991  *		write new entry in dest directory
992  *		update offset and dirclust in denode
993  *		move denode to new hash chain
994  *		clear old directory entry
995  *	}
996  * } else {
997  *	directory move
998  *	if (dest directory exists) {
999  *		if (dest is not empty) {
1000  *			return ENOTEMPTY
1001  *		}
1002  *		remove dest directory
1003  *	}
1004  *	if (dest and src in same directory) {
1005  *		rewrite name in existing entry
1006  *	} else {
1007  *		be sure dest is not a child of src directory
1008  *		write entry in dest directory
1009  *		update "." and ".." in moved directory
1010  *		clear old directory entry for moved directory
1011  *	}
1012  * }
1013  *
1014  * On entry:
1015  *	source's parent directory is unlocked
1016  *	source file or directory is unlocked
1017  *	destination's parent directory is locked
1018  *	destination file or directory is locked if it exists
1019  *
1020  * On exit:
1021  *	all denodes should be released
1022  *
1023  * Notes:
1024  * I'm not sure how the memory containing the pathnames pointed at by the
1025  * componentname structures is freed, there may be some memory bleeding
1026  * for each rename done.
1027  */
1028 static int
1029 msdosfs_rename(ap)
1030 	struct vop_rename_args /* {
1031 		struct vnode *a_fdvp;
1032 		struct vnode *a_fvp;
1033 		struct componentname *a_fcnp;
1034 		struct vnode *a_tdvp;
1035 		struct vnode *a_tvp;
1036 		struct componentname *a_tcnp;
1037 	} */ *ap;
1038 {
1039 	u_char toname[11];
1040 	int error;
1041 	int newparent = 0;
1042 	int sourceisadirectory = 0;
1043 	u_long cn;
1044 	daddr_t bn;
1045 	struct vnode *tvp = ap->a_tvp;
1046 	struct denode *fddep;	/* from file's parent directory	 */
1047 	struct denode *fdep;	/* from file or directory	 */
1048 	struct denode *tddep;	/* to file's parent directory	 */
1049 	struct denode *tdep;	/* to file or directory		 */
1050 	struct msdosfsmount *pmp;
1051 	struct direntry *dotdotp;
1052 	struct direntry *ep;
1053 	struct buf *bp;
1054 
1055 	fddep = VTODE(ap->a_fdvp);
1056 	fdep = VTODE(ap->a_fvp);
1057 	tddep = VTODE(ap->a_tdvp);
1058 	tdep = tvp ? VTODE(tvp) : NULL;
1059 	pmp = fddep->de_pmp;
1060 
1061 	/* Check for cross-device rename */
1062 	if ((ap->a_fvp->v_mount != ap->a_tdvp->v_mount) ||
1063 	    (tvp && (ap->a_fvp->v_mount != tvp->v_mount))) {
1064 		error = EXDEV;
1065 		goto bad;
1066 	}
1067 
1068 	/*
1069 	 * Convert the filename in tcnp into a dos filename. We copy this
1070 	 * into the denode and directory entry for the destination
1071 	 * file/directory.
1072 	 */
1073 	unix2dosfn((u_char *) ap->a_tcnp->cn_nameptr,
1074 		   toname, ap->a_tcnp->cn_namelen);
1075 
1076 	/*
1077 	 * At this point this is the lock state of the denodes:
1078 	 *	fddep referenced
1079 	 *	fdep  referenced
1080 	 *	tddep locked
1081 	 *	tdep  locked if it exists
1082 	 */
1083 
1084 	/*
1085 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
1086 	 * leads to a crippled directory tree.  It's pretty tough to do a
1087 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1088 	 * doesn't work if the ".." entry is missing.
1089 	 */
1090 	if (fdep->de_Attributes & ATTR_DIRECTORY) {
1091 		if ((ap->a_fcnp->cn_namelen == 1
1092 		     && ap->a_fcnp->cn_nameptr[0] == '.')
1093 		    || fddep == fdep
1094 		    || (ap->a_fcnp->cn_flags | ap->a_tcnp->cn_flags)
1095 		       & ISDOTDOT) {
1096 			VOP_ABORTOP(ap->a_tdvp, ap->a_tcnp);
1097 			vput(ap->a_tdvp);
1098 			if (tvp)
1099 				vput(tvp);
1100 			VOP_ABORTOP(ap->a_fdvp, ap->a_fcnp);
1101 			vrele(ap->a_fdvp);
1102 			vrele(ap->a_fvp);
1103 			return EINVAL;
1104 		}
1105 		sourceisadirectory = 1;
1106 	}
1107 
1108 	/*
1109 	 * If we are renaming a directory, and the directory is being moved
1110 	 * to another directory, then we must be sure the destination
1111 	 * directory is not in the subtree of the source directory.  This
1112 	 * could orphan everything under the source directory.
1113 	 * doscheckpath() unlocks the destination's parent directory so we
1114 	 * must look it up again to relock it.
1115 	 */
1116 	if (fddep->de_StartCluster != tddep->de_StartCluster)
1117 		newparent = 1;
1118 	if (sourceisadirectory && newparent) {
1119 		if (tdep) {
1120 			vput(ap->a_tvp);
1121 			tdep = NULL;
1122 		}
1123 		/* doscheckpath() vput()'s tddep */
1124 		error = doscheckpath(fdep, tddep);
1125 		tddep = NULL;
1126 		if (error)
1127 			goto bad;
1128 		if ((ap->a_tcnp->cn_flags & SAVESTART) == 0)
1129 			panic("msdosfs_rename(): lost to startdir");
1130 		error = relookup(ap->a_tdvp, &tvp, ap->a_tcnp);
1131 		if (error)
1132 			goto bad;
1133 		tddep = VTODE(ap->a_tdvp);
1134 		tdep = tvp ? VTODE(tvp) : NULL;
1135 	}
1136 
1137 	/*
1138 	 * If the destination exists, then be sure its type (file or dir)
1139 	 * matches that of the source.  And, if it is a directory make sure
1140 	 * it is empty.  Then delete the destination.
1141 	 */
1142 	if (tdep) {
1143 		if (tdep->de_Attributes & ATTR_DIRECTORY) {
1144 			if (!sourceisadirectory) {
1145 				error = ENOTDIR;
1146 				goto bad;
1147 			}
1148 			if (!dosdirempty(tdep)) {
1149 				error = ENOTEMPTY;
1150 				goto bad;
1151 			}
1152 			cache_purge(DETOV(tddep));
1153 		} else {		/* destination is file */
1154 			if (sourceisadirectory) {
1155 				error = EISDIR;
1156 				goto bad;
1157 			}
1158 		}
1159 		error = removede(tddep,tdep);
1160 		if (error)
1161 			goto bad;
1162 		vput(ap->a_tvp);
1163 		tdep = NULL;
1164 	}
1165 
1166 	/*
1167 	 * If the source and destination are in the same directory then
1168 	 * just read in the directory entry, change the name in the
1169 	 * directory entry and write it back to disk.
1170 	 */
1171 	if (newparent == 0) {
1172 		/* tddep and fddep point to the same denode here */
1173 		VOP_LOCK(ap->a_fvp);	/* ap->a_fdvp is already locked */
1174 		error = readep(fddep->de_pmp, fdep->de_dirclust,
1175 				   fdep->de_diroffset, &bp, &ep);
1176 		if (error) {
1177 			VOP_UNLOCK(ap->a_fvp);
1178 			goto bad;
1179 		}
1180 		bcopy(toname, ep->deName, 11);
1181 		error = bwrite(bp);
1182 		if (error) {
1183 			VOP_UNLOCK(ap->a_fvp);
1184 			goto bad;
1185 		}
1186 		bcopy(toname, fdep->de_Name, 11);	/* update denode */
1187 		/*
1188 		 * fdep locked fddep and tddep point to the same denode
1189 		 * which is locked tdep is NULL
1190 		 */
1191 	} else {
1192 		u_long dirsize = 0L;
1193 
1194 		/*
1195 		 * If the source and destination are in different
1196 		 * directories, then mark the entry in the source directory
1197 		 * as deleted and write a new entry in the destination
1198 		 * directory.  Then move the denode to the correct hash
1199 		 * chain for its new location in the filesystem.  And, if
1200 		 * we moved a directory, then update its .. entry to point
1201 		 * to the new parent directory. If we moved a directory
1202 		 * will also insure that the directory entry on disk has a
1203 		 * filesize of zero.
1204 		 */
1205 		VOP_LOCK(ap->a_fvp);
1206 		bcopy(toname, fdep->de_Name, 11);	/* update denode */
1207 		if (fdep->de_Attributes & ATTR_DIRECTORY) {
1208 			dirsize = fdep->de_FileSize;
1209 			fdep->de_FileSize = 0;
1210 		}
1211 		error = createde(fdep, tddep, (struct denode **) 0);
1212 		if (fdep->de_Attributes & ATTR_DIRECTORY) {
1213 			fdep->de_FileSize = dirsize;
1214 		}
1215 		if (error) {
1216 			/* should put back filename */
1217 			VOP_UNLOCK(ap->a_fvp);
1218 			goto bad;
1219 		}
1220 		VOP_LOCK(ap->a_fdvp);
1221 		error = readep(fddep->de_pmp, fddep->de_fndclust,
1222 				   fddep->de_fndoffset, &bp, &ep);
1223 		if (error) {
1224 			VOP_UNLOCK(ap->a_fvp);
1225 			VOP_UNLOCK(ap->a_fdvp);
1226 			goto bad;
1227 		}
1228 		ep->deName[0] = SLOT_DELETED;
1229 		error = bwrite(bp);
1230 		if (error) {
1231 			VOP_UNLOCK(ap->a_fvp);
1232 			VOP_UNLOCK(ap->a_fdvp);
1233 			goto bad;
1234 		}
1235 		if (!sourceisadirectory) {
1236 			fdep->de_dirclust = tddep->de_fndclust;
1237 			fdep->de_diroffset = tddep->de_fndoffset;
1238 			reinsert(fdep);
1239 		}
1240 		VOP_UNLOCK(ap->a_fdvp);
1241 	}
1242 	/* fdep is still locked here */
1243 
1244 	/*
1245 	 * If we moved a directory to a new parent directory, then we must
1246 	 * fixup the ".." entry in the moved directory.
1247 	 */
1248 	if (sourceisadirectory && newparent) {
1249 		cn = fdep->de_StartCluster;
1250 		if (cn == MSDOSFSROOT) {
1251 			/* this should never happen */
1252 			panic("msdosfs_rename(): updating .. in root directory?");
1253 		} else {
1254 			bn = cntobn(pmp, cn);
1255 		}
1256 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1257 			      NOCRED, &bp);
1258 		if (error) {
1259 			/* should really panic here, fs is corrupt */
1260 			VOP_UNLOCK(ap->a_fvp);
1261 			goto bad;
1262 		}
1263 		dotdotp = (struct direntry *) bp->b_data + 1;
1264 		putushort(dotdotp->deStartCluster, tddep->de_StartCluster);
1265 		error = bwrite(bp);
1266 		VOP_UNLOCK(ap->a_fvp);
1267 		if (error) {
1268 			/* should really panic here, fs is corrupt */
1269 			goto bad;
1270 		}
1271 	} else
1272 		VOP_UNLOCK(ap->a_fvp);
1273 bad:	;
1274 	vrele(DETOV(fdep));
1275 	vrele(DETOV(fddep));
1276 	if (tdep)
1277 		vput(DETOV(tdep));
1278 	if (tddep)
1279 		vput(DETOV(tddep));
1280 	return error;
1281 }
1282 
1283 static struct {
1284 	struct direntry dot;
1285 	struct direntry dotdot;
1286 }      dosdirtemplate = {
1287     {
1288 	".       ", "   ",		/* the . entry */
1289 	ATTR_DIRECTORY,			/* file attribute */
1290 	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},	/* resevered */
1291 	{210, 4}, {210, 4},		/* time and date */
1292 	{0, 0},				/* startcluster */
1293 	{0, 0, 0, 0},			/* filesize */
1294     },{
1295 	"..      ", "   ",		/* the .. entry */
1296 	ATTR_DIRECTORY,			/* file attribute */
1297 	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},	/* resevered */
1298 	{210, 4}, {210, 4},		/* time and date */
1299 	{0, 0},				/* startcluster */
1300 	{0, 0, 0, 0},			/* filesize */
1301     }
1302 };
1303 
1304 static int
1305 msdosfs_mkdir(ap)
1306 	struct vop_mkdir_args /* {
1307 		struct vnode *a_dvp;
1308 		struvt vnode **a_vpp;
1309 		struvt componentname *a_cnp;
1310 		struct vattr *a_vap;
1311 	} */ *ap;
1312 {
1313 	int bn;
1314 	int error;
1315 	u_long newcluster;
1316 	struct denode *pdep;
1317 	struct denode *ndep;
1318 	struct direntry *denp;
1319 	struct denode ndirent;
1320 	struct msdosfsmount *pmp;
1321 	struct buf *bp;
1322 	struct timespec ts;
1323 	u_short dDate, dTime;
1324 
1325 	pdep = VTODE(ap->a_dvp);
1326 
1327 	/*
1328 	 * If this is the root directory and there is no space left we
1329 	 * can't do anything.  This is because the root directory can not
1330 	 * change size.
1331 	 */
1332 	if (pdep->de_StartCluster == MSDOSFSROOT && pdep->de_fndclust == (u_long)-1) {
1333 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1334 		vput(ap->a_dvp);
1335 		return ENOSPC;
1336 	}
1337 
1338 	pmp = pdep->de_pmp;
1339 
1340 	/*
1341 	 * Allocate a cluster to hold the about to be created directory.
1342 	 */
1343 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1344 	if (error) {
1345 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1346 		vput(ap->a_dvp);
1347 		return error;
1348 	}
1349 
1350 	/*
1351 	 * Now fill the cluster with the "." and ".." entries. And write
1352 	 * the cluster to disk.  This way it is there for the parent
1353 	 * directory to be pointing at if there were a crash.
1354 	 */
1355 	bn = cntobn(pmp, newcluster);
1356 	/* always succeeds */
1357 	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1358 	bzero(bp->b_data, pmp->pm_bpcluster);
1359 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1360 	denp = (struct direntry *) bp->b_data;
1361 	putushort(denp->deStartCluster, newcluster);
1362 	TIMEVAL_TO_TIMESPEC(&time, &ts);
1363 	unix2dostime(&ts, &dDate, &dTime);
1364 	putushort(denp->deDate, dDate);
1365 	putushort(denp->deTime, dTime);
1366 	denp++;
1367 	putushort(denp->deStartCluster, pdep->de_StartCluster);
1368 	putushort(denp->deDate, dDate);
1369 	putushort(denp->deTime, dTime);
1370 	error = bwrite(bp);
1371 	if (error) {
1372 		clusterfree(pmp, newcluster, NULL);
1373 		free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1374 		vput(ap->a_dvp);
1375 		return error;
1376 	}
1377 
1378 	/*
1379 	 * Now build up a directory entry pointing to the newly allocated
1380 	 * cluster.  This will be written to an empty slot in the parent
1381 	 * directory.
1382 	 */
1383 	ndep = &ndirent;
1384 	bzero(ndep, sizeof(*ndep));
1385 	unix2dosfn((u_char *)ap->a_cnp->cn_nameptr,
1386 		   ndep->de_Name, ap->a_cnp->cn_namelen);
1387 	TIMEVAL_TO_TIMESPEC(&time, &ts);
1388 	unix2dostime(&ts, &ndep->de_Date, &ndep->de_Time);
1389 	ndep->de_StartCluster = newcluster;
1390 	ndep->de_Attributes = ATTR_DIRECTORY;
1391 
1392 	error = createde(ndep, pdep, &ndep);
1393 	if (error) {
1394 		clusterfree(pmp, newcluster, NULL);
1395 	} else {
1396 		*ap->a_vpp = DETOV(ndep);
1397 	}
1398 	free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1399 #ifdef MSDOSFS_DEBUG
1400 	printf("msdosfs_mkdir(): vput(%08x)\n", ap->a_dvp);
1401 #endif
1402 	vput(ap->a_dvp);
1403 	return error;
1404 }
1405 
1406 static int
1407 msdosfs_rmdir(ap)
1408 	struct vop_rmdir_args /* {
1409 		struct vnode *a_dvp;
1410 		struct vnode *a_vp;
1411 		struct componentname *a_cnp;
1412 	} */ *ap;
1413 {
1414 	struct denode *ddep;
1415 	struct denode *dep;
1416 	int error = 0;
1417 
1418 	ddep = VTODE(ap->a_dvp);	/* parent dir of dir to delete	 */
1419 	dep = VTODE(ap->a_vp);/* directory to delete	 */
1420 
1421 	/*
1422 	 * Don't let "rmdir ." go thru.
1423 	 */
1424 	if (ddep == dep) {
1425 		vrele(ap->a_vp);
1426 		vput(ap->a_vp);
1427 		return EINVAL;
1428 	}
1429 
1430 	/*
1431 	 * Be sure the directory being deleted is empty.
1432 	 */
1433 	if (dosdirempty(dep) == 0) {
1434 		error = ENOTEMPTY;
1435 		goto out;
1436 	}
1437 
1438 	/*
1439 	 * Delete the entry from the directory.  For dos filesystems this
1440 	 * gets rid of the directory entry on disk, the in memory copy
1441 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1442 	 * being found by deget().  When the vput() on dep is done we give
1443 	 * up access and eventually msdosfs_reclaim() will be called which
1444 	 * will remove it from the denode cache.
1445 	 */
1446 	error = removede(ddep,dep);
1447 	if (error)
1448 		goto out;
1449 
1450 	/*
1451 	 * This is where we decrement the link count in the parent
1452 	 * directory.  Since dos filesystems don't do this we just purge
1453 	 * the name cache and let go of the parent directory denode.
1454 	 */
1455 	cache_purge(DETOV(ddep));
1456 	vput(ap->a_dvp);
1457 	ap->a_dvp = NULL;
1458 
1459 	/*
1460 	 * Truncate the directory that is being deleted.
1461 	 */
1462 	error = detrunc(dep, (u_long) 0, IO_SYNC, NOCRED, NULL);
1463 	cache_purge(DETOV(dep));
1464 
1465 out:	;
1466 	if (ap->a_dvp)
1467 		vput(ap->a_dvp);
1468 	vput(ap->a_vp);
1469 	return error;
1470 }
1471 
1472 /*
1473  * DOS filesystems don't know what symlinks are.
1474  */
1475 static int
1476 msdosfs_symlink(ap)
1477 	struct vop_symlink_args /* {
1478 		struct vnode *a_dvp;
1479 		struct vnode **a_vpp;
1480 		struct componentname *a_cnp;
1481 		struct vattr *a_vap;
1482 		char *a_target;
1483 	} */ *ap;
1484 {
1485 	free(ap->a_cnp->cn_pnbuf, M_NAMEI);
1486 	vput(ap->a_dvp);
1487 	return EINVAL;
1488 }
1489 
1490 /*
1491  * Dummy dirents to simulate the "." and ".." entries of the root directory
1492  * in a dos filesystem.  Dos doesn't provide these. Note that each entry
1493  * must be the same size as a dos directory entry (32 bytes).
1494  */
1495 static struct dos_dirent {
1496 	u_long d_fileno;
1497 	u_short d_reclen;
1498 	u_char d_type;
1499 	u_char d_namlen;
1500 	u_char d_name[24];
1501 }          rootdots[2] = {
1502 
1503 	{
1504 		1,		/* d_fileno			 */
1505 		sizeof(struct direntry),	/* d_reclen			 */
1506 		DT_DIR,		/* d_type			 */
1507 		1,		/* d_namlen			 */
1508 		"."		/* d_name			 */
1509 	},
1510 	{
1511 		1,		/* d_fileno			 */
1512 		sizeof(struct direntry),	/* d_reclen			 */
1513 		DT_DIR,		/* d_type			 */
1514 		2,		/* d_namlen			 */
1515 		".."		/* d_name			 */
1516 	}
1517 };
1518 
1519 static int
1520 msdosfs_readdir(ap)
1521 	struct vop_readdir_args /* {
1522 		struct vnode *a_vp;
1523 		struct uio *a_uio;
1524 		struct ucred *a_cred;
1525 		int *a_eofflag;
1526 		u_int *a_cookies;
1527 		int a_ncookies;
1528 	} */ *ap;
1529 {
1530 	int error = 0;
1531 	int diff;
1532 	char pushout;
1533 	long n;
1534 	long on;
1535 	long lost;
1536 	long count;
1537 	u_long cn;
1538 	u_long fileno;
1539 	long bias = 0;
1540 	daddr_t bn;
1541 	daddr_t lbn;
1542 	struct buf *bp;
1543 	struct denode *dep = VTODE(ap->a_vp);
1544 	struct msdosfsmount *pmp = dep->de_pmp;
1545 	struct direntry *dentp;
1546 	struct dirent *prev;
1547 	struct dirent *crnt;
1548 	u_char dirbuf[512];	/* holds converted dos directories */
1549 	struct uio *uio = ap->a_uio;
1550 	off_t off;
1551 	int ncookies = 0;
1552 
1553 #ifdef MSDOSFS_DEBUG
1554 	printf("msdosfs_readdir(): vp %08x, uio %08x, cred %08x, eofflagp %08x\n",
1555 	       ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1556 #endif
1557 
1558 	/*
1559 	 * msdosfs_readdir() won't operate properly on regular files since
1560 	 * it does i/o only with the the filesystem vnode, and hence can
1561 	 * retrieve the wrong block from the buffer cache for a plain file.
1562 	 * So, fail attempts to readdir() on a plain file.
1563 	 */
1564 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1565 		return ENOTDIR;
1566 
1567 	/*
1568 	 * If the user buffer is smaller than the size of one dos directory
1569 	 * entry or the file offset is not a multiple of the size of a
1570 	 * directory entry, then we fail the read.
1571 	 */
1572 	count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1573 	lost = uio->uio_resid - count;
1574 	if (count < sizeof(struct direntry) ||
1575 	    (uio->uio_offset & (sizeof(struct direntry) - 1)))
1576 		return EINVAL;
1577 	uio->uio_resid = count;
1578 	uio->uio_iov->iov_len = count;
1579 	off = uio->uio_offset;
1580 
1581 	/*
1582 	 * If they are reading from the root directory then, we simulate
1583 	 * the . and .. entries since these don't exist in the root
1584 	 * directory.  We also set the offset bias to make up for having to
1585 	 * simulate these entries. By this I mean that at file offset 64 we
1586 	 * read the first entry in the root directory that lives on disk.
1587 	 */
1588 	if (dep->de_StartCluster == MSDOSFSROOT) {
1589 		/*
1590 		 * printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1591 		 *	  uio->uio_offset);
1592 		 */
1593 		bias = 2 * sizeof(struct direntry);
1594 		if (uio->uio_offset < 2 * sizeof(struct direntry)) {
1595 			if (uio->uio_offset
1596 			    && uio->uio_offset != sizeof(struct direntry)) {
1597 				error = EINVAL;
1598 				goto out;
1599 			}
1600 			n = 1;
1601 			if (!uio->uio_offset) {
1602 				n = 2;
1603 				ncookies++;
1604 			}
1605 			ncookies++;
1606 			error = uiomove((char *) rootdots + uio->uio_offset,
1607 					n * sizeof(struct direntry), uio);
1608 		}
1609 	}
1610 	while (!error && uio->uio_resid > 0) {
1611 		lbn = (uio->uio_offset - bias) >> pmp->pm_cnshift;
1612 		on = (uio->uio_offset - bias) & pmp->pm_crbomask;
1613 		n = min((u_long) (pmp->pm_bpcluster - on), uio->uio_resid);
1614 		diff = dep->de_FileSize - (uio->uio_offset - bias);
1615 		if (diff <= 0)
1616 			break;
1617 		if (diff < n)
1618 			n = diff;
1619 		error = pcbmap(dep, lbn, &bn, &cn);
1620 		if (error)
1621 			break;
1622 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, NOCRED, &bp);
1623 		n = min(n, pmp->pm_bpcluster - bp->b_resid);
1624 		if (error) {
1625 			brelse(bp);
1626 			return error;
1627 		}
1628 
1629 		/*
1630 		 * code to convert from dos directory entries to ufs
1631 		 * directory entries
1632 		 */
1633 		pushout = 0;
1634 		dentp = (struct direntry *)(bp->b_data + on);
1635 		prev = 0;
1636 		crnt = (struct dirent *) dirbuf;
1637 		while ((char *) dentp < bp->b_data + on + n) {
1638 			/*
1639 			 * printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1640 			 *	  dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1641 			 */
1642 			/*
1643 			 * If we have an empty entry or a slot from a
1644 			 * deleted file, or a volume label entry just
1645 			 * concatenate its space onto the end of the
1646 			 * previous entry or, manufacture an empty entry if
1647 			 * there is no previous entry.
1648 			 */
1649 			if (dentp->deName[0] == SLOT_EMPTY ||
1650 			    dentp->deName[0] == SLOT_DELETED ||
1651 			    (dentp->deAttributes & ATTR_VOLUME)) {
1652 				if (prev) {
1653 					prev->d_reclen += sizeof(struct direntry);
1654 				} else {
1655 					prev = crnt;
1656 					prev->d_fileno = 0;
1657 					prev->d_reclen = sizeof(struct direntry);
1658 					prev->d_type = DT_UNKNOWN;
1659 					prev->d_namlen = 0;
1660 					prev->d_name[0] = 0;
1661 					ncookies++;
1662 				}
1663 			} else {
1664 				/*
1665 				 * this computation of d_fileno must match
1666 				 * the computation of va_fileid in
1667 				 * msdosfs_getattr
1668 				 */
1669 				if (dentp->deAttributes & ATTR_DIRECTORY) {
1670 					/* if this is the root directory */
1671 					fileno = getushort(dentp->deStartCluster);
1672 					if (fileno == MSDOSFSROOT)
1673 						fileno = 1;
1674 				} else {
1675 					/*
1676 					 * if the file's dirent lives in
1677 					 * root dir
1678 					 */
1679 					if ((fileno = cn) == MSDOSFSROOT)
1680 						fileno = 1;
1681 					fileno = (fileno << 16) |
1682 					    ((dentp - (struct direntry *) bp->b_data) & 0xffff);
1683 				}
1684 				crnt->d_fileno = fileno;
1685 				crnt->d_reclen = sizeof(struct direntry);
1686 				crnt->d_type = (dentp->deAttributes & ATTR_DIRECTORY)
1687 					         ? DT_DIR : DT_REG;
1688 				crnt->d_namlen = dos2unixfn(dentp->deName,
1689 							    (u_char *)crnt->d_name);
1690 				/*
1691 				 * printf("readdir: file %s, fileno %08x, attr %02x, start %08x\n",
1692 				 *	  crnt->d_name, crnt->d_fileno, dentp->deAttributes,
1693 				 *	  dentp->deStartCluster);
1694 				 */
1695 				prev = crnt;
1696 				ncookies++;
1697 			}
1698 			dentp++;
1699 
1700 			crnt = (struct dirent *) ((char *) crnt + sizeof(struct direntry));
1701 			pushout = 1;
1702 
1703 			/*
1704 			 * If our intermediate buffer is full then copy its
1705 			 * contents to user space.  I would just use the
1706 			 * buffer the buf header points to but, I'm afraid
1707 			 * that when we brelse() it someone else might find
1708 			 * it in the cache and think its contents are
1709 			 * valid.  Maybe there is a way to invalidate the
1710 			 * buffer before brelse()'ing it.
1711 			 */
1712 			if ((u_char *) crnt >= &dirbuf[sizeof dirbuf]) {
1713 				pushout = 0;
1714 				error = uiomove(dirbuf, sizeof(dirbuf), uio);
1715 				if (error)
1716 					break;
1717 				prev = 0;
1718 				crnt = (struct dirent *) dirbuf;
1719 			}
1720 		}
1721 		if (pushout) {
1722 			pushout = 0;
1723 			error = uiomove(dirbuf, (char *) crnt - (char *) dirbuf,
1724 			    uio);
1725 		}
1726 
1727 #if 0
1728 		/*
1729 		 * If we have read everything from this block or have read
1730 		 * to end of file then we are done with this block.  Mark
1731 		 * it to say the buffer can be reused if need be.
1732 		 */
1733 		if (n + on == pmp->pm_bpcluster ||
1734 		    (uio->uio_offset - bias) == dep->de_FileSize)
1735 			bp->b_flags |= B_AGE;
1736 #endif /* if 0 */
1737 		brelse(bp);
1738 		if (n == 0)
1739 			break;
1740 	}
1741 out:	;
1742 	uio->uio_resid += lost;
1743 	if (!error && ap->a_ncookies != NULL) {
1744 		struct dirent* dpStart;
1745 		struct dirent* dpEnd;
1746 		struct dirent* dp;
1747 		u_int *cookies;
1748 		u_int *cookiep;
1749 
1750 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1751 			panic("msdosfs_readdir: unexpected uio from NFS server");
1752 		dpStart = (struct dirent *)
1753 		     (uio->uio_iov->iov_base - (uio->uio_offset - off));
1754 		dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1755 		MALLOC(cookies, u_int *, ncookies * sizeof(u_int),
1756 		       M_TEMP, M_WAITOK);
1757 		for (dp = dpStart, cookiep = cookies;
1758 		     dp < dpEnd;
1759 		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1760 			off += dp->d_reclen;
1761 			*cookiep++ = (u_int) off;
1762 		}
1763 		*ap->a_ncookies = ncookies;
1764 		*ap->a_cookies = cookies;
1765 	}
1766 
1767 	/*
1768 	 * Set the eofflag (NFS uses it)
1769 	 */
1770 	if (ap->a_eofflag)
1771 		if (dep->de_FileSize - (uio->uio_offset - bias) <= 0)
1772 			*ap->a_eofflag = 1;
1773 		else
1774 			*ap->a_eofflag = 0;
1775 
1776 	return error;
1777 }
1778 
1779 /*
1780  * DOS filesystems don't know what symlinks are.
1781  */
1782 static int
1783 msdosfs_readlink(ap)
1784 	struct vop_readlink_args /* {
1785 		struct vnode *a_vp;
1786 		struct uio *a_uio;
1787 		struct ucred *a_cred;
1788 	} */ *ap;
1789 {
1790 	return EINVAL;
1791 }
1792 
1793 static int
1794 msdosfs_abortop(ap)
1795 	struct vop_abortop_args /* {
1796 		struct vnode *a_dvp;
1797 		struct componentname *a_cnp;
1798 	} */ *ap;
1799 {
1800 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1801 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
1802 	return 0;
1803 }
1804 
1805 static int
1806 msdosfs_lock(ap)
1807 	struct vop_lock_args /* {
1808 		struct vnode *a_vp;
1809 	} */ *ap;
1810 {
1811 	struct denode *dep = VTODE(ap->a_vp);
1812 
1813 	while (dep->de_flag & DE_LOCKED) {
1814 		dep->de_flag |= DE_WANTED;
1815 		if (dep->de_lockholder == curproc->p_pid)
1816 			panic("msdosfs_lock: locking against myself");
1817 		dep->de_lockwaiter = curproc->p_pid;
1818 		(void) tsleep((caddr_t) dep, PINOD, "msdlck", 0);
1819 	}
1820 	dep->de_lockwaiter = 0;
1821 	dep->de_lockholder = curproc->p_pid;
1822 	dep->de_flag |= DE_LOCKED;
1823 	return 0;
1824 }
1825 
1826 static int
1827 msdosfs_unlock(ap)
1828 	struct vop_unlock_args /* {
1829 		struct vnode *vp;
1830 	} */ *ap;
1831 {
1832 	struct denode *dep = VTODE(ap->a_vp);
1833 
1834 	if (!(dep->de_flag & DE_LOCKED))
1835 		panic("msdosfs_unlock: denode not locked");
1836 	dep->de_lockholder = 0;
1837 	dep->de_flag &= ~DE_LOCKED;
1838 	if (dep->de_flag & DE_WANTED) {
1839 		dep->de_flag &= ~DE_WANTED;
1840 		wakeup((caddr_t) dep);
1841 	}
1842 	return 0;
1843 }
1844 
1845 static int
1846 msdosfs_islocked(ap)
1847 	struct vop_islocked_args /* {
1848 		struct vnode *a_vp;
1849 	} */ *ap;
1850 {
1851 	return VTODE(ap->a_vp)->de_flag & DE_LOCKED ? 1 : 0;
1852 }
1853 
1854 /*
1855  * vp  - address of vnode file the file
1856  * bn  - which cluster we are interested in mapping to a filesystem block number.
1857  * vpp - returns the vnode for the block special file holding the filesystem
1858  *	 containing the file of interest
1859  * bnp - address of where to return the filesystem relative block number
1860  */
1861 static int
1862 msdosfs_bmap(ap)
1863 	struct vop_bmap_args /* {
1864 		struct vnode *a_vp;
1865 		daddr_t a_bn;
1866 		struct vnode **a_vpp;
1867 		daddr_t *a_bnp;
1868 		int *a_runp;
1869 		int *a_runb;
1870 	} */ *ap;
1871 {
1872 	struct denode *dep = VTODE(ap->a_vp);
1873 
1874 	if (ap->a_vpp != NULL)
1875 		*ap->a_vpp = dep->de_devvp;
1876 	if (ap->a_bnp == NULL)
1877 		return 0;
1878 	if (ap->a_runp) {
1879 		/*
1880 		 * Sequential clusters should be counted here.
1881 		 */
1882 		*ap->a_runp = 0;
1883 	}
1884 	if (ap->a_runb) {
1885 		*ap->a_runb = 0;
1886 	}
1887 	return pcbmap(dep, ap->a_bn, ap->a_bnp, 0);
1888 }
1889 
1890 static int
1891 msdosfs_reallocblks(ap)
1892 	struct vop_reallocblks_args /* {
1893 		struct vnode *a_vp;
1894 		struct cluster_save *a_buflist;
1895 	} */ *ap;
1896 {
1897 	/* Currently no support for clustering */		/* XXX */
1898 	return ENOSPC;
1899 }
1900 
1901 static int
1902 msdosfs_strategy(ap)
1903 	struct vop_strategy_args /* {
1904 		struct buf *a_bp;
1905 	} */ *ap;
1906 {
1907 	struct buf *bp = ap->a_bp;
1908 	struct denode *dep = VTODE(bp->b_vp);
1909 	struct vnode *vp;
1910 	int error = 0;
1911 
1912 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1913 		panic("msdosfs_strategy: spec");
1914 	/*
1915 	 * If we don't already know the filesystem relative block number
1916 	 * then get it using pcbmap().  If pcbmap() returns the block
1917 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1918 	 * don't allow files with holes, so we shouldn't ever see this.
1919 	 */
1920 	if (bp->b_blkno == bp->b_lblkno) {
1921 		error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0);
1922 		if (error)
1923 			bp->b_blkno = -1;
1924 		if (bp->b_blkno == -1)
1925 			clrbuf(bp);
1926 	}
1927 	if (bp->b_blkno == -1) {
1928 		biodone(bp);
1929 		return error;
1930 	}
1931 #ifdef DIAGNOSTIC
1932 #endif
1933 	/*
1934 	 * Read/write the block from/to the disk that contains the desired
1935 	 * file block.
1936 	 */
1937 	vp = dep->de_devvp;
1938 	bp->b_dev = vp->v_rdev;
1939 	VOCALL(vp->v_op, VOFFSET(vop_strategy), ap);
1940 	return 0;
1941 }
1942 
1943 static int
1944 msdosfs_print(ap)
1945 	struct vop_print_args /* {
1946 		struct vnode *vp;
1947 	} */ *ap;
1948 {
1949 	struct denode *dep = VTODE(ap->a_vp);
1950 
1951 	printf(
1952 	    "tag VT_MSDOSFS, startcluster %d, dircluster %ld, diroffset %ld ",
1953 	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1954 	printf(" dev %d, %d, %s\n",
1955 	       major(dep->de_dev), minor(dep->de_dev),
1956 	       dep->de_flag & DE_LOCKED ? "(LOCKED)" : "");
1957 	if (dep->de_lockholder) {
1958 		printf("    owner pid %d", (int)dep->de_lockholder);
1959 		if (dep->de_lockwaiter)
1960 			printf(" waiting pid %d", (int)dep->de_lockwaiter);
1961 		printf("\n");
1962 	}
1963 	return 0;
1964 }
1965 
1966 static int
1967 msdosfs_advlock(ap)
1968 	struct vop_advlock_args /* {
1969 		struct vnode *a_vp;
1970 		caddr_t a_id;
1971 		int a_op;
1972 		struct flock *a_fl;
1973 		int a_flags;
1974 	} */ *ap;
1975 {
1976 	return EINVAL;		/* we don't do locking yet		 */
1977 }
1978 
1979 static int
1980 msdosfs_pathconf(ap)
1981 	struct vop_pathconf_args /* {
1982 		struct vnode *a_vp;
1983 		int a_name;
1984 		int *a_retval;
1985 	} */ *ap;
1986 {
1987 	switch (ap->a_name) {
1988 	case _PC_LINK_MAX:
1989 		*ap->a_retval = 1;
1990 		return 0;
1991 	case _PC_NAME_MAX:
1992 		*ap->a_retval = 12;
1993 		return 0;
1994 	case _PC_PATH_MAX:
1995 		*ap->a_retval = PATH_MAX; /* 255? */
1996 		return 0;
1997 	case _PC_CHOWN_RESTRICTED:
1998 		*ap->a_retval = 1;
1999 		return 0;
2000 	case _PC_NO_TRUNC:
2001 		*ap->a_retval = 0;
2002 		return 0;
2003 	default:
2004 		return EINVAL;
2005 	}
2006 }
2007 
2008 /* Global vfs data structures for msdosfs */
2009 vop_t **msdosfs_vnodeop_p;
2010 static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
2011 	{ &vop_default_desc, (vop_t *)vn_default_error },
2012 	{ &vop_lookup_desc, (vop_t *)msdosfs_lookup },		/* lookup */
2013 	{ &vop_create_desc, (vop_t *)msdosfs_create },		/* create */
2014 	{ &vop_mknod_desc, (vop_t *)msdosfs_mknod },		/* mknod */
2015 	{ &vop_open_desc, (vop_t *)msdosfs_open },		/* open */
2016 	{ &vop_close_desc, (vop_t *)msdosfs_close },		/* close */
2017 	{ &vop_access_desc, (vop_t *)msdosfs_access },		/* access */
2018 	{ &vop_getattr_desc, (vop_t *)msdosfs_getattr },	/* getattr */
2019 	{ &vop_setattr_desc, (vop_t *)msdosfs_setattr },	/* setattr */
2020 	{ &vop_read_desc, (vop_t *)msdosfs_read },		/* read */
2021 	{ &vop_write_desc, (vop_t *)msdosfs_write },		/* write */
2022 	{ &vop_ioctl_desc, (vop_t *)msdosfs_ioctl },		/* ioctl */
2023 	{ &vop_select_desc, (vop_t *)msdosfs_select },		/* select */
2024 	{ &vop_mmap_desc, (vop_t *)msdosfs_mmap },		/* mmap */
2025 	{ &vop_fsync_desc, (vop_t *)msdosfs_fsync },		/* fsync */
2026 	{ &vop_seek_desc, (vop_t *)msdosfs_seek },		/* seek */
2027 	{ &vop_remove_desc, (vop_t *)msdosfs_remove },		/* remove */
2028 	{ &vop_link_desc, (vop_t *)msdosfs_link },		/* link */
2029 	{ &vop_rename_desc, (vop_t *)msdosfs_rename },		/* rename */
2030 	{ &vop_mkdir_desc, (vop_t *)msdosfs_mkdir },		/* mkdir */
2031 	{ &vop_rmdir_desc, (vop_t *)msdosfs_rmdir },		/* rmdir */
2032 	{ &vop_symlink_desc, (vop_t *)msdosfs_symlink },	/* symlink */
2033 	{ &vop_readdir_desc, (vop_t *)msdosfs_readdir },	/* readdir */
2034 	{ &vop_readlink_desc, (vop_t *)msdosfs_readlink },	/* readlink */
2035 	{ &vop_abortop_desc, (vop_t *)msdosfs_abortop },	/* abortop */
2036 	{ &vop_inactive_desc, (vop_t *)msdosfs_inactive },	/* inactive */
2037 	{ &vop_reclaim_desc, (vop_t *)msdosfs_reclaim },	/* reclaim */
2038 	{ &vop_lock_desc, (vop_t *)msdosfs_lock },		/* lock */
2039 	{ &vop_unlock_desc, (vop_t *)msdosfs_unlock },		/* unlock */
2040 	{ &vop_bmap_desc, (vop_t *)msdosfs_bmap },		/* bmap */
2041 	{ &vop_strategy_desc, (vop_t *)msdosfs_strategy },	/* strategy */
2042 	{ &vop_print_desc, (vop_t *)msdosfs_print },		/* print */
2043 	{ &vop_islocked_desc, (vop_t *)msdosfs_islocked },	/* islocked */
2044 	{ &vop_pathconf_desc, (vop_t *)msdosfs_pathconf },	/* pathconf */
2045 	{ &vop_advlock_desc, (vop_t *)msdosfs_advlock },	/* advlock */
2046 	{ &vop_reallocblks_desc, (vop_t *)msdosfs_reallocblks },	/* reallocblks */
2047 	{ &vop_bwrite_desc, (vop_t *)vn_bwrite },		/* bwrite */
2048 	{ NULL, NULL }
2049 };
2050 static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
2051 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
2052 
2053 VNODEOP_SET(msdosfs_vnodeop_opv_desc);
2054