0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/quotaops.h>
0017 #include <linux/sched.h>
0018 #include <linux/backing-dev.h>
0019 #include <linux/buffer_head.h>
0020 #include <linux/random.h>
0021 #include "ext2.h"
0022 #include "xattr.h"
0023 #include "acl.h"
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 static struct buffer_head *
0047 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
0048 {
0049 struct ext2_group_desc *desc;
0050 struct buffer_head *bh = NULL;
0051
0052 desc = ext2_get_group_desc(sb, block_group, NULL);
0053 if (!desc)
0054 goto error_out;
0055
0056 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
0057 if (!bh)
0058 ext2_error(sb, "read_inode_bitmap",
0059 "Cannot read inode bitmap - "
0060 "block_group = %lu, inode_bitmap = %u",
0061 block_group, le32_to_cpu(desc->bg_inode_bitmap));
0062 error_out:
0063 return bh;
0064 }
0065
0066 static void ext2_release_inode(struct super_block *sb, int group, int dir)
0067 {
0068 struct ext2_group_desc * desc;
0069 struct buffer_head *bh;
0070
0071 desc = ext2_get_group_desc(sb, group, &bh);
0072 if (!desc) {
0073 ext2_error(sb, "ext2_release_inode",
0074 "can't get descriptor for group %d", group);
0075 return;
0076 }
0077
0078 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
0079 le16_add_cpu(&desc->bg_free_inodes_count, 1);
0080 if (dir)
0081 le16_add_cpu(&desc->bg_used_dirs_count, -1);
0082 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
0083 percpu_counter_inc(&EXT2_SB(sb)->s_freeinodes_counter);
0084 if (dir)
0085 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
0086 mark_buffer_dirty(bh);
0087 }
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 void ext2_free_inode (struct inode * inode)
0106 {
0107 struct super_block * sb = inode->i_sb;
0108 int is_directory;
0109 unsigned long ino;
0110 struct buffer_head *bitmap_bh;
0111 unsigned long block_group;
0112 unsigned long bit;
0113 struct ext2_super_block * es;
0114
0115 ino = inode->i_ino;
0116 ext2_debug ("freeing inode %lu\n", ino);
0117
0118
0119
0120
0121
0122
0123 dquot_free_inode(inode);
0124 dquot_drop(inode);
0125
0126 es = EXT2_SB(sb)->s_es;
0127 is_directory = S_ISDIR(inode->i_mode);
0128
0129 if (ino < EXT2_FIRST_INO(sb) ||
0130 ino > le32_to_cpu(es->s_inodes_count)) {
0131 ext2_error (sb, "ext2_free_inode",
0132 "reserved or nonexistent inode %lu", ino);
0133 return;
0134 }
0135 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
0136 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
0137 bitmap_bh = read_inode_bitmap(sb, block_group);
0138 if (!bitmap_bh)
0139 return;
0140
0141
0142 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
0143 bit, (void *) bitmap_bh->b_data))
0144 ext2_error (sb, "ext2_free_inode",
0145 "bit already cleared for inode %lu", ino);
0146 else
0147 ext2_release_inode(sb, block_group, is_directory);
0148 mark_buffer_dirty(bitmap_bh);
0149 if (sb->s_flags & SB_SYNCHRONOUS)
0150 sync_dirty_buffer(bitmap_bh);
0151
0152 brelse(bitmap_bh);
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 static void ext2_preread_inode(struct inode *inode)
0168 {
0169 unsigned long block_group;
0170 unsigned long offset;
0171 unsigned long block;
0172 struct ext2_group_desc * gdp;
0173
0174 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
0175 gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
0176 if (gdp == NULL)
0177 return;
0178
0179
0180
0181
0182 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
0183 EXT2_INODE_SIZE(inode->i_sb);
0184 block = le32_to_cpu(gdp->bg_inode_table) +
0185 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
0186 sb_breadahead(inode->i_sb, block);
0187 }
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 static int find_group_dir(struct super_block *sb, struct inode *parent)
0200 {
0201 int ngroups = EXT2_SB(sb)->s_groups_count;
0202 int avefreei = ext2_count_free_inodes(sb) / ngroups;
0203 struct ext2_group_desc *desc, *best_desc = NULL;
0204 int group, best_group = -1;
0205
0206 for (group = 0; group < ngroups; group++) {
0207 desc = ext2_get_group_desc (sb, group, NULL);
0208 if (!desc || !desc->bg_free_inodes_count)
0209 continue;
0210 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
0211 continue;
0212 if (!best_desc ||
0213 (le16_to_cpu(desc->bg_free_blocks_count) >
0214 le16_to_cpu(best_desc->bg_free_blocks_count))) {
0215 best_group = group;
0216 best_desc = desc;
0217 }
0218 }
0219
0220 return best_group;
0221 }
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 #define INODE_COST 64
0249 #define BLOCK_COST 256
0250
0251 static int find_group_orlov(struct super_block *sb, struct inode *parent)
0252 {
0253 int parent_group = EXT2_I(parent)->i_block_group;
0254 struct ext2_sb_info *sbi = EXT2_SB(sb);
0255 struct ext2_super_block *es = sbi->s_es;
0256 int ngroups = sbi->s_groups_count;
0257 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
0258 int freei;
0259 int avefreei;
0260 int free_blocks;
0261 int avefreeb;
0262 int blocks_per_dir;
0263 int ndirs;
0264 int max_debt, max_dirs, min_blocks, min_inodes;
0265 int group = -1, i;
0266 struct ext2_group_desc *desc;
0267
0268 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
0269 avefreei = freei / ngroups;
0270 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
0271 avefreeb = free_blocks / ngroups;
0272 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
0273
0274 if ((parent == d_inode(sb->s_root)) ||
0275 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
0276 struct ext2_group_desc *best_desc = NULL;
0277 int best_ndir = inodes_per_group;
0278 int best_group = -1;
0279
0280 group = prandom_u32();
0281 parent_group = (unsigned)group % ngroups;
0282 for (i = 0; i < ngroups; i++) {
0283 group = (parent_group + i) % ngroups;
0284 desc = ext2_get_group_desc (sb, group, NULL);
0285 if (!desc || !desc->bg_free_inodes_count)
0286 continue;
0287 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
0288 continue;
0289 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
0290 continue;
0291 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
0292 continue;
0293 best_group = group;
0294 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
0295 best_desc = desc;
0296 }
0297 if (best_group >= 0) {
0298 desc = best_desc;
0299 group = best_group;
0300 goto found;
0301 }
0302 goto fallback;
0303 }
0304
0305 if (ndirs == 0)
0306 ndirs = 1;
0307
0308 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
0309
0310 max_dirs = ndirs / ngroups + inodes_per_group / 16;
0311 min_inodes = avefreei - inodes_per_group / 4;
0312 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
0313
0314 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
0315 if (max_debt * INODE_COST > inodes_per_group)
0316 max_debt = inodes_per_group / INODE_COST;
0317 if (max_debt > 255)
0318 max_debt = 255;
0319 if (max_debt == 0)
0320 max_debt = 1;
0321
0322 for (i = 0; i < ngroups; i++) {
0323 group = (parent_group + i) % ngroups;
0324 desc = ext2_get_group_desc (sb, group, NULL);
0325 if (!desc || !desc->bg_free_inodes_count)
0326 continue;
0327 if (sbi->s_debts[group] >= max_debt)
0328 continue;
0329 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
0330 continue;
0331 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
0332 continue;
0333 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
0334 continue;
0335 goto found;
0336 }
0337
0338 fallback:
0339 for (i = 0; i < ngroups; i++) {
0340 group = (parent_group + i) % ngroups;
0341 desc = ext2_get_group_desc (sb, group, NULL);
0342 if (!desc || !desc->bg_free_inodes_count)
0343 continue;
0344 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
0345 goto found;
0346 }
0347
0348 if (avefreei) {
0349
0350
0351
0352
0353 avefreei = 0;
0354 goto fallback;
0355 }
0356
0357 return -1;
0358
0359 found:
0360 return group;
0361 }
0362
0363 static int find_group_other(struct super_block *sb, struct inode *parent)
0364 {
0365 int parent_group = EXT2_I(parent)->i_block_group;
0366 int ngroups = EXT2_SB(sb)->s_groups_count;
0367 struct ext2_group_desc *desc;
0368 int group, i;
0369
0370
0371
0372
0373 group = parent_group;
0374 desc = ext2_get_group_desc (sb, group, NULL);
0375 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
0376 le16_to_cpu(desc->bg_free_blocks_count))
0377 goto found;
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 group = (group + parent->i_ino) % ngroups;
0389
0390
0391
0392
0393
0394 for (i = 1; i < ngroups; i <<= 1) {
0395 group += i;
0396 if (group >= ngroups)
0397 group -= ngroups;
0398 desc = ext2_get_group_desc (sb, group, NULL);
0399 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
0400 le16_to_cpu(desc->bg_free_blocks_count))
0401 goto found;
0402 }
0403
0404
0405
0406
0407
0408 group = parent_group;
0409 for (i = 0; i < ngroups; i++) {
0410 if (++group >= ngroups)
0411 group = 0;
0412 desc = ext2_get_group_desc (sb, group, NULL);
0413 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
0414 goto found;
0415 }
0416
0417 return -1;
0418
0419 found:
0420 return group;
0421 }
0422
0423 struct inode *ext2_new_inode(struct inode *dir, umode_t mode,
0424 const struct qstr *qstr)
0425 {
0426 struct super_block *sb;
0427 struct buffer_head *bitmap_bh = NULL;
0428 struct buffer_head *bh2;
0429 int group, i;
0430 ino_t ino = 0;
0431 struct inode * inode;
0432 struct ext2_group_desc *gdp;
0433 struct ext2_super_block *es;
0434 struct ext2_inode_info *ei;
0435 struct ext2_sb_info *sbi;
0436 int err;
0437
0438 sb = dir->i_sb;
0439 inode = new_inode(sb);
0440 if (!inode)
0441 return ERR_PTR(-ENOMEM);
0442
0443 ei = EXT2_I(inode);
0444 sbi = EXT2_SB(sb);
0445 es = sbi->s_es;
0446 if (S_ISDIR(mode)) {
0447 if (test_opt(sb, OLDALLOC))
0448 group = find_group_dir(sb, dir);
0449 else
0450 group = find_group_orlov(sb, dir);
0451 } else
0452 group = find_group_other(sb, dir);
0453
0454 if (group == -1) {
0455 err = -ENOSPC;
0456 goto fail;
0457 }
0458
0459 for (i = 0; i < sbi->s_groups_count; i++) {
0460 gdp = ext2_get_group_desc(sb, group, &bh2);
0461 if (!gdp) {
0462 if (++group == sbi->s_groups_count)
0463 group = 0;
0464 continue;
0465 }
0466 brelse(bitmap_bh);
0467 bitmap_bh = read_inode_bitmap(sb, group);
0468 if (!bitmap_bh) {
0469 err = -EIO;
0470 goto fail;
0471 }
0472 ino = 0;
0473
0474 repeat_in_this_group:
0475 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
0476 EXT2_INODES_PER_GROUP(sb), ino);
0477 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
0478
0479
0480
0481
0482
0483
0484
0485
0486 if (++group == sbi->s_groups_count)
0487 group = 0;
0488 continue;
0489 }
0490 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
0491 ino, bitmap_bh->b_data)) {
0492
0493 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
0494
0495 if (++group == sbi->s_groups_count)
0496 group = 0;
0497 continue;
0498 }
0499
0500 goto repeat_in_this_group;
0501 }
0502 goto got;
0503 }
0504
0505
0506
0507
0508 brelse(bitmap_bh);
0509 err = -ENOSPC;
0510 goto fail;
0511 got:
0512 mark_buffer_dirty(bitmap_bh);
0513 if (sb->s_flags & SB_SYNCHRONOUS)
0514 sync_dirty_buffer(bitmap_bh);
0515 brelse(bitmap_bh);
0516
0517 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
0518 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
0519 ext2_error (sb, "ext2_new_inode",
0520 "reserved inode or inode > inodes count - "
0521 "block_group = %d,inode=%lu", group,
0522 (unsigned long) ino);
0523 err = -EIO;
0524 goto fail;
0525 }
0526
0527 percpu_counter_dec(&sbi->s_freeinodes_counter);
0528 if (S_ISDIR(mode))
0529 percpu_counter_inc(&sbi->s_dirs_counter);
0530
0531 spin_lock(sb_bgl_lock(sbi, group));
0532 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
0533 if (S_ISDIR(mode)) {
0534 if (sbi->s_debts[group] < 255)
0535 sbi->s_debts[group]++;
0536 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
0537 } else {
0538 if (sbi->s_debts[group])
0539 sbi->s_debts[group]--;
0540 }
0541 spin_unlock(sb_bgl_lock(sbi, group));
0542
0543 mark_buffer_dirty(bh2);
0544 if (test_opt(sb, GRPID)) {
0545 inode->i_mode = mode;
0546 inode->i_uid = current_fsuid();
0547 inode->i_gid = dir->i_gid;
0548 } else
0549 inode_init_owner(&init_user_ns, inode, dir, mode);
0550
0551 inode->i_ino = ino;
0552 inode->i_blocks = 0;
0553 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
0554 memset(ei->i_data, 0, sizeof(ei->i_data));
0555 ei->i_flags =
0556 ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
0557 ei->i_faddr = 0;
0558 ei->i_frag_no = 0;
0559 ei->i_frag_size = 0;
0560 ei->i_file_acl = 0;
0561 ei->i_dir_acl = 0;
0562 ei->i_dtime = 0;
0563 ei->i_block_alloc_info = NULL;
0564 ei->i_block_group = group;
0565 ei->i_dir_start_lookup = 0;
0566 ei->i_state = EXT2_STATE_NEW;
0567 ext2_set_inode_flags(inode);
0568 spin_lock(&sbi->s_next_gen_lock);
0569 inode->i_generation = sbi->s_next_generation++;
0570 spin_unlock(&sbi->s_next_gen_lock);
0571 if (insert_inode_locked(inode) < 0) {
0572 ext2_error(sb, "ext2_new_inode",
0573 "inode number already in use - inode=%lu",
0574 (unsigned long) ino);
0575 err = -EIO;
0576 goto fail;
0577 }
0578
0579 err = dquot_initialize(inode);
0580 if (err)
0581 goto fail_drop;
0582
0583 err = dquot_alloc_inode(inode);
0584 if (err)
0585 goto fail_drop;
0586
0587 err = ext2_init_acl(inode, dir);
0588 if (err)
0589 goto fail_free_drop;
0590
0591 err = ext2_init_security(inode, dir, qstr);
0592 if (err)
0593 goto fail_free_drop;
0594
0595 mark_inode_dirty(inode);
0596 ext2_debug("allocating inode %lu\n", inode->i_ino);
0597 ext2_preread_inode(inode);
0598 return inode;
0599
0600 fail_free_drop:
0601 dquot_free_inode(inode);
0602
0603 fail_drop:
0604 dquot_drop(inode);
0605 inode->i_flags |= S_NOQUOTA;
0606 clear_nlink(inode);
0607 discard_new_inode(inode);
0608 return ERR_PTR(err);
0609
0610 fail:
0611 make_bad_inode(inode);
0612 iput(inode);
0613 return ERR_PTR(err);
0614 }
0615
0616 unsigned long ext2_count_free_inodes (struct super_block * sb)
0617 {
0618 struct ext2_group_desc *desc;
0619 unsigned long desc_count = 0;
0620 int i;
0621
0622 #ifdef EXT2FS_DEBUG
0623 struct ext2_super_block *es;
0624 unsigned long bitmap_count = 0;
0625 struct buffer_head *bitmap_bh = NULL;
0626
0627 es = EXT2_SB(sb)->s_es;
0628 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
0629 unsigned x;
0630
0631 desc = ext2_get_group_desc (sb, i, NULL);
0632 if (!desc)
0633 continue;
0634 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
0635 brelse(bitmap_bh);
0636 bitmap_bh = read_inode_bitmap(sb, i);
0637 if (!bitmap_bh)
0638 continue;
0639
0640 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
0641 printk("group %d: stored = %d, counted = %u\n",
0642 i, le16_to_cpu(desc->bg_free_inodes_count), x);
0643 bitmap_count += x;
0644 }
0645 brelse(bitmap_bh);
0646 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
0647 (unsigned long)
0648 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
0649 desc_count, bitmap_count);
0650 return desc_count;
0651 #else
0652 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
0653 desc = ext2_get_group_desc (sb, i, NULL);
0654 if (!desc)
0655 continue;
0656 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
0657 }
0658 return desc_count;
0659 #endif
0660 }
0661
0662
0663 unsigned long ext2_count_dirs (struct super_block * sb)
0664 {
0665 unsigned long count = 0;
0666 int i;
0667
0668 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
0669 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
0670 if (!gdp)
0671 continue;
0672 count += le16_to_cpu(gdp->bg_used_dirs_count);
0673 }
0674 return count;
0675 }
0676