balloc.c 62 KB

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