balloc.c 45 KB

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