PageRenderTime 59ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ext3/balloc.c

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