Import 2.1.118
[davej-history.git] / fs / ext2 / ialloc.c
blob7043188f87e70f4424960def87372698bfae5318
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 int is_directory;
183 unsigned long ino;
184 struct super_block * sb;
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)
194 return;
195 if (!inode->i_dev) {
196 printk ("ext2_free_inode: inode has no device\n");
197 return;
199 if (inode->i_count > 1) {
200 printk ("ext2_free_inode: inode has count=%d\n", inode->i_count);
201 return;
203 if (inode->i_nlink) {
204 printk ("ext2_free_inode: inode has nlink=%d\n",
205 inode->i_nlink);
206 return;
208 if (!inode->i_sb) {
209 printk("ext2_free_inode: inode on nonexistent device\n");
210 return;
213 ino = inode->i_ino;
214 ext2_debug ("freeing inode %lu\n", ino);
216 sb = inode->i_sb;
217 lock_super (sb);
218 if (ino < EXT2_FIRST_INO(sb) ||
219 ino > le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count)) {
220 ext2_error (sb, "free_inode",
221 "reserved inode or nonexistent inode");
222 goto error_return;
224 es = sb->u.ext2_sb.s_es;
225 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
226 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
227 bitmap_nr = load_inode_bitmap (sb, block_group);
228 if (bitmap_nr < 0)
229 goto error_return;
231 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
233 is_directory = S_ISDIR(inode->i_mode);
235 /* Do this BEFORE marking the inode not in use */
236 DQUOT_FREE_INODE(sb, inode);
237 clear_inode (inode);
239 /* Ok, now we can actually update the inode bitmaps.. */
240 if (!ext2_clear_bit (bit, bh->b_data))
241 ext2_warning (sb, "ext2_free_inode",
242 "bit already cleared for inode %lu", ino);
243 else {
244 gdp = ext2_get_group_desc (sb, block_group, &bh2);
245 if (gdp) {
246 gdp->bg_free_inodes_count =
247 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) + 1);
248 if (is_directory)
249 gdp->bg_used_dirs_count =
250 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) - 1);
252 mark_buffer_dirty(bh2, 1);
253 es->s_free_inodes_count =
254 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
255 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
257 mark_buffer_dirty(bh, 1);
258 if (sb->s_flags & MS_SYNCHRONOUS) {
259 ll_rw_block (WRITE, 1, &bh);
260 wait_on_buffer (bh);
262 sb->s_dirt = 1;
263 error_return:
264 unlock_super (sb);
268 * This function increments the inode version number
270 * This may be used one day by the NFS server
272 static void inc_inode_version (struct inode * inode,
273 struct ext2_group_desc *gdp,
274 int mode)
276 inode->u.ext2_i.i_version++;
277 mark_inode_dirty(inode);
279 return;
283 * There are two policies for allocating an inode. If the new inode is
284 * a directory, then a forward search is made for a block group with both
285 * free space and a low directory-to-inode ratio; if that fails, then of
286 * the groups with above-average free space, that group with the fewest
287 * directories already is chosen.
289 * For other inodes, search forward from the parent directory\'s block
290 * group to find a free inode.
292 struct inode * ext2_new_inode (const struct inode * dir, int mode, int * err)
294 struct super_block * sb;
295 struct buffer_head * bh;
296 struct buffer_head * bh2;
297 int i, j, avefreei;
298 struct inode * inode;
299 int bitmap_nr;
300 struct ext2_group_desc * gdp;
301 struct ext2_group_desc * tmp;
302 struct ext2_super_block * es;
304 /* Cannot create files in a deleted directory */
305 if (!dir || !dir->i_nlink) {
306 *err = -EPERM;
307 return NULL;
310 inode = get_empty_inode ();
311 if (!inode) {
312 *err = -ENOMEM;
313 return NULL;
316 sb = dir->i_sb;
317 inode->i_sb = sb;
318 inode->i_flags = sb->s_flags;
319 lock_super (sb);
320 es = sb->u.ext2_sb.s_es;
321 repeat:
322 gdp = NULL; i=0;
324 *err = -ENOSPC;
325 if (S_ISDIR(mode)) {
326 avefreei = le32_to_cpu(es->s_free_inodes_count) /
327 sb->u.ext2_sb.s_groups_count;
328 /* I am not yet convinced that this next bit is necessary.
329 i = dir->u.ext2_i.i_block_group;
330 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
331 tmp = ext2_get_group_desc (sb, i, &bh2);
332 if (tmp &&
333 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
334 le16_to_cpu(tmp->bg_free_inodes_count)) {
335 gdp = tmp;
336 break;
338 else
339 i = ++i % sb->u.ext2_sb.s_groups_count;
342 if (!gdp) {
343 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
344 tmp = ext2_get_group_desc (sb, j, &bh2);
345 if (tmp &&
346 le16_to_cpu(tmp->bg_free_inodes_count) &&
347 le16_to_cpu(tmp->bg_free_inodes_count) >= avefreei) {
348 if (!gdp ||
349 (le16_to_cpu(tmp->bg_free_blocks_count) >
350 le16_to_cpu(gdp->bg_free_blocks_count))) {
351 i = j;
352 gdp = tmp;
358 else
361 * Try to place the inode in its parent directory
363 i = dir->u.ext2_i.i_block_group;
364 tmp = ext2_get_group_desc (sb, i, &bh2);
365 if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
366 gdp = tmp;
367 else
370 * Use a quadratic hash to find a group with a
371 * free inode
373 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
374 i += j;
375 if (i >= sb->u.ext2_sb.s_groups_count)
376 i -= sb->u.ext2_sb.s_groups_count;
377 tmp = ext2_get_group_desc (sb, i, &bh2);
378 if (tmp &&
379 le16_to_cpu(tmp->bg_free_inodes_count)) {
380 gdp = tmp;
381 break;
385 if (!gdp) {
387 * That failed: try linear search for a free inode
389 i = dir->u.ext2_i.i_block_group + 1;
390 for (j = 2; j < sb->u.ext2_sb.s_groups_count; j++) {
391 if (++i >= sb->u.ext2_sb.s_groups_count)
392 i = 0;
393 tmp = ext2_get_group_desc (sb, i, &bh2);
394 if (tmp &&
395 le16_to_cpu(tmp->bg_free_inodes_count)) {
396 gdp = tmp;
397 break;
403 if (!gdp) {
404 unlock_super (sb);
405 iput(inode);
406 return NULL;
408 bitmap_nr = load_inode_bitmap (sb, i);
409 if (bitmap_nr < 0) {
410 unlock_super (sb);
411 iput(inode);
412 *err = -EIO;
413 return NULL;
416 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
417 if ((j = ext2_find_first_zero_bit ((unsigned long *) bh->b_data,
418 EXT2_INODES_PER_GROUP(sb))) <
419 EXT2_INODES_PER_GROUP(sb)) {
420 if (ext2_set_bit (j, bh->b_data)) {
421 ext2_warning (sb, "ext2_new_inode",
422 "bit already set for inode %d", j);
423 goto repeat;
425 mark_buffer_dirty(bh, 1);
426 if (sb->s_flags & MS_SYNCHRONOUS) {
427 ll_rw_block (WRITE, 1, &bh);
428 wait_on_buffer (bh);
430 } else {
431 if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
432 ext2_error (sb, "ext2_new_inode",
433 "Free inodes count corrupted in group %d",
435 unlock_super (sb);
436 iput (inode);
437 return NULL;
439 goto repeat;
441 j += i * EXT2_INODES_PER_GROUP(sb) + 1;
442 if (j < EXT2_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
443 ext2_error (sb, "ext2_new_inode",
444 "reserved inode or inode > inodes count - "
445 "block_group = %d,inode=%d", i, j);
446 unlock_super (sb);
447 iput (inode);
448 return NULL;
450 gdp->bg_free_inodes_count =
451 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
452 if (S_ISDIR(mode))
453 gdp->bg_used_dirs_count =
454 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
455 mark_buffer_dirty(bh2, 1);
456 es->s_free_inodes_count =
457 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
458 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
459 sb->s_dirt = 1;
460 inode->i_mode = mode;
461 inode->i_sb = sb;
462 inode->i_nlink = 1;
463 inode->i_dev = sb->s_dev;
464 inode->i_uid = current->fsuid;
465 if (test_opt (sb, GRPID))
466 inode->i_gid = dir->i_gid;
467 else if (dir->i_mode & S_ISGID) {
468 inode->i_gid = dir->i_gid;
469 if (S_ISDIR(mode))
470 mode |= S_ISGID;
471 } else
472 inode->i_gid = current->fsgid;
474 inode->i_ino = j;
475 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
476 inode->i_blocks = 0;
477 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
478 inode->u.ext2_i.i_new_inode = 1;
479 inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags;
480 if (S_ISLNK(mode))
481 inode->u.ext2_i.i_flags &= ~(EXT2_IMMUTABLE_FL | EXT2_APPEND_FL);
482 inode->u.ext2_i.i_faddr = 0;
483 inode->u.ext2_i.i_frag_no = 0;
484 inode->u.ext2_i.i_frag_size = 0;
485 inode->u.ext2_i.i_file_acl = 0;
486 inode->u.ext2_i.i_dir_acl = 0;
487 inode->u.ext2_i.i_dtime = 0;
488 inode->u.ext2_i.i_block_group = i;
489 inode->i_op = NULL;
490 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
491 inode->i_flags |= MS_SYNCHRONOUS;
492 insert_inode_hash(inode);
493 mark_inode_dirty(inode);
494 inc_inode_version (inode, gdp, mode);
496 unlock_super (sb);
497 if(DQUOT_ALLOC_INODE(sb, inode)) {
498 sb->dq_op->drop(inode);
499 inode->i_nlink = 0;
500 iput(inode);
501 *err = -EDQUOT;
502 return NULL;
504 ext2_debug ("allocating inode %lu\n", inode->i_ino);
506 *err = 0;
507 return inode;
510 unsigned long ext2_count_free_inodes (struct super_block * sb)
512 #ifdef EXT2FS_DEBUG
513 struct ext2_super_block * es;
514 unsigned long desc_count, bitmap_count, x;
515 int bitmap_nr;
516 struct ext2_group_desc * gdp;
517 int i;
519 lock_super (sb);
520 es = sb->u.ext2_sb.s_es;
521 desc_count = 0;
522 bitmap_count = 0;
523 gdp = NULL;
524 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
525 gdp = ext2_get_group_desc (sb, i, NULL);
526 if (!gdp)
527 continue;
528 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
529 bitmap_nr = load_inode_bitmap (sb, i);
530 if (bitmap_nr < 0)
531 continue;
533 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
534 EXT2_INODES_PER_GROUP(sb) / 8);
535 printk ("group %d: stored = %d, counted = %lu\n",
536 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
537 bitmap_count += x;
539 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
540 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
541 unlock_super (sb);
542 return desc_count;
543 #else
544 return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
545 #endif
548 void ext2_check_inodes_bitmap (struct super_block * sb)
550 struct ext2_super_block * es;
551 unsigned long desc_count, bitmap_count, x;
552 int bitmap_nr;
553 struct ext2_group_desc * gdp;
554 int i;
556 lock_super (sb);
557 es = sb->u.ext2_sb.s_es;
558 desc_count = 0;
559 bitmap_count = 0;
560 gdp = NULL;
561 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
562 gdp = ext2_get_group_desc (sb, i, NULL);
563 if (!gdp)
564 continue;
565 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
566 bitmap_nr = load_inode_bitmap (sb, i);
567 if (bitmap_nr < 0)
568 continue;
570 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
571 EXT2_INODES_PER_GROUP(sb) / 8);
572 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
573 ext2_error (sb, "ext2_check_inodes_bitmap",
574 "Wrong free inodes count in group %d, "
575 "stored = %d, counted = %lu", i,
576 le16_to_cpu(gdp->bg_free_inodes_count), x);
577 bitmap_count += x;
579 if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
580 ext2_error (sb, "ext2_check_inodes_bitmap",
581 "Wrong free inodes count in super block, "
582 "stored = %lu, counted = %lu",
583 (unsigned long) le32_to_cpu(es->s_free_inodes_count),
584 bitmap_count);
585 unlock_super (sb);