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