balloc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/balloc.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
  11. * Big-endian to little-endian byte-swapping/bitmaps by
  12. * David S. Miller (davem@caip.rutgers.edu), 1995
  13. */
  14. #include <linux/time.h>
  15. #include <linux/capability.h>
  16. #include <linux/fs.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 block group number for a given block number
  30. */
  31. ext4_group_t ext4_get_group_number(struct super_block *sb,
  32. ext4_fsblk_t block)
  33. {
  34. ext4_group_t group;
  35. if (test_opt2(sb, STD_GROUP_SIZE))
  36. group = (block -
  37. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) >>
  38. (EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
  39. else
  40. ext4_get_group_no_and_offset(sb, block, &group, NULL);
  41. return group;
  42. }
  43. /*
  44. * Calculate the block group number and offset into the block/cluster
  45. * allocation bitmap, given a block number
  46. */
  47. void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
  48. ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
  49. {
  50. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  51. ext4_grpblk_t offset;
  52. blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
  53. offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)) >>
  54. EXT4_SB(sb)->s_cluster_bits;
  55. if (offsetp)
  56. *offsetp = offset;
  57. if (blockgrpp)
  58. *blockgrpp = blocknr;
  59. }
  60. /*
  61. * Check whether the 'block' lives within the 'block_group'. Returns 1 if so
  62. * and 0 otherwise.
  63. */
  64. static inline int ext4_block_in_group(struct super_block *sb,
  65. ext4_fsblk_t block,
  66. ext4_group_t block_group)
  67. {
  68. ext4_group_t actual_group;
  69. actual_group = ext4_get_group_number(sb, block);
  70. return (actual_group == block_group) ? 1 : 0;
  71. }
  72. /* Return the number of clusters used for file system metadata; this
  73. * represents the overhead needed by the file system.
  74. */
  75. static unsigned ext4_num_overhead_clusters(struct super_block *sb,
  76. ext4_group_t block_group,
  77. struct ext4_group_desc *gdp)
  78. {
  79. unsigned num_clusters;
  80. int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c;
  81. ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
  82. ext4_fsblk_t itbl_blk;
  83. struct ext4_sb_info *sbi = EXT4_SB(sb);
  84. /* This is the number of clusters used by the superblock,
  85. * block group descriptors, and reserved block group
  86. * descriptor blocks */
  87. num_clusters = ext4_num_base_meta_clusters(sb, block_group);
  88. /*
  89. * For the allocation bitmaps and inode table, we first need
  90. * to check to see if the block is in the block group. If it
  91. * is, then check to see if the cluster is already accounted
  92. * for in the clusters used for the base metadata cluster, or
  93. * if we can increment the base metadata cluster to include
  94. * that block. Otherwise, we will have to track the cluster
  95. * used for the allocation bitmap or inode table explicitly.
  96. * Normally all of these blocks are contiguous, so the special
  97. * case handling shouldn't be necessary except for *very*
  98. * unusual file system layouts.
  99. */
  100. if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
  101. block_cluster = EXT4_B2C(sbi,
  102. ext4_block_bitmap(sb, gdp) - start);
  103. if (block_cluster < num_clusters)
  104. block_cluster = -1;
  105. else if (block_cluster == num_clusters) {
  106. num_clusters++;
  107. block_cluster = -1;
  108. }
  109. }
  110. if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
  111. inode_cluster = EXT4_B2C(sbi,
  112. ext4_inode_bitmap(sb, gdp) - start);
  113. if (inode_cluster < num_clusters)
  114. inode_cluster = -1;
  115. else if (inode_cluster == num_clusters) {
  116. num_clusters++;
  117. inode_cluster = -1;
  118. }
  119. }
  120. itbl_blk = ext4_inode_table(sb, gdp);
  121. for (i = 0; i < sbi->s_itb_per_group; i++) {
  122. if (ext4_block_in_group(sb, itbl_blk + i, block_group)) {
  123. c = EXT4_B2C(sbi, itbl_blk + i - start);
  124. if ((c < num_clusters) || (c == inode_cluster) ||
  125. (c == block_cluster) || (c == itbl_cluster))
  126. continue;
  127. if (c == num_clusters) {
  128. num_clusters++;
  129. continue;
  130. }
  131. num_clusters++;
  132. itbl_cluster = c;
  133. }
  134. }
  135. if (block_cluster != -1)
  136. num_clusters++;
  137. if (inode_cluster != -1)
  138. num_clusters++;
  139. return num_clusters;
  140. }
  141. static unsigned int num_clusters_in_group(struct super_block *sb,
  142. ext4_group_t block_group)
  143. {
  144. unsigned int blocks;
  145. if (block_group == ext4_get_groups_count(sb) - 1) {
  146. /*
  147. * Even though mke2fs always initializes the first and
  148. * last group, just in case some other tool was used,
  149. * we need to make sure we calculate the right free
  150. * blocks.
  151. */
  152. blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
  153. ext4_group_first_block_no(sb, block_group);
  154. } else
  155. blocks = EXT4_BLOCKS_PER_GROUP(sb);
  156. return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
  157. }
  158. /* Initializes an uninitialized block bitmap */
  159. static int ext4_init_block_bitmap(struct super_block *sb,
  160. struct buffer_head *bh,
  161. ext4_group_t block_group,
  162. struct ext4_group_desc *gdp)
  163. {
  164. unsigned int bit, bit_max;
  165. struct ext4_sb_info *sbi = EXT4_SB(sb);
  166. ext4_fsblk_t start, tmp;
  167. J_ASSERT_BH(bh, buffer_locked(bh));
  168. /* If checksum is bad mark all blocks used to prevent allocation
  169. * essentially implementing a per-group read-only flag. */
  170. if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
  171. ext4_mark_group_bitmap_corrupted(sb, block_group,
  172. EXT4_GROUP_INFO_BBITMAP_CORRUPT |
  173. EXT4_GROUP_INFO_IBITMAP_CORRUPT);
  174. return -EFSBADCRC;
  175. }
  176. memset(bh->b_data, 0, sb->s_blocksize);
  177. bit_max = ext4_num_base_meta_clusters(sb, block_group);
  178. if ((bit_max >> 3) >= bh->b_size)
  179. return -EFSCORRUPTED;
  180. for (bit = 0; bit < bit_max; bit++)
  181. ext4_set_bit(bit, bh->b_data);
  182. start = ext4_group_first_block_no(sb, block_group);
  183. /* Set bits for block and inode bitmaps, and inode table */
  184. tmp = ext4_block_bitmap(sb, gdp);
  185. if (ext4_block_in_group(sb, tmp, block_group))
  186. ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
  187. tmp = ext4_inode_bitmap(sb, gdp);
  188. if (ext4_block_in_group(sb, tmp, block_group))
  189. ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
  190. tmp = ext4_inode_table(sb, gdp);
  191. for (; tmp < ext4_inode_table(sb, gdp) +
  192. sbi->s_itb_per_group; tmp++) {
  193. if (ext4_block_in_group(sb, tmp, block_group))
  194. ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
  195. }
  196. /*
  197. * Also if the number of blocks within the group is less than
  198. * the blocksize * 8 ( which is the size of bitmap ), set rest
  199. * of the block bitmap to 1
  200. */
  201. ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
  202. sb->s_blocksize * 8, bh->b_data);
  203. return 0;
  204. }
  205. /* Return the number of free blocks in a block group. It is used when
  206. * the block bitmap is uninitialized, so we can't just count the bits
  207. * in the bitmap. */
  208. unsigned ext4_free_clusters_after_init(struct super_block *sb,
  209. ext4_group_t block_group,
  210. struct ext4_group_desc *gdp)
  211. {
  212. return num_clusters_in_group(sb, block_group) -
  213. ext4_num_overhead_clusters(sb, block_group, gdp);
  214. }
  215. /*
  216. * The free blocks are managed by bitmaps. A file system contains several
  217. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  218. * block for inodes, N blocks for the inode table and data blocks.
  219. *
  220. * The file system contains group descriptors which are located after the
  221. * super block. Each descriptor contains the number of the bitmap block and
  222. * the free blocks count in the block. The descriptors are loaded in memory
  223. * when a file system is mounted (see ext4_fill_super).
  224. */
  225. /**
  226. * ext4_get_group_desc() -- load group descriptor from disk
  227. * @sb: super block
  228. * @block_group: given block group
  229. * @bh: pointer to the buffer head to store the block
  230. * group descriptor
  231. */
  232. struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
  233. ext4_group_t block_group,
  234. struct buffer_head **bh)
  235. {
  236. unsigned int group_desc;
  237. unsigned int offset;
  238. ext4_group_t ngroups = ext4_get_groups_count(sb);
  239. struct ext4_group_desc *desc;
  240. struct ext4_sb_info *sbi = EXT4_SB(sb);
  241. struct buffer_head *bh_p;
  242. if (block_group >= ngroups) {
  243. ext4_error(sb, "block_group >= groups_count - block_group = %u,"
  244. " groups_count = %u", block_group, ngroups);
  245. return NULL;
  246. }
  247. group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
  248. offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  249. bh_p = sbi_array_rcu_deref(sbi, s_group_desc, group_desc);
  250. /*
  251. * sbi_array_rcu_deref returns with rcu unlocked, this is ok since
  252. * the pointer being dereferenced won't be dereferenced again. By
  253. * looking at the usage in add_new_gdb() the value isn't modified,
  254. * just the pointer, and so it remains valid.
  255. */
  256. if (!bh_p) {
  257. ext4_error(sb, "Group descriptor not loaded - "
  258. "block_group = %u, group_desc = %u, desc = %u",
  259. block_group, group_desc, offset);
  260. return NULL;
  261. }
  262. desc = (struct ext4_group_desc *)(
  263. (__u8 *)bh_p->b_data +
  264. offset * EXT4_DESC_SIZE(sb));
  265. if (bh)
  266. *bh = bh_p;
  267. return desc;
  268. }
  269. /*
  270. * Return the block number which was discovered to be invalid, or 0 if
  271. * the block bitmap is valid.
  272. */
  273. static ext4_fsblk_t ext4_valid_block_bitmap(struct super_block *sb,
  274. struct ext4_group_desc *desc,
  275. ext4_group_t block_group,
  276. struct buffer_head *bh)
  277. {
  278. struct ext4_sb_info *sbi = EXT4_SB(sb);
  279. ext4_grpblk_t offset;
  280. ext4_grpblk_t next_zero_bit;
  281. ext4_grpblk_t max_bit = EXT4_CLUSTERS_PER_GROUP(sb);
  282. ext4_fsblk_t blk;
  283. ext4_fsblk_t group_first_block;
  284. if (ext4_has_feature_flex_bg(sb)) {
  285. /* with FLEX_BG, the inode/block bitmaps and itable
  286. * blocks may not be in the group at all
  287. * so the bitmap validation will be skipped for those groups
  288. * or it has to also read the block group where the bitmaps
  289. * are located to verify they are set.
  290. */
  291. return 0;
  292. }
  293. group_first_block = ext4_group_first_block_no(sb, block_group);
  294. /* check whether block bitmap block number is set */
  295. blk = ext4_block_bitmap(sb, desc);
  296. offset = blk - group_first_block;
  297. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  298. !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
  299. /* bad block bitmap */
  300. return blk;
  301. /* check whether the inode bitmap block number is set */
  302. blk = ext4_inode_bitmap(sb, desc);
  303. offset = blk - group_first_block;
  304. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  305. !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
  306. /* bad block bitmap */
  307. return blk;
  308. /* check whether the inode table block number is set */
  309. blk = ext4_inode_table(sb, desc);
  310. offset = blk - group_first_block;
  311. if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
  312. EXT4_B2C(sbi, offset + sbi->s_itb_per_group) >= max_bit)
  313. return blk;
  314. next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
  315. EXT4_B2C(sbi, offset + sbi->s_itb_per_group),
  316. EXT4_B2C(sbi, offset));
  317. if (next_zero_bit <
  318. EXT4_B2C(sbi, offset + sbi->s_itb_per_group))
  319. /* bad bitmap for inode tables */
  320. return blk;
  321. return 0;
  322. }
  323. static int ext4_validate_block_bitmap(struct super_block *sb,
  324. struct ext4_group_desc *desc,
  325. ext4_group_t block_group,
  326. struct buffer_head *bh)
  327. {
  328. ext4_fsblk_t blk;
  329. struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
  330. if (buffer_verified(bh))
  331. return 0;
  332. if (EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
  333. return -EFSCORRUPTED;
  334. ext4_lock_group(sb, block_group);
  335. if (buffer_verified(bh))
  336. goto verified;
  337. if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
  338. desc, bh))) {
  339. ext4_unlock_group(sb, block_group);
  340. ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
  341. ext4_mark_group_bitmap_corrupted(sb, block_group,
  342. EXT4_GROUP_INFO_BBITMAP_CORRUPT);
  343. return -EFSBADCRC;
  344. }
  345. blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
  346. if (unlikely(blk != 0)) {
  347. ext4_unlock_group(sb, block_group);
  348. ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
  349. block_group, blk);
  350. ext4_mark_group_bitmap_corrupted(sb, block_group,
  351. EXT4_GROUP_INFO_BBITMAP_CORRUPT);
  352. return -EFSCORRUPTED;
  353. }
  354. set_buffer_verified(bh);
  355. verified:
  356. ext4_unlock_group(sb, block_group);
  357. return 0;
  358. }
  359. /**
  360. * ext4_read_block_bitmap_nowait()
  361. * @sb: super block
  362. * @block_group: given block group
  363. *
  364. * Read the bitmap for a given block_group,and validate the
  365. * bits for block/inode/inode tables are set in the bitmaps
  366. *
  367. * Return buffer_head on success or NULL in case of failure.
  368. */
  369. struct buffer_head *
  370. ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
  371. {
  372. struct ext4_group_desc *desc;
  373. struct ext4_sb_info *sbi = EXT4_SB(sb);
  374. struct buffer_head *bh;
  375. ext4_fsblk_t bitmap_blk;
  376. int err;
  377. desc = ext4_get_group_desc(sb, block_group, NULL);
  378. if (!desc)
  379. return ERR_PTR(-EFSCORRUPTED);
  380. bitmap_blk = ext4_block_bitmap(sb, desc);
  381. if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  382. (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
  383. ext4_error(sb, "Invalid block bitmap block %llu in "
  384. "block_group %u", bitmap_blk, block_group);
  385. ext4_mark_group_bitmap_corrupted(sb, block_group,
  386. EXT4_GROUP_INFO_BBITMAP_CORRUPT);
  387. return ERR_PTR(-EFSCORRUPTED);
  388. }
  389. bh = sb_getblk(sb, bitmap_blk);
  390. if (unlikely(!bh)) {
  391. ext4_warning(sb, "Cannot get buffer for block bitmap - "
  392. "block_group = %u, block_bitmap = %llu",
  393. block_group, bitmap_blk);
  394. return ERR_PTR(-ENOMEM);
  395. }
  396. if (bitmap_uptodate(bh))
  397. goto verify;
  398. lock_buffer(bh);
  399. if (bitmap_uptodate(bh)) {
  400. unlock_buffer(bh);
  401. goto verify;
  402. }
  403. ext4_lock_group(sb, block_group);
  404. if (ext4_has_group_desc_csum(sb) &&
  405. (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
  406. if (block_group == 0) {
  407. ext4_unlock_group(sb, block_group);
  408. unlock_buffer(bh);
  409. ext4_error(sb, "Block bitmap for bg 0 marked "
  410. "uninitialized");
  411. err = -EFSCORRUPTED;
  412. goto out;
  413. }
  414. err = ext4_init_block_bitmap(sb, bh, block_group, desc);
  415. set_bitmap_uptodate(bh);
  416. set_buffer_uptodate(bh);
  417. set_buffer_verified(bh);
  418. ext4_unlock_group(sb, block_group);
  419. unlock_buffer(bh);
  420. if (err) {
  421. ext4_error(sb, "Failed to init block bitmap for group "
  422. "%u: %d", block_group, err);
  423. goto out;
  424. }
  425. goto verify;
  426. }
  427. ext4_unlock_group(sb, block_group);
  428. if (buffer_uptodate(bh)) {
  429. /*
  430. * if not uninit if bh is uptodate,
  431. * bitmap is also uptodate
  432. */
  433. set_bitmap_uptodate(bh);
  434. unlock_buffer(bh);
  435. goto verify;
  436. }
  437. /*
  438. * submit the buffer_head for reading
  439. */
  440. set_buffer_new(bh);
  441. trace_ext4_read_block_bitmap_load(sb, block_group);
  442. bh->b_end_io = ext4_end_bitmap_read;
  443. get_bh(bh);
  444. submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
  445. return bh;
  446. verify:
  447. err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
  448. if (err)
  449. goto out;
  450. return bh;
  451. out:
  452. put_bh(bh);
  453. return ERR_PTR(err);
  454. }
  455. /* Returns 0 on success, 1 on error */
  456. int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
  457. struct buffer_head *bh)
  458. {
  459. struct ext4_group_desc *desc;
  460. if (!buffer_new(bh))
  461. return 0;
  462. desc = ext4_get_group_desc(sb, block_group, NULL);
  463. if (!desc)
  464. return -EFSCORRUPTED;
  465. wait_on_buffer(bh);
  466. if (!buffer_uptodate(bh)) {
  467. ext4_error(sb, "Cannot read block bitmap - "
  468. "block_group = %u, block_bitmap = %llu",
  469. block_group, (unsigned long long) bh->b_blocknr);
  470. ext4_mark_group_bitmap_corrupted(sb, block_group,
  471. EXT4_GROUP_INFO_BBITMAP_CORRUPT);
  472. return -EIO;
  473. }
  474. clear_buffer_new(bh);
  475. /* Panic or remount fs read-only if block bitmap is invalid */
  476. return ext4_validate_block_bitmap(sb, desc, block_group, bh);
  477. }
  478. struct buffer_head *
  479. ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
  480. {
  481. struct buffer_head *bh;
  482. int err;
  483. bh = ext4_read_block_bitmap_nowait(sb, block_group);
  484. if (IS_ERR(bh))
  485. return bh;
  486. err = ext4_wait_block_bitmap(sb, block_group, bh);
  487. if (err) {
  488. put_bh(bh);
  489. return ERR_PTR(err);
  490. }
  491. return bh;
  492. }
  493. /**
  494. * ext4_has_free_clusters()
  495. * @sbi: in-core super block structure.
  496. * @nclusters: number of needed blocks
  497. * @flags: flags from ext4_mb_new_blocks()
  498. *
  499. * Check if filesystem has nclusters free & available for allocation.
  500. * On success return 1, return 0 on failure.
  501. */
  502. static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
  503. s64 nclusters, unsigned int flags)
  504. {
  505. s64 free_clusters, dirty_clusters, rsv, resv_clusters;
  506. struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
  507. struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
  508. free_clusters = percpu_counter_read_positive(fcc);
  509. dirty_clusters = percpu_counter_read_positive(dcc);
  510. resv_clusters = atomic64_read(&sbi->s_resv_clusters);
  511. /*
  512. * r_blocks_count should always be multiple of the cluster ratio so
  513. * we are safe to do a plane bit shift only.
  514. */
  515. rsv = (ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits) +
  516. resv_clusters;
  517. if (free_clusters - (nclusters + rsv + dirty_clusters) <
  518. EXT4_FREECLUSTERS_WATERMARK) {
  519. free_clusters = percpu_counter_sum_positive(fcc);
  520. dirty_clusters = percpu_counter_sum_positive(dcc);
  521. }
  522. /* Check whether we have space after accounting for current
  523. * dirty clusters & root reserved clusters.
  524. */
  525. if (free_clusters >= (rsv + nclusters + dirty_clusters))
  526. return 1;
  527. /* Hm, nope. Are (enough) root reserved clusters available? */
  528. if (uid_eq(sbi->s_resuid, current_fsuid()) ||
  529. (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) && in_group_p(sbi->s_resgid)) ||
  530. capable(CAP_SYS_RESOURCE) ||
  531. (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
  532. if (free_clusters >= (nclusters + dirty_clusters +
  533. resv_clusters))
  534. return 1;
  535. }
  536. /* No free blocks. Let's see if we can dip into reserved pool */
  537. if (flags & EXT4_MB_USE_RESERVED) {
  538. if (free_clusters >= (nclusters + dirty_clusters))
  539. return 1;
  540. }
  541. return 0;
  542. }
  543. int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
  544. s64 nclusters, unsigned int flags)
  545. {
  546. if (ext4_has_free_clusters(sbi, nclusters, flags)) {
  547. percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
  548. return 0;
  549. } else
  550. return -ENOSPC;
  551. }
  552. /**
  553. * ext4_should_retry_alloc()
  554. * @sb: super block
  555. * @retries number of attemps has been made
  556. *
  557. * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
  558. * it is profitable to retry the operation, this function will wait
  559. * for the current or committing transaction to complete, and then
  560. * return TRUE. We will only retry once.
  561. */
  562. int ext4_should_retry_alloc(struct super_block *sb, int *retries)
  563. {
  564. if (!ext4_has_free_clusters(EXT4_SB(sb), 1, 0) ||
  565. (*retries)++ > 1 ||
  566. !EXT4_SB(sb)->s_journal)
  567. return 0;
  568. smp_mb();
  569. if (EXT4_SB(sb)->s_mb_free_pending == 0)
  570. return 0;
  571. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  572. jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
  573. return 1;
  574. }
  575. /*
  576. * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
  577. *
  578. * @handle: handle to this transaction
  579. * @inode: file inode
  580. * @goal: given target block(filesystem wide)
  581. * @count: pointer to total number of clusters needed
  582. * @errp: error code
  583. *
  584. * Return 1st allocated block number on success, *count stores total account
  585. * error stores in errp pointer
  586. */
  587. ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  588. ext4_fsblk_t goal, unsigned int flags,
  589. unsigned long *count, int *errp)
  590. {
  591. struct ext4_allocation_request ar;
  592. ext4_fsblk_t ret;
  593. memset(&ar, 0, sizeof(ar));
  594. /* Fill with neighbour allocated blocks */
  595. ar.inode = inode;
  596. ar.goal = goal;
  597. ar.len = count ? *count : 1;
  598. ar.flags = flags;
  599. ret = ext4_mb_new_blocks(handle, &ar, errp);
  600. if (count)
  601. *count = ar.len;
  602. /*
  603. * Account for the allocated meta blocks. We will never
  604. * fail EDQUOT for metdata, but we do account for it.
  605. */
  606. if (!(*errp) && (flags & EXT4_MB_DELALLOC_RESERVED)) {
  607. dquot_alloc_block_nofail(inode,
  608. EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
  609. }
  610. return ret;
  611. }
  612. /**
  613. * ext4_count_free_clusters() -- count filesystem free clusters
  614. * @sb: superblock
  615. *
  616. * Adds up the number of free clusters from each block group.
  617. */
  618. ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
  619. {
  620. ext4_fsblk_t desc_count;
  621. struct ext4_group_desc *gdp;
  622. ext4_group_t i;
  623. ext4_group_t ngroups = ext4_get_groups_count(sb);
  624. struct ext4_group_info *grp;
  625. #ifdef EXT4FS_DEBUG
  626. struct ext4_super_block *es;
  627. ext4_fsblk_t bitmap_count;
  628. unsigned int x;
  629. struct buffer_head *bitmap_bh = NULL;
  630. es = EXT4_SB(sb)->s_es;
  631. desc_count = 0;
  632. bitmap_count = 0;
  633. gdp = NULL;
  634. for (i = 0; i < ngroups; i++) {
  635. gdp = ext4_get_group_desc(sb, i, NULL);
  636. if (!gdp)
  637. continue;
  638. grp = NULL;
  639. if (EXT4_SB(sb)->s_group_info)
  640. grp = ext4_get_group_info(sb, i);
  641. if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
  642. desc_count += ext4_free_group_clusters(sb, gdp);
  643. brelse(bitmap_bh);
  644. bitmap_bh = ext4_read_block_bitmap(sb, i);
  645. if (IS_ERR(bitmap_bh)) {
  646. bitmap_bh = NULL;
  647. continue;
  648. }
  649. x = ext4_count_free(bitmap_bh->b_data,
  650. EXT4_CLUSTERS_PER_GROUP(sb) / 8);
  651. printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
  652. i, ext4_free_group_clusters(sb, gdp), x);
  653. bitmap_count += x;
  654. }
  655. brelse(bitmap_bh);
  656. printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
  657. ", computed = %llu, %llu\n",
  658. EXT4_NUM_B2C(EXT4_SB(sb), ext4_free_blocks_count(es)),
  659. desc_count, bitmap_count);
  660. return bitmap_count;
  661. #else
  662. desc_count = 0;
  663. for (i = 0; i < ngroups; i++) {
  664. gdp = ext4_get_group_desc(sb, i, NULL);
  665. if (!gdp)
  666. continue;
  667. grp = NULL;
  668. if (EXT4_SB(sb)->s_group_info)
  669. grp = ext4_get_group_info(sb, i);
  670. if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
  671. desc_count += ext4_free_group_clusters(sb, gdp);
  672. }
  673. return desc_count;
  674. #endif
  675. }
  676. static inline int test_root(ext4_group_t a, int b)
  677. {
  678. while (1) {
  679. if (a < b)
  680. return 0;
  681. if (a == b)
  682. return 1;
  683. if ((a % b) != 0)
  684. return 0;
  685. a = a / b;
  686. }
  687. }
  688. /**
  689. * ext4_bg_has_super - number of blocks used by the superblock in group
  690. * @sb: superblock for filesystem
  691. * @group: group number to check
  692. *
  693. * Return the number of blocks used by the superblock (primary or backup)
  694. * in this group. Currently this will be only 0 or 1.
  695. */
  696. int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
  697. {
  698. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  699. if (group == 0)
  700. return 1;
  701. if (ext4_has_feature_sparse_super2(sb)) {
  702. if (group == le32_to_cpu(es->s_backup_bgs[0]) ||
  703. group == le32_to_cpu(es->s_backup_bgs[1]))
  704. return 1;
  705. return 0;
  706. }
  707. if ((group <= 1) || !ext4_has_feature_sparse_super(sb))
  708. return 1;
  709. if (!(group & 1))
  710. return 0;
  711. if (test_root(group, 3) || (test_root(group, 5)) ||
  712. test_root(group, 7))
  713. return 1;
  714. return 0;
  715. }
  716. static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
  717. ext4_group_t group)
  718. {
  719. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  720. ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
  721. ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
  722. if (group == first || group == first + 1 || group == last)
  723. return 1;
  724. return 0;
  725. }
  726. static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
  727. ext4_group_t group)
  728. {
  729. if (!ext4_bg_has_super(sb, group))
  730. return 0;
  731. if (ext4_has_feature_meta_bg(sb))
  732. return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  733. else
  734. return EXT4_SB(sb)->s_gdb_count;
  735. }
  736. /**
  737. * ext4_bg_num_gdb - number of blocks used by the group table in group
  738. * @sb: superblock for filesystem
  739. * @group: group number to check
  740. *
  741. * Return the number of blocks used by the group descriptor table
  742. * (primary or backup) in this group. In the future there may be a
  743. * different number of descriptor blocks in each group.
  744. */
  745. unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
  746. {
  747. unsigned long first_meta_bg =
  748. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  749. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  750. if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg)
  751. return ext4_bg_num_gdb_nometa(sb, group);
  752. return ext4_bg_num_gdb_meta(sb,group);
  753. }
  754. /*
  755. * This function returns the number of file system metadata clusters at
  756. * the beginning of a block group, including the reserved gdt blocks.
  757. */
  758. static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
  759. ext4_group_t block_group)
  760. {
  761. struct ext4_sb_info *sbi = EXT4_SB(sb);
  762. unsigned num;
  763. /* Check for superblock and gdt backups in this group */
  764. num = ext4_bg_has_super(sb, block_group);
  765. if (!ext4_has_feature_meta_bg(sb) ||
  766. block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
  767. sbi->s_desc_per_block) {
  768. if (num) {
  769. num += ext4_bg_num_gdb(sb, block_group);
  770. num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
  771. }
  772. } else { /* For META_BG_BLOCK_GROUPS */
  773. num += ext4_bg_num_gdb(sb, block_group);
  774. }
  775. return EXT4_NUM_B2C(sbi, num);
  776. }
  777. /**
  778. * ext4_inode_to_goal_block - return a hint for block allocation
  779. * @inode: inode for block allocation
  780. *
  781. * Return the ideal location to start allocating blocks for a
  782. * newly created inode.
  783. */
  784. ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
  785. {
  786. struct ext4_inode_info *ei = EXT4_I(inode);
  787. ext4_group_t block_group;
  788. ext4_grpblk_t colour;
  789. int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
  790. ext4_fsblk_t bg_start;
  791. ext4_fsblk_t last_block;
  792. block_group = ei->i_block_group;
  793. if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
  794. /*
  795. * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
  796. * block groups per flexgroup, reserve the first block
  797. * group for directories and special files. Regular
  798. * files will start at the second block group. This
  799. * tends to speed up directory access and improves
  800. * fsck times.
  801. */
  802. block_group &= ~(flex_size-1);
  803. if (S_ISREG(inode->i_mode))
  804. block_group++;
  805. }
  806. bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
  807. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  808. /*
  809. * If we are doing delayed allocation, we don't need take
  810. * colour into account.
  811. */
  812. if (test_opt(inode->i_sb, DELALLOC))
  813. return bg_start;
  814. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  815. colour = (current->pid % 16) *
  816. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  817. else
  818. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  819. return bg_start + colour;
  820. }