CTC++ Coverage Report - Execution Profile    #892/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: fs/ext2/ialloc.c
Instrumentation mode: function-decision-multicondition
TER: 0 % ( 0/282)

Start/ End/    
True False - Line Source

  1 /*
  2  *  linux/fs/ext2/ialloc.c
  3  *
  4  * Copyright (C) 1992, 1993, 1994, 1995
  5  * Remy Card (card@masi.ibp.fr)
  6  * Laboratoire MASI - Institut Blaise Pascal
  7  * Universite Pierre et Marie Curie (Paris VI)
  8  *
  9  *  BSD ufs-inspired inode and directory allocation by 
  10  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  11  *  Big-endian to little-endian byte-swapping/bitmaps by
  12  *        David S. Miller (davem@caip.rutgers.edu), 1995
  13  */
  14 
  15 #include <linux/config.h>
  16 #include <linux/quotaops.h>
  17 #include <linux/sched.h>
  18 #include <linux/backing-dev.h>
  19 #include <linux/buffer_head.h>
  20 #include <linux/random.h>
  21 #include "ext2.h"
  22 #include "xattr.h"
  23 #include "acl.h"
  24 
  25 /*
  26  * ialloc.c contains the inodes allocation and deallocation routines
  27  */
  28 
  29 /*
  30  * The free inodes are managed by bitmaps.  A file system contains several
  31  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
  32  * block for inodes, N blocks for the inode table and data blocks.
  33  *
  34  * The file system contains group descriptors which are located after the
  35  * super block.  Each descriptor contains the number of the bitmap block and
  36  * the free blocks count in the block.
  37  */
  38 
  39 
  40 /*
  41  * Read the inode allocation bitmap for a given block_group, reading
  42  * into the specified slot in the superblock's bitmap cache.
  43  *
  44  * Return buffer_head of bitmap on success or NULL.
  45  */
  46 static struct buffer_head *
 
- 47 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
  48 {
  49    struct ext2_group_desc *desc;
  50    struct buffer_head *bh = NULL;
  51 
  52    desc = ext2_get_group_desc(sb, block_group, NULL);
- 53    if (!desc)
 - 54       goto error_out;
  55 
  56    bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
- 57    if (!bh)
  58       ext2_error(sb, "read_inode_bitmap",
  59              "Cannot read inode bitmap - "
  60              "block_group = %lu, inode_bitmap = %u",
  61              block_group, le32_to_cpu(desc->bg_inode_bitmap));
  62 error_out:
 - 63    return bh;
  64 }
  65 
 
- 66 static void ext2_release_inode(struct super_block *sb, int group, int dir)
  67 {
  68    struct ext2_group_desc * desc;
  69    struct buffer_head *bh;
  70 
  71    desc = ext2_get_group_desc(sb, group, &bh);
- 72    if (!desc) {
  73       ext2_error(sb, "ext2_release_inode",
  74          "can't get descriptor for group %d", group);
 - 75       return;
  76    }
  77 
    78    spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
    78   do
- 78   do-while (0)
- 78 do-while (0)
  79    desc->bg_free_inodes_count =
  80       cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
- 81    if (dir)
  82       desc->bg_used_dirs_count =
  83          cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
    84    spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
    84   do
- 84   do-while (0)
- 84 do-while (0)
- 85    if (dir)
  86       percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
  87    sb->s_dirt = 1;
  88    mark_buffer_dirty(bh);
  89 }
  90 
  91 /*
  92  * NOTE! When we get the inode, we're the only people
  93  * that have access to it, and as such there are no
  94  * race conditions we have to worry about. The inode
  95  * is not on the hash-lists, and it cannot be reached
  96  * through the filesystem because the directory entry
  97  * has been deleted earlier.
  98  *
  99  * HOWEVER: we must make sure that we get no aliases,
  100  * which means that we have to call "clear_inode()"
  101  * _before_ we mark the inode not in use in the inode
  102  * bitmaps. Otherwise a newly created file might use
  103  * the same inode number (not actually the same pointer
  104  * though), and then we'd have two inodes sharing the
  105  * same inode number and space on the harddisk.
  106  */
 
