Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/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@dcs.ed.ac.uk), 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> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/backing-dev.h> |
| 18 | #include <linux/buffer_head.h> |
| 19 | #include <linux/random.h> |
| 20 | #include "ext2.h" |
| 21 | #include "xattr.h" |
| 22 | #include "acl.h" |
| 23 | |
| 24 | /* |
| 25 | * ialloc.c contains the inodes allocation and deallocation routines |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * The free inodes are managed by bitmaps. A file system contains several |
| 30 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
| 31 | * block for inodes, N blocks for the inode table and data blocks. |
| 32 | * |
| 33 | * The file system contains group descriptors which are located after the |
| 34 | * super block. Each descriptor contains the number of the bitmap block and |
| 35 | * the free blocks count in the block. |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * Read the inode allocation bitmap for a given block_group, reading |
| 41 | * into the specified slot in the superblock's bitmap cache. |
| 42 | * |
| 43 | * Return buffer_head of bitmap on success or NULL. |
| 44 | */ |
| 45 | static struct buffer_head * |
| 46 | read_inode_bitmap(struct super_block * sb, unsigned long block_group) |
| 47 | { |
| 48 | struct ext2_group_desc *desc; |
| 49 | struct buffer_head *bh = NULL; |
| 50 | |
| 51 | desc = ext2_get_group_desc(sb, block_group, NULL); |
| 52 | if (!desc) |
| 53 | goto error_out; |
| 54 | |
| 55 | bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap)); |
| 56 | if (!bh) |
| 57 | ext2_error(sb, "read_inode_bitmap", |
| 58 | "Cannot read inode bitmap - " |
| 59 | "block_group = %lu, inode_bitmap = %u", |
| 60 | block_group, le32_to_cpu(desc->bg_inode_bitmap)); |
| 61 | error_out: |
| 62 | return bh; |
| 63 | } |
| 64 | |
| 65 | static void ext2_release_inode(struct super_block *sb, int group, int dir) |
| 66 | { |
| 67 | struct ext2_group_desc * desc; |
| 68 | struct buffer_head *bh; |
| 69 | |
| 70 | desc = ext2_get_group_desc(sb, group, &bh); |
| 71 | if (!desc) { |
| 72 | ext2_error(sb, "ext2_release_inode", |
| 73 | "can't get descriptor for group %d", group); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | spin_lock(sb_bgl_lock(EXT2_SB(sb), group)); |
Marcin Slusarz | fba4d39 | 2008-04-28 02:15:59 -0700 | [diff] [blame] | 78 | le16_add_cpu(&desc->bg_free_inodes_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (dir) |
Marcin Slusarz | fba4d39 | 2008-04-28 02:15:59 -0700 | [diff] [blame] | 80 | le16_add_cpu(&desc->bg_used_dirs_count, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | spin_unlock(sb_bgl_lock(EXT2_SB(sb), group)); |
| 82 | if (dir) |
| 83 | percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | mark_buffer_dirty(bh); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * NOTE! When we get the inode, we're the only people |
| 89 | * that have access to it, and as such there are no |
| 90 | * race conditions we have to worry about. The inode |
| 91 | * is not on the hash-lists, and it cannot be reached |
| 92 | * through the filesystem because the directory entry |
| 93 | * has been deleted earlier. |
| 94 | * |
| 95 | * HOWEVER: we must make sure that we get no aliases, |
| 96 | * which means that we have to call "clear_inode()" |
| 97 | * _before_ we mark the inode not in use in the inode |
| 98 | * bitmaps. Otherwise a newly created file might use |
| 99 | * the same inode number (not actually the same pointer |
| 100 | * though), and then we'd have two inodes sharing the |
| 101 | * same inode number and space on the harddisk. |
| 102 | */ |
| 103 | void ext2_free_inode (struct inode * inode) |
| 104 | { |
| 105 | struct super_block * sb = inode->i_sb; |
| 106 | int is_directory; |
| 107 | unsigned long ino; |
Francis Moreau | 524e4a1 | 2010-04-08 11:35:17 +0200 | [diff] [blame] | 108 | struct buffer_head *bitmap_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | unsigned long block_group; |
| 110 | unsigned long bit; |
| 111 | struct ext2_super_block * es; |
| 112 | |
| 113 | ino = inode->i_ino; |
| 114 | ext2_debug ("freeing inode %lu\n", ino); |
| 115 | |
| 116 | /* |
| 117 | * Note: we must free any quota before locking the superblock, |
| 118 | * as writing the quota to disk may need the lock as well. |
| 119 | */ |
Al Viro | 72edc4d | 2010-06-04 23:32:28 -0400 | [diff] [blame] | 120 | /* Quota is already initialized in iput() */ |
Al Viro | 72edc4d | 2010-06-04 23:32:28 -0400 | [diff] [blame] | 121 | dquot_free_inode(inode); |
| 122 | dquot_drop(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | es = EXT2_SB(sb)->s_es; |
| 125 | is_directory = S_ISDIR(inode->i_mode); |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (ino < EXT2_FIRST_INO(sb) || |
| 128 | ino > le32_to_cpu(es->s_inodes_count)) { |
| 129 | ext2_error (sb, "ext2_free_inode", |
| 130 | "reserved or nonexistent inode %lu", ino); |
Francis Moreau | 524e4a1 | 2010-04-08 11:35:17 +0200 | [diff] [blame] | 131 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); |
| 134 | bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | bitmap_bh = read_inode_bitmap(sb, block_group); |
| 136 | if (!bitmap_bh) |
Francis Moreau | 524e4a1 | 2010-04-08 11:35:17 +0200 | [diff] [blame] | 137 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | /* Ok, now we can actually update the inode bitmaps.. */ |
| 140 | if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group), |
| 141 | bit, (void *) bitmap_bh->b_data)) |
| 142 | ext2_error (sb, "ext2_free_inode", |
| 143 | "bit already cleared for inode %lu", ino); |
| 144 | else |
| 145 | ext2_release_inode(sb, block_group, is_directory); |
| 146 | mark_buffer_dirty(bitmap_bh); |
| 147 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 148 | sync_dirty_buffer(bitmap_bh); |
Francis Moreau | 524e4a1 | 2010-04-08 11:35:17 +0200 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | brelse(bitmap_bh); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * We perform asynchronous prereading of the new inode's inode block when |
| 155 | * we create the inode, in the expectation that the inode will be written |
| 156 | * back soon. There are two reasons: |
| 157 | * |
| 158 | * - When creating a large number of files, the async prereads will be |
| 159 | * nicely merged into large reads |
| 160 | * - When writing out a large number of inodes, we don't need to keep on |
| 161 | * stalling the writes while we read the inode block. |
| 162 | * |
| 163 | * FIXME: ext2_get_group_desc() needs to be simplified. |
| 164 | */ |
| 165 | static void ext2_preread_inode(struct inode *inode) |
| 166 | { |
| 167 | unsigned long block_group; |
| 168 | unsigned long offset; |
| 169 | unsigned long block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | struct ext2_group_desc * gdp; |
| 171 | struct backing_dev_info *bdi; |
| 172 | |
| 173 | bdi = inode->i_mapping->backing_dev_info; |
| 174 | if (bdi_read_congested(bdi)) |
| 175 | return; |
| 176 | if (bdi_write_congested(bdi)) |
| 177 | return; |
| 178 | |
| 179 | block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 180 | gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (gdp == NULL) |
| 182 | return; |
| 183 | |
| 184 | /* |
| 185 | * Figure out the offset within the block group inode table |
| 186 | */ |
| 187 | offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) * |
| 188 | EXT2_INODE_SIZE(inode->i_sb); |
| 189 | block = le32_to_cpu(gdp->bg_inode_table) + |
| 190 | (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb)); |
| 191 | sb_breadahead(inode->i_sb, block); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * There are two policies for allocating an inode. If the new inode is |
| 196 | * a directory, then a forward search is made for a block group with both |
| 197 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 198 | * the groups with above-average free space, that group with the fewest |
| 199 | * directories already is chosen. |
| 200 | * |
| 201 | * For other inodes, search forward from the parent directory\'s block |
| 202 | * group to find a free inode. |
| 203 | */ |
| 204 | static int find_group_dir(struct super_block *sb, struct inode *parent) |
| 205 | { |
| 206 | int ngroups = EXT2_SB(sb)->s_groups_count; |
| 207 | int avefreei = ext2_count_free_inodes(sb) / ngroups; |
| 208 | struct ext2_group_desc *desc, *best_desc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int group, best_group = -1; |
| 210 | |
| 211 | for (group = 0; group < ngroups; group++) { |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 212 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (!desc || !desc->bg_free_inodes_count) |
| 214 | continue; |
| 215 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 216 | continue; |
| 217 | if (!best_desc || |
| 218 | (le16_to_cpu(desc->bg_free_blocks_count) > |
| 219 | le16_to_cpu(best_desc->bg_free_blocks_count))) { |
| 220 | best_group = group; |
| 221 | best_desc = desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | if (!best_desc) |
| 225 | return -1; |
| 226 | |
| 227 | return best_group; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Orlov's allocator for directories. |
| 232 | * |
| 233 | * We always try to spread first-level directories. |
| 234 | * |
| 235 | * If there are blockgroups with both free inodes and free blocks counts |
| 236 | * not worse than average we return one with smallest directory count. |
| 237 | * Otherwise we simply return a random group. |
| 238 | * |
| 239 | * For the rest rules look so: |
| 240 | * |
| 241 | * It's OK to put directory into a group unless |
| 242 | * it has too many directories already (max_dirs) or |
| 243 | * it has too few free inodes left (min_inodes) or |
| 244 | * it has too few free blocks left (min_blocks) or |
| 245 | * it's already running too large debt (max_debt). |
Benoit Boissinot | 1cc8dcf5 | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 246 | * Parent's group is preferred, if it doesn't satisfy these |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * conditions we search cyclically through the rest. If none |
| 248 | * of the groups look good we just look for a group with more |
| 249 | * free inodes than average (starting at parent's group). |
| 250 | * |
| 251 | * Debt is incremented each time we allocate a directory and decremented |
| 252 | * when we allocate an inode, within 0--255. |
| 253 | */ |
| 254 | |
| 255 | #define INODE_COST 64 |
| 256 | #define BLOCK_COST 256 |
| 257 | |
| 258 | static int find_group_orlov(struct super_block *sb, struct inode *parent) |
| 259 | { |
| 260 | int parent_group = EXT2_I(parent)->i_block_group; |
| 261 | struct ext2_sb_info *sbi = EXT2_SB(sb); |
| 262 | struct ext2_super_block *es = sbi->s_es; |
| 263 | int ngroups = sbi->s_groups_count; |
| 264 | int inodes_per_group = EXT2_INODES_PER_GROUP(sb); |
| 265 | int freei; |
| 266 | int avefreei; |
| 267 | int free_blocks; |
| 268 | int avefreeb; |
| 269 | int blocks_per_dir; |
| 270 | int ndirs; |
| 271 | int max_debt, max_dirs, min_blocks, min_inodes; |
| 272 | int group = -1, i; |
| 273 | struct ext2_group_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); |
| 276 | avefreei = freei / ngroups; |
| 277 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
| 278 | avefreeb = free_blocks / ngroups; |
| 279 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
| 280 | |
| 281 | if ((parent == sb->s_root->d_inode) || |
| 282 | (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) { |
| 283 | struct ext2_group_desc *best_desc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int best_ndir = inodes_per_group; |
| 285 | int best_group = -1; |
| 286 | |
| 287 | get_random_bytes(&group, sizeof(group)); |
| 288 | parent_group = (unsigned)group % ngroups; |
| 289 | for (i = 0; i < ngroups; i++) { |
| 290 | group = (parent_group + i) % ngroups; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 291 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (!desc || !desc->bg_free_inodes_count) |
| 293 | continue; |
| 294 | if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) |
| 295 | continue; |
| 296 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 297 | continue; |
| 298 | if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) |
| 299 | continue; |
| 300 | best_group = group; |
| 301 | best_ndir = le16_to_cpu(desc->bg_used_dirs_count); |
| 302 | best_desc = desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | if (best_group >= 0) { |
| 305 | desc = best_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | group = best_group; |
| 307 | goto found; |
| 308 | } |
| 309 | goto fallback; |
| 310 | } |
| 311 | |
| 312 | if (ndirs == 0) |
| 313 | ndirs = 1; /* percpu_counters are approximate... */ |
| 314 | |
| 315 | blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs; |
| 316 | |
| 317 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
| 318 | min_inodes = avefreei - inodes_per_group / 4; |
| 319 | min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4; |
| 320 | |
| 321 | max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST); |
| 322 | if (max_debt * INODE_COST > inodes_per_group) |
| 323 | max_debt = inodes_per_group / INODE_COST; |
| 324 | if (max_debt > 255) |
| 325 | max_debt = 255; |
| 326 | if (max_debt == 0) |
| 327 | max_debt = 1; |
| 328 | |
| 329 | for (i = 0; i < ngroups; i++) { |
| 330 | group = (parent_group + i) % ngroups; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 331 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (!desc || !desc->bg_free_inodes_count) |
| 333 | continue; |
| 334 | if (sbi->s_debts[group] >= max_debt) |
| 335 | continue; |
| 336 | if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) |
| 337 | continue; |
| 338 | if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) |
| 339 | continue; |
| 340 | if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) |
| 341 | continue; |
| 342 | goto found; |
| 343 | } |
| 344 | |
| 345 | fallback: |
| 346 | for (i = 0; i < ngroups; i++) { |
| 347 | group = (parent_group + i) % ngroups; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 348 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (!desc || !desc->bg_free_inodes_count) |
| 350 | continue; |
| 351 | if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) |
| 352 | goto found; |
| 353 | } |
| 354 | |
| 355 | if (avefreei) { |
| 356 | /* |
| 357 | * The free-inodes counter is approximate, and for really small |
| 358 | * filesystems the above test can fail to find any blockgroups |
| 359 | */ |
| 360 | avefreei = 0; |
| 361 | goto fallback; |
| 362 | } |
| 363 | |
| 364 | return -1; |
| 365 | |
| 366 | found: |
| 367 | return group; |
| 368 | } |
| 369 | |
| 370 | static int find_group_other(struct super_block *sb, struct inode *parent) |
| 371 | { |
| 372 | int parent_group = EXT2_I(parent)->i_block_group; |
| 373 | int ngroups = EXT2_SB(sb)->s_groups_count; |
| 374 | struct ext2_group_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | int group, i; |
| 376 | |
| 377 | /* |
| 378 | * Try to place the inode in its parent directory |
| 379 | */ |
| 380 | group = parent_group; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 381 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 383 | le16_to_cpu(desc->bg_free_blocks_count)) |
| 384 | goto found; |
| 385 | |
| 386 | /* |
| 387 | * We're going to place this inode in a different blockgroup from its |
| 388 | * parent. We want to cause files in a common directory to all land in |
| 389 | * the same blockgroup. But we want files which are in a different |
| 390 | * directory which shares a blockgroup with our parent to land in a |
| 391 | * different blockgroup. |
| 392 | * |
| 393 | * So add our directory's i_ino into the starting point for the hash. |
| 394 | */ |
| 395 | group = (group + parent->i_ino) % ngroups; |
| 396 | |
| 397 | /* |
| 398 | * Use a quadratic hash to find a group with a free inode and some |
| 399 | * free blocks. |
| 400 | */ |
| 401 | for (i = 1; i < ngroups; i <<= 1) { |
| 402 | group += i; |
| 403 | if (group >= ngroups) |
| 404 | group -= ngroups; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 405 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 407 | le16_to_cpu(desc->bg_free_blocks_count)) |
| 408 | goto found; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * That failed: try linear search for a free inode, even if that group |
| 413 | * has no free blocks. |
| 414 | */ |
| 415 | group = parent_group; |
| 416 | for (i = 0; i < ngroups; i++) { |
| 417 | if (++group >= ngroups) |
| 418 | group = 0; |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 419 | desc = ext2_get_group_desc (sb, group, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (desc && le16_to_cpu(desc->bg_free_inodes_count)) |
| 421 | goto found; |
| 422 | } |
| 423 | |
| 424 | return -1; |
| 425 | |
| 426 | found: |
| 427 | return group; |
| 428 | } |
| 429 | |
Al Viro | 3ea40bc | 2011-07-26 02:46:18 -0400 | [diff] [blame] | 430 | struct inode *ext2_new_inode(struct inode *dir, umode_t mode, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 431 | const struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | struct super_block *sb; |
| 434 | struct buffer_head *bitmap_bh = NULL; |
| 435 | struct buffer_head *bh2; |
| 436 | int group, i; |
| 437 | ino_t ino = 0; |
| 438 | struct inode * inode; |
| 439 | struct ext2_group_desc *gdp; |
| 440 | struct ext2_super_block *es; |
| 441 | struct ext2_inode_info *ei; |
| 442 | struct ext2_sb_info *sbi; |
| 443 | int err; |
| 444 | |
| 445 | sb = dir->i_sb; |
| 446 | inode = new_inode(sb); |
| 447 | if (!inode) |
| 448 | return ERR_PTR(-ENOMEM); |
| 449 | |
| 450 | ei = EXT2_I(inode); |
| 451 | sbi = EXT2_SB(sb); |
| 452 | es = sbi->s_es; |
| 453 | if (S_ISDIR(mode)) { |
| 454 | if (test_opt(sb, OLDALLOC)) |
| 455 | group = find_group_dir(sb, dir); |
| 456 | else |
| 457 | group = find_group_orlov(sb, dir); |
| 458 | } else |
| 459 | group = find_group_other(sb, dir); |
| 460 | |
| 461 | if (group == -1) { |
| 462 | err = -ENOSPC; |
| 463 | goto fail; |
| 464 | } |
| 465 | |
| 466 | for (i = 0; i < sbi->s_groups_count; i++) { |
| 467 | gdp = ext2_get_group_desc(sb, group, &bh2); |
| 468 | brelse(bitmap_bh); |
| 469 | bitmap_bh = read_inode_bitmap(sb, group); |
| 470 | if (!bitmap_bh) { |
| 471 | err = -EIO; |
| 472 | goto fail; |
| 473 | } |
| 474 | ino = 0; |
| 475 | |
| 476 | repeat_in_this_group: |
| 477 | ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data, |
| 478 | EXT2_INODES_PER_GROUP(sb), ino); |
| 479 | if (ino >= EXT2_INODES_PER_GROUP(sb)) { |
| 480 | /* |
| 481 | * Rare race: find_group_xx() decided that there were |
| 482 | * free inodes in this group, but by the time we tried |
| 483 | * to allocate one, they're all gone. This can also |
| 484 | * occur because the counters which find_group_orlov() |
| 485 | * uses are approximate. So just go and search the |
| 486 | * next block group. |
| 487 | */ |
| 488 | if (++group == sbi->s_groups_count) |
| 489 | group = 0; |
| 490 | continue; |
| 491 | } |
| 492 | if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group), |
| 493 | ino, bitmap_bh->b_data)) { |
| 494 | /* we lost this inode */ |
| 495 | if (++ino >= EXT2_INODES_PER_GROUP(sb)) { |
| 496 | /* this group is exhausted, try next group */ |
| 497 | if (++group == sbi->s_groups_count) |
| 498 | group = 0; |
| 499 | continue; |
| 500 | } |
| 501 | /* try to find free inode in the same group */ |
| 502 | goto repeat_in_this_group; |
| 503 | } |
| 504 | goto got; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Scanned all blockgroups. |
| 509 | */ |
| 510 | err = -ENOSPC; |
| 511 | goto fail; |
| 512 | got: |
| 513 | mark_buffer_dirty(bitmap_bh); |
| 514 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 515 | sync_dirty_buffer(bitmap_bh); |
| 516 | brelse(bitmap_bh); |
| 517 | |
| 518 | ino += group * EXT2_INODES_PER_GROUP(sb) + 1; |
| 519 | if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { |
| 520 | ext2_error (sb, "ext2_new_inode", |
| 521 | "reserved inode or inode > inodes count - " |
| 522 | "block_group = %d,inode=%lu", group, |
| 523 | (unsigned long) ino); |
| 524 | err = -EIO; |
| 525 | goto fail; |
| 526 | } |
| 527 | |
Peter Zijlstra | aa0dff2 | 2007-10-16 23:25:42 -0700 | [diff] [blame] | 528 | percpu_counter_add(&sbi->s_freeinodes_counter, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (S_ISDIR(mode)) |
| 530 | percpu_counter_inc(&sbi->s_dirs_counter); |
| 531 | |
| 532 | spin_lock(sb_bgl_lock(sbi, group)); |
Marcin Slusarz | fba4d39 | 2008-04-28 02:15:59 -0700 | [diff] [blame] | 533 | le16_add_cpu(&gdp->bg_free_inodes_count, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (S_ISDIR(mode)) { |
| 535 | if (sbi->s_debts[group] < 255) |
| 536 | sbi->s_debts[group]++; |
Marcin Slusarz | fba4d39 | 2008-04-28 02:15:59 -0700 | [diff] [blame] | 537 | le16_add_cpu(&gdp->bg_used_dirs_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } else { |
| 539 | if (sbi->s_debts[group]) |
| 540 | sbi->s_debts[group]--; |
| 541 | } |
| 542 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | mark_buffer_dirty(bh2); |
Dmitry Monakhov | ffba102 | 2010-03-04 17:31:49 +0300 | [diff] [blame] | 545 | if (test_opt(sb, GRPID)) { |
| 546 | inode->i_mode = mode; |
| 547 | inode->i_uid = current_fsuid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | inode->i_gid = dir->i_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } else |
Dmitry Monakhov | ffba102 | 2010-03-04 17:31:49 +0300 | [diff] [blame] | 550 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | inode->i_ino = ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | inode->i_blocks = 0; |
| 554 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 555 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
Duane Griffin | ef8b646 | 2009-01-07 18:07:21 -0800 | [diff] [blame] | 556 | ei->i_flags = |
| 557 | ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | ei->i_faddr = 0; |
| 559 | ei->i_frag_no = 0; |
| 560 | ei->i_frag_size = 0; |
| 561 | ei->i_file_acl = 0; |
| 562 | ei->i_dir_acl = 0; |
| 563 | ei->i_dtime = 0; |
Martin J. Bligh | a686cd8 | 2007-10-16 23:30:46 -0700 | [diff] [blame] | 564 | ei->i_block_alloc_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | ei->i_block_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | ei->i_dir_start_lookup = 0; |
| 567 | ei->i_state = EXT2_STATE_NEW; |
| 568 | ext2_set_inode_flags(inode); |
| 569 | spin_lock(&sbi->s_next_gen_lock); |
| 570 | inode->i_generation = sbi->s_next_generation++; |
| 571 | spin_unlock(&sbi->s_next_gen_lock); |
Al Viro | 41080b5 | 2008-12-30 01:52:35 -0500 | [diff] [blame] | 572 | if (insert_inode_locked(inode) < 0) { |
Jan Kara | ef6919c | 2011-12-09 00:08:58 +0100 | [diff] [blame] | 573 | ext2_error(sb, "ext2_new_inode", |
| 574 | "inode number already in use - inode=%lu", |
| 575 | (unsigned long) ino); |
| 576 | err = -EIO; |
| 577 | goto fail; |
Al Viro | 41080b5 | 2008-12-30 01:52:35 -0500 | [diff] [blame] | 578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 580 | dquot_initialize(inode); |
Christoph Hellwig | 63936dd | 2010-03-03 09:05:01 -0500 | [diff] [blame] | 581 | err = dquot_alloc_inode(inode); |
| 582 | if (err) |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 583 | goto fail_drop; |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | err = ext2_init_acl(inode, dir); |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 586 | if (err) |
| 587 | goto fail_free_drop; |
| 588 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 589 | err = ext2_init_security(inode, dir, qstr); |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 590 | if (err) |
| 591 | goto fail_free_drop; |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | mark_inode_dirty(inode); |
| 594 | ext2_debug("allocating inode %lu\n", inode->i_ino); |
| 595 | ext2_preread_inode(inode); |
| 596 | return inode; |
| 597 | |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 598 | fail_free_drop: |
Christoph Hellwig | 63936dd | 2010-03-03 09:05:01 -0500 | [diff] [blame] | 599 | dquot_free_inode(inode); |
Chris Sykes | 9ed6c2f | 2005-09-27 21:45:22 -0700 | [diff] [blame] | 600 | |
| 601 | fail_drop: |
Christoph Hellwig | 9f75475 | 2010-03-03 09:05:05 -0500 | [diff] [blame] | 602 | dquot_drop(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | inode->i_flags |= S_NOQUOTA; |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 604 | clear_nlink(inode); |
Al Viro | 41080b5 | 2008-12-30 01:52:35 -0500 | [diff] [blame] | 605 | unlock_new_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | iput(inode); |
| 607 | return ERR_PTR(err); |
| 608 | |
| 609 | fail: |
| 610 | make_bad_inode(inode); |
| 611 | iput(inode); |
| 612 | return ERR_PTR(err); |
| 613 | } |
| 614 | |
| 615 | unsigned long ext2_count_free_inodes (struct super_block * sb) |
| 616 | { |
| 617 | struct ext2_group_desc *desc; |
| 618 | unsigned long desc_count = 0; |
| 619 | int i; |
| 620 | |
| 621 | #ifdef EXT2FS_DEBUG |
| 622 | struct ext2_super_block *es; |
| 623 | unsigned long bitmap_count = 0; |
| 624 | struct buffer_head *bitmap_bh = NULL; |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | es = EXT2_SB(sb)->s_es; |
| 627 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { |
| 628 | unsigned x; |
| 629 | |
| 630 | desc = ext2_get_group_desc (sb, i, NULL); |
| 631 | if (!desc) |
| 632 | continue; |
| 633 | desc_count += le16_to_cpu(desc->bg_free_inodes_count); |
| 634 | brelse(bitmap_bh); |
| 635 | bitmap_bh = read_inode_bitmap(sb, i); |
| 636 | if (!bitmap_bh) |
| 637 | continue; |
| 638 | |
| 639 | x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8); |
| 640 | printk("group %d: stored = %d, counted = %u\n", |
| 641 | i, le16_to_cpu(desc->bg_free_inodes_count), x); |
| 642 | bitmap_count += x; |
| 643 | } |
| 644 | brelse(bitmap_bh); |
| 645 | printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n", |
Akinobu Mita | ecd0afa | 2012-07-30 14:41:05 -0700 | [diff] [blame] | 646 | (unsigned long) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter), |
| 648 | desc_count, bitmap_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return desc_count; |
| 650 | #else |
| 651 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { |
| 652 | desc = ext2_get_group_desc (sb, i, NULL); |
| 653 | if (!desc) |
| 654 | continue; |
| 655 | desc_count += le16_to_cpu(desc->bg_free_inodes_count); |
| 656 | } |
| 657 | return desc_count; |
| 658 | #endif |
| 659 | } |
| 660 | |
| 661 | /* Called at mount-time, super-block is locked */ |
| 662 | unsigned long ext2_count_dirs (struct super_block * sb) |
| 663 | { |
| 664 | unsigned long count = 0; |
| 665 | int i; |
| 666 | |
| 667 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { |
| 668 | struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL); |
| 669 | if (!gdp) |
| 670 | continue; |
| 671 | count += le16_to_cpu(gdp->bg_used_dirs_count); |
| 672 | } |
| 673 | return count; |
| 674 | } |
| 675 | |