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