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