1 /* $FreeBSD: src/sys/msdosfs/msdosfs_denode.c,v 1.47.2.3 2002/08/22 16:20:15 trhodes Exp $ */
2 /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/mount.h>
55 #include <sys/malloc.h>
58 #include <sys/vnode.h>
61 #include <vm/vm_extern.h>
66 #include "msdosfsmount.h"
71 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
74 * Hash table caching denode instances.
76 * denodes are keyed by the disk location (cluster num, entry offset) of the
77 * directory entry of the file they represent.
79 * denodes representing deleted but still opened files are left in this cache
80 * until reclaimed. Deleted directory entries can be reused when files are
81 * renamed or new files created. As a consequence, several denodes associated
82 * with the same entry may coexist in this cache as long as a single one of
83 * them map to an existing file (de_refcnt > 0).
85 * R/w access to this cache is protected by dehash_token.
87 static struct denode **dehashtbl;
88 static u_long dehash; /* size of hash table - 1 */
89 #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \
90 sizeof(struct direntry)) & dehash])
91 static struct lwkt_token dehash_token;
97 #define SETHIGH(q, h) { \
100 tmp.val[_QUAD_HIGHWORD] = (h); \
103 #define SETLOW(q, l) { \
106 tmp.val[_QUAD_LOWWORD] = (l); \
112 msdosfs_init(struct vfsconf *vfsp)
114 dehash = vfs_inodehashsize();
115 dehashtbl = kmalloc(sizeof(void *) * dehash,
116 M_MSDOSFSMNT, M_WAITOK|M_ZERO);
118 lwkt_token_init(&dehash_token, "msdosihash");
124 msdosfs_uninit(struct vfsconf *vfsp)
128 kfree(dehashtbl, M_MSDOSFSMNT);
132 static struct denode *
133 msdosfs_hashget(cdev_t dev, u_long dirclust, u_long diroff)
138 lwkt_gettoken(&dehash_token);
140 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
141 if (dirclust != dep->de_dirclust
142 || diroff != dep->de_diroffset
143 || dev != dep->de_dev
144 || dep->de_refcnt <= 0) {
148 if (vget(vp, LK_EXCLUSIVE))
152 * We must check to see if the inode has been ripped
153 * out from under us after blocking.
155 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
156 if (dirclust == dep->de_dirclust
157 && diroff == dep->de_diroffset
158 && dev == dep->de_dev
159 && dep->de_refcnt > 0) {
163 if (dep == NULL || DETOV(dep) != vp) {
167 lwkt_reltoken(&dehash_token);
170 lwkt_reltoken(&dehash_token);
175 * Try to insert specified denode into the hash table. Return 0 on success
176 * and EBUSY if there is already a denode with the same key.
180 msdosfs_hashins(struct denode *dep)
182 struct denode **depp, *deq;
184 lwkt_gettoken(&dehash_token);
185 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
186 while ((deq = *depp) != NULL) {
187 if (deq->de_dev == dep->de_dev &&
188 deq->de_dirclust == dep->de_dirclust &&
189 deq->de_diroffset == dep->de_diroffset &&
190 deq->de_refcnt > 0) {
191 lwkt_reltoken(&dehash_token);
194 depp = &deq->de_next;
198 lwkt_reltoken(&dehash_token);
204 msdosfs_hashrem(struct denode *dep)
206 struct denode **depp, *deq;
208 lwkt_gettoken(&dehash_token);
209 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
210 while ((deq = *depp) != NULL) {
213 depp = &deq->de_next;
215 KKASSERT(dep == deq);
216 *depp = dep->de_next;
218 lwkt_reltoken(&dehash_token);
222 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
226 lwkt_gettoken(&dehash_token);
228 ip->de_dirclust = new_dirclust;
229 ip->de_diroffset = new_diroffset;
230 error = msdosfs_hashins(ip);
231 KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error));
232 lwkt_reltoken(&dehash_token);
236 * If deget() succeeds it returns with the gotten denode locked().
238 * pmp - address of msdosfsmount structure of the filesystem containing
239 * the denode of interest. The pm_dev field and the address of
240 * the msdosfsmount structure are used.
241 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
242 * diroffset is relative to the beginning of the root directory,
243 * otherwise it is cluster relative.
244 * diroffset - offset past begin of cluster of denode we want
245 * depp - returns the address of the gotten denode.
248 deget(struct msdosfsmount *pmp, /* so we know the maj/min number */
249 u_long dirclust, /* cluster this dir entry came from */
250 u_long diroffset, /* index of entry within the cluster */
251 struct denode **depp) /* returns the addr of the gotten denode */
254 cdev_t dev = pmp->pm_dev;
255 struct mount *mntp = pmp->pm_mountp;
256 struct direntry *direntptr;
263 kprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
264 pmp, dirclust, diroffset, depp);
268 * On FAT32 filesystems, root is a (more or less) normal
271 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
272 dirclust = pmp->pm_rootdirblk;
276 * See if the denode is in the denode cache. Use the location of
277 * the directory entry to compute the hash value. For subdir use
278 * address of "." entry. For root dir (if not FAT32) use cluster
279 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
281 * NOTE: The check for de_refcnt > 0 below insures the denode being
282 * examined does not represent an unlinked but still open file.
283 * These files are not to be accessible even when the directory
284 * entry that represented the file happens to be reused while the
285 * deleted file is still open.
287 ldep = msdosfs_hashget(dev, dirclust, diroffset);
294 * Do the MALLOC before the getnewvnode since doing so afterward
295 * might cause a bogus v_data pointer to get dereferenced
296 * elsewhere if MALLOC should block.
298 ldep = kmalloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
301 * Directory entry was not in cache, have to create a vnode and
302 * copy it from the passed disk buffer.
305 /* getnewvnode() does a vref() on the vnode */
306 error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0);
309 kfree(ldep, M_MSDOSFSNODE);
313 ldep->de_vnode = nvp;
317 ldep->de_dirclust = dirclust;
318 ldep->de_diroffset = diroffset;
319 fc_purge(ldep, 0); /* init the fat cache for this denode */
322 * Insert the denode into the hash queue. If a collision occurs
323 * throw away the vnode and try again.
325 error = msdosfs_hashins(ldep);
326 if (error == EBUSY) {
329 kfree(ldep, M_MSDOSFSNODE);
334 kfree(ldep, M_MSDOSFSNODE);
342 * Copy the directory entry into the denode area of the vnode.
344 if ((dirclust == MSDOSFSROOT
345 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
346 && diroffset == MSDOSFSROOT_OFS) {
348 * Directory entry for the root directory. There isn't one,
349 * so we manufacture one. We should probably rummage
350 * through the root directory and find a label entry (if it
351 * exists), and then use the time and date from that entry
352 * as the time and date for the root denode.
354 vsetflags(nvp, VROOT); /* should be further down XXX */
356 ldep->de_Attributes = ATTR_DIRECTORY;
357 ldep->de_LowerCase = 0;
359 ldep->de_StartCluster = pmp->pm_rootdirblk;
360 /* de_FileSize will be filled in further down */
362 ldep->de_StartCluster = MSDOSFSROOT;
363 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
366 * fill in time and date so that dos2unixtime() doesn't
367 * spit up when called from msdosfs_getattr() with root
371 ldep->de_CTime = 0x0000; /* 00:00:00 */
372 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
373 | (1 << DD_DAY_SHIFT);
375 ldep->de_ADate = ldep->de_CDate;
376 ldep->de_MTime = ldep->de_CTime;
377 ldep->de_MDate = ldep->de_CDate;
378 /* leave the other fields as garbage */
380 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
383 * The denode does not contain anything useful, so
384 * it would be wrong to leave it on its hash chain.
385 * Arrange for vput() to just forget about it.
387 ldep->de_Name[0] = SLOT_DELETED;
394 DE_INTERNALIZE(ldep, direntptr);
399 * Fill in a few fields of the vnode and finish filling in the
400 * denode. Then return the address of the found denode.
402 if (ldep->de_Attributes & ATTR_DIRECTORY) {
404 * Since DOS directory entries that describe directories
405 * have 0 in the filesize field, we take this opportunity
406 * to find out the length of the directory and plug it into
407 * the denode structure.
412 * XXX Sometimes, these arrives that . entry have cluster
413 * number 0, when it shouldn't. Use real cluster number
414 * instead of what is written in directory entry.
416 if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
417 kprintf("deget(): . entry at clust %ld != %ld\n",
418 dirclust, ldep->de_StartCluster);
419 ldep->de_StartCluster = dirclust;
423 if (ldep->de_StartCluster != MSDOSFSROOT) {
424 error = pcbmap(ldep, 0xffff, NULL, &size, NULL);
425 if (error == E2BIG) {
426 ldep->de_FileSize = de_cn2off(pmp, size);
429 kprintf("deget(): pcbmap returned %d\n", error);
435 SETHIGH(ldep->de_modrev, tv.tv_sec);
436 SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
437 ldep->de_devvp = pmp->pm_devvp;
438 vref(ldep->de_devvp);
439 vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1);
441 * Leave nvp locked and refd so the returned inode is effectively
449 deupdat(struct denode *dep, int waitfor)
453 struct direntry *dirp;
456 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
459 DETIMES(dep, &ts, &ts, &ts);
460 if ((dep->de_flag & DE_MODIFIED) == 0)
462 dep->de_flag &= ~DE_MODIFIED;
463 if (dep->de_Attributes & ATTR_DIRECTORY)
465 if (dep->de_refcnt <= 0)
467 error = readde(dep, &bp, &dirp);
470 DE_EXTERNALIZE(dirp, dep);
480 * Truncate the file described by dep to the length specified by length.
483 detrunc(struct denode *dep, u_long length, int flags)
491 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
493 struct msdosfsmount *pmp = dep->de_pmp;
496 kprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name,
501 * Disallow attempts to truncate the root directory since it is of
502 * fixed size. That's just the way dos filesystems are. We use
503 * the VROOT bit in the vnode because checking for the directory
504 * bit and a startcluster of 0 in the denode is not adequate to
505 * recognize the root directory at this point in a file or
508 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
509 kprintf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
510 dep->de_dirclust, dep->de_diroffset);
515 if (dep->de_FileSize < length) {
516 vnode_pager_setsize(DETOV(dep), length);
517 return deextend(dep, length);
521 * If the desired length is 0 then remember the starting cluster of
522 * the file and set the StartCluster field in the directory entry
523 * to 0. If the desired length is not zero, then get the number of
524 * the last cluster in the shortened file. Then get the number of
525 * the first cluster in the part of the file that is to be freed.
526 * Then set the next cluster pointer in the last cluster of the
527 * file to CLUST_EOFE.
530 chaintofree = dep->de_StartCluster;
531 dep->de_StartCluster = 0;
534 error = pcbmap(dep, de_clcount(pmp, length) - 1,
535 NULL, &eofentry, NULL);
538 kprintf("detrunc(): pcbmap fails %d\n", error);
544 fc_purge(dep, de_clcount(pmp, length));
547 * If the new length is not a multiple of the cluster size then we
548 * must zero the tail end of the new last cluster in case it
549 * becomes part of the file again because of a seek.
551 if ((boff = length & pmp->pm_crbomask) != 0) {
553 bn = xcntobn(pmp, eofentry);
554 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
555 pmp->pm_bpcluster, &bp);
557 cn = de_cluster(pmp, length);
558 error = bread(DETOV(dep), de_cn2doff(pmp, cn),
559 pmp->pm_bpcluster, &bp);
564 kprintf("detrunc(): bread fails %d\n", error);
569 * is this the right place for it?
571 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
579 * Write out the updated directory entry. Even if the update fails
580 * we free the trailing clusters.
582 dep->de_FileSize = length;
584 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
585 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
588 kprintf("detrunc(): vtruncbuf error %d\n", allerror);
590 error = deupdat(dep, 1);
591 if (error && (allerror == 0))
594 kprintf("detrunc(): allerror %d, eofentry %lu\n",
599 * If we need to break the cluster chain for the file then do it
602 if (eofentry != ~0) {
603 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
604 &chaintofree, CLUST_EOFE);
607 kprintf("detrunc(): fatentry errors %d\n", error);
611 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
616 * Now free the clusters removed from the file because of the
619 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
620 freeclusterchain(pmp, chaintofree);
626 * Extend the file described by dep to length specified by length.
629 deextend(struct denode *dep, u_long length)
631 struct msdosfsmount *pmp = dep->de_pmp;
636 * The root of a DOS filesystem cannot be extended.
638 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
642 * Directories cannot be extended.
644 if (dep->de_Attributes & ATTR_DIRECTORY)
647 if (length <= dep->de_FileSize)
648 panic("deextend: file too large");
651 * Compute the number of clusters to allocate.
653 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
655 if (count > pmp->pm_freeclustercount)
657 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
659 /* truncate the added clusters away again */
660 detrunc(dep, dep->de_FileSize, 0);
664 dep->de_FileSize = length;
665 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
666 return (deupdat(dep, 1));
670 * msdosfs_reclaim(struct vnode *a_vp)
673 msdosfs_reclaim(struct vop_reclaim_args *ap)
675 struct vnode *vp = ap->a_vp;
676 struct denode *dep = VTODE(vp);
679 kprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
680 dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1);
683 if (prtactive && VREFCNT(vp) > 1)
684 vprint("msdosfs_reclaim(): pushing active", vp);
686 * Remove the denode from its hash chain.
690 msdosfs_hashrem(dep);
692 vrele(dep->de_devvp);
695 kfree(dep, M_MSDOSFSNODE);
701 * msdosfs_inactive(struct vnode *a_vp)
704 msdosfs_inactive(struct vop_inactive_args *ap)
706 struct vnode *vp = ap->a_vp;
707 struct denode *dep = VTODE(vp);
711 kprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n",
712 dep, (dep ? dep->de_Name[0] : 0));
715 if (prtactive && VREFCNT(vp) > 1)
716 vprint("msdosfs_inactive(): pushing active", vp);
719 * Ignore denodes related to stale file handles.
721 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)
725 * If the file has been deleted and it is on a read/write
726 * filesystem, then truncate the file, and mark the directory slot
727 * as empty. (This may not be necessary for the dos filesystem.)
730 kprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
731 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
733 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
734 error = detrunc(dep, (u_long) 0, 0);
735 dep->de_flag |= DE_UPDATE;
736 dep->de_Name[0] = SLOT_DELETED;
742 * If we are done with the denode, reclaim it
743 * so that it can be reused immediately.
746 kprintf("msdosfs_inactive(): v_refcnt 0x%08x, de_Name[0] %x\n",
747 vp->v_refcnt, (dep ? dep->de_Name[0] : 0));
749 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)