- 107 void ext2_free_inode (struct inode * inode)
  108 {
  109    struct super_block * sb = inode->i_sb;
  110    int is_directory;
  111    unsigned long ino;
  112    struct buffer_head *bitmap_bh = NULL;
  113    unsigned long block_group;
  114    unsigned long bit;
  115    struct ext2_super_block * es;
  116 
  117    ino = inode->i_ino;
  118    ext2_debug ("freeing inode %lu\n", ino);
  119 
  120    /*
  121     * Note: we must free any quota before locking the superblock,
  122     * as writing the quota to disk may need the lock as well.
  123     */
- 124    if (!is_bad_inode(inode)) {
  125       /* Quota is already initialized in iput() */
  126       ext2_xattr_delete_inode(inode);
    127           DQUOT_FREE_INODE(inode);
- 127   do-while (0)
    128       DQUOT_DROP(inode);
- 128   do-while (0)
  129    }
  130 
  131    es = EXT2_SB(sb)->s_es;
  132    is_directory = S_ISDIR(inode->i_mode);
  133 
  134    /* Do this BEFORE marking the inode not in use or returning an error */
  135    clear_inode (inode);
  136 
  137    if (ino < EXT2_FIRST_INO(sb) ||
- 138        ino > le32_to_cpu(es->s_inodes_count)) {
 - 138   T || _
 - 138   F || T
 - 138   F || F
  139       ext2_error (sb, "ext2_free_inode",
  140              "reserved or nonexistent inode %lu", ino);
 - 141       goto error_return;
  142    }
  143    block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  144    bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
  145    brelse(bitmap_bh);
  146    bitmap_bh = read_inode_bitmap(sb, block_group);
- 147    if (!bitmap_bh)
 - 148       goto error_return;
  149 
  150    /* Ok, now we can actually update the inode bitmaps.. */
- 151    if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
  152             bit, (void *) bitmap_bh->b_data))
  153       ext2_error (sb, "ext2_free_inode",
  154                "bit already cleared for inode %lu", ino);
    155    else
  156       ext2_release_inode(sb, block_group, is_directory);
  157    mark_buffer_dirty(bitmap_bh);
- 158    if (sb->s_flags & MS_SYNCHRONOUS)
  159       sync_dirty_buffer(bitmap_bh);
  160 error_return:
  161    brelse(bitmap_bh);
  162 }
  163 
  164 /*
  165  * We perform asynchronous prereading of the new inode's inode block when
  166  * we create the inode, in the expectation that the inode will be written
  167  * back soon.  There are two reasons:
  168  *
  169  * - When creating a large number of files, the async prereads will be
  170  *   nicely merged into large reads
  171  * - When writing out a large number of inodes, we don't need to keep on
  172  *   stalling the writes while we read the inode block.
  173  *
  174  * FIXME: ext2_get_group_desc() needs to be simplified.
  175  */
 
