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