xref: /dragonfly/sys/vfs/msdosfs/msdosfs_denode.c (revision 1de703da)
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.2 2003/06/17 04:28:47 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 <msdosfs/bpb.h>
65 #include <msdosfs/msdosfsmount.h>
66 #include <msdosfs/direntry.h>
67 #include <msdosfs/denode.h>
68 #include <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 #ifndef NULL_SIMPLELOCKS
77 static struct simplelock dehash_slock;
78 #endif
79 
80 union _qcvt {
81 	quad_t qcvt;
82 	long val[2];
83 };
84 #define SETHIGH(q, h) { \
85 	union _qcvt tmp; \
86 	tmp.qcvt = (q); \
87 	tmp.val[_QUAD_HIGHWORD] = (h); \
88 	(q) = tmp.qcvt; \
89 }
90 #define SETLOW(q, l) { \
91 	union _qcvt tmp; \
92 	tmp.qcvt = (q); \
93 	tmp.val[_QUAD_LOWWORD] = (l); \
94 	(q) = tmp.qcvt; \
95 }
96 
97 static struct denode *
98 		msdosfs_hashget __P((dev_t dev, u_long dirclust,
99 				     u_long diroff));
100 static void	msdosfs_hashins __P((struct denode *dep));
101 static void	msdosfs_hashrem __P((struct denode *dep));
102 
103 /*ARGSUSED*/
104 int
105 msdosfs_init(vfsp)
106 	struct vfsconf *vfsp;
107 {
108 	dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash);
109 	simple_lock_init(&dehash_slock);
110 	return (0);
111 }
112 
113 int
114 msdosfs_uninit(vfsp)
115 	struct vfsconf *vfsp;
116 {
117 
118 	if (dehashtbl)
119 		free(dehashtbl, M_MSDOSFSMNT);
120 	return (0);
121 }
122 
123 static struct denode *
124 msdosfs_hashget(dev, dirclust, diroff)
125 	dev_t dev;
126 	u_long dirclust;
127 	u_long diroff;
128 {
129 	struct proc *p = curproc;	/* XXX */
130 	struct denode *dep;
131 	struct vnode *vp;
132 
133 loop:
134 	simple_lock(&dehash_slock);
135 	for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
136 		if (dirclust == dep->de_dirclust
137 		    && diroff == dep->de_diroffset
138 		    && dev == dep->de_dev
139 		    && dep->de_refcnt != 0) {
140 			vp = DETOV(dep);
141 			simple_lock(&vp->v_interlock);
142 			simple_unlock(&dehash_slock);
143 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
144 				goto loop;
145 			return (dep);
146 		}
147 	}
148 	simple_unlock(&dehash_slock);
149 	return (NULL);
150 }
151 
152 static void
153 msdosfs_hashins(dep)
154 	struct denode *dep;
155 {
156 	struct denode **depp, *deq;
157 
158 	simple_lock(&dehash_slock);
159 	depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
160 	deq = *depp;
161 	if (deq)
162 		deq->de_prev = &dep->de_next;
163 	dep->de_next = deq;
164 	dep->de_prev = depp;
165 	*depp = dep;
166 	simple_unlock(&dehash_slock);
167 }
168 
169 static void
170 msdosfs_hashrem(dep)
171 	struct denode *dep;
172 {
173 	struct denode *deq;
174 
175 	simple_lock(&dehash_slock);
176 	deq = dep->de_next;
177 	if (deq)
178 		deq->de_prev = dep->de_prev;
179 	*dep->de_prev = deq;
180 #ifdef DIAGNOSTIC
181 	dep->de_next = NULL;
182 	dep->de_prev = NULL;
183 #endif
184 	simple_unlock(&dehash_slock);
185 }
186 
187 /*
188  * If deget() succeeds it returns with the gotten denode locked().
189  *
190  * pmp	     - address of msdosfsmount structure of the filesystem containing
191  *	       the denode of interest.  The pm_dev field and the address of
192  *	       the msdosfsmount structure are used.
193  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
194  *	       diroffset is relative to the beginning of the root directory,
195  *	       otherwise it is cluster relative.
196  * diroffset - offset past begin of cluster of denode we want
197  * depp	     - returns the address of the gotten denode.
198  */
199 int
200 deget(pmp, dirclust, diroffset, depp)
201 	struct msdosfsmount *pmp;	/* so we know the maj/min number */
202 	u_long dirclust;		/* cluster this dir entry came from */
203 	u_long diroffset;		/* index of entry within the cluster */
204 	struct denode **depp;		/* returns the addr of the gotten denode */
205 {
206 	int error;
207 	dev_t dev = pmp->pm_dev;
208 	struct mount *mntp = pmp->pm_mountp;
209 	struct direntry *direntptr;
210 	struct denode *ldep;
211 	struct vnode *nvp;
212 	struct buf *bp;
213 	struct proc *p = curproc;	/* XXX */
214 	struct timeval tv;
215 
216 #ifdef MSDOSFS_DEBUG
217 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
218 	    pmp, dirclust, diroffset, depp);
219 #endif
220 
221 	/*
222 	 * On FAT32 filesystems, root is a (more or less) normal
223 	 * directory
224 	 */
225 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
226 		dirclust = pmp->pm_rootdirblk;
227 
228 	/*
229 	 * See if the denode is in the denode cache. Use the location of
230 	 * the directory entry to compute the hash value. For subdir use
231 	 * address of "." entry. For root dir (if not FAT32) use cluster
232 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
233 	 *
234 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
235 	 * examined does not represent an unlinked but still open file.
236 	 * These files are not to be accessible even when the directory
237 	 * entry that represented the file happens to be reused while the
238 	 * deleted file is still open.
239 	 */
240 	ldep = msdosfs_hashget(dev, dirclust, diroffset);
241 	if (ldep) {
242 		*depp = ldep;
243 		return (0);
244 	}
245 
246 	/*
247 	 * Do the MALLOC before the getnewvnode since doing so afterward
248 	 * might cause a bogus v_data pointer to get dereferenced
249 	 * elsewhere if MALLOC should block.
250 	 */
251 	MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK);
252 
253 	/*
254 	 * Directory entry was not in cache, have to create a vnode and
255 	 * copy it from the passed disk buffer.
256 	 */
257 	/* getnewvnode() does a VREF() on the vnode */
258 	error = getnewvnode(VT_MSDOSFS, mntp, msdosfs_vnodeop_p, &nvp);
259 	if (error) {
260 		*depp = NULL;
261 		FREE(ldep, M_MSDOSFSNODE);
262 		return error;
263 	}
264 	bzero((caddr_t)ldep, sizeof *ldep);
265 	lockinit(&ldep->de_lock, PINOD, "denode", VLKTIMEOUT, 0);
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 (lockmgr(&ldep->de_lock, LK_EXCLUSIVE, (struct simplelock *)0, p))
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 		/*
361 		 * XXX Sometimes, these arrives that . entry have cluster
362 		 * number 0, when it shouldn't.  Use real cluster number
363 		 * instead of what is written in directory entry.
364 		 */
365 		if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
366 			printf("deget(): . entry at clust %ld != %ld\n",
367 					dirclust, ldep->de_StartCluster);
368 			ldep->de_StartCluster = dirclust;
369 		}
370 
371 		nvp->v_type = VDIR;
372 		if (ldep->de_StartCluster != MSDOSFSROOT) {
373 			error = pcbmap(ldep, 0xffff, 0, &size, 0);
374 			if (error == E2BIG) {
375 				ldep->de_FileSize = de_cn2off(pmp, size);
376 				error = 0;
377 			} else
378 				printf("deget(): pcbmap returned %d\n", error);
379 		}
380 	} else
381 		nvp->v_type = VREG;
382 	getmicrouptime(&tv);
383 	SETHIGH(ldep->de_modrev, tv.tv_sec);
384 	SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
385 	ldep->de_devvp = pmp->pm_devvp;
386 	VREF(ldep->de_devvp);
387 	*depp = ldep;
388 	return (0);
389 }
390 
391 int
392 deupdat(dep, waitfor)
393 	struct denode *dep;
394 	int waitfor;
395 {
396 	int error;
397 	struct buf *bp;
398 	struct direntry *dirp;
399 	struct timespec ts;
400 
401 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
402 		return (0);
403 	getnanotime(&ts);
404 	DETIMES(dep, &ts, &ts, &ts);
405 	if ((dep->de_flag & DE_MODIFIED) == 0)
406 		return (0);
407 	dep->de_flag &= ~DE_MODIFIED;
408 	if (dep->de_Attributes & ATTR_DIRECTORY)
409 		return (0);
410 	if (dep->de_refcnt <= 0)
411 		return (0);
412 	error = readde(dep, &bp, &dirp);
413 	if (error)
414 		return (error);
415 	DE_EXTERNALIZE(dirp, dep);
416 	if (waitfor)
417 		return (bwrite(bp));
418 	else {
419 		bdwrite(bp);
420 		return (0);
421 	}
422 }
423 
424 /*
425  * Truncate the file described by dep to the length specified by length.
426  */
427 int
428 detrunc(dep, length, flags, cred, p)
429 	struct denode *dep;
430 	u_long length;
431 	int flags;
432 	struct ucred *cred;
433 	struct proc *p;
434 {
435 	int error;
436 	int allerror;
437 	u_long eofentry;
438 	u_long chaintofree;
439 	daddr_t bn;
440 	int boff;
441 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
442 	struct buf *bp;
443 	struct msdosfsmount *pmp = dep->de_pmp;
444 
445 #ifdef MSDOSFS_DEBUG
446 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
447 #endif
448 
449 	/*
450 	 * Disallow attempts to truncate the root directory since it is of
451 	 * fixed size.  That's just the way dos filesystems are.  We use
452 	 * the VROOT bit in the vnode because checking for the directory
453 	 * bit and a startcluster of 0 in the denode is not adequate to
454 	 * recognize the root directory at this point in a file or
455 	 * directory's life.
456 	 */
457 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
458 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
459 		    dep->de_dirclust, dep->de_diroffset);
460 		return (EINVAL);
461 	}
462 
463 
464 	if (dep->de_FileSize < length) {
465 		vnode_pager_setsize(DETOV(dep), length);
466 		return deextend(dep, length, cred);
467 	}
468 
469 	/*
470 	 * If the desired length is 0 then remember the starting cluster of
471 	 * the file and set the StartCluster field in the directory entry
472 	 * to 0.  If the desired length is not zero, then get the number of
473 	 * the last cluster in the shortened file.  Then get the number of
474 	 * the first cluster in the part of the file that is to be freed.
475 	 * Then set the next cluster pointer in the last cluster of the
476 	 * file to CLUST_EOFE.
477 	 */
478 	if (length == 0) {
479 		chaintofree = dep->de_StartCluster;
480 		dep->de_StartCluster = 0;
481 		eofentry = ~0;
482 	} else {
483 		error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
484 			       &eofentry, 0);
485 		if (error) {
486 #ifdef MSDOSFS_DEBUG
487 			printf("detrunc(): pcbmap fails %d\n", error);
488 #endif
489 			return (error);
490 		}
491 	}
492 
493 	fc_purge(dep, de_clcount(pmp, length));
494 
495 	/*
496 	 * If the new length is not a multiple of the cluster size then we
497 	 * must zero the tail end of the new last cluster in case it
498 	 * becomes part of the file again because of a seek.
499 	 */
500 	if ((boff = length & pmp->pm_crbomask) != 0) {
501 		if (isadir) {
502 			bn = cntobn(pmp, eofentry);
503 			error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
504 			    NOCRED, &bp);
505 		} else {
506 			bn = de_blk(pmp, length);
507 			error = bread(DETOV(dep), bn, pmp->pm_bpcluster,
508 			    NOCRED, &bp);
509 		}
510 		if (error) {
511 			brelse(bp);
512 #ifdef MSDOSFS_DEBUG
513 			printf("detrunc(): bread fails %d\n", error);
514 #endif
515 			return (error);
516 		}
517 		/*
518 		 * is this the right place for it?
519 		 */
520 		bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
521 		if (flags & IO_SYNC)
522 			bwrite(bp);
523 		else
524 			bdwrite(bp);
525 	}
526 
527 	/*
528 	 * Write out the updated directory entry.  Even if the update fails
529 	 * we free the trailing clusters.
530 	 */
531 	dep->de_FileSize = length;
532 	if (!isadir)
533 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
534 	allerror = vtruncbuf(DETOV(dep), cred, p, length, pmp->pm_bpcluster);
535 #ifdef MSDOSFS_DEBUG
536 	if (allerror)
537 		printf("detrunc(): vtruncbuf error %d\n", allerror);
538 #endif
539 	error = deupdat(dep, 1);
540 	if (error && (allerror == 0))
541 		allerror = error;
542 #ifdef MSDOSFS_DEBUG
543 	printf("detrunc(): allerror %d, eofentry %lu\n",
544 	       allerror, eofentry);
545 #endif
546 
547 	/*
548 	 * If we need to break the cluster chain for the file then do it
549 	 * now.
550 	 */
551 	if (eofentry != ~0) {
552 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
553 				 &chaintofree, CLUST_EOFE);
554 		if (error) {
555 #ifdef MSDOSFS_DEBUG
556 			printf("detrunc(): fatentry errors %d\n", error);
557 #endif
558 			return (error);
559 		}
560 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
561 			    eofentry);
562 	}
563 
564 	/*
565 	 * Now free the clusters removed from the file because of the
566 	 * truncation.
567 	 */
568 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
569 		freeclusterchain(pmp, chaintofree);
570 
571 	return (allerror);
572 }
573 
574 /*
575  * Extend the file described by dep to length specified by length.
576  */
577 int
578 deextend(dep, length, cred)
579 	struct denode *dep;
580 	u_long length;
581 	struct ucred *cred;
582 {
583 	struct msdosfsmount *pmp = dep->de_pmp;
584 	u_long count;
585 	int error;
586 
587 	/*
588 	 * The root of a DOS filesystem cannot be extended.
589 	 */
590 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
591 		return (EINVAL);
592 
593 	/*
594 	 * Directories cannot be extended.
595 	 */
596 	if (dep->de_Attributes & ATTR_DIRECTORY)
597 		return (EISDIR);
598 
599 	if (length <= dep->de_FileSize)
600 		panic("deextend: file too large");
601 
602 	/*
603 	 * Compute the number of clusters to allocate.
604 	 */
605 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
606 	if (count > 0) {
607 		if (count > pmp->pm_freeclustercount)
608 			return (ENOSPC);
609 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
610 		if (error) {
611 			/* truncate the added clusters away again */
612 			(void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
613 			return (error);
614 		}
615 	}
616 	dep->de_FileSize = length;
617 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
618 	return (deupdat(dep, 1));
619 }
620 
621 /*
622  * Move a denode to its correct hash queue after the file it represents has
623  * been moved to a new directory.
624  */
625 void
626 reinsert(dep)
627 	struct denode *dep;
628 {
629 	/*
630 	 * Fix up the denode cache.  If the denode is for a directory,
631 	 * there is nothing to do since the hash is based on the starting
632 	 * cluster of the directory file and that hasn't changed.  If for a
633 	 * file the hash is based on the location of the directory entry,
634 	 * so we must remove it from the cache and re-enter it with the
635 	 * hash based on the new location of the directory entry.
636 	 */
637 	if (dep->de_Attributes & ATTR_DIRECTORY)
638 		return;
639 	msdosfs_hashrem(dep);
640 	msdosfs_hashins(dep);
641 }
642 
643 int
644 msdosfs_reclaim(ap)
645 	struct vop_reclaim_args /* {
646 		struct vnode *a_vp;
647 	} */ *ap;
648 {
649 	struct vnode *vp = ap->a_vp;
650 	struct denode *dep = VTODE(vp);
651 
652 #ifdef MSDOSFS_DEBUG
653 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
654 	    dep, dep->de_Name, dep->de_refcnt);
655 #endif
656 
657 	if (prtactive && vp->v_usecount != 0)
658 		vprint("msdosfs_reclaim(): pushing active", vp);
659 	/*
660 	 * Remove the denode from its hash chain.
661 	 */
662 	msdosfs_hashrem(dep);
663 	/*
664 	 * Purge old data structures associated with the denode.
665 	 */
666 	cache_purge(vp);
667 	if (dep->de_devvp) {
668 		vrele(dep->de_devvp);
669 		dep->de_devvp = 0;
670 	}
671 #if 0 /* XXX */
672 	dep->de_flag = 0;
673 #endif
674 	FREE(dep, M_MSDOSFSNODE);
675 	vp->v_data = NULL;
676 
677 	return (0);
678 }
679 
680 int
681 msdosfs_inactive(ap)
682 	struct vop_inactive_args /* {
683 		struct vnode *a_vp;
684 		struct proc *a_p;
685 	} */ *ap;
686 {
687 	struct vnode *vp = ap->a_vp;
688 	struct denode *dep = VTODE(vp);
689 	struct proc *p = ap->a_p;
690 	int error = 0;
691 
692 #ifdef MSDOSFS_DEBUG
693 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
694 #endif
695 
696 	if (prtactive && vp->v_usecount != 0)
697 		vprint("msdosfs_inactive(): pushing active", vp);
698 
699 	/*
700 	 * Ignore denodes related to stale file handles.
701 	 */
702 	if (dep->de_Name[0] == SLOT_DELETED)
703 		goto out;
704 
705 	/*
706 	 * If the file has been deleted and it is on a read/write
707 	 * filesystem, then truncate the file, and mark the directory slot
708 	 * as empty.  (This may not be necessary for the dos filesystem.)
709 	 */
710 #ifdef MSDOSFS_DEBUG
711 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
712 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
713 #endif
714 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
715 		error = detrunc(dep, (u_long) 0, 0, NOCRED, p);
716 		dep->de_flag |= DE_UPDATE;
717 		dep->de_Name[0] = SLOT_DELETED;
718 	}
719 	deupdat(dep, 0);
720 
721 out:
722 	VOP_UNLOCK(vp, 0, p);
723 	/*
724 	 * If we are done with the denode, reclaim it
725 	 * so that it can be reused immediately.
726 	 */
727 #ifdef MSDOSFS_DEBUG
728 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
729 	       dep->de_Name[0]);
730 #endif
731 	if (dep->de_Name[0] == SLOT_DELETED)
732 		vrecycle(vp, (struct simplelock *)0, p);
733 	return (error);
734 }
735