- 176 static void ext2_preread_inode(struct inode *inode)
  177 {
  178    unsigned long block_group;
  179    unsigned long offset;
  180    unsigned long block;
  181    struct buffer_head *bh;
  182    struct ext2_group_desc * gdp;
  183    struct backing_dev_info *bdi;
  184 
  185    bdi = inode->i_mapping->backing_dev_info;
- 186    if (bdi_read_congested(bdi))
 - 187       return;
- 188    if (bdi_write_congested(bdi))
 - 189       return;
  190 
  191    block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  192    gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
- 193    if (gdp == NULL)
 - 194       return;
  195 
  196    /*
  197     * Figure out the offset within the block group inode table
  198     */
  199    offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
  200             EXT2_INODE_SIZE(inode->i_sb);
  201    block = le32_to_cpu(gdp->bg_inode_table) +
  202             (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
  203    sb_breadahead(inode->i_sb, block);
  204 }
  205 
  206 /*
  207  * There are two policies for allocating an inode.  If the new inode is
  208  * a directory, then a forward search is made for a block group with both
  209  * free space and a low directory-to-inode ratio; if that fails, then of
  210  * the groups with above-average free space, that group with the fewest
  211  * directories already is chosen.
  212  *
  213  * For other inodes, search forward from the parent directory\'s block
  214  * group to find a free inode.
  215  */
 
- 216 static int find_group_dir(struct super_block *sb, struct inode *parent)
  217 {
  218    int ngroups = EXT2_SB(sb)->s_groups_count;
  219    int avefreei = ext2_count_free_inodes(sb) / ngroups;
  220    struct ext2_group_desc *desc, *best_desc = NULL;
  221    struct buffer_head *bh, *best_bh = NULL;
  222    int group, best_group = -1;
  223 
- 224    for (group = 0; group < ngroups; group++) {
  225       desc = ext2_get_group_desc (sb, group, &bh);
- 226       if (!desc || !desc->bg_free_inodes_count)
 - 226     T || _
 - 226     F || T
 - 226     F || F
 - 227          continue;
- 228       if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
 - 229          continue;
  230       if (!best_desc || 
  231           (le16_to_cpu(desc->bg_free_blocks_count) >
- 232            le16_to_cpu(best_desc->bg_free_blocks_count))) {
 - 232     T || (_)
 - 232     F || (T)
 - 232     F || (F)
  233          best_group = group;
  234          best_desc = desc;
  235          best_bh = bh;
  236       }
  237    }
- 238    if (!best_desc)
 - 239       return -1;
  240 
 - 241    return best_group;
  242 }
  243 
  244 /* 
  245  * Orlov's allocator for directories. 
  246  * 
  247  * We always try to spread first-level directories.
  248  *
  249  * If there are blockgroups with both free inodes and free blocks counts 
  250  * not worse than average we return one with smallest directory count. 
  251  * Otherwise we simply return a random group. 
  252  * 
  253  * For the rest rules look so: 
  254  * 
  255  * It's OK to put directory into a group unless 
  256  * it has too many directories already (max_dirs) or 
  257  * it has too few free inodes left (min_inodes) or 
  258  * it has too few free blocks left (min_blocks) or 
  259  * it's already running too large debt (max_debt). 
  260  * Parent's group is prefered, if it doesn't satisfy these 
  261  * conditions we search cyclically through the rest. If none 
  262  * of the groups look good we just look for a group with more 
  263  * free inodes than average (starting at parent's group). 
  264  * 
  265  * Debt is incremented each time we allocate a directory and decremented 
  266  * when we allocate an inode, within 0--255. 
  267  */ 
  268 
  269 #define INODE_COST 64
  270 #define BLOCK_COST 256
  271 
 
- 272 static int find_group_orlov(struct super_block *sb, struct inode *parent)
  273 {
  274    int parent_group = EXT2_I(parent)->i_block_group;
  275    struct ext2_sb_info *sbi = EXT2_SB(sb);
  276    struct ext2_super_block *es = sbi->s_es;
  277    int ngroups = sbi->s_groups_count;
  278    int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
  279    int freei;
  280    int avefreei;
  281    int free_blocks;
  282    int avefreeb;
  283    int blocks_per_dir;
  284    int ndirs;
  285    int max_debt, max_dirs, min_blocks, min_inodes;
  286    int group = -1, i;
  287    struct ext2_group_desc *desc;
  288    struct buffer_head *bh;
  289 
  290    freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  291    avefreei = freei / ngroups;
  292    free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  293    avefreeb = free_blocks / ngroups;
  294    ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  295 
  296    if ((parent == sb->s_root->d_inode) ||
- 297        (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
 - 297   (T) || (_)
 - 297   (F) || (T)
 - 297   (F) || (F)
  298       struct ext2_group_desc *best_desc = NULL;
  299       struct buffer_head *best_bh = NULL;
  300       int best_ndir = inodes_per_group;
  301       int best_group = -1;
  302 
  303       get_random_bytes(&group, sizeof(group));
  304       parent_group = (unsigned)group % ngroups;
- 305       for (i = 0; i < ngroups; i++) {
  306          group = (parent_group + i) % ngroups;
  307          desc = ext2_get_group_desc (sb, group, &bh);
- 308          if (!desc || !desc->bg_free_inodes_count)
 - 308       T || _
 - 308       F || T
 - 308       F || F
 - 309             continue;
- 310          if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
 - 311             continue;
- 312          if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
 - 313             continue;
- 314          if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
 - 315             continue;
  316          best_group = group;
  317          best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  318          best_desc = desc;
  319          best_bh = bh;
  320       }
- 321       if (best_group >= 0) {
  322          desc = best_desc;
  323          bh = best_bh;
  324          group = best_group;
 - 325          goto found;
  326       }
 - 327       goto fallback;
  328    }
  329 
- 330    if (ndirs == 0)
  331       ndirs = 1;   /* percpu_counters are approximate... */
  332 
  333    blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
  334 
  335    max_dirs = ndirs / ngroups + inodes_per_group / 16;
  336    min_inodes = avefreei - inodes_per_group / 4;
  337    min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
  338 
  339    max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
- 340    if (max_debt * INODE_COST > inodes_per_group)
  341       max_debt = inodes_per_group / INODE_COST;
- 342    if (max_debt > 255)
  343       max_debt = 255;
- 344    if (max_debt == 0)
  345       max_debt = 1;
  346 
- 347    for (i = 0; i < ngroups; i++) {
  348       group = (parent_group + i) % ngroups;
  349       desc = ext2_get_group_desc (sb, group, &bh);
- 350       if (!desc || !desc->bg_free_inodes_count)
 - 350     T || _
 - 350     F || T
 - 350     F || F
 - 351          continue;
- 352       if (sbi->s_debts[group] >= max_debt)
 - 353          continue;
- 354       if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
 - 355          continue;
- 356       if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
 - 357          continue;
- 358       if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
 - 359          continue;
 - 360       goto found;
  361    }
  362 
  363 fallback:
- 364    for (i = 0; i < ngroups; i++) {
  365       group = (parent_group + i) % ngroups;
  366       desc = ext2_get_group_desc (sb, group, &bh);
- 367       if (!desc || !desc->bg_free_inodes_count)
 - 367     T || _
 - 367     F || T
 - 367     F || F
 - 368          continue;
- 369       if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
 - 370          goto found;
  371    }
  372 
- 373    if (avefreei) {
  374       /*
  375        * The free-inodes counter is approximate, and for really small
  376        * filesystems the above test can fail to find any blockgroups
  377        */
  378       avefreei = 0;
 - 379       goto fallback;
  380    }
  381 
 - 382    return -1;
  383 
  384 found:
 - 385    return group;
  386 }
  387 
 
- 388 static int find_group_other(struct super_block *sb, struct inode *parent)
  389 {
  390    int parent_group = EXT2_I(parent)->i_block_group;
  391    int ngroups = EXT2_SB(sb)->s_groups_count;
  392    struct ext2_group_desc *desc;
  393    struct buffer_head *bh;
  394    int group, i;
  395 
  396    /*
  397     * Try to place the inode in its parent directory
  398     */
  399    group = parent_group;
  400    desc = ext2_get_group_desc (sb, group, &bh);
  401    if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
- 402          le16_to_cpu(desc->bg_free_blocks_count))
 - 402   T && (T) && (T)
 - 402   T && (T) && (F)
 - 402   T && (F) && (_)
 - 402   F && (_) && (_)
 - 403       goto found;
  404 
  405    /*
  406     * We're going to place this inode in a different blockgroup from its
  407     * parent.  We want to cause files in a common directory to all land in
  408     * the same blockgroup.  But we want files which are in a different
  409     * directory which shares a blockgroup with our parent to land in a
  410     * different blockgroup.
  411     *
  412     * So add our directory's i_ino into the starting point for the hash.
  413     */
  414    group = (group + parent->i_ino) % ngroups;
  415 
  416    /*
  417     * Use a quadratic hash to find a group with a free inode and some
  418     * free blocks.
  419     */
- 420    for (i = 1; i < ngroups; i <<= 1) {
  421       group += i;
- 422       if (group >= ngroups)
  423          group -= ngroups;
  424       desc = ext2_get_group_desc (sb, group, &bh);
  425       if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
- 426             le16_to_cpu(desc->bg_free_blocks_count))
 - 426     T && (T) && (T)
 - 426     T && (T) && (F)
 - 426     T && (F) && (_)
 - 426     F && (_) && (_)
 - 427          goto found;
  428    }
  429 
  430    /*
  431     * That failed: try linear search for a free inode, even if that group
  432     * has no free blocks.
  433     */
  434    group = parent_group;
- 435    for (i = 0; i < ngroups; i++) {
- 436       if (++group >= ngroups)
  437          group = 0;
  438       desc = ext2_get_group_desc (sb, group, &bh);
- 439       if (desc && le16_to_cpu(desc->bg_free_inodes_count))
 - 439     T && (T)
 - 439     T && (F)
 - 439     F && (_)
 - 440          goto found;
  441    }
  442 
 - 443    return -1;
  444 
  445 found:
 - 446    return group;
  447 }
  448 
 
- 449 struct inode *ext2_new_inode(struct inode *dir, int mode)
  450 {
  451    struct super_block *sb;
  452    struct buffer_head *bitmap_bh = NULL;
  453    struct buffer_head *bh2;
  454    int group, i;
  455    ino_t ino = 0;
  456    struct inode * inode;
  457    struct ext2_group_desc *gdp;
  458    struct ext2_super_block *es;
  459    struct ext2_inode_info *ei;
  460    struct ext2_sb_info *sbi;
  461    int err;
  462 
  463    sb = dir->i_sb;
  464    inode = new_inode(sb);
- 465    if (!inode)
 - 466       return ERR_PTR(-ENOMEM);
  467 
  468    ei = EXT2_I(inode);
  469    sbi = EXT2_SB(sb);
  470    es = sbi->s_es;
- 471    if (S_ISDIR(mode)) {
- 472       if (test_opt(sb, OLDALLOC))
  473          group = find_group_dir(sb, dir);
    474       else
  475          group = find_group_orlov(sb, dir);
    476    } else 
  477       group = find_group_other(sb, dir);
  478 
- 479    if (group == -1) {
  480       err = -ENOSPC;