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