ext3: add support for extent_map library
[linux-2.6-block.git] / fs / ext3 / ialloc.c
1 /*
2  *  linux/fs/ext3/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@redhat.com), 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/time.h>
16 #include <linux/fs.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/stat.h>
21 #include <linux/string.h>
22 #include <linux/quotaops.h>
23 #include <linux/buffer_head.h>
24 #include <linux/random.h>
25 #include <linux/bitops.h>
26 #include <linux/extent_map.h>
27
28 #include <asm/byteorder.h>
29
30 #include "xattr.h"
31 #include "acl.h"
32
33 /*
34  * ialloc.c contains the inodes allocation and deallocation routines
35  */
36
37 /*
38  * The free inodes are managed by bitmaps.  A file system contains several
39  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
40  * block for inodes, N blocks for the inode table and data blocks.
41  *
42  * The file system contains group descriptors which are located after the
43  * super block.  Each descriptor contains the number of the bitmap block and
44  * the free blocks count in the block.
45  */
46
47
48 /*
49  * Read the inode allocation bitmap for a given block_group, reading
50  * into the specified slot in the superblock's bitmap cache.
51  *
52  * Return buffer_head of bitmap on success or NULL.
53  */
54 static struct buffer_head *
55 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
56 {
57         struct ext3_group_desc *desc;
58         struct buffer_head *bh = NULL;
59
60         desc = ext3_get_group_desc(sb, block_group, NULL);
61         if (!desc)
62                 goto error_out;
63
64         bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
65         if (!bh)
66                 ext3_error(sb, "read_inode_bitmap",
67                             "Cannot read inode bitmap - "
68                             "block_group = %lu, inode_bitmap = %u",
69                             block_group, le32_to_cpu(desc->bg_inode_bitmap));
70 error_out:
71         return bh;
72 }
73
74 /*
75  * NOTE! When we get the inode, we're the only people
76  * that have access to it, and as such there are no
77  * race conditions we have to worry about. The inode
78  * is not on the hash-lists, and it cannot be reached
79  * through the filesystem because the directory entry
80  * has been deleted earlier.
81  *
82  * HOWEVER: we must make sure that we get no aliases,
83  * which means that we have to call "clear_inode()"
84  * _before_ we mark the inode not in use in the inode
85  * bitmaps. Otherwise a newly created file might use
86  * the same inode number (not actually the same pointer
87  * though), and then we'd have two inodes sharing the
88  * same inode number and space on the harddisk.
89  */
90 void ext3_free_inode (handle_t *handle, struct inode * inode)
91 {
92         struct super_block * sb = inode->i_sb;
93         int is_directory;
94         unsigned long ino;
95         struct buffer_head *bitmap_bh = NULL;
96         struct buffer_head *bh2;
97         unsigned long block_group;
98         unsigned long bit;
99         struct ext3_group_desc * gdp;
100         struct ext3_super_block * es;
101         struct ext3_sb_info *sbi;
102         int fatal = 0, err;
103
104         if (atomic_read(&inode->i_count) > 1) {
105                 printk ("ext3_free_inode: inode has count=%d\n",
106                                         atomic_read(&inode->i_count));
107                 return;
108         }
109         if (inode->i_nlink) {
110                 printk ("ext3_free_inode: inode has nlink=%d\n",
111                         inode->i_nlink);
112                 return;
113         }
114         if (!sb) {
115                 printk("ext3_free_inode: inode on nonexistent device\n");
116                 return;
117         }
118         sbi = EXT3_SB(sb);
119
120         ino = inode->i_ino;
121         ext3_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         DQUOT_INIT(inode);
128         ext3_xattr_delete_inode(handle, inode);
129         DQUOT_FREE_INODE(inode);
130         DQUOT_DROP(inode);
131
132         is_directory = S_ISDIR(inode->i_mode);
133
134         /* Do this BEFORE marking the inode not in use or returning an error */
135         clear_inode (inode);
136
137         es = EXT3_SB(sb)->s_es;
138         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
139                 ext3_error (sb, "ext3_free_inode",
140                             "reserved or nonexistent inode %lu", ino);
141                 goto error_return;
142         }
143         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
144         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
145         bitmap_bh = read_inode_bitmap(sb, block_group);
146         if (!bitmap_bh)
147                 goto error_return;
148
149         BUFFER_TRACE(bitmap_bh, "get_write_access");
150         fatal = ext3_journal_get_write_access(handle, bitmap_bh);
151         if (fatal)
152                 goto error_return;
153
154         /* Ok, now we can actually update the inode bitmaps.. */
155         if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
156                                         bit, bitmap_bh->b_data))
157                 ext3_error (sb, "ext3_free_inode",
158                               "bit already cleared for inode %lu", ino);
159         else {
160                 gdp = ext3_get_group_desc (sb, block_group, &bh2);
161
162                 BUFFER_TRACE(bh2, "get_write_access");
163                 fatal = ext3_journal_get_write_access(handle, bh2);
164                 if (fatal) goto error_return;
165
166                 if (gdp) {
167                         spin_lock(sb_bgl_lock(sbi, block_group));
168                         le16_add_cpu(&gdp->bg_free_inodes_count, 1);
169                         if (is_directory)
170                                 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
171                         spin_unlock(sb_bgl_lock(sbi, block_group));
172                         percpu_counter_inc(&sbi->s_freeinodes_counter);
173                         if (is_directory)
174                                 percpu_counter_dec(&sbi->s_dirs_counter);
175
176                 }
177                 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
178                 err = ext3_journal_dirty_metadata(handle, bh2);
179                 if (!fatal) fatal = err;
180         }
181         BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
182         err = ext3_journal_dirty_metadata(handle, bitmap_bh);
183         if (!fatal)
184                 fatal = err;
185         sb->s_dirt = 1;
186 error_return:
187         brelse(bitmap_bh);
188         ext3_std_error(sb, fatal);
189 }
190
191 /*
192  * There are two policies for allocating an inode.  If the new inode is
193  * a directory, then a forward search is made for a block group with both
194  * free space and a low directory-to-inode ratio; if that fails, then of
195  * the groups with above-average free space, that group with the fewest
196  * directories already is chosen.
197  *
198  * For other inodes, search forward from the parent directory\'s block
199  * group to find a free inode.
200  */
201 static int find_group_dir(struct super_block *sb, struct inode *parent)
202 {
203         int ngroups = EXT3_SB(sb)->s_groups_count;
204         unsigned int freei, avefreei;
205         struct ext3_group_desc *desc, *best_desc = NULL;
206         int group, best_group = -1;
207
208         freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
209         avefreei = freei / ngroups;
210
211         for (group = 0; group < ngroups; group++) {
212                 desc = ext3_get_group_desc (sb, group, NULL);
213                 if (!desc || !desc->bg_free_inodes_count)
214                         continue;
215                 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
216                         continue;
217                 if (!best_desc ||
218                     (le16_to_cpu(desc->bg_free_blocks_count) >
219                      le16_to_cpu(best_desc->bg_free_blocks_count))) {
220                         best_group = group;
221                         best_desc = desc;
222                 }
223         }
224         return best_group;
225 }
226
227 /*
228  * Orlov's allocator for directories.
229  *
230  * We always try to spread first-level directories.
231  *
232  * If there are blockgroups with both free inodes and free blocks counts
233  * not worse than average we return one with smallest directory count.
234  * Otherwise we simply return a random group.
235  *
236  * For the rest rules look so:
237  *
238  * It's OK to put directory into a group unless
239  * it has too many directories already (max_dirs) or
240  * it has too few free inodes left (min_inodes) or
241  * it has too few free blocks left (min_blocks) or
242  * it's already running too large debt (max_debt).
243  * Parent's group is preferred, if it doesn't satisfy these
244  * conditions we search cyclically through the rest. If none
245  * of the groups look good we just look for a group with more
246  * free inodes than average (starting at parent's group).
247  *
248  * Debt is incremented each time we allocate a directory and decremented
249  * when we allocate an inode, within 0--255.
250  */
251
252 #define INODE_COST 64
253 #define BLOCK_COST 256
254
255 static int find_group_orlov(struct super_block *sb, struct inode *parent)
256 {
257         int parent_group = EXT3_I(parent)->i_block_group;
258         struct ext3_sb_info *sbi = EXT3_SB(sb);
259         struct ext3_super_block *es = sbi->s_es;
260         int ngroups = sbi->s_groups_count;
261         int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
262         unsigned int freei, avefreei;
263         ext3_fsblk_t freeb, avefreeb;
264         ext3_fsblk_t blocks_per_dir;
265         unsigned int ndirs;
266         int max_debt, max_dirs, min_inodes;
267         ext3_grpblk_t min_blocks;
268         int group = -1, i;
269         struct ext3_group_desc *desc;
270
271         freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
272         avefreei = freei / ngroups;
273         freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
274         avefreeb = freeb / ngroups;
275         ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
276
277         if ((parent == sb->s_root->d_inode) ||
278             (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
279                 int best_ndir = inodes_per_group;
280                 int best_group = -1;
281
282                 get_random_bytes(&group, sizeof(group));
283                 parent_group = (unsigned)group % ngroups;
284                 for (i = 0; i < ngroups; i++) {
285                         group = (parent_group + i) % ngroups;
286                         desc = ext3_get_group_desc (sb, group, NULL);
287                         if (!desc || !desc->bg_free_inodes_count)
288                                 continue;
289                         if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
290                                 continue;
291                         if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
292                                 continue;
293                         if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
294                                 continue;
295                         best_group = group;
296                         best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
297                 }
298                 if (best_group >= 0)
299                         return best_group;
300                 goto fallback;
301         }
302
303         blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
304
305         max_dirs = ndirs / ngroups + inodes_per_group / 16;
306         min_inodes = avefreei - inodes_per_group / 4;
307         min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
308
309         max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, (ext3_fsblk_t)BLOCK_COST);
310         if (max_debt * INODE_COST > inodes_per_group)
311                 max_debt = inodes_per_group / INODE_COST;
312         if (max_debt > 255)
313                 max_debt = 255;
314         if (max_debt == 0)
315                 max_debt = 1;
316
317         for (i = 0; i < ngroups; i++) {
318                 group = (parent_group + i) % ngroups;
319                 desc = ext3_get_group_desc (sb, group, NULL);
320                 if (!desc || !desc->bg_free_inodes_count)
321                         continue;
322                 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
323                         continue;
324                 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
325                         continue;
326                 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
327                         continue;
328                 return group;
329         }
330
331 fallback:
332         for (i = 0; i < ngroups; i++) {
333                 group = (parent_group + i) % ngroups;
334                 desc = ext3_get_group_desc (sb, group, NULL);
335                 if (!desc || !desc->bg_free_inodes_count)
336                         continue;
337                 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
338                         return group;
339         }
340
341         if (avefreei) {
342                 /*
343                  * The free-inodes counter is approximate, and for really small
344                  * filesystems the above test can fail to find any blockgroups
345                  */
346                 avefreei = 0;
347                 goto fallback;
348         }
349
350         return -1;
351 }
352
353 static int find_group_other(struct super_block *sb, struct inode *parent)
354 {
355         int parent_group = EXT3_I(parent)->i_block_group;
356         int ngroups = EXT3_SB(sb)->s_groups_count;
357         struct ext3_group_desc *desc;
358         int group, i;
359
360         /*
361          * Try to place the inode in its parent directory
362          */
363         group = parent_group;
364         desc = ext3_get_group_desc (sb, group, NULL);
365         if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
366                         le16_to_cpu(desc->bg_free_blocks_count))
367                 return group;
368
369         /*
370          * We're going to place this inode in a different blockgroup from its
371          * parent.  We want to cause files in a common directory to all land in
372          * the same blockgroup.  But we want files which are in a different
373          * directory which shares a blockgroup with our parent to land in a
374          * different blockgroup.
375          *
376          * So add our directory's i_ino into the starting point for the hash.
377          */
378         group = (group + parent->i_ino) % ngroups;
379
380         /*
381          * Use a quadratic hash to find a group with a free inode and some free
382          * blocks.
383          */
384         for (i = 1; i < ngroups; i <<= 1) {
385                 group += i;
386                 if (group >= ngroups)
387                         group -= ngroups;
388                 desc = ext3_get_group_desc (sb, group, NULL);
389                 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
390                                 le16_to_cpu(desc->bg_free_blocks_count))
391                         return group;
392         }
393
394         /*
395          * That failed: try linear search for a free inode, even if that group
396          * has no free blocks.
397          */
398         group = parent_group;
399         for (i = 0; i < ngroups; i++) {
400                 if (++group >= ngroups)
401                         group = 0;
402                 desc = ext3_get_group_desc (sb, group, NULL);
403                 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
404                         return group;
405         }
406
407         return -1;
408 }
409
410 /*
411  * There are two policies for allocating an inode.  If the new inode is
412  * a directory, then a forward search is made for a block group with both
413  * free space and a low directory-to-inode ratio; if that fails, then of
414  * the groups with above-average free space, that group with the fewest
415  * directories already is chosen.
416  *
417  * For other inodes, search forward from the parent directory's block
418  * group to find a free inode.
419  */
420 struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
421 {
422         struct super_block *sb;
423         struct buffer_head *bitmap_bh = NULL;
424         struct buffer_head *bh2;
425         int group;
426         unsigned long ino = 0;
427         struct inode * inode;
428         struct ext3_group_desc * gdp = NULL;
429         struct ext3_super_block * es;
430         struct ext3_inode_info *ei;
431         struct ext3_sb_info *sbi;
432         int err = 0;
433         struct inode *ret;
434         int i;
435
436         /* Cannot create files in a deleted directory */
437         if (!dir || !dir->i_nlink)
438                 return ERR_PTR(-EPERM);
439
440         sb = dir->i_sb;
441         inode = new_inode(sb);
442         if (!inode)
443                 return ERR_PTR(-ENOMEM);
444         ei = EXT3_I(inode);
445
446         sbi = EXT3_SB(sb);
447         es = sbi->s_es;
448         if (S_ISDIR(mode)) {
449                 if (test_opt (sb, OLDALLOC))
450                         group = find_group_dir(sb, dir);
451                 else
452                         group = find_group_orlov(sb, dir);
453         } else
454                 group = find_group_other(sb, dir);
455
456         err = -ENOSPC;
457         if (group == -1)
458                 goto out;
459
460         for (i = 0; i < sbi->s_groups_count; i++) {
461                 err = -EIO;
462
463                 gdp = ext3_get_group_desc(sb, group, &bh2);
464                 if (!gdp)
465                         goto fail;
466
467                 brelse(bitmap_bh);
468                 bitmap_bh = read_inode_bitmap(sb, group);
469                 if (!bitmap_bh)
470                         goto fail;
471
472                 ino = 0;
473
474 repeat_in_this_group:
475                 ino = ext3_find_next_zero_bit((unsigned long *)
476                                 bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
477                 if (ino < EXT3_INODES_PER_GROUP(sb)) {
478
479                         BUFFER_TRACE(bitmap_bh, "get_write_access");
480                         err = ext3_journal_get_write_access(handle, bitmap_bh);
481                         if (err)
482                                 goto fail;
483
484                         if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
485                                                 ino, bitmap_bh->b_data)) {
486                                 /* we won it */
487                                 BUFFER_TRACE(bitmap_bh,
488                                         "call ext3_journal_dirty_metadata");
489                                 err = ext3_journal_dirty_metadata(handle,
490                                                                 bitmap_bh);
491                                 if (err)
492                                         goto fail;
493                                 goto got;
494                         }
495                         /* we lost it */
496                         journal_release_buffer(handle, bitmap_bh);
497
498                         if (++ino < EXT3_INODES_PER_GROUP(sb))
499                                 goto repeat_in_this_group;
500                 }
501
502                 /*
503                  * This case is possible in concurrent environment.  It is very
504                  * rare.  We cannot repeat the find_group_xxx() call because
505                  * that will simply return the same blockgroup, because the
506                  * group descriptor metadata has not yet been updated.
507                  * So we just go onto the next blockgroup.
508                  */
509                 if (++group == sbi->s_groups_count)
510                         group = 0;
511         }
512         err = -ENOSPC;
513         goto out;
514
515 got:
516         ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
517         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
518                 ext3_error (sb, "ext3_new_inode",
519                             "reserved inode or inode > inodes count - "
520                             "block_group = %d, inode=%lu", group, ino);
521                 err = -EIO;
522                 goto fail;
523         }
524
525         BUFFER_TRACE(bh2, "get_write_access");
526         err = ext3_journal_get_write_access(handle, bh2);
527         if (err) goto fail;
528         spin_lock(sb_bgl_lock(sbi, group));
529         le16_add_cpu(&gdp->bg_free_inodes_count, -1);
530         if (S_ISDIR(mode)) {
531                 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
532         }
533         spin_unlock(sb_bgl_lock(sbi, group));
534         BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
535         err = ext3_journal_dirty_metadata(handle, bh2);
536         if (err) goto fail;
537
538         percpu_counter_dec(&sbi->s_freeinodes_counter);
539         if (S_ISDIR(mode))
540                 percpu_counter_inc(&sbi->s_dirs_counter);
541         sb->s_dirt = 1;
542
543         inode->i_uid = current_fsuid();
544         if (test_opt (sb, GRPID))
545                 inode->i_gid = dir->i_gid;
546         else if (dir->i_mode & S_ISGID) {
547                 inode->i_gid = dir->i_gid;
548                 if (S_ISDIR(mode))
549                         mode |= S_ISGID;
550         } else
551                 inode->i_gid = current_fsgid();
552         inode->i_mode = mode;
553
554         inode->i_ino = ino;
555         /* This is the optimal IO size (for stat), not the fs block size */
556         inode->i_blocks = 0;
557         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
558
559         memset(ei->i_data, 0, sizeof(ei->i_data));
560         ei->i_dir_start_lookup = 0;
561         ei->i_disksize = 0;
562
563         ei->i_flags =
564                 ext3_mask_flags(mode, EXT3_I(dir)->i_flags & EXT3_FL_INHERITED);
565 #ifdef EXT3_FRAGMENTS
566         ei->i_faddr = 0;
567         ei->i_frag_no = 0;
568         ei->i_frag_size = 0;
569 #endif
570         ei->i_file_acl = 0;
571         ei->i_dir_acl = 0;
572         ei->i_dtime = 0;
573         ei->i_block_alloc_info = NULL;
574         ei->i_block_group = group;
575         extent_map_tree_init(&ei->extent_tree);
576
577         ext3_set_inode_flags(inode);
578         if (IS_DIRSYNC(inode))
579                 handle->h_sync = 1;
580         if (insert_inode_locked(inode) < 0) {
581                 err = -EINVAL;
582                 goto fail_drop;
583         }
584         spin_lock(&sbi->s_next_gen_lock);
585         inode->i_generation = sbi->s_next_generation++;
586         spin_unlock(&sbi->s_next_gen_lock);
587
588         ei->i_state = EXT3_STATE_NEW;
589         ei->i_extra_isize =
590                 (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) ?
591                 sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE : 0;
592
593         ret = inode;
594         if(DQUOT_ALLOC_INODE(inode)) {
595                 err = -EDQUOT;
596                 goto fail_drop;
597         }
598
599         err = ext3_init_acl(handle, inode, dir);
600         if (err)
601                 goto fail_free_drop;
602
603         err = ext3_init_security(handle,inode, dir);
604         if (err)
605                 goto fail_free_drop;
606
607         err = ext3_mark_inode_dirty(handle, inode);
608         if (err) {
609                 ext3_std_error(sb, err);
610                 goto fail_free_drop;
611         }
612
613         ext3_debug("allocating inode %lu\n", inode->i_ino);
614         goto really_out;
615 fail:
616         ext3_std_error(sb, err);
617 out:
618         iput(inode);
619         ret = ERR_PTR(err);
620 really_out:
621         brelse(bitmap_bh);
622         return ret;
623
624 fail_free_drop:
625         DQUOT_FREE_INODE(inode);
626
627 fail_drop:
628         DQUOT_DROP(inode);
629         inode->i_flags |= S_NOQUOTA;
630         inode->i_nlink = 0;
631         unlock_new_inode(inode);
632         iput(inode);
633         brelse(bitmap_bh);
634         return ERR_PTR(err);
635 }
636
637 /* Verify that we are loading a valid orphan from disk */
638 struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
639 {
640         unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
641         unsigned long block_group;
642         int bit;
643         struct buffer_head *bitmap_bh;
644         struct inode *inode = NULL;
645         long err = -EIO;
646
647         /* Error cases - e2fsck has already cleaned up for us */
648         if (ino > max_ino) {
649                 ext3_warning(sb, __func__,
650                              "bad orphan ino %lu!  e2fsck was run?", ino);
651                 goto error;
652         }
653
654         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
655         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
656         bitmap_bh = read_inode_bitmap(sb, block_group);
657         if (!bitmap_bh) {
658                 ext3_warning(sb, __func__,
659                              "inode bitmap error for orphan %lu", ino);
660                 goto error;
661         }
662
663         /* Having the inode bit set should be a 100% indicator that this
664          * is a valid orphan (no e2fsck run on fs).  Orphans also include
665          * inodes that were being truncated, so we can't check i_nlink==0.
666          */
667         if (!ext3_test_bit(bit, bitmap_bh->b_data))
668                 goto bad_orphan;
669
670         inode = ext3_iget(sb, ino);
671         if (IS_ERR(inode))
672                 goto iget_failed;
673
674         /*
675          * If the orphans has i_nlinks > 0 then it should be able to be
676          * truncated, otherwise it won't be removed from the orphan list
677          * during processing and an infinite loop will result.
678          */
679         if (inode->i_nlink && !ext3_can_truncate(inode))
680                 goto bad_orphan;
681
682         if (NEXT_ORPHAN(inode) > max_ino)
683                 goto bad_orphan;
684         brelse(bitmap_bh);
685         return inode;
686
687 iget_failed:
688         err = PTR_ERR(inode);
689         inode = NULL;
690 bad_orphan:
691         ext3_warning(sb, __func__,
692                      "bad orphan inode %lu!  e2fsck was run?", ino);
693         printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
694                bit, (unsigned long long)bitmap_bh->b_blocknr,
695                ext3_test_bit(bit, bitmap_bh->b_data));
696         printk(KERN_NOTICE "inode=%p\n", inode);
697         if (inode) {
698                 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
699                        is_bad_inode(inode));
700                 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
701                        NEXT_ORPHAN(inode));
702                 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
703                 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
704                 /* Avoid freeing blocks if we got a bad deleted inode */
705                 if (inode->i_nlink == 0)
706                         inode->i_blocks = 0;
707                 iput(inode);
708         }
709         brelse(bitmap_bh);
710 error:
711         return ERR_PTR(err);
712 }
713
714 unsigned long ext3_count_free_inodes (struct super_block * sb)
715 {
716         unsigned long desc_count;
717         struct ext3_group_desc *gdp;
718         int i;
719 #ifdef EXT3FS_DEBUG
720         struct ext3_super_block *es;
721         unsigned long bitmap_count, x;
722         struct buffer_head *bitmap_bh = NULL;
723
724         es = EXT3_SB(sb)->s_es;
725         desc_count = 0;
726         bitmap_count = 0;
727         gdp = NULL;
728         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
729                 gdp = ext3_get_group_desc (sb, i, NULL);
730                 if (!gdp)
731                         continue;
732                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
733                 brelse(bitmap_bh);
734                 bitmap_bh = read_inode_bitmap(sb, i);
735                 if (!bitmap_bh)
736                         continue;
737
738                 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
739                 printk("group %d: stored = %d, counted = %lu\n",
740                         i, le16_to_cpu(gdp->bg_free_inodes_count), x);
741                 bitmap_count += x;
742         }
743         brelse(bitmap_bh);
744         printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
745                 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
746         return desc_count;
747 #else
748         desc_count = 0;
749         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
750                 gdp = ext3_get_group_desc (sb, i, NULL);
751                 if (!gdp)
752                         continue;
753                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
754                 cond_resched();
755         }
756         return desc_count;
757 #endif
758 }
759
760 /* Called at mount-time, super-block is locked */
761 unsigned long ext3_count_dirs (struct super_block * sb)
762 {
763         unsigned long count = 0;
764         int i;
765
766         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
767                 struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
768                 if (!gdp)
769                         continue;
770                 count += le16_to_cpu(gdp->bg_used_dirs_count);
771         }
772         return count;
773 }
774