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/string.h>
  18#include <linux/mm.h>
  19#include <linux/fs.h>
  20#include <linux/malloc.h>
  21#include <linux/slab.h>
  22#include <linux/init.h>
  23
  24#include <asm/uaccess.h>
  25#include <asm/cache.h>
  26
  27#define DCACHE_PARANOIA 1
  28/* #define DCACHE_DEBUG 1 */
  29
  30/* For managing the dcache */
  31extern unsigned long num_physpages, page_cache_size;
  32extern int inodes_stat[];
  33#define nr_inodes (inodes_stat[0])
  34
  35kmem_cache_t *dentry_cache; 
  36
  37/*
  38 * This is the single most critical data structure when it comes
  39 * to the dcache: the hashtable for lookups. Somebody should try
  40 * to make this good - I've just made it work.
  41 *
  42 * This hash-function tries to avoid losing too many bits of hash
  43 * information, yet avoid using a prime hash-size or similar.
  44 */
  45#define D_HASHBITS     d_hash_shift
  46#define D_HASHMASK     d_hash_mask
  47
  48static unsigned int d_hash_mask;
  49static unsigned int d_hash_shift;
  50static struct list_head *dentry_hashtable;
  51static LIST_HEAD(dentry_unused);
  52
  53struct {
  54        int nr_dentry;
  55        int nr_unused;
  56        int age_limit;          /* age in seconds */
  57        int want_pages;         /* pages requested by system */
  58        int dummy[2];
  59} dentry_stat = {0, 0, 45, 0,};
  60
  61static inline void d_free(struct dentry *dentry)
  62{
  63        if (dentry->d_op && dentry->d_op->d_release)
  64                dentry->d_op->d_release(dentry);
  65        if (dname_external(dentry)) 
  66                kfree(dentry->d_name.name);
  67        kmem_cache_free(dentry_cache, dentry); 
  68}
  69
  70/*
  71 * Release the dentry's inode, using the filesystem
  72 * d_iput() operation if defined.
  73 */
  74static inline int dentry_iput(struct dentry * dentry)
  75{
  76        struct inode *inode = dentry->d_inode;
  77        int ret = 0;
  78
  79        if (inode) {
  80                dentry->d_inode = NULL;
  81                list_del(&dentry->d_alias);
  82                INIT_LIST_HEAD(&dentry->d_alias);
  83                ret = inode->i_count == 1;
  84                if (dentry->d_op && dentry->d_op->d_iput)
  85                        dentry->d_op->d_iput(dentry, inode);
  86                else
  87                        iput(inode);
  88        }
  89
  90        return ret;
  91}
  92
  93/*
  94 * dput()
  95 *
  96 * This is complicated by the fact that we do not want to put
  97 * dentries that are no longer on any hash chain on the unused
  98 * list: we'd much rather just get rid of them immediately.
  99 *
 100 * However, that implies that we have to traverse the dentry
 101 * tree upwards to the parents which might _also_ now be
 102 * scheduled for deletion (it may have been only waiting for
 103 * its last child to go away).
 104 *
 105 * This tail recursion is done by hand as we don't want to depend
 106 * on the compiler to always get this right (gcc generally doesn't).
 107 * Real recursion would eat up our stack space.
 108 */
 109void dput(struct dentry *dentry)
 110{
 111        int count;
 112
 113        if (!dentry)
 114                return;
 115
 116repeat:
 117        count = dentry->d_count - 1;
 118        if (count != 0)
 119                goto out;
 120
 121        /*
 122         * Note that if d_op->d_delete blocks,
 123         * the dentry could go back in use.
 124         * Each fs will have to watch for this.
 125         */
 126        if (dentry->d_op && dentry->d_op->d_delete) {
 127                dentry->d_op->d_delete(dentry);
 128
 129                count = dentry->d_count - 1;
 130                if (count != 0)
 131                        goto out;
 132        }
 133
 134        if (!list_empty(&dentry->d_lru)) {
 135                dentry_stat.nr_unused--;
 136                list_del(&dentry->d_lru);
 137        }
 138        if (list_empty(&dentry->d_hash)) {
 139                struct dentry * parent;
 140
 141                list_del(&dentry->d_child);
 142                dentry_iput(dentry);
 143                parent = dentry->d_parent;
 144                d_free(dentry);
 145                if (dentry == parent)
 146                        return;
 147                dentry = parent;
 148                goto repeat;
 149        }
 150        list_add(&dentry->d_lru, &dentry_unused);
 151        dentry_stat.nr_unused++;
 152        /*
 153         * Update the timestamp
 154         */
 155        dentry->d_reftime = jiffies;
 156
 157out:
 158        if (count >= 0) {
 159                dentry->d_count = count;
 160                return;
 161        }
 162
 163        printk(KERN_CRIT "Negative d_count (%d) for %s/%s\n",
 164                count,
 165                dentry->d_parent->d_name.name,
 166                dentry->d_name.name);
 167        *(int *)0 = 0;  
 168}
 169
 170/*
 171 * Try to invalidate the dentry if it turns out to be
 172 * possible. If there are other dentries that can be
 173 * reached through this one we can't delete it.
 174 */
 175int d_invalidate(struct dentry * dentry)
 176{
 177        /*
 178         * If it's already been dropped, return OK.
 179         */
 180        if (list_empty(&dentry->d_hash))
 181                return 0;
 182        /*
 183         * Check whether to do a partial shrink_dcache
 184         * to get rid of unused child entries.
 185         */
 186        if (!list_empty(&dentry->d_subdirs)) {
 187                shrink_dcache_parent(dentry);
 188        }
 189
 190        /*
 191         * Somebody else still using it?
 192         *
 193         * If it's a directory, we can't drop it
 194         * for fear of somebody re-populating it
 195         * with children (even though dropping it
 196         * would make it unreachable from the root,
 197         * we might still populate it if it was a
 198         * working directory or similar).
 199         */
 200        if (dentry->d_count > 1) {
 201                if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
 202                        return -EBUSY;
 203        }
 204
 205        d_drop(dentry);
 206        return 0;
 207}
 208
 209/*
 210 * Throw away a dentry - free the inode, dput the parent.
 211 * This requires that the LRU list has already been
 212 * removed.
 213 */
 214static inline int prune_one_dentry(struct dentry * dentry)
 215{
 216        struct dentry * parent;
 217        int ret;
 218
 219        list_del(&dentry->d_hash);
 220        list_del(&dentry->d_child);
 221        ret = dentry_iput(dentry);
 222        parent = dentry->d_parent;
 223        d_free(dentry);
 224        if (parent != dentry)
 225                dput(parent);
 226
 227        return ret;
 228}
 229
 230/*
 231 * Shrink the dcache. This is done when we need
 232 * more memory, or simply when we need to unmount
 233 * something (at which point we need to unuse
 234 * all dentries).
 235 *
 236 * If you don't want a limit on the number of
 237 * inodes that will be released then call
 238 * with i_nr = -1.
 239 *
 240 * If you don't want a limit on the number of
 241 * dentries that will be released then call
 242 * with d_nr = 0.
 243 *
 244 * Returns the number of inodes released.
 245 */
 246int prune_dcache(int d_nr, int i_nr)
 247{
 248        int __i_nr = i_nr;
 249
 250        for (;;) {
 251                struct dentry *dentry;
 252                struct list_head *tmp = dentry_unused.prev;
 253
 254                if (tmp == &dentry_unused)
 255                        break;
 256                list_del(tmp);
 257                dentry = list_entry(tmp, struct dentry, d_lru);
 258                if (dentry->d_flags & DCACHE_REFERENCED) {
 259                        dentry->d_flags &= ~DCACHE_REFERENCED;
 260                        list_add(&dentry->d_lru, &dentry_unused);
 261                        continue;
 262                }
 263                dentry_stat.nr_unused--;
 264                INIT_LIST_HEAD(tmp);
 265                if (!dentry->d_count) {
 266                        i_nr -= prune_one_dentry(dentry);
 267                        if (!i_nr)
 268                                break;
 269                        if (!--d_nr)
 270                                break;
 271                }
 272        }
 273
 274        return __i_nr - i_nr;
 275}
 276
 277/*
 278 * Shrink the dcache for the specified super block.
 279 * This allows us to unmount a device without disturbing
 280 * the dcache for the other devices.
 281 *
 282 * This implementation makes just two traversals of the
 283 * unused list.  On the first pass we move the selected
 284 * dentries to the most recent end, and on the second
 285 * pass we free them.  The second pass must restart after
 286 * each dput(), but since the target dentries are all at
 287 * the end, it's really just a single traversal.
 288 */
 289void shrink_dcache_sb(struct super_block * sb)
 290{
 291        struct list_head *tmp, *next;
 292        struct dentry *dentry;
 293
 294        /*
 295         * Pass one ... move the dentries for the specified
 296         * superblock to the most recent end of the unused list.
 297         */
 298        next = dentry_unused.next;
 299        while (next != &dentry_unused) {
 300                tmp = next;
 301                next = tmp->next;
 302                dentry = list_entry(tmp, struct dentry, d_lru);
 303                if (dentry->d_sb != sb)
 304                        continue;
 305                list_del(tmp);
 306                list_add(tmp, &dentry_unused);
 307        }
 308
 309        /*
 310         * Pass two ... free the dentries for this superblock.
 311         */
 312repeat:
 313        next = dentry_unused.next;
 314        while (next != &dentry_unused) {
 315                tmp = next;
 316                next = tmp->next;
 317                dentry = list_entry(tmp, struct dentry, d_lru);
 318                if (dentry->d_sb != sb)
 319                        continue;
 320                if (dentry->d_count)
 321                        continue;
 322                dentry_stat.nr_unused--;
 323                list_del(tmp);
 324                INIT_LIST_HEAD(tmp);
 325                prune_one_dentry(dentry);
 326                goto repeat;
 327        }
 328}
 329
 330/*
 331 * Check whether a root dentry would be in use if all of its
 332 * child dentries were freed. This allows a non-destructive
 333 * test for unmounting a device.
 334 */
 335int is_root_busy(struct dentry *root)
 336{
 337        struct dentry *this_parent = root;
 338        struct list_head *next;
 339        int count = root->d_count;
 340
 341repeat:
 342        next = this_parent->d_subdirs.next;
 343resume:
 344        while (next != &this_parent->d_subdirs) {
 345                struct list_head *tmp = next;
 346                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
 347                next = tmp->next;
 348                /* Decrement count for unused children */
 349                count += (dentry->d_count - 1);
 350                if (!list_empty(&dentry->d_subdirs)) {
 351                        this_parent = dentry;
 352                        goto repeat;
 353                }
 354                /* root is busy if any leaf is busy */
 355                if (dentry->d_count)
 356                        return 1;
 357        }
 358        /*
 359         * All done at this level ... ascend and resume the search.
 360         */
 361        if (this_parent != root) {
 362                next = this_parent->d_child.next; 
 363                this_parent = this_parent->d_parent;
 364                goto resume;
 365        }
 366        return (count > 1); /* remaining users? */
 367}
 368
 369/*
 370 * Search for at least 1 mount point in the dentry's subdirs.
 371 * We descend to the next level whenever the d_subdirs
 372 * list is non-empty and continue searching.
 373 */
 374int have_submounts(struct dentry *parent)
 375{
 376        struct dentry *this_parent = parent;
 377        struct list_head *next;
 378
 379        if (parent->d_mounts != parent)
 380                return 1;
 381repeat:
 382        next = this_parent->d_subdirs.next;
 383resume:
 384        while (next != &this_parent->d_subdirs) {
 385                struct list_head *tmp = next;
 386                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
 387                next = tmp->next;
 388                /* Have we found a mount point ? */
 389                if (dentry->d_mounts != dentry)
 390                        return 1;
 391                if (!list_empty(&dentry->d_subdirs)) {
 392                        this_parent = dentry;
 393                        goto repeat;
 394                }
 395        }
 396        /*
 397         * All done at this level ... ascend and resume the search.
 398         */
 399        if (this_parent != parent) {
 400                next = this_parent->d_child.next;
 401                this_parent = this_parent->d_parent;
 402                goto resume;
 403        }
 404        return 0; /* No mount points found in tree */
 405}
 406
 407/*
 408 * Search the dentry child list for the specified parent,
 409 * and move any unused dentries to the end of the unused
 410 * list for prune_dcache(). We descend to the next level
 411 * whenever the d_subdirs list is non-empty and continue
 412 * searching.
 413 */
 414static int select_parent(struct dentry * parent)
 415{
 416        struct dentry *this_parent = parent;
 417        struct list_head *next;
 418        int found = 0;
 419
 420repeat:
 421        next = this_parent->d_subdirs.next;
 422resume:
 423        while (next != &this_parent->d_subdirs) {
 424                struct list_head *tmp = next;
 425                struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
 426                next = tmp->next;
 427                if (!dentry->d_count) {
 428                        list_del(&dentry->d_lru);
 429                        list_add(&dentry->d_lru, dentry_unused.prev);
 430                        found++;
 431                }
 432                /*
 433                 * Descend a level if the d_subdirs list is non-empty.
 434                 */
 435                if (!list_empty(&dentry->d_subdirs)) {
 436                        this_parent = dentry;
 437#ifdef DCACHE_DEBUG
 438printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
 439dentry->d_parent->d_name.name, dentry->d_name.name, found);
 440#endif
 441                        goto repeat;
 442                }
 443        }
 444        /*
 445         * All done at this level ... ascend and resume the search.
 446         */
 447        if (this_parent != parent) {
 448                next = this_parent->d_child.next; 
 449                this_parent = this_parent->d_parent;
 450#ifdef DCACHE_DEBUG
 451printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
 452this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
 453#endif
 454                goto resume;
 455        }
 456        return found;
 457}
 458
 459/*
 460 * Prune the dcache to remove unused children of the parent dentry.
 461 */
 462void shrink_dcache_parent(struct dentry * parent)
 463{
 464        int found;
 465
 466        while ((found = select_parent(parent)) != 0)
 467                prune_dcache(found, -1);
 468}
 469
 470/*
 471 * This is called from kswapd when we think we need some
 472 * more memory, but aren't really sure how much. So we
 473 * carefully try to free a _bit_ of our dcache, but not
 474 * too much.
 475 *
 476 * Priority:
 477 *   0 - very urgent: shrink everything
 478 *  ...
 479 *   6 - base-level: try to shrink a bit.
 480 */
 481void shrink_dcache_memory(int priority, unsigned int gfp_mask)
 482{
 483        if (gfp_mask & __GFP_IO && !current->fs_locks) {
 484                int count = 0;
 485                if (priority > 1)
 486                        count = dentry_stat.nr_unused / priority;
 487                prune_dcache(count, -1);
 488        }
 489}
 490
 491#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
 492
 493struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
 494{
 495        char * str;
 496        struct dentry *dentry;
 497
 498        dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
 499        if (!dentry)
 500                return NULL;
 501
 502        if (name->len > DNAME_INLINE_LEN-1) {
 503                str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
 504                if (!str) {
 505                        kmem_cache_free(dentry_cache, dentry); 
 506                        return NULL;
 507                }
 508        } else
 509                str = dentry->d_iname; 
 510
 511        memcpy(str, name->name, name->len);
 512        str[name->len] = 0;
 513
 514        dentry->d_count = 1;
 515        dentry->d_flags = 0;
 516        dentry->d_inode = NULL;
 517        dentry->d_parent = NULL;
 518        dentry->d_sb = NULL;
 519        if (parent) {
 520                dentry->d_parent = dget(parent);
 521                dentry->d_sb = parent->d_sb;
 522                list_add(&dentry->d_child, &parent->d_subdirs);
 523        } else
 524                INIT_LIST_HEAD(&dentry->d_child);
 525                
 526        dentry->d_mounts = dentry;
 527        dentry->d_covers = dentry;
 528        INIT_LIST_HEAD(&dentry->d_hash);
 529        INIT_LIST_HEAD(&dentry->d_lru);
 530        INIT_LIST_HEAD(&dentry->d_subdirs);
 531        INIT_LIST_HEAD(&dentry->d_alias);
 532
 533        dentry->d_name.name = str;
 534        dentry->d_name.len = name->len;
 535        dentry->d_name.hash = name->hash;
 536        dentry->d_op = NULL;
 537        dentry->d_fsdata = NULL;
 538        return dentry;
 539}
 540
 541/*
 542 * Fill in inode information in the entry.
 543 *
 544 * This turns negative dentries into productive full members
 545 * of society.
 546 *
 547 * NOTE! This assumes that the inode count has been incremented
 548 * (or otherwise set) by the caller to indicate that it is now
 549 * in use by the dcache..
 550 */
 551void d_instantiate(struct dentry *entry, struct inode * inode)
 552{
 553        if (inode)
 554                list_add(&entry->d_alias, &inode->i_dentry);
 555        entry->d_inode = inode;
 556}
 557
 558struct dentry * d_alloc_root(struct inode * root_inode, struct dentry *old_root)
 559{
 560        struct dentry *res = NULL;
 561
 562        if (root_inode) {
 563                res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
 564                if (res) {
 565                        res->d_sb = root_inode->i_sb;
 566                        res->d_parent = res;
 567                        d_instantiate(res, root_inode);
 568                }
 569        }
 570        return res;
 571}
 572
 573static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
 574{
 575        hash += (unsigned long) parent / L1_CACHE_BYTES;
 576        hash = hash ^ (hash >> D_HASHBITS);
 577        return dentry_hashtable + (hash & D_HASHMASK);
 578}
 579
 580struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
 581{
 582        unsigned int len = name->len;
 583        unsigned int hash = name->hash;
 584        const unsigned char *str = name->name;
 585        struct list_head *head = d_hash(parent,hash);
 586        struct list_head *tmp = head->next;
 587
 588        for (;;) {
 589                struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
 590                if (tmp == head)
 591                        break;
 592                tmp = tmp->next;
 593                if (dentry->d_name.hash != hash)
 594                        continue;
 595                if (dentry->d_parent != parent)
 596                        continue;
 597                if (parent->d_op && parent->d_op->d_compare) {
 598                        if (parent->d_op->d_compare(parent, &dentry->d_name, name))
 599                                continue;
 600                } else {
 601                        if (dentry->d_name.len != len)
 602                                continue;
 603                        if (memcmp(dentry->d_name.name, str, len))
 604                                continue;
 605                }
 606                dentry->d_flags |= DCACHE_REFERENCED;
 607                return dget(dentry);
 608        }
 609        return NULL;
 610}
 611
 612/*
 613 * An insecure source has sent us a dentry, here we verify it.
 614 *
 615 * This is just to make knfsd able to have the dentry pointer
 616 * in the NFS file handle.
 617 *
 618 * NOTE! Do _not_ dereference the pointers before we have
 619 * validated them. We can test the pointer values, but we
 620 * must not actually use them until we have found a valid
 621 * copy of the pointer in kernel space..
 622 */
 623int d_validate(struct dentry *dentry, struct dentry *dparent,
 624               unsigned int hash, unsigned int len)
 625{
 626        struct list_head *base, *lhp;
 627        int valid = 1;
 628
 629        if (dentry != dparent) {
 630                base = d_hash(dparent, hash);
 631                lhp = base;
 632                while ((lhp = lhp->next) != base) {
 633                        if (dentry == list_entry(lhp, struct dentry, d_hash))
 634                                goto out;
 635                }
 636        } else {
 637                /*
 638                 * Special case: local mount points don't live in
 639                 * the hashes, so we search the super blocks.
 640                 */
 641                struct super_block *sb = sb_entry(super_blocks.next);
 642
 643                for (; sb != sb_entry(&super_blocks); 
 644                     sb = sb_entry(sb->s_list.next)) {
 645                        if (!sb->s_dev)
 646                                continue;
 647                        if (sb->s_root == dentry)
 648                                goto out;
 649                }
 650        }
 651        valid = 0;
 652out:
 653        return valid;
 654}
 655
 656/*
 657 * When a file is deleted, we have two options:
 658 * - turn this dentry into a negative dentry
 659 * - unhash this dentry and free it.
 660 *
 661 * Usually, we want to just turn this into
 662 * a negative dentry, but if anybody else is
 663 * currently using the dentry or the inode
 664 * we can't do that and we fall back on removing
 665 * it from the hash queues and waiting for
 666 * it to be deleted later when it has no users
 667 */
 668void d_delete(struct dentry * dentry)
 669{
 670        /*
 671         * Are we the only user?
 672         */
 673        if (dentry->d_count == 1) {
 674                dentry_iput(dentry);
 675                return;
 676        }
 677
 678        /*
 679         * If not, just drop the dentry and let dput
 680         * pick up the tab..
 681         */
 682        d_drop(dentry);
 683}
 684
 685void d_rehash(struct dentry * entry)
 686{
 687        struct dentry * parent = entry->d_parent;
 688
 689        list_add(&entry->d_hash, d_hash(parent, entry->d_name.hash));
 690}
 691
 692#define do_switch(x,y) do { \
 693        __typeof__ (x) __tmp = x; \
 694        x = y; y = __tmp; } while (0)
 695
 696/*
 697 * When switching names, the actual string doesn't strictly have to
 698 * be preserved in the target - because we're dropping the target
 699 * anyway. As such, we can just do a simple memcpy() to copy over
 700 * the new name before we switch.
 701 *
 702 * Note that we have to be a lot more careful about getting the hash
 703 * switched - we have to switch the hash value properly even if it
 704 * then no longer matches the actual (corrupted) string of the target.
 705 * The hash value has to match the hash queue that the dentry is on..
 706 */
 707static inline void switch_names(struct dentry * dentry, struct dentry * target)
 708{
 709        const unsigned char *old_name, *new_name;
 710
 711        memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN); 
 712        old_name = target->d_name.name;
 713        new_name = dentry->d_name.name;
 714        if (old_name == target->d_iname)
 715                old_name = dentry->d_iname;
 716        if (new_name == dentry->d_iname)
 717                new_name = target->d_iname;
 718        target->d_name.name = new_name;
 719        dentry->d_name.name = old_name;
 720}
 721
 722/*
 723 * We cannibalize "target" when moving dentry on top of it,
 724 * because it's going to be thrown away anyway. We could be more
 725 * polite about it, though.
 726 *
 727 * This forceful removal will result in ugly /proc output if
 728 * somebody holds a file open that got deleted due to a rename.
 729 * We could be nicer about the deleted file, and let it show
 730 * up under the name it got deleted rather than the name that
 731 * deleted it.
 732 *
 733 * Careful with the hash switch. The hash switch depends on
 734 * the fact that any list-entry can be a head of the list.
 735 * Think about it.
 736 */
 737void d_move(struct dentry * dentry, struct dentry * target)
 738{
 739        if (!dentry->d_inode)
 740                printk(KERN_WARNING "VFS: moving negative dcache entry\n");
 741
 742        /* Move the dentry to the target hash queue */
 743        list_del(&dentry->d_hash);
 744        list_add(&dentry->d_hash, &target->d_hash);
 745
 746        /* Unhash the target: dput() will then get rid of it */
 747        list_del(&target->d_hash);
 748        INIT_LIST_HEAD(&target->d_hash);
 749
 750        list_del(&dentry->d_child);
 751        list_del(&target->d_child);
 752
 753        /* Switch the parents and the names.. */
 754        switch_names(dentry, target);
 755        do_switch(dentry->d_parent, target->d_parent);
 756        do_switch(dentry->d_name.len, target->d_name.len);
 757        do_switch(dentry->d_name.hash, target->d_name.hash);
 758
 759        /* And add them back to the (new) parent lists */
 760        list_add(&target->d_child, &target->d_parent->d_subdirs);
 761        list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
 762}
 763
 764/*
 765 * "buflen" should be PAGE_SIZE or more.
 766 */
 767static char * d_path_error(struct dentry *dentry,
 768                           char *buffer, int buflen, int *error)
 769{
 770        char * end = buffer+buflen;
 771        char * retval;
 772        struct dentry * root = current->fs->root;
 773
 774        *error = 0;
 775
 776        *--end = '\0';
 777        buflen--;
 778        if (dentry->d_parent != dentry && list_empty(&dentry->d_hash)) {
 779                buflen -= 10;
 780                end -= 10;
 781                memcpy(end, " (deleted)", 10);
 782        }
 783
 784        /* Get '/' right */
 785        retval = end-1;
 786        *retval = '/';
 787
 788        for (;;) {
 789                struct dentry * parent;
 790                int namelen;
 791
 792                if (dentry == root)
 793                        break;
 794                dentry = dentry->d_covers;
 795                parent = dentry->d_parent;
 796                if (dentry == parent)
 797                        break;
 798                namelen = dentry->d_name.len;
 799                buflen -= namelen + 1;
 800                if (buflen < 0) {
 801                        *error = -ENAMETOOLONG;
 802                        break;
 803                }
 804                end -= namelen;
 805                memcpy(end, dentry->d_name.name, namelen);
 806                *--end = '/';
 807                retval = end;
 808                dentry = parent;
 809        }
 810        return retval;
 811}
 812
 813char * d_path(struct dentry *dentry, char *buffer, int buflen)
 814{
 815        int error;
 816
 817        return d_path_error(dentry, buffer, buflen, &error);
 818}
 819
 820/*
 821 * NOTE! The user-level library version returns a
 822 * character pointer. The kernel system call just
 823 * returns the length of the buffer filled (which
 824 * includes the ending '\0' character), or a negative
 825 * error value. So libc would do something like
 826 *
 827 *      char *getcwd(char * buf, size_t size)
 828 *      {
 829 *              int retval;
 830 *
 831 *              retval = sys_getcwd(buf, size);
 832 *              if (retval >= 0)
 833 *                      return buf;
 834 *              errno = -retval;
 835 *              return NULL;
 836 *      }
 837 */
 838asmlinkage int sys_getcwd(char *buf, unsigned long size)
 839{
 840        int error;
 841        struct dentry *pwd = current->fs->pwd; 
 842
 843        error = -ENOENT;
 844        /* Has the current directory been unlinked? */
 845        if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
 846                char *page = (char *) __get_free_page(GFP_USER);
 847                error = -ENOMEM;
 848                if (page) {
 849                        unsigned long len;
 850                        char * cwd;
 851
 852                        cwd = d_path_error(pwd, page, PAGE_SIZE, &error);
 853                        if (!error) {
 854                                error = -ERANGE;
 855                                len = PAGE_SIZE + page - cwd;
 856                                if (len <= size) {
 857                                        error = len;
 858                                        if (copy_to_user(buf, cwd, len))
 859                                                error = -EFAULT;
 860                                }
 861                        }
 862                        free_page((unsigned long) page);
 863                }
 864        }
 865        return error;
 866}
 867
 868/*
 869 * Test whether new_dentry is a subdirectory of old_dentry.
 870 *
 871 * Trivially implemented using the dcache structure
 872 */
 873int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
 874{
 875        int result;
 876
 877        result = 0;
 878        for (;;) {
 879                if (new_dentry != old_dentry) {
 880                        struct dentry * parent = new_dentry->d_parent;
 881                        if (parent == new_dentry)
 882                                break;
 883                        new_dentry = parent;
 884                        continue;
 885                }
 886                result = 1;
 887                break;
 888        }
 889        return result;
 890}
 891
 892/*
 893 * Check whether a dentry already exists for the given name,
 894 * and return the inode number if it has an inode.
 895 *
 896 * This routine is used to post-process directory listings for
 897 * filesystems using synthetic inode numbers, and is necessary
 898 * to keep getcwd() working.
 899 */
 900ino_t find_inode_number(struct dentry *dir, struct qstr *name)
 901{
 902        struct dentry * dentry;
 903        ino_t ino = 0;
 904
 905        /*
 906         * Check for a fs-specific hash function. Note that we must
 907         * calculate the standard hash first, as the d_op->d_hash()
 908         * routine may choose to leave the hash value unchanged.
 909         */
 910        name->hash = full_name_hash(name->name, name->len);
 911        if (dir->d_op && dir->d_op->d_hash)
 912        {
 913                if (dir->d_op->d_hash(dir, name) != 0)
 914                        goto out;
 915        }
 916
 917        dentry = d_lookup(dir, name);
 918        if (dentry)
 919        {
 920                if (dentry->d_inode)
 921                        ino = dentry->d_inode->i_ino;
 922                dput(dentry);
 923        }
 924out:
 925        return ino;
 926}
 927
 928void __init dcache_init(void)
 929{
 930        int i, order;
 931        struct list_head *d;
 932        unsigned int nr_hash;
 933        unsigned long memory_size;
 934
 935        /* 
 936         * A constructor could be added for stable state like the lists,
 937         * but it is probably not worth it because of the cache nature
 938         * of the dcache. 
 939         * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
 940         * flag could be removed here, to hint to the allocator that
 941         * it should not try to get multiple page regions.  
 942         */
 943        dentry_cache = kmem_cache_create("dentry_cache",
 944                                         sizeof(struct dentry),
 945                                         0,
 946                                         SLAB_HWCACHE_ALIGN,
 947                                         NULL, NULL);
 948        if (!dentry_cache)
 949                panic("Cannot create dentry cache");
 950
 951        memory_size = num_physpages << PAGE_SHIFT;
 952        memory_size >>= 13;
 953        memory_size *= 2 * sizeof(void *);
 954        for (order = 0; ((1UL << order) << PAGE_SHIFT) < memory_size; order++);
 955
 956        do {
 957                unsigned long tmp;
 958
 959                nr_hash = (1UL << order) * PAGE_SIZE /
 960                        sizeof(struct list_head);
 961                d_hash_mask = (nr_hash - 1);
 962
 963                tmp = nr_hash;
 964                d_hash_shift = 0;
 965                while((tmp >>= 1UL) != 0UL)
 966                        d_hash_shift++;
 967
 968                dentry_hashtable = (struct list_head *)
 969                        __get_free_pages(GFP_ATOMIC, order);
 970        } while(dentry_hashtable == NULL && --order >= 0);
 971        printk("Dentry hash table entries: %d (order %d, %ldk)\n",
 972               nr_hash, order, (1UL<<order) * PAGE_SIZE / 1024);
 973
 974        if (!dentry_hashtable)
 975                panic("Failed to allocate dcache hash table\n");
 976
 977        d = dentry_hashtable;
 978        i = nr_hash;
 979        do {
 980                INIT_LIST_HEAD(d);
 981                d++;
 982                i--;
 983        } while (i);
 984}
 985
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.