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/ext2_fs.h>
18 #include <linux/locks.h>
19 #include <linux/quotaops.h>
23 * ialloc.c contains the inodes allocation and deallocation routines
27 * The free inodes are managed by bitmaps. A file system contains several
28 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
29 * block for inodes, N blocks for the inode table and data blocks.
31 * The file system contains group descriptors which are located after the
32 * super block. Each descriptor contains the number of the bitmap block and
33 * the free blocks count in the block. The descriptors are loaded in memory
34 * when a file system is mounted (see ext2_read_super).
39 * Read the inode allocation bitmap for a given block_group, reading
40 * into the specified slot in the superblock's bitmap cache.
42 * Return >=0 on success or a -ve error code.
44 static int read_inode_bitmap (struct super_block
* sb
,
45 unsigned long block_group
,
46 unsigned int bitmap_nr
)
48 struct ext2_group_desc
* gdp
;
49 struct buffer_head
* bh
= NULL
;
52 gdp
= ext2_get_group_desc (sb
, block_group
, NULL
);
57 bh
= bread (sb
->s_dev
, le32_to_cpu(gdp
->bg_inode_bitmap
), sb
->s_blocksize
);
59 ext2_error (sb
, "read_inode_bitmap",
60 "Cannot read inode bitmap - "
61 "block_group = %lu, inode_bitmap = %lu",
62 block_group
, (unsigned long) gdp
->bg_inode_bitmap
);
66 * On IO error, just leave a zero in the superblock's block pointer for
67 * this group. The IO will be retried next time.
70 sb
->u
.ext2_sb
.s_inode_bitmap_number
[bitmap_nr
] = block_group
;
71 sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
] = bh
;
76 * load_inode_bitmap loads the inode bitmap for a blocks group
78 * It maintains a cache for the last bitmaps loaded. This cache is managed
79 * with a LRU algorithm.
82 * 1/ There is one cache per mounted file system.
83 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
84 * this function reads the bitmap without maintaining a LRU cache.
86 * Return the slot used to store the bitmap, or a -ve error code.
88 static int load_inode_bitmap (struct super_block
* sb
,
89 unsigned int block_group
)
92 unsigned long inode_bitmap_number
;
93 struct buffer_head
* inode_bitmap
;
95 if (block_group
>= sb
->u
.ext2_sb
.s_groups_count
)
96 ext2_panic (sb
, "load_inode_bitmap",
97 "block_group >= groups_count - "
98 "block_group = %d, groups_count = %lu",
99 block_group
, sb
->u
.ext2_sb
.s_groups_count
);
100 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
> 0 &&
101 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] == block_group
&&
102 sb
->u
.ext2_sb
.s_inode_bitmap
[0] != NULL
)
104 if (sb
->u
.ext2_sb
.s_groups_count
<= EXT2_MAX_GROUP_LOADED
) {
105 if (sb
->u
.ext2_sb
.s_inode_bitmap
[block_group
]) {
106 if (sb
->u
.ext2_sb
.s_inode_bitmap_number
[block_group
] != block_group
)
107 ext2_panic (sb
, "load_inode_bitmap",
108 "block_group != inode_bitmap_number");
112 retval
= read_inode_bitmap (sb
, block_group
,
120 for (i
= 0; i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
121 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] != block_group
;
124 if (i
< sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
&&
125 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] == block_group
) {
126 inode_bitmap_number
= sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
];
127 inode_bitmap
= sb
->u
.ext2_sb
.s_inode_bitmap
[i
];
128 for (j
= i
; j
> 0; j
--) {
129 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
130 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
131 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
132 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
134 sb
->u
.ext2_sb
.s_inode_bitmap_number
[0] = inode_bitmap_number
;
135 sb
->u
.ext2_sb
.s_inode_bitmap
[0] = inode_bitmap
;
138 * There's still one special case here --- if inode_bitmap == 0
139 * then our last attempt to read the bitmap failed and we have
140 * just ended up caching that failure. Try again to read it.
143 retval
= read_inode_bitmap (sb
, block_group
, 0);
146 if (sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
< EXT2_MAX_GROUP_LOADED
)
147 sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
++;
149 brelse (sb
->u
.ext2_sb
.s_inode_bitmap
[EXT2_MAX_GROUP_LOADED
- 1]);
150 for (j
= sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
- 1; j
> 0; j
--) {
151 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
] =
152 sb
->u
.ext2_sb
.s_inode_bitmap_number
[j
- 1];
153 sb
->u
.ext2_sb
.s_inode_bitmap
[j
] =
154 sb
->u
.ext2_sb
.s_inode_bitmap
[j
- 1];
156 retval
= read_inode_bitmap (sb
, block_group
, 0);
162 * NOTE! When we get the inode, we're the only people
163 * that have access to it, and as such there are no
164 * race conditions we have to worry about. The inode
165 * is not on the hash-lists, and it cannot be reached
166 * through the filesystem because the directory entry
167 * has been deleted earlier.
169 * HOWEVER: we must make sure that we get no aliases,
170 * which means that we have to call "clear_inode()"
171 * _before_ we mark the inode not in use in the inode
172 * bitmaps. Otherwise a newly created file might use
173 * the same inode number (not actually the same pointer
174 * though), and then we'd have two inodes sharing the
175 * same inode number and space on the harddisk.
177 void ext2_free_inode (struct inode
* inode
)
179 struct super_block
* sb
= inode
->i_sb
;
182 struct buffer_head
* bh
;
183 struct buffer_head
* bh2
;
184 unsigned long block_group
;
187 struct ext2_group_desc
* gdp
;
188 struct ext2_super_block
* es
;
191 ext2_debug ("freeing inode %lu\n", ino
);
194 * Note: we must free any quota before locking the superblock,
195 * as writing the quota to disk may need the lock as well.
197 DQUOT_FREE_INODE(sb
, inode
);
201 es
= sb
->u
.ext2_sb
.s_es
;
202 if (ino
< EXT2_FIRST_INO(sb
) ||
203 ino
> le32_to_cpu(es
->s_inodes_count
)) {
204 ext2_error (sb
, "free_inode",
205 "reserved inode or nonexistent inode");
208 block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(sb
);
209 bit
= (ino
- 1) % EXT2_INODES_PER_GROUP(sb
);
210 bitmap_nr
= load_inode_bitmap (sb
, block_group
);
214 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
216 is_directory
= S_ISDIR(inode
->i_mode
);
218 /* Do this BEFORE marking the inode not in use */
221 /* Ok, now we can actually update the inode bitmaps.. */
222 if (!ext2_clear_bit (bit
, bh
->b_data
))
223 ext2_error (sb
, "ext2_free_inode",
224 "bit already cleared for inode %lu", ino
);
226 gdp
= ext2_get_group_desc (sb
, block_group
, &bh2
);
228 gdp
->bg_free_inodes_count
=
229 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) + 1);
231 gdp
->bg_used_dirs_count
=
232 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) - 1);
234 mark_buffer_dirty(bh2
);
235 es
->s_free_inodes_count
=
236 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) + 1);
237 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
239 mark_buffer_dirty(bh
);
240 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
241 ll_rw_block (WRITE
, 1, &bh
);
250 * There are two policies for allocating an inode. If the new inode is
251 * a directory, then a forward search is made for a block group with both
252 * free space and a low directory-to-inode ratio; if that fails, then of
253 * the groups with above-average free space, that group with the fewest
254 * directories already is chosen.
256 * For other inodes, search forward from the parent directory\'s block
257 * group to find a free inode.
259 struct inode
* ext2_new_inode (const struct inode
* dir
, int mode
, int * err
)
261 struct super_block
* sb
;
262 struct buffer_head
* bh
;
263 struct buffer_head
* bh2
;
265 struct inode
* inode
;
267 struct ext2_group_desc
* gdp
;
268 struct ext2_group_desc
* tmp
;
269 struct ext2_super_block
* es
;
271 /* Cannot create files in a deleted directory */
272 if (!dir
|| !dir
->i_nlink
) {
278 inode
= new_inode(sb
);
285 es
= sb
->u
.ext2_sb
.s_es
;
291 avefreei
= le32_to_cpu(es
->s_free_inodes_count
) /
292 sb
->u
.ext2_sb
.s_groups_count
;
293 /* I am not yet convinced that this next bit is necessary.
294 i = dir->u.ext2_i.i_block_group;
295 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
296 tmp = ext2_get_group_desc (sb, i, &bh2);
298 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
299 le16_to_cpu(tmp->bg_free_inodes_count)) {
304 i = ++i % sb->u.ext2_sb.s_groups_count;
308 for (j
= 0; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
309 tmp
= ext2_get_group_desc (sb
, j
, &bh2
);
311 le16_to_cpu(tmp
->bg_free_inodes_count
) &&
312 le16_to_cpu(tmp
->bg_free_inodes_count
) >= avefreei
) {
314 (le16_to_cpu(tmp
->bg_free_blocks_count
) >
315 le16_to_cpu(gdp
->bg_free_blocks_count
))) {
326 * Try to place the inode in its parent directory
328 i
= dir
->u
.ext2_i
.i_block_group
;
329 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
330 if (tmp
&& le16_to_cpu(tmp
->bg_free_inodes_count
))
335 * Use a quadratic hash to find a group with a
338 for (j
= 1; j
< sb
->u
.ext2_sb
.s_groups_count
; j
<<= 1) {
340 if (i
>= sb
->u
.ext2_sb
.s_groups_count
)
341 i
-= sb
->u
.ext2_sb
.s_groups_count
;
342 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
344 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
352 * That failed: try linear search for a free inode
354 i
= dir
->u
.ext2_i
.i_block_group
+ 1;
355 for (j
= 2; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
356 if (++i
>= sb
->u
.ext2_sb
.s_groups_count
)
358 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
360 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
373 bitmap_nr
= load_inode_bitmap (sb
, i
);
381 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
382 if ((j
= ext2_find_first_zero_bit ((unsigned long *) bh
->b_data
,
383 EXT2_INODES_PER_GROUP(sb
))) <
384 EXT2_INODES_PER_GROUP(sb
)) {
385 if (ext2_set_bit (j
, bh
->b_data
)) {
386 ext2_error (sb
, "ext2_new_inode",
387 "bit already set for inode %d", j
);
390 mark_buffer_dirty(bh
);
391 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
392 ll_rw_block (WRITE
, 1, &bh
);
396 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != 0) {
397 ext2_error (sb
, "ext2_new_inode",
398 "Free inodes count corrupted in group %d",
400 if (sb
->s_flags
& MS_RDONLY
) {
405 gdp
->bg_free_inodes_count
= 0;
406 mark_buffer_dirty(bh2
);
410 j
+= i
* EXT2_INODES_PER_GROUP(sb
) + 1;
411 if (j
< EXT2_FIRST_INO(sb
) || j
> le32_to_cpu(es
->s_inodes_count
)) {
412 ext2_error (sb
, "ext2_new_inode",
413 "reserved inode or inode > inodes count - "
414 "block_group = %d,inode=%d", i
, j
);
420 gdp
->bg_free_inodes_count
=
421 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) - 1);
423 gdp
->bg_used_dirs_count
=
424 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) + 1);
425 mark_buffer_dirty(bh2
);
426 es
->s_free_inodes_count
=
427 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) - 1);
428 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
430 inode
->i_mode
= mode
;
431 inode
->i_uid
= current
->fsuid
;
432 if (test_opt (sb
, GRPID
))
433 inode
->i_gid
= dir
->i_gid
;
434 else if (dir
->i_mode
& S_ISGID
) {
435 inode
->i_gid
= dir
->i_gid
;
439 inode
->i_gid
= current
->fsgid
;
442 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
444 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
445 inode
->u
.ext2_i
.i_new_inode
= 1;
446 inode
->u
.ext2_i
.i_flags
= dir
->u
.ext2_i
.i_flags
;
448 inode
->u
.ext2_i
.i_flags
&= ~(EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
);
449 inode
->u
.ext2_i
.i_faddr
= 0;
450 inode
->u
.ext2_i
.i_frag_no
= 0;
451 inode
->u
.ext2_i
.i_frag_size
= 0;
452 inode
->u
.ext2_i
.i_file_acl
= 0;
453 inode
->u
.ext2_i
.i_dir_acl
= 0;
454 inode
->u
.ext2_i
.i_dtime
= 0;
455 inode
->u
.ext2_i
.i_block_group
= i
;
456 if (inode
->u
.ext2_i
.i_flags
& EXT2_SYNC_FL
)
457 inode
->i_flags
|= S_SYNC
;
458 insert_inode_hash(inode
);
459 inode
->i_generation
= event
++;
460 mark_inode_dirty(inode
);
463 if(DQUOT_ALLOC_INODE(sb
, inode
)) {
464 sb
->dq_op
->drop(inode
);
470 ext2_debug ("allocating inode %lu\n", inode
->i_ino
);
476 unsigned long ext2_count_free_inodes (struct super_block
* sb
)
479 struct ext2_super_block
* es
;
480 unsigned long desc_count
, bitmap_count
, x
;
482 struct ext2_group_desc
* gdp
;
486 es
= sb
->u
.ext2_sb
.s_es
;
490 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
491 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
494 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
495 bitmap_nr
= load_inode_bitmap (sb
, i
);
499 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
500 EXT2_INODES_PER_GROUP(sb
) / 8);
501 printk ("group %d: stored = %d, counted = %lu\n",
502 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
505 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
506 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
510 return le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_free_inodes_count
);
514 #ifdef CONFIG_EXT2_CHECK
515 /* Called at mount-time, super-block is locked */
516 void ext2_check_inodes_bitmap (struct super_block
* sb
)
518 struct ext2_super_block
* es
;
519 unsigned long desc_count
, bitmap_count
, x
;
521 struct ext2_group_desc
* gdp
;
524 es
= sb
->u
.ext2_sb
.s_es
;
528 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
529 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
532 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
533 bitmap_nr
= load_inode_bitmap (sb
, i
);
537 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
538 EXT2_INODES_PER_GROUP(sb
) / 8);
539 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != x
)
540 ext2_error (sb
, "ext2_check_inodes_bitmap",
541 "Wrong free inodes count in group %d, "
542 "stored = %d, counted = %lu", i
,
543 le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
546 if (le32_to_cpu(es
->s_free_inodes_count
) != bitmap_count
)
547 ext2_error (sb
, "ext2_check_inodes_bitmap",
548 "Wrong free inodes count in super block, "
549 "stored = %lu, counted = %lu",
550 (unsigned long) le32_to_cpu(es
->s_free_inodes_count
),