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