xref: /freebsd/sys/fs/msdosfs/msdosfs_denode.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/buf.h>
56 #include <sys/clock.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/vmmeter.h>
61 #include <sys/vnode.h>
62 
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65 
66 #include <fs/msdosfs/bpb.h>
67 #include <fs/msdosfs/direntry.h>
68 #include <fs/msdosfs/denode.h>
69 #include <fs/msdosfs/fat.h>
70 #include <fs/msdosfs/msdosfsmount.h>
71 
72 static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part");
73 
74 static int
75 de_vncmpf(struct vnode *vp, void *arg)
76 {
77 	struct denode *de;
78 	uint64_t *a;
79 
80 	a = arg;
81 	de = VTODE(vp);
82 	return (de->de_inode != *a) || (de->de_refcnt <= 0);
83 }
84 
85 /*
86  * If deget() succeeds it returns with the gotten denode locked().
87  *
88  * pmp	     - address of msdosfsmount structure of the filesystem containing
89  *	       the denode of interest.  The address of
90  *	       the msdosfsmount structure are used.
91  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
92  *	       diroffset is relative to the beginning of the root directory,
93  *	       otherwise it is cluster relative.
94  * diroffset - offset past begin of cluster of denode we want
95  * lkflags   - locking flags (LK_NOWAIT)
96  * depp	     - returns the address of the gotten denode.
97  */
98 int
99 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
100     int lkflags, struct denode **depp)
101 {
102 	int error;
103 	uint64_t inode;
104 	struct mount *mntp = pmp->pm_mountp;
105 	struct direntry *direntptr;
106 	struct denode *ldep;
107 	struct vnode *nvp, *xvp;
108 	struct buf *bp;
109 
110 #ifdef MSDOSFS_DEBUG
111 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, flags %#x, "
112 	    "depp %p)\n",
113 	    pmp, dirclust, diroffset, lkflags, depp);
114 #endif
115 	MPASS((lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE);
116 
117 	/*
118 	 * On FAT32 filesystems, root is a (more or less) normal
119 	 * directory
120 	 */
121 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
122 		dirclust = pmp->pm_rootdirblk;
123 
124 	/*
125 	 * See if the denode is in the denode cache. Use the location of
126 	 * the directory entry to compute the hash value. For subdir use
127 	 * address of "." entry. For root dir (if not FAT32) use cluster
128 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
129 	 *
130 	 * NOTE: de_vncmpf will explicitly skip any denodes that do not have
131 	 * a de_refcnt > 0.  This insures that we do not attempt to use
132 	 * a denode that represents an unlinked but still open file.
133 	 * These files are not to be accessible even when the directory
134 	 * entry that represented the file happens to be reused while the
135 	 * deleted file is still open.
136 	 */
137 	inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
138 
139 	error = vfs_hash_get(mntp, inode, lkflags, curthread, &nvp,
140 	    de_vncmpf, &inode);
141 	if (error)
142 		return (error);
143 	if (nvp != NULL) {
144 		*depp = VTODE(nvp);
145 		if ((*depp)->de_dirclust != dirclust) {
146 			printf("%s: wrong dir cluster %lu %lu\n",
147 			    pmp->pm_mountp->mnt_stat.f_mntonname,
148 			    (*depp)->de_dirclust, dirclust);
149 			goto badoff;
150 		}
151 		if ((*depp)->de_diroffset != diroffset) {
152 			printf("%s: wrong dir offset %lu %lu\n",
153 			    pmp->pm_mountp->mnt_stat.f_mntonname,
154 			    (*depp)->de_diroffset,  diroffset);
155 			goto badoff;
156 		}
157 		return (0);
158 badoff:
159 		vgone(nvp);
160 		vput(nvp);
161 		msdosfs_integrity_error(pmp);
162 		return (EBADF);
163 	}
164 	ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
165 
166 	/*
167 	 * Directory entry was not in cache, have to create a vnode and
168 	 * copy it from the passed disk buffer.
169 	 */
170 	/* getnewvnode() does a VREF() on the vnode */
171 	error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
172 	if (error) {
173 		*depp = NULL;
174 		free(ldep, M_MSDOSFSNODE);
175 		return error;
176 	}
177 	nvp->v_data = ldep;
178 	ldep->de_vnode = nvp;
179 	ldep->de_flag = 0;
180 	ldep->de_dirclust = dirclust;
181 	ldep->de_diroffset = diroffset;
182 	ldep->de_inode = inode;
183 	cluster_init_vn(&ldep->de_clusterw);
184 	lockmgr(nvp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
185 	VN_LOCK_AREC(nvp);	/* for doscheckpath */
186 	fc_purge(ldep, 0);	/* init the FAT cache for this denode */
187 	error = insmntque(nvp, mntp);
188 	if (error != 0) {
189 		free(ldep, M_MSDOSFSNODE);
190 		*depp = NULL;
191 		return (error);
192 	}
193 	error = vfs_hash_insert(nvp, inode, lkflags, curthread, &xvp,
194 	    de_vncmpf, &inode);
195 	if (error) {
196 		*depp = NULL;
197 		return (error);
198 	}
199 	if (xvp != NULL) {
200 		*depp = xvp->v_data;
201 		return (0);
202 	}
203 
204 	ldep->de_pmp = pmp;
205 	ldep->de_refcnt = 1;
206 	/*
207 	 * Copy the directory entry into the denode area of the vnode.
208 	 */
209 	if ((dirclust == MSDOSFSROOT ||
210 	    (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) &&
211 	    diroffset == MSDOSFSROOT_OFS) {
212 		/*
213 		 * Directory entry for the root directory. There isn't one,
214 		 * so we manufacture one. We should probably rummage
215 		 * through the root directory and find a label entry (if it
216 		 * exists), and then use the time and date from that entry
217 		 * as the time and date for the root denode.
218 		 */
219 		nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
220 
221 		ldep->de_Attributes = ATTR_DIRECTORY;
222 		ldep->de_LowerCase = 0;
223 		if (FAT32(pmp))
224 			ldep->de_StartCluster = pmp->pm_rootdirblk;
225 			/* de_FileSize will be filled in further down */
226 		else {
227 			ldep->de_StartCluster = MSDOSFSROOT;
228 			ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
229 		}
230 		/*
231 		 * fill in time and date so that fattime2timespec() doesn't
232 		 * spit up when called from msdosfs_getattr() with root
233 		 * denode
234 		 */
235 		ldep->de_CHun = 0;
236 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
237 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
238 		    | (1 << DD_DAY_SHIFT);
239 		/* Jan 1, 1980	 */
240 		ldep->de_ADate = ldep->de_CDate;
241 		ldep->de_MTime = ldep->de_CTime;
242 		ldep->de_MDate = ldep->de_CDate;
243 		/* leave the other fields as garbage */
244 	} else {
245 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
246 		if (error) {
247 			/*
248 			 * The denode does not contain anything useful, so
249 			 * it would be wrong to leave it on its hash chain.
250 			 * Arrange for vput() to just forget about it.
251 			 */
252 			ldep->de_Name[0] = SLOT_DELETED;
253 			vgone(nvp);
254 			vput(nvp);
255 			*depp = NULL;
256 			return (error);
257 		}
258 		(void)DE_INTERNALIZE(ldep, direntptr);
259 		brelse(bp);
260 	}
261 
262 	/*
263 	 * Fill in a few fields of the vnode and finish filling in the
264 	 * denode.  Then return the address of the found denode.
265 	 */
266 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
267 		/*
268 		 * Since DOS directory entries that describe directories
269 		 * have 0 in the filesize field, we take this opportunity
270 		 * to find out the length of the directory and plug it into
271 		 * the denode structure.
272 		 */
273 		u_long size;
274 
275 		/*
276 		 * XXX it sometimes happens that the "." entry has cluster
277 		 * number 0 when it shouldn't.  Use the actual cluster number
278 		 * instead of what is written in directory entry.
279 		 */
280 		if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
281 #ifdef MSDOSFS_DEBUG
282 			printf("deget(): \".\" entry at clust %lu != %lu\n",
283 			    dirclust, ldep->de_StartCluster);
284 #endif
285 			ldep->de_StartCluster = dirclust;
286 		}
287 
288 		nvp->v_type = VDIR;
289 		if (ldep->de_StartCluster != MSDOSFSROOT) {
290 			error = pcbmap(ldep, 0xffff, 0, &size, 0);
291 			if (error == E2BIG) {
292 				ldep->de_FileSize = de_cn2off(pmp, size);
293 				error = 0;
294 			} else {
295 #ifdef MSDOSFS_DEBUG
296 				printf("deget(): pcbmap returned %d\n", error);
297 #endif
298 			}
299 		}
300 	} else
301 		nvp->v_type = VREG;
302 	vn_set_state(nvp, VSTATE_CONSTRUCTED);
303 	ldep->de_modrev = init_va_filerev();
304 	*depp = ldep;
305 	return (0);
306 }
307 
308 int
309 deupdat(struct denode *dep, int waitfor)
310 {
311 	struct direntry dir;
312 	struct timespec ts;
313 	struct buf *bp;
314 	struct direntry *dirp;
315 	int error;
316 
317 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) {
318 		dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS |
319 		    DE_MODIFIED);
320 		return (0);
321 	}
322 	vfs_timestamp(&ts);
323 	DETIMES(dep, &ts, &ts, &ts);
324 	if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0)
325 		return (0);
326 	dep->de_flag &= ~DE_MODIFIED;
327 	if (DETOV(dep)->v_vflag & VV_ROOT)
328 		return (EINVAL);
329 	if (dep->de_refcnt <= 0)
330 		return (0);
331 	error = readde(dep, &bp, &dirp);
332 	if (error)
333 		return (error);
334 	DE_EXTERNALIZE(&dir, dep);
335 	if (bcmp(dirp, &dir, sizeof(dir)) == 0) {
336 		if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) {
337 			brelse(bp);
338 			return (0);
339 		}
340 	} else
341 		*dirp = dir;
342 	if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
343 		bp->b_flags |= B_CLUSTEROK;
344 	if (waitfor)
345 		error = bwrite(bp);
346 	else if (vm_page_count_severe() || buf_dirty_count_severe())
347 		bawrite(bp);
348 	else
349 		bdwrite(bp);
350 	return (error);
351 }
352 
353 /*
354  * Truncate the file described by dep to the length specified by length.
355  */
356 int
357 detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
358 {
359 	int error;
360 	int allerror;
361 	u_long eofentry;
362 	u_long chaintofree;
363 	daddr_t bn;
364 	int boff;
365 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
366 	struct buf *bp;
367 	struct msdosfsmount *pmp = dep->de_pmp;
368 
369 #ifdef MSDOSFS_DEBUG
370 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
371 #endif
372 
373 	/*
374 	 * Disallow attempts to truncate the root directory since it is of
375 	 * fixed size.  That's just the way dos filesystems are.  We use
376 	 * the VROOT bit in the vnode because checking for the directory
377 	 * bit and a startcluster of 0 in the denode is not adequate to
378 	 * recognize the root directory at this point in a file or
379 	 * directory's life.
380 	 */
381 	if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
382 #ifdef MSDOSFS_DEBUG
383 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
384 		    dep->de_dirclust, dep->de_diroffset);
385 #endif
386 		return (EINVAL);
387 	}
388 
389 	if (dep->de_FileSize < length)
390 		return (deextend(dep, length, cred));
391 
392 	/*
393 	 * If the desired length is 0 then remember the starting cluster of
394 	 * the file and set the StartCluster field in the directory entry
395 	 * to 0.  If the desired length is not zero, then get the number of
396 	 * the last cluster in the shortened file.  Then get the number of
397 	 * the first cluster in the part of the file that is to be freed.
398 	 * Then set the next cluster pointer in the last cluster of the
399 	 * file to CLUST_EOFE.
400 	 */
401 	if (length == 0) {
402 		chaintofree = dep->de_StartCluster;
403 		dep->de_StartCluster = 0;
404 		eofentry = ~0;
405 	} else {
406 		error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
407 			       &eofentry, 0);
408 		if (error) {
409 #ifdef MSDOSFS_DEBUG
410 			printf("detrunc(): pcbmap fails %d\n", error);
411 #endif
412 			return (error);
413 		}
414 	}
415 
416 	fc_purge(dep, de_clcount(pmp, length));
417 
418 	/*
419 	 * If the new length is not a multiple of the cluster size then we
420 	 * must zero the tail end of the new last cluster in case it
421 	 * becomes part of the file again because of a seek.
422 	 */
423 	if ((boff = length & pmp->pm_crbomask) != 0) {
424 		if (isadir) {
425 			bn = cntobn(pmp, eofentry);
426 			error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
427 			    NOCRED, &bp);
428 		} else {
429 			error = bread(DETOV(dep), de_cluster(pmp, length),
430 			    pmp->pm_bpcluster, cred, &bp);
431 		}
432 		if (error) {
433 #ifdef MSDOSFS_DEBUG
434 			printf("detrunc(): bread fails %d\n", error);
435 #endif
436 			return (error);
437 		}
438 		memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
439 		if ((flags & IO_SYNC) != 0)
440 			bwrite(bp);
441 		else
442 			bdwrite(bp);
443 	}
444 
445 	/*
446 	 * Write out the updated directory entry.  Even if the update fails
447 	 * we free the trailing clusters.
448 	 */
449 	dep->de_FileSize = length;
450 	if (!isadir)
451 		dep->de_flag |= DE_UPDATE | DE_MODIFIED;
452 	allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
453 #ifdef MSDOSFS_DEBUG
454 	if (allerror)
455 		printf("detrunc(): vtruncbuf error %d\n", allerror);
456 #endif
457 	error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
458 	if (error != 0 && allerror == 0)
459 		allerror = error;
460 #ifdef MSDOSFS_DEBUG
461 	printf("detrunc(): allerror %d, eofentry %lu\n",
462 	       allerror, eofentry);
463 #endif
464 
465 	/*
466 	 * If we need to break the cluster chain for the file then do it
467 	 * now.
468 	 */
469 	if (eofentry != ~0) {
470 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
471 				 &chaintofree, CLUST_EOFE);
472 		if (error) {
473 #ifdef MSDOSFS_DEBUG
474 			printf("detrunc(): fatentry errors %d\n", error);
475 #endif
476 			return (error);
477 		}
478 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
479 		    eofentry);
480 	}
481 
482 	/*
483 	 * Now free the clusters removed from the file because of the
484 	 * truncation.
485 	 */
486 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
487 		freeclusterchain(pmp, chaintofree);
488 
489 	return (allerror);
490 }
491 
492 /*
493  * Extend the file described by dep to length specified by length.
494  */
495 int
496 deextend(struct denode *dep, u_long length, struct ucred *cred)
497 {
498 	struct msdosfsmount *pmp = dep->de_pmp;
499 	struct vnode *vp = DETOV(dep);
500 	struct buf *bp;
501 	off_t eof_clusteroff;
502 	u_long count;
503 	int error;
504 
505 	/*
506 	 * The root of a DOS filesystem cannot be extended.
507 	 */
508 	if ((vp->v_vflag & VV_ROOT) != 0 && !FAT32(pmp))
509 		return (EINVAL);
510 
511 	/*
512 	 * Directories cannot be extended.
513 	 */
514 	if (dep->de_Attributes & ATTR_DIRECTORY)
515 		return (EISDIR);
516 
517 	if (length <= dep->de_FileSize)
518 		panic("deextend: file too large");
519 
520 	/*
521 	 * Compute the number of clusters to allocate.
522 	 */
523 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
524 	if (count > 0) {
525 		if (count > pmp->pm_freeclustercount)
526 			return (ENOSPC);
527 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
528 		if (error != 0)
529 			goto rewind;
530 	}
531 
532 	/*
533 	 * For the case of cluster size larger than the page size, we
534 	 * need to ensure that the possibly dirty partial buffer at
535 	 * the old end of file is not filled with invalid pages by
536 	 * extension.  Otherwise it has a contradictory state of
537 	 * B_CACHE | B_DELWRI but with invalid pages, and cannot be
538 	 * neither written out nor validated.
539 	 *
540 	 * Fix it by proactively clearing extended pages.  Need to do
541 	 * both vfs_bio_clrbuf() to mark pages valid, and to zero
542 	 * actual buffer content which might exist in the tail of the
543 	 * already valid cluster.
544 	 */
545 	error = bread(vp, de_cluster(pmp, dep->de_FileSize), pmp->pm_bpcluster,
546 	    NOCRED, &bp);
547 	if (error != 0)
548 		goto rewind;
549 	vfs_bio_clrbuf(bp);
550 	eof_clusteroff = de_cn2off(pmp, de_cluster(pmp, dep->de_FileSize));
551 	vfs_bio_bzero_buf(bp, dep->de_FileSize - eof_clusteroff,
552 	    pmp->pm_bpcluster - dep->de_FileSize + eof_clusteroff);
553 	if (!DOINGASYNC(vp))
554 		(void)bwrite(bp);
555 	else if (vm_page_count_severe() || buf_dirty_count_severe())
556 		bawrite(bp);
557 	else
558 		bdwrite(bp);
559 
560 	vnode_pager_setsize(vp, length);
561 	dep->de_FileSize = length;
562 	dep->de_flag |= DE_UPDATE | DE_MODIFIED;
563 	return (deupdat(dep, !DOINGASYNC(vp)));
564 
565 rewind:
566 	/* truncate the added clusters away again */
567 	(void)detrunc(dep, dep->de_FileSize, 0, cred);
568 	return (error);
569 }
570 
571 /*
572  * Move a denode to its correct hash queue after the file it represents has
573  * been moved to a new directory.
574  */
575 void
576 reinsert(struct denode *dep)
577 {
578 	struct vnode *vp;
579 
580 	/*
581 	 * Fix up the denode cache.  If the denode is for a directory,
582 	 * there is nothing to do since the hash is based on the starting
583 	 * cluster of the directory file and that hasn't changed.  If for a
584 	 * file the hash is based on the location of the directory entry,
585 	 * so we must remove it from the cache and re-enter it with the
586 	 * hash based on the new location of the directory entry.
587 	 */
588 #if 0
589 	if (dep->de_Attributes & ATTR_DIRECTORY)
590 		return;
591 #endif
592 	vp = DETOV(dep);
593 	dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust +
594 	    dep->de_diroffset;
595 	vfs_hash_rehash(vp, dep->de_inode);
596 }
597 
598 int
599 msdosfs_reclaim(struct vop_reclaim_args *ap)
600 {
601 	struct vnode *vp = ap->a_vp;
602 	struct denode *dep = VTODE(vp);
603 
604 #ifdef MSDOSFS_DEBUG
605 	printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
606 	    dep, dep->de_Name, dep->de_refcnt);
607 #endif
608 
609 	/*
610 	 * Remove the denode from its hash chain.
611 	 */
612 	vfs_hash_remove(vp);
613 	/*
614 	 * Purge old data structures associated with the denode.
615 	 */
616 #if 0 /* XXX */
617 	dep->de_flag = 0;
618 #endif
619 	free(dep, M_MSDOSFSNODE);
620 	vp->v_data = NULL;
621 
622 	return (0);
623 }
624 
625 int
626 msdosfs_inactive(struct vop_inactive_args *ap)
627 {
628 	struct vnode *vp = ap->a_vp;
629 	struct denode *dep = VTODE(vp);
630 	int error = 0;
631 
632 #ifdef MSDOSFS_DEBUG
633 	printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
634 #endif
635 
636 	/*
637 	 * Ignore denodes related to stale file handles.
638 	 */
639 	if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
640 		goto out;
641 
642 	/*
643 	 * If the file has been deleted and it is on a read/write
644 	 * filesystem, then truncate the file, and mark the directory slot
645 	 * as empty.  (This may not be necessary for the dos filesystem.)
646 	 */
647 #ifdef MSDOSFS_DEBUG
648 	printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n",
649 	    dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag,
650 	    (unsigned long long)MNT_RDONLY);
651 #endif
652 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
653 		error = detrunc(dep, (u_long) 0, 0, NOCRED);
654 		dep->de_flag |= DE_UPDATE;
655 		dep->de_Name[0] = SLOT_DELETED;
656 	}
657 	deupdat(dep, 0);
658 
659 out:
660 	/*
661 	 * If we are done with the denode, reclaim it
662 	 * so that it can be reused immediately.
663 	 */
664 #ifdef MSDOSFS_DEBUG
665 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
666 	       vrefcnt(vp), dep->de_Name[0]);
667 #endif
668 	if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
669 		vrecycle(vp);
670 	return (error);
671 }
672