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