2 * linux/fs/ext3/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/sched.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/stat.h>
21 #include <linux/string.h>
22 #include <linux/locks.h>
23 #include <linux/quotaops.h>
25 #include <asm/bitops.h>
26 #include <asm/byteorder.h>
29 * ialloc.c contains the inodes allocation and deallocation routines
33 * The free inodes are managed by bitmaps. A file system contains several
34 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
35 * block for inodes, N blocks for the inode table and data blocks.
37 * The file system contains group descriptors which are located after the
38 * super block. Each descriptor contains the number of the bitmap block and
39 * the free blocks count in the block. The descriptors are loaded in memory
40 * when a file system is mounted (see ext3_read_super).
45 * Read the inode allocation bitmap for a given block_group, reading
46 * into the specified slot in the superblock's bitmap cache.
48 * Return >=0 on success or a -ve error code.
50 static int read_inode_bitmap (struct super_block * sb,
51 unsigned long block_group,
52 unsigned int bitmap_nr)
54 struct ext3_group_desc * gdp;
55 struct buffer_head * bh = NULL;
58 gdp = ext3_get_group_desc (sb, block_group, NULL);
63 bh = sb_bread(sb, le32_to_cpu(gdp->bg_inode_bitmap));
65 ext3_error (sb, "read_inode_bitmap",
66 "Cannot read inode bitmap - "
67 "block_group = %lu, inode_bitmap = %u",
68 block_group, gdp->bg_inode_bitmap);
72 * On IO error, just leave a zero in the superblock's block pointer for
73 * this group. The IO will be retried next time.
76 sb->u.ext3_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
77 sb->u.ext3_sb.s_inode_bitmap[bitmap_nr] = bh;
82 * load_inode_bitmap loads the inode bitmap for a blocks group
84 * It maintains a cache for the last bitmaps loaded. This cache is managed
85 * with a LRU algorithm.
88 * 1/ There is one cache per mounted file system.
89 * 2/ If the file system contains less than EXT3_MAX_GROUP_LOADED groups,
90 * this function reads the bitmap without maintaining a LRU cache.
92 * Return the slot used to store the bitmap, or a -ve error code.
94 static int load_inode_bitmap (struct super_block * sb,
95 unsigned int block_group)
97 struct ext3_sb_info *sbi = EXT3_SB(sb);
98 unsigned long inode_bitmap_number;
99 struct buffer_head * inode_bitmap;
100 int i, j, retval = 0;
102 if (block_group >= sbi->s_groups_count)
103 ext3_panic (sb, "load_inode_bitmap",
104 "block_group >= groups_count - "
105 "block_group = %d, groups_count = %lu",
106 block_group, sbi->s_groups_count);
107 if (sbi->s_loaded_inode_bitmaps > 0 &&
108 sbi->s_inode_bitmap_number[0] == block_group &&
109 sbi->s_inode_bitmap[0] != NULL)
111 if (sbi->s_groups_count <= EXT3_MAX_GROUP_LOADED) {
112 if (sbi->s_inode_bitmap[block_group]) {
113 if (sbi->s_inode_bitmap_number[block_group] !=
115 ext3_panic(sb, "load_inode_bitmap",
116 "block_group != inode_bitmap_number");
119 retval = read_inode_bitmap(sb, block_group, block_group);
125 for (i = 0; i < sbi->s_loaded_inode_bitmaps &&
126 sbi->s_inode_bitmap_number[i] != block_group; i++)
128 if (i < sbi->s_loaded_inode_bitmaps &&
129 sbi->s_inode_bitmap_number[i] == block_group) {
130 inode_bitmap_number = sbi->s_inode_bitmap_number[i];
131 inode_bitmap = sbi->s_inode_bitmap[i];
132 for (j = i; j > 0; j--) {
133 sbi->s_inode_bitmap_number[j] =
134 sbi->s_inode_bitmap_number[j - 1];
135 sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
137 sbi->s_inode_bitmap_number[0] = inode_bitmap_number;
138 sbi->s_inode_bitmap[0] = inode_bitmap;
141 * There's still one special case here --- if inode_bitmap == 0
142 * then our last attempt to read the bitmap failed and we have
143 * just ended up caching that failure. Try again to read it.
146 retval = read_inode_bitmap (sb, block_group, 0);
148 if (sbi->s_loaded_inode_bitmaps < EXT3_MAX_GROUP_LOADED)
149 sbi->s_loaded_inode_bitmaps++;
151 brelse(sbi->s_inode_bitmap[EXT3_MAX_GROUP_LOADED - 1]);
152 for (j = sbi->s_loaded_inode_bitmaps - 1; j > 0; j--) {
153 sbi->s_inode_bitmap_number[j] =
154 sbi->s_inode_bitmap_number[j - 1];
155 sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
157 retval = read_inode_bitmap (sb, block_group, 0);
163 * NOTE! When we get the inode, we're the only people
164 * that have access to it, and as such there are no
165 * race conditions we have to worry about. The inode
166 * is not on the hash-lists, and it cannot be reached
167 * through the filesystem because the directory entry
168 * has been deleted earlier.
170 * HOWEVER: we must make sure that we get no aliases,
171 * which means that we have to call "clear_inode()"
172 * _before_ we mark the inode not in use in the inode
173 * bitmaps. Otherwise a newly created file might use
174 * the same inode number (not actually the same pointer
175 * though), and then we'd have two inodes sharing the
176 * same inode number and space on the harddisk.
178 void ext3_free_inode (handle_t *handle, struct inode * inode)
180 struct super_block * sb = inode->i_sb;
183 struct buffer_head * bh;
184 struct buffer_head * bh2;
185 unsigned long block_group;
188 struct ext3_group_desc * gdp;
189 struct ext3_super_block * es;
193 printk ("ext3_free_inode: inode has no device\n");
196 if (atomic_read(&inode->i_count) > 1) {
197 printk ("ext3_free_inode: inode has count=%d\n",
198 atomic_read(&inode->i_count));
201 if (inode->i_nlink) {
202 printk ("ext3_free_inode: inode has nlink=%d\n",
207 printk("ext3_free_inode: inode on nonexistent device\n");
212 ext3_debug ("freeing inode %lu\n", ino);
215 * Note: we must free any quota before locking the superblock,
216 * as writing the quota to disk may need the lock as well.
219 DQUOT_FREE_INODE(inode);
222 is_directory = S_ISDIR(inode->i_mode);
224 /* Do this BEFORE marking the inode not in use or returning an error */
228 es = sb->u.ext3_sb.s_es;
229 if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
230 ext3_error (sb, "ext3_free_inode",
231 "reserved or nonexistent inode %lu", ino);
234 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
235 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
236 bitmap_nr = load_inode_bitmap (sb, block_group);
240 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
242 BUFFER_TRACE(bh, "get_write_access");
243 fatal = ext3_journal_get_write_access(handle, bh);
247 /* Ok, now we can actually update the inode bitmaps.. */
248 if (!ext3_clear_bit (bit, bh->b_data))
249 ext3_error (sb, "ext3_free_inode",
250 "bit already cleared for inode %lu", ino);
252 gdp = ext3_get_group_desc (sb, block_group, &bh2);
254 BUFFER_TRACE(bh2, "get_write_access");
255 fatal = ext3_journal_get_write_access(handle, bh2);
256 if (fatal) goto error_return;
258 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get write access");
259 fatal = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
260 if (fatal) goto error_return;
263 gdp->bg_free_inodes_count = cpu_to_le16(
264 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
266 gdp->bg_used_dirs_count = cpu_to_le16(
267 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
269 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
270 err = ext3_journal_dirty_metadata(handle, bh2);
271 if (!fatal) fatal = err;
272 es->s_free_inodes_count =
273 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
274 BUFFER_TRACE(sb->u.ext3_sb.s_sbh,
275 "call ext3_journal_dirty_metadata");
276 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
277 if (!fatal) fatal = err;
279 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
280 err = ext3_journal_dirty_metadata(handle, bh);
285 ext3_std_error(sb, fatal);
290 * There are two policies for allocating an inode. If the new inode is
291 * a directory, then a forward search is made for a block group with both
292 * free space and a low directory-to-inode ratio; if that fails, then of
293 * the groups with above-average free space, that group with the fewest
294 * directories already is chosen.
296 * For other inodes, search forward from the parent directory's block
297 * group to find a free inode.
299 struct inode * ext3_new_inode (handle_t *handle,
300 const struct inode * dir, int mode)
302 struct super_block * sb;
303 struct buffer_head * bh;
304 struct buffer_head * bh2;
306 struct inode * inode;
308 struct ext3_group_desc * gdp;
309 struct ext3_group_desc * tmp;
310 struct ext3_super_block * es;
313 /* Cannot create files in a deleted directory */
314 if (!dir || !dir->i_nlink)
315 return ERR_PTR(-EPERM);
318 inode = new_inode(sb);
320 return ERR_PTR(-ENOMEM);
321 init_rwsem(&inode->u.ext3_i.truncate_sem);
324 es = sb->u.ext3_sb.s_es;
330 avefreei = le32_to_cpu(es->s_free_inodes_count) /
331 sb->u.ext3_sb.s_groups_count;
333 for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
334 struct buffer_head *temp_buffer;
335 tmp = ext3_get_group_desc (sb, j, &temp_buffer);
337 le16_to_cpu(tmp->bg_free_inodes_count) &&
338 le16_to_cpu(tmp->bg_free_inodes_count) >=
340 if (!gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
341 le16_to_cpu(gdp->bg_free_blocks_count))) {
351 * Try to place the inode in its parent directory
353 i = dir->u.ext3_i.i_block_group;
354 tmp = ext3_get_group_desc (sb, i, &bh2);
355 if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
360 * Use a quadratic hash to find a group with a
363 for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
365 if (i >= sb->u.ext3_sb.s_groups_count)
366 i -= sb->u.ext3_sb.s_groups_count;
367 tmp = ext3_get_group_desc (sb, i, &bh2);
369 le16_to_cpu(tmp->bg_free_inodes_count)) {
377 * That failed: try linear search for a free inode
379 i = dir->u.ext3_i.i_block_group + 1;
380 for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
381 if (++i >= sb->u.ext3_sb.s_groups_count)
383 tmp = ext3_get_group_desc (sb, i, &bh2);
385 le16_to_cpu(tmp->bg_free_inodes_count)) {
398 bitmap_nr = load_inode_bitmap (sb, i);
402 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
404 if ((j = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
405 EXT3_INODES_PER_GROUP(sb))) <
406 EXT3_INODES_PER_GROUP(sb)) {
407 BUFFER_TRACE(bh, "get_write_access");
408 err = ext3_journal_get_write_access(handle, bh);
411 if (ext3_set_bit (j, bh->b_data)) {
412 ext3_error (sb, "ext3_new_inode",
413 "bit already set for inode %d", j);
416 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
417 err = ext3_journal_dirty_metadata(handle, bh);
420 if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
421 ext3_error (sb, "ext3_new_inode",
422 "Free inodes count corrupted in group %d",
424 /* Is it really ENOSPC? */
426 if (sb->s_flags & MS_RDONLY)
429 BUFFER_TRACE(bh2, "get_write_access");
430 err = ext3_journal_get_write_access(handle, bh2);
432 gdp->bg_free_inodes_count = 0;
433 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
434 err = ext3_journal_dirty_metadata(handle, bh2);
439 j += i * EXT3_INODES_PER_GROUP(sb) + 1;
440 if (j < EXT3_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
441 ext3_error (sb, "ext3_new_inode",
442 "reserved inode or inode > inodes count - "
443 "block_group = %d,inode=%d", i, j);
448 BUFFER_TRACE(bh2, "get_write_access");
449 err = ext3_journal_get_write_access(handle, bh2);
451 gdp->bg_free_inodes_count =
452 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
454 gdp->bg_used_dirs_count =
455 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
456 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
457 err = ext3_journal_dirty_metadata(handle, bh2);
460 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
461 err = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
463 es->s_free_inodes_count =
464 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
465 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "call ext3_journal_dirty_metadata");
466 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
470 inode->i_uid = current->fsuid;
471 if (test_opt (sb, GRPID))
472 inode->i_gid = dir->i_gid;
473 else if (dir->i_mode & S_ISGID) {
474 inode->i_gid = dir->i_gid;
478 inode->i_gid = current->fsgid;
479 inode->i_mode = mode;
482 /* This is the optimal IO size (for stat), not the fs block size */
483 inode->i_blksize = PAGE_SIZE;
485 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
486 inode->u.ext3_i.i_flags = dir->u.ext3_i.i_flags & ~EXT3_INDEX_FL;
488 inode->u.ext3_i.i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
489 #ifdef EXT3_FRAGMENTS
490 inode->u.ext3_i.i_faddr = 0;
491 inode->u.ext3_i.i_frag_no = 0;
492 inode->u.ext3_i.i_frag_size = 0;
494 inode->u.ext3_i.i_file_acl = 0;
495 inode->u.ext3_i.i_dir_acl = 0;
496 inode->u.ext3_i.i_dtime = 0;
497 INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
498 #ifdef EXT3_PREALLOCATE
499 inode->u.ext3_i.i_prealloc_count = 0;
501 inode->u.ext3_i.i_block_group = i;
503 if (inode->u.ext3_i.i_flags & EXT3_SYNC_FL)
504 inode->i_flags |= S_SYNC;
507 insert_inode_hash(inode);
508 inode->i_generation = sb->u.ext3_sb.s_next_generation++;
510 inode->u.ext3_i.i_state = EXT3_STATE_NEW;
511 err = ext3_mark_inode_dirty(handle, inode);
515 if(DQUOT_ALLOC_INODE(inode)) {
517 inode->i_flags |= S_NOQUOTA;
520 return ERR_PTR(-EDQUOT);
522 ext3_debug ("allocating inode %lu\n", inode->i_ino);
526 ext3_std_error(sb, err);
533 /* Verify that we are loading a valid orphan from disk */
534 struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
536 unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
537 unsigned long block_group;
540 struct buffer_head *bh;
541 struct inode *inode = NULL;
543 /* Error cases - e2fsck has already cleaned up for us */
545 ext3_warning(sb, __FUNCTION__,
546 "bad orphan ino %lu! e2fsck was run?\n", ino);
550 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
551 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
552 if ((bitmap_nr = load_inode_bitmap(sb, block_group)) < 0 ||
553 !(bh = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr])) {
554 ext3_warning(sb, __FUNCTION__,
555 "inode bitmap error for orphan %lu\n", ino);
559 /* Having the inode bit set should be a 100% indicator that this
560 * is a valid orphan (no e2fsck run on fs). Orphans also include
561 * inodes that were being truncated, so we can't check i_nlink==0.
563 if (!ext3_test_bit(bit, bh->b_data) || !(inode = iget(sb, ino)) ||
564 is_bad_inode(inode) || NEXT_ORPHAN(inode) > max_ino) {
565 ext3_warning(sb, __FUNCTION__,
566 "bad orphan inode %lu! e2fsck was run?\n", ino);
567 printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%ld) = %d\n",
568 bit, bh->b_blocknr, ext3_test_bit(bit, bh->b_data));
569 printk(KERN_NOTICE "inode=%p\n", inode);
571 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
572 is_bad_inode(inode));
573 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
575 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
577 /* Avoid freeing blocks if we got a bad deleted inode */
578 if (inode && inode->i_nlink == 0)
587 unsigned long ext3_count_free_inodes (struct super_block * sb)
590 struct ext3_super_block * es;
591 unsigned long desc_count, bitmap_count, x;
593 struct ext3_group_desc * gdp;
597 es = sb->u.ext3_sb.s_es;
601 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
602 gdp = ext3_get_group_desc (sb, i, NULL);
605 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
606 bitmap_nr = load_inode_bitmap (sb, i);
610 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
611 EXT3_INODES_PER_GROUP(sb) / 8);
612 printk ("group %d: stored = %d, counted = %lu\n",
613 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
616 printk("ext3_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
617 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
621 return le32_to_cpu(sb->u.ext3_sb.s_es->s_free_inodes_count);
625 #ifdef CONFIG_EXT3_CHECK
626 /* Called at mount-time, super-block is locked */
627 void ext3_check_inodes_bitmap (struct super_block * sb)
629 struct ext3_super_block * es;
630 unsigned long desc_count, bitmap_count, x;
632 struct ext3_group_desc * gdp;
635 es = sb->u.ext3_sb.s_es;
639 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
640 gdp = ext3_get_group_desc (sb, i, NULL);
643 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
644 bitmap_nr = load_inode_bitmap (sb, i);
648 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
649 EXT3_INODES_PER_GROUP(sb) / 8);
650 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
651 ext3_error (sb, "ext3_check_inodes_bitmap",
652 "Wrong free inodes count in group %d, "
653 "stored = %d, counted = %lu", i,
654 le16_to_cpu(gdp->bg_free_inodes_count), x);
657 if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
658 ext3_error (sb, "ext3_check_inodes_bitmap",
659 "Wrong free inodes count in super block, "
660 "stored = %lu, counted = %lu",
661 (unsigned long)le32_to_cpu(es->s_free_inodes_count),