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