Home | History | Annotate | Line # | Download | only in msdosfs
      1 /*	$NetBSD: msdosfs_vnops.c,v 1.114 2026/09/26 22:04:55 riastradh 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.114 2026/09/26 22:04:55 riastradh 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_crap.mlr_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 = msdosfs_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 = msdosfs_createde(&ndirent, pdep, &pdep->de_crap, &dep,
    153 	    cnp)) != 0)
    154 		goto bad;
    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 	msdosfs_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 		msdosfs_dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
    294 		    &vap->va_atime);
    295 		msdosfs_dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
    296 		    pmp->pm_gmtoff, &vap->va_birthtime);
    297 	} else {
    298 		vap->va_atime = vap->va_mtime;
    299 		vap->va_birthtime = vap->va_mtime;
    300 	}
    301 	/* msdos has no separate attribute change timestamps. */
    302 	vap->va_ctime = vap->va_mtime;
    303 	vap->va_flags = 0;
    304 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0) {
    305 		vap->va_flags |= SF_ARCHIVED;
    306 		vap->va_mode  |= S_ARCH1;
    307 	}
    308 	vap->va_gen = 0;
    309 	vap->va_blocksize = pmp->pm_bpcluster;
    310 	vap->va_bytes =
    311 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
    312 	vap->va_type = ap->a_vp->v_type;
    313 	return (0);
    314 }
    315 
    316 int
    317 msdosfs_setattr(void *v)
    318 {
    319 	struct vop_setattr_args /* {
    320 		struct vnode *a_vp;
    321 		struct vattr *a_vap;
    322 		kauth_cred_t a_cred;
    323 	} */ *ap = v;
    324 	int error = 0, de_changed = 0;
    325 	struct denode *dep = VTODE(ap->a_vp);
    326 	struct msdosfsmount *pmp = dep->de_pmp;
    327 	struct vnode *vp  = ap->a_vp;
    328 	struct vattr *vap = ap->a_vap;
    329 	kauth_cred_t cred = ap->a_cred;
    330 
    331 #ifdef MSDOSFS_DEBUG
    332 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
    333 	    ap->a_vp, vap, cred);
    334 #endif
    335 	/*
    336 	 * Note we silently ignore uid or gid changes.
    337 	 */
    338 	if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
    339 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    340 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    341 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
    342 	    (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
    343 	    (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
    344 #ifdef MSDOSFS_DEBUG
    345 		printf("msdosfs_setattr(): returning EINVAL\n");
    346 		printf("    va_type %d, va_nlink %x, va_fsid %"PRIx64", va_fileid %llx\n",
    347 		    vap->va_type, vap->va_nlink, vap->va_fsid,
    348 		    (unsigned long long)vap->va_fileid);
    349 		printf("    va_blocksize %lx, va_rdev %"PRIx64", va_bytes %"PRIx64", va_gen %lx\n",
    350 		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
    351 #endif
    352 		return (EINVAL);
    353 	}
    354 	/*
    355 	 * Silently ignore attributes modifications on directories.
    356 	 */
    357 	if (ap->a_vp->v_type == VDIR)
    358 		return 0;
    359 
    360 	if (vap->va_size != VNOVAL) {
    361 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    362 			error = EROFS;
    363 			goto bad;
    364 		}
    365 		error = msdosfs_detrunc(dep, (u_long)vap->va_size, 0, cred);
    366 		if (error)
    367 			goto bad;
    368 		de_changed = 1;
    369 	}
    370 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
    371 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    372 			error = EROFS;
    373 			goto bad;
    374 		}
    375 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES,
    376 		    ap->a_vp, NULL, genfs_can_chtimes(ap->a_vp, cred,
    377 			pmp->pm_uid, vap->va_vaflags));
    378 		if (error)
    379 			goto bad;
    380 		if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
    381 		    vap->va_atime.tv_sec != VNOVAL)
    382 			msdosfs_unix2dostime(&vap->va_atime, pmp->pm_gmtoff,
    383 			    &dep->de_ADate, NULL, NULL);
    384 		if (vap->va_mtime.tv_sec != VNOVAL)
    385 			msdosfs_unix2dostime(&vap->va_mtime, pmp->pm_gmtoff,
    386 			    &dep->de_MDate, &dep->de_MTime, NULL);
    387 		if (vap->va_birthtime.tv_sec != VNOVAL)
    388 			msdosfs_unix2dostime(&vap->va_mtime, pmp->pm_gmtoff,
    389 			    &dep->de_CDate, &dep->de_CTime, &dep->de_CHun);
    390 		dep->de_Attributes |= ATTR_ARCHIVE;
    391 		dep->de_flag |= DE_MODIFIED;
    392 		de_changed = 1;
    393 	}
    394 	/*
    395 	 * DOS files only have the ability to have their writability
    396 	 * attribute set, so we use the owner write bit to set the readonly
    397 	 * attribute.
    398 	 */
    399 	if (vap->va_mode != (mode_t)VNOVAL) {
    400 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    401 			error = EROFS;
    402 			goto bad;
    403 		}
    404 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
    405 		    NULL, genfs_can_chflags(vp, cred, pmp->pm_uid, false));
    406 		if (error)
    407 			goto bad;
    408 		/* We ignore the read and execute bits. */
    409 		if (vap->va_mode & S_IWUSR)
    410 			dep->de_Attributes &= ~ATTR_READONLY;
    411 		else
    412 			dep->de_Attributes |= ATTR_READONLY;
    413 		dep->de_flag |= DE_MODIFIED;
    414 		de_changed = 1;
    415 	}
    416 	/*
    417 	 * Allow the `archived' bit to be toggled.
    418 	 */
    419 	if (vap->va_flags != VNOVAL) {
    420 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    421 			error = EROFS;
    422 			goto bad;
    423 		}
    424 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
    425 		    NULL, genfs_can_chflags(vp, cred, pmp->pm_uid, false));
    426 		if (error)
    427 			goto bad;
    428 		if (vap->va_flags & SF_ARCHIVED)
    429 			dep->de_Attributes &= ~ATTR_ARCHIVE;
    430 		else
    431 			dep->de_Attributes |= ATTR_ARCHIVE;
    432 		dep->de_flag |= DE_MODIFIED;
    433 		de_changed = 1;
    434 	}
    435 
    436 	if (de_changed) {
    437 		error = msdosfs_deupdat(dep, 1);
    438 		if (error)
    439 			goto bad;
    440 	}
    441 
    442 bad:
    443 	return error;
    444 }
    445 
    446 int
    447 msdosfs_read(void *v)
    448 {
    449 	struct vop_read_args /* {
    450 		struct vnode *a_vp;
    451 		struct uio *a_uio;
    452 		int a_ioflag;
    453 		kauth_cred_t a_cred;
    454 	} */ *ap = v;
    455 	int error = 0;
    456 	int64_t diff;
    457 	int blsize;
    458 	long n;
    459 	long on;
    460 	daddr_t lbn;
    461 	vsize_t bytelen;
    462 	struct buf *bp;
    463 	struct vnode *vp = ap->a_vp;
    464 	struct denode *dep = VTODE(vp);
    465 	struct msdosfsmount *pmp = dep->de_pmp;
    466 	struct uio *uio = ap->a_uio;
    467 
    468 	/*
    469 	 * If they didn't ask for any data, then we are done.
    470 	 */
    471 
    472 	if (uio->uio_resid == 0)
    473 		return (0);
    474 	if (uio->uio_offset < 0)
    475 		return (EINVAL);
    476 	if (uio->uio_offset >= dep->de_FileSize)
    477 		return (0);
    478 
    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_VNODE_FLAGS(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 			return (0);
    504 		}
    505 		/* file size (and hence diff) may be up to 4GB */
    506 		diff = dep->de_FileSize - uio->uio_offset;
    507 		if (diff < n)
    508 			n = (long) diff;
    509 
    510 		/* convert cluster # to sector # */
    511 		error = msdosfs_pcbmap(dep, lbn, &lbn, 0, &blsize);
    512 		if (error)
    513 			goto bad;
    514 
    515 		/*
    516 		 * If we are operating on a directory file then be sure to
    517 		 * do i/o with the vnode for the filesystem instead of the
    518 		 * vnode for the directory.
    519 		 */
    520 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, lbn), blsize,
    521 		    0, &bp);
    522 		if (error) {
    523 			goto bad;
    524 		}
    525 		n = MIN(n, pmp->pm_bpcluster - bp->b_resid);
    526 		error = uiomove((char *)bp->b_data + on, (int) n, uio);
    527 		brelse(bp, 0);
    528 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
    529 
    530 out:
    531 	if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
    532 		int uerror;
    533 
    534 		uerror = msdosfs_deupdat(dep, 1);
    535 		if (error == 0)
    536 			error = uerror;
    537 	}
    538 bad:
    539 	return (error);
    540 }
    541 
    542 /*
    543  * Write data to a file or directory.
    544  */
    545 int
    546 msdosfs_write(void *v)
    547 {
    548 	struct vop_write_args /* {
    549 		struct vnode *a_vp;
    550 		struct uio *a_uio;
    551 		int a_ioflag;
    552 		kauth_cred_t a_cred;
    553 	} */ *ap = v;
    554 	int resid;
    555 	int error = 0;
    556 	int ioflag = ap->a_ioflag;
    557 	u_long osize;
    558 	u_long count;
    559 	vsize_t bytelen;
    560 	off_t oldoff;
    561 	size_t rem;
    562 	struct uio *uio = ap->a_uio;
    563 	struct vnode *vp = ap->a_vp;
    564 	struct denode *dep = VTODE(vp);
    565 	struct msdosfsmount *pmp = dep->de_pmp;
    566 	kauth_cred_t cred = ap->a_cred;
    567 	bool async;
    568 
    569 #ifdef MSDOSFS_DEBUG
    570 	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
    571 	    vp, uio, ioflag, cred);
    572 	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
    573 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
    574 #endif
    575 
    576 	switch (vp->v_type) {
    577 	case VREG:
    578 		if (ioflag & IO_APPEND)
    579 			uio->uio_offset = dep->de_FileSize;
    580 		break;
    581 	case VDIR:
    582 		return EISDIR;
    583 	default:
    584 		panic("msdosfs_write(): bad file type");
    585 	}
    586 
    587 	if (uio->uio_offset < 0)
    588 		return (EINVAL);
    589 
    590 	if (uio->uio_resid == 0)
    591 		return (0);
    592 
    593 	/* Don't bother to try to write files larger than the fs limit */
    594 	if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
    595 		return (EFBIG);
    596 
    597 	/*
    598 	 * If the offset we are starting the write at is beyond the end of
    599 	 * the file, then they've done a seek.  Unix filesystems allow
    600 	 * files with holes in them, DOS doesn't so we must fill the hole
    601 	 * with zeroed blocks.
    602 	 */
    603 	if (uio->uio_offset > dep->de_FileSize) {
    604 		if ((error = msdosfs_deextend(dep, uio->uio_offset,
    605 		    cred)) != 0) {
    606 			return (error);
    607 		}
    608 	}
    609 
    610 	/*
    611 	 * Remember some values in case the write fails.
    612 	 */
    613 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    614 	resid = uio->uio_resid;
    615 	osize = dep->de_FileSize;
    616 
    617 	/*
    618 	 * If we write beyond the end of the file, extend it to its ultimate
    619 	 * size ahead of the time to hopefully get a contiguous area.
    620 	 */
    621 	if (uio->uio_offset + resid > osize) {
    622 		count = de_clcount(pmp, uio->uio_offset + resid) -
    623 			de_clcount(pmp, osize);
    624 		if ((error = msdosfs_extendfile(dep, count, NULL, NULL, 0)))
    625 			goto errexit;
    626 
    627 		dep->de_FileSize = uio->uio_offset + resid;
    628 		/* hint uvm to not read in extended part */
    629 		uvm_vnp_setwritesize(vp, dep->de_FileSize);
    630 		/* zero out the remainder of the last page */
    631 		rem = round_page(dep->de_FileSize) - dep->de_FileSize;
    632 		if (rem > 0)
    633 			ubc_zerorange(&vp->v_uobj, (off_t)dep->de_FileSize,
    634 			    rem, UBC_VNODE_FLAGS(vp));
    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_VNODE_FLAGS(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 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    653 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    654 			    (uio->uio_offset >> 16) << 16,
    655 			    PGO_CLEANIT | PGO_LAZY);
    656 		}
    657 	} while (error == 0 && uio->uio_resid > 0);
    658 
    659 	/* set final size */
    660 	uvm_vnp_setsize(vp, dep->de_FileSize);
    661 	if (error == 0 && ioflag & IO_SYNC) {
    662 		rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    663 		error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    664 		    round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
    665 	}
    666 	dep->de_flag |= DE_UPDATE;
    667 
    668 	/*
    669 	 * If the write failed and they want us to, truncate the file back
    670 	 * to the size it was before the write was attempted.
    671 	 */
    672 errexit:
    673 	if (error) {
    674 		msdosfs_detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
    675 		uio->uio_offset -= resid - uio->uio_resid;
    676 		uio->uio_resid = resid;
    677 	} else if ((ioflag & IO_SYNC) == IO_SYNC)
    678 		error = msdosfs_deupdat(dep, 1);
    679 	KASSERT(vp->v_size == dep->de_FileSize);
    680 	return (error);
    681 }
    682 
    683 int
    684 msdosfs_update(struct vnode *vp, const struct timespec *acc,
    685     const struct timespec *mod, int flags)
    686 {
    687 	struct buf *bp;
    688 	struct direntry *dirp;
    689 	struct denode *dep;
    690 	int error;
    691 
    692 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    693 		return (0);
    694 	dep = VTODE(vp);
    695 	DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
    696 	if ((dep->de_flag & DE_MODIFIED) == 0)
    697 		return (0);
    698 	dep->de_flag &= ~DE_MODIFIED;
    699 	if (dep->de_Attributes & ATTR_DIRECTORY)
    700 		return (0);
    701 	if (dep->de_refcnt <= 0)
    702 		return (0);
    703 	error = msdosfs_readde(dep, &bp, &dirp);
    704 	if (error)
    705 		return (error);
    706 	DE_EXTERNALIZE(dirp, dep);
    707 	if (flags & (UPDATE_WAIT|UPDATE_DIROP))
    708 		return (bwrite(bp));
    709 	else {
    710 		bdwrite(bp);
    711 		return (0);
    712 	}
    713 }
    714 
    715 int
    716 msdosfs_remove(void *v)
    717 {
    718 	struct vop_remove_v3_args /* {
    719 		struct vnode *a_dvp;
    720 		struct vnode *a_vp;
    721 		struct componentname *a_cnp;
    722 		nlink_t ctx_vp_new_nlink;
    723 	} */ *ap = v;
    724 	struct denode *dep = VTODE(ap->a_vp);
    725 	struct denode *ddep = VTODE(ap->a_dvp);
    726 	int error;
    727 
    728 	if (ap->a_vp->v_type == VDIR)
    729 		error = EPERM;
    730 	else
    731 		error = msdosfs_removede(ddep, dep, &ddep->de_crap);
    732 #ifdef MSDOSFS_DEBUG
    733 	printf("msdosfs_remove(), dep %p, usecount %d\n",
    734 		dep, vrefcnt(ap->a_vp));
    735 #endif
    736 	if (ddep == dep)
    737 		vrele(ap->a_vp);
    738 	else
    739 		vput(ap->a_vp);	/* causes msdosfs_inactive() to be called
    740 				 * via vrele() */
    741 
    742 	return (error);
    743 }
    744 
    745 static const struct {
    746 	struct direntry dot;
    747 	struct direntry dotdot;
    748 } dosdirtemplate = {
    749 	{	".       ", "   ",			/* the . entry */
    750 		ATTR_DIRECTORY,				/* file attribute */
    751 		0,	 				/* reserved */
    752 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
    753 		{ 0, 0 },				/* access date */
    754 		{ 0, 0 },				/* high bits of start cluster */
    755 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
    756 		{ 0, 0 },				/* startcluster */
    757 		{ 0, 0, 0, 0 } 				/* filesize */
    758 	},
    759 	{	"..      ", "   ",			/* the .. entry */
    760 		ATTR_DIRECTORY,				/* file attribute */
    761 		0,	 				/* reserved */
    762 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
    763 		{ 0, 0 },				/* access date */
    764 		{ 0, 0 },				/* high bits of start cluster */
    765 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
    766 		{ 0, 0 },				/* startcluster */
    767 		{ 0, 0, 0, 0 }				/* filesize */
    768 	}
    769 };
    770 
    771 int
    772 msdosfs_mkdir(void *v)
    773 {
    774 	struct vop_mkdir_v3_args /* {
    775 		struct vnode *a_dvp;
    776 		struvt vnode **a_vpp;
    777 		struvt componentname *a_cnp;
    778 		struct vattr *a_vap;
    779 	} */ *ap = v;
    780 	struct componentname *cnp = ap->a_cnp;
    781 	struct denode ndirent;
    782 	struct denode *dep;
    783 	struct denode *pdep = VTODE(ap->a_dvp);
    784 	int error;
    785 	int bn;
    786 	u_long newcluster, pcl;
    787 	daddr_t lbn;
    788 	struct direntry *denp;
    789 	struct msdosfsmount *pmp = pdep->de_pmp;
    790 	struct buf *bp;
    791 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
    792 
    793 	/*
    794 	 * If this is the root directory and there is no space left we
    795 	 * can't do anything.  This is because the root directory can not
    796 	 * change size.
    797 	 */
    798 	if (pdep->de_StartCluster == MSDOSFSROOT
    799 	    && pdep->de_crap.mlr_fndoffset >= pdep->de_FileSize) {
    800 		error = ENOSPC;
    801 		goto bad2;
    802 	}
    803 
    804 	/*
    805 	 * Allocate a cluster to hold the about to be created directory.
    806 	 */
    807 	error = msdosfs_clusteralloc(pmp, 0, 1, &newcluster, NULL);
    808 	if (error)
    809 		goto bad2;
    810 
    811 	memset(&ndirent, 0, sizeof(ndirent));
    812 	ndirent.de_pmp = pmp;
    813 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
    814 	DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
    815 
    816 	/*
    817 	 * Now fill the cluster with the "." and ".." entries. And write
    818 	 * the cluster to disk.  This way it is there for the parent
    819 	 * directory to be pointing at if there were a crash.
    820 	 */
    821 	bn = cntobn(pmp, newcluster);
    822 	lbn = de_bn2kb(pmp, bn);
    823 	/* always succeeds */
    824 	bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
    825 	memset(bp->b_data, 0, pmp->pm_bpcluster);
    826 	memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
    827 	denp = (struct direntry *)bp->b_data;
    828 	putushort(denp[0].deStartCluster, newcluster);
    829 	putushort(denp[0].deCDate, ndirent.de_CDate);
    830 	putushort(denp[0].deCTime, ndirent.de_CTime);
    831 	denp[0].deCHundredth = ndirent.de_CHun;
    832 	putushort(denp[0].deADate, ndirent.de_ADate);
    833 	putushort(denp[0].deMDate, ndirent.de_MDate);
    834 	putushort(denp[0].deMTime, ndirent.de_MTime);
    835 	pcl = pdep->de_StartCluster;
    836 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
    837 		pcl = 0;
    838 	putushort(denp[1].deStartCluster, pcl);
    839 	putushort(denp[1].deCDate, ndirent.de_CDate);
    840 	putushort(denp[1].deCTime, ndirent.de_CTime);
    841 	denp[1].deCHundredth = ndirent.de_CHun;
    842 	putushort(denp[1].deADate, ndirent.de_ADate);
    843 	putushort(denp[1].deMDate, ndirent.de_MDate);
    844 	putushort(denp[1].deMTime, ndirent.de_MTime);
    845 	if (FAT32(pmp)) {
    846 		putushort(denp[0].deHighClust, newcluster >> 16);
    847 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
    848 	} else {
    849 		putushort(denp[0].deHighClust, 0);
    850 		putushort(denp[1].deHighClust, 0);
    851 	}
    852 
    853 	if (async)
    854 		bdwrite(bp);
    855 	else if ((error = bwrite(bp)) != 0)
    856 		goto bad;
    857 
    858 	/*
    859 	 * Now build up a directory entry pointing to the newly allocated
    860 	 * cluster.  This will be written to an empty slot in the parent
    861 	 * directory.
    862 	 */
    863 	if ((error = msdosfs_uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
    864 		goto bad;
    865 
    866 	ndirent.de_Attributes = ATTR_DIRECTORY;
    867 	ndirent.de_StartCluster = newcluster;
    868 	ndirent.de_FileSize = 0;
    869 	ndirent.de_dev = pdep->de_dev;
    870 	ndirent.de_devvp = pdep->de_devvp;
    871 	if ((error = msdosfs_createde(&ndirent, pdep, &pdep->de_crap, &dep,
    872 	    cnp)) != 0)
    873 		goto bad;
    874 	*ap->a_vpp = DETOV(dep);
    875 	return (0);
    876 
    877 bad:
    878 	msdosfs_clusterfree(pmp, newcluster, NULL);
    879 bad2:
    880 	return (error);
    881 }
    882 
    883 int
    884 msdosfs_rmdir(void *v)
    885 {
    886 	struct vop_rmdir_v2_args /* {
    887 		struct vnode *a_dvp;
    888 		struct vnode *a_vp;
    889 		struct componentname *a_cnp;
    890 	} */ *ap = v;
    891 	struct vnode *vp = ap->a_vp;
    892 	struct vnode *dvp = ap->a_dvp;
    893 	struct componentname *cnp = ap->a_cnp;
    894 	struct denode *ip, *dp;
    895 	int error;
    896 
    897 	ip = VTODE(vp);
    898 	dp = VTODE(dvp);
    899 	/*
    900 	 * No rmdir "." please.
    901 	 */
    902 	if (dp == ip) {
    903 		vrele(vp);
    904 		return (EINVAL);
    905 	}
    906 	/*
    907 	 * Verify the directory is empty (and valid).
    908 	 * (Rmdir ".." won't be valid since
    909 	 *  ".." will contain a reference to
    910 	 *  the current directory and thus be
    911 	 *  non-empty.)
    912 	 */
    913 	error = 0;
    914 	if (!msdosfs_dosdirempty(ip) || ip->de_flag & DE_RENAME) {
    915 		error = ENOTEMPTY;
    916 		goto out;
    917 	}
    918 	/*
    919 	 * Delete the entry from the directory.  For dos filesystems this
    920 	 * gets rid of the directory entry on disk, the in memory copy
    921 	 * still exists but the de_refcnt is <= 0.  This prevents it from
    922 	 * being found by deget().  When the vput() on dep is done we give
    923 	 * up access and eventually msdosfs_reclaim() will be called which
    924 	 * will remove it from the denode cache.
    925 	 */
    926 	if ((error = msdosfs_removede(dp, ip, &dp->de_crap)) != 0)
    927 		goto out;
    928 	/*
    929 	 * This is where we decrement the link count in the parent
    930 	 * directory.  Since dos filesystems don't do this we just purge
    931 	 * the name cache and let go of the parent directory denode.
    932 	 */
    933 	cache_purge(dvp);
    934 	/*
    935 	 * Truncate the directory that is being deleted.
    936 	 */
    937 	error = msdosfs_detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
    938 	cache_purge(vp);
    939 out:
    940 	vput(vp);
    941 	return (error);
    942 }
    943 
    944 int
    945 msdosfs_readdir(void *v)
    946 {
    947 	struct vop_readdir_args /* {
    948 		struct vnode *a_vp;
    949 		struct uio *a_uio;
    950 		kauth_cred_t a_cred;
    951 		int *a_eofflag;
    952 		off_t **a_cookies;
    953 		int *a_ncookies;
    954 	} */ *ap = v;
    955 	int error = 0;
    956 	int diff;
    957 	long n;
    958 	int blsize;
    959 	long on;
    960 	long lost;
    961 	long count;
    962 	u_long cn;
    963 	ino_t fileno;
    964 	u_long dirsperblk;
    965 	long bias = 0;
    966 	daddr_t bn, lbn;
    967 	struct buf *bp;
    968 	struct denode *dep = VTODE(ap->a_vp);
    969 	struct msdosfsmount *pmp = dep->de_pmp;
    970 	struct direntry *dentp;
    971 	struct dirent *dirbuf;
    972 	struct uio *uio = ap->a_uio;
    973 	off_t *cookies = NULL;
    974 	int ncookies = 0, nc = 0;
    975 	off_t offset, uio_off;
    976 	int chksum = -1;
    977 	uint16_t namlen;
    978 
    979 #ifdef MSDOSFS_DEBUG
    980 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
    981 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
    982 #endif
    983 
    984 	/*
    985 	 * msdosfs_readdir() won't operate properly on regular files since
    986 	 * it does i/o only with the filesystem vnode, and hence can
    987 	 * retrieve the wrong block from the buffer cache for a plain file.
    988 	 * So, fail attempts to readdir() on a plain file.
    989 	 */
    990 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
    991 		return (ENOTDIR);
    992 
    993 	/*
    994 	 * If the user buffer is smaller than the size of one dos directory
    995 	 * entry or the file offset is not a multiple of the size of a
    996 	 * directory entry, then we fail the read.
    997 	 */
    998 	count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
    999 	offset = uio->uio_offset;
   1000 	if (count < sizeof(struct direntry) ||
   1001 	    (offset & (sizeof(struct direntry) - 1)))
   1002 		return (EINVAL);
   1003 	lost = uio->uio_resid - count;
   1004 	uio->uio_resid = count;
   1005 	uio_off = uio->uio_offset;
   1006 
   1007 
   1008 	/* Allocate a temporary dirent buffer. */
   1009 	dirbuf = malloc(sizeof(struct dirent), M_MSDOSFSTMP, M_WAITOK | M_ZERO);
   1010 
   1011 	if (ap->a_ncookies) {
   1012 		nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
   1013 		cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
   1014 		*ap->a_cookies = cookies;
   1015 	}
   1016 
   1017 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
   1018 
   1019 	/*
   1020 	 * If they are reading from the root directory then, we simulate
   1021 	 * the . and .. entries since these don't exist in the root
   1022 	 * directory.  We also set the offset bias to make up for having to
   1023 	 * simulate these entries. By this I mean that at file offset 64 we
   1024 	 * read the first entry in the root directory that lives on disk.
   1025 	 */
   1026 	if (dep->de_StartCluster == MSDOSFSROOT
   1027 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
   1028 #if 0
   1029 		printf("msdosfs_readdir(): going after . or .. in root dir, "
   1030 		    "offset %" PRIu64 "\n", offset);
   1031 #endif
   1032 		bias = 2 * sizeof(struct direntry);
   1033 		if (offset < bias) {
   1034 			for (n = (int)offset / sizeof(struct direntry);
   1035 			     n < 2; n++) {
   1036 				if (FAT32(pmp))
   1037 					dirbuf->d_fileno = cntobn(pmp,
   1038 					     (ino_t)pmp->pm_rootdirblk)
   1039 					     * dirsperblk;
   1040 				else
   1041 					dirbuf->d_fileno = 1;
   1042 				dirbuf->d_type = DT_DIR;
   1043 				switch (n) {
   1044 				case 0:
   1045 					dirbuf->d_namlen = 1;
   1046 					strlcpy(dirbuf->d_name, ".",
   1047 					    sizeof(dirbuf->d_name));
   1048 					break;
   1049 				case 1:
   1050 					dirbuf->d_namlen = 2;
   1051 					strlcpy(dirbuf->d_name, "..",
   1052 					    sizeof(dirbuf->d_name));
   1053 					break;
   1054 				}
   1055 				dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
   1056 				if (uio->uio_resid < dirbuf->d_reclen)
   1057 					goto out;
   1058 				error = uiomove(dirbuf, dirbuf->d_reclen, uio);
   1059 				if (error)
   1060 					goto out;
   1061 				offset += sizeof(struct direntry);
   1062 				uio_off = offset;
   1063 				if (cookies) {
   1064 					*cookies++ = offset;
   1065 					ncookies++;
   1066 					if (ncookies >= nc)
   1067 						goto out;
   1068 				}
   1069 			}
   1070 		}
   1071 	}
   1072 
   1073 	while (uio->uio_resid > 0) {
   1074 		lbn = de_cluster(pmp, offset - bias);
   1075 		on = (offset - bias) & pmp->pm_crbomask;
   1076 		n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
   1077 		diff = dep->de_FileSize - (offset - bias);
   1078 		if (diff <= 0)
   1079 			break;
   1080 		n = MIN(n, diff);
   1081 		if ((error = msdosfs_pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
   1082 			break;
   1083 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
   1084 		    0, &bp);
   1085 		if (error) {
   1086 			goto bad;
   1087 		}
   1088 		n = MIN(n, blsize - bp->b_resid);
   1089 
   1090 		/*
   1091 		 * Convert from dos directory entries to fs-independent
   1092 		 * directory entries.
   1093 		 */
   1094 		for (dentp = (struct direntry *)((char *)bp->b_data + on);
   1095 		     (char *)dentp < (char *)bp->b_data + on + n;
   1096 		     dentp++, offset += sizeof(struct direntry)) {
   1097 #if 0
   1098 
   1099 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
   1100 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
   1101 #endif
   1102 			/*
   1103 			 * If this is an unused entry, we can stop.
   1104 			 */
   1105 			if (dentp->deName[0] == SLOT_EMPTY) {
   1106 				brelse(bp, 0);
   1107 				goto out;
   1108 			}
   1109 			/*
   1110 			 * Skip deleted entries.
   1111 			 */
   1112 			if (dentp->deName[0] == SLOT_DELETED) {
   1113 				chksum = -1;
   1114 				continue;
   1115 			}
   1116 
   1117 			/*
   1118 			 * Handle Win95 long directory entries
   1119 			 */
   1120 			if (dentp->deAttributes == ATTR_WIN95) {
   1121 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
   1122 					continue;
   1123 				chksum =
   1124 				    msdosfs_win2unixfn((struct winentry *)dentp,
   1125 				    dirbuf, chksum, &namlen,
   1126 				    pmp->pm_flags & MSDOSFSMNT_UTF8);
   1127 				if (chksum != -1)
   1128 					dirbuf->d_namlen = namlen;
   1129 				continue;
   1130 			}
   1131 
   1132 			/*
   1133 			 * Skip volume labels
   1134 			 */
   1135 			if (dentp->deAttributes & ATTR_VOLUME) {
   1136 				chksum = -1;
   1137 				continue;
   1138 			}
   1139 			/*
   1140 			 * This computation of d_fileno must match
   1141 			 * the computation of va_fileid in
   1142 			 * msdosfs_getattr.
   1143 			 */
   1144 			if (dentp->deAttributes & ATTR_DIRECTORY) {
   1145 				fileno = getushort(dentp->deStartCluster);
   1146 				if (FAT32(pmp))
   1147 					fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
   1148 				/* if this is the root directory */
   1149 				if (fileno == MSDOSFSROOT)
   1150 					if (FAT32(pmp))
   1151 						fileno = cntobn(pmp,
   1152 						    (ino_t)pmp->pm_rootdirblk)
   1153 						    * dirsperblk;
   1154 					else
   1155 						fileno = 1;
   1156 				else
   1157 					fileno = cntobn(pmp, fileno) * dirsperblk;
   1158 				dirbuf->d_fileno = fileno;
   1159 				dirbuf->d_type = DT_DIR;
   1160 			} else {
   1161 				dirbuf->d_fileno =
   1162 				    offset / sizeof(struct direntry);
   1163 				dirbuf->d_type = DT_REG;
   1164 			}
   1165 			if (chksum != msdosfs_winChksum(dentp->deName)) {
   1166 				char deName[11];
   1167 
   1168 				memcpy(deName, dentp->deName,
   1169 				       sizeof dentp->deName);
   1170 				memcpy(deName + 8, dentp->deExtension,
   1171 				       sizeof dentp->deExtension);
   1172 				assert(sizeof(deName) == sizeof(dentp->deName) +
   1173 					sizeof(dentp->deExtension));
   1174 				dirbuf->d_namlen =
   1175 				    msdosfs_dos2unixfn(deName,
   1176 				        (u_char *)dirbuf->d_name,
   1177 				        pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
   1178 			} else
   1179 				dirbuf->d_name[dirbuf->d_namlen] = 0;
   1180 			namlen = dirbuf->d_namlen;
   1181 			chksum = -1;
   1182 			dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
   1183 			if (uio->uio_resid < dirbuf->d_reclen) {
   1184 				brelse(bp, 0);
   1185 				goto out;
   1186 			}
   1187 			error = uiomove(dirbuf, dirbuf->d_reclen, uio);
   1188 			if (error) {
   1189 				brelse(bp, 0);
   1190 				goto out;
   1191 			}
   1192 			uio_off = offset + sizeof(struct direntry);
   1193 			if (cookies) {
   1194 				*cookies++ = offset + sizeof(struct direntry);
   1195 				ncookies++;
   1196 				if (ncookies >= nc) {
   1197 					brelse(bp, 0);
   1198 					goto out;
   1199 				}
   1200 			}
   1201 		}
   1202 		brelse(bp, 0);
   1203 	}
   1204 
   1205 out:
   1206 	uio->uio_offset = uio_off;
   1207 	uio->uio_resid += lost;
   1208 	if (dep->de_FileSize - (offset - bias) <= 0)
   1209 		*ap->a_eofflag = 1;
   1210 	else
   1211 		*ap->a_eofflag = 0;
   1212 
   1213 	if (ap->a_ncookies) {
   1214 		if (error) {
   1215 			free(*ap->a_cookies, M_TEMP);
   1216 			*ap->a_ncookies = 0;
   1217 			*ap->a_cookies = NULL;
   1218 		} else
   1219 			*ap->a_ncookies = ncookies;
   1220 	}
   1221 
   1222 bad:
   1223 	free(dirbuf, M_MSDOSFSTMP);
   1224 	return (error);
   1225 }
   1226 
   1227 /*
   1228  * vp  - address of vnode file the file
   1229  * bn  - which cluster we are interested in mapping to a filesystem block number.
   1230  * vpp - returns the vnode for the block special file holding the filesystem
   1231  *	 containing the file of interest
   1232  * bnp - address of where to return the filesystem relative block number
   1233  */
   1234 int
   1235 msdosfs_bmap(void *v)
   1236 {
   1237 	struct vop_bmap_args /* {
   1238 		struct vnode *a_vp;
   1239 		daddr_t a_bn;
   1240 		struct vnode **a_vpp;
   1241 		daddr_t *a_bnp;
   1242 		int *a_runp;
   1243 	} */ *ap = v;
   1244 	struct denode *dep = VTODE(ap->a_vp);
   1245 	int run, maxrun;
   1246 	daddr_t runbn;
   1247 	int status;
   1248 
   1249 	if (ap->a_vpp != NULL)
   1250 		*ap->a_vpp = dep->de_devvp;
   1251 	if (ap->a_bnp == NULL)
   1252 		return (0);
   1253 	status = msdosfs_pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
   1254 
   1255 	/*
   1256 	 * From FreeBSD:
   1257 	 * A little kludgy, but we loop calling pcbmap until we
   1258 	 * reach the end of the contiguous piece, or reach MAXPHYS.
   1259 	 * Since it reduces disk I/Os, the "wasted" CPU is put to
   1260 	 * good use (4 to 5 fold sequential read I/O improvement on USB
   1261 	 * drives).
   1262 	 */
   1263 	if (ap->a_runp != NULL) {
   1264 		/* taken from ufs_bmap */
   1265 		maxrun = ulmin(MAXPHYS / dep->de_pmp->pm_bpcluster - 1,
   1266 			       dep->de_pmp->pm_maxcluster - ap->a_bn);
   1267 		for (run = 1; run <= maxrun; run++) {
   1268 			if (msdosfs_pcbmap(dep, ap->a_bn + run, &runbn, NULL,
   1269 			    NULL) != 0 || runbn !=
   1270 			            *ap->a_bnp + de_cn2bn(dep->de_pmp, run))
   1271 				break;
   1272 		}
   1273 		*ap->a_runp = run - 1;
   1274 	}
   1275 
   1276 	/*
   1277 	 * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
   1278 	 */
   1279 	*ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
   1280 	return status;
   1281 }
   1282 
   1283 int
   1284 msdosfs_strategy(void *v)
   1285 {
   1286 	struct vop_strategy_args /* {
   1287 		struct vnode *a_vp;
   1288 		struct buf *a_bp;
   1289 	} */ *ap = v;
   1290 	struct vnode *vp = ap->a_vp;
   1291 	struct buf *bp = ap->a_bp;
   1292 	struct denode *dep = VTODE(bp->b_vp);
   1293 	int error = 0;
   1294 
   1295 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1296 		panic("msdosfs_strategy: spec");
   1297 	/*
   1298 	 * If we don't already know the filesystem relative block number
   1299 	 * then get it using pcbmap().  If pcbmap() returns the block
   1300 	 * number as -1 then we've got a hole in the file.  DOS filesystems
   1301 	 * don't allow files with holes, so we shouldn't ever see this.
   1302 	 */
   1303 	if (bp->b_blkno == bp->b_lblkno) {
   1304 		error = msdosfs_pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
   1305 			       &bp->b_blkno, 0, 0);
   1306 		if (error)
   1307 			bp->b_blkno = -1;
   1308 		if (bp->b_blkno == -1)
   1309 			clrbuf(bp);
   1310 		else
   1311 			bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
   1312 	}
   1313 	if (bp->b_blkno == -1) {
   1314 		biodone(bp);
   1315 		return (error);
   1316 	}
   1317 
   1318 	/*
   1319 	 * Read/write the block from/to the disk that contains the desired
   1320 	 * file block.
   1321 	 */
   1322 
   1323 	vp = dep->de_devvp;
   1324 	return (VOP_STRATEGY(vp, bp));
   1325 }
   1326 
   1327 int
   1328 msdosfs_print(void *v)
   1329 {
   1330 	struct vop_print_args /* {
   1331 		struct vnode *vp;
   1332 	} */ *ap = v;
   1333 	struct denode *dep = VTODE(ap->a_vp);
   1334 
   1335 	printf(
   1336 	    "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
   1337 	    dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
   1338 	printf(" dev %llu, %llu ", (unsigned long long)major(dep->de_dev),
   1339 	    (unsigned long long)minor(dep->de_dev));
   1340 	printf("\n");
   1341 	return (0);
   1342 }
   1343 
   1344 int
   1345 msdosfs_advlock(void *v)
   1346 {
   1347 	struct vop_advlock_args /* {
   1348 		struct vnode *a_vp;
   1349 		void *a_id;
   1350 		int a_op;
   1351 		struct flock *a_fl;
   1352 		int a_flags;
   1353 	} */ *ap = v;
   1354 	struct denode *dep = VTODE(ap->a_vp);
   1355 
   1356 	return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
   1357 }
   1358 
   1359 int
   1360 msdosfs_pathconf(void *v)
   1361 {
   1362 	struct vop_pathconf_args /* {
   1363 		struct vnode *a_vp;
   1364 		int a_name;
   1365 		register_t *a_retval;
   1366 	} */ *ap = v;
   1367 
   1368 	switch (ap->a_name) {
   1369 	case _PC_LINK_MAX:
   1370 		*ap->a_retval = 1;
   1371 		return (0);
   1372 	case _PC_NAME_MAX:
   1373 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
   1374 		return (0);
   1375 	case _PC_PATH_MAX:
   1376 		*ap->a_retval = PATH_MAX;
   1377 		return (0);
   1378 	case _PC_CHOWN_RESTRICTED:
   1379 		*ap->a_retval = 1;
   1380 		return (0);
   1381 	case _PC_NO_TRUNC:
   1382 		*ap->a_retval = 1;
   1383 		return (0);
   1384 	case _PC_SYNC_IO:
   1385 		*ap->a_retval = 1;
   1386 		return (0);
   1387 	case _PC_FILESIZEBITS:
   1388 		*ap->a_retval = 32;
   1389 		return (0);
   1390 	default:
   1391 		return genfs_pathconf(ap);
   1392 	}
   1393 	/* NOTREACHED */
   1394 }
   1395 
   1396 /*
   1397  * Flush the blocks of a file to disk.
   1398  *
   1399  * This function is worthless for vnodes that represent directories. Maybe we
   1400  * could just do a sync if they try an fsync on a directory file.
   1401  */
   1402 int
   1403 msdosfs_fsync(void *v)
   1404 {
   1405 	struct vop_fsync_args /* {
   1406 		struct vnode *a_vp;
   1407 		kauth_cred_t a_cred;
   1408 		int a_flags;
   1409 		off_t offlo;
   1410 		off_t offhi;
   1411 	} */ *ap = v;
   1412 	struct vnode *vp = ap->a_vp;
   1413 	int wait;
   1414 	int error;
   1415 
   1416 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
   1417 	error = vflushbuf(vp, ap->a_flags);
   1418 	if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
   1419 		error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
   1420 
   1421 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
   1422 		struct denode *dep = VTODE(vp);
   1423 		struct vnode *devvp = dep->de_devvp;
   1424 
   1425 		int l = 0;
   1426 		error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
   1427 					  curlwp->l_cred);
   1428 	}
   1429 
   1430 	return (error);
   1431 }
   1432 
   1433 void
   1434 msdosfs_detimes(struct denode *dep, const struct timespec *acc,
   1435     const struct timespec *mod, const struct timespec *cre, int gmtoff)
   1436 {
   1437 	struct timespec *ts = NULL, tsb;
   1438 
   1439 	KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
   1440 	/* XXX just call getnanotime early and use result if needed? */
   1441 	dep->de_flag |= DE_MODIFIED;
   1442 	if (dep->de_flag & DE_UPDATE) {
   1443 		if (mod == NULL) {
   1444 			getnanotime(&tsb);
   1445 			mod = ts = &tsb;
   1446 		}
   1447 		msdosfs_unix2dostime(mod, gmtoff, &dep->de_MDate,
   1448 		    &dep->de_MTime, NULL);
   1449 		dep->de_Attributes |= ATTR_ARCHIVE;
   1450 	}
   1451 	if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
   1452 		if (dep->de_flag & DE_ACCESS)  {
   1453 			if (acc == NULL)
   1454 				acc = ts == NULL ?
   1455 				    (getnanotime(&tsb), ts = &tsb) : ts;
   1456 			msdosfs_unix2dostime(acc, gmtoff, &dep->de_ADate,
   1457 			    NULL, NULL);
   1458 		}
   1459 		if (dep->de_flag & DE_CREATE) {
   1460 			if (cre == NULL)
   1461 				cre = ts == NULL ?
   1462 				    (getnanotime(&tsb), ts = &tsb) : ts;
   1463 			msdosfs_unix2dostime(cre, gmtoff, &dep->de_CDate,
   1464 			    &dep->de_CTime, &dep->de_CHun);
   1465 		}
   1466 	}
   1467 
   1468 	dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
   1469 }
   1470 
   1471 /* Global vfs data structures for msdosfs */
   1472 int (**msdosfs_vnodeop_p)(void *);
   1473 const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
   1474 	{ &vop_default_desc, vn_default_error },
   1475 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
   1476 	{ &vop_lookup_desc, msdosfs_lookup },		/* lookup */
   1477 	{ &vop_create_desc, msdosfs_create },		/* create */
   1478 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
   1479 	{ &vop_open_desc, genfs_nullop },		/* open */
   1480 	{ &vop_close_desc, msdosfs_close },		/* close */
   1481 	{ &vop_access_desc, msdosfs_access },		/* access */
   1482 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
   1483 	{ &vop_getattr_desc, msdosfs_getattr },		/* getattr */
   1484 	{ &vop_setattr_desc, msdosfs_setattr },		/* setattr */
   1485 	{ &vop_read_desc, msdosfs_read },		/* read */
   1486 	{ &vop_write_desc, msdosfs_write },		/* write */
   1487 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
   1488 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
   1489 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
   1490 	{ &vop_ioctl_desc, genfs_enoioctl },		/* ioctl */
   1491 	{ &vop_poll_desc, genfs_poll },			/* poll */
   1492 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
   1493 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
   1494 	{ &vop_mmap_desc, genfs_mmap },			/* mmap */
   1495 	{ &vop_fsync_desc, msdosfs_fsync },		/* fsync */
   1496 	{ &vop_seek_desc, genfs_seek },			/* seek */
   1497 	{ &vop_remove_desc, msdosfs_remove },		/* remove */
   1498 	{ &vop_link_desc, genfs_eopnotsupp },		/* link */
   1499 	{ &vop_rename_desc, msdosfs_rename },		/* rename */
   1500 	{ &vop_mkdir_desc, msdosfs_mkdir },		/* mkdir */
   1501 	{ &vop_rmdir_desc, msdosfs_rmdir },		/* rmdir */
   1502 	{ &vop_symlink_desc, genfs_eopnotsupp },	/* symlink */
   1503 	{ &vop_readdir_desc, msdosfs_readdir },		/* readdir */
   1504 	{ &vop_readlink_desc, genfs_einval },		/* readlink */
   1505 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
   1506 	{ &vop_inactive_desc, msdosfs_inactive },	/* inactive */
   1507 	{ &vop_reclaim_desc, msdosfs_reclaim },		/* reclaim */
   1508 	{ &vop_lock_desc, genfs_lock },			/* lock */
   1509 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
   1510 	{ &vop_bmap_desc, msdosfs_bmap },		/* bmap */
   1511 	{ &vop_strategy_desc, msdosfs_strategy },	/* strategy */
   1512 	{ &vop_print_desc, msdosfs_print },		/* print */
   1513 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
   1514 	{ &vop_pathconf_desc, msdosfs_pathconf },	/* pathconf */
   1515 	{ &vop_advlock_desc, msdosfs_advlock },		/* advlock */
   1516 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1517 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
   1518 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
   1519 	{ NULL, NULL }
   1520 };
   1521 const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
   1522 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
   1523