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