Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_denode.c revision 1.39
      1 /*	$NetBSD: msdosfs_denode.c,v 1.39 2010/06/24 13:03:09 hannken 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_denode.c,v 1.39 2010/06/24 13:03:09 hannken Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/mount.h>
     56 #include <sys/fstrans.h>
     57 #include <sys/malloc.h>
     58 #include <sys/pool.h>
     59 #include <sys/proc.h>
     60 #include <sys/buf.h>
     61 #include <sys/vnode.h>
     62 #include <sys/kernel.h>		/* defines "time" */
     63 #include <sys/dirent.h>
     64 #include <sys/namei.h>
     65 #include <sys/kauth.h>
     66 
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <fs/msdosfs/bpb.h>
     70 #include <fs/msdosfs/msdosfsmount.h>
     71 #include <fs/msdosfs/direntry.h>
     72 #include <fs/msdosfs/denode.h>
     73 #include <fs/msdosfs/fat.h>
     74 
     75 LIST_HEAD(ihashhead, denode) *dehashtbl;
     76 u_long dehash;			/* size of hash table - 1 */
     77 #define	DEHASH(dev, dcl, doff) \
     78     (((dev) + (dcl) + (doff) / sizeof(struct direntry)) & dehash)
     79 
     80 kmutex_t msdosfs_ihash_lock;
     81 kmutex_t msdosfs_hashlock;
     82 
     83 struct pool msdosfs_denode_pool;
     84 
     85 extern int prtactive;
     86 
     87 static const struct genfs_ops msdosfs_genfsops = {
     88 	.gop_size = genfs_size,
     89 	.gop_alloc = msdosfs_gop_alloc,
     90 	.gop_write = genfs_gop_write,
     91 	.gop_markupdate = msdosfs_gop_markupdate,
     92 };
     93 
     94 static struct denode *msdosfs_hashget(dev_t, u_long, u_long, int);
     95 static void msdosfs_hashins(struct denode *);
     96 static void msdosfs_hashrem(struct denode *);
     97 
     98 MALLOC_DECLARE(M_MSDOSFSFAT);
     99 
    100 void
    101 msdosfs_init(void)
    102 {
    103 
    104 	malloc_type_attach(M_MSDOSFSMNT);
    105 	malloc_type_attach(M_MSDOSFSFAT);
    106 	malloc_type_attach(M_MSDOSFSTMP);
    107 	pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
    108 	    "msdosnopl", &pool_allocator_nointr, IPL_NONE);
    109 	dehashtbl = hashinit(desiredvnodes / 2, HASH_LIST, true, &dehash);
    110 	mutex_init(&msdosfs_ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    111 	mutex_init(&msdosfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    112 }
    113 
    114 /*
    115  * Reinitialize inode hash table.
    116  */
    117 
    118 void
    119 msdosfs_reinit(void)
    120 {
    121 	struct denode *dep;
    122 	struct ihashhead *oldhash, *hash;
    123 	u_long oldmask, mask, val;
    124 	int i;
    125 
    126 	hash = hashinit(desiredvnodes / 2, HASH_LIST, true, &mask);
    127 
    128 	mutex_enter(&msdosfs_ihash_lock);
    129 	oldhash = dehashtbl;
    130 	oldmask = dehash;
    131 	dehashtbl = hash;
    132 	dehash = mask;
    133 	for (i = 0; i <= oldmask; i++) {
    134 		while ((dep = LIST_FIRST(&oldhash[i])) != NULL) {
    135 			LIST_REMOVE(dep, de_hash);
    136 			val = DEHASH(dep->de_dev, dep->de_dirclust,
    137 			    dep->de_diroffset);
    138 			LIST_INSERT_HEAD(&hash[val], dep, de_hash);
    139 		}
    140 	}
    141 	mutex_exit(&msdosfs_ihash_lock);
    142 	hashdone(oldhash, HASH_LIST, oldmask);
    143 }
    144 
    145 void
    146 msdosfs_done(void)
    147 {
    148 	hashdone(dehashtbl, HASH_LIST, dehash);
    149 	pool_destroy(&msdosfs_denode_pool);
    150 	mutex_destroy(&msdosfs_ihash_lock);
    151 	mutex_destroy(&msdosfs_hashlock);
    152 	malloc_type_detach(M_MSDOSFSTMP);
    153 	malloc_type_detach(M_MSDOSFSFAT);
    154 	malloc_type_detach(M_MSDOSFSMNT);
    155 }
    156 
    157 static struct denode *
    158 msdosfs_hashget(dev_t dev, u_long dirclust, u_long diroff, int flags)
    159 {
    160 	struct denode *dep;
    161 	struct vnode *vp;
    162 
    163 loop:
    164 	mutex_enter(&msdosfs_ihash_lock);
    165 	LIST_FOREACH(dep, &dehashtbl[DEHASH(dev, dirclust, diroff)], de_hash) {
    166 		if (dirclust == dep->de_dirclust &&
    167 		    diroff == dep->de_diroffset &&
    168 		    dev == dep->de_dev &&
    169 		    dep->de_refcnt != 0) {
    170 			vp = DETOV(dep);
    171 			if (flags == 0) {
    172 				mutex_exit(&msdosfs_ihash_lock);
    173 			} else {
    174 				mutex_enter(&vp->v_interlock);
    175 				mutex_exit(&msdosfs_ihash_lock);
    176 				if (vget(vp, flags | LK_INTERLOCK))
    177 					goto loop;
    178 			}
    179 			return (dep);
    180 		}
    181 	}
    182 	mutex_exit(&msdosfs_ihash_lock);
    183 	return (NULL);
    184 }
    185 
    186 static void
    187 msdosfs_hashins(struct denode *dep)
    188 {
    189 	struct ihashhead *depp;
    190 	int val;
    191 
    192 	KASSERT(mutex_owned(&msdosfs_hashlock));
    193 
    194 	mutex_enter(&msdosfs_ihash_lock);
    195 	val = DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
    196 	depp = &dehashtbl[val];
    197 	LIST_INSERT_HEAD(depp, dep, de_hash);
    198 	mutex_exit(&msdosfs_ihash_lock);
    199 }
    200 
    201 static void
    202 msdosfs_hashrem(struct denode *dep)
    203 {
    204 	mutex_enter(&msdosfs_ihash_lock);
    205 	LIST_REMOVE(dep, de_hash);
    206 	mutex_exit(&msdosfs_ihash_lock);
    207 }
    208 
    209 /*
    210  * If deget() succeeds it returns with the gotten denode locked().
    211  *
    212  * pmp	     - address of msdosfsmount structure of the filesystem containing
    213  *	       the denode of interest.  The pm_dev field and the address of
    214  *	       the msdosfsmount structure are used.
    215  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
    216  *	       diroffset is relative to the beginning of the root directory,
    217  *	       otherwise it is cluster relative.
    218  * diroffset - offset past begin of cluster of denode we want
    219  * depp	     - returns the address of the gotten denode.
    220  */
    221 int
    222 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct denode **depp)
    223 	/* pmp:	 so we know the maj/min number */
    224 	/* dirclust:		 cluster this dir entry came from */
    225 	/* diroffset:		 index of entry within the cluster */
    226 	/* depp:		 returns the addr of the gotten denode */
    227 {
    228 	int error;
    229 	extern int (**msdosfs_vnodeop_p)(void *);
    230 	struct direntry *direntptr;
    231 	struct denode *ldep;
    232 	struct vnode *nvp;
    233 	struct buf *bp;
    234 
    235 #ifdef MSDOSFS_DEBUG
    236 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
    237 	    pmp, dirclust, diroffset, depp);
    238 #endif
    239 
    240 	/*
    241 	 * On FAT32 filesystems, root is a (more or less) normal
    242 	 * directory
    243 	 */
    244 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
    245 		dirclust = pmp->pm_rootdirblk;
    246 
    247 	/*
    248 	 * See if the denode is in the denode cache. Use the location of
    249 	 * the directory entry to compute the hash value. For subdir use
    250 	 * address of "." entry. For root dir (if not FAT32) use cluster
    251 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
    252 	 *
    253 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
    254 	 * examined does not represent an unlinked but still open file.
    255 	 * These files are not to be accessible even when the directory
    256 	 * entry that represented the file happens to be reused while the
    257 	 * deleted file is still open.
    258 	 */
    259  retry:
    260 	ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset, LK_EXCLUSIVE);
    261 	if (ldep) {
    262 		*depp = ldep;
    263 		return (0);
    264 	}
    265 
    266 	/*
    267 	 * Directory entry was not in cache, have to create a vnode and
    268 	 * copy it from the passed disk buffer.
    269 	 */
    270 	/* getnewvnode() does a VREF() on the vnode */
    271 	error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp,
    272 			    msdosfs_vnodeop_p, &nvp);
    273 	if (error) {
    274 		*depp = 0;
    275 		return (error);
    276 	}
    277 	ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
    278 
    279 	/*
    280 	 * If someone beat us to it, put back the freshly allocated
    281 	 * vnode/inode pair and retry.
    282 	 */
    283 	mutex_enter(&msdosfs_hashlock);
    284 	if (msdosfs_hashget(pmp->pm_dev, dirclust, diroffset, 0)) {
    285 		mutex_exit(&msdosfs_hashlock);
    286 		ungetnewvnode(nvp);
    287 		pool_put(&msdosfs_denode_pool, ldep);
    288 		goto retry;
    289 	}
    290 	memset(ldep, 0, sizeof *ldep);
    291 	nvp->v_data = ldep;
    292 	ldep->de_vnode = nvp;
    293 	ldep->de_flag = 0;
    294 	ldep->de_devvp = 0;
    295 	ldep->de_lockf = 0;
    296 	ldep->de_dev = pmp->pm_dev;
    297 	ldep->de_dirclust = dirclust;
    298 	ldep->de_diroffset = diroffset;
    299 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
    300 
    301 	/*
    302 	 * Insert the denode into the hash queue and lock the denode so it
    303 	 * can't be accessed until we've read it in and have done what we
    304 	 * need to it.
    305 	 */
    306 	vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
    307 	genfs_node_init(nvp, &msdosfs_genfsops);
    308 	msdosfs_hashins(ldep);
    309 	mutex_exit(&msdosfs_hashlock);
    310 
    311 	ldep->de_pmp = pmp;
    312 	ldep->de_devvp = pmp->pm_devvp;
    313 	ldep->de_refcnt = 1;
    314 	/*
    315 	 * Copy the directory entry into the denode area of the vnode.
    316 	 */
    317 	if ((dirclust == MSDOSFSROOT
    318 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
    319 	    && diroffset == MSDOSFSROOT_OFS) {
    320 		/*
    321 		 * Directory entry for the root directory. There isn't one,
    322 		 * so we manufacture one. We should probably rummage
    323 		 * through the root directory and find a label entry (if it
    324 		 * exists), and then use the time and date from that entry
    325 		 * as the time and date for the root denode.
    326 		 */
    327 		nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
    328 
    329 		ldep->de_Attributes = ATTR_DIRECTORY;
    330 		if (FAT32(pmp))
    331 			ldep->de_StartCluster = pmp->pm_rootdirblk;
    332 			/* de_FileSize will be filled in further down */
    333 		else {
    334 			ldep->de_StartCluster = MSDOSFSROOT;
    335 			ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
    336 		}
    337 		/*
    338 		 * fill in time and date so that dos2unixtime() doesn't
    339 		 * spit up when called from msdosfs_getattr() with root
    340 		 * denode
    341 		 */
    342 		ldep->de_CHun = 0;
    343 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
    344 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
    345 		    | (1 << DD_DAY_SHIFT);
    346 		/* Jan 1, 1980	 */
    347 		ldep->de_ADate = ldep->de_CDate;
    348 		ldep->de_MTime = ldep->de_CTime;
    349 		ldep->de_MDate = ldep->de_CDate;
    350 		/* leave the other fields as garbage */
    351 	} else {
    352 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
    353 		if (error) {
    354 			ldep->de_devvp = NULL;
    355 			ldep->de_Name[0] = SLOT_DELETED;
    356 			vput(nvp);
    357 			return (error);
    358 		}
    359 		DE_INTERNALIZE(ldep, direntptr);
    360 		brelse(bp, 0);
    361 	}
    362 
    363 	/*
    364 	 * Fill in a few fields of the vnode and finish filling in the
    365 	 * denode.  Then return the address of the found denode.
    366 	 */
    367 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
    368 		/*
    369 		 * Since DOS directory entries that describe directories
    370 		 * have 0 in the filesize field, we take this opportunity
    371 		 * to find out the length of the directory and plug it into
    372 		 * the denode structure.
    373 		 */
    374 		u_long size;
    375 
    376 		nvp->v_type = VDIR;
    377 		if (ldep->de_StartCluster != MSDOSFSROOT) {
    378 			error = pcbmap(ldep, CLUST_END, 0, &size, 0);
    379 			if (error == E2BIG) {
    380 				ldep->de_FileSize = de_cn2off(pmp, size);
    381 				error = 0;
    382 			} else
    383 				printf("deget(): pcbmap returned %d\n", error);
    384 		}
    385 	} else
    386 		nvp->v_type = VREG;
    387 	vref(ldep->de_devvp);
    388 	*depp = ldep;
    389 	uvm_vnp_setsize(nvp, ldep->de_FileSize);
    390 	return (0);
    391 }
    392 
    393 int
    394 deupdat(struct denode *dep, int waitfor)
    395 {
    396 
    397 	return (msdosfs_update(DETOV(dep), NULL, NULL,
    398 	    waitfor ? UPDATE_WAIT : 0));
    399 }
    400 
    401 /*
    402  * Truncate the file described by dep to the length specified by length.
    403  */
    404 int
    405 detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
    406 {
    407 	int error;
    408 	int allerror;
    409 	u_long eofentry;
    410 	u_long chaintofree = 0;
    411 	daddr_t bn, lastblock;
    412 	int boff;
    413 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
    414 	struct buf *bp;
    415 	struct msdosfsmount *pmp = dep->de_pmp;
    416 
    417 #ifdef MSDOSFS_DEBUG
    418 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
    419 #endif
    420 
    421 	/*
    422 	 * Disallow attempts to truncate the root directory since it is of
    423 	 * fixed size.  That's just the way dos filesystems are.  We use
    424 	 * the VROOT bit in the vnode because checking for the directory
    425 	 * bit and a startcluster of 0 in the denode is not adequate to
    426 	 * recognize the root directory at this point in a file or
    427 	 * directory's life.
    428 	 */
    429 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
    430 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
    431 		    dep->de_dirclust, dep->de_diroffset);
    432 		return (EINVAL);
    433 	}
    434 
    435 	uvm_vnp_setsize(DETOV(dep), length);
    436 
    437 	if (dep->de_FileSize < length)
    438 		return (deextend(dep, length, cred));
    439 	lastblock = de_clcount(pmp, length) - 1;
    440 
    441 	/*
    442 	 * If the desired length is 0 then remember the starting cluster of
    443 	 * the file and set the StartCluster field in the directory entry
    444 	 * to 0.  If the desired length is not zero, then get the number of
    445 	 * the last cluster in the shortened file.  Then get the number of
    446 	 * the first cluster in the part of the file that is to be freed.
    447 	 * Then set the next cluster pointer in the last cluster of the
    448 	 * file to CLUST_EOFE.
    449 	 */
    450 	if (length == 0) {
    451 		chaintofree = dep->de_StartCluster;
    452 		dep->de_StartCluster = 0;
    453 		eofentry = ~0;
    454 	} else {
    455 		error = pcbmap(dep, lastblock, 0, &eofentry, 0);
    456 		if (error) {
    457 #ifdef MSDOSFS_DEBUG
    458 			printf("detrunc(): pcbmap fails %d\n", error);
    459 #endif
    460 			return (error);
    461 		}
    462 	}
    463 
    464 	fc_purge(dep, lastblock + 1);
    465 
    466 	/*
    467 	 * If the new length is not a multiple of the cluster size then we
    468 	 * must zero the tail end of the new last cluster in case it
    469 	 * becomes part of the file again because of a seek.
    470 	 */
    471 	if ((boff = length & pmp->pm_crbomask) != 0) {
    472 		if (isadir) {
    473 			bn = cntobn(pmp, eofentry);
    474 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    475 			    pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
    476 			if (error) {
    477 				brelse(bp, 0);
    478 #ifdef MSDOSFS_DEBUG
    479 				printf("detrunc(): bread fails %d\n", error);
    480 #endif
    481 				return (error);
    482 			}
    483 			memset((char *)bp->b_data + boff, 0,
    484 			    pmp->pm_bpcluster - boff);
    485 			if (flags & IO_SYNC)
    486 				bwrite(bp);
    487 			else
    488 				bdwrite(bp);
    489 		} else {
    490 			uvm_vnp_zerorange(DETOV(dep), length,
    491 					  pmp->pm_bpcluster - boff);
    492 		}
    493 	}
    494 
    495 	/*
    496 	 * Write out the updated directory entry.  Even if the update fails
    497 	 * we free the trailing clusters.
    498 	 */
    499 	dep->de_FileSize = length;
    500 	if (!isadir)
    501 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
    502 	vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
    503 	allerror = deupdat(dep, 1);
    504 #ifdef MSDOSFS_DEBUG
    505 	printf("detrunc(): allerror %d, eofentry %lu\n",
    506 	       allerror, eofentry);
    507 #endif
    508 
    509 	/*
    510 	 * If we need to break the cluster chain for the file then do it
    511 	 * now.
    512 	 */
    513 	if (eofentry != ~0) {
    514 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
    515 				 &chaintofree, CLUST_EOFE);
    516 		if (error) {
    517 #ifdef MSDOSFS_DEBUG
    518 			printf("detrunc(): fatentry errors %d\n", error);
    519 #endif
    520 			return (error);
    521 		}
    522 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
    523 			    eofentry);
    524 	}
    525 
    526 	/*
    527 	 * Now free the clusters removed from the file because of the
    528 	 * truncation.
    529 	 */
    530 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
    531 		freeclusterchain(pmp, chaintofree);
    532 
    533 	return (allerror);
    534 }
    535 
    536 /*
    537  * Extend the file described by dep to length specified by length.
    538  */
    539 int
    540 deextend(struct denode *dep, u_long length, kauth_cred_t cred)
    541 {
    542 	struct msdosfsmount *pmp = dep->de_pmp;
    543 	u_long count, osize;
    544 	int error;
    545 
    546 	/*
    547 	 * The root of a DOS filesystem cannot be extended.
    548 	 */
    549 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
    550 		return (EINVAL);
    551 
    552 	/*
    553 	 * Directories cannot be extended.
    554 	 */
    555 	if (dep->de_Attributes & ATTR_DIRECTORY)
    556 		return (EISDIR);
    557 
    558 	if (length <= dep->de_FileSize)
    559 		panic("deextend: file too large");
    560 
    561 	/*
    562 	 * Compute the number of clusters to allocate.
    563 	 */
    564 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
    565 	if (count > 0) {
    566 		if (count > pmp->pm_freeclustercount)
    567 			return (ENOSPC);
    568 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
    569 		if (error) {
    570 			/* truncate the added clusters away again */
    571 			(void) detrunc(dep, dep->de_FileSize, 0, cred);
    572 			return (error);
    573 		}
    574 	}
    575 
    576 	/*
    577 	 * Zero extend file range; uvm_vnp_zerorange() uses ubc_alloc() and a
    578 	 * memset(); we set the write size so ubc won't read in file data that
    579 	 * is zero'd later.
    580 	 */
    581 	osize = dep->de_FileSize;
    582 	dep->de_FileSize = length;
    583 	uvm_vnp_setwritesize(DETOV(dep), (voff_t)dep->de_FileSize);
    584 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
    585 	uvm_vnp_zerorange(DETOV(dep), (off_t)osize,
    586 	    (size_t)(dep->de_FileSize - osize));
    587 	uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
    588 	return (deupdat(dep, 1));
    589 }
    590 
    591 /*
    592  * Move a denode to its correct hash queue after the file it represents has
    593  * been moved to a new directory.
    594  */
    595 void
    596 reinsert(struct denode *dep)
    597 {
    598 	/*
    599 	 * Fix up the denode cache.  If the denode is for a directory,
    600 	 * there is nothing to do since the hash is based on the starting
    601 	 * cluster of the directory file and that hasn't changed.  If for a
    602 	 * file the hash is based on the location of the directory entry,
    603 	 * so we must remove it from the cache and re-enter it with the
    604 	 * hash based on the new location of the directory entry.
    605 	 */
    606 	if (dep->de_Attributes & ATTR_DIRECTORY)
    607 		return;
    608 	mutex_enter(&msdosfs_hashlock);
    609 	msdosfs_hashrem(dep);
    610 	msdosfs_hashins(dep);
    611 	mutex_exit(&msdosfs_hashlock);
    612 }
    613 
    614 int
    615 msdosfs_reclaim(void *v)
    616 {
    617 	struct vop_reclaim_args /* {
    618 		struct vnode *a_vp;
    619 	} */ *ap = v;
    620 	struct vnode *vp = ap->a_vp;
    621 	struct denode *dep = VTODE(vp);
    622 
    623 #ifdef MSDOSFS_DEBUG
    624 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
    625 	    dep, dep->de_Name, dep->de_refcnt);
    626 #endif
    627 
    628 	if (prtactive && vp->v_usecount > 1)
    629 		vprint("msdosfs_reclaim(): pushing active", vp);
    630 	/*
    631 	 * Remove the denode from its hash chain.
    632 	 */
    633 	msdosfs_hashrem(dep);
    634 	/*
    635 	 * Purge old data structures associated with the denode.
    636 	 */
    637 	cache_purge(vp);
    638 	if (dep->de_devvp) {
    639 		vrele(dep->de_devvp);
    640 		dep->de_devvp = 0;
    641 	}
    642 #if 0 /* XXX */
    643 	dep->de_flag = 0;
    644 #endif
    645 	genfs_node_destroy(vp);
    646 	pool_put(&msdosfs_denode_pool, dep);
    647 	vp->v_data = NULL;
    648 	return (0);
    649 }
    650 
    651 int
    652 msdosfs_inactive(void *v)
    653 {
    654 	struct vop_inactive_args /* {
    655 		struct vnode *a_vp;
    656 		bool *a_recycle;
    657 	} */ *ap = v;
    658 	struct vnode *vp = ap->a_vp;
    659 	struct mount *mp = vp->v_mount;
    660 	struct denode *dep = VTODE(vp);
    661 	int error = 0;
    662 
    663 #ifdef MSDOSFS_DEBUG
    664 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
    665 #endif
    666 
    667 	fstrans_start(mp, FSTRANS_LAZY);
    668 	/*
    669 	 * Get rid of denodes related to stale file handles.
    670 	 */
    671 	if (dep->de_Name[0] == SLOT_DELETED)
    672 		goto out;
    673 
    674 	/*
    675 	 * If the file has been deleted and it is on a read/write
    676 	 * filesystem, then truncate the file, and mark the directory slot
    677 	 * as empty.  (This may not be necessary for the dos filesystem.)
    678 	 */
    679 #ifdef MSDOSFS_DEBUG
    680 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
    681 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag,
    682 		(vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
    683 #endif
    684 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
    685 		if (dep->de_FileSize != 0) {
    686 			error = detrunc(dep, (u_long)0, 0, NOCRED);
    687 		}
    688 		dep->de_Name[0] = SLOT_DELETED;
    689 	}
    690 	deupdat(dep, 0);
    691 out:
    692 	/*
    693 	 * If we are done with the denode, reclaim it
    694 	 * so that it can be reused immediately.
    695 	 */
    696 #ifdef MSDOSFS_DEBUG
    697 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
    698 		vp->v_usecount, dep->de_Name[0]);
    699 #endif
    700 	*ap->a_recycle = (dep->de_Name[0] == SLOT_DELETED);
    701 	VOP_UNLOCK(vp);
    702 	fstrans_done(mp);
    703 	return (error);
    704 }
    705 
    706 int
    707 msdosfs_gop_alloc(struct vnode *vp, off_t off,
    708     off_t len, int flags, kauth_cred_t cred)
    709 {
    710 	return 0;
    711 }
    712 
    713 void
    714 msdosfs_gop_markupdate(struct vnode *vp, int flags)
    715 {
    716 	u_long mask = 0;
    717 
    718 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
    719 		mask = DE_ACCESS;
    720 	}
    721 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
    722 		mask |= DE_UPDATE;
    723 	}
    724 	if (mask) {
    725 		struct denode *dep = VTODE(vp);
    726 
    727 		dep->de_flag |= mask;
    728 	}
    729 }
    730