xref: /dflybsd-src/sys/vfs/msdosfs/msdosfs_denode.c (revision d9bb5dfdc3905237fb704584b71c9e0a9627656c)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/buf.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/proc.h>
59 #include <sys/mount.h>
60 #include <sys/vnode.h>
61 #include <sys/time.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_page2.h>
66 
67 #include <sys/buf2.h>
68 
69 #include "bpb.h"
70 #include "msdosfsmount.h"
71 #include "direntry.h"
72 #include "denode.h"
73 #include "fat.h"
74 
75 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
76 
77 /*
78  * Hash table caching denode instances.
79  *
80  * denodes are keyed by the disk location (cluster num, entry offset) of the
81  * directory entry of the file they represent.
82  *
83  * denodes representing deleted but still opened files are left in this cache
84  * until reclaimed.  Deleted directory entries can be reused when files are
85  * renamed or new files created.  As a consequence, several denodes associated
86  * with the same entry may coexist in this cache as long as a single one of
87  * them map to an existing file (de_refcnt > 0).
88  *
89  * R/w access to this cache is protected by dehash_token.
90  */
91 static struct denode **dehashtbl;
92 static u_long dehash;			/* size of hash table - 1 */
93 static struct lwkt_token dehash_token;
94 
95 #define	DEHASH(dev, dcl, doff)	(dehashtbl[(minor(dev) + (dcl) + (doff) / \
96 				sizeof(struct direntry)) & dehash])
97 
98 /*ARGSUSED*/
99 int
100 msdosfs_init(struct vfsconf *vfsp)
101 {
102 	dehash = vfs_inodehashsize();
103 	dehashtbl = kmalloc(sizeof(void *) * dehash,
104 			    M_MSDOSFSMNT, M_WAITOK|M_ZERO);
105 	--dehash;
106 	lwkt_token_init(&dehash_token, "msdosihash");
107 
108 	return (0);
109 }
110 
111 int
112 msdosfs_uninit(struct vfsconf *vfsp)
113 {
114 	if (dehashtbl)
115 		kfree(dehashtbl, M_MSDOSFSMNT);
116 	return (0);
117 }
118 
119 static struct denode *
120 msdosfs_hashget(cdev_t dev, u_long dirclust, u_long diroff)
121 {
122 	struct denode *dep;
123 	struct vnode *vp;
124 
125 	lwkt_gettoken(&dehash_token);
126 loop:
127 	for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
128 		if (dirclust != dep->de_dirclust
129 		    || diroff != dep->de_diroffset
130 		    || dev != dep->de_dev
131 		    || dep->de_refcnt <= 0) {
132 			continue;
133 		}
134 		vp = DETOV(dep);
135 		if (vget(vp, LK_EXCLUSIVE))
136 			goto loop;
137 
138 		/*
139 		 * We must check to see if the inode has been ripped
140 		 * out from under us after blocking.
141 		 */
142 		for (dep = DEHASH(dev, dirclust, diroff); dep;
143 		     dep = dep->de_next) {
144 			if (dirclust == dep->de_dirclust
145 			    && diroff == dep->de_diroffset
146 			    && dev == dep->de_dev
147 			    && dep->de_refcnt > 0) {
148 				break;
149 			}
150 		}
151 		if (dep == NULL || DETOV(dep) != vp) {
152 			vput(vp);
153 			goto loop;
154 		}
155 		lwkt_reltoken(&dehash_token);
156 		return (dep);
157 	}
158 	lwkt_reltoken(&dehash_token);
159 	return (NULL);
160 }
161 
162 /*
163  * Try to insert specified denode into the hash table.  Return 0 on success
164  * and EBUSY if there is already a denode with the same key.
165  */
166 static
167 int
168 msdosfs_hashins(struct denode *dep)
169 {
170 	struct denode **depp, *deq;
171 
172 	lwkt_gettoken(&dehash_token);
173 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
174 	while ((deq = *depp) != NULL) {
175 		if (deq->de_dev == dep->de_dev &&
176 		    deq->de_dirclust == dep->de_dirclust &&
177 		    deq->de_diroffset == dep->de_diroffset &&
178 		    deq->de_refcnt > 0) {
179 			lwkt_reltoken(&dehash_token);
180 			return(EBUSY);
181 		}
182 		depp = &deq->de_next;
183 	}
184 	dep->de_next = NULL;
185 	*depp = dep;
186 	lwkt_reltoken(&dehash_token);
187 	return(0);
188 }
189 
190 static
191 void
192 msdosfs_hashrem(struct denode *dep)
193 {
194 	struct denode **depp, *deq;
195 
196 	lwkt_gettoken(&dehash_token);
197 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
198 	while ((deq = *depp) != NULL) {
199 		if (dep == deq)
200 			break;
201 		depp = &deq->de_next;
202 	}
203 	KKASSERT(dep == deq);
204 	*depp = dep->de_next;
205 	dep->de_next = NULL;
206 	lwkt_reltoken(&dehash_token);
207 }
208 
209 void
210 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
211 {
212 	int error;
213 
214 	lwkt_gettoken(&dehash_token);
215 	msdosfs_hashrem(ip);
216 	ip->de_dirclust = new_dirclust;
217 	ip->de_diroffset = new_diroffset;
218 	error = msdosfs_hashins(ip);
219 	KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error));
220 	lwkt_reltoken(&dehash_token);
221 }
222 
223 /*
224  * If deget() succeeds it returns with the gotten denode locked().
225  *
226  * pmp	     - address of msdosfsmount structure of the filesystem containing
227  *	       the denode of interest.  The address of
228  *	       the msdosfsmount structure are used.
229  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
230  *	       diroffset is relative to the beginning of the root directory,
231  *	       otherwise it is cluster relative.
232  * diroffset - offset past begin of cluster of denode we want
233  * depp	     - returns the address of the gotten denode.
234  */
235 int
236 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
237     struct denode **depp)
238 {
239 	int error;
240 	cdev_t dev = pmp->pm_dev;
241 	struct mount *mntp = pmp->pm_mountp;
242 	struct direntry *direntptr;
243 	struct denode *ldep;
244 	struct vnode *nvp;
245 	struct buf *bp;
246 
247 	mprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
248 	    pmp, dirclust, diroffset, depp);
249 
250 	/*
251 	 * On FAT32 filesystems, root is a (more or less) normal
252 	 * directory
253 	 */
254 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
255 		dirclust = pmp->pm_rootdirblk;
256 
257 again:
258 	/*
259 	 * See if the denode is in the denode cache. Use the location of
260 	 * the directory entry to compute the hash value. For subdir use
261 	 * address of "." entry. For root dir (if not FAT32) use cluster
262 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
263 	 *
264 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
265 	 * examined does not represent an unlinked but still open file.
266 	 * These files are not to be accessible even when the directory
267 	 * entry that represented the file happens to be reused while the
268 	 * deleted file is still open.
269 	 */
270 	ldep = msdosfs_hashget(dev, dirclust, diroffset);
271 	if (ldep) {
272 		*depp = ldep;
273 		return (0);
274 	}
275 
276 	/*
277 	 * Do the MALLOC before the getnewvnode since doing so afterward
278 	 * might cause a bogus v_data pointer to get dereferenced
279 	 * elsewhere if MALLOC should block.
280 	 */
281 	ldep = kmalloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
282 
283 	/*
284 	 * Directory entry was not in cache, have to create a vnode and
285 	 * copy it from the passed disk buffer.
286 	 */
287 
288 	/* getnewvnode() does a vref() on the vnode */
289 	error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0);
290 	if (error) {
291 		*depp = NULL;
292 		kfree(ldep, M_MSDOSFSNODE);
293 		return error;
294 	}
295 
296 	ldep->de_vnode = nvp;
297 	ldep->de_flag = 0;
298 	ldep->de_devvp = 0;
299 	ldep->de_dev = dev;
300 	ldep->de_dirclust = dirclust;
301 	ldep->de_diroffset = diroffset;
302 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
303 
304 	/*
305 	 * Insert the denode into the hash queue.  If a collision occurs
306 	 * throw away the vnode and try again.
307 	 */
308 	error = msdosfs_hashins(ldep);
309 	if (error == EBUSY) {
310 		nvp->v_type = VBAD;
311 		vx_put(nvp);
312 		kfree(ldep, M_MSDOSFSNODE);
313 		goto again;
314 	} else if (error) {
315 		nvp->v_type = VBAD;
316 		vx_put(nvp);
317 		kfree(ldep, M_MSDOSFSNODE);
318 		*depp = NULL;
319 		return (EINVAL);
320 	}
321 	nvp->v_data = ldep;
322 	ldep->de_pmp = pmp;
323 	ldep->de_refcnt = 1;
324 	/*
325 	 * Copy the directory entry into the denode area of the vnode.
326 	 */
327 	if ((dirclust == MSDOSFSROOT
328 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
329 	    && diroffset == MSDOSFSROOT_OFS) {
330 		/*
331 		 * Directory entry for the root directory. There isn't one,
332 		 * so we manufacture one. We should probably rummage
333 		 * through the root directory and find a label entry (if it
334 		 * exists), and then use the time and date from that entry
335 		 * as the time and date for the root denode.
336 		 */
337 		vsetflags(nvp, VROOT); /* should be further down XXX */
338 
339 		ldep->de_Attributes = ATTR_DIRECTORY;
340 		ldep->de_LowerCase = 0;
341 		if (FAT32(pmp)) {
342 			ldep->de_StartCluster = pmp->pm_rootdirblk;
343 			/* de_FileSize will be filled in further down */
344 		} else {
345 			ldep->de_StartCluster = MSDOSFSROOT;
346 			ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
347 		}
348 		/*
349 		 * fill in time and date so that fattime2timespec() doesn't
350 		 * spit up when called from msdosfs_getattr() with root
351 		 * denode
352 		 */
353 		ldep->de_CHun = 0;
354 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
355 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
356 		    | (1 << DD_DAY_SHIFT);
357 		/* Jan 1, 1980	 */
358 		ldep->de_ADate = ldep->de_CDate;
359 		ldep->de_MTime = ldep->de_CTime;
360 		ldep->de_MDate = ldep->de_CDate;
361 		/* leave the other fields as garbage */
362 	} else {
363 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
364 		if (error) {
365 			/*
366 			 * The denode does not contain anything useful, so
367 			 * it would be wrong to leave it on its hash chain.
368 			 * Arrange for vput() to just forget about it.
369 			 */
370 			ldep->de_Name[0] = SLOT_DELETED;
371 			nvp->v_type = VBAD;
372 
373 			vx_put(nvp);
374 			*depp = NULL;
375 			return (error);
376 		}
377 		DE_INTERNALIZE(ldep, direntptr);
378 		brelse(bp);
379 	}
380 
381 	/*
382 	 * Fill in a few fields of the vnode and finish filling in the
383 	 * denode.  Then return the address of the found denode.
384 	 */
385 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
386 		/*
387 		 * Since DOS directory entries that describe directories
388 		 * have 0 in the filesize field, we take this opportunity
389 		 * to find out the length of the directory and plug it into
390 		 * the denode structure.
391 		 */
392 		u_long size;
393 
394 		/*
395 		 * XXX it sometimes happens that the "." entry has cluster
396 		 * number 0 when it shouldn't.  Use the actual cluster number
397 		 * instead of what is written in directory entry.
398 		 */
399 		if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
400 			kprintf("deget(): \".\" entry at clust %lu != %lu\n",
401 			    dirclust, ldep->de_StartCluster);
402 			ldep->de_StartCluster = dirclust;
403 		}
404 
405 		nvp->v_type = VDIR;
406 		if (ldep->de_StartCluster != MSDOSFSROOT) {
407 			error = pcbmap(ldep, 0xffff, NULL, &size, NULL);
408 			if (error == E2BIG) {
409 				ldep->de_FileSize = de_cn2off(pmp, size);
410 				error = 0;
411 			} else
412 				kprintf("deget(): pcbmap returned %d\n", error);
413 		}
414 	} else {
415 		nvp->v_type = VREG;
416 	}
417 
418 	ldep->de_modrev = init_va_filerev();
419 	ldep->de_devvp = pmp->pm_devvp;
420 	vref(ldep->de_devvp);
421 	vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1);
422 	/*
423 	 * Leave nvp locked and refd so the returned inode is effectively
424 	 * locked and refd.
425 	 */
426 	*depp = ldep;
427 	return (0);
428 }
429 
430 int
431 deupdat(struct denode *dep, int waitfor)
432 {
433 	struct direntry dir;
434 	struct timespec ts;
435 	struct buf *bp;
436 	struct direntry *dirp;
437 	int error;
438 
439 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) {
440 		dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS |
441 		    DE_MODIFIED);
442 		return (0);
443 	}
444 	vfs_timestamp(&ts);
445 	DETIMES(dep, &ts, &ts, &ts);
446 	if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0)
447 		return (0);
448 	dep->de_flag &= ~DE_MODIFIED;
449 	if (dep->de_Attributes & ATTR_DIRECTORY)
450 		return (0);
451 	if (dep->de_refcnt <= 0)
452 		return (0);
453 	error = readde(dep, &bp, &dirp);
454 	if (error)
455 		return (error);
456 	DE_EXTERNALIZE(&dir, dep);
457 	if (bcmp(dirp, &dir, sizeof(dir)) == 0) {
458 		if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) {
459 			brelse(bp);
460 			return (0);
461 		}
462 	} else
463 		*dirp = dir;
464 	if (waitfor)
465 		error = bwrite(bp);
466 	else if (vm_page_count_severe() || buf_dirty_count_severe())
467 		bawrite(bp);
468 	else
469 		bdwrite(bp);
470 	return (error);
471 }
472 
473 /*
474  * Truncate the file described by dep to the length specified by length.
475  */
476 int
477 detrunc(struct denode *dep, u_long length, int flags)
478 {
479 	int error;
480 	int allerror;
481 	u_long eofentry;
482 	u_long chaintofree;
483 	daddr_t bn;
484 	int boff;
485 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
486 	struct buf *bp;
487 	struct msdosfsmount *pmp = dep->de_pmp;
488 
489 	mprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name,
490 		length, flags);
491 
492 	/*
493 	 * Disallow attempts to truncate the root directory since it is of
494 	 * fixed size.  That's just the way dos filesystems are.  We use
495 	 * the VROOT bit in the vnode because checking for the directory
496 	 * bit and a startcluster of 0 in the denode is not adequate to
497 	 * recognize the root directory at this point in a file or
498 	 * directory's life.
499 	 */
500 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
501 		kprintf("detrunc(): can't truncate root directory, clust %ld, "
502 			"offset %ld\n",
503 			dep->de_dirclust, dep->de_diroffset);
504 		return (EINVAL);
505 	}
506 
507 	if (dep->de_FileSize < length) {
508 		vnode_pager_setsize(DETOV(dep), length);
509 		return deextend(dep, length);
510 	}
511 
512 	/*
513 	 * If the desired length is 0 then remember the starting cluster of
514 	 * the file and set the StartCluster field in the directory entry
515 	 * to 0.  If the desired length is not zero, then get the number of
516 	 * the last cluster in the shortened file.  Then get the number of
517 	 * the first cluster in the part of the file that is to be freed.
518 	 * Then set the next cluster pointer in the last cluster of the
519 	 * file to CLUST_EOFE.
520 	 */
521 	if (length == 0) {
522 		chaintofree = dep->de_StartCluster;
523 		dep->de_StartCluster = 0;
524 		eofentry = ~0;
525 	} else {
526 		error = pcbmap(dep, de_clcount(pmp, length) - 1,
527 			       NULL, &eofentry, NULL);
528 		if (error) {
529 			mprintf("detrunc(): pcbmap fails %d\n", error);
530 			return (error);
531 		}
532 	}
533 
534 	fc_purge(dep, de_clcount(pmp, length));
535 
536 	/*
537 	 * If the new length is not a multiple of the cluster size then we
538 	 * must zero the tail end of the new last cluster in case it
539 	 * becomes part of the file again because of a seek.
540 	 */
541 	if ((boff = length & pmp->pm_crbomask) != 0) {
542 		if (isadir) {
543 			bn = cntobn(pmp, eofentry);
544 			error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn),
545 				      pmp->pm_bpcluster, &bp);
546 		} else {
547 			u_long cn = de_cluster(pmp, length);
548 			error = bread(DETOV(dep), de_cn2doff(pmp, cn),
549 				      pmp->pm_bpcluster, &bp);
550 		}
551 		if (error) {
552 			brelse(bp);
553 			mprintf("detrunc(): bread fails %d\n", error);
554 			return (error);
555 		}
556 		memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
557 		if ((flags & IO_SYNC) != 0)
558 			bwrite(bp);
559 		else
560 			bdwrite(bp);
561 	}
562 
563 	/*
564 	 * Write out the updated directory entry.  Even if the update fails
565 	 * we free the trailing clusters.
566 	 */
567 	dep->de_FileSize = length;
568 	if (!isadir)
569 		dep->de_flag |= DE_UPDATE | DE_MODIFIED;
570 	allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
571 #ifdef MSDOSFS_DEBUG
572 	if (allerror)
573 		kprintf("detrunc(): vtruncbuf error %d\n", allerror);
574 #endif
575 	error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
576 	if (error != 0 && allerror == 0)
577 		allerror = error;
578 	mprintf("detrunc(): allerror %d, eofentry %lu\n",
579 		allerror, eofentry);
580 
581 	/*
582 	 * If we need to break the cluster chain for the file then do it
583 	 * now.
584 	 */
585 	if (eofentry != ~0) {
586 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
587 				 &chaintofree, CLUST_EOFE);
588 		if (error) {
589 			mprintf("detrunc(): fatentry errors %d\n", error);
590 			return (error);
591 		}
592 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
593 			    eofentry);
594 	}
595 
596 	/*
597 	 * Now free the clusters removed from the file because of the
598 	 * truncation.
599 	 */
600 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
601 		freeclusterchain(pmp, chaintofree);
602 
603 	return (allerror);
604 }
605 
606 /*
607  * Extend the file described by dep to length specified by length.
608  */
609 int
610 deextend(struct denode *dep, u_long length)
611 {
612 	struct msdosfsmount *pmp = dep->de_pmp;
613 	u_long count;
614 	int error;
615 
616 	/*
617 	 * The root of a DOS filesystem cannot be extended.
618 	 */
619 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
620 		return (EINVAL);
621 
622 	/*
623 	 * Directories cannot be extended.
624 	 */
625 	if (dep->de_Attributes & ATTR_DIRECTORY)
626 		return (EISDIR);
627 
628 	if (length <= dep->de_FileSize)
629 		panic("deextend: file too large");
630 
631 	/*
632 	 * Compute the number of clusters to allocate.
633 	 */
634 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
635 	if (count > 0) {
636 		if (count > pmp->pm_freeclustercount)
637 			return (ENOSPC);
638 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
639 		if (error) {
640 			/* truncate the added clusters away again */
641 			detrunc(dep, dep->de_FileSize, 0);
642 			return (error);
643 		}
644 	}
645 	dep->de_FileSize = length;
646 	dep->de_flag |= DE_UPDATE | DE_MODIFIED;
647 	return (deupdat(dep, !DOINGASYNC(DETOV(dep))));
648 }
649 
650 int
651 msdosfs_reclaim(struct vop_reclaim_args *ap)
652 {
653 	struct vnode *vp = ap->a_vp;
654 	struct denode *dep = VTODE(vp);
655 
656 	mprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
657 	    dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1);
658 
659 	if (prtactive && VREFCNT(vp) > 1)
660 		vprint("msdosfs_reclaim(): pushing active", vp);
661 	/*
662 	 * Remove the denode from its hash chain.
663 	 */
664 	vp->v_data = NULL;
665 	if (dep) {
666 		msdosfs_hashrem(dep);
667 		if (dep->de_devvp) {
668 			vrele(dep->de_devvp);
669 			dep->de_devvp = 0;
670 		}
671 		kfree(dep, M_MSDOSFSNODE);
672 	}
673 	return (0);
674 }
675 
676 int
677 msdosfs_inactive(struct vop_inactive_args *ap)
678 {
679 	struct vnode *vp = ap->a_vp;
680 	struct denode *dep = VTODE(vp);
681 	int error = 0;
682 
683 	mprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n",
684 		dep, (dep ? dep->de_Name[0] : 0));
685 
686 	if (prtactive && VREFCNT(vp) > 1)
687 		vprint("msdosfs_inactive(): pushing active", vp);
688 
689 	/*
690 	 * Ignore denodes related to stale file handles.
691 	 */
692 	if (dep == NULL || dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
693 		goto out;
694 
695 	/*
696 	 * If the file has been deleted and it is on a read/write
697 	 * filesystem, then truncate the file, and mark the directory slot
698 	 * as empty.  (This may not be necessary for the dos filesystem.)
699 	 */
700 	mprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n",
701 	    dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag,
702 	    (unsigned long long)MNT_RDONLY);
703 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
704 		error = detrunc(dep, (u_long) 0, 0);
705 		dep->de_flag |= DE_UPDATE;
706 		dep->de_Name[0] = SLOT_DELETED;
707 	}
708 	deupdat(dep, 0);
709 
710 out:
711 	/*
712 	 * If we are done with the denode, reclaim it
713 	 * so that it can be reused immediately.
714 	 */
715 	mprintf("msdosfs_inactive(): v_refcnt 0x%08x, de_Name[0] %x\n",
716 		vp->v_refcnt, (dep ? dep->de_Name[0] : 0));
717 	if (dep == NULL || dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
718 		vrecycle(vp);
719 	return (error);
720 }
721