balloc.c 44 KB

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