balloc.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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 "group.h"
  22. /*
  23. * balloc.c contains the blocks allocation and deallocation routines
  24. */
  25. /*
  26. * Calculate the block group number and offset, given a block number
  27. */
  28. void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
  29. ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
  30. {
  31. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  32. ext4_grpblk_t offset;
  33. blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
  34. offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
  35. if (offsetp)
  36. *offsetp = offset;
  37. if (blockgrpp)
  38. *blockgrpp = blocknr;
  39. }
  40. static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
  41. ext4_group_t block_group)
  42. {
  43. ext4_group_t actual_group;
  44. ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
  45. if (actual_group == block_group)
  46. return 1;
  47. return 0;
  48. }
  49. static int ext4_group_used_meta_blocks(struct super_block *sb,
  50. ext4_group_t block_group)
  51. {
  52. ext4_fsblk_t tmp;
  53. struct ext4_sb_info *sbi = EXT4_SB(sb);
  54. /* block bitmap, inode bitmap, and inode table blocks */
  55. int used_blocks = sbi->s_itb_per_group + 2;
  56. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
  57. struct ext4_group_desc *gdp;
  58. struct buffer_head *bh;
  59. gdp = ext4_get_group_desc(sb, block_group, &bh);
  60. if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
  61. block_group))
  62. used_blocks--;
  63. if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
  64. block_group))
  65. used_blocks--;
  66. tmp = ext4_inode_table(sb, gdp);
  67. for (; tmp < ext4_inode_table(sb, gdp) +
  68. sbi->s_itb_per_group; tmp++) {
  69. if (!ext4_block_in_group(sb, tmp, block_group))
  70. used_blocks -= 1;
  71. }
  72. }
  73. return used_blocks;
  74. }
  75. /* Initializes an uninitialized block bitmap if given, and returns the
  76. * number of blocks free in the group. */
  77. unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
  78. ext4_group_t block_group, struct ext4_group_desc *gdp)
  79. {
  80. int bit, bit_max;
  81. unsigned free_blocks, group_blocks;
  82. struct ext4_sb_info *sbi = EXT4_SB(sb);
  83. if (bh) {
  84. J_ASSERT_BH(bh, buffer_locked(bh));
  85. /* If checksum is bad mark all blocks used to prevent allocation
  86. * essentially implementing a per-group read-only flag. */
  87. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  88. ext4_error(sb, __func__,
  89. "Checksum bad for group %lu\n", block_group);
  90. gdp->bg_free_blocks_count = 0;
  91. gdp->bg_free_inodes_count = 0;
  92. gdp->bg_itable_unused = 0;
  93. memset(bh->b_data, 0xff, sb->s_blocksize);
  94. return 0;
  95. }
  96. memset(bh->b_data, 0, sb->s_blocksize);
  97. }
  98. /* Check for superblock and gdt backups in this group */
  99. bit_max = ext4_bg_has_super(sb, block_group);
  100. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  101. block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
  102. sbi->s_desc_per_block) {
  103. if (bit_max) {
  104. bit_max += ext4_bg_num_gdb(sb, block_group);
  105. bit_max +=
  106. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
  107. }
  108. } else { /* For META_BG_BLOCK_GROUPS */
  109. bit_max += ext4_bg_num_gdb(sb, block_group);
  110. }
  111. if (block_group == sbi->s_groups_count - 1) {
  112. /*
  113. * Even though mke2fs always initialize first and last group
  114. * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
  115. * to make sure we calculate the right free blocks
  116. */
  117. group_blocks = ext4_blocks_count(sbi->s_es) -
  118. le32_to_cpu(sbi->s_es->s_first_data_block) -
  119. (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count -1));
  120. } else {
  121. group_blocks = EXT4_BLOCKS_PER_GROUP(sb);
  122. }
  123. free_blocks = group_blocks - bit_max;
  124. if (bh) {
  125. ext4_fsblk_t start, tmp;
  126. int flex_bg = 0;
  127. for (bit = 0; bit < bit_max; bit++)
  128. ext4_set_bit(bit, bh->b_data);
  129. start = ext4_group_first_block_no(sb, block_group);
  130. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  131. EXT4_FEATURE_INCOMPAT_FLEX_BG))
  132. flex_bg = 1;
  133. /* Set bits for block and inode bitmaps, and inode table */
  134. tmp = ext4_block_bitmap(sb, gdp);
  135. if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
  136. ext4_set_bit(tmp - start, bh->b_data);
  137. tmp = ext4_inode_bitmap(sb, gdp);
  138. if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
  139. ext4_set_bit(tmp - start, bh->b_data);
  140. tmp = ext4_inode_table(sb, gdp);
  141. for (; tmp < ext4_inode_table(sb, gdp) +
  142. sbi->s_itb_per_group; tmp++) {
  143. if (!flex_bg ||
  144. ext4_block_in_group(sb, tmp, block_group))
  145. ext4_set_bit(tmp - start, bh->b_data);
  146. }
  147. /*
  148. * Also if the number of blocks within the group is
  149. * less than the blocksize * 8 ( which is the size
  150. * of bitmap ), set rest of the block bitmap to 1
  151. */
  152. mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data);
  153. }
  154. return free_blocks - ext4_group_used_meta_blocks(sb, block_group);
  155. }
  156. /*
  157. * The free blocks are managed by bitmaps. A file system contains several
  158. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  159. * block for inodes, N blocks for the inode table and data blocks.
  160. *
  161. * The file system contains group descriptors which are located after the
  162. * super block. Each descriptor contains the number of the bitmap block and
  163. * the free blocks count in the block. The descriptors are loaded in memory
  164. * when a file system is mounted (see ext4_fill_super).
  165. */
  166. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  167. /**
  168. * ext4_get_group_desc() -- load group descriptor from disk
  169. * @sb: super block
  170. * @block_group: given block group
  171. * @bh: pointer to the buffer head to store the block
  172. * group descriptor
  173. */
  174. struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
  175. ext4_group_t block_group,
  176. struct buffer_head ** bh)
  177. {
  178. unsigned long group_desc;
  179. unsigned long offset;
  180. struct ext4_group_desc * desc;
  181. struct ext4_sb_info *sbi = EXT4_SB(sb);
  182. if (block_group >= sbi->s_groups_count) {
  183. ext4_error (sb, "ext4_get_group_desc",
  184. "block_group >= groups_count - "
  185. "block_group = %lu, groups_count = %lu",
  186. block_group, sbi->s_groups_count);
  187. return NULL;
  188. }
  189. smp_rmb();
  190. group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
  191. offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  192. if (!sbi->s_group_desc[group_desc]) {
  193. ext4_error (sb, "ext4_get_group_desc",
  194. "Group descriptor not loaded - "
  195. "block_group = %lu, group_desc = %lu, desc = %lu",
  196. block_group, group_desc, offset);
  197. return NULL;
  198. }
  199. desc = (struct ext4_group_desc *)(
  200. (__u8 *)sbi->s_group_desc[group_desc]->b_data +
  201. offset * EXT4_DESC_SIZE(sb));
  202. if (bh)
  203. *bh = sbi->s_group_desc[group_desc];
  204. return desc;
  205. }
  206. static int ext4_valid_block_bitmap(struct super_block *sb,
  207. struct ext4_group_desc *desc,
  208. unsigned int block_group,
  209. struct buffer_head *bh)
  210. {
  211. ext4_grpblk_t offset;
  212. ext4_grpblk_t next_zero_bit;
  213. ext4_fsblk_t bitmap_blk;
  214. ext4_fsblk_t group_first_block;
  215. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
  216. /* with FLEX_BG, the inode/block bitmaps and itable
  217. * blocks may not be in the group at all
  218. * so the bitmap validation will be skipped for those groups
  219. * or it has to also read the block group where the bitmaps
  220. * are located to verify they are set.
  221. */
  222. return 1;
  223. }
  224. group_first_block = ext4_group_first_block_no(sb, block_group);
  225. /* check whether block bitmap block number is set */
  226. bitmap_blk = ext4_block_bitmap(sb, desc);
  227. offset = bitmap_blk - group_first_block;
  228. if (!ext4_test_bit(offset, bh->b_data))
  229. /* bad block bitmap */
  230. goto err_out;
  231. /* check whether the inode bitmap block number is set */
  232. bitmap_blk = ext4_inode_bitmap(sb, desc);
  233. offset = bitmap_blk - group_first_block;
  234. if (!ext4_test_bit(offset, bh->b_data))
  235. /* bad block bitmap */
  236. goto err_out;
  237. /* check whether the inode table block number is set */
  238. bitmap_blk = ext4_inode_table(sb, desc);
  239. offset = bitmap_blk - group_first_block;
  240. next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
  241. offset + EXT4_SB(sb)->s_itb_per_group,
  242. offset);
  243. if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
  244. /* good bitmap for inode tables */
  245. return 1;
  246. err_out:
  247. ext4_error(sb, __func__,
  248. "Invalid block bitmap - "
  249. "block_group = %d, block = %llu",
  250. block_group, bitmap_blk);
  251. return 0;
  252. }
  253. /**
  254. * ext4_read_block_bitmap()
  255. * @sb: super block
  256. * @block_group: given block group
  257. *
  258. * Read the bitmap for a given block_group,and validate the
  259. * bits for block/inode/inode tables are set in the bitmaps
  260. *
  261. * Return buffer_head on success or NULL in case of failure.
  262. */
  263. struct buffer_head *
  264. ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
  265. {
  266. struct ext4_group_desc * desc;
  267. struct buffer_head * bh = NULL;
  268. ext4_fsblk_t bitmap_blk;
  269. desc = ext4_get_group_desc(sb, block_group, NULL);
  270. if (!desc)
  271. return NULL;
  272. bitmap_blk = ext4_block_bitmap(sb, desc);
  273. bh = sb_getblk(sb, bitmap_blk);
  274. if (unlikely(!bh)) {
  275. ext4_error(sb, __func__,
  276. "Cannot read block bitmap - "
  277. "block_group = %d, block_bitmap = %llu",
  278. (int)block_group, (unsigned long long)bitmap_blk);
  279. return NULL;
  280. }
  281. if (bh_uptodate_or_lock(bh))
  282. return bh;
  283. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  284. ext4_init_block_bitmap(sb, bh, block_group, desc);
  285. set_buffer_uptodate(bh);
  286. unlock_buffer(bh);
  287. return bh;
  288. }
  289. if (bh_submit_read(bh) < 0) {
  290. put_bh(bh);
  291. ext4_error(sb, __func__,
  292. "Cannot read block bitmap - "
  293. "block_group = %d, block_bitmap = %llu",
  294. (int)block_group, (unsigned long long)bitmap_blk);
  295. return NULL;
  296. }
  297. ext4_valid_block_bitmap(sb, desc, block_group, bh);
  298. /*
  299. * file system mounted not to panic on error,
  300. * continue with corrupt bitmap
  301. */
  302. return bh;
  303. }
  304. /*
  305. * The reservation window structure operations
  306. * --------------------------------------------
  307. * Operations include:
  308. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  309. *
  310. * We use a red-black tree to represent per-filesystem reservation
  311. * windows.
  312. *
  313. */
  314. /**
  315. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  316. * @rb_root: root of per-filesystem reservation rb tree
  317. * @verbose: verbose mode
  318. * @fn: function which wishes to dump the reservation map
  319. *
  320. * If verbose is turned on, it will print the whole block reservation
  321. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  322. * those windows that overlap with their immediate neighbors.
  323. */
  324. #if 1
  325. static void __rsv_window_dump(struct rb_root *root, int verbose,
  326. const char *fn)
  327. {
  328. struct rb_node *n;
  329. struct ext4_reserve_window_node *rsv, *prev;
  330. int bad;
  331. restart:
  332. n = rb_first(root);
  333. bad = 0;
  334. prev = NULL;
  335. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  336. while (n) {
  337. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  338. if (verbose)
  339. printk("reservation window 0x%p "
  340. "start: %llu, end: %llu\n",
  341. rsv, rsv->rsv_start, rsv->rsv_end);
  342. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  343. printk("Bad reservation %p (start >= end)\n",
  344. rsv);
  345. bad = 1;
  346. }
  347. if (prev && prev->rsv_end >= rsv->rsv_start) {
  348. printk("Bad reservation %p (prev->end >= start)\n",
  349. rsv);
  350. bad = 1;
  351. }
  352. if (bad) {
  353. if (!verbose) {
  354. printk("Restarting reservation walk in verbose mode\n");
  355. verbose = 1;
  356. goto restart;
  357. }
  358. }
  359. n = rb_next(n);
  360. prev = rsv;
  361. }
  362. printk("Window map complete.\n");
  363. BUG_ON(bad);
  364. }
  365. #define rsv_window_dump(root, verbose) \
  366. __rsv_window_dump((root), (verbose), __func__)
  367. #else
  368. #define rsv_window_dump(root, verbose) do {} while (0)
  369. #endif
  370. /**
  371. * goal_in_my_reservation()
  372. * @rsv: inode's reservation window
  373. * @grp_goal: given goal block relative to the allocation block group
  374. * @group: the current allocation block group
  375. * @sb: filesystem super block
  376. *
  377. * Test if the given goal block (group relative) is within the file's
  378. * own block reservation window range.
  379. *
  380. * If the reservation window is outside the goal allocation group, return 0;
  381. * grp_goal (given goal block) could be -1, which means no specific
  382. * goal block. In this case, always return 1.
  383. * If the goal block is within the reservation window, return 1;
  384. * otherwise, return 0;
  385. */
  386. static int
  387. goal_in_my_reservation(struct ext4_reserve_window *rsv, ext4_grpblk_t grp_goal,
  388. ext4_group_t group, struct super_block *sb)
  389. {
  390. ext4_fsblk_t group_first_block, group_last_block;
  391. group_first_block = ext4_group_first_block_no(sb, group);
  392. group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  393. if ((rsv->_rsv_start > group_last_block) ||
  394. (rsv->_rsv_end < group_first_block))
  395. return 0;
  396. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  397. || (grp_goal + group_first_block > rsv->_rsv_end)))
  398. return 0;
  399. return 1;
  400. }
  401. /**
  402. * search_reserve_window()
  403. * @rb_root: root of reservation tree
  404. * @goal: target allocation block
  405. *
  406. * Find the reserved window which includes the goal, or the previous one
  407. * if the goal is not in any window.
  408. * Returns NULL if there are no windows or if all windows start after the goal.
  409. */
  410. static struct ext4_reserve_window_node *
  411. search_reserve_window(struct rb_root *root, ext4_fsblk_t goal)
  412. {
  413. struct rb_node *n = root->rb_node;
  414. struct ext4_reserve_window_node *rsv;
  415. if (!n)
  416. return NULL;
  417. do {
  418. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  419. if (goal < rsv->rsv_start)
  420. n = n->rb_left;
  421. else if (goal > rsv->rsv_end)
  422. n = n->rb_right;
  423. else
  424. return rsv;
  425. } while (n);
  426. /*
  427. * We've fallen off the end of the tree: the goal wasn't inside
  428. * any particular node. OK, the previous node must be to one
  429. * side of the interval containing the goal. If it's the RHS,
  430. * we need to back up one.
  431. */
  432. if (rsv->rsv_start > goal) {
  433. n = rb_prev(&rsv->rsv_node);
  434. rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node);
  435. }
  436. return rsv;
  437. }
  438. /**
  439. * ext4_rsv_window_add() -- Insert a window to the block reservation rb tree.
  440. * @sb: super block
  441. * @rsv: reservation window to add
  442. *
  443. * Must be called with rsv_lock hold.
  444. */
  445. void ext4_rsv_window_add(struct super_block *sb,
  446. struct ext4_reserve_window_node *rsv)
  447. {
  448. struct rb_root *root = &EXT4_SB(sb)->s_rsv_window_root;
  449. struct rb_node *node = &rsv->rsv_node;
  450. ext4_fsblk_t start = rsv->rsv_start;
  451. struct rb_node ** p = &root->rb_node;
  452. struct rb_node * parent = NULL;
  453. struct ext4_reserve_window_node *this;
  454. while (*p)
  455. {
  456. parent = *p;
  457. this = rb_entry(parent, struct ext4_reserve_window_node, rsv_node);
  458. if (start < this->rsv_start)
  459. p = &(*p)->rb_left;
  460. else if (start > this->rsv_end)
  461. p = &(*p)->rb_right;
  462. else {
  463. rsv_window_dump(root, 1);
  464. BUG();
  465. }
  466. }
  467. rb_link_node(node, parent, p);
  468. rb_insert_color(node, root);
  469. }
  470. /**
  471. * ext4_rsv_window_remove() -- unlink a window from the reservation rb tree
  472. * @sb: super block
  473. * @rsv: reservation window to remove
  474. *
  475. * Mark the block reservation window as not allocated, and unlink it
  476. * from the filesystem reservation window rb tree. Must be called with
  477. * rsv_lock hold.
  478. */
  479. static void rsv_window_remove(struct super_block *sb,
  480. struct ext4_reserve_window_node *rsv)
  481. {
  482. rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  483. rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  484. rsv->rsv_alloc_hit = 0;
  485. rb_erase(&rsv->rsv_node, &EXT4_SB(sb)->s_rsv_window_root);
  486. }
  487. /*
  488. * rsv_is_empty() -- Check if the reservation window is allocated.
  489. * @rsv: given reservation window to check
  490. *
  491. * returns 1 if the end block is EXT4_RESERVE_WINDOW_NOT_ALLOCATED.
  492. */
  493. static inline int rsv_is_empty(struct ext4_reserve_window *rsv)
  494. {
  495. /* a valid reservation end block could not be 0 */
  496. return rsv->_rsv_end == EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  497. }
  498. /**
  499. * ext4_init_block_alloc_info()
  500. * @inode: file inode structure
  501. *
  502. * Allocate and initialize the reservation window structure, and
  503. * link the window to the ext4 inode structure at last
  504. *
  505. * The reservation window structure is only dynamically allocated
  506. * and linked to ext4 inode the first time the open file
  507. * needs a new block. So, before every ext4_new_block(s) call, for
  508. * regular files, we should check whether the reservation window
  509. * structure exists or not. In the latter case, this function is called.
  510. * Fail to do so will result in block reservation being turned off for that
  511. * open file.
  512. *
  513. * This function is called from ext4_get_blocks_handle(), also called
  514. * when setting the reservation window size through ioctl before the file
  515. * is open for write (needs block allocation).
  516. *
  517. * Needs down_write(i_data_sem) protection prior to call this function.
  518. */
  519. void ext4_init_block_alloc_info(struct inode *inode)
  520. {
  521. struct ext4_inode_info *ei = EXT4_I(inode);
  522. struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info;
  523. struct super_block *sb = inode->i_sb;
  524. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  525. if (block_i) {
  526. struct ext4_reserve_window_node *rsv = &block_i->rsv_window_node;
  527. rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  528. rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
  529. /*
  530. * if filesystem is mounted with NORESERVATION, the goal
  531. * reservation window size is set to zero to indicate
  532. * block reservation is off
  533. */
  534. if (!test_opt(sb, RESERVATION))
  535. rsv->rsv_goal_size = 0;
  536. else
  537. rsv->rsv_goal_size = EXT4_DEFAULT_RESERVE_BLOCKS;
  538. rsv->rsv_alloc_hit = 0;
  539. block_i->last_alloc_logical_block = 0;
  540. block_i->last_alloc_physical_block = 0;
  541. }
  542. ei->i_block_alloc_info = block_i;
  543. }
  544. /**
  545. * ext4_discard_reservation()
  546. * @inode: inode
  547. *
  548. * Discard(free) block reservation window on last file close, or truncate
  549. * or at last iput().
  550. *
  551. * It is being called in three cases:
  552. * ext4_release_file(): last writer close the file
  553. * ext4_clear_inode(): last iput(), when nobody link to this file.
  554. * ext4_truncate(): when the block indirect map is about to change.
  555. *
  556. */
  557. void ext4_discard_reservation(struct inode *inode)
  558. {
  559. struct ext4_inode_info *ei = EXT4_I(inode);
  560. struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info;
  561. struct ext4_reserve_window_node *rsv;
  562. spinlock_t *rsv_lock = &EXT4_SB(inode->i_sb)->s_rsv_window_lock;
  563. ext4_mb_discard_inode_preallocations(inode);
  564. if (!block_i)
  565. return;
  566. rsv = &block_i->rsv_window_node;
  567. if (!rsv_is_empty(&rsv->rsv_window)) {
  568. spin_lock(rsv_lock);
  569. if (!rsv_is_empty(&rsv->rsv_window))
  570. rsv_window_remove(inode->i_sb, rsv);
  571. spin_unlock(rsv_lock);
  572. }
  573. }
  574. /**
  575. * ext4_free_blocks_sb() -- Free given blocks and update quota
  576. * @handle: handle to this transaction
  577. * @sb: super block
  578. * @block: start physcial block to free
  579. * @count: number of blocks to free
  580. * @pdquot_freed_blocks: pointer to quota
  581. */
  582. void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb,
  583. ext4_fsblk_t block, unsigned long count,
  584. unsigned long *pdquot_freed_blocks)
  585. {
  586. struct buffer_head *bitmap_bh = NULL;
  587. struct buffer_head *gd_bh;
  588. ext4_group_t block_group;
  589. ext4_grpblk_t bit;
  590. unsigned long i;
  591. unsigned long overflow;
  592. struct ext4_group_desc * desc;
  593. struct ext4_super_block * es;
  594. struct ext4_sb_info *sbi;
  595. int err = 0, ret;
  596. ext4_grpblk_t group_freed;
  597. *pdquot_freed_blocks = 0;
  598. sbi = EXT4_SB(sb);
  599. es = sbi->s_es;
  600. if (block < le32_to_cpu(es->s_first_data_block) ||
  601. block + count < block ||
  602. block + count > ext4_blocks_count(es)) {
  603. ext4_error (sb, "ext4_free_blocks",
  604. "Freeing blocks not in datazone - "
  605. "block = %llu, count = %lu", block, count);
  606. goto error_return;
  607. }
  608. ext4_debug ("freeing block(s) %llu-%llu\n", block, block + count - 1);
  609. do_more:
  610. overflow = 0;
  611. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  612. /*
  613. * Check to see if we are freeing blocks across a group
  614. * boundary.
  615. */
  616. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  617. overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
  618. count -= overflow;
  619. }
  620. brelse(bitmap_bh);
  621. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  622. if (!bitmap_bh)
  623. goto error_return;
  624. desc = ext4_get_group_desc (sb, block_group, &gd_bh);
  625. if (!desc)
  626. goto error_return;
  627. if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
  628. in_range(ext4_inode_bitmap(sb, desc), block, count) ||
  629. in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
  630. in_range(block + count - 1, ext4_inode_table(sb, desc),
  631. sbi->s_itb_per_group)) {
  632. ext4_error (sb, "ext4_free_blocks",
  633. "Freeing blocks in system zones - "
  634. "Block = %llu, count = %lu",
  635. block, count);
  636. goto error_return;
  637. }
  638. /*
  639. * We are about to start releasing blocks in the bitmap,
  640. * so we need undo access.
  641. */
  642. /* @@@ check errors */
  643. BUFFER_TRACE(bitmap_bh, "getting undo access");
  644. err = ext4_journal_get_undo_access(handle, bitmap_bh);
  645. if (err)
  646. goto error_return;
  647. /*
  648. * We are about to modify some metadata. Call the journal APIs
  649. * to unshare ->b_data if a currently-committing transaction is
  650. * using it
  651. */
  652. BUFFER_TRACE(gd_bh, "get_write_access");
  653. err = ext4_journal_get_write_access(handle, gd_bh);
  654. if (err)
  655. goto error_return;
  656. jbd_lock_bh_state(bitmap_bh);
  657. for (i = 0, group_freed = 0; i < count; i++) {
  658. /*
  659. * An HJ special. This is expensive...
  660. */
  661. #ifdef CONFIG_JBD2_DEBUG
  662. jbd_unlock_bh_state(bitmap_bh);
  663. {
  664. struct buffer_head *debug_bh;
  665. debug_bh = sb_find_get_block(sb, block + i);
  666. if (debug_bh) {
  667. BUFFER_TRACE(debug_bh, "Deleted!");
  668. if (!bh2jh(bitmap_bh)->b_committed_data)
  669. BUFFER_TRACE(debug_bh,
  670. "No commited data in bitmap");
  671. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  672. __brelse(debug_bh);
  673. }
  674. }
  675. jbd_lock_bh_state(bitmap_bh);
  676. #endif
  677. if (need_resched()) {
  678. jbd_unlock_bh_state(bitmap_bh);
  679. cond_resched();
  680. jbd_lock_bh_state(bitmap_bh);
  681. }
  682. /* @@@ This prevents newly-allocated data from being
  683. * freed and then reallocated within the same
  684. * transaction.
  685. *
  686. * Ideally we would want to allow that to happen, but to
  687. * do so requires making jbd2_journal_forget() capable of
  688. * revoking the queued write of a data block, which
  689. * implies blocking on the journal lock. *forget()
  690. * cannot block due to truncate races.
  691. *
  692. * Eventually we can fix this by making jbd2_journal_forget()
  693. * return a status indicating whether or not it was able
  694. * to revoke the buffer. On successful revoke, it is
  695. * safe not to set the allocation bit in the committed
  696. * bitmap, because we know that there is no outstanding
  697. * activity on the buffer any more and so it is safe to
  698. * reallocate it.
  699. */
  700. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  701. J_ASSERT_BH(bitmap_bh,
  702. bh2jh(bitmap_bh)->b_committed_data != NULL);
  703. ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  704. bh2jh(bitmap_bh)->b_committed_data);
  705. /*
  706. * We clear the bit in the bitmap after setting the committed
  707. * data bit, because this is the reverse order to that which
  708. * the allocator uses.
  709. */
  710. BUFFER_TRACE(bitmap_bh, "clear bit");
  711. if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  712. bit + i, bitmap_bh->b_data)) {
  713. jbd_unlock_bh_state(bitmap_bh);
  714. ext4_error(sb, __func__,
  715. "bit already cleared for block %llu",
  716. (ext4_fsblk_t)(block + i));
  717. jbd_lock_bh_state(bitmap_bh);
  718. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  719. } else {
  720. group_freed++;
  721. }
  722. }
  723. jbd_unlock_bh_state(bitmap_bh);
  724. spin_lock(sb_bgl_lock(sbi, block_group));
  725. le16_add_cpu(&desc->bg_free_blocks_count, group_freed);
  726. desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
  727. spin_unlock(sb_bgl_lock(sbi, block_group));
  728. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  729. if (sbi->s_log_groups_per_flex) {
  730. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  731. spin_lock(sb_bgl_lock(sbi, flex_group));
  732. sbi->s_flex_groups[flex_group].free_blocks += count;
  733. spin_unlock(sb_bgl_lock(sbi, flex_group));
  734. }
  735. /* We dirtied the bitmap block */
  736. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  737. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  738. /* And the group descriptor block */
  739. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  740. ret = ext4_journal_dirty_metadata(handle, gd_bh);
  741. if (!err) err = ret;
  742. *pdquot_freed_blocks += group_freed;
  743. if (overflow && !err) {
  744. block += count;
  745. count = overflow;
  746. goto do_more;
  747. }
  748. sb->s_dirt = 1;
  749. error_return:
  750. brelse(bitmap_bh);
  751. ext4_std_error(sb, err);
  752. return;
  753. }
  754. /**
  755. * ext4_free_blocks() -- Free given blocks and update quota
  756. * @handle: handle for this transaction
  757. * @inode: inode
  758. * @block: start physical block to free
  759. * @count: number of blocks to count
  760. * @metadata: Are these metadata blocks
  761. */
  762. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  763. ext4_fsblk_t block, unsigned long count,
  764. int metadata)
  765. {
  766. struct super_block * sb;
  767. unsigned long dquot_freed_blocks;
  768. /* this isn't the right place to decide whether block is metadata
  769. * inode.c/extents.c knows better, but for safety ... */
  770. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
  771. ext4_should_journal_data(inode))
  772. metadata = 1;
  773. sb = inode->i_sb;
  774. if (!test_opt(sb, MBALLOC) || !EXT4_SB(sb)->s_group_info)
  775. ext4_free_blocks_sb(handle, sb, block, count,
  776. &dquot_freed_blocks);
  777. else
  778. ext4_mb_free_blocks(handle, inode, block, count,
  779. metadata, &dquot_freed_blocks);
  780. if (dquot_freed_blocks)
  781. DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
  782. return;
  783. }
  784. /**
  785. * ext4_test_allocatable()
  786. * @nr: given allocation block group
  787. * @bh: bufferhead contains the bitmap of the given block group
  788. *
  789. * For ext4 allocations, we must not reuse any blocks which are
  790. * allocated in the bitmap buffer's "last committed data" copy. This
  791. * prevents deletes from freeing up the page for reuse until we have
  792. * committed the delete transaction.
  793. *
  794. * If we didn't do this, then deleting something and reallocating it as
  795. * data would allow the old block to be overwritten before the
  796. * transaction committed (because we force data to disk before commit).
  797. * This would lead to corruption if we crashed between overwriting the
  798. * data and committing the delete.
  799. *
  800. * @@@ We may want to make this allocation behaviour conditional on
  801. * data-writes at some point, and disable it for metadata allocations or
  802. * sync-data inodes.
  803. */
  804. static int ext4_test_allocatable(ext4_grpblk_t nr, struct buffer_head *bh)
  805. {
  806. int ret;
  807. struct journal_head *jh = bh2jh(bh);
  808. if (ext4_test_bit(nr, bh->b_data))
  809. return 0;
  810. jbd_lock_bh_state(bh);
  811. if (!jh->b_committed_data)
  812. ret = 1;
  813. else
  814. ret = !ext4_test_bit(nr, jh->b_committed_data);
  815. jbd_unlock_bh_state(bh);
  816. return ret;
  817. }
  818. /**
  819. * bitmap_search_next_usable_block()
  820. * @start: the starting block (group relative) of the search
  821. * @bh: bufferhead contains the block group bitmap
  822. * @maxblocks: the ending block (group relative) of the reservation
  823. *
  824. * The bitmap search --- search forward alternately through the actual
  825. * bitmap on disk and the last-committed copy in journal, until we find a
  826. * bit free in both bitmaps.
  827. */
  828. static ext4_grpblk_t
  829. bitmap_search_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh,
  830. ext4_grpblk_t maxblocks)
  831. {
  832. ext4_grpblk_t next;
  833. struct journal_head *jh = bh2jh(bh);
  834. while (start < maxblocks) {
  835. next = ext4_find_next_zero_bit(bh->b_data, maxblocks, start);
  836. if (next >= maxblocks)
  837. return -1;
  838. if (ext4_test_allocatable(next, bh))
  839. return next;
  840. jbd_lock_bh_state(bh);
  841. if (jh->b_committed_data)
  842. start = ext4_find_next_zero_bit(jh->b_committed_data,
  843. maxblocks, next);
  844. jbd_unlock_bh_state(bh);
  845. }
  846. return -1;
  847. }
  848. /**
  849. * find_next_usable_block()
  850. * @start: the starting block (group relative) to find next
  851. * allocatable block in bitmap.
  852. * @bh: bufferhead contains the block group bitmap
  853. * @maxblocks: the ending block (group relative) for the search
  854. *
  855. * Find an allocatable block in a bitmap. We honor both the bitmap and
  856. * its last-committed copy (if that exists), and perform the "most
  857. * appropriate allocation" algorithm of looking for a free block near
  858. * the initial goal; then for a free byte somewhere in the bitmap; then
  859. * for any free bit in the bitmap.
  860. */
  861. static ext4_grpblk_t
  862. find_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh,
  863. ext4_grpblk_t maxblocks)
  864. {
  865. ext4_grpblk_t here, next;
  866. char *p, *r;
  867. if (start > 0) {
  868. /*
  869. * The goal was occupied; search forward for a free
  870. * block within the next XX blocks.
  871. *
  872. * end_goal is more or less random, but it has to be
  873. * less than EXT4_BLOCKS_PER_GROUP. Aligning up to the
  874. * next 64-bit boundary is simple..
  875. */
  876. ext4_grpblk_t end_goal = (start + 63) & ~63;
  877. if (end_goal > maxblocks)
  878. end_goal = maxblocks;
  879. here = ext4_find_next_zero_bit(bh->b_data, end_goal, start);
  880. if (here < end_goal && ext4_test_allocatable(here, bh))
  881. return here;
  882. ext4_debug("Bit not found near goal\n");
  883. }
  884. here = start;
  885. if (here < 0)
  886. here = 0;
  887. p = ((char *)bh->b_data) + (here >> 3);
  888. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  889. next = (r - ((char *)bh->b_data)) << 3;
  890. if (next < maxblocks && next >= start && ext4_test_allocatable(next, bh))
  891. return next;
  892. /*
  893. * The bitmap search --- search forward alternately through the actual
  894. * bitmap and the last-committed copy until we find a bit free in
  895. * both
  896. */
  897. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  898. return here;
  899. }
  900. /**
  901. * claim_block()
  902. * @block: the free block (group relative) to allocate
  903. * @bh: the bufferhead containts the block group bitmap
  904. *
  905. * We think we can allocate this block in this bitmap. Try to set the bit.
  906. * If that succeeds then check that nobody has allocated and then freed the
  907. * block since we saw that is was not marked in b_committed_data. If it _was_
  908. * allocated and freed then clear the bit in the bitmap again and return
  909. * zero (failure).
  910. */
  911. static inline int
  912. claim_block(spinlock_t *lock, ext4_grpblk_t block, struct buffer_head *bh)
  913. {
  914. struct journal_head *jh = bh2jh(bh);
  915. int ret;
  916. if (ext4_set_bit_atomic(lock, block, bh->b_data))
  917. return 0;
  918. jbd_lock_bh_state(bh);
  919. if (jh->b_committed_data && ext4_test_bit(block,jh->b_committed_data)) {
  920. ext4_clear_bit_atomic(lock, block, bh->b_data);
  921. ret = 0;
  922. } else {
  923. ret = 1;
  924. }
  925. jbd_unlock_bh_state(bh);
  926. return ret;
  927. }
  928. /**
  929. * ext4_try_to_allocate()
  930. * @sb: superblock
  931. * @handle: handle to this transaction
  932. * @group: given allocation block group
  933. * @bitmap_bh: bufferhead holds the block bitmap
  934. * @grp_goal: given target block within the group
  935. * @count: target number of blocks to allocate
  936. * @my_rsv: reservation window
  937. *
  938. * Attempt to allocate blocks within a give range. Set the range of allocation
  939. * first, then find the first free bit(s) from the bitmap (within the range),
  940. * and at last, allocate the blocks by claiming the found free bit as allocated.
  941. *
  942. * To set the range of this allocation:
  943. * if there is a reservation window, only try to allocate block(s) from the
  944. * file's own reservation window;
  945. * Otherwise, the allocation range starts from the give goal block, ends at
  946. * the block group's last block.
  947. *
  948. * If we failed to allocate the desired block then we may end up crossing to a
  949. * new bitmap. In that case we must release write access to the old one via
  950. * ext4_journal_release_buffer(), else we'll run out of credits.
  951. */
  952. static ext4_grpblk_t
  953. ext4_try_to_allocate(struct super_block *sb, handle_t *handle,
  954. ext4_group_t group, struct buffer_head *bitmap_bh,
  955. ext4_grpblk_t grp_goal, unsigned long *count,
  956. struct ext4_reserve_window *my_rsv)
  957. {
  958. ext4_fsblk_t group_first_block;
  959. ext4_grpblk_t start, end;
  960. unsigned long num = 0;
  961. /* we do allocation within the reservation window if we have a window */
  962. if (my_rsv) {
  963. group_first_block = ext4_group_first_block_no(sb, group);
  964. if (my_rsv->_rsv_start >= group_first_block)
  965. start = my_rsv->_rsv_start - group_first_block;
  966. else
  967. /* reservation window cross group boundary */
  968. start = 0;
  969. end = my_rsv->_rsv_end - group_first_block + 1;
  970. if (end > EXT4_BLOCKS_PER_GROUP(sb))
  971. /* reservation window crosses group boundary */
  972. end = EXT4_BLOCKS_PER_GROUP(sb);
  973. if ((start <= grp_goal) && (grp_goal < end))
  974. start = grp_goal;
  975. else
  976. grp_goal = -1;
  977. } else {
  978. if (grp_goal > 0)
  979. start = grp_goal;
  980. else
  981. start = 0;
  982. end = EXT4_BLOCKS_PER_GROUP(sb);
  983. }
  984. BUG_ON(start > EXT4_BLOCKS_PER_GROUP(sb));
  985. repeat:
  986. if (grp_goal < 0 || !ext4_test_allocatable(grp_goal, bitmap_bh)) {
  987. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  988. if (grp_goal < 0)
  989. goto fail_access;
  990. if (!my_rsv) {
  991. int i;
  992. for (i = 0; i < 7 && grp_goal > start &&
  993. ext4_test_allocatable(grp_goal - 1,
  994. bitmap_bh);
  995. i++, grp_goal--)
  996. ;
  997. }
  998. }
  999. start = grp_goal;
  1000. if (!claim_block(sb_bgl_lock(EXT4_SB(sb), group),
  1001. grp_goal, bitmap_bh)) {
  1002. /*
  1003. * The block was allocated by another thread, or it was
  1004. * allocated and then freed by another thread
  1005. */
  1006. start++;
  1007. grp_goal++;
  1008. if (start >= end)
  1009. goto fail_access;
  1010. goto repeat;
  1011. }
  1012. num++;
  1013. grp_goal++;
  1014. while (num < *count && grp_goal < end
  1015. && ext4_test_allocatable(grp_goal, bitmap_bh)
  1016. && claim_block(sb_bgl_lock(EXT4_SB(sb), group),
  1017. grp_goal, bitmap_bh)) {
  1018. num++;
  1019. grp_goal++;
  1020. }
  1021. *count = num;
  1022. return grp_goal - num;
  1023. fail_access:
  1024. *count = num;
  1025. return -1;
  1026. }
  1027. /**
  1028. * find_next_reservable_window():
  1029. * find a reservable space within the given range.
  1030. * It does not allocate the reservation window for now:
  1031. * alloc_new_reservation() will do the work later.
  1032. *
  1033. * @search_head: the head of the searching list;
  1034. * This is not necessarily the list head of the whole filesystem
  1035. *
  1036. * We have both head and start_block to assist the search
  1037. * for the reservable space. The list starts from head,
  1038. * but we will shift to the place where start_block is,
  1039. * then start from there, when looking for a reservable space.
  1040. *
  1041. * @size: the target new reservation window size
  1042. *
  1043. * @group_first_block: the first block we consider to start
  1044. * the real search from
  1045. *
  1046. * @last_block:
  1047. * the maximum block number that our goal reservable space
  1048. * could start from. This is normally the last block in this
  1049. * group. The search will end when we found the start of next
  1050. * possible reservable space is out of this boundary.
  1051. * This could handle the cross boundary reservation window
  1052. * request.
  1053. *
  1054. * basically we search from the given range, rather than the whole
  1055. * reservation double linked list, (start_block, last_block)
  1056. * to find a free region that is of my size and has not
  1057. * been reserved.
  1058. *
  1059. */
  1060. static int find_next_reservable_window(
  1061. struct ext4_reserve_window_node *search_head,
  1062. struct ext4_reserve_window_node *my_rsv,
  1063. struct super_block * sb,
  1064. ext4_fsblk_t start_block,
  1065. ext4_fsblk_t last_block)
  1066. {
  1067. struct rb_node *next;
  1068. struct ext4_reserve_window_node *rsv, *prev;
  1069. ext4_fsblk_t cur;
  1070. int size = my_rsv->rsv_goal_size;
  1071. /* TODO: make the start of the reservation window byte-aligned */
  1072. /* cur = *start_block & ~7;*/
  1073. cur = start_block;
  1074. rsv = search_head;
  1075. if (!rsv)
  1076. return -1;
  1077. while (1) {
  1078. if (cur <= rsv->rsv_end)
  1079. cur = rsv->rsv_end + 1;
  1080. /* TODO?
  1081. * in the case we could not find a reservable space
  1082. * that is what is expected, during the re-search, we could
  1083. * remember what's the largest reservable space we could have
  1084. * and return that one.
  1085. *
  1086. * For now it will fail if we could not find the reservable
  1087. * space with expected-size (or more)...
  1088. */
  1089. if (cur > last_block)
  1090. return -1; /* fail */
  1091. prev = rsv;
  1092. next = rb_next(&rsv->rsv_node);
  1093. rsv = rb_entry(next,struct ext4_reserve_window_node,rsv_node);
  1094. /*
  1095. * Reached the last reservation, we can just append to the
  1096. * previous one.
  1097. */
  1098. if (!next)
  1099. break;
  1100. if (cur + size <= rsv->rsv_start) {
  1101. /*
  1102. * Found a reserveable space big enough. We could
  1103. * have a reservation across the group boundary here
  1104. */
  1105. break;
  1106. }
  1107. }
  1108. /*
  1109. * we come here either :
  1110. * when we reach the end of the whole list,
  1111. * and there is empty reservable space after last entry in the list.
  1112. * append it to the end of the list.
  1113. *
  1114. * or we found one reservable space in the middle of the list,
  1115. * return the reservation window that we could append to.
  1116. * succeed.
  1117. */
  1118. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  1119. rsv_window_remove(sb, my_rsv);
  1120. /*
  1121. * Let's book the whole avaliable window for now. We will check the
  1122. * disk bitmap later and then, if there are free blocks then we adjust
  1123. * the window size if it's larger than requested.
  1124. * Otherwise, we will remove this node from the tree next time
  1125. * call find_next_reservable_window.
  1126. */
  1127. my_rsv->rsv_start = cur;
  1128. my_rsv->rsv_end = cur + size - 1;
  1129. my_rsv->rsv_alloc_hit = 0;
  1130. if (prev != my_rsv)
  1131. ext4_rsv_window_add(sb, my_rsv);
  1132. return 0;
  1133. }
  1134. /**
  1135. * alloc_new_reservation()--allocate a new reservation window
  1136. *
  1137. * To make a new reservation, we search part of the filesystem
  1138. * reservation list (the list that inside the group). We try to
  1139. * allocate a new reservation window near the allocation goal,
  1140. * or the beginning of the group, if there is no goal.
  1141. *
  1142. * We first find a reservable space after the goal, then from
  1143. * there, we check the bitmap for the first free block after
  1144. * it. If there is no free block until the end of group, then the
  1145. * whole group is full, we failed. Otherwise, check if the free
  1146. * block is inside the expected reservable space, if so, we
  1147. * succeed.
  1148. * If the first free block is outside the reservable space, then
  1149. * start from the first free block, we search for next available
  1150. * space, and go on.
  1151. *
  1152. * on succeed, a new reservation will be found and inserted into the list
  1153. * It contains at least one free block, and it does not overlap with other
  1154. * reservation windows.
  1155. *
  1156. * failed: we failed to find a reservation window in this group
  1157. *
  1158. * @rsv: the reservation
  1159. *
  1160. * @grp_goal: The goal (group-relative). It is where the search for a
  1161. * free reservable space should start from.
  1162. * if we have a grp_goal(grp_goal >0 ), then start from there,
  1163. * no grp_goal(grp_goal = -1), we start from the first block
  1164. * of the group.
  1165. *
  1166. * @sb: the super block
  1167. * @group: the group we are trying to allocate in
  1168. * @bitmap_bh: the block group block bitmap
  1169. *
  1170. */
  1171. static int alloc_new_reservation(struct ext4_reserve_window_node *my_rsv,
  1172. ext4_grpblk_t grp_goal, struct super_block *sb,
  1173. ext4_group_t group, struct buffer_head *bitmap_bh)
  1174. {
  1175. struct ext4_reserve_window_node *search_head;
  1176. ext4_fsblk_t group_first_block, group_end_block, start_block;
  1177. ext4_grpblk_t first_free_block;
  1178. struct rb_root *fs_rsv_root = &EXT4_SB(sb)->s_rsv_window_root;
  1179. unsigned long size;
  1180. int ret;
  1181. spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock;
  1182. group_first_block = ext4_group_first_block_no(sb, group);
  1183. group_end_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1184. if (grp_goal < 0)
  1185. start_block = group_first_block;
  1186. else
  1187. start_block = grp_goal + group_first_block;
  1188. size = my_rsv->rsv_goal_size;
  1189. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  1190. /*
  1191. * if the old reservation is cross group boundary
  1192. * and if the goal is inside the old reservation window,
  1193. * we will come here when we just failed to allocate from
  1194. * the first part of the window. We still have another part
  1195. * that belongs to the next group. In this case, there is no
  1196. * point to discard our window and try to allocate a new one
  1197. * in this group(which will fail). we should
  1198. * keep the reservation window, just simply move on.
  1199. *
  1200. * Maybe we could shift the start block of the reservation
  1201. * window to the first block of next group.
  1202. */
  1203. if ((my_rsv->rsv_start <= group_end_block) &&
  1204. (my_rsv->rsv_end > group_end_block) &&
  1205. (start_block >= my_rsv->rsv_start))
  1206. return -1;
  1207. if ((my_rsv->rsv_alloc_hit >
  1208. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  1209. /*
  1210. * if the previously allocation hit ratio is
  1211. * greater than 1/2, then we double the size of
  1212. * the reservation window the next time,
  1213. * otherwise we keep the same size window
  1214. */
  1215. size = size * 2;
  1216. if (size > EXT4_MAX_RESERVE_BLOCKS)
  1217. size = EXT4_MAX_RESERVE_BLOCKS;
  1218. my_rsv->rsv_goal_size= size;
  1219. }
  1220. }
  1221. spin_lock(rsv_lock);
  1222. /*
  1223. * shift the search start to the window near the goal block
  1224. */
  1225. search_head = search_reserve_window(fs_rsv_root, start_block);
  1226. /*
  1227. * find_next_reservable_window() simply finds a reservable window
  1228. * inside the given range(start_block, group_end_block).
  1229. *
  1230. * To make sure the reservation window has a free bit inside it, we
  1231. * need to check the bitmap after we found a reservable window.
  1232. */
  1233. retry:
  1234. ret = find_next_reservable_window(search_head, my_rsv, sb,
  1235. start_block, group_end_block);
  1236. if (ret == -1) {
  1237. if (!rsv_is_empty(&my_rsv->rsv_window))
  1238. rsv_window_remove(sb, my_rsv);
  1239. spin_unlock(rsv_lock);
  1240. return -1;
  1241. }
  1242. /*
  1243. * On success, find_next_reservable_window() returns the
  1244. * reservation window where there is a reservable space after it.
  1245. * Before we reserve this reservable space, we need
  1246. * to make sure there is at least a free block inside this region.
  1247. *
  1248. * searching the first free bit on the block bitmap and copy of
  1249. * last committed bitmap alternatively, until we found a allocatable
  1250. * block. Search start from the start block of the reservable space
  1251. * we just found.
  1252. */
  1253. spin_unlock(rsv_lock);
  1254. first_free_block = bitmap_search_next_usable_block(
  1255. my_rsv->rsv_start - group_first_block,
  1256. bitmap_bh, group_end_block - group_first_block + 1);
  1257. if (first_free_block < 0) {
  1258. /*
  1259. * no free block left on the bitmap, no point
  1260. * to reserve the space. return failed.
  1261. */
  1262. spin_lock(rsv_lock);
  1263. if (!rsv_is_empty(&my_rsv->rsv_window))
  1264. rsv_window_remove(sb, my_rsv);
  1265. spin_unlock(rsv_lock);
  1266. return -1; /* failed */
  1267. }
  1268. start_block = first_free_block + group_first_block;
  1269. /*
  1270. * check if the first free block is within the
  1271. * free space we just reserved
  1272. */
  1273. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  1274. return 0; /* success */
  1275. /*
  1276. * if the first free bit we found is out of the reservable space
  1277. * continue search for next reservable space,
  1278. * start from where the free block is,
  1279. * we also shift the list head to where we stopped last time
  1280. */
  1281. search_head = my_rsv;
  1282. spin_lock(rsv_lock);
  1283. goto retry;
  1284. }
  1285. /**
  1286. * try_to_extend_reservation()
  1287. * @my_rsv: given reservation window
  1288. * @sb: super block
  1289. * @size: the delta to extend
  1290. *
  1291. * Attempt to expand the reservation window large enough to have
  1292. * required number of free blocks
  1293. *
  1294. * Since ext4_try_to_allocate() will always allocate blocks within
  1295. * the reservation window range, if the window size is too small,
  1296. * multiple blocks allocation has to stop at the end of the reservation
  1297. * window. To make this more efficient, given the total number of
  1298. * blocks needed and the current size of the window, we try to
  1299. * expand the reservation window size if necessary on a best-effort
  1300. * basis before ext4_new_blocks() tries to allocate blocks,
  1301. */
  1302. static void try_to_extend_reservation(struct ext4_reserve_window_node *my_rsv,
  1303. struct super_block *sb, int size)
  1304. {
  1305. struct ext4_reserve_window_node *next_rsv;
  1306. struct rb_node *next;
  1307. spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock;
  1308. if (!spin_trylock(rsv_lock))
  1309. return;
  1310. next = rb_next(&my_rsv->rsv_node);
  1311. if (!next)
  1312. my_rsv->rsv_end += size;
  1313. else {
  1314. next_rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node);
  1315. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  1316. my_rsv->rsv_end += size;
  1317. else
  1318. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  1319. }
  1320. spin_unlock(rsv_lock);
  1321. }
  1322. /**
  1323. * ext4_try_to_allocate_with_rsv()
  1324. * @sb: superblock
  1325. * @handle: handle to this transaction
  1326. * @group: given allocation block group
  1327. * @bitmap_bh: bufferhead holds the block bitmap
  1328. * @grp_goal: given target block within the group
  1329. * @count: target number of blocks to allocate
  1330. * @my_rsv: reservation window
  1331. * @errp: pointer to store the error code
  1332. *
  1333. * This is the main function used to allocate a new block and its reservation
  1334. * window.
  1335. *
  1336. * Each time when a new block allocation is need, first try to allocate from
  1337. * its own reservation. If it does not have a reservation window, instead of
  1338. * looking for a free bit on bitmap first, then look up the reservation list to
  1339. * see if it is inside somebody else's reservation window, we try to allocate a
  1340. * reservation window for it starting from the goal first. Then do the block
  1341. * allocation within the reservation window.
  1342. *
  1343. * This will avoid keeping on searching the reservation list again and
  1344. * again when somebody is looking for a free block (without
  1345. * reservation), and there are lots of free blocks, but they are all
  1346. * being reserved.
  1347. *
  1348. * We use a red-black tree for the per-filesystem reservation list.
  1349. *
  1350. */
  1351. static ext4_grpblk_t
  1352. ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  1353. ext4_group_t group, struct buffer_head *bitmap_bh,
  1354. ext4_grpblk_t grp_goal,
  1355. struct ext4_reserve_window_node * my_rsv,
  1356. unsigned long *count, int *errp)
  1357. {
  1358. ext4_fsblk_t group_first_block, group_last_block;
  1359. ext4_grpblk_t ret = 0;
  1360. int fatal;
  1361. unsigned long num = *count;
  1362. *errp = 0;
  1363. /*
  1364. * Make sure we use undo access for the bitmap, because it is critical
  1365. * that we do the frozen_data COW on bitmap buffers in all cases even
  1366. * if the buffer is in BJ_Forget state in the committing transaction.
  1367. */
  1368. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  1369. fatal = ext4_journal_get_undo_access(handle, bitmap_bh);
  1370. if (fatal) {
  1371. *errp = fatal;
  1372. return -1;
  1373. }
  1374. /*
  1375. * we don't deal with reservation when
  1376. * filesystem is mounted without reservation
  1377. * or the file is not a regular file
  1378. * or last attempt to allocate a block with reservation turned on failed
  1379. */
  1380. if (my_rsv == NULL ) {
  1381. ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh,
  1382. grp_goal, count, NULL);
  1383. goto out;
  1384. }
  1385. /*
  1386. * grp_goal is a group relative block number (if there is a goal)
  1387. * 0 <= grp_goal < EXT4_BLOCKS_PER_GROUP(sb)
  1388. * first block is a filesystem wide block number
  1389. * first block is the block number of the first block in this group
  1390. */
  1391. group_first_block = ext4_group_first_block_no(sb, group);
  1392. group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1393. /*
  1394. * Basically we will allocate a new block from inode's reservation
  1395. * window.
  1396. *
  1397. * We need to allocate a new reservation window, if:
  1398. * a) inode does not have a reservation window; or
  1399. * b) last attempt to allocate a block from existing reservation
  1400. * failed; or
  1401. * c) we come here with a goal and with a reservation window
  1402. *
  1403. * We do not need to allocate a new reservation window if we come here
  1404. * at the beginning with a goal and the goal is inside the window, or
  1405. * we don't have a goal but already have a reservation window.
  1406. * then we could go to allocate from the reservation window directly.
  1407. */
  1408. while (1) {
  1409. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1410. !goal_in_my_reservation(&my_rsv->rsv_window,
  1411. grp_goal, group, sb)) {
  1412. if (my_rsv->rsv_goal_size < *count)
  1413. my_rsv->rsv_goal_size = *count;
  1414. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1415. group, bitmap_bh);
  1416. if (ret < 0)
  1417. break; /* failed */
  1418. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  1419. grp_goal, group, sb))
  1420. grp_goal = -1;
  1421. } else if (grp_goal >= 0) {
  1422. int curr = my_rsv->rsv_end -
  1423. (grp_goal + group_first_block) + 1;
  1424. if (curr < *count)
  1425. try_to_extend_reservation(my_rsv, sb,
  1426. *count - curr);
  1427. }
  1428. if ((my_rsv->rsv_start > group_last_block) ||
  1429. (my_rsv->rsv_end < group_first_block)) {
  1430. rsv_window_dump(&EXT4_SB(sb)->s_rsv_window_root, 1);
  1431. BUG();
  1432. }
  1433. ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh,
  1434. grp_goal, &num, &my_rsv->rsv_window);
  1435. if (ret >= 0) {
  1436. my_rsv->rsv_alloc_hit += num;
  1437. *count = num;
  1438. break; /* succeed */
  1439. }
  1440. num = *count;
  1441. }
  1442. out:
  1443. if (ret >= 0) {
  1444. BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
  1445. "bitmap block");
  1446. fatal = ext4_journal_dirty_metadata(handle, bitmap_bh);
  1447. if (fatal) {
  1448. *errp = fatal;
  1449. return -1;
  1450. }
  1451. return ret;
  1452. }
  1453. BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
  1454. ext4_journal_release_buffer(handle, bitmap_bh);
  1455. return ret;
  1456. }
  1457. /**
  1458. * ext4_has_free_blocks()
  1459. * @sbi: in-core super block structure.
  1460. * @nblocks: number of neeed blocks
  1461. *
  1462. * Check if filesystem has free blocks available for allocation.
  1463. * Return the number of blocks avaible for allocation for this request
  1464. * On success, return nblocks
  1465. */
  1466. ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi,
  1467. ext4_fsblk_t nblocks)
  1468. {
  1469. ext4_fsblk_t free_blocks;
  1470. ext4_fsblk_t root_blocks = 0;
  1471. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1472. if (!capable(CAP_SYS_RESOURCE) &&
  1473. sbi->s_resuid != current->fsuid &&
  1474. (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
  1475. root_blocks = ext4_r_blocks_count(sbi->s_es);
  1476. #ifdef CONFIG_SMP
  1477. if (free_blocks - root_blocks < FBC_BATCH)
  1478. free_blocks =
  1479. percpu_counter_sum_and_set(&sbi->s_freeblocks_counter);
  1480. #endif
  1481. if (free_blocks - root_blocks < nblocks)
  1482. return free_blocks - root_blocks;
  1483. return nblocks;
  1484. }
  1485. /**
  1486. * ext4_should_retry_alloc()
  1487. * @sb: super block
  1488. * @retries number of attemps has been made
  1489. *
  1490. * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
  1491. * it is profitable to retry the operation, this function will wait
  1492. * for the current or commiting transaction to complete, and then
  1493. * return TRUE.
  1494. *
  1495. * if the total number of retries exceed three times, return FALSE.
  1496. */
  1497. int ext4_should_retry_alloc(struct super_block *sb, int *retries)
  1498. {
  1499. if (!ext4_has_free_blocks(EXT4_SB(sb), 1) || (*retries)++ > 3)
  1500. return 0;
  1501. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  1502. return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
  1503. }
  1504. /**
  1505. * ext4_old_new_blocks() -- core block bitmap based block allocation function
  1506. *
  1507. * @handle: handle to this transaction
  1508. * @inode: file inode
  1509. * @goal: given target block(filesystem wide)
  1510. * @count: target number of blocks to allocate
  1511. * @errp: error code
  1512. *
  1513. * ext4_old_new_blocks uses a goal block to assist allocation and look up
  1514. * the block bitmap directly to do block allocation. It tries to
  1515. * allocate block(s) from the block group contains the goal block first. If
  1516. * that fails, it will try to allocate block(s) from other block groups
  1517. * without any specific goal block.
  1518. *
  1519. * This function is called when -o nomballoc mount option is enabled
  1520. *
  1521. */
  1522. ext4_fsblk_t ext4_old_new_blocks(handle_t *handle, struct inode *inode,
  1523. ext4_fsblk_t goal, unsigned long *count, int *errp)
  1524. {
  1525. struct buffer_head *bitmap_bh = NULL;
  1526. struct buffer_head *gdp_bh;
  1527. ext4_group_t group_no;
  1528. ext4_group_t goal_group;
  1529. ext4_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1530. ext4_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1531. ext4_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1532. ext4_group_t bgi; /* blockgroup iteration index */
  1533. int fatal = 0, err;
  1534. int performed_allocation = 0;
  1535. ext4_grpblk_t free_blocks; /* number of free blocks in a group */
  1536. struct super_block *sb;
  1537. struct ext4_group_desc *gdp;
  1538. struct ext4_super_block *es;
  1539. struct ext4_sb_info *sbi;
  1540. struct ext4_reserve_window_node *my_rsv = NULL;
  1541. struct ext4_block_alloc_info *block_i;
  1542. unsigned short windowsz = 0;
  1543. ext4_group_t ngroups;
  1544. unsigned long num = *count;
  1545. sb = inode->i_sb;
  1546. if (!sb) {
  1547. *errp = -ENODEV;
  1548. printk("ext4_new_block: nonexistent device");
  1549. return 0;
  1550. }
  1551. sbi = EXT4_SB(sb);
  1552. if (!EXT4_I(inode)->i_delalloc_reserved_flag) {
  1553. /*
  1554. * With delalloc we already reserved the blocks
  1555. */
  1556. *count = ext4_has_free_blocks(sbi, *count);
  1557. }
  1558. if (*count == 0) {
  1559. *errp = -ENOSPC;
  1560. return 0; /*return with ENOSPC error */
  1561. }
  1562. num = *count;
  1563. /*
  1564. * Check quota for allocation of this block.
  1565. */
  1566. if (DQUOT_ALLOC_BLOCK(inode, num)) {
  1567. *errp = -EDQUOT;
  1568. return 0;
  1569. }
  1570. sbi = EXT4_SB(sb);
  1571. es = EXT4_SB(sb)->s_es;
  1572. ext4_debug("goal=%llu.\n", goal);
  1573. /*
  1574. * Allocate a block from reservation only when
  1575. * filesystem is mounted with reservation(default,-o reservation), and
  1576. * it's a regular file, and
  1577. * the desired window size is greater than 0 (One could use ioctl
  1578. * command EXT4_IOC_SETRSVSZ to set the window size to 0 to turn off
  1579. * reservation on that particular file)
  1580. */
  1581. block_i = EXT4_I(inode)->i_block_alloc_info;
  1582. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1583. my_rsv = &block_i->rsv_window_node;
  1584. /*
  1585. * First, test whether the goal block is free.
  1586. */
  1587. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1588. goal >= ext4_blocks_count(es))
  1589. goal = le32_to_cpu(es->s_first_data_block);
  1590. ext4_get_group_no_and_offset(sb, goal, &group_no, &grp_target_blk);
  1591. goal_group = group_no;
  1592. retry_alloc:
  1593. gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
  1594. if (!gdp)
  1595. goto io_error;
  1596. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1597. /*
  1598. * if there is not enough free blocks to make a new resevation
  1599. * turn off reservation for this allocation
  1600. */
  1601. if (my_rsv && (free_blocks < windowsz)
  1602. && (rsv_is_empty(&my_rsv->rsv_window)))
  1603. my_rsv = NULL;
  1604. if (free_blocks > 0) {
  1605. bitmap_bh = ext4_read_block_bitmap(sb, group_no);
  1606. if (!bitmap_bh)
  1607. goto io_error;
  1608. grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle,
  1609. group_no, bitmap_bh, grp_target_blk,
  1610. my_rsv, &num, &fatal);
  1611. if (fatal)
  1612. goto out;
  1613. if (grp_alloc_blk >= 0)
  1614. goto allocated;
  1615. }
  1616. ngroups = EXT4_SB(sb)->s_groups_count;
  1617. smp_rmb();
  1618. /*
  1619. * Now search the rest of the groups. We assume that
  1620. * group_no and gdp correctly point to the last group visited.
  1621. */
  1622. for (bgi = 0; bgi < ngroups; bgi++) {
  1623. group_no++;
  1624. if (group_no >= ngroups)
  1625. group_no = 0;
  1626. gdp = ext4_get_group_desc(sb, group_no, &gdp_bh);
  1627. if (!gdp)
  1628. goto io_error;
  1629. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1630. /*
  1631. * skip this group if the number of
  1632. * free blocks is less than half of the reservation
  1633. * window size.
  1634. */
  1635. if (free_blocks <= (windowsz/2))
  1636. continue;
  1637. brelse(bitmap_bh);
  1638. bitmap_bh = ext4_read_block_bitmap(sb, group_no);
  1639. if (!bitmap_bh)
  1640. goto io_error;
  1641. /*
  1642. * try to allocate block(s) from this group, without a goal(-1).
  1643. */
  1644. grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle,
  1645. group_no, bitmap_bh, -1, my_rsv,
  1646. &num, &fatal);
  1647. if (fatal)
  1648. goto out;
  1649. if (grp_alloc_blk >= 0)
  1650. goto allocated;
  1651. }
  1652. /*
  1653. * We may end up a bogus ealier ENOSPC error due to
  1654. * filesystem is "full" of reservations, but
  1655. * there maybe indeed free blocks avaliable on disk
  1656. * In this case, we just forget about the reservations
  1657. * just do block allocation as without reservations.
  1658. */
  1659. if (my_rsv) {
  1660. my_rsv = NULL;
  1661. windowsz = 0;
  1662. group_no = goal_group;
  1663. goto retry_alloc;
  1664. }
  1665. /* No space left on the device */
  1666. *errp = -ENOSPC;
  1667. goto out;
  1668. allocated:
  1669. ext4_debug("using block group %lu(%d)\n",
  1670. group_no, gdp->bg_free_blocks_count);
  1671. BUFFER_TRACE(gdp_bh, "get_write_access");
  1672. fatal = ext4_journal_get_write_access(handle, gdp_bh);
  1673. if (fatal)
  1674. goto out;
  1675. ret_block = grp_alloc_blk + ext4_group_first_block_no(sb, group_no);
  1676. if (in_range(ext4_block_bitmap(sb, gdp), ret_block, num) ||
  1677. in_range(ext4_inode_bitmap(sb, gdp), ret_block, num) ||
  1678. in_range(ret_block, ext4_inode_table(sb, gdp),
  1679. EXT4_SB(sb)->s_itb_per_group) ||
  1680. in_range(ret_block + num - 1, ext4_inode_table(sb, gdp),
  1681. EXT4_SB(sb)->s_itb_per_group)) {
  1682. ext4_error(sb, "ext4_new_block",
  1683. "Allocating block in system zone - "
  1684. "blocks from %llu, length %lu",
  1685. ret_block, num);
  1686. /*
  1687. * claim_block marked the blocks we allocated
  1688. * as in use. So we may want to selectively
  1689. * mark some of the blocks as free
  1690. */
  1691. goto retry_alloc;
  1692. }
  1693. performed_allocation = 1;
  1694. #ifdef CONFIG_JBD2_DEBUG
  1695. {
  1696. struct buffer_head *debug_bh;
  1697. /* Record bitmap buffer state in the newly allocated block */
  1698. debug_bh = sb_find_get_block(sb, ret_block);
  1699. if (debug_bh) {
  1700. BUFFER_TRACE(debug_bh, "state when allocated");
  1701. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1702. brelse(debug_bh);
  1703. }
  1704. }
  1705. jbd_lock_bh_state(bitmap_bh);
  1706. spin_lock(sb_bgl_lock(sbi, group_no));
  1707. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1708. int i;
  1709. for (i = 0; i < num; i++) {
  1710. if (ext4_test_bit(grp_alloc_blk+i,
  1711. bh2jh(bitmap_bh)->b_committed_data)) {
  1712. printk("%s: block was unexpectedly set in "
  1713. "b_committed_data\n", __func__);
  1714. }
  1715. }
  1716. }
  1717. ext4_debug("found bit %d\n", grp_alloc_blk);
  1718. spin_unlock(sb_bgl_lock(sbi, group_no));
  1719. jbd_unlock_bh_state(bitmap_bh);
  1720. #endif
  1721. if (ret_block + num - 1 >= ext4_blocks_count(es)) {
  1722. ext4_error(sb, "ext4_new_block",
  1723. "block(%llu) >= blocks count(%llu) - "
  1724. "block_group = %lu, es == %p ", ret_block,
  1725. ext4_blocks_count(es), group_no, es);
  1726. goto out;
  1727. }
  1728. /*
  1729. * It is up to the caller to add the new buffer to a journal
  1730. * list of some description. We don't know in advance whether
  1731. * the caller wants to use it as metadata or data.
  1732. */
  1733. spin_lock(sb_bgl_lock(sbi, group_no));
  1734. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
  1735. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  1736. le16_add_cpu(&gdp->bg_free_blocks_count, -num);
  1737. gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp);
  1738. spin_unlock(sb_bgl_lock(sbi, group_no));
  1739. if (!EXT4_I(inode)->i_delalloc_reserved_flag)
  1740. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1741. if (sbi->s_log_groups_per_flex) {
  1742. ext4_group_t flex_group = ext4_flex_group(sbi, group_no);
  1743. spin_lock(sb_bgl_lock(sbi, flex_group));
  1744. sbi->s_flex_groups[flex_group].free_blocks -= num;
  1745. spin_unlock(sb_bgl_lock(sbi, flex_group));
  1746. }
  1747. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1748. err = ext4_journal_dirty_metadata(handle, gdp_bh);
  1749. if (!fatal)
  1750. fatal = err;
  1751. sb->s_dirt = 1;
  1752. if (fatal)
  1753. goto out;
  1754. *errp = 0;
  1755. brelse(bitmap_bh);
  1756. DQUOT_FREE_BLOCK(inode, *count-num);
  1757. *count = num;
  1758. return ret_block;
  1759. io_error:
  1760. *errp = -EIO;
  1761. out:
  1762. if (fatal) {
  1763. *errp = fatal;
  1764. ext4_std_error(sb, fatal);
  1765. }
  1766. /*
  1767. * Undo the block allocation
  1768. */
  1769. if (!performed_allocation)
  1770. DQUOT_FREE_BLOCK(inode, *count);
  1771. brelse(bitmap_bh);
  1772. return 0;
  1773. }
  1774. #define EXT4_META_BLOCK 0x1
  1775. static ext4_fsblk_t do_blk_alloc(handle_t *handle, struct inode *inode,
  1776. ext4_lblk_t iblock, ext4_fsblk_t goal,
  1777. unsigned long *count, int *errp, int flags)
  1778. {
  1779. struct ext4_allocation_request ar;
  1780. ext4_fsblk_t ret;
  1781. if (!test_opt(inode->i_sb, MBALLOC)) {
  1782. return ext4_old_new_blocks(handle, inode, goal, count, errp);
  1783. }
  1784. memset(&ar, 0, sizeof(ar));
  1785. /* Fill with neighbour allocated blocks */
  1786. ar.inode = inode;
  1787. ar.goal = goal;
  1788. ar.len = *count;
  1789. ar.logical = iblock;
  1790. if (S_ISREG(inode->i_mode) && !(flags & EXT4_META_BLOCK))
  1791. /* enable in-core preallocation for data block allocation */
  1792. ar.flags = EXT4_MB_HINT_DATA;
  1793. else
  1794. /* disable in-core preallocation for non-regular files */
  1795. ar.flags = 0;
  1796. ret = ext4_mb_new_blocks(handle, &ar, errp);
  1797. *count = ar.len;
  1798. return ret;
  1799. }
  1800. /*
  1801. * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
  1802. *
  1803. * @handle: handle to this transaction
  1804. * @inode: file inode
  1805. * @goal: given target block(filesystem wide)
  1806. * @count: total number of blocks need
  1807. * @errp: error code
  1808. *
  1809. * Return 1st allocated block numberon success, *count stores total account
  1810. * error stores in errp pointer
  1811. */
  1812. ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  1813. ext4_fsblk_t goal, unsigned long *count, int *errp)
  1814. {
  1815. ext4_fsblk_t ret;
  1816. ret = do_blk_alloc(handle, inode, 0, goal,
  1817. count, errp, EXT4_META_BLOCK);
  1818. /*
  1819. * Account for the allocated meta blocks
  1820. */
  1821. if (!(*errp)) {
  1822. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1823. EXT4_I(inode)->i_allocated_meta_blocks += *count;
  1824. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1825. }
  1826. return ret;
  1827. }
  1828. /*
  1829. * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks
  1830. *
  1831. * @handle: handle to this transaction
  1832. * @inode: file inode
  1833. * @goal: given target block(filesystem wide)
  1834. * @errp: error code
  1835. *
  1836. * Return allocated block number on success
  1837. */
  1838. ext4_fsblk_t ext4_new_meta_block(handle_t *handle, struct inode *inode,
  1839. ext4_fsblk_t goal, int *errp)
  1840. {
  1841. unsigned long count = 1;
  1842. return ext4_new_meta_blocks(handle, inode, goal, &count, errp);
  1843. }
  1844. /*
  1845. * ext4_new_blocks() -- allocate data blocks
  1846. *
  1847. * @handle: handle to this transaction
  1848. * @inode: file inode
  1849. * @goal: given target block(filesystem wide)
  1850. * @count: total number of blocks need
  1851. * @errp: error code
  1852. *
  1853. * Return 1st allocated block numberon success, *count stores total account
  1854. * error stores in errp pointer
  1855. */
  1856. ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
  1857. ext4_lblk_t iblock, ext4_fsblk_t goal,
  1858. unsigned long *count, int *errp)
  1859. {
  1860. return do_blk_alloc(handle, inode, iblock, goal, count, errp, 0);
  1861. }
  1862. /**
  1863. * ext4_count_free_blocks() -- count filesystem free blocks
  1864. * @sb: superblock
  1865. *
  1866. * Adds up the number of free blocks from each block group.
  1867. */
  1868. ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
  1869. {
  1870. ext4_fsblk_t desc_count;
  1871. struct ext4_group_desc *gdp;
  1872. ext4_group_t i;
  1873. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  1874. #ifdef EXT4FS_DEBUG
  1875. struct ext4_super_block *es;
  1876. ext4_fsblk_t bitmap_count;
  1877. unsigned long x;
  1878. struct buffer_head *bitmap_bh = NULL;
  1879. es = EXT4_SB(sb)->s_es;
  1880. desc_count = 0;
  1881. bitmap_count = 0;
  1882. gdp = NULL;
  1883. smp_rmb();
  1884. for (i = 0; i < ngroups; i++) {
  1885. gdp = ext4_get_group_desc(sb, i, NULL);
  1886. if (!gdp)
  1887. continue;
  1888. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1889. brelse(bitmap_bh);
  1890. bitmap_bh = ext4_read_block_bitmap(sb, i);
  1891. if (bitmap_bh == NULL)
  1892. continue;
  1893. x = ext4_count_free(bitmap_bh, sb->s_blocksize);
  1894. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1895. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1896. bitmap_count += x;
  1897. }
  1898. brelse(bitmap_bh);
  1899. printk("ext4_count_free_blocks: stored = %llu"
  1900. ", computed = %llu, %llu\n",
  1901. ext4_free_blocks_count(es),
  1902. desc_count, bitmap_count);
  1903. return bitmap_count;
  1904. #else
  1905. desc_count = 0;
  1906. smp_rmb();
  1907. for (i = 0; i < ngroups; i++) {
  1908. gdp = ext4_get_group_desc(sb, i, NULL);
  1909. if (!gdp)
  1910. continue;
  1911. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1912. }
  1913. return desc_count;
  1914. #endif
  1915. }
  1916. static inline int test_root(ext4_group_t a, int b)
  1917. {
  1918. int num = b;
  1919. while (a > num)
  1920. num *= b;
  1921. return num == a;
  1922. }
  1923. static int ext4_group_sparse(ext4_group_t group)
  1924. {
  1925. if (group <= 1)
  1926. return 1;
  1927. if (!(group & 1))
  1928. return 0;
  1929. return (test_root(group, 7) || test_root(group, 5) ||
  1930. test_root(group, 3));
  1931. }
  1932. /**
  1933. * ext4_bg_has_super - number of blocks used by the superblock in group
  1934. * @sb: superblock for filesystem
  1935. * @group: group number to check
  1936. *
  1937. * Return the number of blocks used by the superblock (primary or backup)
  1938. * in this group. Currently this will be only 0 or 1.
  1939. */
  1940. int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
  1941. {
  1942. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  1943. EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1944. !ext4_group_sparse(group))
  1945. return 0;
  1946. return 1;
  1947. }
  1948. static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
  1949. ext4_group_t group)
  1950. {
  1951. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  1952. ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
  1953. ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
  1954. if (group == first || group == first + 1 || group == last)
  1955. return 1;
  1956. return 0;
  1957. }
  1958. static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
  1959. ext4_group_t group)
  1960. {
  1961. return ext4_bg_has_super(sb, group) ? EXT4_SB(sb)->s_gdb_count : 0;
  1962. }
  1963. /**
  1964. * ext4_bg_num_gdb - number of blocks used by the group table in group
  1965. * @sb: superblock for filesystem
  1966. * @group: group number to check
  1967. *
  1968. * Return the number of blocks used by the group descriptor table
  1969. * (primary or backup) in this group. In the future there may be a
  1970. * different number of descriptor blocks in each group.
  1971. */
  1972. unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
  1973. {
  1974. unsigned long first_meta_bg =
  1975. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
  1976. unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
  1977. if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
  1978. metagroup < first_meta_bg)
  1979. return ext4_bg_num_gdb_nometa(sb,group);
  1980. return ext4_bg_num_gdb_meta(sb,group);
  1981. }