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
15 #include <linux/config.h>
17 #include <linux/locks.h>
18 #include <linux/quotaops.h>
22 * ialloc.c contains the inodes allocation and deallocation routines
26 * The free inodes are managed by bitmaps. A file system contains several
27 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
28 * block for inodes, N blocks for the inode table and data blocks.
30 * The file system contains group descriptors which are located after the
31 * super block. Each descriptor contains the number of the bitmap block and
32 * the free blocks count in the block. The descriptors are loaded in memory
33 * when a file system is mounted (see ext2_read_super).
38 * Read the inode allocation bitmap for a given block_group, reading
39 * into the specified slot in the superblock's bitmap cache.
41 * Return >=0 on success or a -ve error code.
43 static int read_inode_bitmap (struct super_block
* sb
,
44 unsigned long block_group
,
45 unsigned int bitmap_nr
)
47 struct ext2_group_desc
* gdp
;
48 struct buffer_head
* bh
= NULL
;
51 gdp
= ext2_get_group_desc (sb
, block_group
, NULL
);
56 bh
= bread (sb
->s_dev
, le32_to_cpu(gdp
->bg_inode_bitmap
), sb
->s_blocksize
);
58 ext2_error (sb
, "read_inode_bitmap",
59 "Cannot read inode bitmap - "
60 "block_group = %lu, inode_bitmap = %lu",
61 block_group
, (unsigned long) gdp
->bg_inode_bitmap
);
65 * On IO error, just leave a zero in the superblock's block pointer for
66 * this group. The IO will be retried next time.
69 sb
->u
.ext2_sb
.s_inode_bitmap_number
[bitmap_nr
] = block_group
;
70 sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
] = bh
;
75 * load_inode_bitmap loads the inode bitmap for a blocks group
77 * It maintains a cache for the last bitmaps loaded. This cache is managed
78 * with a LRU algorithm.
81 * 1/ There is one cache per mounted file system.
82 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
83 * this function reads the bitmap without maintaining a LRU cache.
85 * Return the slot used to store the bitmap, or a -ve error code.
87 static int load_inode_bitmap (struct super_block
* sb
,
88 unsigned int block_group
)
91 unsigned long inode_bitmap_number
;
92 struct buffer_head
* inode_bitmap
;
94 if (block_group
>= sb
->u
.ext2_sb
.s_groups_count
)
95 ext2_panic (sb
, "load_inode_bitmap",
96 "block_group >= groups_count - "
97 "block_group = %d, groups_count = %lu",
98 block_group
, sb
->u
.ext2_sb
.s_groups_count
);
99 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
> 0 &&
100 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] == block_group
&&
101 sb
->u
.ext2_sb
.s_inode_bitmap
[0] != NULL
)
103 if (sb
->u
.ext2_sb
.s_groups_count
<= EXT2_MAX_GROUP_LOADED
) {
104 if (sb
->u
.ext2_sb
.s_inode_bitmap
[block_group
]) {
105 if (sb
->u
.ext2_sb
.s_inode_bitmap_number
[block_group
] != block_group
)
106 ext2_panic (sb
, "load_inode_bitmap",
107 "block_group != inode_bitmap_number");
111 retval
= read_inode_bitmap (sb
, block_group
,
119 for (i
= 0; i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
120 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] != block_group
;
123 if (i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
124 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] == block_group
) {
125 inode_bitmap_number
= sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
];
126 inode_bitmap
= sb
->u
.ext2_sb
.s_inode_bitmap
[i
];
127 for (j
= i
; j
> 0; j
--) {
128 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
129 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
130 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
131 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
133 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] = inode_bitmap_number
;
134 sb
->u
.ext2_sb
.s_inode_bitmap
[0] = inode_bitmap
;
137 * There's still one special case here --- if inode_bitmap == 0
138 * then our last attempt to read the bitmap failed and we have
139 * just ended up caching that failure. Try again to read it.
142 retval
= read_inode_bitmap (sb
, block_group
, 0);
145 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
< EXT2_MAX_GROUP_LOADED
)
146 sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
++;
148 brelse (sb
->u
.ext2_sb
.s_inode_bitmap
[EXT2_MAX_GROUP_LOADED
- 1]);
149 for (j
= sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
- 1; j
> 0; j
--) {
150 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
151 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
152 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
153 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
155 retval
= read_inode_bitmap (sb
, block_group
, 0);
161 * NOTE! When we get the inode, we're the only people
162 * that have access to it, and as such there are no
163 * race conditions we have to worry about. The inode
164 * is not on the hash-lists, and it cannot be reached
165 * through the filesystem because the directory entry
166 * has been deleted earlier.
168 * HOWEVER: we must make sure that we get no aliases,
169 * which means that we have to call "clear_inode()"
170 * _before_ we mark the inode not in use in the inode
171 * bitmaps. Otherwise a newly created file might use
172 * the same inode number (not actually the same pointer
173 * though), and then we'd have two inodes sharing the
174 * same inode number and space on the harddisk.
176 void ext2_free_inode (struct inode
* inode
)
178 struct super_block
* sb
= inode
->i_sb
;
181 struct buffer_head
* bh
;
182 struct buffer_head
* bh2
;
183 unsigned long block_group
;
186 struct ext2_group_desc
* gdp
;
187 struct ext2_super_block
* es
;
190 ext2_debug ("freeing inode %lu\n", ino
);
193 * Note: we must free any quota before locking the superblock,
194 * as writing the quota to disk may need the lock as well.
196 DQUOT_FREE_INODE(sb
, inode
);
200 es
= sb
->u
.ext2_sb
.s_es
;
201 if (ino
< EXT2_FIRST_INO(sb
) ||
202 ino
> le32_to_cpu(es
->s_inodes_count
)) {
203 ext2_error (sb
, "free_inode",
204 "reserved inode or nonexistent inode");
207 block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(sb
);
208 bit
= (ino
- 1) % EXT2_INODES_PER_GROUP(sb
);
209 bitmap_nr
= load_inode_bitmap (sb
, block_group
);
213 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
215 is_directory
= S_ISDIR(inode
->i_mode
);
217 /* Do this BEFORE marking the inode not in use */
220 /* Ok, now we can actually update the inode bitmaps.. */
221 if (!ext2_clear_bit (bit
, bh
->b_data
))
222 ext2_error (sb
, "ext2_free_inode",
223 "bit already cleared for inode %lu", ino
);
225 gdp
= ext2_get_group_desc (sb
, block_group
, &bh2
);
227 gdp
->bg_free_inodes_count
=
228 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) + 1);
230 gdp
->bg_used_dirs_count
=
231 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) - 1);
233 mark_buffer_dirty(bh2
, 1);
234 es
->s_free_inodes_count
=
235 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) + 1);
236 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
238 mark_buffer_dirty(bh
, 1);
239 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
240 ll_rw_block (WRITE
, 1, &bh
);
249 * There are two policies for allocating an inode. If the new inode is
250 * a directory, then a forward search is made for a block group with both
251 * free space and a low directory-to-inode ratio; if that fails, then of
252 * the groups with above-average free space, that group with the fewest
253 * directories already is chosen.
255 * For other inodes, search forward from the parent directory\'s block
256 * group to find a free inode.
258 struct inode
* ext2_new_inode (const struct inode
* dir
, int mode
, int * err
)
260 struct super_block
* sb
;
261 struct buffer_head
* bh
;
262 struct buffer_head
* bh2
;
264 struct inode
* inode
;
266 struct ext2_group_desc
* gdp
;
267 struct ext2_group_desc
* tmp
;
268 struct ext2_super_block
* es
;
270 /* Cannot create files in a deleted directory */
271 if (!dir
|| !dir
->i_nlink
) {
276 inode
= get_empty_inode ();
286 es
= sb
->u
.ext2_sb
.s_es
;
292 avefreei
= le32_to_cpu(es
->s_free_inodes_count
) /
293 sb
->u
.ext2_sb
.s_groups_count
;
294 /* I am not yet convinced that this next bit is necessary.
295 i = dir->u.ext2_i.i_block_group;
296 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
297 tmp = ext2_get_group_desc (sb, i, &bh2);
299 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
300 le16_to_cpu(tmp->bg_free_inodes_count)) {
305 i = ++i % sb->u.ext2_sb.s_groups_count;
309 for (j
= 0; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
310 tmp
= ext2_get_group_desc (sb
, j
, &bh2
);
312 le16_to_cpu(tmp
->bg_free_inodes_count
) &&
313 le16_to_cpu(tmp
->bg_free_inodes_count
) >= avefreei
) {
315 (le16_to_cpu(tmp
->bg_free_blocks_count
) >
316 le16_to_cpu(gdp
->bg_free_blocks_count
))) {
327 * Try to place the inode in its parent directory
329 i
= dir
->u
.ext2_i
.i_block_group
;
330 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
331 if (tmp
&& le16_to_cpu(tmp
->bg_free_inodes_count
))
336 * Use a quadratic hash to find a group with a
339 for (j
= 1; j
< sb
->u
.ext2_sb
.s_groups_count
; j
<<= 1) {
341 if (i
>= sb
->u
.ext2_sb
.s_groups_count
)
342 i
-= sb
->u
.ext2_sb
.s_groups_count
;
343 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
345 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
353 * That failed: try linear search for a free inode
355 i
= dir
->u
.ext2_i
.i_block_group
+ 1;
356 for (j
= 2; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
357 if (++i
>= sb
->u
.ext2_sb
.s_groups_count
)
359 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
361 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
374 bitmap_nr
= load_inode_bitmap (sb
, i
);
382 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
383 if ((j
= ext2_find_first_zero_bit ((unsigned long *) bh
->b_data
,
384 EXT2_INODES_PER_GROUP(sb
))) <
385 EXT2_INODES_PER_GROUP(sb
)) {
386 if (ext2_set_bit (j
, bh
->b_data
)) {
387 ext2_error (sb
, "ext2_new_inode",
388 "bit already set for inode %d", j
);
391 mark_buffer_dirty(bh
, 1);
392 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
393 ll_rw_block (WRITE
, 1, &bh
);
397 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != 0) {
398 ext2_error (sb
, "ext2_new_inode",
399 "Free inodes count corrupted in group %d",
407 j
+= i
* EXT2_INODES_PER_GROUP(sb
) + 1;
408 if (j
< EXT2_FIRST_INO(sb
) || j
> le32_to_cpu(es
->s_inodes_count
)) {
409 ext2_error (sb
, "ext2_new_inode",
410 "reserved inode or inode > inodes count - "
411 "block_group = %d,inode=%d", i
, j
);
416 gdp
->bg_free_inodes_count
=
417 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) - 1);
419 gdp
->bg_used_dirs_count
=
420 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) + 1);
421 mark_buffer_dirty(bh2
, 1);
422 es
->s_free_inodes_count
=
423 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) - 1);
424 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
426 inode
->i_mode
= mode
;
429 inode
->i_dev
= sb
->s_dev
;
430 inode
->i_uid
= current
->fsuid
;
431 if (test_opt (sb
, GRPID
))
432 inode
->i_gid
= dir
->i_gid
;
433 else if (dir
->i_mode
& S_ISGID
) {
434 inode
->i_gid
= dir
->i_gid
;
438 inode
->i_gid
= current
->fsgid
;
441 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
443 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
444 inode
->u
.ext2_i
.i_new_inode
= 1;
445 inode
->u
.ext2_i
.i_flags
= dir
->u
.ext2_i
.i_flags
;
447 inode
->u
.ext2_i
.i_flags
&= ~(EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
);
448 inode
->u
.ext2_i
.i_faddr
= 0;
449 inode
->u
.ext2_i
.i_frag_no
= 0;
450 inode
->u
.ext2_i
.i_frag_size
= 0;
451 inode
->u
.ext2_i
.i_file_acl
= 0;
452 inode
->u
.ext2_i
.i_dir_acl
= 0;
453 inode
->u
.ext2_i
.i_dtime
= 0;
454 inode
->u
.ext2_i
.i_block_group
= i
;
455 if (inode
->u
.ext2_i
.i_flags
& EXT2_SYNC_FL
)
456 inode
->i_flags
|= MS_SYNCHRONOUS
;
457 insert_inode_hash(inode
);
458 inode
->i_generation
= event
++;
459 mark_inode_dirty(inode
);
462 if(DQUOT_ALLOC_INODE(sb
, inode
)) {
463 sb
->dq_op
->drop(inode
);
469 ext2_debug ("allocating inode %lu\n", inode
->i_ino
);
475 unsigned long ext2_count_free_inodes (struct super_block
* sb
)
478 struct ext2_super_block
* es
;
479 unsigned long desc_count
, bitmap_count
, x
;
481 struct ext2_group_desc
* gdp
;
485 es
= sb
->u
.ext2_sb
.s_es
;
489 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
490 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
493 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
494 bitmap_nr
= load_inode_bitmap (sb
, i
);
498 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
499 EXT2_INODES_PER_GROUP(sb
) / 8);
500 printk ("group %d: stored = %d, counted = %lu\n",
501 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
504 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
505 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
509 return le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_free_inodes_count
);
513 #ifdef CONFIG_EXT2_CHECK
514 /* Called at mount-time, super-block is locked */
515 void ext2_check_inodes_bitmap (struct super_block
* sb
)
517 struct ext2_super_block
* es
;
518 unsigned long desc_count
, bitmap_count
, x
;
520 struct ext2_group_desc
* gdp
;
523 es
= sb
->u
.ext2_sb
.s_es
;
527 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
528 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
531 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
532 bitmap_nr
= load_inode_bitmap (sb
, i
);
536 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
537 EXT2_INODES_PER_GROUP(sb
) / 8);
538 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != x
)
539 ext2_error (sb
, "ext2_check_inodes_bitmap",
540 "Wrong free inodes count in group %d, "
541 "stored = %d, counted = %lu", i
,
542 le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
545 if (le32_to_cpu(es
->s_free_inodes_count
) != bitmap_count
)
546 ext2_error (sb
, "ext2_check_inodes_bitmap",
547 "Wrong free inodes count in super block, "
548 "stored = %lu, counted = %lu",
549 (unsigned long) le32_to_cpu(es
->s_free_inodes_count
),