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