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