balloc.c 45 KB

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