balloc.c 23 KB

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