Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/ialloc.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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/time.h> |
| 16 | #include <linux/fs.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 17 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | #include <linux/stat.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/quotaops.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/random.h> |
| 23 | #include <linux/bitops.h> |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 25 | #include <asm/byteorder.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 26 | #include "ext4.h" |
| 27 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 28 | #include "xattr.h" |
| 29 | #include "acl.h" |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 30 | #include "group.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * ialloc.c contains the inodes allocation and deallocation routines |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * The free inodes are managed by bitmaps. A file system contains several |
| 38 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
| 39 | * block for inodes, N blocks for the inode table and data blocks. |
| 40 | * |
| 41 | * The file system contains group descriptors which are located after the |
| 42 | * super block. Each descriptor contains the number of the bitmap block and |
| 43 | * the free blocks count in the block. |
| 44 | */ |
| 45 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 46 | /* |
| 47 | * To avoid calling the atomic setbit hundreds or thousands of times, we only |
| 48 | * need to use it within a single byte (to ensure we get endianness right). |
| 49 | * We can use memset for the rest of the bitmap as there are no other users. |
| 50 | */ |
| 51 | void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | if (start_bit >= end_bit) |
| 56 | return; |
| 57 | |
| 58 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); |
| 59 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) |
| 60 | ext4_set_bit(i, bitmap); |
| 61 | if (i < end_bit) |
| 62 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); |
| 63 | } |
| 64 | |
| 65 | /* Initializes an uninitialized inode bitmap */ |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 66 | unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, |
| 67 | ext4_group_t block_group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 68 | struct ext4_group_desc *gdp) |
| 69 | { |
| 70 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 71 | |
| 72 | J_ASSERT_BH(bh, buffer_locked(bh)); |
| 73 | |
| 74 | /* If checksum is bad mark all blocks and inodes use to prevent |
| 75 | * allocation, essentially implementing a per-group read-only flag. */ |
| 76 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 77 | ext4_error(sb, __func__, "Checksum bad for group %lu\n", |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 78 | block_group); |
| 79 | gdp->bg_free_blocks_count = 0; |
| 80 | gdp->bg_free_inodes_count = 0; |
| 81 | gdp->bg_itable_unused = 0; |
| 82 | memset(bh->b_data, 0xff, sb->s_blocksize); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); |
| 87 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), |
| 88 | bh->b_data); |
| 89 | |
| 90 | return EXT4_INODES_PER_GROUP(sb); |
| 91 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Read the inode allocation bitmap for a given block_group, reading |
| 95 | * into the specified slot in the superblock's bitmap cache. |
| 96 | * |
| 97 | * Return buffer_head of bitmap on success or NULL. |
| 98 | */ |
| 99 | static struct buffer_head * |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 100 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 101 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 102 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 103 | struct buffer_head *bh = NULL; |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 104 | ext4_fsblk_t bitmap_blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 106 | desc = ext4_get_group_desc(sb, block_group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 107 | if (!desc) |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 108 | return NULL; |
| 109 | bitmap_blk = ext4_inode_bitmap(sb, desc); |
| 110 | bh = sb_getblk(sb, bitmap_blk); |
| 111 | if (unlikely(!bh)) { |
| 112 | ext4_error(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | "Cannot read inode bitmap - " |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 114 | "block_group = %lu, inode_bitmap = %llu", |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 115 | block_group, bitmap_blk); |
| 116 | return NULL; |
| 117 | } |
| 118 | if (bh_uptodate_or_lock(bh)) |
| 119 | return bh; |
| 120 | |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 121 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 122 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 123 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
| 124 | set_buffer_uptodate(bh); |
| 125 | unlock_buffer(bh); |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 126 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 127 | return bh; |
| 128 | } |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 129 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 130 | if (bh_submit_read(bh) < 0) { |
| 131 | put_bh(bh); |
| 132 | ext4_error(sb, __func__, |
| 133 | "Cannot read inode bitmap - " |
| 134 | "block_group = %lu, inode_bitmap = %llu", |
| 135 | block_group, bitmap_blk); |
| 136 | return NULL; |
| 137 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 138 | return bh; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * NOTE! When we get the inode, we're the only people |
| 143 | * that have access to it, and as such there are no |
| 144 | * race conditions we have to worry about. The inode |
| 145 | * is not on the hash-lists, and it cannot be reached |
| 146 | * through the filesystem because the directory entry |
| 147 | * has been deleted earlier. |
| 148 | * |
| 149 | * HOWEVER: we must make sure that we get no aliases, |
| 150 | * which means that we have to call "clear_inode()" |
| 151 | * _before_ we mark the inode not in use in the inode |
| 152 | * bitmaps. Otherwise a newly created file might use |
| 153 | * the same inode number (not actually the same pointer |
| 154 | * though), and then we'd have two inodes sharing the |
| 155 | * same inode number and space on the harddisk. |
| 156 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 157 | void ext4_free_inode (handle_t *handle, struct inode * inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 158 | { |
| 159 | struct super_block * sb = inode->i_sb; |
| 160 | int is_directory; |
| 161 | unsigned long ino; |
| 162 | struct buffer_head *bitmap_bh = NULL; |
| 163 | struct buffer_head *bh2; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 164 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 165 | unsigned long bit; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | struct ext4_group_desc * gdp; |
| 167 | struct ext4_super_block * es; |
| 168 | struct ext4_sb_info *sbi; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 169 | int fatal = 0, err; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 170 | ext4_group_t flex_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | |
| 172 | if (atomic_read(&inode->i_count) > 1) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 173 | printk ("ext4_free_inode: inode has count=%d\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | atomic_read(&inode->i_count)); |
| 175 | return; |
| 176 | } |
| 177 | if (inode->i_nlink) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 178 | printk ("ext4_free_inode: inode has nlink=%d\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 179 | inode->i_nlink); |
| 180 | return; |
| 181 | } |
| 182 | if (!sb) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 183 | printk("ext4_free_inode: inode on nonexistent device\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 186 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | |
| 188 | ino = inode->i_ino; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 189 | ext4_debug ("freeing inode %lu\n", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Note: we must free any quota before locking the superblock, |
| 193 | * as writing the quota to disk may need the lock as well. |
| 194 | */ |
| 195 | DQUOT_INIT(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 196 | ext4_xattr_delete_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 197 | DQUOT_FREE_INODE(inode); |
| 198 | DQUOT_DROP(inode); |
| 199 | |
| 200 | is_directory = S_ISDIR(inode->i_mode); |
| 201 | |
| 202 | /* Do this BEFORE marking the inode not in use or returning an error */ |
| 203 | clear_inode (inode); |
| 204 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 205 | es = EXT4_SB(sb)->s_es; |
| 206 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { |
| 207 | ext4_error (sb, "ext4_free_inode", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 208 | "reserved or nonexistent inode %lu", ino); |
| 209 | goto error_return; |
| 210 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 211 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 212 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 213 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 214 | if (!bitmap_bh) |
| 215 | goto error_return; |
| 216 | |
| 217 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 218 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | if (fatal) |
| 220 | goto error_return; |
| 221 | |
| 222 | /* Ok, now we can actually update the inode bitmaps.. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 223 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 224 | bit, bitmap_bh->b_data)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 225 | ext4_error (sb, "ext4_free_inode", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 226 | "bit already cleared for inode %lu", ino); |
| 227 | else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 228 | gdp = ext4_get_group_desc (sb, block_group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 229 | |
| 230 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 231 | fatal = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 232 | if (fatal) goto error_return; |
| 233 | |
| 234 | if (gdp) { |
| 235 | spin_lock(sb_bgl_lock(sbi, block_group)); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 236 | le16_add_cpu(&gdp->bg_free_inodes_count, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 237 | if (is_directory) |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 238 | le16_add_cpu(&gdp->bg_used_dirs_count, -1); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 239 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
| 240 | block_group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 241 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
| 242 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
| 243 | if (is_directory) |
| 244 | percpu_counter_dec(&sbi->s_dirs_counter); |
| 245 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 246 | if (sbi->s_log_groups_per_flex) { |
| 247 | flex_group = ext4_flex_group(sbi, block_group); |
| 248 | spin_lock(sb_bgl_lock(sbi, flex_group)); |
| 249 | sbi->s_flex_groups[flex_group].free_inodes++; |
| 250 | spin_unlock(sb_bgl_lock(sbi, flex_group)); |
| 251 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 252 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 253 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
| 254 | err = ext4_journal_dirty_metadata(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 255 | if (!fatal) fatal = err; |
| 256 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 257 | BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata"); |
| 258 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 259 | if (!fatal) |
| 260 | fatal = err; |
| 261 | sb->s_dirt = 1; |
| 262 | error_return: |
| 263 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 264 | ext4_std_error(sb, fatal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * There are two policies for allocating an inode. If the new inode is |
| 269 | * a directory, then a forward search is made for a block group with both |
| 270 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 271 | * the groups with above-average free space, that group with the fewest |
| 272 | * directories already is chosen. |
| 273 | * |
| 274 | * For other inodes, search forward from the parent directory\'s block |
| 275 | * group to find a free inode. |
| 276 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 277 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
| 278 | ext4_group_t *best_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 279 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 280 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 281 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 282 | struct ext4_group_desc *desc, *best_desc = NULL; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 283 | ext4_group_t group; |
| 284 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 286 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 287 | avefreei = freei / ngroups; |
| 288 | |
| 289 | for (group = 0; group < ngroups; group++) { |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 290 | desc = ext4_get_group_desc (sb, group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 291 | if (!desc || !desc->bg_free_inodes_count) |
| 292 | continue; |
| 293 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 294 | continue; |
| 295 | if (!best_desc || |
| 296 | (le16_to_cpu(desc->bg_free_blocks_count) > |
| 297 | le16_to_cpu(best_desc->bg_free_blocks_count))) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 298 | *best_group = group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 299 | best_desc = desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 300 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 303 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 306 | #define free_block_ratio 10 |
| 307 | |
| 308 | static int find_group_flex(struct super_block *sb, struct inode *parent, |
| 309 | ext4_group_t *best_group) |
| 310 | { |
| 311 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 312 | struct ext4_group_desc *desc; |
| 313 | struct buffer_head *bh; |
| 314 | struct flex_groups *flex_group = sbi->s_flex_groups; |
| 315 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 316 | ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); |
| 317 | ext4_group_t ngroups = sbi->s_groups_count; |
| 318 | int flex_size = ext4_flex_bg_size(sbi); |
| 319 | ext4_group_t best_flex = parent_fbg_group; |
| 320 | int blocks_per_flex = sbi->s_blocks_per_group * flex_size; |
| 321 | int flexbg_free_blocks; |
| 322 | int flex_freeb_ratio; |
| 323 | ext4_group_t n_fbg_groups; |
| 324 | ext4_group_t i; |
| 325 | |
| 326 | n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> |
| 327 | sbi->s_log_groups_per_flex; |
| 328 | |
| 329 | find_close_to_parent: |
| 330 | flexbg_free_blocks = flex_group[best_flex].free_blocks; |
| 331 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
| 332 | if (flex_group[best_flex].free_inodes && |
| 333 | flex_freeb_ratio > free_block_ratio) |
| 334 | goto found_flexbg; |
| 335 | |
| 336 | if (best_flex && best_flex == parent_fbg_group) { |
| 337 | best_flex--; |
| 338 | goto find_close_to_parent; |
| 339 | } |
| 340 | |
| 341 | for (i = 0; i < n_fbg_groups; i++) { |
| 342 | if (i == parent_fbg_group || i == parent_fbg_group - 1) |
| 343 | continue; |
| 344 | |
| 345 | flexbg_free_blocks = flex_group[i].free_blocks; |
| 346 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
| 347 | |
| 348 | if (flex_freeb_ratio > free_block_ratio && |
| 349 | flex_group[i].free_inodes) { |
| 350 | best_flex = i; |
| 351 | goto found_flexbg; |
| 352 | } |
| 353 | |
Eric Sandeen | c001077 | 2008-08-19 22:19:50 -0400 | [diff] [blame] | 354 | if (flex_group[best_flex].free_inodes == 0 || |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 355 | (flex_group[i].free_blocks > |
| 356 | flex_group[best_flex].free_blocks && |
| 357 | flex_group[i].free_inodes)) |
| 358 | best_flex = i; |
| 359 | } |
| 360 | |
| 361 | if (!flex_group[best_flex].free_inodes || |
| 362 | !flex_group[best_flex].free_blocks) |
| 363 | return -1; |
| 364 | |
| 365 | found_flexbg: |
| 366 | for (i = best_flex * flex_size; i < ngroups && |
| 367 | i < (best_flex + 1) * flex_size; i++) { |
| 368 | desc = ext4_get_group_desc(sb, i, &bh); |
| 369 | if (le16_to_cpu(desc->bg_free_inodes_count)) { |
| 370 | *best_group = i; |
| 371 | goto out; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return -1; |
| 376 | out: |
| 377 | return 0; |
| 378 | } |
| 379 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Orlov's allocator for directories. |
| 382 | * |
| 383 | * We always try to spread first-level directories. |
| 384 | * |
| 385 | * If there are blockgroups with both free inodes and free blocks counts |
| 386 | * not worse than average we return one with smallest directory count. |
| 387 | * Otherwise we simply return a random group. |
| 388 | * |
| 389 | * For the rest rules look so: |
| 390 | * |
| 391 | * It's OK to put directory into a group unless |
| 392 | * it has too many directories already (max_dirs) or |
| 393 | * it has too few free inodes left (min_inodes) or |
| 394 | * it has too few free blocks left (min_blocks) or |
| 395 | * it's already running too large debt (max_debt). |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 396 | * Parent's group is preferred, if it doesn't satisfy these |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 397 | * conditions we search cyclically through the rest. If none |
| 398 | * of the groups look good we just look for a group with more |
| 399 | * free inodes than average (starting at parent's group). |
| 400 | * |
| 401 | * Debt is incremented each time we allocate a directory and decremented |
| 402 | * when we allocate an inode, within 0--255. |
| 403 | */ |
| 404 | |
| 405 | #define INODE_COST 64 |
| 406 | #define BLOCK_COST 256 |
| 407 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 408 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
| 409 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 410 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 411 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 412 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 413 | struct ext4_super_block *es = sbi->s_es; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 414 | ext4_group_t ngroups = sbi->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 415 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 416 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 417 | ext4_fsblk_t freeb, avefreeb; |
| 418 | ext4_fsblk_t blocks_per_dir; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 419 | unsigned int ndirs; |
| 420 | int max_debt, max_dirs, min_inodes; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 421 | ext4_grpblk_t min_blocks; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 422 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 423 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 424 | |
| 425 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); |
| 426 | avefreei = freei / ngroups; |
| 427 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 428 | avefreeb = freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 429 | do_div(avefreeb, ngroups); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 430 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
| 431 | |
| 432 | if ((parent == sb->s_root->d_inode) || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 433 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 434 | int best_ndir = inodes_per_group; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 435 | ext4_group_t grp; |
| 436 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 437 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 438 | get_random_bytes(&grp, sizeof(grp)); |
| 439 | parent_group = (unsigned)grp % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 440 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 441 | grp = (parent_group + i) % ngroups; |
| 442 | desc = ext4_get_group_desc(sb, grp, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 443 | if (!desc || !desc->bg_free_inodes_count) |
| 444 | continue; |
| 445 | if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) |
| 446 | continue; |
| 447 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 448 | continue; |
| 449 | if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) |
| 450 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 451 | *group = grp; |
| 452 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 453 | best_ndir = le16_to_cpu(desc->bg_used_dirs_count); |
| 454 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 455 | if (ret == 0) |
| 456 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | goto fallback; |
| 458 | } |
| 459 | |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 460 | blocks_per_dir = ext4_blocks_count(es) - freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 461 | do_div(blocks_per_dir, ndirs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 462 | |
| 463 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
| 464 | min_inodes = avefreei - inodes_per_group / 4; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 465 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 466 | |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 467 | max_debt = EXT4_BLOCKS_PER_GROUP(sb); |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 468 | max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 469 | if (max_debt * INODE_COST > inodes_per_group) |
| 470 | max_debt = inodes_per_group / INODE_COST; |
| 471 | if (max_debt > 255) |
| 472 | max_debt = 255; |
| 473 | if (max_debt == 0) |
| 474 | max_debt = 1; |
| 475 | |
| 476 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 477 | *group = (parent_group + i) % ngroups; |
| 478 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 479 | if (!desc || !desc->bg_free_inodes_count) |
| 480 | continue; |
| 481 | if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) |
| 482 | continue; |
| 483 | if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) |
| 484 | continue; |
| 485 | if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) |
| 486 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 487 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | fallback: |
| 491 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 492 | *group = (parent_group + i) % ngroups; |
| 493 | desc = ext4_get_group_desc(sb, *group, NULL); |
| 494 | if (desc && desc->bg_free_inodes_count && |
| 495 | le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) |
| 496 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if (avefreei) { |
| 500 | /* |
| 501 | * The free-inodes counter is approximate, and for really small |
| 502 | * filesystems the above test can fail to find any blockgroups |
| 503 | */ |
| 504 | avefreei = 0; |
| 505 | goto fallback; |
| 506 | } |
| 507 | |
| 508 | return -1; |
| 509 | } |
| 510 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 511 | static int find_group_other(struct super_block *sb, struct inode *parent, |
| 512 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 513 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 514 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 515 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 516 | struct ext4_group_desc *desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 517 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Try to place the inode in its parent directory |
| 521 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 522 | *group = parent_group; |
| 523 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 525 | le16_to_cpu(desc->bg_free_blocks_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 526 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 527 | |
| 528 | /* |
| 529 | * We're going to place this inode in a different blockgroup from its |
| 530 | * parent. We want to cause files in a common directory to all land in |
| 531 | * the same blockgroup. But we want files which are in a different |
| 532 | * directory which shares a blockgroup with our parent to land in a |
| 533 | * different blockgroup. |
| 534 | * |
| 535 | * So add our directory's i_ino into the starting point for the hash. |
| 536 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 537 | *group = (*group + parent->i_ino) % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * Use a quadratic hash to find a group with a free inode and some free |
| 541 | * blocks. |
| 542 | */ |
| 543 | for (i = 1; i < ngroups; i <<= 1) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 544 | *group += i; |
| 545 | if (*group >= ngroups) |
| 546 | *group -= ngroups; |
| 547 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 549 | le16_to_cpu(desc->bg_free_blocks_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 550 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
| 554 | * That failed: try linear search for a free inode, even if that group |
| 555 | * has no free blocks. |
| 556 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 557 | *group = parent_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 558 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 559 | if (++*group >= ngroups) |
| 560 | *group = 0; |
| 561 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 562 | if (desc && le16_to_cpu(desc->bg_free_inodes_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 563 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * There are two policies for allocating an inode. If the new inode is |
| 571 | * a directory, then a forward search is made for a block group with both |
| 572 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 573 | * the groups with above-average free space, that group with the fewest |
| 574 | * directories already is chosen. |
| 575 | * |
| 576 | * For other inodes, search forward from the parent directory's block |
| 577 | * group to find a free inode. |
| 578 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 579 | struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 580 | { |
| 581 | struct super_block *sb; |
| 582 | struct buffer_head *bitmap_bh = NULL; |
| 583 | struct buffer_head *bh2; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 584 | ext4_group_t group = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 585 | unsigned long ino = 0; |
| 586 | struct inode * inode; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 587 | struct ext4_group_desc * gdp = NULL; |
| 588 | struct ext4_super_block * es; |
| 589 | struct ext4_inode_info *ei; |
| 590 | struct ext4_sb_info *sbi; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 591 | int ret2, err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 592 | struct inode *ret; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 593 | ext4_group_t i; |
| 594 | int free = 0; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 595 | ext4_group_t flex_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 596 | |
| 597 | /* Cannot create files in a deleted directory */ |
| 598 | if (!dir || !dir->i_nlink) |
| 599 | return ERR_PTR(-EPERM); |
| 600 | |
| 601 | sb = dir->i_sb; |
| 602 | inode = new_inode(sb); |
| 603 | if (!inode) |
| 604 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 605 | ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 606 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 607 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 608 | es = sbi->s_es; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 609 | |
| 610 | if (sbi->s_log_groups_per_flex) { |
| 611 | ret2 = find_group_flex(sb, dir, &group); |
| 612 | goto got_group; |
| 613 | } |
| 614 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 615 | if (S_ISDIR(mode)) { |
| 616 | if (test_opt (sb, OLDALLOC)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 617 | ret2 = find_group_dir(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 618 | else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 619 | ret2 = find_group_orlov(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 620 | } else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 621 | ret2 = find_group_other(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 622 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 623 | got_group: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 624 | err = -ENOSPC; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 625 | if (ret2 == -1) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 626 | goto out; |
| 627 | |
| 628 | for (i = 0; i < sbi->s_groups_count; i++) { |
| 629 | err = -EIO; |
| 630 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 631 | gdp = ext4_get_group_desc(sb, group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 632 | if (!gdp) |
| 633 | goto fail; |
| 634 | |
| 635 | brelse(bitmap_bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 636 | bitmap_bh = ext4_read_inode_bitmap(sb, group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 637 | if (!bitmap_bh) |
| 638 | goto fail; |
| 639 | |
| 640 | ino = 0; |
| 641 | |
| 642 | repeat_in_this_group: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 643 | ino = ext4_find_next_zero_bit((unsigned long *) |
| 644 | bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino); |
| 645 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 646 | |
| 647 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 648 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 649 | if (err) |
| 650 | goto fail; |
| 651 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 652 | if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 653 | ino, bitmap_bh->b_data)) { |
| 654 | /* we won it */ |
| 655 | BUFFER_TRACE(bitmap_bh, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 656 | "call ext4_journal_dirty_metadata"); |
| 657 | err = ext4_journal_dirty_metadata(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | bitmap_bh); |
| 659 | if (err) |
| 660 | goto fail; |
| 661 | goto got; |
| 662 | } |
| 663 | /* we lost it */ |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 664 | jbd2_journal_release_buffer(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 665 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 666 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 667 | goto repeat_in_this_group; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * This case is possible in concurrent environment. It is very |
| 672 | * rare. We cannot repeat the find_group_xxx() call because |
| 673 | * that will simply return the same blockgroup, because the |
| 674 | * group descriptor metadata has not yet been updated. |
| 675 | * So we just go onto the next blockgroup. |
| 676 | */ |
| 677 | if (++group == sbi->s_groups_count) |
| 678 | group = 0; |
| 679 | } |
| 680 | err = -ENOSPC; |
| 681 | goto out; |
| 682 | |
| 683 | got: |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 684 | ino++; |
| 685 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || |
| 686 | ino > EXT4_INODES_PER_GROUP(sb)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 687 | ext4_error(sb, __func__, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 688 | "reserved inode or inode > inodes count - " |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 689 | "block_group = %lu, inode=%lu", group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 690 | ino + group * EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 691 | err = -EIO; |
| 692 | goto fail; |
| 693 | } |
| 694 | |
| 695 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 696 | err = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 697 | if (err) goto fail; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 698 | |
| 699 | /* We may have to initialize the block bitmap if it isn't already */ |
| 700 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && |
| 701 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 702 | struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 703 | |
| 704 | BUFFER_TRACE(block_bh, "get block bitmap access"); |
| 705 | err = ext4_journal_get_write_access(handle, block_bh); |
| 706 | if (err) { |
| 707 | brelse(block_bh); |
| 708 | goto fail; |
| 709 | } |
| 710 | |
| 711 | free = 0; |
| 712 | spin_lock(sb_bgl_lock(sbi, group)); |
| 713 | /* recheck and clear flag under lock if we still need to */ |
| 714 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 715 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
| 716 | free = ext4_free_blocks_after_init(sb, group, gdp); |
| 717 | gdp->bg_free_blocks_count = cpu_to_le16(free); |
| 718 | } |
| 719 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 720 | |
| 721 | /* Don't need to dirty bitmap block if we didn't change it */ |
| 722 | if (free) { |
| 723 | BUFFER_TRACE(block_bh, "dirty block bitmap"); |
| 724 | err = ext4_journal_dirty_metadata(handle, block_bh); |
| 725 | } |
| 726 | |
| 727 | brelse(block_bh); |
| 728 | if (err) |
| 729 | goto fail; |
| 730 | } |
| 731 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 732 | spin_lock(sb_bgl_lock(sbi, group)); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 733 | /* If we didn't allocate from within the initialized part of the inode |
| 734 | * table then we need to initialize up to this inode. */ |
| 735 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { |
| 736 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 737 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); |
| 738 | |
| 739 | /* When marking the block group with |
| 740 | * ~EXT4_BG_INODE_UNINIT we don't want to depend |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 741 | * on the value of bg_itable_unused even though |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 742 | * mke2fs could have initialized the same for us. |
| 743 | * Instead we calculated the value below |
| 744 | */ |
| 745 | |
| 746 | free = 0; |
| 747 | } else { |
| 748 | free = EXT4_INODES_PER_GROUP(sb) - |
| 749 | le16_to_cpu(gdp->bg_itable_unused); |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Check the relative inode number against the last used |
| 754 | * relative inode number in this group. if it is greater |
| 755 | * we need to update the bg_itable_unused count |
| 756 | * |
| 757 | */ |
| 758 | if (ino > free) |
| 759 | gdp->bg_itable_unused = |
| 760 | cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino); |
| 761 | } |
| 762 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 763 | le16_add_cpu(&gdp->bg_free_inodes_count, -1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 764 | if (S_ISDIR(mode)) { |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 765 | le16_add_cpu(&gdp->bg_used_dirs_count, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 766 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 767 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 768 | spin_unlock(sb_bgl_lock(sbi, group)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 769 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
| 770 | err = ext4_journal_dirty_metadata(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 771 | if (err) goto fail; |
| 772 | |
| 773 | percpu_counter_dec(&sbi->s_freeinodes_counter); |
| 774 | if (S_ISDIR(mode)) |
| 775 | percpu_counter_inc(&sbi->s_dirs_counter); |
| 776 | sb->s_dirt = 1; |
| 777 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 778 | if (sbi->s_log_groups_per_flex) { |
| 779 | flex_group = ext4_flex_group(sbi, group); |
| 780 | spin_lock(sb_bgl_lock(sbi, flex_group)); |
| 781 | sbi->s_flex_groups[flex_group].free_inodes--; |
| 782 | spin_unlock(sb_bgl_lock(sbi, flex_group)); |
| 783 | } |
| 784 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 785 | inode->i_uid = current->fsuid; |
| 786 | if (test_opt (sb, GRPID)) |
| 787 | inode->i_gid = dir->i_gid; |
| 788 | else if (dir->i_mode & S_ISGID) { |
| 789 | inode->i_gid = dir->i_gid; |
| 790 | if (S_ISDIR(mode)) |
| 791 | mode |= S_ISGID; |
| 792 | } else |
| 793 | inode->i_gid = current->fsgid; |
| 794 | inode->i_mode = mode; |
| 795 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 796 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 797 | /* This is the optimal IO size (for stat), not the fs block size */ |
| 798 | inode->i_blocks = 0; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 799 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 800 | ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 801 | |
| 802 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
| 803 | ei->i_dir_start_lookup = 0; |
| 804 | ei->i_disksize = 0; |
| 805 | |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 806 | /* |
| 807 | * Don't inherit extent flag from directory. We set extent flag on |
| 808 | * newly created directory and file only if -o extent mount option is |
| 809 | * specified |
| 810 | */ |
| 811 | ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 812 | if (S_ISLNK(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 813 | ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 814 | /* dirsync only applies to directories */ |
| 815 | if (!S_ISDIR(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 816 | ei->i_flags &= ~EXT4_DIRSYNC_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 817 | ei->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 818 | ei->i_dtime = 0; |
| 819 | ei->i_block_alloc_info = NULL; |
| 820 | ei->i_block_group = group; |
| 821 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 822 | ext4_set_inode_flags(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 823 | if (IS_DIRSYNC(inode)) |
| 824 | handle->h_sync = 1; |
| 825 | insert_inode_hash(inode); |
| 826 | spin_lock(&sbi->s_next_gen_lock); |
| 827 | inode->i_generation = sbi->s_next_generation++; |
| 828 | spin_unlock(&sbi->s_next_gen_lock); |
| 829 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 830 | ei->i_state = EXT4_STATE_NEW; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 831 | |
| 832 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 833 | |
| 834 | ret = inode; |
| 835 | if(DQUOT_ALLOC_INODE(inode)) { |
| 836 | err = -EDQUOT; |
| 837 | goto fail_drop; |
| 838 | } |
| 839 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 840 | err = ext4_init_acl(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 841 | if (err) |
| 842 | goto fail_free_drop; |
| 843 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 844 | err = ext4_init_security(handle,inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 845 | if (err) |
| 846 | goto fail_free_drop; |
| 847 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 848 | if (test_opt(sb, EXTENTS)) { |
Eric Sandeen | e4079a1 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 849 | /* set extent flag only for directory, file and normal symlink*/ |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 850 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 851 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
| 852 | ext4_ext_tree_init(handle, inode); |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 853 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 854 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 855 | |
Aneesh Kumar K.V | 8753e88 | 2008-04-29 22:00:36 -0400 | [diff] [blame] | 856 | err = ext4_mark_inode_dirty(handle, inode); |
| 857 | if (err) { |
| 858 | ext4_std_error(sb, err); |
| 859 | goto fail_free_drop; |
| 860 | } |
| 861 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 862 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 863 | goto really_out; |
| 864 | fail: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 865 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 866 | out: |
| 867 | iput(inode); |
| 868 | ret = ERR_PTR(err); |
| 869 | really_out: |
| 870 | brelse(bitmap_bh); |
| 871 | return ret; |
| 872 | |
| 873 | fail_free_drop: |
| 874 | DQUOT_FREE_INODE(inode); |
| 875 | |
| 876 | fail_drop: |
| 877 | DQUOT_DROP(inode); |
| 878 | inode->i_flags |= S_NOQUOTA; |
| 879 | inode->i_nlink = 0; |
| 880 | iput(inode); |
| 881 | brelse(bitmap_bh); |
| 882 | return ERR_PTR(err); |
| 883 | } |
| 884 | |
| 885 | /* Verify that we are loading a valid orphan from disk */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 886 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 887 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 888 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 889 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 890 | int bit; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 891 | struct buffer_head *bitmap_bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 892 | struct inode *inode = NULL; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 893 | long err = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 894 | |
| 895 | /* Error cases - e2fsck has already cleaned up for us */ |
| 896 | if (ino > max_ino) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 897 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 898 | "bad orphan ino %lu! e2fsck was run?", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 899 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 902 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 903 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 904 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 905 | if (!bitmap_bh) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 906 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 907 | "inode bitmap error for orphan %lu", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 908 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* Having the inode bit set should be a 100% indicator that this |
| 912 | * is a valid orphan (no e2fsck run on fs). Orphans also include |
| 913 | * inodes that were being truncated, so we can't check i_nlink==0. |
| 914 | */ |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 915 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
| 916 | goto bad_orphan; |
| 917 | |
| 918 | inode = ext4_iget(sb, ino); |
| 919 | if (IS_ERR(inode)) |
| 920 | goto iget_failed; |
| 921 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 922 | /* |
| 923 | * If the orphans has i_nlinks > 0 then it should be able to be |
| 924 | * truncated, otherwise it won't be removed from the orphan list |
| 925 | * during processing and an infinite loop will result. |
| 926 | */ |
| 927 | if (inode->i_nlink && !ext4_can_truncate(inode)) |
| 928 | goto bad_orphan; |
| 929 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 930 | if (NEXT_ORPHAN(inode) > max_ino) |
| 931 | goto bad_orphan; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 932 | brelse(bitmap_bh); |
| 933 | return inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 934 | |
| 935 | iget_failed: |
| 936 | err = PTR_ERR(inode); |
| 937 | inode = NULL; |
| 938 | bad_orphan: |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 939 | ext4_warning(sb, __func__, |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 940 | "bad orphan inode %lu! e2fsck was run?", ino); |
| 941 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", |
| 942 | bit, (unsigned long long)bitmap_bh->b_blocknr, |
| 943 | ext4_test_bit(bit, bitmap_bh->b_data)); |
| 944 | printk(KERN_NOTICE "inode=%p\n", inode); |
| 945 | if (inode) { |
| 946 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", |
| 947 | is_bad_inode(inode)); |
| 948 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", |
| 949 | NEXT_ORPHAN(inode)); |
| 950 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 951 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 952 | /* Avoid freeing blocks if we got a bad deleted inode */ |
| 953 | if (inode->i_nlink == 0) |
| 954 | inode->i_blocks = 0; |
| 955 | iput(inode); |
| 956 | } |
| 957 | brelse(bitmap_bh); |
| 958 | error: |
| 959 | return ERR_PTR(err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 962 | unsigned long ext4_count_free_inodes (struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 963 | { |
| 964 | unsigned long desc_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 965 | struct ext4_group_desc *gdp; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 966 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 967 | #ifdef EXT4FS_DEBUG |
| 968 | struct ext4_super_block *es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 969 | unsigned long bitmap_count, x; |
| 970 | struct buffer_head *bitmap_bh = NULL; |
| 971 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 972 | es = EXT4_SB(sb)->s_es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 973 | desc_count = 0; |
| 974 | bitmap_count = 0; |
| 975 | gdp = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 976 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 977 | gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 978 | if (!gdp) |
| 979 | continue; |
| 980 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); |
| 981 | brelse(bitmap_bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 982 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 983 | if (!bitmap_bh) |
| 984 | continue; |
| 985 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 986 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
Eric Sandeen | c549a95 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 987 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 988 | i, le16_to_cpu(gdp->bg_free_inodes_count), x); |
| 989 | bitmap_count += x; |
| 990 | } |
| 991 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 992 | printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 993 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); |
| 994 | return desc_count; |
| 995 | #else |
| 996 | desc_count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 997 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 998 | gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 999 | if (!gdp) |
| 1000 | continue; |
| 1001 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); |
| 1002 | cond_resched(); |
| 1003 | } |
| 1004 | return desc_count; |
| 1005 | #endif |
| 1006 | } |
| 1007 | |
| 1008 | /* Called at mount-time, super-block is locked */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1009 | unsigned long ext4_count_dirs (struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | { |
| 1011 | unsigned long count = 0; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1012 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1013 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1014 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 1015 | struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1016 | if (!gdp) |
| 1017 | continue; |
| 1018 | count += le16_to_cpu(gdp->bg_used_dirs_count); |
| 1019 | } |
| 1020 | return count; |
| 1021 | } |
| 1022 | |