linux-bk/fs/dcache.c
<<
>>
Prefs
   1/*
   2 * fs/dcache.c
   3 *
   4 * Complete reimplementation
   5 * (C) 1997 Thomas Schoebel-Theuer,
   6 * with heavy changes by Linus Torvalds
   7 */
   8
   9/*
  10 * Notes on the allocation strategy:
  11 *
  12 * The dcache is a master of the icache - whenever a dcache entry
  13 * exists, the inode will always exist. "iput()" is done either when
  14 * the dcache entry is deleted or garbage collected.
  15 */
  16
  17#include <linux/config.h>
  18#include <linux/string.h>
  19#include <linux/mm.h>
  20#include <linux/fs.h>
  21#include <linux/slab.h>
  22#include <linux/init.h>
  23#include <linux/smp_lock.h>
  24#include <linux/cache.h>
  25#include <linux/module.h>
  26
  27#include <asm/uaccess.h>
  28
  29#define DCACHE_PARANOIA 1
  30/* #define DCACHE_DEBUG 1 */
  31
  32spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
  33rwlock_t dparent_lock __cacheline_aligned_in_smp = RW_LOCK_UNLOCKED;
  34
  35static kmem_cache_t *dentry_cache; 
  36
  37/*
  38 * This is the single most critical data structure when it comes
  39 * to the dcache: the hashtable for lookups. Somebody should try
  40 * to make this good - I've just made it work.
  41 *
  42 * This hash-function tries to avoid losing too many bits of hash
  43 * information, yet avoid using a prime hash-size or similar.
  44 */
  45#define D_HASHBITS     d_hash_shift
  46#define D_HASHMASK     d_hash_mask
  47
  48static unsigned int d_hash_mask;
  49static unsigned int d_hash_shift;
  50static struct list_head *dentry_hashtable;
  51static LIST_HEAD(dentry_unused);
  52
  53/* Statistics gathering. */
  54struct dentry_stat_t dentry_stat = {0, 0, 45, 0,};
  55
  56/* no dcache_lock, please */
  57static inline void d_free(struct dentry *dentry)
  58{
  59        if (dentry->d_op && dentry->d_op->d_release)
  60                dentry->d_op->d_release(dentry);
  61        if (dname_external(dentry)) 
  62                kfree(dentry->d_name.name);
  63        kmem_cache_free(dentry_cache, dentry); 
  64        dentry_stat.nr_dentry--;
  65}
  66
  67/*
  68 * Release the dentry's inode, using the filesystem
  69 * d_iput() operation if defined.
  70 * Called with dcache_lock held, drops it.
  71 */
  72static inline void dentry_iput(struct dentry * dentry)
  73{
  74        struct inode *inode = dentry->d_inode;
  75        if (inode) {
  76                dentry->d_inode = NULL;
  77                list_del_init(&dentry->d_alias);
  78                spin_unlock(&dcache_lock);
  79                if (dentry->d_op && dentry->d_op->d_iput)
  80                        dentry->d_op->d_iput(dentry, inode);
  81                else
  82                        iput(inode);
  83        } else
  84                spin_unlock(&dcache_lock);
  85}
  86
  87/* 
  88 * This is dput
  89 *
  90 * This is complicated by the fact that we do not want to put
  91 * dentries that are no longer on any hash chain on the unused
  92 * list: we'd much rather just get rid of them immediately.
  93 *
  94 * However, that implies that we have to traverse the dentry
  95 * tree upwards to the parents which might _also_ now be
  96 * scheduled for deletion (it may have been only waiting for
  97 * its last child to go away).
  98 *
  99 * This tail recursion is done by hand as we don't want to depend
 100 * on the compiler to always get this right (gcc generally doesn't).
 101 * Real recursion would eat up our stack space.
 102 */
 103
 104/*
 105 * dput - release a dentry
 106 * @dentry: dentry to release 
 107 *
 108 * Release a dentry. This will drop the usage count and if appropriate
 109 * call the dentry unlink method as well as removing it from the queues and
 110 * releasing its resources. If the parent dentries were scheduled for release
 111 * they too may now get deleted.
 112 *
 113 * no dcache lock, please.
 114 */
 115
 116void dput(struct dentry *dentry)
 117{
 118        if (!dentry)
 119                return;
 120
 121repeat:
 122        if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
 123                return;
 124
 125        /* dput on a free dentry? */
 126        if (!list_empty(&dentry->d_lru))
 127                BUG();
 128        /*
 129         * AV: ->d_delete() is _NOT_ allowed to block now.
 130         */
 131        if (dentry->d_op && dentry->d_op->d_delete) {
 132                if (dentry->d_op->d_delete(dentry))
 133                        goto unhash_it;
 134        }
 135        /* Unreachable? Get rid of it */
 136        if (list_empty(&dentry->d_hash))
 137                goto kill_it;
 138        list_add(&dentry->d_lru, &dentry_unused);
 139        dentry_stat.nr_unused++;
 140        dentry->d_vfs_flags |= DCACHE_REFERENCED;
 141        spin_unlock(&dcache_lock);
 142        return;
 143
 144unhash_it:
 145        list_del_init(&dentry->d_hash);
 146
 147kill_it: {
 148                struct dentry *parent;
 149                list_del(&dentry->d_child);
 150                /* drops the lock, at that point nobody can reach this dentry */
 151                dentry_iput(dentry);
 152                parent = dentry->d_parent;
 153                d_free(dentry);
 154                if (dentry == parent)
 155                        return;
 156                dentry = parent;
 157                goto repeat;
 158        }
 159}
 160
 161/**
 162 * d_invalidate - invalidate a dentry
 163 * @dentry: dentry to invalidate
 164 *
 165 * Try to invalidate the dentry if it turns out to be
 166 * possible. If there are other dentries that can be
 167 * reached through this one we can't delete it and we
 168 * return -EBUSY. On success we return 0.
 169 *
 170 * no dcache lock.
 171 */
 172 
 173int d_invalidate(struct dentry * dentry)
 174{
 175        /*
 176         * If it's already been dropped, return OK.
 177         */
 178        spin_lock(&dcache_lock);
 179        if (list_empty(&dentry->d_hash)) {
 180                spin_unlock(&dcache_lock);
 181                return 0;
 182        }
 183        /*
 184         * Check whether to do a partial shrink_dcache
 185         * to get rid of unused child entries.
 186         */
 187        if (!list_empty(&dentry->d_subdirs)) {
 188                spin_unlock(&dcache_lock);
 189                shrink_dcache_parent(dentry);
 190                spin_lock(&dcache_lock);
 191        }
 192
 193        /*
 194         * Somebody else still using it?
 195         *
 196         * If it's a directory, we can't drop it
 197         * for fear of somebody re-populating it
 198         * with children (even though dropping it
 199         * would make it unreachable from the root,
 200         * we might still populate it if it was a
 201         * working directory or similar).
 202         */
 203        if (atomic_read(&dentry->d_count) > 1) {
 204                if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
 205                        spin_unlock(&dcache_lock);
 206                        return -EBUSY;
 207                }
 208        }
 209
 210        list_del_init(&dentry->d_hash);
 211        spin_unlock(&dcache_lock);
 212        return 0;
 213}
 214
 215/* This should be called _only_ with dcache_lock held */
 216
 217static inline struct dentry * __dget_locked(struct dentry *dentry)
 218{
 219        atomic_inc(&dentry->d_count);
 220        if (atomic_read(&dentry->d_count) == 1) {
 221                dentry_stat.nr_unused--;
 222                list_del_init(&dentry->d_lru);
 223        }
 224        return dentry;
 225}
 226
 227struct dentry * dget_locked(struct dentry *dentry)
 228{
 229        return __dget_locked(dentry);
 230}
 231
 232/**
 233 * d_find_alias - grab a hashed alias of inode
 234 * @inode: inode in question
 235 *
 236 * If inode has a hashed alias - acquire the reference to alias and
 237 * return it. Otherwise return NULL. Notice that if inode is a directory
 238 * there can be only one alias and it can be unhashed only if it has
 239 * no children.
 240 *
 241 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
 242 * any other hashed alias over that one.
 243 */
 244
 245struct dentry * d_find_alias(struct inode *inode)
 246{
 247        struct list_head *head, *next, *tmp;
 248        struct dentry *alias, *discon_alias=NULL;
 249
 250        spin_lock(&dcache_lock);
 251        head = &inode->i_dentry;
 252        next = inode->i_dentry.next;
 253        while (next != head) {
 254                tmp = next;
 255                next = tmp->next;
 256                alias = list_entry(tmp, struct dentry, d_alias);
 257                if (!list_empty(&alias->d_hash)) {
 258                        if (alias->d_flags & DCACHE_DISCONNECTED)
 259                                discon_alias = alias;
 260                        else {
 261                                __dget_locked(alias);
 262                                spin_unlock(&dcache_lock);
 263                                return alias;
 264                        }
 265                }
 266        }
 267        if (discon_alias)
 268                __dget_locked(discon_alias);
 269        spin_unlock(&dcache_lock);
 270        return discon_alias;
 271}
 272
 273/*
 274 *      Try to kill dentries associated with this inode.
 275 * WARNING: you must own a reference to inode.
 276 */
 277void d_prune_aliases(struct inode *inode)
 278{
 279        struct list_head *tmp, *head = &inode->i_dentry;
 280restart:
 281        spin_lock(&dcache_lock);
 282        tmp = head;
 283        while ((tmp = tmp->next) != head) {
 284                struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
 285                if (!atomic_read(&dentry->d_count)) {
 286                        __dget_locked(dentry);
 287                        spin_unlock(&dcache_lock);
 288                        d_drop(dentry);
 289                        dput(dentry);
 290                        goto restart;
 291                }
 292        }
 293        spin_unlock(&dcache_lock);
 294}
 295
 296/*
 297 * Throw away a dentry - free the inode, dput the parent.
 298 * This requires that the LRU list has already been
 299 * removed.
 300 * Called with dcache_lock, drops it and then regains.
 301 */
 302static inline void prune_one_dentry(struct dentry * dentry)
 303{
 304        struct dentry * parent;
 305
 306        list_del_init(&dentry->d_hash);
 307        list_del(&dentry->d_child);
 308        dentry_iput(dentry);
 309        parent = dentry->d_parent;
 310        d_free(dentry);
 311        if (parent != dentry)
 312                dput(parent);
 313        spin_lock(&dcache_lock);
 314}
 315
 316/**
 317 * prune_dcache - shrink the dcache
 318 * @count: number of entries to try and free
 319 *
 320 * Shrink the dcache. This is done when we need
 321 * more memory, or simply when we need to unmount
 322 * something (at which point we need to unuse
 323 * all dentries).
 324 *
 325 * This function may fail to free any resources if
 326 * all the dentries are in use.
 327 */
 328 
 329void prune_dcache(int count)
 330{
 331        spin_lock(&dcache_lock);
 332        for (;;) {
 333                struct dentry *dentry;
 334                struct list_head *tmp;
 335
 336                tmp = dentry_unused.prev;
 337
 338                if (tmp == &dentry_unused)
 339                        break;
 340                list_del_init(tmp);
 341                dentry = list_entry(tmp, struct dentry, d_lru);
 342
 343                /* If the dentry was recently referenced, don't free it. */
 344                if (dentry->d_vfs_flags & DCACHE_REFERENCED) {
 345                        dentry->d_vfs_flags &= ~DCACHE_REFERENCED;
 346                        list_add(&dentry->d_lru, &dentry_unused);
 347                        continue;
 348                }
 349                dentry_stat.nr_unused--;
 350
 351                /* Unused dentry with a count? */
 352                if (atomic_read(&dentry->d_count))
 353                        BUG();
 354
 355                prune_one_dentry(dentry);
 356                if (!--count)
 357                        break;
 358        }
 359        spin_unlock(&dcache_lock);
 360}
 361
 362/*
 363 * Shrink the dcache for the specified super block.
 364 * This allows us to unmount a device without disturbing
 365 * the dcache for the other devices.
 366 *
 367 * This implementation makes just two traversals of the
 368 * unused list.  On the first pass we move the selected
 369 * dentries to the most recent end, and on the second
 370 * pass we free them.  The second pass must restart after
 371 * each dput(), but since the target dentries are all at
 372 * the end, it's really just a single traversal.
 373 */
 374
 375/**
 376 * shrink_dcache_sb - shrink dcache for a superblock
 377 * @sb: superblock
 378 *
 379 * Shrink the dcache for the specified super block. This
 380 * is used to free the dcache before unmounting a file
 381 * system
 382 */
 383
 384void shrink_dcache_sb(struct super_block * sb)
 385{
 386        struct list_head *tmp, *next;
 387        struct dentry *dentry;
 388
 389        /*
 390         * Pass one ... move the dentries for the specified
 391         * superblock to the most recent end of the unused list.
 392         */
 393        spin_lock(&dcache_lock);
 394        next = dentry_unused.next;
 395        while (next != &dentry_unused) {
 396                tmp = next;
 397                next = tmp->next;
 398                dentry = list_entry(tmp, struct dentry, d_lru);
 399                if (dentry->d_sb != sb)
 400                        continue;
 401                list_del(tmp);
 402                list_add(tmp, &dentry_unused);
 403        }
 404
 405        /*
 406         * Pass two ... free the dentries for this superblock.
 407         */
 408repeat:
 409        next = dentry_unused.next;
 410        while (next != &dentry_unused) {
 411                tmp = next;
 412                next = tmp->next;
 413                dentry = list_entry(tmp, struct dentry, d_lru);
 414                if (dentry->d_sb != sb)
 415                        continue;
 416                if (atomic_read(&dentry->d_count))
 417                        continue;
 418                dentry_stat.nr_unused--;
 419                list_del_init(tmp);
 420                prune_one_dentry(dentry);
 421                goto repeat;
 422        }
 423        spin_unlock(&dcache_lock);
 424}
 425
 426/*
 427 * Search for at least 1 mount point in the dentry's subdirs.
 428 * We descend to the next level whenever the d_subdirs
 429 * list is non-empty and continue searching.
 430 */
 431 
 432/**
 433 * have_submounts - check for mounts over a dentry
 434 * @parent: dentry to check.
 435 *
 436 * Return true if the parent or its subdirectories contain
 437 * a mount point
 438 */
 439 
 440int have_submounts(struct dentry *parent)
 441{
 442        struct dentry *this_parent = parent;
 443        struct list_head *next;
 444
 445        spin_lock(&dcache_lock);
 446        if (d_mountpoint(parent))
 447                goto positive;
 448repeat:
 449        next = this_parent->d_subdirs.next;
 450resume:
 451        while (next != &this_parent->d_subdirs) {
 452                struct list_head *tmp = next;
 453                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
 454                next = tmp->next;
 455                /* Have we found a mount point ? */
 456                if (d_mountpoint(dentry))
 457                        goto positive;
 458                if (!list_empty(&dentry->d_subdirs)) {
 459                        this_parent = dentry;
 460                        goto repeat;
 461                }
 462        }
 463        /*
 464         * All done at this level ... ascend and resume the search.
 465         */
 466        if (this_parent != parent) {
 467                next = this_parent->d_child.next; 
 468                this_parent = this_parent->d_parent;
 469                goto resume;
 470        }
 471        spin_unlock(&dcache_lock);
 472        return 0; /* No mount points found in tree */
 473positive:
 474        spin_unlock(&dcache_lock);
 475        return 1;
 476}
 477
 478/*
 479 * Search the dentry child list for the specified parent,
 480 * and move any unused dentries to the end of the unused
 481 * list for prune_dcache(). We descend to the next level
 482 * whenever the d_subdirs list is non-empty and continue
 483 * searching.
 484 */
 485static int select_parent(struct dentry * parent)
 486{
 487        struct dentry *this_parent = parent;
 488        struct list_head *next;
 489        int found = 0;
 490
 491        spin_lock(&dcache_lock);
 492repeat:
 493        next = this_parent->d_subdirs.next;
 494resume:
 495        while (next != &this_parent->d_subdirs) {
 496                struct list_head *tmp = next;
 497                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
 498                next = tmp->next;
 499                if (!atomic_read(&dentry->d_count)) {
 500                        list_del(&dentry->d_lru);
 501                        list_add(&dentry->d_lru, dentry_unused.prev);
 502                        found++;
 503                }
 504                /*
 505                 * Descend a level if the d_subdirs list is non-empty.
 506                 */
 507                if (!list_empty(&dentry->d_subdirs)) {
 508                        this_parent = dentry;
 509#ifdef DCACHE_DEBUG
 510printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
 511dentry->d_parent->d_name.name, dentry->d_name.name, found);
 512#endif
 513                        goto repeat;
 514                }
 515        }
 516        /*
 517         * All done at this level ... ascend and resume the search.
 518         */
 519        if (this_parent != parent) {
 520                next = this_parent->d_child.next; 
 521                this_parent = this_parent->d_parent;
 522#ifdef DCACHE_DEBUG
 523printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
 524this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
 525#endif
 526                goto resume;
 527        }
 528        spin_unlock(&dcache_lock);
 529        return found;
 530}
 531
 532/**
 533 * shrink_dcache_parent - prune dcache
 534 * @parent: parent of entries to prune
 535 *
 536 * Prune the dcache to remove unused children of the parent dentry.
 537 */
 538 
 539void shrink_dcache_parent(struct dentry * parent)
 540{
 541        int found;
 542
 543        while ((found = select_parent(parent)) != 0)
 544                prune_dcache(found);
 545}
 546
 547/**
 548 * shrink_dcache_anon - further prune the cache
 549 * @head: head of d_hash list of dentries to prune
 550 *
 551 * Prune the dentries that are anonymous
 552 *
 553 */
 554void shrink_dcache_anon(struct list_head *head)
 555{
 556        struct list_head *lp;
 557        int found;
 558        do {
 559                found = 0;
 560                spin_lock(&dcache_lock);
 561                list_for_each(lp, head) {
 562                        struct dentry *this = list_entry(lp, struct dentry, d_hash);
 563                        if (!atomic_read(&this->d_count)) {
 564                                list_del(&this->d_lru);
 565                                list_add_tail(&this->d_lru, &dentry_unused);
 566                                found++;
 567                        }
 568                }
 569                spin_unlock(&dcache_lock);
 570                prune_dcache(found);
 571        } while(found);
 572}
 573
 574/*
 575 * This is called from kswapd when we think we need some
 576 * more memory, but aren't really sure how much. So we
 577 * carefully try to free a _bit_ of our dcache, but not
 578 * too much.
 579 *
 580 * Priority:
 581 *   1 - very urgent: shrink everything
 582 *  ...
 583 *   6 - base-level: try to shrink a bit.
 584 */
 585int shrink_dcache_memory(int priority, unsigned int gfp_mask)
 586{
 587        int count = 0;
 588
 589        /*
 590         * Nasty deadlock avoidance.
 591         *
 592         * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
 593         * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->
 594         * put_inode->ext2_discard_prealloc->ext2_free_blocks->lock_super->
 595         * DEADLOCK.
 596         *
 597         * We should make sure we don't hold the superblock lock over
 598         * block allocations, but for now:
 599         */
 600        if (!(gfp_mask & __GFP_FS))
 601                return 0;
 602
 603        count = dentry_stat.nr_unused / priority;
 604
 605        prune_dcache(count);
 606        kmem_cache_shrink(dentry_cache);
 607        return 0;
 608}
 609
 610#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
 611
 612/**
 613 * d_alloc      -       allocate a dcache entry
 614 * @parent: parent of entry to allocate
 615 * @name: qstr of the name
 616 *
 617 * Allocates a dentry. It returns %NULL if there is insufficient memory
 618 * available. On a success the dentry is returned. The name passed in is
 619 * copied and the copy passed in may be reused after this call.
 620 */
 621 
 622struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
 623{
 624        char * str;
 625        struct dentry *dentry;
 626
 627        dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
 628        if (!dentry)
 629                return NULL;
 630
 631        if (name->len > DNAME_INLINE_LEN-1) {
 632                str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
 633                if (!str) {
 634                        kmem_cache_free(dentry_cache, dentry); 
 635                        return NULL;
 636                }
 637        } else
 638                str = dentry->d_iname; 
 639
 640        memcpy(str, name->name, name->len);
 641        str[name->len] = 0;
 642
 643        atomic_set(&dentry->d_count, 1);
 644        dentry->d_vfs_flags = 0;
 645        dentry->d_flags = 0;
 646        dentry->d_inode = NULL;
 647        dentry->d_parent = NULL;
 648        dentry->d_sb = NULL;
 649        dentry->d_name.name = str;
 650        dentry->d_name.len = name->len;
 651        dentry->d_name.hash = name->hash;
 652        dentry->d_op = NULL;
 653        dentry->d_fsdata = NULL;
 654        dentry->d_mounted = 0;
 655        INIT_LIST_HEAD(&dentry->d_hash);
 656        INIT_LIST_HEAD(&dentry->d_lru);
 657        INIT_LIST_HEAD(&dentry->d_subdirs);
 658        INIT_LIST_HEAD(&dentry->d_alias);
 659        if (parent) {
 660                dentry->d_parent = dget(parent);
 661                dentry->d_sb = parent->d_sb;
 662                spin_lock(&dcache_lock);
 663                list_add(&dentry->d_child, &parent->d_subdirs);
 664                spin_unlock(&dcache_lock);
 665        } else
 666                INIT_LIST_HEAD(&dentry->d_child);
 667
 668        dentry_stat.nr_dentry++;
 669        return dentry;
 670}
 671
 672/**
 673 * d_instantiate - fill in inode information for a dentry
 674 * @entry: dentry to complete
 675 * @inode: inode to attach to this dentry
 676 *
 677 * Fill in inode information in the entry.
 678 *
 679 * This turns negative dentries into productive full members
 680 * of society.
 681 *
 682 * NOTE! This assumes that the inode count has been incremented
 683 * (or otherwise set) by the caller to indicate that it is now
 684 * in use by the dcache.
 685 */
 686 
 687void d_instantiate(struct dentry *entry, struct inode * inode)
 688{
 689        if (!list_empty(&entry->d_alias)) BUG();
 690        spin_lock(&dcache_lock);
 691        if (inode)
 692                list_add(&entry->d_alias, &inode->i_dentry);
 693        entry->d_inode = inode;
 694        spin_unlock(&dcache_lock);
 695}
 696
 697/**
 698 * d_alloc_root - allocate root dentry
 699 * @root_inode: inode to allocate the root for
 700 *
 701 * Allocate a root ("/") dentry for the inode given. The inode is
 702 * instantiated and returned. %NULL is returned if there is insufficient
 703 * memory or the inode passed is %NULL.
 704 */
 705 
 706struct dentry * d_alloc_root(struct inode * root_inode)
 707{
 708        struct dentry *res = NULL;
 709
 710        if (root_inode) {
 711                res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
 712                if (res) {
 713                        res->d_sb = root_inode->i_sb;
 714                        res->d_parent = res;
 715                        d_instantiate(res, root_inode);
 716                }
 717        }
 718        return res;
 719}
 720
 721static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
 722{
 723        hash += (unsigned long) parent / L1_CACHE_BYTES;
 724        hash = hash ^ (hash >> D_HASHBITS);
 725        return dentry_hashtable + (hash & D_HASHMASK);
 726}
 727
 728/**
 729 * d_alloc_anon - allocate an anonymous dentry
 730 * @inode: inode to allocate the dentry for
 731 *
 732 * This is similar to d_alloc_root.  It is used by filesystems when
 733 * creating a dentry for a given inode, often in the process of 
 734 * mapping a filehandle to a dentry.  The returned dentry may be
 735 * anonymous, or may have a full name (if the inode was already
 736 * in the cache).  The file system may need to make further
 737 * efforts to connect this dentry into the dcache properly.
 738 *
 739 * When called on a directory inode, we must ensure that
 740 * the inode only ever has one dentry.  If a dentry is
 741 * found, that is returned instead of allocating a new one.
 742 *
 743 * On successful return, the reference to the inode has been transferred
 744 * to the dentry.  If %NULL is returned (indicating kmalloc failure),
 745 * the reference on the inode has not been released.
 746 */
 747
 748struct dentry * d_alloc_anon(struct inode *inode)
 749{
 750        struct dentry *tmp;
 751        struct dentry *res;
 752
 753        if ((res = d_find_alias(inode))) {
 754                iput(inode);
 755                return res;
 756        }
 757
 758        tmp = d_alloc(NULL, &(const struct qstr) {"",0,0});
 759        if (!tmp)
 760                return NULL;
 761
 762        tmp->d_parent = tmp; /* make sure dput doesn't croak */
 763        
 764        spin_lock(&dcache_lock);
 765        if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
 766                /* A directory can only have one dentry.
 767                 * This (now) has one, so use it.
 768                 */
 769                res = list_entry(inode->i_dentry.next, struct dentry, d_alias);
 770                __dget_locked(res);
 771        } else {
 772                /* attach a disconnected dentry */
 773                res = tmp;
 774                tmp = NULL;
 775                if (res) {
 776                        res->d_sb = inode->i_sb;
 777                        res->d_parent = res;
 778                        res->d_inode = inode;
 779                        res->d_flags |= DCACHE_DISCONNECTED;
 780                        list_add(&res->d_alias, &inode->i_dentry);
 781                        list_add(&res->d_hash, &inode->i_sb->s_anon);
 782                }
 783                inode = NULL; /* don't drop reference */
 784        }
 785        spin_unlock(&dcache_lock);
 786
 787        if (inode)
 788                iput(inode);
 789        if (tmp)
 790                dput(tmp);
 791        return res;
 792}
 793
 794
 795/**
 796 * d_splice_alias - splice a disconnected dentry into the tree if one exists
 797 * @inode:  the inode which may have a disconnected dentry
 798 * @dentry: a negative dentry which we want to point to the inode.
 799 *
 800 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
 801 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
 802 * and return it, else simply d_add the inode to the dentry and return NULL.
 803 *
 804 * This is (will be) needed in the lookup routine of any filesystem that is exportable
 805 * (via knfsd) so that we can build dcache paths to directories effectively.
 806 *
 807 * If a dentry was found and moved, then it is returned.  Otherwise NULL
 808 * is returned.  This matches the expected return value of ->lookup.
 809 *
 810 */
 811struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
 812{
 813        struct dentry *new = NULL;
 814
 815        if (inode && S_ISDIR(inode->i_mode)) {
 816                spin_lock(&dcache_lock);
 817                if (!list_empty(&inode->i_dentry)) {
 818                        new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
 819                        __dget_locked(new);
 820                        spin_unlock(&dcache_lock);
 821                        d_rehash(dentry);
 822                        d_move(new, dentry);
 823                        iput(inode);
 824                } else {
 825                        /* d_instantiate takes dcache_lock, so we do it by hand */
 826                        list_add(&dentry->d_alias, &inode->i_dentry);
 827                        dentry->d_inode = inode;
 828                        spin_unlock(&dcache_lock);
 829                        d_rehash(dentry);
 830                }
 831        } else
 832                d_add(dentry, inode);
 833        return new;
 834}
 835
 836
 837/**
 838 * d_lookup - search for a dentry
 839 * @parent: parent dentry
 840 * @name: qstr of name we wish to find
 841 *
 842 * Searches the children of the parent dentry for the name in question. If
 843 * the dentry is found its reference count is incremented and the dentry
 844 * is returned. The caller must use d_put to free the entry when it has
 845 * finished using it. %NULL is returned on failure.
 846 */
 847 
 848struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
 849{
 850        struct dentry * dentry;
 851        spin_lock(&dcache_lock);
 852        dentry = __d_lookup(parent,name);
 853        if (dentry)
 854                __dget_locked(dentry);
 855        spin_unlock(&dcache_lock);
 856        return dentry;
 857}
 858
 859struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)  
 860{
 861
 862        unsigned int len = name->len;
 863        unsigned int hash = name->hash;
 864        const unsigned char *str = name->name;
 865        struct list_head *head = d_hash(parent,hash);
 866        struct list_head *tmp;
 867
 868        tmp = head->next;
 869        for (;;) {
 870                struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
 871                if (tmp == head)
 872                        break;
 873                tmp = tmp->next;
 874                if (dentry->d_name.hash != hash)
 875                        continue;
 876                if (dentry->d_parent != parent)
 877                        continue;
 878                if (parent->d_op && parent->d_op->d_compare) {
 879                        if (parent->d_op->d_compare(parent, &dentry->d_name, name))
 880                                continue;
 881                } else {
 882                        if (dentry->d_name.len != len)
 883                                continue;
 884                        if (memcmp(dentry->d_name.name, str, len))
 885                                continue;
 886                }
 887                return dentry;
 888        }
 889        return NULL;
 890}
 891
 892/**
 893 * d_validate - verify dentry provided from insecure source
 894 * @dentry: The dentry alleged to be valid child of @dparent
 895 * @dparent: The parent dentry (known to be valid)
 896 * @hash: Hash of the dentry
 897 * @len: Length of the name
 898 *
 899 * An insecure source has sent us a dentry, here we verify it and dget() it.
 900 * This is used by ncpfs in its readdir implementation.
 901 * Zero is returned in the dentry is invalid.
 902 */
 903 
 904int d_validate(struct dentry *dentry, struct dentry *dparent)
 905{
 906        unsigned long dent_addr = (unsigned long) dentry;
 907        unsigned long min_addr = PAGE_OFFSET;
 908        unsigned long align_mask = 0x0F;
 909        struct list_head *base, *lhp;
 910
 911        if (dent_addr < min_addr)
 912                goto out;
 913        if (dent_addr > (unsigned long)high_memory - sizeof(struct dentry))
 914                goto out;
 915        if (dent_addr & align_mask)
 916                goto out;
 917        if ((!kern_addr_valid(dent_addr)) || (!kern_addr_valid(dent_addr -1 +
 918                                                sizeof(struct dentry))))
 919                goto out;
 920
 921        if (dentry->d_parent != dparent)
 922                goto out;
 923
 924        spin_lock(&dcache_lock);
 925        lhp = base = d_hash(dparent, dentry->d_name.hash);
 926        while ((lhp = lhp->next) != base) {
 927                if (dentry == list_entry(lhp, struct dentry, d_hash)) {
 928                        __dget_locked(dentry);
 929                        spin_unlock(&dcache_lock);
 930                        return 1;
 931                }
 932        }
 933        spin_unlock(&dcache_lock);
 934out:
 935        return 0;
 936}
 937
 938/*
 939 * When a file is deleted, we have two options:
 940 * - turn this dentry into a negative dentry
 941 * - unhash this dentry and free it.
 942 *
 943 * Usually, we want to just turn this into
 944 * a negative dentry, but if anybody else is
 945 * currently using the dentry or the inode
 946 * we can't do that and we fall back on removing
 947 * it from the hash queues and waiting for
 948 * it to be deleted later when it has no users
 949 */
 950 
 951/**
 952 * d_delete - delete a dentry
 953 * @dentry: The dentry to delete
 954 *
 955 * Turn the dentry into a negative dentry if possible, otherwise
 956 * remove it from the hash queues so it can be deleted later
 957 */
 958 
 959void d_delete(struct dentry * dentry)
 960{
 961        /*
 962         * Are we the only user?
 963         */
 964        spin_lock(&dcache_lock);
 965        if (atomic_read(&dentry->d_count) == 1) {
 966                dentry_iput(dentry);
 967                return;
 968        }
 969        spin_unlock(&dcache_lock);
 970
 971        /*
 972         * If not, just drop the dentry and let dput
 973         * pick up the tab..
 974         */
 975        d_drop(dentry);
 976}
 977
 978/**
 979 * d_rehash     - add an entry back to the hash
 980 * @entry: dentry to add to the hash
 981 *
 982 * Adds a dentry to the hash according to its name.
 983 */
 984 
 985void d_rehash(struct dentry * entry)
 986{
 987        struct list_head *list = d_hash(entry->d_parent, entry->d_name.hash);
 988        if (!list_empty(&entry->d_hash)) BUG();
 989        spin_lock(&dcache_lock);
 990        list_add(&entry->d_hash, list);
 991        spin_unlock(&dcache_lock);
 992}
 993
 994#define do_switch(x,y) do { \
 995        __typeof__ (x) __tmp = x; \
 996        x = y; y = __tmp; } while (0)
 997
 998/*
 999 * When switching names, the actual string doesn't strictly have to
1000 * be preserved in the target - because we're dropping the target
1001 * anyway. As such, we can just do a simple memcpy() to copy over
1002 * the new name before we switch.
1003 *
1004 * Note that we have to be a lot more careful about getting the hash
1005 * switched - we have to switch the hash value properly even if it
1006 * then no longer matches the actual (corrupted) string of the target.
1007 * The hash value has to match the hash queue that the dentry is on..
1008 */
1009static inline void switch_names(struct dentry * dentry, struct dentry * target)
1010{
1011        const unsigned char *old_name, *new_name;
1012
1013        memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN); 
1014        old_name = target->d_name.name;
1015        new_name = dentry->d_name.name;
1016        if (old_name == target->d_iname)
1017                old_name = dentry->d_iname;
1018        if (new_name == dentry->d_iname)
1019                new_name = target->d_iname;
1020        target->d_name.name = new_name;
1021        dentry->d_name.name = old_name;
1022}
1023
1024/*
1025 * We cannibalize "target" when moving dentry on top of it,
1026 * because it's going to be thrown away anyway. We could be more
1027 * polite about it, though.
1028 *
1029 * This forceful removal will result in ugly /proc output if
1030 * somebody holds a file open that got deleted due to a rename.
1031 * We could be nicer about the deleted file, and let it show
1032 * up under the name it got deleted rather than the name that
1033 * deleted it.
1034 *
1035 * Careful with the hash switch. The hash switch depends on
1036 * the fact that any list-entry can be a head of the list.
1037 * Think about it.
1038 */
1039 
1040/**
1041 * d_move - move a dentry
1042 * @dentry: entry to move
1043 * @target: new dentry
1044 *
1045 * Update the dcache to reflect the move of a file name. Negative
1046 * dcache entries should not be moved in this way.
1047 */
1048  
1049void d_move(struct dentry * dentry, struct dentry * target)
1050{
1051        if (!dentry->d_inode)
1052                printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1053
1054        spin_lock(&dcache_lock);
1055        /* Move the dentry to the target hash queue */
1056        list_del(&dentry->d_hash);
1057        list_add(&dentry->d_hash, &target->d_hash);
1058
1059        /* Unhash the target: dput() will then get rid of it */
1060        list_del_init(&target->d_hash);
1061
1062        list_del(&dentry->d_child);
1063        list_del(&target->d_child);
1064
1065        /* Switch the names.. */
1066        switch_names(dentry, target);
1067        do_switch(dentry->d_name.len, target->d_name.len);
1068        do_switch(dentry->d_name.hash, target->d_name.hash);
1069        /* ... and switch the parents */
1070        write_lock(&dparent_lock);
1071        if (IS_ROOT(dentry)) {
1072                dentry->d_parent = target->d_parent;
1073                target->d_parent = target;
1074                INIT_LIST_HEAD(&target->d_child);
1075        } else {
1076                do_switch(dentry->d_parent, target->d_parent);
1077
1078                /* And add them back to the (new) parent lists */
1079                list_add(&target->d_child, &target->d_parent->d_subdirs);
1080        }
1081        write_unlock(&dparent_lock);
1082
1083        list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1084        spin_unlock(&dcache_lock);
1085}
1086
1087/**
1088 * d_path - return the path of a dentry
1089 * @dentry: dentry to report
1090 * @vfsmnt: vfsmnt to which the dentry belongs
1091 * @root: root dentry
1092 * @rootmnt: vfsmnt to which the root dentry belongs
1093 * @buffer: buffer to return value in
1094 * @buflen: buffer length
1095 *
1096 * Convert a dentry into an ASCII path name. If the entry has been deleted
1097 * the string " (deleted)" is appended. Note that this is ambiguous. Returns
1098 * the buffer.
1099 *
1100 * "buflen" should be %PAGE_SIZE or more. Caller holds the dcache_lock.
1101 */
1102char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1103                struct dentry *root, struct vfsmount *rootmnt,
1104                char *buffer, int buflen)
1105{
1106        char * end = buffer+buflen;
1107        char * retval;
1108        int namelen;
1109
1110        *--end = '\0';
1111        buflen--;
1112        if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
1113                buflen -= 10;
1114                end -= 10;
1115                memcpy(end, " (deleted)", 10);
1116        }
1117
1118        /* Get '/' right */
1119        retval = end-1;
1120        *retval = '/';
1121
1122        for (;;) {
1123                struct dentry * parent;
1124
1125                if (dentry == root && vfsmnt == rootmnt)
1126                        break;
1127                if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1128                        /* Global root? */
1129                        if (vfsmnt->mnt_parent == vfsmnt)
1130                                goto global_root;
1131                        dentry = vfsmnt->mnt_mountpoint;
1132                        vfsmnt = vfsmnt->mnt_parent;
1133                        continue;
1134                }
1135                parent = dentry->d_parent;
1136                namelen = dentry->d_name.len;
1137                buflen -= namelen + 1;
1138                if (buflen < 0)
1139                        break;
1140                end -= namelen;
1141                memcpy(end, dentry->d_name.name, namelen);
1142                *--end = '/';
1143                retval = end;
1144                dentry = parent;
1145        }
1146        return retval;
1147global_root:
1148        namelen = dentry->d_name.len;
1149        buflen -= namelen;
1150        if (buflen >= 0) {
1151                retval -= namelen-1;    /* hit the slash */
1152                memcpy(retval, dentry->d_name.name, namelen);
1153        }
1154        return retval;
1155}
1156
1157/*
1158 * NOTE! The user-level library version returns a
1159 * character pointer. The kernel system call just
1160 * returns the length of the buffer filled (which
1161 * includes the ending '\0' character), or a negative
1162 * error value. So libc would do something like
1163 *
1164 *      char *getcwd(char * buf, size_t size)
1165 *      {
1166 *              int retval;
1167 *
1168 *              retval = sys_getcwd(buf, size);
1169 *              if (retval >= 0)
1170 *                      return buf;
1171 *              errno = -retval;
1172 *              return NULL;
1173 *      }
1174 */
1175asmlinkage long sys_getcwd(char *buf, unsigned long size)
1176{
1177        int error;
1178        struct vfsmount *pwdmnt, *rootmnt;
1179        struct dentry *pwd, *root;
1180        char *page = (char *) __get_free_page(GFP_USER);
1181
1182        if (!page)
1183                return -ENOMEM;
1184
1185        read_lock(&current->fs->lock);
1186        pwdmnt = mntget(current->fs->pwdmnt);
1187        pwd = dget(current->fs->pwd);
1188        rootmnt = mntget(current->fs->rootmnt);
1189        root = dget(current->fs->root);
1190        read_unlock(&current->fs->lock);
1191
1192        error = -ENOENT;
1193        /* Has the current directory has been unlinked? */
1194        spin_lock(&dcache_lock);
1195        if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
1196                unsigned long len;
1197                char * cwd;
1198
1199                cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1200                spin_unlock(&dcache_lock);
1201
1202                error = -ERANGE;
1203                len = PAGE_SIZE + page - cwd;
1204                if (len <= size) {
1205                        error = len;
1206                        if (copy_to_user(buf, cwd, len))
1207                                error = -EFAULT;
1208                }
1209        } else
1210                spin_unlock(&dcache_lock);
1211        dput(pwd);
1212        mntput(pwdmnt);
1213        dput(root);
1214        mntput(rootmnt);
1215        free_page((unsigned long) page);
1216        return error;
1217}
1218
1219/*
1220 * Test whether new_dentry is a subdirectory of old_dentry.
1221 *
1222 * Trivially implemented using the dcache structure
1223 */
1224
1225/**
1226 * is_subdir - is new dentry a subdirectory of old_dentry
1227 * @new_dentry: new dentry
1228 * @old_dentry: old dentry
1229 *
1230 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1231 * Returns 0 otherwise.
1232 */
1233  
1234int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1235{
1236        int result;
1237
1238        result = 0;
1239        for (;;) {
1240                if (new_dentry != old_dentry) {
1241                        struct dentry * parent = new_dentry->d_parent;
1242                        if (parent == new_dentry)
1243                                break;
1244                        new_dentry = parent;
1245                        continue;
1246                }
1247                result = 1;
1248                break;
1249        }
1250        return result;
1251}
1252
1253void d_genocide(struct dentry *root)
1254{
1255        struct dentry *this_parent = root;
1256        struct list_head *next;
1257
1258        spin_lock(&dcache_lock);
1259repeat:
1260        next = this_parent->d_subdirs.next;
1261resume:
1262        while (next != &this_parent->d_subdirs) {
1263                struct list_head *tmp = next;
1264                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1265                next = tmp->next;
1266                if (d_unhashed(dentry)||!dentry->d_inode)
1267                        continue;
1268                if (!list_empty(&dentry->d_subdirs)) {
1269                        this_parent = dentry;
1270                        goto repeat;
1271                }
1272                atomic_dec(&dentry->d_count);
1273        }
1274        if (this_parent != root) {
1275                next = this_parent->d_child.next; 
1276                atomic_dec(&this_parent->d_count);
1277                this_parent = this_parent->d_parent;
1278                goto resume;
1279        }
1280        spin_unlock(&dcache_lock);
1281}
1282
1283/**
1284 * find_inode_number - check for dentry with name
1285 * @dir: directory to check
1286 * @name: Name to find.
1287 *
1288 * Check whether a dentry already exists for the given name,
1289 * and return the inode number if it has an inode. Otherwise
1290 * 0 is returned.
1291 *
1292 * This routine is used to post-process directory listings for
1293 * filesystems using synthetic inode numbers, and is necessary
1294 * to keep getcwd() working.
1295 */
1296 
1297ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1298{
1299        struct dentry * dentry;
1300        ino_t ino = 0;
1301
1302        /*
1303         * Check for a fs-specific hash function. Note that we must
1304         * calculate the standard hash first, as the d_op->d_hash()
1305         * routine may choose to leave the hash value unchanged.
1306         */
1307        name->hash = full_name_hash(name->name, name->len);
1308        if (dir->d_op && dir->d_op->d_hash)
1309        {
1310                if (dir->d_op->d_hash(dir, name) != 0)
1311                        goto out;
1312        }
1313
1314        dentry = d_lookup(dir, name);
1315        if (dentry)
1316        {
1317                if (dentry->d_inode)
1318                        ino = dentry->d_inode->i_ino;
1319                dput(dentry);
1320        }
1321out:
1322        return ino;
1323}
1324
1325static void __init dcache_init(unsigned long mempages)
1326{
1327        struct list_head *d;
1328        unsigned long order;
1329        unsigned int nr_hash;
1330        int i;
1331
1332        /* 
1333         * A constructor could be added for stable state like the lists,
1334         * but it is probably not worth it because of the cache nature
1335         * of the dcache. 
1336         * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1337         * flag could be removed here, to hint to the allocator that
1338         * it should not try to get multiple page regions.  
1339         */
1340        dentry_cache = kmem_cache_create("dentry_cache",
1341                                         sizeof(struct dentry),
1342                                         0,
1343                                         SLAB_HWCACHE_ALIGN,
1344                                         NULL, NULL);
1345        if (!dentry_cache)
1346                panic("Cannot create dentry cache");
1347
1348#if PAGE_SHIFT < 13
1349        mempages >>= (13 - PAGE_SHIFT);
1350#endif
1351        mempages *= sizeof(struct list_head);
1352        for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1353                ;
1354
1355        do {
1356                unsigned long tmp;
1357
1358                nr_hash = (1UL << order) * PAGE_SIZE /
1359                        sizeof(struct list_head);
1360                d_hash_mask = (nr_hash - 1);
1361
1362                tmp = nr_hash;
1363                d_hash_shift = 0;
1364                while ((tmp >>= 1UL) != 0UL)
1365                        d_hash_shift++;
1366
1367                dentry_hashtable = (struct list_head *)
1368                        __get_free_pages(GFP_ATOMIC, order);
1369        } while (dentry_hashtable == NULL && --order >= 0);
1370
1371        printk("Dentry-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1372                        nr_hash, order, (PAGE_SIZE << order));
1373
1374        if (!dentry_hashtable)
1375                panic("Failed to allocate dcache hash table\n");
1376
1377        d = dentry_hashtable;
1378        i = nr_hash;
1379        do {
1380                INIT_LIST_HEAD(d);
1381                d++;
1382                i--;
1383        } while (i);
1384}
1385
1386/* SLAB cache for __getname() consumers */
1387kmem_cache_t *names_cachep;
1388
1389/* SLAB cache for file structures */
1390kmem_cache_t *filp_cachep;
1391
1392/* SLAB cache for dquot structures */
1393kmem_cache_t *dquot_cachep;
1394
1395EXPORT_SYMBOL(d_genocide);
1396
1397extern void bdev_cache_init(void);
1398extern void cdev_cache_init(void);
1399
1400void __init vfs_caches_init(unsigned long mempages)
1401{
1402        names_cachep = kmem_cache_create("names_cache", 
1403                        PATH_MAX, 0, 
1404                        SLAB_HWCACHE_ALIGN, NULL, NULL);
1405        if (!names_cachep)
1406                panic("Cannot create names SLAB cache");
1407
1408        filp_cachep = kmem_cache_create("filp", 
1409                        sizeof(struct file), 0,
1410                        SLAB_HWCACHE_ALIGN, NULL, NULL);
1411        if(!filp_cachep)
1412                panic("Cannot create filp SLAB cache");
1413
1414#if defined (CONFIG_QUOTA)
1415        dquot_cachep = kmem_cache_create("dquot", 
1416                        sizeof(struct dquot), sizeof(unsigned long) * 4,
1417                        SLAB_HWCACHE_ALIGN, NULL, NULL);
1418        if (!dquot_cachep)
1419                panic("Cannot create dquot SLAB cache");
1420#endif
1421
1422        dcache_init(mempages);
1423        inode_init(mempages);
1424        files_init(mempages); 
1425        mnt_init(mempages);
1426        bdev_cache_init();
1427        cdev_cache_init();
1428}
1429
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.