Make a DFly buildworld work on a FreeBSD-4.x system again by fixing
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_denode.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/msdosfs/msdosfs_denode.c,v 1.47.2.3 2002/08/22 16:20:15 trhodes Exp $ */
8e005a45 2/* $DragonFly: src/sys/vfs/msdosfs/msdosfs_denode.c,v 1.16 2004/10/05 03:24:30 dillon Exp $ */
984263bc
MD
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
1f2de5d4
MD
64#include "bpb.h"
65#include "msdosfsmount.h"
66#include "direntry.h"
67#include "denode.h"
68#include "fat.h"
984263bc 69
3446c007
MD
70static int msdosfs_hashins (struct denode *dep);
71static void msdosfs_hashrem (struct denode *dep);
72
984263bc
MD
73static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
74
75static struct denode **dehashtbl;
76static u_long dehash; /* size of hash table - 1 */
77#define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \
78 sizeof(struct direntry)) & dehash])
8a8d5d85 79static struct lwkt_token dehash_token;
984263bc
MD
80
81union _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
98static struct denode *
3446c007 99 msdosfs_hashget (dev_t dev, u_long dirclust, u_long diroff);
984263bc
MD
100
101/*ARGSUSED*/
102int
4625f023 103msdosfs_init(struct vfsconf *vfsp)
984263bc 104{
3446c007
MD
105 dehash = 16;
106 while (dehash < desiredvnodes)
107 dehash <<= 1;
108 dehashtbl = malloc(sizeof(void *) * dehash, M_MSDOSFSMNT,
109 M_WAITOK|M_ZERO);
110 --dehash;
41a01a4d 111 lwkt_token_init(&dehash_token);
984263bc
MD
112 return (0);
113}
114
115int
4625f023 116msdosfs_uninit(struct vfsconf *vfsp)
984263bc
MD
117{
118
119 if (dehashtbl)
120 free(dehashtbl, M_MSDOSFSMNT);
121 return (0);
122}
123
124static struct denode *
4625f023 125msdosfs_hashget(dev_t dev, u_long dirclust, u_long diroff)
984263bc 126{
dadab5e9 127 struct thread *td = curthread; /* XXX */
984263bc 128 struct denode *dep;
41a01a4d
MD
129 lwkt_tokref ilock;
130 lwkt_tokref vlock;
984263bc
MD
131 struct vnode *vp;
132
41a01a4d 133 lwkt_gettoken(&ilock, &dehash_token);
984263bc 134loop:
984263bc 135 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
41a01a4d
MD
136 if (dirclust != dep->de_dirclust
137 || diroff != dep->de_diroffset
138 || dev != dep->de_dev
139 || dep->de_refcnt == 0) {
140 continue;
141 }
142 vp = DETOV(dep);
143 lwkt_gettoken(&vlock, vp->v_interlock);
3446c007 144
41a01a4d
MD
145 /*
146 * We must check to see if the inode has been ripped
147 * out from under us after blocking.
148 */
149 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
150 if (dirclust == dep->de_dirclust
151 && diroff == dep->de_diroffset
152 && dev == dep->de_dev
153 && dep->de_refcnt != 0) {
154 break;
4954c633 155 }
984263bc 156 }
41a01a4d
MD
157 if (dep == NULL || DETOV(dep) != vp) {
158 lwkt_reltoken(&vlock);
159 goto loop;
160 }
161 if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td))
162 goto loop;
163 lwkt_reltoken(&ilock);
164 return (dep);
984263bc 165 }
41a01a4d 166 lwkt_reltoken(&ilock);
984263bc
MD
167 return (NULL);
168}
169
3446c007
MD
170static
171int
4625f023 172msdosfs_hashins(struct denode *dep)
984263bc
MD
173{
174 struct denode **depp, *deq;
41a01a4d 175 lwkt_tokref ilock;
984263bc 176
41a01a4d 177 lwkt_gettoken(&ilock, &dehash_token);
984263bc 178 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
3446c007
MD
179 while ((deq = *depp) != NULL) {
180 if (deq->de_dev == dep->de_dev &&
181 deq->de_dirclust == dep->de_dirclust &&
182 deq->de_diroffset == dep->de_diroffset) {
183 lwkt_reltoken(&ilock);
184 return(EBUSY);
185 }
186 depp = &deq->de_next;
187 }
188 dep->de_next = NULL;
984263bc 189 *depp = dep;
41a01a4d 190 lwkt_reltoken(&ilock);
3446c007 191 return(0);
984263bc
MD
192}
193
3446c007
MD
194static
195void
4625f023 196msdosfs_hashrem(struct denode *dep)
984263bc 197{
3446c007 198 struct denode **depp, *deq;
41a01a4d 199 lwkt_tokref ilock;
984263bc 200
41a01a4d 201 lwkt_gettoken(&ilock, &dehash_token);
3446c007
MD
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;
984263bc 210 dep->de_next = NULL;
3446c007
MD
211 lwkt_reltoken(&ilock);
212}
213
214void
215msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
216{
217 lwkt_tokref ilock;
218
219 lwkt_gettoken(&ilock, &dehash_token);
220 msdosfs_hashrem(ip);
221 ip->de_dirclust = new_dirclust;
222 ip->de_diroffset = new_diroffset;
223 msdosfs_hashins(ip);
41a01a4d 224 lwkt_reltoken(&ilock);
984263bc
MD
225}
226
227/*
228 * If deget() succeeds it returns with the gotten denode locked().
229 *
230 * pmp - address of msdosfsmount structure of the filesystem containing
231 * the denode of interest. The pm_dev field and the address of
232 * the msdosfsmount structure are used.
233 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
234 * diroffset is relative to the beginning of the root directory,
235 * otherwise it is cluster relative.
236 * diroffset - offset past begin of cluster of denode we want
237 * depp - returns the address of the gotten denode.
238 */
239int
4625f023
CP
240deget(struct msdosfsmount *pmp, /* so we know the maj/min number */
241 u_long dirclust, /* cluster this dir entry came from */
242 u_long diroffset, /* index of entry within the cluster */
243 struct denode **depp) /* returns the addr of the gotten denode */
984263bc 244{
dadab5e9 245 struct thread *td = curthread; /* XXX */
984263bc
MD
246 int error;
247 dev_t dev = pmp->pm_dev;
248 struct mount *mntp = pmp->pm_mountp;
249 struct direntry *direntptr;
250 struct denode *ldep;
251 struct vnode *nvp;
252 struct buf *bp;
984263bc
MD
253 struct timeval tv;
254
255#ifdef MSDOSFS_DEBUG
256 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
257 pmp, dirclust, diroffset, depp);
258#endif
259
260 /*
261 * On FAT32 filesystems, root is a (more or less) normal
262 * directory
263 */
264 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
265 dirclust = pmp->pm_rootdirblk;
266
3446c007 267again:
984263bc
MD
268 /*
269 * See if the denode is in the denode cache. Use the location of
270 * the directory entry to compute the hash value. For subdir use
271 * address of "." entry. For root dir (if not FAT32) use cluster
272 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
273 *
274 * NOTE: The check for de_refcnt > 0 below insures the denode being
275 * examined does not represent an unlinked but still open file.
276 * These files are not to be accessible even when the directory
277 * entry that represented the file happens to be reused while the
278 * deleted file is still open.
279 */
280 ldep = msdosfs_hashget(dev, dirclust, diroffset);
281 if (ldep) {
282 *depp = ldep;
283 return (0);
284 }
285
286 /*
287 * Do the MALLOC before the getnewvnode since doing so afterward
288 * might cause a bogus v_data pointer to get dereferenced
289 * elsewhere if MALLOC should block.
290 */
291 MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK);
292
293 /*
294 * Directory entry was not in cache, have to create a vnode and
295 * copy it from the passed disk buffer.
296 */
3446c007 297
597aea93 298 /* getnewvnode() does a vref() on the vnode */
3446c007
MD
299 error = getnewvnode(VT_MSDOSFS, mntp, mntp->mnt_vn_ops, &nvp,
300 VLKTIMEOUT, 0);
984263bc
MD
301 if (error) {
302 *depp = NULL;
303 FREE(ldep, M_MSDOSFSNODE);
304 return error;
305 }
3446c007
MD
306
307 /*
308 * Lock the denode so that it can't be accessed until we've read
309 * it in and have done what we need to it.
310 */
311 if (lockmgr(&nvp->v_lock, LK_EXCLUSIVE, NULL, td))
312 panic("deget: unexpected lock failure");
313
984263bc 314 bzero((caddr_t)ldep, sizeof *ldep);
984263bc
MD
315 ldep->de_vnode = nvp;
316 ldep->de_flag = 0;
317 ldep->de_devvp = 0;
318 ldep->de_dev = dev;
319 ldep->de_dirclust = dirclust;
320 ldep->de_diroffset = diroffset;
321 fc_purge(ldep, 0); /* init the fat cache for this denode */
322
323 /*
3446c007
MD
324 * Insert the denode into the hash queue. If a collision occurs
325 * throw away the vnode and try again.
984263bc 326 */
3446c007
MD
327 if (msdosfs_hashins(ldep) != 0) {
328 printf("debug: msdosfs: hashins collision, retrying\n");
329 vput(nvp);
330 free(ldep, M_MSDOSFSNODE);
331 goto again;
332 }
333 nvp->v_data = ldep;
984263bc
MD
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
384 vput(nvp);
385 *depp = NULL;
386 return (error);
387 }
388 DE_INTERNALIZE(ldep, direntptr);
389 brelse(bp);
390 }
391
392 /*
393 * Fill in a few fields of the vnode and finish filling in the
394 * denode. Then return the address of the found denode.
395 */
396 if (ldep->de_Attributes & ATTR_DIRECTORY) {
397 /*
398 * Since DOS directory entries that describe directories
399 * have 0 in the filesize field, we take this opportunity
400 * to find out the length of the directory and plug it into
401 * the denode structure.
402 */
403 u_long size;
404
405 /*
406 * XXX Sometimes, these arrives that . entry have cluster
407 * number 0, when it shouldn't. Use real cluster number
408 * instead of what is written in directory entry.
409 */
410 if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
411 printf("deget(): . entry at clust %ld != %ld\n",
412 dirclust, ldep->de_StartCluster);
413 ldep->de_StartCluster = dirclust;
414 }
415
416 nvp->v_type = VDIR;
417 if (ldep->de_StartCluster != MSDOSFSROOT) {
418 error = pcbmap(ldep, 0xffff, 0, &size, 0);
419 if (error == E2BIG) {
420 ldep->de_FileSize = de_cn2off(pmp, size);
421 error = 0;
422 } else
423 printf("deget(): pcbmap returned %d\n", error);
424 }
425 } else
426 nvp->v_type = VREG;
427 getmicrouptime(&tv);
428 SETHIGH(ldep->de_modrev, tv.tv_sec);
429 SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
430 ldep->de_devvp = pmp->pm_devvp;
597aea93 431 vref(ldep->de_devvp);
984263bc
MD
432 *depp = ldep;
433 return (0);
434}
435
436int
4625f023 437deupdat(struct denode *dep, int waitfor)
984263bc
MD
438{
439 int error;
440 struct buf *bp;
441 struct direntry *dirp;
442 struct timespec ts;
443
444 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
445 return (0);
446 getnanotime(&ts);
447 DETIMES(dep, &ts, &ts, &ts);
448 if ((dep->de_flag & DE_MODIFIED) == 0)
449 return (0);
450 dep->de_flag &= ~DE_MODIFIED;
451 if (dep->de_Attributes & ATTR_DIRECTORY)
452 return (0);
453 if (dep->de_refcnt <= 0)
454 return (0);
455 error = readde(dep, &bp, &dirp);
456 if (error)
457 return (error);
458 DE_EXTERNALIZE(dirp, dep);
459 if (waitfor)
460 return (bwrite(bp));
461 else {
462 bdwrite(bp);
463 return (0);
464 }
465}
466
467/*
468 * Truncate the file described by dep to the length specified by length.
469 */
470int
4625f023 471detrunc(struct denode *dep, u_long length, int flags, struct thread *td)
984263bc
MD
472{
473 int error;
474 int allerror;
475 u_long eofentry;
476 u_long chaintofree;
477 daddr_t bn;
478 int boff;
479 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
480 struct buf *bp;
481 struct msdosfsmount *pmp = dep->de_pmp;
482
483#ifdef MSDOSFS_DEBUG
484 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
485#endif
486
487 /*
488 * Disallow attempts to truncate the root directory since it is of
489 * fixed size. That's just the way dos filesystems are. We use
490 * the VROOT bit in the vnode because checking for the directory
491 * bit and a startcluster of 0 in the denode is not adequate to
492 * recognize the root directory at this point in a file or
493 * directory's life.
494 */
495 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
496 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
497 dep->de_dirclust, dep->de_diroffset);
498 return (EINVAL);
499 }
500
501
502 if (dep->de_FileSize < length) {
503 vnode_pager_setsize(DETOV(dep), length);
3b568787 504 return deextend(dep, length);
984263bc
MD
505 }
506
507 /*
508 * If the desired length is 0 then remember the starting cluster of
509 * the file and set the StartCluster field in the directory entry
510 * to 0. If the desired length is not zero, then get the number of
511 * the last cluster in the shortened file. Then get the number of
512 * the first cluster in the part of the file that is to be freed.
513 * Then set the next cluster pointer in the last cluster of the
514 * file to CLUST_EOFE.
515 */
516 if (length == 0) {
517 chaintofree = dep->de_StartCluster;
518 dep->de_StartCluster = 0;
519 eofentry = ~0;
520 } else {
521 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
522 &eofentry, 0);
523 if (error) {
524#ifdef MSDOSFS_DEBUG
525 printf("detrunc(): pcbmap fails %d\n", error);
526#endif
527 return (error);
528 }
529 }
530
531 fc_purge(dep, de_clcount(pmp, length));
532
533 /*
534 * If the new length is not a multiple of the cluster size then we
535 * must zero the tail end of the new last cluster in case it
536 * becomes part of the file again because of a seek.
537 */
538 if ((boff = length & pmp->pm_crbomask) != 0) {
539 if (isadir) {
540 bn = cntobn(pmp, eofentry);
3b568787 541 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
984263bc
MD
542 } else {
543 bn = de_blk(pmp, length);
3b568787 544 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, &bp);
984263bc
MD
545 }
546 if (error) {
547 brelse(bp);
548#ifdef MSDOSFS_DEBUG
549 printf("detrunc(): bread fails %d\n", error);
550#endif
551 return (error);
552 }
553 /*
554 * is this the right place for it?
555 */
556 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
557 if (flags & IO_SYNC)
558 bwrite(bp);
559 else
560 bdwrite(bp);
561 }
562
563 /*
564 * Write out the updated directory entry. Even if the update fails
565 * we free the trailing clusters.
566 */
567 dep->de_FileSize = length;
568 if (!isadir)
569 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
3b568787 570 allerror = vtruncbuf(DETOV(dep), td, length, pmp->pm_bpcluster);
984263bc
MD
571#ifdef MSDOSFS_DEBUG
572 if (allerror)
573 printf("detrunc(): vtruncbuf error %d\n", allerror);
574#endif
575 error = deupdat(dep, 1);
576 if (error && (allerror == 0))
577 allerror = error;
578#ifdef MSDOSFS_DEBUG
579 printf("detrunc(): allerror %d, eofentry %lu\n",
580 allerror, eofentry);
581#endif
582
583 /*
584 * If we need to break the cluster chain for the file then do it
585 * now.
586 */
587 if (eofentry != ~0) {
588 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
589 &chaintofree, CLUST_EOFE);
590 if (error) {
591#ifdef MSDOSFS_DEBUG
592 printf("detrunc(): fatentry errors %d\n", error);
593#endif
594 return (error);
595 }
596 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
597 eofentry);
598 }
599
600 /*
601 * Now free the clusters removed from the file because of the
602 * truncation.
603 */
604 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
605 freeclusterchain(pmp, chaintofree);
606
607 return (allerror);
608}
609
610/*
611 * Extend the file described by dep to length specified by length.
612 */
613int
4625f023 614deextend(struct denode *dep, u_long length)
984263bc
MD
615{
616 struct msdosfsmount *pmp = dep->de_pmp;
617 u_long count;
618 int error;
619
620 /*
621 * The root of a DOS filesystem cannot be extended.
622 */
623 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
624 return (EINVAL);
625
626 /*
627 * Directories cannot be extended.
628 */
629 if (dep->de_Attributes & ATTR_DIRECTORY)
630 return (EISDIR);
631
632 if (length <= dep->de_FileSize)
633 panic("deextend: file too large");
634
635 /*
636 * Compute the number of clusters to allocate.
637 */
638 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
639 if (count > 0) {
640 if (count > pmp->pm_freeclustercount)
641 return (ENOSPC);
642 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
643 if (error) {
644 /* truncate the added clusters away again */
3b568787 645 (void) detrunc(dep, dep->de_FileSize, 0, NULL);
984263bc
MD
646 return (error);
647 }
648 }
649 dep->de_FileSize = length;
650 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
651 return (deupdat(dep, 1));
652}
653
4625f023
CP
654/*
655 * msdosfs_reclaim(struct vnode *a_vp)
656 */
984263bc 657int
4625f023 658msdosfs_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
659{
660 struct vnode *vp = ap->a_vp;
661 struct denode *dep = VTODE(vp);
662
663#ifdef MSDOSFS_DEBUG
664 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
3446c007 665 dep, dep ? dep->de_Name : "?", dep ? dep->de_refcnt : -1);
984263bc
MD
666#endif
667
668 if (prtactive && vp->v_usecount != 0)
669 vprint("msdosfs_reclaim(): pushing active", vp);
670 /*
3446c007
MD
671 * Remove the denode from its hash chain and purge namecache
672 * data associated with the vnode.
984263bc 673 */
8e005a45 674 cache_inval_vp(vp, CINV_SELF);
984263bc 675 vp->v_data = NULL;
3446c007
MD
676 if (dep) {
677 msdosfs_hashrem(dep);
678 if (dep->de_devvp) {
679 vrele(dep->de_devvp);
680 dep->de_devvp = 0;
681 }
682 free(dep, M_MSDOSFSNODE);
683 }
984263bc
MD
684 return (0);
685}
686
4625f023
CP
687/*
688 * msdosfs_inactive(struct vnode *a_vp, struct thread *a_td)
689 */
984263bc 690int
4625f023 691msdosfs_inactive(struct vop_inactive_args *ap)
984263bc
MD
692{
693 struct vnode *vp = ap->a_vp;
694 struct denode *dep = VTODE(vp);
984263bc
MD
695 int error = 0;
696
697#ifdef MSDOSFS_DEBUG
698 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
699#endif
700
701 if (prtactive && vp->v_usecount != 0)
702 vprint("msdosfs_inactive(): pushing active", vp);
703
704 /*
705 * Ignore denodes related to stale file handles.
706 */
707 if (dep->de_Name[0] == SLOT_DELETED)
708 goto out;
709
710 /*
711 * If the file has been deleted and it is on a read/write
712 * filesystem, then truncate the file, and mark the directory slot
713 * as empty. (This may not be necessary for the dos filesystem.)
714 */
715#ifdef MSDOSFS_DEBUG
716 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
717 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
718#endif
719 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3b568787 720 error = detrunc(dep, (u_long) 0, 0, ap->a_td);
984263bc
MD
721 dep->de_flag |= DE_UPDATE;
722 dep->de_Name[0] = SLOT_DELETED;
723 }
724 deupdat(dep, 0);
725
726out:
41a01a4d 727 VOP_UNLOCK(vp, NULL, 0, ap->a_td);
984263bc
MD
728 /*
729 * If we are done with the denode, reclaim it
730 * so that it can be reused immediately.
731 */
732#ifdef MSDOSFS_DEBUG
733 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
734 dep->de_Name[0]);
735#endif
736 if (dep->de_Name[0] == SLOT_DELETED)
8a8d5d85 737 vrecycle(vp, NULL, ap->a_td);
984263bc
MD
738 return (error);
739}