aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext2/ialloc.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/ext2/ialloc.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/ext2/ialloc.c')
-rw-r--r--fs/ext2/ialloc.c735
1 files changed, 735 insertions, 0 deletions
diff --git a/fs/ext2/ialloc.c b/fs/ext2/ialloc.c
new file mode 100644
index 000000000000..77e059149212
--- /dev/null
+++ b/fs/ext2/ialloc.c
@@ -0,0 +1,735 @@
1/*
2 * linux/fs/ext2/ialloc.c
3 *
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)
8 *
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
13 */
14
15#include <linux/config.h>
16#include <linux/quotaops.h>
17#include <linux/sched.h>
18#include <linux/backing-dev.h>
19#include <linux/buffer_head.h>
20#include <linux/random.h>
21#include "ext2.h"
22#include "xattr.h"
23#include "acl.h"
24
25/*
26 * ialloc.c contains the inodes allocation and deallocation routines
27 */
28
29/*
30 * The free inodes are managed by bitmaps. A file system contains several
31 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
32 * block for inodes, N blocks for the inode table and data blocks.
33 *
34 * The file system contains group descriptors which are located after the
35 * super block. Each descriptor contains the number of the bitmap block and
36 * the free blocks count in the block.
37 */
38
39
40/*
41 * Read the inode allocation bitmap for a given block_group, reading
42 * into the specified slot in the superblock's bitmap cache.
43 *
44 * Return buffer_head of bitmap on success or NULL.
45 */
46static struct buffer_head *
47read_inode_bitmap(struct super_block * sb, unsigned long block_group)
48{
49 struct ext2_group_desc *desc;
50 struct buffer_head *bh = NULL;
51
52 desc = ext2_get_group_desc(sb, block_group, NULL);
53 if (!desc)
54 goto error_out;
55
56 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
57 if (!bh)
58 ext2_error(sb, "read_inode_bitmap",
59 "Cannot read inode bitmap - "
60 "block_group = %lu, inode_bitmap = %u",
61 block_group, le32_to_cpu(desc->bg_inode_bitmap));
62error_out:
63 return bh;
64}
65
66static void ext2_release_inode(struct super_block *sb, int group, int dir)
67{
68 struct ext2_group_desc * desc;
69 struct buffer_head *bh;
70
71 desc = ext2_get_group_desc(sb, group, &bh);
72 if (!desc) {
73 ext2_error(sb, "ext2_release_inode",
74 "can't get descriptor for group %d", group);
75 return;
76 }
77
78 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
79 desc->bg_free_inodes_count =
80 cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
81 if (dir)
82 desc->bg_used_dirs_count =
83 cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
84 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
85 if (dir)
86 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
87 sb->s_dirt = 1;
88 mark_buffer_dirty(bh);
89}
90
91/*
92 * NOTE! When we get the inode, we're the only people
93 * that have access to it, and as such there are no
94 * race conditions we have to worry about. The inode
95 * is not on the hash-lists, and it cannot be reached
96 * through the filesystem because the directory entry
97 * has been deleted earlier.
98 *
99 * HOWEVER: we must make sure that we get no aliases,
100 * which means that we have to call "clear_inode()"
101 * _before_ we mark the inode not in use in the inode
102 * bitmaps. Otherwise a newly created file might use
103 * the same inode number (not actually the same pointer
104 * though), and then we'd have two inodes sharing the
105 * same inode number and space on the harddisk.
106 */
107void ext2_free_inode (struct inode * inode)
108{
109 struct super_block * sb = inode->i_sb;
110 int is_directory;
111 unsigned long ino;
112 struct buffer_head *bitmap_bh = NULL;
113 unsigned long block_group;
114 unsigned long bit;
115 struct ext2_super_block * es;
116
117 ino = inode->i_ino;
118 ext2_debug ("freeing inode %lu\n", ino);
119
120 /*
121 * Note: we must free any quota before locking the superblock,
122 * as writing the quota to disk may need the lock as well.
123 */
124 if (!is_bad_inode(inode)) {
125 /* Quota is already initialized in iput() */
126 ext2_xattr_delete_inode(inode);
127 DQUOT_FREE_INODE(inode);
128 DQUOT_DROP(inode);
129 }
130
131 es = EXT2_SB(sb)->s_es;
132 is_directory = S_ISDIR(inode->i_mode);
133
134 /* Do this BEFORE marking the inode not in use or returning an error */
135 clear_inode (inode);
136
137 if (ino < EXT2_FIRST_INO(sb) ||
138 ino > le32_to_cpu(es->s_inodes_count)) {
139 ext2_error (sb, "ext2_free_inode",
140 "reserved or nonexistent inode %lu", ino);
141 goto error_return;
142 }
143 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
144 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
145 brelse(bitmap_bh);
146 bitmap_bh = read_inode_bitmap(sb, block_group);
147 if (!bitmap_bh)
148 goto error_return;
149
150 /* Ok, now we can actually update the inode bitmaps.. */
151 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
152 bit, (void *) bitmap_bh->b_data))
153 ext2_error (sb, "ext2_free_inode",
154 "bit already cleared for inode %lu", ino);
155 else
156 ext2_release_inode(sb, block_group, is_directory);
157 mark_buffer_dirty(bitmap_bh);
158 if (sb->s_flags & MS_SYNCHRONOUS)
159 sync_dirty_buffer(bitmap_bh);
160error_return:
161 brelse(bitmap_bh);
162}
163
164/*
165 * We perform asynchronous prereading of the new inode's inode block when
166 * we create the inode, in the expectation that the inode will be written
167 * back soon. There are two reasons:
168 *
169 * - When creating a large number of files, the async prereads will be
170 * nicely merged into large reads
171 * - When writing out a large number of inodes, we don't need to keep on
172 * stalling the writes while we read the inode block.
173 *
174 * FIXME: ext2_get_group_desc() needs to be simplified.
175 */
176static void ext2_preread_inode(struct inode *inode)
177{
178 unsigned long block_group;
179 unsigned long offset;
180 unsigned long block;
181 struct buffer_head *bh;
182 struct ext2_group_desc * gdp;
183 struct backing_dev_info *bdi;
184
185 bdi = inode->i_mapping->backing_dev_info;
186 if (bdi_read_congested(bdi))
187 return;
188 if (bdi_write_congested(bdi))
189 return;
190
191 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
192 gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
193 if (gdp == NULL)
194 return;
195
196 /*
197 * Figure out the offset within the block group inode table
198 */
199 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
200 EXT2_INODE_SIZE(inode->i_sb);
201 block = le32_to_cpu(gdp->bg_inode_table) +
202 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
203 sb_breadahead(inode->i_sb, block);
204}
205
206/*
207 * There are two policies for allocating an inode. If the new inode is
208 * a directory, then a forward search is made for a block group with both
209 * free space and a low directory-to-inode ratio; if that fails, then of
210 * the groups with above-average free space, that group with the fewest
211 * directories already is chosen.
212 *
213 * For other inodes, search forward from the parent directory\'s block
214 * group to find a free inode.
215 */
216static int find_group_dir(struct super_block *sb, struct inode *parent)
217{
218 int ngroups = EXT2_SB(sb)->s_groups_count;
219 int avefreei = ext2_count_free_inodes(sb) / ngroups;
220 struct ext2_group_desc *desc, *best_desc = NULL;
221 struct buffer_head *bh, *best_bh = NULL;
222 int group, best_group = -1;
223
224 for (group = 0; group < ngroups; group++) {
225 desc = ext2_get_group_desc (sb, group, &bh);
226 if (!desc || !desc->bg_free_inodes_count)
227 continue;
228 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
229 continue;
230 if (!best_desc ||
231 (le16_to_cpu(desc->bg_free_blocks_count) >
232 le16_to_cpu(best_desc->bg_free_blocks_count))) {
233 best_group = group;
234 best_desc = desc;
235 best_bh = bh;
236 }
237 }
238 if (!best_desc)
239 return -1;
240
241 return best_group;
242}
243
244/*
245 * Orlov's allocator for directories.
246 *
247 * We always try to spread first-level directories.
248 *
249 * If there are blockgroups with both free inodes and free blocks counts
250 * not worse than average we return one with smallest directory count.
251 * Otherwise we simply return a random group.
252 *
253 * For the rest rules look so:
254 *
255 * It's OK to put directory into a group unless
256 * it has too many directories already (max_dirs) or
257 * it has too few free inodes left (min_inodes) or
258 * it has too few free blocks left (min_blocks) or
259 * it's already running too large debt (max_debt).
260 * Parent's group is prefered, if it doesn't satisfy these
261 * conditions we search cyclically through the rest. If none
262 * of the groups look good we just look for a group with more
263 * free inodes than average (starting at parent's group).
264 *
265 * Debt is incremented each time we allocate a directory and decremented
266 * when we allocate an inode, within 0--255.
267 */
268
269#define INODE_COST 64
270#define BLOCK_COST 256
271
272static int find_group_orlov(struct super_block *sb, struct inode *parent)
273{
274 int parent_group = EXT2_I(parent)->i_block_group;
275 struct ext2_sb_info *sbi = EXT2_SB(sb);
276 struct ext2_super_block *es = sbi->s_es;
277 int ngroups = sbi->s_groups_count;
278 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
279 int freei;
280 int avefreei;
281 int free_blocks;
282 int avefreeb;
283 int blocks_per_dir;
284 int ndirs;
285 int max_debt, max_dirs, min_blocks, min_inodes;
286 int group = -1, i;
287 struct ext2_group_desc *desc;
288 struct buffer_head *bh;
289
290 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
291 avefreei = freei / ngroups;
292 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
293 avefreeb = free_blocks / ngroups;
294 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
295
296 if ((parent == sb->s_root->d_inode) ||
297 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
298 struct ext2_group_desc *best_desc = NULL;
299 struct buffer_head *best_bh = NULL;
300 int best_ndir = inodes_per_group;
301 int best_group = -1;
302
303 get_random_bytes(&group, sizeof(group));
304 parent_group = (unsigned)group % ngroups;
305 for (i = 0; i < ngroups; i++) {
306 group = (parent_group + i) % ngroups;
307 desc = ext2_get_group_desc (sb, group, &bh);
308 if (!desc || !desc->bg_free_inodes_count)
309 continue;
310 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
311 continue;
312 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
313 continue;
314 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
315 continue;
316 best_group = group;
317 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
318 best_desc = desc;
319 best_bh = bh;
320 }
321 if (best_group >= 0) {
322 desc = best_desc;
323 bh = best_bh;
324 group = best_group;
325 goto found;
326 }
327 goto fallback;
328 }
329
330 if (ndirs == 0)
331 ndirs = 1; /* percpu_counters are approximate... */
332
333 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
334
335 max_dirs = ndirs / ngroups + inodes_per_group / 16;
336 min_inodes = avefreei - inodes_per_group / 4;
337 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
338
339 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
340 if (max_debt * INODE_COST > inodes_per_group)
341 max_debt = inodes_per_group / INODE_COST;
342 if (max_debt > 255)
343 max_debt = 255;
344 if (max_debt == 0)
345 max_debt = 1;
346
347 for (i = 0; i < ngroups; i++) {
348 group = (parent_group + i) % ngroups;
349 desc = ext2_get_group_desc (sb, group, &bh);
350 if (!desc || !desc->bg_free_inodes_count)
351 continue;
352 if (sbi->s_debts[group] >= max_debt)
353 continue;
354 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
355 continue;
356 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
357 continue;
358 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
359 continue;
360 goto found;
361 }
362
363fallback:
364 for (i = 0; i < ngroups; i++) {
365 group = (parent_group + i) % ngroups;
366 desc = ext2_get_group_desc (sb, group, &bh);
367 if (!desc || !desc->bg_free_inodes_count)
368 continue;
369 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
370 goto found;
371 }
372
373 if (avefreei) {
374 /*
375 * The free-inodes counter is approximate, and for really small
376 * filesystems the above test can fail to find any blockgroups
377 */
378 avefreei = 0;
379 goto fallback;
380 }
381
382 return -1;
383
384found:
385 return group;
386}
387
388static int find_group_other(struct super_block *sb, struct inode *parent)
389{
390 int parent_group = EXT2_I(parent)->i_block_group;
391 int ngroups = EXT2_SB(sb)->s_groups_count;
392 struct ext2_group_desc *desc;
393 struct buffer_head *bh;
394 int group, i;
395
396 /*
397 * Try to place the inode in its parent directory
398 */
399 group = parent_group;
400 desc = ext2_get_group_desc (sb, group, &bh);
401 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
402 le16_to_cpu(desc->bg_free_blocks_count))
403 goto found;
404
405 /*
406 * We're going to place this inode in a different blockgroup from its
407 * parent. We want to cause files in a common directory to all land in
408 * the same blockgroup. But we want files which are in a different
409 * directory which shares a blockgroup with our parent to land in a
410 * different blockgroup.
411 *
412 * So add our directory's i_ino into the starting point for the hash.
413 */
414 group = (group + parent->i_ino) % ngroups;
415
416 /*
417 * Use a quadratic hash to find a group with a free inode and some
418 * free blocks.
419 */
420 for (i = 1; i < ngroups; i <<= 1) {
421 group += i;
422 if (group >= ngroups)
423 group -= ngroups;
424 desc = ext2_get_group_desc (sb, group, &bh);
425 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
426 le16_to_cpu(desc->bg_free_blocks_count))
427 goto found;
428 }
429
430 /*
431 * That failed: try linear search for a free inode, even if that group
432 * has no free blocks.
433 */
434 group = parent_group;
435 for (i = 0; i < ngroups; i++) {
436 if (++group >= ngroups)
437 group = 0;
438 desc = ext2_get_group_desc (sb, group, &bh);
439 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
440 goto found;
441 }
442
443 return -1;
444
445found:
446 return group;
447}
448
449struct inode *ext2_new_inode(struct inode *dir, int mode)
450{
451 struct super_block *sb;
452 struct buffer_head *bitmap_bh = NULL;
453 struct buffer_head *bh2;
454 int group, i;
455 ino_t ino = 0;
456 struct inode * inode;
457 struct ext2_group_desc *gdp;
458 struct ext2_super_block *es;
459 struct ext2_inode_info *ei;
460 struct ext2_sb_info *sbi;
461 int err;
462
463 sb = dir->i_sb;
464 inode = new_inode(sb);
465 if (!inode)
466 return ERR_PTR(-ENOMEM);
467
468 ei = EXT2_I(inode);
469 sbi = EXT2_SB(sb);
470 es = sbi->s_es;
471 if (S_ISDIR(mode)) {
472 if (test_opt(sb, OLDALLOC))
473 group = find_group_dir(sb, dir);
474 else
475 group = find_group_orlov(sb, dir);
476 } else
477 group = find_group_other(sb, dir);
478
479 if (group == -1) {
480 err = -ENOSPC;
481 goto fail;
482 }
483
484 for (i = 0; i < sbi->s_groups_count; i++) {
485 gdp = ext2_get_group_desc(sb, group, &bh2);
486 brelse(bitmap_bh);
487 bitmap_bh = read_inode_bitmap(sb, group);
488 if (!bitmap_bh) {
489 err = -EIO;
490 goto fail;
491 }
492 ino = 0;
493
494repeat_in_this_group:
495 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
496 EXT2_INODES_PER_GROUP(sb), ino);
497 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
498 /*
499 * Rare race: find_group_xx() decided that there were
500 * free inodes in this group, but by the time we tried
501 * to allocate one, they're all gone. This can also
502 * occur because the counters which find_group_orlov()
503 * uses are approximate. So just go and search the
504 * next block group.
505 */
506 if (++group == sbi->s_groups_count)
507 group = 0;
508 continue;
509 }
510 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
511 ino, bitmap_bh->b_data)) {
512 /* we lost this inode */
513 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
514 /* this group is exhausted, try next group */
515 if (++group == sbi->s_groups_count)
516 group = 0;
517 continue;
518 }
519 /* try to find free inode in the same group */
520 goto repeat_in_this_group;
521 }
522 goto got;
523 }
524
525 /*
526 * Scanned all blockgroups.
527 */
528 err = -ENOSPC;
529 goto fail;
530got:
531 mark_buffer_dirty(bitmap_bh);
532 if (sb->s_flags & MS_SYNCHRONOUS)
533 sync_dirty_buffer(bitmap_bh);
534 brelse(bitmap_bh);
535
536 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
537 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
538 ext2_error (sb, "ext2_new_inode",
539 "reserved inode or inode > inodes count - "
540 "block_group = %d,inode=%lu", group,
541 (unsigned long) ino);
542 err = -EIO;
543 goto fail;
544 }
545
546 percpu_counter_mod(&sbi->s_freeinodes_counter, -1);
547 if (S_ISDIR(mode))
548 percpu_counter_inc(&sbi->s_dirs_counter);
549
550 spin_lock(sb_bgl_lock(sbi, group));
551 gdp->bg_free_inodes_count =
552 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
553 if (S_ISDIR(mode)) {
554 if (sbi->s_debts[group] < 255)
555 sbi->s_debts[group]++;
556 gdp->bg_used_dirs_count =
557 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
558 } else {
559 if (sbi->s_debts[group])
560 sbi->s_debts[group]--;
561 }
562 spin_unlock(sb_bgl_lock(sbi, group));
563
564 sb->s_dirt = 1;
565 mark_buffer_dirty(bh2);
566 inode->i_uid = current->fsuid;
567 if (test_opt (sb, GRPID))
568 inode->i_gid = dir->i_gid;
569 else if (dir->i_mode & S_ISGID) {
570 inode->i_gid = dir->i_gid;
571 if (S_ISDIR(mode))
572 mode |= S_ISGID;
573 } else
574 inode->i_gid = current->fsgid;
575 inode->i_mode = mode;
576
577 inode->i_ino = ino;
578 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
579 inode->i_blocks = 0;
580 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
581 memset(ei->i_data, 0, sizeof(ei->i_data));
582 ei->i_flags = EXT2_I(dir)->i_flags & ~EXT2_BTREE_FL;
583 if (S_ISLNK(mode))
584 ei->i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
585 /* dirsync is only applied to directories */
586 if (!S_ISDIR(mode))
587 ei->i_flags &= ~EXT2_DIRSYNC_FL;
588 ei->i_faddr = 0;
589 ei->i_frag_no = 0;
590 ei->i_frag_size = 0;
591 ei->i_file_acl = 0;
592 ei->i_dir_acl = 0;
593 ei->i_dtime = 0;
594 ei->i_block_group = group;
595 ei->i_next_alloc_block = 0;
596 ei->i_next_alloc_goal = 0;
597 ei->i_prealloc_block = 0;
598 ei->i_prealloc_count = 0;
599 ei->i_dir_start_lookup = 0;
600 ei->i_state = EXT2_STATE_NEW;
601 ext2_set_inode_flags(inode);
602 spin_lock(&sbi->s_next_gen_lock);
603 inode->i_generation = sbi->s_next_generation++;
604 spin_unlock(&sbi->s_next_gen_lock);
605 insert_inode_hash(inode);
606
607 if (DQUOT_ALLOC_INODE(inode)) {
608 DQUOT_DROP(inode);
609 err = -ENOSPC;
610 goto fail2;
611 }
612 err = ext2_init_acl(inode, dir);
613 if (err) {
614 DQUOT_FREE_INODE(inode);
615 goto fail2;
616 }
617 mark_inode_dirty(inode);
618 ext2_debug("allocating inode %lu\n", inode->i_ino);
619 ext2_preread_inode(inode);
620 return inode;
621
622fail2:
623 inode->i_flags |= S_NOQUOTA;
624 inode->i_nlink = 0;
625 iput(inode);
626 return ERR_PTR(err);
627
628fail:
629 make_bad_inode(inode);
630 iput(inode);
631 return ERR_PTR(err);
632}
633
634unsigned long ext2_count_free_inodes (struct super_block * sb)
635{
636 struct ext2_group_desc *desc;
637 unsigned long desc_count = 0;
638 int i;
639
640#ifdef EXT2FS_DEBUG
641 struct ext2_super_block *es;
642 unsigned long bitmap_count = 0;
643 struct buffer_head *bitmap_bh = NULL;
644
645 lock_super (sb);
646 es = EXT2_SB(sb)->s_es;
647 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
648 unsigned x;
649
650 desc = ext2_get_group_desc (sb, i, NULL);
651 if (!desc)
652 continue;
653 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
654 brelse(bitmap_bh);
655 bitmap_bh = read_inode_bitmap(sb, i);
656 if (!bitmap_bh)
657 continue;
658
659 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
660 printk("group %d: stored = %d, counted = %u\n",
661 i, le16_to_cpu(desc->bg_free_inodes_count), x);
662 bitmap_count += x;
663 }
664 brelse(bitmap_bh);
665 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
666 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
667 desc_count, bitmap_count);
668 unlock_super(sb);
669 return desc_count;
670#else
671 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
672 desc = ext2_get_group_desc (sb, i, NULL);
673 if (!desc)
674 continue;
675 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
676 }
677 return desc_count;
678#endif
679}
680
681/* Called at mount-time, super-block is locked */
682unsigned long ext2_count_dirs (struct super_block * sb)
683{
684 unsigned long count = 0;
685 int i;
686
687 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
688 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
689 if (!gdp)
690 continue;
691 count += le16_to_cpu(gdp->bg_used_dirs_count);
692 }
693 return count;
694}
695
696#ifdef CONFIG_EXT2_CHECK
697/* Called at mount-time, super-block is locked */
698void ext2_check_inodes_bitmap (struct super_block * sb)
699{
700 struct ext2_super_block * es = EXT2_SB(sb)->s_es;
701 unsigned long desc_count = 0, bitmap_count = 0;
702 struct buffer_head *bitmap_bh = NULL;
703 int i;
704
705 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
706 struct ext2_group_desc *desc;
707 unsigned x;
708
709 desc = ext2_get_group_desc(sb, i, NULL);
710 if (!desc)
711 continue;
712 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
713 brelse(bitmap_bh);
714 bitmap_bh = read_inode_bitmap(sb, i);
715 if (!bitmap_bh)
716 continue;
717
718 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
719 if (le16_to_cpu(desc->bg_free_inodes_count) != x)
720 ext2_error (sb, "ext2_check_inodes_bitmap",
721 "Wrong free inodes count in group %d, "
722 "stored = %d, counted = %lu", i,
723 le16_to_cpu(desc->bg_free_inodes_count), x);
724 bitmap_count += x;
725 }
726 brelse(bitmap_bh);
727 if (percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter) !=
728 bitmap_count)
729 ext2_error(sb, "ext2_check_inodes_bitmap",
730 "Wrong free inodes count in super block, "
731 "stored = %lu, counted = %lu",
732 (unsigned long)le32_to_cpu(es->s_free_inodes_count),
733 bitmap_count);
734}
735#endif