gru: update GRU structures to match latest hardware spec
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ext2 / ialloc.c
blob15387c9c17d8f77f36c7eeb053a9fcb779f15598
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/quotaops.h>
16 #include <linux/sched.h>
17 #include <linux/backing-dev.h>
18 #include <linux/buffer_head.h>
19 #include <linux/random.h>
20 #include "ext2.h"
21 #include "xattr.h"
22 #include "acl.h"
25 * ialloc.c contains the inodes allocation and deallocation routines
29 * The free inodes are managed by bitmaps. A file system contains several
30 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
31 * block for inodes, N blocks for the inode table and data blocks.
33 * The file system contains group descriptors which are located after the
34 * super block. Each descriptor contains the number of the bitmap block and
35 * the free blocks count in the block.
40 * Read the inode allocation bitmap for a given block_group, reading
41 * into the specified slot in the superblock's bitmap cache.
43 * Return buffer_head of bitmap on success or NULL.
45 static struct buffer_head *
46 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
48 struct ext2_group_desc *desc;
49 struct buffer_head *bh = NULL;
51 desc = ext2_get_group_desc(sb, block_group, NULL);
52 if (!desc)
53 goto error_out;
55 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
56 if (!bh)
57 ext2_error(sb, "read_inode_bitmap",
58 "Cannot read inode bitmap - "
59 "block_group = %lu, inode_bitmap = %u",
60 block_group, le32_to_cpu(desc->bg_inode_bitmap));
61 error_out:
62 return bh;
65 static void ext2_release_inode(struct super_block *sb, int group, int dir)
67 struct ext2_group_desc * desc;
68 struct buffer_head *bh;
70 desc = ext2_get_group_desc(sb, group, &bh);
71 if (!desc) {
72 ext2_error(sb, "ext2_release_inode",
73 "can't get descriptor for group %d", group);
74 return;
77 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
78 le16_add_cpu(&desc->bg_free_inodes_count, 1);
79 if (dir)
80 le16_add_cpu(&desc->bg_used_dirs_count, -1);
81 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
82 if (dir)
83 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
84 sb->s_dirt = 1;
85 mark_buffer_dirty(bh);
89 * NOTE! When we get the inode, we're the only people
90 * that have access to it, and as such there are no
91 * race conditions we have to worry about. The inode
92 * is not on the hash-lists, and it cannot be reached
93 * through the filesystem because the directory entry
94 * has been deleted earlier.
96 * HOWEVER: we must make sure that we get no aliases,
97 * which means that we have to call "clear_inode()"
98 * _before_ we mark the inode not in use in the inode
99 * bitmaps. Otherwise a newly created file might use
100 * the same inode number (not actually the same pointer
101 * though), and then we'd have two inodes sharing the
102 * same inode number and space on the harddisk.
104 void ext2_free_inode (struct inode * inode)
106 struct super_block * sb = inode->i_sb;
107 int is_directory;
108 unsigned long ino;
109 struct buffer_head *bitmap_bh = NULL;
110 unsigned long block_group;
111 unsigned long bit;
112 struct ext2_super_block * es;
114 ino = inode->i_ino;
115 ext2_debug ("freeing inode %lu\n", ino);
118 * Note: we must free any quota before locking the superblock,
119 * as writing the quota to disk may need the lock as well.
121 if (!is_bad_inode(inode)) {
122 /* Quota is already initialized in iput() */
123 ext2_xattr_delete_inode(inode);
124 vfs_dq_free_inode(inode);
125 vfs_dq_drop(inode);
128 es = EXT2_SB(sb)->s_es;
129 is_directory = S_ISDIR(inode->i_mode);
131 /* Do this BEFORE marking the inode not in use or returning an error */
132 clear_inode (inode);
134 if (ino < EXT2_FIRST_INO(sb) ||
135 ino > le32_to_cpu(es->s_inodes_count)) {
136 ext2_error (sb, "ext2_free_inode",
137 "reserved or nonexistent inode %lu", ino);
138 goto error_return;
140 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
141 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
142 brelse(bitmap_bh);
143 bitmap_bh = read_inode_bitmap(sb, block_group);
144 if (!bitmap_bh)
145 goto error_return;
147 /* Ok, now we can actually update the inode bitmaps.. */
148 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
149 bit, (void *) bitmap_bh->b_data))
150 ext2_error (sb, "ext2_free_inode",
151 "bit already cleared for inode %lu", ino);
152 else
153 ext2_release_inode(sb, block_group, is_directory);
154 mark_buffer_dirty(bitmap_bh);
155 if (sb->s_flags & MS_SYNCHRONOUS)
156 sync_dirty_buffer(bitmap_bh);
157 error_return:
158 brelse(bitmap_bh);
162 * We perform asynchronous prereading of the new inode's inode block when
163 * we create the inode, in the expectation that the inode will be written
164 * back soon. There are two reasons:
166 * - When creating a large number of files, the async prereads will be
167 * nicely merged into large reads
168 * - When writing out a large number of inodes, we don't need to keep on
169 * stalling the writes while we read the inode block.
171 * FIXME: ext2_get_group_desc() needs to be simplified.
173 static void ext2_preread_inode(struct inode *inode)
175 unsigned long block_group;
176 unsigned long offset;
177 unsigned long block;
178 struct ext2_group_desc * gdp;
179 struct backing_dev_info *bdi;
181 bdi = inode->i_mapping->backing_dev_info;
182 if (bdi_read_congested(bdi))
183 return;
184 if (bdi_write_congested(bdi))
185 return;
187 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
188 gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
189 if (gdp == NULL)
190 return;
193 * Figure out the offset within the block group inode table
195 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
196 EXT2_INODE_SIZE(inode->i_sb);
197 block = le32_to_cpu(gdp->bg_inode_table) +
198 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
199 sb_breadahead(inode->i_sb, block);
203 * There are two policies for allocating an inode. If the new inode is
204 * a directory, then a forward search is made for a block group with both
205 * free space and a low directory-to-inode ratio; if that fails, then of
206 * the groups with above-average free space, that group with the fewest
207 * directories already is chosen.
209 * For other inodes, search forward from the parent directory\'s block
210 * group to find a free inode.
212 static int find_group_dir(struct super_block *sb, struct inode *parent)
214 int ngroups = EXT2_SB(sb)->s_groups_count;
215 int avefreei = ext2_count_free_inodes(sb) / ngroups;
216 struct ext2_group_desc *desc, *best_desc = NULL;
217 int group, best_group = -1;
219 for (group = 0; group < ngroups; group++) {
220 desc = ext2_get_group_desc (sb, group, NULL);
221 if (!desc || !desc->bg_free_inodes_count)
222 continue;
223 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
224 continue;
225 if (!best_desc ||
226 (le16_to_cpu(desc->bg_free_blocks_count) >
227 le16_to_cpu(best_desc->bg_free_blocks_count))) {
228 best_group = group;
229 best_desc = desc;
232 if (!best_desc)
233 return -1;
235 return best_group;
239 * Orlov's allocator for directories.
241 * We always try to spread first-level directories.
243 * If there are blockgroups with both free inodes and free blocks counts
244 * not worse than average we return one with smallest directory count.
245 * Otherwise we simply return a random group.
247 * For the rest rules look so:
249 * It's OK to put directory into a group unless
250 * it has too many directories already (max_dirs) or
251 * it has too few free inodes left (min_inodes) or
252 * it has too few free blocks left (min_blocks) or
253 * it's already running too large debt (max_debt).
254 * Parent's group is preferred, if it doesn't satisfy these
255 * conditions we search cyclically through the rest. If none
256 * of the groups look good we just look for a group with more
257 * free inodes than average (starting at parent's group).
259 * Debt is incremented each time we allocate a directory and decremented
260 * when we allocate an inode, within 0--255.
263 #define INODE_COST 64
264 #define BLOCK_COST 256
266 static int find_group_orlov(struct super_block *sb, struct inode *parent)
268 int parent_group = EXT2_I(parent)->i_block_group;
269 struct ext2_sb_info *sbi = EXT2_SB(sb);
270 struct ext2_super_block *es = sbi->s_es;
271 int ngroups = sbi->s_groups_count;
272 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
273 int freei;
274 int avefreei;
275 int free_blocks;
276 int avefreeb;
277 int blocks_per_dir;
278 int ndirs;
279 int max_debt, max_dirs, min_blocks, min_inodes;
280 int group = -1, i;
281 struct ext2_group_desc *desc;
283 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
284 avefreei = freei / ngroups;
285 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
286 avefreeb = free_blocks / ngroups;
287 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
289 if ((parent == sb->s_root->d_inode) ||
290 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
291 struct ext2_group_desc *best_desc = NULL;
292 int best_ndir = inodes_per_group;
293 int best_group = -1;
295 get_random_bytes(&group, sizeof(group));
296 parent_group = (unsigned)group % ngroups;
297 for (i = 0; i < ngroups; i++) {
298 group = (parent_group + i) % ngroups;
299 desc = ext2_get_group_desc (sb, group, NULL);
300 if (!desc || !desc->bg_free_inodes_count)
301 continue;
302 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
303 continue;
304 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
305 continue;
306 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
307 continue;
308 best_group = group;
309 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
310 best_desc = desc;
312 if (best_group >= 0) {
313 desc = best_desc;
314 group = best_group;
315 goto found;
317 goto fallback;
320 if (ndirs == 0)
321 ndirs = 1; /* percpu_counters are approximate... */
323 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
325 max_dirs = ndirs / ngroups + inodes_per_group / 16;
326 min_inodes = avefreei - inodes_per_group / 4;
327 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
329 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
330 if (max_debt * INODE_COST > inodes_per_group)
331 max_debt = inodes_per_group / INODE_COST;
332 if (max_debt > 255)
333 max_debt = 255;
334 if (max_debt == 0)
335 max_debt = 1;
337 for (i = 0; i < ngroups; i++) {
338 group = (parent_group + i) % ngroups;
339 desc = ext2_get_group_desc (sb, group, NULL);
340 if (!desc || !desc->bg_free_inodes_count)
341 continue;
342 if (sbi->s_debts[group] >= max_debt)
343 continue;
344 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
345 continue;
346 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
347 continue;
348 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
349 continue;
350 goto found;
353 fallback:
354 for (i = 0; i < ngroups; i++) {
355 group = (parent_group + i) % ngroups;
356 desc = ext2_get_group_desc (sb, group, NULL);
357 if (!desc || !desc->bg_free_inodes_count)
358 continue;
359 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
360 goto found;
363 if (avefreei) {
365 * The free-inodes counter is approximate, and for really small
366 * filesystems the above test can fail to find any blockgroups
368 avefreei = 0;
369 goto fallback;
372 return -1;
374 found:
375 return group;
378 static int find_group_other(struct super_block *sb, struct inode *parent)
380 int parent_group = EXT2_I(parent)->i_block_group;
381 int ngroups = EXT2_SB(sb)->s_groups_count;
382 struct ext2_group_desc *desc;
383 int group, i;
386 * Try to place the inode in its parent directory
388 group = parent_group;
389 desc = ext2_get_group_desc (sb, group, NULL);
390 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
391 le16_to_cpu(desc->bg_free_blocks_count))
392 goto found;
395 * We're going to place this inode in a different blockgroup from its
396 * parent. We want to cause files in a common directory to all land in
397 * the same blockgroup. But we want files which are in a different
398 * directory which shares a blockgroup with our parent to land in a
399 * different blockgroup.
401 * So add our directory's i_ino into the starting point for the hash.
403 group = (group + parent->i_ino) % ngroups;
406 * Use a quadratic hash to find a group with a free inode and some
407 * free blocks.
409 for (i = 1; i < ngroups; i <<= 1) {
410 group += i;
411 if (group >= ngroups)
412 group -= ngroups;
413 desc = ext2_get_group_desc (sb, group, NULL);
414 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
415 le16_to_cpu(desc->bg_free_blocks_count))
416 goto found;
420 * That failed: try linear search for a free inode, even if that group
421 * has no free blocks.
423 group = parent_group;
424 for (i = 0; i < ngroups; i++) {
425 if (++group >= ngroups)
426 group = 0;
427 desc = ext2_get_group_desc (sb, group, NULL);
428 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
429 goto found;
432 return -1;
434 found:
435 return group;
438 struct inode *ext2_new_inode(struct inode *dir, int mode)
440 struct super_block *sb;
441 struct buffer_head *bitmap_bh = NULL;
442 struct buffer_head *bh2;
443 int group, i;
444 ino_t ino = 0;
445 struct inode * inode;
446 struct ext2_group_desc *gdp;
447 struct ext2_super_block *es;
448 struct ext2_inode_info *ei;
449 struct ext2_sb_info *sbi;
450 int err;
452 sb = dir->i_sb;
453 inode = new_inode(sb);
454 if (!inode)
455 return ERR_PTR(-ENOMEM);
457 ei = EXT2_I(inode);
458 sbi = EXT2_SB(sb);
459 es = sbi->s_es;
460 if (S_ISDIR(mode)) {
461 if (test_opt(sb, OLDALLOC))
462 group = find_group_dir(sb, dir);
463 else
464 group = find_group_orlov(sb, dir);
465 } else
466 group = find_group_other(sb, dir);
468 if (group == -1) {
469 err = -ENOSPC;
470 goto fail;
473 for (i = 0; i < sbi->s_groups_count; i++) {
474 gdp = ext2_get_group_desc(sb, group, &bh2);
475 brelse(bitmap_bh);
476 bitmap_bh = read_inode_bitmap(sb, group);
477 if (!bitmap_bh) {
478 err = -EIO;
479 goto fail;
481 ino = 0;
483 repeat_in_this_group:
484 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
485 EXT2_INODES_PER_GROUP(sb), ino);
486 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
488 * Rare race: find_group_xx() decided that there were
489 * free inodes in this group, but by the time we tried
490 * to allocate one, they're all gone. This can also
491 * occur because the counters which find_group_orlov()
492 * uses are approximate. So just go and search the
493 * next block group.
495 if (++group == sbi->s_groups_count)
496 group = 0;
497 continue;
499 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
500 ino, bitmap_bh->b_data)) {
501 /* we lost this inode */
502 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
503 /* this group is exhausted, try next group */
504 if (++group == sbi->s_groups_count)
505 group = 0;
506 continue;
508 /* try to find free inode in the same group */
509 goto repeat_in_this_group;
511 goto got;
515 * Scanned all blockgroups.
517 err = -ENOSPC;
518 goto fail;
519 got:
520 mark_buffer_dirty(bitmap_bh);
521 if (sb->s_flags & MS_SYNCHRONOUS)
522 sync_dirty_buffer(bitmap_bh);
523 brelse(bitmap_bh);
525 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
526 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
527 ext2_error (sb, "ext2_new_inode",
528 "reserved inode or inode > inodes count - "
529 "block_group = %d,inode=%lu", group,
530 (unsigned long) ino);
531 err = -EIO;
532 goto fail;
535 percpu_counter_add(&sbi->s_freeinodes_counter, -1);
536 if (S_ISDIR(mode))
537 percpu_counter_inc(&sbi->s_dirs_counter);
539 spin_lock(sb_bgl_lock(sbi, group));
540 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
541 if (S_ISDIR(mode)) {
542 if (sbi->s_debts[group] < 255)
543 sbi->s_debts[group]++;
544 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
545 } else {
546 if (sbi->s_debts[group])
547 sbi->s_debts[group]--;
549 spin_unlock(sb_bgl_lock(sbi, group));
551 sb->s_dirt = 1;
552 mark_buffer_dirty(bh2);
553 inode->i_uid = current_fsuid();
554 if (test_opt (sb, GRPID))
555 inode->i_gid = dir->i_gid;
556 else if (dir->i_mode & S_ISGID) {
557 inode->i_gid = dir->i_gid;
558 if (S_ISDIR(mode))
559 mode |= S_ISGID;
560 } else
561 inode->i_gid = current_fsgid();
562 inode->i_mode = mode;
564 inode->i_ino = ino;
565 inode->i_blocks = 0;
566 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
567 memset(ei->i_data, 0, sizeof(ei->i_data));
568 ei->i_flags =
569 ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
570 ei->i_faddr = 0;
571 ei->i_frag_no = 0;
572 ei->i_frag_size = 0;
573 ei->i_file_acl = 0;
574 ei->i_dir_acl = 0;
575 ei->i_dtime = 0;
576 ei->i_block_alloc_info = NULL;
577 ei->i_block_group = group;
578 ei->i_dir_start_lookup = 0;
579 ei->i_state = EXT2_STATE_NEW;
580 ext2_set_inode_flags(inode);
581 spin_lock(&sbi->s_next_gen_lock);
582 inode->i_generation = sbi->s_next_generation++;
583 spin_unlock(&sbi->s_next_gen_lock);
584 if (insert_inode_locked(inode) < 0) {
585 err = -EINVAL;
586 goto fail_drop;
589 if (vfs_dq_alloc_inode(inode)) {
590 err = -EDQUOT;
591 goto fail_drop;
594 err = ext2_init_acl(inode, dir);
595 if (err)
596 goto fail_free_drop;
598 err = ext2_init_security(inode,dir);
599 if (err)
600 goto fail_free_drop;
602 mark_inode_dirty(inode);
603 ext2_debug("allocating inode %lu\n", inode->i_ino);
604 ext2_preread_inode(inode);
605 return inode;
607 fail_free_drop:
608 vfs_dq_free_inode(inode);
610 fail_drop:
611 vfs_dq_drop(inode);
612 inode->i_flags |= S_NOQUOTA;
613 inode->i_nlink = 0;
614 unlock_new_inode(inode);
615 iput(inode);
616 return ERR_PTR(err);
618 fail:
619 make_bad_inode(inode);
620 iput(inode);
621 return ERR_PTR(err);
624 unsigned long ext2_count_free_inodes (struct super_block * sb)
626 struct ext2_group_desc *desc;
627 unsigned long desc_count = 0;
628 int i;
630 #ifdef EXT2FS_DEBUG
631 struct ext2_super_block *es;
632 unsigned long bitmap_count = 0;
633 struct buffer_head *bitmap_bh = NULL;
635 es = EXT2_SB(sb)->s_es;
636 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
637 unsigned x;
639 desc = ext2_get_group_desc (sb, i, NULL);
640 if (!desc)
641 continue;
642 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
643 brelse(bitmap_bh);
644 bitmap_bh = read_inode_bitmap(sb, i);
645 if (!bitmap_bh)
646 continue;
648 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
649 printk("group %d: stored = %d, counted = %u\n",
650 i, le16_to_cpu(desc->bg_free_inodes_count), x);
651 bitmap_count += x;
653 brelse(bitmap_bh);
654 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
655 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
656 desc_count, bitmap_count);
657 return desc_count;
658 #else
659 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
660 desc = ext2_get_group_desc (sb, i, NULL);
661 if (!desc)
662 continue;
663 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
665 return desc_count;
666 #endif
669 /* Called at mount-time, super-block is locked */
670 unsigned long ext2_count_dirs (struct super_block * sb)
672 unsigned long count = 0;
673 int i;
675 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
676 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
677 if (!gdp)
678 continue;
679 count += le16_to_cpu(gdp->bg_used_dirs_count);
681 return count;