balloc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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/config.h>
  14. #include <linux/time.h>
  15. #include <linux/capability.h>
  16. #include <linux/fs.h>
  17. #include <linux/jbd.h>
  18. #include <linux/ext3_fs.h>
  19. #include <linux/ext3_jbd.h>
  20. #include <linux/quotaops.h>
  21. #include <linux/buffer_head.h>
  22. /*
  23. * balloc.c contains the blocks allocation and deallocation routines
  24. */
  25. /*
  26. * The free blocks are managed by bitmaps. A file system contains several
  27. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  28. * block for inodes, N blocks for the inode table and data blocks.
  29. *
  30. * The file system contains group descriptors which are located after the
  31. * super block. Each descriptor contains the number of the bitmap block and
  32. * the free blocks count in the block. The descriptors are loaded in memory
  33. * when a file system is mounted (see ext3_read_super).
  34. */
  35. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  36. struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
  37. unsigned int block_group,
  38. struct buffer_head ** bh)
  39. {
  40. unsigned long group_desc;
  41. unsigned long offset;
  42. struct ext3_group_desc * desc;
  43. struct ext3_sb_info *sbi = EXT3_SB(sb);
  44. if (block_group >= sbi->s_groups_count) {
  45. ext3_error (sb, "ext3_get_group_desc",
  46. "block_group >= groups_count - "
  47. "block_group = %d, groups_count = %lu",
  48. block_group, sbi->s_groups_count);
  49. return NULL;
  50. }
  51. smp_rmb();
  52. group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
  53. offset = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
  54. if (!sbi->s_group_desc[group_desc]) {
  55. ext3_error (sb, "ext3_get_group_desc",
  56. "Group descriptor not loaded - "
  57. "block_group = %d, group_desc = %lu, desc = %lu",
  58. block_group, group_desc, offset);
  59. return NULL;
  60. }
  61. desc = (struct ext3_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  62. if (bh)
  63. *bh = sbi->s_group_desc[group_desc];
  64. return desc + offset;
  65. }
  66. /*
  67. * Read the bitmap for a given block_group, reading into the specified
  68. * slot in the superblock's bitmap cache.
  69. *
  70. * Return buffer_head on success or NULL in case of failure.
  71. */
  72. static struct buffer_head *
  73. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  74. {
  75. struct ext3_group_desc * desc;
  76. struct buffer_head * bh = NULL;
  77. desc = ext3_get_group_desc (sb, block_group, NULL);
  78. if (!desc)
  79. goto error_out;
  80. bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
  81. if (!bh)
  82. ext3_error (sb, "read_block_bitmap",
  83. "Cannot read block bitmap - "
  84. "block_group = %d, block_bitmap = %u",
  85. block_group, le32_to_cpu(desc->bg_block_bitmap));
  86. error_out:
  87. return bh;
  88. }
  89. /*
  90. * The reservation window structure operations
  91. * --------------------------------------------
  92. * Operations include:
  93. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  94. *
  95. * We use sorted double linked list for the per-filesystem reservation
  96. * window list. (like in vm_region).
  97. *
  98. * Initially, we keep those small operations in the abstract functions,
  99. * so later if we need a better searching tree than double linked-list,
  100. * we could easily switch to that without changing too much
  101. * code.
  102. */
  103. #if 0
  104. static void __rsv_window_dump(struct rb_root *root, int verbose,
  105. const char *fn)
  106. {
  107. struct rb_node *n;
  108. struct ext3_reserve_window_node *rsv, *prev;
  109. int bad;
  110. restart:
  111. n = rb_first(root);
  112. bad = 0;
  113. prev = NULL;
  114. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  115. while (n) {
  116. rsv = list_entry(n, struct ext3_reserve_window_node, rsv_node);
  117. if (verbose)
  118. printk("reservation window 0x%p "
  119. "start: %d, end: %d\n",
  120. rsv, rsv->rsv_start, rsv->rsv_end);
  121. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  122. printk("Bad reservation %p (start >= end)\n",
  123. rsv);
  124. bad = 1;
  125. }
  126. if (prev && prev->rsv_end >= rsv->rsv_start) {
  127. printk("Bad reservation %p (prev->end >= start)\n",
  128. rsv);
  129. bad = 1;
  130. }
  131. if (bad) {
  132. if (!verbose) {
  133. printk("Restarting reservation walk in verbose mode\n");
  134. verbose = 1;
  135. goto restart;
  136. }
  137. }
  138. n = rb_next(n);
  139. prev = rsv;
  140. }
  141. printk("Window map complete.\n");
  142. if (bad)
  143. BUG();
  144. }
  145. #define rsv_window_dump(root, verbose) \
  146. __rsv_window_dump((root), (verbose), __FUNCTION__)
  147. #else
  148. #define rsv_window_dump(root, verbose) do {} while (0)
  149. #endif
  150. static int
  151. goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal,
  152. unsigned int group, struct super_block * sb)
  153. {
  154. ext3_fsblk_t group_first_block, group_last_block;
  155. group_first_block = ext3_group_first_block_no(sb, group);
  156. group_last_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
  157. if ((rsv->_rsv_start > group_last_block) ||
  158. (rsv->_rsv_end < group_first_block))
  159. return 0;
  160. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  161. || (grp_goal + group_first_block > rsv->_rsv_end)))
  162. return 0;
  163. return 1;
  164. }
  165. /*
  166. * Find the reserved window which includes the goal, or the previous one
  167. * if the goal is not in any window.
  168. * Returns NULL if there are no windows or if all windows start after the goal.
  169. */
  170. static struct ext3_reserve_window_node *
  171. search_reserve_window(struct rb_root *root, ext3_fsblk_t goal)
  172. {
  173. struct rb_node *n = root->rb_node;
  174. struct ext3_reserve_window_node *rsv;
  175. if (!n)
  176. return NULL;
  177. do {
  178. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  179. if (goal < rsv->rsv_start)
  180. n = n->rb_left;
  181. else if (goal > rsv->rsv_end)
  182. n = n->rb_right;
  183. else
  184. return rsv;
  185. } while (n);
  186. /*
  187. * We've fallen off the end of the tree: the goal wasn't inside
  188. * any particular node. OK, the previous node must be to one
  189. * side of the interval containing the goal. If it's the RHS,
  190. * we need to back up one.
  191. */
  192. if (rsv->rsv_start > goal) {
  193. n = rb_prev(&rsv->rsv_node);
  194. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  195. }
  196. return rsv;
  197. }
  198. void ext3_rsv_window_add(struct super_block *sb,
  199. struct ext3_reserve_window_node *rsv)
  200. {
  201. struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root;
  202. struct rb_node *node = &rsv->rsv_node;
  203. ext3_fsblk_t start = rsv->rsv_start;
  204. struct rb_node ** p = &root->rb_node;
  205. struct rb_node * parent = NULL;
  206. struct ext3_reserve_window_node *this;
  207. while (*p)
  208. {
  209. parent = *p;
  210. this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node);
  211. if (start < this->rsv_start)
  212. p = &(*p)->rb_left;
  213. else if (start > this->rsv_end)
  214. p = &(*p)->rb_right;
  215. else
  216. BUG();
  217. }
  218. rb_link_node(node, parent, p);
  219. rb_insert_color(node, root);
  220. }
  221. static void rsv_window_remove(struct super_block *sb,
  222. struct ext3_reserve_window_node *rsv)
  223. {
  224. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  225. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  226. rsv->rsv_alloc_hit = 0;
  227. rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root);
  228. }
  229. static inline int rsv_is_empty(struct ext3_reserve_window *rsv)
  230. {
  231. /* a valid reservation end block could not be 0 */
  232. return (rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED);
  233. }
  234. void ext3_init_block_alloc_info(struct inode *inode)
  235. {
  236. struct ext3_inode_info *ei = EXT3_I(inode);
  237. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  238. struct super_block *sb = inode->i_sb;
  239. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  240. if (block_i) {
  241. struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node;
  242. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  243. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  244. /*
  245. * if filesystem is mounted with NORESERVATION, the goal
  246. * reservation window size is set to zero to indicate
  247. * block reservation is off
  248. */
  249. if (!test_opt(sb, RESERVATION))
  250. rsv->rsv_goal_size = 0;
  251. else
  252. rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS;
  253. rsv->rsv_alloc_hit = 0;
  254. block_i->last_alloc_logical_block = 0;
  255. block_i->last_alloc_physical_block = 0;
  256. }
  257. ei->i_block_alloc_info = block_i;
  258. }
  259. void ext3_discard_reservation(struct inode *inode)
  260. {
  261. struct ext3_inode_info *ei = EXT3_I(inode);
  262. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  263. struct ext3_reserve_window_node *rsv;
  264. spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock;
  265. if (!block_i)
  266. return;
  267. rsv = &block_i->rsv_window_node;
  268. if (!rsv_is_empty(&rsv->rsv_window)) {
  269. spin_lock(rsv_lock);
  270. if (!rsv_is_empty(&rsv->rsv_window))
  271. rsv_window_remove(inode->i_sb, rsv);
  272. spin_unlock(rsv_lock);
  273. }
  274. }
  275. /* Free given blocks, update quota and i_blocks field */
  276. void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb,
  277. ext3_fsblk_t block, unsigned long count,
  278. unsigned long *pdquot_freed_blocks)
  279. {
  280. struct buffer_head *bitmap_bh = NULL;
  281. struct buffer_head *gd_bh;
  282. unsigned long block_group;
  283. ext3_grpblk_t bit;
  284. unsigned long i;
  285. unsigned long overflow;
  286. struct ext3_group_desc * desc;
  287. struct ext3_super_block * es;
  288. struct ext3_sb_info *sbi;
  289. int err = 0, ret;
  290. ext3_grpblk_t group_freed;
  291. *pdquot_freed_blocks = 0;
  292. sbi = EXT3_SB(sb);
  293. es = sbi->s_es;
  294. if (block < le32_to_cpu(es->s_first_data_block) ||
  295. block + count < block ||
  296. block + count > le32_to_cpu(es->s_blocks_count)) {
  297. ext3_error (sb, "ext3_free_blocks",
  298. "Freeing blocks not in datazone - "
  299. "block = "E3FSBLK", count = %lu", block, count);
  300. goto error_return;
  301. }
  302. ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  303. do_more:
  304. overflow = 0;
  305. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  306. EXT3_BLOCKS_PER_GROUP(sb);
  307. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  308. EXT3_BLOCKS_PER_GROUP(sb);
  309. /*
  310. * Check to see if we are freeing blocks across a group
  311. * boundary.
  312. */
  313. if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
  314. overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
  315. count -= overflow;
  316. }
  317. brelse(bitmap_bh);
  318. bitmap_bh = read_block_bitmap(sb, block_group);
  319. if (!bitmap_bh)
  320. goto error_return;
  321. desc = ext3_get_group_desc (sb, block_group, &gd_bh);
  322. if (!desc)
  323. goto error_return;
  324. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  325. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  326. in_range (block, le32_to_cpu(desc->bg_inode_table),
  327. sbi->s_itb_per_group) ||
  328. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  329. sbi->s_itb_per_group))
  330. ext3_error (sb, "ext3_free_blocks",
  331. "Freeing blocks in system zones - "
  332. "Block = "E3FSBLK", count = %lu",
  333. block, count);
  334. /*
  335. * We are about to start releasing blocks in the bitmap,
  336. * so we need undo access.
  337. */
  338. /* @@@ check errors */
  339. BUFFER_TRACE(bitmap_bh, "getting undo access");
  340. err = ext3_journal_get_undo_access(handle, bitmap_bh);
  341. if (err)
  342. goto error_return;
  343. /*
  344. * We are about to modify some metadata. Call the journal APIs
  345. * to unshare ->b_data if a currently-committing transaction is
  346. * using it
  347. */
  348. BUFFER_TRACE(gd_bh, "get_write_access");
  349. err = ext3_journal_get_write_access(handle, gd_bh);
  350. if (err)
  351. goto error_return;
  352. jbd_lock_bh_state(bitmap_bh);
  353. for (i = 0, group_freed = 0; i < count; i++) {
  354. /*
  355. * An HJ special. This is expensive...
  356. */
  357. #ifdef CONFIG_JBD_DEBUG
  358. jbd_unlock_bh_state(bitmap_bh);
  359. {
  360. struct buffer_head *debug_bh;
  361. debug_bh = sb_find_get_block(sb, block + i);
  362. if (debug_bh) {
  363. BUFFER_TRACE(debug_bh, "Deleted!");
  364. if (!bh2jh(bitmap_bh)->b_committed_data)
  365. BUFFER_TRACE(debug_bh,
  366. "No commited data in bitmap");
  367. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  368. __brelse(debug_bh);
  369. }
  370. }
  371. jbd_lock_bh_state(bitmap_bh);
  372. #endif
  373. if (need_resched()) {
  374. jbd_unlock_bh_state(bitmap_bh);
  375. cond_resched();
  376. jbd_lock_bh_state(bitmap_bh);
  377. }
  378. /* @@@ This prevents newly-allocated data from being
  379. * freed and then reallocated within the same
  380. * transaction.
  381. *
  382. * Ideally we would want to allow that to happen, but to
  383. * do so requires making journal_forget() capable of
  384. * revoking the queued write of a data block, which
  385. * implies blocking on the journal lock. *forget()
  386. * cannot block due to truncate races.
  387. *
  388. * Eventually we can fix this by making journal_forget()
  389. * return a status indicating whether or not it was able
  390. * to revoke the buffer. On successful revoke, it is
  391. * safe not to set the allocation bit in the committed
  392. * bitmap, because we know that there is no outstanding
  393. * activity on the buffer any more and so it is safe to
  394. * reallocate it.
  395. */
  396. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  397. J_ASSERT_BH(bitmap_bh,
  398. bh2jh(bitmap_bh)->b_committed_data != NULL);
  399. ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  400. bh2jh(bitmap_bh)->b_committed_data);
  401. /*
  402. * We clear the bit in the bitmap after setting the committed
  403. * data bit, because this is the reverse order to that which
  404. * the allocator uses.
  405. */
  406. BUFFER_TRACE(bitmap_bh, "clear bit");
  407. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  408. bit + i, bitmap_bh->b_data)) {
  409. jbd_unlock_bh_state(bitmap_bh);
  410. ext3_error(sb, __FUNCTION__,
  411. "bit already cleared for block "E3FSBLK,
  412. block + i);
  413. jbd_lock_bh_state(bitmap_bh);
  414. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  415. } else {
  416. group_freed++;
  417. }
  418. }
  419. jbd_unlock_bh_state(bitmap_bh);
  420. spin_lock(sb_bgl_lock(sbi, block_group));
  421. desc->bg_free_blocks_count =
  422. cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) +
  423. group_freed);
  424. spin_unlock(sb_bgl_lock(sbi, block_group));
  425. percpu_counter_mod(&sbi->s_freeblocks_counter, count);
  426. /* We dirtied the bitmap block */
  427. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  428. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  429. /* And the group descriptor block */
  430. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  431. ret = ext3_journal_dirty_metadata(handle, gd_bh);
  432. if (!err) err = ret;
  433. *pdquot_freed_blocks += group_freed;
  434. if (overflow && !err) {
  435. block += count;
  436. count = overflow;
  437. goto do_more;
  438. }
  439. sb->s_dirt = 1;
  440. error_return:
  441. brelse(bitmap_bh);
  442. ext3_std_error(sb, err);
  443. return;
  444. }
  445. /* Free given blocks, update quota and i_blocks field */
  446. void ext3_free_blocks(handle_t *handle, struct inode *inode,
  447. ext3_fsblk_t block, unsigned long count)
  448. {
  449. struct super_block * sb;
  450. unsigned long dquot_freed_blocks;
  451. sb = inode->i_sb;
  452. if (!sb) {
  453. printk ("ext3_free_blocks: nonexistent device");
  454. return;
  455. }
  456. ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
  457. if (dquot_freed_blocks)
  458. DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
  459. return;
  460. }
  461. /*
  462. * For ext3 allocations, we must not reuse any blocks which are
  463. * allocated in the bitmap buffer's "last committed data" copy. This
  464. * prevents deletes from freeing up the page for reuse until we have
  465. * committed the delete transaction.
  466. *
  467. * If we didn't do this, then deleting something and reallocating it as
  468. * data would allow the old block to be overwritten before the
  469. * transaction committed (because we force data to disk before commit).
  470. * This would lead to corruption if we crashed between overwriting the
  471. * data and committing the delete.
  472. *
  473. * @@@ We may want to make this allocation behaviour conditional on
  474. * data-writes at some point, and disable it for metadata allocations or
  475. * sync-data inodes.
  476. */
  477. static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh)
  478. {
  479. int ret;
  480. struct journal_head *jh = bh2jh(bh);
  481. if (ext3_test_bit(nr, bh->b_data))
  482. return 0;
  483. jbd_lock_bh_state(bh);
  484. if (!jh->b_committed_data)
  485. ret = 1;
  486. else
  487. ret = !ext3_test_bit(nr, jh->b_committed_data);
  488. jbd_unlock_bh_state(bh);
  489. return ret;
  490. }
  491. static ext3_grpblk_t
  492. bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  493. ext3_grpblk_t maxblocks)
  494. {
  495. ext3_grpblk_t next;
  496. struct journal_head *jh = bh2jh(bh);
  497. /*
  498. * The bitmap search --- search forward alternately through the actual
  499. * bitmap and the last-committed copy until we find a bit free in
  500. * both
  501. */
  502. while (start < maxblocks) {
  503. next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start);
  504. if (next >= maxblocks)
  505. return -1;
  506. if (ext3_test_allocatable(next, bh))
  507. return next;
  508. jbd_lock_bh_state(bh);
  509. if (jh->b_committed_data)
  510. start = ext3_find_next_zero_bit(jh->b_committed_data,
  511. maxblocks, next);
  512. jbd_unlock_bh_state(bh);
  513. }
  514. return -1;
  515. }
  516. /*
  517. * Find an allocatable block in a bitmap. We honour both the bitmap and
  518. * its last-committed copy (if that exists), and perform the "most
  519. * appropriate allocation" algorithm of looking for a free block near
  520. * the initial goal; then for a free byte somewhere in the bitmap; then
  521. * for any free bit in the bitmap.
  522. */
  523. static ext3_grpblk_t
  524. find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  525. ext3_grpblk_t maxblocks)
  526. {
  527. ext3_grpblk_t here, next;
  528. char *p, *r;
  529. if (start > 0) {
  530. /*
  531. * The goal was occupied; search forward for a free
  532. * block within the next XX blocks.
  533. *
  534. * end_goal is more or less random, but it has to be
  535. * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
  536. * next 64-bit boundary is simple..
  537. */
  538. ext3_grpblk_t end_goal = (start + 63) & ~63;
  539. if (end_goal > maxblocks)
  540. end_goal = maxblocks;
  541. here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
  542. if (here < end_goal && ext3_test_allocatable(here, bh))
  543. return here;
  544. ext3_debug("Bit not found near goal\n");
  545. }
  546. here = start;
  547. if (here < 0)
  548. here = 0;
  549. p = ((char *)bh->b_data) + (here >> 3);
  550. r = memscan(p, 0, (maxblocks - here + 7) >> 3);
  551. next = (r - ((char *)bh->b_data)) << 3;
  552. if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
  553. return next;
  554. /*
  555. * The bitmap search --- search forward alternately through the actual
  556. * bitmap and the last-committed copy until we find a bit free in
  557. * both
  558. */
  559. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  560. return here;
  561. }
  562. /*
  563. * We think we can allocate this block in this bitmap. Try to set the bit.
  564. * If that succeeds then check that nobody has allocated and then freed the
  565. * block since we saw that is was not marked in b_committed_data. If it _was_
  566. * allocated and freed then clear the bit in the bitmap again and return
  567. * zero (failure).
  568. */
  569. static inline int
  570. claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh)
  571. {
  572. struct journal_head *jh = bh2jh(bh);
  573. int ret;
  574. if (ext3_set_bit_atomic(lock, block, bh->b_data))
  575. return 0;
  576. jbd_lock_bh_state(bh);
  577. if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
  578. ext3_clear_bit_atomic(lock, block, bh->b_data);
  579. ret = 0;
  580. } else {
  581. ret = 1;
  582. }
  583. jbd_unlock_bh_state(bh);
  584. return ret;
  585. }
  586. /*
  587. * If we failed to allocate the desired block then we may end up crossing to a
  588. * new bitmap. In that case we must release write access to the old one via
  589. * ext3_journal_release_buffer(), else we'll run out of credits.
  590. */
  591. static ext3_grpblk_t
  592. ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
  593. struct buffer_head *bitmap_bh, ext3_grpblk_t grp_goal,
  594. unsigned long *count, struct ext3_reserve_window *my_rsv)
  595. {
  596. ext3_fsblk_t group_first_block;
  597. ext3_grpblk_t start, end;
  598. unsigned long num = 0;
  599. /* we do allocation within the reservation window if we have a window */
  600. if (my_rsv) {
  601. group_first_block = ext3_group_first_block_no(sb, group);
  602. if (my_rsv->_rsv_start >= group_first_block)
  603. start = my_rsv->_rsv_start - group_first_block;
  604. else
  605. /* reservation window cross group boundary */
  606. start = 0;
  607. end = my_rsv->_rsv_end - group_first_block + 1;
  608. if (end > EXT3_BLOCKS_PER_GROUP(sb))
  609. /* reservation window crosses group boundary */
  610. end = EXT3_BLOCKS_PER_GROUP(sb);
  611. if ((start <= grp_goal) && (grp_goal < end))
  612. start = grp_goal;
  613. else
  614. grp_goal = -1;
  615. } else {
  616. if (grp_goal > 0)
  617. start = grp_goal;
  618. else
  619. start = 0;
  620. end = EXT3_BLOCKS_PER_GROUP(sb);
  621. }
  622. BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb));
  623. repeat:
  624. if (grp_goal < 0 || !ext3_test_allocatable(grp_goal, bitmap_bh)) {
  625. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  626. if (grp_goal < 0)
  627. goto fail_access;
  628. if (!my_rsv) {
  629. int i;
  630. for (i = 0; i < 7 && grp_goal > start &&
  631. ext3_test_allocatable(grp_goal - 1,
  632. bitmap_bh);
  633. i++, grp_goal--)
  634. ;
  635. }
  636. }
  637. start = grp_goal;
  638. if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), grp_goal, bitmap_bh)) {
  639. /*
  640. * The block was allocated by another thread, or it was
  641. * allocated and then freed by another thread
  642. */
  643. start++;
  644. grp_goal++;
  645. if (start >= end)
  646. goto fail_access;
  647. goto repeat;
  648. }
  649. num++;
  650. grp_goal++;
  651. while (num < *count && grp_goal < end
  652. && ext3_test_allocatable(grp_goal, bitmap_bh)
  653. && claim_block(sb_bgl_lock(EXT3_SB(sb), group), grp_goal, bitmap_bh)) {
  654. num++;
  655. grp_goal++;
  656. }
  657. *count = num;
  658. return grp_goal - num;
  659. fail_access:
  660. *count = num;
  661. return -1;
  662. }
  663. /**
  664. * find_next_reservable_window():
  665. * find a reservable space within the given range.
  666. * It does not allocate the reservation window for now:
  667. * alloc_new_reservation() will do the work later.
  668. *
  669. * @search_head: the head of the searching list;
  670. * This is not necessarily the list head of the whole filesystem
  671. *
  672. * We have both head and start_block to assist the search
  673. * for the reservable space. The list starts from head,
  674. * but we will shift to the place where start_block is,
  675. * then start from there, when looking for a reservable space.
  676. *
  677. * @size: the target new reservation window size
  678. *
  679. * @group_first_block: the first block we consider to start
  680. * the real search from
  681. *
  682. * @last_block:
  683. * the maximum block number that our goal reservable space
  684. * could start from. This is normally the last block in this
  685. * group. The search will end when we found the start of next
  686. * possible reservable space is out of this boundary.
  687. * This could handle the cross boundary reservation window
  688. * request.
  689. *
  690. * basically we search from the given range, rather than the whole
  691. * reservation double linked list, (start_block, last_block)
  692. * to find a free region that is of my size and has not
  693. * been reserved.
  694. *
  695. */
  696. static int find_next_reservable_window(
  697. struct ext3_reserve_window_node *search_head,
  698. struct ext3_reserve_window_node *my_rsv,
  699. struct super_block * sb,
  700. ext3_fsblk_t start_block,
  701. ext3_fsblk_t last_block)
  702. {
  703. struct rb_node *next;
  704. struct ext3_reserve_window_node *rsv, *prev;
  705. ext3_fsblk_t cur;
  706. int size = my_rsv->rsv_goal_size;
  707. /* TODO: make the start of the reservation window byte-aligned */
  708. /* cur = *start_block & ~7;*/
  709. cur = start_block;
  710. rsv = search_head;
  711. if (!rsv)
  712. return -1;
  713. while (1) {
  714. if (cur <= rsv->rsv_end)
  715. cur = rsv->rsv_end + 1;
  716. /* TODO?
  717. * in the case we could not find a reservable space
  718. * that is what is expected, during the re-search, we could
  719. * remember what's the largest reservable space we could have
  720. * and return that one.
  721. *
  722. * For now it will fail if we could not find the reservable
  723. * space with expected-size (or more)...
  724. */
  725. if (cur > last_block)
  726. return -1; /* fail */
  727. prev = rsv;
  728. next = rb_next(&rsv->rsv_node);
  729. rsv = list_entry(next,struct ext3_reserve_window_node,rsv_node);
  730. /*
  731. * Reached the last reservation, we can just append to the
  732. * previous one.
  733. */
  734. if (!next)
  735. break;
  736. if (cur + size <= rsv->rsv_start) {
  737. /*
  738. * Found a reserveable space big enough. We could
  739. * have a reservation across the group boundary here
  740. */
  741. break;
  742. }
  743. }
  744. /*
  745. * we come here either :
  746. * when we reach the end of the whole list,
  747. * and there is empty reservable space after last entry in the list.
  748. * append it to the end of the list.
  749. *
  750. * or we found one reservable space in the middle of the list,
  751. * return the reservation window that we could append to.
  752. * succeed.
  753. */
  754. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  755. rsv_window_remove(sb, my_rsv);
  756. /*
  757. * Let's book the whole avaliable window for now. We will check the
  758. * disk bitmap later and then, if there are free blocks then we adjust
  759. * the window size if it's larger than requested.
  760. * Otherwise, we will remove this node from the tree next time
  761. * call find_next_reservable_window.
  762. */
  763. my_rsv->rsv_start = cur;
  764. my_rsv->rsv_end = cur + size - 1;
  765. my_rsv->rsv_alloc_hit = 0;
  766. if (prev != my_rsv)
  767. ext3_rsv_window_add(sb, my_rsv);
  768. return 0;
  769. }
  770. /**
  771. * alloc_new_reservation()--allocate a new reservation window
  772. *
  773. * To make a new reservation, we search part of the filesystem
  774. * reservation list (the list that inside the group). We try to
  775. * allocate a new reservation window near the allocation goal,
  776. * or the beginning of the group, if there is no goal.
  777. *
  778. * We first find a reservable space after the goal, then from
  779. * there, we check the bitmap for the first free block after
  780. * it. If there is no free block until the end of group, then the
  781. * whole group is full, we failed. Otherwise, check if the free
  782. * block is inside the expected reservable space, if so, we
  783. * succeed.
  784. * If the first free block is outside the reservable space, then
  785. * start from the first free block, we search for next available
  786. * space, and go on.
  787. *
  788. * on succeed, a new reservation will be found and inserted into the list
  789. * It contains at least one free block, and it does not overlap with other
  790. * reservation windows.
  791. *
  792. * failed: we failed to find a reservation window in this group
  793. *
  794. * @rsv: the reservation
  795. *
  796. * @grp_goal: The goal (group-relative). It is where the search for a
  797. * free reservable space should start from.
  798. * if we have a grp_goal(grp_goal >0 ), then start from there,
  799. * no grp_goal(grp_goal = -1), we start from the first block
  800. * of the group.
  801. *
  802. * @sb: the super block
  803. * @group: the group we are trying to allocate in
  804. * @bitmap_bh: the block group block bitmap
  805. *
  806. */
  807. static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
  808. ext3_grpblk_t grp_goal, struct super_block *sb,
  809. unsigned int group, struct buffer_head *bitmap_bh)
  810. {
  811. struct ext3_reserve_window_node *search_head;
  812. ext3_fsblk_t group_first_block, group_end_block, start_block;
  813. ext3_grpblk_t first_free_block;
  814. struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root;
  815. unsigned long size;
  816. int ret;
  817. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  818. group_first_block = ext3_group_first_block_no(sb, group);
  819. group_end_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
  820. if (grp_goal < 0)
  821. start_block = group_first_block;
  822. else
  823. start_block = grp_goal + group_first_block;
  824. size = my_rsv->rsv_goal_size;
  825. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  826. /*
  827. * if the old reservation is cross group boundary
  828. * and if the goal is inside the old reservation window,
  829. * we will come here when we just failed to allocate from
  830. * the first part of the window. We still have another part
  831. * that belongs to the next group. In this case, there is no
  832. * point to discard our window and try to allocate a new one
  833. * in this group(which will fail). we should
  834. * keep the reservation window, just simply move on.
  835. *
  836. * Maybe we could shift the start block of the reservation
  837. * window to the first block of next group.
  838. */
  839. if ((my_rsv->rsv_start <= group_end_block) &&
  840. (my_rsv->rsv_end > group_end_block) &&
  841. (start_block >= my_rsv->rsv_start))
  842. return -1;
  843. if ((my_rsv->rsv_alloc_hit >
  844. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  845. /*
  846. * if we previously allocation hit ration is greater than half
  847. * we double the size of reservation window next time
  848. * otherwise keep the same
  849. */
  850. size = size * 2;
  851. if (size > EXT3_MAX_RESERVE_BLOCKS)
  852. size = EXT3_MAX_RESERVE_BLOCKS;
  853. my_rsv->rsv_goal_size= size;
  854. }
  855. }
  856. spin_lock(rsv_lock);
  857. /*
  858. * shift the search start to the window near the goal block
  859. */
  860. search_head = search_reserve_window(fs_rsv_root, start_block);
  861. /*
  862. * find_next_reservable_window() simply finds a reservable window
  863. * inside the given range(start_block, group_end_block).
  864. *
  865. * To make sure the reservation window has a free bit inside it, we
  866. * need to check the bitmap after we found a reservable window.
  867. */
  868. retry:
  869. ret = find_next_reservable_window(search_head, my_rsv, sb,
  870. start_block, group_end_block);
  871. if (ret == -1) {
  872. if (!rsv_is_empty(&my_rsv->rsv_window))
  873. rsv_window_remove(sb, my_rsv);
  874. spin_unlock(rsv_lock);
  875. return -1;
  876. }
  877. /*
  878. * On success, find_next_reservable_window() returns the
  879. * reservation window where there is a reservable space after it.
  880. * Before we reserve this reservable space, we need
  881. * to make sure there is at least a free block inside this region.
  882. *
  883. * searching the first free bit on the block bitmap and copy of
  884. * last committed bitmap alternatively, until we found a allocatable
  885. * block. Search start from the start block of the reservable space
  886. * we just found.
  887. */
  888. spin_unlock(rsv_lock);
  889. first_free_block = bitmap_search_next_usable_block(
  890. my_rsv->rsv_start - group_first_block,
  891. bitmap_bh, group_end_block - group_first_block + 1);
  892. if (first_free_block < 0) {
  893. /*
  894. * no free block left on the bitmap, no point
  895. * to reserve the space. return failed.
  896. */
  897. spin_lock(rsv_lock);
  898. if (!rsv_is_empty(&my_rsv->rsv_window))
  899. rsv_window_remove(sb, my_rsv);
  900. spin_unlock(rsv_lock);
  901. return -1; /* failed */
  902. }
  903. start_block = first_free_block + group_first_block;
  904. /*
  905. * check if the first free block is within the
  906. * free space we just reserved
  907. */
  908. if (start_block >= my_rsv->rsv_start && start_block < my_rsv->rsv_end)
  909. return 0; /* success */
  910. /*
  911. * if the first free bit we found is out of the reservable space
  912. * continue search for next reservable space,
  913. * start from where the free block is,
  914. * we also shift the list head to where we stopped last time
  915. */
  916. search_head = my_rsv;
  917. spin_lock(rsv_lock);
  918. goto retry;
  919. }
  920. static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv,
  921. struct super_block *sb, int size)
  922. {
  923. struct ext3_reserve_window_node *next_rsv;
  924. struct rb_node *next;
  925. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  926. if (!spin_trylock(rsv_lock))
  927. return;
  928. next = rb_next(&my_rsv->rsv_node);
  929. if (!next)
  930. my_rsv->rsv_end += size;
  931. else {
  932. next_rsv = list_entry(next, struct ext3_reserve_window_node, rsv_node);
  933. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  934. my_rsv->rsv_end += size;
  935. else
  936. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  937. }
  938. spin_unlock(rsv_lock);
  939. }
  940. /*
  941. * This is the main function used to allocate a new block and its reservation
  942. * window.
  943. *
  944. * Each time when a new block allocation is need, first try to allocate from
  945. * its own reservation. If it does not have a reservation window, instead of
  946. * looking for a free bit on bitmap first, then look up the reservation list to
  947. * see if it is inside somebody else's reservation window, we try to allocate a
  948. * reservation window for it starting from the goal first. Then do the block
  949. * allocation within the reservation window.
  950. *
  951. * This will avoid keeping on searching the reservation list again and
  952. * again when somebody is looking for a free block (without
  953. * reservation), and there are lots of free blocks, but they are all
  954. * being reserved.
  955. *
  956. * We use a sorted double linked list for the per-filesystem reservation list.
  957. * The insert, remove and find a free space(non-reserved) operations for the
  958. * sorted double linked list should be fast.
  959. *
  960. */
  961. static ext3_grpblk_t
  962. ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  963. unsigned int group, struct buffer_head *bitmap_bh,
  964. ext3_grpblk_t grp_goal,
  965. struct ext3_reserve_window_node * my_rsv,
  966. unsigned long *count, int *errp)
  967. {
  968. ext3_fsblk_t group_first_block;
  969. ext3_grpblk_t ret = 0;
  970. int fatal;
  971. unsigned long num = *count;
  972. *errp = 0;
  973. /*
  974. * Make sure we use undo access for the bitmap, because it is critical
  975. * that we do the frozen_data COW on bitmap buffers in all cases even
  976. * if the buffer is in BJ_Forget state in the committing transaction.
  977. */
  978. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  979. fatal = ext3_journal_get_undo_access(handle, bitmap_bh);
  980. if (fatal) {
  981. *errp = fatal;
  982. return -1;
  983. }
  984. /*
  985. * we don't deal with reservation when
  986. * filesystem is mounted without reservation
  987. * or the file is not a regular file
  988. * or last attempt to allocate a block with reservation turned on failed
  989. */
  990. if (my_rsv == NULL ) {
  991. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  992. grp_goal, count, NULL);
  993. goto out;
  994. }
  995. /*
  996. * grp_goal is a group relative block number (if there is a goal)
  997. * 0 < grp_goal < EXT3_BLOCKS_PER_GROUP(sb)
  998. * first block is a filesystem wide block number
  999. * first block is the block number of the first block in this group
  1000. */
  1001. group_first_block = ext3_group_first_block_no(sb, group);
  1002. /*
  1003. * Basically we will allocate a new block from inode's reservation
  1004. * window.
  1005. *
  1006. * We need to allocate a new reservation window, if:
  1007. * a) inode does not have a reservation window; or
  1008. * b) last attempt to allocate a block from existing reservation
  1009. * failed; or
  1010. * c) we come here with a goal and with a reservation window
  1011. *
  1012. * We do not need to allocate a new reservation window if we come here
  1013. * at the beginning with a goal and the goal is inside the window, or
  1014. * we don't have a goal but already have a reservation window.
  1015. * then we could go to allocate from the reservation window directly.
  1016. */
  1017. while (1) {
  1018. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1019. !goal_in_my_reservation(&my_rsv->rsv_window, grp_goal, group, sb)) {
  1020. if (my_rsv->rsv_goal_size < *count)
  1021. my_rsv->rsv_goal_size = *count;
  1022. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1023. group, bitmap_bh);
  1024. if (ret < 0)
  1025. break; /* failed */
  1026. if (!goal_in_my_reservation(&my_rsv->rsv_window, grp_goal, group, sb))
  1027. grp_goal = -1;
  1028. } else if (grp_goal > 0 && (my_rsv->rsv_end-grp_goal+1) < *count)
  1029. try_to_extend_reservation(my_rsv, sb,
  1030. *count-my_rsv->rsv_end + grp_goal - 1);
  1031. if ((my_rsv->rsv_start >= group_first_block + EXT3_BLOCKS_PER_GROUP(sb))
  1032. || (my_rsv->rsv_end < group_first_block))
  1033. BUG();
  1034. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, grp_goal,
  1035. &num, &my_rsv->rsv_window);
  1036. if (ret >= 0) {
  1037. my_rsv->rsv_alloc_hit += num;
  1038. *count = num;
  1039. break; /* succeed */
  1040. }
  1041. num = *count;
  1042. }
  1043. out:
  1044. if (ret >= 0) {
  1045. BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
  1046. "bitmap block");
  1047. fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
  1048. if (fatal) {
  1049. *errp = fatal;
  1050. return -1;
  1051. }
  1052. return ret;
  1053. }
  1054. BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
  1055. ext3_journal_release_buffer(handle, bitmap_bh);
  1056. return ret;
  1057. }
  1058. static int ext3_has_free_blocks(struct ext3_sb_info *sbi)
  1059. {
  1060. ext3_fsblk_t free_blocks, root_blocks;
  1061. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1062. root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
  1063. if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
  1064. sbi->s_resuid != current->fsuid &&
  1065. (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
  1066. return 0;
  1067. }
  1068. return 1;
  1069. }
  1070. /*
  1071. * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
  1072. * it is profitable to retry the operation, this function will wait
  1073. * for the current or commiting transaction to complete, and then
  1074. * return TRUE.
  1075. */
  1076. int ext3_should_retry_alloc(struct super_block *sb, int *retries)
  1077. {
  1078. if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3)
  1079. return 0;
  1080. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  1081. return journal_force_commit_nested(EXT3_SB(sb)->s_journal);
  1082. }
  1083. /*
  1084. * ext3_new_block uses a goal block to assist allocation. If the goal is
  1085. * free, or there is a free block within 32 blocks of the goal, that block
  1086. * is allocated. Otherwise a forward search is made for a free block; within
  1087. * each block group the search first looks for an entire free byte in the block
  1088. * bitmap, and then for any free bit if that fails.
  1089. * This function also updates quota and i_blocks field.
  1090. */
  1091. ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode,
  1092. ext3_fsblk_t goal, unsigned long *count, int *errp)
  1093. {
  1094. struct buffer_head *bitmap_bh = NULL;
  1095. struct buffer_head *gdp_bh;
  1096. int group_no;
  1097. int goal_group;
  1098. ext3_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1099. ext3_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1100. ext3_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1101. int bgi; /* blockgroup iteration index */
  1102. int fatal = 0, err;
  1103. int performed_allocation = 0;
  1104. ext3_grpblk_t free_blocks; /* number of free blocks in a group */
  1105. struct super_block *sb;
  1106. struct ext3_group_desc *gdp;
  1107. struct ext3_super_block *es;
  1108. struct ext3_sb_info *sbi;
  1109. struct ext3_reserve_window_node *my_rsv = NULL;
  1110. struct ext3_block_alloc_info *block_i;
  1111. unsigned short windowsz = 0;
  1112. #ifdef EXT3FS_DEBUG
  1113. static int goal_hits, goal_attempts;
  1114. #endif
  1115. unsigned long ngroups;
  1116. unsigned long num = *count;
  1117. *errp = -ENOSPC;
  1118. sb = inode->i_sb;
  1119. if (!sb) {
  1120. printk("ext3_new_block: nonexistent device");
  1121. return 0;
  1122. }
  1123. /*
  1124. * Check quota for allocation of this block.
  1125. */
  1126. if (DQUOT_ALLOC_BLOCK(inode, num)) {
  1127. *errp = -EDQUOT;
  1128. return 0;
  1129. }
  1130. sbi = EXT3_SB(sb);
  1131. es = EXT3_SB(sb)->s_es;
  1132. ext3_debug("goal=%lu.\n", goal);
  1133. /*
  1134. * Allocate a block from reservation only when
  1135. * filesystem is mounted with reservation(default,-o reservation), and
  1136. * it's a regular file, and
  1137. * the desired window size is greater than 0 (One could use ioctl
  1138. * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off
  1139. * reservation on that particular file)
  1140. */
  1141. block_i = EXT3_I(inode)->i_block_alloc_info;
  1142. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1143. my_rsv = &block_i->rsv_window_node;
  1144. if (!ext3_has_free_blocks(sbi)) {
  1145. *errp = -ENOSPC;
  1146. goto out;
  1147. }
  1148. /*
  1149. * First, test whether the goal block is free.
  1150. */
  1151. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1152. goal >= le32_to_cpu(es->s_blocks_count))
  1153. goal = le32_to_cpu(es->s_first_data_block);
  1154. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1155. EXT3_BLOCKS_PER_GROUP(sb);
  1156. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1157. if (!gdp)
  1158. goto io_error;
  1159. goal_group = group_no;
  1160. retry:
  1161. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1162. /*
  1163. * if there is not enough free blocks to make a new resevation
  1164. * turn off reservation for this allocation
  1165. */
  1166. if (my_rsv && (free_blocks < windowsz)
  1167. && (rsv_is_empty(&my_rsv->rsv_window)))
  1168. my_rsv = NULL;
  1169. if (free_blocks > 0) {
  1170. grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1171. EXT3_BLOCKS_PER_GROUP(sb));
  1172. bitmap_bh = read_block_bitmap(sb, group_no);
  1173. if (!bitmap_bh)
  1174. goto io_error;
  1175. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1176. group_no, bitmap_bh, grp_target_blk,
  1177. my_rsv, &num, &fatal);
  1178. if (fatal)
  1179. goto out;
  1180. if (grp_alloc_blk >= 0)
  1181. goto allocated;
  1182. }
  1183. ngroups = EXT3_SB(sb)->s_groups_count;
  1184. smp_rmb();
  1185. /*
  1186. * Now search the rest of the groups. We assume that
  1187. * i and gdp correctly point to the last group visited.
  1188. */
  1189. for (bgi = 0; bgi < ngroups; bgi++) {
  1190. group_no++;
  1191. if (group_no >= ngroups)
  1192. group_no = 0;
  1193. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1194. if (!gdp) {
  1195. *errp = -EIO;
  1196. goto out;
  1197. }
  1198. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1199. /*
  1200. * skip this group if the number of
  1201. * free blocks is less than half of the reservation
  1202. * window size.
  1203. */
  1204. if (free_blocks <= (windowsz/2))
  1205. continue;
  1206. brelse(bitmap_bh);
  1207. bitmap_bh = read_block_bitmap(sb, group_no);
  1208. if (!bitmap_bh)
  1209. goto io_error;
  1210. /*
  1211. * try to allocate block(s) from this group, without a goal(-1).
  1212. */
  1213. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1214. group_no, bitmap_bh, -1, my_rsv,
  1215. &num, &fatal);
  1216. if (fatal)
  1217. goto out;
  1218. if (grp_alloc_blk >= 0)
  1219. goto allocated;
  1220. }
  1221. /*
  1222. * We may end up a bogus ealier ENOSPC error due to
  1223. * filesystem is "full" of reservations, but
  1224. * there maybe indeed free blocks avaliable on disk
  1225. * In this case, we just forget about the reservations
  1226. * just do block allocation as without reservations.
  1227. */
  1228. if (my_rsv) {
  1229. my_rsv = NULL;
  1230. group_no = goal_group;
  1231. goto retry;
  1232. }
  1233. /* No space left on the device */
  1234. *errp = -ENOSPC;
  1235. goto out;
  1236. allocated:
  1237. ext3_debug("using block group %d(%d)\n",
  1238. group_no, gdp->bg_free_blocks_count);
  1239. BUFFER_TRACE(gdp_bh, "get_write_access");
  1240. fatal = ext3_journal_get_write_access(handle, gdp_bh);
  1241. if (fatal)
  1242. goto out;
  1243. ret_block = grp_alloc_blk + ext3_group_first_block_no(sb, group_no);
  1244. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
  1245. in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
  1246. in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
  1247. EXT3_SB(sb)->s_itb_per_group) ||
  1248. in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1249. EXT3_SB(sb)->s_itb_per_group))
  1250. ext3_error(sb, "ext3_new_block",
  1251. "Allocating block in system zone - "
  1252. "blocks from "E3FSBLK", length %lu",
  1253. ret_block, num);
  1254. performed_allocation = 1;
  1255. #ifdef CONFIG_JBD_DEBUG
  1256. {
  1257. struct buffer_head *debug_bh;
  1258. /* Record bitmap buffer state in the newly allocated block */
  1259. debug_bh = sb_find_get_block(sb, ret_block);
  1260. if (debug_bh) {
  1261. BUFFER_TRACE(debug_bh, "state when allocated");
  1262. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1263. brelse(debug_bh);
  1264. }
  1265. }
  1266. jbd_lock_bh_state(bitmap_bh);
  1267. spin_lock(sb_bgl_lock(sbi, group_no));
  1268. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1269. int i;
  1270. for (i = 0; i < num; i++) {
  1271. if (ext3_test_bit(grp_alloc_blk+i,
  1272. bh2jh(bitmap_bh)->b_committed_data)) {
  1273. printk("%s: block was unexpectedly set in "
  1274. "b_committed_data\n", __FUNCTION__);
  1275. }
  1276. }
  1277. }
  1278. ext3_debug("found bit %d\n", grp_alloc_blk);
  1279. spin_unlock(sb_bgl_lock(sbi, group_no));
  1280. jbd_unlock_bh_state(bitmap_bh);
  1281. #endif
  1282. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1283. ext3_error(sb, "ext3_new_block",
  1284. "block("E3FSBLK") >= blocks count(%d) - "
  1285. "block_group = %d, es == %p ", ret_block,
  1286. le32_to_cpu(es->s_blocks_count), group_no, es);
  1287. goto out;
  1288. }
  1289. /*
  1290. * It is up to the caller to add the new buffer to a journal
  1291. * list of some description. We don't know in advance whether
  1292. * the caller wants to use it as metadata or data.
  1293. */
  1294. ext3_debug("allocating block %lu. Goal hits %d of %d.\n",
  1295. ret_block, goal_hits, goal_attempts);
  1296. spin_lock(sb_bgl_lock(sbi, group_no));
  1297. gdp->bg_free_blocks_count =
  1298. cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - num);
  1299. spin_unlock(sb_bgl_lock(sbi, group_no));
  1300. percpu_counter_mod(&sbi->s_freeblocks_counter, -num);
  1301. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1302. err = ext3_journal_dirty_metadata(handle, gdp_bh);
  1303. if (!fatal)
  1304. fatal = err;
  1305. sb->s_dirt = 1;
  1306. if (fatal)
  1307. goto out;
  1308. *errp = 0;
  1309. brelse(bitmap_bh);
  1310. DQUOT_FREE_BLOCK(inode, *count-num);
  1311. *count = num;
  1312. return ret_block;
  1313. io_error:
  1314. *errp = -EIO;
  1315. out:
  1316. if (fatal) {
  1317. *errp = fatal;
  1318. ext3_std_error(sb, fatal);
  1319. }
  1320. /*
  1321. * Undo the block allocation
  1322. */
  1323. if (!performed_allocation)
  1324. DQUOT_FREE_BLOCK(inode, *count);
  1325. brelse(bitmap_bh);
  1326. return 0;
  1327. }
  1328. ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
  1329. ext3_fsblk_t goal, int *errp)
  1330. {
  1331. unsigned long count = 1;
  1332. return ext3_new_blocks(handle, inode, goal, &count, errp);
  1333. }
  1334. ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb)
  1335. {
  1336. ext3_fsblk_t desc_count;
  1337. struct ext3_group_desc *gdp;
  1338. int i;
  1339. unsigned long ngroups = EXT3_SB(sb)->s_groups_count;
  1340. #ifdef EXT3FS_DEBUG
  1341. struct ext3_super_block *es;
  1342. ext3_fsblk_t bitmap_count;
  1343. unsigned long x;
  1344. struct buffer_head *bitmap_bh = NULL;
  1345. es = EXT3_SB(sb)->s_es;
  1346. desc_count = 0;
  1347. bitmap_count = 0;
  1348. gdp = NULL;
  1349. smp_rmb();
  1350. for (i = 0; i < ngroups; i++) {
  1351. gdp = ext3_get_group_desc(sb, i, NULL);
  1352. if (!gdp)
  1353. continue;
  1354. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1355. brelse(bitmap_bh);
  1356. bitmap_bh = read_block_bitmap(sb, i);
  1357. if (bitmap_bh == NULL)
  1358. continue;
  1359. x = ext3_count_free(bitmap_bh, sb->s_blocksize);
  1360. printk("group %d: stored = %d, counted = %lu\n",
  1361. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1362. bitmap_count += x;
  1363. }
  1364. brelse(bitmap_bh);
  1365. printk("ext3_count_free_blocks: stored = "E3FSBLK
  1366. ", computed = "E3FSBLK", "E3FSBLK"\n",
  1367. le32_to_cpu(es->s_free_blocks_count),
  1368. desc_count, bitmap_count);
  1369. return bitmap_count;
  1370. #else
  1371. desc_count = 0;
  1372. smp_rmb();
  1373. for (i = 0; i < ngroups; i++) {
  1374. gdp = ext3_get_group_desc(sb, i, NULL);
  1375. if (!gdp)
  1376. continue;
  1377. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1378. }
  1379. return desc_count;
  1380. #endif
  1381. }
  1382. static inline int
  1383. block_in_use(ext3_fsblk_t block, struct super_block *sb, unsigned char *map)
  1384. {
  1385. return ext3_test_bit ((block -
  1386. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
  1387. EXT3_BLOCKS_PER_GROUP(sb), map);
  1388. }
  1389. static inline int test_root(int a, int b)
  1390. {
  1391. int num = b;
  1392. while (a > num)
  1393. num *= b;
  1394. return num == a;
  1395. }
  1396. static int ext3_group_sparse(int group)
  1397. {
  1398. if (group <= 1)
  1399. return 1;
  1400. if (!(group & 1))
  1401. return 0;
  1402. return (test_root(group, 7) || test_root(group, 5) ||
  1403. test_root(group, 3));
  1404. }
  1405. /**
  1406. * ext3_bg_has_super - number of blocks used by the superblock in group
  1407. * @sb: superblock for filesystem
  1408. * @group: group number to check
  1409. *
  1410. * Return the number of blocks used by the superblock (primary or backup)
  1411. * in this group. Currently this will be only 0 or 1.
  1412. */
  1413. int ext3_bg_has_super(struct super_block *sb, int group)
  1414. {
  1415. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1416. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1417. !ext3_group_sparse(group))
  1418. return 0;
  1419. return 1;
  1420. }
  1421. static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group)
  1422. {
  1423. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1424. unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb);
  1425. unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1;
  1426. if (group == first || group == first + 1 || group == last)
  1427. return 1;
  1428. return 0;
  1429. }
  1430. static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group)
  1431. {
  1432. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1433. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1434. !ext3_group_sparse(group))
  1435. return 0;
  1436. return EXT3_SB(sb)->s_gdb_count;
  1437. }
  1438. /**
  1439. * ext3_bg_num_gdb - number of blocks used by the group table in group
  1440. * @sb: superblock for filesystem
  1441. * @group: group number to check
  1442. *
  1443. * Return the number of blocks used by the group descriptor table
  1444. * (primary or backup) in this group. In the future there may be a
  1445. * different number of descriptor blocks in each group.
  1446. */
  1447. unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
  1448. {
  1449. unsigned long first_meta_bg =
  1450. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg);
  1451. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1452. if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) ||
  1453. metagroup < first_meta_bg)
  1454. return ext3_bg_num_gdb_nometa(sb,group);
  1455. return ext3_bg_num_gdb_meta(sb,group);
  1456. }