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