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