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