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