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