The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ext3/ialloc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  *  linux/fs/ext3/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@redhat.com), 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/sched.h>
   16 #include <linux/fs.h>
   17 #include <linux/jbd.h>
   18 #include <linux/ext3_fs.h>
   19 #include <linux/ext3_jbd.h>
   20 #include <linux/stat.h>
   21 #include <linux/string.h>
   22 #include <linux/locks.h>
   23 #include <linux/quotaops.h>
   24 
   25 #include <asm/bitops.h>
   26 #include <asm/byteorder.h>
   27 
   28 /*
   29  * ialloc.c contains the inodes allocation and deallocation routines
   30  */
   31 
   32 /*
   33  * The free inodes are managed by bitmaps.  A file system contains several
   34  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
   35  * block for inodes, N blocks for the inode table and data blocks.
   36  *
   37  * The file system contains group descriptors which are located after the
   38  * super block.  Each descriptor contains the number of the bitmap block and
   39  * the free blocks count in the block.  The descriptors are loaded in memory
   40  * when a file system is mounted (see ext3_read_super).
   41  */
   42 
   43 
   44 /*
   45  * Read the inode allocation bitmap for a given block_group, reading
   46  * into the specified slot in the superblock's bitmap cache.
   47  *
   48  * Return >=0 on success or a -ve error code.
   49  */
   50 static int read_inode_bitmap (struct super_block * sb,
   51                                unsigned long block_group,
   52                                unsigned int bitmap_nr)
   53 {
   54         struct ext3_group_desc * gdp;
   55         struct buffer_head * bh = NULL;
   56         int retval = 0;
   57 
   58         gdp = ext3_get_group_desc (sb, block_group, NULL);
   59         if (!gdp) {
   60                 retval = -EIO;
   61                 goto error_out;
   62         }
   63         bh = sb_bread(sb, le32_to_cpu(gdp->bg_inode_bitmap));
   64         if (!bh) {
   65                 ext3_error (sb, "read_inode_bitmap",
   66                             "Cannot read inode bitmap - "
   67                             "block_group = %lu, inode_bitmap = %u",
   68                             block_group, gdp->bg_inode_bitmap);
   69                 retval = -EIO;
   70         }
   71         /*
   72          * On IO error, just leave a zero in the superblock's block pointer for
   73          * this group.  The IO will be retried next time.
   74          */
   75 error_out:
   76         sb->u.ext3_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
   77         sb->u.ext3_sb.s_inode_bitmap[bitmap_nr] = bh;
   78         return retval;
   79 }
   80 
   81 /*
   82  * load_inode_bitmap loads the inode bitmap for a blocks group
   83  *
   84  * It maintains a cache for the last bitmaps loaded.  This cache is managed
   85  * with a LRU algorithm.
   86  *
   87  * Notes:
   88  * 1/ There is one cache per mounted file system.
   89  * 2/ If the file system contains less than EXT3_MAX_GROUP_LOADED groups,
   90  *    this function reads the bitmap without maintaining a LRU cache.
   91  *
   92  * Return the slot used to store the bitmap, or a -ve error code.
   93  */
   94 static int load_inode_bitmap (struct super_block * sb,
   95                               unsigned int block_group)
   96 {
   97         struct ext3_sb_info *sbi = EXT3_SB(sb);
   98         unsigned long inode_bitmap_number;
   99         struct buffer_head * inode_bitmap;
  100         int i, j, retval = 0;
  101 
  102         if (block_group >= sbi->s_groups_count)
  103                 ext3_panic (sb, "load_inode_bitmap",
  104                             "block_group >= groups_count - "
  105                             "block_group = %d, groups_count = %lu",
  106                             block_group, sbi->s_groups_count);
  107         if (sbi->s_loaded_inode_bitmaps > 0 &&
  108             sbi->s_inode_bitmap_number[0] == block_group &&
  109             sbi->s_inode_bitmap[0] != NULL)
  110                 return 0;
  111         if (sbi->s_groups_count <= EXT3_MAX_GROUP_LOADED) {
  112                 if (sbi->s_inode_bitmap[block_group]) {
  113                         if (sbi->s_inode_bitmap_number[block_group] !=
  114                                                 block_group)
  115                                 ext3_panic(sb, "load_inode_bitmap",
  116                                         "block_group != inode_bitmap_number");
  117                         return block_group;
  118                 }
  119                 retval = read_inode_bitmap(sb, block_group, block_group);
  120                 if (retval < 0)
  121                         return retval;
  122                 return block_group;
  123         }
  124 
  125         for (i = 0; i < sbi->s_loaded_inode_bitmaps &&
  126                     sbi->s_inode_bitmap_number[i] != block_group; i++)
  127                 /* do nothing */;
  128         if (i < sbi->s_loaded_inode_bitmaps &&
  129             sbi->s_inode_bitmap_number[i] == block_group) {
  130                 inode_bitmap_number = sbi->s_inode_bitmap_number[i];
  131                 inode_bitmap = sbi->s_inode_bitmap[i];
  132                 for (j = i; j > 0; j--) {
  133                         sbi->s_inode_bitmap_number[j] =
  134                                 sbi->s_inode_bitmap_number[j - 1];
  135                         sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
  136                 }
  137                 sbi->s_inode_bitmap_number[0] = inode_bitmap_number;
  138                 sbi->s_inode_bitmap[0] = inode_bitmap;
  139 
  140                 /*
  141                  * There's still one special case here --- if inode_bitmap == 0
  142                  * then our last attempt to read the bitmap failed and we have
  143                  * just ended up caching that failure.  Try again to read it.
  144                  */
  145                 if (!inode_bitmap)
  146                         retval = read_inode_bitmap (sb, block_group, 0);
  147         } else {
  148                 if (sbi->s_loaded_inode_bitmaps < EXT3_MAX_GROUP_LOADED)
  149                         sbi->s_loaded_inode_bitmaps++;
  150                 else
  151                         brelse(sbi->s_inode_bitmap[EXT3_MAX_GROUP_LOADED - 1]);
  152                 for (j = sbi->s_loaded_inode_bitmaps - 1; j > 0; j--) {
  153                         sbi->s_inode_bitmap_number[j] =
  154                                 sbi->s_inode_bitmap_number[j - 1];
  155                         sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
  156                 }
  157                 retval = read_inode_bitmap (sb, block_group, 0);
  158         }
  159         return retval;
  160 }
  161 
  162 /*
  163  * NOTE! When we get the inode, we're the only people
  164  * that have access to it, and as such there are no
  165  * race conditions we have to worry about. The inode
  166  * is not on the hash-lists, and it cannot be reached
  167  * through the filesystem because the directory entry
  168  * has been deleted earlier.
  169  *
  170  * HOWEVER: we must make sure that we get no aliases,
  171  * which means that we have to call "clear_inode()"
  172  * _before_ we mark the inode not in use in the inode
  173  * bitmaps. Otherwise a newly created file might use
  174  * the same inode number (not actually the same pointer
  175  * though), and then we'd have two inodes sharing the
  176  * same inode number and space on the harddisk.
  177  */
  178 void ext3_free_inode (handle_t *handle, struct inode * inode)
  179 {
  180         struct super_block * sb = inode->i_sb;
  181         int is_directory;
  182         unsigned long ino;
  183         struct buffer_head * bh;
  184         struct buffer_head * bh2;
  185         unsigned long block_group;
  186         unsigned long bit;
  187         int bitmap_nr;
  188         struct ext3_group_desc * gdp;
  189         struct ext3_super_block * es;
  190         int fatal = 0, err;
  191 
  192         if (!inode->i_dev) {
  193                 printk ("ext3_free_inode: inode has no device\n");
  194                 return;
  195         }
  196         if (atomic_read(&inode->i_count) > 1) {
  197                 printk ("ext3_free_inode: inode has count=%d\n",
  198                                         atomic_read(&inode->i_count));
  199                 return;
  200         }
  201         if (inode->i_nlink) {
  202                 printk ("ext3_free_inode: inode has nlink=%d\n",
  203                         inode->i_nlink);
  204                 return;
  205         }
  206         if (!sb) {
  207                 printk("ext3_free_inode: inode on nonexistent device\n");
  208                 return;
  209         }
  210 
  211         ino = inode->i_ino;
  212         ext3_debug ("freeing inode %lu\n", ino);
  213 
  214         /*
  215          * Note: we must free any quota before locking the superblock,
  216          * as writing the quota to disk may need the lock as well.
  217          */
  218         DQUOT_INIT(inode);
  219         DQUOT_FREE_INODE(inode);
  220         DQUOT_DROP(inode);
  221 
  222         is_directory = S_ISDIR(inode->i_mode);
  223 
  224         /* Do this BEFORE marking the inode not in use or returning an error */
  225         clear_inode (inode);
  226 
  227         lock_super (sb);
  228         es = sb->u.ext3_sb.s_es;
  229         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  230                 ext3_error (sb, "ext3_free_inode",
  231                             "reserved or nonexistent inode %lu", ino);
  232                 goto error_return;
  233         }
  234         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  235         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  236         bitmap_nr = load_inode_bitmap (sb, block_group);
  237         if (bitmap_nr < 0)
  238                 goto error_return;
  239 
  240         bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
  241 
  242         BUFFER_TRACE(bh, "get_write_access");
  243         fatal = ext3_journal_get_write_access(handle, bh);
  244         if (fatal)
  245                 goto error_return;
  246 
  247         /* Ok, now we can actually update the inode bitmaps.. */
  248         if (!ext3_clear_bit (bit, bh->b_data))
  249                 ext3_error (sb, "ext3_free_inode",
  250                               "bit already cleared for inode %lu", ino);
  251         else {
  252                 gdp = ext3_get_group_desc (sb, block_group, &bh2);
  253 
  254                 BUFFER_TRACE(bh2, "get_write_access");
  255                 fatal = ext3_journal_get_write_access(handle, bh2);
  256                 if (fatal) goto error_return;
  257 
  258                 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get write access");
  259                 fatal = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
  260                 if (fatal) goto error_return;
  261 
  262                 if (gdp) {
  263                         gdp->bg_free_inodes_count = cpu_to_le16(
  264                                 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
  265                         if (is_directory)
  266                                 gdp->bg_used_dirs_count = cpu_to_le16(
  267                                   le16_to_cpu(gdp->bg_used_dirs_count) - 1);
  268                 }
  269                 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  270                 err = ext3_journal_dirty_metadata(handle, bh2);
  271                 if (!fatal) fatal = err;
  272                 es->s_free_inodes_count =
  273                         cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
  274                 BUFFER_TRACE(sb->u.ext3_sb.s_sbh,
  275                                         "call ext3_journal_dirty_metadata");
  276                 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
  277                 if (!fatal) fatal = err;
  278         }
  279         BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  280         err = ext3_journal_dirty_metadata(handle, bh);
  281         if (!fatal)
  282                 fatal = err;
  283         sb->s_dirt = 1;
  284 error_return:
  285         ext3_std_error(sb, fatal);
  286         unlock_super(sb);
  287 }
  288 
  289 /*
  290  * There are two policies for allocating an inode.  If the new inode is
  291  * a directory, then a forward search is made for a block group with both
  292  * free space and a low directory-to-inode ratio; if that fails, then of
  293  * the groups with above-average free space, that group with the fewest
  294  * directories already is chosen.
  295  *
  296  * For other inodes, search forward from the parent directory's block
  297  * group to find a free inode.
  298  */
  299 struct inode * ext3_new_inode (handle_t *handle,
  300                                 const struct inode * dir, int mode)
  301 {
  302         struct super_block * sb;
  303         struct buffer_head * bh;
  304         struct buffer_head * bh2;
  305         int i, j, avefreei;
  306         struct inode * inode;
  307         int bitmap_nr;
  308         struct ext3_group_desc * gdp;
  309         struct ext3_group_desc * tmp;
  310         struct ext3_super_block * es;
  311         int err = 0;
  312 
  313         /* Cannot create files in a deleted directory */
  314         if (!dir || !dir->i_nlink)
  315                 return ERR_PTR(-EPERM);
  316 
  317         sb = dir->i_sb;
  318         inode = new_inode(sb);
  319         if (!inode)
  320                 return ERR_PTR(-ENOMEM);
  321         init_rwsem(&inode->u.ext3_i.truncate_sem);
  322 
  323         lock_super (sb);
  324         es = sb->u.ext3_sb.s_es;
  325 repeat:
  326         gdp = NULL;
  327         i = 0;
  328 
  329         if (S_ISDIR(mode)) {
  330                 avefreei = le32_to_cpu(es->s_free_inodes_count) /
  331                         sb->u.ext3_sb.s_groups_count;
  332                 if (!gdp) {
  333                         for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
  334                                 struct buffer_head *temp_buffer;
  335                                 tmp = ext3_get_group_desc (sb, j, &temp_buffer);
  336                                 if (tmp &&
  337                                     le16_to_cpu(tmp->bg_free_inodes_count) &&
  338                                     le16_to_cpu(tmp->bg_free_inodes_count) >=
  339                                                         avefreei) {
  340                                         if (!gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
  341                                                 le16_to_cpu(gdp->bg_free_blocks_count))) {
  342                                                 i = j;
  343                                                 gdp = tmp;
  344                                                 bh2 = temp_buffer;
  345                                         }
  346                                 }
  347                         }
  348                 }
  349         } else {
  350                 /*
  351                  * Try to place the inode in its parent directory
  352                  */
  353                 i = dir->u.ext3_i.i_block_group;
  354                 tmp = ext3_get_group_desc (sb, i, &bh2);
  355                 if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
  356                         gdp = tmp;
  357                 else
  358                 {
  359                         /*
  360                          * Use a quadratic hash to find a group with a
  361                          * free inode
  362                          */
  363                         for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
  364                                 i += j;
  365                                 if (i >= sb->u.ext3_sb.s_groups_count)
  366                                         i -= sb->u.ext3_sb.s_groups_count;
  367                                 tmp = ext3_get_group_desc (sb, i, &bh2);
  368                                 if (tmp &&
  369                                     le16_to_cpu(tmp->bg_free_inodes_count)) {
  370                                         gdp = tmp;
  371                                         break;
  372                                 }
  373                         }
  374                 }
  375                 if (!gdp) {
  376                         /*
  377                          * That failed: try linear search for a free inode
  378                          */
  379                         i = dir->u.ext3_i.i_block_group + 1;
  380                         for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
  381                                 if (++i >= sb->u.ext3_sb.s_groups_count)
  382                                         i = 0;
  383                                 tmp = ext3_get_group_desc (sb, i, &bh2);
  384                                 if (tmp &&
  385                                     le16_to_cpu(tmp->bg_free_inodes_count)) {
  386                                         gdp = tmp;
  387                                         break;
  388                                 }
  389                         }
  390                 }
  391         }
  392 
  393         err = -ENOSPC;
  394         if (!gdp)
  395                 goto out;
  396 
  397         err = -EIO;
  398         bitmap_nr = load_inode_bitmap (sb, i);
  399         if (bitmap_nr < 0)
  400                 goto fail;
  401 
  402         bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
  403 
  404         if ((j = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
  405                                       EXT3_INODES_PER_GROUP(sb))) <
  406             EXT3_INODES_PER_GROUP(sb)) {
  407                 BUFFER_TRACE(bh, "get_write_access");
  408                 err = ext3_journal_get_write_access(handle, bh);
  409                 if (err) goto fail;
  410                 
  411                 if (ext3_set_bit (j, bh->b_data)) {
  412                         ext3_error (sb, "ext3_new_inode",
  413                                       "bit already set for inode %d", j);
  414                         goto repeat;
  415                 }
  416                 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  417                 err = ext3_journal_dirty_metadata(handle, bh);
  418                 if (err) goto fail;
  419         } else {
  420                 if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
  421                         ext3_error (sb, "ext3_new_inode",
  422                                     "Free inodes count corrupted in group %d",
  423                                     i);
  424                         /* Is it really ENOSPC? */
  425                         err = -ENOSPC;
  426                         if (sb->s_flags & MS_RDONLY)
  427                                 goto fail;
  428 
  429                         BUFFER_TRACE(bh2, "get_write_access");
  430                         err = ext3_journal_get_write_access(handle, bh2);
  431                         if (err) goto fail;
  432                         gdp->bg_free_inodes_count = 0;
  433                         BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  434                         err = ext3_journal_dirty_metadata(handle, bh2);
  435                         if (err) goto fail;
  436                 }
  437                 goto repeat;
  438         }
  439         j += i * EXT3_INODES_PER_GROUP(sb) + 1;
  440         if (j < EXT3_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
  441                 ext3_error (sb, "ext3_new_inode",
  442                             "reserved inode or inode > inodes count - "
  443                             "block_group = %d,inode=%d", i, j);
  444                 err = -EIO;
  445                 goto fail;
  446         }
  447 
  448         BUFFER_TRACE(bh2, "get_write_access");
  449         err = ext3_journal_get_write_access(handle, bh2);
  450         if (err) goto fail;
  451         gdp->bg_free_inodes_count =
  452                 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
  453         if (S_ISDIR(mode))
  454                 gdp->bg_used_dirs_count =
  455                         cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
  456         BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  457         err = ext3_journal_dirty_metadata(handle, bh2);
  458         if (err) goto fail;
  459         
  460         BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
  461         err = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
  462         if (err) goto fail;
  463         es->s_free_inodes_count =
  464                 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
  465         BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "call ext3_journal_dirty_metadata");
  466         err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
  467         sb->s_dirt = 1;
  468         if (err) goto fail;
  469 
  470         inode->i_uid = current->fsuid;
  471         if (test_opt (sb, GRPID))
  472                 inode->i_gid = dir->i_gid;
  473         else if (dir->i_mode & S_ISGID) {
  474                 inode->i_gid = dir->i_gid;
  475                 if (S_ISDIR(mode))
  476                         mode |= S_ISGID;
  477         } else
  478                 inode->i_gid = current->fsgid;
  479         inode->i_mode = mode;
  480 
  481         inode->i_ino = j;
  482         /* This is the optimal IO size (for stat), not the fs block size */
  483         inode->i_blksize = PAGE_SIZE;
  484         inode->i_blocks = 0;
  485         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  486         inode->u.ext3_i.i_flags = dir->u.ext3_i.i_flags & ~EXT3_INDEX_FL;
  487         if (S_ISLNK(mode))
  488                 inode->u.ext3_i.i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
  489 #ifdef EXT3_FRAGMENTS
  490         inode->u.ext3_i.i_faddr = 0;
  491         inode->u.ext3_i.i_frag_no = 0;
  492         inode->u.ext3_i.i_frag_size = 0;
  493 #endif
  494         inode->u.ext3_i.i_file_acl = 0;
  495         inode->u.ext3_i.i_dir_acl = 0;
  496         inode->u.ext3_i.i_dtime = 0;
  497         INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
  498 #ifdef EXT3_PREALLOCATE
  499         inode->u.ext3_i.i_prealloc_count = 0;
  500 #endif
  501         inode->u.ext3_i.i_block_group = i;
  502         
  503         ext3_set_inode_flags(inode);
  504         if (IS_SYNC(inode))
  505                 handle->h_sync = 1;
  506         insert_inode_hash(inode);
  507         inode->i_generation = sb->u.ext3_sb.s_next_generation++;
  508 
  509         inode->u.ext3_i.i_state = EXT3_STATE_NEW;
  510         err = ext3_mark_inode_dirty(handle, inode);
  511         if (err) goto fail;
  512         
  513         unlock_super (sb);
  514         if(DQUOT_ALLOC_INODE(inode)) {
  515                 DQUOT_DROP(inode);
  516                 inode->i_flags |= S_NOQUOTA;
  517                 inode->i_nlink = 0;
  518                 iput(inode);
  519                 return ERR_PTR(-EDQUOT);
  520         }
  521         ext3_debug ("allocating inode %lu\n", inode->i_ino);
  522         return inode;
  523 
  524 fail:
  525         ext3_std_error(sb, err);
  526 out:
  527         unlock_super(sb);
  528         iput(inode);
  529         return ERR_PTR(err);
  530 }
  531 
  532 /* Verify that we are loading a valid orphan from disk */
  533 struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
  534 {
  535         unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
  536         unsigned long block_group;
  537         int bit;
  538         int bitmap_nr;
  539         struct buffer_head *bh;
  540         struct inode *inode = NULL;
  541 
  542         /* Error cases - e2fsck has already cleaned up for us */
  543         if (ino > max_ino) {
  544                 ext3_warning(sb, __FUNCTION__,
  545                              "bad orphan ino %lu!  e2fsck was run?\n", ino);
  546                 return NULL;
  547         }
  548 
  549         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  550         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  551         if ((bitmap_nr = load_inode_bitmap(sb, block_group)) < 0 ||
  552             !(bh = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr])) {
  553                 ext3_warning(sb, __FUNCTION__,
  554                              "inode bitmap error for orphan %lu\n", ino);
  555                 return NULL;
  556         }
  557 
  558         /* Having the inode bit set should be a 100% indicator that this
  559          * is a valid orphan (no e2fsck run on fs).  Orphans also include
  560          * inodes that were being truncated, so we can't check i_nlink==0.
  561          */
  562         if (!ext3_test_bit(bit, bh->b_data) || !(inode = iget(sb, ino)) ||
  563             is_bad_inode(inode) || NEXT_ORPHAN(inode) > max_ino) {
  564                 ext3_warning(sb, __FUNCTION__,
  565                              "bad orphan inode %lu!  e2fsck was run?\n", ino);
  566                 printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%ld) = %d\n",
  567                        bit, bh->b_blocknr, ext3_test_bit(bit, bh->b_data));
  568                 printk(KERN_NOTICE "inode=%p\n", inode);
  569                 if (inode) {
  570                         printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  571                                is_bad_inode(inode));
  572                         printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  573                                NEXT_ORPHAN(inode));
  574                         printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  575                 }
  576                 /* Avoid freeing blocks if we got a bad deleted inode */
  577                 if (inode && inode->i_nlink == 0)
  578                         inode->i_blocks = 0;
  579                 iput(inode);
  580                 return NULL;
  581         }
  582 
  583         return inode;
  584 }
  585 
  586 unsigned long ext3_count_free_inodes (struct super_block * sb)
  587 {
  588 #ifdef EXT3FS_DEBUG
  589         struct ext3_super_block * es;
  590         unsigned long desc_count, bitmap_count, x;
  591         int bitmap_nr;
  592         struct ext3_group_desc * gdp;
  593         int i;
  594 
  595         lock_super (sb);
  596         es = sb->u.ext3_sb.s_es;
  597         desc_count = 0;
  598         bitmap_count = 0;
  599         gdp = NULL;
  600         for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
  601                 gdp = ext3_get_group_desc (sb, i, NULL);
  602                 if (!gdp)
  603                         continue;
  604                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  605                 bitmap_nr = load_inode_bitmap (sb, i);
  606                 if (bitmap_nr < 0)
  607                         continue;
  608 
  609                 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
  610                                      EXT3_INODES_PER_GROUP(sb) / 8);
  611                 printk ("group %d: stored = %d, counted = %lu\n",
  612                         i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  613                 bitmap_count += x;
  614         }
  615         printk("ext3_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
  616                 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  617         unlock_super (sb);
  618         return desc_count;
  619 #else
  620         return le32_to_cpu(sb->u.ext3_sb.s_es->s_free_inodes_count);
  621 #endif
  622 }
  623 
  624 #ifdef CONFIG_EXT3_CHECK
  625 /* Called at mount-time, super-block is locked */
  626 void ext3_check_inodes_bitmap (struct super_block * sb)
  627 {
  628         struct ext3_super_block * es;
  629         unsigned long desc_count, bitmap_count, x;
  630         int bitmap_nr;
  631         struct ext3_group_desc * gdp;
  632         int i;
  633 
  634         es = sb->u.ext3_sb.s_es;
  635         desc_count = 0;
  636         bitmap_count = 0;
  637         gdp = NULL;
  638         for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
  639                 gdp = ext3_get_group_desc (sb, i, NULL);
  640                 if (!gdp)
  641                         continue;
  642                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  643                 bitmap_nr = load_inode_bitmap (sb, i);
  644                 if (bitmap_nr < 0)
  645                         continue;
  646 
  647                 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
  648                                      EXT3_INODES_PER_GROUP(sb) / 8);
  649                 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
  650                         ext3_error (sb, "ext3_check_inodes_bitmap",
  651                                     "Wrong free inodes count in group %d, "
  652                                     "stored = %d, counted = %lu", i,
  653                                     le16_to_cpu(gdp->bg_free_inodes_count), x);
  654                 bitmap_count += x;
  655         }
  656         if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
  657                 ext3_error (sb, "ext3_check_inodes_bitmap",
  658                             "Wrong free inodes count in super block, "
  659                             "stored = %lu, counted = %lu",
  660                             (unsigned long)le32_to_cpu(es->s_free_inodes_count),
  661                             bitmap_count);
  662 }
  663 #endif

Cache object: ce99330877f6377e8efe0a2286f195d0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.