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