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