balloc.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /*
  2. * linux/fs/ext2/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 "ext2.h"
  14. #include <linux/quotaops.h>
  15. #include <linux/sched.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/capability.h>
  18. /*
  19. * balloc.c contains the blocks allocation and deallocation routines
  20. */
  21. /*
  22. * The free blocks are managed by bitmaps. A file system contains several
  23. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  24. * block for inodes, N blocks for the inode table and data blocks.
  25. *
  26. * The file system contains group descriptors which are located after the
  27. * super block. Each descriptor contains the number of the bitmap block and
  28. * the free blocks count in the block. The descriptors are loaded in memory
  29. * when a file system is mounted (see ext2_fill_super).
  30. */
  31. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  32. struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
  33. unsigned int block_group,
  34. struct buffer_head ** bh)
  35. {
  36. unsigned long group_desc;
  37. unsigned long offset;
  38. struct ext2_group_desc * desc;
  39. struct ext2_sb_info *sbi = EXT2_SB(sb);
  40. if (block_group >= sbi->s_groups_count) {
  41. ext2_error (sb, "ext2_get_group_desc",
  42. "block_group >= groups_count - "
  43. "block_group = %d, groups_count = %lu",
  44. block_group, sbi->s_groups_count);
  45. return NULL;
  46. }
  47. group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
  48. offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
  49. if (!sbi->s_group_desc[group_desc]) {
  50. ext2_error (sb, "ext2_get_group_desc",
  51. "Group descriptor not loaded - "
  52. "block_group = %d, group_desc = %lu, desc = %lu",
  53. block_group, group_desc, offset);
  54. return NULL;
  55. }
  56. desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  57. if (bh)
  58. *bh = sbi->s_group_desc[group_desc];
  59. return desc + offset;
  60. }
  61. /*
  62. * Read the bitmap for a given block_group, reading into the specified
  63. * slot in the superblock's bitmap cache.
  64. *
  65. * Return buffer_head on success or NULL in case of failure.
  66. */
  67. static struct buffer_head *
  68. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  69. {
  70. struct ext2_group_desc * desc;
  71. struct buffer_head * bh = NULL;
  72. desc = ext2_get_group_desc (sb, block_group, NULL);
  73. if (!desc)
  74. goto error_out;
  75. bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
  76. if (!bh)
  77. ext2_error (sb, "read_block_bitmap",
  78. "Cannot read block bitmap - "
  79. "block_group = %d, block_bitmap = %u",
  80. block_group, le32_to_cpu(desc->bg_block_bitmap));
  81. error_out:
  82. return bh;
  83. }
  84. static void release_blocks(struct super_block *sb, int count)
  85. {
  86. if (count) {
  87. struct ext2_sb_info *sbi = EXT2_SB(sb);
  88. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  89. sb->s_dirt = 1;
  90. }
  91. }
  92. static void group_adjust_blocks(struct super_block *sb, int group_no,
  93. struct ext2_group_desc *desc, struct buffer_head *bh, int count)
  94. {
  95. if (count) {
  96. struct ext2_sb_info *sbi = EXT2_SB(sb);
  97. unsigned free_blocks;
  98. spin_lock(sb_bgl_lock(sbi, group_no));
  99. free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
  100. desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
  101. spin_unlock(sb_bgl_lock(sbi, group_no));
  102. sb->s_dirt = 1;
  103. mark_buffer_dirty(bh);
  104. }
  105. }
  106. /*
  107. * The reservation window structure operations
  108. * --------------------------------------------
  109. * Operations include:
  110. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  111. *
  112. * We use a red-black tree to represent per-filesystem reservation
  113. * windows.
  114. *
  115. */
  116. /**
  117. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  118. * @rb_root: root of per-filesystem reservation rb tree
  119. * @verbose: verbose mode
  120. * @fn: function which wishes to dump the reservation map
  121. *
  122. * If verbose is turned on, it will print the whole block reservation
  123. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  124. * those windows that overlap with their immediate neighbors.
  125. */
  126. #if 1
  127. static void __rsv_window_dump(struct rb_root *root, int verbose,
  128. const char *fn)
  129. {
  130. struct rb_node *n;
  131. struct ext2_reserve_window_node *rsv, *prev;
  132. int bad;
  133. restart:
  134. n = rb_first(root);
  135. bad = 0;
  136. prev = NULL;
  137. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  138. while (n) {
  139. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  140. if (verbose)
  141. printk("reservation window 0x%p "
  142. "start: %lu, end: %lu\n",
  143. rsv, rsv->rsv_start, rsv->rsv_end);
  144. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  145. printk("Bad reservation %p (start >= end)\n",
  146. rsv);
  147. bad = 1;
  148. }
  149. if (prev && prev->rsv_end >= rsv->rsv_start) {
  150. printk("Bad reservation %p (prev->end >= start)\n",
  151. rsv);
  152. bad = 1;
  153. }
  154. if (bad) {
  155. if (!verbose) {
  156. printk("Restarting reservation walk in verbose mode\n");
  157. verbose = 1;
  158. goto restart;
  159. }
  160. }
  161. n = rb_next(n);
  162. prev = rsv;
  163. }
  164. printk("Window map complete.\n");
  165. if (bad)
  166. BUG();
  167. }
  168. #define rsv_window_dump(root, verbose) \
  169. __rsv_window_dump((root), (verbose), __FUNCTION__)
  170. #else
  171. #define rsv_window_dump(root, verbose) do {} while (0)
  172. #endif
  173. /**
  174. * goal_in_my_reservation()
  175. * @rsv: inode's reservation window
  176. * @grp_goal: given goal block relative to the allocation block group
  177. * @group: the current allocation block group
  178. * @sb: filesystem super block
  179. *
  180. * Test if the given goal block (group relative) is within the file's
  181. * own block reservation window range.
  182. *
  183. * If the reservation window is outside the goal allocation group, return 0;
  184. * grp_goal (given goal block) could be -1, which means no specific
  185. * goal block. In this case, always return 1.
  186. * If the goal block is within the reservation window, return 1;
  187. * otherwise, return 0;
  188. */
  189. static int
  190. goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
  191. unsigned int group, struct super_block * sb)
  192. {
  193. ext2_fsblk_t group_first_block, group_last_block;
  194. group_first_block = ext2_group_first_block_no(sb, group);
  195. group_last_block = group_first_block + EXT2_BLOCKS_PER_GROUP(sb) - 1;
  196. if ((rsv->_rsv_start > group_last_block) ||
  197. (rsv->_rsv_end < group_first_block))
  198. return 0;
  199. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  200. || (grp_goal + group_first_block > rsv->_rsv_end)))
  201. return 0;
  202. return 1;
  203. }
  204. /**
  205. * search_reserve_window()
  206. * @rb_root: root of reservation tree
  207. * @goal: target allocation block
  208. *
  209. * Find the reserved window which includes the goal, or the previous one
  210. * if the goal is not in any window.
  211. * Returns NULL if there are no windows or if all windows start after the goal.
  212. */
  213. static struct ext2_reserve_window_node *
  214. search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
  215. {
  216. struct rb_node *n = root->rb_node;
  217. struct ext2_reserve_window_node *rsv;
  218. if (!n)
  219. return NULL;
  220. do {
  221. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  222. if (goal < rsv->rsv_start)
  223. n = n->rb_left;
  224. else if (goal > rsv->rsv_end)
  225. n = n->rb_right;
  226. else
  227. return rsv;
  228. } while (n);
  229. /*
  230. * We've fallen off the end of the tree: the goal wasn't inside
  231. * any particular node. OK, the previous node must be to one
  232. * side of the interval containing the goal. If it's the RHS,
  233. * we need to back up one.
  234. */
  235. if (rsv->rsv_start > goal) {
  236. n = rb_prev(&rsv->rsv_node);
  237. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  238. }
  239. return rsv;
  240. }
  241. /*
  242. * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
  243. * @sb: super block
  244. * @rsv: reservation window to add
  245. *
  246. * Must be called with rsv_lock held.
  247. */
  248. void ext2_rsv_window_add(struct super_block *sb,
  249. struct ext2_reserve_window_node *rsv)
  250. {
  251. struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
  252. struct rb_node *node = &rsv->rsv_node;
  253. ext2_fsblk_t start = rsv->rsv_start;
  254. struct rb_node ** p = &root->rb_node;
  255. struct rb_node * parent = NULL;
  256. struct ext2_reserve_window_node *this;
  257. while (*p)
  258. {
  259. parent = *p;
  260. this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
  261. if (start < this->rsv_start)
  262. p = &(*p)->rb_left;
  263. else if (start > this->rsv_end)
  264. p = &(*p)->rb_right;
  265. else {
  266. rsv_window_dump(root, 1);
  267. BUG();
  268. }
  269. }
  270. rb_link_node(node, parent, p);
  271. rb_insert_color(node, root);
  272. }
  273. /**
  274. * rsv_window_remove() -- unlink a window from the reservation rb tree
  275. * @sb: super block
  276. * @rsv: reservation window to remove
  277. *
  278. * Mark the block reservation window as not allocated, and unlink it
  279. * from the filesystem reservation window rb tree. Must be called with
  280. * rsv_lock held.
  281. */
  282. static void rsv_window_remove(struct super_block *sb,
  283. struct ext2_reserve_window_node *rsv)
  284. {
  285. rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  286. rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  287. rsv->rsv_alloc_hit = 0;
  288. rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
  289. }
  290. /*
  291. * rsv_is_empty() -- Check if the reservation window is allocated.
  292. * @rsv: given reservation window to check
  293. *
  294. * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
  295. */
  296. static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
  297. {
  298. /* a valid reservation end block could not be 0 */
  299. return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
  300. }
  301. /**
  302. * ext2_init_block_alloc_info()
  303. * @inode: file inode structure
  304. *
  305. * Allocate and initialize the reservation window structure, and
  306. * link the window to the ext2 inode structure at last
  307. *
  308. * The reservation window structure is only dynamically allocated
  309. * and linked to ext2 inode the first time the open file
  310. * needs a new block. So, before every ext2_new_block(s) call, for
  311. * regular files, we should check whether the reservation window
  312. * structure exists or not. In the latter case, this function is called.
  313. * Fail to do so will result in block reservation being turned off for that
  314. * open file.
  315. *
  316. * This function is called from ext2_get_blocks_handle(), also called
  317. * when setting the reservation window size through ioctl before the file
  318. * is open for write (needs block allocation).
  319. *
  320. * Needs truncate_mutex protection prior to calling this function.
  321. */
  322. void ext2_init_block_alloc_info(struct inode *inode)
  323. {
  324. struct ext2_inode_info *ei = EXT2_I(inode);
  325. struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
  326. struct super_block *sb = inode->i_sb;
  327. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  328. if (block_i) {
  329. struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
  330. rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  331. rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  332. /*
  333. * if filesystem is mounted with NORESERVATION, the goal
  334. * reservation window size is set to zero to indicate
  335. * block reservation is off
  336. */
  337. if (!test_opt(sb, RESERVATION))
  338. rsv->rsv_goal_size = 0;
  339. else
  340. rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
  341. rsv->rsv_alloc_hit = 0;
  342. block_i->last_alloc_logical_block = 0;
  343. block_i->last_alloc_physical_block = 0;
  344. }
  345. ei->i_block_alloc_info = block_i;
  346. }
  347. /**
  348. * ext2_discard_reservation()
  349. * @inode: inode
  350. *
  351. * Discard(free) block reservation window on last file close, or truncate
  352. * or at last iput().
  353. *
  354. * It is being called in three cases:
  355. * ext2_release_file(): last writer closes the file
  356. * ext2_clear_inode(): last iput(), when nobody links to this file.
  357. * ext2_truncate(): when the block indirect map is about to change.
  358. */
  359. void ext2_discard_reservation(struct inode *inode)
  360. {
  361. struct ext2_inode_info *ei = EXT2_I(inode);
  362. struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
  363. struct ext2_reserve_window_node *rsv;
  364. spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
  365. if (!block_i)
  366. return;
  367. rsv = &block_i->rsv_window_node;
  368. if (!rsv_is_empty(&rsv->rsv_window)) {
  369. spin_lock(rsv_lock);
  370. if (!rsv_is_empty(&rsv->rsv_window))
  371. rsv_window_remove(inode->i_sb, rsv);
  372. spin_unlock(rsv_lock);
  373. }
  374. }
  375. /**
  376. * ext2_free_blocks_sb() -- Free given blocks and update quota and i_blocks
  377. * @inode: inode
  378. * @block: start physcial block to free
  379. * @count: number of blocks to free
  380. */
  381. void ext2_free_blocks (struct inode * inode, unsigned long block,
  382. unsigned long count)
  383. {
  384. struct buffer_head *bitmap_bh = NULL;
  385. struct buffer_head * bh2;
  386. unsigned long block_group;
  387. unsigned long bit;
  388. unsigned long i;
  389. unsigned long overflow;
  390. struct super_block * sb = inode->i_sb;
  391. struct ext2_sb_info * sbi = EXT2_SB(sb);
  392. struct ext2_group_desc * desc;
  393. struct ext2_super_block * es = sbi->s_es;
  394. unsigned freed = 0, group_freed;
  395. if (block < le32_to_cpu(es->s_first_data_block) ||
  396. block + count < block ||
  397. block + count > le32_to_cpu(es->s_blocks_count)) {
  398. ext2_error (sb, "ext2_free_blocks",
  399. "Freeing blocks not in datazone - "
  400. "block = %lu, count = %lu", block, count);
  401. goto error_return;
  402. }
  403. ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  404. do_more:
  405. overflow = 0;
  406. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  407. EXT2_BLOCKS_PER_GROUP(sb);
  408. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  409. EXT2_BLOCKS_PER_GROUP(sb);
  410. /*
  411. * Check to see if we are freeing blocks across a group
  412. * boundary.
  413. */
  414. if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
  415. overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
  416. count -= overflow;
  417. }
  418. brelse(bitmap_bh);
  419. bitmap_bh = read_block_bitmap(sb, block_group);
  420. if (!bitmap_bh)
  421. goto error_return;
  422. desc = ext2_get_group_desc (sb, block_group, &bh2);
  423. if (!desc)
  424. goto error_return;
  425. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  426. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  427. in_range (block, le32_to_cpu(desc->bg_inode_table),
  428. sbi->s_itb_per_group) ||
  429. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  430. sbi->s_itb_per_group)) {
  431. ext2_error (sb, "ext2_free_blocks",
  432. "Freeing blocks in system zones - "
  433. "Block = %lu, count = %lu",
  434. block, count);
  435. goto error_return;
  436. }
  437. for (i = 0, group_freed = 0; i < count; i++) {
  438. if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  439. bit + i, bitmap_bh->b_data)) {
  440. ext2_error(sb, __FUNCTION__,
  441. "bit already cleared for block %lu", block + i);
  442. } else {
  443. group_freed++;
  444. }
  445. }
  446. mark_buffer_dirty(bitmap_bh);
  447. if (sb->s_flags & MS_SYNCHRONOUS)
  448. sync_dirty_buffer(bitmap_bh);
  449. group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
  450. freed += group_freed;
  451. if (overflow) {
  452. block += count;
  453. count = overflow;
  454. goto do_more;
  455. }
  456. error_return:
  457. brelse(bitmap_bh);
  458. release_blocks(sb, freed);
  459. DQUOT_FREE_BLOCK(inode, freed);
  460. }
  461. /**
  462. * bitmap_search_next_usable_block()
  463. * @start: the starting block (group relative) of the search
  464. * @bh: bufferhead contains the block group bitmap
  465. * @maxblocks: the ending block (group relative) of the reservation
  466. *
  467. * The bitmap search --- search forward through the actual bitmap on disk until
  468. * we find a bit free.
  469. */
  470. static ext2_grpblk_t
  471. bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
  472. ext2_grpblk_t maxblocks)
  473. {
  474. ext2_grpblk_t next;
  475. next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
  476. if (next >= maxblocks)
  477. return -1;
  478. return next;
  479. }
  480. /**
  481. * find_next_usable_block()
  482. * @start: the starting block (group relative) to find next
  483. * allocatable block in bitmap.
  484. * @bh: bufferhead contains the block group bitmap
  485. * @maxblocks: the ending block (group relative) for the search
  486. *
  487. * Find an allocatable block in a bitmap. We perform the "most
  488. * appropriate allocation" algorithm of looking for a free block near
  489. * the initial goal; then for a free byte somewhere in the bitmap;
  490. * then for any free bit in the bitmap.
  491. */
  492. static ext2_grpblk_t
  493. find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
  494. {
  495. ext2_grpblk_t here, next;
  496. char *p, *r;
  497. if (start > 0) {
  498. /*
  499. * The goal was occupied; search forward for a free
  500. * block within the next XX blocks.
  501. *
  502. * end_goal is more or less random, but it has to be
  503. * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
  504. * next 64-bit boundary is simple..
  505. */
  506. ext2_grpblk_t end_goal = (start + 63) & ~63;
  507. if (end_goal > maxblocks)
  508. end_goal = maxblocks;
  509. here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
  510. if (here < end_goal)
  511. return here;
  512. ext2_debug("Bit not found near goal\n");
  513. }
  514. here = start;
  515. if (here < 0)
  516. here = 0;
  517. p = ((char *)bh->b_data) + (here >> 3);
  518. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  519. next = (r - ((char *)bh->b_data)) << 3;
  520. if (next < maxblocks && next >= here)
  521. return next;
  522. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  523. return here;
  524. }
  525. /*
  526. * ext2_try_to_allocate()
  527. * @sb: superblock
  528. * @handle: handle to this transaction
  529. * @group: given allocation block group
  530. * @bitmap_bh: bufferhead holds the block bitmap
  531. * @grp_goal: given target block within the group
  532. * @count: target number of blocks to allocate
  533. * @my_rsv: reservation window
  534. *
  535. * Attempt to allocate blocks within a give range. Set the range of allocation
  536. * first, then find the first free bit(s) from the bitmap (within the range),
  537. * and at last, allocate the blocks by claiming the found free bit as allocated.
  538. *
  539. * To set the range of this allocation:
  540. * if there is a reservation window, only try to allocate block(s)
  541. * from the file's own reservation window;
  542. * Otherwise, the allocation range starts from the give goal block,
  543. * ends at the block group's last block.
  544. *
  545. * If we failed to allocate the desired block then we may end up crossing to a
  546. * new bitmap.
  547. */
  548. static int
  549. ext2_try_to_allocate(struct super_block *sb, int group,
  550. struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
  551. unsigned long *count,
  552. struct ext2_reserve_window *my_rsv)
  553. {
  554. ext2_fsblk_t group_first_block;
  555. ext2_grpblk_t start, end;
  556. unsigned long num = 0;
  557. /* we do allocation within the reservation window if we have a window */
  558. if (my_rsv) {
  559. group_first_block = ext2_group_first_block_no(sb, group);
  560. if (my_rsv->_rsv_start >= group_first_block)
  561. start = my_rsv->_rsv_start - group_first_block;
  562. else
  563. /* reservation window cross group boundary */
  564. start = 0;
  565. end = my_rsv->_rsv_end - group_first_block + 1;
  566. if (end > EXT2_BLOCKS_PER_GROUP(sb))
  567. /* reservation window crosses group boundary */
  568. end = EXT2_BLOCKS_PER_GROUP(sb);
  569. if ((start <= grp_goal) && (grp_goal < end))
  570. start = grp_goal;
  571. else
  572. grp_goal = -1;
  573. } else {
  574. if (grp_goal > 0)
  575. start = grp_goal;
  576. else
  577. start = 0;
  578. end = EXT2_BLOCKS_PER_GROUP(sb);
  579. }
  580. BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
  581. repeat:
  582. if (grp_goal < 0) {
  583. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  584. if (grp_goal < 0)
  585. goto fail_access;
  586. if (!my_rsv) {
  587. int i;
  588. for (i = 0; i < 7 && grp_goal > start &&
  589. !ext2_test_bit(grp_goal - 1,
  590. bitmap_bh->b_data);
  591. i++, grp_goal--)
  592. ;
  593. }
  594. }
  595. start = grp_goal;
  596. if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), grp_goal,
  597. bitmap_bh->b_data)) {
  598. /*
  599. * The block was allocated by another thread, or it was
  600. * allocated and then freed by another thread
  601. */
  602. start++;
  603. grp_goal++;
  604. if (start >= end)
  605. goto fail_access;
  606. goto repeat;
  607. }
  608. num++;
  609. grp_goal++;
  610. while (num < *count && grp_goal < end
  611. && !ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
  612. grp_goal, bitmap_bh->b_data)) {
  613. num++;
  614. grp_goal++;
  615. }
  616. *count = num;
  617. return grp_goal - num;
  618. fail_access:
  619. *count = num;
  620. return -1;
  621. }
  622. /**
  623. * find_next_reservable_window():
  624. * find a reservable space within the given range.
  625. * It does not allocate the reservation window for now:
  626. * alloc_new_reservation() will do the work later.
  627. *
  628. * @search_head: the head of the searching list;
  629. * This is not necessarily the list head of the whole filesystem
  630. *
  631. * We have both head and start_block to assist the search
  632. * for the reservable space. The list starts from head,
  633. * but we will shift to the place where start_block is,
  634. * then start from there, when looking for a reservable space.
  635. *
  636. * @size: the target new reservation window size
  637. *
  638. * @group_first_block: the first block we consider to start
  639. * the real search from
  640. *
  641. * @last_block:
  642. * the maximum block number that our goal reservable space
  643. * could start from. This is normally the last block in this
  644. * group. The search will end when we found the start of next
  645. * possible reservable space is out of this boundary.
  646. * This could handle the cross boundary reservation window
  647. * request.
  648. *
  649. * basically we search from the given range, rather than the whole
  650. * reservation double linked list, (start_block, last_block)
  651. * to find a free region that is of my size and has not
  652. * been reserved.
  653. *
  654. */
  655. static int find_next_reservable_window(
  656. struct ext2_reserve_window_node *search_head,
  657. struct ext2_reserve_window_node *my_rsv,
  658. struct super_block * sb,
  659. ext2_fsblk_t start_block,
  660. ext2_fsblk_t last_block)
  661. {
  662. struct rb_node *next;
  663. struct ext2_reserve_window_node *rsv, *prev;
  664. ext2_fsblk_t cur;
  665. int size = my_rsv->rsv_goal_size;
  666. /* TODO: make the start of the reservation window byte-aligned */
  667. /* cur = *start_block & ~7;*/
  668. cur = start_block;
  669. rsv = search_head;
  670. if (!rsv)
  671. return -1;
  672. while (1) {
  673. if (cur <= rsv->rsv_end)
  674. cur = rsv->rsv_end + 1;
  675. /* TODO?
  676. * in the case we could not find a reservable space
  677. * that is what is expected, during the re-search, we could
  678. * remember what's the largest reservable space we could have
  679. * and return that one.
  680. *
  681. * For now it will fail if we could not find the reservable
  682. * space with expected-size (or more)...
  683. */
  684. if (cur > last_block)
  685. return -1; /* fail */
  686. prev = rsv;
  687. next = rb_next(&rsv->rsv_node);
  688. rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
  689. /*
  690. * Reached the last reservation, we can just append to the
  691. * previous one.
  692. */
  693. if (!next)
  694. break;
  695. if (cur + size <= rsv->rsv_start) {
  696. /*
  697. * Found a reserveable space big enough. We could
  698. * have a reservation across the group boundary here
  699. */
  700. break;
  701. }
  702. }
  703. /*
  704. * we come here either :
  705. * when we reach the end of the whole list,
  706. * and there is empty reservable space after last entry in the list.
  707. * append it to the end of the list.
  708. *
  709. * or we found one reservable space in the middle of the list,
  710. * return the reservation window that we could append to.
  711. * succeed.
  712. */
  713. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  714. rsv_window_remove(sb, my_rsv);
  715. /*
  716. * Let's book the whole avaliable window for now. We will check the
  717. * disk bitmap later and then, if there are free blocks then we adjust
  718. * the window size if it's larger than requested.
  719. * Otherwise, we will remove this node from the tree next time
  720. * call find_next_reservable_window.
  721. */
  722. my_rsv->rsv_start = cur;
  723. my_rsv->rsv_end = cur + size - 1;
  724. my_rsv->rsv_alloc_hit = 0;
  725. if (prev != my_rsv)
  726. ext2_rsv_window_add(sb, my_rsv);
  727. return 0;
  728. }
  729. /**
  730. * alloc_new_reservation()--allocate a new reservation window
  731. *
  732. * To make a new reservation, we search part of the filesystem
  733. * reservation list (the list that inside the group). We try to
  734. * allocate a new reservation window near the allocation goal,
  735. * or the beginning of the group, if there is no goal.
  736. *
  737. * We first find a reservable space after the goal, then from
  738. * there, we check the bitmap for the first free block after
  739. * it. If there is no free block until the end of group, then the
  740. * whole group is full, we failed. Otherwise, check if the free
  741. * block is inside the expected reservable space, if so, we
  742. * succeed.
  743. * If the first free block is outside the reservable space, then
  744. * start from the first free block, we search for next available
  745. * space, and go on.
  746. *
  747. * on succeed, a new reservation will be found and inserted into the list
  748. * It contains at least one free block, and it does not overlap with other
  749. * reservation windows.
  750. *
  751. * failed: we failed to find a reservation window in this group
  752. *
  753. * @rsv: the reservation
  754. *
  755. * @grp_goal: The goal (group-relative). It is where the search for a
  756. * free reservable space should start from.
  757. * if we have a goal(goal >0 ), then start from there,
  758. * no goal(goal = -1), we start from the first block
  759. * of the group.
  760. *
  761. * @sb: the super block
  762. * @group: the group we are trying to allocate in
  763. * @bitmap_bh: the block group block bitmap
  764. *
  765. */
  766. static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
  767. ext2_grpblk_t grp_goal, struct super_block *sb,
  768. unsigned int group, struct buffer_head *bitmap_bh)
  769. {
  770. struct ext2_reserve_window_node *search_head;
  771. ext2_fsblk_t group_first_block, group_end_block, start_block;
  772. ext2_grpblk_t first_free_block;
  773. struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
  774. unsigned long size;
  775. int ret;
  776. spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
  777. group_first_block = ext2_group_first_block_no(sb, group);
  778. group_end_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1);
  779. if (grp_goal < 0)
  780. start_block = group_first_block;
  781. else
  782. start_block = grp_goal + group_first_block;
  783. size = my_rsv->rsv_goal_size;
  784. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  785. /*
  786. * if the old reservation is cross group boundary
  787. * and if the goal is inside the old reservation window,
  788. * we will come here when we just failed to allocate from
  789. * the first part of the window. We still have another part
  790. * that belongs to the next group. In this case, there is no
  791. * point to discard our window and try to allocate a new one
  792. * in this group(which will fail). we should
  793. * keep the reservation window, just simply move on.
  794. *
  795. * Maybe we could shift the start block of the reservation
  796. * window to the first block of next group.
  797. */
  798. if ((my_rsv->rsv_start <= group_end_block) &&
  799. (my_rsv->rsv_end > group_end_block) &&
  800. (start_block >= my_rsv->rsv_start))
  801. return -1;
  802. if ((my_rsv->rsv_alloc_hit >
  803. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  804. /*
  805. * if the previously allocation hit ratio is
  806. * greater than 1/2, then we double the size of
  807. * the reservation window the next time,
  808. * otherwise we keep the same size window
  809. */
  810. size = size * 2;
  811. if (size > EXT2_MAX_RESERVE_BLOCKS)
  812. size = EXT2_MAX_RESERVE_BLOCKS;
  813. my_rsv->rsv_goal_size= size;
  814. }
  815. }
  816. spin_lock(rsv_lock);
  817. /*
  818. * shift the search start to the window near the goal block
  819. */
  820. search_head = search_reserve_window(fs_rsv_root, start_block);
  821. /*
  822. * find_next_reservable_window() simply finds a reservable window
  823. * inside the given range(start_block, group_end_block).
  824. *
  825. * To make sure the reservation window has a free bit inside it, we
  826. * need to check the bitmap after we found a reservable window.
  827. */
  828. retry:
  829. ret = find_next_reservable_window(search_head, my_rsv, sb,
  830. start_block, group_end_block);
  831. if (ret == -1) {
  832. if (!rsv_is_empty(&my_rsv->rsv_window))
  833. rsv_window_remove(sb, my_rsv);
  834. spin_unlock(rsv_lock);
  835. return -1;
  836. }
  837. /*
  838. * On success, find_next_reservable_window() returns the
  839. * reservation window where there is a reservable space after it.
  840. * Before we reserve this reservable space, we need
  841. * to make sure there is at least a free block inside this region.
  842. *
  843. * Search the first free bit on the block bitmap. Search starts from
  844. * the start block of the reservable space we just found.
  845. */
  846. spin_unlock(rsv_lock);
  847. first_free_block = bitmap_search_next_usable_block(
  848. my_rsv->rsv_start - group_first_block,
  849. bitmap_bh, group_end_block - group_first_block + 1);
  850. if (first_free_block < 0) {
  851. /*
  852. * no free block left on the bitmap, no point
  853. * to reserve the space. return failed.
  854. */
  855. spin_lock(rsv_lock);
  856. if (!rsv_is_empty(&my_rsv->rsv_window))
  857. rsv_window_remove(sb, my_rsv);
  858. spin_unlock(rsv_lock);
  859. return -1; /* failed */
  860. }
  861. start_block = first_free_block + group_first_block;
  862. /*
  863. * check if the first free block is within the
  864. * free space we just reserved
  865. */
  866. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  867. return 0; /* success */
  868. /*
  869. * if the first free bit we found is out of the reservable space
  870. * continue search for next reservable space,
  871. * start from where the free block is,
  872. * we also shift the list head to where we stopped last time
  873. */
  874. search_head = my_rsv;
  875. spin_lock(rsv_lock);
  876. goto retry;
  877. }
  878. /**
  879. * try_to_extend_reservation()
  880. * @my_rsv: given reservation window
  881. * @sb: super block
  882. * @size: the delta to extend
  883. *
  884. * Attempt to expand the reservation window large enough to have
  885. * required number of free blocks
  886. *
  887. * Since ext2_try_to_allocate() will always allocate blocks within
  888. * the reservation window range, if the window size is too small,
  889. * multiple blocks allocation has to stop at the end of the reservation
  890. * window. To make this more efficient, given the total number of
  891. * blocks needed and the current size of the window, we try to
  892. * expand the reservation window size if necessary on a best-effort
  893. * basis before ext2_new_blocks() tries to allocate blocks.
  894. */
  895. static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
  896. struct super_block *sb, int size)
  897. {
  898. struct ext2_reserve_window_node *next_rsv;
  899. struct rb_node *next;
  900. spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
  901. if (!spin_trylock(rsv_lock))
  902. return;
  903. next = rb_next(&my_rsv->rsv_node);
  904. if (!next)
  905. my_rsv->rsv_end += size;
  906. else {
  907. next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
  908. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  909. my_rsv->rsv_end += size;
  910. else
  911. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  912. }
  913. spin_unlock(rsv_lock);
  914. }
  915. /**
  916. * ext2_try_to_allocate_with_rsv()
  917. * @sb: superblock
  918. * @group: given allocation block group
  919. * @bitmap_bh: bufferhead holds the block bitmap
  920. * @grp_goal: given target block within the group
  921. * @count: target number of blocks to allocate
  922. * @my_rsv: reservation window
  923. *
  924. * This is the main function used to allocate a new block and its reservation
  925. * window.
  926. *
  927. * Each time when a new block allocation is need, first try to allocate from
  928. * its own reservation. If it does not have a reservation window, instead of
  929. * looking for a free bit on bitmap first, then look up the reservation list to
  930. * see if it is inside somebody else's reservation window, we try to allocate a
  931. * reservation window for it starting from the goal first. Then do the block
  932. * allocation within the reservation window.
  933. *
  934. * This will avoid keeping on searching the reservation list again and
  935. * again when somebody is looking for a free block (without
  936. * reservation), and there are lots of free blocks, but they are all
  937. * being reserved.
  938. *
  939. * We use a red-black tree for the per-filesystem reservation list.
  940. */
  941. static ext2_grpblk_t
  942. ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
  943. struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
  944. struct ext2_reserve_window_node * my_rsv,
  945. unsigned long *count)
  946. {
  947. ext2_fsblk_t group_first_block, group_last_block;
  948. ext2_grpblk_t ret = 0;
  949. unsigned long num = *count;
  950. /*
  951. * we don't deal with reservation when
  952. * filesystem is mounted without reservation
  953. * or the file is not a regular file
  954. * or last attempt to allocate a block with reservation turned on failed
  955. */
  956. if (my_rsv == NULL) {
  957. return ext2_try_to_allocate(sb, group, bitmap_bh,
  958. grp_goal, count, NULL);
  959. }
  960. /*
  961. * grp_goal is a group relative block number (if there is a goal)
  962. * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
  963. * first block is a filesystem wide block number
  964. * first block is the block number of the first block in this group
  965. */
  966. group_first_block = ext2_group_first_block_no(sb, group);
  967. group_last_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1);
  968. /*
  969. * Basically we will allocate a new block from inode's reservation
  970. * window.
  971. *
  972. * We need to allocate a new reservation window, if:
  973. * a) inode does not have a reservation window; or
  974. * b) last attempt to allocate a block from existing reservation
  975. * failed; or
  976. * c) we come here with a goal and with a reservation window
  977. *
  978. * We do not need to allocate a new reservation window if we come here
  979. * at the beginning with a goal and the goal is inside the window, or
  980. * we don't have a goal but already have a reservation window.
  981. * then we could go to allocate from the reservation window directly.
  982. */
  983. while (1) {
  984. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  985. !goal_in_my_reservation(&my_rsv->rsv_window,
  986. grp_goal, group, sb)) {
  987. if (my_rsv->rsv_goal_size < *count)
  988. my_rsv->rsv_goal_size = *count;
  989. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  990. group, bitmap_bh);
  991. if (ret < 0)
  992. break; /* failed */
  993. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  994. grp_goal, group, sb))
  995. grp_goal = -1;
  996. } else if (grp_goal >= 0) {
  997. int curr = my_rsv->rsv_end -
  998. (grp_goal + group_first_block) + 1;
  999. if (curr < *count)
  1000. try_to_extend_reservation(my_rsv, sb,
  1001. *count - curr);
  1002. }
  1003. if ((my_rsv->rsv_start > group_last_block) ||
  1004. (my_rsv->rsv_end < group_first_block)) {
  1005. rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
  1006. BUG();
  1007. }
  1008. ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
  1009. &num, &my_rsv->rsv_window);
  1010. if (ret >= 0) {
  1011. my_rsv->rsv_alloc_hit += num;
  1012. *count = num;
  1013. break; /* succeed */
  1014. }
  1015. num = *count;
  1016. }
  1017. return ret;
  1018. }
  1019. /**
  1020. * ext2_has_free_blocks()
  1021. * @sbi: in-core super block structure.
  1022. *
  1023. * Check if filesystem has at least 1 free block available for allocation.
  1024. */
  1025. static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
  1026. {
  1027. ext2_fsblk_t free_blocks, root_blocks;
  1028. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1029. root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
  1030. if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
  1031. sbi->s_resuid != current->fsuid &&
  1032. (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
  1033. return 0;
  1034. }
  1035. return 1;
  1036. }
  1037. /*
  1038. * ext2_new_blocks() -- core block(s) allocation function
  1039. * @inode: file inode
  1040. * @goal: given target block(filesystem wide)
  1041. * @count: target number of blocks to allocate
  1042. * @errp: error code
  1043. *
  1044. * ext2_new_blocks uses a goal block to assist allocation. If the goal is
  1045. * free, or there is a free block within 32 blocks of the goal, that block
  1046. * is allocated. Otherwise a forward search is made for a free block; within
  1047. * each block group the search first looks for an entire free byte in the block
  1048. * bitmap, and then for any free bit if that fails.
  1049. * This function also updates quota and i_blocks field.
  1050. */
  1051. ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
  1052. unsigned long *count, int *errp)
  1053. {
  1054. struct buffer_head *bitmap_bh = NULL;
  1055. struct buffer_head *gdp_bh;
  1056. int group_no;
  1057. int goal_group;
  1058. ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1059. ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1060. ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1061. int bgi; /* blockgroup iteration index */
  1062. int performed_allocation = 0;
  1063. ext2_grpblk_t free_blocks; /* number of free blocks in a group */
  1064. struct super_block *sb;
  1065. struct ext2_group_desc *gdp;
  1066. struct ext2_super_block *es;
  1067. struct ext2_sb_info *sbi;
  1068. struct ext2_reserve_window_node *my_rsv = NULL;
  1069. struct ext2_block_alloc_info *block_i;
  1070. unsigned short windowsz = 0;
  1071. unsigned long ngroups;
  1072. unsigned long num = *count;
  1073. *errp = -ENOSPC;
  1074. sb = inode->i_sb;
  1075. if (!sb) {
  1076. printk("ext2_new_blocks: nonexistent device");
  1077. return 0;
  1078. }
  1079. /*
  1080. * Check quota for allocation of this block.
  1081. */
  1082. if (DQUOT_ALLOC_BLOCK(inode, num)) {
  1083. *errp = -EDQUOT;
  1084. return 0;
  1085. }
  1086. sbi = EXT2_SB(sb);
  1087. es = EXT2_SB(sb)->s_es;
  1088. ext2_debug("goal=%lu.\n", goal);
  1089. /*
  1090. * Allocate a block from reservation only when
  1091. * filesystem is mounted with reservation(default,-o reservation), and
  1092. * it's a regular file, and
  1093. * the desired window size is greater than 0 (One could use ioctl
  1094. * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off
  1095. * reservation on that particular file)
  1096. */
  1097. block_i = EXT2_I(inode)->i_block_alloc_info;
  1098. if (block_i) {
  1099. windowsz = block_i->rsv_window_node.rsv_goal_size;
  1100. if (windowsz > 0)
  1101. my_rsv = &block_i->rsv_window_node;
  1102. }
  1103. if (!ext2_has_free_blocks(sbi)) {
  1104. *errp = -ENOSPC;
  1105. goto out;
  1106. }
  1107. /*
  1108. * First, test whether the goal block is free.
  1109. */
  1110. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1111. goal >= le32_to_cpu(es->s_blocks_count))
  1112. goal = le32_to_cpu(es->s_first_data_block);
  1113. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1114. EXT2_BLOCKS_PER_GROUP(sb);
  1115. goal_group = group_no;
  1116. retry_alloc:
  1117. gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
  1118. if (!gdp)
  1119. goto io_error;
  1120. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1121. /*
  1122. * if there is not enough free blocks to make a new resevation
  1123. * turn off reservation for this allocation
  1124. */
  1125. if (my_rsv && (free_blocks < windowsz)
  1126. && (rsv_is_empty(&my_rsv->rsv_window)))
  1127. my_rsv = NULL;
  1128. if (free_blocks > 0) {
  1129. grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1130. EXT2_BLOCKS_PER_GROUP(sb));
  1131. bitmap_bh = read_block_bitmap(sb, group_no);
  1132. if (!bitmap_bh)
  1133. goto io_error;
  1134. grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
  1135. bitmap_bh, grp_target_blk,
  1136. my_rsv, &num);
  1137. if (grp_alloc_blk >= 0)
  1138. goto allocated;
  1139. }
  1140. ngroups = EXT2_SB(sb)->s_groups_count;
  1141. smp_rmb();
  1142. /*
  1143. * Now search the rest of the groups. We assume that
  1144. * i and gdp correctly point to the last group visited.
  1145. */
  1146. for (bgi = 0; bgi < ngroups; bgi++) {
  1147. group_no++;
  1148. if (group_no >= ngroups)
  1149. group_no = 0;
  1150. gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
  1151. if (!gdp)
  1152. goto io_error;
  1153. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1154. /*
  1155. * skip this group if the number of
  1156. * free blocks is less than half of the reservation
  1157. * window size.
  1158. */
  1159. if (free_blocks <= (windowsz/2))
  1160. continue;
  1161. brelse(bitmap_bh);
  1162. bitmap_bh = read_block_bitmap(sb, group_no);
  1163. if (!bitmap_bh)
  1164. goto io_error;
  1165. /*
  1166. * try to allocate block(s) from this group, without a goal(-1).
  1167. */
  1168. grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
  1169. bitmap_bh, -1, my_rsv, &num);
  1170. if (grp_alloc_blk >= 0)
  1171. goto allocated;
  1172. }
  1173. /*
  1174. * We may end up a bogus ealier ENOSPC error due to
  1175. * filesystem is "full" of reservations, but
  1176. * there maybe indeed free blocks avaliable on disk
  1177. * In this case, we just forget about the reservations
  1178. * just do block allocation as without reservations.
  1179. */
  1180. if (my_rsv) {
  1181. my_rsv = NULL;
  1182. windowsz = 0;
  1183. group_no = goal_group;
  1184. goto retry_alloc;
  1185. }
  1186. /* No space left on the device */
  1187. *errp = -ENOSPC;
  1188. goto out;
  1189. allocated:
  1190. ext2_debug("using block group %d(%d)\n",
  1191. group_no, gdp->bg_free_blocks_count);
  1192. ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
  1193. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
  1194. in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
  1195. in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
  1196. EXT2_SB(sb)->s_itb_per_group) ||
  1197. in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1198. EXT2_SB(sb)->s_itb_per_group)) {
  1199. ext2_error(sb, "ext2_new_blocks",
  1200. "Allocating block in system zone - "
  1201. "blocks from "E2FSBLK", length %lu",
  1202. ret_block, num);
  1203. goto out;
  1204. }
  1205. performed_allocation = 1;
  1206. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1207. ext2_error(sb, "ext2_new_blocks",
  1208. "block("E2FSBLK") >= blocks count(%d) - "
  1209. "block_group = %d, es == %p ", ret_block,
  1210. le32_to_cpu(es->s_blocks_count), group_no, es);
  1211. goto out;
  1212. }
  1213. group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
  1214. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1215. mark_buffer_dirty(bitmap_bh);
  1216. if (sb->s_flags & MS_SYNCHRONOUS)
  1217. sync_dirty_buffer(bitmap_bh);
  1218. *errp = 0;
  1219. brelse(bitmap_bh);
  1220. DQUOT_FREE_BLOCK(inode, *count-num);
  1221. *count = num;
  1222. return ret_block;
  1223. io_error:
  1224. *errp = -EIO;
  1225. out:
  1226. /*
  1227. * Undo the block allocation
  1228. */
  1229. if (!performed_allocation)
  1230. DQUOT_FREE_BLOCK(inode, *count);
  1231. brelse(bitmap_bh);
  1232. return 0;
  1233. }
  1234. ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp)
  1235. {
  1236. unsigned long count = 1;
  1237. return ext2_new_blocks(inode, goal, &count, errp);
  1238. }
  1239. #ifdef EXT2FS_DEBUG
  1240. static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
  1241. unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
  1242. {
  1243. unsigned int i;
  1244. unsigned long sum = 0;
  1245. if (!map)
  1246. return (0);
  1247. for (i = 0; i < numchars; i++)
  1248. sum += nibblemap[map->b_data[i] & 0xf] +
  1249. nibblemap[(map->b_data[i] >> 4) & 0xf];
  1250. return (sum);
  1251. }
  1252. #endif /* EXT2FS_DEBUG */
  1253. unsigned long ext2_count_free_blocks (struct super_block * sb)
  1254. {
  1255. struct ext2_group_desc * desc;
  1256. unsigned long desc_count = 0;
  1257. int i;
  1258. #ifdef EXT2FS_DEBUG
  1259. unsigned long bitmap_count, x;
  1260. struct ext2_super_block *es;
  1261. es = EXT2_SB(sb)->s_es;
  1262. desc_count = 0;
  1263. bitmap_count = 0;
  1264. desc = NULL;
  1265. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  1266. struct buffer_head *bitmap_bh;
  1267. desc = ext2_get_group_desc (sb, i, NULL);
  1268. if (!desc)
  1269. continue;
  1270. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  1271. bitmap_bh = read_block_bitmap(sb, i);
  1272. if (!bitmap_bh)
  1273. continue;
  1274. x = ext2_count_free(bitmap_bh, sb->s_blocksize);
  1275. printk ("group %d: stored = %d, counted = %lu\n",
  1276. i, le16_to_cpu(desc->bg_free_blocks_count), x);
  1277. bitmap_count += x;
  1278. brelse(bitmap_bh);
  1279. }
  1280. printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
  1281. (long)le32_to_cpu(es->s_free_blocks_count),
  1282. desc_count, bitmap_count);
  1283. return bitmap_count;
  1284. #else
  1285. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  1286. desc = ext2_get_group_desc (sb, i, NULL);
  1287. if (!desc)
  1288. continue;
  1289. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  1290. }
  1291. return desc_count;
  1292. #endif
  1293. }
  1294. static inline int test_root(int a, int b)
  1295. {
  1296. int num = b;
  1297. while (a > num)
  1298. num *= b;
  1299. return num == a;
  1300. }
  1301. static int ext2_group_sparse(int group)
  1302. {
  1303. if (group <= 1)
  1304. return 1;
  1305. return (test_root(group, 3) || test_root(group, 5) ||
  1306. test_root(group, 7));
  1307. }
  1308. /**
  1309. * ext2_bg_has_super - number of blocks used by the superblock in group
  1310. * @sb: superblock for filesystem
  1311. * @group: group number to check
  1312. *
  1313. * Return the number of blocks used by the superblock (primary or backup)
  1314. * in this group. Currently this will be only 0 or 1.
  1315. */
  1316. int ext2_bg_has_super(struct super_block *sb, int group)
  1317. {
  1318. if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
  1319. !ext2_group_sparse(group))
  1320. return 0;
  1321. return 1;
  1322. }
  1323. /**
  1324. * ext2_bg_num_gdb - number of blocks used by the group table in group
  1325. * @sb: superblock for filesystem
  1326. * @group: group number to check
  1327. *
  1328. * Return the number of blocks used by the group descriptor table
  1329. * (primary or backup) in this group. In the future there may be a
  1330. * different number of descriptor blocks in each group.
  1331. */
  1332. unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
  1333. {
  1334. if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
  1335. !ext2_group_sparse(group))
  1336. return 0;
  1337. return EXT2_SB(sb)->s_gdb_count;
  1338. }