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