ext4: sync up block and inode bitmap reading functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ext4 / ialloc.c
blob09cdcd5914d0a526941ca84ddb9b70fe71eb8b79
1 /*
2 * linux/fs/ext4/ialloc.c
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)
9 * BSD ufs-inspired inode and directory allocation by
10 * Stephen Tweedie (sct@redhat.com), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
15 #include <linux/time.h>
16 #include <linux/fs.h>
17 #include <linux/jbd2.h>
18 #include <linux/stat.h>
19 #include <linux/string.h>
20 #include <linux/quotaops.h>
21 #include <linux/buffer_head.h>
22 #include <linux/random.h>
23 #include <linux/bitops.h>
24 #include <linux/blkdev.h>
25 #include <asm/byteorder.h>
26 #include "ext4.h"
27 #include "ext4_jbd2.h"
28 #include "xattr.h"
29 #include "acl.h"
30 #include "group.h"
33 * ialloc.c contains the inodes allocation and deallocation routines
37 * The free inodes are managed by bitmaps. A file system contains several
38 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
39 * block for inodes, N blocks for the inode table and data blocks.
41 * The file system contains group descriptors which are located after the
42 * super block. Each descriptor contains the number of the bitmap block and
43 * the free blocks count in the block.
47 * To avoid calling the atomic setbit hundreds or thousands of times, we only
48 * need to use it within a single byte (to ensure we get endianness right).
49 * We can use memset for the rest of the bitmap as there are no other users.
51 void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
53 int i;
55 if (start_bit >= end_bit)
56 return;
58 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60 ext4_set_bit(i, bitmap);
61 if (i < end_bit)
62 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
65 /* Initializes an uninitialized inode bitmap */
66 unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67 ext4_group_t block_group,
68 struct ext4_group_desc *gdp)
70 struct ext4_sb_info *sbi = EXT4_SB(sb);
72 J_ASSERT_BH(bh, buffer_locked(bh));
74 /* If checksum is bad mark all blocks and inodes use to prevent
75 * allocation, essentially implementing a per-group read-only flag. */
76 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
77 ext4_error(sb, __func__, "Checksum bad for group %lu\n",
78 block_group);
79 gdp->bg_free_blocks_count = 0;
80 gdp->bg_free_inodes_count = 0;
81 gdp->bg_itable_unused = 0;
82 memset(bh->b_data, 0xff, sb->s_blocksize);
83 return 0;
86 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
88 bh->b_data);
90 return EXT4_INODES_PER_GROUP(sb);
94 * Read the inode allocation bitmap for a given block_group, reading
95 * into the specified slot in the superblock's bitmap cache.
97 * Return buffer_head of bitmap on success or NULL.
99 static struct buffer_head *
100 ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
102 struct ext4_group_desc *desc;
103 struct buffer_head *bh = NULL;
104 ext4_fsblk_t bitmap_blk;
106 desc = ext4_get_group_desc(sb, block_group, NULL);
107 if (!desc)
108 return NULL;
109 bitmap_blk = ext4_inode_bitmap(sb, desc);
110 bh = sb_getblk(sb, bitmap_blk);
111 if (unlikely(!bh)) {
112 ext4_error(sb, __func__,
113 "Cannot read inode bitmap - "
114 "block_group = %lu, inode_bitmap = %llu",
115 block_group, bitmap_blk);
116 return NULL;
118 if (bh_uptodate_or_lock(bh))
119 return bh;
121 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
122 ext4_init_inode_bitmap(sb, bh, block_group, desc);
123 set_buffer_uptodate(bh);
124 unlock_buffer(bh);
125 return bh;
127 if (bh_submit_read(bh) < 0) {
128 put_bh(bh);
129 ext4_error(sb, __func__,
130 "Cannot read inode bitmap - "
131 "block_group = %lu, inode_bitmap = %llu",
132 block_group, bitmap_blk);
133 return NULL;
135 return bh;
139 * NOTE! When we get the inode, we're the only people
140 * that have access to it, and as such there are no
141 * race conditions we have to worry about. The inode
142 * is not on the hash-lists, and it cannot be reached
143 * through the filesystem because the directory entry
144 * has been deleted earlier.
146 * HOWEVER: we must make sure that we get no aliases,
147 * which means that we have to call "clear_inode()"
148 * _before_ we mark the inode not in use in the inode
149 * bitmaps. Otherwise a newly created file might use
150 * the same inode number (not actually the same pointer
151 * though), and then we'd have two inodes sharing the
152 * same inode number and space on the harddisk.
154 void ext4_free_inode (handle_t *handle, struct inode * inode)
156 struct super_block * sb = inode->i_sb;
157 int is_directory;
158 unsigned long ino;
159 struct buffer_head *bitmap_bh = NULL;
160 struct buffer_head *bh2;
161 ext4_group_t block_group;
162 unsigned long bit;
163 struct ext4_group_desc * gdp;
164 struct ext4_super_block * es;
165 struct ext4_sb_info *sbi;
166 int fatal = 0, err;
167 ext4_group_t flex_group;
169 if (atomic_read(&inode->i_count) > 1) {
170 printk ("ext4_free_inode: inode has count=%d\n",
171 atomic_read(&inode->i_count));
172 return;
174 if (inode->i_nlink) {
175 printk ("ext4_free_inode: inode has nlink=%d\n",
176 inode->i_nlink);
177 return;
179 if (!sb) {
180 printk("ext4_free_inode: inode on nonexistent device\n");
181 return;
183 sbi = EXT4_SB(sb);
185 ino = inode->i_ino;
186 ext4_debug ("freeing inode %lu\n", ino);
189 * Note: we must free any quota before locking the superblock,
190 * as writing the quota to disk may need the lock as well.
192 DQUOT_INIT(inode);
193 ext4_xattr_delete_inode(handle, inode);
194 DQUOT_FREE_INODE(inode);
195 DQUOT_DROP(inode);
197 is_directory = S_ISDIR(inode->i_mode);
199 /* Do this BEFORE marking the inode not in use or returning an error */
200 clear_inode (inode);
202 es = EXT4_SB(sb)->s_es;
203 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
204 ext4_error (sb, "ext4_free_inode",
205 "reserved or nonexistent inode %lu", ino);
206 goto error_return;
208 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
209 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
210 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
211 if (!bitmap_bh)
212 goto error_return;
214 BUFFER_TRACE(bitmap_bh, "get_write_access");
215 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
216 if (fatal)
217 goto error_return;
219 /* Ok, now we can actually update the inode bitmaps.. */
220 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
221 bit, bitmap_bh->b_data))
222 ext4_error (sb, "ext4_free_inode",
223 "bit already cleared for inode %lu", ino);
224 else {
225 gdp = ext4_get_group_desc (sb, block_group, &bh2);
227 BUFFER_TRACE(bh2, "get_write_access");
228 fatal = ext4_journal_get_write_access(handle, bh2);
229 if (fatal) goto error_return;
231 if (gdp) {
232 spin_lock(sb_bgl_lock(sbi, block_group));
233 le16_add_cpu(&gdp->bg_free_inodes_count, 1);
234 if (is_directory)
235 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
236 gdp->bg_checksum = ext4_group_desc_csum(sbi,
237 block_group, gdp);
238 spin_unlock(sb_bgl_lock(sbi, block_group));
239 percpu_counter_inc(&sbi->s_freeinodes_counter);
240 if (is_directory)
241 percpu_counter_dec(&sbi->s_dirs_counter);
243 if (sbi->s_log_groups_per_flex) {
244 flex_group = ext4_flex_group(sbi, block_group);
245 spin_lock(sb_bgl_lock(sbi, flex_group));
246 sbi->s_flex_groups[flex_group].free_inodes++;
247 spin_unlock(sb_bgl_lock(sbi, flex_group));
250 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
251 err = ext4_journal_dirty_metadata(handle, bh2);
252 if (!fatal) fatal = err;
254 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
255 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
256 if (!fatal)
257 fatal = err;
258 sb->s_dirt = 1;
259 error_return:
260 brelse(bitmap_bh);
261 ext4_std_error(sb, fatal);
265 * There are two policies for allocating an inode. If the new inode is
266 * a directory, then a forward search is made for a block group with both
267 * free space and a low directory-to-inode ratio; if that fails, then of
268 * the groups with above-average free space, that group with the fewest
269 * directories already is chosen.
271 * For other inodes, search forward from the parent directory\'s block
272 * group to find a free inode.
274 static int find_group_dir(struct super_block *sb, struct inode *parent,
275 ext4_group_t *best_group)
277 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
278 unsigned int freei, avefreei;
279 struct ext4_group_desc *desc, *best_desc = NULL;
280 ext4_group_t group;
281 int ret = -1;
283 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
284 avefreei = freei / ngroups;
286 for (group = 0; group < ngroups; group++) {
287 desc = ext4_get_group_desc (sb, group, NULL);
288 if (!desc || !desc->bg_free_inodes_count)
289 continue;
290 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
291 continue;
292 if (!best_desc ||
293 (le16_to_cpu(desc->bg_free_blocks_count) >
294 le16_to_cpu(best_desc->bg_free_blocks_count))) {
295 *best_group = group;
296 best_desc = desc;
297 ret = 0;
300 return ret;
303 #define free_block_ratio 10
305 static int find_group_flex(struct super_block *sb, struct inode *parent,
306 ext4_group_t *best_group)
308 struct ext4_sb_info *sbi = EXT4_SB(sb);
309 struct ext4_group_desc *desc;
310 struct buffer_head *bh;
311 struct flex_groups *flex_group = sbi->s_flex_groups;
312 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
313 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
314 ext4_group_t ngroups = sbi->s_groups_count;
315 int flex_size = ext4_flex_bg_size(sbi);
316 ext4_group_t best_flex = parent_fbg_group;
317 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
318 int flexbg_free_blocks;
319 int flex_freeb_ratio;
320 ext4_group_t n_fbg_groups;
321 ext4_group_t i;
323 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
324 sbi->s_log_groups_per_flex;
326 find_close_to_parent:
327 flexbg_free_blocks = flex_group[best_flex].free_blocks;
328 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
329 if (flex_group[best_flex].free_inodes &&
330 flex_freeb_ratio > free_block_ratio)
331 goto found_flexbg;
333 if (best_flex && best_flex == parent_fbg_group) {
334 best_flex--;
335 goto find_close_to_parent;
338 for (i = 0; i < n_fbg_groups; i++) {
339 if (i == parent_fbg_group || i == parent_fbg_group - 1)
340 continue;
342 flexbg_free_blocks = flex_group[i].free_blocks;
343 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
345 if (flex_freeb_ratio > free_block_ratio &&
346 flex_group[i].free_inodes) {
347 best_flex = i;
348 goto found_flexbg;
351 if (best_flex < 0 ||
352 (flex_group[i].free_blocks >
353 flex_group[best_flex].free_blocks &&
354 flex_group[i].free_inodes))
355 best_flex = i;
358 if (!flex_group[best_flex].free_inodes ||
359 !flex_group[best_flex].free_blocks)
360 return -1;
362 found_flexbg:
363 for (i = best_flex * flex_size; i < ngroups &&
364 i < (best_flex + 1) * flex_size; i++) {
365 desc = ext4_get_group_desc(sb, i, &bh);
366 if (le16_to_cpu(desc->bg_free_inodes_count)) {
367 *best_group = i;
368 goto out;
372 return -1;
373 out:
374 return 0;
378 * Orlov's allocator for directories.
380 * We always try to spread first-level directories.
382 * If there are blockgroups with both free inodes and free blocks counts
383 * not worse than average we return one with smallest directory count.
384 * Otherwise we simply return a random group.
386 * For the rest rules look so:
388 * It's OK to put directory into a group unless
389 * it has too many directories already (max_dirs) or
390 * it has too few free inodes left (min_inodes) or
391 * it has too few free blocks left (min_blocks) or
392 * it's already running too large debt (max_debt).
393 * Parent's group is preferred, if it doesn't satisfy these
394 * conditions we search cyclically through the rest. If none
395 * of the groups look good we just look for a group with more
396 * free inodes than average (starting at parent's group).
398 * Debt is incremented each time we allocate a directory and decremented
399 * when we allocate an inode, within 0--255.
402 #define INODE_COST 64
403 #define BLOCK_COST 256
405 static int find_group_orlov(struct super_block *sb, struct inode *parent,
406 ext4_group_t *group)
408 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
409 struct ext4_sb_info *sbi = EXT4_SB(sb);
410 struct ext4_super_block *es = sbi->s_es;
411 ext4_group_t ngroups = sbi->s_groups_count;
412 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
413 unsigned int freei, avefreei;
414 ext4_fsblk_t freeb, avefreeb;
415 ext4_fsblk_t blocks_per_dir;
416 unsigned int ndirs;
417 int max_debt, max_dirs, min_inodes;
418 ext4_grpblk_t min_blocks;
419 ext4_group_t i;
420 struct ext4_group_desc *desc;
422 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
423 avefreei = freei / ngroups;
424 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
425 avefreeb = freeb;
426 do_div(avefreeb, ngroups);
427 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
429 if ((parent == sb->s_root->d_inode) ||
430 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
431 int best_ndir = inodes_per_group;
432 ext4_group_t grp;
433 int ret = -1;
435 get_random_bytes(&grp, sizeof(grp));
436 parent_group = (unsigned)grp % ngroups;
437 for (i = 0; i < ngroups; i++) {
438 grp = (parent_group + i) % ngroups;
439 desc = ext4_get_group_desc(sb, grp, NULL);
440 if (!desc || !desc->bg_free_inodes_count)
441 continue;
442 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
443 continue;
444 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
445 continue;
446 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
447 continue;
448 *group = grp;
449 ret = 0;
450 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
452 if (ret == 0)
453 return ret;
454 goto fallback;
457 blocks_per_dir = ext4_blocks_count(es) - freeb;
458 do_div(blocks_per_dir, ndirs);
460 max_dirs = ndirs / ngroups + inodes_per_group / 16;
461 min_inodes = avefreei - inodes_per_group / 4;
462 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
464 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
465 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
466 if (max_debt * INODE_COST > inodes_per_group)
467 max_debt = inodes_per_group / INODE_COST;
468 if (max_debt > 255)
469 max_debt = 255;
470 if (max_debt == 0)
471 max_debt = 1;
473 for (i = 0; i < ngroups; i++) {
474 *group = (parent_group + i) % ngroups;
475 desc = ext4_get_group_desc(sb, *group, NULL);
476 if (!desc || !desc->bg_free_inodes_count)
477 continue;
478 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
479 continue;
480 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
481 continue;
482 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
483 continue;
484 return 0;
487 fallback:
488 for (i = 0; i < ngroups; i++) {
489 *group = (parent_group + i) % ngroups;
490 desc = ext4_get_group_desc(sb, *group, NULL);
491 if (desc && desc->bg_free_inodes_count &&
492 le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
493 return 0;
496 if (avefreei) {
498 * The free-inodes counter is approximate, and for really small
499 * filesystems the above test can fail to find any blockgroups
501 avefreei = 0;
502 goto fallback;
505 return -1;
508 static int find_group_other(struct super_block *sb, struct inode *parent,
509 ext4_group_t *group)
511 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
512 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
513 struct ext4_group_desc *desc;
514 ext4_group_t i;
517 * Try to place the inode in its parent directory
519 *group = parent_group;
520 desc = ext4_get_group_desc(sb, *group, NULL);
521 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
522 le16_to_cpu(desc->bg_free_blocks_count))
523 return 0;
526 * We're going to place this inode in a different blockgroup from its
527 * parent. We want to cause files in a common directory to all land in
528 * the same blockgroup. But we want files which are in a different
529 * directory which shares a blockgroup with our parent to land in a
530 * different blockgroup.
532 * So add our directory's i_ino into the starting point for the hash.
534 *group = (*group + parent->i_ino) % ngroups;
537 * Use a quadratic hash to find a group with a free inode and some free
538 * blocks.
540 for (i = 1; i < ngroups; i <<= 1) {
541 *group += i;
542 if (*group >= ngroups)
543 *group -= ngroups;
544 desc = ext4_get_group_desc(sb, *group, NULL);
545 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
546 le16_to_cpu(desc->bg_free_blocks_count))
547 return 0;
551 * That failed: try linear search for a free inode, even if that group
552 * has no free blocks.
554 *group = parent_group;
555 for (i = 0; i < ngroups; i++) {
556 if (++*group >= ngroups)
557 *group = 0;
558 desc = ext4_get_group_desc(sb, *group, NULL);
559 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
560 return 0;
563 return -1;
567 * There are two policies for allocating an inode. If the new inode is
568 * a directory, then a forward search is made for a block group with both
569 * free space and a low directory-to-inode ratio; if that fails, then of
570 * the groups with above-average free space, that group with the fewest
571 * directories already is chosen.
573 * For other inodes, search forward from the parent directory's block
574 * group to find a free inode.
576 struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
578 struct super_block *sb;
579 struct buffer_head *bitmap_bh = NULL;
580 struct buffer_head *bh2;
581 ext4_group_t group = 0;
582 unsigned long ino = 0;
583 struct inode * inode;
584 struct ext4_group_desc * gdp = NULL;
585 struct ext4_super_block * es;
586 struct ext4_inode_info *ei;
587 struct ext4_sb_info *sbi;
588 int ret2, err = 0;
589 struct inode *ret;
590 ext4_group_t i;
591 int free = 0;
592 ext4_group_t flex_group;
594 /* Cannot create files in a deleted directory */
595 if (!dir || !dir->i_nlink)
596 return ERR_PTR(-EPERM);
598 sb = dir->i_sb;
599 inode = new_inode(sb);
600 if (!inode)
601 return ERR_PTR(-ENOMEM);
602 ei = EXT4_I(inode);
604 sbi = EXT4_SB(sb);
605 es = sbi->s_es;
607 if (sbi->s_log_groups_per_flex) {
608 ret2 = find_group_flex(sb, dir, &group);
609 goto got_group;
612 if (S_ISDIR(mode)) {
613 if (test_opt (sb, OLDALLOC))
614 ret2 = find_group_dir(sb, dir, &group);
615 else
616 ret2 = find_group_orlov(sb, dir, &group);
617 } else
618 ret2 = find_group_other(sb, dir, &group);
620 got_group:
621 err = -ENOSPC;
622 if (ret2 == -1)
623 goto out;
625 for (i = 0; i < sbi->s_groups_count; i++) {
626 err = -EIO;
628 gdp = ext4_get_group_desc(sb, group, &bh2);
629 if (!gdp)
630 goto fail;
632 brelse(bitmap_bh);
633 bitmap_bh = ext4_read_inode_bitmap(sb, group);
634 if (!bitmap_bh)
635 goto fail;
637 ino = 0;
639 repeat_in_this_group:
640 ino = ext4_find_next_zero_bit((unsigned long *)
641 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
642 if (ino < EXT4_INODES_PER_GROUP(sb)) {
644 BUFFER_TRACE(bitmap_bh, "get_write_access");
645 err = ext4_journal_get_write_access(handle, bitmap_bh);
646 if (err)
647 goto fail;
649 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
650 ino, bitmap_bh->b_data)) {
651 /* we won it */
652 BUFFER_TRACE(bitmap_bh,
653 "call ext4_journal_dirty_metadata");
654 err = ext4_journal_dirty_metadata(handle,
655 bitmap_bh);
656 if (err)
657 goto fail;
658 goto got;
660 /* we lost it */
661 jbd2_journal_release_buffer(handle, bitmap_bh);
663 if (++ino < EXT4_INODES_PER_GROUP(sb))
664 goto repeat_in_this_group;
668 * This case is possible in concurrent environment. It is very
669 * rare. We cannot repeat the find_group_xxx() call because
670 * that will simply return the same blockgroup, because the
671 * group descriptor metadata has not yet been updated.
672 * So we just go onto the next blockgroup.
674 if (++group == sbi->s_groups_count)
675 group = 0;
677 err = -ENOSPC;
678 goto out;
680 got:
681 ino++;
682 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
683 ino > EXT4_INODES_PER_GROUP(sb)) {
684 ext4_error(sb, __func__,
685 "reserved inode or inode > inodes count - "
686 "block_group = %lu, inode=%lu", group,
687 ino + group * EXT4_INODES_PER_GROUP(sb));
688 err = -EIO;
689 goto fail;
692 BUFFER_TRACE(bh2, "get_write_access");
693 err = ext4_journal_get_write_access(handle, bh2);
694 if (err) goto fail;
696 /* We may have to initialize the block bitmap if it isn't already */
697 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
698 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
699 struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
701 BUFFER_TRACE(block_bh, "get block bitmap access");
702 err = ext4_journal_get_write_access(handle, block_bh);
703 if (err) {
704 brelse(block_bh);
705 goto fail;
708 free = 0;
709 spin_lock(sb_bgl_lock(sbi, group));
710 /* recheck and clear flag under lock if we still need to */
711 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
712 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
713 free = ext4_free_blocks_after_init(sb, group, gdp);
714 gdp->bg_free_blocks_count = cpu_to_le16(free);
716 spin_unlock(sb_bgl_lock(sbi, group));
718 /* Don't need to dirty bitmap block if we didn't change it */
719 if (free) {
720 BUFFER_TRACE(block_bh, "dirty block bitmap");
721 err = ext4_journal_dirty_metadata(handle, block_bh);
724 brelse(block_bh);
725 if (err)
726 goto fail;
729 spin_lock(sb_bgl_lock(sbi, group));
730 /* If we didn't allocate from within the initialized part of the inode
731 * table then we need to initialize up to this inode. */
732 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
733 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
734 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
736 /* When marking the block group with
737 * ~EXT4_BG_INODE_UNINIT we don't want to depend
738 * on the value of bg_itable_unsed even though
739 * mke2fs could have initialized the same for us.
740 * Instead we calculated the value below
743 free = 0;
744 } else {
745 free = EXT4_INODES_PER_GROUP(sb) -
746 le16_to_cpu(gdp->bg_itable_unused);
750 * Check the relative inode number against the last used
751 * relative inode number in this group. if it is greater
752 * we need to update the bg_itable_unused count
755 if (ino > free)
756 gdp->bg_itable_unused =
757 cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
760 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
761 if (S_ISDIR(mode)) {
762 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
764 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
765 spin_unlock(sb_bgl_lock(sbi, group));
766 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
767 err = ext4_journal_dirty_metadata(handle, bh2);
768 if (err) goto fail;
770 percpu_counter_dec(&sbi->s_freeinodes_counter);
771 if (S_ISDIR(mode))
772 percpu_counter_inc(&sbi->s_dirs_counter);
773 sb->s_dirt = 1;
775 if (sbi->s_log_groups_per_flex) {
776 flex_group = ext4_flex_group(sbi, group);
777 spin_lock(sb_bgl_lock(sbi, flex_group));
778 sbi->s_flex_groups[flex_group].free_inodes--;
779 spin_unlock(sb_bgl_lock(sbi, flex_group));
782 inode->i_uid = current->fsuid;
783 if (test_opt (sb, GRPID))
784 inode->i_gid = dir->i_gid;
785 else if (dir->i_mode & S_ISGID) {
786 inode->i_gid = dir->i_gid;
787 if (S_ISDIR(mode))
788 mode |= S_ISGID;
789 } else
790 inode->i_gid = current->fsgid;
791 inode->i_mode = mode;
793 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
794 /* This is the optimal IO size (for stat), not the fs block size */
795 inode->i_blocks = 0;
796 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
797 ext4_current_time(inode);
799 memset(ei->i_data, 0, sizeof(ei->i_data));
800 ei->i_dir_start_lookup = 0;
801 ei->i_disksize = 0;
804 * Don't inherit extent flag from directory. We set extent flag on
805 * newly created directory and file only if -o extent mount option is
806 * specified
808 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
809 if (S_ISLNK(mode))
810 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
811 /* dirsync only applies to directories */
812 if (!S_ISDIR(mode))
813 ei->i_flags &= ~EXT4_DIRSYNC_FL;
814 ei->i_file_acl = 0;
815 ei->i_dtime = 0;
816 ei->i_block_alloc_info = NULL;
817 ei->i_block_group = group;
819 ext4_set_inode_flags(inode);
820 if (IS_DIRSYNC(inode))
821 handle->h_sync = 1;
822 insert_inode_hash(inode);
823 spin_lock(&sbi->s_next_gen_lock);
824 inode->i_generation = sbi->s_next_generation++;
825 spin_unlock(&sbi->s_next_gen_lock);
827 ei->i_state = EXT4_STATE_NEW;
829 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
831 ret = inode;
832 if(DQUOT_ALLOC_INODE(inode)) {
833 err = -EDQUOT;
834 goto fail_drop;
837 err = ext4_init_acl(handle, inode, dir);
838 if (err)
839 goto fail_free_drop;
841 err = ext4_init_security(handle,inode, dir);
842 if (err)
843 goto fail_free_drop;
845 if (test_opt(sb, EXTENTS)) {
846 /* set extent flag only for directory, file and normal symlink*/
847 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
848 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
849 ext4_ext_tree_init(handle, inode);
853 err = ext4_mark_inode_dirty(handle, inode);
854 if (err) {
855 ext4_std_error(sb, err);
856 goto fail_free_drop;
859 ext4_debug("allocating inode %lu\n", inode->i_ino);
860 goto really_out;
861 fail:
862 ext4_std_error(sb, err);
863 out:
864 iput(inode);
865 ret = ERR_PTR(err);
866 really_out:
867 brelse(bitmap_bh);
868 return ret;
870 fail_free_drop:
871 DQUOT_FREE_INODE(inode);
873 fail_drop:
874 DQUOT_DROP(inode);
875 inode->i_flags |= S_NOQUOTA;
876 inode->i_nlink = 0;
877 iput(inode);
878 brelse(bitmap_bh);
879 return ERR_PTR(err);
882 /* Verify that we are loading a valid orphan from disk */
883 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
885 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
886 ext4_group_t block_group;
887 int bit;
888 struct buffer_head *bitmap_bh;
889 struct inode *inode = NULL;
890 long err = -EIO;
892 /* Error cases - e2fsck has already cleaned up for us */
893 if (ino > max_ino) {
894 ext4_warning(sb, __func__,
895 "bad orphan ino %lu! e2fsck was run?", ino);
896 goto error;
899 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
900 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
901 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
902 if (!bitmap_bh) {
903 ext4_warning(sb, __func__,
904 "inode bitmap error for orphan %lu", ino);
905 goto error;
908 /* Having the inode bit set should be a 100% indicator that this
909 * is a valid orphan (no e2fsck run on fs). Orphans also include
910 * inodes that were being truncated, so we can't check i_nlink==0.
912 if (!ext4_test_bit(bit, bitmap_bh->b_data))
913 goto bad_orphan;
915 inode = ext4_iget(sb, ino);
916 if (IS_ERR(inode))
917 goto iget_failed;
920 * If the orphans has i_nlinks > 0 then it should be able to be
921 * truncated, otherwise it won't be removed from the orphan list
922 * during processing and an infinite loop will result.
924 if (inode->i_nlink && !ext4_can_truncate(inode))
925 goto bad_orphan;
927 if (NEXT_ORPHAN(inode) > max_ino)
928 goto bad_orphan;
929 brelse(bitmap_bh);
930 return inode;
932 iget_failed:
933 err = PTR_ERR(inode);
934 inode = NULL;
935 bad_orphan:
936 ext4_warning(sb, __func__,
937 "bad orphan inode %lu! e2fsck was run?", ino);
938 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
939 bit, (unsigned long long)bitmap_bh->b_blocknr,
940 ext4_test_bit(bit, bitmap_bh->b_data));
941 printk(KERN_NOTICE "inode=%p\n", inode);
942 if (inode) {
943 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
944 is_bad_inode(inode));
945 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
946 NEXT_ORPHAN(inode));
947 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
948 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
949 /* Avoid freeing blocks if we got a bad deleted inode */
950 if (inode->i_nlink == 0)
951 inode->i_blocks = 0;
952 iput(inode);
954 brelse(bitmap_bh);
955 error:
956 return ERR_PTR(err);
959 unsigned long ext4_count_free_inodes (struct super_block * sb)
961 unsigned long desc_count;
962 struct ext4_group_desc *gdp;
963 ext4_group_t i;
964 #ifdef EXT4FS_DEBUG
965 struct ext4_super_block *es;
966 unsigned long bitmap_count, x;
967 struct buffer_head *bitmap_bh = NULL;
969 es = EXT4_SB(sb)->s_es;
970 desc_count = 0;
971 bitmap_count = 0;
972 gdp = NULL;
973 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
974 gdp = ext4_get_group_desc (sb, i, NULL);
975 if (!gdp)
976 continue;
977 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
978 brelse(bitmap_bh);
979 bitmap_bh = ext4_read_inode_bitmap(sb, i);
980 if (!bitmap_bh)
981 continue;
983 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
984 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
985 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
986 bitmap_count += x;
988 brelse(bitmap_bh);
989 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
990 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
991 return desc_count;
992 #else
993 desc_count = 0;
994 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
995 gdp = ext4_get_group_desc (sb, i, NULL);
996 if (!gdp)
997 continue;
998 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
999 cond_resched();
1001 return desc_count;
1002 #endif
1005 /* Called at mount-time, super-block is locked */
1006 unsigned long ext4_count_dirs (struct super_block * sb)
1008 unsigned long count = 0;
1009 ext4_group_t i;
1011 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1012 struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
1013 if (!gdp)
1014 continue;
1015 count += le16_to_cpu(gdp->bg_used_dirs_count);
1017 return count;