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