balloc.c 22 KB

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