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