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