balloc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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, int goal,
  152. unsigned int group, struct super_block * sb)
  153. {
  154. unsigned long group_first_block, group_last_block;
  155. group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
  156. group * EXT3_BLOCKS_PER_GROUP(sb);
  157. group_last_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
  158. if ((rsv->_rsv_start > group_last_block) ||
  159. (rsv->_rsv_end < group_first_block))
  160. return 0;
  161. if ((goal >= 0) && ((goal + group_first_block < rsv->_rsv_start)
  162. || (goal + group_first_block > rsv->_rsv_end)))
  163. return 0;
  164. return 1;
  165. }
  166. /*
  167. * Find the reserved window which includes the goal, or the previous one
  168. * if the goal is not in any window.
  169. * Returns NULL if there are no windows or if all windows start after the goal.
  170. */
  171. static struct ext3_reserve_window_node *
  172. search_reserve_window(struct rb_root *root, unsigned long goal)
  173. {
  174. struct rb_node *n = root->rb_node;
  175. struct ext3_reserve_window_node *rsv;
  176. if (!n)
  177. return NULL;
  178. do {
  179. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  180. if (goal < rsv->rsv_start)
  181. n = n->rb_left;
  182. else if (goal > rsv->rsv_end)
  183. n = n->rb_right;
  184. else
  185. return rsv;
  186. } while (n);
  187. /*
  188. * We've fallen off the end of the tree: the goal wasn't inside
  189. * any particular node. OK, the previous node must be to one
  190. * side of the interval containing the goal. If it's the RHS,
  191. * we need to back up one.
  192. */
  193. if (rsv->rsv_start > goal) {
  194. n = rb_prev(&rsv->rsv_node);
  195. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  196. }
  197. return rsv;
  198. }
  199. void ext3_rsv_window_add(struct super_block *sb,
  200. struct ext3_reserve_window_node *rsv)
  201. {
  202. struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root;
  203. struct rb_node *node = &rsv->rsv_node;
  204. unsigned int start = rsv->rsv_start;
  205. struct rb_node ** p = &root->rb_node;
  206. struct rb_node * parent = NULL;
  207. struct ext3_reserve_window_node *this;
  208. while (*p)
  209. {
  210. parent = *p;
  211. this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node);
  212. if (start < this->rsv_start)
  213. p = &(*p)->rb_left;
  214. else if (start > this->rsv_end)
  215. p = &(*p)->rb_right;
  216. else
  217. BUG();
  218. }
  219. rb_link_node(node, parent, p);
  220. rb_insert_color(node, root);
  221. }
  222. static void rsv_window_remove(struct super_block *sb,
  223. struct ext3_reserve_window_node *rsv)
  224. {
  225. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  226. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  227. rsv->rsv_alloc_hit = 0;
  228. rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root);
  229. }
  230. static inline int rsv_is_empty(struct ext3_reserve_window *rsv)
  231. {
  232. /* a valid reservation end block could not be 0 */
  233. return (rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED);
  234. }
  235. void ext3_init_block_alloc_info(struct inode *inode)
  236. {
  237. struct ext3_inode_info *ei = EXT3_I(inode);
  238. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  239. struct super_block *sb = inode->i_sb;
  240. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  241. if (block_i) {
  242. struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node;
  243. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  244. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  245. /*
  246. * if filesystem is mounted with NORESERVATION, the goal
  247. * reservation window size is set to zero to indicate
  248. * block reservation is off
  249. */
  250. if (!test_opt(sb, RESERVATION))
  251. rsv->rsv_goal_size = 0;
  252. else
  253. rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS;
  254. rsv->rsv_alloc_hit = 0;
  255. block_i->last_alloc_logical_block = 0;
  256. block_i->last_alloc_physical_block = 0;
  257. }
  258. ei->i_block_alloc_info = block_i;
  259. }
  260. void ext3_discard_reservation(struct inode *inode)
  261. {
  262. struct ext3_inode_info *ei = EXT3_I(inode);
  263. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  264. struct ext3_reserve_window_node *rsv;
  265. spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock;
  266. if (!block_i)
  267. return;
  268. rsv = &block_i->rsv_window_node;
  269. if (!rsv_is_empty(&rsv->rsv_window)) {
  270. spin_lock(rsv_lock);
  271. if (!rsv_is_empty(&rsv->rsv_window))
  272. rsv_window_remove(inode->i_sb, rsv);
  273. spin_unlock(rsv_lock);
  274. }
  275. }
  276. /* Free given blocks, update quota and i_blocks field */
  277. void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb,
  278. unsigned long block, unsigned long count,
  279. int *pdquot_freed_blocks)
  280. {
  281. struct buffer_head *bitmap_bh = NULL;
  282. struct buffer_head *gd_bh;
  283. unsigned long block_group;
  284. unsigned long bit;
  285. unsigned long i;
  286. unsigned long overflow;
  287. struct ext3_group_desc * desc;
  288. struct ext3_super_block * es;
  289. struct ext3_sb_info *sbi;
  290. int err = 0, ret;
  291. unsigned group_freed;
  292. *pdquot_freed_blocks = 0;
  293. sbi = EXT3_SB(sb);
  294. es = sbi->s_es;
  295. if (block < le32_to_cpu(es->s_first_data_block) ||
  296. block + count < block ||
  297. block + count > le32_to_cpu(es->s_blocks_count)) {
  298. ext3_error (sb, "ext3_free_blocks",
  299. "Freeing blocks not in datazone - "
  300. "block = %lu, count = %lu", block, count);
  301. goto error_return;
  302. }
  303. ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  304. do_more:
  305. overflow = 0;
  306. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  307. EXT3_BLOCKS_PER_GROUP(sb);
  308. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  309. EXT3_BLOCKS_PER_GROUP(sb);
  310. /*
  311. * Check to see if we are freeing blocks across a group
  312. * boundary.
  313. */
  314. if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
  315. overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
  316. count -= overflow;
  317. }
  318. brelse(bitmap_bh);
  319. bitmap_bh = read_block_bitmap(sb, block_group);
  320. if (!bitmap_bh)
  321. goto error_return;
  322. desc = ext3_get_group_desc (sb, block_group, &gd_bh);
  323. if (!desc)
  324. goto error_return;
  325. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  326. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  327. in_range (block, le32_to_cpu(desc->bg_inode_table),
  328. sbi->s_itb_per_group) ||
  329. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  330. sbi->s_itb_per_group))
  331. ext3_error (sb, "ext3_free_blocks",
  332. "Freeing blocks in system zones - "
  333. "Block = %lu, count = %lu",
  334. block, count);
  335. /*
  336. * We are about to start releasing blocks in the bitmap,
  337. * so we need undo access.
  338. */
  339. /* @@@ check errors */
  340. BUFFER_TRACE(bitmap_bh, "getting undo access");
  341. err = ext3_journal_get_undo_access(handle, bitmap_bh);
  342. if (err)
  343. goto error_return;
  344. /*
  345. * We are about to modify some metadata. Call the journal APIs
  346. * to unshare ->b_data if a currently-committing transaction is
  347. * using it
  348. */
  349. BUFFER_TRACE(gd_bh, "get_write_access");
  350. err = ext3_journal_get_write_access(handle, gd_bh);
  351. if (err)
  352. goto error_return;
  353. jbd_lock_bh_state(bitmap_bh);
  354. for (i = 0, group_freed = 0; i < count; i++) {
  355. /*
  356. * An HJ special. This is expensive...
  357. */
  358. #ifdef CONFIG_JBD_DEBUG
  359. jbd_unlock_bh_state(bitmap_bh);
  360. {
  361. struct buffer_head *debug_bh;
  362. debug_bh = sb_find_get_block(sb, block + i);
  363. if (debug_bh) {
  364. BUFFER_TRACE(debug_bh, "Deleted!");
  365. if (!bh2jh(bitmap_bh)->b_committed_data)
  366. BUFFER_TRACE(debug_bh,
  367. "No commited data in bitmap");
  368. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  369. __brelse(debug_bh);
  370. }
  371. }
  372. jbd_lock_bh_state(bitmap_bh);
  373. #endif
  374. if (need_resched()) {
  375. jbd_unlock_bh_state(bitmap_bh);
  376. cond_resched();
  377. jbd_lock_bh_state(bitmap_bh);
  378. }
  379. /* @@@ This prevents newly-allocated data from being
  380. * freed and then reallocated within the same
  381. * transaction.
  382. *
  383. * Ideally we would want to allow that to happen, but to
  384. * do so requires making journal_forget() capable of
  385. * revoking the queued write of a data block, which
  386. * implies blocking on the journal lock. *forget()
  387. * cannot block due to truncate races.
  388. *
  389. * Eventually we can fix this by making journal_forget()
  390. * return a status indicating whether or not it was able
  391. * to revoke the buffer. On successful revoke, it is
  392. * safe not to set the allocation bit in the committed
  393. * bitmap, because we know that there is no outstanding
  394. * activity on the buffer any more and so it is safe to
  395. * reallocate it.
  396. */
  397. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  398. J_ASSERT_BH(bitmap_bh,
  399. bh2jh(bitmap_bh)->b_committed_data != NULL);
  400. ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  401. bh2jh(bitmap_bh)->b_committed_data);
  402. /*
  403. * We clear the bit in the bitmap after setting the committed
  404. * data bit, because this is the reverse order to that which
  405. * the allocator uses.
  406. */
  407. BUFFER_TRACE(bitmap_bh, "clear bit");
  408. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  409. bit + i, bitmap_bh->b_data)) {
  410. jbd_unlock_bh_state(bitmap_bh);
  411. ext3_error(sb, __FUNCTION__,
  412. "bit already cleared for block %lu", 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. unsigned long block, unsigned long count)
  448. {
  449. struct super_block * sb;
  450. int 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(int 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 int
  492. bitmap_search_next_usable_block(int start, struct buffer_head *bh,
  493. int maxblocks)
  494. {
  495. int 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 int
  524. find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
  525. {
  526. int here, next;
  527. char *p, *r;
  528. if (start > 0) {
  529. /*
  530. * The goal was occupied; search forward for a free
  531. * block within the next XX blocks.
  532. *
  533. * end_goal is more or less random, but it has to be
  534. * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
  535. * next 64-bit boundary is simple..
  536. */
  537. int end_goal = (start + 63) & ~63;
  538. if (end_goal > maxblocks)
  539. end_goal = maxblocks;
  540. here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
  541. if (here < end_goal && ext3_test_allocatable(here, bh))
  542. return here;
  543. ext3_debug("Bit not found near goal\n");
  544. }
  545. here = start;
  546. if (here < 0)
  547. here = 0;
  548. p = ((char *)bh->b_data) + (here >> 3);
  549. r = memscan(p, 0, (maxblocks - here + 7) >> 3);
  550. next = (r - ((char *)bh->b_data)) << 3;
  551. if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
  552. return next;
  553. /*
  554. * The bitmap search --- search forward alternately through the actual
  555. * bitmap and the last-committed copy until we find a bit free in
  556. * both
  557. */
  558. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  559. return here;
  560. }
  561. /*
  562. * We think we can allocate this block in this bitmap. Try to set the bit.
  563. * If that succeeds then check that nobody has allocated and then freed the
  564. * block since we saw that is was not marked in b_committed_data. If it _was_
  565. * allocated and freed then clear the bit in the bitmap again and return
  566. * zero (failure).
  567. */
  568. static inline int
  569. claim_block(spinlock_t *lock, int block, struct buffer_head *bh)
  570. {
  571. struct journal_head *jh = bh2jh(bh);
  572. int ret;
  573. if (ext3_set_bit_atomic(lock, block, bh->b_data))
  574. return 0;
  575. jbd_lock_bh_state(bh);
  576. if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
  577. ext3_clear_bit_atomic(lock, block, bh->b_data);
  578. ret = 0;
  579. } else {
  580. ret = 1;
  581. }
  582. jbd_unlock_bh_state(bh);
  583. return ret;
  584. }
  585. /*
  586. * If we failed to allocate the desired block then we may end up crossing to a
  587. * new bitmap. In that case we must release write access to the old one via
  588. * ext3_journal_release_buffer(), else we'll run out of credits.
  589. */
  590. static int
  591. ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
  592. struct buffer_head *bitmap_bh, int goal,
  593. unsigned long *count, struct ext3_reserve_window *my_rsv)
  594. {
  595. int group_first_block, start, end;
  596. unsigned long num = 0;
  597. /* we do allocation within the reservation window if we have a window */
  598. if (my_rsv) {
  599. group_first_block =
  600. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
  601. group * EXT3_BLOCKS_PER_GROUP(sb);
  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 <= goal) && (goal < end))
  612. start = goal;
  613. else
  614. goal = -1;
  615. } else {
  616. if (goal > 0)
  617. start = 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 (goal < 0 || !ext3_test_allocatable(goal, bitmap_bh)) {
  625. goal = find_next_usable_block(start, bitmap_bh, end);
  626. if (goal < 0)
  627. goto fail_access;
  628. if (!my_rsv) {
  629. int i;
  630. for (i = 0; i < 7 && goal > start &&
  631. ext3_test_allocatable(goal - 1,
  632. bitmap_bh);
  633. i++, goal--)
  634. ;
  635. }
  636. }
  637. start = goal;
  638. if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), 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. goal++;
  645. if (start >= end)
  646. goto fail_access;
  647. goto repeat;
  648. }
  649. num++;
  650. goal++;
  651. while (num < *count && goal < end
  652. && ext3_test_allocatable(goal, bitmap_bh)
  653. && claim_block(sb_bgl_lock(EXT3_SB(sb), group), goal, bitmap_bh)) {
  654. num++;
  655. goal++;
  656. }
  657. *count = num;
  658. return 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, int start_block,
  700. int last_block)
  701. {
  702. struct rb_node *next;
  703. struct ext3_reserve_window_node *rsv, *prev;
  704. int cur;
  705. int size = my_rsv->rsv_goal_size;
  706. /* TODO: make the start of the reservation window byte-aligned */
  707. /* cur = *start_block & ~7;*/
  708. cur = start_block;
  709. rsv = search_head;
  710. if (!rsv)
  711. return -1;
  712. while (1) {
  713. if (cur <= rsv->rsv_end)
  714. cur = rsv->rsv_end + 1;
  715. /* TODO?
  716. * in the case we could not find a reservable space
  717. * that is what is expected, during the re-search, we could
  718. * remember what's the largest reservable space we could have
  719. * and return that one.
  720. *
  721. * For now it will fail if we could not find the reservable
  722. * space with expected-size (or more)...
  723. */
  724. if (cur > last_block)
  725. return -1; /* fail */
  726. prev = rsv;
  727. next = rb_next(&rsv->rsv_node);
  728. rsv = list_entry(next,struct ext3_reserve_window_node,rsv_node);
  729. /*
  730. * Reached the last reservation, we can just append to the
  731. * previous one.
  732. */
  733. if (!next)
  734. break;
  735. if (cur + size <= rsv->rsv_start) {
  736. /*
  737. * Found a reserveable space big enough. We could
  738. * have a reservation across the group boundary here
  739. */
  740. break;
  741. }
  742. }
  743. /*
  744. * we come here either :
  745. * when we reach the end of the whole list,
  746. * and there is empty reservable space after last entry in the list.
  747. * append it to the end of the list.
  748. *
  749. * or we found one reservable space in the middle of the list,
  750. * return the reservation window that we could append to.
  751. * succeed.
  752. */
  753. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  754. rsv_window_remove(sb, my_rsv);
  755. /*
  756. * Let's book the whole avaliable window for now. We will check the
  757. * disk bitmap later and then, if there are free blocks then we adjust
  758. * the window size if it's larger than requested.
  759. * Otherwise, we will remove this node from the tree next time
  760. * call find_next_reservable_window.
  761. */
  762. my_rsv->rsv_start = cur;
  763. my_rsv->rsv_end = cur + size - 1;
  764. my_rsv->rsv_alloc_hit = 0;
  765. if (prev != my_rsv)
  766. ext3_rsv_window_add(sb, my_rsv);
  767. return 0;
  768. }
  769. /**
  770. * alloc_new_reservation()--allocate a new reservation window
  771. *
  772. * To make a new reservation, we search part of the filesystem
  773. * reservation list (the list that inside the group). We try to
  774. * allocate a new reservation window near the allocation goal,
  775. * or the beginning of the group, if there is no goal.
  776. *
  777. * We first find a reservable space after the goal, then from
  778. * there, we check the bitmap for the first free block after
  779. * it. If there is no free block until the end of group, then the
  780. * whole group is full, we failed. Otherwise, check if the free
  781. * block is inside the expected reservable space, if so, we
  782. * succeed.
  783. * If the first free block is outside the reservable space, then
  784. * start from the first free block, we search for next available
  785. * space, and go on.
  786. *
  787. * on succeed, a new reservation will be found and inserted into the list
  788. * It contains at least one free block, and it does not overlap with other
  789. * reservation windows.
  790. *
  791. * failed: we failed to find a reservation window in this group
  792. *
  793. * @rsv: the reservation
  794. *
  795. * @goal: The goal (group-relative). It is where the search for a
  796. * free reservable space should start from.
  797. * if we have a goal(goal >0 ), then start from there,
  798. * no goal(goal = -1), we start from the first block
  799. * of the group.
  800. *
  801. * @sb: the super block
  802. * @group: the group we are trying to allocate in
  803. * @bitmap_bh: the block group block bitmap
  804. *
  805. */
  806. static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
  807. int goal, struct super_block *sb,
  808. unsigned int group, struct buffer_head *bitmap_bh)
  809. {
  810. struct ext3_reserve_window_node *search_head;
  811. int group_first_block, group_end_block, start_block;
  812. int first_free_block;
  813. struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root;
  814. unsigned long size;
  815. int ret;
  816. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  817. group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
  818. group * EXT3_BLOCKS_PER_GROUP(sb);
  819. group_end_block = group_first_block + EXT3_BLOCKS_PER_GROUP(sb) - 1;
  820. if (goal < 0)
  821. start_block = group_first_block;
  822. else
  823. start_block = 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 int
  962. ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  963. unsigned int group, struct buffer_head *bitmap_bh,
  964. int goal, struct ext3_reserve_window_node * my_rsv,
  965. unsigned long *count, int *errp)
  966. {
  967. unsigned long group_first_block;
  968. int ret = 0;
  969. int fatal;
  970. unsigned long num = *count;
  971. *errp = 0;
  972. /*
  973. * Make sure we use undo access for the bitmap, because it is critical
  974. * that we do the frozen_data COW on bitmap buffers in all cases even
  975. * if the buffer is in BJ_Forget state in the committing transaction.
  976. */
  977. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  978. fatal = ext3_journal_get_undo_access(handle, bitmap_bh);
  979. if (fatal) {
  980. *errp = fatal;
  981. return -1;
  982. }
  983. /*
  984. * we don't deal with reservation when
  985. * filesystem is mounted without reservation
  986. * or the file is not a regular file
  987. * or last attempt to allocate a block with reservation turned on failed
  988. */
  989. if (my_rsv == NULL ) {
  990. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  991. goal, count, NULL);
  992. goto out;
  993. }
  994. /*
  995. * goal is a group relative block number (if there is a goal)
  996. * 0 < goal < EXT3_BLOCKS_PER_GROUP(sb)
  997. * first block is a filesystem wide block number
  998. * first block is the block number of the first block in this group
  999. */
  1000. group_first_block = le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block) +
  1001. group * EXT3_BLOCKS_PER_GROUP(sb);
  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, 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, goal, sb,
  1023. group, bitmap_bh);
  1024. if (ret < 0)
  1025. break; /* failed */
  1026. if (!goal_in_my_reservation(&my_rsv->rsv_window, goal, group, sb))
  1027. goal = -1;
  1028. } else if (goal > 0 && (my_rsv->rsv_end-goal+1) < *count)
  1029. try_to_extend_reservation(my_rsv, sb,
  1030. *count-my_rsv->rsv_end + 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, 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. int 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. int ext3_new_blocks(handle_t *handle, struct inode *inode,
  1092. unsigned long 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. int ret_block;
  1099. int bgi; /* blockgroup iteration index */
  1100. int target_block;
  1101. int fatal = 0, err;
  1102. int performed_allocation = 0;
  1103. int free_blocks;
  1104. struct super_block *sb;
  1105. struct ext3_group_desc *gdp;
  1106. struct ext3_super_block *es;
  1107. struct ext3_sb_info *sbi;
  1108. struct ext3_reserve_window_node *my_rsv = NULL;
  1109. struct ext3_block_alloc_info *block_i;
  1110. unsigned short windowsz = 0;
  1111. #ifdef EXT3FS_DEBUG
  1112. static int goal_hits, goal_attempts;
  1113. #endif
  1114. unsigned long ngroups;
  1115. unsigned long num = *count;
  1116. *errp = -ENOSPC;
  1117. sb = inode->i_sb;
  1118. if (!sb) {
  1119. printk("ext3_new_block: nonexistent device");
  1120. return 0;
  1121. }
  1122. /*
  1123. * Check quota for allocation of this block.
  1124. */
  1125. if (DQUOT_ALLOC_BLOCK(inode, num)) {
  1126. *errp = -EDQUOT;
  1127. return 0;
  1128. }
  1129. sbi = EXT3_SB(sb);
  1130. es = EXT3_SB(sb)->s_es;
  1131. ext3_debug("goal=%lu.\n", goal);
  1132. /*
  1133. * Allocate a block from reservation only when
  1134. * filesystem is mounted with reservation(default,-o reservation), and
  1135. * it's a regular file, and
  1136. * the desired window size is greater than 0 (One could use ioctl
  1137. * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off
  1138. * reservation on that particular file)
  1139. */
  1140. block_i = EXT3_I(inode)->i_block_alloc_info;
  1141. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1142. my_rsv = &block_i->rsv_window_node;
  1143. if (!ext3_has_free_blocks(sbi)) {
  1144. *errp = -ENOSPC;
  1145. goto out;
  1146. }
  1147. /*
  1148. * First, test whether the goal block is free.
  1149. */
  1150. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1151. goal >= le32_to_cpu(es->s_blocks_count))
  1152. goal = le32_to_cpu(es->s_first_data_block);
  1153. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1154. EXT3_BLOCKS_PER_GROUP(sb);
  1155. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1156. if (!gdp)
  1157. goto io_error;
  1158. goal_group = group_no;
  1159. retry:
  1160. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1161. /*
  1162. * if there is not enough free blocks to make a new resevation
  1163. * turn off reservation for this allocation
  1164. */
  1165. if (my_rsv && (free_blocks < windowsz)
  1166. && (rsv_is_empty(&my_rsv->rsv_window)))
  1167. my_rsv = NULL;
  1168. if (free_blocks > 0) {
  1169. ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1170. EXT3_BLOCKS_PER_GROUP(sb));
  1171. bitmap_bh = read_block_bitmap(sb, group_no);
  1172. if (!bitmap_bh)
  1173. goto io_error;
  1174. ret_block = ext3_try_to_allocate_with_rsv(sb, handle, group_no,
  1175. bitmap_bh, ret_block, my_rsv, &num, &fatal);
  1176. if (fatal)
  1177. goto out;
  1178. if (ret_block >= 0)
  1179. goto allocated;
  1180. }
  1181. ngroups = EXT3_SB(sb)->s_groups_count;
  1182. smp_rmb();
  1183. /*
  1184. * Now search the rest of the groups. We assume that
  1185. * i and gdp correctly point to the last group visited.
  1186. */
  1187. for (bgi = 0; bgi < ngroups; bgi++) {
  1188. group_no++;
  1189. if (group_no >= ngroups)
  1190. group_no = 0;
  1191. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1192. if (!gdp) {
  1193. *errp = -EIO;
  1194. goto out;
  1195. }
  1196. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1197. /*
  1198. * skip this group if the number of
  1199. * free blocks is less than half of the reservation
  1200. * window size.
  1201. */
  1202. if (free_blocks <= (windowsz/2))
  1203. continue;
  1204. brelse(bitmap_bh);
  1205. bitmap_bh = read_block_bitmap(sb, group_no);
  1206. if (!bitmap_bh)
  1207. goto io_error;
  1208. ret_block = ext3_try_to_allocate_with_rsv(sb, handle, group_no,
  1209. bitmap_bh, -1, my_rsv, &num, &fatal);
  1210. if (fatal)
  1211. goto out;
  1212. if (ret_block >= 0)
  1213. goto allocated;
  1214. }
  1215. /*
  1216. * We may end up a bogus ealier ENOSPC error due to
  1217. * filesystem is "full" of reservations, but
  1218. * there maybe indeed free blocks avaliable on disk
  1219. * In this case, we just forget about the reservations
  1220. * just do block allocation as without reservations.
  1221. */
  1222. if (my_rsv) {
  1223. my_rsv = NULL;
  1224. group_no = goal_group;
  1225. goto retry;
  1226. }
  1227. /* No space left on the device */
  1228. *errp = -ENOSPC;
  1229. goto out;
  1230. allocated:
  1231. ext3_debug("using block group %d(%d)\n",
  1232. group_no, gdp->bg_free_blocks_count);
  1233. BUFFER_TRACE(gdp_bh, "get_write_access");
  1234. fatal = ext3_journal_get_write_access(handle, gdp_bh);
  1235. if (fatal)
  1236. goto out;
  1237. target_block = ret_block + group_no * EXT3_BLOCKS_PER_GROUP(sb)
  1238. + le32_to_cpu(es->s_first_data_block);
  1239. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), target_block, num) ||
  1240. in_range(le32_to_cpu(gdp->bg_inode_bitmap), target_block, num) ||
  1241. in_range(target_block, le32_to_cpu(gdp->bg_inode_table),
  1242. EXT3_SB(sb)->s_itb_per_group) ||
  1243. in_range(target_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1244. EXT3_SB(sb)->s_itb_per_group))
  1245. ext3_error(sb, "ext3_new_block",
  1246. "Allocating block in system zone - "
  1247. "blocks from %u, length %lu", target_block, num);
  1248. performed_allocation = 1;
  1249. #ifdef CONFIG_JBD_DEBUG
  1250. {
  1251. struct buffer_head *debug_bh;
  1252. /* Record bitmap buffer state in the newly allocated block */
  1253. debug_bh = sb_find_get_block(sb, target_block);
  1254. if (debug_bh) {
  1255. BUFFER_TRACE(debug_bh, "state when allocated");
  1256. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1257. brelse(debug_bh);
  1258. }
  1259. }
  1260. jbd_lock_bh_state(bitmap_bh);
  1261. spin_lock(sb_bgl_lock(sbi, group_no));
  1262. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1263. int i;
  1264. for (i = 0; i < num; i++) {
  1265. if (ext3_test_bit(ret_block,
  1266. bh2jh(bitmap_bh)->b_committed_data)) {
  1267. printk("%s: block was unexpectedly set in "
  1268. "b_committed_data\n", __FUNCTION__);
  1269. }
  1270. }
  1271. }
  1272. ext3_debug("found bit %d\n", ret_block);
  1273. spin_unlock(sb_bgl_lock(sbi, group_no));
  1274. jbd_unlock_bh_state(bitmap_bh);
  1275. #endif
  1276. /* ret_block was blockgroup-relative. Now it becomes fs-relative */
  1277. ret_block = target_block;
  1278. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1279. ext3_error(sb, "ext3_new_block",
  1280. "block(%d) >= blocks count(%d) - "
  1281. "block_group = %d, es == %p ", ret_block,
  1282. le32_to_cpu(es->s_blocks_count), group_no, es);
  1283. goto out;
  1284. }
  1285. /*
  1286. * It is up to the caller to add the new buffer to a journal
  1287. * list of some description. We don't know in advance whether
  1288. * the caller wants to use it as metadata or data.
  1289. */
  1290. ext3_debug("allocating block %d. Goal hits %d of %d.\n",
  1291. ret_block, goal_hits, goal_attempts);
  1292. spin_lock(sb_bgl_lock(sbi, group_no));
  1293. gdp->bg_free_blocks_count =
  1294. cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - num);
  1295. spin_unlock(sb_bgl_lock(sbi, group_no));
  1296. percpu_counter_mod(&sbi->s_freeblocks_counter, -num);
  1297. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1298. err = ext3_journal_dirty_metadata(handle, gdp_bh);
  1299. if (!fatal)
  1300. fatal = err;
  1301. sb->s_dirt = 1;
  1302. if (fatal)
  1303. goto out;
  1304. *errp = 0;
  1305. brelse(bitmap_bh);
  1306. DQUOT_FREE_BLOCK(inode, *count-num);
  1307. *count = num;
  1308. return ret_block;
  1309. io_error:
  1310. *errp = -EIO;
  1311. out:
  1312. if (fatal) {
  1313. *errp = fatal;
  1314. ext3_std_error(sb, fatal);
  1315. }
  1316. /*
  1317. * Undo the block allocation
  1318. */
  1319. if (!performed_allocation)
  1320. DQUOT_FREE_BLOCK(inode, *count);
  1321. brelse(bitmap_bh);
  1322. return 0;
  1323. }
  1324. int ext3_new_block(handle_t *handle, struct inode *inode,
  1325. unsigned long goal, int *errp)
  1326. {
  1327. unsigned long count = 1;
  1328. return ext3_new_blocks(handle, inode, goal, &count, errp);
  1329. }
  1330. unsigned long ext3_count_free_blocks(struct super_block *sb)
  1331. {
  1332. unsigned long desc_count;
  1333. struct ext3_group_desc *gdp;
  1334. int i;
  1335. unsigned long ngroups = EXT3_SB(sb)->s_groups_count;
  1336. #ifdef EXT3FS_DEBUG
  1337. struct ext3_super_block *es;
  1338. unsigned long bitmap_count, x;
  1339. struct buffer_head *bitmap_bh = NULL;
  1340. es = EXT3_SB(sb)->s_es;
  1341. desc_count = 0;
  1342. bitmap_count = 0;
  1343. gdp = NULL;
  1344. smp_rmb();
  1345. for (i = 0; i < ngroups; i++) {
  1346. gdp = ext3_get_group_desc(sb, i, NULL);
  1347. if (!gdp)
  1348. continue;
  1349. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1350. brelse(bitmap_bh);
  1351. bitmap_bh = read_block_bitmap(sb, i);
  1352. if (bitmap_bh == NULL)
  1353. continue;
  1354. x = ext3_count_free(bitmap_bh, sb->s_blocksize);
  1355. printk("group %d: stored = %d, counted = %lu\n",
  1356. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1357. bitmap_count += x;
  1358. }
  1359. brelse(bitmap_bh);
  1360. printk("ext3_count_free_blocks: stored = %u, computed = %lu, %lu\n",
  1361. le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
  1362. return bitmap_count;
  1363. #else
  1364. desc_count = 0;
  1365. smp_rmb();
  1366. for (i = 0; i < ngroups; i++) {
  1367. gdp = ext3_get_group_desc(sb, i, NULL);
  1368. if (!gdp)
  1369. continue;
  1370. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1371. }
  1372. return desc_count;
  1373. #endif
  1374. }
  1375. static inline int
  1376. block_in_use(unsigned long block, struct super_block *sb, unsigned char *map)
  1377. {
  1378. return ext3_test_bit ((block -
  1379. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
  1380. EXT3_BLOCKS_PER_GROUP(sb), map);
  1381. }
  1382. static inline int test_root(int a, int b)
  1383. {
  1384. int num = b;
  1385. while (a > num)
  1386. num *= b;
  1387. return num == a;
  1388. }
  1389. static int ext3_group_sparse(int group)
  1390. {
  1391. if (group <= 1)
  1392. return 1;
  1393. if (!(group & 1))
  1394. return 0;
  1395. return (test_root(group, 7) || test_root(group, 5) ||
  1396. test_root(group, 3));
  1397. }
  1398. /**
  1399. * ext3_bg_has_super - number of blocks used by the superblock in group
  1400. * @sb: superblock for filesystem
  1401. * @group: group number to check
  1402. *
  1403. * Return the number of blocks used by the superblock (primary or backup)
  1404. * in this group. Currently this will be only 0 or 1.
  1405. */
  1406. int ext3_bg_has_super(struct super_block *sb, int group)
  1407. {
  1408. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1409. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1410. !ext3_group_sparse(group))
  1411. return 0;
  1412. return 1;
  1413. }
  1414. static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group)
  1415. {
  1416. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1417. unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb);
  1418. unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1;
  1419. if (group == first || group == first + 1 || group == last)
  1420. return 1;
  1421. return 0;
  1422. }
  1423. static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group)
  1424. {
  1425. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1426. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1427. !ext3_group_sparse(group))
  1428. return 0;
  1429. return EXT3_SB(sb)->s_gdb_count;
  1430. }
  1431. /**
  1432. * ext3_bg_num_gdb - number of blocks used by the group table in group
  1433. * @sb: superblock for filesystem
  1434. * @group: group number to check
  1435. *
  1436. * Return the number of blocks used by the group descriptor table
  1437. * (primary or backup) in this group. In the future there may be a
  1438. * different number of descriptor blocks in each group.
  1439. */
  1440. unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
  1441. {
  1442. unsigned long first_meta_bg =
  1443. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg);
  1444. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1445. if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) ||
  1446. metagroup < first_meta_bg)
  1447. return ext3_bg_num_gdb_nometa(sb,group);
  1448. return ext3_bg_num_gdb_meta(sb,group);
  1449. }