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