2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * linux/fs/ext2/ialloc.c
10 * Copyright (C) 1992, 1993, 1994, 1995
11 * Remy Card (card@masi.ibp.fr)
12 * Laboratoire MASI - Institut Blaise Pascal
13 * Universite Pierre et Marie Curie (Paris VI)
15 * BSD ufs-inspired inode and directory allocation by
16 * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
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).
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/vnode.h>
37 #include <ufs/ufs/quota.h>
38 #include <ufs/ufs/inode.h>
39 #include <ufs/ufs/ufsmount.h>
40 #include <gnu/ext2fs/ext2_extern.h>
41 #include <gnu/ext2fs/ext2_fs.h>
42 #include <gnu/ext2fs/ext2_fs_sb.h>
43 #include <gnu/ext2fs/fs.h>
47 #include <gnu/ext2fs/i386-bitops.h>
49 #error please provide bit operation functions
52 /* this is supposed to mark a buffer dirty on ready for delayed writing
54 void mark_buffer_dirty(struct buf *bh)
59 bh->b_flags |= B_DIRTY;
63 struct ext2_group_desc * get_group_desc (struct mount * mp,
64 unsigned int block_group,
65 struct buffer_head ** bh)
67 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
68 unsigned long group_desc;
70 struct ext2_group_desc * gdp;
72 if (block_group >= sb->s_groups_count)
73 panic ("get_group_desc: "
74 "block_group >= groups_count - "
75 "block_group = %d, groups_count = %lu",
76 block_group, sb->s_groups_count);
78 group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
79 desc = block_group % EXT2_DESC_PER_BLOCK(sb);
80 if (!sb->s_group_desc[group_desc])
81 panic ( "get_group_desc:"
82 "Group descriptor not loaded - "
83 "block_group = %d, group_desc = %lu, desc = %lu",
84 block_group, group_desc, desc);
85 gdp = (struct ext2_group_desc *)
86 sb->s_group_desc[group_desc]->b_data;
88 *bh = sb->s_group_desc[group_desc];
92 static void read_inode_bitmap (struct mount * mp,
93 unsigned long block_group,
94 unsigned int bitmap_nr)
96 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
97 struct ext2_group_desc * gdp;
98 struct buffer_head * bh;
101 gdp = get_group_desc (mp, block_group, NULL);
102 if ((error = bread (VFSTOUFS(mp)->um_devvp,
103 fsbtodb(sb, gdp->bg_inode_bitmap),
106 panic ( "read_inode_bitmap:"
107 "Cannot read inode bitmap - "
108 "block_group = %lu, inode_bitmap = %lu",
109 block_group, (unsigned long) gdp->bg_inode_bitmap);
110 sb->s_inode_bitmap_number[bitmap_nr] = block_group;
111 sb->s_inode_bitmap[bitmap_nr] = bh;
116 * load_inode_bitmap loads the inode bitmap for a blocks group
118 * It maintains a cache for the last bitmaps loaded. This cache is managed
119 * with a LRU algorithm.
122 * 1/ There is one cache per mounted file system.
123 * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
124 * this function reads the bitmap without maintaining a LRU cache.
126 static int load_inode_bitmap (struct mount * mp,
127 unsigned int block_group)
129 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
131 unsigned long inode_bitmap_number;
132 struct buffer_head * inode_bitmap;
134 if (block_group >= sb->s_groups_count)
135 panic ("load_inode_bitmap:"
136 "block_group >= groups_count - "
137 "block_group = %d, groups_count = %lu",
138 block_group, sb->s_groups_count);
139 if (sb->s_loaded_inode_bitmaps > 0 &&
140 sb->s_inode_bitmap_number[0] == block_group)
142 if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
143 if (sb->s_inode_bitmap[block_group]) {
144 if (sb->s_inode_bitmap_number[block_group] !=
146 panic ( "load_inode_bitmap:"
147 "block_group != inode_bitmap_number");
151 read_inode_bitmap (mp, block_group, block_group);
156 for (i = 0; i < sb->s_loaded_inode_bitmaps &&
157 sb->s_inode_bitmap_number[i] != block_group;
160 if (i < sb->s_loaded_inode_bitmaps &&
161 sb->s_inode_bitmap_number[i] == block_group) {
162 inode_bitmap_number = sb->s_inode_bitmap_number[i];
163 inode_bitmap = sb->s_inode_bitmap[i];
164 for (j = i; j > 0; j--) {
165 sb->s_inode_bitmap_number[j] =
166 sb->s_inode_bitmap_number[j - 1];
167 sb->s_inode_bitmap[j] =
168 sb->s_inode_bitmap[j - 1];
170 sb->s_inode_bitmap_number[0] = inode_bitmap_number;
171 sb->s_inode_bitmap[0] = inode_bitmap;
173 if (sb->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
174 sb->s_loaded_inode_bitmaps++;
176 ULCK_BUF(sb->s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1])
177 for (j = sb->s_loaded_inode_bitmaps - 1; j > 0; j--) {
178 sb->s_inode_bitmap_number[j] =
179 sb->s_inode_bitmap_number[j - 1];
180 sb->s_inode_bitmap[j] =
181 sb->s_inode_bitmap[j - 1];
183 read_inode_bitmap (mp, block_group, 0);
189 void ext2_free_inode (struct inode * inode)
191 struct ext2_sb_info * sb;
192 struct buffer_head * bh;
193 struct buffer_head * bh2;
194 unsigned long block_group;
197 struct ext2_group_desc * gdp;
198 struct ext2_super_block * es;
203 if (inode->i_nlink) {
204 printf ("ext2_free_inode: inode has nlink=%d\n",
209 ext2_debug ("freeing inode %lu\n", inode->i_number);
212 lock_super (DEVVP(inode));
213 if (inode->i_number < EXT2_FIRST_INO ||
214 inode->i_number > sb->s_es->s_inodes_count) {
215 printf ("free_inode reserved inode or nonexistent inode");
216 unlock_super (DEVVP(inode));
220 block_group = (inode->i_number - 1) / EXT2_INODES_PER_GROUP(sb);
221 bit = (inode->i_number - 1) % EXT2_INODES_PER_GROUP(sb);
222 bitmap_nr = load_inode_bitmap (ITOV(inode)->v_mount, block_group);
223 bh = sb->s_inode_bitmap[bitmap_nr];
224 if (!clear_bit (bit, bh->b_data))
225 printf ( "ext2_free_inode:"
226 "bit already cleared for inode %lu",
227 (unsigned long)inode->i_number);
229 gdp = get_group_desc (ITOV(inode)->v_mount, block_group, &bh2);
230 gdp->bg_free_inodes_count++;
231 if (S_ISDIR(inode->i_mode))
232 gdp->bg_used_dirs_count--;
233 mark_buffer_dirty(bh2);
234 es->s_free_inodes_count++;
236 mark_buffer_dirty(bh);
238 if (sb->s_flags & MS_SYNCHRONOUS) {
239 ll_rw_block (WRITE, 1, &bh);
244 unlock_super (DEVVP(inode));
249 * This function increments the inode version number
251 * This may be used one day by the NFS server
253 static void inc_inode_version (struct inode * inode,
254 struct ext2_group_desc *gdp,
257 unsigned long inode_block;
258 struct buffer_head * bh;
259 struct ext2_inode * raw_inode;
261 inode_block = gdp->bg_inode_table + (((inode->i_number - 1) %
262 EXT2_INODES_PER_GROUP(inode->i_sb)) /
263 EXT2_INODES_PER_BLOCK(inode->i_sb));
264 bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
266 printf ("inc_inode_version Cannot load inode table block - "
267 "inode=%lu, inode_block=%lu\n",
268 inode->i_number, inode_block);
269 inode->u.ext2_i.i_version = 1;
272 raw_inode = ((struct ext2_inode *) bh->b_data) +
273 (((inode->i_number - 1) %
274 EXT2_INODES_PER_GROUP(inode->i_sb)) %
275 EXT2_INODES_PER_BLOCK(inode->i_sb));
276 raw_inode->i_version++;
277 inode->u.ext2_i.i_version = raw_inode->i_version;
284 * There are two policies for allocating an inode. If the new inode is
285 * a directory, then a forward search is made for a block group with both
286 * free space and a low directory-to-inode ratio; if that fails, then of
287 * the groups with above-average free space, that group with the fewest
288 * directories already is chosen.
290 * For other inodes, search forward from the parent directory\'s block
291 * group to find a free inode.
294 * this functino has been reduced to the actual 'find the inode number' part
296 ino_t ext2_new_inode (const struct inode * dir, int mode)
298 struct ext2_sb_info * sb;
299 struct buffer_head * bh;
300 struct buffer_head * bh2;
303 struct ext2_group_desc * gdp;
304 struct ext2_group_desc * tmp;
305 struct ext2_super_block * es;
311 lock_super (DEVVP(dir));
317 avefreei = es->s_free_inodes_count /
319 /* I am not yet convinced that this next bit is necessary.
320 i = dir->u.ext2_i.i_block_group;
321 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
322 tmp = get_group_desc (sb, i, &bh2);
323 if ((tmp->bg_used_dirs_count << 8) <
324 tmp->bg_free_inodes_count) {
329 i = ++i % sb->u.ext2_sb.s_groups_count;
333 for (j = 0; j < sb->s_groups_count; j++) {
334 tmp = get_group_desc(ITOV(dir)->v_mount,j,&bh2);
335 if (tmp->bg_free_inodes_count &&
336 tmp->bg_free_inodes_count >= avefreei) {
338 (tmp->bg_free_blocks_count >
339 gdp->bg_free_blocks_count)) {
350 * Try to place the inode in its parent directory
352 i = dir->i_block_group;
353 tmp = get_group_desc (ITOV(dir)->v_mount, i, &bh2);
354 if (tmp->bg_free_inodes_count)
359 * Use a quadratic hash to find a group with a
362 for (j = 1; j < sb->s_groups_count; j <<= 1) {
364 if (i >= sb->s_groups_count)
365 i -= sb->s_groups_count;
366 tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2);
367 if (tmp->bg_free_inodes_count) {
375 * That failed: try linear search for a free inode
377 i = dir->i_block_group + 1;
378 for (j = 2; j < sb->s_groups_count; j++) {
379 if (++i >= sb->s_groups_count)
381 tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2);
382 if (tmp->bg_free_inodes_count) {
391 unlock_super (DEVVP(dir));
394 bitmap_nr = load_inode_bitmap (ITOV(dir)->v_mount, i);
395 bh = sb->s_inode_bitmap[bitmap_nr];
396 if ((j = find_first_zero_bit ((unsigned long *) bh->b_data,
397 EXT2_INODES_PER_GROUP(sb))) <
398 EXT2_INODES_PER_GROUP(sb)) {
399 if (set_bit (j, bh->b_data)) {
400 printf ( "ext2_new_inode:"
401 "bit already set for inode %d", j);
404 /* Linux now does the following:
405 mark_buffer_dirty(bh);
406 if (sb->s_flags & MS_SYNCHRONOUS) {
407 ll_rw_block (WRITE, 1, &bh);
411 mark_buffer_dirty(bh);
413 if (gdp->bg_free_inodes_count != 0) {
414 printf ( "ext2_new_inode:"
415 "Free inodes count corrupted in group %d",
417 unlock_super (DEVVP(dir));
422 j += i * EXT2_INODES_PER_GROUP(sb) + 1;
423 if (j < EXT2_FIRST_INO || j > es->s_inodes_count) {
424 printf ( "ext2_new_inode:"
425 "reserved inode or inode > inodes count - "
426 "block_group = %d,inode=%d", i, j);
427 unlock_super (DEVVP(dir));
430 gdp->bg_free_inodes_count--;
432 gdp->bg_used_dirs_count++;
433 mark_buffer_dirty(bh2);
434 es->s_free_inodes_count--;
435 /* mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1); */
437 unlock_super (DEVVP(dir));
442 static unsigned long ext2_count_free_inodes (struct mount * mp)
445 struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
446 struct ext2_super_block * es;
447 unsigned long desc_count, bitmap_count, x;
449 struct ext2_group_desc * gdp;
452 lock_super (VFSTOUFS(mp)->um_devvp);
457 for (i = 0; i < sb->s_groups_count; i++) {
458 gdp = get_group_desc (mp, i, NULL);
459 desc_count += gdp->bg_free_inodes_count;
460 bitmap_nr = load_inode_bitmap (mp, i);
461 x = ext2_count_free (sb->s_inode_bitmap[bitmap_nr],
462 EXT2_INODES_PER_GROUP(sb) / 8);
463 ext2_debug ("group %d: stored = %d, counted = %lu\n",
464 i, gdp->bg_free_inodes_count, x);
467 ext2_debug("stored = %lu, computed = %lu, %lu\n",
468 es->s_free_inodes_count, desc_count, bitmap_count);
469 unlock_super (VFSTOUFS(mp)->um_devvp);
472 return VFSTOUFS(mp)->um_e2fsb->s_free_inodes_count;
478 void ext2_check_inodes_bitmap (struct mount * mp)
480 struct ext2_super_block * es;
481 unsigned long desc_count, bitmap_count, x;
483 struct ext2_group_desc * gdp;
487 es = sb->u.ext2_sb.s_es;
491 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
492 gdp = get_group_desc (sb, i, NULL);
493 desc_count += gdp->bg_free_inodes_count;
494 bitmap_nr = load_inode_bitmap (sb, i);
495 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
496 EXT2_INODES_PER_GROUP(sb) / 8);
497 if (gdp->bg_free_inodes_count != x)
498 printf ( "ext2_check_inodes_bitmap:"
499 "Wrong free inodes count in group %d, "
500 "stored = %d, counted = %lu", i,
501 gdp->bg_free_inodes_count, x);
504 if (es->s_free_inodes_count != bitmap_count)
505 printf ( "ext2_check_inodes_bitmap:"
506 "Wrong free inodes count in super block, "
507 "stored = %lu, counted = %lu",
508 (unsigned long) es->s_free_inodes_count, bitmap_count);