1 /**	$MirOS: src/sys/msdosfs/msdosfs_denode.c,v 1.2 2005/03/06 21:28:12 tg Exp $ */
2 /*	$OpenBSD: msdosfs_denode.c,v 1.26 2004/05/14 04:05:05 tedu Exp $	*/
3 /*	$NetBSD: msdosfs_denode.c,v 1.23 1997/10/17 11:23:58 ws 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/mount.h>
55 #include <sys/malloc.h>
56 #include <sys/proc.h>
57 #include <sys/buf.h>
58 #include <sys/vnode.h>
59 #include <sys/kernel.h>		/* defines "time" */
60 #include <sys/dirent.h>
61 #include <sys/namei.h>
62 
63 #include <uvm/uvm_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, dcl, doff)	(((dev) + (dcl) + (doff) / sizeof(struct direntry)) \
74 				 & dehash)
75 
76 static struct denode *msdosfs_hashget(dev_t, uint32_t, uint32_t);
77 static int msdosfs_hashins(struct denode *);
78 static void msdosfs_hashrem(struct denode *);
79 
80 /*ARGSUSED*/
81 int
msdosfs_init(vfsp)82 msdosfs_init(vfsp)
83 	struct vfsconf *vfsp;
84 {
85 	dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, M_WAITOK, &dehash);
86 	return (0);
87 }
88 
89 static struct denode *
msdosfs_hashget(dev,dirclust,diroff)90 msdosfs_hashget(dev, dirclust, diroff)
91 	dev_t dev;
92 	uint32_t dirclust;
93 	uint32_t diroff;
94 {
95 	struct denode *dep;
96 	struct proc *p = curproc; /* XXX */
97 
98 	for (;;)
99 		for (dep = dehashtbl[DEHASH(dev, dirclust, diroff)];;
100 		     dep = dep->de_next) {
101 			if (dep == NULL)
102 				return (NULL);
103 			if (dirclust == dep->de_dirclust &&
104 			    diroff == dep->de_diroffset &&
105 			    dev == dep->de_dev &&
106 			    dep->de_refcnt != 0) {
107 				struct vnode *vp = DETOV(dep);
108 
109 				simple_lock(&vp->v_interlock);
110 				if (!vget(vp, LK_EXCLUSIVE  | LK_INTERLOCK, p))
111 					return (dep);
112 				break;
113 			}
114 		}
115 	/* NOTREACHED */
116 }
117 
118 static int
msdosfs_hashins(dep)119 msdosfs_hashins(dep)
120 	struct denode *dep;
121 {
122 	struct denode **depp, *deq;
123 
124 	depp = &dehashtbl[DEHASH(dep->de_dev, dep->de_dirclust,
125 				 dep->de_diroffset)];
126 
127 	for (deq = *depp; deq; deq = deq->de_next) {
128 		if (dep->de_dirclust == deq->de_dirclust &&
129 		    dep->de_diroffset == deq->de_diroffset &&
130 		    dep->de_dev == deq->de_dev &&
131 		    deq->de_refcnt != 0) {
132 			return (EEXIST);
133 		}
134 	}
135 
136 	if ((deq = *depp) != NULL)
137 		deq->de_prev = &dep->de_next;
138 	dep->de_next = deq;
139 	dep->de_prev = depp;
140 	*depp = dep;
141 	return (0);
142 }
143 
144 static void
msdosfs_hashrem(dep)145 msdosfs_hashrem(dep)
146 	struct denode *dep;
147 {
148 	struct denode *deq;
149 
150 	if (dep->de_prev == NULL)
151 		return;
152 
153 	if ((deq = dep->de_next) != NULL)
154 		deq->de_prev = dep->de_prev;
155 	*dep->de_prev = deq;
156 #ifdef DIAGNOSTIC
157 	dep->de_next = NULL;
158 	dep->de_prev = NULL;
159 #endif
160 }
161 
162 /*
163  * If deget() succeeds it returns with the gotten denode locked().
164  *
165  * pmp	     - address of msdosfsmount structure of the filesystem containing
166  *	       the denode of interest.  The pm_dev field and the address of
167  *	       the msdosfsmount structure are used.
168  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
169  *	       diroffset is relative to the beginning of the root directory,
170  *	       otherwise it is cluster relative.
171  * diroffset - offset past begin of cluster of denode we want
172  * depp	     - returns the address of the gotten denode.
173  */
174 int
deget(pmp,dirclust,diroffset,depp)175 deget(pmp, dirclust, diroffset, depp)
176 	struct msdosfsmount *pmp;	/* so we know the maj/min number */
177 	uint32_t dirclust;		/* cluster this dir entry came from */
178 	uint32_t diroffset;		/* index of entry within the cluster */
179 	struct denode **depp;		/* returns the addr of the gotten denode */
180 {
181 	int error;
182 	extern int (**msdosfs_vnodeop_p)(void *);
183 	struct direntry *direntptr;
184 	struct denode *ldep;
185 	struct vnode *nvp;
186 	struct buf *bp;
187 	struct proc *p = curproc; /* XXX */
188 
189 #ifdef MSDOSFS_DEBUG
190 	printf("deget(pmp %08x, dirclust %d, diroffset %x, depp %08x)\n",
191 	    pmp, dirclust, diroffset, depp);
192 #endif
193 
194 	/*
195 	 * On FAT32 filesystems, root is a (more or less) normal
196 	 * directory
197 	 */
198 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
199 		dirclust = pmp->pm_rootdirblk;
200 
201 	/*
202 	 * See if the denode is in the denode cache. Use the location of
203 	 * the directory entry to compute the hash value. For subdir use
204 	 * address of "." entry. For root dir (if not FAT32) use cluster
205 	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
206 	 *
207 	 * NOTE: The check for de_refcnt > 0 below insures the denode being
208 	 * examined does not represent an unlinked but still open file.
209 	 * These files are not to be accessible even when the directory
210 	 * entry that represented the file happens to be reused while the
211 	 * deleted file is still open.
212 	 */
213 retry:
214 	ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset);
215 	if (ldep) {
216 		*depp = ldep;
217 		return (0);
218 	}
219 
220 	/*
221 	 * Directory entry was not in cache, have to create a vnode and
222 	 * copy it from the passed disk buffer.
223 	 */
224 	/* getnewvnode() does a VREF() on the vnode */
225 	error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp,
226 			    msdosfs_vnodeop_p, &nvp);
227 	if (error) {
228 		*depp = 0;
229 		return (error);
230 	}
231 	MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE,
232 	    M_WAITOK);
233 	bzero((caddr_t)ldep, sizeof *ldep);
234 	lockinit(&ldep->de_lock, PINOD, "denode", 0, 0);
235 	nvp->v_data = ldep;
236 	ldep->de_vnode = nvp;
237 	ldep->de_flag = 0;
238 	ldep->de_devvp = 0;
239 	ldep->de_lockf = 0;
240 	ldep->de_dev = pmp->pm_dev;
241 	ldep->de_dirclust = dirclust;
242 	ldep->de_diroffset = diroffset;
243 	fc_purge(ldep, 0);	/* init the fat cache for this denode */
244 
245 	/*
246 	 * Insert the denode into the hash queue and lock the denode so it
247 	 * can't be accessed until we've read it in and have done what we
248 	 * need to it.
249 	 */
250 	vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY, p);
251 	error = msdosfs_hashins(ldep);
252 
253 	if (error) {
254 		vput (nvp);
255 
256 		if (error == EEXIST)
257 			goto retry;
258 
259 		return (error);
260 	}
261 
262 	ldep->de_pmp = pmp;
263 	ldep->de_devvp = pmp->pm_devvp;
264 	ldep->de_refcnt = 1;
265 	/*
266 	 * Copy the directory entry into the denode area of the vnode.
267 	 */
268 	if ((dirclust == MSDOSFSROOT
269 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
270 	    && diroffset == MSDOSFSROOT_OFS) {
271 		/*
272 		 * Directory entry for the root directory. There isn't one,
273 		 * so we manufacture one. We should probably rummage
274 		 * through the root directory and find a label entry (if it
275 		 * exists), and then use the time and date from that entry
276 		 * as the time and date for the root denode.
277 		 */
278 	        nvp->v_flag |= VROOT; /* should be further down         XXX */
279 
280 		ldep->de_Attributes = ATTR_DIRECTORY;
281 		if (FAT32(pmp))
282 		        ldep->de_StartCluster = pmp->pm_rootdirblk;
283 		        /* de_FileSize will be filled in further down */
284 		else {
285 		        ldep->de_StartCluster = MSDOSFSROOT;
286 		        ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
287 		}
288 		/*
289 		 * fill in time and date so that dos2unixtime() doesn't
290 		 * spit up when called from msdosfs_getattr() with root
291 		 * denode
292 		 */
293 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
294 		ldep->de_CTimeHundredth = 0;
295 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
296 		    | (1 << DD_DAY_SHIFT);
297 		/* Jan 1, 1980	 */
298 		ldep->de_ADate = ldep->de_CDate;
299 		ldep->de_MTime = ldep->de_CTime;
300 		ldep->de_MDate = ldep->de_CDate;
301 		/* leave the other fields as garbage */
302 	} else {
303 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
304 		if (error)
305 			return (error);
306 		DE_INTERNALIZE(ldep, direntptr);
307 		brelse(bp);
308 	}
309 
310 	/*
311 	 * Fill in a few fields of the vnode and finish filling in the
312 	 * denode.  Then return the address of the found denode.
313 	 */
314 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
315 		/*
316 		 * Since DOS directory entries that describe directories
317 		 * have 0 in the filesize field, we take this opportunity
318 		 * to find out the length of the directory and plug it into
319 		 * the denode structure.
320 		 */
321 		uint32_t size;
322 
323 		nvp->v_type = VDIR;
324 		if (ldep->de_StartCluster != MSDOSFSROOT) {
325 			error = pcbmap(ldep, 0xffff, 0, &size, 0);
326 			if (error == E2BIG) {
327 				ldep->de_FileSize = de_cn2off(pmp, size);
328 				error = 0;
329 			} else
330 				printf("deget(): pcbmap returned %d\n", error);
331 		}
332 	} else
333 		nvp->v_type = VREG;
334 	VREF(ldep->de_devvp);
335 	*depp = ldep;
336 	return (0);
337 }
338 
339 int
deupdat(dep,waitfor)340 deupdat(dep, waitfor)
341 	struct denode *dep;
342 	int waitfor;
343 {
344 	struct buf *bp;
345 	struct direntry *dirp;
346 	int error;
347 	struct timespec ts;
348 
349 	if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
350 		return (0);
351 	TIMEVAL_TO_TIMESPEC(&time, &ts);
352 	DETIMES(dep, &ts, &ts, &ts);
353 	if ((dep->de_flag & DE_MODIFIED) == 0)
354 		return (0);
355 	dep->de_flag &= ~DE_MODIFIED;
356 	if (dep->de_Attributes & ATTR_DIRECTORY)
357 		return (0);
358 	if (dep->de_refcnt <= 0)
359 		return (0);
360 	error = readde(dep, &bp, &dirp);
361 	if (error)
362 		return (error);
363 	DE_EXTERNALIZE(dirp, dep);
364 	if (waitfor)
365 		return (bwrite(bp));
366 	else {
367 		bdwrite(bp);
368 		return (0);
369 	}
370 }
371 
372 /*
373  * Truncate the file described by dep to the length specified by length.
374  */
375 int
detrunc(dep,length,flags,cred,p)376 detrunc(dep, length, flags, cred, p)
377 	struct denode *dep;
378 	uint32_t length;
379 	int flags;
380 	struct ucred *cred;
381 	struct proc *p;
382 {
383 	int error;
384 	int allerror;
385 	int vflags;
386 	uint32_t eofentry;
387 	uint32_t chaintofree;
388 	daddr_t bn;
389 	int boff;
390 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
391 	struct buf *bp;
392 	struct msdosfsmount *pmp = dep->de_pmp;
393 
394 #ifdef MSDOSFS_DEBUG
395 	printf("detrunc(): file %s, length %ld, flags %d\n", dep->de_Name, length, flags);
396 #endif
397 
398 	/*
399 	 * Disallow attempts to truncate the root directory since it is of
400 	 * fixed size.  That's just the way dos filesystems are.  We use
401 	 * the VROOT bit in the vnode because checking for the directory
402 	 * bit and a startcluster of 0 in the denode is not adequate to
403 	 * recognize the root directory at this point in a file or
404 	 * directory's life.
405 	 */
406 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
407 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
408 		    (long)dep->de_dirclust, (long)dep->de_diroffset);
409 		return (EINVAL);
410 	}
411 
412 	uvm_vnp_setsize(DETOV(dep), length);
413 
414 	if (dep->de_FileSize < length)
415 		return (deextend(dep, length, cred));
416 
417 	/*
418 	 * If the desired length is 0 then remember the starting cluster of
419 	 * the file and set the StartCluster field in the directory entry
420 	 * to 0.  If the desired length is not zero, then get the number of
421 	 * the last cluster in the shortened file.  Then get the number of
422 	 * the first cluster in the part of the file that is to be freed.
423 	 * Then set the next cluster pointer in the last cluster of the
424 	 * file to CLUST_EOFE.
425 	 */
426 	if (length == 0) {
427 		chaintofree = dep->de_StartCluster;
428 		dep->de_StartCluster = 0;
429 		eofentry = ~0;
430 	} else {
431 		error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
432 			       &eofentry, 0);
433 		if (error) {
434 #ifdef MSDOSFS_DEBUG
435 			printf("detrunc(): pcbmap fails %d\n", error);
436 #endif
437 			return (error);
438 		}
439 	}
440 
441 	fc_purge(dep, de_clcount(pmp, length));
442 
443 	/*
444 	 * If the new length is not a multiple of the cluster size then we
445 	 * must zero the tail end of the new last cluster in case it
446 	 * becomes part of the file again because of a seek.
447 	 */
448 	if ((boff = length & pmp->pm_crbomask) != 0) {
449 		if (isadir) {
450 			bn = cntobn(pmp, eofentry);
451 			error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
452 			    NOCRED, &bp);
453 		} else {
454 			bn = de_blk(pmp, length);
455 			error = bread(DETOV(dep), bn, pmp->pm_bpcluster,
456 			    NOCRED, &bp);
457 		}
458 		if (error) {
459 			brelse(bp);
460 #ifdef MSDOSFS_DEBUG
461 			printf("detrunc(): bread fails %d\n", error);
462 #endif
463 			return (error);
464 		}
465 		uvm_vnp_uncache(DETOV(dep));
466 		/*
467 		 * is this the right place for it?
468 		 */
469 		bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
470 		if (flags & IO_SYNC)
471 			bwrite(bp);
472 		else
473 			bdwrite(bp);
474 	}
475 
476 	/*
477 	 * Write out the updated directory entry.  Even if the update fails
478 	 * we free the trailing clusters.
479 	 */
480 	dep->de_FileSize = length;
481 	if (!isadir)
482 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
483 	vflags = (length > 0 ? V_SAVE : 0) | V_SAVEMETA;
484 	vinvalbuf(DETOV(dep), vflags, cred, p, 0, 0);
485 	allerror = deupdat(dep, 1);
486 #ifdef MSDOSFS_DEBUG
487 	printf("detrunc(): allerror %d, eofentry %d\n",
488 	       allerror, eofentry);
489 #endif
490 
491 	/*
492 	 * If we need to break the cluster chain for the file then do it
493 	 * now.
494 	 */
495 	if (eofentry != ~0) {
496 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
497 				 &chaintofree, CLUST_EOFE);
498 		if (error) {
499 #ifdef MSDOSFS_DEBUG
500 			printf("detrunc(): fatentry errors %d\n", error);
501 #endif
502 			return (error);
503 		}
504 		fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
505 			    eofentry);
506 	}
507 
508 	/*
509 	 * Now free the clusters removed from the file because of the
510 	 * truncation.
511 	 */
512 	if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
513 		freeclusterchain(pmp, chaintofree);
514 
515 	return (allerror);
516 }
517 
518 /*
519  * Extend the file described by dep to length specified by length.
520  */
521 int
deextend(dep,length,cred)522 deextend(dep, length, cred)
523 	struct denode *dep;
524 	uint32_t length;
525 	struct ucred *cred;
526 {
527 	struct msdosfsmount *pmp = dep->de_pmp;
528 	uint32_t count;
529 	int error;
530 
531 	/*
532 	 * The root of a DOS filesystem cannot be extended.
533 	 */
534 	if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
535 		return (EINVAL);
536 
537 	/*
538 	 * Directories cannot be extended.
539 	 */
540 	if (dep->de_Attributes & ATTR_DIRECTORY)
541 		return (EISDIR);
542 
543 	if (length <= dep->de_FileSize)
544 		panic("deextend: file too large");
545 
546 	/*
547 	 * Compute the number of clusters to allocate.
548 	 */
549 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
550 	if (count > 0) {
551 		if (count > pmp->pm_freeclustercount)
552 			return (ENOSPC);
553 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
554 		if (error) {
555 			/* truncate the added clusters away again */
556 			(void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
557 			return (error);
558 		}
559 	}
560 
561 	dep->de_FileSize = length;
562 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
563 	return (deupdat(dep, 1));
564 }
565 
566 /*
567  * Move a denode to its correct hash queue after the file it represents has
568  * been moved to a new directory.
569  */
570 void
reinsert(dep)571 reinsert(dep)
572 	struct denode *dep;
573 {
574 	/*
575 	 * Fix up the denode cache.  If the denode is for a directory,
576 	 * there is nothing to do since the hash is based on the starting
577 	 * cluster of the directory file and that hasn't changed.  If for a
578 	 * file the hash is based on the location of the directory entry,
579 	 * so we must remove it from the cache and re-enter it with the
580 	 * hash based on the new location of the directory entry.
581 	 */
582 	if (dep->de_Attributes & ATTR_DIRECTORY)
583 		return;
584 	msdosfs_hashrem(dep);
585 	msdosfs_hashins(dep);
586 }
587 
588 int
msdosfs_reclaim(v)589 msdosfs_reclaim(v)
590 	void *v;
591 {
592 	struct vop_reclaim_args /* {
593 		struct vnode *a_vp;
594 	} */ *ap = v;
595 	struct vnode *vp = ap->a_vp;
596 	struct denode *dep = VTODE(vp);
597 	extern int prtactive;
598 
599 #ifdef MSDOSFS_DEBUG
600 	printf("msdosfs_reclaim(): dep %08x, file %s, refcnt %d\n",
601 	    dep, dep->de_Name, dep->de_refcnt);
602 #endif
603 
604 	if (prtactive && vp->v_usecount != 0)
605 		vprint("msdosfs_reclaim(): pushing active", vp);
606 	/*
607 	 * Remove the denode from its hash chain.
608 	 */
609 	msdosfs_hashrem(dep);
610 	/*
611 	 * Purge old data structures associated with the denode.
612 	 */
613 	cache_purge(vp);
614 	if (dep->de_devvp) {
615 		vrele(dep->de_devvp);
616 		dep->de_devvp = 0;
617 	}
618 #if 0 /* XXX */
619 	dep->de_flag = 0;
620 #endif
621 	FREE(dep, M_MSDOSFSNODE);
622 	vp->v_data = NULL;
623 	return (0);
624 }
625 
626 int
msdosfs_inactive(v)627 msdosfs_inactive(v)
628 	void *v;
629 {
630 	struct vop_inactive_args /* {
631 		struct vnode *a_vp;
632 		struct proc *a_p;
633 	} */ *ap = v;
634 	struct vnode *vp = ap->a_vp;
635 	struct denode *dep = VTODE(vp);
636 	struct proc *p = ap->a_p;
637 	int error;
638 	extern int prtactive;
639 
640 #ifdef MSDOSFS_DEBUG
641 	printf("msdosfs_inactive(): dep %08x, de_Name[0] %x\n", dep, dep->de_Name[0]);
642 #endif
643 
644 	if (prtactive && vp->v_usecount != 0)
645 		vprint("msdosfs_inactive(): pushing active", vp);
646 
647 	error = 0;
648 
649 	/*
650 	 * Get rid of denodes related to stale file handles.
651 	 */
652 	if (dep->de_Name[0] == SLOT_DELETED)
653 		goto out;
654 
655 	/*
656 	 * If the file has been deleted and it is on a read/write
657 	 * filesystem, then truncate the file, and mark the directory slot
658 	 * as empty.  (This may not be necessary for the dos filesystem.)
659 	 */
660 #ifdef MSDOSFS_DEBUG
661 	printf("msdosfs_inactive(): dep %08x, refcnt %d, mntflag %x, MNT_RDONLY %x\n",
662 	       dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
663 #endif
664 	if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
665 		error = detrunc(dep, (uint32_t)0, 0, NOCRED, NULL);
666 		dep->de_Name[0] = SLOT_DELETED;
667 	}
668 	deupdat(dep, 0);
669 
670 out:
671 	VOP_UNLOCK(vp, 0, p);
672 	/*
673 	 * If we are done with the denode, reclaim it
674 	 * so that it can be reused immediately.
675 	 */
676 #ifdef MSDOSFS_DEBUG
677 	printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
678 	       dep->de_Name[0]);
679 #endif
680 	if (dep->de_Name[0] == SLOT_DELETED)
681 		vrecycle(vp, (struct simplelock *)0, p);
682 	return (error);
683 }
684