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