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