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