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