balloc.c 19 KB

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