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