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