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