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