ext4: split out ext4_free_blocks_after_init()
[pandora-kernel.git] / fs / ext4 / balloc.c
1 /*
2  *  linux/fs/ext4/balloc.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  *  Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
10  *  Big-endian to little-endian byte-swapping/bitmaps by
11  *        David S. Miller (davem@caip.rutgers.edu), 1995
12  */
13
14 #include <linux/time.h>
15 #include <linux/capability.h>
16 #include <linux/fs.h>
17 #include <linux/jbd2.h>
18 #include <linux/quotaops.h>
19 #include <linux/buffer_head.h>
20 #include "ext4.h"
21 #include "ext4_jbd2.h"
22 #include "mballoc.h"
23
24 #include <trace/events/ext4.h>
25
26 static unsigned int num_base_meta_blocks(struct super_block *sb,
27                                          ext4_group_t block_group);
28
29 /*
30  * balloc.c contains the blocks allocation and deallocation routines
31  */
32
33 /*
34  * Calculate the block group number and offset, given a block number
35  */
36 void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
37                 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
38 {
39         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
40         ext4_grpblk_t offset;
41
42         blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
43         offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
44         if (offsetp)
45                 *offsetp = offset;
46         if (blockgrpp)
47                 *blockgrpp = blocknr;
48
49 }
50
51 static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
52                         ext4_group_t block_group)
53 {
54         ext4_group_t actual_group;
55         ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
56         if (actual_group == block_group)
57                 return 1;
58         return 0;
59 }
60
61 static int ext4_group_used_meta_blocks(struct super_block *sb,
62                                        ext4_group_t block_group,
63                                        struct ext4_group_desc *gdp)
64 {
65         ext4_fsblk_t tmp;
66         struct ext4_sb_info *sbi = EXT4_SB(sb);
67         /* block bitmap, inode bitmap, and inode table blocks */
68         int used_blocks = sbi->s_itb_per_group + 2;
69
70         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
71                 if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
72                                         block_group))
73                         used_blocks--;
74
75                 if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
76                                         block_group))
77                         used_blocks--;
78
79                 tmp = ext4_inode_table(sb, gdp);
80                 for (; tmp < ext4_inode_table(sb, gdp) +
81                                 sbi->s_itb_per_group; tmp++) {
82                         if (!ext4_block_in_group(sb, tmp, block_group))
83                                 used_blocks -= 1;
84                 }
85         }
86         return used_blocks;
87 }
88
89 static unsigned int num_blocks_in_group(struct super_block *sb,
90                                         ext4_group_t block_group)
91 {
92         if (block_group == ext4_get_groups_count(sb) - 1) {
93                 /*
94                  * Even though mke2fs always initializes the first and
95                  * last group, just in case some other tool was used,
96                  * we need to make sure we calculate the right free
97                  * blocks.
98                  */
99                 return ext4_blocks_count(EXT4_SB(sb)->s_es) -
100                         ext4_group_first_block_no(sb, block_group);
101         } else
102                 return EXT4_BLOCKS_PER_GROUP(sb);
103 }
104
105 /* Initializes an uninitialized block bitmap */
106 void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
107                             ext4_group_t block_group,
108                             struct ext4_group_desc *gdp)
109 {
110         unsigned int bit, bit_max = num_base_meta_blocks(sb, block_group);
111         struct ext4_sb_info *sbi = EXT4_SB(sb);
112         ext4_fsblk_t start, tmp;
113         int flex_bg = 0;
114
115         J_ASSERT_BH(bh, buffer_locked(bh));
116
117         /* If checksum is bad mark all blocks used to prevent allocation
118          * essentially implementing a per-group read-only flag. */
119         if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
120                 ext4_error(sb, "Checksum bad for group %u", block_group);
121                 ext4_free_blks_set(sb, gdp, 0);
122                 ext4_free_inodes_set(sb, gdp, 0);
123                 ext4_itable_unused_set(sb, gdp, 0);
124                 memset(bh->b_data, 0xff, sb->s_blocksize);
125                 return;
126         }
127         memset(bh->b_data, 0, sb->s_blocksize);
128
129         for (bit = 0; bit < bit_max; bit++)
130                 ext4_set_bit(bit, bh->b_data);
131
132         start = ext4_group_first_block_no(sb, block_group);
133
134         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
135                 flex_bg = 1;
136
137         /* Set bits for block and inode bitmaps, and inode table */
138         tmp = ext4_block_bitmap(sb, gdp);
139         if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
140                 ext4_set_bit(tmp - start, bh->b_data);
141
142         tmp = ext4_inode_bitmap(sb, gdp);
143         if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
144                 ext4_set_bit(tmp - start, bh->b_data);
145
146         tmp = ext4_inode_table(sb, gdp);
147         for (; tmp < ext4_inode_table(sb, gdp) +
148                      sbi->s_itb_per_group; tmp++) {
149                 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
150                         ext4_set_bit(tmp - start, bh->b_data);
151         }
152         /*
153          * Also if the number of blocks within the group is less than
154          * the blocksize * 8 ( which is the size of bitmap ), set rest
155          * of the block bitmap to 1
156          */
157         ext4_mark_bitmap_end(num_blocks_in_group(sb, block_group),
158                              sb->s_blocksize * 8, bh->b_data);
159 }
160
161 /* Return the number of free blocks in a block group.  It is used when
162  * the block bitmap is uninitialized, so we can't just count the bits
163  * in the bitmap. */
164 unsigned ext4_free_blocks_after_init(struct super_block *sb,
165                                      ext4_group_t block_group,
166                                      struct ext4_group_desc *gdp)
167 {
168         return num_blocks_in_group(sb, block_group) -
169                 num_base_meta_blocks(sb, block_group) -
170                 ext4_group_used_meta_blocks(sb, block_group, gdp);
171 }
172
173 /*
174  * The free blocks are managed by bitmaps.  A file system contains several
175  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
176  * block for inodes, N blocks for the inode table and data blocks.
177  *
178  * The file system contains group descriptors which are located after the
179  * super block.  Each descriptor contains the number of the bitmap block and
180  * the free blocks count in the block.  The descriptors are loaded in memory
181  * when a file system is mounted (see ext4_fill_super).
182  */
183
184 /**
185  * ext4_get_group_desc() -- load group descriptor from disk
186  * @sb:                 super block
187  * @block_group:        given block group
188  * @bh:                 pointer to the buffer head to store the block
189  *                      group descriptor
190  */
191 struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
192                                              ext4_group_t block_group,
193                                              struct buffer_head **bh)
194 {
195         unsigned int group_desc;
196         unsigned int offset;
197         ext4_group_t ngroups = ext4_get_groups_count(sb);
198         struct ext4_group_desc *desc;
199         struct ext4_sb_info *sbi = EXT4_SB(sb);
200
201         if (block_group >= ngroups) {
202                 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
203                            " groups_count = %u", block_group, ngroups);
204
205                 return NULL;
206         }
207
208         group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
209         offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
210         if (!sbi->s_group_desc[group_desc]) {
211                 ext4_error(sb, "Group descriptor not loaded - "
212                            "block_group = %u, group_desc = %u, desc = %u",
213                            block_group, group_desc, offset);
214                 return NULL;
215         }
216
217         desc = (struct ext4_group_desc *)(
218                 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
219                 offset * EXT4_DESC_SIZE(sb));
220         if (bh)
221                 *bh = sbi->s_group_desc[group_desc];
222         return desc;
223 }
224
225 static int ext4_valid_block_bitmap(struct super_block *sb,
226                                         struct ext4_group_desc *desc,
227                                         unsigned int block_group,
228                                         struct buffer_head *bh)
229 {
230         ext4_grpblk_t offset;
231         ext4_grpblk_t next_zero_bit;
232         ext4_fsblk_t bitmap_blk;
233         ext4_fsblk_t group_first_block;
234
235         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
236                 /* with FLEX_BG, the inode/block bitmaps and itable
237                  * blocks may not be in the group at all
238                  * so the bitmap validation will be skipped for those groups
239                  * or it has to also read the block group where the bitmaps
240                  * are located to verify they are set.
241                  */
242                 return 1;
243         }
244         group_first_block = ext4_group_first_block_no(sb, block_group);
245
246         /* check whether block bitmap block number is set */
247         bitmap_blk = ext4_block_bitmap(sb, desc);
248         offset = bitmap_blk - group_first_block;
249         if (!ext4_test_bit(offset, bh->b_data))
250                 /* bad block bitmap */
251                 goto err_out;
252
253         /* check whether the inode bitmap block number is set */
254         bitmap_blk = ext4_inode_bitmap(sb, desc);
255         offset = bitmap_blk - group_first_block;
256         if (!ext4_test_bit(offset, bh->b_data))
257                 /* bad block bitmap */
258                 goto err_out;
259
260         /* check whether the inode table block number is set */
261         bitmap_blk = ext4_inode_table(sb, desc);
262         offset = bitmap_blk - group_first_block;
263         next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
264                                 offset + EXT4_SB(sb)->s_itb_per_group,
265                                 offset);
266         if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
267                 /* good bitmap for inode tables */
268                 return 1;
269
270 err_out:
271         ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
272                         block_group, bitmap_blk);
273         return 0;
274 }
275 /**
276  * ext4_read_block_bitmap()
277  * @sb:                 super block
278  * @block_group:        given block group
279  *
280  * Read the bitmap for a given block_group,and validate the
281  * bits for block/inode/inode tables are set in the bitmaps
282  *
283  * Return buffer_head on success or NULL in case of failure.
284  */
285 struct buffer_head *
286 ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
287 {
288         struct ext4_group_desc *desc;
289         struct buffer_head *bh = NULL;
290         ext4_fsblk_t bitmap_blk;
291
292         desc = ext4_get_group_desc(sb, block_group, NULL);
293         if (!desc)
294                 return NULL;
295         bitmap_blk = ext4_block_bitmap(sb, desc);
296         bh = sb_getblk(sb, bitmap_blk);
297         if (unlikely(!bh)) {
298                 ext4_error(sb, "Cannot read block bitmap - "
299                             "block_group = %u, block_bitmap = %llu",
300                             block_group, bitmap_blk);
301                 return NULL;
302         }
303
304         if (bitmap_uptodate(bh))
305                 return bh;
306
307         lock_buffer(bh);
308         if (bitmap_uptodate(bh)) {
309                 unlock_buffer(bh);
310                 return bh;
311         }
312         ext4_lock_group(sb, block_group);
313         if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
314                 ext4_init_block_bitmap(sb, bh, block_group, desc);
315                 set_bitmap_uptodate(bh);
316                 set_buffer_uptodate(bh);
317                 ext4_unlock_group(sb, block_group);
318                 unlock_buffer(bh);
319                 return bh;
320         }
321         ext4_unlock_group(sb, block_group);
322         if (buffer_uptodate(bh)) {
323                 /*
324                  * if not uninit if bh is uptodate,
325                  * bitmap is also uptodate
326                  */
327                 set_bitmap_uptodate(bh);
328                 unlock_buffer(bh);
329                 return bh;
330         }
331         /*
332          * submit the buffer_head for read. We can
333          * safely mark the bitmap as uptodate now.
334          * We do it here so the bitmap uptodate bit
335          * get set with buffer lock held.
336          */
337         trace_ext4_read_block_bitmap_load(sb, block_group);
338         set_bitmap_uptodate(bh);
339         if (bh_submit_read(bh) < 0) {
340                 put_bh(bh);
341                 ext4_error(sb, "Cannot read block bitmap - "
342                             "block_group = %u, block_bitmap = %llu",
343                             block_group, bitmap_blk);
344                 return NULL;
345         }
346         ext4_valid_block_bitmap(sb, desc, block_group, bh);
347         /*
348          * file system mounted not to panic on error,
349          * continue with corrupt bitmap
350          */
351         return bh;
352 }
353
354 /**
355  * ext4_has_free_blocks()
356  * @sbi:        in-core super block structure.
357  * @nblocks:    number of needed blocks
358  *
359  * Check if filesystem has nblocks free & available for allocation.
360  * On success return 1, return 0 on failure.
361  */
362 static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
363                                 s64 nblocks, unsigned int flags)
364 {
365         s64 free_blocks, dirty_blocks, root_blocks;
366         struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
367         struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
368
369         free_blocks  = percpu_counter_read_positive(fbc);
370         dirty_blocks = percpu_counter_read_positive(dbc);
371         root_blocks = ext4_r_blocks_count(sbi->s_es);
372
373         if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
374                                                 EXT4_FREEBLOCKS_WATERMARK) {
375                 free_blocks  = percpu_counter_sum_positive(fbc);
376                 dirty_blocks = percpu_counter_sum_positive(dbc);
377         }
378         /* Check whether we have space after
379          * accounting for current dirty blocks & root reserved blocks.
380          */
381         if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
382                 return 1;
383
384         /* Hm, nope.  Are (enough) root reserved blocks available? */
385         if (sbi->s_resuid == current_fsuid() ||
386             ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
387             capable(CAP_SYS_RESOURCE) ||
388                 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
389
390                 if (free_blocks >= (nblocks + dirty_blocks))
391                         return 1;
392         }
393
394         return 0;
395 }
396
397 int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
398                            s64 nblocks, unsigned int flags)
399 {
400         if (ext4_has_free_blocks(sbi, nblocks, flags)) {
401                 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
402                 return 0;
403         } else
404                 return -ENOSPC;
405 }
406
407 /**
408  * ext4_should_retry_alloc()
409  * @sb:                 super block
410  * @retries             number of attemps has been made
411  *
412  * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
413  * it is profitable to retry the operation, this function will wait
414  * for the current or committing transaction to complete, and then
415  * return TRUE.
416  *
417  * if the total number of retries exceed three times, return FALSE.
418  */
419 int ext4_should_retry_alloc(struct super_block *sb, int *retries)
420 {
421         if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
422             (*retries)++ > 3 ||
423             !EXT4_SB(sb)->s_journal)
424                 return 0;
425
426         jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
427
428         return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
429 }
430
431 /*
432  * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
433  *
434  * @handle:             handle to this transaction
435  * @inode:              file inode
436  * @goal:               given target block(filesystem wide)
437  * @count:              pointer to total number of blocks needed
438  * @errp:               error code
439  *
440  * Return 1st allocated block number on success, *count stores total account
441  * error stores in errp pointer
442  */
443 ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
444                                   ext4_fsblk_t goal, unsigned int flags,
445                                   unsigned long *count, int *errp)
446 {
447         struct ext4_allocation_request ar;
448         ext4_fsblk_t ret;
449
450         memset(&ar, 0, sizeof(ar));
451         /* Fill with neighbour allocated blocks */
452         ar.inode = inode;
453         ar.goal = goal;
454         ar.len = count ? *count : 1;
455         ar.flags = flags;
456
457         ret = ext4_mb_new_blocks(handle, &ar, errp);
458         if (count)
459                 *count = ar.len;
460         /*
461          * Account for the allocated meta blocks.  We will never
462          * fail EDQUOT for metdata, but we do account for it.
463          */
464         if (!(*errp) &&
465             ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
466                 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
467                 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
468                 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
469                 dquot_alloc_block_nofail(inode, ar.len);
470         }
471         return ret;
472 }
473
474 /**
475  * ext4_count_free_blocks() -- count filesystem free blocks
476  * @sb:         superblock
477  *
478  * Adds up the number of free blocks from each block group.
479  */
480 ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
481 {
482         ext4_fsblk_t desc_count;
483         struct ext4_group_desc *gdp;
484         ext4_group_t i;
485         ext4_group_t ngroups = ext4_get_groups_count(sb);
486 #ifdef EXT4FS_DEBUG
487         struct ext4_super_block *es;
488         ext4_fsblk_t bitmap_count;
489         unsigned int x;
490         struct buffer_head *bitmap_bh = NULL;
491
492         es = EXT4_SB(sb)->s_es;
493         desc_count = 0;
494         bitmap_count = 0;
495         gdp = NULL;
496
497         for (i = 0; i < ngroups; i++) {
498                 gdp = ext4_get_group_desc(sb, i, NULL);
499                 if (!gdp)
500                         continue;
501                 desc_count += ext4_free_blks_count(sb, gdp);
502                 brelse(bitmap_bh);
503                 bitmap_bh = ext4_read_block_bitmap(sb, i);
504                 if (bitmap_bh == NULL)
505                         continue;
506
507                 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
508                 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
509                         i, ext4_free_blks_count(sb, gdp), x);
510                 bitmap_count += x;
511         }
512         brelse(bitmap_bh);
513         printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
514                 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
515                desc_count, bitmap_count);
516         return bitmap_count;
517 #else
518         desc_count = 0;
519         for (i = 0; i < ngroups; i++) {
520                 gdp = ext4_get_group_desc(sb, i, NULL);
521                 if (!gdp)
522                         continue;
523                 desc_count += ext4_free_blks_count(sb, gdp);
524         }
525
526         return desc_count;
527 #endif
528 }
529
530 static inline int test_root(ext4_group_t a, int b)
531 {
532         int num = b;
533
534         while (a > num)
535                 num *= b;
536         return num == a;
537 }
538
539 static int ext4_group_sparse(ext4_group_t group)
540 {
541         if (group <= 1)
542                 return 1;
543         if (!(group & 1))
544                 return 0;
545         return (test_root(group, 7) || test_root(group, 5) ||
546                 test_root(group, 3));
547 }
548
549 /**
550  *      ext4_bg_has_super - number of blocks used by the superblock in group
551  *      @sb: superblock for filesystem
552  *      @group: group number to check
553  *
554  *      Return the number of blocks used by the superblock (primary or backup)
555  *      in this group.  Currently this will be only 0 or 1.
556  */
557 int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
558 {
559         if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560                                 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
561                         !ext4_group_sparse(group))
562                 return 0;
563         return 1;
564 }
565
566 static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
567                                         ext4_group_t group)
568 {
569         unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
570         ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
571         ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
572
573         if (group == first || group == first + 1 || group == last)
574                 return 1;
575         return 0;
576 }
577
578 static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
579                                         ext4_group_t group)
580 {
581         if (!ext4_bg_has_super(sb, group))
582                 return 0;
583
584         if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
585                 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
586         else
587                 return EXT4_SB(sb)->s_gdb_count;
588 }
589
590 /**
591  *      ext4_bg_num_gdb - number of blocks used by the group table in group
592  *      @sb: superblock for filesystem
593  *      @group: group number to check
594  *
595  *      Return the number of blocks used by the group descriptor table
596  *      (primary or backup) in this group.  In the future there may be a
597  *      different number of descriptor blocks in each group.
598  */
599 unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
600 {
601         unsigned long first_meta_bg =
602                         le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
603         unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
604
605         if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
606                         metagroup < first_meta_bg)
607                 return ext4_bg_num_gdb_nometa(sb, group);
608
609         return ext4_bg_num_gdb_meta(sb,group);
610
611 }
612
613 /*
614  * This function returns the number of file system metadata blocks at
615  * the beginning of a block group, including the reserved gdt blocks.
616  */
617 static unsigned int num_base_meta_blocks(struct super_block *sb,
618                                          ext4_group_t block_group)
619 {
620         struct ext4_sb_info *sbi = EXT4_SB(sb);
621         int num;
622
623         /* Check for superblock and gdt backups in this group */
624         num = ext4_bg_has_super(sb, block_group);
625
626         if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
627             block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
628                           sbi->s_desc_per_block) {
629                 if (num) {
630                         num += ext4_bg_num_gdb(sb, block_group);
631                         num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
632                 }
633         } else { /* For META_BG_BLOCK_GROUPS */
634                 num += ext4_bg_num_gdb(sb, block_group);
635         }
636         return num;
637 }
638 /**
639  *      ext4_inode_to_goal_block - return a hint for block allocation
640  *      @inode: inode for block allocation
641  *
642  *      Return the ideal location to start allocating blocks for a
643  *      newly created inode.
644  */
645 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
646 {
647         struct ext4_inode_info *ei = EXT4_I(inode);
648         ext4_group_t block_group;
649         ext4_grpblk_t colour;
650         int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
651         ext4_fsblk_t bg_start;
652         ext4_fsblk_t last_block;
653
654         block_group = ei->i_block_group;
655         if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
656                 /*
657                  * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
658                  * block groups per flexgroup, reserve the first block
659                  * group for directories and special files.  Regular
660                  * files will start at the second block group.  This
661                  * tends to speed up directory access and improves
662                  * fsck times.
663                  */
664                 block_group &= ~(flex_size-1);
665                 if (S_ISREG(inode->i_mode))
666                         block_group++;
667         }
668         bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
669         last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
670
671         /*
672          * If we are doing delayed allocation, we don't need take
673          * colour into account.
674          */
675         if (test_opt(inode->i_sb, DELALLOC))
676                 return bg_start;
677
678         if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
679                 colour = (current->pid % 16) *
680                         (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
681         else
682                 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
683         return bg_start + colour;
684 }
685