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