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