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