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