balloc.c 45 KB

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