linux-bk/fs/inode.c
<<
>>
Prefs
   1/*
   2 * linux/fs/inode.c
   3 *
   4 * (C) 1997 Linus Torvalds
   5 */
   6
   7#include <linux/config.h>
   8#include <linux/fs.h>
   9#include <linux/mm.h>
  10#include <linux/dcache.h>
  11#include <linux/init.h>
  12#include <linux/quotaops.h>
  13#include <linux/slab.h>
  14#include <linux/writeback.h>
  15#include <linux/module.h>
  16#include <linux/backing-dev.h>
  17#include <linux/wait.h>
  18#include <linux/hash.h>
  19#include <linux/swap.h>
  20#include <linux/security.h>
  21#include <linux/pagemap.h>
  22#include <linux/cdev.h>
  23
  24/*
  25 * This is needed for the following functions:
  26 *  - inode_has_buffers
  27 *  - invalidate_inode_buffers
  28 *  - fsync_bdev
  29 *  - invalidate_bdev
  30 *
  31 * FIXME: remove all knowledge of the buffer layer from this file
  32 */
  33#include <linux/buffer_head.h>
  34
  35/*
  36 * New inode.c implementation.
  37 *
  38 * This implementation has the basic premise of trying
  39 * to be extremely low-overhead and SMP-safe, yet be
  40 * simple enough to be "obviously correct".
  41 *
  42 * Famous last words.
  43 */
  44
  45/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
  46
  47/* #define INODE_PARANOIA 1 */
  48/* #define INODE_DEBUG 1 */
  49
  50/*
  51 * Inode lookup is no longer as critical as it used to be:
  52 * most of the lookups are going to be through the dcache.
  53 */
  54#define I_HASHBITS      i_hash_shift
  55#define I_HASHMASK      i_hash_mask
  56
  57static unsigned int i_hash_mask;
  58static unsigned int i_hash_shift;
  59
  60/*
  61 * Each inode can be on two separate lists. One is
  62 * the hash list of the inode, used for lookups. The
  63 * other linked list is the "type" list:
  64 *  "in_use" - valid inode, i_count > 0, i_nlink > 0
  65 *  "dirty"  - as "in_use" but also dirty
  66 *  "unused" - valid inode, i_count = 0
  67 *
  68 * A "dirty" list is maintained for each super block,
  69 * allowing for low-overhead inode sync() operations.
  70 */
  71
  72LIST_HEAD(inode_in_use);
  73LIST_HEAD(inode_unused);
  74static struct hlist_head *inode_hashtable;
  75
  76/*
  77 * A simple spinlock to protect the list manipulations.
  78 *
  79 * NOTE! You also have to own the lock if you change
  80 * the i_state of an inode while it is in use..
  81 */
  82spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
  83
  84/*
  85 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
  86 * icache shrinking path, and the umount path.  Without this exclusion,
  87 * by the time prune_icache calls iput for the inode whose pages it has
  88 * been invalidating, or by the time it calls clear_inode & destroy_inode
  89 * from its final dispose_list, the struct super_block they refer to
  90 * (for inode->i_sb->s_op) may already have been freed and reused.
  91 */
  92static DECLARE_MUTEX(iprune_sem);
  93
  94/*
  95 * Statistics gathering..
  96 */
  97struct inodes_stat_t inodes_stat;
  98
  99static kmem_cache_t * inode_cachep;
 100
 101static struct inode *alloc_inode(struct super_block *sb)
 102{
 103        static struct address_space_operations empty_aops;
 104        static struct inode_operations empty_iops;
 105        static struct file_operations empty_fops;
 106        struct inode *inode;
 107
 108        if (sb->s_op->alloc_inode)
 109                inode = sb->s_op->alloc_inode(sb);
 110        else
 111                inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
 112
 113        if (inode) {
 114                struct address_space * const mapping = &inode->i_data;
 115
 116                inode->i_sb = sb;
 117                inode->i_blkbits = sb->s_blocksize_bits;
 118                inode->i_flags = 0;
 119                atomic_set(&inode->i_count, 1);
 120                inode->i_sock = 0;
 121                inode->i_op = &empty_iops;
 122                inode->i_fop = &empty_fops;
 123                inode->i_nlink = 1;
 124                atomic_set(&inode->i_writecount, 0);
 125                inode->i_size = 0;
 126                inode->i_blocks = 0;
 127                inode->i_bytes = 0;
 128                inode->i_generation = 0;
 129                memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
 130                inode->i_pipe = NULL;
 131                inode->i_bdev = NULL;
 132                inode->i_cdev = NULL;
 133                inode->i_rdev = 0;
 134                inode->i_security = NULL;
 135                if (security_inode_alloc(inode)) {
 136                        if (inode->i_sb->s_op->destroy_inode)
 137                                inode->i_sb->s_op->destroy_inode(inode);
 138                        else
 139                                kmem_cache_free(inode_cachep, (inode));
 140                        return NULL;
 141                }
 142
 143                mapping->a_ops = &empty_aops;
 144                mapping->host = inode;
 145                mapping->flags = 0;
 146                mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
 147                mapping->dirtied_when = 0;
 148                mapping->assoc_mapping = NULL;
 149                mapping->backing_dev_info = &default_backing_dev_info;
 150                if (sb->s_bdev)
 151                        mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
 152                memset(&inode->u, 0, sizeof(inode->u));
 153                inode->i_mapping = mapping;
 154        }
 155        return inode;
 156}
 157
 158void destroy_inode(struct inode *inode) 
 159{
 160        if (inode_has_buffers(inode))
 161                BUG();
 162        security_inode_free(inode);
 163        if (inode->i_sb->s_op->destroy_inode)
 164                inode->i_sb->s_op->destroy_inode(inode);
 165        else
 166                kmem_cache_free(inode_cachep, (inode));
 167}
 168
 169
 170/*
 171 * These are initializations that only need to be done
 172 * once, because the fields are idempotent across use
 173 * of the inode, so let the slab aware of that.
 174 */
 175void inode_init_once(struct inode *inode)
 176{
 177        memset(inode, 0, sizeof(*inode));
 178        INIT_HLIST_NODE(&inode->i_hash);
 179        INIT_LIST_HEAD(&inode->i_data.clean_pages);
 180        INIT_LIST_HEAD(&inode->i_data.dirty_pages);
 181        INIT_LIST_HEAD(&inode->i_data.locked_pages);
 182        INIT_LIST_HEAD(&inode->i_data.io_pages);
 183        INIT_LIST_HEAD(&inode->i_dentry);
 184        INIT_LIST_HEAD(&inode->i_devices);
 185        sema_init(&inode->i_sem, 1);
 186        INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
 187        spin_lock_init(&inode->i_data.page_lock);
 188        init_MUTEX(&inode->i_data.i_shared_sem);
 189        atomic_set(&inode->i_data.truncate_count, 0);
 190        INIT_LIST_HEAD(&inode->i_data.private_list);
 191        spin_lock_init(&inode->i_data.private_lock);
 192        INIT_LIST_HEAD(&inode->i_data.i_mmap);
 193        INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
 194        spin_lock_init(&inode->i_lock);
 195        i_size_ordered_init(inode);
 196}
 197
 198EXPORT_SYMBOL(inode_init_once);
 199
 200static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
 201{
 202        struct inode * inode = (struct inode *) foo;
 203
 204        if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
 205            SLAB_CTOR_CONSTRUCTOR)
 206                inode_init_once(inode);
 207}
 208
 209/*
 210 * inode_lock must be held
 211 */
 212void __iget(struct inode * inode)
 213{
 214        if (atomic_read(&inode->i_count)) {
 215                atomic_inc(&inode->i_count);
 216                return;
 217        }
 218        atomic_inc(&inode->i_count);
 219        if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
 220                list_del(&inode->i_list);
 221                list_add(&inode->i_list, &inode_in_use);
 222        }
 223        inodes_stat.nr_unused--;
 224}
 225
 226/**
 227 * clear_inode - clear an inode
 228 * @inode: inode to clear
 229 *
 230 * This is called by the filesystem to tell us
 231 * that the inode is no longer useful. We just
 232 * terminate it with extreme prejudice.
 233 */
 234void clear_inode(struct inode *inode)
 235{
 236        invalidate_inode_buffers(inode);
 237       
 238        if (inode->i_data.nrpages)
 239                BUG();
 240        if (!(inode->i_state & I_FREEING))
 241                BUG();
 242        if (inode->i_state & I_CLEAR)
 243                BUG();
 244        wait_on_inode(inode);
 245        DQUOT_DROP(inode);
 246        if (inode->i_sb && inode->i_sb->s_op->clear_inode)
 247                inode->i_sb->s_op->clear_inode(inode);
 248        if (inode->i_bdev)
 249                bd_forget(inode);
 250        if (inode->i_cdev)
 251                cd_forget(inode);
 252        inode->i_state = I_CLEAR;
 253}
 254
 255EXPORT_SYMBOL(clear_inode);
 256
 257/*
 258 * dispose_list - dispose of the contents of a local list
 259 * @head: the head of the list to free
 260 *
 261 * Dispose-list gets a local list with local inodes in it, so it doesn't
 262 * need to worry about list corruption and SMP locks.
 263 */
 264static void dispose_list(struct list_head *head)
 265{
 266        int nr_disposed = 0;
 267
 268        while (!list_empty(head)) {
 269                struct inode *inode;
 270
 271                inode = list_entry(head->next, struct inode, i_list);
 272                list_del(&inode->i_list);
 273
 274                if (inode->i_data.nrpages)
 275                        truncate_inode_pages(&inode->i_data, 0);
 276                clear_inode(inode);
 277                destroy_inode(inode);
 278                nr_disposed++;
 279        }
 280        spin_lock(&inode_lock);
 281        inodes_stat.nr_inodes -= nr_disposed;
 282        spin_unlock(&inode_lock);
 283}
 284
 285/*
 286 * Invalidate all inodes for a device.
 287 */
 288static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
 289{
 290        struct list_head *next;
 291        int busy = 0, count = 0;
 292
 293        next = head->next;
 294        for (;;) {
 295                struct list_head * tmp = next;
 296                struct inode * inode;
 297
 298                next = next->next;
 299                if (tmp == head)
 300                        break;
 301                inode = list_entry(tmp, struct inode, i_list);
 302                if (inode->i_sb != sb)
 303                        continue;
 304                invalidate_inode_buffers(inode);
 305                if (!atomic_read(&inode->i_count)) {
 306                        hlist_del_init(&inode->i_hash);
 307                        list_del(&inode->i_list);
 308                        list_add(&inode->i_list, dispose);
 309                        inode->i_state |= I_FREEING;
 310                        count++;
 311                        continue;
 312                }
 313                busy = 1;
 314        }
 315        /* only unused inodes may be cached with i_count zero */
 316        inodes_stat.nr_unused -= count;
 317        return busy;
 318}
 319
 320/*
 321 * This is a two-stage process. First we collect all
 322 * offending inodes onto the throw-away list, and in
 323 * the second stage we actually dispose of them. This
 324 * is because we don't want to sleep while messing
 325 * with the global lists..
 326 */
 327 
 328/**
 329 *      invalidate_inodes       - discard the inodes on a device
 330 *      @sb: superblock
 331 *
 332 *      Discard all of the inodes for a given superblock. If the discard
 333 *      fails because there are busy inodes then a non zero value is returned.
 334 *      If the discard is successful all the inodes have been discarded.
 335 */
 336int invalidate_inodes(struct super_block * sb)
 337{
 338        int busy;
 339        LIST_HEAD(throw_away);
 340
 341        down(&iprune_sem);
 342        spin_lock(&inode_lock);
 343        busy = invalidate_list(&inode_in_use, sb, &throw_away);
 344        busy |= invalidate_list(&inode_unused, sb, &throw_away);
 345        busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
 346        busy |= invalidate_list(&sb->s_io, sb, &throw_away);
 347        spin_unlock(&inode_lock);
 348
 349        dispose_list(&throw_away);
 350        up(&iprune_sem);
 351
 352        return busy;
 353}
 354
 355EXPORT_SYMBOL(invalidate_inodes);
 356 
 357int __invalidate_device(struct block_device *bdev, int do_sync)
 358{
 359        struct super_block *sb;
 360        int res;
 361
 362        if (do_sync)
 363                fsync_bdev(bdev);
 364
 365        res = 0;
 366        sb = get_super(bdev);
 367        if (sb) {
 368                /*
 369                 * no need to lock the super, get_super holds the
 370                 * read semaphore so the filesystem cannot go away
 371                 * under us (->put_super runs with the write lock
 372                 * hold).
 373                 */
 374                shrink_dcache_sb(sb);
 375                res = invalidate_inodes(sb);
 376                drop_super(sb);
 377        }
 378        invalidate_bdev(bdev, 0);
 379        return res;
 380}
 381
 382EXPORT_SYMBOL(__invalidate_device);
 383
 384static int can_unuse(struct inode *inode)
 385{
 386        if (inode->i_state)
 387                return 0;
 388        if (inode_has_buffers(inode))
 389                return 0;
 390        if (atomic_read(&inode->i_count))
 391                return 0;
 392        if (inode->i_data.nrpages)
 393                return 0;
 394        return 1;
 395}
 396
 397/*
 398 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
 399 * a temporary list and then are freed outside inode_lock by dispose_list().
 400 *
 401 * Any inodes which are pinned purely because of attached pagecache have their
 402 * pagecache removed.  We expect the final iput() on that inode to add it to
 403 * the front of the inode_unused list.  So look for it there and if the
 404 * inode is still freeable, proceed.  The right inode is found 99.9% of the
 405 * time in testing on a 4-way.
 406 *
 407 * If the inode has metadata buffers attached to mapping->private_list then
 408 * try to remove them.
 409 */
 410static void prune_icache(int nr_to_scan)
 411{
 412        LIST_HEAD(freeable);
 413        int nr_pruned = 0;
 414        int nr_scanned;
 415        unsigned long reap = 0;
 416
 417        down(&iprune_sem);
 418        spin_lock(&inode_lock);
 419        for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
 420                struct inode *inode;
 421
 422                if (list_empty(&inode_unused))
 423                        break;
 424
 425                inode = list_entry(inode_unused.prev, struct inode, i_list);
 426
 427                if (inode->i_state || atomic_read(&inode->i_count)) {
 428                        list_move(&inode->i_list, &inode_unused);
 429                        continue;
 430                }
 431                if (inode_has_buffers(inode) || inode->i_data.nrpages) {
 432                        __iget(inode);
 433                        spin_unlock(&inode_lock);
 434                        if (remove_inode_buffers(inode))
 435                                reap += invalidate_inode_pages(&inode->i_data);
 436                        iput(inode);
 437                        spin_lock(&inode_lock);
 438
 439                        if (inode != list_entry(inode_unused.next,
 440                                                struct inode, i_list))
 441                                continue;       /* wrong inode or list_empty */
 442                        if (!can_unuse(inode))
 443                                continue;
 444                }
 445                hlist_del_init(&inode->i_hash);
 446                list_move(&inode->i_list, &freeable);
 447                inode->i_state |= I_FREEING;
 448                nr_pruned++;
 449        }
 450        inodes_stat.nr_unused -= nr_pruned;
 451        spin_unlock(&inode_lock);
 452
 453        dispose_list(&freeable);
 454        up(&iprune_sem);
 455
 456        if (current_is_kswapd())
 457                mod_page_state(kswapd_inodesteal, reap);
 458        else
 459                mod_page_state(pginodesteal, reap);
 460}
 461
 462/*
 463 * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here,
 464 * "unused" means that no dentries are referring to the inodes: the files are
 465 * not open and the dcache references to those inodes have already been
 466 * reclaimed.
 467 *
 468 * This function is passed the number of inodes to scan, and it returns the
 469 * total number of remaining possibly-reclaimable inodes.
 470 */
 471static int shrink_icache_memory(int nr, unsigned int gfp_mask)
 472{
 473        if (nr) {
 474                /*
 475                 * Nasty deadlock avoidance.  We may hold various FS locks,
 476                 * and we don't want to recurse into the FS that called us
 477                 * in clear_inode() and friends..
 478                 */
 479                if (gfp_mask & __GFP_FS)
 480                        prune_icache(nr);
 481        }
 482        return inodes_stat.nr_unused;
 483}
 484
 485static void __wait_on_freeing_inode(struct inode *inode);
 486/*
 487 * Called with the inode lock held.
 488 * NOTE: we are not increasing the inode-refcount, you must call __iget()
 489 * by hand after calling find_inode now! This simplifies iunique and won't
 490 * add any additional branch in the common code.
 491 */
 492static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
 493{
 494        struct hlist_node *node;
 495        struct inode * inode = NULL;
 496
 497repeat:
 498        hlist_for_each (node, head) { 
 499                inode = hlist_entry(node, struct inode, i_hash);
 500                if (inode->i_sb != sb)
 501                        continue;
 502                if (!test(inode, data))
 503                        continue;
 504                if (inode->i_state & (I_FREEING|I_CLEAR)) {
 505                        __wait_on_freeing_inode(inode);
 506                        goto repeat;
 507                }
 508                break;
 509        }
 510        return node ? inode : NULL;
 511}
 512
 513/*
 514 * find_inode_fast is the fast path version of find_inode, see the comment at
 515 * iget_locked for details.
 516 */
 517static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
 518{
 519        struct hlist_node *node;
 520        struct inode * inode = NULL;
 521
 522repeat:
 523        hlist_for_each (node, head) {
 524                inode = hlist_entry(node, struct inode, i_hash);
 525                if (inode->i_ino != ino)
 526                        continue;
 527                if (inode->i_sb != sb)
 528                        continue;
 529                if (inode->i_state & (I_FREEING|I_CLEAR)) {
 530                        __wait_on_freeing_inode(inode);
 531                        goto repeat;
 532                }
 533                break;
 534        }
 535        return node ? inode : NULL;
 536}
 537
 538/**
 539 *      new_inode       - obtain an inode
 540 *      @sb: superblock
 541 *
 542 *      Allocates a new inode for given superblock.
 543 */
 544struct inode *new_inode(struct super_block *sb)
 545{
 546        static unsigned long last_ino;
 547        struct inode * inode;
 548
 549        spin_lock_prefetch(&inode_lock);
 550        
 551        inode = alloc_inode(sb);
 552        if (inode) {
 553                spin_lock(&inode_lock);
 554                inodes_stat.nr_inodes++;
 555                list_add(&inode->i_list, &inode_in_use);
 556                inode->i_ino = ++last_ino;
 557                inode->i_state = 0;
 558                spin_unlock(&inode_lock);
 559        }
 560        return inode;
 561}
 562
 563EXPORT_SYMBOL(new_inode);
 564
 565void unlock_new_inode(struct inode *inode)
 566{
 567        /*
 568         * This is special!  We do not need the spinlock
 569         * when clearing I_LOCK, because we're guaranteed
 570         * that nobody else tries to do anything about the
 571         * state of the inode when it is locked, as we
 572         * just created it (so there can be no old holders
 573         * that haven't tested I_LOCK).
 574         */
 575        inode->i_state &= ~(I_LOCK|I_NEW);
 576        wake_up_inode(inode);
 577}
 578
 579EXPORT_SYMBOL(unlock_new_inode);
 580
 581/*
 582 * This is called without the inode lock held.. Be careful.
 583 *
 584 * We no longer cache the sb_flags in i_flags - see fs.h
 585 *      -- rmk@arm.uk.linux.org
 586 */
 587static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data)
 588{
 589        struct inode * inode;
 590
 591        inode = alloc_inode(sb);
 592        if (inode) {
 593                struct inode * old;
 594
 595                spin_lock(&inode_lock);
 596                /* We released the lock, so.. */
 597                old = find_inode(sb, head, test, data);
 598                if (!old) {
 599                        if (set(inode, data))
 600                                goto set_failed;
 601
 602                        inodes_stat.nr_inodes++;
 603                        list_add(&inode->i_list, &inode_in_use);
 604                        hlist_add_head(&inode->i_hash, head);
 605                        inode->i_state = I_LOCK|I_NEW;
 606                        spin_unlock(&inode_lock);
 607
 608                        /* Return the locked inode with I_NEW set, the
 609                         * caller is responsible for filling in the contents
 610                         */
 611                        return inode;
 612                }
 613
 614                /*
 615                 * Uhhuh, somebody else created the same inode under
 616                 * us. Use the old inode instead of the one we just
 617                 * allocated.
 618                 */
 619                __iget(old);
 620                spin_unlock(&inode_lock);
 621                destroy_inode(inode);
 622                inode = old;
 623                wait_on_inode(inode);
 624        }
 625        return inode;
 626
 627set_failed:
 628        spin_unlock(&inode_lock);
 629        destroy_inode(inode);
 630        return NULL;
 631}
 632
 633/*
 634 * get_new_inode_fast is the fast path version of get_new_inode, see the
 635 * comment at iget_locked for details.
 636 */
 637static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
 638{
 639        struct inode * inode;
 640
 641        inode = alloc_inode(sb);
 642        if (inode) {
 643                struct inode * old;
 644
 645                spin_lock(&inode_lock);
 646                /* We released the lock, so.. */
 647                old = find_inode_fast(sb, head, ino);
 648                if (!old) {
 649                        inode->i_ino = ino;
 650                        inodes_stat.nr_inodes++;
 651                        list_add(&inode->i_list, &inode_in_use);
 652                        hlist_add_head(&inode->i_hash, head);
 653                        inode->i_state = I_LOCK|I_NEW;
 654                        spin_unlock(&inode_lock);
 655
 656                        /* Return the locked inode with I_NEW set, the
 657                         * caller is responsible for filling in the contents
 658                         */
 659                        return inode;
 660                }
 661
 662                /*
 663                 * Uhhuh, somebody else created the same inode under
 664                 * us. Use the old inode instead of the one we just
 665                 * allocated.
 666                 */
 667                __iget(old);
 668                spin_unlock(&inode_lock);
 669                destroy_inode(inode);
 670                inode = old;
 671                wait_on_inode(inode);
 672        }
 673        return inode;
 674}
 675
 676static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
 677{
 678        unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
 679        tmp = tmp + (tmp >> I_HASHBITS);
 680        return tmp & I_HASHMASK;
 681}
 682
 683/* Yeah, I know about quadratic hash. Maybe, later. */
 684
 685/**
 686 *      iunique - get a unique inode number
 687 *      @sb: superblock
 688 *      @max_reserved: highest reserved inode number
 689 *
 690 *      Obtain an inode number that is unique on the system for a given
 691 *      superblock. This is used by file systems that have no natural
 692 *      permanent inode numbering system. An inode number is returned that
 693 *      is higher than the reserved limit but unique.
 694 *
 695 *      BUGS:
 696 *      With a large number of inodes live on the file system this function
 697 *      currently becomes quite slow.
 698 */
 699ino_t iunique(struct super_block *sb, ino_t max_reserved)
 700{
 701        static ino_t counter;
 702        struct inode *inode;
 703        struct hlist_head * head;
 704        ino_t res;
 705        spin_lock(&inode_lock);
 706retry:
 707        if (counter > max_reserved) {
 708                head = inode_hashtable + hash(sb,counter);
 709                res = counter++;
 710                inode = find_inode_fast(sb, head, res);
 711                if (!inode) {
 712                        spin_unlock(&inode_lock);
 713                        return res;
 714                }
 715        } else {
 716                counter = max_reserved + 1;
 717        }
 718        goto retry;
 719        
 720}
 721
 722EXPORT_SYMBOL(iunique);
 723
 724struct inode *igrab(struct inode *inode)
 725{
 726        spin_lock(&inode_lock);
 727        if (!(inode->i_state & I_FREEING))
 728                __iget(inode);
 729        else
 730                /*
 731                 * Handle the case where s_op->clear_inode is not been
 732                 * called yet, and somebody is calling igrab
 733                 * while the inode is getting freed.
 734                 */
 735                inode = NULL;
 736        spin_unlock(&inode_lock);
 737        return inode;
 738}
 739
 740EXPORT_SYMBOL(igrab);
 741
 742/**
 743 * ifind - internal function, you want ilookup5() or iget5().
 744 * @sb:         super block of file system to search
 745 * @head:       the head of the list to search
 746 * @test:       callback used for comparisons between inodes
 747 * @data:       opaque data pointer to pass to @test
 748 *
 749 * ifind() searches for the inode specified by @data in the inode
 750 * cache. This is a generalized version of ifind_fast() for file systems where
 751 * the inode number is not sufficient for unique identification of an inode.
 752 *
 753 * If the inode is in the cache, the inode is returned with an incremented
 754 * reference count.
 755 *
 756 * Otherwise NULL is returned.
 757 *
 758 * Note, @test is called with the inode_lock held, so can't sleep.
 759 */
 760static inline struct inode *ifind(struct super_block *sb,
 761                struct hlist_head *head, int (*test)(struct inode *, void *),
 762                void *data)
 763{
 764        struct inode *inode;
 765
 766        spin_lock(&inode_lock);
 767        inode = find_inode(sb, head, test, data);
 768        if (inode) {
 769                __iget(inode);
 770                spin_unlock(&inode_lock);
 771                wait_on_inode(inode);
 772                return inode;
 773        }
 774        spin_unlock(&inode_lock);
 775        return NULL;
 776}
 777
 778/**
 779 * ifind_fast - internal function, you want ilookup() or iget().
 780 * @sb:         super block of file system to search
 781 * @head:       head of the list to search
 782 * @ino:        inode number to search for
 783 *
 784 * ifind_fast() searches for the inode @ino in the inode cache. This is for
 785 * file systems where the inode number is sufficient for unique identification
 786 * of an inode.
 787 *
 788 * If the inode is in the cache, the inode is returned with an incremented
 789 * reference count.
 790 *
 791 * Otherwise NULL is returned.
 792 */
 793static inline struct inode *ifind_fast(struct super_block *sb,
 794                struct hlist_head *head, unsigned long ino)
 795{
 796        struct inode *inode;
 797
 798        spin_lock(&inode_lock);
 799        inode = find_inode_fast(sb, head, ino);
 800        if (inode) {
 801                __iget(inode);
 802                spin_unlock(&inode_lock);
 803                wait_on_inode(inode);
 804                return inode;
 805        }
 806        spin_unlock(&inode_lock);
 807        return NULL;
 808}
 809
 810/**
 811 * ilookup5 - search for an inode in the inode cache
 812 * @sb:         super block of file system to search
 813 * @hashval:    hash value (usually inode number) to search for
 814 * @test:       callback used for comparisons between inodes
 815 * @data:       opaque data pointer to pass to @test
 816 *
 817 * ilookup5() uses ifind() to search for the inode specified by @hashval and
 818 * @data in the inode cache. This is a generalized version of ilookup() for
 819 * file systems where the inode number is not sufficient for unique
 820 * identification of an inode.
 821 *
 822 * If the inode is in the cache, the inode is returned with an incremented
 823 * reference count.
 824 *
 825 * Otherwise NULL is returned.
 826 *
 827 * Note, @test is called with the inode_lock held, so can't sleep.
 828 */
 829struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
 830                int (*test)(struct inode *, void *), void *data)
 831{
 832        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
 833
 834        return ifind(sb, head, test, data);
 835}
 836
 837EXPORT_SYMBOL(ilookup5);
 838
 839/**
 840 * ilookup - search for an inode in the inode cache
 841 * @sb:         super block of file system to search
 842 * @ino:        inode number to search for
 843 *
 844 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
 845 * This is for file systems where the inode number is sufficient for unique
 846 * identification of an inode.
 847 *
 848 * If the inode is in the cache, the inode is returned with an incremented
 849 * reference count.
 850 *
 851 * Otherwise NULL is returned.
 852 */
 853struct inode *ilookup(struct super_block *sb, unsigned long ino)
 854{
 855        struct hlist_head *head = inode_hashtable + hash(sb, ino);
 856
 857        return ifind_fast(sb, head, ino);
 858}
 859
 860EXPORT_SYMBOL(ilookup);
 861
 862/**
 863 * iget5_locked - obtain an inode from a mounted file system
 864 * @sb:         super block of file system
 865 * @hashval:    hash value (usually inode number) to get
 866 * @test:       callback used for comparisons between inodes
 867 * @set:        callback used to initialize a new struct inode
 868 * @data:       opaque data pointer to pass to @test and @set
 869 *
 870 * This is iget() without the read_inode() portion of get_new_inode().
 871 *
 872 * iget5_locked() uses ifind() to search for the inode specified by @hashval
 873 * and @data in the inode cache and if present it is returned with an increased
 874 * reference count. This is a generalized version of iget_locked() for file
 875 * systems where the inode number is not sufficient for unique identification
 876 * of an inode.
 877 *
 878 * If the inode is not in cache, get_new_inode() is called to allocate a new
 879 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
 880 * file system gets to fill it in before unlocking it via unlock_new_inode().
 881 *
 882 * Note both @test and @set are called with the inode_lock held, so can't sleep.
 883 */
 884struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
 885                int (*test)(struct inode *, void *),
 886                int (*set)(struct inode *, void *), void *data)
 887{
 888        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
 889        struct inode *inode;
 890
 891        inode = ifind(sb, head, test, data);
 892        if (inode)
 893                return inode;
 894        /*
 895         * get_new_inode() will do the right thing, re-trying the search
 896         * in case it had to block at any point.
 897         */
 898        return get_new_inode(sb, head, test, set, data);
 899}
 900
 901EXPORT_SYMBOL(iget5_locked);
 902
 903/**
 904 * iget_locked - obtain an inode from a mounted file system
 905 * @sb:         super block of file system
 906 * @ino:        inode number to get
 907 *
 908 * This is iget() without the read_inode() portion of get_new_inode_fast().
 909 *
 910 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
 911 * the inode cache and if present it is returned with an increased reference
 912 * count. This is for file systems where the inode number is sufficient for
 913 * unique identification of an inode.
 914 *
 915 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
 916 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
 917 * The file system gets to fill it in before unlocking it via
 918 * unlock_new_inode().
 919 */
 920struct inode *iget_locked(struct super_block *sb, unsigned long ino)
 921{
 922        struct hlist_head *head = inode_hashtable + hash(sb, ino);
 923        struct inode *inode;
 924
 925        inode = ifind_fast(sb, head, ino);
 926        if (inode)
 927                return inode;
 928        /*
 929         * get_new_inode_fast() will do the right thing, re-trying the search
 930         * in case it had to block at any point.
 931         */
 932        return get_new_inode_fast(sb, head, ino);
 933}
 934
 935EXPORT_SYMBOL(iget_locked);
 936
 937/**
 938 *      __insert_inode_hash - hash an inode
 939 *      @inode: unhashed inode
 940 *      @hashval: unsigned long value used to locate this object in the
 941 *              inode_hashtable.
 942 *
 943 *      Add an inode to the inode hash for this superblock.
 944 */
 945void __insert_inode_hash(struct inode *inode, unsigned long hashval)
 946{
 947        struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
 948        spin_lock(&inode_lock);
 949        hlist_add_head(&inode->i_hash, head);
 950        spin_unlock(&inode_lock);
 951}
 952
 953EXPORT_SYMBOL(__insert_inode_hash);
 954
 955/**
 956 *      remove_inode_hash - remove an inode from the hash
 957 *      @inode: inode to unhash
 958 *
 959 *      Remove an inode from the superblock.
 960 */
 961void remove_inode_hash(struct inode *inode)
 962{
 963        spin_lock(&inode_lock);
 964        hlist_del_init(&inode->i_hash);
 965        spin_unlock(&inode_lock);
 966}
 967
 968EXPORT_SYMBOL(remove_inode_hash);
 969
 970/*
 971 * Tell the filesystem that this inode is no longer of any interest and should
 972 * be completely destroyed.
 973 *
 974 * We leave the inode in the inode hash table until *after* the filesystem's
 975 * ->delete_inode completes.  This ensures that an iget (such as nfsd might
 976 * instigate) will always find up-to-date information either in the hash or on
 977 * disk.
 978 *
 979 * I_FREEING is set so that no-one will take a new reference to the inode while
 980 * it is being deleted.
 981 */
 982void generic_delete_inode(struct inode *inode)
 983{
 984        struct super_operations *op = inode->i_sb->s_op;
 985
 986        list_del_init(&inode->i_list);
 987        inode->i_state|=I_FREEING;
 988        inodes_stat.nr_inodes--;
 989        spin_unlock(&inode_lock);
 990
 991        if (inode->i_data.nrpages)
 992                truncate_inode_pages(&inode->i_data, 0);
 993
 994        security_inode_delete(inode);
 995
 996        if (op->delete_inode) {
 997                void (*delete)(struct inode *) = op->delete_inode;
 998                if (!is_bad_inode(inode))
 999                        DQUOT_INIT(inode);
1000                /* s_op->delete_inode internally recalls clear_inode() */
1001                delete(inode);
1002        } else
1003                clear_inode(inode);
1004        spin_lock(&inode_lock);
1005        hlist_del_init(&inode->i_hash);
1006        spin_unlock(&inode_lock);
1007        wake_up_inode(inode);
1008        if (inode->i_state != I_CLEAR)
1009                BUG();
1010        destroy_inode(inode);
1011}
1012
1013EXPORT_SYMBOL(generic_delete_inode);
1014
1015static void generic_forget_inode(struct inode *inode)
1016{
1017        struct super_block *sb = inode->i_sb;
1018
1019        if (!hlist_unhashed(&inode->i_hash)) {
1020                if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
1021                        list_del(&inode->i_list);
1022                        list_add(&inode->i_list, &inode_unused);
1023                }
1024                inodes_stat.nr_unused++;
1025                spin_unlock(&inode_lock);
1026                if (!sb || (sb->s_flags & MS_ACTIVE))
1027                        return;
1028                write_inode_now(inode, 1);
1029                spin_lock(&inode_lock);
1030                inodes_stat.nr_unused--;
1031                hlist_del_init(&inode->i_hash);
1032        }
1033        list_del_init(&inode->i_list);
1034        inode->i_state|=I_FREEING;
1035        inodes_stat.nr_inodes--;
1036        spin_unlock(&inode_lock);
1037        if (inode->i_data.nrpages)
1038                truncate_inode_pages(&inode->i_data, 0);
1039        clear_inode(inode);
1040        destroy_inode(inode);
1041}
1042
1043/*
1044 * Normal UNIX filesystem behaviour: delete the
1045 * inode when the usage count drops to zero, and
1046 * i_nlink is zero.
1047 */
1048static void generic_drop_inode(struct inode *inode)
1049{
1050        if (!inode->i_nlink)
1051                generic_delete_inode(inode);
1052        else
1053                generic_forget_inode(inode);
1054}
1055
1056/*
1057 * Called when we're dropping the last reference
1058 * to an inode. 
1059 *
1060 * Call the FS "drop()" function, defaulting to
1061 * the legacy UNIX filesystem behaviour..
1062 *
1063 * NOTE! NOTE! NOTE! We're called with the inode lock
1064 * held, and the drop function is supposed to release
1065 * the lock!
1066 */
1067static inline void iput_final(struct inode *inode)
1068{
1069        struct super_operations *op = inode->i_sb->s_op;
1070        void (*drop)(struct inode *) = generic_drop_inode;
1071
1072        if (op && op->drop_inode)
1073                drop = op->drop_inode;
1074        drop(inode);
1075}
1076
1077/**
1078 *      iput    - put an inode 
1079 *      @inode: inode to put
1080 *
1081 *      Puts an inode, dropping its usage count. If the inode use count hits
1082 *      zero the inode is also then freed and may be destroyed.
1083 */
1084void iput(struct inode *inode)
1085{
1086        if (inode) {
1087                struct super_operations *op = inode->i_sb->s_op;
1088
1089                if (inode->i_state == I_CLEAR)
1090                        BUG();
1091
1092                if (op && op->put_inode)
1093                        op->put_inode(inode);
1094
1095                if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1096                        iput_final(inode);
1097        }
1098}
1099
1100EXPORT_SYMBOL(iput);
1101
1102/**
1103 *      bmap    - find a block number in a file
1104 *      @inode: inode of file
1105 *      @block: block to find
1106 *
1107 *      Returns the block number on the device holding the inode that
1108 *      is the disk block number for the block of the file requested.
1109 *      That is, asked for block 4 of inode 1 the function will return the
1110 *      disk block relative to the disk start that holds that block of the 
1111 *      file.
1112 */
1113sector_t bmap(struct inode * inode, sector_t block)
1114{
1115        sector_t res = 0;
1116        if (inode->i_mapping->a_ops->bmap)
1117                res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1118        return res;
1119}
1120
1121EXPORT_SYMBOL(bmap);
1122
1123/*
1124 * Return true if the filesystem which backs this inode considers the two
1125 * passed timespecs to be sufficiently different to warrant flushing the
1126 * altered time out to disk.
1127 */
1128static int inode_times_differ(struct inode *inode,
1129                        struct timespec *old, struct timespec *new)
1130{
1131        if (IS_ONE_SECOND(inode))
1132                return old->tv_sec != new->tv_sec;
1133        return !timespec_equal(old, new);
1134}
1135
1136/**
1137 *      update_atime    -       update the access time
1138 *      @inode: inode accessed
1139 *
1140 *      Update the accessed time on an inode and mark it for writeback.
1141 *      This function automatically handles read only file systems and media,
1142 *      as well as the "noatime" flag and inode specific "noatime" markers.
1143 */
1144void update_atime(struct inode *inode)
1145{
1146        struct timespec now;
1147
1148        if (IS_NOATIME(inode))
1149                return;
1150        if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1151                return;
1152        if (IS_RDONLY(inode))
1153                return;
1154
1155        now = current_kernel_time();
1156        if (inode_times_differ(inode, &inode->i_atime, &now)) {
1157                inode->i_atime = now;
1158                mark_inode_dirty_sync(inode);
1159        } else {
1160                if (!timespec_equal(&inode->i_atime, &now))
1161                        inode->i_atime = now;
1162        }
1163}
1164
1165EXPORT_SYMBOL(update_atime);
1166
1167/**
1168 *      inode_update_time       -       update mtime and ctime time
1169 *      @inode: inode accessed
1170 *      @ctime_too: update ctime too
1171 *
1172 *      Update the mtime time on an inode and mark it for writeback.
1173 *      When ctime_too is specified update the ctime too.
1174 */
1175
1176void inode_update_time(struct inode *inode, int ctime_too)
1177{
1178        struct timespec now;
1179        int sync_it = 0;
1180
1181        if (IS_RDONLY(inode))
1182                return;
1183
1184        now = current_kernel_time();
1185
1186        if (inode_times_differ(inode, &inode->i_mtime, &now))
1187                sync_it = 1;
1188        inode->i_mtime = now;
1189
1190        if (ctime_too) {
1191                if (inode_times_differ(inode, &inode->i_ctime, &now))
1192                        sync_it = 1;
1193                inode->i_ctime = now;
1194        }
1195        if (sync_it)
1196                mark_inode_dirty_sync(inode);
1197}
1198
1199EXPORT_SYMBOL(inode_update_time);
1200
1201int inode_needs_sync(struct inode *inode)
1202{
1203        if (IS_SYNC(inode))
1204                return 1;
1205        if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1206                return 1;
1207        return 0;
1208}
1209
1210EXPORT_SYMBOL(inode_needs_sync);
1211
1212/*
1213 *      Quota functions that want to walk the inode lists..
1214 */
1215#ifdef CONFIG_QUOTA
1216
1217/* Functions back in dquot.c */
1218void put_dquot_list(struct list_head *);
1219int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1220
1221void remove_dquot_ref(struct super_block *sb, int type)
1222{
1223        struct inode *inode;
1224        struct list_head *act_head;
1225        LIST_HEAD(tofree_head);
1226
1227        if (!sb->dq_op)
1228                return; /* nothing to do */
1229        spin_lock(&inode_lock); /* This lock is for inodes code */
1230        /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1231 
1232        list_for_each(act_head, &inode_in_use) {
1233                inode = list_entry(act_head, struct inode, i_list);
1234                if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1235                        remove_inode_dquot_ref(inode, type, &tofree_head);
1236        }
1237        list_for_each(act_head, &inode_unused) {
1238                inode = list_entry(act_head, struct inode, i_list);
1239                if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1240                        remove_inode_dquot_ref(inode, type, &tofree_head);
1241        }
1242        list_for_each(act_head, &sb->s_dirty) {
1243                inode = list_entry(act_head, struct inode, i_list);
1244                if (IS_QUOTAINIT(inode))
1245                        remove_inode_dquot_ref(inode, type, &tofree_head);
1246        }
1247        list_for_each(act_head, &sb->s_io) {
1248                inode = list_entry(act_head, struct inode, i_list);
1249                if (IS_QUOTAINIT(inode))
1250                        remove_inode_dquot_ref(inode, type, &tofree_head);
1251        }
1252        spin_unlock(&inode_lock);
1253
1254        put_dquot_list(&tofree_head);
1255}
1256
1257#endif
1258
1259/*
1260 * Hashed waitqueues for wait_on_inode().  The table is pretty small - the
1261 * kernel doesn't lock many inodes at the same time.
1262 */
1263#define I_WAIT_TABLE_ORDER      3
1264static struct i_wait_queue_head {
1265        wait_queue_head_t wqh;
1266} ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1267
1268/*
1269 * Return the address of the waitqueue_head to be used for this inode
1270 */
1271static wait_queue_head_t *i_waitq_head(struct inode *inode)
1272{
1273        return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1274}
1275
1276void __wait_on_inode(struct inode *inode)
1277{
1278        DECLARE_WAITQUEUE(wait, current);
1279        wait_queue_head_t *wq = i_waitq_head(inode);
1280
1281        add_wait_queue(wq, &wait);
1282repeat:
1283        set_current_state(TASK_UNINTERRUPTIBLE);
1284        if (inode->i_state & I_LOCK) {
1285                schedule();
1286                goto repeat;
1287        }
1288        remove_wait_queue(wq, &wait);
1289        __set_current_state(TASK_RUNNING);
1290}
1291
1292/*
1293 * If we try to find an inode in the inode hash while it is being deleted, we
1294 * have to wait until the filesystem completes its deletion before reporting
1295 * that it isn't found.  This is because iget will immediately call
1296 * ->read_inode, and we want to be sure that evidence of the deletion is found
1297 * by ->read_inode.
1298 *
1299 * This call might return early if an inode which shares the waitq is woken up.
1300 * This is most easily handled by the caller which will loop around again
1301 * looking for the inode.
1302 *
1303 * This is called with inode_lock held.
1304 */
1305static void __wait_on_freeing_inode(struct inode *inode)
1306{
1307        DECLARE_WAITQUEUE(wait, current);
1308        wait_queue_head_t *wq = i_waitq_head(inode);
1309
1310        add_wait_queue(wq, &wait);
1311        set_current_state(TASK_UNINTERRUPTIBLE);
1312        spin_unlock(&inode_lock);
1313        schedule();
1314        remove_wait_queue(wq, &wait);
1315        spin_lock(&inode_lock);
1316}
1317
1318void wake_up_inode(struct inode *inode)
1319{
1320        wait_queue_head_t *wq = i_waitq_head(inode);
1321
1322        /*
1323         * Prevent speculative execution through spin_unlock(&inode_lock);
1324         */
1325        smp_mb();
1326        if (waitqueue_active(wq))
1327                wake_up_all(wq);
1328}
1329
1330/*
1331 * Initialize the waitqueues and inode hash table.
1332 */
1333void __init inode_init(unsigned long mempages)
1334{
1335        struct hlist_head *head;
1336        unsigned long order;
1337        unsigned int nr_hash;
1338        int i;
1339
1340        for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1341                init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1342
1343        mempages >>= (14 - PAGE_SHIFT);
1344        mempages *= sizeof(struct hlist_head);
1345        for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1346                ;
1347
1348        do {
1349                unsigned long tmp;
1350
1351                nr_hash = (1UL << order) * PAGE_SIZE /
1352                        sizeof(struct hlist_head);
1353                i_hash_mask = (nr_hash - 1);
1354
1355                tmp = nr_hash;
1356                i_hash_shift = 0;
1357                while ((tmp >>= 1UL) != 0UL)
1358                        i_hash_shift++;
1359
1360                inode_hashtable = (struct hlist_head *)
1361                        __get_free_pages(GFP_ATOMIC, order);
1362        } while (inode_hashtable == NULL && --order >= 0);
1363
1364        printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1365                        nr_hash, order, (PAGE_SIZE << order));
1366
1367        if (!inode_hashtable)
1368                panic("Failed to allocate inode hash table\n");
1369
1370        head = inode_hashtable;
1371        i = nr_hash;
1372        do {
1373                INIT_HLIST_HEAD(head);
1374                head++;
1375                i--;
1376        } while (i);
1377
1378        /* inode slab cache */
1379        inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1380                                         0, SLAB_HWCACHE_ALIGN, init_once,
1381                                         NULL);
1382        if (!inode_cachep)
1383                panic("cannot create inode slab cache");
1384
1385        set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1386}
1387
1388void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1389{
1390        inode->i_mode = mode;
1391        if (S_ISCHR(mode)) {
1392                inode->i_fop = &def_chr_fops;
1393                inode->i_rdev = rdev;
1394        } else if (S_ISBLK(mode)) {
1395                inode->i_fop = &def_blk_fops;
1396                inode->i_rdev = rdev;
1397        } else if (S_ISFIFO(mode))
1398                inode->i_fop = &def_fifo_fops;
1399        else if (S_ISSOCK(mode))
1400                inode->i_fop = &bad_sock_fops;
1401        else
1402                printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1403                       mode);
1404}
1405
1406EXPORT_SYMBOL(init_special_inode);
1407
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.