linux-old/fs/inode.c
<<
>>
Prefs
   1/*
   2 * linux/fs/inode.c
   3 *
   4 * (C) 1997 Linus Torvalds
   5 */
   6
   7#include <linux/fs.h>
   8#include <linux/string.h>
   9#include <linux/mm.h>
  10#include <linux/dcache.h>
  11#include <linux/init.h>
  12#include <linux/quotaops.h>
  13#include <linux/random.h>
  14
  15/*
  16 * New inode.c implementation.
  17 *
  18 * This implementation has the basic premise of trying
  19 * to be extremely low-overhead and SMP-safe, yet be
  20 * simple enough to be "obviously correct".
  21 *
  22 * Famous last words.
  23 */
  24
  25#define INODE_PARANOIA 1
  26/* #define INODE_DEBUG 1 */
  27
  28/*
  29 * Inode lookup is no longer as critical as it used to be:
  30 * most of the lookups are going to be through the dcache.
  31 */
  32#define HASH_BITS       8
  33#define HASH_SIZE       (1UL << HASH_BITS)
  34#define HASH_MASK       (HASH_SIZE-1)
  35
  36/*
  37 * Each inode can be on two separate lists. One is
  38 * the hash list of the inode, used for lookups. The
  39 * other linked list is the "type" list:
  40 *  "in_use" - valid inode, hashed if i_nlink > 0
  41 *  "dirty"  - valid inode, hashed if i_nlink > 0, dirty.
  42 *  "unused" - ready to be re-used. Not hashed.
  43 *
  44 * A "dirty" list is maintained for each super block,
  45 * allowing for low-overhead inode sync() operations.
  46 */
  47
  48LIST_HEAD(inode_in_use);
  49static LIST_HEAD(inode_unused);
  50static struct list_head inode_hashtable[HASH_SIZE];
  51
  52__u32 inode_generation_count = 0;
  53
  54/*
  55 * A simple spinlock to protect the list manipulations.
  56 *
  57 * NOTE! You also have to own the lock if you change
  58 * the i_state of an inode while it is in use..
  59 */
  60spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
  61
  62/*
  63 * Statistics gathering..
  64 */
  65struct {
  66        int nr_inodes;
  67        int nr_free_inodes;
  68        int dummy[5];
  69} inodes_stat = {0, 0,};
  70
  71int max_inodes;
  72
  73/*
  74 * Put the inode on the super block's dirty list.
  75 *
  76 * CAREFUL! We mark it dirty unconditionally, but
  77 * move it onto the dirty list only if it is hashed.
  78 * If it was not hashed, it will never be added to
  79 * the dirty list even if it is later hashed, as it
  80 * will have been marked dirty already.
  81 *
  82 * In short, make sure you hash any inodes _before_
  83 * you start marking them dirty..
  84 */
  85void __mark_inode_dirty(struct inode *inode)
  86{
  87        struct super_block * sb = inode->i_sb;
  88
  89        if (sb) {
  90                spin_lock(&inode_lock);
  91                if (!(inode->i_state & I_DIRTY)) {
  92                        inode->i_state |= I_DIRTY;
  93                        /* Only add valid (ie hashed) inodes to the dirty list */
  94                        if (!list_empty(&inode->i_hash)) {
  95                                list_del(&inode->i_list);
  96                                list_add(&inode->i_list, &sb->s_dirty);
  97                        }
  98                }
  99                spin_unlock(&inode_lock);
 100        }
 101}
 102
 103static void __wait_on_inode(struct inode * inode)
 104{
 105        struct wait_queue wait = { current, NULL };
 106
 107        add_wait_queue(&inode->i_wait, &wait);
 108repeat:
 109        current->state = TASK_UNINTERRUPTIBLE;
 110        if (inode->i_state & I_LOCK) {
 111                schedule();
 112                goto repeat;
 113        }
 114        remove_wait_queue(&inode->i_wait, &wait);
 115        current->state = TASK_RUNNING;
 116}
 117
 118static inline void wait_on_inode(struct inode *inode)
 119{
 120        if (inode->i_state & I_LOCK)
 121                __wait_on_inode(inode);
 122}
 123
 124/*
 125 * These are initializations that only need to be done
 126 * once, because the fields are idempotent across use
 127 * of the inode..
 128 */
 129static inline void init_once(struct inode * inode)
 130{
 131        memset(inode, 0, sizeof(*inode));
 132        init_waitqueue(&inode->i_wait);
 133        INIT_LIST_HEAD(&inode->i_hash);
 134        INIT_LIST_HEAD(&inode->i_dentry);
 135        sema_init(&inode->i_sem, 1);
 136        sema_init(&inode->i_atomic_write, 1);
 137}
 138
 139static inline void write_inode(struct inode *inode)
 140{
 141        if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->write_inode)
 142                inode->i_sb->s_op->write_inode(inode);
 143}
 144
 145static inline void sync_one(struct inode *inode)
 146{
 147        if (inode->i_state & I_LOCK) {
 148                spin_unlock(&inode_lock);
 149                __wait_on_inode(inode);
 150                spin_lock(&inode_lock);
 151        } else {
 152                list_del(&inode->i_list);
 153                list_add(&inode->i_list, &inode_in_use);
 154                /* Set I_LOCK, reset I_DIRTY */
 155                inode->i_state ^= I_DIRTY | I_LOCK;
 156                spin_unlock(&inode_lock);
 157
 158                write_inode(inode);
 159
 160                spin_lock(&inode_lock);
 161                inode->i_state &= ~I_LOCK;
 162                wake_up(&inode->i_wait);
 163        }
 164}
 165
 166static inline void sync_list(struct list_head *head)
 167{
 168        struct list_head * tmp;
 169
 170        while ((tmp = head->prev) != head)
 171                sync_one(list_entry(tmp, struct inode, i_list));
 172}
 173
 174/*
 175 * "sync_inodes()" goes through the super block's dirty list, 
 176 * writes them out, and puts them back on the normal list.
 177 */
 178void sync_inodes(kdev_t dev)
 179{
 180        struct super_block * sb = sb_entry(super_blocks.next);
 181
 182        /*
 183         * Search the super_blocks array for the device(s) to sync.
 184         */
 185        spin_lock(&inode_lock);
 186        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
 187                if (!sb->s_dev)
 188                        continue;
 189                if (dev && sb->s_dev != dev)
 190                        continue;
 191
 192                sync_list(&sb->s_dirty);
 193
 194                if (dev)
 195                        break;
 196        }
 197        spin_unlock(&inode_lock);
 198}
 199
 200/*
 201 * Called with the spinlock already held..
 202 */
 203static void sync_all_inodes(void)
 204{
 205        struct super_block * sb = sb_entry(super_blocks.next);
 206        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
 207                if (!sb->s_dev)
 208                        continue;
 209                sync_list(&sb->s_dirty);
 210        }
 211}
 212
 213/*
 214 * Needed by knfsd
 215 */
 216void write_inode_now(struct inode *inode)
 217{
 218        struct super_block * sb = inode->i_sb;
 219
 220        if (sb) {
 221                spin_lock(&inode_lock);
 222                while (inode->i_state & I_DIRTY)
 223                        sync_one(inode);
 224                spin_unlock(&inode_lock);
 225        }
 226        else
 227                printk("write_inode_now: no super block\n");
 228}
 229
 230/*
 231 * This is called by the filesystem to tell us
 232 * that the inode is no longer useful. We just
 233 * terminate it with extreme prejudice.
 234 */
 235void clear_inode(struct inode *inode)
 236{
 237        if (inode->i_nrpages)
 238                truncate_inode_pages(inode, 0);
 239        wait_on_inode(inode);
 240        if (IS_QUOTAINIT(inode))
 241                DQUOT_DROP(inode);
 242        if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->clear_inode)
 243                inode->i_sb->s_op->clear_inode(inode);
 244
 245        inode->i_state = 0;
 246}
 247
 248/*
 249 * Dispose-list gets a local list, so it doesn't need to
 250 * worry about list corruption. It releases the inode lock
 251 * while clearing the inodes.
 252 */
 253static void dispose_list(struct list_head * head)
 254{
 255        struct list_head *next;
 256        int count = 0;
 257
 258        spin_unlock(&inode_lock);
 259        next = head->next;
 260        for (;;) {
 261                struct list_head * tmp = next;
 262                struct inode * inode;
 263
 264                next = next->next;
 265                if (tmp == head)
 266                        break;
 267                inode = list_entry(tmp, struct inode, i_list);
 268                clear_inode(inode);
 269                count++;
 270        }
 271
 272        /* Add them all to the unused list in one fell swoop */
 273        spin_lock(&inode_lock);
 274        list_splice(head, &inode_unused);
 275        inodes_stat.nr_free_inodes += count;
 276}
 277
 278/*
 279 * Invalidate all inodes for a device.
 280 */
 281static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
 282{
 283        struct list_head *next;
 284        int busy = 0;
 285
 286        next = head->next;
 287        for (;;) {
 288                struct list_head * tmp = next;
 289                struct inode * inode;
 290
 291                next = next->next;
 292                if (tmp == head)
 293                        break;
 294                inode = list_entry(tmp, struct inode, i_list);
 295                if (inode->i_sb != sb)
 296                        continue;
 297                if (!inode->i_count) {
 298                        list_del(&inode->i_hash);
 299                        INIT_LIST_HEAD(&inode->i_hash);
 300                        list_del(&inode->i_list);
 301                        list_add(&inode->i_list, dispose);
 302                        inode->i_state |= I_FREEING;
 303                        continue;
 304                }
 305                busy = 1;
 306        }
 307        return busy;
 308}
 309
 310/*
 311 * This is a two-stage process. First we collect all
 312 * offending inodes onto the throw-away list, and in
 313 * the second stage we actually dispose of them. This
 314 * is because we don't want to sleep while messing
 315 * with the global lists..
 316 */
 317int invalidate_inodes(struct super_block * sb)
 318{
 319        int busy;
 320        LIST_HEAD(throw_away);
 321
 322        spin_lock(&inode_lock);
 323        busy = invalidate_list(&inode_in_use, sb, &throw_away);
 324        busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
 325        dispose_list(&throw_away);
 326        spin_unlock(&inode_lock);
 327
 328        return busy;
 329}
 330
 331/*
 332 * This is called with the inode lock held. It searches
 333 * the in-use for freeable inodes, which are moved to a
 334 * temporary list and then placed on the unused list by
 335 * dispose_list. 
 336 *
 337 * We don't expect to have to call this very often.
 338 *
 339 * N.B. The spinlock is released during the call to
 340 *      dispose_list.
 341 */
 342#define CAN_UNUSE(inode) \
 343        (((inode)->i_count | (inode)->i_state) == 0)
 344#define INODE(entry)    (list_entry(entry, struct inode, i_list))
 345
 346static int __free_inodes(struct list_head * freeable)
 347{
 348        struct list_head *entry;
 349        int found = 0;
 350
 351        entry = inode_in_use.next;
 352        while (entry != &inode_in_use) {
 353                struct list_head *tmp = entry;
 354
 355                entry = entry->next;
 356                if (!CAN_UNUSE(INODE(tmp)))
 357                        continue;
 358                list_del(tmp);
 359                list_del(&INODE(tmp)->i_hash);
 360                INIT_LIST_HEAD(&INODE(tmp)->i_hash);
 361                list_add(tmp, freeable);
 362                list_entry(tmp, struct inode, i_list)->i_state = I_FREEING;
 363                found++;
 364        }
 365
 366        return found;
 367}
 368
 369static void free_inodes(void)
 370{
 371        LIST_HEAD(throw_away);
 372        if (__free_inodes(&throw_away))
 373                dispose_list(&throw_away);
 374}
 375
 376/*
 377 * Searches the inodes list for freeable inodes,
 378 * shrinking the dcache before (and possible after,
 379 * if we're low)
 380 */
 381static void try_to_free_inodes(int goal)
 382{
 383        static int block;
 384        static struct wait_queue * wait_inode_freeing;
 385        LIST_HEAD(throw_away);
 386        
 387        /* We must make sure to not eat the inodes
 388           while the blocker task sleeps otherwise
 389           the blocker task may find none inode
 390           available. */
 391        if (block)
 392        {
 393                struct wait_queue __wait;
 394
 395                __wait.task = current;
 396                add_wait_queue(&wait_inode_freeing, &__wait);
 397                for (;;)
 398                {
 399                        /* NOTE: we rely only on the inode_lock to be sure
 400                           to not miss the unblock event */
 401                        current->state = TASK_UNINTERRUPTIBLE;
 402                        spin_unlock(&inode_lock);
 403                        schedule();
 404                        spin_lock(&inode_lock);
 405                        if (!block)
 406                                break;
 407                }
 408                remove_wait_queue(&wait_inode_freeing, &__wait);
 409                current->state = TASK_RUNNING;
 410        }
 411
 412        block = 1;
 413        /*
 414         * First stry to just get rid of unused inodes.
 415         *
 416         * If we can't reach our goal that way, we'll have
 417         * to try to shrink the dcache and sync existing
 418         * inodes..
 419         */
 420        goal -= __free_inodes(&throw_away);
 421        if (goal > 0) {
 422                spin_unlock(&inode_lock);
 423                prune_dcache(0, goal);
 424                spin_lock(&inode_lock);
 425                sync_all_inodes();
 426                __free_inodes(&throw_away);
 427        }
 428        if (!list_empty(&throw_away))
 429                dispose_list(&throw_away);
 430        block = 0;
 431        wake_up(&wait_inode_freeing);
 432}
 433
 434/*
 435 * This is the externally visible routine for
 436 * inode memory management.
 437 */
 438void free_inode_memory(void)
 439{
 440        spin_lock(&inode_lock);
 441        free_inodes();
 442        spin_unlock(&inode_lock);
 443}
 444
 445
 446/*
 447 * This is called with the spinlock held, but releases
 448 * the lock when freeing or allocating inodes.
 449 * Look out! This returns with the inode lock held if
 450 * it got an inode..
 451 *
 452 * We do inode allocations two pages at a time to reduce
 453 * fragmentation.
 454 */
 455#define INODE_PAGE_ORDER        1
 456#define INODE_ALLOCATION_SIZE   (PAGE_SIZE << INODE_PAGE_ORDER)
 457#define INODES_PER_ALLOCATION   (INODE_ALLOCATION_SIZE/sizeof(struct inode))
 458
 459static struct inode * grow_inodes(void)
 460{
 461        struct inode * inode;
 462
 463        /*
 464         * Check whether to restock the unused list.
 465         */
 466        if (inodes_stat.nr_inodes > max_inodes) {
 467                struct list_head *tmp;
 468                try_to_free_inodes(inodes_stat.nr_inodes >> 2);
 469                tmp = inode_unused.next;
 470                if (tmp != &inode_unused) {
 471                        inodes_stat.nr_free_inodes--;
 472                        list_del(tmp);
 473                        inode = list_entry(tmp, struct inode, i_list);
 474                        return inode;
 475                }
 476                spin_unlock(&inode_lock);
 477                printk(KERN_WARNING
 478                       "grow_inodes: inode-max limit reached\n");
 479                return NULL;
 480        }
 481                
 482        spin_unlock(&inode_lock);
 483        inode = (struct inode *)__get_free_pages(GFP_KERNEL,INODE_PAGE_ORDER);
 484        if (inode) {
 485                int size;
 486                struct inode * tmp;
 487
 488                size = INODE_ALLOCATION_SIZE - 2*sizeof(struct inode);
 489                tmp = inode;
 490                spin_lock(&inode_lock);
 491                do {
 492                        tmp++;
 493                        init_once(tmp);
 494                        list_add(&tmp->i_list, &inode_unused);
 495                        size -= sizeof(struct inode);
 496                } while (size >= 0);
 497                init_once(inode);
 498                /*
 499                 * Update the inode statistics
 500                 */
 501                inodes_stat.nr_inodes += INODES_PER_ALLOCATION;
 502                inodes_stat.nr_free_inodes += INODES_PER_ALLOCATION - 1;
 503                return inode;
 504        }
 505
 506        /*
 507         * If the allocation failed, do an extensive pruning of 
 508         * the dcache and then try again to free some inodes.
 509         */
 510        prune_dcache(0, inodes_stat.nr_inodes >> 2);
 511
 512        spin_lock(&inode_lock);
 513        free_inodes();
 514        {
 515                struct list_head *tmp = inode_unused.next;
 516                if (tmp != &inode_unused) {
 517                        inodes_stat.nr_free_inodes--;
 518                        list_del(tmp);
 519                        inode = list_entry(tmp, struct inode, i_list);
 520                        return inode;
 521                }
 522        }
 523        spin_unlock(&inode_lock);
 524
 525        printk("grow_inodes: allocation failed\n");
 526        return NULL;
 527}
 528
 529/*
 530 * Called with the inode lock held.
 531 */
 532static struct inode * find_inode(struct super_block * sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
 533{
 534        struct list_head *tmp;
 535        struct inode * inode;
 536
 537        tmp = head;
 538        for (;;) {
 539                tmp = tmp->next;
 540                inode = NULL;
 541                if (tmp == head)
 542                        break;
 543                inode = list_entry(tmp, struct inode, i_hash);
 544                if (inode->i_sb != sb)
 545                        continue;
 546                if (inode->i_ino != ino)
 547                        continue;
 548                if (find_actor && !find_actor(inode, ino, opaque))
 549                        continue;
 550                inode->i_count++;
 551                break;
 552        }
 553        return inode;
 554}
 555
 556/*
 557 * This just initializes the inode fields
 558 * to known values before returning the inode..
 559 *
 560 * i_sb, i_ino, i_count, i_state and the lists have
 561 * been initialized elsewhere..
 562 */
 563void clean_inode(struct inode *inode)
 564{
 565        memset(&inode->u, 0, sizeof(inode->u));
 566        inode->i_sock = 0;
 567        inode->i_op = NULL;
 568        inode->i_nlink = 1;
 569        inode->i_writecount = 0;
 570        inode->i_size = 0;
 571        inode->i_generation = 0;
 572        memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
 573        sema_init(&inode->i_sem, 1);
 574}
 575
 576/*
 577 * This is called by things like the networking layer
 578 * etc that want to get an inode without any inode
 579 * number, or filesystems that allocate new inodes with
 580 * no pre-existing information.
 581 */
 582struct inode * get_empty_inode(void)
 583{
 584        static unsigned long last_ino = 0;
 585        struct inode * inode;
 586        struct list_head * tmp;
 587
 588        spin_lock(&inode_lock);
 589        tmp = inode_unused.next;
 590        if (tmp != &inode_unused) {
 591                list_del(tmp);
 592                inodes_stat.nr_free_inodes--;
 593                inode = list_entry(tmp, struct inode, i_list);
 594add_new_inode:
 595                list_add(&inode->i_list, &inode_in_use);
 596                inode->i_sb = NULL;
 597                inode->i_dev = 0;
 598                inode->i_ino = ++last_ino;
 599                inode->i_flags = 0;
 600                inode->i_count = 1;
 601                inode->i_state = 0;
 602                spin_unlock(&inode_lock);
 603                clean_inode(inode);
 604                return inode;
 605        }
 606
 607        /*
 608         * Warning: if this succeeded, we will now
 609         * return with the inode lock.
 610         */
 611        inode = grow_inodes();
 612        if (inode)
 613                goto add_new_inode;
 614
 615        return inode;
 616}
 617
 618/*
 619 * This is called with the inode lock held.. Be careful.
 620 *
 621 * We no longer cache the sb_flags in i_flags - see fs.h
 622 *      -- rmk@arm.uk.linux.org
 623 */
 624static struct inode * get_new_inode(struct super_block *sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
 625{
 626        struct inode * inode;
 627        struct list_head * tmp = inode_unused.next;
 628
 629        if (tmp != &inode_unused) {
 630                list_del(tmp);
 631                inodes_stat.nr_free_inodes--;
 632                inode = list_entry(tmp, struct inode, i_list);
 633add_new_inode:
 634                list_add(&inode->i_list, &inode_in_use);
 635                list_add(&inode->i_hash, head);
 636                inode->i_sb = sb;
 637                inode->i_dev = sb->s_dev;
 638                inode->i_ino = ino;
 639                inode->i_flags = 0;
 640                inode->i_count = 1;
 641                inode->i_state = I_LOCK;
 642                spin_unlock(&inode_lock);
 643
 644                clean_inode(inode);
 645                sb->s_op->read_inode(inode);
 646
 647                /*
 648                 * This is special!  We do not need the spinlock
 649                 * when clearing I_LOCK, because we're guaranteed
 650                 * that nobody else tries to do anything about the
 651                 * state of the inode when it is locked, as we
 652                 * just created it (so there can be no old holders
 653                 * that haven't tested I_LOCK).
 654                 */
 655                inode->i_state &= ~I_LOCK;
 656                wake_up(&inode->i_wait);
 657
 658                return inode;
 659        }
 660
 661        /*
 662         * We need to expand. Note that "grow_inodes()" will
 663         * release the spinlock, but will return with the lock 
 664         * held again if the allocation succeeded.
 665         */
 666        inode = grow_inodes();
 667        if (inode) {
 668                /* We released the lock, so.. */
 669                struct inode * old = find_inode(sb, ino, head, find_actor, opaque);
 670                if (!old)
 671                        goto add_new_inode;
 672                list_add(&inode->i_list, &inode_unused);
 673                inodes_stat.nr_free_inodes++;
 674                spin_unlock(&inode_lock);
 675                wait_on_inode(old);
 676                return old;
 677        }
 678        return inode;
 679}
 680
 681static inline unsigned long hash(struct super_block *sb, unsigned long i_ino)
 682{
 683        unsigned long tmp = i_ino | (unsigned long) sb;
 684        tmp = tmp + (tmp >> HASH_BITS);
 685        return tmp & HASH_MASK;
 686}
 687
 688/* Yeah, I know about quadratic hash. Maybe, later. */
 689ino_t iunique(struct super_block *sb, ino_t max_reserved)
 690{
 691        static ino_t counter = 0;
 692        struct inode *inode;
 693        struct list_head * head;
 694        ino_t res;
 695        spin_lock(&inode_lock);
 696retry:
 697        if (counter > max_reserved) {
 698                head = inode_hashtable + hash(sb,counter);
 699                inode = find_inode(sb, res = counter++, head, NULL, NULL);
 700                if (!inode) {
 701                        spin_unlock(&inode_lock);
 702                        return res;
 703                }
 704                inode->i_count--; /* compensate find_inode() */
 705        } else {
 706                counter = max_reserved + 1;
 707        }
 708        goto retry;
 709        
 710}
 711
 712struct inode *igrab(struct inode *inode)
 713{
 714        spin_lock(&inode_lock);
 715        if (inode->i_state & I_FREEING)
 716                inode = NULL;
 717        else
 718                inode->i_count++;
 719        spin_unlock(&inode_lock);
 720        if (inode)
 721                wait_on_inode(inode);
 722        return inode;
 723}
 724
 725struct inode *iget4(struct super_block *sb, unsigned long ino, find_inode_t find_actor, void *opaque)
 726{
 727        struct list_head * head = inode_hashtable + hash(sb,ino);
 728        struct inode * inode;
 729
 730        spin_lock(&inode_lock);
 731        inode = find_inode(sb, ino, head, find_actor, opaque);
 732        if (inode) {
 733                spin_unlock(&inode_lock);
 734                wait_on_inode(inode);
 735                return inode;
 736        }
 737        /*
 738         * get_new_inode() will do the right thing, releasing
 739         * the inode lock and re-trying the search in case it
 740         * had to block at any point.
 741         */
 742        return get_new_inode(sb, ino, head, find_actor, opaque);
 743}
 744
 745struct inode *iget(struct super_block *sb, unsigned long ino)
 746{
 747        return iget4(sb, ino, NULL, NULL);
 748}
 749
 750void insert_inode_hash(struct inode *inode)
 751{
 752        struct list_head *head = inode_hashtable + hash(inode->i_sb, inode->i_ino);
 753        spin_lock(&inode_lock);
 754        list_add(&inode->i_hash, head);
 755        spin_unlock(&inode_lock);
 756}
 757
 758void remove_inode_hash(struct inode *inode)
 759{
 760        spin_lock(&inode_lock);
 761        list_del(&inode->i_hash);
 762        INIT_LIST_HEAD(&inode->i_hash);
 763        spin_unlock(&inode_lock);
 764}
 765
 766void iput(struct inode *inode)
 767{
 768        if (inode) {
 769                struct super_operations *op = NULL;
 770
 771                if (inode->i_sb && inode->i_sb->s_op)
 772                        op = inode->i_sb->s_op;
 773                if (op && op->put_inode)
 774                        op->put_inode(inode);
 775
 776                spin_lock(&inode_lock);
 777                if (!--inode->i_count) {
 778                        if (!inode->i_nlink) {
 779                                list_del(&inode->i_hash);
 780                                INIT_LIST_HEAD(&inode->i_hash);
 781                                list_del(&inode->i_list);
 782                                INIT_LIST_HEAD(&inode->i_list);
 783                                inode->i_state|=I_FREEING;
 784                                if (op && op->delete_inode) {
 785                                        void (*delete)(struct inode *) = op->delete_inode;
 786                                        spin_unlock(&inode_lock);
 787                                        delete(inode);
 788                                        spin_lock(&inode_lock);
 789                                }
 790                        }
 791                        if (list_empty(&inode->i_hash)) {
 792                                list_del(&inode->i_list);
 793                                INIT_LIST_HEAD(&inode->i_list);
 794                                inode->i_state|=I_FREEING;
 795                                spin_unlock(&inode_lock);
 796                                clear_inode(inode);
 797                                spin_lock(&inode_lock);
 798                                list_add(&inode->i_list, &inode_unused);
 799                                inodes_stat.nr_free_inodes++;
 800                        }
 801                        else if (!(inode->i_state & I_DIRTY)) {
 802                                list_del(&inode->i_list);
 803                                list_add(&inode->i_list, &inode_in_use);
 804                        }
 805#ifdef INODE_PARANOIA
 806if (inode->i_flock)
 807printk(KERN_ERR "iput: inode %s/%ld still has locks!\n",
 808kdevname(inode->i_dev), inode->i_ino);
 809if (!list_empty(&inode->i_dentry))
 810printk(KERN_ERR "iput: device %s inode %ld still has aliases!\n",
 811kdevname(inode->i_dev), inode->i_ino);
 812if (inode->i_count)
 813printk(KERN_ERR "iput: device %s inode %ld count changed, count=%d\n",
 814kdevname(inode->i_dev), inode->i_ino, inode->i_count);
 815if (atomic_read(&inode->i_sem.count) != 1)
 816printk(KERN_ERR "iput: Aieee, semaphore in use inode %s/%ld, count=%d\n",
 817kdevname(inode->i_dev), inode->i_ino, atomic_read(&inode->i_sem.count));
 818if (atomic_read(&inode->i_atomic_write.count) != 1)
 819printk(KERN_ERR "iput: Aieee, atomic write semaphore in use inode %s/%ld, count=%d\n",
 820kdevname(inode->i_dev), inode->i_ino, atomic_read(&inode->i_sem.count));
 821#endif
 822                }
 823                if (inode->i_count > (1<<31)) {
 824                        printk(KERN_ERR "iput: inode %s/%ld count wrapped\n",
 825                                kdevname(inode->i_dev), inode->i_ino);
 826                }
 827                spin_unlock(&inode_lock);
 828        }
 829}
 830
 831int bmap(struct inode * inode, int block)
 832{
 833        if (inode->i_op && inode->i_op->bmap)
 834                return inode->i_op->bmap(inode, block);
 835        return 0;
 836}
 837
 838/*
 839 * Initialize the hash tables and default
 840 * value for max inodes
 841 */
 842#define MAX_INODE (16384)
 843
 844void __init inode_init(void)
 845{
 846        int i, max;
 847        struct list_head *head = inode_hashtable;
 848
 849        i = HASH_SIZE;
 850        do {
 851                INIT_LIST_HEAD(head);
 852                head++;
 853                i--;
 854        } while (i);
 855
 856        /* Initial guess at reasonable inode number */
 857        max = num_physpages >> 1;
 858        if (max > MAX_INODE)
 859                max = MAX_INODE;
 860        max_inodes = max;
 861
 862        /* Get a random number. */
 863        get_random_bytes (&inode_generation_count,
 864                          sizeof (inode_generation_count));
 865}
 866
 867/* This belongs in file_table.c, not here... */
 868int fs_may_remount_ro(struct super_block *sb)
 869{
 870        struct file *file;
 871
 872        /* Check that no files are currently opened for writing. */
 873        for (file = inuse_filps; file; file = file->f_next) {
 874                struct inode *inode;
 875                if (!file->f_dentry)
 876                        continue;
 877                inode = file->f_dentry->d_inode;
 878                if (!inode || inode->i_sb != sb)
 879                        continue;
 880
 881                /* File with pending delete? */
 882                if (inode->i_nlink == 0)
 883                        return 0;
 884
 885                /* Writable file? */
 886                if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
 887                        return 0;
 888        }
 889        return 1; /* Tis' cool bro. */
 890}
 891
 892void update_atime (struct inode *inode)
 893{
 894    if ( inode->i_atime == CURRENT_TIME ) return;
 895    if ( IS_NOATIME (inode) ) return;
 896    if ( IS_NODIRATIME (inode) && S_ISDIR (inode->i_mode) ) return;
 897    if ( IS_RDONLY (inode) ) return;
 898    inode->i_atime = CURRENT_TIME;
 899    mark_inode_dirty (inode);
 900}   /*  End Function update_atime  */
 901
 902/* This function is called by nfsd. */
 903struct inode *iget_in_use(struct super_block *sb, unsigned long ino)
 904{
 905        struct list_head * head = inode_hashtable + hash(sb,ino);
 906        struct inode * inode;
 907
 908        spin_lock(&inode_lock);
 909        inode = find_inode(sb, ino, head, NULL, NULL);
 910        if (inode) {
 911                spin_unlock(&inode_lock);
 912                wait_on_inode(inode);
 913        }
 914        else
 915                inode = get_new_inode (sb, ino, head, NULL, NULL);
 916
 917        /* When we get the inode, we have to check if it is in use. We
 918           have to release it if it is not. */
 919        if (inode) {
 920                spin_lock(&inode_lock);
 921                if (inode->i_nlink == 0 && inode->i_count == 1) {
 922                        --inode->i_count;
 923                        list_del(&inode->i_hash);
 924                        INIT_LIST_HEAD(&inode->i_hash);
 925                        list_del(&inode->i_list);
 926                        INIT_LIST_HEAD(&inode->i_list);
 927                        if (list_empty(&inode->i_hash)) {
 928                                list_del(&inode->i_list);
 929                                INIT_LIST_HEAD(&inode->i_list);
 930                                spin_unlock(&inode_lock);
 931                                clear_inode(inode);
 932                                spin_lock(&inode_lock);
 933                                list_add(&inode->i_list, &inode_unused);
 934                                inodes_stat.nr_free_inodes++;
 935                        }
 936                        else if (!(inode->i_state & I_DIRTY)) {
 937                                list_del(&inode->i_list);
 938                                list_add(&inode->i_list, &inode_in_use);
 939                        }
 940                        inode = NULL;
 941                }
 942                spin_unlock(&inode_lock);
 943        }
 944        return inode;
 945}
 946
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.