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 int msdosfs_hashins (struct denode *dep);
72 static void msdosfs_hashrem (struct denode *dep);
74 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
77 * Hash table caching denode instances.
79 * denodes are keyed by the disk location (cluster num, entry offset) of the
80 * directory entry of the file they represent.
82 * denodes representing deleted but still opened files are left in this cache
83 * until reclaimed. Deleted directory entries can be reused when files are
84 * renamed or new files created. As a consequence, several denodes associated
85 * with the same entry may coexist in this cache as long as a single one of
86 * them map to an existing file (de_refcnt > 0).
88 * R/w access to this cache is protected by dehash_token.
90 static struct denode **dehashtbl;
91 static u_long dehash; /* size of hash table - 1 */
92 #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \
93 sizeof(struct direntry)) & dehash])
94 static struct lwkt_token dehash_token;
100 #define SETHIGH(q, h) { \
103 tmp.val[_QUAD_HIGHWORD] = (h); \
106 #define SETLOW(q, l) { \
109 tmp.val[_QUAD_LOWWORD] = (l); \
113 static struct denode *
114 msdosfs_hashget (cdev_t dev, u_long dirclust, u_long diroff);
118 msdosfs_init(struct vfsconf *vfsp)
120 dehash = vfs_inodehashsize();
121 dehashtbl = kmalloc(sizeof(void *) * dehash,
122 M_MSDOSFSMNT, M_WAITOK|M_ZERO);
124 lwkt_token_init(&dehash_token, "msdosihash");
130 msdosfs_uninit(struct vfsconf *vfsp)
134 kfree(dehashtbl, M_MSDOSFSMNT);
138 static struct denode *
139 msdosfs_hashget(cdev_t dev, u_long dirclust, u_long diroff)
144 lwkt_gettoken(&dehash_token);
146 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
147 if (dirclust != dep->de_dirclust
148 || diroff != dep->de_diroffset
149 || dev != dep->de_dev
150 || dep->de_refcnt <= 0) {
154 if (vget(vp, LK_EXCLUSIVE))
158 * We must check to see if the inode has been ripped
159 * out from under us after blocking.
161 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
162 if (dirclust == dep->de_dirclust
163 && diroff == dep->de_diroffset
164 && dev == dep->de_dev
165 && dep->de_refcnt > 0) {
169 if (dep == NULL || DETOV(dep) != vp) {
173 lwkt_reltoken(&dehash_token);
176 lwkt_reltoken(&dehash_token);
181 * Try to insert specified denode into the hash table. Return 0 on success
182 * and EBUSY if there is already a denode with the same key.
186 msdosfs_hashins(struct denode *dep)
188 struct denode **depp, *deq;
190 lwkt_gettoken(&dehash_token);
191 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
192 while ((deq = *depp) != NULL) {
193 if (deq->de_dev == dep->de_dev &&
194 deq->de_dirclust == dep->de_dirclust &&
195 deq->de_diroffset == dep->de_diroffset &&
196 deq->de_refcnt > 0) {
197 lwkt_reltoken(&dehash_token);
200 depp = &deq->de_next;
204 lwkt_reltoken(&dehash_token);
210 msdosfs_hashrem(struct denode *dep)
212 struct denode **depp, *deq;
214 lwkt_gettoken(&dehash_token);
215 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
216 while ((deq = *depp) != NULL) {
219 depp = &deq->de_next;
221 KKASSERT(dep == deq);
222 *depp = dep->de_next;
224 lwkt_reltoken(&dehash_token);
228 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
232 lwkt_gettoken(&dehash_token);
234 ip->de_dirclust = new_dirclust;
235 ip->de_diroffset = new_diroffset;
236 error = msdosfs_hashins(ip);
237 KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error));
238 lwkt_reltoken(&dehash_token);
242 * If deget() succeeds it returns with the gotten denode locked().
244 * pmp - address of msdosfsmount structure of the filesystem containing
245 * the denode of interest. The pm_dev field and the address of
246 * the msdosfsmount structure are used.
247 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
248 * diroffset is relative to the beginning of the root directory,
249 * otherwise it is cluster relative.
250 * diroffset - offset past begin of cluster of denode we want
251 * depp - returns the address of the gotten denode.
254 deget(struct msdosfsmount *pmp, /* so we know the maj/min number */
255 u_long dirclust, /* cluster this dir entry came from */
256 u_long diroffset, /* index of entry within the cluster */
257 struct denode **depp) /* returns the addr of the gotten denode */
260 cdev_t dev = pmp->pm_dev;
261 struct mount *mntp = pmp->pm_mountp;
262 struct direntry *direntptr;
269 kprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
270 pmp, dirclust, diroffset, depp);
274 * On FAT32 filesystems, root is a (more or less) normal
277 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
278 dirclust = pmp->pm_rootdirblk;
282 * See if the denode is in the denode cache. Use the location of
283 * the directory entry to compute the hash value. For subdir use
284 * address of "." entry. For root dir (if not FAT32) use cluster
285 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
287 * NOTE: The check for de_refcnt > 0 below insures the denode being
288 * examined does not represent an unlinked but still open file.
289 * These files are not to be accessible even when the directory
290 * entry that represented the file happens to be reused while the
291 * deleted file is still open.
293 ldep = msdosfs_hashget(dev, dirclust, diroffset);
300 * Do the MALLOC before the getnewvnode since doing so afterward
301 * might cause a bogus v_data pointer to get dereferenced
302 * elsewhere if MALLOC should block.
304 ldep = kmalloc(sizeof(struct denode), M_MSDOSFSNODE,
308 * Directory entry was not in cache, have to create a vnode and
309 * copy it from the passed disk buffer.
312 /* getnewvnode() does a vref() on the vnode */
313 error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0);
316 kfree(ldep, M_MSDOSFSNODE);
320 ldep->de_vnode = nvp;
324 ldep->de_dirclust = dirclust;
325 ldep->de_diroffset = diroffset;
326 fc_purge(ldep, 0); /* init the fat cache for this denode */
329 * Insert the denode into the hash queue. If a collision occurs
330 * throw away the vnode and try again.
332 error = msdosfs_hashins(ldep);
333 if (error == EBUSY) {
336 kfree(ldep, M_MSDOSFSNODE);
341 kfree(ldep, M_MSDOSFSNODE);
349 * Copy the directory entry into the denode area of the vnode.
351 if ((dirclust == MSDOSFSROOT
352 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
353 && diroffset == MSDOSFSROOT_OFS) {
355 * Directory entry for the root directory. There isn't one,
356 * so we manufacture one. We should probably rummage
357 * through the root directory and find a label entry (if it
358 * exists), and then use the time and date from that entry
359 * as the time and date for the root denode.
361 vsetflags(nvp, VROOT); /* should be further down XXX */
363 ldep->de_Attributes = ATTR_DIRECTORY;
364 ldep->de_LowerCase = 0;
366 ldep->de_StartCluster = pmp->pm_rootdirblk;
367 /* de_FileSize will be filled in further down */
369 ldep->de_StartCluster = MSDOSFSROOT;
370 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
373 * fill in time and date so that dos2unixtime() doesn't
374 * spit up when called from msdosfs_getattr() with root
378 ldep->de_CTime = 0x0000; /* 00:00:00 */
379 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
380 | (1 << DD_DAY_SHIFT);
382 ldep->de_ADate = ldep->de_CDate;
383 ldep->de_MTime = ldep->de_CTime;
384 ldep->de_MDate = ldep->de_CDate;
385 /* leave the other fields as garbage */
387 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
390 * The denode does not contain anything useful, so
391 * it would be wrong to leave it on its hash chain.
392 * Arrange for vput() to just forget about it.
394 ldep->de_Name[0] = SLOT_DELETED;
401 DE_INTERNALIZE(ldep, direntptr);
406 * Fill in a few fields of the vnode and finish filling in the
407 * denode. Then return the address of the found denode.
409 if (ldep->de_Attributes & ATTR_DIRECTORY) {
411 * Since DOS directory entries that describe directories
412 * have 0 in the filesize field, we take this opportunity
413 * to find out the length of the directory and plug it into
414 * the denode structure.
419 * XXX Sometimes, these arrives that . entry have cluster
420 * number 0, when it shouldn't. Use real cluster number
421 * instead of what is written in directory entry.
423 if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
424 kprintf("deget(): . entry at clust %ld != %ld\n",
425 dirclust, ldep->de_StartCluster);
426 ldep->de_StartCluster = dirclust;
430 if (ldep->de_StartCluster != MSDOSFSROOT) {
431 error = pcbmap(ldep, 0xffff, NULL, &size, NULL);
432 if (error == E2BIG) {
433 ldep->de_FileSize = de_cn2off(pmp, size);
436 kprintf("deget(): pcbmap returned %d\n", error);
442 SETHIGH(ldep->de_modrev, tv.tv_sec);
443 SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
444 ldep->de_devvp = pmp->pm_devvp;
445 vref(ldep->de_devvp);
446 vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1);
448 * Leave nvp locked and refd so the returned inode is effectively
456 deupdat(struct denode *dep, int waitfor)
460 struct direntry *dirp;
463 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
466 DETIMES(dep, &ts, &ts, &ts);
467 if ((dep->de_flag & DE_MODIFIED) == 0)
469 dep->de_flag &= ~DE_MODIFIED;
470 if (dep->de_Attributes & ATTR_DIRECTORY)
472 if (dep->de_refcnt <= 0)
474 error = readde(dep, &bp, &dirp);
477 DE_EXTERNALIZE(dirp, dep);
487 * Truncate the file described by dep to the length specified by length.
490 detrunc(struct denode *dep, u_long length, int flags)
498 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
500 struct msdosfsmount *pmp = dep->de_pmp;
503 kprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
507 * Disallow attempts to truncate the root directory since it is of
508 * fixed size. That's just the way dos filesystems are. We use
509 * the VROOT bit in the vnode because checking for the directory
510 * bit and a startcluster of 0 in the denode is not adequate to
511 * recognize the root directory at this point in a file or
514 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
515 kprintf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
516 dep->de_dirclust, dep->de_diroffset);
521 if (dep->de_FileSize < length) {
522 vnode_pager_setsize(DETOV(dep), length);
523 return deextend(dep, length);
527 * If the desired length is 0 then remember the starting cluster of
528 * the file and set the StartCluster field in the directory entry
529 * to 0. If the desired length is not zero, then get the number of
530 * the last cluster in the shortened file. Then get the number of
531 * the first cluster in the part of the file that is to be freed.
532 * Then set the next cluster pointer in the last cluster of the
533 * file to CLUST_EOFE.
536 chaintofree = dep->de_StartCluster;
537 dep->de_StartCluster = 0;
540 error = pcbmap(dep, de_clcount(pmp, length) - 1,
541 NULL, &eofentry, NULL);
544 kprintf("detrunc(): pcbmap fails %d\n", error);
550 fc_purge(dep, de_clcount(pmp, length));
553 * If the new length is not a multiple of the cluster size then we
554 * must zero the tail end of the new last cluster in case it
555 * becomes part of the file again because of a seek.
557 if ((boff = length & pmp->pm_crbomask) != 0) {
559 bn = xcntobn(pmp, eofentry);
560 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
562 cn = de_cluster(pmp, length);
563 error = bread(DETOV(dep), de_cn2doff(pmp, cn), pmp->pm_bpcluster, &bp);
568 kprintf("detrunc(): bread fails %d\n", error);
573 * is this the right place for it?
575 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
583 * Write out the updated directory entry. Even if the update fails
584 * we free the trailing clusters.
586 dep->de_FileSize = length;
588 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
589 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
592 kprintf("detrunc(): vtruncbuf error %d\n", allerror);
594 error = deupdat(dep, 1);
595 if (error && (allerror == 0))
598 kprintf("detrunc(): allerror %d, eofentry %lu\n",
603 * If we need to break the cluster chain for the file then do it
606 if (eofentry != ~0) {
607 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
608 &chaintofree, CLUST_EOFE);
611 kprintf("detrunc(): fatentry errors %d\n", error);
615 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
620 * Now free the clusters removed from the file because of the
623 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
624 freeclusterchain(pmp, chaintofree);
630 * Extend the file described by dep to length specified by length.
633 deextend(struct denode *dep, u_long length)
635 struct msdosfsmount *pmp = dep->de_pmp;
640 * The root of a DOS filesystem cannot be extended.
642 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
646 * Directories cannot be extended.
648 if (dep->de_Attributes & ATTR_DIRECTORY)
651 if (length <= dep->de_FileSize)
652 panic("deextend: file too large");
655 * Compute the number of clusters to allocate.
657 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
659 if (count > pmp->pm_freeclustercount)
661 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
663 /* truncate the added clusters away again */
664 detrunc(dep, dep->de_FileSize, 0);
668 dep->de_FileSize = length;
669 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
670 return (deupdat(dep, 1));
674 * msdosfs_reclaim(struct vnode *a_vp)
677 msdosfs_reclaim(struct vop_reclaim_args *ap)
679 struct vnode *vp = ap->a_vp;
680 struct denode *dep = VTODE(vp);
683 kprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
684 dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1);
687 if (prtactive && VREFCNT(vp) > 1)
688 vprint("msdosfs_reclaim(): pushing active", vp);
690 * Remove the denode from its hash chain.
694 msdosfs_hashrem(dep);
696 vrele(dep->de_devvp);
699 kfree(dep, M_MSDOSFSNODE);
705 * msdosfs_inactive(struct vnode *a_vp)
708 msdosfs_inactive(struct vop_inactive_args *ap)
710 struct vnode *vp = ap->a_vp;
711 struct denode *dep = VTODE(vp);
715 kprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n",
716 dep, (dep ? dep->de_Name[0] : 0));
719 if (prtactive && VREFCNT(vp) > 1)
720 vprint("msdosfs_inactive(): pushing active", vp);
723 * Ignore denodes related to stale file handles.
725 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)
729 * If the file has been deleted and it is on a read/write
730 * filesystem, then truncate the file, and mark the directory slot
731 * as empty. (This may not be necessary for the dos filesystem.)
734 kprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
735 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
737 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
738 error = detrunc(dep, (u_long) 0, 0);
739 dep->de_flag |= DE_UPDATE;
740 dep->de_Name[0] = SLOT_DELETED;
746 * If we are done with the denode, reclaim it
747 * so that it can be reused immediately.
750 kprintf("msdosfs_inactive(): v_refcnt 0x%08x, de_Name[0] %x\n",
751 vp->v_refcnt, (dep ? dep->de_Name[0] : 0));
753 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)