LCOV - code coverage report
Current view: top level - fs/ext3 - ialloc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 248 0.0 %
Date: 2014-02-18 Functions: 0 8 0.0 %
Branches: 0 184 0.0 %

           Branch data     Line data    Source code
       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/quotaops.h>
      16                 :            : #include <linux/random.h>
      17                 :            : 
      18                 :            : #include "ext3.h"
      19                 :            : #include "xattr.h"
      20                 :            : #include "acl.h"
      21                 :            : 
      22                 :            : /*
      23                 :            :  * ialloc.c contains the inodes allocation and deallocation routines
      24                 :            :  */
      25                 :            : 
      26                 :            : /*
      27                 :            :  * The free inodes are managed by bitmaps.  A file system contains several
      28                 :            :  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
      29                 :            :  * block for inodes, N blocks for the inode table and data blocks.
      30                 :            :  *
      31                 :            :  * The file system contains group descriptors which are located after the
      32                 :            :  * super block.  Each descriptor contains the number of the bitmap block and
      33                 :            :  * the free blocks count in the block.
      34                 :            :  */
      35                 :            : 
      36                 :            : 
      37                 :            : /*
      38                 :            :  * Read the inode allocation bitmap for a given block_group, reading
      39                 :            :  * into the specified slot in the superblock's bitmap cache.
      40                 :            :  *
      41                 :            :  * Return buffer_head of bitmap on success or NULL.
      42                 :            :  */
      43                 :            : static struct buffer_head *
      44                 :          0 : read_inode_bitmap(struct super_block * sb, unsigned long block_group)
      45                 :            : {
      46                 :            :         struct ext3_group_desc *desc;
      47                 :            :         struct buffer_head *bh = NULL;
      48                 :            : 
      49                 :          0 :         desc = ext3_get_group_desc(sb, block_group, NULL);
      50         [ #  # ]:          0 :         if (!desc)
      51                 :            :                 goto error_out;
      52                 :            : 
      53                 :          0 :         bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
      54         [ #  # ]:          0 :         if (!bh)
      55                 :          0 :                 ext3_error(sb, "read_inode_bitmap",
      56                 :            :                             "Cannot read inode bitmap - "
      57                 :            :                             "block_group = %lu, inode_bitmap = %u",
      58                 :            :                             block_group, le32_to_cpu(desc->bg_inode_bitmap));
      59                 :            : error_out:
      60                 :          0 :         return bh;
      61                 :            : }
      62                 :            : 
      63                 :            : /*
      64                 :            :  * NOTE! When we get the inode, we're the only people
      65                 :            :  * that have access to it, and as such there are no
      66                 :            :  * race conditions we have to worry about. The inode
      67                 :            :  * is not on the hash-lists, and it cannot be reached
      68                 :            :  * through the filesystem because the directory entry
      69                 :            :  * has been deleted earlier.
      70                 :            :  *
      71                 :            :  * HOWEVER: we must make sure that we get no aliases,
      72                 :            :  * which means that we have to call "clear_inode()"
      73                 :            :  * _before_ we mark the inode not in use in the inode
      74                 :            :  * bitmaps. Otherwise a newly created file might use
      75                 :            :  * the same inode number (not actually the same pointer
      76                 :            :  * though), and then we'd have two inodes sharing the
      77                 :            :  * same inode number and space on the harddisk.
      78                 :            :  */
      79                 :          0 : void ext3_free_inode (handle_t *handle, struct inode * inode)
      80                 :            : {
      81                 :          0 :         struct super_block * sb = inode->i_sb;
      82                 :            :         int is_directory;
      83                 :            :         unsigned long ino;
      84                 :            :         struct buffer_head *bitmap_bh = NULL;
      85                 :            :         struct buffer_head *bh2;
      86                 :            :         unsigned long block_group;
      87                 :            :         unsigned long bit;
      88                 :            :         struct ext3_group_desc * gdp;
      89                 :            :         struct ext3_super_block * es;
      90                 :          0 :         struct ext3_sb_info *sbi;
      91                 :            :         int fatal = 0, err;
      92                 :            : 
      93         [ #  # ]:          0 :         if (atomic_read(&inode->i_count) > 1) {
      94                 :          0 :                 printk ("ext3_free_inode: inode has count=%d\n",
      95                 :            :                                         atomic_read(&inode->i_count));
      96                 :          0 :                 return;
      97                 :            :         }
      98         [ #  # ]:          0 :         if (inode->i_nlink) {
      99                 :          0 :                 printk ("ext3_free_inode: inode has nlink=%d\n",
     100                 :            :                         inode->i_nlink);
     101                 :          0 :                 return;
     102                 :            :         }
     103         [ #  # ]:          0 :         if (!sb) {
     104                 :          0 :                 printk("ext3_free_inode: inode on nonexistent device\n");
     105                 :          0 :                 return;
     106                 :            :         }
     107                 :            :         sbi = EXT3_SB(sb);
     108                 :            : 
     109                 :          0 :         ino = inode->i_ino;
     110                 :            :         ext3_debug ("freeing inode %lu\n", ino);
     111                 :            :         trace_ext3_free_inode(inode);
     112                 :            : 
     113                 :          0 :         is_directory = S_ISDIR(inode->i_mode);
     114                 :            : 
     115                 :          0 :         es = EXT3_SB(sb)->s_es;
     116 [ #  # ][ #  # ]:          0 :         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
     117                 :          0 :                 ext3_error (sb, "ext3_free_inode",
     118                 :            :                             "reserved or nonexistent inode %lu", ino);
     119                 :          0 :                 goto error_return;
     120                 :            :         }
     121                 :          0 :         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
     122                 :          0 :         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
     123                 :          0 :         bitmap_bh = read_inode_bitmap(sb, block_group);
     124         [ #  # ]:          0 :         if (!bitmap_bh)
     125                 :            :                 goto error_return;
     126                 :            : 
     127                 :            :         BUFFER_TRACE(bitmap_bh, "get_write_access");
     128                 :          0 :         fatal = ext3_journal_get_write_access(handle, bitmap_bh);
     129         [ #  # ]:          0 :         if (fatal)
     130                 :            :                 goto error_return;
     131                 :            : 
     132                 :            :         /* Ok, now we can actually update the inode bitmaps.. */
     133         [ #  # ]:          0 :         if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
     134                 :            :                                         bit, bitmap_bh->b_data))
     135                 :          0 :                 ext3_error (sb, "ext3_free_inode",
     136                 :            :                               "bit already cleared for inode %lu", ino);
     137                 :            :         else {
     138                 :          0 :                 gdp = ext3_get_group_desc (sb, block_group, &bh2);
     139                 :            : 
     140                 :            :                 BUFFER_TRACE(bh2, "get_write_access");
     141                 :          0 :                 fatal = ext3_journal_get_write_access(handle, bh2);
     142         [ #  # ]:          0 :                 if (fatal) goto error_return;
     143                 :            : 
     144         [ #  # ]:          0 :                 if (gdp) {
     145                 :          0 :                         spin_lock(sb_bgl_lock(sbi, block_group));
     146                 :            :                         le16_add_cpu(&gdp->bg_free_inodes_count, 1);
     147         [ #  # ]:          0 :                         if (is_directory)
     148                 :            :                                 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
     149                 :          0 :                         spin_unlock(sb_bgl_lock(sbi, block_group));
     150                 :          0 :                         percpu_counter_inc(&sbi->s_freeinodes_counter);
     151         [ #  # ]:          0 :                         if (is_directory)
     152                 :          0 :                                 percpu_counter_dec(&sbi->s_dirs_counter);
     153                 :            : 
     154                 :            :                 }
     155                 :            :                 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
     156                 :          0 :                 err = ext3_journal_dirty_metadata(handle, bh2);
     157         [ #  # ]:          0 :                 if (!fatal) fatal = err;
     158                 :            :         }
     159                 :            :         BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
     160                 :          0 :         err = ext3_journal_dirty_metadata(handle, bitmap_bh);
     161         [ #  # ]:          0 :         if (!fatal)
     162                 :            :                 fatal = err;
     163                 :            : 
     164                 :            : error_return:
     165                 :            :         brelse(bitmap_bh);
     166         [ #  # ]:          0 :         ext3_std_error(sb, fatal);
     167                 :            : }
     168                 :            : 
     169                 :            : /*
     170                 :            :  * Orlov's allocator for directories.
     171                 :            :  *
     172                 :            :  * We always try to spread first-level directories.
     173                 :            :  *
     174                 :            :  * If there are blockgroups with both free inodes and free blocks counts
     175                 :            :  * not worse than average we return one with smallest directory count.
     176                 :            :  * Otherwise we simply return a random group.
     177                 :            :  *
     178                 :            :  * For the rest rules look so:
     179                 :            :  *
     180                 :            :  * It's OK to put directory into a group unless
     181                 :            :  * it has too many directories already (max_dirs) or
     182                 :            :  * it has too few free inodes left (min_inodes) or
     183                 :            :  * it has too few free blocks left (min_blocks).
     184                 :            :  * Parent's group is preferred, if it doesn't satisfy these
     185                 :            :  * conditions we search cyclically through the rest. If none
     186                 :            :  * of the groups look good we just look for a group with more
     187                 :            :  * free inodes than average (starting at parent's group).
     188                 :            :  *
     189                 :            :  * Debt is incremented each time we allocate a directory and decremented
     190                 :            :  * when we allocate an inode, within 0--255.
     191                 :            :  */
     192                 :            : 
     193                 :          0 : static int find_group_orlov(struct super_block *sb, struct inode *parent)
     194                 :            : {
     195                 :          0 :         int parent_group = EXT3_I(parent)->i_block_group;
     196                 :            :         struct ext3_sb_info *sbi = EXT3_SB(sb);
     197                 :          0 :         int ngroups = sbi->s_groups_count;
     198                 :          0 :         int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
     199                 :            :         unsigned int freei, avefreei;
     200                 :            :         ext3_fsblk_t freeb, avefreeb;
     201                 :            :         unsigned int ndirs;
     202                 :            :         int max_dirs, min_inodes;
     203                 :            :         ext3_grpblk_t min_blocks;
     204                 :          0 :         int group = -1, i;
     205                 :            :         struct ext3_group_desc *desc;
     206                 :            : 
     207                 :          0 :         freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
     208                 :          0 :         avefreei = freei / ngroups;
     209                 :          0 :         freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
     210                 :          0 :         avefreeb = freeb / ngroups;
     211                 :          0 :         ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
     212                 :            : 
     213 [ #  # ][ #  # ]:          0 :         if ((parent == sb->s_root->d_inode) ||
     214                 :          0 :             (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
     215                 :            :                 int best_ndir = inodes_per_group;
     216                 :            :                 int best_group = -1;
     217                 :            : 
     218                 :          0 :                 get_random_bytes(&group, sizeof(group));
     219                 :          0 :                 parent_group = (unsigned)group % ngroups;
     220         [ #  # ]:          0 :                 for (i = 0; i < ngroups; i++) {
     221                 :          0 :                         group = (parent_group + i) % ngroups;
     222                 :          0 :                         desc = ext3_get_group_desc (sb, group, NULL);
     223 [ #  # ][ #  # ]:          0 :                         if (!desc || !desc->bg_free_inodes_count)
     224                 :          0 :                                 continue;
     225         [ #  # ]:          0 :                         if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
     226                 :          0 :                                 continue;
     227         [ #  # ]:          0 :                         if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
     228                 :          0 :                                 continue;
     229         [ #  # ]:          0 :                         if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
     230                 :          0 :                                 continue;
     231                 :          0 :                         best_group = group;
     232                 :            :                         best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
     233                 :            :                 }
     234         [ #  # ]:          0 :                 if (best_group >= 0)
     235                 :            :                         return best_group;
     236                 :            :                 goto fallback;
     237                 :            :         }
     238                 :            : 
     239                 :          0 :         max_dirs = ndirs / ngroups + inodes_per_group / 16;
     240                 :          0 :         min_inodes = avefreei - inodes_per_group / 4;
     241                 :          0 :         min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
     242                 :            : 
     243         [ #  # ]:          0 :         for (i = 0; i < ngroups; i++) {
     244                 :          0 :                 group = (parent_group + i) % ngroups;
     245                 :          0 :                 desc = ext3_get_group_desc (sb, group, NULL);
     246 [ #  # ][ #  # ]:          0 :                 if (!desc || !desc->bg_free_inodes_count)
     247                 :          0 :                         continue;
     248         [ #  # ]:          0 :                 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
     249                 :          0 :                         continue;
     250         [ #  # ]:          0 :                 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
     251                 :          0 :                         continue;
     252         [ #  # ]:          0 :                 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
     253                 :          0 :                         continue;
     254                 :          0 :                 return group;
     255                 :            :         }
     256                 :            : 
     257                 :            : fallback:
     258         [ #  # ]:          0 :         for (i = 0; i < ngroups; i++) {
     259                 :          0 :                 group = (parent_group + i) % ngroups;
     260                 :          0 :                 desc = ext3_get_group_desc (sb, group, NULL);
     261 [ #  # ][ #  # ]:          0 :                 if (!desc || !desc->bg_free_inodes_count)
     262                 :          0 :                         continue;
     263         [ #  # ]:          0 :                 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
     264                 :          0 :                         return group;
     265                 :            :         }
     266                 :            : 
     267         [ #  # ]:          0 :         if (avefreei) {
     268                 :            :                 /*
     269                 :            :                  * The free-inodes counter is approximate, and for really small
     270                 :            :                  * filesystems the above test can fail to find any blockgroups
     271                 :            :                  */
     272                 :            :                 avefreei = 0;
     273                 :            :                 goto fallback;
     274                 :            :         }
     275                 :            : 
     276                 :            :         return -1;
     277                 :            : }
     278                 :            : 
     279                 :          0 : static int find_group_other(struct super_block *sb, struct inode *parent)
     280                 :            : {
     281                 :          0 :         int parent_group = EXT3_I(parent)->i_block_group;
     282                 :          0 :         int ngroups = EXT3_SB(sb)->s_groups_count;
     283                 :            :         struct ext3_group_desc *desc;
     284                 :            :         int group, i;
     285                 :            : 
     286                 :            :         /*
     287                 :            :          * Try to place the inode in its parent directory
     288                 :            :          */
     289                 :            :         group = parent_group;
     290                 :          0 :         desc = ext3_get_group_desc (sb, group, NULL);
     291 [ #  # ][ #  # ]:          0 :         if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
                 [ #  # ]
     292                 :          0 :                         le16_to_cpu(desc->bg_free_blocks_count))
     293                 :            :                 return group;
     294                 :            : 
     295                 :            :         /*
     296                 :            :          * We're going to place this inode in a different blockgroup from its
     297                 :            :          * parent.  We want to cause files in a common directory to all land in
     298                 :            :          * the same blockgroup.  But we want files which are in a different
     299                 :            :          * directory which shares a blockgroup with our parent to land in a
     300                 :            :          * different blockgroup.
     301                 :            :          *
     302                 :            :          * So add our directory's i_ino into the starting point for the hash.
     303                 :            :          */
     304                 :          0 :         group = (group + parent->i_ino) % ngroups;
     305                 :            : 
     306                 :            :         /*
     307                 :            :          * Use a quadratic hash to find a group with a free inode and some free
     308                 :            :          * blocks.
     309                 :            :          */
     310         [ #  # ]:          0 :         for (i = 1; i < ngroups; i <<= 1) {
     311                 :          0 :                 group += i;
     312         [ #  # ]:          0 :                 if (group >= ngroups)
     313                 :          0 :                         group -= ngroups;
     314                 :          0 :                 desc = ext3_get_group_desc (sb, group, NULL);
     315 [ #  # ][ #  # ]:          0 :                 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
                 [ #  # ]
     316                 :          0 :                                 le16_to_cpu(desc->bg_free_blocks_count))
     317                 :            :                         return group;
     318                 :            :         }
     319                 :            : 
     320                 :            :         /*
     321                 :            :          * That failed: try linear search for a free inode, even if that group
     322                 :            :          * has no free blocks.
     323                 :            :          */
     324                 :            :         group = parent_group;
     325         [ #  # ]:          0 :         for (i = 0; i < ngroups; i++) {
     326         [ #  # ]:          0 :                 if (++group >= ngroups)
     327                 :            :                         group = 0;
     328                 :          0 :                 desc = ext3_get_group_desc (sb, group, NULL);
     329 [ #  # ][ #  # ]:          0 :                 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
     330                 :            :                         return group;
     331                 :            :         }
     332                 :            : 
     333                 :            :         return -1;
     334                 :            : }
     335                 :            : 
     336                 :            : /*
     337                 :            :  * There are two policies for allocating an inode.  If the new inode is
     338                 :            :  * a directory, then a forward search is made for a block group with both
     339                 :            :  * free space and a low directory-to-inode ratio; if that fails, then of
     340                 :            :  * the groups with above-average free space, that group with the fewest
     341                 :            :  * directories already is chosen.
     342                 :            :  *
     343                 :            :  * For other inodes, search forward from the parent directory's block
     344                 :            :  * group to find a free inode.
     345                 :            :  */
     346                 :          0 : struct inode *ext3_new_inode(handle_t *handle, struct inode * dir,
     347                 :            :                              const struct qstr *qstr, umode_t mode)
     348                 :            : {
     349                 :          0 :         struct super_block *sb;
     350                 :            :         struct buffer_head *bitmap_bh = NULL;
     351                 :            :         struct buffer_head *bh2;
     352                 :            :         int group;
     353                 :            :         unsigned long ino = 0;
     354                 :            :         struct inode * inode;
     355                 :            :         struct ext3_group_desc * gdp = NULL;
     356                 :            :         struct ext3_super_block * es;
     357                 :            :         struct ext3_inode_info *ei;
     358                 :          0 :         struct ext3_sb_info *sbi;
     359                 :            :         int err = 0;
     360                 :            :         struct inode *ret;
     361                 :            :         int i;
     362                 :            : 
     363                 :            :         /* Cannot create files in a deleted directory */
     364 [ #  # ][ #  # ]:          0 :         if (!dir || !dir->i_nlink)
     365                 :            :                 return ERR_PTR(-EPERM);
     366                 :            : 
     367                 :          0 :         sb = dir->i_sb;
     368                 :          0 :         trace_ext3_request_inode(dir, mode);
     369                 :          0 :         inode = new_inode(sb);
     370         [ #  # ]:          0 :         if (!inode)
     371                 :            :                 return ERR_PTR(-ENOMEM);
     372                 :            :         ei = EXT3_I(inode);
     373                 :            : 
     374                 :            :         sbi = EXT3_SB(sb);
     375                 :          0 :         es = sbi->s_es;
     376         [ #  # ]:          0 :         if (S_ISDIR(mode))
     377                 :          0 :                 group = find_group_orlov(sb, dir);
     378                 :            :         else
     379                 :          0 :                 group = find_group_other(sb, dir);
     380                 :            : 
     381                 :            :         err = -ENOSPC;
     382         [ #  # ]:          0 :         if (group == -1)
     383                 :            :                 goto out;
     384                 :            : 
     385         [ #  # ]:          0 :         for (i = 0; i < sbi->s_groups_count; i++) {
     386                 :            :                 err = -EIO;
     387                 :            : 
     388                 :          0 :                 gdp = ext3_get_group_desc(sb, group, &bh2);
     389         [ #  # ]:          0 :                 if (!gdp)
     390                 :            :                         goto fail;
     391                 :            : 
     392                 :            :                 brelse(bitmap_bh);
     393                 :          0 :                 bitmap_bh = read_inode_bitmap(sb, group);
     394         [ #  # ]:          0 :                 if (!bitmap_bh)
     395                 :            :                         goto fail;
     396                 :            : 
     397                 :            :                 ino = 0;
     398                 :            : 
     399                 :            : repeat_in_this_group:
     400                 :          0 :                 ino = ext3_find_next_zero_bit((unsigned long *)
     401                 :          0 :                                 bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
     402         [ #  # ]:          0 :                 if (ino < EXT3_INODES_PER_GROUP(sb)) {
     403                 :            : 
     404                 :            :                         BUFFER_TRACE(bitmap_bh, "get_write_access");
     405                 :          0 :                         err = ext3_journal_get_write_access(handle, bitmap_bh);
     406         [ #  # ]:          0 :                         if (err)
     407                 :            :                                 goto fail;
     408                 :            : 
     409         [ #  # ]:          0 :                         if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
     410                 :            :                                                 ino, bitmap_bh->b_data)) {
     411                 :            :                                 /* we won it */
     412                 :            :                                 BUFFER_TRACE(bitmap_bh,
     413                 :            :                                         "call ext3_journal_dirty_metadata");
     414                 :          0 :                                 err = ext3_journal_dirty_metadata(handle,
     415                 :            :                                                                 bitmap_bh);
     416         [ #  # ]:          0 :                                 if (err)
     417                 :            :                                         goto fail;
     418                 :            :                                 goto got;
     419                 :            :                         }
     420                 :            :                         /* we lost it */
     421                 :          0 :                         journal_release_buffer(handle, bitmap_bh);
     422                 :            : 
     423         [ #  # ]:          0 :                         if (++ino < EXT3_INODES_PER_GROUP(sb))
     424                 :            :                                 goto repeat_in_this_group;
     425                 :            :                 }
     426                 :            : 
     427                 :            :                 /*
     428                 :            :                  * This case is possible in concurrent environment.  It is very
     429                 :            :                  * rare.  We cannot repeat the find_group_xxx() call because
     430                 :            :                  * that will simply return the same blockgroup, because the
     431                 :            :                  * group descriptor metadata has not yet been updated.
     432                 :            :                  * So we just go onto the next blockgroup.
     433                 :            :                  */
     434         [ #  # ]:          0 :                 if (++group == sbi->s_groups_count)
     435                 :            :                         group = 0;
     436                 :            :         }
     437                 :            :         err = -ENOSPC;
     438                 :            :         goto out;
     439                 :            : 
     440                 :            : got:
     441                 :          0 :         ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
     442 [ #  # ][ #  # ]:          0 :         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
     443                 :          0 :                 ext3_error (sb, "ext3_new_inode",
     444                 :            :                             "reserved inode or inode > inodes count - "
     445                 :            :                             "block_group = %d, inode=%lu", group, ino);
     446                 :            :                 err = -EIO;
     447                 :          0 :                 goto fail;
     448                 :            :         }
     449                 :            : 
     450                 :            :         BUFFER_TRACE(bh2, "get_write_access");
     451                 :          0 :         err = ext3_journal_get_write_access(handle, bh2);
     452         [ #  # ]:          0 :         if (err) goto fail;
     453                 :          0 :         spin_lock(sb_bgl_lock(sbi, group));
     454                 :            :         le16_add_cpu(&gdp->bg_free_inodes_count, -1);
     455         [ #  # ]:          0 :         if (S_ISDIR(mode)) {
     456                 :            :                 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
     457                 :            :         }
     458                 :          0 :         spin_unlock(sb_bgl_lock(sbi, group));
     459                 :            :         BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
     460                 :          0 :         err = ext3_journal_dirty_metadata(handle, bh2);
     461         [ #  # ]:          0 :         if (err) goto fail;
     462                 :            : 
     463                 :          0 :         percpu_counter_dec(&sbi->s_freeinodes_counter);
     464         [ #  # ]:          0 :         if (S_ISDIR(mode))
     465                 :          0 :                 percpu_counter_inc(&sbi->s_dirs_counter);
     466                 :            : 
     467                 :            : 
     468         [ #  # ]:          0 :         if (test_opt(sb, GRPID)) {
     469                 :          0 :                 inode->i_mode = mode;
     470                 :          0 :                 inode->i_uid = current_fsuid();
     471                 :          0 :                 inode->i_gid = dir->i_gid;
     472                 :            :         } else
     473                 :          0 :                 inode_init_owner(inode, dir, mode);
     474                 :            : 
     475                 :          0 :         inode->i_ino = ino;
     476                 :            :         /* This is the optimal IO size (for stat), not the fs block size */
     477                 :          0 :         inode->i_blocks = 0;
     478                 :          0 :         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
     479                 :            : 
     480                 :          0 :         memset(ei->i_data, 0, sizeof(ei->i_data));
     481                 :          0 :         ei->i_dir_start_lookup = 0;
     482                 :          0 :         ei->i_disksize = 0;
     483                 :            : 
     484                 :          0 :         ei->i_flags =
     485                 :          0 :                 ext3_mask_flags(mode, EXT3_I(dir)->i_flags & EXT3_FL_INHERITED);
     486                 :            : #ifdef EXT3_FRAGMENTS
     487                 :            :         ei->i_faddr = 0;
     488                 :            :         ei->i_frag_no = 0;
     489                 :            :         ei->i_frag_size = 0;
     490                 :            : #endif
     491                 :          0 :         ei->i_file_acl = 0;
     492                 :          0 :         ei->i_dir_acl = 0;
     493                 :          0 :         ei->i_dtime = 0;
     494                 :          0 :         ei->i_block_alloc_info = NULL;
     495                 :          0 :         ei->i_block_group = group;
     496                 :            : 
     497                 :          0 :         ext3_set_inode_flags(inode);
     498 [ #  # ][ #  # ]:          0 :         if (IS_DIRSYNC(inode))
     499                 :          0 :                 handle->h_sync = 1;
     500         [ #  # ]:          0 :         if (insert_inode_locked(inode) < 0) {
     501                 :            :                 /*
     502                 :            :                  * Likely a bitmap corruption causing inode to be allocated
     503                 :            :                  * twice.
     504                 :            :                  */
     505                 :            :                 err = -EIO;
     506                 :            :                 goto fail;
     507                 :            :         }
     508                 :            :         spin_lock(&sbi->s_next_gen_lock);
     509                 :          0 :         inode->i_generation = sbi->s_next_generation++;
     510                 :            :         spin_unlock(&sbi->s_next_gen_lock);
     511                 :            : 
     512                 :          0 :         ei->i_state_flags = 0;
     513                 :            :         ext3_set_inode_state(inode, EXT3_STATE_NEW);
     514                 :            : 
     515                 :            :         /* See comment in ext3_iget for explanation */
     516 [ #  # ][ #  # ]:          0 :         if (ino >= EXT3_FIRST_INO(sb) + 1 &&
     517                 :          0 :             EXT3_INODE_SIZE(sb) > EXT3_GOOD_OLD_INODE_SIZE) {
     518                 :          0 :                 ei->i_extra_isize =
     519                 :            :                         sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE;
     520                 :            :         } else {
     521                 :          0 :                 ei->i_extra_isize = 0;
     522                 :            :         }
     523                 :            : 
     524                 :            :         ret = inode;
     525                 :          0 :         dquot_initialize(inode);
     526                 :          0 :         err = dquot_alloc_inode(inode);
     527         [ #  # ]:          0 :         if (err)
     528                 :            :                 goto fail_drop;
     529                 :            : 
     530                 :            :         err = ext3_init_acl(handle, inode, dir);
     531                 :            :         if (err)
     532                 :            :                 goto fail_free_drop;
     533                 :            : 
     534                 :            :         err = ext3_init_security(handle, inode, dir, qstr);
     535                 :            :         if (err)
     536                 :            :                 goto fail_free_drop;
     537                 :            : 
     538                 :          0 :         err = ext3_mark_inode_dirty(handle, inode);
     539         [ #  # ]:          0 :         if (err) {
     540         [ #  # ]:          0 :                 ext3_std_error(sb, err);
     541                 :            :                 goto fail_free_drop;
     542                 :            :         }
     543                 :            : 
     544                 :            :         ext3_debug("allocating inode %lu\n", inode->i_ino);
     545                 :            :         trace_ext3_allocate_inode(inode, dir, mode);
     546                 :            :         goto really_out;
     547                 :            : fail:
     548         [ #  # ]:          0 :         ext3_std_error(sb, err);
     549                 :            : out:
     550                 :          0 :         iput(inode);
     551                 :            :         ret = ERR_PTR(err);
     552                 :            : really_out:
     553                 :            :         brelse(bitmap_bh);
     554                 :          0 :         return ret;
     555                 :            : 
     556                 :            : fail_free_drop:
     557                 :          0 :         dquot_free_inode(inode);
     558                 :            : 
     559                 :            : fail_drop:
     560                 :          0 :         dquot_drop(inode);
     561                 :          0 :         inode->i_flags |= S_NOQUOTA;
     562                 :          0 :         clear_nlink(inode);
     563                 :          0 :         unlock_new_inode(inode);
     564                 :          0 :         iput(inode);
     565                 :            :         brelse(bitmap_bh);
     566                 :          0 :         return ERR_PTR(err);
     567                 :            : }
     568                 :            : 
     569                 :            : /* Verify that we are loading a valid orphan from disk */
     570                 :          0 : struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
     571                 :            : {
     572                 :          0 :         unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
     573                 :            :         unsigned long block_group;
     574                 :            :         int bit;
     575                 :            :         struct buffer_head *bitmap_bh;
     576                 :            :         struct inode *inode = NULL;
     577                 :            :         long err = -EIO;
     578                 :            : 
     579                 :            :         /* Error cases - e2fsck has already cleaned up for us */
     580         [ #  # ]:          0 :         if (ino > max_ino) {
     581                 :          0 :                 ext3_warning(sb, __func__,
     582                 :            :                              "bad orphan ino %lu!  e2fsck was run?", ino);
     583                 :          0 :                 goto error;
     584                 :            :         }
     585                 :            : 
     586                 :          0 :         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
     587                 :          0 :         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
     588                 :          0 :         bitmap_bh = read_inode_bitmap(sb, block_group);
     589         [ #  # ]:          0 :         if (!bitmap_bh) {
     590                 :          0 :                 ext3_warning(sb, __func__,
     591                 :            :                              "inode bitmap error for orphan %lu", ino);
     592                 :          0 :                 goto error;
     593                 :            :         }
     594                 :            : 
     595                 :            :         /* Having the inode bit set should be a 100% indicator that this
     596                 :            :          * is a valid orphan (no e2fsck run on fs).  Orphans also include
     597                 :            :          * inodes that were being truncated, so we can't check i_nlink==0.
     598                 :            :          */
     599         [ #  # ]:          0 :         if (!ext3_test_bit(bit, bitmap_bh->b_data))
     600                 :            :                 goto bad_orphan;
     601                 :            : 
     602                 :          0 :         inode = ext3_iget(sb, ino);
     603         [ #  # ]:          0 :         if (IS_ERR(inode))
     604                 :            :                 goto iget_failed;
     605                 :            : 
     606                 :            :         /*
     607                 :            :          * If the orphans has i_nlinks > 0 then it should be able to be
     608                 :            :          * truncated, otherwise it won't be removed from the orphan list
     609                 :            :          * during processing and an infinite loop will result.
     610                 :            :          */
     611 [ #  # ][ #  # ]:          0 :         if (inode->i_nlink && !ext3_can_truncate(inode))
     612                 :            :                 goto bad_orphan;
     613                 :            : 
     614         [ #  # ]:          0 :         if (NEXT_ORPHAN(inode) > max_ino)
     615                 :            :                 goto bad_orphan;
     616                 :            :         brelse(bitmap_bh);
     617                 :          0 :         return inode;
     618                 :            : 
     619                 :            : iget_failed:
     620                 :            :         err = PTR_ERR(inode);
     621                 :            :         inode = NULL;
     622                 :            : bad_orphan:
     623                 :          0 :         ext3_warning(sb, __func__,
     624                 :            :                      "bad orphan inode %lu!  e2fsck was run?", ino);
     625                 :          0 :         printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
     626                 :            :                bit, (unsigned long long)bitmap_bh->b_blocknr,
     627                 :          0 :                ext3_test_bit(bit, bitmap_bh->b_data));
     628                 :          0 :         printk(KERN_NOTICE "inode=%p\n", inode);
     629         [ #  # ]:          0 :         if (inode) {
     630                 :          0 :                 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
     631                 :            :                        is_bad_inode(inode));
     632                 :          0 :                 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
     633                 :            :                        NEXT_ORPHAN(inode));
     634                 :          0 :                 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
     635                 :          0 :                 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
     636                 :            :                 /* Avoid freeing blocks if we got a bad deleted inode */
     637         [ #  # ]:          0 :                 if (inode->i_nlink == 0)
     638                 :          0 :                         inode->i_blocks = 0;
     639                 :          0 :                 iput(inode);
     640                 :            :         }
     641                 :            :         brelse(bitmap_bh);
     642                 :            : error:
     643                 :          0 :         return ERR_PTR(err);
     644                 :            : }
     645                 :            : 
     646                 :          0 : unsigned long ext3_count_free_inodes (struct super_block * sb)
     647                 :            : {
     648                 :            :         unsigned long desc_count;
     649                 :            :         struct ext3_group_desc *gdp;
     650                 :            :         int i;
     651                 :            : #ifdef EXT3FS_DEBUG
     652                 :            :         struct ext3_super_block *es;
     653                 :            :         unsigned long bitmap_count, x;
     654                 :            :         struct buffer_head *bitmap_bh = NULL;
     655                 :            : 
     656                 :            :         es = EXT3_SB(sb)->s_es;
     657                 :            :         desc_count = 0;
     658                 :            :         bitmap_count = 0;
     659                 :            :         gdp = NULL;
     660                 :            :         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
     661                 :            :                 gdp = ext3_get_group_desc (sb, i, NULL);
     662                 :            :                 if (!gdp)
     663                 :            :                         continue;
     664                 :            :                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
     665                 :            :                 brelse(bitmap_bh);
     666                 :            :                 bitmap_bh = read_inode_bitmap(sb, i);
     667                 :            :                 if (!bitmap_bh)
     668                 :            :                         continue;
     669                 :            : 
     670                 :            :                 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
     671                 :            :                 printk("group %d: stored = %d, counted = %lu\n",
     672                 :            :                         i, le16_to_cpu(gdp->bg_free_inodes_count), x);
     673                 :            :                 bitmap_count += x;
     674                 :            :         }
     675                 :            :         brelse(bitmap_bh);
     676                 :            :         printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
     677                 :            :                 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
     678                 :            :         return desc_count;
     679                 :            : #else
     680                 :            :         desc_count = 0;
     681         [ #  # ]:          0 :         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
     682                 :          0 :                 gdp = ext3_get_group_desc (sb, i, NULL);
     683         [ #  # ]:          0 :                 if (!gdp)
     684                 :          0 :                         continue;
     685                 :          0 :                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
     686                 :          0 :                 cond_resched();
     687                 :            :         }
     688                 :          0 :         return desc_count;
     689                 :            : #endif
     690                 :            : }
     691                 :            : 
     692                 :            : /* Called at mount-time, super-block is locked */
     693                 :          0 : unsigned long ext3_count_dirs (struct super_block * sb)
     694                 :            : {
     695                 :            :         unsigned long count = 0;
     696                 :            :         int i;
     697                 :            : 
     698         [ #  # ]:          0 :         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
     699                 :          0 :                 struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
     700         [ #  # ]:          0 :                 if (!gdp)
     701                 :          0 :                         continue;
     702                 :          0 :                 count += le16_to_cpu(gdp->bg_used_dirs_count);
     703                 :            :         }
     704                 :          0 :         return count;
     705                 :            : }
     706                 :            : 

Generated by: LCOV version 1.9