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