balloc.c 25 KB

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