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