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