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.16 2004/10/05 03:24:30 dillon Exp $ */
3 /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
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.
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>
59 #include <sys/vnode.h>
62 #include <vm/vm_extern.h>
65 #include "msdosfsmount.h"
70 static int msdosfs_hashins (struct denode *dep);
71 static void msdosfs_hashrem (struct denode *dep);
73 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
75 static struct denode **dehashtbl;
76 static u_long dehash; /* size of hash table - 1 */
77 #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \
78 sizeof(struct direntry)) & dehash])
79 static struct lwkt_token dehash_token;
85 #define SETHIGH(q, h) { \
88 tmp.val[_QUAD_HIGHWORD] = (h); \
91 #define SETLOW(q, l) { \
94 tmp.val[_QUAD_LOWWORD] = (l); \
98 static struct denode *
99 msdosfs_hashget (dev_t dev, u_long dirclust, u_long diroff);
103 msdosfs_init(struct vfsconf *vfsp)
106 while (dehash < desiredvnodes)
108 dehashtbl = malloc(sizeof(void *) * dehash, M_MSDOSFSMNT,
111 lwkt_token_init(&dehash_token);
116 msdosfs_uninit(struct vfsconf *vfsp)
120 free(dehashtbl, M_MSDOSFSMNT);
124 static struct denode *
125 msdosfs_hashget(dev_t dev, u_long dirclust, u_long diroff)
127 struct thread *td = curthread; /* XXX */
133 lwkt_gettoken(&ilock, &dehash_token);
135 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
136 if (dirclust != dep->de_dirclust
137 || diroff != dep->de_diroffset
138 || dev != dep->de_dev
139 || dep->de_refcnt == 0) {
143 lwkt_gettoken(&vlock, vp->v_interlock);
146 * We must check to see if the inode has been ripped
147 * out from under us after blocking.
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) {
157 if (dep == NULL || DETOV(dep) != vp) {
158 lwkt_reltoken(&vlock);
161 if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td))
163 lwkt_reltoken(&ilock);
166 lwkt_reltoken(&ilock);
172 msdosfs_hashins(struct denode *dep)
174 struct denode **depp, *deq;
177 lwkt_gettoken(&ilock, &dehash_token);
178 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
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);
186 depp = &deq->de_next;
190 lwkt_reltoken(&ilock);
196 msdosfs_hashrem(struct denode *dep)
198 struct denode **depp, *deq;
201 lwkt_gettoken(&ilock, &dehash_token);
202 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
203 while ((deq = *depp) != NULL) {
206 depp = &deq->de_next;
208 KKASSERT(dep == deq);
209 *depp = dep->de_next;
211 lwkt_reltoken(&ilock);
215 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
219 lwkt_gettoken(&ilock, &dehash_token);
221 ip->de_dirclust = new_dirclust;
222 ip->de_diroffset = new_diroffset;
224 lwkt_reltoken(&ilock);
228 * If deget() succeeds it returns with the gotten denode locked().
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.
240 deget(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 */
245 struct thread *td = curthread; /* XXX */
247 dev_t dev = pmp->pm_dev;
248 struct mount *mntp = pmp->pm_mountp;
249 struct direntry *direntptr;
256 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
257 pmp, dirclust, diroffset, depp);
261 * On FAT32 filesystems, root is a (more or less) normal
264 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
265 dirclust = pmp->pm_rootdirblk;
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
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.
280 ldep = msdosfs_hashget(dev, dirclust, diroffset);
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.
291 MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK);
294 * Directory entry was not in cache, have to create a vnode and
295 * copy it from the passed disk buffer.
298 /* getnewvnode() does a vref() on the vnode */
299 error = getnewvnode(VT_MSDOSFS, mntp, mntp->mnt_vn_ops, &nvp,
303 FREE(ldep, M_MSDOSFSNODE);
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.
311 if (lockmgr(&nvp->v_lock, LK_EXCLUSIVE, NULL, td))
312 panic("deget: unexpected lock failure");
314 bzero((caddr_t)ldep, sizeof *ldep);
315 ldep->de_vnode = nvp;
319 ldep->de_dirclust = dirclust;
320 ldep->de_diroffset = diroffset;
321 fc_purge(ldep, 0); /* init the fat cache for this denode */
324 * Insert the denode into the hash queue. If a collision occurs
325 * throw away the vnode and try again.
327 if (msdosfs_hashins(ldep) != 0) {
328 printf("debug: msdosfs: hashins collision, retrying\n");
330 free(ldep, M_MSDOSFSNODE);
337 * Copy the directory entry into the denode area of the vnode.
339 if ((dirclust == MSDOSFSROOT
340 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
341 && diroffset == MSDOSFSROOT_OFS) {
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.
349 nvp->v_flag |= VROOT; /* should be further down XXX */
351 ldep->de_Attributes = ATTR_DIRECTORY;
352 ldep->de_LowerCase = 0;
354 ldep->de_StartCluster = pmp->pm_rootdirblk;
355 /* de_FileSize will be filled in further down */
357 ldep->de_StartCluster = MSDOSFSROOT;
358 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
361 * fill in time and date so that dos2unixtime() doesn't
362 * spit up when called from msdosfs_getattr() with root
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);
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 */
375 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
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.
382 ldep->de_Name[0] = SLOT_DELETED;
388 DE_INTERNALIZE(ldep, direntptr);
393 * Fill in a few fields of the vnode and finish filling in the
394 * denode. Then return the address of the found denode.
396 if (ldep->de_Attributes & ATTR_DIRECTORY) {
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.
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.
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;
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);
423 printf("deget(): pcbmap returned %d\n", error);
428 SETHIGH(ldep->de_modrev, tv.tv_sec);
429 SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
430 ldep->de_devvp = pmp->pm_devvp;
431 vref(ldep->de_devvp);
437 deupdat(struct denode *dep, int waitfor)
441 struct direntry *dirp;
444 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
447 DETIMES(dep, &ts, &ts, &ts);
448 if ((dep->de_flag & DE_MODIFIED) == 0)
450 dep->de_flag &= ~DE_MODIFIED;
451 if (dep->de_Attributes & ATTR_DIRECTORY)
453 if (dep->de_refcnt <= 0)
455 error = readde(dep, &bp, &dirp);
458 DE_EXTERNALIZE(dirp, dep);
468 * Truncate the file described by dep to the length specified by length.
471 detrunc(struct denode *dep, u_long length, int flags, struct thread *td)
479 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
481 struct msdosfsmount *pmp = dep->de_pmp;
484 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
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
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);
502 if (dep->de_FileSize < length) {
503 vnode_pager_setsize(DETOV(dep), length);
504 return deextend(dep, length);
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.
517 chaintofree = dep->de_StartCluster;
518 dep->de_StartCluster = 0;
521 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
525 printf("detrunc(): pcbmap fails %d\n", error);
531 fc_purge(dep, de_clcount(pmp, length));
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.
538 if ((boff = length & pmp->pm_crbomask) != 0) {
540 bn = cntobn(pmp, eofentry);
541 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
543 bn = de_blk(pmp, length);
544 error = bread(DETOV(dep), bn, pmp->pm_bpcluster, &bp);
549 printf("detrunc(): bread fails %d\n", error);
554 * is this the right place for it?
556 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
564 * Write out the updated directory entry. Even if the update fails
565 * we free the trailing clusters.
567 dep->de_FileSize = length;
569 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
570 allerror = vtruncbuf(DETOV(dep), td, length, pmp->pm_bpcluster);
573 printf("detrunc(): vtruncbuf error %d\n", allerror);
575 error = deupdat(dep, 1);
576 if (error && (allerror == 0))
579 printf("detrunc(): allerror %d, eofentry %lu\n",
584 * If we need to break the cluster chain for the file then do it
587 if (eofentry != ~0) {
588 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
589 &chaintofree, CLUST_EOFE);
592 printf("detrunc(): fatentry errors %d\n", error);
596 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
601 * Now free the clusters removed from the file because of the
604 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
605 freeclusterchain(pmp, chaintofree);
611 * Extend the file described by dep to length specified by length.
614 deextend(struct denode *dep, u_long length)
616 struct msdosfsmount *pmp = dep->de_pmp;
621 * The root of a DOS filesystem cannot be extended.
623 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
627 * Directories cannot be extended.
629 if (dep->de_Attributes & ATTR_DIRECTORY)
632 if (length <= dep->de_FileSize)
633 panic("deextend: file too large");
636 * Compute the number of clusters to allocate.
638 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
640 if (count > pmp->pm_freeclustercount)
642 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
644 /* truncate the added clusters away again */
645 (void) detrunc(dep, dep->de_FileSize, 0, NULL);
649 dep->de_FileSize = length;
650 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
651 return (deupdat(dep, 1));
655 * msdosfs_reclaim(struct vnode *a_vp)
658 msdosfs_reclaim(struct vop_reclaim_args *ap)
660 struct vnode *vp = ap->a_vp;
661 struct denode *dep = VTODE(vp);
664 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
665 dep, dep ? dep->de_Name : "?", dep ? dep->de_refcnt : -1);
668 if (prtactive && vp->v_usecount != 0)
669 vprint("msdosfs_reclaim(): pushing active", vp);
671 * Remove the denode from its hash chain and purge namecache
672 * data associated with the vnode.
674 cache_inval_vp(vp, CINV_SELF);
677 msdosfs_hashrem(dep);
679 vrele(dep->de_devvp);
682 free(dep, M_MSDOSFSNODE);
688 * msdosfs_inactive(struct vnode *a_vp, struct thread *a_td)
691 msdosfs_inactive(struct vop_inactive_args *ap)
693 struct vnode *vp = ap->a_vp;
694 struct denode *dep = VTODE(vp);
698 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
701 if (prtactive && vp->v_usecount != 0)
702 vprint("msdosfs_inactive(): pushing active", vp);
705 * Ignore denodes related to stale file handles.
707 if (dep->de_Name[0] == SLOT_DELETED)
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.)
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);
719 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
720 error = detrunc(dep, (u_long) 0, 0, ap->a_td);
721 dep->de_flag |= DE_UPDATE;
722 dep->de_Name[0] = SLOT_DELETED;
727 VOP_UNLOCK(vp, NULL, 0, ap->a_td);
729 * If we are done with the denode, reclaim it
730 * so that it can be reused immediately.
733 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
736 if (dep->de_Name[0] == SLOT_DELETED)
737 vrecycle(vp, NULL, ap->a_td);