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