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