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