balloc.c 44 KB

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