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