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