Linux 2.3.0
[davej-history.git] / fs / ext2 / ialloc.c
blobc8208227c99d7c97a1d0efdf5a310ff2e27aa55d
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
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).
30 #include <linux/fs.h>
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;
53 int retval = 0;
55 gdp = ext2_get_group_desc (sb, block_group, NULL);
56 if (!gdp) {
57 retval = -EIO;
58 goto error_out;
60 bh = bread (sb->s_dev, le32_to_cpu(gdp->bg_inode_bitmap), sb->s_blocksize);
61 if (!bh) {
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);
66 retval = -EIO;
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.
72 error_out:
73 sb->u.ext2_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
74 sb->u.ext2_sb.s_inode_bitmap[bitmap_nr] = bh;
75 return retval;
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.
84 * Notes:
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)
94 int i, j, retval = 0;
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)
106 return 0;
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");
112 else
113 return block_group;
114 } else {
115 retval = read_inode_bitmap (sb, block_group,
116 block_group);
117 if (retval < 0)
118 return retval;
119 return 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;
125 i++)
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.
145 if (!inode_bitmap)
146 retval = read_inode_bitmap (sb, block_group, 0);
148 } else {
149 if (sb->u.ext2_sb.s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
150 sb->u.ext2_sb.s_loaded_inode_bitmaps++;
151 else
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);
161 return retval;
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;
183 int is_directory;
184 unsigned long ino;
185 struct buffer_head * bh;
186 struct buffer_head * bh2;
187 unsigned long block_group;
188 unsigned long bit;
189 int bitmap_nr;
190 struct ext2_group_desc * gdp;
191 struct ext2_super_block * es;
193 if (!inode->i_dev) {
194 printk ("ext2_free_inode: inode has no device\n");
195 return;
197 if (inode->i_count > 1) {
198 printk ("ext2_free_inode: inode has count=%d\n", inode->i_count);
199 return;
201 if (inode->i_nlink) {
202 printk ("ext2_free_inode: inode has nlink=%d\n",
203 inode->i_nlink);
204 return;
206 if (!sb) {
207 printk("ext2_free_inode: inode on nonexistent device\n");
208 return;
211 ino = inode->i_ino;
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);
219 DQUOT_DROP(inode);
221 lock_super (sb);
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");
227 goto error_return;
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);
232 if (bitmap_nr < 0)
233 goto error_return;
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 */
240 clear_inode (inode);
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);
246 else {
247 gdp = ext2_get_group_desc (sb, block_group, &bh2);
248 if (gdp) {
249 gdp->bg_free_inodes_count =
250 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) + 1);
251 if (is_directory)
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);
263 wait_on_buffer (bh);
265 sb->s_dirt = 1;
266 error_return:
267 unlock_super (sb);
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,
277 int mode)
279 inode->u.ext2_i.i_version++;
280 mark_inode_dirty(inode);
282 return;
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;
300 int i, j, avefreei;
301 struct inode * inode;
302 int bitmap_nr;
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) {
309 *err = -EPERM;
310 return NULL;
313 inode = get_empty_inode ();
314 if (!inode) {
315 *err = -ENOMEM;
316 return NULL;
319 sb = dir->i_sb;
320 inode->i_sb = sb;
321 inode->i_flags = 0;
322 lock_super (sb);
323 es = sb->u.ext2_sb.s_es;
324 repeat:
325 gdp = NULL; i=0;
327 *err = -ENOSPC;
328 if (S_ISDIR(mode)) {
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);
335 if (tmp &&
336 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
337 le16_to_cpu(tmp->bg_free_inodes_count)) {
338 gdp = tmp;
339 break;
341 else
342 i = ++i % sb->u.ext2_sb.s_groups_count;
345 if (!gdp) {
346 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
347 tmp = ext2_get_group_desc (sb, j, &bh2);
348 if (tmp &&
349 le16_to_cpu(tmp->bg_free_inodes_count) &&
350 le16_to_cpu(tmp->bg_free_inodes_count) >= avefreei) {
351 if (!gdp ||
352 (le16_to_cpu(tmp->bg_free_blocks_count) >
353 le16_to_cpu(gdp->bg_free_blocks_count))) {
354 i = j;
355 gdp = tmp;
361 else
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))
369 gdp = tmp;
370 else
373 * Use a quadratic hash to find a group with a
374 * free inode
376 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
377 i += j;
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);
381 if (tmp &&
382 le16_to_cpu(tmp->bg_free_inodes_count)) {
383 gdp = tmp;
384 break;
388 if (!gdp) {
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)
395 i = 0;
396 tmp = ext2_get_group_desc (sb, i, &bh2);
397 if (tmp &&
398 le16_to_cpu(tmp->bg_free_inodes_count)) {
399 gdp = tmp;
400 break;
406 if (!gdp) {
407 unlock_super (sb);
408 iput(inode);
409 return NULL;
411 bitmap_nr = load_inode_bitmap (sb, i);
412 if (bitmap_nr < 0) {
413 unlock_super (sb);
414 iput(inode);
415 *err = -EIO;
416 return NULL;
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);
426 goto repeat;
428 mark_buffer_dirty(bh, 1);
429 if (sb->s_flags & MS_SYNCHRONOUS) {
430 ll_rw_block (WRITE, 1, &bh);
431 wait_on_buffer (bh);
433 } else {
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",
438 unlock_super (sb);
439 iput (inode);
440 return NULL;
442 goto repeat;
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);
449 unlock_super (sb);
450 iput (inode);
451 return NULL;
453 gdp->bg_free_inodes_count =
454 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
455 if (S_ISDIR(mode))
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);
462 sb->s_dirt = 1;
463 inode->i_mode = mode;
464 inode->i_sb = sb;
465 inode->i_nlink = 1;
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;
472 if (S_ISDIR(mode))
473 mode |= S_ISGID;
474 } else
475 inode->i_gid = current->fsgid;
477 inode->i_ino = j;
478 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
479 inode->i_blocks = 0;
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;
483 if (S_ISLNK(mode))
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;
492 inode->i_op = NULL;
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);
499 unlock_super (sb);
500 if(DQUOT_ALLOC_INODE(sb, inode)) {
501 sb->dq_op->drop(inode);
502 inode->i_nlink = 0;
503 iput(inode);
504 *err = -EDQUOT;
505 return NULL;
507 ext2_debug ("allocating inode %lu\n", inode->i_ino);
509 *err = 0;
510 return inode;
513 unsigned long ext2_count_free_inodes (struct super_block * sb)
515 #ifdef EXT2FS_DEBUG
516 struct ext2_super_block * es;
517 unsigned long desc_count, bitmap_count, x;
518 int bitmap_nr;
519 struct ext2_group_desc * gdp;
520 int i;
522 lock_super (sb);
523 es = sb->u.ext2_sb.s_es;
524 desc_count = 0;
525 bitmap_count = 0;
526 gdp = NULL;
527 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
528 gdp = ext2_get_group_desc (sb, i, NULL);
529 if (!gdp)
530 continue;
531 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
532 bitmap_nr = load_inode_bitmap (sb, i);
533 if (bitmap_nr < 0)
534 continue;
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);
540 bitmap_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);
544 unlock_super (sb);
545 return desc_count;
546 #else
547 return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
548 #endif
551 void ext2_check_inodes_bitmap (struct super_block * sb)
553 struct ext2_super_block * es;
554 unsigned long desc_count, bitmap_count, x;
555 int bitmap_nr;
556 struct ext2_group_desc * gdp;
557 int i;
559 lock_super (sb);
560 es = sb->u.ext2_sb.s_es;
561 desc_count = 0;
562 bitmap_count = 0;
563 gdp = NULL;
564 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
565 gdp = ext2_get_group_desc (sb, i, NULL);
566 if (!gdp)
567 continue;
568 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
569 bitmap_nr = load_inode_bitmap (sb, i);
570 if (bitmap_nr < 0)
571 continue;
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);
580 bitmap_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),
587 bitmap_count);
588 unlock_super (sb);