2 * linux/fs/ext2/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@dcs.ed.ac.uk), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
16 * ialloc.c contains the inodes allocation and deallocation routines
20 * The free inodes are managed by bitmaps. A file system contains several
21 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
22 * block for inodes, N blocks for the inode table and data blocks.
24 * The file system contains group descriptors which are located after the
25 * super block. Each descriptor contains the number of the bitmap block and
26 * the free blocks count in the block. The descriptors are loaded in memory
27 * when a file system is mounted (see ext2_read_super).
31 #include <linux/ext2_fs.h>
32 #include <linux/sched.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
35 #include <linux/locks.h>
36 #include <linux/quotaops.h>
38 #include <asm/bitops.h>
39 #include <asm/byteorder.h>
42 * Read the inode allocation bitmap for a given block_group, reading
43 * into the specified slot in the superblock's bitmap cache.
45 * Return >=0 on success or a -ve error code.
47 static int read_inode_bitmap (struct super_block
* sb
,
48 unsigned long block_group
,
49 unsigned int bitmap_nr
)
51 struct ext2_group_desc
* gdp
;
52 struct buffer_head
* bh
= NULL
;
55 gdp
= ext2_get_group_desc (sb
, block_group
, NULL
);
60 bh
= bread (sb
->s_dev
, le32_to_cpu(gdp
->bg_inode_bitmap
), sb
->s_blocksize
);
62 ext2_error (sb
, "read_inode_bitmap",
63 "Cannot read inode bitmap - "
64 "block_group = %lu, inode_bitmap = %lu",
65 block_group
, (unsigned long) gdp
->bg_inode_bitmap
);
69 * On IO error, just leave a zero in the superblock's block pointer for
70 * this group. The IO will be retried next time.
73 sb
->u
.ext2_sb
.s_inode_bitmap_number
[bitmap_nr
] = block_group
;
74 sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
] = bh
;
79 * load_inode_bitmap loads the inode bitmap for a blocks group
81 * It maintains a cache for the last bitmaps loaded. This cache is managed
82 * with a LRU algorithm.
85 * 1/ There is one cache per mounted file system.
86 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
87 * this function reads the bitmap without maintaining a LRU cache.
89 * Return the slot used to store the bitmap, or a -ve error code.
91 static int load_inode_bitmap (struct super_block
* sb
,
92 unsigned int block_group
)
95 unsigned long inode_bitmap_number
;
96 struct buffer_head
* inode_bitmap
;
98 if (block_group
>= sb
->u
.ext2_sb
.s_groups_count
)
99 ext2_panic (sb
, "load_inode_bitmap",
100 "block_group >= groups_count - "
101 "block_group = %d, groups_count = %lu",
102 block_group
, sb
->u
.ext2_sb
.s_groups_count
);
103 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
> 0 &&
104 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] == block_group
&&
105 sb
->u
.ext2_sb
.s_inode_bitmap
[0] != NULL
)
107 if (sb
->u
.ext2_sb
.s_groups_count
<= EXT2_MAX_GROUP_LOADED
) {
108 if (sb
->u
.ext2_sb
.s_inode_bitmap
[block_group
]) {
109 if (sb
->u
.ext2_sb
.s_inode_bitmap_number
[block_group
] != block_group
)
110 ext2_panic (sb
, "load_inode_bitmap",
111 "block_group != inode_bitmap_number");
115 retval
= read_inode_bitmap (sb
, block_group
,
123 for (i
= 0; i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
124 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] != block_group
;
127 if (i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
128 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] == block_group
) {
129 inode_bitmap_number
= sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
];
130 inode_bitmap
= sb
->u
.ext2_sb
.s_inode_bitmap
[i
];
131 for (j
= i
; j
> 0; j
--) {
132 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
133 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
134 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
135 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
137 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] = inode_bitmap_number
;
138 sb
->u
.ext2_sb
.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);
149 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
< EXT2_MAX_GROUP_LOADED
)
150 sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
++;
152 brelse (sb
->u
.ext2_sb
.s_inode_bitmap
[EXT2_MAX_GROUP_LOADED
- 1]);
153 for (j
= sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
- 1; j
> 0; j
--) {
154 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
155 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
156 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
157 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
159 retval
= read_inode_bitmap (sb
, block_group
, 0);
165 * NOTE! When we get the inode, we're the only people
166 * that have access to it, and as such there are no
167 * race conditions we have to worry about. The inode
168 * is not on the hash-lists, and it cannot be reached
169 * through the filesystem because the directory entry
170 * has been deleted earlier.
172 * HOWEVER: we must make sure that we get no aliases,
173 * which means that we have to call "clear_inode()"
174 * _before_ we mark the inode not in use in the inode
175 * bitmaps. Otherwise a newly created file might use
176 * the same inode number (not actually the same pointer
177 * though), and then we'd have two inodes sharing the
178 * same inode number and space on the harddisk.
180 void ext2_free_inode (struct inode
* inode
)
184 struct super_block
* sb
;
185 struct buffer_head
* bh
;
186 struct buffer_head
* bh2
;
187 unsigned long block_group
;
190 struct ext2_group_desc
* gdp
;
191 struct ext2_super_block
* es
;
196 printk ("ext2_free_inode: inode has no device\n");
199 if (inode
->i_count
> 1) {
200 printk ("ext2_free_inode: inode has count=%d\n", inode
->i_count
);
203 if (inode
->i_nlink
) {
204 printk ("ext2_free_inode: inode has nlink=%d\n",
209 printk("ext2_free_inode: inode on nonexistent device\n");
214 ext2_debug ("freeing inode %lu\n", ino
);
218 if (ino
< EXT2_FIRST_INO(sb
) ||
219 ino
> le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_inodes_count
)) {
220 ext2_error (sb
, "free_inode",
221 "reserved inode or nonexistent inode");
224 es
= sb
->u
.ext2_sb
.s_es
;
225 block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(sb
);
226 bit
= (ino
- 1) % EXT2_INODES_PER_GROUP(sb
);
227 bitmap_nr
= load_inode_bitmap (sb
, block_group
);
231 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
233 is_directory
= S_ISDIR(inode
->i_mode
);
235 /* Do this BEFORE marking the inode not in use */
236 DQUOT_FREE_INODE(sb
, inode
);
239 /* Ok, now we can actually update the inode bitmaps.. */
240 if (!ext2_clear_bit (bit
, bh
->b_data
))
241 ext2_warning (sb
, "ext2_free_inode",
242 "bit already cleared for inode %lu", ino
);
244 gdp
= ext2_get_group_desc (sb
, block_group
, &bh2
);
246 gdp
->bg_free_inodes_count
=
247 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) + 1);
249 gdp
->bg_used_dirs_count
=
250 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) - 1);
252 mark_buffer_dirty(bh2
, 1);
253 es
->s_free_inodes_count
=
254 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) + 1);
255 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
257 mark_buffer_dirty(bh
, 1);
258 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
259 ll_rw_block (WRITE
, 1, &bh
);
268 * This function increments the inode version number
270 * This may be used one day by the NFS server
272 static void inc_inode_version (struct inode
* inode
,
273 struct ext2_group_desc
*gdp
,
276 inode
->u
.ext2_i
.i_version
++;
277 mark_inode_dirty(inode
);
283 * There are two policies for allocating an inode. If the new inode is
284 * a directory, then a forward search is made for a block group with both
285 * free space and a low directory-to-inode ratio; if that fails, then of
286 * the groups with above-average free space, that group with the fewest
287 * directories already is chosen.
289 * For other inodes, search forward from the parent directory\'s block
290 * group to find a free inode.
292 struct inode
* ext2_new_inode (const struct inode
* dir
, int mode
, int * err
)
294 struct super_block
* sb
;
295 struct buffer_head
* bh
;
296 struct buffer_head
* bh2
;
298 struct inode
* inode
;
300 struct ext2_group_desc
* gdp
;
301 struct ext2_group_desc
* tmp
;
302 struct ext2_super_block
* es
;
304 /* Cannot create files in a deleted directory */
305 if (!dir
|| !dir
->i_nlink
) {
310 inode
= get_empty_inode ();
318 inode
->i_flags
= sb
->s_flags
;
320 es
= sb
->u
.ext2_sb
.s_es
;
326 avefreei
= le32_to_cpu(es
->s_free_inodes_count
) /
327 sb
->u
.ext2_sb
.s_groups_count
;
328 /* I am not yet convinced that this next bit is necessary.
329 i = dir->u.ext2_i.i_block_group;
330 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
331 tmp = ext2_get_group_desc (sb, i, &bh2);
333 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
334 le16_to_cpu(tmp->bg_free_inodes_count)) {
339 i = ++i % sb->u.ext2_sb.s_groups_count;
343 for (j
= 0; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
344 tmp
= ext2_get_group_desc (sb
, j
, &bh2
);
346 le16_to_cpu(tmp
->bg_free_inodes_count
) &&
347 le16_to_cpu(tmp
->bg_free_inodes_count
) >= avefreei
) {
349 (le16_to_cpu(tmp
->bg_free_blocks_count
) >
350 le16_to_cpu(gdp
->bg_free_blocks_count
))) {
361 * Try to place the inode in its parent directory
363 i
= dir
->u
.ext2_i
.i_block_group
;
364 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
365 if (tmp
&& le16_to_cpu(tmp
->bg_free_inodes_count
))
370 * Use a quadratic hash to find a group with a
373 for (j
= 1; j
< sb
->u
.ext2_sb
.s_groups_count
; j
<<= 1) {
375 if (i
>= sb
->u
.ext2_sb
.s_groups_count
)
376 i
-= sb
->u
.ext2_sb
.s_groups_count
;
377 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
379 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
387 * That failed: try linear search for a free inode
389 i
= dir
->u
.ext2_i
.i_block_group
+ 1;
390 for (j
= 2; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
391 if (++i
>= sb
->u
.ext2_sb
.s_groups_count
)
393 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
395 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
408 bitmap_nr
= load_inode_bitmap (sb
, i
);
416 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
417 if ((j
= ext2_find_first_zero_bit ((unsigned long *) bh
->b_data
,
418 EXT2_INODES_PER_GROUP(sb
))) <
419 EXT2_INODES_PER_GROUP(sb
)) {
420 if (ext2_set_bit (j
, bh
->b_data
)) {
421 ext2_warning (sb
, "ext2_new_inode",
422 "bit already set for inode %d", j
);
425 mark_buffer_dirty(bh
, 1);
426 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
427 ll_rw_block (WRITE
, 1, &bh
);
431 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != 0) {
432 ext2_error (sb
, "ext2_new_inode",
433 "Free inodes count corrupted in group %d",
441 j
+= i
* EXT2_INODES_PER_GROUP(sb
) + 1;
442 if (j
< EXT2_FIRST_INO(sb
) || j
> le32_to_cpu(es
->s_inodes_count
)) {
443 ext2_error (sb
, "ext2_new_inode",
444 "reserved inode or inode > inodes count - "
445 "block_group = %d,inode=%d", i
, j
);
450 gdp
->bg_free_inodes_count
=
451 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) - 1);
453 gdp
->bg_used_dirs_count
=
454 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) + 1);
455 mark_buffer_dirty(bh2
, 1);
456 es
->s_free_inodes_count
=
457 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) - 1);
458 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
460 inode
->i_mode
= mode
;
463 inode
->i_dev
= sb
->s_dev
;
464 inode
->i_uid
= current
->fsuid
;
465 if (test_opt (sb
, GRPID
))
466 inode
->i_gid
= dir
->i_gid
;
467 else if (dir
->i_mode
& S_ISGID
) {
468 inode
->i_gid
= dir
->i_gid
;
472 inode
->i_gid
= current
->fsgid
;
475 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
477 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
478 inode
->u
.ext2_i
.i_new_inode
= 1;
479 inode
->u
.ext2_i
.i_flags
= dir
->u
.ext2_i
.i_flags
;
481 inode
->u
.ext2_i
.i_flags
&= ~(EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
);
482 inode
->u
.ext2_i
.i_faddr
= 0;
483 inode
->u
.ext2_i
.i_frag_no
= 0;
484 inode
->u
.ext2_i
.i_frag_size
= 0;
485 inode
->u
.ext2_i
.i_file_acl
= 0;
486 inode
->u
.ext2_i
.i_dir_acl
= 0;
487 inode
->u
.ext2_i
.i_dtime
= 0;
488 inode
->u
.ext2_i
.i_block_group
= i
;
490 if (inode
->u
.ext2_i
.i_flags
& EXT2_SYNC_FL
)
491 inode
->i_flags
|= MS_SYNCHRONOUS
;
492 insert_inode_hash(inode
);
493 mark_inode_dirty(inode
);
494 inc_inode_version (inode
, gdp
, mode
);
497 if(DQUOT_ALLOC_INODE(sb
, inode
)) {
498 sb
->dq_op
->drop(inode
);
504 ext2_debug ("allocating inode %lu\n", inode
->i_ino
);
510 unsigned long ext2_count_free_inodes (struct super_block
* sb
)
513 struct ext2_super_block
* es
;
514 unsigned long desc_count
, bitmap_count
, x
;
516 struct ext2_group_desc
* gdp
;
520 es
= sb
->u
.ext2_sb
.s_es
;
524 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
525 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
528 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
529 bitmap_nr
= load_inode_bitmap (sb
, i
);
533 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
534 EXT2_INODES_PER_GROUP(sb
) / 8);
535 printk ("group %d: stored = %d, counted = %lu\n",
536 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
539 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
540 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
544 return le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_free_inodes_count
);
548 void ext2_check_inodes_bitmap (struct super_block
* sb
)
550 struct ext2_super_block
* es
;
551 unsigned long desc_count
, bitmap_count
, x
;
553 struct ext2_group_desc
* gdp
;
557 es
= sb
->u
.ext2_sb
.s_es
;
561 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
562 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
565 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
566 bitmap_nr
= load_inode_bitmap (sb
, i
);
570 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
571 EXT2_INODES_PER_GROUP(sb
) / 8);
572 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != x
)
573 ext2_error (sb
, "ext2_check_inodes_bitmap",
574 "Wrong free inodes count in group %d, "
575 "stored = %d, counted = %lu", i
,
576 le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
579 if (le32_to_cpu(es
->s_free_inodes_count
) != bitmap_count
)
580 ext2_error (sb
, "ext2_check_inodes_bitmap",
581 "Wrong free inodes count in super block, "
582 "stored = %lu, counted = %lu",
583 (unsigned long) le32_to_cpu(es
->s_free_inodes_count
),