balloc.c 64 KB

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