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