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