balloc.c 21 KB

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