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