vserver 2.0 rc7
[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_dlimit.h>
22 #include <linux/vserver/xid.h>
23 #include "ext2.h"
24 #include "xattr.h"
25 #include "acl.h"
26
27 /*
28  * ialloc.c contains the inodes allocation and deallocation routines
29  */
30
31 /*
32  * The free inodes are managed by bitmaps.  A file system contains several
33  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
34  * block for inodes, N blocks for the inode table and data blocks.
35  *
36  * The file system contains group descriptors which are located after the
37  * super block.  Each descriptor contains the number of the bitmap block and
38  * the free blocks count in the block.
39  */
40
41
42 /*
43  * Read the inode allocation bitmap for a given block_group, reading
44  * into the specified slot in the superblock's bitmap cache.
45  *
46  * Return buffer_head of bitmap on success or NULL.
47  */
48 static struct buffer_head *
49 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
50 {
51         struct ext2_group_desc *desc;
52         struct buffer_head *bh = NULL;
53
54         desc = ext2_get_group_desc(sb, block_group, NULL);
55         if (!desc)
56                 goto error_out;
57
58         bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
59         if (!bh)
60                 ext2_error(sb, "read_inode_bitmap",
61                             "Cannot read inode bitmap - "
62                             "block_group = %lu, inode_bitmap = %u",
63                             block_group, le32_to_cpu(desc->bg_inode_bitmap));
64 error_out:
65         return bh;
66 }
67
68 static void ext2_release_inode(struct super_block *sb, int group, int dir)
69 {
70         struct ext2_group_desc * desc;
71         struct buffer_head *bh;
72
73         desc = ext2_get_group_desc(sb, group, &bh);
74         if (!desc) {
75                 ext2_error(sb, "ext2_release_inode",
76                         "can't get descriptor for group %d", group);
77                 return;
78         }
79
80         spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
81         desc->bg_free_inodes_count =
82                 cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
83         if (dir)
84                 desc->bg_used_dirs_count =
85                         cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
86         spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
87         if (dir)
88                 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
89         sb->s_dirt = 1;
90         mark_buffer_dirty(bh);
91 }
92
93 /*
94  * NOTE! When we get the inode, we're the only people
95  * that have access to it, and as such there are no
96  * race conditions we have to worry about. The inode
97  * is not on the hash-lists, and it cannot be reached
98  * through the filesystem because the directory entry
99  * has been deleted earlier.
100  *
101  * HOWEVER: we must make sure that we get no aliases,
102  * which means that we have to call "clear_inode()"
103  * _before_ we mark the inode not in use in the inode
104  * bitmaps. Otherwise a newly created file might use
105  * the same inode number (not actually the same pointer
106  * though), and then we'd have two inodes sharing the
107  * same inode number and space on the harddisk.
108  */
109 void ext2_free_inode (struct inode * inode)
110 {
111         struct super_block * sb = inode->i_sb;
112         int is_directory;
113         unsigned long ino;
114         struct buffer_head *bitmap_bh = NULL;
115         unsigned long block_group;
116         unsigned long bit;
117         struct ext2_super_block * es;
118
119         ino = inode->i_ino;
120         ext2_debug ("freeing inode %lu\n", ino);
121
122         /*
123          * Note: we must free any quota before locking the superblock,
124          * as writing the quota to disk may need the lock as well.
125          */
126         if (!is_bad_inode(inode)) {
127                 /* Quota is already initialized in iput() */
128                 ext2_xattr_delete_inode(inode);
129                 DLIMIT_FREE_INODE(sb, inode->i_xid);
130                 DQUOT_FREE_INODE(inode);
131                 DQUOT_DROP(inode);
132         }
133
134         es = EXT2_SB(sb)->s_es;
135         is_directory = S_ISDIR(inode->i_mode);
136
137         /* Do this BEFORE marking the inode not in use or returning an error */
138         clear_inode (inode);
139
140         if (ino < EXT2_FIRST_INO(sb) ||
141             ino > le32_to_cpu(es->s_inodes_count)) {
142                 ext2_error (sb, "ext2_free_inode",
143                             "reserved or nonexistent inode %lu", ino);
144                 goto error_return;
145         }
146         block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
147         bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
148         brelse(bitmap_bh);
149         bitmap_bh = read_inode_bitmap(sb, block_group);
150         if (!bitmap_bh)
151                 goto error_return;
152
153         /* Ok, now we can actually update the inode bitmaps.. */
154         if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
155                                 bit, (void *) bitmap_bh->b_data))
156                 ext2_error (sb, "ext2_free_inode",
157                               "bit already cleared for inode %lu", ino);
158         else
159                 ext2_release_inode(sb, block_group, is_directory);
160         mark_buffer_dirty(bitmap_bh);
161         if (sb->s_flags & MS_SYNCHRONOUS)
162                 sync_dirty_buffer(bitmap_bh);
163 error_return:
164         brelse(bitmap_bh);
165 }
166
167 /*
168  * We perform asynchronous prereading of the new inode's inode block when
169  * we create the inode, in the expectation that the inode will be written
170  * back soon.  There are two reasons:
171  *
172  * - When creating a large number of files, the async prereads will be
173  *   nicely merged into large reads
174  * - When writing out a large number of inodes, we don't need to keep on
175  *   stalling the writes while we read the inode block.
176  *
177  * FIXME: ext2_get_group_desc() needs to be simplified.
178  */
179 static void ext2_preread_inode(struct inode *inode)
180 {
181         unsigned long block_group;
182         unsigned long offset;
183         unsigned long block;
184         struct buffer_head *bh;
185         struct ext2_group_desc * gdp;
186         struct backing_dev_info *bdi;
187
188         bdi = inode->i_mapping->backing_dev_info;
189         if (bdi_read_congested(bdi))
190                 return;
191         if (bdi_write_congested(bdi))
192                 return;
193
194         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
195         gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
196         if (gdp == NULL)
197                 return;
198
199         /*
200          * Figure out the offset within the block group inode table
201          */
202         offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
203                                 EXT2_INODE_SIZE(inode->i_sb);
204         block = le32_to_cpu(gdp->bg_inode_table) +
205                                 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
206         sb_breadahead(inode->i_sb, block);
207 }
208
209 /*
210  * There are two policies for allocating an inode.  If the new inode is
211  * a directory, then a forward search is made for a block group with both
212  * free space and a low directory-to-inode ratio; if that fails, then of
213  * the groups with above-average free space, that group with the fewest
214  * directories already is chosen.
215  *
216  * For other inodes, search forward from the parent directory\'s block
217  * group to find a free inode.
218  */
219 static int find_group_dir(struct super_block *sb, struct inode *parent)
220 {
221         int ngroups = EXT2_SB(sb)->s_groups_count;
222         int avefreei = ext2_count_free_inodes(sb) / ngroups;
223         struct ext2_group_desc *desc, *best_desc = NULL;
224         struct buffer_head *bh, *best_bh = NULL;
225         int group, best_group = -1;
226
227         for (group = 0; group < ngroups; group++) {
228                 desc = ext2_get_group_desc (sb, group, &bh);
229                 if (!desc || !desc->bg_free_inodes_count)
230                         continue;
231                 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
232                         continue;
233                 if (!best_desc || 
234                     (le16_to_cpu(desc->bg_free_blocks_count) >
235                      le16_to_cpu(best_desc->bg_free_blocks_count))) {
236                         best_group = group;
237                         best_desc = desc;
238                         best_bh = bh;
239                 }
240         }
241         if (!best_desc)
242                 return -1;
243
244         return best_group;
245 }
246
247 /* 
248  * Orlov's allocator for directories. 
249  * 
250  * We always try to spread first-level directories.
251  *
252  * If there are blockgroups with both free inodes and free blocks counts 
253  * not worse than average we return one with smallest directory count. 
254  * Otherwise we simply return a random group. 
255  * 
256  * For the rest rules look so: 
257  * 
258  * It's OK to put directory into a group unless 
259  * it has too many directories already (max_dirs) or 
260  * it has too few free inodes left (min_inodes) or 
261  * it has too few free blocks left (min_blocks) or 
262  * it's already running too large debt (max_debt). 
263  * Parent's group is prefered, if it doesn't satisfy these 
264  * conditions we search cyclically through the rest. If none 
265  * of the groups look good we just look for a group with more 
266  * free inodes than average (starting at parent's group). 
267  * 
268  * Debt is incremented each time we allocate a directory and decremented 
269  * when we allocate an inode, within 0--255. 
270  */ 
271
272 #define INODE_COST 64
273 #define BLOCK_COST 256
274
275 static int find_group_orlov(struct super_block *sb, struct inode *parent)
276 {
277         int parent_group = EXT2_I(parent)->i_block_group;
278         struct ext2_sb_info *sbi = EXT2_SB(sb);
279         struct ext2_super_block *es = sbi->s_es;
280         int ngroups = sbi->s_groups_count;
281         int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
282         int freei;
283         int avefreei;
284         int free_blocks;
285         int avefreeb;
286         int blocks_per_dir;
287         int ndirs;
288         int max_debt, max_dirs, min_blocks, min_inodes;
289         int group = -1, i;
290         struct ext2_group_desc *desc;
291         struct buffer_head *bh;
292
293         freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
294         avefreei = freei / ngroups;
295         free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
296         avefreeb = free_blocks / ngroups;
297         ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
298
299         if ((parent == sb->s_root->d_inode) ||
300             (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
301                 struct ext2_group_desc *best_desc = NULL;
302                 struct buffer_head *best_bh = NULL;
303                 int best_ndir = inodes_per_group;
304                 int best_group = -1;
305
306                 get_random_bytes(&group, sizeof(group));
307                 parent_group = (unsigned)group % ngroups;
308                 for (i = 0; i < ngroups; i++) {
309                         group = (parent_group + i) % ngroups;
310                         desc = ext2_get_group_desc (sb, group, &bh);
311                         if (!desc || !desc->bg_free_inodes_count)
312                                 continue;
313                         if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
314                                 continue;
315                         if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
316                                 continue;
317                         if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
318                                 continue;
319                         best_group = group;
320                         best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
321                         best_desc = desc;
322                         best_bh = bh;
323                 }
324                 if (best_group >= 0) {
325                         desc = best_desc;
326                         bh = best_bh;
327                         group = best_group;
328                         goto found;
329                 }
330                 goto fallback;
331         }
332
333         if (ndirs == 0)
334                 ndirs = 1;      /* percpu_counters are approximate... */
335
336         blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
337
338         max_dirs = ndirs / ngroups + inodes_per_group / 16;
339         min_inodes = avefreei - inodes_per_group / 4;
340         min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
341
342         max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
343         if (max_debt * INODE_COST > inodes_per_group)
344                 max_debt = inodes_per_group / INODE_COST;
345         if (max_debt > 255)
346                 max_debt = 255;
347         if (max_debt == 0)
348                 max_debt = 1;
349
350         for (i = 0; i < ngroups; i++) {
351                 group = (parent_group + i) % ngroups;
352                 desc = ext2_get_group_desc (sb, group, &bh);
353                 if (!desc || !desc->bg_free_inodes_count)
354                         continue;
355                 if (sbi->s_debts[group] >= max_debt)
356                         continue;
357                 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
358                         continue;
359                 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
360                         continue;
361                 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
362                         continue;
363                 goto found;
364         }
365
366 fallback:
367         for (i = 0; i < ngroups; i++) {
368                 group = (parent_group + i) % ngroups;
369                 desc = ext2_get_group_desc (sb, group, &bh);
370                 if (!desc || !desc->bg_free_inodes_count)
371                         continue;
372                 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
373                         goto found;
374         }
375
376         if (avefreei) {
377                 /*
378                  * The free-inodes counter is approximate, and for really small
379                  * filesystems the above test can fail to find any blockgroups
380                  */
381                 avefreei = 0;
382                 goto fallback;
383         }
384
385         return -1;
386
387 found:
388         return group;
389 }
390
391 static int find_group_other(struct super_block *sb, struct inode *parent)
392 {
393         int parent_group = EXT2_I(parent)->i_block_group;
394         int ngroups = EXT2_SB(sb)->s_groups_count;
395         struct ext2_group_desc *desc;
396         struct buffer_head *bh;
397         int group, i;
398
399         /*
400          * Try to place the inode in its parent directory
401          */
402         group = parent_group;
403         desc = ext2_get_group_desc (sb, group, &bh);
404         if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
405                         le16_to_cpu(desc->bg_free_blocks_count))
406                 goto found;
407
408         /*
409          * We're going to place this inode in a different blockgroup from its
410          * parent.  We want to cause files in a common directory to all land in
411          * the same blockgroup.  But we want files which are in a different
412          * directory which shares a blockgroup with our parent to land in a
413          * different blockgroup.
414          *
415          * So add our directory's i_ino into the starting point for the hash.
416          */
417         group = (group + parent->i_ino) % ngroups;
418
419         /*
420          * Use a quadratic hash to find a group with a free inode and some
421          * free blocks.
422          */
423         for (i = 1; i < ngroups; i <<= 1) {
424                 group += i;
425                 if (group >= ngroups)
426                         group -= ngroups;
427                 desc = ext2_get_group_desc (sb, group, &bh);
428                 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
429                                 le16_to_cpu(desc->bg_free_blocks_count))
430                         goto found;
431         }
432
433         /*
434          * That failed: try linear search for a free inode, even if that group
435          * has no free blocks.
436          */
437         group = parent_group;
438         for (i = 0; i < ngroups; i++) {
439                 if (++group >= ngroups)
440                         group = 0;
441                 desc = ext2_get_group_desc (sb, group, &bh);
442                 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
443                         goto found;
444         }
445
446         return -1;
447
448 found:
449         return group;
450 }
451
452 struct inode *ext2_new_inode(struct inode *dir, int mode)
453 {
454         struct super_block *sb;
455         struct buffer_head *bitmap_bh = NULL;
456         struct buffer_head *bh2;
457         int group, i;
458         ino_t ino = 0;
459         struct inode * inode;
460         struct ext2_group_desc *gdp;
461         struct ext2_super_block *es;
462         struct ext2_inode_info *ei;
463         struct ext2_sb_info *sbi;
464         int err;
465
466         sb = dir->i_sb;
467         inode = new_inode(sb);
468         if (!inode)
469                 return ERR_PTR(-ENOMEM);
470
471         inode->i_xid = vx_current_fsxid(sb);
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_SEC;
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