balloc.c 57 KB

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