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.35 2008/06/08 07:56:06 nth 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");
76 * Hash table caching denode instances.
78 * denodes are keyed by the disk location (cluster num, entry offset) of the
79 * directory entry of the file they represent.
81 * denodes representing deleted but still opened files are left in this cache
82 * until reclaimed. Deleted directory entries can be reused when files are
83 * renamed or new files created. As a consequence, several denodes associated
84 * with the same entry may coexist in this cache as long as a single one of
85 * them map to an existing file (de_refcnt > 0).
87 * R/w access to this cache is protected by dehash_token.
89 static struct denode **dehashtbl;
90 static u_long dehash; /* size of hash table - 1 */
91 #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \
92 sizeof(struct direntry)) & dehash])
93 static struct lwkt_token dehash_token;
99 #define SETHIGH(q, h) { \
102 tmp.val[_QUAD_HIGHWORD] = (h); \
105 #define SETLOW(q, l) { \
108 tmp.val[_QUAD_LOWWORD] = (l); \
112 static struct denode *
113 msdosfs_hashget (cdev_t dev, u_long dirclust, u_long diroff);
117 msdosfs_init(struct vfsconf *vfsp)
120 while (dehash < desiredvnodes)
122 dehashtbl = kmalloc(sizeof(void *) * dehash, M_MSDOSFSMNT,
125 lwkt_token_init(&dehash_token);
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)
145 lwkt_gettoken(&ilock, &dehash_token);
147 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
148 if (dirclust != dep->de_dirclust
149 || diroff != dep->de_diroffset
150 || dev != dep->de_dev
151 || dep->de_refcnt <= 0) {
155 if (vget(vp, LK_EXCLUSIVE))
159 * We must check to see if the inode has been ripped
160 * out from under us after blocking.
162 for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
163 if (dirclust == dep->de_dirclust
164 && diroff == dep->de_diroffset
165 && dev == dep->de_dev
166 && dep->de_refcnt > 0) {
170 if (dep == NULL || DETOV(dep) != vp) {
174 lwkt_reltoken(&ilock);
177 lwkt_reltoken(&ilock);
182 * Try to insert specified denode into the hash table. Return 0 on success
183 * and EBUSY if there is already a denode with the same key.
187 msdosfs_hashins(struct denode *dep)
189 struct denode **depp, *deq;
192 lwkt_gettoken(&ilock, &dehash_token);
193 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
194 while ((deq = *depp) != NULL) {
195 if (deq->de_dev == dep->de_dev &&
196 deq->de_dirclust == dep->de_dirclust &&
197 deq->de_diroffset == dep->de_diroffset &&
198 deq->de_refcnt > 0) {
199 lwkt_reltoken(&ilock);
202 depp = &deq->de_next;
206 lwkt_reltoken(&ilock);
212 msdosfs_hashrem(struct denode *dep)
214 struct denode **depp, *deq;
217 lwkt_gettoken(&ilock, &dehash_token);
218 depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
219 while ((deq = *depp) != NULL) {
222 depp = &deq->de_next;
224 KKASSERT(dep == deq);
225 *depp = dep->de_next;
227 lwkt_reltoken(&ilock);
231 msdosfs_reinsert(struct denode *ip, u_long new_dirclust, u_long new_diroffset)
236 lwkt_gettoken(&ilock, &dehash_token);
238 ip->de_dirclust = new_dirclust;
239 ip->de_diroffset = new_diroffset;
240 error = msdosfs_hashins(ip);
241 KASSERT(!error, ("msdosfs_reinsert: insertion failed %d", error));
242 lwkt_reltoken(&ilock);
246 * If deget() succeeds it returns with the gotten denode locked().
248 * pmp - address of msdosfsmount structure of the filesystem containing
249 * the denode of interest. The pm_dev field and the address of
250 * the msdosfsmount structure are used.
251 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
252 * diroffset is relative to the beginning of the root directory,
253 * otherwise it is cluster relative.
254 * diroffset - offset past begin of cluster of denode we want
255 * depp - returns the address of the gotten denode.
258 deget(struct msdosfsmount *pmp, /* so we know the maj/min number */
259 u_long dirclust, /* cluster this dir entry came from */
260 u_long diroffset, /* index of entry within the cluster */
261 struct denode **depp) /* returns the addr of the gotten denode */
264 cdev_t dev = pmp->pm_dev;
265 struct mount *mntp = pmp->pm_mountp;
266 struct direntry *direntptr;
273 kprintf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
274 pmp, dirclust, diroffset, depp);
278 * On FAT32 filesystems, root is a (more or less) normal
281 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
282 dirclust = pmp->pm_rootdirblk;
286 * See if the denode is in the denode cache. Use the location of
287 * the directory entry to compute the hash value. For subdir use
288 * address of "." entry. For root dir (if not FAT32) use cluster
289 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
291 * NOTE: The check for de_refcnt > 0 below insures the denode being
292 * examined does not represent an unlinked but still open file.
293 * These files are not to be accessible even when the directory
294 * entry that represented the file happens to be reused while the
295 * deleted file is still open.
297 ldep = msdosfs_hashget(dev, dirclust, diroffset);
304 * Do the MALLOC before the getnewvnode since doing so afterward
305 * might cause a bogus v_data pointer to get dereferenced
306 * elsewhere if MALLOC should block.
308 MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE,
312 * Directory entry was not in cache, have to create a vnode and
313 * copy it from the passed disk buffer.
316 /* getnewvnode() does a vref() on the vnode */
317 error = getnewvnode(VT_MSDOSFS, mntp, &nvp, VLKTIMEOUT, 0);
320 FREE(ldep, M_MSDOSFSNODE);
324 ldep->de_vnode = nvp;
328 ldep->de_dirclust = dirclust;
329 ldep->de_diroffset = diroffset;
330 fc_purge(ldep, 0); /* init the fat cache for this denode */
333 * Insert the denode into the hash queue. If a collision occurs
334 * throw away the vnode and try again.
336 error = msdosfs_hashins(ldep);
337 if (error == EBUSY) {
340 kfree(ldep, M_MSDOSFSNODE);
345 kfree(ldep, M_MSDOSFSNODE);
353 * Copy the directory entry into the denode area of the vnode.
355 if ((dirclust == MSDOSFSROOT
356 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
357 && diroffset == MSDOSFSROOT_OFS) {
359 * Directory entry for the root directory. There isn't one,
360 * so we manufacture one. We should probably rummage
361 * through the root directory and find a label entry (if it
362 * exists), and then use the time and date from that entry
363 * as the time and date for the root denode.
365 vsetflags(nvp, VROOT); /* should be further down XXX */
367 ldep->de_Attributes = ATTR_DIRECTORY;
368 ldep->de_LowerCase = 0;
370 ldep->de_StartCluster = pmp->pm_rootdirblk;
371 /* de_FileSize will be filled in further down */
373 ldep->de_StartCluster = MSDOSFSROOT;
374 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
377 * fill in time and date so that dos2unixtime() doesn't
378 * spit up when called from msdosfs_getattr() with root
382 ldep->de_CTime = 0x0000; /* 00:00:00 */
383 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
384 | (1 << DD_DAY_SHIFT);
386 ldep->de_ADate = ldep->de_CDate;
387 ldep->de_MTime = ldep->de_CTime;
388 ldep->de_MDate = ldep->de_CDate;
389 /* leave the other fields as garbage */
391 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
394 * The denode does not contain anything useful, so
395 * it would be wrong to leave it on its hash chain.
396 * Arrange for vput() to just forget about it.
398 ldep->de_Name[0] = SLOT_DELETED;
405 DE_INTERNALIZE(ldep, direntptr);
410 * Fill in a few fields of the vnode and finish filling in the
411 * denode. Then return the address of the found denode.
413 if (ldep->de_Attributes & ATTR_DIRECTORY) {
415 * Since DOS directory entries that describe directories
416 * have 0 in the filesize field, we take this opportunity
417 * to find out the length of the directory and plug it into
418 * the denode structure.
423 * XXX Sometimes, these arrives that . entry have cluster
424 * number 0, when it shouldn't. Use real cluster number
425 * instead of what is written in directory entry.
427 if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
428 kprintf("deget(): . entry at clust %ld != %ld\n",
429 dirclust, ldep->de_StartCluster);
430 ldep->de_StartCluster = dirclust;
434 if (ldep->de_StartCluster != MSDOSFSROOT) {
435 error = pcbmap(ldep, 0xffff, NULL, &size, NULL);
436 if (error == E2BIG) {
437 ldep->de_FileSize = de_cn2off(pmp, size);
440 kprintf("deget(): pcbmap returned %d\n", error);
446 SETHIGH(ldep->de_modrev, tv.tv_sec);
447 SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
448 ldep->de_devvp = pmp->pm_devvp;
449 vref(ldep->de_devvp);
450 vinitvmio(nvp, ldep->de_FileSize, PAGE_SIZE, -1);
452 * Leave nvp locked and refd so the returned inode is effectively
460 deupdat(struct denode *dep, int waitfor)
464 struct direntry *dirp;
467 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
470 DETIMES(dep, &ts, &ts, &ts);
471 if ((dep->de_flag & DE_MODIFIED) == 0)
473 dep->de_flag &= ~DE_MODIFIED;
474 if (dep->de_Attributes & ATTR_DIRECTORY)
476 if (dep->de_refcnt <= 0)
478 error = readde(dep, &bp, &dirp);
481 DE_EXTERNALIZE(dirp, dep);
491 * Truncate the file described by dep to the length specified by length.
494 detrunc(struct denode *dep, u_long length, int flags)
502 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
504 struct msdosfsmount *pmp = dep->de_pmp;
507 kprintf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
511 * Disallow attempts to truncate the root directory since it is of
512 * fixed size. That's just the way dos filesystems are. We use
513 * the VROOT bit in the vnode because checking for the directory
514 * bit and a startcluster of 0 in the denode is not adequate to
515 * recognize the root directory at this point in a file or
518 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
519 kprintf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
520 dep->de_dirclust, dep->de_diroffset);
525 if (dep->de_FileSize < length) {
526 vnode_pager_setsize(DETOV(dep), length);
527 return deextend(dep, length);
531 * If the desired length is 0 then remember the starting cluster of
532 * the file and set the StartCluster field in the directory entry
533 * to 0. If the desired length is not zero, then get the number of
534 * the last cluster in the shortened file. Then get the number of
535 * the first cluster in the part of the file that is to be freed.
536 * Then set the next cluster pointer in the last cluster of the
537 * file to CLUST_EOFE.
540 chaintofree = dep->de_StartCluster;
541 dep->de_StartCluster = 0;
544 error = pcbmap(dep, de_clcount(pmp, length) - 1,
545 NULL, &eofentry, NULL);
548 kprintf("detrunc(): pcbmap fails %d\n", error);
554 fc_purge(dep, de_clcount(pmp, length));
557 * If the new length is not a multiple of the cluster size then we
558 * must zero the tail end of the new last cluster in case it
559 * becomes part of the file again because of a seek.
561 if ((boff = length & pmp->pm_crbomask) != 0) {
563 bn = xcntobn(pmp, eofentry);
564 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
566 cn = de_cluster(pmp, length);
567 error = bread(DETOV(dep), de_cn2doff(pmp, cn), pmp->pm_bpcluster, &bp);
572 kprintf("detrunc(): bread fails %d\n", error);
577 * is this the right place for it?
579 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
587 * Write out the updated directory entry. Even if the update fails
588 * we free the trailing clusters.
590 dep->de_FileSize = length;
592 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
593 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
596 kprintf("detrunc(): vtruncbuf error %d\n", allerror);
598 error = deupdat(dep, 1);
599 if (error && (allerror == 0))
602 kprintf("detrunc(): allerror %d, eofentry %lu\n",
607 * If we need to break the cluster chain for the file then do it
610 if (eofentry != ~0) {
611 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
612 &chaintofree, CLUST_EOFE);
615 kprintf("detrunc(): fatentry errors %d\n", error);
619 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
624 * Now free the clusters removed from the file because of the
627 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
628 freeclusterchain(pmp, chaintofree);
634 * Extend the file described by dep to length specified by length.
637 deextend(struct denode *dep, u_long length)
639 struct msdosfsmount *pmp = dep->de_pmp;
644 * The root of a DOS filesystem cannot be extended.
646 if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
650 * Directories cannot be extended.
652 if (dep->de_Attributes & ATTR_DIRECTORY)
655 if (length <= dep->de_FileSize)
656 panic("deextend: file too large");
659 * Compute the number of clusters to allocate.
661 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
663 if (count > pmp->pm_freeclustercount)
665 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
667 /* truncate the added clusters away again */
668 detrunc(dep, dep->de_FileSize, 0);
672 dep->de_FileSize = length;
673 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
674 return (deupdat(dep, 1));
678 * msdosfs_reclaim(struct vnode *a_vp)
681 msdosfs_reclaim(struct vop_reclaim_args *ap)
683 struct vnode *vp = ap->a_vp;
684 struct denode *dep = VTODE(vp);
687 kprintf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
688 dep, dep ? (char *)dep->de_Name : "?", dep ? dep->de_refcnt : -1);
691 if (prtactive && vp->v_sysref.refcnt > 1)
692 vprint("msdosfs_reclaim(): pushing active", vp);
694 * Remove the denode from its hash chain.
698 msdosfs_hashrem(dep);
700 vrele(dep->de_devvp);
703 kfree(dep, M_MSDOSFSNODE);
709 * msdosfs_inactive(struct vnode *a_vp)
712 msdosfs_inactive(struct vop_inactive_args *ap)
714 struct vnode *vp = ap->a_vp;
715 struct denode *dep = VTODE(vp);
719 kprintf("msdosfs_inactive(): dep %p, de_Name[0] %x\n",
720 dep, (dep ? dep->de_Name[0] : 0));
723 if (prtactive && vp->v_sysref.refcnt > 1)
724 vprint("msdosfs_inactive(): pushing active", vp);
727 * Ignore denodes related to stale file handles.
729 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)
733 * If the file has been deleted and it is on a read/write
734 * filesystem, then truncate the file, and mark the directory slot
735 * as empty. (This may not be necessary for the dos filesystem.)
738 kprintf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
739 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
741 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
742 error = detrunc(dep, (u_long) 0, 0);
743 dep->de_flag |= DE_UPDATE;
744 dep->de_Name[0] = SLOT_DELETED;
750 * If we are done with the denode, reclaim it
751 * so that it can be reused immediately.
754 kprintf("msdosfs_inactive(): v_sysrefs %d, de_Name[0] %x\n",
755 vp->v_sysref.refcnt, (dep ? dep->de_Name[0] : 0));
757 if (dep == NULL || dep->de_Name[0] == SLOT_DELETED)