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