- Andries Brouwer: final isofs pieces.
[davej-history.git] / fs / ext2 / ialloc.c
blob9e4a9b6b021156cab0413cdae351703a884208be
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)
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;
270 int err;
272 /* Cannot create files in a deleted directory */
273 if (!dir || !dir->i_nlink)
274 return ERR_PTR(-EPERM);
276 sb = dir->i_sb;
277 inode = new_inode(sb);
278 if (!inode)
279 return ERR_PTR(-ENOMEM);
281 lock_super (sb);
282 es = sb->u.ext2_sb.s_es;
283 repeat:
284 gdp = NULL; i=0;
286 if (S_ISDIR(mode)) {
287 avefreei = le32_to_cpu(es->s_free_inodes_count) /
288 sb->u.ext2_sb.s_groups_count;
289 /* I am not yet convinced that this next bit is necessary.
290 i = dir->u.ext2_i.i_block_group;
291 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
292 tmp = ext2_get_group_desc (sb, i, &bh2);
293 if (tmp &&
294 (le16_to_cpu(tmp->bg_used_dirs_count) << 8) <
295 le16_to_cpu(tmp->bg_free_inodes_count)) {
296 gdp = tmp;
297 break;
299 else
300 i = ++i % sb->u.ext2_sb.s_groups_count;
303 if (!gdp) {
304 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
305 tmp = ext2_get_group_desc (sb, j, &bh2);
306 if (tmp &&
307 le16_to_cpu(tmp->bg_free_inodes_count) &&
308 le16_to_cpu(tmp->bg_free_inodes_count) >= avefreei) {
309 if (!gdp ||
310 (le16_to_cpu(tmp->bg_free_blocks_count) >
311 le16_to_cpu(gdp->bg_free_blocks_count))) {
312 i = j;
313 gdp = tmp;
319 else
322 * Try to place the inode in its parent directory
324 i = dir->u.ext2_i.i_block_group;
325 tmp = ext2_get_group_desc (sb, i, &bh2);
326 if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
327 gdp = tmp;
328 else
331 * Use a quadratic hash to find a group with a
332 * free inode
334 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
335 i += j;
336 if (i >= sb->u.ext2_sb.s_groups_count)
337 i -= sb->u.ext2_sb.s_groups_count;
338 tmp = ext2_get_group_desc (sb, i, &bh2);
339 if (tmp &&
340 le16_to_cpu(tmp->bg_free_inodes_count)) {
341 gdp = tmp;
342 break;
346 if (!gdp) {
348 * That failed: try linear search for a free inode
350 i = dir->u.ext2_i.i_block_group + 1;
351 for (j = 2; j < sb->u.ext2_sb.s_groups_count; j++) {
352 if (++i >= sb->u.ext2_sb.s_groups_count)
353 i = 0;
354 tmp = ext2_get_group_desc (sb, i, &bh2);
355 if (tmp &&
356 le16_to_cpu(tmp->bg_free_inodes_count)) {
357 gdp = tmp;
358 break;
364 err = -ENOSPC;
365 if (!gdp)
366 goto fail;
368 err = -EIO;
369 bitmap_nr = load_inode_bitmap (sb, i);
370 if (bitmap_nr < 0)
371 goto fail;
373 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
374 if ((j = ext2_find_first_zero_bit ((unsigned long *) bh->b_data,
375 EXT2_INODES_PER_GROUP(sb))) <
376 EXT2_INODES_PER_GROUP(sb)) {
377 if (ext2_set_bit (j, bh->b_data)) {
378 ext2_error (sb, "ext2_new_inode",
379 "bit already set for inode %d", j);
380 goto repeat;
382 mark_buffer_dirty(bh);
383 if (sb->s_flags & MS_SYNCHRONOUS) {
384 ll_rw_block (WRITE, 1, &bh);
385 wait_on_buffer (bh);
387 } else {
388 if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
389 ext2_error (sb, "ext2_new_inode",
390 "Free inodes count corrupted in group %d",
392 /* Is it really ENOSPC? */
393 err = -ENOSPC;
394 if (sb->s_flags & MS_RDONLY)
395 goto fail;
397 gdp->bg_free_inodes_count = 0;
398 mark_buffer_dirty(bh2);
400 goto repeat;
402 j += i * EXT2_INODES_PER_GROUP(sb) + 1;
403 if (j < EXT2_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
404 ext2_error (sb, "ext2_new_inode",
405 "reserved inode or inode > inodes count - "
406 "block_group = %d,inode=%d", i, j);
407 err = -EIO;
408 goto fail;
410 gdp->bg_free_inodes_count =
411 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
412 if (S_ISDIR(mode))
413 gdp->bg_used_dirs_count =
414 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
415 mark_buffer_dirty(bh2);
416 es->s_free_inodes_count =
417 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
418 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
419 sb->s_dirt = 1;
420 inode->i_mode = mode;
421 inode->i_uid = current->fsuid;
422 if (test_opt (sb, GRPID))
423 inode->i_gid = dir->i_gid;
424 else if (dir->i_mode & S_ISGID) {
425 inode->i_gid = dir->i_gid;
426 if (S_ISDIR(mode))
427 mode |= S_ISGID;
428 } else
429 inode->i_gid = current->fsgid;
431 inode->i_ino = j;
432 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
433 inode->i_blocks = 0;
434 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
435 inode->u.ext2_i.i_new_inode = 1;
436 inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags;
437 if (S_ISLNK(mode))
438 inode->u.ext2_i.i_flags &= ~(EXT2_IMMUTABLE_FL | EXT2_APPEND_FL);
439 inode->u.ext2_i.i_faddr = 0;
440 inode->u.ext2_i.i_frag_no = 0;
441 inode->u.ext2_i.i_frag_size = 0;
442 inode->u.ext2_i.i_file_acl = 0;
443 inode->u.ext2_i.i_dir_acl = 0;
444 inode->u.ext2_i.i_dtime = 0;
445 inode->u.ext2_i.i_block_group = i;
446 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
447 inode->i_flags |= S_SYNC;
448 insert_inode_hash(inode);
449 inode->i_generation = event++;
450 mark_inode_dirty(inode);
452 unlock_super (sb);
453 if(DQUOT_ALLOC_INODE(sb, inode)) {
454 sb->dq_op->drop(inode);
455 inode->i_nlink = 0;
456 iput(inode);
457 return ERR_PTR(-EDQUOT);
459 ext2_debug ("allocating inode %lu\n", inode->i_ino);
460 return inode;
462 fail:
463 unlock_super(sb);
464 iput(inode);
465 return ERR_PTR(err);
468 unsigned long ext2_count_free_inodes (struct super_block * sb)
470 #ifdef EXT2FS_DEBUG
471 struct ext2_super_block * es;
472 unsigned long desc_count, bitmap_count, x;
473 int bitmap_nr;
474 struct ext2_group_desc * gdp;
475 int i;
477 lock_super (sb);
478 es = sb->u.ext2_sb.s_es;
479 desc_count = 0;
480 bitmap_count = 0;
481 gdp = NULL;
482 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
483 gdp = ext2_get_group_desc (sb, i, NULL);
484 if (!gdp)
485 continue;
486 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
487 bitmap_nr = load_inode_bitmap (sb, i);
488 if (bitmap_nr < 0)
489 continue;
491 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
492 EXT2_INODES_PER_GROUP(sb) / 8);
493 printk ("group %d: stored = %d, counted = %lu\n",
494 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
495 bitmap_count += x;
497 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
498 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
499 unlock_super (sb);
500 return desc_count;
501 #else
502 return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
503 #endif
506 #ifdef CONFIG_EXT2_CHECK
507 /* Called at mount-time, super-block is locked */
508 void ext2_check_inodes_bitmap (struct super_block * sb)
510 struct ext2_super_block * es;
511 unsigned long desc_count, bitmap_count, x;
512 int bitmap_nr;
513 struct ext2_group_desc * gdp;
514 int i;
516 es = sb->u.ext2_sb.s_es;
517 desc_count = 0;
518 bitmap_count = 0;
519 gdp = NULL;
520 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
521 gdp = ext2_get_group_desc (sb, i, NULL);
522 if (!gdp)
523 continue;
524 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
525 bitmap_nr = load_inode_bitmap (sb, i);
526 if (bitmap_nr < 0)
527 continue;
529 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
530 EXT2_INODES_PER_GROUP(sb) / 8);
531 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
532 ext2_error (sb, "ext2_check_inodes_bitmap",
533 "Wrong free inodes count in group %d, "
534 "stored = %d, counted = %lu", i,
535 le16_to_cpu(gdp->bg_free_inodes_count), x);
536 bitmap_count += x;
538 if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
539 ext2_error (sb, "ext2_check_inodes_bitmap",
540 "Wrong free inodes count in super block, "
541 "stored = %lu, counted = %lu",
542 (unsigned long) le32_to_cpu(es->s_free_inodes_count),
543 bitmap_count);
545 #endif