- pre6:
[davej-history.git] / fs / ext2 / ialloc.c
blobcf8fa51543b0ffe8a2171db199fe9bb8ba125d41
1 /*
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>
16 #include <linux/fs.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;
50 int retval = 0;
52 gdp = ext2_get_group_desc (sb, block_group, NULL);
53 if (!gdp) {
54 retval = -EIO;
55 goto error_out;
57 bh = bread (sb->s_dev, le32_to_cpu(gdp->bg_inode_bitmap), sb->s_blocksize);
58 if (!bh) {
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);
63 retval = -EIO;
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.
69 error_out:
70 sb->u.ext2_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
71 sb->u.ext2_sb.s_inode_bitmap[bitmap_nr] = bh;
72 return retval;
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.
81 * Notes:
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)
91 int i, j, retval = 0;
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)
103 return 0;
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");
109 else
110 return block_group;
111 } else {
112 retval = read_inode_bitmap (sb, block_group,
113 block_group);
114 if (retval < 0)
115 return retval;
116 return 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;
122 i++)
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.
142 if (!inode_bitmap)
143 retval = read_inode_bitmap (sb, block_group, 0);
145 } else {
146 if (sb->u.ext2_sb.s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
147 sb->u.ext2_sb.s_loaded_inode_bitmaps++;
148 else
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);
158 return retval;
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;
180 int is_directory;
181 unsigned long ino;
182 struct buffer_head * bh;
183 struct buffer_head * bh2;
184 unsigned long block_group;
185 unsigned long bit;
186 int bitmap_nr;
187 struct ext2_group_desc * gdp;
188 struct ext2_super_block * es;
190 ino = inode->i_ino;
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);
198 DQUOT_DROP(inode);
200 lock_super (sb);
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");
206 goto error_return;
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);
211 if (bitmap_nr < 0)
212 goto error_return;
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 */
219 clear_inode (inode);
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);
225 else {
226 gdp = ext2_get_group_desc (sb, block_group, &bh2);
227 if (gdp) {
228 gdp->bg_free_inodes_count =
229 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) + 1);
230 if (is_directory)
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);
242 wait_on_buffer (bh);
244 sb->s_dirt = 1;
245 error_return:
246 unlock_super (sb);
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;
264 int i, j, avefreei;
265 struct inode * inode;
266 int bitmap_nr;
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) {
273 *err = -EPERM;
274 return NULL;
277 sb = dir->i_sb;
278 inode = new_inode(sb);
279 if (!inode) {
280 *err = -ENOMEM;
281 return NULL;
284 lock_super (sb);
285 es = sb->u.ext2_sb.s_es;
286 repeat:
287 gdp = NULL; i=0;
289 *err = -ENOSPC;
290 if (S_ISDIR(mode)) {
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);
297 if (tmp &&
298 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
299 le16_to_cpu(tmp->bg_free_inodes_count)) {
300 gdp = tmp;
301 break;
303 else
304 i = ++i % sb->u.ext2_sb.s_groups_count;
307 if (!gdp) {
308 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
309 tmp = ext2_get_group_desc (sb, j, &bh2);
310 if (tmp &&
311 le16_to_cpu(tmp->bg_free_inodes_count) &&
312 le16_to_cpu(tmp->bg_free_inodes_count) >= avefreei) {
313 if (!gdp ||
314 (le16_to_cpu(tmp->bg_free_blocks_count) >
315 le16_to_cpu(gdp->bg_free_blocks_count))) {
316 i = j;
317 gdp = tmp;
323 else
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))
331 gdp = tmp;
332 else
335 * Use a quadratic hash to find a group with a
336 * free inode
338 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
339 i += j;
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);
343 if (tmp &&
344 le16_to_cpu(tmp->bg_free_inodes_count)) {
345 gdp = tmp;
346 break;
350 if (!gdp) {
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)
357 i = 0;
358 tmp = ext2_get_group_desc (sb, i, &bh2);
359 if (tmp &&
360 le16_to_cpu(tmp->bg_free_inodes_count)) {
361 gdp = tmp;
362 break;
368 if (!gdp) {
369 unlock_super (sb);
370 iput(inode);
371 return NULL;
373 bitmap_nr = load_inode_bitmap (sb, i);
374 if (bitmap_nr < 0) {
375 unlock_super (sb);
376 iput(inode);
377 *err = -EIO;
378 return NULL;
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);
388 goto repeat;
390 mark_buffer_dirty(bh);
391 if (sb->s_flags & MS_SYNCHRONOUS) {
392 ll_rw_block (WRITE, 1, &bh);
393 wait_on_buffer (bh);
395 } else {
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) {
401 unlock_super (sb);
402 iput (inode);
403 return NULL;
405 gdp->bg_free_inodes_count = 0;
406 mark_buffer_dirty(bh2);
408 goto repeat;
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);
415 unlock_super (sb);
416 iput (inode);
417 *err = -EIO;
418 return NULL;
420 gdp->bg_free_inodes_count =
421 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
422 if (S_ISDIR(mode))
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);
429 sb->s_dirt = 1;
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;
436 if (S_ISDIR(mode))
437 mode |= S_ISGID;
438 } else
439 inode->i_gid = current->fsgid;
441 inode->i_ino = j;
442 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
443 inode->i_blocks = 0;
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;
447 if (S_ISLNK(mode))
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);
462 unlock_super (sb);
463 if(DQUOT_ALLOC_INODE(sb, inode)) {
464 sb->dq_op->drop(inode);
465 inode->i_nlink = 0;
466 iput(inode);
467 *err = -EDQUOT;
468 return NULL;
470 ext2_debug ("allocating inode %lu\n", inode->i_ino);
472 *err = 0;
473 return inode;
476 unsigned long ext2_count_free_inodes (struct super_block * sb)
478 #ifdef EXT2FS_DEBUG
479 struct ext2_super_block * es;
480 unsigned long desc_count, bitmap_count, x;
481 int bitmap_nr;
482 struct ext2_group_desc * gdp;
483 int i;
485 lock_super (sb);
486 es = sb->u.ext2_sb.s_es;
487 desc_count = 0;
488 bitmap_count = 0;
489 gdp = NULL;
490 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
491 gdp = ext2_get_group_desc (sb, i, NULL);
492 if (!gdp)
493 continue;
494 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
495 bitmap_nr = load_inode_bitmap (sb, i);
496 if (bitmap_nr < 0)
497 continue;
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);
503 bitmap_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);
507 unlock_super (sb);
508 return desc_count;
509 #else
510 return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
511 #endif
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;
520 int bitmap_nr;
521 struct ext2_group_desc * gdp;
522 int i;
524 es = sb->u.ext2_sb.s_es;
525 desc_count = 0;
526 bitmap_count = 0;
527 gdp = NULL;
528 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
529 gdp = ext2_get_group_desc (sb, i, NULL);
530 if (!gdp)
531 continue;
532 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
533 bitmap_nr = load_inode_bitmap (sb, i);
534 if (bitmap_nr < 0)
535 continue;
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);
544 bitmap_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),
551 bitmap_count);
553 #endif