xref: /freebsd/sys/fs/msdosfs/msdosfs_vnops.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
55 #include <sys/kernel.h>
56 #include <sys/stat.h>
57 #include <sys/bio.h>
58 #include <sys/buf.h>
59 #include <sys/proc.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 #include <sys/signalvar.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vnode_pager.h>
70 
71 #include <machine/mutex.h>
72 
73 #include <fs/msdosfs/bpb.h>
74 #include <fs/msdosfs/direntry.h>
75 #include <fs/msdosfs/denode.h>
76 #include <fs/msdosfs/msdosfsmount.h>
77 #include <fs/msdosfs/fat.h>
78 
79 #define	DOS_FILESIZE_MAX	0xffffffff
80 
81 /*
82  * Prototypes for MSDOSFS vnode operations
83  */
84 static int msdosfs_create(struct vop_create_args *);
85 static int msdosfs_mknod(struct vop_mknod_args *);
86 static int msdosfs_close(struct vop_close_args *);
87 static int msdosfs_access(struct vop_access_args *);
88 static int msdosfs_getattr(struct vop_getattr_args *);
89 static int msdosfs_setattr(struct vop_setattr_args *);
90 static int msdosfs_read(struct vop_read_args *);
91 static int msdosfs_write(struct vop_write_args *);
92 static int msdosfs_fsync(struct vop_fsync_args *);
93 static int msdosfs_remove(struct vop_remove_args *);
94 static int msdosfs_link(struct vop_link_args *);
95 static int msdosfs_rename(struct vop_rename_args *);
96 static int msdosfs_mkdir(struct vop_mkdir_args *);
97 static int msdosfs_rmdir(struct vop_rmdir_args *);
98 static int msdosfs_symlink(struct vop_symlink_args *);
99 static int msdosfs_readdir(struct vop_readdir_args *);
100 static int msdosfs_bmap(struct vop_bmap_args *);
101 static int msdosfs_strategy(struct vop_strategy_args *);
102 static int msdosfs_print(struct vop_print_args *);
103 static int msdosfs_pathconf(struct vop_pathconf_args *ap);
104 
105 /*
106  * Some general notes:
107  *
108  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
109  * read/written using the vnode for the filesystem. Blocks that represent
110  * the contents of a file are read/written using the vnode for the file
111  * (including directories when they are read/written as files). This
112  * presents problems for the dos filesystem because data that should be in
113  * an inode (if dos had them) resides in the directory itself.  Since we
114  * must update directory entries without the benefit of having the vnode
115  * for the directory we must use the vnode for the filesystem.  This means
116  * that when a directory is actually read/written (via read, write, or
117  * readdir, or seek) we must use the vnode for the filesystem instead of
118  * the vnode for the directory as would happen in ufs. This is to insure we
119  * retreive the correct block from the buffer cache since the hash value is
120  * based upon the vnode address and the desired block number.
121  */
122 
123 /*
124  * Create a regular file. On entry the directory to contain the file being
125  * created is locked.  We must release before we return. We must also free
126  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
127  * only if the SAVESTART bit in cn_flags is clear on success.
128  */
129 static int
130 msdosfs_create(ap)
131 	struct vop_create_args /* {
132 		struct vnode *a_dvp;
133 		struct vnode **a_vpp;
134 		struct componentname *a_cnp;
135 		struct vattr *a_vap;
136 	} */ *ap;
137 {
138 	struct componentname *cnp = ap->a_cnp;
139 	struct denode ndirent;
140 	struct denode *dep;
141 	struct denode *pdep = VTODE(ap->a_dvp);
142 	struct timespec ts;
143 	int error;
144 
145 #ifdef MSDOSFS_DEBUG
146 	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
147 #endif
148 
149 	/*
150 	 * If this is the root directory and there is no space left we
151 	 * can't do anything.  This is because the root directory can not
152 	 * change size.
153 	 */
154 	if (pdep->de_StartCluster == MSDOSFSROOT
155 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
156 		error = ENOSPC;
157 		goto bad;
158 	}
159 
160 	/*
161 	 * Create a directory entry for the file, then call createde() to
162 	 * have it installed. NOTE: DOS files are always executable.  We
163 	 * use the absence of the owner write bit to make the file
164 	 * readonly.
165 	 */
166 #ifdef DIAGNOSTIC
167 	if ((cnp->cn_flags & HASBUF) == 0)
168 		panic("msdosfs_create: no name");
169 #endif
170 	bzero(&ndirent, sizeof(ndirent));
171 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
172 	if (error)
173 		goto bad;
174 
175 	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
176 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
177 	ndirent.de_LowerCase = 0;
178 	ndirent.de_StartCluster = 0;
179 	ndirent.de_FileSize = 0;
180 	ndirent.de_dev = pdep->de_dev;
181 	ndirent.de_devvp = pdep->de_devvp;
182 	ndirent.de_pmp = pdep->de_pmp;
183 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
184 	getnanotime(&ts);
185 	DETIMES(&ndirent, &ts, &ts, &ts);
186 	error = createde(&ndirent, pdep, &dep, cnp);
187 	if (error)
188 		goto bad;
189 	*ap->a_vpp = DETOV(dep);
190 	return (0);
191 
192 bad:
193 	return (error);
194 }
195 
196 static int
197 msdosfs_mknod(ap)
198 	struct vop_mknod_args /* {
199 		struct vnode *a_dvp;
200 		struct vnode **a_vpp;
201 		struct componentname *a_cnp;
202 		struct vattr *a_vap;
203 	} */ *ap;
204 {
205     return (EINVAL);
206 }
207 
208 static int
209 msdosfs_close(ap)
210 	struct vop_close_args /* {
211 		struct vnode *a_vp;
212 		int a_fflag;
213 		struct ucred *a_cred;
214 		struct thread *a_td;
215 	} */ *ap;
216 {
217 	struct vnode *vp = ap->a_vp;
218 	struct denode *dep = VTODE(vp);
219 	struct timespec ts;
220 
221 	mtx_lock(&vp->v_interlock);
222 	if (vp->v_usecount > 1) {
223 		getnanotime(&ts);
224 		DETIMES(dep, &ts, &ts, &ts);
225 	}
226 	mtx_unlock(&vp->v_interlock);
227 	return 0;
228 }
229 
230 static int
231 msdosfs_access(ap)
232 	struct vop_access_args /* {
233 		struct vnode *a_vp;
234 		int a_mode;
235 		struct ucred *a_cred;
236 		struct thread *a_td;
237 	} */ *ap;
238 {
239 	struct vnode *vp = ap->a_vp;
240 	struct denode *dep = VTODE(ap->a_vp);
241 	struct msdosfsmount *pmp = dep->de_pmp;
242 	mode_t file_mode, mode = ap->a_mode;
243 
244 	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
245 	    ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
246 	file_mode &= pmp->pm_mask;
247 
248 	/*
249 	 * Disallow write attempts on read-only file systems;
250 	 * unless the file is a socket, fifo, or a block or
251 	 * character device resident on the file system.
252 	 */
253 	if (mode & VWRITE) {
254 		switch (vp->v_type) {
255 		case VDIR:
256 		case VLNK:
257 		case VREG:
258 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
259 				return (EROFS);
260 			break;
261 		default:
262 			break;
263 		}
264 	}
265 
266 	return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
267 	    ap->a_mode, ap->a_cred, NULL));
268 }
269 
270 static int
271 msdosfs_getattr(ap)
272 	struct vop_getattr_args /* {
273 		struct vnode *a_vp;
274 		struct vattr *a_vap;
275 		struct ucred *a_cred;
276 		struct thread *a_td;
277 	} */ *ap;
278 {
279 	struct denode *dep = VTODE(ap->a_vp);
280 	struct msdosfsmount *pmp = dep->de_pmp;
281 	struct vattr *vap = ap->a_vap;
282 	mode_t mode;
283 	struct timespec ts;
284 	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
285 	u_long fileid;
286 
287 	getnanotime(&ts);
288 	DETIMES(dep, &ts, &ts, &ts);
289 	vap->va_fsid = dev2udev(dep->de_dev);
290 	/*
291 	 * The following computation of the fileid must be the same as that
292 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
293 	 * doesn't work.
294 	 */
295 	if (dep->de_Attributes & ATTR_DIRECTORY) {
296 		fileid = cntobn(pmp, dep->de_StartCluster) * dirsperblk;
297 		if (dep->de_StartCluster == MSDOSFSROOT)
298 			fileid = 1;
299 	} else {
300 		fileid = cntobn(pmp, dep->de_dirclust) * dirsperblk;
301 		if (dep->de_dirclust == MSDOSFSROOT)
302 			fileid = roottobn(pmp, 0) * dirsperblk;
303 		fileid += dep->de_diroffset / sizeof(struct direntry);
304 	}
305 	vap->va_fileid = fileid;
306 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
307 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
308 	else
309 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
310 	vap->va_mode = mode & pmp->pm_mask;
311 	vap->va_uid = pmp->pm_uid;
312 	vap->va_gid = pmp->pm_gid;
313 	vap->va_nlink = 1;
314 	vap->va_rdev = 0;
315 	vap->va_size = dep->de_FileSize;
316 	dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
317 	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
318 		dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
319 		dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
320 	} else {
321 		vap->va_atime = vap->va_mtime;
322 		vap->va_ctime = vap->va_mtime;
323 	}
324 	vap->va_flags = 0;
325 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
326 		vap->va_flags |= SF_ARCHIVED;
327 	vap->va_gen = 0;
328 	vap->va_blocksize = pmp->pm_bpcluster;
329 	vap->va_bytes =
330 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
331 	vap->va_type = ap->a_vp->v_type;
332 	vap->va_filerev = dep->de_modrev;
333 	return (0);
334 }
335 
336 static int
337 msdosfs_setattr(ap)
338 	struct vop_setattr_args /* {
339 		struct vnode *a_vp;
340 		struct vattr *a_vap;
341 		struct ucred *a_cred;
342 		struct thread *a_td;
343 	} */ *ap;
344 {
345 	struct vnode *vp = ap->a_vp;
346 	struct denode *dep = VTODE(ap->a_vp);
347 	struct msdosfsmount *pmp = dep->de_pmp;
348 	struct vattr *vap = ap->a_vap;
349 	struct ucred *cred = ap->a_cred;
350 	int error = 0;
351 
352 #ifdef MSDOSFS_DEBUG
353 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
354 	    ap->a_vp, vap, cred, ap->a_td);
355 #endif
356 
357 	/*
358 	 * Check for unsettable attributes.
359 	 */
360 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
361 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
362 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
363 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
364 #ifdef MSDOSFS_DEBUG
365 		printf("msdosfs_setattr(): returning EINVAL\n");
366 		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
367 		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
368 		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
369 		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
370 		printf("    va_uid %x, va_gid %x\n",
371 		    vap->va_uid, vap->va_gid);
372 #endif
373 		return (EINVAL);
374 	}
375 	if (vap->va_flags != VNOVAL) {
376 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
377 			return (EROFS);
378 		if (cred->cr_uid != pmp->pm_uid &&
379 		    (error = suser_xxx(cred, ap->a_td->td_proc, PRISON_ROOT)))
380 			return (error);
381 		/*
382 		 * We are very inconsistent about handling unsupported
383 		 * attributes.  We ignored the access time and the
384 		 * read and execute bits.  We were strict for the other
385 		 * attributes.
386 		 *
387 		 * Here we are strict, stricter than ufs in not allowing
388 		 * users to attempt to set SF_SETTABLE bits or anyone to
389 		 * set unsupported bits.  However, we ignore attempts to
390 		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
391 		 * sensible file system attempts it a lot.
392 		 */
393 		if (suser_xxx(cred, ap->a_td->td_proc, PRISON_ROOT)) {
394 			if (vap->va_flags & SF_SETTABLE)
395 				return EPERM;
396 		}
397 		if (vap->va_flags & ~SF_ARCHIVED)
398 			return EOPNOTSUPP;
399 		if (vap->va_flags & SF_ARCHIVED)
400 			dep->de_Attributes &= ~ATTR_ARCHIVE;
401 		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
402 			dep->de_Attributes |= ATTR_ARCHIVE;
403 		dep->de_flag |= DE_MODIFIED;
404 	}
405 
406 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
407 		uid_t uid;
408 		gid_t gid;
409 
410 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
411 			return (EROFS);
412 		uid = vap->va_uid;
413 		if (uid == (uid_t)VNOVAL)
414 			uid = pmp->pm_uid;
415 		gid = vap->va_gid;
416 		if (gid == (gid_t)VNOVAL)
417 			gid = pmp->pm_gid;
418 		if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
419 		    (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
420 		    (error = suser_xxx(cred, ap->a_td->td_proc, PRISON_ROOT)))
421 			return error;
422 		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
423 			return EINVAL;
424 	}
425 
426 	if (vap->va_size != VNOVAL) {
427 		/*
428 		 * Disallow write attempts on read-only file systems;
429 		 * unless the file is a socket, fifo, or a block or
430 		 * character device resident on the file system.
431 		 */
432 		switch (vp->v_type) {
433 		case VDIR:
434 			return (EISDIR);
435 			/* NOT REACHED */
436 		case VLNK:
437 		case VREG:
438 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
439 				return (EROFS);
440 			break;
441 		default:
442 			break;
443 		}
444 		error = detrunc(dep, vap->va_size, 0, cred, ap->a_td);
445 		if (error)
446 			return error;
447 	}
448 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
449 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
450 			return (EROFS);
451 		if (cred->cr_uid != pmp->pm_uid &&
452 		    (error = suser_xxx(cred, ap->a_td->td_proc, PRISON_ROOT)) &&
453 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
454 		    (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_td))))
455 			return (error);
456 		if (vp->v_type != VDIR) {
457 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
458 			    vap->va_atime.tv_sec != VNOVAL)
459 				unix2dostime(&vap->va_atime, &dep->de_ADate, NULL, NULL);
460 			if (vap->va_mtime.tv_sec != VNOVAL)
461 				unix2dostime(&vap->va_mtime, &dep->de_MDate, &dep->de_MTime, NULL);
462 			dep->de_Attributes |= ATTR_ARCHIVE;
463 			dep->de_flag |= DE_MODIFIED;
464 		}
465 	}
466 	/*
467 	 * DOS files only have the ability to have their writability
468 	 * attribute set, so we use the owner write bit to set the readonly
469 	 * attribute.
470 	 */
471 	if (vap->va_mode != (mode_t)VNOVAL) {
472 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
473 			return (EROFS);
474 		if (cred->cr_uid != pmp->pm_uid &&
475 		    (error = suser_xxx(cred, ap->a_td->td_proc, PRISON_ROOT)))
476 			return (error);
477 		if (vp->v_type != VDIR) {
478 			/* We ignore the read and execute bits. */
479 			if (vap->va_mode & VWRITE)
480 				dep->de_Attributes &= ~ATTR_READONLY;
481 			else
482 				dep->de_Attributes |= ATTR_READONLY;
483 			dep->de_flag |= DE_MODIFIED;
484 		}
485 	}
486 	return (deupdat(dep, 1));
487 }
488 
489 static int
490 msdosfs_read(ap)
491 	struct vop_read_args /* {
492 		struct vnode *a_vp;
493 		struct uio *a_uio;
494 		int a_ioflag;
495 		struct ucred *a_cred;
496 	} */ *ap;
497 {
498 	int error = 0;
499 	int blsize;
500 	int isadir;
501 	int orig_resid;
502 	u_int n;
503 	u_long diff;
504 	u_long on;
505 	daddr_t lbn;
506 	daddr_t rablock;
507 	int rasize;
508 	int seqcount;
509 	struct buf *bp;
510 	struct vnode *vp = ap->a_vp;
511 	struct denode *dep = VTODE(vp);
512 	struct msdosfsmount *pmp = dep->de_pmp;
513 	struct uio *uio = ap->a_uio;
514 
515 	if (uio->uio_offset < 0)
516 		return (EINVAL);
517 
518 	if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
519                 return (0);
520 	/*
521 	 * If they didn't ask for any data, then we are done.
522 	 */
523 	orig_resid = uio->uio_resid;
524 	if (orig_resid <= 0)
525 		return (0);
526 
527 	seqcount = ap->a_ioflag >> 16;
528 
529 	isadir = dep->de_Attributes & ATTR_DIRECTORY;
530 	do {
531 		if (uio->uio_offset >= dep->de_FileSize)
532 			break;
533 		lbn = de_cluster(pmp, uio->uio_offset);
534 		/*
535 		 * If we are operating on a directory file then be sure to
536 		 * do i/o with the vnode for the filesystem instead of the
537 		 * vnode for the directory.
538 		 */
539 		if (isadir) {
540 			/* convert cluster # to block # */
541 			error = pcbmap(dep, lbn, &lbn, 0, &blsize);
542 			if (error == E2BIG) {
543 				error = EINVAL;
544 				break;
545 			} else if (error)
546 				break;
547 			error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
548 		} else {
549 			blsize = pmp->pm_bpcluster;
550 			rablock = lbn + 1;
551 			if (seqcount > 1 &&
552 			    de_cn2off(pmp, rablock) < dep->de_FileSize) {
553 				rasize = pmp->pm_bpcluster;
554 				error = breadn(vp, lbn, blsize,
555 				    &rablock, &rasize, 1, NOCRED, &bp);
556 			} else {
557 				error = bread(vp, lbn, blsize, NOCRED, &bp);
558 			}
559 		}
560 		if (error) {
561 			brelse(bp);
562 			break;
563 		}
564 		on = uio->uio_offset & pmp->pm_crbomask;
565 		diff = pmp->pm_bpcluster - on;
566 		n = diff > uio->uio_resid ? uio->uio_resid : diff;
567 		diff = dep->de_FileSize - uio->uio_offset;
568 		if (diff < n)
569 			n = diff;
570 		diff = blsize - bp->b_resid;
571 		if (diff < n)
572 			n = diff;
573 		error = uiomove(bp->b_data + on, (int) n, uio);
574 		brelse(bp);
575 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
576 	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
577 	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
578 		dep->de_flag |= DE_ACCESS;
579 	return (error);
580 }
581 
582 /*
583  * Write data to a file or directory.
584  */
585 static int
586 msdosfs_write(ap)
587 	struct vop_write_args /* {
588 		struct vnode *a_vp;
589 		struct uio *a_uio;
590 		int a_ioflag;
591 		struct ucred *a_cred;
592 	} */ *ap;
593 {
594 	int n;
595 	int croffset;
596 	int resid;
597 	u_long osize;
598 	int error = 0;
599 	u_long count;
600 	daddr_t bn, lastcn;
601 	struct buf *bp;
602 	int ioflag = ap->a_ioflag;
603 	struct uio *uio = ap->a_uio;
604 	struct thread *td = uio->uio_td;
605 	struct vnode *vp = ap->a_vp;
606 	struct vnode *thisvp;
607 	struct denode *dep = VTODE(vp);
608 	struct msdosfsmount *pmp = dep->de_pmp;
609 	struct ucred *cred = ap->a_cred;
610 
611 #ifdef MSDOSFS_DEBUG
612 	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
613 	    vp, uio, ioflag, cred);
614 	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
615 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
616 #endif
617 
618 	switch (vp->v_type) {
619 	case VREG:
620 		if (ioflag & IO_APPEND)
621 			uio->uio_offset = dep->de_FileSize;
622 		thisvp = vp;
623 		break;
624 	case VDIR:
625 		return EISDIR;
626 	default:
627 		panic("msdosfs_write(): bad file type");
628 	}
629 
630 	if (uio->uio_offset < 0)
631 		return (EFBIG);
632 
633 	if (uio->uio_resid == 0)
634 		return (0);
635 
636 	/*
637 	 * If they've exceeded their filesize limit, tell them about it.
638 	 */
639 	if (td &&
640 	    ((uoff_t)uio->uio_offset + uio->uio_resid >
641 	    td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
642 		PROC_LOCK(td->td_proc);
643 		psignal(td->td_proc, SIGXFSZ);
644 		PROC_UNLOCK(td->td_proc);
645 		return (EFBIG);
646 	}
647 
648 	if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
649                 return (EFBIG);
650 
651 	/*
652 	 * If the offset we are starting the write at is beyond the end of
653 	 * the file, then they've done a seek.  Unix filesystems allow
654 	 * files with holes in them, DOS doesn't so we must fill the hole
655 	 * with zeroed blocks.
656 	 */
657 	if (uio->uio_offset > dep->de_FileSize) {
658 		error = deextend(dep, uio->uio_offset, cred);
659 		if (error)
660 			return (error);
661 	}
662 
663 	/*
664 	 * Remember some values in case the write fails.
665 	 */
666 	resid = uio->uio_resid;
667 	osize = dep->de_FileSize;
668 
669 	/*
670 	 * If we write beyond the end of the file, extend it to its ultimate
671 	 * size ahead of the time to hopefully get a contiguous area.
672 	 */
673 	if (uio->uio_offset + resid > osize) {
674 		count = de_clcount(pmp, uio->uio_offset + resid) -
675 			de_clcount(pmp, osize);
676 		error = extendfile(dep, count, NULL, NULL, 0);
677 		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
678 			goto errexit;
679 		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
680 	} else
681 		lastcn = de_clcount(pmp, osize) - 1;
682 
683 	do {
684 		if (de_cluster(pmp, uio->uio_offset) > lastcn) {
685 			error = ENOSPC;
686 			break;
687 		}
688 
689 		croffset = uio->uio_offset & pmp->pm_crbomask;
690 		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
691 		if (uio->uio_offset + n > dep->de_FileSize) {
692 			dep->de_FileSize = uio->uio_offset + n;
693 			/* The object size needs to be set before buffer is allocated */
694 			vnode_pager_setsize(vp, dep->de_FileSize);
695 		}
696 
697 		bn = de_cluster(pmp, uio->uio_offset);
698 		if ((uio->uio_offset & pmp->pm_crbomask) == 0
699 		    && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
700 		        > de_cluster(pmp, uio->uio_offset)
701 			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
702 			/*
703 			 * If either the whole cluster gets written,
704 			 * or we write the cluster from its start beyond EOF,
705 			 * then no need to read data from disk.
706 			 */
707 			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
708 			clrbuf(bp);
709 			/*
710 			 * Do the bmap now, since pcbmap needs buffers
711 			 * for the fat table. (see msdosfs_strategy)
712 			 */
713 			if (bp->b_blkno == bp->b_lblkno) {
714 				error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
715 				if (error)
716 					bp->b_blkno = -1;
717 				else
718 					bp->b_blkno = bn;
719 			}
720 			if (bp->b_blkno == -1) {
721 				brelse(bp);
722 				if (!error)
723 					error = EIO;		/* XXX */
724 				break;
725 			}
726 		} else {
727 			/*
728 			 * The block we need to write into exists, so read it in.
729 			 */
730 			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
731 			if (error) {
732 				brelse(bp);
733 				break;
734 			}
735 		}
736 
737 		/*
738 		 * Should these vnode_pager_* functions be done on dir
739 		 * files?
740 		 */
741 
742 		/*
743 		 * Copy the data from user space into the buf header.
744 		 */
745 		error = uiomove(bp->b_data + croffset, n, uio);
746 		if (error) {
747 			brelse(bp);
748 			break;
749 		}
750 
751 		/*
752 		 * If they want this synchronous then write it and wait for
753 		 * it.  Otherwise, if on a cluster boundary write it
754 		 * asynchronously so we can move on to the next block
755 		 * without delay.  Otherwise do a delayed write because we
756 		 * may want to write somemore into the block later.
757 		 */
758 		if (ioflag & IO_SYNC)
759 			(void) bwrite(bp);
760 		else if (n + croffset == pmp->pm_bpcluster)
761 			bawrite(bp);
762 		else
763 			bdwrite(bp);
764 		dep->de_flag |= DE_UPDATE;
765 	} while (error == 0 && uio->uio_resid > 0);
766 
767 	/*
768 	 * If the write failed and they want us to, truncate the file back
769 	 * to the size it was before the write was attempted.
770 	 */
771 errexit:
772 	if (error) {
773 		if (ioflag & IO_UNIT) {
774 			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
775 			uio->uio_offset -= resid - uio->uio_resid;
776 			uio->uio_resid = resid;
777 		} else {
778 			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
779 			if (uio->uio_resid != resid)
780 				error = 0;
781 		}
782 	} else if (ioflag & IO_SYNC)
783 		error = deupdat(dep, 1);
784 	return (error);
785 }
786 
787 /*
788  * Flush the blocks of a file to disk.
789  *
790  * This function is worthless for vnodes that represent directories. Maybe we
791  * could just do a sync if they try an fsync on a directory file.
792  */
793 static int
794 msdosfs_fsync(ap)
795 	struct vop_fsync_args /* {
796 		struct vnode *a_vp;
797 		struct ucred *a_cred;
798 		int a_waitfor;
799 		struct thread *a_td;
800 	} */ *ap;
801 {
802 	struct vnode *vp = ap->a_vp;
803 	int s;
804 	struct buf *bp, *nbp;
805 
806 	/*
807 	 * Flush all dirty buffers associated with a vnode.
808 	 */
809 loop:
810 	s = splbio();
811 	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
812 		nbp = TAILQ_NEXT(bp, b_vnbufs);
813 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
814 			continue;
815 		if ((bp->b_flags & B_DELWRI) == 0)
816 			panic("msdosfs_fsync: not dirty");
817 		bremfree(bp);
818 		splx(s);
819 		(void) bwrite(bp);
820 		goto loop;
821 	}
822 	while (vp->v_numoutput) {
823 		vp->v_flag |= VBWAIT;
824 		(void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
825 	}
826 #ifdef DIAGNOSTIC
827 	if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
828 		vprint("msdosfs_fsync: dirty", vp);
829 		goto loop;
830 	}
831 #endif
832 	splx(s);
833 	return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
834 }
835 
836 static int
837 msdosfs_remove(ap)
838 	struct vop_remove_args /* {
839 		struct vnode *a_dvp;
840 		struct vnode *a_vp;
841 		struct componentname *a_cnp;
842 	} */ *ap;
843 {
844 	struct denode *dep = VTODE(ap->a_vp);
845 	struct denode *ddep = VTODE(ap->a_dvp);
846 	int error;
847 
848 	if (ap->a_vp->v_type == VDIR)
849 		error = EPERM;
850 	else
851 		error = removede(ddep, dep);
852 #ifdef MSDOSFS_DEBUG
853 	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
854 #endif
855 	return (error);
856 }
857 
858 /*
859  * DOS filesystems don't know what links are.
860  */
861 static int
862 msdosfs_link(ap)
863 	struct vop_link_args /* {
864 		struct vnode *a_tdvp;
865 		struct vnode *a_vp;
866 		struct componentname *a_cnp;
867 	} */ *ap;
868 {
869 	return (EOPNOTSUPP);
870 }
871 
872 /*
873  * Renames on files require moving the denode to a new hash queue since the
874  * denode's location is used to compute which hash queue to put the file
875  * in. Unless it is a rename in place.  For example "mv a b".
876  *
877  * What follows is the basic algorithm:
878  *
879  * if (file move) {
880  *	if (dest file exists) {
881  *		remove dest file
882  *	}
883  *	if (dest and src in same directory) {
884  *		rewrite name in existing directory slot
885  *	} else {
886  *		write new entry in dest directory
887  *		update offset and dirclust in denode
888  *		move denode to new hash chain
889  *		clear old directory entry
890  *	}
891  * } else {
892  *	directory move
893  *	if (dest directory exists) {
894  *		if (dest is not empty) {
895  *			return ENOTEMPTY
896  *		}
897  *		remove dest directory
898  *	}
899  *	if (dest and src in same directory) {
900  *		rewrite name in existing entry
901  *	} else {
902  *		be sure dest is not a child of src directory
903  *		write entry in dest directory
904  *		update "." and ".." in moved directory
905  *		clear old directory entry for moved directory
906  *	}
907  * }
908  *
909  * On entry:
910  *	source's parent directory is unlocked
911  *	source file or directory is unlocked
912  *	destination's parent directory is locked
913  *	destination file or directory is locked if it exists
914  *
915  * On exit:
916  *	all denodes should be released
917  */
918 static int
919 msdosfs_rename(ap)
920 	struct vop_rename_args /* {
921 		struct vnode *a_fdvp;
922 		struct vnode *a_fvp;
923 		struct componentname *a_fcnp;
924 		struct vnode *a_tdvp;
925 		struct vnode *a_tvp;
926 		struct componentname *a_tcnp;
927 	} */ *ap;
928 {
929 	struct vnode *tdvp = ap->a_tdvp;
930 	struct vnode *fvp = ap->a_fvp;
931 	struct vnode *fdvp = ap->a_fdvp;
932 	struct vnode *tvp = ap->a_tvp;
933 	struct componentname *tcnp = ap->a_tcnp;
934 	struct componentname *fcnp = ap->a_fcnp;
935 	struct thread *td = fcnp->cn_thread;
936 	struct denode *ip, *xp, *dp, *zp;
937 	u_char toname[11], oldname[11];
938 	u_long from_diroffset, to_diroffset;
939 	u_char to_count;
940 	int doingdirectory = 0, newparent = 0;
941 	int error;
942 	u_long cn;
943 	daddr_t bn;
944 	struct denode *fddep;	/* from file's parent directory	 */
945 	struct denode *fdep;	/* from file or directory	 */
946 	struct denode *tddep;	/* to file's parent directory	 */
947 	struct denode *tdep;	/* to file or directory		 */
948 	struct msdosfsmount *pmp;
949 	struct direntry *dotdotp;
950 	struct buf *bp;
951 
952 	fddep = VTODE(ap->a_fdvp);
953 	fdep = VTODE(ap->a_fvp);
954 	tddep = VTODE(ap->a_tdvp);
955 	tdep = tvp ? VTODE(tvp) : NULL;
956 	pmp = fddep->de_pmp;
957 
958 	pmp = VFSTOMSDOSFS(fdvp->v_mount);
959 
960 #ifdef DIAGNOSTIC
961 	if ((tcnp->cn_flags & HASBUF) == 0 ||
962 	    (fcnp->cn_flags & HASBUF) == 0)
963 		panic("msdosfs_rename: no name");
964 #endif
965 	/*
966 	 * Check for cross-device rename.
967 	 */
968 	if ((fvp->v_mount != tdvp->v_mount) ||
969 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
970 		error = EXDEV;
971 abortit:
972 		if (tdvp == tvp)
973 			vrele(tdvp);
974 		else
975 			vput(tdvp);
976 		if (tvp)
977 			vput(tvp);
978 		vrele(fdvp);
979 		vrele(fvp);
980 		return (error);
981 	}
982 
983 	/*
984 	 * If source and dest are the same, do nothing.
985 	 */
986 	if (tvp == fvp) {
987 		error = 0;
988 		goto abortit;
989 	}
990 
991 	error = vn_lock(fvp, LK_EXCLUSIVE, td);
992 	if (error)
993 		goto abortit;
994 	dp = VTODE(fdvp);
995 	ip = VTODE(fvp);
996 
997 	/*
998 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
999 	 * leads to a crippled directory tree.  It's pretty tough to do a
1000 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1001 	 * doesn't work if the ".." entry is missing.
1002 	 */
1003 	if (ip->de_Attributes & ATTR_DIRECTORY) {
1004 		/*
1005 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1006 		 */
1007 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1008 		    dp == ip ||
1009 		    (fcnp->cn_flags & ISDOTDOT) ||
1010 		    (tcnp->cn_flags & ISDOTDOT) ||
1011 		    (ip->de_flag & DE_RENAME)) {
1012 			VOP_UNLOCK(fvp, 0, td);
1013 			error = EINVAL;
1014 			goto abortit;
1015 		}
1016 		ip->de_flag |= DE_RENAME;
1017 		doingdirectory++;
1018 	}
1019 
1020 	/*
1021 	 * When the target exists, both the directory
1022 	 * and target vnodes are returned locked.
1023 	 */
1024 	dp = VTODE(tdvp);
1025 	xp = tvp ? VTODE(tvp) : NULL;
1026 	/*
1027 	 * Remember direntry place to use for destination
1028 	 */
1029 	to_diroffset = dp->de_fndoffset;
1030 	to_count = dp->de_fndcnt;
1031 
1032 	/*
1033 	 * If ".." must be changed (ie the directory gets a new
1034 	 * parent) then the source directory must not be in the
1035 	 * directory heirarchy above the target, as this would
1036 	 * orphan everything below the source directory. Also
1037 	 * the user must have write permission in the source so
1038 	 * as to be able to change "..". We must repeat the call
1039 	 * to namei, as the parent directory is unlocked by the
1040 	 * call to doscheckpath().
1041 	 */
1042 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1043 	VOP_UNLOCK(fvp, 0, td);
1044 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1045 		newparent = 1;
1046 	if (doingdirectory && newparent) {
1047 		if (error)	/* write access check above */
1048 			goto bad;
1049 		if (xp != NULL)
1050 			vput(tvp);
1051 		/*
1052 		 * doscheckpath() vput()'s dp,
1053 		 * so we have to do a relookup afterwards
1054 		 */
1055 		error = doscheckpath(ip, dp);
1056 		if (error)
1057 			goto out;
1058 		if ((tcnp->cn_flags & SAVESTART) == 0)
1059 			panic("msdosfs_rename: lost to startdir");
1060 		error = relookup(tdvp, &tvp, tcnp);
1061 		if (error)
1062 			goto out;
1063 		dp = VTODE(tdvp);
1064 		xp = tvp ? VTODE(tvp) : NULL;
1065 	}
1066 
1067 	if (xp != NULL) {
1068 		/*
1069 		 * Target must be empty if a directory and have no links
1070 		 * to it. Also, ensure source and target are compatible
1071 		 * (both directories, or both not directories).
1072 		 */
1073 		if (xp->de_Attributes & ATTR_DIRECTORY) {
1074 			if (!dosdirempty(xp)) {
1075 				error = ENOTEMPTY;
1076 				goto bad;
1077 			}
1078 			if (!doingdirectory) {
1079 				error = ENOTDIR;
1080 				goto bad;
1081 			}
1082 			cache_purge(tdvp);
1083 		} else if (doingdirectory) {
1084 			error = EISDIR;
1085 			goto bad;
1086 		}
1087 		error = removede(dp, xp);
1088 		if (error)
1089 			goto bad;
1090 		vput(tvp);
1091 		xp = NULL;
1092 	}
1093 
1094 	/*
1095 	 * Convert the filename in tcnp into a dos filename. We copy this
1096 	 * into the denode and directory entry for the destination
1097 	 * file/directory.
1098 	 */
1099 	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1100 	if (error)
1101 		goto abortit;
1102 
1103 	/*
1104 	 * Since from wasn't locked at various places above,
1105 	 * have to do a relookup here.
1106 	 */
1107 	fcnp->cn_flags &= ~MODMASK;
1108 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1109 	if ((fcnp->cn_flags & SAVESTART) == 0)
1110 		panic("msdosfs_rename: lost from startdir");
1111 	if (!newparent)
1112 		VOP_UNLOCK(tdvp, 0, td);
1113 	if (relookup(fdvp, &fvp, fcnp) == 0)
1114 		vrele(fdvp);
1115 	if (fvp == NULL) {
1116 		/*
1117 		 * From name has disappeared.
1118 		 */
1119 		if (doingdirectory)
1120 			panic("rename: lost dir entry");
1121 		vrele(ap->a_fvp);
1122 		if (newparent)
1123 			VOP_UNLOCK(tdvp, 0, td);
1124 		vrele(tdvp);
1125 		return 0;
1126 	}
1127 	xp = VTODE(fvp);
1128 	zp = VTODE(fdvp);
1129 	from_diroffset = zp->de_fndoffset;
1130 
1131 	/*
1132 	 * Ensure that the directory entry still exists and has not
1133 	 * changed till now. If the source is a file the entry may
1134 	 * have been unlinked or renamed. In either case there is
1135 	 * no further work to be done. If the source is a directory
1136 	 * then it cannot have been rmdir'ed or renamed; this is
1137 	 * prohibited by the DE_RENAME flag.
1138 	 */
1139 	if (xp != ip) {
1140 		if (doingdirectory)
1141 			panic("rename: lost dir entry");
1142 		vrele(ap->a_fvp);
1143 		VOP_UNLOCK(fvp, 0, td);
1144 		if (newparent)
1145 			VOP_UNLOCK(fdvp, 0, td);
1146 		xp = NULL;
1147 	} else {
1148 		vrele(fvp);
1149 		xp = NULL;
1150 
1151 		/*
1152 		 * First write a new entry in the destination
1153 		 * directory and mark the entry in the source directory
1154 		 * as deleted.  Then move the denode to the correct hash
1155 		 * chain for its new location in the filesystem.  And, if
1156 		 * we moved a directory, then update its .. entry to point
1157 		 * to the new parent directory.
1158 		 */
1159 		bcopy(ip->de_Name, oldname, 11);
1160 		bcopy(toname, ip->de_Name, 11);	/* update denode */
1161 		dp->de_fndoffset = to_diroffset;
1162 		dp->de_fndcnt = to_count;
1163 		error = createde(ip, dp, (struct denode **)0, tcnp);
1164 		if (error) {
1165 			bcopy(oldname, ip->de_Name, 11);
1166 			if (newparent)
1167 				VOP_UNLOCK(fdvp, 0, td);
1168 			VOP_UNLOCK(fvp, 0, td);
1169 			goto bad;
1170 		}
1171 		ip->de_refcnt++;
1172 		zp->de_fndoffset = from_diroffset;
1173 		error = removede(zp, ip);
1174 		if (error) {
1175 			/* XXX should really panic here, fs is corrupt */
1176 			if (newparent)
1177 				VOP_UNLOCK(fdvp, 0, td);
1178 			VOP_UNLOCK(fvp, 0, td);
1179 			goto bad;
1180 		}
1181 		if (!doingdirectory) {
1182 			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1183 				       &ip->de_dirclust, 0);
1184 			if (error) {
1185 				/* XXX should really panic here, fs is corrupt */
1186 				if (newparent)
1187 					VOP_UNLOCK(fdvp, 0, td);
1188 				VOP_UNLOCK(fvp, 0, td);
1189 				goto bad;
1190 			}
1191 			if (ip->de_dirclust == MSDOSFSROOT)
1192 				ip->de_diroffset = to_diroffset;
1193 			else
1194 				ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1195 		}
1196 		reinsert(ip);
1197 		if (newparent)
1198 			VOP_UNLOCK(fdvp, 0, td);
1199 	}
1200 
1201 	/*
1202 	 * If we moved a directory to a new parent directory, then we must
1203 	 * fixup the ".." entry in the moved directory.
1204 	 */
1205 	if (doingdirectory && newparent) {
1206 		cn = ip->de_StartCluster;
1207 		if (cn == MSDOSFSROOT) {
1208 			/* this should never happen */
1209 			panic("msdosfs_rename(): updating .. in root directory?");
1210 		} else
1211 			bn = cntobn(pmp, cn);
1212 		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1213 			      NOCRED, &bp);
1214 		if (error) {
1215 			/* XXX should really panic here, fs is corrupt */
1216 			brelse(bp);
1217 			VOP_UNLOCK(fvp, 0, td);
1218 			goto bad;
1219 		}
1220 		dotdotp = (struct direntry *)bp->b_data + 1;
1221 		putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1222 		if (FAT32(pmp))
1223 			putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1224 		error = bwrite(bp);
1225 		if (error) {
1226 			/* XXX should really panic here, fs is corrupt */
1227 			VOP_UNLOCK(fvp, 0, td);
1228 			goto bad;
1229 		}
1230 	}
1231 
1232 	VOP_UNLOCK(fvp, 0, td);
1233 bad:
1234 	if (xp)
1235 		vput(tvp);
1236 	vput(tdvp);
1237 out:
1238 	ip->de_flag &= ~DE_RENAME;
1239 	vrele(fdvp);
1240 	vrele(fvp);
1241 	return (error);
1242 
1243 }
1244 
1245 static struct {
1246 	struct direntry dot;
1247 	struct direntry dotdot;
1248 } dosdirtemplate = {
1249 	{	".       ", "   ",			/* the . entry */
1250 		ATTR_DIRECTORY,				/* file attribute */
1251 		0,	 				/* reserved */
1252 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1253 		{ 0, 0 },				/* access date */
1254 		{ 0, 0 },				/* high bits of start cluster */
1255 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1256 		{ 0, 0 },				/* startcluster */
1257 		{ 0, 0, 0, 0 } 				/* filesize */
1258 	},
1259 	{	"..      ", "   ",			/* the .. entry */
1260 		ATTR_DIRECTORY,				/* file attribute */
1261 		0,	 				/* reserved */
1262 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1263 		{ 0, 0 },				/* access date */
1264 		{ 0, 0 },				/* high bits of start cluster */
1265 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1266 		{ 0, 0 },				/* startcluster */
1267 		{ 0, 0, 0, 0 }				/* filesize */
1268 	}
1269 };
1270 
1271 static int
1272 msdosfs_mkdir(ap)
1273 	struct vop_mkdir_args /* {
1274 		struct vnode *a_dvp;
1275 		struvt vnode **a_vpp;
1276 		struvt componentname *a_cnp;
1277 		struct vattr *a_vap;
1278 	} */ *ap;
1279 {
1280 	struct componentname *cnp = ap->a_cnp;
1281 	struct denode *dep;
1282 	struct denode *pdep = VTODE(ap->a_dvp);
1283 	struct direntry *denp;
1284 	struct msdosfsmount *pmp = pdep->de_pmp;
1285 	struct buf *bp;
1286 	u_long newcluster, pcl;
1287 	int bn;
1288 	int error;
1289 	struct denode ndirent;
1290 	struct timespec ts;
1291 
1292 	/*
1293 	 * If this is the root directory and there is no space left we
1294 	 * can't do anything.  This is because the root directory can not
1295 	 * change size.
1296 	 */
1297 	if (pdep->de_StartCluster == MSDOSFSROOT
1298 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1299 		error = ENOSPC;
1300 		goto bad2;
1301 	}
1302 
1303 	/*
1304 	 * Allocate a cluster to hold the about to be created directory.
1305 	 */
1306 	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1307 	if (error)
1308 		goto bad2;
1309 
1310 	bzero(&ndirent, sizeof(ndirent));
1311 	ndirent.de_pmp = pmp;
1312 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1313 	getnanotime(&ts);
1314 	DETIMES(&ndirent, &ts, &ts, &ts);
1315 
1316 	/*
1317 	 * Now fill the cluster with the "." and ".." entries. And write
1318 	 * the cluster to disk.  This way it is there for the parent
1319 	 * directory to be pointing at if there were a crash.
1320 	 */
1321 	bn = cntobn(pmp, newcluster);
1322 	/* always succeeds */
1323 	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1324 	bzero(bp->b_data, pmp->pm_bpcluster);
1325 	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1326 	denp = (struct direntry *)bp->b_data;
1327 	putushort(denp[0].deStartCluster, newcluster);
1328 	putushort(denp[0].deCDate, ndirent.de_CDate);
1329 	putushort(denp[0].deCTime, ndirent.de_CTime);
1330 	denp[0].deCHundredth = ndirent.de_CHun;
1331 	putushort(denp[0].deADate, ndirent.de_ADate);
1332 	putushort(denp[0].deMDate, ndirent.de_MDate);
1333 	putushort(denp[0].deMTime, ndirent.de_MTime);
1334 	pcl = pdep->de_StartCluster;
1335 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1336 		pcl = 0;
1337 	putushort(denp[1].deStartCluster, pcl);
1338 	putushort(denp[1].deCDate, ndirent.de_CDate);
1339 	putushort(denp[1].deCTime, ndirent.de_CTime);
1340 	denp[1].deCHundredth = ndirent.de_CHun;
1341 	putushort(denp[1].deADate, ndirent.de_ADate);
1342 	putushort(denp[1].deMDate, ndirent.de_MDate);
1343 	putushort(denp[1].deMTime, ndirent.de_MTime);
1344 	if (FAT32(pmp)) {
1345 		putushort(denp[0].deHighClust, newcluster >> 16);
1346 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1347 	}
1348 
1349 	error = bwrite(bp);
1350 	if (error)
1351 		goto bad;
1352 
1353 	/*
1354 	 * Now build up a directory entry pointing to the newly allocated
1355 	 * cluster.  This will be written to an empty slot in the parent
1356 	 * directory.
1357 	 */
1358 #ifdef DIAGNOSTIC
1359 	if ((cnp->cn_flags & HASBUF) == 0)
1360 		panic("msdosfs_mkdir: no name");
1361 #endif
1362 	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1363 	if (error)
1364 		goto bad;
1365 
1366 	ndirent.de_Attributes = ATTR_DIRECTORY;
1367 	ndirent.de_LowerCase = 0;
1368 	ndirent.de_StartCluster = newcluster;
1369 	ndirent.de_FileSize = 0;
1370 	ndirent.de_dev = pdep->de_dev;
1371 	ndirent.de_devvp = pdep->de_devvp;
1372 	error = createde(&ndirent, pdep, &dep, cnp);
1373 	if (error)
1374 		goto bad;
1375 	*ap->a_vpp = DETOV(dep);
1376 	return (0);
1377 
1378 bad:
1379 	clusterfree(pmp, newcluster, NULL);
1380 bad2:
1381 	return (error);
1382 }
1383 
1384 static int
1385 msdosfs_rmdir(ap)
1386 	struct vop_rmdir_args /* {
1387 		struct vnode *a_dvp;
1388 		struct vnode *a_vp;
1389 		struct componentname *a_cnp;
1390 	} */ *ap;
1391 {
1392 	register struct vnode *vp = ap->a_vp;
1393 	register struct vnode *dvp = ap->a_dvp;
1394 	register struct componentname *cnp = ap->a_cnp;
1395 	register struct denode *ip, *dp;
1396 	struct thread *td = cnp->cn_thread;
1397 	int error;
1398 
1399 	ip = VTODE(vp);
1400 	dp = VTODE(dvp);
1401 
1402 	/*
1403 	 * Verify the directory is empty (and valid).
1404 	 * (Rmdir ".." won't be valid since
1405 	 *  ".." will contain a reference to
1406 	 *  the current directory and thus be
1407 	 *  non-empty.)
1408 	 */
1409 	error = 0;
1410 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1411 		error = ENOTEMPTY;
1412 		goto out;
1413 	}
1414 	/*
1415 	 * Delete the entry from the directory.  For dos filesystems this
1416 	 * gets rid of the directory entry on disk, the in memory copy
1417 	 * still exists but the de_refcnt is <= 0.  This prevents it from
1418 	 * being found by deget().  When the vput() on dep is done we give
1419 	 * up access and eventually msdosfs_reclaim() will be called which
1420 	 * will remove it from the denode cache.
1421 	 */
1422 	error = removede(dp, ip);
1423 	if (error)
1424 		goto out;
1425 	/*
1426 	 * This is where we decrement the link count in the parent
1427 	 * directory.  Since dos filesystems don't do this we just purge
1428 	 * the name cache.
1429 	 */
1430 	cache_purge(dvp);
1431 	VOP_UNLOCK(dvp, 0, td);
1432 	/*
1433 	 * Truncate the directory that is being deleted.
1434 	 */
1435 	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, td);
1436 	cache_purge(vp);
1437 
1438 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1439 out:
1440 	return (error);
1441 }
1442 
1443 /*
1444  * DOS filesystems don't know what symlinks are.
1445  */
1446 static int
1447 msdosfs_symlink(ap)
1448 	struct vop_symlink_args /* {
1449 		struct vnode *a_dvp;
1450 		struct vnode **a_vpp;
1451 		struct componentname *a_cnp;
1452 		struct vattr *a_vap;
1453 		char *a_target;
1454 	} */ *ap;
1455 {
1456 	return (EOPNOTSUPP);
1457 }
1458 
1459 static int
1460 msdosfs_readdir(ap)
1461 	struct vop_readdir_args /* {
1462 		struct vnode *a_vp;
1463 		struct uio *a_uio;
1464 		struct ucred *a_cred;
1465 		int *a_eofflag;
1466 		int *a_ncookies;
1467 		u_long **a_cookies;
1468 	} */ *ap;
1469 {
1470 	int error = 0;
1471 	int diff;
1472 	long n;
1473 	int blsize;
1474 	long on;
1475 	u_long cn;
1476 	u_long fileno;
1477 	u_long dirsperblk;
1478 	long bias = 0;
1479 	daddr_t bn, 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 dirbuf;
1485 	struct uio *uio = ap->a_uio;
1486 	u_long *cookies = NULL;
1487 	int ncookies = 0;
1488 	off_t offset, off;
1489 	int chksum = -1;
1490 
1491 #ifdef MSDOSFS_DEBUG
1492 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\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 	 * To be safe, initialize dirbuf
1507 	 */
1508 	bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1509 
1510 	/*
1511 	 * If the user buffer is smaller than the size of one dos directory
1512 	 * entry or the file offset is not a multiple of the size of a
1513 	 * directory entry, then we fail the read.
1514 	 */
1515 	off = offset = uio->uio_offset;
1516 	if (uio->uio_resid < sizeof(struct direntry) ||
1517 	    (offset & (sizeof(struct direntry) - 1)))
1518 		return (EINVAL);
1519 
1520 	if (ap->a_ncookies) {
1521 		ncookies = uio->uio_resid / 16;
1522 		MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1523 		       M_WAITOK);
1524 		*ap->a_cookies = cookies;
1525 		*ap->a_ncookies = ncookies;
1526 	}
1527 
1528 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1529 
1530 	/*
1531 	 * If they are reading from the root directory then, we simulate
1532 	 * the . and .. entries since these don't exist in the root
1533 	 * directory.  We also set the offset bias to make up for having to
1534 	 * simulate these entries. By this I mean that at file offset 64 we
1535 	 * read the first entry in the root directory that lives on disk.
1536 	 */
1537 	if (dep->de_StartCluster == MSDOSFSROOT
1538 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1539 #if 0
1540 		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1541 		    offset);
1542 #endif
1543 		bias = 2 * sizeof(struct direntry);
1544 		if (offset < bias) {
1545 			for (n = (int)offset / sizeof(struct direntry);
1546 			     n < 2; n++) {
1547 				if (FAT32(pmp))
1548 					dirbuf.d_fileno = cntobn(pmp,
1549 								 pmp->pm_rootdirblk)
1550 							  * dirsperblk;
1551 				else
1552 					dirbuf.d_fileno = 1;
1553 				dirbuf.d_type = DT_DIR;
1554 				switch (n) {
1555 				case 0:
1556 					dirbuf.d_namlen = 1;
1557 					strcpy(dirbuf.d_name, ".");
1558 					break;
1559 				case 1:
1560 					dirbuf.d_namlen = 2;
1561 					strcpy(dirbuf.d_name, "..");
1562 					break;
1563 				}
1564 				dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1565 				if (uio->uio_resid < dirbuf.d_reclen)
1566 					goto out;
1567 				error = uiomove((caddr_t) &dirbuf,
1568 						dirbuf.d_reclen, uio);
1569 				if (error)
1570 					goto out;
1571 				offset += sizeof(struct direntry);
1572 				off = offset;
1573 				if (cookies) {
1574 					*cookies++ = offset;
1575 					if (--ncookies <= 0)
1576 						goto out;
1577 				}
1578 			}
1579 		}
1580 	}
1581 
1582 	off = offset;
1583 	while (uio->uio_resid > 0) {
1584 		lbn = de_cluster(pmp, offset - bias);
1585 		on = (offset - bias) & pmp->pm_crbomask;
1586 		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1587 		diff = dep->de_FileSize - (offset - bias);
1588 		if (diff <= 0)
1589 			break;
1590 		n = min(n, diff);
1591 		error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1592 		if (error)
1593 			break;
1594 		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1595 		if (error) {
1596 			brelse(bp);
1597 			return (error);
1598 		}
1599 		n = min(n, blsize - bp->b_resid);
1600 
1601 		/*
1602 		 * Convert from dos directory entries to fs-independent
1603 		 * directory entries.
1604 		 */
1605 		for (dentp = (struct direntry *)(bp->b_data + on);
1606 		     (char *)dentp < bp->b_data + on + n;
1607 		     dentp++, offset += sizeof(struct direntry)) {
1608 #if 0
1609 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1610 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1611 #endif
1612 			/*
1613 			 * If this is an unused entry, we can stop.
1614 			 */
1615 			if (dentp->deName[0] == SLOT_EMPTY) {
1616 				brelse(bp);
1617 				goto out;
1618 			}
1619 			/*
1620 			 * Skip deleted entries.
1621 			 */
1622 			if (dentp->deName[0] == SLOT_DELETED) {
1623 				chksum = -1;
1624 				continue;
1625 			}
1626 
1627 			/*
1628 			 * Handle Win95 long directory entries
1629 			 */
1630 			if (dentp->deAttributes == ATTR_WIN95) {
1631 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1632 					continue;
1633 				chksum = win2unixfn((struct winentry *)dentp,
1634 					&dirbuf, chksum,
1635 					pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1636 					pmp->pm_u2w);
1637 				continue;
1638 			}
1639 
1640 			/*
1641 			 * Skip volume labels
1642 			 */
1643 			if (dentp->deAttributes & ATTR_VOLUME) {
1644 				chksum = -1;
1645 				continue;
1646 			}
1647 			/*
1648 			 * This computation of d_fileno must match
1649 			 * the computation of va_fileid in
1650 			 * msdosfs_getattr.
1651 			 */
1652 			if (dentp->deAttributes & ATTR_DIRECTORY) {
1653 				fileno = getushort(dentp->deStartCluster);
1654 				if (FAT32(pmp))
1655 					fileno |= getushort(dentp->deHighClust) << 16;
1656 				/* if this is the root directory */
1657 				if (fileno == MSDOSFSROOT)
1658 					if (FAT32(pmp))
1659 						fileno = cntobn(pmp,
1660 								pmp->pm_rootdirblk)
1661 							 * dirsperblk;
1662 					else
1663 						fileno = 1;
1664 				else
1665 					fileno = cntobn(pmp, fileno) * dirsperblk;
1666 				dirbuf.d_fileno = fileno;
1667 				dirbuf.d_type = DT_DIR;
1668 			} else {
1669 				dirbuf.d_fileno = offset / sizeof(struct direntry);
1670 				dirbuf.d_type = DT_REG;
1671 			}
1672 			if (chksum != winChksum(dentp->deName))
1673 				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1674 				    (u_char *)dirbuf.d_name,
1675 				    dentp->deLowerCase |
1676 					((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1677 					(LCASE_BASE | LCASE_EXT) : 0),
1678 				    pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1679 				    pmp->pm_d2u,
1680 				    pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1681 				    pmp->pm_ul);
1682 			else
1683 				dirbuf.d_name[dirbuf.d_namlen] = 0;
1684 			chksum = -1;
1685 			dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1686 			if (uio->uio_resid < dirbuf.d_reclen) {
1687 				brelse(bp);
1688 				goto out;
1689 			}
1690 			error = uiomove((caddr_t) &dirbuf,
1691 					dirbuf.d_reclen, uio);
1692 			if (error) {
1693 				brelse(bp);
1694 				goto out;
1695 			}
1696 			if (cookies) {
1697 				*cookies++ = offset + sizeof(struct direntry);
1698 				if (--ncookies <= 0) {
1699 					brelse(bp);
1700 					goto out;
1701 				}
1702 			}
1703 			off = offset + sizeof(struct direntry);
1704 		}
1705 		brelse(bp);
1706 	}
1707 out:
1708 	/* Subtract unused cookies */
1709 	if (ap->a_ncookies)
1710 		*ap->a_ncookies -= ncookies;
1711 
1712 	uio->uio_offset = off;
1713 
1714 	/*
1715 	 * Set the eofflag (NFS uses it)
1716 	 */
1717 	if (ap->a_eofflag) {
1718 		if (dep->de_FileSize - (offset - bias) <= 0)
1719 			*ap->a_eofflag = 1;
1720 		else
1721 			*ap->a_eofflag = 0;
1722 	}
1723 	return (error);
1724 }
1725 
1726 /*
1727  * vp  - address of vnode file the file
1728  * bn  - which cluster we are interested in mapping to a filesystem block number.
1729  * vpp - returns the vnode for the block special file holding the filesystem
1730  *	 containing the file of interest
1731  * bnp - address of where to return the filesystem relative block number
1732  */
1733 static int
1734 msdosfs_bmap(ap)
1735 	struct vop_bmap_args /* {
1736 		struct vnode *a_vp;
1737 		daddr64_t a_bn;
1738 		struct vnode **a_vpp;
1739 		daddr64_t *a_bnp;
1740 		int *a_runp;
1741 		int *a_runb;
1742 	} */ *ap;
1743 {
1744 	struct denode *dep = VTODE(ap->a_vp);
1745 	daddr_t blkno;
1746 	int error;
1747 
1748 	if (ap->a_vpp != NULL)
1749 		*ap->a_vpp = dep->de_devvp;
1750 	if (ap->a_bnp == NULL)
1751 		return (0);
1752 	if (ap->a_runp) {
1753 		/*
1754 		 * Sequential clusters should be counted here.
1755 		 */
1756 		*ap->a_runp = 0;
1757 	}
1758 	if (ap->a_runb) {
1759 		*ap->a_runb = 0;
1760 	}
1761 	error = pcbmap(dep, ap->a_bn, &blkno, 0, 0);
1762 	*ap->a_bnp = blkno;
1763 	return (error);
1764 }
1765 
1766 static int
1767 msdosfs_strategy(ap)
1768 	struct vop_strategy_args /* {
1769 		struct vnode *a_vp;
1770 		struct buf *a_bp;
1771 	} */ *ap;
1772 {
1773 	struct buf *bp = ap->a_bp;
1774 	struct denode *dep = VTODE(bp->b_vp);
1775 	struct vnode *vp;
1776 	int error = 0;
1777 	daddr_t blkno;
1778 
1779 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1780 		panic("msdosfs_strategy: spec");
1781 	/*
1782 	 * If we don't already know the filesystem relative block number
1783 	 * then get it using pcbmap().  If pcbmap() returns the block
1784 	 * number as -1 then we've got a hole in the file.  DOS filesystems
1785 	 * don't allow files with holes, so we shouldn't ever see this.
1786 	 */
1787 	if (bp->b_blkno == bp->b_lblkno) {
1788 		error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1789 		bp->b_blkno = blkno;
1790 		if (error) {
1791 			bp->b_error = error;
1792 			bp->b_ioflags |= BIO_ERROR;
1793 			bufdone(bp);
1794 			return (error);
1795 		}
1796 		if ((long)bp->b_blkno == -1)
1797 			vfs_bio_clrbuf(bp);
1798 	}
1799 	if (bp->b_blkno == -1) {
1800 		bufdone(bp);
1801 		return (0);
1802 	}
1803 	/*
1804 	 * Read/write the block from/to the disk that contains the desired
1805 	 * file block.
1806 	 */
1807 	vp = dep->de_devvp;
1808 	bp->b_dev = vp->v_rdev;
1809 	VOP_STRATEGY(vp, bp);
1810 	return (0);
1811 }
1812 
1813 static int
1814 msdosfs_print(ap)
1815 	struct vop_print_args /* {
1816 		struct vnode *vp;
1817 	} */ *ap;
1818 {
1819 	struct denode *dep = VTODE(ap->a_vp);
1820 
1821 	printf(
1822 	    "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1823 	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1824 	printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1825 	lockmgr_printinfo(&ap->a_vp->v_lock);
1826 	printf("\n");
1827 	return (0);
1828 }
1829 
1830 static int
1831 msdosfs_pathconf(ap)
1832 	struct vop_pathconf_args /* {
1833 		struct vnode *a_vp;
1834 		int a_name;
1835 		int *a_retval;
1836 	} */ *ap;
1837 {
1838 	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1839 
1840 	switch (ap->a_name) {
1841 	case _PC_LINK_MAX:
1842 		*ap->a_retval = 1;
1843 		return (0);
1844 	case _PC_NAME_MAX:
1845 		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1846 		return (0);
1847 	case _PC_PATH_MAX:
1848 		*ap->a_retval = PATH_MAX;
1849 		return (0);
1850 	case _PC_CHOWN_RESTRICTED:
1851 		*ap->a_retval = 1;
1852 		return (0);
1853 	case _PC_NO_TRUNC:
1854 		*ap->a_retval = 0;
1855 		return (0);
1856 	default:
1857 		return (EINVAL);
1858 	}
1859 	/* NOTREACHED */
1860 }
1861 
1862 
1863 /* Global vfs data structures for msdosfs */
1864 vop_t **msdosfs_vnodeop_p;
1865 static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1866 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1867 	{ &vop_access_desc,		(vop_t *) msdosfs_access },
1868 	{ &vop_bmap_desc,		(vop_t *) msdosfs_bmap },
1869 	{ &vop_cachedlookup_desc,	(vop_t *) msdosfs_lookup },
1870 	{ &vop_close_desc,		(vop_t *) msdosfs_close },
1871 	{ &vop_create_desc,		(vop_t *) msdosfs_create },
1872 	{ &vop_fsync_desc,		(vop_t *) msdosfs_fsync },
1873 	{ &vop_getattr_desc,		(vop_t *) msdosfs_getattr },
1874 	{ &vop_inactive_desc,		(vop_t *) msdosfs_inactive },
1875 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
1876 	{ &vop_link_desc,		(vop_t *) msdosfs_link },
1877 	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
1878 	{ &vop_lookup_desc,		(vop_t *) vfs_cache_lookup },
1879 	{ &vop_mkdir_desc,		(vop_t *) msdosfs_mkdir },
1880 	{ &vop_mknod_desc,		(vop_t *) msdosfs_mknod },
1881 	{ &vop_pathconf_desc,		(vop_t *) msdosfs_pathconf },
1882 	{ &vop_print_desc,		(vop_t *) msdosfs_print },
1883 	{ &vop_read_desc,		(vop_t *) msdosfs_read },
1884 	{ &vop_readdir_desc,		(vop_t *) msdosfs_readdir },
1885 	{ &vop_reclaim_desc,		(vop_t *) msdosfs_reclaim },
1886 	{ &vop_remove_desc,		(vop_t *) msdosfs_remove },
1887 	{ &vop_rename_desc,		(vop_t *) msdosfs_rename },
1888 	{ &vop_rmdir_desc,		(vop_t *) msdosfs_rmdir },
1889 	{ &vop_setattr_desc,		(vop_t *) msdosfs_setattr },
1890 	{ &vop_strategy_desc,		(vop_t *) msdosfs_strategy },
1891 	{ &vop_symlink_desc,		(vop_t *) msdosfs_symlink },
1892 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
1893 	{ &vop_write_desc,		(vop_t *) msdosfs_write },
1894 	{ NULL, NULL }
1895 };
1896 static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1897 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1898 
1899 VNODEOP_SET(msdosfs_vnodeop_opv_desc);
1900