balloc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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,
  84. ext4_block_bitmap(sb, gdp) - start);
  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. ext4_inode_bitmap(sb, gdp) - start);
  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, itbl_blk + i - start);
  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. struct ext4_sb_info *sbi = EXT4_SB(sb);
  250. ext4_grpblk_t offset;
  251. ext4_grpblk_t next_zero_bit;
  252. ext4_grpblk_t max_bit = EXT4_CLUSTERS_PER_GROUP(sb);
  253. ext4_fsblk_t bitmap_blk;
  254. ext4_fsblk_t group_first_block;
  255. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
  256. /* with FLEX_BG, the inode/block bitmaps and itable
  257. * blocks may not be in the group at all
  258. * so the bitmap validation will be skipped for those groups
  259. * or it has to also read the block group where the bitmaps
  260. * are located to verify they are set.
  261. */
  262. return 1;
  263. }
  264. group_first_block = ext4_group_first_block_no(sb, block_group);
  265. /* check whether block bitmap block number is set */
  266. bitmap_blk = ext4_block_bitmap(sb, desc);
  267. offset = bitmap_blk - group_first_block;
  268. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  269. !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
  270. /* bad block bitmap */
  271. goto err_out;
  272. /* check whether the inode bitmap block number is set */
  273. bitmap_blk = ext4_inode_bitmap(sb, desc);
  274. offset = bitmap_blk - group_first_block;
  275. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  276. !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
  277. /* bad block bitmap */
  278. goto err_out;
  279. /* check whether the inode table block number is set */
  280. bitmap_blk = ext4_inode_table(sb, desc);
  281. offset = bitmap_blk - group_first_block;
  282. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  283. EXT4_B2C(sbi, offset + sbi->s_itb_per_group) >= max_bit)
  284. goto err_out;
  285. next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
  286. EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group),
  287. EXT4_B2C(sbi, offset));
  288. if (next_zero_bit >=
  289. EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group))
  290. /* good bitmap for inode tables */
  291. return 1;
  292. err_out:
  293. ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
  294. block_group, bitmap_blk);
  295. return 0;
  296. }
  297. /**
  298. * ext4_read_block_bitmap_nowait()
  299. * @sb: super block
  300. * @block_group: given block group
  301. *
  302. * Read the bitmap for a given block_group,and validate the
  303. * bits for block/inode/inode tables are set in the bitmaps
  304. *
  305. * Return buffer_head on success or NULL in case of failure.
  306. */
  307. struct buffer_head *
  308. ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
  309. {
  310. struct ext4_group_desc *desc;
  311. struct ext4_sb_info *sbi = EXT4_SB(sb);
  312. struct buffer_head *bh;
  313. ext4_fsblk_t bitmap_blk;
  314. desc = ext4_get_group_desc(sb, block_group, NULL);
  315. if (!desc)
  316. return NULL;
  317. bitmap_blk = ext4_block_bitmap(sb, desc);
  318. if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  319. (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
  320. ext4_error(sb, "Invalid block bitmap block %llu in "
  321. "block_group %u", bitmap_blk, block_group);
  322. return NULL;
  323. }
  324. bh = sb_getblk(sb, bitmap_blk);
  325. if (unlikely(!bh)) {
  326. ext4_error(sb, "Cannot get buffer for block bitmap - "
  327. "block_group = %u, block_bitmap = %llu",
  328. block_group, bitmap_blk);
  329. return NULL;
  330. }
  331. if (bitmap_uptodate(bh))
  332. return bh;
  333. lock_buffer(bh);
  334. if (bitmap_uptodate(bh)) {
  335. unlock_buffer(bh);
  336. return bh;
  337. }
  338. ext4_lock_group(sb, block_group);
  339. if (ext4_has_group_desc_csum(sb) &&
  340. (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
  341. if (block_group == 0) {
  342. ext4_unlock_group(sb, block_group);
  343. unlock_buffer(bh);
  344. ext4_error(sb, "Block bitmap for bg 0 marked "
  345. "uninitialized");
  346. put_bh(bh);
  347. return NULL;
  348. }
  349. ext4_init_block_bitmap(sb, bh, block_group, desc);
  350. set_bitmap_uptodate(bh);
  351. set_buffer_uptodate(bh);
  352. ext4_unlock_group(sb, block_group);
  353. unlock_buffer(bh);
  354. return bh;
  355. }
  356. ext4_unlock_group(sb, block_group);
  357. if (buffer_uptodate(bh)) {
  358. /*
  359. * if not uninit if bh is uptodate,
  360. * bitmap is also uptodate
  361. */
  362. set_bitmap_uptodate(bh);
  363. unlock_buffer(bh);
  364. return bh;
  365. }
  366. /*
  367. * submit the buffer_head for reading
  368. */
  369. set_buffer_new(bh);
  370. trace_ext4_read_block_bitmap_load(sb, block_group);
  371. bh->b_end_io = ext4_end_bitmap_read;
  372. get_bh(bh);
  373. submit_bh(READ, bh);
  374. return bh;
  375. }
  376. /* Returns 0 on success, 1 on error */
  377. int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
  378. struct buffer_head *bh)
  379. {
  380. struct ext4_group_desc *desc;
  381. if (!buffer_new(bh))
  382. return 0;
  383. desc = ext4_get_group_desc(sb, block_group, NULL);
  384. if (!desc)
  385. return 1;
  386. wait_on_buffer(bh);
  387. if (!buffer_uptodate(bh)) {
  388. ext4_error(sb, "Cannot read block bitmap - "
  389. "block_group = %u, block_bitmap = %llu",
  390. block_group, (unsigned long long) bh->b_blocknr);
  391. return 1;
  392. }
  393. clear_buffer_new(bh);
  394. /* Panic or remount fs read-only if block bitmap is invalid */
  395. ext4_valid_block_bitmap(sb, desc, block_group, bh);
  396. return 0;
  397. }
  398. struct buffer_head *
  399. ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
  400. {
  401. struct buffer_head *bh;
  402. bh = ext4_read_block_bitmap_nowait(sb, block_group);
  403. if (!bh)
  404. return NULL;
  405. if (ext4_wait_block_bitmap(sb, block_group, bh)) {
  406. put_bh(bh);
  407. return NULL;
  408. }
  409. return bh;
  410. }
  411. /**
  412. * ext4_has_free_clusters()
  413. * @sbi: in-core super block structure.
  414. * @nclusters: number of needed blocks
  415. * @flags: flags from ext4_mb_new_blocks()
  416. *
  417. * Check if filesystem has nclusters free & available for allocation.
  418. * On success return 1, return 0 on failure.
  419. */
  420. static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
  421. s64 nclusters, unsigned int flags)
  422. {
  423. s64 free_clusters, dirty_clusters, root_clusters;
  424. struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
  425. struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
  426. free_clusters = percpu_counter_read_positive(fcc);
  427. dirty_clusters = percpu_counter_read_positive(dcc);
  428. /*
  429. * r_blocks_count should always be multiple of the cluster ratio so
  430. * we are safe to do a plane bit shift only.
  431. */
  432. root_clusters = ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits;
  433. if (free_clusters - (nclusters + root_clusters + dirty_clusters) <
  434. EXT4_FREECLUSTERS_WATERMARK) {
  435. free_clusters = percpu_counter_sum_positive(fcc);
  436. dirty_clusters = percpu_counter_sum_positive(dcc);
  437. }
  438. /* Check whether we have space after accounting for current
  439. * dirty clusters & root reserved clusters.
  440. */
  441. if (free_clusters >= ((root_clusters + nclusters) + dirty_clusters))
  442. return 1;
  443. /* Hm, nope. Are (enough) root reserved clusters available? */
  444. if (sbi->s_resuid == current_fsuid() ||
  445. ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
  446. capable(CAP_SYS_RESOURCE) ||
  447. (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
  448. if (free_clusters >= (nclusters + dirty_clusters))
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
  454. s64 nclusters, unsigned int flags)
  455. {
  456. if (ext4_has_free_clusters(sbi, nclusters, flags)) {
  457. percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
  458. return 0;
  459. } else
  460. return -ENOSPC;
  461. }
  462. /**
  463. * ext4_should_retry_alloc()
  464. * @sb: super block
  465. * @retries number of attemps has been made
  466. *
  467. * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
  468. * it is profitable to retry the operation, this function will wait
  469. * for the current or committing transaction to complete, and then
  470. * return TRUE.
  471. *
  472. * if the total number of retries exceed three times, return FALSE.
  473. */
  474. int ext4_should_retry_alloc(struct super_block *sb, int *retries)
  475. {
  476. if (!ext4_has_free_clusters(EXT4_SB(sb), 1, 0) ||
  477. (*retries)++ > 3 ||
  478. !EXT4_SB(sb)->s_journal)
  479. return 0;
  480. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  481. return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
  482. }
  483. /*
  484. * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
  485. *
  486. * @handle: handle to this transaction
  487. * @inode: file inode
  488. * @goal: given target block(filesystem wide)
  489. * @count: pointer to total number of clusters needed
  490. * @errp: error code
  491. *
  492. * Return 1st allocated block number on success, *count stores total account
  493. * error stores in errp pointer
  494. */
  495. ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  496. ext4_fsblk_t goal, unsigned int flags,
  497. unsigned long *count, int *errp)
  498. {
  499. struct ext4_allocation_request ar;
  500. ext4_fsblk_t ret;
  501. memset(&ar, 0, sizeof(ar));
  502. /* Fill with neighbour allocated blocks */
  503. ar.inode = inode;
  504. ar.goal = goal;
  505. ar.len = count ? *count : 1;
  506. ar.flags = flags;
  507. ret = ext4_mb_new_blocks(handle, &ar, errp);
  508. if (count)
  509. *count = ar.len;
  510. /*
  511. * Account for the allocated meta blocks. We will never
  512. * fail EDQUOT for metdata, but we do account for it.
  513. */
  514. if (!(*errp) &&
  515. ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
  516. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  517. EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
  518. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  519. dquot_alloc_block_nofail(inode,
  520. EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
  521. }
  522. return ret;
  523. }
  524. /**
  525. * ext4_count_free_clusters() -- count filesystem free clusters
  526. * @sb: superblock
  527. *
  528. * Adds up the number of free clusters from each block group.
  529. */
  530. ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
  531. {
  532. ext4_fsblk_t desc_count;
  533. struct ext4_group_desc *gdp;
  534. ext4_group_t i;
  535. ext4_group_t ngroups = ext4_get_groups_count(sb);
  536. #ifdef EXT4FS_DEBUG
  537. struct ext4_super_block *es;
  538. ext4_fsblk_t bitmap_count;
  539. unsigned int x;
  540. struct buffer_head *bitmap_bh = NULL;
  541. es = EXT4_SB(sb)->s_es;
  542. desc_count = 0;
  543. bitmap_count = 0;
  544. gdp = NULL;
  545. for (i = 0; i < ngroups; i++) {
  546. gdp = ext4_get_group_desc(sb, i, NULL);
  547. if (!gdp)
  548. continue;
  549. desc_count += ext4_free_group_clusters(sb, gdp);
  550. brelse(bitmap_bh);
  551. bitmap_bh = ext4_read_block_bitmap(sb, i);
  552. if (bitmap_bh == NULL)
  553. continue;
  554. x = ext4_count_free(bitmap_bh->b_data,
  555. EXT4_BLOCKS_PER_GROUP(sb) / 8);
  556. printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
  557. i, ext4_free_group_clusters(sb, gdp), x);
  558. bitmap_count += x;
  559. }
  560. brelse(bitmap_bh);
  561. printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
  562. ", computed = %llu, %llu\n",
  563. EXT4_NUM_B2C(EXT4_SB(sb), 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_group_clusters(sb, gdp);
  573. }
  574. return desc_count;
  575. #endif
  576. }
  577. static inline int test_root(ext4_group_t a, int b)
  578. {
  579. int num = b;
  580. while (a > num)
  581. num *= b;
  582. return num == a;
  583. }
  584. static int ext4_group_sparse(ext4_group_t group)
  585. {
  586. if (group <= 1)
  587. return 1;
  588. if (!(group & 1))
  589. return 0;
  590. return (test_root(group, 7) || test_root(group, 5) ||
  591. test_root(group, 3));
  592. }
  593. /**
  594. * ext4_bg_has_super - number of blocks used by the superblock in group
  595. * @sb: superblock for filesystem
  596. * @group: group number to check
  597. *
  598. * Return the number of blocks used by the superblock (primary or backup)
  599. * in this group. Currently this will be only 0 or 1.
  600. */
  601. int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
  602. {
  603. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  604. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  605. !ext4_group_sparse(group))
  606. return 0;
  607. return 1;
  608. }
  609. static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
  610. ext4_group_t group)
  611. {
  612. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  613. ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
  614. ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
  615. if (group == first || group == first + 1 || group == last)
  616. return 1;
  617. return 0;
  618. }
  619. static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
  620. ext4_group_t group)
  621. {
  622. if (!ext4_bg_has_super(sb, group))
  623. return 0;
  624. if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
  625. return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  626. else
  627. return EXT4_SB(sb)->s_gdb_count;
  628. }
  629. /**
  630. * ext4_bg_num_gdb - number of blocks used by the group table in group
  631. * @sb: superblock for filesystem
  632. * @group: group number to check
  633. *
  634. * Return the number of blocks used by the group descriptor table
  635. * (primary or backup) in this group. In the future there may be a
  636. * different number of descriptor blocks in each group.
  637. */
  638. unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
  639. {
  640. unsigned long first_meta_bg =
  641. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  642. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  643. if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
  644. metagroup < first_meta_bg)
  645. return ext4_bg_num_gdb_nometa(sb, group);
  646. return ext4_bg_num_gdb_meta(sb,group);
  647. }
  648. /*
  649. * This function returns the number of file system metadata clusters at
  650. * the beginning of a block group, including the reserved gdt blocks.
  651. */
  652. static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
  653. ext4_group_t block_group)
  654. {
  655. struct ext4_sb_info *sbi = EXT4_SB(sb);
  656. unsigned num;
  657. /* Check for superblock and gdt backups in this group */
  658. num = ext4_bg_has_super(sb, block_group);
  659. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  660. block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
  661. sbi->s_desc_per_block) {
  662. if (num) {
  663. num += ext4_bg_num_gdb(sb, block_group);
  664. num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
  665. }
  666. } else { /* For META_BG_BLOCK_GROUPS */
  667. num += ext4_bg_num_gdb(sb, block_group);
  668. }
  669. return EXT4_NUM_B2C(sbi, num);
  670. }
  671. /**
  672. * ext4_inode_to_goal_block - return a hint for block allocation
  673. * @inode: inode for block allocation
  674. *
  675. * Return the ideal location to start allocating blocks for a
  676. * newly created inode.
  677. */
  678. ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
  679. {
  680. struct ext4_inode_info *ei = EXT4_I(inode);
  681. ext4_group_t block_group;
  682. ext4_grpblk_t colour;
  683. int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
  684. ext4_fsblk_t bg_start;
  685. ext4_fsblk_t last_block;
  686. block_group = ei->i_block_group;
  687. if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
  688. /*
  689. * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
  690. * block groups per flexgroup, reserve the first block
  691. * group for directories and special files. Regular
  692. * files will start at the second block group. This
  693. * tends to speed up directory access and improves
  694. * fsck times.
  695. */
  696. block_group &= ~(flex_size-1);
  697. if (S_ISREG(inode->i_mode))
  698. block_group++;
  699. }
  700. bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
  701. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  702. /*
  703. * If we are doing delayed allocation, we don't need take
  704. * colour into account.
  705. */
  706. if (test_opt(inode->i_sb, DELALLOC))
  707. return bg_start;
  708. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  709. colour = (current->pid % 16) *
  710. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  711. else
  712. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  713. return bg_start + colour;
  714. }