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