balloc.c 45 KB

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