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
)
182 struct super_block
* sb
= inode
->i_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
;
194 printk ("ext2_free_inode: inode has no device\n");
197 if (inode
->i_count
> 1) {
198 printk ("ext2_free_inode: inode has count=%d\n", inode
->i_count
);
201 if (inode
->i_nlink
) {
202 printk ("ext2_free_inode: inode has nlink=%d\n",
207 printk("ext2_free_inode: inode on nonexistent device\n");
212 ext2_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.
218 DQUOT_FREE_INODE(sb
, inode
);
222 es
= sb
->u
.ext2_sb
.s_es
;
223 if (ino
< EXT2_FIRST_INO(sb
) ||
224 ino
> le32_to_cpu(es
->s_inodes_count
)) {
225 ext2_error (sb
, "free_inode",
226 "reserved inode or nonexistent inode");
229 block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(sb
);
230 bit
= (ino
- 1) % EXT2_INODES_PER_GROUP(sb
);
231 bitmap_nr
= load_inode_bitmap (sb
, block_group
);
235 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
237 is_directory
= S_ISDIR(inode
->i_mode
);
239 /* Do this BEFORE marking the inode not in use */
242 /* Ok, now we can actually update the inode bitmaps.. */
243 if (!ext2_clear_bit (bit
, bh
->b_data
))
244 ext2_warning (sb
, "ext2_free_inode",
245 "bit already cleared for inode %lu", ino
);
247 gdp
= ext2_get_group_desc (sb
, block_group
, &bh2
);
249 gdp
->bg_free_inodes_count
=
250 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) + 1);
252 gdp
->bg_used_dirs_count
=
253 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) - 1);
255 mark_buffer_dirty(bh2
, 1);
256 es
->s_free_inodes_count
=
257 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) + 1);
258 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
260 mark_buffer_dirty(bh
, 1);
261 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
262 ll_rw_block (WRITE
, 1, &bh
);
271 * This function increments the inode version number
273 * This may be used one day by the NFS server
275 static void inc_inode_version (struct inode
* inode
,
276 struct ext2_group_desc
*gdp
,
279 inode
->u
.ext2_i
.i_version
++;
280 mark_inode_dirty(inode
);
286 * There are two policies for allocating an inode. If the new inode is
287 * a directory, then a forward search is made for a block group with both
288 * free space and a low directory-to-inode ratio; if that fails, then of
289 * the groups with above-average free space, that group with the fewest
290 * directories already is chosen.
292 * For other inodes, search forward from the parent directory\'s block
293 * group to find a free inode.
295 struct inode
* ext2_new_inode (const struct inode
* dir
, int mode
, int * err
)
297 struct super_block
* sb
;
298 struct buffer_head
* bh
;
299 struct buffer_head
* bh2
;
301 struct inode
* inode
;
303 struct ext2_group_desc
* gdp
;
304 struct ext2_group_desc
* tmp
;
305 struct ext2_super_block
* es
;
307 /* Cannot create files in a deleted directory */
308 if (!dir
|| !dir
->i_nlink
) {
313 inode
= get_empty_inode ();
323 es
= sb
->u
.ext2_sb
.s_es
;
329 avefreei
= le32_to_cpu(es
->s_free_inodes_count
) /
330 sb
->u
.ext2_sb
.s_groups_count
;
331 /* I am not yet convinced that this next bit is necessary.
332 i = dir->u.ext2_i.i_block_group;
333 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
334 tmp = ext2_get_group_desc (sb, i, &bh2);
336 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
337 le16_to_cpu(tmp->bg_free_inodes_count)) {
342 i = ++i % sb->u.ext2_sb.s_groups_count;
346 for (j
= 0; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
347 tmp
= ext2_get_group_desc (sb
, j
, &bh2
);
349 le16_to_cpu(tmp
->bg_free_inodes_count
) &&
350 le16_to_cpu(tmp
->bg_free_inodes_count
) >= avefreei
) {
352 (le16_to_cpu(tmp
->bg_free_blocks_count
) >
353 le16_to_cpu(gdp
->bg_free_blocks_count
))) {
364 * Try to place the inode in its parent directory
366 i
= dir
->u
.ext2_i
.i_block_group
;
367 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
368 if (tmp
&& le16_to_cpu(tmp
->bg_free_inodes_count
))
373 * Use a quadratic hash to find a group with a
376 for (j
= 1; j
< sb
->u
.ext2_sb
.s_groups_count
; j
<<= 1) {
378 if (i
>= sb
->u
.ext2_sb
.s_groups_count
)
379 i
-= sb
->u
.ext2_sb
.s_groups_count
;
380 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
382 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
390 * That failed: try linear search for a free inode
392 i
= dir
->u
.ext2_i
.i_block_group
+ 1;
393 for (j
= 2; j
< sb
->u
.ext2_sb
.s_groups_count
; j
++) {
394 if (++i
>= sb
->u
.ext2_sb
.s_groups_count
)
396 tmp
= ext2_get_group_desc (sb
, i
, &bh2
);
398 le16_to_cpu(tmp
->bg_free_inodes_count
)) {
411 bitmap_nr
= load_inode_bitmap (sb
, i
);
419 bh
= sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
];
420 if ((j
= ext2_find_first_zero_bit ((unsigned long *) bh
->b_data
,
421 EXT2_INODES_PER_GROUP(sb
))) <
422 EXT2_INODES_PER_GROUP(sb
)) {
423 if (ext2_set_bit (j
, bh
->b_data
)) {
424 ext2_warning (sb
, "ext2_new_inode",
425 "bit already set for inode %d", j
);
428 mark_buffer_dirty(bh
, 1);
429 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
430 ll_rw_block (WRITE
, 1, &bh
);
434 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != 0) {
435 ext2_error (sb
, "ext2_new_inode",
436 "Free inodes count corrupted in group %d",
444 j
+= i
* EXT2_INODES_PER_GROUP(sb
) + 1;
445 if (j
< EXT2_FIRST_INO(sb
) || j
> le32_to_cpu(es
->s_inodes_count
)) {
446 ext2_error (sb
, "ext2_new_inode",
447 "reserved inode or inode > inodes count - "
448 "block_group = %d,inode=%d", i
, j
);
453 gdp
->bg_free_inodes_count
=
454 cpu_to_le16(le16_to_cpu(gdp
->bg_free_inodes_count
) - 1);
456 gdp
->bg_used_dirs_count
=
457 cpu_to_le16(le16_to_cpu(gdp
->bg_used_dirs_count
) + 1);
458 mark_buffer_dirty(bh2
, 1);
459 es
->s_free_inodes_count
=
460 cpu_to_le32(le32_to_cpu(es
->s_free_inodes_count
) - 1);
461 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
463 inode
->i_mode
= mode
;
466 inode
->i_dev
= sb
->s_dev
;
467 inode
->i_uid
= current
->fsuid
;
468 if (test_opt (sb
, GRPID
))
469 inode
->i_gid
= dir
->i_gid
;
470 else if (dir
->i_mode
& S_ISGID
) {
471 inode
->i_gid
= dir
->i_gid
;
475 inode
->i_gid
= current
->fsgid
;
478 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
480 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
481 inode
->u
.ext2_i
.i_new_inode
= 1;
482 inode
->u
.ext2_i
.i_flags
= dir
->u
.ext2_i
.i_flags
;
484 inode
->u
.ext2_i
.i_flags
&= ~(EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
);
485 inode
->u
.ext2_i
.i_faddr
= 0;
486 inode
->u
.ext2_i
.i_frag_no
= 0;
487 inode
->u
.ext2_i
.i_frag_size
= 0;
488 inode
->u
.ext2_i
.i_file_acl
= 0;
489 inode
->u
.ext2_i
.i_dir_acl
= 0;
490 inode
->u
.ext2_i
.i_dtime
= 0;
491 inode
->u
.ext2_i
.i_block_group
= i
;
493 if (inode
->u
.ext2_i
.i_flags
& EXT2_SYNC_FL
)
494 inode
->i_flags
|= MS_SYNCHRONOUS
;
495 insert_inode_hash(inode
);
496 mark_inode_dirty(inode
);
497 inc_inode_version (inode
, gdp
, mode
);
500 if(DQUOT_ALLOC_INODE(sb
, inode
)) {
501 sb
->dq_op
->drop(inode
);
507 ext2_debug ("allocating inode %lu\n", inode
->i_ino
);
513 unsigned long ext2_count_free_inodes (struct super_block
* sb
)
516 struct ext2_super_block
* es
;
517 unsigned long desc_count
, bitmap_count
, x
;
519 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 printk ("group %d: stored = %d, counted = %lu\n",
539 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
542 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
543 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
547 return le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_free_inodes_count
);
551 void ext2_check_inodes_bitmap (struct super_block
* sb
)
553 struct ext2_super_block
* es
;
554 unsigned long desc_count
, bitmap_count
, x
;
556 struct ext2_group_desc
* gdp
;
560 es
= sb
->u
.ext2_sb
.s_es
;
564 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++) {
565 gdp
= ext2_get_group_desc (sb
, i
, NULL
);
568 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
569 bitmap_nr
= load_inode_bitmap (sb
, i
);
573 x
= ext2_count_free (sb
->u
.ext2_sb
.s_inode_bitmap
[bitmap_nr
],
574 EXT2_INODES_PER_GROUP(sb
) / 8);
575 if (le16_to_cpu(gdp
->bg_free_inodes_count
) != x
)
576 ext2_error (sb
, "ext2_check_inodes_bitmap",
577 "Wrong free inodes count in group %d, "
578 "stored = %d, counted = %lu", i
,
579 le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
582 if (le32_to_cpu(es
->s_free_inodes_count
) != bitmap_count
)
583 ext2_error (sb
, "ext2_check_inodes_bitmap",
584 "Wrong free inodes count in super block, "
585 "stored = %lu, counted = %lu",
586 (unsigned long) le32_to_cpu(es
->s_free_inodes_count
),