xref: /dragonfly/sys/vfs/msdosfs/msdosfs_denode.c (revision 3d33658b)
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 
66 #include <sys/buf2.h>
67 
68 #include "bpb.h"
69 #include "msdosfsmount.h"
70 #include "direntry.h"
71 #include "denode.h"
72 #include "fat.h"
73 
74 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
75 
76 /*
77  * Hash table caching denode instances.
78  *
79  * denodes are keyed by the disk location (cluster num, entry offset) of the
80  * directory entry of the file they represent.
81  *
82  * denodes representing deleted but still opened files are left in this cache
83  * until reclaimed.  Deleted directory entries can be reused when files are
84  * renamed or new files created.  As a consequence, several denodes associated
85  * with the same entry may coexist in this cache as long as a single one of
86  * them map to an existing file (de_refcnt > 0).
87  *
88  * R/w access to this cache is protected by dehash_token.
89  */
90 static struct denode **dehashtbl;
91 static u_long dehash;			/* size of hash table - 1 */
92 static struct lwkt_token dehash_token;
93 
94 #define	DEHASH(dev, dcl, doff)	(dehashtbl[(minor(dev) + (dcl) + (doff) / \
95 				sizeof(struct direntry)) & dehash])
96 
97 union _qcvt {
98 	quad_t qcvt;
99 	long val[2];
100 };
101 #define SETHIGH(q, h) { \
102 	union _qcvt tmp; \
103 	tmp.qcvt = (q); \
104 	tmp.val[_QUAD_HIGHWORD] = (h); \
105 	(q) = tmp.qcvt; \
106 }
107 #define SETLOW(q, l) { \
108 	union _qcvt tmp; \
109 	tmp.qcvt = (q); \
110 	tmp.val[_QUAD_LOWWORD] = (l); \
111 	(q) = tmp.qcvt; \
112 }
113 
114 /*ARGSUSED*/
115 int
116 msdosfs_init(struct vfsconf *vfsp)
117 {
118 	dehash = vfs_inodehashsize();
119 	dehashtbl = kmalloc(sizeof(void *) * dehash,
120 			    M_MSDOSFSMNT, M_WAITOK|M_ZERO);
121 	--dehash;
122 	lwkt_token_init(&dehash_token, "msdosihash");
123 
124 	return (0);
125 }
126 
127 int
128 msdosfs_uninit(struct vfsconf *vfsp)
129 {
130 	if (dehashtbl)
131 		kfree(dehashtbl, M_MSDOSFSMNT);
132 	return (0);
133 }
134 
135 static struct denode *
136 msdosfs_hashget(cdev_t dev, u_long dirclust, u_long diroff)
137 {
138 	struct denode *dep;
139 	struct vnode *vp;
140 
141 	lwkt_gettoken(&dehash_token);
142 loop:
143 	for (dep = DEHASH(dev, dirclust, diroff); dep; 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 			continue;
149 		}
150 		vp = DETOV(dep);
151 		if (vget(vp, LK_EXCLUSIVE))
152 			goto loop;
153 
154 		/*
155 		 * We must check to see if the inode has been ripped
156 		 * out from under us after blocking.
157 		 */
158 		for (dep = DEHASH(dev, dirclust, diroff); dep;
159 		     dep = dep->de_next) {
160 			if (dirclust == dep->de_dirclust
161 			    && diroff == dep->de_diroffset
162 			    && dev == dep->de_dev
163 			    && dep->de_refcnt > 0) {
164 				break;
165 			}
166 		}
167 		if (dep == NULL || DETOV(dep) != vp) {
168 			vput(vp);
169 			goto loop;
170 		}
171 		lwkt_reltoken(&dehash_token);
172 		return (dep);
173 	}
174 	lwkt_reltoken(&dehash_token);
175 	return (NULL);
176 }
177 
178 /*
179  * Try to insert specified denode into the hash table.  Return 0 on success
180  * and EBUSY if there is already a denode with the same key.
181  */
182 static
183 int
184 msdosfs_hashins(struct denode *dep)
185 {
186 	struct denode **depp, *deq;
187 
188 	lwkt_gettoken(&dehash_token);
189 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
190 	while ((deq = *depp) != NULL) {
191 		if (deq->de_dev == dep->de_dev &&
192 		    deq->de_dirclust == dep->de_dirclust &&
193 		    deq->de_diroffset == dep->de_diroffset &&
194 		    deq->de_refcnt > 0) {
195 			lwkt_reltoken(&dehash_token);
196 			return(EBUSY);
197 		}
198 		depp = &deq->de_next;
199 	}
200 	dep->de_next = NULL;
201 	*depp = dep;
202 	lwkt_reltoken(&dehash_token);
203 	return(0);
204 }
205 
206 static
207 void
208 msdosfs_hashrem(struct denode *dep)
209 {
210 	struct denode **depp, *deq;
211 
212 	lwkt_gettoken(&dehash_token);
213 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
214 	while ((deq = *depp) != NULL) {
215 		if (dep == deq)
216 			break;
217 		depp = &deq->de_next;
218 	}
219 	KKASSERT(dep == deq);
220 	*depp = dep->de_next;
221 	dep->de_next = NULL;
222 	lwkt_reltoken(&dehash_token);
223 }
224 
225 void
226 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
227 {
228 	int error;
229 
230 	lwkt_gettoken(&dehash_token);
231 	msdosfs_hashrem(ip);
232 	ip->de_dirclust = new_dirclust;
233 	ip->de_diroffset = new_diroffset;
234 	error = msdosfs_hashins(ip);
235 	KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error));
236 	lwkt_reltoken(&dehash_token);
237 }
238 
239 /*
240  * If deget() succeeds it returns with the gotten denode locked().
241  *
242  * pmp	     - address of msdosfsmount structure of the filesystem containing
243  *	       the denode of interest.  The pm_dev field and the address of
244  *	       the msdosfsmount structure are used.
245  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
246  *	       diroffset is relative to the beginning of the root directory,
247  *	       otherwise it is cluster relative.
248  * diroffset - offset past begin of cluster of denode we want
249  * depp	     - returns the address of the gotten denode.
250  */
251 int
252 deget(struct msdosfsmount *pmp,	/* so we know the maj/min number */
253       u_long dirclust,		/* cluster this dir entry came from */
254       u_long diroffset,		/* index of entry within the cluster */
255       struct denode **depp)	/* returns the addr of the gotten denode */
256 {
257 	int error;
258 	cdev_t dev = pmp->pm_dev;
259 	struct mount *mntp = pmp->pm_mountp;
260 	struct direntry *direntptr;
261 	struct denode *ldep;
262 	struct vnode *nvp;
263 	struct buf *bp;
264 	struct timeval tv;
265 
266 	mprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
267 	    pmp, dirclust, diroffset, depp);
268 
269 	/*
270 	 * On FAT32 filesystems, root is a (more or less) normal
271 	 * directory
272 	 */
273 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
274 		dirclust = pmp->pm_rootdirblk;
275 
276 again:
277 	/*
278 	 * See if the denode is in the denode cache. Use the location of
279 	 * the directory entry to compute the hash value. For subdir use
280 	 * address of "." entry. For root dir (if not FAT32) use cluster
281 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
282 	 *
283 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
284 	 * examined does not represent an unlinked but still open file.
285 	 * These files are not to be accessible even when the directory
286 	 * entry that represented the file happens to be reused while the
287 	 * deleted file is still open.
288 	 */
289 	ldep = msdosfs_hashget(dev, dirclust, diroffset);
290 	if (ldep) {
291 		*depp = ldep;
292 		return (0);
293 	}
294 
295 	/*
296 	 * Do the MALLOC before the getnewvnode since doing so afterward
297 	 * might cause a bogus v_data pointer to get dereferenced
298 	 * elsewhere if MALLOC should block.
299 	 */
300 	ldep = kmalloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
301 
302 	/*
303 	 * Directory entry was not in cache, have to create a vnode and
304 	 * copy it from the passed disk buffer.
305 	 */
306 
307 	/* getnewvnode() does a vref() on the vnode */
308 	error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0);
309 	if (error) {
310 		*depp = NULL;
311 		kfree(ldep, M_MSDOSFSNODE);
312 		return error;
313 	}
314 
315 	ldep->de_vnode = nvp;
316 	ldep->de_flag = 0;
317 	ldep->de_devvp = 0;
318 	ldep->de_dev = dev;
319 	ldep->de_dirclust = dirclust;
320 	ldep->de_diroffset = diroffset;
321 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
322 
323 	/*
324 	 * Insert the denode into the hash queue.  If a collision occurs
325 	 * throw away the vnode and try again.
326 	 */
327 	error = msdosfs_hashins(ldep);
328 	if (error == EBUSY) {
329 		nvp->v_type = VBAD;
330 		vx_put(nvp);
331 		kfree(ldep, M_MSDOSFSNODE);
332 		goto again;
333 	} else if (error) {
334 		nvp->v_type = VBAD;
335 		vx_put(nvp);
336 		kfree(ldep, M_MSDOSFSNODE);
337 		*depp = NULL;
338 		return (EINVAL);
339 	}
340 	nvp->v_data = ldep;
341 	ldep->de_pmp = pmp;
342 	ldep->de_refcnt = 1;
343 	/*
344 	 * Copy the directory entry into the denode area of the vnode.
345 	 */
346 	if ((dirclust == MSDOSFSROOT
347 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
348 	    && diroffset == MSDOSFSROOT_OFS) {
349 		/*
350 		 * Directory entry for the root directory. There isn't one,
351 		 * so we manufacture one. We should probably rummage
352 		 * through the root directory and find a label entry (if it
353 		 * exists), and then use the time and date from that entry
354 		 * as the time and date for the root denode.
355 		 */
356 		vsetflags(nvp, VROOT); /* should be further down XXX */
357 
358 		ldep->de_Attributes = ATTR_DIRECTORY;
359 		ldep->de_LowerCase = 0;
360 		if (FAT32(pmp)) {
361 			ldep->de_StartCluster = pmp->pm_rootdirblk;
362 			/* de_FileSize will be filled in further down */
363 		} else {
364 			ldep->de_StartCluster = MSDOSFSROOT;
365 			ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
366 		}
367 		/*
368 		 * fill in time and date so that fattime2timespec() doesn't
369 		 * spit up when called from msdosfs_getattr() with root
370 		 * denode
371 		 */
372 		ldep->de_CHun = 0;
373 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
374 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
375 		    | (1 << DD_DAY_SHIFT);
376 		/* Jan 1, 1980	 */
377 		ldep->de_ADate = ldep->de_CDate;
378 		ldep->de_MTime = ldep->de_CTime;
379 		ldep->de_MDate = ldep->de_CDate;
380 		/* leave the other fields as garbage */
381 	} else {
382 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
383 		if (error) {
384 			/*
385 			 * The denode does not contain anything useful, so
386 			 * it would be wrong to leave it on its hash chain.
387 			 * Arrange for vput() to just forget about it.
388 			 */
389 			ldep->de_Name[0] = SLOT_DELETED;
390 			nvp->v_type = VBAD;
391 
392 			vx_put(nvp);
393 			*depp = NULL;
394 			return (error);
395 		}
396 		DE_INTERNALIZE(ldep, direntptr);
397 		brelse(bp);
398 	}
399 
400 	/*
401 	 * Fill in a few fields of the vnode and finish filling in the
402 	 * denode.  Then return the address of the found denode.
403 	 */
404 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
405 		/*
406 		 * Since DOS directory entries that describe directories
407 		 * have 0 in the filesize field, we take this opportunity
408 		 * to find out the length of the directory and plug it into
409 		 * the denode structure.
410 		 */
411 		u_long size;
412 
413 		/*
414 		 * XXX Sometimes, these arrives that . entry have cluster
415 		 * number 0, when it shouldn't.  Use real cluster number
416 		 * instead of what is written in directory entry.
417 		 */
418 		if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
419 			kprintf("deget(): . entry at clust %ld != %ld\n",
420 				dirclust, ldep->de_StartCluster);
421 			ldep->de_StartCluster = dirclust;
422 		}
423 
424 		nvp->v_type = VDIR;
425 		if (ldep->de_StartCluster != MSDOSFSROOT) {
426 			error = pcbmap(ldep, 0xffff, NULL, &size, NULL);
427 			if (error == E2BIG) {
428 				ldep->de_FileSize = de_cn2off(pmp, size);
429 				error = 0;
430 			} else
431 				kprintf("deget(): pcbmap returned %d\n", error);
432 		}
433 	} else {
434 		nvp->v_type = VREG;
435 	}
436 	getmicrouptime(&tv);
437 	SETHIGH(ldep->de_modrev, tv.tv_sec);
438 	SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
439 	ldep->de_devvp = pmp->pm_devvp;
440 	vref(ldep->de_devvp);
441 	vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1);
442 	/*
443 	 * Leave nvp locked and refd so the returned inode is effectively
444 	 * locked and refd.
445 	 */
446 	*depp = ldep;
447 	return (0);
448 }
449 
450 int
451 deupdat(struct denode *dep, int waitfor)
452 {
453 	int error;
454 	struct buf *bp;
455 	struct direntry *dirp;
456 	struct timespec ts;
457 
458 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
459 		return (0);
460 	getnanotime(&ts);
461 	DETIMES(dep, &ts, &ts, &ts);
462 	if ((dep->de_flag & DE_MODIFIED) == 0)
463 		return (0);
464 	dep->de_flag &= ~DE_MODIFIED;
465 	if (dep->de_Attributes & ATTR_DIRECTORY)
466 		return (0);
467 	if (dep->de_refcnt <= 0)
468 		return (0);
469 	error = readde(dep, &bp, &dirp);
470 	if (error)
471 		return (error);
472 	DE_EXTERNALIZE(dirp, dep);
473 	if (waitfor) {
474 		return (bwrite(bp));
475 	} else {
476 		bdwrite(bp);
477 		return (0);
478 	}
479 }
480 
481 /*
482  * Truncate the file described by dep to the length specified by length.
483  */
484 int
485 detrunc(struct denode *dep, u_long length, int flags)
486 {
487 	int error;
488 	int allerror;
489 	u_long eofentry;
490 	u_long chaintofree;
491 	daddr_t bn, cn;
492 	int boff;
493 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
494 	struct buf *bp;
495 	struct msdosfsmount *pmp = dep->de_pmp;
496 
497 	mprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name,
498 		length, flags);
499 
500 	/*
501 	 * Disallow attempts to truncate the root directory since it is of
502 	 * fixed size.  That's just the way dos filesystems are.  We use
503 	 * the VROOT bit in the vnode because checking for the directory
504 	 * bit and a startcluster of 0 in the denode is not adequate to
505 	 * recognize the root directory at this point in a file or
506 	 * directory's life.
507 	 */
508 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
509 		kprintf("detrunc(): can't truncate root directory, clust %ld, "
510 			"offset %ld\n",
511 			dep->de_dirclust, dep->de_diroffset);
512 		return (EINVAL);
513 	}
514 
515 	if (dep->de_FileSize < length) {
516 		vnode_pager_setsize(DETOV(dep), length);
517 		return deextend(dep, length);
518 	}
519 
520 	/*
521 	 * If the desired length is 0 then remember the starting cluster of
522 	 * the file and set the StartCluster field in the directory entry
523 	 * to 0.  If the desired length is not zero, then get the number of
524 	 * the last cluster in the shortened file.  Then get the number of
525 	 * the first cluster in the part of the file that is to be freed.
526 	 * Then set the next cluster pointer in the last cluster of the
527 	 * file to CLUST_EOFE.
528 	 */
529 	if (length == 0) {
530 		chaintofree = dep->de_StartCluster;
531 		dep->de_StartCluster = 0;
532 		eofentry = ~0;
533 	} else {
534 		error = pcbmap(dep, de_clcount(pmp, length) - 1,
535 			       NULL, &eofentry, NULL);
536 		if (error) {
537 			mprintf("detrunc(): pcbmap fails %d\n", error);
538 			return (error);
539 		}
540 	}
541 
542 	fc_purge(dep, de_clcount(pmp, length));
543 
544 	/*
545 	 * If the new length is not a multiple of the cluster size then we
546 	 * must zero the tail end of the new last cluster in case it
547 	 * becomes part of the file again because of a seek.
548 	 */
549 	if ((boff = length & pmp->pm_crbomask) != 0) {
550 		if (isadir) {
551 			bn = xcntobn(pmp, eofentry);
552 			error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
553 				      pmp->pm_bpcluster, &bp);
554 		} else {
555 			cn = de_cluster(pmp, length);
556 			error = bread(DETOV(dep), de_cn2doff(pmp, cn),
557 				      pmp->pm_bpcluster, &bp);
558 		}
559 		if (error) {
560 			brelse(bp);
561 			mprintf("detrunc(): bread fails %d\n", error);
562 			return (error);
563 		}
564 		/*
565 		 * is this the right place for it?
566 		 */
567 		memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
568 		if (flags & IO_SYNC)
569 			bwrite(bp);
570 		else
571 			bdwrite(bp);
572 	}
573 
574 	/*
575 	 * Write out the updated directory entry.  Even if the update fails
576 	 * we free the trailing clusters.
577 	 */
578 	dep->de_FileSize = length;
579 	if (!isadir)
580 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
581 	allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
582 #ifdef MSDOSFS_DEBUG
583 	if (allerror)
584 		kprintf("detrunc(): vtruncbuf error %d\n", allerror);
585 #endif
586 	error = deupdat(dep, 1);
587 	if (error && (allerror == 0))
588 		allerror = error;
589 	mprintf("detrunc(): allerror %d, eofentry %lu\n",
590 		allerror, eofentry);
591 
592 	/*
593 	 * If we need to break the cluster chain for the file then do it
594 	 * now.
595 	 */
596 	if (eofentry != ~0) {
597 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
598 				 &chaintofree, CLUST_EOFE);
599 		if (error) {
600 			mprintf("detrunc(): fatentry errors %d\n", error);
601 			return (error);
602 		}
603 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
604 			    eofentry);
605 	}
606 
607 	/*
608 	 * Now free the clusters removed from the file because of the
609 	 * truncation.
610 	 */
611 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
612 		freeclusterchain(pmp, chaintofree);
613 
614 	return (allerror);
615 }
616 
617 /*
618  * Extend the file described by dep to length specified by length.
619  */
620 int
621 deextend(struct denode *dep, u_long length)
622 {
623 	struct msdosfsmount *pmp = dep->de_pmp;
624 	u_long count;
625 	int error;
626 
627 	/*
628 	 * The root of a DOS filesystem cannot be extended.
629 	 */
630 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
631 		return (EINVAL);
632 
633 	/*
634 	 * Directories cannot be extended.
635 	 */
636 	if (dep->de_Attributes & ATTR_DIRECTORY)
637 		return (EISDIR);
638 
639 	if (length <= dep->de_FileSize)
640 		panic("deextend: file too large");
641 
642 	/*
643 	 * Compute the number of clusters to allocate.
644 	 */
645 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
646 	if (count > 0) {
647 		if (count > pmp->pm_freeclustercount)
648 			return (ENOSPC);
649 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
650 		if (error) {
651 			/* truncate the added clusters away again */
652 			detrunc(dep, dep->de_FileSize, 0);
653 			return (error);
654 		}
655 	}
656 	dep->de_FileSize = length;
657 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
658 	return (deupdat(dep, 1));
659 }
660 
661 /*
662  * msdosfs_reclaim(struct vnode *a_vp)
663  */
664 int
665 msdosfs_reclaim(struct vop_reclaim_args *ap)
666 {
667 	struct vnode *vp = ap->a_vp;
668 	struct denode *dep = VTODE(vp);
669 
670 	mprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
671 	    dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1);
672 
673 	if (prtactive && VREFCNT(vp) > 1)
674 		vprint("msdosfs_reclaim(): pushing active", vp);
675 	/*
676 	 * Remove the denode from its hash chain.
677 	 */
678 	vp->v_data = NULL;
679 	if (dep) {
680 		msdosfs_hashrem(dep);
681 		if (dep->de_devvp) {
682 			vrele(dep->de_devvp);
683 			dep->de_devvp = 0;
684 		}
685 		kfree(dep, M_MSDOSFSNODE);
686 	}
687 	return (0);
688 }
689 
690 /*
691  * msdosfs_inactive(struct vnode *a_vp)
692  */
693 int
694 msdosfs_inactive(struct vop_inactive_args *ap)
695 {
696 	struct vnode *vp = ap->a_vp;
697 	struct denode *dep = VTODE(vp);
698 	int error = 0;
699 
700 	mprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n",
701 		dep, (dep ? dep->de_Name[0] : 0));
702 
703 	if (prtactive && VREFCNT(vp) > 1)
704 		vprint("msdosfs_inactive(): pushing active", vp);
705 
706 	/*
707 	 * Ignore denodes related to stale file handles.
708 	 */
709 	if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)
710 		goto out;
711 
712 	/*
713 	 * If the file has been deleted and it is on a read/write
714 	 * filesystem, then truncate the file, and mark the directory slot
715 	 * as empty.  (This may not be necessary for the dos filesystem.)
716 	 */
717 	mprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
718 		dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
719 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
720 		error = detrunc(dep, (u_long) 0, 0);
721 		dep->de_flag |= DE_UPDATE;
722 		dep->de_Name[0] = SLOT_DELETED;
723 	}
724 	deupdat(dep, 0);
725 
726 out:
727 	/*
728 	 * If we are done with the denode, reclaim it
729 	 * so that it can be reused immediately.
730 	 */
731 	mprintf("msdosfs_inactive(): v_refcnt 0x%08x, de_Name[0] %x\n",
732 		vp->v_refcnt, (dep ? dep->de_Name[0] : 0));
733 	if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)
734 		vrecycle(vp);
735 	return (error);
736 }
737