balloc.c 61 KB

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