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