History

Merge tag 'for-5.8-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux

Pull btrfs updates from David Sterba: "Highlights: - speedup dead root detection during orphan cleanup, eg. when there are many deleted subvolumes waiting to be cleaned, the trees are now looked up in radix tree instead of a O(N^2) search - snapshot creation with inherited qgroup will mark the qgroup inconsistent, requires a rescan - send will emit file capabilities after chown, this produces a stream that does not need postprocessing to set the capabilities again - direct io ported to iomap infrastructure, cleaned up and simplified code, notably removing last use of struct buffer_head in btrfs code Core changes: - factor out backreference iteration, to be used by ordinary backreferences and relocation code - improved global block reserve utilization * better logic to serialize requests * increased maximum available for unlink * improved handling on large pages (64K) - direct io cleanups and fixes * simplify layering, where cloned bios were unnecessarily created for some cases * error handling fixes (submit, endio) * remove repair worker thread, used to avoid deadlocks during repair - refactored block group reading code, preparatory work for new type of block group storage that should improve mount time on large filesystems Cleanups: - cleaned up (and slightly sped up) set/get helpers for metadata data structure members - root bit REF_COWS got renamed to SHAREABLE to reflect the that the blocks of the tree get shared either among subvolumes or with the relocation trees Fixes: - when subvolume deletion fails due to ENOSPC, the filesystem is not turned read-only - device scan deals with devices from other filesystems that changed ownership due to overwrite (mkfs) - fix a race between scrub and block group removal/allocation - fix long standing bug of a runaway balance operation, printing the same line to the syslog, caused by a stale status bit on a reloc tree that prevented progress - fix corrupt log due to concurrent fsync of inodes with shared extents - fix space underflow for NODATACOW and buffered writes when it for some reason needs to fallback to COW mode" * tag 'for-5.8-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: (133 commits) btrfs: fix space_info bytes_may_use underflow during space cache writeout btrfs: fix space_info bytes_may_use underflow after nocow buffered write btrfs: fix wrong file range cleanup after an error filling dealloc range btrfs: remove redundant local variable in read_block_for_search btrfs: open code key_search btrfs: split btrfs_direct_IO to read and write part btrfs: remove BTRFS_INODE_READDIO_NEED_LOCK fs: remove dio_end_io() btrfs: switch to iomap_dio_rw() for dio iomap: remove lockdep_assert_held() iomap: add a filesystem hook for direct I/O bio submission fs: export generic_file_buffered_read() btrfs: turn space cache writeout failure messages into debug messages btrfs: include error on messages about failure to write space/inode caches btrfs: remove useless 'fail_unlock' label from btrfs_csum_file_blocks() btrfs: do not ignore error from btrfs_next_leaf() when inserting checksums btrfs: make checksum item extension more efficient btrfs: fix corrupt log due to concurrent fsync of inodes with shared extents btrfs: unexport btrfs_compress_set_level() btrfs: simplify iget helpers ...

Linus Torvalds committed 6 years ago

Commit f3cdc8a2 parents 8eeae5b 2166e5e52 files · +3224 −3045Browse files

Unified · Split

52 files
  1. .clang-format +1 −0
  2. Documentation/block/biovecs.rst +2 −0
  3. fs/btrfs/Kconfig +1 −0
  4. fs/btrfs/backref.c +831 −6
  5. fs/btrfs/backref.h +297 −0
  6. fs/btrfs/block-group.c +154 −79
  7. fs/btrfs/block-group.h +13 −1
  8. fs/btrfs/block-rsv.c +4 −1
  9. fs/btrfs/btrfs_inode.h +8 −36
  10. fs/btrfs/compression.c +17 −19
  11. fs/btrfs/compression.h +0 −2
  12. fs/btrfs/ctree.c +76 −104
  13. fs/btrfs/ctree.h +66 −55
  14. fs/btrfs/disk-io.c +51 −42
  15. fs/btrfs/disk-io.h +1 −3
  16. fs/btrfs/export.c +3 −14
  17. fs/btrfs/extent-io-tree.h +1 −0
  18. fs/btrfs/extent-tree.c +3 −20
  19. fs/btrfs/extent_io.c +132 −156
  20. fs/btrfs/extent_io.h +35 −32
  21. fs/btrfs/file-item.c +35 −27
  22. fs/btrfs/file.c +94 −17
  23. fs/btrfs/free-space-cache.c +17 −64
  24. fs/btrfs/inode.c +452 −867
  25. fs/btrfs/ioctl.c +49 −53
  26. fs/btrfs/locking.c +1 −0
  27. fs/btrfs/misc.h +54 −0
  28. fs/btrfs/props.c +2 −7
  29. fs/btrfs/qgroup.c +14 −0
  30. fs/btrfs/relocation.c +288 −1031
  31. fs/btrfs/root-tree.c +5 −7
  32. fs/btrfs/scrub.c +45 −14
  33. fs/btrfs/send.c +72 −17
  34. fs/btrfs/space-info.c +73 −8
  35. fs/btrfs/space-info.h +1 −0
  36. fs/btrfs/struct-funcs.c +112 −111
  37. fs/btrfs/super.c +20 −18
  38. fs/btrfs/transaction.c +32 −46
  39. fs/btrfs/transaction.h +1 −2
  40. fs/btrfs/tree-checker.c +0 −4
  41. fs/btrfs/tree-defrag.c +1 −1
  42. fs/btrfs/tree-log.c +90 −102
  43. fs/btrfs/uuid-tree.c +1 −5
  44. fs/btrfs/volumes.c +44 −36
  45. fs/direct-io.c +0 −19
  46. fs/iomap/direct-io.c +10 −7
  47. include/linux/bio.h +8 −0
  48. include/linux/fs.h +2 −2
  49. include/linux/iomap.h +2 −0
  50. include/trace/events/btrfs.h +1 −0
  51. include/uapi/linux/btrfs_tree.h +0 −9
  52. mm/filemap.c +2 −1
.clang-format+1−0
@@ -80,6 +80,7 @@ ForEachMacros: · 79 unmodified lines
80 - 'ax25_uid_for_each'80 - 'ax25_uid_for_each'
81 - '__bio_for_each_bvec'81 - '__bio_for_each_bvec'
82 - 'bio_for_each_bvec'82 - 'bio_for_each_bvec'
83 - 'bio_for_each_bvec_all'
83 - 'bio_for_each_integrity_vec'84 - 'bio_for_each_integrity_vec'
84 - '__bio_for_each_segment'85 - '__bio_for_each_segment'
85 - 'bio_for_each_segment'86 - 'bio_for_each_segment'
Documentation/block/biovecs.rst+2−0
@@ -129,6 +129,7 @@ Usage of helpers: · 128 unmodified lines
129::129::
130 130
131 bio_for_each_segment_all()131 bio_for_each_segment_all()
132 bio_for_each_bvec_all()
132 bio_first_bvec_all()133 bio_first_bvec_all()
133 bio_first_page_all()134 bio_first_page_all()
134 bio_last_bvec_all()135 bio_last_bvec_all()
@@ -143,4 +144,5 @@ Usage of helpers: · 8 unmodified lines
143 bio_vec' will contain a multi-page IO vector during the iteration::144 bio_vec' will contain a multi-page IO vector during the iteration::
144 145
145 bio_for_each_bvec()146 bio_for_each_bvec()
147 bio_for_each_bvec_all()
146 rq_for_each_bvec()148 rq_for_each_bvec()
fs/btrfs/Kconfig+1−0
@@ -14,6 +14,7 @@ config BTRFS_FS · 13 unmodified lines
14 select LZO_DECOMPRESS14 select LZO_DECOMPRESS
15 select ZSTD_COMPRESS15 select ZSTD_COMPRESS
16 select ZSTD_DECOMPRESS16 select ZSTD_DECOMPRESS
17 select FS_IOMAP
17 select RAID6_PQ18 select RAID6_PQ
18 select XOR_BLOCKS19 select XOR_BLOCKS
19 select SRCU20 select SRCU
fs/btrfs/backref.c+831−6
@@ -13,6 +13,7 @@ · 12 unmodified lines
13#include "transaction.h"13#include "transaction.h"
14#include "delayed-ref.h"14#include "delayed-ref.h"
15#include "locking.h"15#include "locking.h"
16#include "misc.h"
16 17
17/* Just an arbitrary number so we can be sure this happened */18/* Just an arbitrary number so we can be sure this happened */
18#define BACKREF_FOUND_SHARED 619#define BACKREF_FOUND_SHARED 6
@@ -537,18 +538,13 @@ static int resolve_indirect_ref(struct btrfs_fs_info *fs_info, · 518 unmodified lines
537 const u64 *extent_item_pos, bool ignore_offset)538 const u64 *extent_item_pos, bool ignore_offset)
538{539{
539 struct btrfs_root *root;540 struct btrfs_root *root;
540 struct btrfs_key root_key;
541 struct extent_buffer *eb;541 struct extent_buffer *eb;
542 int ret = 0;542 int ret = 0;
543 int root_level;543 int root_level;
544 int level = ref->level;544 int level = ref->level;
545 struct btrfs_key search_key = ref->key_for_search;545 struct btrfs_key search_key = ref->key_for_search;
546 546
547 root_key.objectid = ref->root_id;547 root = btrfs_get_fs_root(fs_info, ref->root_id, false);
548 root_key.type = BTRFS_ROOT_ITEM_KEY;
549 root_key.offset = (u64)-1;
550
551 root = btrfs_get_fs_root(fs_info, &root_key, false);
552 if (IS_ERR(root)) {548 if (IS_ERR(root)) {
553 ret = PTR_ERR(root);549 ret = PTR_ERR(root);
554 goto out_free;550 goto out_free;
@@ -2295,3 +2291,832 @@ void free_ipath(struct inode_fs_paths *ipath) · 1740 unmodified lines
2295 kvfree(ipath->fspath);2291 kvfree(ipath->fspath);
2296 kfree(ipath);2292 kfree(ipath);
2297}2293}
2294
2295struct btrfs_backref_iter *btrfs_backref_iter_alloc(
2296 struct btrfs_fs_info *fs_info, gfp_t gfp_flag)
2297{
2298 struct btrfs_backref_iter *ret;
2299
2300 ret = kzalloc(sizeof(*ret), gfp_flag);
2301 if (!ret)
2302 return NULL;
2303
2304 ret->path = btrfs_alloc_path();
2305 if (!ret) {
2306 kfree(ret);
2307 return NULL;
2308 }
2309
2310 /* Current backref iterator only supports iteration in commit root */
2311 ret->path->search_commit_root = 1;
2312 ret->path->skip_locking = 1;
2313 ret->fs_info = fs_info;
2314
2315 return ret;
2316}
2317
2318int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr)
2319{
2320 struct btrfs_fs_info *fs_info = iter->fs_info;
2321 struct btrfs_path *path = iter->path;
2322 struct btrfs_extent_item *ei;
2323 struct btrfs_key key;
2324 int ret;
2325
2326 key.objectid = bytenr;
2327 key.type = BTRFS_METADATA_ITEM_KEY;
2328 key.offset = (u64)-1;
2329 iter->bytenr = bytenr;
2330
2331 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
2332 if (ret < 0)
2333 return ret;
2334 if (ret == 0) {
2335 ret = -EUCLEAN;
2336 goto release;
2337 }
2338 if (path->slots[0] == 0) {
2339 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
2340 ret = -EUCLEAN;
2341 goto release;
2342 }
2343 path->slots[0]--;
2344
2345 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2346 if ((key.type != BTRFS_EXTENT_ITEM_KEY &&
2347 key.type != BTRFS_METADATA_ITEM_KEY) || key.objectid != bytenr) {
2348 ret = -ENOENT;
2349 goto release;
2350 }
2351 memcpy(&iter->cur_key, &key, sizeof(key));
2352 iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
2353 path->slots[0]);
2354 iter->end_ptr = (u32)(iter->item_ptr +
2355 btrfs_item_size_nr(path->nodes[0], path->slots[0]));
2356 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2357 struct btrfs_extent_item);
2358
2359 /*
2360 * Only support iteration on tree backref yet.
2361 *
2362 * This is an extra precaution for non skinny-metadata, where
2363 * EXTENT_ITEM is also used for tree blocks, that we can only use
2364 * extent flags to determine if it's a tree block.
2365 */
2366 if (btrfs_extent_flags(path->nodes[0], ei) & BTRFS_EXTENT_FLAG_DATA) {
2367 ret = -ENOTSUPP;
2368 goto release;
2369 }
2370 iter->cur_ptr = (u32)(iter->item_ptr + sizeof(*ei));
2371
2372 /* If there is no inline backref, go search for keyed backref */
2373 if (iter->cur_ptr >= iter->end_ptr) {
2374 ret = btrfs_next_item(fs_info->extent_root, path);
2375
2376 /* No inline nor keyed ref */
2377 if (ret > 0) {
2378 ret = -ENOENT;
2379 goto release;
2380 }
2381 if (ret < 0)
2382 goto release;
2383
2384 btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key,
2385 path->slots[0]);
2386 if (iter->cur_key.objectid != bytenr ||
2387 (iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
2388 iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY)) {
2389 ret = -ENOENT;
2390 goto release;
2391 }
2392 iter->cur_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
2393 path->slots[0]);
2394 iter->item_ptr = iter->cur_ptr;
2395 iter->end_ptr = (u32)(iter->item_ptr + btrfs_item_size_nr(
2396 path->nodes[0], path->slots[0]));
2397 }
2398
2399 return 0;
2400release:
2401 btrfs_backref_iter_release(iter);
2402 return ret;
2403}
2404
2405/*
2406 * Go to the next backref item of current bytenr, can be either inlined or
2407 * keyed.
2408 *
2409 * Caller needs to check whether it's inline ref or not by iter->cur_key.
2410 *
2411 * Return 0 if we get next backref without problem.
2412 * Return >0 if there is no extra backref for this bytenr.
2413 * Return <0 if there is something wrong happened.
2414 */
2415int btrfs_backref_iter_next(struct btrfs_backref_iter *iter)
2416{
2417 struct extent_buffer *eb = btrfs_backref_get_eb(iter);
2418 struct btrfs_path *path = iter->path;
2419 struct btrfs_extent_inline_ref *iref;
2420 int ret;
2421 u32 size;
2422
2423 if (btrfs_backref_iter_is_inline_ref(iter)) {
2424 /* We're still inside the inline refs */
2425 ASSERT(iter->cur_ptr < iter->end_ptr);
2426
2427 if (btrfs_backref_has_tree_block_info(iter)) {
2428 /* First tree block info */
2429 size = sizeof(struct btrfs_tree_block_info);
2430 } else {
2431 /* Use inline ref type to determine the size */
2432 int type;
2433
2434 iref = (struct btrfs_extent_inline_ref *)
2435 ((unsigned long)iter->cur_ptr);
2436 type = btrfs_extent_inline_ref_type(eb, iref);
2437
2438 size = btrfs_extent_inline_ref_size(type);
2439 }
2440 iter->cur_ptr += size;
2441 if (iter->cur_ptr < iter->end_ptr)
2442 return 0;
2443
2444 /* All inline items iterated, fall through */
2445 }
2446
2447 /* We're at keyed items, there is no inline item, go to the next one */
2448 ret = btrfs_next_item(iter->fs_info->extent_root, iter->path);
2449 if (ret)
2450 return ret;
2451
2452 btrfs_item_key_to_cpu(path->nodes[0], &iter->cur_key, path->slots[0]);
2453 if (iter->cur_key.objectid != iter->bytenr ||
2454 (iter->cur_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2455 iter->cur_key.type != BTRFS_SHARED_BLOCK_REF_KEY))
2456 return 1;
2457 iter->item_ptr = (u32)btrfs_item_ptr_offset(path->nodes[0],
2458 path->slots[0]);
2459 iter->cur_ptr = iter->item_ptr;
2460 iter->end_ptr = iter->item_ptr + (u32)btrfs_item_size_nr(path->nodes[0],
2461 path->slots[0]);
2462 return 0;
2463}
2464
2465void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
2466 struct btrfs_backref_cache *cache, int is_reloc)
2467{
2468 int i;
2469
2470 cache->rb_root = RB_ROOT;
2471 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
2472 INIT_LIST_HEAD(&cache->pending[i]);
2473 INIT_LIST_HEAD(&cache->changed);
2474 INIT_LIST_HEAD(&cache->detached);
2475 INIT_LIST_HEAD(&cache->leaves);
2476 INIT_LIST_HEAD(&cache->pending_edge);
2477 INIT_LIST_HEAD(&cache->useless_node);
2478 cache->fs_info = fs_info;
2479 cache->is_reloc = is_reloc;
2480}
2481
2482struct btrfs_backref_node *btrfs_backref_alloc_node(
2483 struct btrfs_backref_cache *cache, u64 bytenr, int level)
2484{
2485 struct btrfs_backref_node *node;
2486
2487 ASSERT(level >= 0 && level < BTRFS_MAX_LEVEL);
2488 node = kzalloc(sizeof(*node), GFP_NOFS);
2489 if (!node)
2490 return node;
2491
2492 INIT_LIST_HEAD(&node->list);
2493 INIT_LIST_HEAD(&node->upper);
2494 INIT_LIST_HEAD(&node->lower);
2495 RB_CLEAR_NODE(&node->rb_node);
2496 cache->nr_nodes++;
2497 node->level = level;
2498 node->bytenr = bytenr;
2499
2500 return node;
2501}
2502
2503struct btrfs_backref_edge *btrfs_backref_alloc_edge(
2504 struct btrfs_backref_cache *cache)
2505{
2506 struct btrfs_backref_edge *edge;
2507
2508 edge = kzalloc(sizeof(*edge), GFP_NOFS);
2509 if (edge)
2510 cache->nr_edges++;
2511 return edge;
2512}
2513
2514/*
2515 * Drop the backref node from cache, also cleaning up all its
2516 * upper edges and any uncached nodes in the path.
2517 *
2518 * This cleanup happens bottom up, thus the node should either
2519 * be the lowest node in the cache or a detached node.
2520 */
2521void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
2522 struct btrfs_backref_node *node)
2523{
2524 struct btrfs_backref_node *upper;
2525 struct btrfs_backref_edge *edge;
2526
2527 if (!node)
2528 return;
2529
2530 BUG_ON(!node->lowest && !node->detached);
2531 while (!list_empty(&node->upper)) {
2532 edge = list_entry(node->upper.next, struct btrfs_backref_edge,
2533 list[LOWER]);
2534 upper = edge->node[UPPER];
2535 list_del(&edge->list[LOWER]);
2536 list_del(&edge->list[UPPER]);
2537 btrfs_backref_free_edge(cache, edge);
2538
2539 if (RB_EMPTY_NODE(&upper->rb_node)) {
2540 BUG_ON(!list_empty(&node->upper));
2541 btrfs_backref_drop_node(cache, node);
2542 node = upper;
2543 node->lowest = 1;
2544 continue;
2545 }
2546 /*
2547 * Add the node to leaf node list if no other child block
2548 * cached.
2549 */
2550 if (list_empty(&upper->lower)) {
2551 list_add_tail(&upper->lower, &cache->leaves);
2552 upper->lowest = 1;
2553 }
2554 }
2555
2556 btrfs_backref_drop_node(cache, node);
2557}
2558
2559/*
2560 * Release all nodes/edges from current cache
2561 */
2562void btrfs_backref_release_cache(struct btrfs_backref_cache *cache)
2563{
2564 struct btrfs_backref_node *node;
2565 int i;
2566
2567 while (!list_empty(&cache->detached)) {
2568 node = list_entry(cache->detached.next,
2569 struct btrfs_backref_node, list);
2570 btrfs_backref_cleanup_node(cache, node);
2571 }
2572
2573 while (!list_empty(&cache->leaves)) {
2574 node = list_entry(cache->leaves.next,
2575 struct btrfs_backref_node, lower);
2576 btrfs_backref_cleanup_node(cache, node);
2577 }
2578
2579 cache->last_trans = 0;
2580
2581 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
2582 ASSERT(list_empty(&cache->pending[i]));
2583 ASSERT(list_empty(&cache->pending_edge));
2584 ASSERT(list_empty(&cache->useless_node));
2585 ASSERT(list_empty(&cache->changed));
2586 ASSERT(list_empty(&cache->detached));
2587 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
2588 ASSERT(!cache->nr_nodes);
2589 ASSERT(!cache->nr_edges);
2590}
2591
2592/*
2593 * Handle direct tree backref
2594 *
2595 * Direct tree backref means, the backref item shows its parent bytenr
2596 * directly. This is for SHARED_BLOCK_REF backref (keyed or inlined).
2597 *
2598 * @ref_key: The converted backref key.
2599 * For keyed backref, it's the item key.
2600 * For inlined backref, objectid is the bytenr,
2601 * type is btrfs_inline_ref_type, offset is
2602 * btrfs_inline_ref_offset.
2603 */
2604static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,
2605 struct btrfs_key *ref_key,
2606 struct btrfs_backref_node *cur)
2607{
2608 struct btrfs_backref_edge *edge;
2609 struct btrfs_backref_node *upper;
2610 struct rb_node *rb_node;
2611
2612 ASSERT(ref_key->type == BTRFS_SHARED_BLOCK_REF_KEY);
2613
2614 /* Only reloc root uses backref pointing to itself */
2615 if (ref_key->objectid == ref_key->offset) {
2616 struct btrfs_root *root;
2617
2618 cur->is_reloc_root = 1;
2619 /* Only reloc backref cache cares about a specific root */
2620 if (cache->is_reloc) {
2621 root = find_reloc_root(cache->fs_info, cur->bytenr);
2622 if (WARN_ON(!root))
2623 return -ENOENT;
2624 cur->root = root;
2625 } else {
2626 /*
2627 * For generic purpose backref cache, reloc root node
2628 * is useless.
2629 */
2630 list_add(&cur->list, &cache->useless_node);
2631 }
2632 return 0;
2633 }
2634
2635 edge = btrfs_backref_alloc_edge(cache);
2636 if (!edge)
2637 return -ENOMEM;
2638
2639 rb_node = rb_simple_search(&cache->rb_root, ref_key->offset);
2640 if (!rb_node) {
2641 /* Parent node not yet cached */
2642 upper = btrfs_backref_alloc_node(cache, ref_key->offset,
2643 cur->level + 1);
2644 if (!upper) {
2645 btrfs_backref_free_edge(cache, edge);
2646 return -ENOMEM;
2647 }
2648
2649 /*
2650 * Backrefs for the upper level block isn't cached, add the
2651 * block to pending list
2652 */
2653 list_add_tail(&edge->list[UPPER], &cache->pending_edge);
2654 } else {
2655 /* Parent node already cached */
2656 upper = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
2657 ASSERT(upper->checked);
2658 INIT_LIST_HEAD(&edge->list[UPPER]);
2659 }
2660 btrfs_backref_link_edge(edge, cur, upper, LINK_LOWER);
2661 return 0;
2662}
2663
2664/*
2665 * Handle indirect tree backref
2666 *
2667 * Indirect tree backref means, we only know which tree the node belongs to.
2668 * We still need to do a tree search to find out the parents. This is for
2669 * TREE_BLOCK_REF backref (keyed or inlined).
2670 *
2671 * @ref_key: The same as @ref_key in handle_direct_tree_backref()
2672 * @tree_key: The first key of this tree block.
2673 * @path: A clean (released) path, to avoid allocating path everytime
2674 * the function get called.
2675 */
2676static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache,
2677 struct btrfs_path *path,
2678 struct btrfs_key *ref_key,
2679 struct btrfs_key *tree_key,
2680 struct btrfs_backref_node *cur)
2681{
2682 struct btrfs_fs_info *fs_info = cache->fs_info;
2683 struct btrfs_backref_node *upper;
2684 struct btrfs_backref_node *lower;
2685 struct btrfs_backref_edge *edge;
2686 struct extent_buffer *eb;
2687 struct btrfs_root *root;
2688 struct rb_node *rb_node;
2689 int level;
2690 bool need_check = true;
2691 int ret;
2692
2693 root = btrfs_get_fs_root(fs_info, ref_key->offset, false);
2694 if (IS_ERR(root))
2695 return PTR_ERR(root);
2696 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2697 cur->cowonly = 1;
2698
2699 if (btrfs_root_level(&root->root_item) == cur->level) {
2700 /* Tree root */
2701 ASSERT(btrfs_root_bytenr(&root->root_item) == cur->bytenr);
2702 /*
2703 * For reloc backref cache, we may ignore reloc root. But for
2704 * general purpose backref cache, we can't rely on
2705 * btrfs_should_ignore_reloc_root() as it may conflict with
2706 * current running relocation and lead to missing root.
2707 *
2708 * For general purpose backref cache, reloc root detection is
2709 * completely relying on direct backref (key->offset is parent
2710 * bytenr), thus only do such check for reloc cache.
2711 */
2712 if (btrfs_should_ignore_reloc_root(root) && cache->is_reloc) {
2713 btrfs_put_root(root);
2714 list_add(&cur->list, &cache->useless_node);
2715 } else {
2716 cur->root = root;
2717 }
2718 return 0;
2719 }
2720
2721 level = cur->level + 1;
2722
2723 /* Search the tree to find parent blocks referring to the block */
2724 path->search_commit_root = 1;
2725 path->skip_locking = 1;
2726 path->lowest_level = level;
2727 ret = btrfs_search_slot(NULL, root, tree_key, path, 0, 0);
2728 path->lowest_level = 0;
2729 if (ret < 0) {
2730 btrfs_put_root(root);
2731 return ret;
2732 }
2733 if (ret > 0 && path->slots[level] > 0)
2734 path->slots[level]--;
2735
2736 eb = path->nodes[level];
2737 if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) {
2738 btrfs_err(fs_info,
2739"couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
2740 cur->bytenr, level - 1, root->root_key.objectid,
2741 tree_key->objectid, tree_key->type, tree_key->offset);
2742 btrfs_put_root(root);
2743 ret = -ENOENT;
2744 goto out;
2745 }
2746 lower = cur;
2747
2748 /* Add all nodes and edges in the path */
2749 for (; level < BTRFS_MAX_LEVEL; level++) {
2750 if (!path->nodes[level]) {
2751 ASSERT(btrfs_root_bytenr(&root->root_item) ==
2752 lower->bytenr);
2753 /* Same as previous should_ignore_reloc_root() call */
2754 if (btrfs_should_ignore_reloc_root(root) &&
2755 cache->is_reloc) {
2756 btrfs_put_root(root);
2757 list_add(&lower->list, &cache->useless_node);
2758 } else {
2759 lower->root = root;
2760 }
2761 break;
2762 }
2763
2764 edge = btrfs_backref_alloc_edge(cache);
2765 if (!edge) {
2766 btrfs_put_root(root);
2767 ret = -ENOMEM;
2768 goto out;
2769 }
2770
2771 eb = path->nodes[level];
2772 rb_node = rb_simple_search(&cache->rb_root, eb->start);
2773 if (!rb_node) {
2774 upper = btrfs_backref_alloc_node(cache, eb->start,
2775 lower->level + 1);
2776 if (!upper) {
2777 btrfs_put_root(root);
2778 btrfs_backref_free_edge(cache, edge);
2779 ret = -ENOMEM;
2780 goto out;
2781 }
2782 upper->owner = btrfs_header_owner(eb);
2783 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2784 upper->cowonly = 1;
2785
2786 /*
2787 * If we know the block isn't shared we can avoid
2788 * checking its backrefs.
2789 */
2790 if (btrfs_block_can_be_shared(root, eb))
2791 upper->checked = 0;
2792 else
2793 upper->checked = 1;
2794
2795 /*
2796 * Add the block to pending list if we need to check its
2797 * backrefs, we only do this once while walking up a
2798 * tree as we will catch anything else later on.
2799 */
2800 if (!upper->checked && need_check) {
2801 need_check = false;
2802 list_add_tail(&edge->list[UPPER],
2803 &cache->pending_edge);
2804 } else {
2805 if (upper->checked)
2806 need_check = true;
2807 INIT_LIST_HEAD(&edge->list[UPPER]);
2808 }
2809 } else {
2810 upper = rb_entry(rb_node, struct btrfs_backref_node,
2811 rb_node);
2812 ASSERT(upper->checked);
2813 INIT_LIST_HEAD(&edge->list[UPPER]);
2814 if (!upper->owner)
2815 upper->owner = btrfs_header_owner(eb);
2816 }
2817 btrfs_backref_link_edge(edge, lower, upper, LINK_LOWER);
2818
2819 if (rb_node) {
2820 btrfs_put_root(root);
2821 break;
2822 }
2823 lower = upper;
2824 upper = NULL;
2825 }
2826out:
2827 btrfs_release_path(path);
2828 return ret;
2829}
2830
2831/*
2832 * Add backref node @cur into @cache.
2833 *
2834 * NOTE: Even if the function returned 0, @cur is not yet cached as its upper
2835 * links aren't yet bi-directional. Needs to finish such links.
2836 * Use btrfs_backref_finish_upper_links() to finish such linkage.
2837 *
2838 * @path: Released path for indirect tree backref lookup
2839 * @iter: Released backref iter for extent tree search
2840 * @node_key: The first key of the tree block
2841 */
2842int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
2843 struct btrfs_path *path,
2844 struct btrfs_backref_iter *iter,
2845 struct btrfs_key *node_key,
2846 struct btrfs_backref_node *cur)
2847{
2848 struct btrfs_fs_info *fs_info = cache->fs_info;
2849 struct btrfs_backref_edge *edge;
2850 struct btrfs_backref_node *exist;
2851 int ret;
2852
2853 ret = btrfs_backref_iter_start(iter, cur->bytenr);
2854 if (ret < 0)
2855 return ret;
2856 /*
2857 * We skip the first btrfs_tree_block_info, as we don't use the key
2858 * stored in it, but fetch it from the tree block
2859 */
2860 if (btrfs_backref_has_tree_block_info(iter)) {
2861 ret = btrfs_backref_iter_next(iter);
2862 if (ret < 0)
2863 goto out;
2864 /* No extra backref? This means the tree block is corrupted */
2865 if (ret > 0) {
2866 ret = -EUCLEAN;
2867 goto out;
2868 }
2869 }
2870 WARN_ON(cur->checked);
2871 if (!list_empty(&cur->upper)) {
2872 /*
2873 * The backref was added previously when processing backref of
2874 * type BTRFS_TREE_BLOCK_REF_KEY
2875 */
2876 ASSERT(list_is_singular(&cur->upper));
2877 edge = list_entry(cur->upper.next, struct btrfs_backref_edge,
2878 list[LOWER]);
2879 ASSERT(list_empty(&edge->list[UPPER]));
2880 exist = edge->node[UPPER];
2881 /*
2882 * Add the upper level block to pending list if we need check
2883 * its backrefs
2884 */
2885 if (!exist->checked)
2886 list_add_tail(&edge->list[UPPER], &cache->pending_edge);
2887 } else {
2888 exist = NULL;
2889 }
2890
2891 for (; ret == 0; ret = btrfs_backref_iter_next(iter)) {
2892 struct extent_buffer *eb;
2893 struct btrfs_key key;
2894 int type;
2895
2896 cond_resched();
2897 eb = btrfs_backref_get_eb(iter);
2898
2899 key.objectid = iter->bytenr;
2900 if (btrfs_backref_iter_is_inline_ref(iter)) {
2901 struct btrfs_extent_inline_ref *iref;
2902
2903 /* Update key for inline backref */
2904 iref = (struct btrfs_extent_inline_ref *)
2905 ((unsigned long)iter->cur_ptr);
2906 type = btrfs_get_extent_inline_ref_type(eb, iref,
2907 BTRFS_REF_TYPE_BLOCK);
2908 if (type == BTRFS_REF_TYPE_INVALID) {
2909 ret = -EUCLEAN;
2910 goto out;
2911 }
2912 key.type = type;
2913 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
2914 } else {
2915 key.type = iter->cur_key.type;
2916 key.offset = iter->cur_key.offset;
2917 }
2918
2919 /*
2920 * Parent node found and matches current inline ref, no need to
2921 * rebuild this node for this inline ref
2922 */
2923 if (exist &&
2924 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
2925 exist->owner == key.offset) ||
2926 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
2927 exist->bytenr == key.offset))) {
2928 exist = NULL;
2929 continue;
2930 }
2931
2932 /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
2933 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
2934 ret = handle_direct_tree_backref(cache, &key, cur);
2935 if (ret < 0)
2936 goto out;
2937 continue;
2938 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
2939 ret = -EINVAL;
2940 btrfs_print_v0_err(fs_info);
2941 btrfs_handle_fs_error(fs_info, ret, NULL);
2942 goto out;
2943 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
2944 continue;
2945 }
2946
2947 /*
2948 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
2949 * means the root objectid. We need to search the tree to get
2950 * its parent bytenr.
2951 */
2952 ret = handle_indirect_tree_backref(cache, path, &key, node_key,
2953 cur);
2954 if (ret < 0)
2955 goto out;
2956 }
2957 ret = 0;
2958 cur->checked = 1;
2959 WARN_ON(exist);
2960out:
2961 btrfs_backref_iter_release(iter);
2962 return ret;
2963}
2964
2965/*
2966 * Finish the upwards linkage created by btrfs_backref_add_tree_node()
2967 */
2968int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
2969 struct btrfs_backref_node *start)
2970{
2971 struct list_head *useless_node = &cache->useless_node;
2972 struct btrfs_backref_edge *edge;
2973 struct rb_node *rb_node;
2974 LIST_HEAD(pending_edge);
2975
2976 ASSERT(start->checked);
2977
2978 /* Insert this node to cache if it's not COW-only */
2979 if (!start->cowonly) {
2980 rb_node = rb_simple_insert(&cache->rb_root, start->bytenr,
2981 &start->rb_node);
2982 if (rb_node)
2983 btrfs_backref_panic(cache->fs_info, start->bytenr,
2984 -EEXIST);
2985 list_add_tail(&start->lower, &cache->leaves);
2986 }
2987
2988 /*
2989 * Use breadth first search to iterate all related edges.
2990 *
2991 * The starting points are all the edges of this node
2992 */
2993 list_for_each_entry(edge, &start->upper, list[LOWER])
2994 list_add_tail(&edge->list[UPPER], &pending_edge);
2995
2996 while (!list_empty(&pending_edge)) {
2997 struct btrfs_backref_node *upper;
2998 struct btrfs_backref_node *lower;
2999 struct rb_node *rb_node;
3000
3001 edge = list_first_entry(&pending_edge,
3002 struct btrfs_backref_edge, list[UPPER]);
3003 list_del_init(&edge->list[UPPER]);
3004 upper = edge->node[UPPER];
3005 lower = edge->node[LOWER];
3006
3007 /* Parent is detached, no need to keep any edges */
3008 if (upper->detached) {
3009 list_del(&edge->list[LOWER]);
3010 btrfs_backref_free_edge(cache, edge);
3011
3012 /* Lower node is orphan, queue for cleanup */
3013 if (list_empty(&lower->upper))
3014 list_add(&lower->list, useless_node);
3015 continue;
3016 }
3017
3018 /*
3019 * All new nodes added in current build_backref_tree() haven't
3020 * been linked to the cache rb tree.
3021 * So if we have upper->rb_node populated, this means a cache
3022 * hit. We only need to link the edge, as @upper and all its
3023 * parents have already been linked.
3024 */
3025 if (!RB_EMPTY_NODE(&upper->rb_node)) {
3026 if (upper->lowest) {
3027 list_del_init(&upper->lower);
3028 upper->lowest = 0;
3029 }
3030
3031 list_add_tail(&edge->list[UPPER], &upper->lower);
3032 continue;
3033 }
3034
3035 /* Sanity check, we shouldn't have any unchecked nodes */
3036 if (!upper->checked) {
3037 ASSERT(0);
3038 return -EUCLEAN;
3039 }
3040
3041 /* Sanity check, COW-only node has non-COW-only parent */
3042 if (start->cowonly != upper->cowonly) {
3043 ASSERT(0);
3044 return -EUCLEAN;
3045 }
3046
3047 /* Only cache non-COW-only (subvolume trees) tree blocks */
3048 if (!upper->cowonly) {
3049 rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr,
3050 &upper->rb_node);
3051 if (rb_node) {
3052 btrfs_backref_panic(cache->fs_info,
3053 upper->bytenr, -EEXIST);
3054 return -EUCLEAN;
3055 }
3056 }
3057
3058 list_add_tail(&edge->list[UPPER], &upper->lower);
3059
3060 /*
3061 * Also queue all the parent edges of this uncached node
3062 * to finish the upper linkage
3063 */
3064 list_for_each_entry(edge, &upper->upper, list[LOWER])
3065 list_add_tail(&edge->list[UPPER], &pending_edge);
3066 }
3067 return 0;
3068}
3069
3070void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
3071 struct btrfs_backref_node *node)
3072{
3073 struct btrfs_backref_node *lower;
3074 struct btrfs_backref_node *upper;
3075 struct btrfs_backref_edge *edge;
3076
3077 while (!list_empty(&cache->useless_node)) {
3078 lower = list_first_entry(&cache->useless_node,
3079 struct btrfs_backref_node, list);
3080 list_del_init(&lower->list);
3081 }
3082 while (!list_empty(&cache->pending_edge)) {
3083 edge = list_first_entry(&cache->pending_edge,
3084 struct btrfs_backref_edge, list[UPPER]);
3085 list_del(&edge->list[UPPER]);
3086 list_del(&edge->list[LOWER]);
3087 lower = edge->node[LOWER];
3088 upper = edge->node[UPPER];
3089 btrfs_backref_free_edge(cache, edge);
3090
3091 /*
3092 * Lower is no longer linked to any upper backref nodes and
3093 * isn't in the cache, we can free it ourselves.
3094 */
3095 if (list_empty(&lower->upper) &&
3096 RB_EMPTY_NODE(&lower->rb_node))
3097 list_add(&lower->list, &cache->useless_node);
3098
3099 if (!RB_EMPTY_NODE(&upper->rb_node))
3100 continue;
3101
3102 /* Add this guy's upper edges to the list to process */
3103 list_for_each_entry(edge, &upper->upper, list[LOWER])
3104 list_add_tail(&edge->list[UPPER],
3105 &cache->pending_edge);
3106 if (list_empty(&upper->upper))
3107 list_add(&upper->list, &cache->useless_node);
3108 }
3109
3110 while (!list_empty(&cache->useless_node)) {
3111 lower = list_first_entry(&cache->useless_node,
3112 struct btrfs_backref_node, list);
3113 list_del_init(&lower->list);
3114 if (lower == node)
3115 node = NULL;
3116 btrfs_backref_free_node(cache, lower);
3117 }
3118
3119 btrfs_backref_cleanup_node(cache, node);
3120 ASSERT(list_empty(&cache->useless_node) &&
3121 list_empty(&cache->pending_edge));
3122}
fs/btrfs/backref.h+297−0
@@ -8,6 +8,7 @@ · 7 unmodified lines
8 8
9#include <linux/btrfs.h>9#include <linux/btrfs.h>
10#include "ulist.h"10#include "ulist.h"
11#include "disk-io.h"
11#include "extent_io.h"12#include "extent_io.h"
12 13
13struct inode_fs_paths {14struct inode_fs_paths {
@@ -78,4 +79,300 @@ struct prelim_ref { · 64 unmodified lines
78 u64 wanted_disk_byte;79 u64 wanted_disk_byte;
79};80};
80 81
82/*
83 * Iterate backrefs of one extent.
84 *
85 * Now it only supports iteration of tree block in commit root.
86 */
87struct btrfs_backref_iter {
88 u64 bytenr;
89 struct btrfs_path *path;
90 struct btrfs_fs_info *fs_info;
91 struct btrfs_key cur_key;
92 u32 item_ptr;
93 u32 cur_ptr;
94 u32 end_ptr;
95};
96
97struct btrfs_backref_iter *btrfs_backref_iter_alloc(
98 struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
99
100static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
101{
102 if (!iter)
103 return;
104 btrfs_free_path(iter->path);
105 kfree(iter);
106}
107
108static inline struct extent_buffer *btrfs_backref_get_eb(
109 struct btrfs_backref_iter *iter)
110{
111 if (!iter)
112 return NULL;
113 return iter->path->nodes[0];
114}
115
116/*
117 * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
118 * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
119 *
120 * This helper determines if that's the case.
121 */
122static inline bool btrfs_backref_has_tree_block_info(
123 struct btrfs_backref_iter *iter)
124{
125 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
126 iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
127 return true;
128 return false;
129}
130
131int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
132
133int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
134
135static inline bool btrfs_backref_iter_is_inline_ref(
136 struct btrfs_backref_iter *iter)
137{
138 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
139 iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
140 return true;
141 return false;
142}
143
144static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
145{
146 iter->bytenr = 0;
147 iter->item_ptr = 0;
148 iter->cur_ptr = 0;
149 iter->end_ptr = 0;
150 btrfs_release_path(iter->path);
151 memset(&iter->cur_key, 0, sizeof(iter->cur_key));
152}
153
154/*
155 * Backref cache related structures
156 *
157 * The whole objective of backref_cache is to build a bi-directional map
158 * of tree blocks (represented by backref_node) and all their parents.
159 */
160
161/*
162 * Represent a tree block in the backref cache
163 */
164struct btrfs_backref_node {
165 struct {
166 struct rb_node rb_node;
167 u64 bytenr;
168 }; /* Use rb_simple_node for search/insert */
169
170 u64 new_bytenr;
171 /* Objectid of tree block owner, can be not uptodate */
172 u64 owner;
173 /* Link to pending, changed or detached list */
174 struct list_head list;
175
176 /* List of upper level edges, which link this node to its parents */
177 struct list_head upper;
178 /* List of lower level edges, which link this node to its children */
179 struct list_head lower;
180
181 /* NULL if this node is not tree root */
182 struct btrfs_root *root;
183 /* Extent buffer got by COWing the block */
184 struct extent_buffer *eb;
185 /* Level of the tree block */
186 unsigned int level:8;
187 /* Is the block in a non-shareable tree */
188 unsigned int cowonly:1;
189 /* 1 if no child node is in the cache */
190 unsigned int lowest:1;
191 /* Is the extent buffer locked */
192 unsigned int locked:1;
193 /* Has the block been processed */
194 unsigned int processed:1;
195 /* Have backrefs of this block been checked */
196 unsigned int checked:1;
197 /*
198 * 1 if corresponding block has been COWed but some upper level block
199 * pointers may not point to the new location
200 */
201 unsigned int pending:1;
202 /* 1 if the backref node isn't connected to any other backref node */
203 unsigned int detached:1;
204
205 /*
206 * For generic purpose backref cache, where we only care if it's a reloc
207 * root, doesn't care the source subvolid.
208 */
209 unsigned int is_reloc_root:1;
210};
211
212#define LOWER 0
213#define UPPER 1
214
215/*
216 * Represent an edge connecting upper and lower backref nodes.
217 */
218struct btrfs_backref_edge {
219 /*
220 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
221 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
222 * upper level node.
223 *
224 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
225 * linking list[UPPER] to its upper level nodes.
226 */
227 struct list_head list[2];
228
229 /* Two related nodes */
230 struct btrfs_backref_node *node[2];
231};
232
233struct btrfs_backref_cache {
234 /* Red black tree of all backref nodes in the cache */
235 struct rb_root rb_root;
236 /* For passing backref nodes to btrfs_reloc_cow_block */
237 struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
238 /*
239 * List of blocks that have been COWed but some block pointers in upper
240 * level blocks may not reflect the new location
241 */
242 struct list_head pending[BTRFS_MAX_LEVEL];
243 /* List of backref nodes with no child node */
244 struct list_head leaves;
245 /* List of blocks that have been COWed in current transaction */
246 struct list_head changed;
247 /* List of detached backref node. */
248 struct list_head detached;
249
250 u64 last_trans;
251
252 int nr_nodes;
253 int nr_edges;
254
255 /* List of unchecked backref edges during backref cache build */
256 struct list_head pending_edge;
257
258 /* List of useless backref nodes during backref cache build */
259 struct list_head useless_node;
260
261 struct btrfs_fs_info *fs_info;
262
263 /*
264 * Whether this cache is for relocation
265 *
266 * Reloction backref cache require more info for reloc root compared
267 * to generic backref cache.
268 */
269 unsigned int is_reloc;
270};
271
272void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
273 struct btrfs_backref_cache *cache, int is_reloc);
274struct btrfs_backref_node *btrfs_backref_alloc_node(
275 struct btrfs_backref_cache *cache, u64 bytenr, int level);
276struct btrfs_backref_edge *btrfs_backref_alloc_edge(
277 struct btrfs_backref_cache *cache);
278
279#define LINK_LOWER (1 << 0)
280#define LINK_UPPER (1 << 1)
281static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
282 struct btrfs_backref_node *lower,
283 struct btrfs_backref_node *upper,
284 int link_which)
285{
286 ASSERT(upper && lower && upper->level == lower->level + 1);
287 edge->node[LOWER] = lower;
288 edge->node[UPPER] = upper;
289 if (link_which & LINK_LOWER)
290 list_add_tail(&edge->list[LOWER], &lower->upper);
291 if (link_which & LINK_UPPER)
292 list_add_tail(&edge->list[UPPER], &upper->lower);
293}
294
295static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
296 struct btrfs_backref_node *node)
297{
298 if (node) {
299 cache->nr_nodes--;
300 btrfs_put_root(node->root);
301 kfree(node);
302 }
303}
304
305static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
306 struct btrfs_backref_edge *edge)
307{
308 if (edge) {
309 cache->nr_edges--;
310 kfree(edge);
311 }
312}
313
314static inline void btrfs_backref_unlock_node_buffer(
315 struct btrfs_backref_node *node)
316{
317 if (node->locked) {
318 btrfs_tree_unlock(node->eb);
319 node->locked = 0;
320 }
321}
322
323static inline void btrfs_backref_drop_node_buffer(
324 struct btrfs_backref_node *node)
325{
326 if (node->eb) {
327 btrfs_backref_unlock_node_buffer(node);
328 free_extent_buffer(node->eb);
329 node->eb = NULL;
330 }
331}
332
333/*
334 * Drop the backref node from cache without cleaning up its children
335 * edges.
336 *
337 * This can only be called on node without parent edges.
338 * The children edges are still kept as is.
339 */
340static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
341 struct btrfs_backref_node *node)
342{
343 BUG_ON(!list_empty(&node->upper));
344
345 btrfs_backref_drop_node_buffer(node);
346 list_del(&node->list);
347 list_del(&node->lower);
348 if (!RB_EMPTY_NODE(&node->rb_node))
349 rb_erase(&node->rb_node, &tree->rb_root);
350 btrfs_backref_free_node(tree, node);
351}
352
353void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
354 struct btrfs_backref_node *node);
355
356void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
357
358static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
359 u64 bytenr, int errno)
360{
361 btrfs_panic(fs_info, errno,
362 "Inconsistency in backref cache found at offset %llu",
363 bytenr);
364}
365
366int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
367 struct btrfs_path *path,
368 struct btrfs_backref_iter *iter,
369 struct btrfs_key *node_key,
370 struct btrfs_backref_node *cur);
371
372int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
373 struct btrfs_backref_node *start);
374
375void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
376 struct btrfs_backref_node *node);
377
81#endif378#endif
fs/btrfs/block-group.c+154−79
@@ -7,7 +7,6 @@ · 6 unmodified lines
7#include "disk-io.h"7#include "disk-io.h"
8#include "free-space-cache.h"8#include "free-space-cache.h"
9#include "free-space-tree.h"9#include "free-space-tree.h"
10#include "disk-io.h"
11#include "volumes.h"10#include "volumes.h"
12#include "transaction.h"11#include "transaction.h"
13#include "ref-verify.h"12#include "ref-verify.h"
@@ -161,6 +160,8 @@ static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, · 147 unmodified lines
161 struct rb_node *parent = NULL;160 struct rb_node *parent = NULL;
162 struct btrfs_block_group *cache;161 struct btrfs_block_group *cache;
163 162
163 ASSERT(block_group->length != 0);
164
164 spin_lock(&info->block_group_cache_lock);165 spin_lock(&info->block_group_cache_lock);
165 p = &info->block_group_cache_tree.rb_node;166 p = &info->block_group_cache_tree.rb_node;
166 167
@@ -863,11 +864,34 @@ static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) · 696 unmodified lines
863 }864 }
864}865}
865 866
867static int remove_block_group_item(struct btrfs_trans_handle *trans,
868 struct btrfs_path *path,
869 struct btrfs_block_group *block_group)
870{
871 struct btrfs_fs_info *fs_info = trans->fs_info;
872 struct btrfs_root *root;
873 struct btrfs_key key;
874 int ret;
875
876 root = fs_info->extent_root;
877 key.objectid = block_group->start;
878 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
879 key.offset = block_group->length;
880
881 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
882 if (ret > 0)
883 ret = -ENOENT;
884 if (ret < 0)
885 return ret;
886
887 ret = btrfs_del_item(trans, root, path);
888 return ret;
889}
890
866int btrfs_remove_block_group(struct btrfs_trans_handle *trans,891int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
867 u64 group_start, struct extent_map *em)892 u64 group_start, struct extent_map *em)
868{893{
869 struct btrfs_fs_info *fs_info = trans->fs_info;894 struct btrfs_fs_info *fs_info = trans->fs_info;
870 struct btrfs_root *root = fs_info->extent_root;
871 struct btrfs_path *path;895 struct btrfs_path *path;
872 struct btrfs_block_group *block_group;896 struct btrfs_block_group *block_group;
873 struct btrfs_free_cluster *cluster;897 struct btrfs_free_cluster *cluster;
@@ -1065,26 +1089,25 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 191 unmodified lines
1065 1089
1066 spin_unlock(&block_group->space_info->lock);1090 spin_unlock(&block_group->space_info->lock);
1067 1091
1068 key.objectid = block_group->start;
1069 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1070 key.offset = block_group->length;
1071
1072 mutex_lock(&fs_info->chunk_mutex);1092 mutex_lock(&fs_info->chunk_mutex);
1073 spin_lock(&block_group->lock);1093 spin_lock(&block_group->lock);
1074 block_group->removed = 1;1094 block_group->removed = 1;
1075 /*1095 /*
1076 * At this point trimming can't start on this block group, because we1096 * At this point trimming or scrub can't start on this block group,
1077 * removed the block group from the tree fs_info->block_group_cache_tree1097 * because we removed the block group from the rbtree
1078 * so no one can't find it anymore and even if someone already got this1098 * fs_info->block_group_cache_tree so no one can't find it anymore and
1079 * block group before we removed it from the rbtree, they have already1099 * even if someone already got this block group before we removed it
1080 * incremented block_group->trimming - if they didn't, they won't find1100 * from the rbtree, they have already incremented block_group->frozen -
1081 * any free space entries because we already removed them all when we1101 * if they didn't, for the trimming case they won't find any free space
1082 * called btrfs_remove_free_space_cache().1102 * entries because we already removed them all when we called
1103 * btrfs_remove_free_space_cache().
1083 *1104 *
1084 * And we must not remove the extent map from the fs_info->mapping_tree1105 * And we must not remove the extent map from the fs_info->mapping_tree
1085 * to prevent the same logical address range and physical device space1106 * to prevent the same logical address range and physical device space
1086 * ranges from being reused for a new block group. This is because our1107 * ranges from being reused for a new block group. This is needed to
1087 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is1108 * avoid races with trimming and scrub.
1109 *
1110 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1088 * completely transactionless, so while it is trimming a range the1111 * completely transactionless, so while it is trimming a range the
1089 * currently running transaction might finish and a new one start,1112 * currently running transaction might finish and a new one start,
1090 * allowing for new block groups to be created that can reuse the same1113 * allowing for new block groups to be created that can reuse the same
@@ -1095,7 +1118,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 4 unmodified lines
1095 * in place until the extents have been discarded completely when1118 * in place until the extents have been discarded completely when
1096 * the transaction commit has completed.1119 * the transaction commit has completed.
1097 */1120 */
1098 remove_em = (atomic_read(&block_group->trimming) == 0);1121 remove_em = (atomic_read(&block_group->frozen) == 0);
1099 spin_unlock(&block_group->lock);1122 spin_unlock(&block_group->lock);
1100 1123
1101 mutex_unlock(&fs_info->chunk_mutex);1124 mutex_unlock(&fs_info->chunk_mutex);
@@ -1107,16 +1130,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 5 unmodified lines
1107 /* Once for the block groups rbtree */1130 /* Once for the block groups rbtree */
1108 btrfs_put_block_group(block_group);1131 btrfs_put_block_group(block_group);
1109 1132
1110 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);1133 ret = remove_block_group_item(trans, path, block_group);
1111 if (ret > 0)
1112 ret = -EIO;
1113 if (ret < 0)1134 if (ret < 0)
1114 goto out;1135 goto out;
1115 1136
1116 ret = btrfs_del_item(trans, root, path);
1117 if (ret)
1118 goto out;
1119
1120 if (remove_em) {1137 if (remove_em) {
1121 struct extent_map_tree *em_tree;1138 struct extent_map_tree *em_tree;
1122 1139
@@ -1175,7 +1192,7 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 52 unmodified lines
1175 free_extent_map(em);1192 free_extent_map(em);
1176 1193
1177 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,1194 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
1178 num_items, 1);1195 num_items);
1179}1196}
1180 1197
1181/*1198/*
@@ -1284,25 +1301,17 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans, · 102 unmodified lines
1284 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,1301 ret = clear_extent_bits(&prev_trans->pinned_extents, start, end,
1285 EXTENT_DIRTY);1302 EXTENT_DIRTY);
1286 if (ret)1303 if (ret)
1287 goto err;1304 goto out;
1288 }1305 }
1289 1306
1290 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,1307 ret = clear_extent_bits(&trans->transaction->pinned_extents, start, end,
1291 EXTENT_DIRTY);1308 EXTENT_DIRTY);
1292 if (ret)1309out:
1293 goto err;
1294 mutex_unlock(&fs_info->unused_bg_unpin_mutex);1310 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
1295 if (prev_trans)1311 if (prev_trans)
1296 btrfs_put_transaction(prev_trans);1312 btrfs_put_transaction(prev_trans);
1297 1313
1298 return true;1314 return ret == 0;
1299
1300err:
1301 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
1302 if (prev_trans)
1303 btrfs_put_transaction(prev_trans);
1304 btrfs_dec_block_group_ro(bg);
1305 return false;
1306}1315}
1307 1316
1308/*1317/*
@@ -1400,8 +1409,10 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) · 91 unmodified lines
1400 * We could have pending pinned extents for this block group,1409 * We could have pending pinned extents for this block group,
1401 * just delete them, we don't care about them anymore.1410 * just delete them, we don't care about them anymore.
1402 */1411 */
1403 if (!clean_pinned_extents(trans, block_group))1412 if (!clean_pinned_extents(trans, block_group)) {
1413 btrfs_dec_block_group_ro(block_group);
1404 goto end_trans;1414 goto end_trans;
1415 }
1405 1416
1406 /*1417 /*
1407 * At this point, the block_group is read only and should fail1418 * At this point, the block_group is read only and should fail
@@ -1450,7 +1461,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) · 42 unmodified lines
1450 1461
1451 /* Implicit trim during transaction commit. */1462 /* Implicit trim during transaction commit. */
1452 if (trimming)1463 if (trimming)
1453 btrfs_get_block_group_trimming(block_group);1464 btrfs_freeze_block_group(block_group);
1454 1465
1455 /*1466 /*
1456 * Btrfs_remove_chunk will abort the transaction if things go1467 * Btrfs_remove_chunk will abort the transaction if things go
@@ -1460,7 +1471,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) · 3 unmodified lines
1460 1471
1461 if (ret) {1472 if (ret) {
1462 if (trimming)1473 if (trimming)
1463 btrfs_put_block_group_trimming(block_group);1474 btrfs_unfreeze_block_group(block_group);
1464 goto end_trans;1475 goto end_trans;
1465 }1476 }
1466 1477
@@ -1774,7 +1785,7 @@ static void link_block_group(struct btrfs_block_group *cache) · 307 unmodified lines
1774}1785}
1775 1786
1776static struct btrfs_block_group *btrfs_create_block_group_cache(1787static struct btrfs_block_group *btrfs_create_block_group_cache(
1777 struct btrfs_fs_info *fs_info, u64 start, u64 size)1788 struct btrfs_fs_info *fs_info, u64 start)
1778{1789{
1779 struct btrfs_block_group *cache;1790 struct btrfs_block_group *cache;
1780 1791
@@ -1790,7 +1801,6 @@ static struct btrfs_block_group *btrfs_create_block_group_cache( · 9 unmodified lines
1790 }1801 }
1791 1802
1792 cache->start = start;1803 cache->start = start;
1793 cache->length = size;
1794 1804
1795 cache->fs_info = fs_info;1805 cache->fs_info = fs_info;
1796 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);1806 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
@@ -1809,7 +1819,7 @@ static struct btrfs_block_group *btrfs_create_block_group_cache( · 12 unmodified lines
1809 INIT_LIST_HEAD(&cache->dirty_list);1819 INIT_LIST_HEAD(&cache->dirty_list);
1810 INIT_LIST_HEAD(&cache->io_list);1820 INIT_LIST_HEAD(&cache->io_list);
1811 btrfs_init_free_space_ctl(cache);1821 btrfs_init_free_space_ctl(cache);
1812 atomic_set(&cache->trimming, 0);1822 atomic_set(&cache->frozen, 0);
1813 mutex_init(&cache->free_space_lock);1823 mutex_init(&cache->free_space_lock);
1814 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);1824 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1815 1825
@@ -1870,25 +1880,44 @@ static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) · 54 unmodified lines
1870 return ret;1880 return ret;
1871}1881}
1872 1882
1883static int read_block_group_item(struct btrfs_block_group *cache,
1884 struct btrfs_path *path,
1885 const struct btrfs_key *key)
1886{
1887 struct extent_buffer *leaf = path->nodes[0];
1888 struct btrfs_block_group_item bgi;
1889 int slot = path->slots[0];
1890
1891 cache->length = key->offset;
1892
1893 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
1894 sizeof(bgi));
1895 cache->used = btrfs_stack_block_group_used(&bgi);
1896 cache->flags = btrfs_stack_block_group_flags(&bgi);
1897
1898 return 0;
1899}
1900
1873static int read_one_block_group(struct btrfs_fs_info *info,1901static int read_one_block_group(struct btrfs_fs_info *info,
1874 struct btrfs_path *path,1902 struct btrfs_path *path,
1875 const struct btrfs_key *key,1903 const struct btrfs_key *key,
1876 int need_clear)1904 int need_clear)
1877{1905{
1878 struct extent_buffer *leaf = path->nodes[0];
1879 struct btrfs_block_group *cache;1906 struct btrfs_block_group *cache;
1880 struct btrfs_space_info *space_info;1907 struct btrfs_space_info *space_info;
1881 struct btrfs_block_group_item bgi;
1882 const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);1908 const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS);
1883 int slot = path->slots[0];
1884 int ret;1909 int ret;
1885 1910
1886 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);1911 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY);
1887 1912
1888 cache = btrfs_create_block_group_cache(info, key->objectid, key->offset);1913 cache = btrfs_create_block_group_cache(info, key->objectid);
1889 if (!cache)1914 if (!cache)
1890 return -ENOMEM;1915 return -ENOMEM;
1891 1916
1917 ret = read_block_group_item(cache, path, key);
1918 if (ret < 0)
1919 goto error;
1920
1892 if (need_clear) {1921 if (need_clear) {
1893 /*1922 /*
1894 * When we mount with old space cache, we need to1923 * When we mount with old space cache, we need to
@@ -1903,10 +1932,6 @@ static int read_one_block_group(struct btrfs_fs_info *info, · 8 unmodified lines
1903 if (btrfs_test_opt(info, SPACE_CACHE))1932 if (btrfs_test_opt(info, SPACE_CACHE))
1904 cache->disk_cache_state = BTRFS_DC_CLEAR;1933 cache->disk_cache_state = BTRFS_DC_CLEAR;
1905 }1934 }
1906 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
1907 sizeof(bgi));
1908 cache->used = btrfs_stack_block_group_used(&bgi);
1909 cache->flags = btrfs_stack_block_group_flags(&bgi);
1910 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&1935 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
1911 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {1936 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
1912 btrfs_err(info,1937 btrfs_err(info,
@@ -1934,15 +1959,15 @@ static int read_one_block_group(struct btrfs_fs_info *info, · 21 unmodified lines
1934 * are empty, and we can just add all the space in and be done with it.1959 * are empty, and we can just add all the space in and be done with it.
1935 * This saves us _a_lot_ of time, particularly in the full case.1960 * This saves us _a_lot_ of time, particularly in the full case.
1936 */1961 */
1937 if (key->offset == cache->used) {1962 if (cache->length == cache->used) {
1938 cache->last_byte_to_unpin = (u64)-1;1963 cache->last_byte_to_unpin = (u64)-1;
1939 cache->cached = BTRFS_CACHE_FINISHED;1964 cache->cached = BTRFS_CACHE_FINISHED;
1940 btrfs_free_excluded_extents(cache);1965 btrfs_free_excluded_extents(cache);
1941 } else if (cache->used == 0) {1966 } else if (cache->used == 0) {
1942 cache->last_byte_to_unpin = (u64)-1;1967 cache->last_byte_to_unpin = (u64)-1;
1943 cache->cached = BTRFS_CACHE_FINISHED;1968 cache->cached = BTRFS_CACHE_FINISHED;
1944 add_new_free_space(cache, key->objectid,1969 add_new_free_space(cache, cache->start,
1945 key->objectid + key->offset);1970 cache->start + cache->length);
1946 btrfs_free_excluded_extents(cache);1971 btrfs_free_excluded_extents(cache);
1947 }1972 }
1948 1973
@@ -1952,7 +1977,7 @@ static int read_one_block_group(struct btrfs_fs_info *info, · 3 unmodified lines
1952 goto error;1977 goto error;
1953 }1978 }
1954 trace_btrfs_add_block_group(info, cache, 0);1979 trace_btrfs_add_block_group(info, cache, 0);
1955 btrfs_update_space_info(info, cache->flags, key->offset,1980 btrfs_update_space_info(info, cache->flags, cache->length,
1956 cache->used, cache->bytes_super, &space_info);1981 cache->used, cache->bytes_super, &space_info);
1957 1982
1958 cache->space_info = space_info;1983 cache->space_info = space_info;
@@ -1991,7 +2016,6 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) · 32 unmodified lines
1991 path = btrfs_alloc_path();2016 path = btrfs_alloc_path();
1992 if (!path)2017 if (!path)
1993 return -ENOMEM;2018 return -ENOMEM;
1994 path->reada = READA_FORWARD;
1995 2019
1996 cache_gen = btrfs_super_cache_generation(info->super_copy);2020 cache_gen = btrfs_super_cache_generation(info->super_copy);
1997 if (btrfs_test_opt(info, SPACE_CACHE) &&2021 if (btrfs_test_opt(info, SPACE_CACHE) &&
@@ -2046,13 +2070,32 @@ error: · 48 unmodified lines
2046 return ret;2070 return ret;
2047}2071}
2048 2072
2073static int insert_block_group_item(struct btrfs_trans_handle *trans,
2074 struct btrfs_block_group *block_group)
2075{
2076 struct btrfs_fs_info *fs_info = trans->fs_info;
2077 struct btrfs_block_group_item bgi;
2078 struct btrfs_root *root;
2079 struct btrfs_key key;
2080
2081 spin_lock(&block_group->lock);
2082 btrfs_set_stack_block_group_used(&bgi, block_group->used);
2083 btrfs_set_stack_block_group_chunk_objectid(&bgi,
2084 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2085 btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
2086 key.objectid = block_group->start;
2087 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2088 key.offset = block_group->length;
2089 spin_unlock(&block_group->lock);
2090
2091 root = fs_info->extent_root;
2092 return btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
2093}
2094
2049void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)2095void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
2050{2096{
2051 struct btrfs_fs_info *fs_info = trans->fs_info;2097 struct btrfs_fs_info *fs_info = trans->fs_info;
2052 struct btrfs_block_group *block_group;2098 struct btrfs_block_group *block_group;
2053 struct btrfs_root *extent_root = fs_info->extent_root;
2054 struct btrfs_block_group_item item;
2055 struct btrfs_key key;
2056 int ret = 0;2099 int ret = 0;
2057 2100
2058 if (!trans->can_flush_pending_bgs)2101 if (!trans->can_flush_pending_bgs)
@@ -2065,21 +2108,11 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) · 6 unmodified lines
2065 if (ret)2108 if (ret)
2066 goto next;2109 goto next;
2067 2110
2068 spin_lock(&block_group->lock);2111 ret = insert_block_group_item(trans, block_group);
2069 btrfs_set_stack_block_group_used(&item, block_group->used);
2070 btrfs_set_stack_block_group_chunk_objectid(&item,
2071 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
2072 btrfs_set_stack_block_group_flags(&item, block_group->flags);
2073 key.objectid = block_group->start;
2074 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2075 key.offset = block_group->length;
2076 spin_unlock(&block_group->lock);
2077
2078 ret = btrfs_insert_item(trans, extent_root, &key, &item,
2079 sizeof(item));
2080 if (ret)2112 if (ret)
2081 btrfs_abort_transaction(trans, ret);2113 btrfs_abort_transaction(trans, ret);
2082 ret = btrfs_finish_chunk_alloc(trans, key.objectid, key.offset);2114 ret = btrfs_finish_chunk_alloc(trans, block_group->start,
2115 block_group->length);
2083 if (ret)2116 if (ret)
2084 btrfs_abort_transaction(trans, ret);2117 btrfs_abort_transaction(trans, ret);
2085 add_block_group_free_space(trans, block_group);2118 add_block_group_free_space(trans, block_group);
@@ -2100,10 +2133,11 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used, · 14 unmodified lines
2100 2133
2101 btrfs_set_log_full_commit(trans);2134 btrfs_set_log_full_commit(trans);
2102 2135
2103 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);2136 cache = btrfs_create_block_group_cache(fs_info, chunk_offset);
2104 if (!cache)2137 if (!cache)
2105 return -ENOMEM;2138 return -ENOMEM;
2106 2139
2140 cache->length = size;
2107 cache->used = bytes_used;2141 cache->used = bytes_used;
2108 cache->flags = type;2142 cache->flags = type;
2109 cache->last_byte_to_unpin = (u64)-1;2143 cache->last_byte_to_unpin = (u64)-1;
@@ -2314,13 +2348,13 @@ void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) · 204 unmodified lines
2314 spin_unlock(&sinfo->lock);2348 spin_unlock(&sinfo->lock);
2315}2349}
2316 2350
2317static int write_one_cache_group(struct btrfs_trans_handle *trans,2351static int update_block_group_item(struct btrfs_trans_handle *trans,
2318 struct btrfs_path *path,2352 struct btrfs_path *path,
2319 struct btrfs_block_group *cache)2353 struct btrfs_block_group *cache)
2320{2354{
2321 struct btrfs_fs_info *fs_info = trans->fs_info;2355 struct btrfs_fs_info *fs_info = trans->fs_info;
2322 int ret;2356 int ret;
2323 struct btrfs_root *extent_root = fs_info->extent_root;2357 struct btrfs_root *root = fs_info->extent_root;
2324 unsigned long bi;2358 unsigned long bi;
2325 struct extent_buffer *leaf;2359 struct extent_buffer *leaf;
2326 struct btrfs_block_group_item bgi;2360 struct btrfs_block_group_item bgi;
@@ -2330,7 +2364,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans, · 3 unmodified lines
2330 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;2364 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
2331 key.offset = cache->length;2365 key.offset = cache->length;
2332 2366
2333 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);2367 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2334 if (ret) {2368 if (ret) {
2335 if (ret > 0)2369 if (ret > 0)
2336 ret = -ENOENT;2370 ret = -ENOENT;
@@ -2642,7 +2676,7 @@ again: · 305 unmodified lines
2642 }2676 }
2643 }2677 }
2644 if (!ret) {2678 if (!ret) {
2645 ret = write_one_cache_group(trans, path, cache);2679 ret = update_block_group_item(trans, path, cache);
2646 /*2680 /*
2647 * Our block group might still be attached to the list2681 * Our block group might still be attached to the list
2648 * of new block groups in the transaction handle of some2682 * of new block groups in the transaction handle of some
@@ -2791,7 +2825,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) · 142 unmodified lines
2791 }2825 }
2792 }2826 }
2793 if (!ret) {2827 if (!ret) {
2794 ret = write_one_cache_group(trans, path, cache);2828 ret = update_block_group_item(trans, path, cache);
2795 /*2829 /*
2796 * One of the free space endio workers might have2830 * One of the free space endio workers might have
2797 * created a new block group while updating a free space2831 * created a new block group while updating a free space
@@ -2808,7 +2842,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) · 10 unmodified lines
2808 if (ret == -ENOENT) {2842 if (ret == -ENOENT) {
2809 wait_event(cur_trans->writer_wait,2843 wait_event(cur_trans->writer_wait,
2810 atomic_read(&cur_trans->num_writers) == 1);2844 atomic_read(&cur_trans->num_writers) == 1);
2811 ret = write_one_cache_group(trans, path, cache);2845 ret = update_block_group_item(trans, path, cache);
2812 }2846 }
2813 if (ret)2847 if (ret)
2814 btrfs_abort_transaction(trans, ret);2848 btrfs_abort_transaction(trans, ret);
@@ -3384,3 +3418,44 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info) · 569 unmodified lines
3384 }3418 }
3385 return 0;3419 return 0;
3386}3420}
3421
3422void btrfs_freeze_block_group(struct btrfs_block_group *cache)
3423{
3424 atomic_inc(&cache->frozen);
3425}
3426
3427void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
3428{
3429 struct btrfs_fs_info *fs_info = block_group->fs_info;
3430 struct extent_map_tree *em_tree;
3431 struct extent_map *em;
3432 bool cleanup;
3433
3434 spin_lock(&block_group->lock);
3435 cleanup = (atomic_dec_and_test(&block_group->frozen) &&
3436 block_group->removed);
3437 spin_unlock(&block_group->lock);
3438
3439 if (cleanup) {
3440 mutex_lock(&fs_info->chunk_mutex);
3441 em_tree = &fs_info->mapping_tree;
3442 write_lock(&em_tree->lock);
3443 em = lookup_extent_mapping(em_tree, block_group->start,
3444 1);
3445 BUG_ON(!em); /* logic error, can't happen */
3446 remove_extent_mapping(em_tree, em);
3447 write_unlock(&em_tree->lock);
3448 mutex_unlock(&fs_info->chunk_mutex);
3449
3450 /* once for us and once for the tree */
3451 free_extent_map(em);
3452 free_extent_map(em);
3453
3454 /*
3455 * We may have left one free space entry and other possible
3456 * tasks trimming this block group have left 1 entry each one.
3457 * Free them if any.
3458 */
3459 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3460 }
3461}
fs/btrfs/block-group.h+13−1
@@ -129,8 +129,17 @@ struct btrfs_block_group { · 128 unmodified lines
129 /* For read-only block groups */129 /* For read-only block groups */
130 struct list_head ro_list;130 struct list_head ro_list;
131 131
132 /*
133 * When non-zero it means the block group's logical address and its
134 * device extents can not be reused for future block group allocations
135 * until the counter goes down to 0. This is to prevent them from being
136 * reused while some task is still using the block group after it was
137 * deleted - we want to make sure they can only be reused for new block
138 * groups after that task is done with the deleted block group.
139 */
140 atomic_t frozen;
141
132 /* For discard operations */142 /* For discard operations */
133 atomic_t trimming;
134 struct list_head discard_list;143 struct list_head discard_list;
135 int discard_index;144 int discard_index;
136 u64 discard_eligible_time;145 u64 discard_eligible_time;
@@ -283,6 +292,9 @@ static inline int btrfs_block_group_done(struct btrfs_block_group *cache) · 146 unmodified lines
283 cache->cached == BTRFS_CACHE_ERROR;292 cache->cached == BTRFS_CACHE_ERROR;
284}293}
285 294
295void btrfs_freeze_block_group(struct btrfs_block_group *cache);
296void btrfs_unfreeze_block_group(struct btrfs_block_group *cache);
297
286#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS298#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
287int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,299int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
288 u64 physical, u64 **logical, int *naddrs, int *stripe_len);300 u64 physical, u64 **logical, int *naddrs, int *stripe_len);
fs/btrfs/block-rsv.c+4−1
@@ -5,6 +5,7 @@ · 4 unmodified lines
5#include "block-rsv.h"5#include "block-rsv.h"
6#include "space-info.h"6#include "space-info.h"
7#include "transaction.h"7#include "transaction.h"
8#include "block-group.h"
8 9
9/*10/*
10 * HOW DO BLOCK RESERVES WORK11 * HOW DO BLOCK RESERVES WORK
@@ -405,6 +406,8 @@ void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info) · 394 unmodified lines
405 else406 else
406 block_rsv->full = 0;407 block_rsv->full = 0;
407 408
409 if (block_rsv->size >= sinfo->total_bytes)
410 sinfo->force_alloc = CHUNK_ALLOC_FORCE;
408 spin_unlock(&block_rsv->lock);411 spin_unlock(&block_rsv->lock);
409 spin_unlock(&sinfo->lock);412 spin_unlock(&sinfo->lock);
410}413}
@@ -455,7 +458,7 @@ static struct btrfs_block_rsv *get_block_rsv( · 44 unmodified lines
455 struct btrfs_fs_info *fs_info = root->fs_info;458 struct btrfs_fs_info *fs_info = root->fs_info;
456 struct btrfs_block_rsv *block_rsv = NULL;459 struct btrfs_block_rsv *block_rsv = NULL;
457 460
458 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||461 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
459 (root == fs_info->csum_root && trans->adding_csums) ||462 (root == fs_info->csum_root && trans->adding_csums) ||
460 (root == fs_info->uuid_root))463 (root == fs_info->uuid_root))
461 block_rsv = trans->block_rsv;464 block_rsv = trans->block_rsv;
fs/btrfs/btrfs_inode.h+8−36
@@ -7,6 +7,7 @@ · 6 unmodified lines
7#define BTRFS_INODE_H7#define BTRFS_INODE_H
8 8
9#include <linux/hash.h>9#include <linux/hash.h>
10#include <linux/refcount.h>
10#include "extent_map.h"11#include "extent_map.h"
11#include "extent_io.h"12#include "extent_io.h"
12#include "ordered-data.h"13#include "ordered-data.h"
@@ -27,7 +28,6 @@ enum { · 14 unmodified lines
27 BTRFS_INODE_NEEDS_FULL_SYNC,28 BTRFS_INODE_NEEDS_FULL_SYNC,
28 BTRFS_INODE_COPY_EVERYTHING,29 BTRFS_INODE_COPY_EVERYTHING,
29 BTRFS_INODE_IN_DELALLOC_LIST,30 BTRFS_INODE_IN_DELALLOC_LIST,
30 BTRFS_INODE_READDIO_NEED_LOCK,
31 BTRFS_INODE_HAS_PROPS,31 BTRFS_INODE_HAS_PROPS,
32 BTRFS_INODE_SNAPSHOT_FLUSH,32 BTRFS_INODE_SNAPSHOT_FLUSH,
33};33};
@@ -293,53 +293,25 @@ static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) · 259 unmodified lines
293 return ret;293 return ret;
294}294}
295 295
296#define BTRFS_DIO_ORIG_BIO_SUBMITTED 0x1
297
298struct btrfs_dio_private {296struct btrfs_dio_private {
299 struct inode *inode;297 struct inode *inode;
300 unsigned long flags;
301 u64 logical_offset;298 u64 logical_offset;
302 u64 disk_bytenr;299 u64 disk_bytenr;
303 u64 bytes;300 u64 bytes;
304 void *private;
305
306 /* number of bios pending for this dio */
307 atomic_t pending_bios;
308 301
309 /* IO errors */302 /*
310 int errors;303 * References to this structure. There is one reference per in-flight
311 304 * bio plus one while we're still setting up.
312 /* orig_bio is our btrfs_io_bio */305 */
313 struct bio *orig_bio;306 refcount_t refs;
314 307
315 /* dio_bio came from fs/direct-io.c */308 /* dio_bio came from fs/direct-io.c */
316 struct bio *dio_bio;309 struct bio *dio_bio;
317 310
318 /*311 /* Array of checksums */
319 * The original bio may be split to several sub-bios, this is312 u8 csums[];
320 * done during endio of sub-bios
321 */
322 blk_status_t (*subio_endio)(struct inode *, struct btrfs_io_bio *,
323 blk_status_t);
324};313};
325 314
326/*
327 * Disable DIO read nolock optimization, so new dio readers will be forced
328 * to grab i_mutex. It is used to avoid the endless truncate due to
329 * nonlocked dio read.
330 */
331static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode)
332{
333 set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
334 smp_mb();
335}
336
337static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode)
338{
339 smp_mb__before_atomic();
340 clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
341}
342
343/* Array of bytes with variable length, hexadecimal format 0x1234 */315/* Array of bytes with variable length, hexadecimal format 0x1234 */
344#define CSUM_FMT "0x%*phN"316#define CSUM_FMT "0x%*phN"
345#define CSUM_FMT_VALUE(size, bytes) size, bytes317#define CSUM_FMT_VALUE(size, bytes) size, bytes
fs/btrfs/compression.c+17−19
@@ -194,11 +194,9 @@ static int check_compressed_csum(struct btrfs_inode *inode, · 193 unmodified lines
194 for (i = 0; i < cb->nr_pages; i++) {194 for (i = 0; i < cb->nr_pages; i++) {
195 page = cb->compressed_pages[i];195 page = cb->compressed_pages[i];
196 196
197 crypto_shash_init(shash);
198 kaddr = kmap_atomic(page);197 kaddr = kmap_atomic(page);
199 crypto_shash_update(shash, kaddr, PAGE_SIZE);198 crypto_shash_digest(shash, kaddr, PAGE_SIZE, csum);
200 kunmap_atomic(kaddr);199 kunmap_atomic(kaddr);
201 crypto_shash_final(shash, (u8 *)&csum);
202 200
203 if (memcmp(&csum, cb_sum, csum_size)) {201 if (memcmp(&csum, cb_sum, csum_size)) {
204 btrfs_print_data_csum_error(inode, disk_start,202 btrfs_print_data_csum_error(inode, disk_start,
@@ -1141,6 +1139,22 @@ static void put_workspace(int type, struct list_head *ws) · 936 unmodified lines
1141 }1139 }
1142}1140}
1143 1141
1142/*
1143 * Adjust @level according to the limits of the compression algorithm or
1144 * fallback to default
1145 */
1146static unsigned int btrfs_compress_set_level(int type, unsigned level)
1147{
1148 const struct btrfs_compress_op *ops = btrfs_compress_op[type];
1149
1150 if (level == 0)
1151 level = ops->default_level;
1152 else
1153 level = min(level, ops->max_level);
1154
1155 return level;
1156}
1157
1144/*1158/*
1145 * Given an address space and start and length, compress the bytes into @pages1159 * Given an address space and start and length, compress the bytes into @pages
1146 * that are allocated on demand.1160 * that are allocated on demand.
@@ -1748,19 +1762,3 @@ unsigned int btrfs_compress_str2level(unsigned int type, const char *str) · 601 unmodified lines
1748 1762
1749 return level;1763 return level;
1750}1764}
1751
1752/*
1753 * Adjust @level according to the limits of the compression algorithm or
1754 * fallback to default
1755 */
1756unsigned int btrfs_compress_set_level(int type, unsigned level)
1757{
1758 const struct btrfs_compress_op *ops = btrfs_compress_op[type];
1759
1760 if (level == 0)
1761 level = ops->default_level;
1762 else
1763 level = min(level, ops->max_level);
1764
1765 return level;
1766}
fs/btrfs/compression.h+0−2
@@ -140,8 +140,6 @@ extern const struct btrfs_compress_op btrfs_zstd_compress; · 139 unmodified lines
140const char* btrfs_compress_type2str(enum btrfs_compression_type type);140const char* btrfs_compress_type2str(enum btrfs_compression_type type);
141bool btrfs_compress_is_valid_type(const char *str, size_t len);141bool btrfs_compress_is_valid_type(const char *str, size_t len);
142 142
143unsigned int btrfs_compress_set_level(int type, unsigned level);
144
145int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);143int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
146 144
147#endif145#endif
fs/btrfs/ctree.c+76−104
@@ -144,9 +144,10 @@ struct extent_buffer *btrfs_root_node(struct btrfs_root *root) · 143 unmodified lines
144 return eb;144 return eb;
145}145}
146 146
147/* cowonly root (everything not a reference counted cow subvolume), just get147/*
148 * put onto a simple dirty list. transaction.c walks this to make sure they148 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
149 * get properly updated on disk.149 * just get put onto a simple dirty list. Transaction walks this list to make
150 * sure they get properly updated on disk.
150 */151 */
151static void add_root_to_dirty_list(struct btrfs_root *root)152static void add_root_to_dirty_list(struct btrfs_root *root)
152{153{
@@ -185,9 +186,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 32 unmodified lines
185 int level;186 int level;
186 struct btrfs_disk_key disk_key;187 struct btrfs_disk_key disk_key;
187 188
188 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&189 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
189 trans->transid != fs_info->running_transaction->transid);190 trans->transid != fs_info->running_transaction->transid);
190 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&191 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
191 trans->transid != root->last_trans);192 trans->transid != root->last_trans);
192 193
193 level = btrfs_header_level(buf);194 level = btrfs_header_level(buf);
@@ -826,12 +827,11 @@ int btrfs_block_can_be_shared(struct btrfs_root *root, · 632 unmodified lines
826 struct extent_buffer *buf)827 struct extent_buffer *buf)
827{828{
828 /*829 /*
829 * Tree blocks not in reference counted trees and tree roots830 * Tree blocks not in shareable trees and tree roots are never shared.
830 * are never shared. If a block was allocated after the last831 * If a block was allocated after the last snapshot and the block was
831 * snapshot and the block was not allocated by tree relocation,832 * not allocated by tree relocation, we know the block is not shared.
832 * we know the block is not shared.
833 */833 */
834 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&834 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
835 buf != root->node && buf != root->commit_root &&835 buf != root->node && buf != root->commit_root &&
836 (btrfs_header_generation(buf) <=836 (btrfs_header_generation(buf) <=
837 btrfs_root_last_snapshot(&root->root_item) ||837 btrfs_root_last_snapshot(&root->root_item) ||
@@ -1024,9 +1024,9 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 186 unmodified lines
1024 1024
1025 btrfs_assert_tree_locked(buf);1025 btrfs_assert_tree_locked(buf);
1026 1026
1027 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&1027 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
1028 trans->transid != fs_info->running_transaction->transid);1028 trans->transid != fs_info->running_transaction->transid);
1029 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&1029 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
1030 trans->transid != root->last_trans);1030 trans->transid != root->last_trans);
1031 1031
1032 level = btrfs_header_level(buf);1032 level = btrfs_header_level(buf);
@@ -1065,7 +1065,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 32 unmodified lines
1065 return ret;1065 return ret;
1066 }1066 }
1067 1067
1068 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {1068 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
1069 ret = btrfs_reloc_cow_block(trans, root, buf, cow);1069 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1070 if (ret) {1070 if (ret) {
1071 btrfs_abort_transaction(trans, ret);1071 btrfs_abort_transaction(trans, ret);
@@ -1668,15 +1668,8 @@ static noinline int generic_bin_search(struct extent_buffer *eb, · 596 unmodified lines
1668{1668{
1669 int low = 0;1669 int low = 0;
1670 int high = max;1670 int high = max;
1671 int mid;
1672 int ret;1671 int ret;
1673 struct btrfs_disk_key *tmp = NULL;1672 const int key_size = sizeof(struct btrfs_disk_key);
1674 struct btrfs_disk_key unaligned;
1675 unsigned long offset;
1676 char *kaddr = NULL;
1677 unsigned long map_start = 0;
1678 unsigned long map_len = 0;
1679 int err;
1680 1673
1681 if (low > high) {1674 if (low > high) {
1682 btrfs_err(eb->fs_info,1675 btrfs_err(eb->fs_info,
@@ -1687,32 +1680,26 @@ static noinline int generic_bin_search(struct extent_buffer *eb, · 4 unmodified lines
1687 }1680 }
1688 1681
1689 while (low < high) {1682 while (low < high) {
1683 unsigned long oip;
1684 unsigned long offset;
1685 struct btrfs_disk_key *tmp;
1686 struct btrfs_disk_key unaligned;
1687 int mid;
1688
1690 mid = (low + high) / 2;1689 mid = (low + high) / 2;
1691 offset = p + mid * item_size;1690 offset = p + mid * item_size;
1691 oip = offset_in_page(offset);
1692 1692
1693 if (!kaddr || offset < map_start ||1693 if (oip + key_size <= PAGE_SIZE) {
1694 (offset + sizeof(struct btrfs_disk_key)) >1694 const unsigned long idx = offset >> PAGE_SHIFT;
1695 map_start + map_len) {1695 char *kaddr = page_address(eb->pages[idx]);
1696
1697 err = map_private_extent_buffer(eb, offset,
1698 sizeof(struct btrfs_disk_key),
1699 &kaddr, &map_start, &map_len);
1700
1701 if (!err) {
1702 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1703 map_start);
1704 } else if (err == 1) {
1705 read_extent_buffer(eb, &unaligned,
1706 offset, sizeof(unaligned));
1707 tmp = &unaligned;
1708 } else {
1709 return err;
1710 }
1711 1696
1697 tmp = (struct btrfs_disk_key *)(kaddr + oip);
1712 } else {1698 } else {
1713 tmp = (struct btrfs_disk_key *)(kaddr + offset -1699 read_extent_buffer(eb, &unaligned, offset, key_size);
1714 map_start);1700 tmp = &unaligned;
1715 }1701 }
1702
1716 ret = comp_keys(tmp, key);1703 ret = comp_keys(tmp, key);
1717 1704
1718 if (ret < 0)1705 if (ret < 0)
@@ -1733,9 +1720,9 @@ static noinline int generic_bin_search(struct extent_buffer *eb, · 14 unmodified lines
1733 * leaves vs nodes1720 * leaves vs nodes
1734 */1721 */
1735int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,1722int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1736 int level, int *slot)1723 int *slot)
1737{1724{
1738 if (level == 0)1725 if (btrfs_header_level(eb) == 0)
1739 return generic_bin_search(eb,1726 return generic_bin_search(eb,
1740 offsetof(struct btrfs_leaf, items),1727 offsetof(struct btrfs_leaf, items),
1741 sizeof(struct btrfs_item),1728 sizeof(struct btrfs_item),
@@ -2348,16 +2335,15 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, · 606 unmodified lines
2348 struct btrfs_fs_info *fs_info = root->fs_info;2335 struct btrfs_fs_info *fs_info = root->fs_info;
2349 u64 blocknr;2336 u64 blocknr;
2350 u64 gen;2337 u64 gen;
2351 struct extent_buffer *b = *eb_ret;
2352 struct extent_buffer *tmp;2338 struct extent_buffer *tmp;
2353 struct btrfs_key first_key;2339 struct btrfs_key first_key;
2354 int ret;2340 int ret;
2355 int parent_level;2341 int parent_level;
2356 2342
2357 blocknr = btrfs_node_blockptr(b, slot);2343 blocknr = btrfs_node_blockptr(*eb_ret, slot);
2358 gen = btrfs_node_ptr_generation(b, slot);2344 gen = btrfs_node_ptr_generation(*eb_ret, slot);
2359 parent_level = btrfs_header_level(b);2345 parent_level = btrfs_header_level(*eb_ret);
2360 btrfs_node_key_to_cpu(b, &first_key, slot);2346 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
2361 2347
2362 tmp = find_extent_buffer(fs_info, blocknr);2348 tmp = find_extent_buffer(fs_info, blocknr);
2363 if (tmp) {2349 if (tmp) {
@@ -2501,19 +2487,6 @@ done: · 137 unmodified lines
2501 return ret;2487 return ret;
2502}2488}
2503 2489
2504static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2505 int level, int *prev_cmp, int *slot)
2506{
2507 if (*prev_cmp != 0) {
2508 *prev_cmp = btrfs_bin_search(b, key, level, slot);
2509 return *prev_cmp;
2510 }
2511
2512 *slot = 0;
2513
2514 return 0;
2515}
2516
2517int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,2490int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2518 u64 iobjectid, u64 ioff, u8 key_type,2491 u64 iobjectid, u64 ioff, u8 key_type,
2519 struct btrfs_key *found_key)2492 struct btrfs_key *found_key)
@@ -2783,9 +2756,23 @@ cow_done: · 263 unmodified lines
2783 }2756 }
2784 }2757 }
2785 2758
2786 ret = key_search(b, key, level, &prev_cmp, &slot);2759 /*
2787 if (ret < 0)2760 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
2788 goto done;2761 * we can safely assume the target key will always be in slot 0
2762 * on lower levels due to the invariants BTRFS' btree provides,
2763 * namely that a btrfs_key_ptr entry always points to the
2764 * lowest key in the child node, thus we can skip searching
2765 * lower levels
2766 */
2767 if (prev_cmp == 0) {
2768 slot = 0;
2769 ret = 0;
2770 } else {
2771 ret = btrfs_bin_search(b, key, &slot);
2772 prev_cmp = ret;
2773 if (ret < 0)
2774 goto done;
2775 }
2789 2776
2790 if (level == 0) {2777 if (level == 0) {
2791 p->slots[level] = slot;2778 p->slots[level] = slot;
@@ -2909,7 +2896,6 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, · 117 unmodified lines
2909 int level;2896 int level;
2910 int lowest_unlock = 1;2897 int lowest_unlock = 1;
2911 u8 lowest_level = 0;2898 u8 lowest_level = 0;
2912 int prev_cmp = -1;
2913 2899
2914 lowest_level = p->lowest_level;2900 lowest_level = p->lowest_level;
2915 WARN_ON(p->nodes[0] != NULL);2901 WARN_ON(p->nodes[0] != NULL);
@@ -2942,12 +2928,7 @@ again: · 26 unmodified lines
2942 */2928 */
2943 btrfs_unlock_up_safe(p, level + 1);2929 btrfs_unlock_up_safe(p, level + 1);
2944 2930
2945 /*2931 ret = btrfs_bin_search(b, key, &slot);
2946 * Since we can unwind ebs we want to do a real search every
2947 * time.
2948 */
2949 prev_cmp = -1;
2950 ret = key_search(b, key, level, &prev_cmp, &slot);
2951 if (ret < 0)2932 if (ret < 0)
2952 goto done;2933 goto done;
2953 2934
@@ -3507,19 +3488,17 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr) · 553 unmodified lines
3507{3488{
3508 struct btrfs_item *start_item;3489 struct btrfs_item *start_item;
3509 struct btrfs_item *end_item;3490 struct btrfs_item *end_item;
3510 struct btrfs_map_token token;
3511 int data_len;3491 int data_len;
3512 int nritems = btrfs_header_nritems(l);3492 int nritems = btrfs_header_nritems(l);
3513 int end = min(nritems, start + nr) - 1;3493 int end = min(nritems, start + nr) - 1;
3514 3494
3515 if (!nr)3495 if (!nr)
3516 return 0;3496 return 0;
3517 btrfs_init_map_token(&token, l);
3518 start_item = btrfs_item_nr(start);3497 start_item = btrfs_item_nr(start);
3519 end_item = btrfs_item_nr(end);3498 end_item = btrfs_item_nr(end);
3520 data_len = btrfs_token_item_offset(l, start_item, &token) +3499 data_len = btrfs_item_offset(l, start_item) +
3521 btrfs_token_item_size(l, start_item, &token);3500 btrfs_item_size(l, start_item);
3522 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);3501 data_len = data_len - btrfs_item_offset(l, end_item);
3523 data_len += sizeof(struct btrfs_item) * nr;3502 data_len += sizeof(struct btrfs_item) * nr;
3524 WARN_ON(data_len < 0);3503 WARN_ON(data_len < 0);
3525 return data_len;3504 return data_len;
@@ -3650,8 +3629,8 @@ static noinline int __push_leaf_right(struct btrfs_path *path, · 124 unmodified lines
3650 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);3629 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3651 for (i = 0; i < right_nritems; i++) {3630 for (i = 0; i < right_nritems; i++) {
3652 item = btrfs_item_nr(i);3631 item = btrfs_item_nr(i);
3653 push_space -= btrfs_token_item_size(right, item, &token);3632 push_space -= btrfs_token_item_size(&token, item);
3654 btrfs_set_token_item_offset(right, item, push_space, &token);3633 btrfs_set_token_item_offset(&token, item, push_space);
3655 }3634 }
3656 3635
3657 left_nritems -= push_items;3636 left_nritems -= push_items;
@@ -3859,10 +3838,9 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, · 201 unmodified lines
3859 3838
3860 item = btrfs_item_nr(i);3839 item = btrfs_item_nr(i);
3861 3840
3862 ioff = btrfs_token_item_offset(left, item, &token);3841 ioff = btrfs_token_item_offset(&token, item);
3863 btrfs_set_token_item_offset(left, item,3842 btrfs_set_token_item_offset(&token, item,
3864 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),3843 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3865 &token);
3866 }3844 }
3867 btrfs_set_header_nritems(left, old_left_nritems + push_items);3845 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3868 3846
@@ -3892,9 +3870,8 @@ static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, · 23 unmodified lines
3892 for (i = 0; i < right_nritems; i++) {3870 for (i = 0; i < right_nritems; i++) {
3893 item = btrfs_item_nr(i);3871 item = btrfs_item_nr(i);
3894 3872
3895 push_space = push_space - btrfs_token_item_size(right,3873 push_space = push_space - btrfs_token_item_size(&token, item);
3896 item, &token);3874 btrfs_set_token_item_offset(&token, item, push_space);
3897 btrfs_set_token_item_offset(right, item, push_space, &token);
3898 }3875 }
3899 3876
3900 btrfs_mark_buffer_dirty(left);3877 btrfs_mark_buffer_dirty(left);
@@ -4036,9 +4013,8 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans, · 135 unmodified lines
4036 struct btrfs_item *item = btrfs_item_nr(i);4013 struct btrfs_item *item = btrfs_item_nr(i);
4037 u32 ioff;4014 u32 ioff;
4038 4015
4039 ioff = btrfs_token_item_offset(right, item, &token);4016 ioff = btrfs_token_item_offset(&token, item);
4040 btrfs_set_token_item_offset(right, item,4017 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
4041 ioff + rt_data_off, &token);
4042 }4018 }
4043 4019
4044 btrfs_set_header_nritems(l, mid);4020 btrfs_set_header_nritems(l, mid);
@@ -4541,9 +4517,8 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) · 496 unmodified lines
4541 u32 ioff;4517 u32 ioff;
4542 item = btrfs_item_nr(i);4518 item = btrfs_item_nr(i);
4543 4519
4544 ioff = btrfs_token_item_offset(leaf, item, &token);4520 ioff = btrfs_token_item_offset(&token, item);
4545 btrfs_set_token_item_offset(leaf, item,4521 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
4546 ioff + size_diff, &token);
4547 }4522 }
4548 4523
4549 /* shift the data */4524 /* shift the data */
@@ -4640,9 +4615,8 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 90 unmodified lines
4640 u32 ioff;4615 u32 ioff;
4641 item = btrfs_item_nr(i);4616 item = btrfs_item_nr(i);
4642 4617
4643 ioff = btrfs_token_item_offset(leaf, item, &token);4618 ioff = btrfs_token_item_offset(&token, item);
4644 btrfs_set_token_item_offset(leaf, item,4619 btrfs_set_token_item_offset(&token, item, ioff - data_size);
4645 ioff - data_size, &token);
4646 }4620 }
4647 4621
4648 /* shift the data */4622 /* shift the data */
@@ -4718,9 +4692,9 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, · 69 unmodified lines
4718 u32 ioff;4692 u32 ioff;
4719 4693
4720 item = btrfs_item_nr(i);4694 item = btrfs_item_nr(i);
4721 ioff = btrfs_token_item_offset(leaf, item, &token);4695 ioff = btrfs_token_item_offset(&token, item);
4722 btrfs_set_token_item_offset(leaf, item,4696 btrfs_set_token_item_offset(&token, item,
4723 ioff - total_data, &token);4697 ioff - total_data);
4724 }4698 }
4725 /* shift the items */4699 /* shift the items */
4726 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),4700 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
@@ -4739,10 +4713,9 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, · 12 unmodified lines
4739 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);4713 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4740 btrfs_set_item_key(leaf, &disk_key, slot + i);4714 btrfs_set_item_key(leaf, &disk_key, slot + i);
4741 item = btrfs_item_nr(slot + i);4715 item = btrfs_item_nr(slot + i);
4742 btrfs_set_token_item_offset(leaf, item,4716 btrfs_set_token_item_offset(&token, item, data_end - data_size[i]);
4743 data_end - data_size[i], &token);
4744 data_end -= data_size[i];4717 data_end -= data_size[i];
4745 btrfs_set_token_item_size(leaf, item, data_size[i], &token);4718 btrfs_set_token_item_size(&token, item, data_size[i]);
4746 }4719 }
4747 4720
4748 btrfs_set_header_nritems(leaf, nritems + nr);4721 btrfs_set_header_nritems(leaf, nritems + nr);
@@ -4930,9 +4903,8 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 181 unmodified lines
4930 u32 ioff;4903 u32 ioff;
4931 4904
4932 item = btrfs_item_nr(i);4905 item = btrfs_item_nr(i);
4933 ioff = btrfs_token_item_offset(leaf, item, &token);4906 ioff = btrfs_token_item_offset(&token, item);
4934 btrfs_set_token_item_offset(leaf, item,4907 btrfs_set_token_item_offset(&token, item, ioff + dsize);
4935 ioff + dsize, &token);
4936 }4908 }
4937 4909
4938 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),4910 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
@@ -5103,7 +5075,7 @@ again: · 164 unmodified lines
5103 while (1) {5075 while (1) {
5104 nritems = btrfs_header_nritems(cur);5076 nritems = btrfs_header_nritems(cur);
5105 level = btrfs_header_level(cur);5077 level = btrfs_header_level(cur);
5106 sret = btrfs_bin_search(cur, min_key, level, &slot);5078 sret = btrfs_bin_search(cur, min_key, &slot);
5107 if (sret < 0) {5079 if (sret < 0) {
5108 ret = sret;5080 ret = sret;
5109 goto out;5081 goto out;
fs/btrfs/ctree.h+66−55
@@ -28,6 +28,7 @@ · 27 unmodified lines
28#include <linux/dynamic_debug.h>28#include <linux/dynamic_debug.h>
29#include <linux/refcount.h>29#include <linux/refcount.h>
30#include <linux/crc32c.h>30#include <linux/crc32c.h>
31#include <linux/iomap.h>
31#include "extent-io-tree.h"32#include "extent-io-tree.h"
32#include "extent_io.h"33#include "extent_io.h"
33#include "extent_map.h"34#include "extent_map.h"
@@ -582,6 +583,7 @@ struct btrfs_fs_info { · 548 unmodified lines
582 struct btrfs_root *quota_root;583 struct btrfs_root *quota_root;
583 struct btrfs_root *uuid_root;584 struct btrfs_root *uuid_root;
584 struct btrfs_root *free_space_root;585 struct btrfs_root *free_space_root;
586 struct btrfs_root *data_reloc_root;
585 587
586 /* the log root tree is a directory of all the other log roots */588 /* the log root tree is a directory of all the other log roots */
587 struct btrfs_root *log_root_tree;589 struct btrfs_root *log_root_tree;
@@ -758,7 +760,6 @@ struct btrfs_fs_info { · 170 unmodified lines
758 struct btrfs_workqueue *endio_workers;760 struct btrfs_workqueue *endio_workers;
759 struct btrfs_workqueue *endio_meta_workers;761 struct btrfs_workqueue *endio_meta_workers;
760 struct btrfs_workqueue *endio_raid56_workers;762 struct btrfs_workqueue *endio_raid56_workers;
761 struct btrfs_workqueue *endio_repair_workers;
762 struct btrfs_workqueue *rmw_workers;763 struct btrfs_workqueue *rmw_workers;
763 struct btrfs_workqueue *endio_meta_write_workers;764 struct btrfs_workqueue *endio_meta_write_workers;
764 struct btrfs_workqueue *endio_write_workers;765 struct btrfs_workqueue *endio_write_workers;
@@ -970,7 +971,28 @@ enum { · 205 unmodified lines
970 * is used to tell us when more checks are required971 * is used to tell us when more checks are required
971 */972 */
972 BTRFS_ROOT_IN_TRANS_SETUP,973 BTRFS_ROOT_IN_TRANS_SETUP,
973 BTRFS_ROOT_REF_COWS,974
975 /*
976 * Set if tree blocks of this root can be shared by other roots.
977 * Only subvolume trees and their reloc trees have this bit set.
978 * Conflicts with TRACK_DIRTY bit.
979 *
980 * This affects two things:
981 *
982 * - How balance works
983 * For shareable roots, we need to use reloc tree and do path
984 * replacement for balance, and need various pre/post hooks for
985 * snapshot creation to handle them.
986 *
987 * While for non-shareable trees, we just simply do a tree search
988 * with COW.
989 *
990 * - How dirty roots are tracked
991 * For shareable roots, btrfs_record_root_in_trans() is needed to
992 * track them, while non-subvolume roots have TRACK_DIRTY bit, they
993 * don't need to set this manually.
994 */
995 BTRFS_ROOT_SHAREABLE,
974 BTRFS_ROOT_TRACK_DIRTY,996 BTRFS_ROOT_TRACK_DIRTY,
975 BTRFS_ROOT_IN_RADIX,997 BTRFS_ROOT_IN_RADIX,
976 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,998 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
@@ -1056,7 +1078,7 @@ struct btrfs_root { · 79 unmodified lines
1056 struct btrfs_key defrag_progress;1078 struct btrfs_key defrag_progress;
1057 struct btrfs_key defrag_max;1079 struct btrfs_key defrag_max;
1058 1080
1059 /* the dirty list is only used by non-reference counted roots */1081 /* The dirty list is only used by non-shareable roots */
1060 struct list_head dirty_list;1082 struct list_head dirty_list;
1061 1083
1062 struct list_head root_list;1084 struct list_head root_list;
@@ -1146,6 +1168,9 @@ struct btrfs_root { · 83 unmodified lines
1146 /* Record pairs of swapped blocks for qgroup */1168 /* Record pairs of swapped blocks for qgroup */
1147 struct btrfs_qgroup_swapped_blocks swapped_blocks;1169 struct btrfs_qgroup_swapped_blocks swapped_blocks;
1148 1170
1171 /* Used only by log trees, when logging csum items */
1172 struct extent_io_tree log_csum_range;
1173
1149#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS1174#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1150 u64 alloc_bytenr;1175 u64 alloc_bytenr;
1151#endif1176#endif
@@ -1341,7 +1366,7 @@ do { \ · 189 unmodified lines
1341 BTRFS_INODE_ROOT_ITEM_INIT)1366 BTRFS_INODE_ROOT_ITEM_INIT)
1342 1367
1343struct btrfs_map_token {1368struct btrfs_map_token {
1344 const struct extent_buffer *eb;1369 struct extent_buffer *eb;
1345 char *kaddr;1370 char *kaddr;
1346 unsigned long offset;1371 unsigned long offset;
1347};1372};
@@ -1353,7 +1378,8 @@ static inline void btrfs_init_map_token(struct btrfs_map_token *token, · 5 unmodified lines
1353 struct extent_buffer *eb)1378 struct extent_buffer *eb)
1354{1379{
1355 token->eb = eb;1380 token->eb = eb;
1356 token->kaddr = NULL;1381 token->kaddr = page_address(eb->pages[0]);
1382 token->offset = 0;
1357}1383}
1358 1384
1359/* some macros to generate set/get functions for the struct fields. This1385/* some macros to generate set/get functions for the struct fields. This
@@ -1377,15 +1403,14 @@ static inline void btrfs_init_map_token(struct btrfs_map_token *token, · 17 unmodified lines
1377 sizeof(((type *)0)->member)))1403 sizeof(((type *)0)->member)))
1378 1404
1379#define DECLARE_BTRFS_SETGET_BITS(bits) \1405#define DECLARE_BTRFS_SETGET_BITS(bits) \
1380u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \1406u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
1381 const void *ptr, unsigned long off, \1407 const void *ptr, unsigned long off); \
1382 struct btrfs_map_token *token); \1408void btrfs_set_token_##bits(struct btrfs_map_token *token, \
1383void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr, \1409 const void *ptr, unsigned long off, \
1384 unsigned long off, u##bits val, \1410 u##bits val); \
1385 struct btrfs_map_token *token); \
1386u##bits btrfs_get_##bits(const struct extent_buffer *eb, \1411u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
1387 const void *ptr, unsigned long off); \1412 const void *ptr, unsigned long off); \
1388void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \1413void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
1389 unsigned long off, u##bits val);1414 unsigned long off, u##bits val);
1390 1415
1391DECLARE_BTRFS_SETGET_BITS(8)1416DECLARE_BTRFS_SETGET_BITS(8)
@@ -1400,25 +1425,23 @@ static inline u##bits btrfs_##name(const struct extent_buffer *eb, \ · 8 unmodified lines
1400 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \1425 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1401 return btrfs_get_##bits(eb, s, offsetof(type, member)); \1426 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
1402} \1427} \
1403static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \1428static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
1404 u##bits val) \1429 u##bits val) \
1405{ \1430{ \
1406 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \1431 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1407 btrfs_set_##bits(eb, s, offsetof(type, member), val); \1432 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
1408} \1433} \
1409static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\1434static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \
1410 const type *s, \1435 const type *s) \
1411 struct btrfs_map_token *token) \
1412{ \1436{ \
1413 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \1437 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1414 return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \1438 return btrfs_get_token_##bits(token, s, offsetof(type, member));\
1415} \1439} \
1416static inline void btrfs_set_token_##name(struct extent_buffer *eb, \1440static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
1417 type *s, u##bits val, \1441 type *s, u##bits val) \
1418 struct btrfs_map_token *token) \
1419{ \1442{ \
1420 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \1443 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1421 btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \1444 btrfs_set_token_##bits(token, s, offsetof(type, member), val); \
1422}1445}
1423 1446
1424#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \1447#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
@@ -1428,7 +1451,7 @@ static inline u##bits btrfs_##name(const struct extent_buffer *eb) \ · 3 unmodified lines
1428 u##bits res = le##bits##_to_cpu(p->member); \1451 u##bits res = le##bits##_to_cpu(p->member); \
1429 return res; \1452 return res; \
1430} \1453} \
1431static inline void btrfs_set_##name(struct extent_buffer *eb, \1454static inline void btrfs_set_##name(const struct extent_buffer *eb, \
1432 u##bits val) \1455 u##bits val) \
1433{ \1456{ \
1434 type *p = page_address(eb->pages[0]); \1457 type *p = page_address(eb->pages[0]); \
@@ -1446,7 +1469,7 @@ static inline void btrfs_set_##name(type *s, u##bits val) \ · 11 unmodified lines
1446}1469}
1447 1470
1448 1471
1449static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,1472static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
1450 struct btrfs_dev_item *s)1473 struct btrfs_dev_item *s)
1451{1474{
1452 BUILD_BUG_ON(sizeof(u64) !=1475 BUILD_BUG_ON(sizeof(u64) !=
@@ -1454,7 +1477,7 @@ static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb, · 1 unmodified line
1454 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,1477 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1455 total_bytes));1478 total_bytes));
1456}1479}
1457static inline void btrfs_set_device_total_bytes(struct extent_buffer *eb,1480static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
1458 struct btrfs_dev_item *s,1481 struct btrfs_dev_item *s,
1459 u64 val)1482 u64 val)
1460{1483{
@@ -1558,13 +1581,13 @@ static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr) · 97 unmodified lines
1558 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));1581 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1559}1582}
1560 1583
1561static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,1584static inline u64 btrfs_stripe_offset_nr(const struct extent_buffer *eb,
1562 struct btrfs_chunk *c, int nr)1585 struct btrfs_chunk *c, int nr)
1563{1586{
1564 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));1587 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1565}1588}
1566 1589
1567static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,1590static inline u64 btrfs_stripe_devid_nr(const struct extent_buffer *eb,
1568 struct btrfs_chunk *c, int nr)1591 struct btrfs_chunk *c, int nr)
1569{1592{
1570 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));1593 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
@@ -1644,31 +1667,21 @@ BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent, · 73 unmodified lines
1644BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,1667BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1645 chunk_offset, 64);1668 chunk_offset, 64);
1646BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);1669BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1647
1648static inline unsigned long btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1649{
1650 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1651 return (unsigned long)dev + ptr;
1652}
1653
1654BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);1670BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1655BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,1671BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1656 generation, 64);1672 generation, 64);
1657BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);1673BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1658 1674
1659BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
1660
1661
1662BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);1675BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1663 1676
1664static inline void btrfs_tree_block_key(struct extent_buffer *eb,1677static inline void btrfs_tree_block_key(const struct extent_buffer *eb,
1665 struct btrfs_tree_block_info *item,1678 struct btrfs_tree_block_info *item,
1666 struct btrfs_disk_key *key)1679 struct btrfs_disk_key *key)
1667{1680{
1668 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);1681 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1669}1682}
1670 1683
1671static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,1684static inline void btrfs_set_tree_block_key(const struct extent_buffer *eb,
1672 struct btrfs_tree_block_info *item,1685 struct btrfs_tree_block_info *item,
1673 struct btrfs_disk_key *key)1686 struct btrfs_disk_key *key)
1674{1687{
@@ -1706,12 +1719,6 @@ static inline u32 btrfs_extent_inline_ref_size(int type) · 31 unmodified lines
1706 return 0;1719 return 0;
1707}1720}
1708 1721
1709BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
1710BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
1711 generation, 64);
1712BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
1713BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
1714
1715/* struct btrfs_node */1722/* struct btrfs_node */
1716BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);1723BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1717BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);1724BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
@@ -1720,7 +1727,7 @@ BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr, · 2 unmodified lines
1720BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,1727BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1721 generation, 64);1728 generation, 64);
1722 1729
1723static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)1730static inline u64 btrfs_node_blockptr(const struct extent_buffer *eb, int nr)
1724{1731{
1725 unsigned long ptr;1732 unsigned long ptr;
1726 ptr = offsetof(struct btrfs_node, ptrs) +1733 ptr = offsetof(struct btrfs_node, ptrs) +
@@ -1728,7 +1735,7 @@ static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr) · 1 unmodified line
1728 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);1735 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1729}1736}
1730 1737
1731static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,1738static inline void btrfs_set_node_blockptr(const struct extent_buffer *eb,
1732 int nr, u64 val)1739 int nr, u64 val)
1733{1740{
1734 unsigned long ptr;1741 unsigned long ptr;
@@ -1737,7 +1744,7 @@ static inline void btrfs_set_node_blockptr(struct extent_buffer *eb, · 2 unmodified lines
1737 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);1744 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1738}1745}
1739 1746
1740static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)1747static inline u64 btrfs_node_ptr_generation(const struct extent_buffer *eb, int nr)
1741{1748{
1742 unsigned long ptr;1749 unsigned long ptr;
1743 ptr = offsetof(struct btrfs_node, ptrs) +1750 ptr = offsetof(struct btrfs_node, ptrs) +
@@ -1745,7 +1752,7 @@ static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr) · 1 unmodified line
1745 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);1752 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1746}1753}
1747 1754
1748static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,1755static inline void btrfs_set_node_ptr_generation(const struct extent_buffer *eb,
1749 int nr, u64 val)1756 int nr, u64 val)
1750{1757{
1751 unsigned long ptr;1758 unsigned long ptr;
@@ -1763,7 +1770,7 @@ static inline unsigned long btrfs_node_key_ptr_offset(int nr) · 11 unmodified lines
1763void btrfs_node_key(const struct extent_buffer *eb,1770void btrfs_node_key(const struct extent_buffer *eb,
1764 struct btrfs_disk_key *disk_key, int nr);1771 struct btrfs_disk_key *disk_key, int nr);
1765 1772
1766static inline void btrfs_set_node_key(struct extent_buffer *eb,1773static inline void btrfs_set_node_key(const struct extent_buffer *eb,
1767 struct btrfs_disk_key *disk_key, int nr)1774 struct btrfs_disk_key *disk_key, int nr)
1768{1775{
1769 unsigned long ptr;1776 unsigned long ptr;
@@ -2498,8 +2505,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 728 unmodified lines
2498 struct btrfs_ref *generic_ref);2505 struct btrfs_ref *generic_ref);
2499 2506
2500int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);2507int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
2501void btrfs_get_block_group_trimming(struct btrfs_block_group *cache);
2502void btrfs_put_block_group_trimming(struct btrfs_block_group *cache);
2503void btrfs_clear_space_info_full(struct btrfs_fs_info *info);2508void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2504 2509
2505enum btrfs_reserve_flush_enum {2510enum btrfs_reserve_flush_enum {
@@ -2512,6 +2517,7 @@ enum btrfs_reserve_flush_enum { · 6 unmodified lines
2512 BTRFS_RESERVE_FLUSH_LIMIT,2517 BTRFS_RESERVE_FLUSH_LIMIT,
2513 BTRFS_RESERVE_FLUSH_EVICT,2518 BTRFS_RESERVE_FLUSH_EVICT,
2514 BTRFS_RESERVE_FLUSH_ALL,2519 BTRFS_RESERVE_FLUSH_ALL,
2520 BTRFS_RESERVE_FLUSH_ALL_STEAL,
2515};2521};
2516 2522
2517enum btrfs_flush_state {2523enum btrfs_flush_state {
@@ -2551,7 +2557,7 @@ void btrfs_wait_for_snapshot_creation(struct btrfs_root *root); · 33 unmodified lines
2551 2557
2552/* ctree.c */2558/* ctree.c */
2553int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,2559int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
2554 int level, int *slot);2560 int *slot);
2555int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);2561int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
2556int btrfs_previous_item(struct btrfs_root *root,2562int btrfs_previous_item(struct btrfs_root *root,
2557 struct btrfs_path *path, u64 min_objectid,2563 struct btrfs_path *path, u64 min_objectid,
@@ -2896,10 +2902,9 @@ void btrfs_free_inode(struct inode *inode); · 338 unmodified lines
2896int btrfs_drop_inode(struct inode *inode);2902int btrfs_drop_inode(struct inode *inode);
2897int __init btrfs_init_cachep(void);2903int __init btrfs_init_cachep(void);
2898void __cold btrfs_destroy_cachep(void);2904void __cold btrfs_destroy_cachep(void);
2899struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,2905struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
2900 struct btrfs_root *root, struct btrfs_path *path);2906 struct btrfs_root *root, struct btrfs_path *path);
2901struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,2907struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
2902 struct btrfs_root *root);
2903struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,2908struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
2904 struct page *page, size_t pg_offset,2909 struct page *page, size_t pg_offset,
2905 u64 start, u64 end);2910 u64 start, u64 end);
@@ -2929,6 +2934,9 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end); · 23 unmodified lines
2929void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,2934void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2930 u64 end, int uptodate);2935 u64 end, int uptodate);
2931extern const struct dentry_operations btrfs_dentry_operations;2936extern const struct dentry_operations btrfs_dentry_operations;
2937ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
2938extern const struct iomap_ops btrfs_dio_iomap_ops;
2939extern const struct iomap_dio_ops btrfs_dops;
2932 2940
2933/* ioctl.c */2941/* ioctl.c */
2934long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);2942long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
@@ -3381,6 +3389,9 @@ void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, · 446 unmodified lines
3381int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,3389int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3382 struct btrfs_pending_snapshot *pending);3390 struct btrfs_pending_snapshot *pending);
3383int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);3391int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);
3392struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info,
3393 u64 bytenr);
3394int btrfs_should_ignore_reloc_root(struct btrfs_root *root);
3384 3395
3385/* scrub.c */3396/* scrub.c */
3386int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,3397int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
fs/btrfs/disk-io.c+51−42
@@ -358,16 +358,14 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · 357 unmodified lines
358 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);358 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
359 359
360 shash->tfm = fs_info->csum_shash;360 shash->tfm = fs_info->csum_shash;
361 crypto_shash_init(shash);
362 361
363 /*362 /*
364 * The super_block structure does not span the whole363 * The super_block structure does not span the whole
365 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is364 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
366 * filled with zeros and is included in the checksum.365 * filled with zeros and is included in the checksum.
367 */366 */
368 crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE,367 crypto_shash_digest(shash, raw_disk_sb + BTRFS_CSUM_SIZE,
369 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);368 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
370 crypto_shash_final(shash, result);
371 369
372 if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))370 if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))
373 return 1;371 return 1;
@@ -709,9 +707,7 @@ static void end_workqueue_bio(struct bio *bio) · 335 unmodified lines
709 else707 else
710 wq = fs_info->endio_write_workers;708 wq = fs_info->endio_write_workers;
711 } else {709 } else {
712 if (unlikely(end_io_wq->metadata == BTRFS_WQ_ENDIO_DIO_REPAIR))710 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56)
713 wq = fs_info->endio_repair_workers;
714 else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56)
715 wq = fs_info->endio_raid56_workers;711 wq = fs_info->endio_raid56_workers;
716 else if (end_io_wq->metadata)712 else if (end_io_wq->metadata)
717 wq = fs_info->endio_meta_workers;713 wq = fs_info->endio_meta_workers;
@@ -1135,9 +1131,12 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 417 unmodified lines
1135 root->log_transid = 0;1131 root->log_transid = 0;
1136 root->log_transid_committed = -1;1132 root->log_transid_committed = -1;
1137 root->last_log_commit = 0;1133 root->last_log_commit = 0;
1138 if (!dummy)1134 if (!dummy) {
1139 extent_io_tree_init(fs_info, &root->dirty_log_pages,1135 extent_io_tree_init(fs_info, &root->dirty_log_pages,
1140 IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL);1136 IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL);
1137 extent_io_tree_init(fs_info, &root->log_csum_range,
1138 IO_TREE_LOG_CSUM_RANGE, NULL);
1139 }
1141 1140
1142 memset(&root->root_key, 0, sizeof(root->root_key));1141 memset(&root->root_key, 0, sizeof(root->root_key));
1143 memset(&root->root_item, 0, sizeof(root->root_item));1142 memset(&root->root_item, 0, sizeof(root->root_item));
@@ -1275,12 +1274,13 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, · 131 unmodified lines
1275 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;1274 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
1276 1275
1277 /*1276 /*
1278 * DON'T set REF_COWS for log trees1277 * DON'T set SHAREABLE bit for log trees.
1279 *1278 *
1280 * log trees do not get reference counted because they go away1279 * Log trees are not exposed to user space thus can't be snapshotted,
1281 * before a real commit is actually done. They do store pointers1280 * and they go away before a real commit is actually done.
1282 * to file data extents, and those reference counts still get1281 *
1283 * updated (along with back refs to the log tree).1282 * They do store pointers to file data extents, and those reference
1283 * counts still get updated (along with back refs to the log tree).
1284 */1284 */
1285 1285
1286 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,1286 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
@@ -1418,8 +1418,9 @@ static int btrfs_init_fs_root(struct btrfs_root *root) · 131 unmodified lines
1418 if (ret)1418 if (ret)
1419 goto fail;1419 goto fail;
1420 1420
1421 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {1421 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
1422 set_bit(BTRFS_ROOT_REF_COWS, &root->state);1422 root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
1423 set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
1423 btrfs_check_and_init_root_item(&root->root_item);1424 btrfs_check_and_init_root_item(&root->root_item);
1424 }1425 }
1425 1426
@@ -1524,6 +1525,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) · 98 unmodified lines
1524 btrfs_put_root(fs_info->uuid_root);1525 btrfs_put_root(fs_info->uuid_root);
1525 btrfs_put_root(fs_info->free_space_root);1526 btrfs_put_root(fs_info->free_space_root);
1526 btrfs_put_root(fs_info->fs_root);1527 btrfs_put_root(fs_info->fs_root);
1528 btrfs_put_root(fs_info->data_reloc_root);
1527 btrfs_check_leaked_roots(fs_info);1529 btrfs_check_leaked_roots(fs_info);
1528 btrfs_extent_buffer_leak_debug_check(fs_info);1530 btrfs_extent_buffer_leak_debug_check(fs_info);
1529 kfree(fs_info->super_copy);1531 kfree(fs_info->super_copy);
@@ -1533,35 +1535,34 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) · 3 unmodified lines
1533 1535
1534 1536
1535struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,1537struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1536 struct btrfs_key *location,1538 u64 objectid, bool check_ref)
1537 bool check_ref)
1538{1539{
1539 struct btrfs_root *root;1540 struct btrfs_root *root;
1540 struct btrfs_path *path;1541 struct btrfs_path *path;
1541 struct btrfs_key key;1542 struct btrfs_key key;
1542 int ret;1543 int ret;
1543 1544
1544 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)1545 if (objectid == BTRFS_ROOT_TREE_OBJECTID)
1545 return btrfs_grab_root(fs_info->tree_root);1546 return btrfs_grab_root(fs_info->tree_root);
1546 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)1547 if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
1547 return btrfs_grab_root(fs_info->extent_root);1548 return btrfs_grab_root(fs_info->extent_root);
1548 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)1549 if (objectid == BTRFS_CHUNK_TREE_OBJECTID)
1549 return btrfs_grab_root(fs_info->chunk_root);1550 return btrfs_grab_root(fs_info->chunk_root);
1550 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)1551 if (objectid == BTRFS_DEV_TREE_OBJECTID)
1551 return btrfs_grab_root(fs_info->dev_root);1552 return btrfs_grab_root(fs_info->dev_root);
1552 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)1553 if (objectid == BTRFS_CSUM_TREE_OBJECTID)
1553 return btrfs_grab_root(fs_info->csum_root);1554 return btrfs_grab_root(fs_info->csum_root);
1554 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)1555 if (objectid == BTRFS_QUOTA_TREE_OBJECTID)
1555 return btrfs_grab_root(fs_info->quota_root) ?1556 return btrfs_grab_root(fs_info->quota_root) ?
1556 fs_info->quota_root : ERR_PTR(-ENOENT);1557 fs_info->quota_root : ERR_PTR(-ENOENT);
1557 if (location->objectid == BTRFS_UUID_TREE_OBJECTID)1558 if (objectid == BTRFS_UUID_TREE_OBJECTID)
1558 return btrfs_grab_root(fs_info->uuid_root) ?1559 return btrfs_grab_root(fs_info->uuid_root) ?
1559 fs_info->uuid_root : ERR_PTR(-ENOENT);1560 fs_info->uuid_root : ERR_PTR(-ENOENT);
1560 if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)1561 if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1561 return btrfs_grab_root(fs_info->free_space_root) ?1562 return btrfs_grab_root(fs_info->free_space_root) ?
1562 fs_info->free_space_root : ERR_PTR(-ENOENT);1563 fs_info->free_space_root : ERR_PTR(-ENOENT);
1563again:1564again:
1564 root = btrfs_lookup_fs_root(fs_info, location->objectid);1565 root = btrfs_lookup_fs_root(fs_info, objectid);
1565 if (root) {1566 if (root) {
1566 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {1567 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1567 btrfs_put_root(root);1568 btrfs_put_root(root);
@@ -1570,7 +1571,10 @@ again: · 2 unmodified lines
1570 return root;1571 return root;
1571 }1572 }
1572 1573
1573 root = btrfs_read_tree_root(fs_info->tree_root, location);1574 key.objectid = objectid;
1575 key.type = BTRFS_ROOT_ITEM_KEY;
1576 key.offset = (u64)-1;
1577 root = btrfs_read_tree_root(fs_info->tree_root, &key);
1574 if (IS_ERR(root))1578 if (IS_ERR(root))
1575 return root;1579 return root;
1576 1580
@@ -1590,7 +1594,7 @@ again: · 13 unmodified lines
1590 }1594 }
1591 key.objectid = BTRFS_ORPHAN_OBJECTID;1595 key.objectid = BTRFS_ORPHAN_OBJECTID;
1592 key.type = BTRFS_ORPHAN_ITEM_KEY;1596 key.type = BTRFS_ORPHAN_ITEM_KEY;
1593 key.offset = location->objectid;1597 key.offset = objectid;
1594 1598
1595 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);1599 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1596 btrfs_free_path(path);1600 btrfs_free_path(path);
@@ -1940,7 +1944,6 @@ static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info) · 343 unmodified lines
1940 btrfs_destroy_workqueue(fs_info->workers);1944 btrfs_destroy_workqueue(fs_info->workers);
1941 btrfs_destroy_workqueue(fs_info->endio_workers);1945 btrfs_destroy_workqueue(fs_info->endio_workers);
1942 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);1946 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
1943 btrfs_destroy_workqueue(fs_info->endio_repair_workers);
1944 btrfs_destroy_workqueue(fs_info->rmw_workers);1947 btrfs_destroy_workqueue(fs_info->rmw_workers);
1945 btrfs_destroy_workqueue(fs_info->endio_write_workers);1948 btrfs_destroy_workqueue(fs_info->endio_write_workers);
1946 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);1949 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
@@ -1981,6 +1984,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) · 34 unmodified lines
1981 free_root_extent_buffers(info->quota_root);1984 free_root_extent_buffers(info->quota_root);
1982 free_root_extent_buffers(info->uuid_root);1985 free_root_extent_buffers(info->uuid_root);
1983 free_root_extent_buffers(info->fs_root);1986 free_root_extent_buffers(info->fs_root);
1987 free_root_extent_buffers(info->data_reloc_root);
1984 if (free_chunk_root)1988 if (free_chunk_root)
1985 free_root_extent_buffers(info->chunk_root);1989 free_root_extent_buffers(info->chunk_root);
1986 free_root_extent_buffers(info->free_space_root);1990 free_root_extent_buffers(info->free_space_root);
@@ -1993,6 +1997,7 @@ void btrfs_put_root(struct btrfs_root *root) · 6 unmodified lines
1993 1997
1994 if (refcount_dec_and_test(&root->refs)) {1998 if (refcount_dec_and_test(&root->refs)) {
1995 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));1999 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2000 WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
1996 if (root->anon_dev)2001 if (root->anon_dev)
1997 free_anon_bdev(root->anon_dev);2002 free_anon_bdev(root->anon_dev);
1998 btrfs_drew_lock_destroy(&root->snapshot_lock);2003 btrfs_drew_lock_destroy(&root->snapshot_lock);
@@ -2143,8 +2148,6 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, · 144 unmodified lines
2143 fs_info->endio_raid56_workers =2148 fs_info->endio_raid56_workers =
2144 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,2149 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
2145 max_active, 4);2150 max_active, 4);
2146 fs_info->endio_repair_workers =
2147 btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
2148 fs_info->rmw_workers =2151 fs_info->rmw_workers =
2149 btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);2152 btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
2150 fs_info->endio_write_workers =2153 fs_info->endio_write_workers =
@@ -2168,7 +2171,6 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info, · 17 unmodified lines
2168 fs_info->flush_workers &&2171 fs_info->flush_workers &&
2169 fs_info->endio_workers && fs_info->endio_meta_workers &&2172 fs_info->endio_workers && fs_info->endio_meta_workers &&
2170 fs_info->endio_meta_write_workers &&2173 fs_info->endio_meta_write_workers &&
2171 fs_info->endio_repair_workers &&
2172 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&2174 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
2173 fs_info->endio_freespace_worker && fs_info->rmw_workers &&2175 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2174 fs_info->caching_workers && fs_info->readahead_workers &&2176 fs_info->caching_workers && fs_info->readahead_workers &&
@@ -2290,6 +2292,19 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) · 115 unmodified lines
2290 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);2292 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2291 fs_info->csum_root = root;2293 fs_info->csum_root = root;
2292 2294
2295 /*
2296 * This tree can share blocks with some other fs tree during relocation
2297 * and we need a proper setup by btrfs_get_fs_root
2298 */
2299 root = btrfs_get_fs_root(tree_root->fs_info,
2300 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
2301 if (IS_ERR(root)) {
2302 ret = PTR_ERR(root);
2303 goto out;
2304 }
2305 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2306 fs_info->data_reloc_root = root;
2307
2293 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;2308 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
2294 root = btrfs_read_tree_root(tree_root, &location);2309 root = btrfs_read_tree_root(tree_root, &location);
2295 if (!IS_ERR(root)) {2310 if (!IS_ERR(root)) {
@@ -2827,7 +2842,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 531 unmodified lines
2827 u64 generation;2842 u64 generation;
2828 u64 features;2843 u64 features;
2829 u16 csum_type;2844 u16 csum_type;
2830 struct btrfs_key location;
2831 struct btrfs_super_block *disk_super;2845 struct btrfs_super_block *disk_super;
2832 struct btrfs_fs_info *fs_info = btrfs_sb(sb);2846 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2833 struct btrfs_root *tree_root;2847 struct btrfs_root *tree_root;
@@ -3241,11 +3255,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 407 unmodified lines
3241 }3255 }
3242 }3256 }
3243 3257
3244 location.objectid = BTRFS_FS_TREE_OBJECTID;3258 fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
3245 location.type = BTRFS_ROOT_ITEM_KEY;
3246 location.offset = 0;
3247
3248 fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true);
3249 if (IS_ERR(fs_info->fs_root)) {3259 if (IS_ERR(fs_info->fs_root)) {
3250 err = PTR_ERR(fs_info->fs_root);3260 err = PTR_ERR(fs_info->fs_root);
3251 btrfs_warn(fs_info, "failed to read fs tree: %d", err);3261 btrfs_warn(fs_info, "failed to read fs tree: %d", err);
@@ -3508,10 +3518,9 @@ static int write_dev_supers(struct btrfs_device *device, · 256 unmodified lines
3508 3518
3509 btrfs_set_super_bytenr(sb, bytenr);3519 btrfs_set_super_bytenr(sb, bytenr);
3510 3520
3511 crypto_shash_init(shash);3521 crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
3512 crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE,3522 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
3513 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);3523 sb->csum);
3514 crypto_shash_final(shash, sb->csum);
3515 3524
3516 page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT,3525 page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT,
3517 GFP_NOFS);3526 GFP_NOFS);
fs/btrfs/disk-io.h+1−3
@@ -25,7 +25,6 @@ enum btrfs_wq_endio_type { · 24 unmodified lines
25 BTRFS_WQ_ENDIO_METADATA,25 BTRFS_WQ_ENDIO_METADATA,
26 BTRFS_WQ_ENDIO_FREE_SPACE,26 BTRFS_WQ_ENDIO_FREE_SPACE,
27 BTRFS_WQ_ENDIO_RAID56,27 BTRFS_WQ_ENDIO_RAID56,
28 BTRFS_WQ_ENDIO_DIO_REPAIR,
29};28};
30 29
31static inline u64 btrfs_sb_offset(int mirror)30static inline u64 btrfs_sb_offset(int mirror)
@@ -67,8 +66,7 @@ int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, · 35 unmodified lines
67void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info);66void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info);
68 67
69struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,68struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
70 struct btrfs_key *key,69 u64 objectid, bool check_ref);
71 bool check_ref);
72 70
73void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);71void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);
74int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);72int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);
fs/btrfs/export.c+3−14
@@ -64,24 +64,15 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid, · 63 unmodified lines
64 struct btrfs_fs_info *fs_info = btrfs_sb(sb);64 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
65 struct btrfs_root *root;65 struct btrfs_root *root;
66 struct inode *inode;66 struct inode *inode;
67 struct btrfs_key key;
68 67
69 if (objectid < BTRFS_FIRST_FREE_OBJECTID)68 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
70 return ERR_PTR(-ESTALE);69 return ERR_PTR(-ESTALE);
71 70
72 key.objectid = root_objectid;71 root = btrfs_get_fs_root(fs_info, root_objectid, true);
73 key.type = BTRFS_ROOT_ITEM_KEY;
74 key.offset = (u64)-1;
75
76 root = btrfs_get_fs_root(fs_info, &key, true);
77 if (IS_ERR(root))72 if (IS_ERR(root))
78 return ERR_CAST(root);73 return ERR_CAST(root);
79 74
80 key.objectid = objectid;75 inode = btrfs_iget(sb, objectid, root);
81 key.type = BTRFS_INODE_ITEM_KEY;
82 key.offset = 0;
83
84 inode = btrfs_iget(sb, &key, root);
85 btrfs_put_root(root);76 btrfs_put_root(root);
86 if (IS_ERR(inode))77 if (IS_ERR(inode))
87 return ERR_CAST(inode);78 return ERR_CAST(inode);
@@ -200,9 +191,7 @@ struct dentry *btrfs_get_parent(struct dentry *child) · 112 unmodified lines
200 found_key.offset, 0, 0);191 found_key.offset, 0, 0);
201 }192 }
202 193
203 key.type = BTRFS_INODE_ITEM_KEY;194 return d_obtain_alias(btrfs_iget(fs_info->sb, key.objectid, root));
204 key.offset = 0;
205 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
206fail:195fail:
207 btrfs_free_path(path);196 btrfs_free_path(path);
208 return ERR_PTR(ret);197 return ERR_PTR(ret);
fs/btrfs/extent-io-tree.h+1−0
@@ -44,6 +44,7 @@ enum { · 43 unmodified lines
44 IO_TREE_TRANS_DIRTY_PAGES,44 IO_TREE_TRANS_DIRTY_PAGES,
45 IO_TREE_ROOT_DIRTY_LOG_PAGES,45 IO_TREE_ROOT_DIRTY_LOG_PAGES,
46 IO_TREE_INODE_FILE_EXTENT,46 IO_TREE_INODE_FILE_EXTENT,
47 IO_TREE_LOG_CSUM_RANGE,
47 IO_TREE_SELFTEST,48 IO_TREE_SELFTEST,
48};49};
49 50
fs/btrfs/extent-tree.c+3−20
@@ -2114,22 +2114,6 @@ static u64 find_middle(struct rb_root *root) · 2113 unmodified lines
2114}2114}
2115#endif2115#endif
2116 2116
2117static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
2118{
2119 u64 num_bytes;
2120
2121 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2122 sizeof(struct btrfs_extent_inline_ref));
2123 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2124 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2125
2126 /*
2127 * We don't ever fill up leaves all the way so multiply by 2 just to be
2128 * closer to what we're really going to want to use.
2129 */
2130 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
2131}
2132
2133/*2117/*
2134 * Takes the number of bytes to be csumm'ed and figures out how many leaves it2118 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2135 * would require to store the csums for that many bytes.2119 * would require to store the csums for that many bytes.
@@ -2442,7 +2426,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, · 306 unmodified lines
2442 nritems = btrfs_header_nritems(buf);2426 nritems = btrfs_header_nritems(buf);
2443 level = btrfs_header_level(buf);2427 level = btrfs_header_level(buf);
2444 2428
2445 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)2429 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0)
2446 return 0;2430 return 0;
2447 2431
2448 if (full_backref)2432 if (full_backref)
@@ -2932,7 +2916,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) · 483 unmodified lines
2932 &trimmed);2916 &trimmed);
2933 2917
2934 list_del_init(&block_group->bg_list);2918 list_del_init(&block_group->bg_list);
2935 btrfs_put_block_group_trimming(block_group);2919 btrfs_unfreeze_block_group(block_group);
2936 btrfs_put_block_group(block_group);2920 btrfs_put_block_group(block_group);
2937 2921
2938 if (ret) {2922 if (ret) {
@@ -3369,6 +3353,7 @@ static struct btrfs_block_group *btrfs_lock_cluster( · 430 unmodified lines
3369 struct btrfs_block_group *block_group,3353 struct btrfs_block_group *block_group,
3370 struct btrfs_free_cluster *cluster,3354 struct btrfs_free_cluster *cluster,
3371 int delalloc)3355 int delalloc)
3356 __acquires(&cluster->refill_lock)
3372{3357{
3373 struct btrfs_block_group *used_bg = NULL;3358 struct btrfs_block_group *used_bg = NULL;
3374 3359
@@ -5501,8 +5486,6 @@ out: · 2126 unmodified lines
5501 */5486 */
5502 if (!for_reloc && !root_dropped)5487 if (!for_reloc && !root_dropped)
5503 btrfs_add_dead_root(root);5488 btrfs_add_dead_root(root);
5504 if (err && err != -EAGAIN)
5505 btrfs_handle_fs_error(fs_info, err, NULL);
5506 return err;5489 return err;
5507}5490}
5508 5491
fs/btrfs/extent_io.c+132−156
@@ -2333,7 +2333,7 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, · 2332 unmodified lines
2333 return 0;2333 return 0;
2334}2334}
2335 2335
2336int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num)2336int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num)
2337{2337{
2338 struct btrfs_fs_info *fs_info = eb->fs_info;2338 struct btrfs_fs_info *fs_info = eb->fs_info;
2339 u64 start = eb->start;2339 u64 start = eb->start;
@@ -2537,8 +2537,9 @@ int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end, · 197 unmodified lines
2537 return 0;2537 return 0;
2538}2538}
2539 2539
2540bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,2540static bool btrfs_check_repairable(struct inode *inode, bool needs_validation,
2541 struct io_failure_record *failrec, int failed_mirror)2541 struct io_failure_record *failrec,
2542 int failed_mirror)
2542{2543{
2543 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);2544 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2544 int num_copies;2545 int num_copies;
@@ -2561,7 +2562,7 @@ bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages, · 16 unmodified lines
2561 * a) deliver good data to the caller2562 * a) deliver good data to the caller
2562 * b) correct the bad sectors on disk2563 * b) correct the bad sectors on disk
2563 */2564 */
2564 if (failed_bio_pages > 1) {2565 if (needs_validation) {
2565 /*2566 /*
2566 * to fulfill b), we need to know the exact failing sectors, as2567 * to fulfill b), we need to know the exact failing sectors, as
2567 * we don't want to rewrite any more than the failed ones. thus,2568 * we don't want to rewrite any more than the failed ones. thus,
@@ -2600,94 +2601,115 @@ bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages, · 32 unmodified lines
2600 return true;2601 return true;
2601}2602}
2602 2603
2603 2604static bool btrfs_io_needs_validation(struct inode *inode, struct bio *bio)
2604struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2605 struct io_failure_record *failrec,
2606 struct page *page, int pg_offset, int icsum,
2607 bio_end_io_t *endio_func, void *data)
2608{2605{
2609 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);2606 u64 len = 0;
2610 struct bio *bio;2607 const u32 blocksize = inode->i_sb->s_blocksize;
2611 struct btrfs_io_bio *btrfs_failed_bio;
2612 struct btrfs_io_bio *btrfs_bio;
2613 2608
2614 bio = btrfs_io_bio_alloc(1);2609 /*
2615 bio->bi_end_io = endio_func;2610 * If bi_status is BLK_STS_OK, then this was a checksum error, not an
2616 bio->bi_iter.bi_sector = failrec->logical >> 9;2611 * I/O error. In this case, we already know exactly which sector was
2617 bio->bi_iter.bi_size = 0;2612 * bad, so we don't need to validate.
2618 bio->bi_private = data;2613 */
2614 if (bio->bi_status == BLK_STS_OK)
2615 return false;
2619 2616
2620 btrfs_failed_bio = btrfs_io_bio(failed_bio);2617 /*
2621 if (btrfs_failed_bio->csum) {2618 * We need to validate each sector individually if the failed I/O was
2622 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);2619 * for multiple sectors.
2620 *
2621 * There are a few possible bios that can end up here:
2622 * 1. A buffered read bio, which is not cloned.
2623 * 2. A direct I/O read bio, which is cloned.
2624 * 3. A (buffered or direct) repair bio, which is not cloned.
2625 *
2626 * For cloned bios (case 2), we can get the size from
2627 * btrfs_io_bio->iter; for non-cloned bios (cases 1 and 3), we can get
2628 * it from the bvecs.
2629 */
2630 if (bio_flagged(bio, BIO_CLONED)) {
2631 if (btrfs_io_bio(bio)->iter.bi_size > blocksize)
2632 return true;
2633 } else {
2634 struct bio_vec *bvec;
2635 int i;
2623 2636
2624 btrfs_bio = btrfs_io_bio(bio);2637 bio_for_each_bvec_all(bvec, bio, i) {
2625 btrfs_bio->csum = btrfs_bio->csum_inline;2638 len += bvec->bv_len;
2626 icsum *= csum_size;2639 if (len > blocksize)
2627 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,2640 return true;
2628 csum_size);2641 }
2629 }2642 }
2630 2643 return false;
2631 bio_add_page(bio, page, failrec->len, pg_offset);
2632
2633 return bio;
2634}2644}
2635 2645
2636/*2646blk_status_t btrfs_submit_read_repair(struct inode *inode,
2637 * This is a generic handler for readpage errors. If other copies exist, read2647 struct bio *failed_bio, u64 phy_offset,
2638 * those and write back good data to the failed position. Does not investigate2648 struct page *page, unsigned int pgoff,
2639 * in remapping the failed extent elsewhere, hoping the device will be smart2649 u64 start, u64 end, int failed_mirror,
2640 * enough to do this as needed2650 submit_bio_hook_t *submit_bio_hook)
2641 */
2642static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2643 struct page *page, u64 start, u64 end,
2644 int failed_mirror)
2645{2651{
2646 struct io_failure_record *failrec;2652 struct io_failure_record *failrec;
2647 struct inode *inode = page->mapping->host;2653 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2648 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;2654 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2649 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;2655 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2650 struct bio *bio;2656 struct btrfs_io_bio *failed_io_bio = btrfs_io_bio(failed_bio);
2651 int read_mode = 0;2657 const int icsum = phy_offset >> inode->i_sb->s_blocksize_bits;
2658 bool need_validation;
2659 struct bio *repair_bio;
2660 struct btrfs_io_bio *repair_io_bio;
2652 blk_status_t status;2661 blk_status_t status;
2653 int ret;2662 int ret;
2654 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;2663
2664 btrfs_debug(fs_info,
2665 "repair read error: read error at %llu", start);
2655 2666
2656 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);2667 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
2657 2668
2658 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);2669 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2659 if (ret)2670 if (ret)
2660 return ret;2671 return errno_to_blk_status(ret);
2672
2673 need_validation = btrfs_io_needs_validation(inode, failed_bio);
2661 2674
2662 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,2675 if (!btrfs_check_repairable(inode, need_validation, failrec,
2663 failed_mirror)) {2676 failed_mirror)) {
2664 free_io_failure(failure_tree, tree, failrec);2677 free_io_failure(failure_tree, tree, failrec);
2665 return -EIO;2678 return BLK_STS_IOERR;
2666 }2679 }
2667 2680
2668 if (failed_bio_pages > 1)2681 repair_bio = btrfs_io_bio_alloc(1);
2669 read_mode |= REQ_FAILFAST_DEV;2682 repair_io_bio = btrfs_io_bio(repair_bio);
2683 repair_bio->bi_opf = REQ_OP_READ;
2684 if (need_validation)
2685 repair_bio->bi_opf |= REQ_FAILFAST_DEV;
2686 repair_bio->bi_end_io = failed_bio->bi_end_io;
2687 repair_bio->bi_iter.bi_sector = failrec->logical >> 9;
2688 repair_bio->bi_private = failed_bio->bi_private;
2670 2689
2671 phy_offset >>= inode->i_sb->s_blocksize_bits;2690 if (failed_io_bio->csum) {
2672 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,2691 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2673 start - page_offset(page),2692
2674 (int)phy_offset, failed_bio->bi_end_io,2693 repair_io_bio->csum = repair_io_bio->csum_inline;
2675 NULL);2694 memcpy(repair_io_bio->csum,
2676 bio->bi_opf = REQ_OP_READ | read_mode;2695 failed_io_bio->csum + csum_size * icsum, csum_size);
2696 }
2697
2698 bio_add_page(repair_bio, page, failrec->len, pgoff);
2699 repair_io_bio->logical = failrec->start;
2700 repair_io_bio->iter = repair_bio->bi_iter;
2677 2701
2678 btrfs_debug(btrfs_sb(inode->i_sb),2702 btrfs_debug(btrfs_sb(inode->i_sb),
2679 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",2703"repair read error: submitting new read to mirror %d, in_validation=%d",
2680 read_mode, failrec->this_mirror, failrec->in_validation);2704 failrec->this_mirror, failrec->in_validation);
2681 2705
2682 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,2706 status = submit_bio_hook(inode, repair_bio, failrec->this_mirror,
2683 failrec->bio_flags);2707 failrec->bio_flags);
2684 if (status) {2708 if (status) {
2685 free_io_failure(failure_tree, tree, failrec);2709 free_io_failure(failure_tree, tree, failrec);
2686 bio_put(bio);2710 bio_put(repair_bio);
2687 ret = blk_status_to_errno(status);
2688 }2711 }
2689 2712 return status;
2690 return ret;
2691}2713}
2692 2714
2693/* lots and lots of room for performance fixes in the end_bio funcs */2715/* lots and lots of room for performance fixes in the end_bio funcs */
@@ -2859,9 +2881,10 @@ static void end_bio_extent_readpage(struct bio *bio) · 165 unmodified lines
2859 * If it can't handle the error it will return -EIO and2881 * If it can't handle the error it will return -EIO and
2860 * we remain responsible for that page.2882 * we remain responsible for that page.
2861 */2883 */
2862 ret = bio_readpage_error(bio, offset, page, start, end,2884 if (!btrfs_submit_read_repair(inode, bio, offset, page,
2863 mirror);2885 start - page_offset(page),
2864 if (ret == 0) {2886 start, end, mirror,
2887 tree->ops->submit_bio_hook)) {
2865 uptodate = !bio->bi_status;2888 uptodate = !bio->bi_status;
2866 offset += len;2889 offset += len;
2867 continue;2890 continue;
@@ -4862,7 +4885,7 @@ static void __free_extent_buffer(struct extent_buffer *eb) · 1994 unmodified lines
4862 kmem_cache_free(extent_buffer_cache, eb);4885 kmem_cache_free(extent_buffer_cache, eb);
4863}4886}
4864 4887
4865int extent_buffer_under_io(struct extent_buffer *eb)4888int extent_buffer_under_io(const struct extent_buffer *eb)
4866{4889{
4867 return (atomic_read(&eb->io_pages) ||4890 return (atomic_read(&eb->io_pages) ||
4868 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||4891 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
@@ -4967,7 +4990,7 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, · 98 unmodified lines
4967 return eb;4990 return eb;
4968}4991}
4969 4992
4970struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)4993struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
4971{4994{
4972 int i;4995 int i;
4973 struct page *p;4996 struct page *p;
@@ -5373,7 +5396,7 @@ void free_extent_buffer_stale(struct extent_buffer *eb) · 399 unmodified lines
5373 release_extent_buffer(eb);5396 release_extent_buffer(eb);
5374}5397}
5375 5398
5376void clear_extent_buffer_dirty(struct extent_buffer *eb)5399void clear_extent_buffer_dirty(const struct extent_buffer *eb)
5377{5400{
5378 int i;5401 int i;
5379 int num_pages;5402 int num_pages;
@@ -5571,8 +5594,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, · 191 unmodified lines
5571 struct page *page;5594 struct page *page;
5572 char *kaddr;5595 char *kaddr;
5573 char *dst = (char *)dstv;5596 char *dst = (char *)dstv;
5574 size_t start_offset = offset_in_page(eb->start);5597 unsigned long i = start >> PAGE_SHIFT;
5575 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5576 5598
5577 if (start + len > eb->len) {5599 if (start + len > eb->len) {
5578 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",5600 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
@@ -5581,7 +5603,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, · 2 unmodified lines
5581 return;5603 return;
5582 }5604 }
5583 5605
5584 offset = offset_in_page(start_offset + start);5606 offset = offset_in_page(start);
5585 5607
5586 while (len > 0) {5608 while (len > 0) {
5587 page = eb->pages[i];5609 page = eb->pages[i];
@@ -5606,14 +5628,13 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb, · 18 unmodified lines
5606 struct page *page;5628 struct page *page;
5607 char *kaddr;5629 char *kaddr;
5608 char __user *dst = (char __user *)dstv;5630 char __user *dst = (char __user *)dstv;
5609 size_t start_offset = offset_in_page(eb->start);5631 unsigned long i = start >> PAGE_SHIFT;
5610 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5611 int ret = 0;5632 int ret = 0;
5612 5633
5613 WARN_ON(start > eb->len);5634 WARN_ON(start > eb->len);
5614 WARN_ON(start + len > eb->start + eb->len);5635 WARN_ON(start + len > eb->start + eb->len);
5615 5636
5616 offset = offset_in_page(start_offset + start);5637 offset = offset_in_page(start);
5617 5638
5618 while (len > 0) {5639 while (len > 0) {
5619 page = eb->pages[i];5640 page = eb->pages[i];
@@ -5634,48 +5655,6 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb, · 14 unmodified lines
5634 return ret;5655 return ret;
5635}5656}
5636 5657
5637/*
5638 * return 0 if the item is found within a page.
5639 * return 1 if the item spans two pages.
5640 * return -EINVAL otherwise.
5641 */
5642int map_private_extent_buffer(const struct extent_buffer *eb,
5643 unsigned long start, unsigned long min_len,
5644 char **map, unsigned long *map_start,
5645 unsigned long *map_len)
5646{
5647 size_t offset;
5648 char *kaddr;
5649 struct page *p;
5650 size_t start_offset = offset_in_page(eb->start);
5651 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5652 unsigned long end_i = (start_offset + start + min_len - 1) >>
5653 PAGE_SHIFT;
5654
5655 if (start + min_len > eb->len) {
5656 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5657 eb->start, eb->len, start, min_len);
5658 return -EINVAL;
5659 }
5660
5661 if (i != end_i)
5662 return 1;
5663
5664 if (i == 0) {
5665 offset = start_offset;
5666 *map_start = 0;
5667 } else {
5668 offset = 0;
5669 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
5670 }
5671
5672 p = eb->pages[i];
5673 kaddr = page_address(p);
5674 *map = kaddr + offset;
5675 *map_len = PAGE_SIZE - offset;
5676 return 0;
5677}
5678
5679int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,5658int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5680 unsigned long start, unsigned long len)5659 unsigned long start, unsigned long len)
5681{5660{
@@ -5684,14 +5663,13 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, · 2 unmodified lines
5684 struct page *page;5663 struct page *page;
5685 char *kaddr;5664 char *kaddr;
5686 char *ptr = (char *)ptrv;5665 char *ptr = (char *)ptrv;
5687 size_t start_offset = offset_in_page(eb->start);5666 unsigned long i = start >> PAGE_SHIFT;
5688 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5689 int ret = 0;5667 int ret = 0;
5690 5668
5691 WARN_ON(start > eb->len);5669 WARN_ON(start > eb->len);
5692 WARN_ON(start + len > eb->start + eb->len);5670 WARN_ON(start + len > eb->start + eb->len);
5693 5671
5694 offset = offset_in_page(start_offset + start);5672 offset = offset_in_page(start);
5695 5673
5696 while (len > 0) {5674 while (len > 0) {
5697 page = eb->pages[i];5675 page = eb->pages[i];
@@ -5711,7 +5689,7 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, · 13 unmodified lines
5711 return ret;5689 return ret;
5712}5690}
5713 5691
5714void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,5692void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
5715 const void *srcv)5693 const void *srcv)
5716{5694{
5717 char *kaddr;5695 char *kaddr;
@@ -5722,7 +5700,7 @@ void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, · 4 unmodified lines
5722 BTRFS_FSID_SIZE);5700 BTRFS_FSID_SIZE);
5723}5701}
5724 5702
5725void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)5703void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv)
5726{5704{
5727 char *kaddr;5705 char *kaddr;
5728 5706
@@ -5732,7 +5710,7 @@ void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) · 3 unmodified lines
5732 BTRFS_FSID_SIZE);5710 BTRFS_FSID_SIZE);
5733}5711}
5734 5712
5735void write_extent_buffer(struct extent_buffer *eb, const void *srcv,5713void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
5736 unsigned long start, unsigned long len)5714 unsigned long start, unsigned long len)
5737{5715{
5738 size_t cur;5716 size_t cur;
@@ -5740,13 +5718,12 @@ void write_extent_buffer(struct extent_buffer *eb, const void *srcv, · 1 unmodified line
5740 struct page *page;5718 struct page *page;
5741 char *kaddr;5719 char *kaddr;
5742 char *src = (char *)srcv;5720 char *src = (char *)srcv;
5743 size_t start_offset = offset_in_page(eb->start);5721 unsigned long i = start >> PAGE_SHIFT;
5744 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5745 5722
5746 WARN_ON(start > eb->len);5723 WARN_ON(start > eb->len);
5747 WARN_ON(start + len > eb->start + eb->len);5724 WARN_ON(start + len > eb->start + eb->len);
5748 5725
5749 offset = offset_in_page(start_offset + start);5726 offset = offset_in_page(start);
5750 5727
5751 while (len > 0) {5728 while (len > 0) {
5752 page = eb->pages[i];5729 page = eb->pages[i];
@@ -5763,20 +5740,19 @@ void write_extent_buffer(struct extent_buffer *eb, const void *srcv, · 10 unmodified lines
5763 }5740 }
5764}5741}
5765 5742
5766void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,5743void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
5767 unsigned long len)5744 unsigned long len)
5768{5745{
5769 size_t cur;5746 size_t cur;
5770 size_t offset;5747 size_t offset;
5771 struct page *page;5748 struct page *page;
5772 char *kaddr;5749 char *kaddr;
5773 size_t start_offset = offset_in_page(eb->start);5750 unsigned long i = start >> PAGE_SHIFT;
5774 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5775 5751
5776 WARN_ON(start > eb->len);5752 WARN_ON(start > eb->len);
5777 WARN_ON(start + len > eb->start + eb->len);5753 WARN_ON(start + len > eb->start + eb->len);
5778 5754
5779 offset = offset_in_page(start_offset + start);5755 offset = offset_in_page(start);
5780 5756
5781 while (len > 0) {5757 while (len > 0) {
5782 page = eb->pages[i];5758 page = eb->pages[i];
@@ -5792,8 +5768,8 @@ void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, · 9 unmodified lines
5792 }5768 }
5793}5769}
5794 5770
5795void copy_extent_buffer_full(struct extent_buffer *dst,5771void copy_extent_buffer_full(const struct extent_buffer *dst,
5796 struct extent_buffer *src)5772 const struct extent_buffer *src)
5797{5773{
5798 int i;5774 int i;
5799 int num_pages;5775 int num_pages;
@@ -5806,7 +5782,8 @@ void copy_extent_buffer_full(struct extent_buffer *dst, · 6 unmodified lines
5806 page_address(src->pages[i]));5782 page_address(src->pages[i]));
5807}5783}
5808 5784
5809void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,5785void copy_extent_buffer(const struct extent_buffer *dst,
5786 const struct extent_buffer *src,
5810 unsigned long dst_offset, unsigned long src_offset,5787 unsigned long dst_offset, unsigned long src_offset,
5811 unsigned long len)5788 unsigned long len)
5812{5789{
@@ -5815,12 +5792,11 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, · 2 unmodified lines
5815 size_t offset;5792 size_t offset;
5816 struct page *page;5793 struct page *page;
5817 char *kaddr;5794 char *kaddr;
5818 size_t start_offset = offset_in_page(dst->start);5795 unsigned long i = dst_offset >> PAGE_SHIFT;
5819 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
5820 5796
5821 WARN_ON(src->len != dst_len);5797 WARN_ON(src->len != dst_len);
5822 5798
5823 offset = offset_in_page(start_offset + dst_offset);5799 offset = offset_in_page(dst_offset);
5824 5800
5825 while (len > 0) {5801 while (len > 0) {
5826 page = dst->pages[i];5802 page = dst->pages[i];
@@ -5851,12 +5827,11 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, · 24 unmodified lines
5851 * This helper hides the ugliness of finding the byte in an extent buffer which5827 * This helper hides the ugliness of finding the byte in an extent buffer which
5852 * contains a given bit.5828 * contains a given bit.
5853 */5829 */
5854static inline void eb_bitmap_offset(struct extent_buffer *eb,5830static inline void eb_bitmap_offset(const struct extent_buffer *eb,
5855 unsigned long start, unsigned long nr,5831 unsigned long start, unsigned long nr,
5856 unsigned long *page_index,5832 unsigned long *page_index,
5857 size_t *page_offset)5833 size_t *page_offset)
5858{5834{
5859 size_t start_offset = offset_in_page(eb->start);
5860 size_t byte_offset = BIT_BYTE(nr);5835 size_t byte_offset = BIT_BYTE(nr);
5861 size_t offset;5836 size_t offset;
5862 5837
@@ -5865,7 +5840,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb, · 2 unmodified lines
5865 * the bitmap item in the extent buffer + the offset of the byte in the5840 * the bitmap item in the extent buffer + the offset of the byte in the
5866 * bitmap item.5841 * bitmap item.
5867 */5842 */
5868 offset = start_offset + start + byte_offset;5843 offset = start + byte_offset;
5869 5844
5870 *page_index = offset >> PAGE_SHIFT;5845 *page_index = offset >> PAGE_SHIFT;
5871 *page_offset = offset_in_page(offset);5846 *page_offset = offset_in_page(offset);
@@ -5877,7 +5852,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb, · 5 unmodified lines
5877 * @start: offset of the bitmap item in the extent buffer5852 * @start: offset of the bitmap item in the extent buffer
5878 * @nr: bit number to test5853 * @nr: bit number to test
5879 */5854 */
5880int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,5855int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
5881 unsigned long nr)5856 unsigned long nr)
5882{5857{
5883 u8 *kaddr;5858 u8 *kaddr;
@@ -5899,7 +5874,7 @@ int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, · 15 unmodified lines
5899 * @pos: bit number of the first bit5874 * @pos: bit number of the first bit
5900 * @len: number of bits to set5875 * @len: number of bits to set
5901 */5876 */
5902void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,5877void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
5903 unsigned long pos, unsigned long len)5878 unsigned long pos, unsigned long len)
5904{5879{
5905 u8 *kaddr;5880 u8 *kaddr;
@@ -5941,8 +5916,9 @@ void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, · 35 unmodified lines
5941 * @pos: bit number of the first bit5916 * @pos: bit number of the first bit
5942 * @len: number of bits to clear5917 * @len: number of bits to clear
5943 */5918 */
5944void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,5919void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
5945 unsigned long pos, unsigned long len)5920 unsigned long start, unsigned long pos,
5921 unsigned long len)
5946{5922{
5947 u8 *kaddr;5923 u8 *kaddr;
5948 struct page *page;5924 struct page *page;
@@ -6003,14 +5979,14 @@ static void copy_pages(struct page *dst_page, struct page *src_page, · 54 unmodified lines
6003 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);5979 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
6004}5980}
6005 5981
6006void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,5982void memcpy_extent_buffer(const struct extent_buffer *dst,
6007 unsigned long src_offset, unsigned long len)5983 unsigned long dst_offset, unsigned long src_offset,
5984 unsigned long len)
6008{5985{
6009 struct btrfs_fs_info *fs_info = dst->fs_info;5986 struct btrfs_fs_info *fs_info = dst->fs_info;
6010 size_t cur;5987 size_t cur;
6011 size_t dst_off_in_page;5988 size_t dst_off_in_page;
6012 size_t src_off_in_page;5989 size_t src_off_in_page;
6013 size_t start_offset = offset_in_page(dst->start);
6014 unsigned long dst_i;5990 unsigned long dst_i;
6015 unsigned long src_i;5991 unsigned long src_i;
6016 5992
@@ -6028,11 +6004,11 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, · 11 unmodified lines
6028 }6004 }
6029 6005
6030 while (len > 0) {6006 while (len > 0) {
6031 dst_off_in_page = offset_in_page(start_offset + dst_offset);6007 dst_off_in_page = offset_in_page(dst_offset);
6032 src_off_in_page = offset_in_page(start_offset + src_offset);6008 src_off_in_page = offset_in_page(src_offset);
6033 6009
6034 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;6010 dst_i = dst_offset >> PAGE_SHIFT;
6035 src_i = (start_offset + src_offset) >> PAGE_SHIFT;6011 src_i = src_offset >> PAGE_SHIFT;
6036 6012
6037 cur = min(len, (unsigned long)(PAGE_SIZE -6013 cur = min(len, (unsigned long)(PAGE_SIZE -
6038 src_off_in_page));6014 src_off_in_page));
@@ -6048,8 +6024,9 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, · 9 unmodified lines
6048 }6024 }
6049}6025}
6050 6026
6051void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,6027void memmove_extent_buffer(const struct extent_buffer *dst,
6052 unsigned long src_offset, unsigned long len)6028 unsigned long dst_offset, unsigned long src_offset,
6029 unsigned long len)
6053{6030{
6054 struct btrfs_fs_info *fs_info = dst->fs_info;6031 struct btrfs_fs_info *fs_info = dst->fs_info;
6055 size_t cur;6032 size_t cur;
@@ -6057,7 +6034,6 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, · 1 unmodified line
6057 size_t src_off_in_page;6034 size_t src_off_in_page;
6058 unsigned long dst_end = dst_offset + len - 1;6035 unsigned long dst_end = dst_offset + len - 1;
6059 unsigned long src_end = src_offset + len - 1;6036 unsigned long src_end = src_offset + len - 1;
6060 size_t start_offset = offset_in_page(dst->start);
6061 unsigned long dst_i;6037 unsigned long dst_i;
6062 unsigned long src_i;6038 unsigned long src_i;
6063 6039
@@ -6078,11 +6054,11 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, · 14 unmodified lines
6078 return;6054 return;
6079 }6055 }
6080 while (len > 0) {6056 while (len > 0) {
6081 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;6057 dst_i = dst_end >> PAGE_SHIFT;
6082 src_i = (start_offset + src_end) >> PAGE_SHIFT;6058 src_i = src_end >> PAGE_SHIFT;
6083 6059
6084 dst_off_in_page = offset_in_page(start_offset + dst_end);6060 dst_off_in_page = offset_in_page(dst_end);
6085 src_off_in_page = offset_in_page(start_offset + src_end);6061 src_off_in_page = offset_in_page(src_end);
6086 6062
6087 cur = min_t(unsigned long, len, src_off_in_page + 1);6063 cur = min_t(unsigned long, len, src_off_in_page + 1);
6088 cur = min(cur, dst_off_in_page + 1);6064 cur = min(cur, dst_off_in_page + 1);
fs/btrfs/extent_io.h+35−32
@@ -66,6 +66,10 @@ struct btrfs_io_bio; · 65 unmodified lines
66struct io_failure_record;66struct io_failure_record;
67struct extent_io_tree;67struct extent_io_tree;
68 68
69typedef blk_status_t (submit_bio_hook_t)(struct inode *inode, struct bio *bio,
70 int mirror_num,
71 unsigned long bio_flags);
72
69typedef blk_status_t (extent_submit_bio_start_t)(void *private_data,73typedef blk_status_t (extent_submit_bio_start_t)(void *private_data,
70 struct bio *bio, u64 bio_offset);74 struct bio *bio, u64 bio_offset);
71 75
@@ -74,8 +78,7 @@ struct extent_io_ops { · 2 unmodified lines
74 * The following callbacks must be always defined, the function78 * The following callbacks must be always defined, the function
75 * pointer will be called unconditionally.79 * pointer will be called unconditionally.
76 */80 */
77 blk_status_t (*submit_bio_hook)(struct inode *inode, struct bio *bio,81 submit_bio_hook_t *submit_bio_hook;
78 int mirror_num, unsigned long bio_flags);
79 int (*readpage_end_io_hook)(struct btrfs_io_bio *io_bio, u64 phy_offset,82 int (*readpage_end_io_hook)(struct btrfs_io_bio *io_bio, u64 phy_offset,
80 struct page *page, u64 start, u64 end,83 struct page *page, u64 start, u64 end,
81 int mirror);84 int mirror);
@@ -209,7 +212,7 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, · 127 unmodified lines
209 u64 start, unsigned long len);212 u64 start, unsigned long len);
210struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,213struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
211 u64 start);214 u64 start);
212struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src);215struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src);
213struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,216struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
214 u64 start);217 u64 start);
215void free_extent_buffer(struct extent_buffer *eb);218void free_extent_buffer(struct extent_buffer *eb);
@@ -227,7 +230,7 @@ static inline int num_extent_pages(const struct extent_buffer *eb) · 11 unmodified lines
227 (eb->start >> PAGE_SHIFT);230 (eb->start >> PAGE_SHIFT);
228}231}
229 232
230static inline int extent_buffer_uptodate(struct extent_buffer *eb)233static inline int extent_buffer_uptodate(const struct extent_buffer *eb)
231{234{
232 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);235 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
233}236}
@@ -240,37 +243,37 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dst, · 6 unmodified lines
240int read_extent_buffer_to_user(const struct extent_buffer *eb,243int read_extent_buffer_to_user(const struct extent_buffer *eb,
241 void __user *dst, unsigned long start,244 void __user *dst, unsigned long start,
242 unsigned long len);245 unsigned long len);
243void write_extent_buffer_fsid(struct extent_buffer *eb, const void *src);246void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *src);
244void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,247void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
245 const void *src);248 const void *src);
246void write_extent_buffer(struct extent_buffer *eb, const void *src,249void write_extent_buffer(const struct extent_buffer *eb, const void *src,
247 unsigned long start, unsigned long len);250 unsigned long start, unsigned long len);
248void copy_extent_buffer_full(struct extent_buffer *dst,251void copy_extent_buffer_full(const struct extent_buffer *dst,
249 struct extent_buffer *src);252 const struct extent_buffer *src);
250void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,253void copy_extent_buffer(const struct extent_buffer *dst,
254 const struct extent_buffer *src,
251 unsigned long dst_offset, unsigned long src_offset,255 unsigned long dst_offset, unsigned long src_offset,
252 unsigned long len);256 unsigned long len);
253void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,257void memcpy_extent_buffer(const struct extent_buffer *dst,
254 unsigned long src_offset, unsigned long len);258 unsigned long dst_offset, unsigned long src_offset,
255void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,259 unsigned long len);
256 unsigned long src_offset, unsigned long len);260void memmove_extent_buffer(const struct extent_buffer *dst,
257void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,261 unsigned long dst_offset, unsigned long src_offset,
262 unsigned long len);
263void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
258 unsigned long len);264 unsigned long len);
259int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,265int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
260 unsigned long pos);266 unsigned long pos);
261void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,267void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
262 unsigned long pos, unsigned long len);268 unsigned long pos, unsigned long len);
263void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,269void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
264 unsigned long pos, unsigned long len);270 unsigned long start, unsigned long pos,
265void clear_extent_buffer_dirty(struct extent_buffer *eb);271 unsigned long len);
272void clear_extent_buffer_dirty(const struct extent_buffer *eb);
266bool set_extent_buffer_dirty(struct extent_buffer *eb);273bool set_extent_buffer_dirty(struct extent_buffer *eb);
267void set_extent_buffer_uptodate(struct extent_buffer *eb);274void set_extent_buffer_uptodate(struct extent_buffer *eb);
268void clear_extent_buffer_uptodate(struct extent_buffer *eb);275void clear_extent_buffer_uptodate(struct extent_buffer *eb);
269int extent_buffer_under_io(struct extent_buffer *eb);276int extent_buffer_under_io(const struct extent_buffer *eb);
270int map_private_extent_buffer(const struct extent_buffer *eb,
271 unsigned long offset, unsigned long min_len,
272 char **map, unsigned long *map_start,
273 unsigned long *map_len);
274void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);277void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
275void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);278void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
276void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,279void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
@@ -289,7 +292,7 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, · 12 unmodified lines
289 u64 length, u64 logical, struct page *page,292 u64 length, u64 logical, struct page *page,
290 unsigned int pg_offset, int mirror_num);293 unsigned int pg_offset, int mirror_num);
291void end_extent_writepage(struct page *page, int err, u64 start, u64 end);294void end_extent_writepage(struct page *page, int err, u64 start, u64 end);
292int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num);295int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num);
293 296
294/*297/*
295 * When IO fails, either with EIO or csum verification fails, we298 * When IO fails, either with EIO or csum verification fails, we
@@ -311,12 +314,12 @@ struct io_failure_record { · 15 unmodified lines
311};314};
312 315
313 316
314bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,317blk_status_t btrfs_submit_read_repair(struct inode *inode,
315 struct io_failure_record *failrec, int fail_mirror);318 struct bio *failed_bio, u64 phy_offset,
316struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,319 struct page *page, unsigned int pgoff,
317 struct io_failure_record *failrec,320 u64 start, u64 end, int failed_mirror,
318 struct page *page, int pg_offset, int icsum,321 submit_bio_hook_t *submit_bio_hook);
319 bio_end_io_t *endio_func, void *data);322
320#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS323#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
321bool find_lock_delalloc_range(struct inode *inode,324bool find_lock_delalloc_range(struct inode *inode,
322 struct page *locked_page, u64 *start,325 struct page *locked_page, u64 *start,
fs/btrfs/file-item.c+35−27
@@ -242,11 +242,13 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, · 241 unmodified lines
242/**242/**
243 * btrfs_lookup_bio_sums - Look up checksums for a bio.243 * btrfs_lookup_bio_sums - Look up checksums for a bio.
244 * @inode: inode that the bio is for.244 * @inode: inode that the bio is for.
245 * @bio: bio embedded in btrfs_io_bio.245 * @bio: bio to look up.
246 * @offset: Unless (u64)-1, look up checksums for this offset in the file.246 * @offset: Unless (u64)-1, look up checksums for this offset in the file.
247 * If (u64)-1, use the page offsets from the bio instead.247 * If (u64)-1, use the page offsets from the bio instead.
248 * @dst: Buffer of size btrfs_super_csum_size() used to return checksum. If248 * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return
249 * NULL, the checksum is returned in btrfs_io_bio(bio)->csum instead.249 * checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If
250 * NULL, the checksum buffer is allocated and returned in
251 * btrfs_io_bio(bio)->csum instead.
250 *252 *
251 * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.253 * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
252 */254 */
@@ -256,7 +258,6 @@ blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, · 3 unmodified lines
256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);258 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
257 struct bio_vec bvec;259 struct bio_vec bvec;
258 struct bvec_iter iter;260 struct bvec_iter iter;
259 struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
260 struct btrfs_csum_item *item = NULL;261 struct btrfs_csum_item *item = NULL;
261 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;262 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
262 struct btrfs_path *path;263 struct btrfs_path *path;
@@ -277,6 +278,8 @@ blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, · 14 unmodified lines
277 278
278 nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;279 nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
279 if (!dst) {280 if (!dst) {
281 struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
282
280 if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {283 if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
281 btrfs_bio->csum = kmalloc_array(nblocks, csum_size,284 btrfs_bio->csum = kmalloc_array(nblocks, csum_size,
282 GFP_NOFS);285 GFP_NOFS);
@@ -598,13 +601,12 @@ blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio, · 315 unmodified lines
598 index = 0;601 index = 0;
599 }602 }
600 603
601 crypto_shash_init(shash);
602 data = kmap_atomic(bvec.bv_page);604 data = kmap_atomic(bvec.bv_page);
603 crypto_shash_update(shash, data + bvec.bv_offset605 crypto_shash_digest(shash, data + bvec.bv_offset
604 + (i * fs_info->sectorsize),606 + (i * fs_info->sectorsize),
605 fs_info->sectorsize);607 fs_info->sectorsize,
608 sums->sums + index);
606 kunmap_atomic(data);609 kunmap_atomic(data);
607 crypto_shash_final(shash, (char *)(sums->sums + index));
608 index += csum_size;610 index += csum_size;
609 offset += fs_info->sectorsize;611 offset += fs_info->sectorsize;
610 this_sum_bytes += fs_info->sectorsize;612 this_sum_bytes += fs_info->sectorsize;
@@ -869,7 +871,7 @@ again: · 258 unmodified lines
869 }871 }
870 ret = PTR_ERR(item);872 ret = PTR_ERR(item);
871 if (ret != -EFBIG && ret != -ENOENT)873 if (ret != -EFBIG && ret != -ENOENT)
872 goto fail_unlock;874 goto out;
873 875
874 if (ret == -EFBIG) {876 if (ret == -EFBIG) {
875 u32 item_size;877 u32 item_size;
@@ -887,10 +889,12 @@ again: · 11 unmodified lines
887 nritems = btrfs_header_nritems(path->nodes[0]);889 nritems = btrfs_header_nritems(path->nodes[0]);
888 if (!nritems || (path->slots[0] >= nritems - 1)) {890 if (!nritems || (path->slots[0] >= nritems - 1)) {
889 ret = btrfs_next_leaf(root, path);891 ret = btrfs_next_leaf(root, path);
890 if (ret == 1)892 if (ret < 0) {
893 goto out;
894 } else if (ret > 0) {
891 found_next = 1;895 found_next = 1;
892 if (ret != 0)
893 goto insert;896 goto insert;
897 }
894 slot = path->slots[0];898 slot = path->slots[0];
895 }899 }
896 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);900 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
@@ -905,14 +909,27 @@ again: · 8 unmodified lines
905 }909 }
906 910
907 /*911 /*
908 * at this point, we know the tree has an item, but it isn't big912 * At this point, we know the tree has a checksum item that ends at an
909 * enough yet to put our csum in. Grow it913 * offset matching the start of the checksum range we want to insert.
914 * We try to extend that item as much as possible and then add as many
915 * checksums to it as they fit.
916 *
917 * First check if the leaf has enough free space for at least one
918 * checksum. If it has go directly to the item extension code, otherwise
919 * release the path and do a search for insertion before the extension.
910 */920 */
921 if (btrfs_leaf_free_space(leaf) >= csum_size) {
922 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
923 csum_offset = (bytenr - found_key.offset) >>
924 fs_info->sb->s_blocksize_bits;
925 goto extend_csum;
926 }
927
911 btrfs_release_path(path);928 btrfs_release_path(path);
912 ret = btrfs_search_slot(trans, root, &file_key, path,929 ret = btrfs_search_slot(trans, root, &file_key, path,
913 csum_size, 1);930 csum_size, 1);
914 if (ret < 0)931 if (ret < 0)
915 goto fail_unlock;932 goto out;
916 933
917 if (ret > 0) {934 if (ret > 0) {
918 if (path->slots[0] == 0)935 if (path->slots[0] == 0)
@@ -931,19 +948,13 @@ again: · 12 unmodified lines
931 goto insert;948 goto insert;
932 }949 }
933 950
951extend_csum:
934 if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /952 if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
935 csum_size) {953 csum_size) {
936 int extend_nr;954 int extend_nr;
937 u64 tmp;955 u64 tmp;
938 u32 diff;956 u32 diff;
939 u32 free_space;
940
941 if (btrfs_leaf_free_space(leaf) <
942 sizeof(struct btrfs_item) + csum_size * 2)
943 goto insert;
944 957
945 free_space = btrfs_leaf_free_space(leaf) -
946 sizeof(struct btrfs_item) - csum_size;
947 tmp = sums->len - total_bytes;958 tmp = sums->len - total_bytes;
948 tmp >>= fs_info->sb->s_blocksize_bits;959 tmp >>= fs_info->sb->s_blocksize_bits;
949 WARN_ON(tmp < 1);960 WARN_ON(tmp < 1);
@@ -954,7 +965,7 @@ again: · 4 unmodified lines
954 MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);965 MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
955 966
956 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);967 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
957 diff = min(free_space, diff);968 diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
958 diff /= csum_size;969 diff /= csum_size;
959 diff *= csum_size;970 diff *= csum_size;
960 971
@@ -985,9 +996,9 @@ insert: · 24 unmodified lines
985 ins_size);996 ins_size);
986 path->leave_spinning = 0;997 path->leave_spinning = 0;
987 if (ret < 0)998 if (ret < 0)
988 goto fail_unlock;999 goto out;
989 if (WARN_ON(ret != 0))1000 if (WARN_ON(ret != 0))
990 goto fail_unlock;1001 goto out;
991 leaf = path->nodes[0];1002 leaf = path->nodes[0];
992csum:1003csum:
993 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);1004 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
@@ -1017,9 +1028,6 @@ found: · 23 unmodified lines
1017out:1028out:
1018 btrfs_free_path(path);1029 btrfs_free_path(path);
1019 return ret;1030 return ret;
1020
1021fail_unlock:
1022 goto out;
1023}1031}
1024 1032
1025void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,1033void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
fs/btrfs/file.c+94−17
@@ -275,26 +275,18 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info, · 274 unmodified lines
275{275{
276 struct btrfs_root *inode_root;276 struct btrfs_root *inode_root;
277 struct inode *inode;277 struct inode *inode;
278 struct btrfs_key key;
279 struct btrfs_ioctl_defrag_range_args range;278 struct btrfs_ioctl_defrag_range_args range;
280 int num_defrag;279 int num_defrag;
281 int ret;280 int ret;
282 281
283 /* get the inode */282 /* get the inode */
284 key.objectid = defrag->root;283 inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
285 key.type = BTRFS_ROOT_ITEM_KEY;
286 key.offset = (u64)-1;
287
288 inode_root = btrfs_get_fs_root(fs_info, &key, true);
289 if (IS_ERR(inode_root)) {284 if (IS_ERR(inode_root)) {
290 ret = PTR_ERR(inode_root);285 ret = PTR_ERR(inode_root);
291 goto cleanup;286 goto cleanup;
292 }287 }
293 288
294 key.objectid = defrag->ino;289 inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
295 key.type = BTRFS_INODE_ITEM_KEY;
296 key.offset = 0;
297 inode = btrfs_iget(fs_info->sb, &key, inode_root);
298 btrfs_put_root(inode_root);290 btrfs_put_root(inode_root);
299 if (IS_ERR(inode)) {291 if (IS_ERR(inode)) {
300 ret = PTR_ERR(inode);292 ret = PTR_ERR(inode);
@@ -775,7 +767,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans, · 474 unmodified lines
775 if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)767 if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
776 modify_tree = 0;768 modify_tree = 0;
777 769
778 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||770 update_refs = (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
779 root == fs_info->tree_root);771 root == fs_info->tree_root);
780 while (1) {772 while (1) {
781 recow = 0;773 recow = 0;
@@ -1817,21 +1809,61 @@ again: · 1035 unmodified lines
1817 return num_written ? num_written : ret;1809 return num_written ? num_written : ret;
1818}1810}
1819 1811
1820static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)1812static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
1813 const struct iov_iter *iter, loff_t offset)
1814{
1815 const unsigned int blocksize_mask = fs_info->sectorsize - 1;
1816
1817 if (offset & blocksize_mask)
1818 return -EINVAL;
1819
1820 if (iov_iter_alignment(iter) & blocksize_mask)
1821 return -EINVAL;
1822
1823 return 0;
1824}
1825
1826static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
1821{1827{
1822 struct file *file = iocb->ki_filp;1828 struct file *file = iocb->ki_filp;
1823 struct inode *inode = file_inode(file);1829 struct inode *inode = file_inode(file);
1824 loff_t pos;1830 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1825 ssize_t written;1831 loff_t pos = iocb->ki_pos;
1832 ssize_t written = 0;
1826 ssize_t written_buffered;1833 ssize_t written_buffered;
1827 loff_t endbyte;1834 loff_t endbyte;
1828 int err;1835 int err;
1836 size_t count = 0;
1837 bool relock = false;
1829 1838
1830 written = generic_file_direct_write(iocb, from);1839 if (check_direct_IO(fs_info, from, pos))
1840 goto buffered;
1841
1842 count = iov_iter_count(from);
1843 /*
1844 * If the write DIO is beyond the EOF, we need update the isize, but it
1845 * is protected by i_mutex. So we can not unlock the i_mutex at this
1846 * case.
1847 */
1848 if (pos + count <= inode->i_size) {
1849 inode_unlock(inode);
1850 relock = true;
1851 } else if (iocb->ki_flags & IOCB_NOWAIT) {
1852 return -EAGAIN;
1853 }
1854
1855 down_read(&BTRFS_I(inode)->dio_sem);
1856 written = iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops, &btrfs_dops,
1857 is_sync_kiocb(iocb));
1858 up_read(&BTRFS_I(inode)->dio_sem);
1859
1860 if (relock)
1861 inode_lock(inode);
1831 1862
1832 if (written < 0 || !iov_iter_count(from))1863 if (written < 0 || !iov_iter_count(from))
1833 return written;1864 return written;
1834 1865
1866buffered:
1835 pos = iocb->ki_pos;1867 pos = iocb->ki_pos;
1836 written_buffered = btrfs_buffered_write(iocb, from);1868 written_buffered = btrfs_buffered_write(iocb, from);
1837 if (written_buffered < 0) {1869 if (written_buffered < 0) {
@@ -1970,7 +2002,7 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb, · 132 unmodified lines
1970 atomic_inc(&BTRFS_I(inode)->sync_writers);2002 atomic_inc(&BTRFS_I(inode)->sync_writers);
1971 2003
1972 if (iocb->ki_flags & IOCB_DIRECT) {2004 if (iocb->ki_flags & IOCB_DIRECT) {
1973 num_written = __btrfs_direct_write(iocb, from);2005 num_written = btrfs_direct_write(iocb, from);
1974 } else {2006 } else {
1975 num_written = btrfs_buffered_write(iocb, from);2007 num_written = btrfs_buffered_write(iocb, from);
1976 if (num_written > 0)2008 if (num_written > 0)
@@ -3484,9 +3516,54 @@ static int btrfs_file_open(struct inode *inode, struct file *filp) · 1507 unmodified lines
3484 return generic_file_open(inode, filp);3516 return generic_file_open(inode, filp);
3485}3517}
3486 3518
3519static int check_direct_read(struct btrfs_fs_info *fs_info,
3520 const struct iov_iter *iter, loff_t offset)
3521{
3522 int ret;
3523 int i, seg;
3524
3525 ret = check_direct_IO(fs_info, iter, offset);
3526 if (ret < 0)
3527 return ret;
3528
3529 for (seg = 0; seg < iter->nr_segs; seg++)
3530 for (i = seg + 1; i < iter->nr_segs; i++)
3531 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
3532 return -EINVAL;
3533 return 0;
3534}
3535
3536static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
3537{
3538 struct inode *inode = file_inode(iocb->ki_filp);
3539 ssize_t ret;
3540
3541 if (check_direct_read(btrfs_sb(inode->i_sb), to, iocb->ki_pos))
3542 return 0;
3543
3544 inode_lock_shared(inode);
3545 ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dops,
3546 is_sync_kiocb(iocb));
3547 inode_unlock_shared(inode);
3548 return ret;
3549}
3550
3551static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3552{
3553 ssize_t ret = 0;
3554
3555 if (iocb->ki_flags & IOCB_DIRECT) {
3556 ret = btrfs_direct_read(iocb, to);
3557 if (ret < 0)
3558 return ret;
3559 }
3560
3561 return generic_file_buffered_read(iocb, to, ret);
3562}
3563
3487const struct file_operations btrfs_file_operations = {3564const struct file_operations btrfs_file_operations = {
3488 .llseek = btrfs_file_llseek,3565 .llseek = btrfs_file_llseek,
3489 .read_iter = generic_file_read_iter,3566 .read_iter = btrfs_file_read_iter,
3490 .splice_read = generic_file_splice_read,3567 .splice_read = generic_file_splice_read,
3491 .write_iter = btrfs_file_write_iter,3568 .write_iter = btrfs_file_write_iter,
3492 .mmap = btrfs_file_mmap,3569 .mmap = btrfs_file_mmap,
fs/btrfs/free-space-cache.c+17−64
@@ -82,7 +82,7 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root, · 81 unmodified lines
82 * sure NOFS is set to keep us from deadlocking.82 * sure NOFS is set to keep us from deadlocking.
83 */83 */
84 nofs_flag = memalloc_nofs_save();84 nofs_flag = memalloc_nofs_save();
85 inode = btrfs_iget_path(fs_info->sb, &location, root, path);85 inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
86 btrfs_release_path(path);86 btrfs_release_path(path);
87 memalloc_nofs_restore(nofs_flag);87 memalloc_nofs_restore(nofs_flag);
88 if (IS_ERR(inode))88 if (IS_ERR(inode))
@@ -1190,13 +1190,10 @@ out: · 1101 unmodified lines
1190 if (ret) {1190 if (ret) {
1191 invalidate_inode_pages2(inode->i_mapping);1191 invalidate_inode_pages2(inode->i_mapping);
1192 BTRFS_I(inode)->generation = 0;1192 BTRFS_I(inode)->generation = 0;
1193 if (block_group) {1193 if (block_group)
1194#ifdef CONFIG_BTRFS_DEBUG1194 btrfs_debug(root->fs_info,
1195 btrfs_err(root->fs_info,1195 "failed to write free space cache for block group %llu error %d",
1196 "failed to write free space cache for block group %llu",1196 block_group->start, ret);
1197 block_group->start);
1198#endif
1199 }
1200 }1197 }
1201 btrfs_update_inode(trans, root, inode);1198 btrfs_update_inode(trans, root, inode);
1202 1199
@@ -1415,11 +1412,9 @@ int btrfs_write_out_cache(struct btrfs_trans_handle *trans, · 212 unmodified lines
1415 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,1412 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1416 block_group, &block_group->io_ctl, trans);1413 block_group, &block_group->io_ctl, trans);
1417 if (ret) {1414 if (ret) {
1418#ifdef CONFIG_BTRFS_DEBUG1415 btrfs_debug(fs_info,
1419 btrfs_err(fs_info,1416 "failed to write free space cache for block group %llu error %d",
1420 "failed to write free space cache for block group %llu",1417 block_group->start, ret);
1421 block_group->start);
1422#endif
1423 spin_lock(&block_group->lock);1418 spin_lock(&block_group->lock);
1424 block_group->disk_cache_state = BTRFS_DC_ERROR;1419 block_group->disk_cache_state = BTRFS_DC_ERROR;
1425 spin_unlock(&block_group->lock);1420 spin_unlock(&block_group->lock);
@@ -3762,46 +3757,6 @@ out: · 2336 unmodified lines
3762 return ret;3757 return ret;
3763}3758}
3764 3759
3765void btrfs_get_block_group_trimming(struct btrfs_block_group *cache)
3766{
3767 atomic_inc(&cache->trimming);
3768}
3769
3770void btrfs_put_block_group_trimming(struct btrfs_block_group *block_group)
3771{
3772 struct btrfs_fs_info *fs_info = block_group->fs_info;
3773 struct extent_map_tree *em_tree;
3774 struct extent_map *em;
3775 bool cleanup;
3776
3777 spin_lock(&block_group->lock);
3778 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3779 block_group->removed);
3780 spin_unlock(&block_group->lock);
3781
3782 if (cleanup) {
3783 mutex_lock(&fs_info->chunk_mutex);
3784 em_tree = &fs_info->mapping_tree;
3785 write_lock(&em_tree->lock);
3786 em = lookup_extent_mapping(em_tree, block_group->start,
3787 1);
3788 BUG_ON(!em); /* logic error, can't happen */
3789 remove_extent_mapping(em_tree, em);
3790 write_unlock(&em_tree->lock);
3791 mutex_unlock(&fs_info->chunk_mutex);
3792
3793 /* once for us and once for the tree */
3794 free_extent_map(em);
3795 free_extent_map(em);
3796
3797 /*
3798 * We've left one free space entry and other tasks trimming
3799 * this block group have left 1 entry each one. Free them.
3800 */
3801 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3802 }
3803}
3804
3805int btrfs_trim_block_group(struct btrfs_block_group *block_group,3760int btrfs_trim_block_group(struct btrfs_block_group *block_group,
3806 u64 *trimmed, u64 start, u64 end, u64 minlen)3761 u64 *trimmed, u64 start, u64 end, u64 minlen)
3807{3762{
@@ -3816,7 +3771,7 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group, · 8 unmodified lines
3816 spin_unlock(&block_group->lock);3771 spin_unlock(&block_group->lock);
3817 return 0;3772 return 0;
3818 }3773 }
3819 btrfs_get_block_group_trimming(block_group);3774 btrfs_freeze_block_group(block_group);
3820 spin_unlock(&block_group->lock);3775 spin_unlock(&block_group->lock);
3821 3776
3822 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);3777 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
@@ -3829,7 +3784,7 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group, · 6 unmodified lines
3829 if (rem)3784 if (rem)
3830 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));3785 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
3831out:3786out:
3832 btrfs_put_block_group_trimming(block_group);3787 btrfs_unfreeze_block_group(block_group);
3833 return ret;3788 return ret;
3834}3789}
3835 3790
@@ -3846,11 +3801,11 @@ int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group, · 10 unmodified lines
3846 spin_unlock(&block_group->lock);3801 spin_unlock(&block_group->lock);
3847 return 0;3802 return 0;
3848 }3803 }
3849 btrfs_get_block_group_trimming(block_group);3804 btrfs_freeze_block_group(block_group);
3850 spin_unlock(&block_group->lock);3805 spin_unlock(&block_group->lock);
3851 3806
3852 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);3807 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
3853 btrfs_put_block_group_trimming(block_group);3808 btrfs_unfreeze_block_group(block_group);
3854 3809
3855 return ret;3810 return ret;
3856}3811}
@@ -3868,13 +3823,13 @@ int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group, · 11 unmodified lines
3868 spin_unlock(&block_group->lock);3823 spin_unlock(&block_group->lock);
3869 return 0;3824 return 0;
3870 }3825 }
3871 btrfs_get_block_group_trimming(block_group);3826 btrfs_freeze_block_group(block_group);
3872 spin_unlock(&block_group->lock);3827 spin_unlock(&block_group->lock);
3873 3828
3874 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,3829 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
3875 async);3830 async);
3876 3831
3877 btrfs_put_block_group_trimming(block_group);3832 btrfs_unfreeze_block_group(block_group);
3878 3833
3879 return ret;3834 return ret;
3880}3835}
@@ -4035,11 +3990,9 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root, · 154 unmodified lines
4035 if (release_metadata)3990 if (release_metadata)
4036 btrfs_delalloc_release_metadata(BTRFS_I(inode),3991 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4037 inode->i_size, true);3992 inode->i_size, true);
4038#ifdef CONFIG_BTRFS_DEBUG3993 btrfs_debug(fs_info,
4039 btrfs_err(fs_info,3994 "failed to write free ino cache for root %llu error %d",
4040 "failed to write free ino cache for root %llu",3995 root->root_key.objectid, ret);
4041 root->root_key.objectid);
4042#endif
4043 }3996 }
4044 3997
4045 return ret;3998 return ret;
fs/btrfs/inode.c+452−867
@@ -5,7 +5,6 @@ · 4 unmodified lines
5 5
6#include <linux/kernel.h>6#include <linux/kernel.h>
7#include <linux/bio.h>7#include <linux/bio.h>
8#include <linux/buffer_head.h>
9#include <linux/file.h>8#include <linux/file.h>
10#include <linux/fs.h>9#include <linux/fs.h>
11#include <linux/pagemap.h>10#include <linux/pagemap.h>
@@ -49,17 +48,18 @@ · 37 unmodified lines
49#include "qgroup.h"48#include "qgroup.h"
50#include "delalloc-space.h"49#include "delalloc-space.h"
51#include "block-group.h"50#include "block-group.h"
51#include "space-info.h"
52 52
53struct btrfs_iget_args {53struct btrfs_iget_args {
54 struct btrfs_key *location;54 u64 ino;
55 struct btrfs_root *root;55 struct btrfs_root *root;
56};56};
57 57
58struct btrfs_dio_data {58struct btrfs_dio_data {
59 u64 reserve;59 u64 reserve;
60 u64 unsubmitted_oe_range_start;60 loff_t length;
61 u64 unsubmitted_oe_range_end;61 ssize_t submitted;
62 int overwrite;62 struct extent_changeset *data_reserved;
63};63};
64 64
65static const struct inode_operations btrfs_dir_inode_operations;65static const struct inode_operations btrfs_dir_inode_operations;
@@ -1142,7 +1142,7 @@ out_unlock: · 1076 unmodified lines
1142 */1142 */
1143 if (extent_reserved) {1143 if (extent_reserved) {
1144 extent_clear_unlock_delalloc(inode, start,1144 extent_clear_unlock_delalloc(inode, start,
1145 start + cur_alloc_size,1145 start + cur_alloc_size - 1,
1146 locked_page,1146 locked_page,
1147 clear_bits,1147 clear_bits,
1148 page_ops);1148 page_ops);
@@ -1355,6 +1355,66 @@ static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, · 206 unmodified lines
1355 return 1;1355 return 1;
1356}1356}
1357 1357
1358static int fallback_to_cow(struct inode *inode, struct page *locked_page,
1359 const u64 start, const u64 end,
1360 int *page_started, unsigned long *nr_written)
1361{
1362 const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode));
1363 const u64 range_bytes = end + 1 - start;
1364 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1365 u64 range_start = start;
1366 u64 count;
1367
1368 /*
1369 * If EXTENT_NORESERVE is set it means that when the buffered write was
1370 * made we had not enough available data space and therefore we did not
1371 * reserve data space for it, since we though we could do NOCOW for the
1372 * respective file range (either there is prealloc extent or the inode
1373 * has the NOCOW bit set).
1374 *
1375 * However when we need to fallback to COW mode (because for example the
1376 * block group for the corresponding extent was turned to RO mode by a
1377 * scrub or relocation) we need to do the following:
1378 *
1379 * 1) We increment the bytes_may_use counter of the data space info.
1380 * If COW succeeds, it allocates a new data extent and after doing
1381 * that it decrements the space info's bytes_may_use counter and
1382 * increments its bytes_reserved counter by the same amount (we do
1383 * this at btrfs_add_reserved_bytes()). So we need to increment the
1384 * bytes_may_use counter to compensate (when space is reserved at
1385 * buffered write time, the bytes_may_use counter is incremented);
1386 *
1387 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1388 * that if the COW path fails for any reason, it decrements (through
1389 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1390 * data space info, which we incremented in the step above.
1391 *
1392 * If we need to fallback to cow and the inode corresponds to a free
1393 * space cache inode, we must also increment bytes_may_use of the data
1394 * space_info for the same reason. Space caches always get a prealloc
1395 * extent for them, however scrub or balance may have set the block
1396 * group that contains that extent to RO mode.
1397 */
1398 count = count_range_bits(io_tree, &range_start, end, range_bytes,
1399 EXTENT_NORESERVE, 0);
1400 if (count > 0 || is_space_ino) {
1401 const u64 bytes = is_space_ino ? range_bytes : count;
1402 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1403 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1404
1405 spin_lock(&sinfo->lock);
1406 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1407 spin_unlock(&sinfo->lock);
1408
1409 if (count > 0)
1410 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1411 0, 0, NULL);
1412 }
1413
1414 return cow_file_range(inode, locked_page, start, end, page_started,
1415 nr_written, 1);
1416}
1417
1358/*1418/*
1359 * when nowcow writeback call back. This checks for snapshots or COW copies1419 * when nowcow writeback call back. This checks for snapshots or COW copies
1360 * of the extents that exist in the file, and COWs the file as required.1420 * of the extents that exist in the file, and COWs the file as required.
@@ -1602,9 +1662,9 @@ out_check: · 241 unmodified lines
1602 * NOCOW, following one which needs to be COW'ed1662 * NOCOW, following one which needs to be COW'ed
1603 */1663 */
1604 if (cow_start != (u64)-1) {1664 if (cow_start != (u64)-1) {
1605 ret = cow_file_range(inode, locked_page,1665 ret = fallback_to_cow(inode, locked_page, cow_start,
1606 cow_start, found_key.offset - 1,1666 found_key.offset - 1,
1607 page_started, nr_written, 1);1667 page_started, nr_written);
1608 if (ret) {1668 if (ret) {
1609 if (nocow)1669 if (nocow)
1610 btrfs_dec_nocow_writers(fs_info,1670 btrfs_dec_nocow_writers(fs_info,
@@ -1693,8 +1753,8 @@ out_check: · 82 unmodified lines
1693 1753
1694 if (cow_start != (u64)-1) {1754 if (cow_start != (u64)-1) {
1695 cur_offset = end;1755 cur_offset = end;
1696 ret = cow_file_range(inode, locked_page, cow_start, end,1756 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1697 page_started, nr_written, 1);1757 page_started, nr_written);
1698 if (ret)1758 if (ret)
1699 goto error;1759 goto error;
1700 }1760 }
@@ -2726,10 +2786,9 @@ void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, · 1025 unmodified lines
2726 btrfs_queue_work(wq, &ordered_extent->work);2786 btrfs_queue_work(wq, &ordered_extent->work);
2727}2787}
2728 2788
2729static int __readpage_endio_check(struct inode *inode,2789static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2730 struct btrfs_io_bio *io_bio,2790 int icsum, struct page *page, int pgoff, u64 start,
2731 int icsum, struct page *page,2791 size_t len)
2732 int pgoff, u64 start, size_t len)
2733{2792{
2734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);2793 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2735 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);2794 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
@@ -2743,9 +2802,7 @@ static int __readpage_endio_check(struct inode *inode, · 7 unmodified lines
2743 kaddr = kmap_atomic(page);2802 kaddr = kmap_atomic(page);
2744 shash->tfm = fs_info->csum_shash;2803 shash->tfm = fs_info->csum_shash;
2745 2804
2746 crypto_shash_init(shash);2805 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
2747 crypto_shash_update(shash, kaddr + pgoff, len);
2748 crypto_shash_final(shash, csum);
2749 2806
2750 if (memcmp(csum, csum_expected, csum_size))2807 if (memcmp(csum, csum_expected, csum_size))
2751 goto zeroit;2808 goto zeroit;
@@ -2790,8 +2847,8 @@ static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, · 38 unmodified lines
2790 }2847 }
2791 2848
2792 phy_offset >>= inode->i_sb->s_blocksize_bits;2849 phy_offset >>= inode->i_sb->s_blocksize_bits;
2793 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,2850 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2794 start, (size_t)(end - start + 1));2851 (size_t)(end - start + 1));
2795}2852}
2796 2853
2797/*2854/*
@@ -2981,7 +3038,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root) · 183 unmodified lines
2981 found_key.objectid = found_key.offset;3038 found_key.objectid = found_key.offset;
2982 found_key.type = BTRFS_INODE_ITEM_KEY;3039 found_key.type = BTRFS_INODE_ITEM_KEY;
2983 found_key.offset = 0;3040 found_key.offset = 0;
2984 inode = btrfs_iget(fs_info->sb, &found_key, root);3041 inode = btrfs_iget(fs_info->sb, last_objectid, root);
2985 ret = PTR_ERR_OR_ZERO(inode);3042 ret = PTR_ERR_OR_ZERO(inode);
2986 if (ret && ret != -ENOENT)3043 if (ret && ret != -ENOENT)
2987 goto out;3044 goto out;
@@ -3000,18 +3057,16 @@ int btrfs_orphan_cleanup(struct btrfs_root *root) · 12 unmodified lines
3000 * orphan must not get deleted.3057 * orphan must not get deleted.
3001 * find_dead_roots already ran before us, so if this3058 * find_dead_roots already ran before us, so if this
3002 * is a snapshot deletion, we should find the root3059 * is a snapshot deletion, we should find the root
3003 * in the dead_roots list3060 * in the fs_roots radix tree.
3004 */3061 */
3005 spin_lock(&fs_info->trans_lock);3062
3006 list_for_each_entry(dead_root, &fs_info->dead_roots,3063 spin_lock(&fs_info->fs_roots_radix_lock);
3007 root_list) {3064 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3008 if (dead_root->root_key.objectid ==3065 (unsigned long)found_key.objectid);
3009 found_key.objectid) {3066 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3010 is_dead_root = 1;3067 is_dead_root = 1;
3011 break;3068 spin_unlock(&fs_info->fs_roots_radix_lock);
3012 }3069
3013 }
3014 spin_unlock(&fs_info->trans_lock);
3015 if (is_dead_root) {3070 if (is_dead_root) {
3016 /* prevent this orphan from being found again */3071 /* prevent this orphan from being found again */
3017 key.offset = found_key.objectid - 1;3072 key.offset = found_key.objectid - 1;
@@ -3357,43 +3412,40 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 339 unmodified lines
3357 3412
3358 btrfs_init_map_token(&token, leaf);3413 btrfs_init_map_token(&token, leaf);
3359 3414
3360 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);3415 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3361 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);3416 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3362 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,3417 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3363 &token);3418 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3364 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);3419 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3365 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);3420
3366 3421 btrfs_set_token_timespec_sec(&token, &item->atime,
3367 btrfs_set_token_timespec_sec(leaf, &item->atime,3422 inode->i_atime.tv_sec);
3368 inode->i_atime.tv_sec, &token);3423 btrfs_set_token_timespec_nsec(&token, &item->atime,
3369 btrfs_set_token_timespec_nsec(leaf, &item->atime,3424 inode->i_atime.tv_nsec);
3370 inode->i_atime.tv_nsec, &token);3425
3371 3426 btrfs_set_token_timespec_sec(&token, &item->mtime,
3372 btrfs_set_token_timespec_sec(leaf, &item->mtime,3427 inode->i_mtime.tv_sec);
3373 inode->i_mtime.tv_sec, &token);3428 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3374 btrfs_set_token_timespec_nsec(leaf, &item->mtime,3429 inode->i_mtime.tv_nsec);
3375 inode->i_mtime.tv_nsec, &token);3430
3376 3431 btrfs_set_token_timespec_sec(&token, &item->ctime,
3377 btrfs_set_token_timespec_sec(leaf, &item->ctime,3432 inode->i_ctime.tv_sec);
3378 inode->i_ctime.tv_sec, &token);3433 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3379 btrfs_set_token_timespec_nsec(leaf, &item->ctime,3434 inode->i_ctime.tv_nsec);
3380 inode->i_ctime.tv_nsec, &token);3435
3381 3436 btrfs_set_token_timespec_sec(&token, &item->otime,
3382 btrfs_set_token_timespec_sec(leaf, &item->otime,3437 BTRFS_I(inode)->i_otime.tv_sec);
3383 BTRFS_I(inode)->i_otime.tv_sec, &token);3438 btrfs_set_token_timespec_nsec(&token, &item->otime,
3384 btrfs_set_token_timespec_nsec(leaf, &item->otime,3439 BTRFS_I(inode)->i_otime.tv_nsec);
3385 BTRFS_I(inode)->i_otime.tv_nsec, &token);3440
3386 3441 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3387 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),3442 btrfs_set_token_inode_generation(&token, item,
3388 &token);3443 BTRFS_I(inode)->generation);
3389 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,3444 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3390 &token);3445 btrfs_set_token_inode_transid(&token, item, trans->transid);
3391 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),3446 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3392 &token);3447 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3393 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);3448 btrfs_set_token_inode_block_group(&token, item, 0);
3394 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3395 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3396 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3397}3449}
3398 3450
3399/*3451/*
@@ -3618,7 +3670,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) · 218 unmodified lines
3618 * 1 for the inode ref3670 * 1 for the inode ref
3619 * 1 for the inode3671 * 1 for the inode
3620 */3672 */
3621 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);3673 return btrfs_start_transaction_fallback_global_rsv(root, 5);
3622}3674}
3623 3675
3624static int btrfs_unlink(struct inode *dir, struct dentry *dentry)3676static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
@@ -4108,11 +4160,12 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · 483 unmodified lines
4108 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);4160 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4109 4161
4110 /*4162 /*
4111 * for non-free space inodes and ref cows, we want to back off from4163 * For non-free space inodes and non-shareable roots, we want to back
4112 * time to time4164 * off from time to time. This means all inodes in subvolume roots,
4165 * reloc roots, and data reloc roots.
4113 */4166 */
4114 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&4167 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
4115 test_bit(BTRFS_ROOT_REF_COWS, &root->state))4168 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4116 be_nice = true;4169 be_nice = true;
4117 4170
4118 path = btrfs_alloc_path();4171 path = btrfs_alloc_path();
@@ -4120,20 +4173,19 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · 1 unmodified line
4120 return -ENOMEM;4173 return -ENOMEM;
4121 path->reada = READA_BACK;4174 path->reada = READA_BACK;
4122 4175
4123 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)4176 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4124 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,4177 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4125 &cached_state);4178 &cached_state);
4126 4179
4127 /*4180 /*
4128 * We want to drop from the next block forward in case this new size is4181 * We want to drop from the next block forward in case this
4129 * not block aligned since we will be keeping the last block of the4182 * new size is not block aligned since we will be keeping the
4130 * extent just the way it is.4183 * last block of the extent just the way it is.
4131 */4184 */
4132 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4133 root == fs_info->tree_root)
4134 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,4185 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
4135 fs_info->sectorsize),4186 fs_info->sectorsize),
4136 (u64)-1, 0);4187 (u64)-1, 0);
4188 }
4137 4189
4138 /*4190 /*
4139 * This function is also used to drop the items in the log tree before4191 * This function is also used to drop the items in the log tree before
@@ -4241,7 +4293,7 @@ search_again: · 101 unmodified lines
4241 extent_num_bytes);4293 extent_num_bytes);
4242 num_dec = (orig_num_bytes -4294 num_dec = (orig_num_bytes -
4243 extent_num_bytes);4295 extent_num_bytes);
4244 if (test_bit(BTRFS_ROOT_REF_COWS,4296 if (test_bit(BTRFS_ROOT_SHAREABLE,
4245 &root->state) &&4297 &root->state) &&
4246 extent_start != 0)4298 extent_start != 0)
4247 inode_sub_bytes(inode, num_dec);4299 inode_sub_bytes(inode, num_dec);
@@ -4257,7 +4309,7 @@ search_again: · 9 unmodified lines
4257 num_dec = btrfs_file_extent_num_bytes(leaf, fi);4309 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4258 if (extent_start != 0) {4310 if (extent_start != 0) {
4259 found_extent = 1;4311 found_extent = 1;
4260 if (test_bit(BTRFS_ROOT_REF_COWS,4312 if (test_bit(BTRFS_ROOT_SHAREABLE,
4261 &root->state))4313 &root->state))
4262 inode_sub_bytes(inode, num_dec);4314 inode_sub_bytes(inode, num_dec);
4263 }4315 }
@@ -4293,7 +4345,7 @@ search_again: · 29 unmodified lines
4293 clear_len = fs_info->sectorsize;4345 clear_len = fs_info->sectorsize;
4294 }4346 }
4295 4347
4296 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))4348 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4297 inode_sub_bytes(inode, item_end + 1 - new_size);4349 inode_sub_bytes(inode, item_end + 1 - new_size);
4298 }4350 }
4299delete:4351delete:
@@ -4334,8 +4386,7 @@ delete: · 34 unmodified lines
4334 should_throttle = false;4386 should_throttle = false;
4335 4387
4336 if (found_extent &&4388 if (found_extent &&
4337 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||4389 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4338 root == fs_info->tree_root)) {
4339 struct btrfs_ref ref = { 0 };4390 struct btrfs_ref ref = { 0 };
4340 4391
4341 bytes_deleted += extent_num_bytes;4392 bytes_deleted += extent_num_bytes;
@@ -4759,10 +4810,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr) · 417 unmodified lines
4759 4810
4760 truncate_setsize(inode, newsize);4811 truncate_setsize(inode, newsize);
4761 4812
4762 /* Disable nonlocked read DIO to avoid the endless truncate */
4763 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
4764 inode_dio_wait(inode);4813 inode_dio_wait(inode);
4765 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
4766 4814
4767 ret = btrfs_truncate(inode, newsize == oldsize);4815 ret = btrfs_truncate(inode, newsize == oldsize);
4768 if (ret && inode->i_nlink) {4816 if (ret && inode->i_nlink) {
@@ -5154,7 +5202,7 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, · 385 unmodified lines
5154 5202
5155 btrfs_release_path(path);5203 btrfs_release_path(path);
5156 5204
5157 new_root = btrfs_get_fs_root(fs_info, location, true);5205 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
5158 if (IS_ERR(new_root)) {5206 if (IS_ERR(new_root)) {
5159 err = PTR_ERR(new_root);5207 err = PTR_ERR(new_root);
5160 goto out;5208 goto out;
@@ -5232,9 +5280,11 @@ static void inode_tree_del(struct inode *inode) · 71 unmodified lines
5232static int btrfs_init_locked_inode(struct inode *inode, void *p)5280static int btrfs_init_locked_inode(struct inode *inode, void *p)
5233{5281{
5234 struct btrfs_iget_args *args = p;5282 struct btrfs_iget_args *args = p;
5235 inode->i_ino = args->location->objectid;5283
5236 memcpy(&BTRFS_I(inode)->location, args->location,5284 inode->i_ino = args->ino;
5237 sizeof(*args->location));5285 BTRFS_I(inode)->location.objectid = args->ino;
5286 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5287 BTRFS_I(inode)->location.offset = 0;
5238 BTRFS_I(inode)->root = btrfs_grab_root(args->root);5288 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5239 BUG_ON(args->root && !BTRFS_I(inode)->root);5289 BUG_ON(args->root && !BTRFS_I(inode)->root);
5240 return 0;5290 return 0;
@@ -5243,19 +5293,19 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p) · 2 unmodified lines
5243static int btrfs_find_actor(struct inode *inode, void *opaque)5293static int btrfs_find_actor(struct inode *inode, void *opaque)
5244{5294{
5245 struct btrfs_iget_args *args = opaque;5295 struct btrfs_iget_args *args = opaque;
5246 return args->location->objectid == BTRFS_I(inode)->location.objectid &&5296
5297 return args->ino == BTRFS_I(inode)->location.objectid &&
5247 args->root == BTRFS_I(inode)->root;5298 args->root == BTRFS_I(inode)->root;
5248}5299}
5249 5300
5250static struct inode *btrfs_iget_locked(struct super_block *s,5301static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5251 struct btrfs_key *location,
5252 struct btrfs_root *root)5302 struct btrfs_root *root)
5253{5303{
5254 struct inode *inode;5304 struct inode *inode;
5255 struct btrfs_iget_args args;5305 struct btrfs_iget_args args;
5256 unsigned long hashval = btrfs_inode_hash(location->objectid, root);5306 unsigned long hashval = btrfs_inode_hash(ino, root);
5257 5307
5258 args.location = location;5308 args.ino = ino;
5259 args.root = root;5309 args.root = root;
5260 5310
5261 inode = iget5_locked(s, hashval, btrfs_find_actor,5311 inode = iget5_locked(s, hashval, btrfs_find_actor,
@@ -5265,17 +5315,17 @@ static struct inode *btrfs_iget_locked(struct super_block *s, · 3 unmodified lines
5265}5315}
5266 5316
5267/*5317/*
5268 * Get an inode object given its location and corresponding root.5318 * Get an inode object given its inode number and corresponding root.
5269 * Path can be preallocated to prevent recursing back to iget through5319 * Path can be preallocated to prevent recursing back to iget through
5270 * allocator. NULL is also valid but may require an additional allocation5320 * allocator. NULL is also valid but may require an additional allocation
5271 * later.5321 * later.
5272 */5322 */
5273struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,5323struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5274 struct btrfs_root *root, struct btrfs_path *path)5324 struct btrfs_root *root, struct btrfs_path *path)
5275{5325{
5276 struct inode *inode;5326 struct inode *inode;
5277 5327
5278 inode = btrfs_iget_locked(s, location, root);5328 inode = btrfs_iget_locked(s, ino, root);
5279 if (!inode)5329 if (!inode)
5280 return ERR_PTR(-ENOMEM);5330 return ERR_PTR(-ENOMEM);
5281 5331
@@ -5302,10 +5352,9 @@ struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location, · 20 unmodified lines
5302 return inode;5352 return inode;
5303}5353}
5304 5354
5305struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,5355struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5306 struct btrfs_root *root)
5307{5356{
5308 return btrfs_iget_path(s, location, root, NULL);5357 return btrfs_iget_path(s, ino, root, NULL);
5309}5358}
5310 5359
5311static struct inode *new_simple_dir(struct super_block *s,5360static struct inode *new_simple_dir(struct super_block *s,
@@ -5374,7 +5423,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) · 62 unmodified lines
5374 return ERR_PTR(ret);5423 return ERR_PTR(ret);
5375 5424
5376 if (location.type == BTRFS_INODE_ITEM_KEY) {5425 if (location.type == BTRFS_INODE_ITEM_KEY) {
5377 inode = btrfs_iget(dir->i_sb, &location, root);5426 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5378 if (IS_ERR(inode))5427 if (IS_ERR(inode))
5379 return inode;5428 return inode;
5380 5429
@@ -5398,7 +5447,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) · 17 unmodified lines
5398 else5447 else
5399 inode = new_simple_dir(dir->i_sb, &location, sub_root);5448 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5400 } else {5449 } else {
5401 inode = btrfs_iget(dir->i_sb, &location, sub_root);5450 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
5402 }5451 }
5403 if (root != sub_root)5452 if (root != sub_root)
5404 btrfs_put_root(sub_root);5453 btrfs_put_root(sub_root);
@@ -5779,7 +5828,8 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) · 374 unmodified lines
5779static int btrfs_insert_inode_locked(struct inode *inode)5828static int btrfs_insert_inode_locked(struct inode *inode)
5780{5829{
5781 struct btrfs_iget_args args;5830 struct btrfs_iget_args args;
5782 args.location = &BTRFS_I(inode)->location;5831
5832 args.ino = BTRFS_I(inode)->location.objectid;
5783 args.root = BTRFS_I(inode)->root;5833 args.root = BTRFS_I(inode)->root;
5784 5834
5785 return insert_inode_locked4(inode,5835 return insert_inode_locked4(inode,
@@ -6991,7 +7041,7 @@ out: · 1205 unmodified lines
6991}7041}
6992 7042
6993static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,7043static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6994 struct extent_state **cached_state, int writing)7044 struct extent_state **cached_state, bool writing)
6995{7045{
6996 struct btrfs_ordered_extent *ordered;7046 struct btrfs_ordered_extent *ordered;
6997 int ret = 0;7047 int ret = 0;
@@ -7129,30 +7179,7 @@ static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, · 131 unmodified lines
7129}7179}
7130 7180
7131 7181
7132static int btrfs_get_blocks_direct_read(struct extent_map *em,
7133 struct buffer_head *bh_result,
7134 struct inode *inode,
7135 u64 start, u64 len)
7136{
7137 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7138
7139 if (em->block_start == EXTENT_MAP_HOLE ||
7140 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7141 return -ENOENT;
7142
7143 len = min(len, em->len - (start - em->start));
7144
7145 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7146 inode->i_blkbits;
7147 bh_result->b_size = len;
7148 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7149 set_buffer_mapped(bh_result);
7150
7151 return 0;
7152}
7153
7154static int btrfs_get_blocks_direct_write(struct extent_map **map,7182static int btrfs_get_blocks_direct_write(struct extent_map **map,
7155 struct buffer_head *bh_result,
7156 struct inode *inode,7183 struct inode *inode,
7157 struct btrfs_dio_data *dio_data,7184 struct btrfs_dio_data *dio_data,
7158 u64 start, u64 len)7185 u64 start, u64 len)
@@ -7214,7 +7241,6 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, · 55 unmodified lines
7214 }7241 }
7215 7242
7216 /* this will cow the extent */7243 /* this will cow the extent */
7217 len = bh_result->b_size;
7218 free_extent_map(em);7244 free_extent_map(em);
7219 *map = em = btrfs_new_extent_direct(inode, start, len);7245 *map = em = btrfs_new_extent_direct(inode, start, len);
7220 if (IS_ERR(em)) {7246 if (IS_ERR(em)) {
@@ -7225,64 +7251,73 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, · 4 unmodified lines
7225 len = min(len, em->len - (start - em->start));7251 len = min(len, em->len - (start - em->start));
7226 7252
7227skip_cow:7253skip_cow:
7228 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7229 inode->i_blkbits;
7230 bh_result->b_size = len;
7231 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7232 set_buffer_mapped(bh_result);
7233
7234 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7235 set_buffer_new(bh_result);
7236
7237 /*7254 /*
7238 * Need to update the i_size under the extent lock so buffered7255 * Need to update the i_size under the extent lock so buffered
7239 * readers will get the updated i_size when we unlock.7256 * readers will get the updated i_size when we unlock.
7240 */7257 */
7241 if (!dio_data->overwrite && start + len > i_size_read(inode))7258 if (start + len > i_size_read(inode))
7242 i_size_write(inode, start + len);7259 i_size_write(inode, start + len);
7243 7260
7244 WARN_ON(dio_data->reserve < len);
7245 dio_data->reserve -= len;7261 dio_data->reserve -= len;
7246 dio_data->unsubmitted_oe_range_end = start + len;
7247 current->journal_info = dio_data;
7248out:7262out:
7249 return ret;7263 return ret;
7250}7264}
7251 7265
7252static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,7266static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7253 struct buffer_head *bh_result, int create)7267 loff_t length, unsigned flags, struct iomap *iomap,
7268 struct iomap *srcmap)
7254{7269{
7255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);7270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7256 struct extent_map *em;7271 struct extent_map *em;
7257 struct extent_state *cached_state = NULL;7272 struct extent_state *cached_state = NULL;
7258 struct btrfs_dio_data *dio_data = NULL;7273 struct btrfs_dio_data *dio_data = NULL;
7259 u64 start = iblock << inode->i_blkbits;
7260 u64 lockstart, lockend;7274 u64 lockstart, lockend;
7261 u64 len = bh_result->b_size;7275 const bool write = !!(flags & IOMAP_WRITE);
7262 int ret = 0;7276 int ret = 0;
7277 u64 len = length;
7278 bool unlock_extents = false;
7263 7279
7264 if (!create)7280 if (!write)
7265 len = min_t(u64, len, fs_info->sectorsize);7281 len = min_t(u64, len, fs_info->sectorsize);
7266 7282
7267 lockstart = start;7283 lockstart = start;
7268 lockend = start + len - 1;7284 lockend = start + len - 1;
7269 7285
7270 if (current->journal_info) {7286 /*
7271 /*7287 * The generic stuff only does filemap_write_and_wait_range, which
7272 * Need to pull our outstanding extents and set journal_info to NULL so7288 * isn't enough if we've written compressed pages to this area, so we
7273 * that anything that needs to check if there's a transaction doesn't get7289 * need to flush the dirty pages again to make absolutely sure that any
7274 * confused.7290 * outstanding dirty pages are on disk.
7275 */7291 */
7276 dio_data = current->journal_info;7292 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7277 current->journal_info = NULL;7293 &BTRFS_I(inode)->runtime_flags))
7294 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7295 start + length - 1);
7296
7297 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7298 if (!dio_data)
7299 return -ENOMEM;
7300
7301 dio_data->length = length;
7302 if (write) {
7303 dio_data->reserve = round_up(length, fs_info->sectorsize);
7304 ret = btrfs_delalloc_reserve_space(inode,
7305 &dio_data->data_reserved,
7306 start, dio_data->reserve);
7307 if (ret) {
7308 extent_changeset_free(dio_data->data_reserved);
7309 kfree(dio_data);
7310 return ret;
7311 }
7278 }7312 }
7313 iomap->private = dio_data;
7314
7279 7315
7280 /*7316 /*
7281 * If this errors out it's because we couldn't invalidate pagecache for7317 * If this errors out it's because we couldn't invalidate pagecache for
7282 * this range and we need to fallback to buffered.7318 * this range and we need to fallback to buffered.
7283 */7319 */
7284 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,7320 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
7285 create)) {
7286 ret = -ENOTBLK;7321 ret = -ENOTBLK;
7287 goto err;7322 goto err;
7288 }7323 }
@@ -7314,36 +7349,48 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, · 25 unmodified lines
7314 goto unlock_err;7349 goto unlock_err;
7315 }7350 }
7316 7351
7317 if (create) {7352 len = min(len, em->len - (start - em->start));
7318 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,7353 if (write) {
7319 dio_data, start, len);7354 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7355 start, len);
7320 if (ret < 0)7356 if (ret < 0)
7321 goto unlock_err;7357 goto unlock_err;
7322 7358 unlock_extents = true;
7323 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,7359 /* Recalc len in case the new em is smaller than requested */
7324 lockend, &cached_state);7360 len = min(len, em->len - (start - em->start));
7325 } else {7361 } else {
7326 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7327 start, len);
7328 /* Can be negative only if we read from a hole */
7329 if (ret < 0) {
7330 ret = 0;
7331 free_extent_map(em);
7332 goto unlock_err;
7333 }
7334 /*7362 /*
7335 * We need to unlock only the end area that we aren't using.7363 * We need to unlock only the end area that we aren't using.
7336 * The rest is going to be unlocked by the endio routine.7364 * The rest is going to be unlocked by the endio routine.
7337 */7365 */
7338 lockstart = start + bh_result->b_size;7366 lockstart = start + len;
7339 if (lockstart < lockend) {7367 if (lockstart < lockend)
7340 unlock_extent_cached(&BTRFS_I(inode)->io_tree,7368 unlock_extents = true;
7341 lockstart, lockend, &cached_state);
7342 } else {
7343 free_extent_state(cached_state);
7344 }
7345 }7369 }
7346 7370
7371 if (unlock_extents)
7372 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7373 lockstart, lockend, &cached_state);
7374 else
7375 free_extent_state(cached_state);
7376
7377 /*
7378 * Translate extent map information to iomap.
7379 * We trim the extents (and move the addr) even though iomap code does
7380 * that, since we have locked only the parts we are performing I/O in.
7381 */
7382 if ((em->block_start == EXTENT_MAP_HOLE) ||
7383 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7384 iomap->addr = IOMAP_NULL_ADDR;
7385 iomap->type = IOMAP_HOLE;
7386 } else {
7387 iomap->addr = em->block_start + (start - em->start);
7388 iomap->type = IOMAP_MAPPED;
7389 }
7390 iomap->offset = start;
7391 iomap->bdev = fs_info->fs_devices->latest_bdev;
7392 iomap->length = len;
7393
7347 free_extent_map(em);7394 free_extent_map(em);
7348 7395
7349 return 0;7396 return 0;
@@ -7352,370 +7399,152 @@ unlock_err: · 2 unmodified lines
7352 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,7399 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7353 &cached_state);7400 &cached_state);
7354err:7401err:
7355 if (dio_data)7402 if (dio_data) {
7356 current->journal_info = dio_data;7403 btrfs_delalloc_release_space(inode, dio_data->data_reserved,
7404 start, dio_data->reserve, true);
7405 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7406 extent_changeset_free(dio_data->data_reserved);
7407 kfree(dio_data);
7408 }
7357 return ret;7409 return ret;
7358}7410}
7359 7411
7360static inline blk_status_t submit_dio_repair_bio(struct inode *inode,7412static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7361 struct bio *bio,7413 ssize_t written, unsigned flags, struct iomap *iomap)
7362 int mirror_num)
7363{7414{
7364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);7415 int ret = 0;
7365 blk_status_t ret;7416 struct btrfs_dio_data *dio_data = iomap->private;
7417 size_t submitted = dio_data->submitted;
7418 const bool write = !!(flags & IOMAP_WRITE);
7366 7419
7367 BUG_ON(bio_op(bio) == REQ_OP_WRITE);7420 if (!write && (iomap->type == IOMAP_HOLE)) {
7421 /* If reading from a hole, unlock and return */
7422 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7423 goto out;
7424 }
7368 7425
7369 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);7426 if (submitted < length) {
7370 if (ret)7427 pos += submitted;
7371 return ret;7428 length -= submitted;
7429 if (write)
7430 __endio_write_update_ordered(inode, pos, length, false);
7431 else
7432 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7433 pos + length - 1);
7434 ret = -ENOTBLK;
7435 }
7372 7436
7373 ret = btrfs_map_bio(fs_info, bio, mirror_num);7437 if (write) {
7438 if (dio_data->reserve)
7439 btrfs_delalloc_release_space(inode,
7440 dio_data->data_reserved, pos,
7441 dio_data->reserve, true);
7442 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7443 extent_changeset_free(dio_data->data_reserved);
7444 }
7445out:
7446 kfree(dio_data);
7447 iomap->private = NULL;
7374 7448
7375 return ret;7449 return ret;
7376}7450}
7377 7451
7378static int btrfs_check_dio_repairable(struct inode *inode,7452static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7379 struct bio *failed_bio,
7380 struct io_failure_record *failrec,
7381 int failed_mirror)
7382{7453{
7383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);7454 /*
7384 int num_copies;7455 * This implies a barrier so that stores to dio_bio->bi_status before
7385 7456 * this and loads of dio_bio->bi_status after this are fully ordered.
7386 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);7457 */
7387 if (num_copies == 1) {7458 if (!refcount_dec_and_test(&dip->refs))
7388 /*7459 return;
7389 * we only have a single copy of the data, so don't bother with
7390 * all the retry and error correction code that follows. no
7391 * matter what the error is, it is very likely to persist.
7392 */
7393 btrfs_debug(fs_info,
7394 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7395 num_copies, failrec->this_mirror, failed_mirror);
7396 return 0;
7397 }
7398
7399 failrec->failed_mirror = failed_mirror;
7400 failrec->this_mirror++;
7401 if (failrec->this_mirror == failed_mirror)
7402 failrec->this_mirror++;
7403 7460
7404 if (failrec->this_mirror > num_copies) {7461 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7405 btrfs_debug(fs_info,7462 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7406 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",7463 dip->bytes,
7407 num_copies, failrec->this_mirror, failed_mirror);7464 !dip->dio_bio->bi_status);
7408 return 0;7465 } else {
7466 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7467 dip->logical_offset,
7468 dip->logical_offset + dip->bytes - 1);
7409 }7469 }
7410 7470
7411 return 1;7471 bio_endio(dip->dio_bio);
7472 kfree(dip);
7412}7473}
7413 7474
7414static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,7475static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7415 struct page *page, unsigned int pgoff,7476 int mirror_num,
7416 u64 start, u64 end, int failed_mirror,7477 unsigned long bio_flags)
7417 bio_end_io_t *repair_endio, void *repair_arg)
7418{7478{
7419 struct io_failure_record *failrec;7479 struct btrfs_dio_private *dip = bio->bi_private;
7420 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;7480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7421 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;7481 blk_status_t ret;
7422 struct bio *bio;
7423 int isector;
7424 unsigned int read_mode = 0;
7425 int segs;
7426 int ret;
7427 blk_status_t status;
7428 struct bio_vec bvec;
7429 7482
7430 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);7483 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7431 7484
7432 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);7485 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
7433 if (ret)7486 if (ret)
7434 return errno_to_blk_status(ret);7487 return ret;
7435
7436 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7437 failed_mirror);
7438 if (!ret) {
7439 free_io_failure(failure_tree, io_tree, failrec);
7440 return BLK_STS_IOERR;
7441 }
7442
7443 segs = bio_segments(failed_bio);
7444 bio_get_first_bvec(failed_bio, &bvec);
7445 if (segs > 1 ||
7446 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
7447 read_mode |= REQ_FAILFAST_DEV;
7448
7449 isector = start - btrfs_io_bio(failed_bio)->logical;
7450 isector >>= inode->i_sb->s_blocksize_bits;
7451 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7452 pgoff, isector, repair_endio, repair_arg);
7453 bio->bi_opf = REQ_OP_READ | read_mode;
7454
7455 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7456 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
7457 read_mode, failrec->this_mirror, failrec->in_validation);
7458
7459 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7460 if (status) {
7461 free_io_failure(failure_tree, io_tree, failrec);
7462 bio_put(bio);
7463 }
7464
7465 return status;
7466}
7467
7468struct btrfs_retry_complete {
7469 struct completion done;
7470 struct inode *inode;
7471 u64 start;
7472 int uptodate;
7473};
7474 7488
7475static void btrfs_retry_endio_nocsum(struct bio *bio)7489 refcount_inc(&dip->refs);
7476{7490 ret = btrfs_map_bio(fs_info, bio, mirror_num);
7477 struct btrfs_retry_complete *done = bio->bi_private;7491 if (ret)
7478 struct inode *inode = done->inode;7492 refcount_dec(&dip->refs);
7479 struct bio_vec *bvec;7493 return ret;
7480 struct extent_io_tree *io_tree, *failure_tree;
7481 struct bvec_iter_all iter_all;
7482
7483 if (bio->bi_status)
7484 goto end;
7485
7486 ASSERT(bio->bi_vcnt == 1);
7487 io_tree = &BTRFS_I(inode)->io_tree;
7488 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7489 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
7490
7491 done->uptodate = 1;
7492 ASSERT(!bio_flagged(bio, BIO_CLONED));
7493 bio_for_each_segment_all(bvec, bio, iter_all)
7494 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7495 io_tree, done->start, bvec->bv_page,
7496 btrfs_ino(BTRFS_I(inode)), 0);
7497end:
7498 complete(&done->done);
7499 bio_put(bio);
7500}7494}
7501 7495
7502static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,7496static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7503 struct btrfs_io_bio *io_bio)7497 struct btrfs_io_bio *io_bio,
7498 const bool uptodate)
7504{7499{
7505 struct btrfs_fs_info *fs_info;7500 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7501 const u32 sectorsize = fs_info->sectorsize;
7502 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7503 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7504 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7506 struct bio_vec bvec;7505 struct bio_vec bvec;
7507 struct bvec_iter iter;7506 struct bvec_iter iter;
7508 struct btrfs_retry_complete done;7507 u64 start = io_bio->logical;
7509 u64 start;7508 int icsum = 0;
7510 unsigned int pgoff;
7511 u32 sectorsize;
7512 int nr_sectors;
7513 blk_status_t ret;
7514 blk_status_t err = BLK_STS_OK;7509 blk_status_t err = BLK_STS_OK;
7515 7510
7516 fs_info = BTRFS_I(inode)->root->fs_info;7511 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7517 sectorsize = fs_info->sectorsize;7512 unsigned int i, nr_sectors, pgoff;
7518
7519 start = io_bio->logical;
7520 done.inode = inode;
7521 io_bio->bio.bi_iter = io_bio->iter;
7522 7513
7523 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7524 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);7514 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7525 pgoff = bvec.bv_offset;7515 pgoff = bvec.bv_offset;
7526 7516 for (i = 0; i < nr_sectors; i++) {
7527next_block_or_try_again:
7528 done.uptodate = 0;
7529 done.start = start;
7530 init_completion(&done.done);
7531
7532 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7533 pgoff, start, start + sectorsize - 1,
7534 io_bio->mirror_num,
7535 btrfs_retry_endio_nocsum, &done);
7536 if (ret) {
7537 err = ret;
7538 goto next;
7539 }
7540
7541 wait_for_completion_io(&done.done);
7542
7543 if (!done.uptodate) {
7544 /* We might have another mirror, so try again */
7545 goto next_block_or_try_again;
7546 }
7547
7548next:
7549 start += sectorsize;
7550
7551 nr_sectors--;
7552 if (nr_sectors) {
7553 pgoff += sectorsize;
7554 ASSERT(pgoff < PAGE_SIZE);7517 ASSERT(pgoff < PAGE_SIZE);
7555 goto next_block_or_try_again;7518 if (uptodate &&
7556 }7519 (!csum || !check_data_csum(inode, io_bio, icsum,
7557 }7520 bvec.bv_page, pgoff,
7558 7521 start, sectorsize))) {
7559 return err;7522 clean_io_failure(fs_info, failure_tree, io_tree,
7560}7523 start, bvec.bv_page,
7561 7524 btrfs_ino(BTRFS_I(inode)),
7562static void btrfs_retry_endio(struct bio *bio)7525 pgoff);
7563{7526 } else {
7564 struct btrfs_retry_complete *done = bio->bi_private;7527 blk_status_t status;
7565 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);7528
7566 struct extent_io_tree *io_tree, *failure_tree;7529 status = btrfs_submit_read_repair(inode,
7567 struct inode *inode = done->inode;7530 &io_bio->bio,
7568 struct bio_vec *bvec;7531 start - io_bio->logical,
7569 int uptodate;7532 bvec.bv_page, pgoff,
7570 int ret;7533 start,
7571 int i = 0;7534 start + sectorsize - 1,
7572 struct bvec_iter_all iter_all;7535 io_bio->mirror_num,
7573 7536 submit_dio_repair_bio);
7574 if (bio->bi_status)7537 if (status)
7575 goto end;7538 err = status;
7576 7539 }
7577 uptodate = 1;7540 start += sectorsize;
7578 7541 icsum++;
7579 ASSERT(bio->bi_vcnt == 1);
7580 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
7581
7582 io_tree = &BTRFS_I(inode)->io_tree;
7583 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7584
7585 ASSERT(!bio_flagged(bio, BIO_CLONED));
7586 bio_for_each_segment_all(bvec, bio, iter_all) {
7587 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7588 bvec->bv_offset, done->start,
7589 bvec->bv_len);
7590 if (!ret)
7591 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7592 failure_tree, io_tree, done->start,
7593 bvec->bv_page,
7594 btrfs_ino(BTRFS_I(inode)),
7595 bvec->bv_offset);
7596 else
7597 uptodate = 0;
7598 i++;
7599 }
7600
7601 done->uptodate = uptodate;
7602end:
7603 complete(&done->done);
7604 bio_put(bio);
7605}
7606
7607static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7608 struct btrfs_io_bio *io_bio, blk_status_t err)
7609{
7610 struct btrfs_fs_info *fs_info;
7611 struct bio_vec bvec;
7612 struct bvec_iter iter;
7613 struct btrfs_retry_complete done;
7614 u64 start;
7615 u64 offset = 0;
7616 u32 sectorsize;
7617 int nr_sectors;
7618 unsigned int pgoff;
7619 int csum_pos;
7620 bool uptodate = (err == 0);
7621 int ret;
7622 blk_status_t status;
7623
7624 fs_info = BTRFS_I(inode)->root->fs_info;
7625 sectorsize = fs_info->sectorsize;
7626
7627 err = BLK_STS_OK;
7628 start = io_bio->logical;
7629 done.inode = inode;
7630 io_bio->bio.bi_iter = io_bio->iter;
7631
7632 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7633 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7634
7635 pgoff = bvec.bv_offset;
7636next_block:
7637 if (uptodate) {
7638 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7639 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7640 bvec.bv_page, pgoff, start, sectorsize);
7641 if (likely(!ret))
7642 goto next;
7643 }
7644try_again:
7645 done.uptodate = 0;
7646 done.start = start;
7647 init_completion(&done.done);
7648
7649 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7650 pgoff, start, start + sectorsize - 1,
7651 io_bio->mirror_num, btrfs_retry_endio,
7652 &done);
7653 if (status) {
7654 err = status;
7655 goto next;
7656 }
7657
7658 wait_for_completion_io(&done.done);
7659
7660 if (!done.uptodate) {
7661 /* We might have another mirror, so try again */
7662 goto try_again;
7663 }
7664next:
7665 offset += sectorsize;
7666 start += sectorsize;
7667
7668 ASSERT(nr_sectors);
7669
7670 nr_sectors--;
7671 if (nr_sectors) {
7672 pgoff += sectorsize;7542 pgoff += sectorsize;
7673 ASSERT(pgoff < PAGE_SIZE);
7674 goto next_block;
7675 }7543 }
7676 }7544 }
7677
7678 return err;7545 return err;
7679}7546}
7680 7547
7681static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7682 struct btrfs_io_bio *io_bio, blk_status_t err)
7683{
7684 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7685
7686 if (skip_csum) {
7687 if (unlikely(err))
7688 return __btrfs_correct_data_nocsum(inode, io_bio);
7689 else
7690 return BLK_STS_OK;
7691 } else {
7692 return __btrfs_subio_endio_read(inode, io_bio, err);
7693 }
7694}
7695
7696static void btrfs_endio_direct_read(struct bio *bio)
7697{
7698 struct btrfs_dio_private *dip = bio->bi_private;
7699 struct inode *inode = dip->inode;
7700 struct bio *dio_bio;
7701 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7702 blk_status_t err = bio->bi_status;
7703
7704 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7705 err = btrfs_subio_endio_read(inode, io_bio, err);
7706
7707 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
7708 dip->logical_offset + dip->bytes - 1);
7709 dio_bio = dip->dio_bio;
7710
7711 kfree(dip);
7712
7713 dio_bio->bi_status = err;
7714 dio_end_io(dio_bio);
7715 btrfs_io_bio_free_csum(io_bio);
7716 bio_put(bio);
7717}
7718
7719static void __endio_write_update_ordered(struct inode *inode,7548static void __endio_write_update_ordered(struct inode *inode,
7720 const u64 offset, const u64 bytes,7549 const u64 offset, const u64 bytes,
7721 const bool uptodate)7550 const bool uptodate)
@@ -7759,21 +7588,6 @@ static void __endio_write_update_ordered(struct inode *inode, · 37 unmodified lines
7759 }7588 }
7760}7589}
7761 7590
7762static void btrfs_endio_direct_write(struct bio *bio)
7763{
7764 struct btrfs_dio_private *dip = bio->bi_private;
7765 struct bio *dio_bio = dip->dio_bio;
7766
7767 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7768 dip->bytes, !bio->bi_status);
7769
7770 kfree(dip);
7771
7772 dio_bio->bi_status = bio->bi_status;
7773 dio_end_io(dio_bio);
7774 bio_put(bio);
7775}
7776
7777static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,7591static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
7778 struct bio *bio, u64 offset)7592 struct bio *bio, u64 offset)
7779{7593{
@@ -7797,64 +7611,16 @@ static void btrfs_end_dio_bio(struct bio *bio) · 17 unmodified lines
7797 (unsigned long long)bio->bi_iter.bi_sector,7611 (unsigned long long)bio->bi_iter.bi_sector,
7798 bio->bi_iter.bi_size, err);7612 bio->bi_iter.bi_size, err);
7799 7613
7800 if (dip->subio_endio)7614 if (bio_op(bio) == REQ_OP_READ) {
7801 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);7615 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
7802 7616 !err);
7803 if (err) {
7804 /*
7805 * We want to perceive the errors flag being set before
7806 * decrementing the reference count. We don't need a barrier
7807 * since atomic operations with a return value are fully
7808 * ordered as per atomic_t.txt
7809 */
7810 dip->errors = 1;
7811 }7617 }
7812 7618
7813 /* if there are more bios still pending for this dio, just exit */7619 if (err)
7814 if (!atomic_dec_and_test(&dip->pending_bios))7620 dip->dio_bio->bi_status = err;
7815 goto out;
7816 7621
7817 if (dip->errors) {
7818 bio_io_error(dip->orig_bio);
7819 } else {
7820 dip->dio_bio->bi_status = BLK_STS_OK;
7821 bio_endio(dip->orig_bio);
7822 }
7823out:
7824 bio_put(bio);7622 bio_put(bio);
7825}7623 btrfs_dio_private_put(dip);
7826
7827static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
7828 struct btrfs_dio_private *dip,
7829 struct bio *bio,
7830 u64 file_offset)
7831{
7832 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7833 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7834 u16 csum_size;
7835 blk_status_t ret;
7836
7837 /*
7838 * We load all the csum data we need when we submit
7839 * the first bio to reduce the csum tree search and
7840 * contention.
7841 */
7842 if (dip->logical_offset == file_offset) {
7843 ret = btrfs_lookup_bio_sums(inode, dip->orig_bio, file_offset,
7844 NULL);
7845 if (ret)
7846 return ret;
7847 }
7848
7849 if (bio == dip->orig_bio)
7850 return 0;
7851
7852 file_offset -= dip->logical_offset;
7853 file_offset >>= inode->i_sb->s_blocksize_bits;
7854 csum_size = btrfs_super_csum_size(btrfs_sb(inode->i_sb)->super_copy);
7855 io_bio->csum = orig_io_bio->csum + csum_size * file_offset;
7856
7857 return 0;
7858}7624}
7859 7625
7860static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,7626static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
@@ -7892,10 +7658,12 @@ static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, · 31 unmodified lines
7892 if (ret)7658 if (ret)
7893 goto err;7659 goto err;
7894 } else {7660 } else {
7895 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,7661 u64 csum_offset;
7896 file_offset);7662
7897 if (ret)7663 csum_offset = file_offset - dip->logical_offset;
7898 goto err;7664 csum_offset >>= inode->i_sb->s_blocksize_bits;
7665 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7666 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
7899 }7667 }
7900map:7668map:
7901 ret = btrfs_map_bio(fs_info, bio, 0);7669 ret = btrfs_map_bio(fs_info, bio, 0);
@@ -7903,14 +7671,53 @@ err: · 1 unmodified line
7903 return ret;7671 return ret;
7904}7672}
7905 7673
7906static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)7674/*
7675 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7676 * or ordered extents whether or not we submit any bios.
7677 */
7678static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7679 struct inode *inode,
7680 loff_t file_offset)
7907{7681{
7908 struct inode *inode = dip->inode;7682 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
7683 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7684 size_t dip_size;
7685 struct btrfs_dio_private *dip;
7686
7687 dip_size = sizeof(*dip);
7688 if (!write && csum) {
7689 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7690 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7691 size_t nblocks;
7692
7693 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7694 dip_size += csum_size * nblocks;
7695 }
7696
7697 dip = kzalloc(dip_size, GFP_NOFS);
7698 if (!dip)
7699 return NULL;
7700
7701 dip->inode = inode;
7702 dip->logical_offset = file_offset;
7703 dip->bytes = dio_bio->bi_iter.bi_size;
7704 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
7705 dip->dio_bio = dio_bio;
7706 refcount_set(&dip->refs, 1);
7707 return dip;
7708}
7709
7710static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7711 struct bio *dio_bio, loff_t file_offset)
7712{
7713 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
7714 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);7715 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7716 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7717 BTRFS_BLOCK_GROUP_RAID56_MASK);
7718 struct btrfs_dio_private *dip;
7910 struct bio *bio;7719 struct bio *bio;
7911 struct bio *orig_bio = dip->orig_bio;7720 u64 start_sector;
7912 u64 start_sector = orig_bio->bi_iter.bi_sector;
7913 u64 file_offset = dip->logical_offset;
7914 int async_submit = 0;7721 int async_submit = 0;
7915 u64 submit_len;7722 u64 submit_len;
7916 int clone_offset = 0;7723 int clone_offset = 0;
@@ -7918,330 +7725,108 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) · 1 unmodified line
7918 int ret;7725 int ret;
7919 blk_status_t status;7726 blk_status_t status;
7920 struct btrfs_io_geometry geom;7727 struct btrfs_io_geometry geom;
7728 struct btrfs_dio_data *dio_data = iomap->private;
7921 7729
7922 submit_len = orig_bio->bi_iter.bi_size;7730 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7923 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),7731 if (!dip) {
7924 start_sector << 9, submit_len, &geom);7732 if (!write) {
7925 if (ret)7733 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7926 return -EIO;7734 file_offset + dio_bio->bi_iter.bi_size - 1);
7735 }
7736 dio_bio->bi_status = BLK_STS_RESOURCE;
7737 bio_endio(dio_bio);
7738 return BLK_QC_T_NONE;
7739 }
7927 7740
7928 if (geom.len >= submit_len) {7741 if (!write && csum) {
7929 bio = orig_bio;7742 /*
7930 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;7743 * Load the csums up front to reduce csum tree searches and
7931 goto submit;7744 * contention when submitting bios.
7745 */
7746 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7747 dip->csums);
7748 if (status != BLK_STS_OK)
7749 goto out_err;
7932 }7750 }
7933 7751
7934 /* async crcs make it difficult to collect full stripe writes. */7752 start_sector = dio_bio->bi_iter.bi_sector;
7935 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)7753 submit_len = dio_bio->bi_iter.bi_size;
7936 async_submit = 0;
7937 else
7938 async_submit = 1;
7939 7754
7940 /* bio split */
7941 ASSERT(geom.len <= INT_MAX);
7942 atomic_inc(&dip->pending_bios);
7943 do {7755 do {
7756 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7757 start_sector << 9, submit_len,
7758 &geom);
7759 if (ret) {
7760 status = errno_to_blk_status(ret);
7761 goto out_err;
7762 }
7763 ASSERT(geom.len <= INT_MAX);
7764
7944 clone_len = min_t(int, submit_len, geom.len);7765 clone_len = min_t(int, submit_len, geom.len);
7945 7766
7946 /*7767 /*
7947 * This will never fail as it's passing GPF_NOFS and7768 * This will never fail as it's passing GPF_NOFS and
7948 * the allocation is backed by btrfs_bioset.7769 * the allocation is backed by btrfs_bioset.
7949 */7770 */
7950 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,7771 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
7951 clone_len);
7952 bio->bi_private = dip;7772 bio->bi_private = dip;
7953 bio->bi_end_io = btrfs_end_dio_bio;7773 bio->bi_end_io = btrfs_end_dio_bio;
7954 btrfs_io_bio(bio)->logical = file_offset;7774 btrfs_io_bio(bio)->logical = file_offset;
7955 7775
7956 ASSERT(submit_len >= clone_len);7776 ASSERT(submit_len >= clone_len);
7957 submit_len -= clone_len;7777 submit_len -= clone_len;
7958 if (submit_len == 0)
7959 break;
7960 7778
7961 /*7779 /*
7962 * Increase the count before we submit the bio so we know7780 * Increase the count before we submit the bio so we know
7963 * the end IO handler won't happen before we increase the7781 * the end IO handler won't happen before we increase the
7964 * count. Otherwise, the dip might get freed before we're7782 * count. Otherwise, the dip might get freed before we're
7965 * done setting it up.7783 * done setting it up.
7784 *
7785 * We transfer the initial reference to the last bio, so we
7786 * don't need to increment the reference count for the last one.
7966 */7787 */
7967 atomic_inc(&dip->pending_bios);7788 if (submit_len > 0) {
7789 refcount_inc(&dip->refs);
7790 /*
7791 * If we are submitting more than one bio, submit them
7792 * all asynchronously. The exception is RAID 5 or 6, as
7793 * asynchronous checksums make it difficult to collect
7794 * full stripe writes.
7795 */
7796 if (!raid56)
7797 async_submit = 1;
7798 }
7968 7799
7969 status = btrfs_submit_dio_bio(bio, inode, file_offset,7800 status = btrfs_submit_dio_bio(bio, inode, file_offset,
7970 async_submit);7801 async_submit);
7971 if (status) {7802 if (status) {
7972 bio_put(bio);7803 bio_put(bio);
7973 atomic_dec(&dip->pending_bios);7804 if (submit_len > 0)
7805 refcount_dec(&dip->refs);
7974 goto out_err;7806 goto out_err;
7975 }7807 }
7976 7808
7809 dio_data->submitted += clone_len;
7977 clone_offset += clone_len;7810 clone_offset += clone_len;
7978 start_sector += clone_len >> 9;7811 start_sector += clone_len >> 9;
7979 file_offset += clone_len;7812 file_offset += clone_len;
7980
7981 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7982 start_sector << 9, submit_len, &geom);
7983 if (ret)
7984 goto out_err;
7985 } while (submit_len > 0);7813 } while (submit_len > 0);
7814 return BLK_QC_T_NONE;
7986 7815
7987submit:
7988 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
7989 if (!status)
7990 return 0;
7991
7992 bio_put(bio);
7993out_err:7816out_err:
7994 dip->errors = 1;7817 dip->dio_bio->bi_status = status;
7995 /*7818 btrfs_dio_private_put(dip);
7996 * Before atomic variable goto zero, we must make sure dip->errors is7819 return BLK_QC_T_NONE;
7997 * perceived to be set. This ordering is ensured by the fact that an
7998 * atomic operations with a return value are fully ordered as per
7999 * atomic_t.txt
8000 */
8001 if (atomic_dec_and_test(&dip->pending_bios))
8002 bio_io_error(dip->orig_bio);
8003
8004 /* bio_end_io() will handle error, so we needn't return it */
8005 return 0;
8006}
8007
8008static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8009 loff_t file_offset)
8010{
8011 struct btrfs_dio_private *dip = NULL;
8012 struct bio *bio = NULL;
8013 struct btrfs_io_bio *io_bio;
8014 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8015 int ret = 0;
8016
8017 bio = btrfs_bio_clone(dio_bio);
8018
8019 dip = kzalloc(sizeof(*dip), GFP_NOFS);
8020 if (!dip) {
8021 ret = -ENOMEM;
8022 goto free_ordered;
8023 }
8024
8025 dip->private = dio_bio->bi_private;
8026 dip->inode = inode;
8027 dip->logical_offset = file_offset;
8028 dip->bytes = dio_bio->bi_iter.bi_size;
8029 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8030 bio->bi_private = dip;
8031 dip->orig_bio = bio;
8032 dip->dio_bio = dio_bio;
8033 atomic_set(&dip->pending_bios, 0);
8034 io_bio = btrfs_io_bio(bio);
8035 io_bio->logical = file_offset;
8036
8037 if (write) {
8038 bio->bi_end_io = btrfs_endio_direct_write;
8039 } else {
8040 bio->bi_end_io = btrfs_endio_direct_read;
8041 dip->subio_endio = btrfs_subio_endio_read;
8042 }
8043
8044 /*
8045 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8046 * even if we fail to submit a bio, because in such case we do the
8047 * corresponding error handling below and it must not be done a second
8048 * time by btrfs_direct_IO().
8049 */
8050 if (write) {
8051 struct btrfs_dio_data *dio_data = current->journal_info;
8052
8053 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8054 dip->bytes;
8055 dio_data->unsubmitted_oe_range_start =
8056 dio_data->unsubmitted_oe_range_end;
8057 }
8058
8059 ret = btrfs_submit_direct_hook(dip);
8060 if (!ret)
8061 return;
8062
8063 btrfs_io_bio_free_csum(io_bio);
8064
8065free_ordered:
8066 /*
8067 * If we arrived here it means either we failed to submit the dip
8068 * or we either failed to clone the dio_bio or failed to allocate the
8069 * dip. If we cloned the dio_bio and allocated the dip, we can just
8070 * call bio_endio against our io_bio so that we get proper resource
8071 * cleanup if we fail to submit the dip, otherwise, we must do the
8072 * same as btrfs_endio_direct_[write|read] because we can't call these
8073 * callbacks - they require an allocated dip and a clone of dio_bio.
8074 */
8075 if (bio && dip) {
8076 bio_io_error(bio);
8077 /*
8078 * The end io callbacks free our dip, do the final put on bio
8079 * and all the cleanup and final put for dio_bio (through
8080 * dio_end_io()).
8081 */
8082 dip = NULL;
8083 bio = NULL;
8084 } else {
8085 if (write)
8086 __endio_write_update_ordered(inode,
8087 file_offset,
8088 dio_bio->bi_iter.bi_size,
8089 false);
8090 else
8091 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8092 file_offset + dio_bio->bi_iter.bi_size - 1);
8093
8094 dio_bio->bi_status = BLK_STS_IOERR;
8095 /*
8096 * Releases and cleans up our dio_bio, no need to bio_put()
8097 * nor bio_endio()/bio_io_error() against dio_bio.
8098 */
8099 dio_end_io(dio_bio);
8100 }
8101 if (bio)
8102 bio_put(bio);
8103 kfree(dip);
8104}7820}
8105 7821
8106static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,7822const struct iomap_ops btrfs_dio_iomap_ops = {
8107 const struct iov_iter *iter, loff_t offset)7823 .iomap_begin = btrfs_dio_iomap_begin,
8108{7824 .iomap_end = btrfs_dio_iomap_end,
8109 int seg;7825};
8110 int i;
8111 unsigned int blocksize_mask = fs_info->sectorsize - 1;
8112 ssize_t retval = -EINVAL;
8113
8114 if (offset & blocksize_mask)
8115 goto out;
8116
8117 if (iov_iter_alignment(iter) & blocksize_mask)
8118 goto out;
8119
8120 /* If this is a write we don't need to check anymore */
8121 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
8122 return 0;
8123 /*
8124 * Check to make sure we don't have duplicate iov_base's in this
8125 * iovec, if so return EINVAL, otherwise we'll get csum errors
8126 * when reading back.
8127 */
8128 for (seg = 0; seg < iter->nr_segs; seg++) {
8129 for (i = seg + 1; i < iter->nr_segs; i++) {
8130 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8131 goto out;
8132 }
8133 }
8134 retval = 0;
8135out:
8136 return retval;
8137}
8138
8139static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8140{
8141 struct file *file = iocb->ki_filp;
8142 struct inode *inode = file->f_mapping->host;
8143 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8144 struct btrfs_dio_data dio_data = { 0 };
8145 struct extent_changeset *data_reserved = NULL;
8146 loff_t offset = iocb->ki_pos;
8147 size_t count = 0;
8148 int flags = 0;
8149 bool wakeup = true;
8150 bool relock = false;
8151 ssize_t ret;
8152
8153 if (check_direct_IO(fs_info, iter, offset))
8154 return 0;
8155
8156 inode_dio_begin(inode);
8157
8158 /*
8159 * The generic stuff only does filemap_write_and_wait_range, which
8160 * isn't enough if we've written compressed pages to this area, so
8161 * we need to flush the dirty pages again to make absolutely sure
8162 * that any outstanding dirty pages are on disk.
8163 */
8164 count = iov_iter_count(iter);
8165 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8166 &BTRFS_I(inode)->runtime_flags))
8167 filemap_fdatawrite_range(inode->i_mapping, offset,
8168 offset + count - 1);
8169
8170 if (iov_iter_rw(iter) == WRITE) {
8171 /*
8172 * If the write DIO is beyond the EOF, we need update
8173 * the isize, but it is protected by i_mutex. So we can
8174 * not unlock the i_mutex at this case.
8175 */
8176 if (offset + count <= inode->i_size) {
8177 dio_data.overwrite = 1;
8178 inode_unlock(inode);
8179 relock = true;
8180 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8181 ret = -EAGAIN;
8182 goto out;
8183 }
8184 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8185 offset, count);
8186 if (ret)
8187 goto out;
8188
8189 /*
8190 * We need to know how many extents we reserved so that we can
8191 * do the accounting properly if we go over the number we
8192 * originally calculated. Abuse current->journal_info for this.
8193 */
8194 dio_data.reserve = round_up(count,
8195 fs_info->sectorsize);
8196 dio_data.unsubmitted_oe_range_start = (u64)offset;
8197 dio_data.unsubmitted_oe_range_end = (u64)offset;
8198 current->journal_info = &dio_data;
8199 down_read(&BTRFS_I(inode)->dio_sem);
8200 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8201 &BTRFS_I(inode)->runtime_flags)) {
8202 inode_dio_end(inode);
8203 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8204 wakeup = false;
8205 }
8206
8207 ret = __blockdev_direct_IO(iocb, inode,
8208 fs_info->fs_devices->latest_bdev,
8209 iter, btrfs_get_blocks_direct, NULL,
8210 btrfs_submit_direct, flags);
8211 if (iov_iter_rw(iter) == WRITE) {
8212 up_read(&BTRFS_I(inode)->dio_sem);
8213 current->journal_info = NULL;
8214 if (ret < 0 && ret != -EIOCBQUEUED) {
8215 if (dio_data.reserve)
8216 btrfs_delalloc_release_space(inode, data_reserved,
8217 offset, dio_data.reserve, true);
8218 /*
8219 * On error we might have left some ordered extents
8220 * without submitting corresponding bios for them, so
8221 * cleanup them up to avoid other tasks getting them
8222 * and waiting for them to complete forever.
8223 */
8224 if (dio_data.unsubmitted_oe_range_start <
8225 dio_data.unsubmitted_oe_range_end)
8226 __endio_write_update_ordered(inode,
8227 dio_data.unsubmitted_oe_range_start,
8228 dio_data.unsubmitted_oe_range_end -
8229 dio_data.unsubmitted_oe_range_start,
8230 false);
8231 } else if (ret >= 0 && (size_t)ret < count)
8232 btrfs_delalloc_release_space(inode, data_reserved,
8233 offset, count - (size_t)ret, true);
8234 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
8235 }
8236out:
8237 if (wakeup)
8238 inode_dio_end(inode);
8239 if (relock)
8240 inode_lock(inode);
8241 7826
8242 extent_changeset_free(data_reserved);7827const struct iomap_dio_ops btrfs_dops = {
8243 return ret;7828 .submit_io = btrfs_submit_direct,
8244}7829};
8245 7830
8246#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)7831#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8247 7832
@@ -10539,7 +10124,7 @@ static const struct address_space_operations btrfs_aops = { · 2291 unmodified lines
10539 .writepage = btrfs_writepage,10124 .writepage = btrfs_writepage,
10540 .writepages = btrfs_writepages,10125 .writepages = btrfs_writepages,
10541 .readahead = btrfs_readahead,10126 .readahead = btrfs_readahead,
10542 .direct_IO = btrfs_direct_IO,10127 .direct_IO = noop_direct_IO,
10543 .invalidatepage = btrfs_invalidatepage,10128 .invalidatepage = btrfs_invalidatepage,
10544 .releasepage = btrfs_releasepage,10129 .releasepage = btrfs_releasepage,
10545#ifdef CONFIG_MIGRATION10130#ifdef CONFIG_MIGRATION
fs/btrfs/ioctl.c+49−53
@@ -660,7 +660,7 @@ static noinline int create_subvol(struct inode *dir, · 659 unmodified lines
660 goto fail;660 goto fail;
661 661
662 key.offset = (u64)-1;662 key.offset = (u64)-1;
663 new_root = btrfs_get_fs_root(fs_info, &key, true);663 new_root = btrfs_get_fs_root(fs_info, objectid, true);
664 if (IS_ERR(new_root)) {664 if (IS_ERR(new_root)) {
665 ret = PTR_ERR(new_root);665 ret = PTR_ERR(new_root);
666 btrfs_abort_transaction(trans, ret);666 btrfs_abort_transaction(trans, ret);
@@ -748,9 +748,8 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir, · 81 unmodified lines
748 struct btrfs_pending_snapshot *pending_snapshot;748 struct btrfs_pending_snapshot *pending_snapshot;
749 struct btrfs_trans_handle *trans;749 struct btrfs_trans_handle *trans;
750 int ret;750 int ret;
751 bool snapshot_force_cow = false;
752 751
753 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))752 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
754 return -EINVAL;753 return -EINVAL;
755 754
756 if (atomic_read(&root->nr_swapfiles)) {755 if (atomic_read(&root->nr_swapfiles)) {
@@ -771,27 +770,6 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir, · 14 unmodified lines
771 goto free_pending;770 goto free_pending;
772 }771 }
773 772
774 /*
775 * Force new buffered writes to reserve space even when NOCOW is
776 * possible. This is to avoid later writeback (running dealloc) to
777 * fallback to COW mode and unexpectedly fail with ENOSPC.
778 */
779 btrfs_drew_read_lock(&root->snapshot_lock);
780
781 ret = btrfs_start_delalloc_snapshot(root);
782 if (ret)
783 goto dec_and_free;
784
785 /*
786 * All previous writes have started writeback in NOCOW mode, so now
787 * we force future writes to fallback to COW mode during snapshot
788 * creation.
789 */
790 atomic_inc(&root->snapshot_force_cow);
791 snapshot_force_cow = true;
792
793 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
794
795 btrfs_init_block_rsv(&pending_snapshot->block_rsv,773 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
796 BTRFS_BLOCK_RSV_TEMP);774 BTRFS_BLOCK_RSV_TEMP);
797 /*775 /*
@@ -806,7 +784,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir, · 8 unmodified lines
806 &pending_snapshot->block_rsv, 8,784 &pending_snapshot->block_rsv, 8,
807 false);785 false);
808 if (ret)786 if (ret)
809 goto dec_and_free;787 goto free_pending;
810 788
811 pending_snapshot->dentry = dentry;789 pending_snapshot->dentry = dentry;
812 pending_snapshot->root = root;790 pending_snapshot->root = root;
@@ -848,11 +826,6 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir, · 35 unmodified lines
848fail:826fail:
849 btrfs_put_root(pending_snapshot->snap);827 btrfs_put_root(pending_snapshot->snap);
850 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);828 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
851dec_and_free:
852 if (snapshot_force_cow)
853 atomic_dec(&root->snapshot_force_cow);
854 btrfs_drew_read_unlock(&root->snapshot_lock);
855
856free_pending:829free_pending:
857 kfree(pending_snapshot->root_item);830 kfree(pending_snapshot->root_item);
858 btrfs_free_path(pending_snapshot->path);831 btrfs_free_path(pending_snapshot->path);
@@ -983,6 +956,45 @@ out_unlock: · 124 unmodified lines
983 return error;956 return error;
984}957}
985 958
959static noinline int btrfs_mksnapshot(const struct path *parent,
960 const char *name, int namelen,
961 struct btrfs_root *root,
962 bool readonly,
963 struct btrfs_qgroup_inherit *inherit)
964{
965 int ret;
966 bool snapshot_force_cow = false;
967
968 /*
969 * Force new buffered writes to reserve space even when NOCOW is
970 * possible. This is to avoid later writeback (running dealloc) to
971 * fallback to COW mode and unexpectedly fail with ENOSPC.
972 */
973 btrfs_drew_read_lock(&root->snapshot_lock);
974
975 ret = btrfs_start_delalloc_snapshot(root);
976 if (ret)
977 goto out;
978
979 /*
980 * All previous writes have started writeback in NOCOW mode, so now
981 * we force future writes to fallback to COW mode during snapshot
982 * creation.
983 */
984 atomic_inc(&root->snapshot_force_cow);
985 snapshot_force_cow = true;
986
987 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
988
989 ret = btrfs_mksubvol(parent, name, namelen,
990 root, readonly, inherit);
991out:
992 if (snapshot_force_cow)
993 atomic_dec(&root->snapshot_force_cow);
994 btrfs_drew_read_unlock(&root->snapshot_lock);
995 return ret;
996}
997
986/*998/*
987 * When we're defragging a range, we don't want to kick it off again999 * When we're defragging a range, we don't want to kick it off again
988 * if it is really just waiting for delalloc to send it down.1000 * if it is really just waiting for delalloc to send it down.
@@ -1762,7 +1774,7 @@ static noinline int __btrfs_ioctl_snap_create(struct file *file, · 773 unmodified lines
1762 */1774 */
1763 ret = -EPERM;1775 ret = -EPERM;
1764 } else {1776 } else {
1765 ret = btrfs_mksubvol(&file->f_path, name, namelen,1777 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
1766 BTRFS_I(src_inode)->root,1778 BTRFS_I(src_inode)->root,
1767 readonly, inherit);1779 readonly, inherit);
1768 }1780 }
@@ -2127,10 +2139,7 @@ static noinline int search_ioctl(struct inode *inode, · 358 unmodified lines
2127 /* search the root of the inode that was passed */2139 /* search the root of the inode that was passed */
2128 root = btrfs_grab_root(BTRFS_I(inode)->root);2140 root = btrfs_grab_root(BTRFS_I(inode)->root);
2129 } else {2141 } else {
2130 key.objectid = sk->tree_id;2142 root = btrfs_get_fs_root(info, sk->tree_id, true);
2131 key.type = BTRFS_ROOT_ITEM_KEY;
2132 key.offset = (u64)-1;
2133 root = btrfs_get_fs_root(info, &key, true);
2134 if (IS_ERR(root)) {2143 if (IS_ERR(root)) {
2135 btrfs_free_path(path);2144 btrfs_free_path(path);
2136 return PTR_ERR(root);2145 return PTR_ERR(root);
@@ -2263,10 +2272,7 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, · 126 unmodified lines
2263 2272
2264 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];2273 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2265 2274
2266 key.objectid = tree_id;2275 root = btrfs_get_fs_root(info, tree_id, true);
2267 key.type = BTRFS_ROOT_ITEM_KEY;
2268 key.offset = (u64)-1;
2269 root = btrfs_get_fs_root(info, &key, true);
2270 if (IS_ERR(root)) {2276 if (IS_ERR(root)) {
2271 ret = PTR_ERR(root);2277 ret = PTR_ERR(root);
2272 root = NULL;2278 root = NULL;
@@ -2359,10 +2365,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode, · 86 unmodified lines
2359 if (dirid != upper_limit.objectid) {2365 if (dirid != upper_limit.objectid) {
2360 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];2366 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2361 2367
2362 key.objectid = treeid;2368 root = btrfs_get_fs_root(fs_info, treeid, true);
2363 key.type = BTRFS_ROOT_ITEM_KEY;
2364 key.offset = (u64)-1;
2365 root = btrfs_get_fs_root(fs_info, &key, true);
2366 if (IS_ERR(root)) {2369 if (IS_ERR(root)) {
2367 ret = PTR_ERR(root);2370 ret = PTR_ERR(root);
2368 goto out;2371 goto out;
@@ -2421,7 +2424,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode, · 52 unmodified lines
2421 goto out_put;2424 goto out_put;
2422 }2425 }
2423 2426
2424 temp_inode = btrfs_iget(sb, &key2, root);2427 temp_inode = btrfs_iget(sb, key2.objectid, root);
2425 if (IS_ERR(temp_inode)) {2428 if (IS_ERR(temp_inode)) {
2426 ret = PTR_ERR(temp_inode);2429 ret = PTR_ERR(temp_inode);
2427 goto out_put;2430 goto out_put;
@@ -2608,9 +2611,7 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) · 180 unmodified lines
2608 2611
2609 /* Get root_item of inode's subvolume */2612 /* Get root_item of inode's subvolume */
2610 key.objectid = BTRFS_I(inode)->root->root_key.objectid;2613 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2611 key.type = BTRFS_ROOT_ITEM_KEY;2614 root = btrfs_get_fs_root(fs_info, key.objectid, true);
2612 key.offset = (u64)-1;
2613 root = btrfs_get_fs_root(fs_info, &key, true);
2614 if (IS_ERR(root)) {2615 if (IS_ERR(root)) {
2615 ret = PTR_ERR(root);2616 ret = PTR_ERR(root);
2616 goto out_free;2617 goto out_free;
@@ -3278,7 +3279,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) · 661 unmodified lines
3278 struct btrfs_dir_item *di;3279 struct btrfs_dir_item *di;
3279 struct btrfs_trans_handle *trans;3280 struct btrfs_trans_handle *trans;
3280 struct btrfs_path *path = NULL;3281 struct btrfs_path *path = NULL;
3281 struct btrfs_key location;
3282 struct btrfs_disk_key disk_key;3282 struct btrfs_disk_key disk_key;
3283 u64 objectid = 0;3283 u64 objectid = 0;
3284 u64 dir_id;3284 u64 dir_id;
@@ -3299,11 +3299,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) · 14 unmodified lines
3299 if (!objectid)3299 if (!objectid)
3300 objectid = BTRFS_FS_TREE_OBJECTID;3300 objectid = BTRFS_FS_TREE_OBJECTID;
3301 3301
3302 location.objectid = objectid;3302 new_root = btrfs_get_fs_root(fs_info, objectid, true);
3303 location.type = BTRFS_ROOT_ITEM_KEY;
3304 location.offset = (u64)-1;
3305
3306 new_root = btrfs_get_fs_root(fs_info, &location, true);
3307 if (IS_ERR(new_root)) {3303 if (IS_ERR(new_root)) {
3308 ret = PTR_ERR(new_root);3304 ret = PTR_ERR(new_root);
3309 goto out;3305 goto out;
fs/btrfs/locking.c+1−0
@@ -410,6 +410,7 @@ void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb) · 409 unmodified lines
410 * The rwlock is held for write upon exit.410 * The rwlock is held for write upon exit.
411 */411 */
412void btrfs_tree_lock(struct extent_buffer *eb)412void btrfs_tree_lock(struct extent_buffer *eb)
413 __acquires(&eb->lock)
413{414{
414 u64 start_ns = 0;415 u64 start_ns = 0;
415 416
fs/btrfs/misc.h+54−0
@@ -6,6 +6,7 @@ · 5 unmodified lines
6#include <linux/sched.h>6#include <linux/sched.h>
7#include <linux/wait.h>7#include <linux/wait.h>
8#include <asm/div64.h>8#include <asm/div64.h>
9#include <linux/rbtree.h>
9 10
10#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))11#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
11 12
@@ -58,4 +59,57 @@ static inline bool has_single_bit_set(u64 n) · 46 unmodified lines
58 return is_power_of_two_u64(n);59 return is_power_of_two_u64(n);
59}60}
60 61
62/*
63 * Simple bytenr based rb_tree relate structures
64 *
65 * Any structure wants to use bytenr as single search index should have their
66 * structure start with these members.
67 */
68struct rb_simple_node {
69 struct rb_node rb_node;
70 u64 bytenr;
71};
72
73static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)
74{
75 struct rb_node *node = root->rb_node;
76 struct rb_simple_node *entry;
77
78 while (node) {
79 entry = rb_entry(node, struct rb_simple_node, rb_node);
80
81 if (bytenr < entry->bytenr)
82 node = node->rb_left;
83 else if (bytenr > entry->bytenr)
84 node = node->rb_right;
85 else
86 return node;
87 }
88 return NULL;
89}
90
91static inline struct rb_node *rb_simple_insert(struct rb_root *root, u64 bytenr,
92 struct rb_node *node)
93{
94 struct rb_node **p = &root->rb_node;
95 struct rb_node *parent = NULL;
96 struct rb_simple_node *entry;
97
98 while (*p) {
99 parent = *p;
100 entry = rb_entry(parent, struct rb_simple_node, rb_node);
101
102 if (bytenr < entry->bytenr)
103 p = &(*p)->rb_left;
104 else if (bytenr > entry->bytenr)
105 p = &(*p)->rb_right;
106 else
107 return parent;
108 }
109
110 rb_link_node(node, parent, p);
111 rb_insert_color(node, root);
112 return NULL;
113}
114
61#endif115#endif
fs/btrfs/props.c+2−7
@@ -408,19 +408,14 @@ int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans, · 407 unmodified lines
408 struct btrfs_root *parent_root)408 struct btrfs_root *parent_root)
409{409{
410 struct super_block *sb = root->fs_info->sb;410 struct super_block *sb = root->fs_info->sb;
411 struct btrfs_key key;
412 struct inode *parent_inode, *child_inode;411 struct inode *parent_inode, *child_inode;
413 int ret;412 int ret;
414 413
415 key.objectid = BTRFS_FIRST_FREE_OBJECTID;414 parent_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, parent_root);
416 key.type = BTRFS_INODE_ITEM_KEY;
417 key.offset = 0;
418
419 parent_inode = btrfs_iget(sb, &key, parent_root);
420 if (IS_ERR(parent_inode))415 if (IS_ERR(parent_inode))
421 return PTR_ERR(parent_inode);416 return PTR_ERR(parent_inode);
422 417
423 child_inode = btrfs_iget(sb, &key, root);418 child_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, root);
424 if (IS_ERR(child_inode)) {419 if (IS_ERR(child_inode)) {
425 iput(parent_inode);420 iput(parent_inode);
426 return PTR_ERR(child_inode);421 return PTR_ERR(child_inode);
fs/btrfs/qgroup.c+14−0
@@ -2622,6 +2622,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 2621 unmodified lines
2622 struct btrfs_root *quota_root;2622 struct btrfs_root *quota_root;
2623 struct btrfs_qgroup *srcgroup;2623 struct btrfs_qgroup *srcgroup;
2624 struct btrfs_qgroup *dstgroup;2624 struct btrfs_qgroup *dstgroup;
2625 bool need_rescan = false;
2625 u32 level_size = 0;2626 u32 level_size = 0;
2626 u64 nums;2627 u64 nums;
2627 2628
@@ -2765,6 +2766,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 137 unmodified lines
2765 goto unlock;2766 goto unlock;
2766 }2767 }
2767 ++i_qgroups;2768 ++i_qgroups;
2769
2770 /*
2771 * If we're doing a snapshot, and adding the snapshot to a new
2772 * qgroup, the numbers are guaranteed to be incorrect.
2773 */
2774 if (srcid)
2775 need_rescan = true;
2768 }2776 }
2769 2777
2770 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {2778 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
@@ -2784,6 +2792,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 13 unmodified lines
2784 2792
2785 dst->rfer = src->rfer - level_size;2793 dst->rfer = src->rfer - level_size;
2786 dst->rfer_cmpr = src->rfer_cmpr - level_size;2794 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2795
2796 /* Manually tweaking numbers certainly needs a rescan */
2797 need_rescan = true;
2787 }2798 }
2788 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {2799 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2789 struct btrfs_qgroup *src;2800 struct btrfs_qgroup *src;
@@ -2802,6 +2813,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 12 unmodified lines
2802 2813
2803 dst->excl = src->excl + level_size;2814 dst->excl = src->excl + level_size;
2804 dst->excl_cmpr = src->excl_cmpr + level_size;2815 dst->excl_cmpr = src->excl_cmpr + level_size;
2816 need_rescan = true;
2805 }2817 }
2806 2818
2807unlock:2819unlock:
@@ -2809,6 +2821,8 @@ unlock: · 1 unmodified line
2809out:2821out:
2810 if (!committing)2822 if (!committing)
2811 mutex_unlock(&fs_info->qgroup_ioctl_lock);2823 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2824 if (need_rescan)
2825 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2812 return ret;2826 return ret;
2813}2827}
2814 2828
fs/btrfs/relocation.c+288−1031
@@ -24,6 +24,7 @@ · 23 unmodified lines
24#include "delalloc-space.h"24#include "delalloc-space.h"
25#include "block-group.h"25#include "block-group.h"
26#include "backref.h"26#include "backref.h"
27#include "misc.h"
27 28
28/*29/*
29 * Relocation overview30 * Relocation overview
@@ -72,100 +73,15 @@ · 42 unmodified lines
72 * The entry point of relocation is relocate_block_group() function.73 * The entry point of relocation is relocate_block_group() function.
73 */74 */
74 75
75/*
76 * backref_node, mapping_node and tree_block start with this
77 */
78struct tree_entry {
79 struct rb_node rb_node;
80 u64 bytenr;
81};
82
83/*
84 * present a tree block in the backref cache
85 */
86struct backref_node {
87 struct rb_node rb_node;
88 u64 bytenr;
89
90 u64 new_bytenr;
91 /* objectid of tree block owner, can be not uptodate */
92 u64 owner;
93 /* link to pending, changed or detached list */
94 struct list_head list;
95 /* list of upper level blocks reference this block */
96 struct list_head upper;
97 /* list of child blocks in the cache */
98 struct list_head lower;
99 /* NULL if this node is not tree root */
100 struct btrfs_root *root;
101 /* extent buffer got by COW the block */
102 struct extent_buffer *eb;
103 /* level of tree block */
104 unsigned int level:8;
105 /* is the block in non-reference counted tree */
106 unsigned int cowonly:1;
107 /* 1 if no child node in the cache */
108 unsigned int lowest:1;
109 /* is the extent buffer locked */
110 unsigned int locked:1;
111 /* has the block been processed */
112 unsigned int processed:1;
113 /* have backrefs of this block been checked */
114 unsigned int checked:1;
115 /*
116 * 1 if corresponding block has been cowed but some upper
117 * level block pointers may not point to the new location
118 */
119 unsigned int pending:1;
120 /*
121 * 1 if the backref node isn't connected to any other
122 * backref node.
123 */
124 unsigned int detached:1;
125};
126
127/*
128 * present a block pointer in the backref cache
129 */
130struct backref_edge {
131 struct list_head list[2];
132 struct backref_node *node[2];
133};
134
135#define LOWER 0
136#define UPPER 1
137#define RELOCATION_RESERVED_NODES 25676#define RELOCATION_RESERVED_NODES 256
138
139struct backref_cache {
140 /* red black tree of all backref nodes in the cache */
141 struct rb_root rb_root;
142 /* for passing backref nodes to btrfs_reloc_cow_block */
143 struct backref_node *path[BTRFS_MAX_LEVEL];
144 /*
145 * list of blocks that have been cowed but some block
146 * pointers in upper level blocks may not reflect the
147 * new location
148 */
149 struct list_head pending[BTRFS_MAX_LEVEL];
150 /* list of backref nodes with no child node */
151 struct list_head leaves;
152 /* list of blocks that have been cowed in current transaction */
153 struct list_head changed;
154 /* list of detached backref node. */
155 struct list_head detached;
156
157 u64 last_trans;
158
159 int nr_nodes;
160 int nr_edges;
161};
162
163/*77/*
164 * map address of tree root to tree78 * map address of tree root to tree
165 */79 */
166struct mapping_node {80struct mapping_node {
167 struct rb_node rb_node;81 struct {
168 u64 bytenr;82 struct rb_node rb_node;
83 u64 bytenr;
84 }; /* Use rb_simle_node for search/insert */
169 void *data;85 void *data;
170};86};
171 87
@@ -178,8 +94,10 @@ struct mapping_tree { · 6 unmodified lines
178 * present a tree block to process94 * present a tree block to process
179 */95 */
180struct tree_block {96struct tree_block {
181 struct rb_node rb_node;97 struct {
182 u64 bytenr;98 struct rb_node rb_node;
99 u64 bytenr;
100 }; /* Use rb_simple_node for search/insert */
183 struct btrfs_key key;101 struct btrfs_key key;
184 unsigned int level:8;102 unsigned int level:8;
185 unsigned int key_ready:1;103 unsigned int key_ready:1;
@@ -204,7 +122,7 @@ struct reloc_control { · 18 unmodified lines
204 122
205 struct btrfs_block_rsv *block_rsv;123 struct btrfs_block_rsv *block_rsv;
206 124
207 struct backref_cache backref_cache;125 struct btrfs_backref_cache backref_cache;
208 126
209 struct file_extent_cluster cluster;127 struct file_extent_cluster cluster;
210 /* tree blocks have been processed */128 /* tree blocks have been processed */
@@ -235,168 +153,41 @@ struct reloc_control { · 24 unmodified lines
235#define MOVE_DATA_EXTENTS 0153#define MOVE_DATA_EXTENTS 0
236#define UPDATE_DATA_PTRS 1154#define UPDATE_DATA_PTRS 1
237 155
238static void remove_backref_node(struct backref_cache *cache,156static void mark_block_processed(struct reloc_control *rc,
239 struct backref_node *node);157 struct btrfs_backref_node *node)
240static void __mark_block_processed(struct reloc_control *rc,
241 struct backref_node *node);
242
243static void mapping_tree_init(struct mapping_tree *tree)
244{
245 tree->rb_root = RB_ROOT;
246 spin_lock_init(&tree->lock);
247}
248
249static void backref_cache_init(struct backref_cache *cache)
250{
251 int i;
252 cache->rb_root = RB_ROOT;
253 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
254 INIT_LIST_HEAD(&cache->pending[i]);
255 INIT_LIST_HEAD(&cache->changed);
256 INIT_LIST_HEAD(&cache->detached);
257 INIT_LIST_HEAD(&cache->leaves);
258}
259
260static void backref_cache_cleanup(struct backref_cache *cache)
261{
262 struct backref_node *node;
263 int i;
264
265 while (!list_empty(&cache->detached)) {
266 node = list_entry(cache->detached.next,
267 struct backref_node, list);
268 remove_backref_node(cache, node);
269 }
270
271 while (!list_empty(&cache->leaves)) {
272 node = list_entry(cache->leaves.next,
273 struct backref_node, lower);
274 remove_backref_node(cache, node);
275 }
276
277 cache->last_trans = 0;
278
279 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
280 ASSERT(list_empty(&cache->pending[i]));
281 ASSERT(list_empty(&cache->changed));
282 ASSERT(list_empty(&cache->detached));
283 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
284 ASSERT(!cache->nr_nodes);
285 ASSERT(!cache->nr_edges);
286}
287
288static struct backref_node *alloc_backref_node(struct backref_cache *cache)
289{
290 struct backref_node *node;
291
292 node = kzalloc(sizeof(*node), GFP_NOFS);
293 if (node) {
294 INIT_LIST_HEAD(&node->list);
295 INIT_LIST_HEAD(&node->upper);
296 INIT_LIST_HEAD(&node->lower);
297 RB_CLEAR_NODE(&node->rb_node);
298 cache->nr_nodes++;
299 }
300 return node;
301}
302
303static void free_backref_node(struct backref_cache *cache,
304 struct backref_node *node)
305{
306 if (node) {
307 cache->nr_nodes--;
308 btrfs_put_root(node->root);
309 kfree(node);
310 }
311}
312
313static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
314{
315 struct backref_edge *edge;
316
317 edge = kzalloc(sizeof(*edge), GFP_NOFS);
318 if (edge)
319 cache->nr_edges++;
320 return edge;
321}
322
323static void free_backref_edge(struct backref_cache *cache,
324 struct backref_edge *edge)
325{158{
326 if (edge) {159 u32 blocksize;
327 cache->nr_edges--;
328 kfree(edge);
329 }
330}
331 160
332static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,161 if (node->level == 0 ||
333 struct rb_node *node)162 in_range(node->bytenr, rc->block_group->start,
334{163 rc->block_group->length)) {
335 struct rb_node **p = &root->rb_node;164 blocksize = rc->extent_root->fs_info->nodesize;
336 struct rb_node *parent = NULL;165 set_extent_bits(&rc->processed_blocks, node->bytenr,
337 struct tree_entry *entry;166 node->bytenr + blocksize - 1, EXTENT_DIRTY);
338
339 while (*p) {
340 parent = *p;
341 entry = rb_entry(parent, struct tree_entry, rb_node);
342
343 if (bytenr < entry->bytenr)
344 p = &(*p)->rb_left;
345 else if (bytenr > entry->bytenr)
346 p = &(*p)->rb_right;
347 else
348 return parent;
349 }167 }
350 168 node->processed = 1;
351 rb_link_node(node, parent, p);
352 rb_insert_color(node, root);
353 return NULL;
354}169}
355 170
356static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
357{
358 struct rb_node *n = root->rb_node;
359 struct tree_entry *entry;
360
361 while (n) {
362 entry = rb_entry(n, struct tree_entry, rb_node);
363 171
364 if (bytenr < entry->bytenr)172static void mapping_tree_init(struct mapping_tree *tree)
365 n = n->rb_left;
366 else if (bytenr > entry->bytenr)
367 n = n->rb_right;
368 else
369 return n;
370 }
371 return NULL;
372}
373
374static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
375{173{
376 174 tree->rb_root = RB_ROOT;
377 struct btrfs_fs_info *fs_info = NULL;175 spin_lock_init(&tree->lock);
378 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
379 rb_node);
380 if (bnode->root)
381 fs_info = bnode->root->fs_info;
382 btrfs_panic(fs_info, errno,
383 "Inconsistency in backref cache found at offset %llu",
384 bytenr);
385}176}
386 177
387/*178/*
388 * walk up backref nodes until reach node presents tree root179 * walk up backref nodes until reach node presents tree root
389 */180 */
390static struct backref_node *walk_up_backref(struct backref_node *node,181static struct btrfs_backref_node *walk_up_backref(
391 struct backref_edge *edges[],182 struct btrfs_backref_node *node,
392 int *index)183 struct btrfs_backref_edge *edges[], int *index)
393{184{
394 struct backref_edge *edge;185 struct btrfs_backref_edge *edge;
395 int idx = *index;186 int idx = *index;
396 187
397 while (!list_empty(&node->upper)) {188 while (!list_empty(&node->upper)) {
398 edge = list_entry(node->upper.next,189 edge = list_entry(node->upper.next,
399 struct backref_edge, list[LOWER]);190 struct btrfs_backref_edge, list[LOWER]);
400 edges[idx++] = edge;191 edges[idx++] = edge;
401 node = edge->node[UPPER];192 node = edge->node[UPPER];
402 }193 }
@@ -408,11 +199,11 @@ static struct backref_node *walk_up_backref(struct backref_node *node, · 5 unmodified lines
408/*199/*
409 * walk down backref nodes to find start of next reference path200 * walk down backref nodes to find start of next reference path
410 */201 */
411static struct backref_node *walk_down_backref(struct backref_edge *edges[],202static struct btrfs_backref_node *walk_down_backref(
412 int *index)203 struct btrfs_backref_edge *edges[], int *index)
413{204{
414 struct backref_edge *edge;205 struct btrfs_backref_edge *edge;
415 struct backref_node *lower;206 struct btrfs_backref_node *lower;
416 int idx = *index;207 int idx = *index;
417 208
418 while (idx > 0) {209 while (idx > 0) {
@@ -423,7 +214,7 @@ static struct backref_node *walk_down_backref(struct backref_edge *edges[], · 4 unmodified lines
423 continue;214 continue;
424 }215 }
425 edge = list_entry(edge->list[LOWER].next,216 edge = list_entry(edge->list[LOWER].next,
426 struct backref_edge, list[LOWER]);217 struct btrfs_backref_edge, list[LOWER]);
427 edges[idx - 1] = edge;218 edges[idx - 1] = edge;
428 *index = idx;219 *index = idx;
429 return edge->node[UPPER];220 return edge->node[UPPER];
@@ -432,95 +223,24 @@ static struct backref_node *walk_down_backref(struct backref_edge *edges[], · 2 unmodified lines
432 return NULL;223 return NULL;
433}224}
434 225
435static void unlock_node_buffer(struct backref_node *node)226static void update_backref_node(struct btrfs_backref_cache *cache,
436{227 struct btrfs_backref_node *node, u64 bytenr)
437 if (node->locked) {
438 btrfs_tree_unlock(node->eb);
439 node->locked = 0;
440 }
441}
442
443static void drop_node_buffer(struct backref_node *node)
444{
445 if (node->eb) {
446 unlock_node_buffer(node);
447 free_extent_buffer(node->eb);
448 node->eb = NULL;
449 }
450}
451
452static void drop_backref_node(struct backref_cache *tree,
453 struct backref_node *node)
454{
455 BUG_ON(!list_empty(&node->upper));
456
457 drop_node_buffer(node);
458 list_del(&node->list);
459 list_del(&node->lower);
460 if (!RB_EMPTY_NODE(&node->rb_node))
461 rb_erase(&node->rb_node, &tree->rb_root);
462 free_backref_node(tree, node);
463}
464
465/*
466 * remove a backref node from the backref cache
467 */
468static void remove_backref_node(struct backref_cache *cache,
469 struct backref_node *node)
470{
471 struct backref_node *upper;
472 struct backref_edge *edge;
473
474 if (!node)
475 return;
476
477 BUG_ON(!node->lowest && !node->detached);
478 while (!list_empty(&node->upper)) {
479 edge = list_entry(node->upper.next, struct backref_edge,
480 list[LOWER]);
481 upper = edge->node[UPPER];
482 list_del(&edge->list[LOWER]);
483 list_del(&edge->list[UPPER]);
484 free_backref_edge(cache, edge);
485
486 if (RB_EMPTY_NODE(&upper->rb_node)) {
487 BUG_ON(!list_empty(&node->upper));
488 drop_backref_node(cache, node);
489 node = upper;
490 node->lowest = 1;
491 continue;
492 }
493 /*
494 * add the node to leaf node list if no other
495 * child block cached.
496 */
497 if (list_empty(&upper->lower)) {
498 list_add_tail(&upper->lower, &cache->leaves);
499 upper->lowest = 1;
500 }
501 }
502
503 drop_backref_node(cache, node);
504}
505
506static void update_backref_node(struct backref_cache *cache,
507 struct backref_node *node, u64 bytenr)
508{228{
509 struct rb_node *rb_node;229 struct rb_node *rb_node;
510 rb_erase(&node->rb_node, &cache->rb_root);230 rb_erase(&node->rb_node, &cache->rb_root);
511 node->bytenr = bytenr;231 node->bytenr = bytenr;
512 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);232 rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
513 if (rb_node)233 if (rb_node)
514 backref_tree_panic(rb_node, -EEXIST, bytenr);234 btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
515}235}
516 236
517/*237/*
518 * update backref cache after a transaction commit238 * update backref cache after a transaction commit
519 */239 */
520static int update_backref_cache(struct btrfs_trans_handle *trans,240static int update_backref_cache(struct btrfs_trans_handle *trans,
521 struct backref_cache *cache)241 struct btrfs_backref_cache *cache)
522{242{
523 struct backref_node *node;243 struct btrfs_backref_node *node;
524 int level = 0;244 int level = 0;
525 245
526 if (cache->last_trans == 0) {246 if (cache->last_trans == 0) {
@@ -538,13 +258,13 @@ static int update_backref_cache(struct btrfs_trans_handle *trans, · 11 unmodified lines
538 */258 */
539 while (!list_empty(&cache->detached)) {259 while (!list_empty(&cache->detached)) {
540 node = list_entry(cache->detached.next,260 node = list_entry(cache->detached.next,
541 struct backref_node, list);261 struct btrfs_backref_node, list);
542 remove_backref_node(cache, node);262 btrfs_backref_cleanup_node(cache, node);
543 }263 }
544 264
545 while (!list_empty(&cache->changed)) {265 while (!list_empty(&cache->changed)) {
546 node = list_entry(cache->changed.next,266 node = list_entry(cache->changed.next,
547 struct backref_node, list);267 struct btrfs_backref_node, list);
548 list_del_init(&node->list);268 list_del_init(&node->list);
549 BUG_ON(node->pending);269 BUG_ON(node->pending);
550 update_backref_node(cache, node, node->new_bytenr);270 update_backref_node(cache, node, node->new_bytenr);
@@ -585,7 +305,8 @@ static bool reloc_root_is_dead(struct btrfs_root *root) · 34 unmodified lines
585 *305 *
586 * Reloc tree after swap is considered dead, thus not considered as valid.306 * Reloc tree after swap is considered dead, thus not considered as valid.
587 * This is enough for most callers, as they don't distinguish dead reloc root307 * This is enough for most callers, as they don't distinguish dead reloc root
588 * from no reloc root. But should_ignore_root() below is a special case.308 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
309 * special case.
589 */310 */
590static bool have_reloc_root(struct btrfs_root *root)311static bool have_reloc_root(struct btrfs_root *root)
591{312{
@@ -596,11 +317,11 @@ static bool have_reloc_root(struct btrfs_root *root) · 4 unmodified lines
596 return true;317 return true;
597}318}
598 319
599static int should_ignore_root(struct btrfs_root *root)320int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
600{321{
601 struct btrfs_root *reloc_root;322 struct btrfs_root *reloc_root;
602 323
603 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))324 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
604 return 0;325 return 0;
605 326
606 /* This root has been merged with its reloc tree, we can ignore it */327 /* This root has been merged with its reloc tree, we can ignore it */
@@ -622,18 +343,20 @@ static int should_ignore_root(struct btrfs_root *root) · 15 unmodified lines
622 */343 */
623 return 1;344 return 1;
624}345}
346
625/*347/*
626 * find reloc tree by address of tree root348 * find reloc tree by address of tree root
627 */349 */
628static struct btrfs_root *find_reloc_root(struct reloc_control *rc,350struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
629 u64 bytenr)
630{351{
352 struct reloc_control *rc = fs_info->reloc_ctl;
631 struct rb_node *rb_node;353 struct rb_node *rb_node;
632 struct mapping_node *node;354 struct mapping_node *node;
633 struct btrfs_root *root = NULL;355 struct btrfs_root *root = NULL;
634 356
357 ASSERT(rc);
635 spin_lock(&rc->reloc_root_tree.lock);358 spin_lock(&rc->reloc_root_tree.lock);
636 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);359 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
637 if (rb_node) {360 if (rb_node) {
638 node = rb_entry(rb_node, struct mapping_node, rb_node);361 node = rb_entry(rb_node, struct mapping_node, rb_node);
639 root = (struct btrfs_root *)node->data;362 root = (struct btrfs_root *)node->data;
@@ -642,594 +365,165 @@ static struct btrfs_root *find_reloc_root(struct reloc_control *rc, · 2 unmodified lines
642 return btrfs_grab_root(root);365 return btrfs_grab_root(root);
643}366}
644 367
645static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,368/*
646 u64 root_objectid)369 * For useless nodes, do two major clean ups:
370 *
371 * - Cleanup the children edges and nodes
372 * If child node is also orphan (no parent) during cleanup, then the child
373 * node will also be cleaned up.
374 *
375 * - Freeing up leaves (level 0), keeps nodes detached
376 * For nodes, the node is still cached as "detached"
377 *
378 * Return false if @node is not in the @useless_nodes list.
379 * Return true if @node is in the @useless_nodes list.
380 */
381static bool handle_useless_nodes(struct reloc_control *rc,
382 struct btrfs_backref_node *node)
647{383{
648 struct btrfs_key key;384 struct btrfs_backref_cache *cache = &rc->backref_cache;
385 struct list_head *useless_node = &cache->useless_node;
386 bool ret = false;
649 387
650 key.objectid = root_objectid;388 while (!list_empty(useless_node)) {
651 key.type = BTRFS_ROOT_ITEM_KEY;389 struct btrfs_backref_node *cur;
652 key.offset = (u64)-1;
653 390
654 return btrfs_get_fs_root(fs_info, &key, false);391 cur = list_first_entry(useless_node, struct btrfs_backref_node,
655}392 list);
393 list_del_init(&cur->list);
656 394
657static noinline_for_stack395 /* Only tree root nodes can be added to @useless_nodes */
658int find_inline_backref(struct extent_buffer *leaf, int slot,396 ASSERT(list_empty(&cur->upper));
659 unsigned long *ptr, unsigned long *end)
660{
661 struct btrfs_key key;
662 struct btrfs_extent_item *ei;
663 struct btrfs_tree_block_info *bi;
664 u32 item_size;
665 397
666 btrfs_item_key_to_cpu(leaf, &key, slot);398 if (cur == node)
399 ret = true;
667 400
668 item_size = btrfs_item_size_nr(leaf, slot);401 /* The node is the lowest node */
669 if (item_size < sizeof(*ei)) {402 if (cur->lowest) {
670 btrfs_print_v0_err(leaf->fs_info);403 list_del_init(&cur->lower);
671 btrfs_handle_fs_error(leaf->fs_info, -EINVAL, NULL);404 cur->lowest = 0;
672 return 1;405 }
673 }
674 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
675 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
676 BTRFS_EXTENT_FLAG_TREE_BLOCK));
677 406
678 if (key.type == BTRFS_EXTENT_ITEM_KEY &&407 /* Cleanup the lower edges */
679 item_size <= sizeof(*ei) + sizeof(*bi)) {408 while (!list_empty(&cur->lower)) {
680 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));409 struct btrfs_backref_edge *edge;
681 return 1;410 struct btrfs_backref_node *lower;
682 }
683 if (key.type == BTRFS_METADATA_ITEM_KEY &&
684 item_size <= sizeof(*ei)) {
685 WARN_ON(item_size < sizeof(*ei));
686 return 1;
687 }
688 411
689 if (key.type == BTRFS_EXTENT_ITEM_KEY) {412 edge = list_entry(cur->lower.next,
690 bi = (struct btrfs_tree_block_info *)(ei + 1);413 struct btrfs_backref_edge, list[UPPER]);
691 *ptr = (unsigned long)(bi + 1);414 list_del(&edge->list[UPPER]);
692 } else {415 list_del(&edge->list[LOWER]);
693 *ptr = (unsigned long)(ei + 1);416 lower = edge->node[LOWER];
417 btrfs_backref_free_edge(cache, edge);
418
419 /* Child node is also orphan, queue for cleanup */
420 if (list_empty(&lower->upper))
421 list_add(&lower->list, useless_node);
422 }
423 /* Mark this block processed for relocation */
424 mark_block_processed(rc, cur);
425
426 /*
427 * Backref nodes for tree leaves are deleted from the cache.
428 * Backref nodes for upper level tree blocks are left in the
429 * cache to avoid unnecessary backref lookup.
430 */
431 if (cur->level > 0) {
432 list_add(&cur->list, &cache->detached);
433 cur->detached = 1;
434 } else {
435 rb_erase(&cur->rb_node, &cache->rb_root);
436 btrfs_backref_free_node(cache, cur);
437 }
694 }438 }
695 *end = (unsigned long)ei + item_size;439 return ret;
696 return 0;
697}440}
698 441
699/*442/*
700 * build backref tree for a given tree block. root of the backref tree443 * Build backref tree for a given tree block. Root of the backref tree
701 * corresponds the tree block, leaves of the backref tree correspond444 * corresponds the tree block, leaves of the backref tree correspond roots of
702 * roots of b-trees that reference the tree block.445 * b-trees that reference the tree block.
703 *446 *
704 * the basic idea of this function is check backrefs of a given block447 * The basic idea of this function is check backrefs of a given block to find
705 * to find upper level blocks that reference the block, and then check448 * upper level blocks that reference the block, and then check backrefs of
706 * backrefs of these upper level blocks recursively. the recursion stop449 * these upper level blocks recursively. The recursion stops when tree root is
707 * when tree root is reached or backrefs for the block is cached.450 * reached or backrefs for the block is cached.
708 *451 *
709 * NOTE: if we find backrefs for a block are cached, we know backrefs452 * NOTE: if we find that backrefs for a block are cached, we know backrefs for
710 * for all upper level blocks that directly/indirectly reference the453 * all upper level blocks that directly/indirectly reference the block are also
711 * block are also cached.454 * cached.
712 */455 */
713static noinline_for_stack456static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
714struct backref_node *build_backref_tree(struct reloc_control *rc,457 struct reloc_control *rc, struct btrfs_key *node_key,
715 struct btrfs_key *node_key,458 int level, u64 bytenr)
716 int level, u64 bytenr)
717{459{
718 struct backref_cache *cache = &rc->backref_cache;460 struct btrfs_backref_iter *iter;
719 struct btrfs_path *path1; /* For searching extent root */461 struct btrfs_backref_cache *cache = &rc->backref_cache;
720 struct btrfs_path *path2; /* For searching parent of TREE_BLOCK_REF */462 /* For searching parent of TREE_BLOCK_REF */
721 struct extent_buffer *eb;463 struct btrfs_path *path;
722 struct btrfs_root *root;464 struct btrfs_backref_node *cur;
723 struct backref_node *cur;465 struct btrfs_backref_node *node = NULL;
724 struct backref_node *upper;466 struct btrfs_backref_edge *edge;
725 struct backref_node *lower;
726 struct backref_node *node = NULL;
727 struct backref_node *exist = NULL;
728 struct backref_edge *edge;
729 struct rb_node *rb_node;
730 struct btrfs_key key;
731 unsigned long end;
732 unsigned long ptr;
733 LIST_HEAD(list); /* Pending edge list, upper node needs to be checked */
734 LIST_HEAD(useless);
735 int cowonly;
736 int ret;467 int ret;
737 int err = 0;468 int err = 0;
738 bool need_check = true;
739 469
740 path1 = btrfs_alloc_path();470 iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
741 path2 = btrfs_alloc_path();471 if (!iter)
742 if (!path1 || !path2) {472 return ERR_PTR(-ENOMEM);
473 path = btrfs_alloc_path();
474 if (!path) {
743 err = -ENOMEM;475 err = -ENOMEM;
744 goto out;476 goto out;
745 }477 }
746 478
747 node = alloc_backref_node(cache);479 node = btrfs_backref_alloc_node(cache, bytenr, level);
748 if (!node) {480 if (!node) {
749 err = -ENOMEM;481 err = -ENOMEM;
750 goto out;482 goto out;
751 }483 }
752 484
753 node->bytenr = bytenr;
754 node->level = level;
755 node->lowest = 1;485 node->lowest = 1;
756 cur = node;486 cur = node;
757again:
758 end = 0;
759 ptr = 0;
760 key.objectid = cur->bytenr;
761 key.type = BTRFS_METADATA_ITEM_KEY;
762 key.offset = (u64)-1;
763
764 path1->search_commit_root = 1;
765 path1->skip_locking = 1;
766 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
767 0, 0);
768 if (ret < 0) {
769 err = ret;
770 goto out;
771 }
772 ASSERT(ret);
773 ASSERT(path1->slots[0]);
774
775 path1->slots[0]--;
776 487
777 WARN_ON(cur->checked);488 /* Breadth-first search to build backref cache */
778 if (!list_empty(&cur->upper)) {489 do {
779 /*490 ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
780 * the backref was added previously when processing491 cur);
781 * backref of type BTRFS_TREE_BLOCK_REF_KEY
782 */
783 ASSERT(list_is_singular(&cur->upper));
784 edge = list_entry(cur->upper.next, struct backref_edge,
785 list[LOWER]);
786 ASSERT(list_empty(&edge->list[UPPER]));
787 exist = edge->node[UPPER];
788 /*
789 * add the upper level block to pending list if we need
790 * check its backrefs
791 */
792 if (!exist->checked)
793 list_add_tail(&edge->list[UPPER], &list);
794 } else {
795 exist = NULL;
796 }
797
798 while (1) {
799 cond_resched();
800 eb = path1->nodes[0];
801
802 if (ptr >= end) {
803 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
804 ret = btrfs_next_leaf(rc->extent_root, path1);
805 if (ret < 0) {
806 err = ret;
807 goto out;
808 }
809 if (ret > 0)
810 break;
811 eb = path1->nodes[0];
812 }
813
814 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
815 if (key.objectid != cur->bytenr) {
816 WARN_ON(exist);
817 break;
818 }
819
820 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
821 key.type == BTRFS_METADATA_ITEM_KEY) {
822 ret = find_inline_backref(eb, path1->slots[0],
823 &ptr, &end);
824 if (ret)
825 goto next;
826 }
827 }
828
829 if (ptr < end) {
830 /* update key for inline back ref */
831 struct btrfs_extent_inline_ref *iref;
832 int type;
833 iref = (struct btrfs_extent_inline_ref *)ptr;
834 type = btrfs_get_extent_inline_ref_type(eb, iref,
835 BTRFS_REF_TYPE_BLOCK);
836 if (type == BTRFS_REF_TYPE_INVALID) {
837 err = -EUCLEAN;
838 goto out;
839 }
840 key.type = type;
841 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
842
843 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
844 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
845 }
846
847 /*
848 * Parent node found and matches current inline ref, no need to
849 * rebuild this node for this inline ref.
850 */
851 if (exist &&
852 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
853 exist->owner == key.offset) ||
854 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
855 exist->bytenr == key.offset))) {
856 exist = NULL;
857 goto next;
858 }
859
860 /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
861 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
862 if (key.objectid == key.offset) {
863 /*
864 * Only root blocks of reloc trees use backref
865 * pointing to itself.
866 */
867 root = find_reloc_root(rc, cur->bytenr);
868 ASSERT(root);
869 cur->root = root;
870 break;
871 }
872
873 edge = alloc_backref_edge(cache);
874 if (!edge) {
875 err = -ENOMEM;
876 goto out;
877 }
878 rb_node = tree_search(&cache->rb_root, key.offset);
879 if (!rb_node) {
880 upper = alloc_backref_node(cache);
881 if (!upper) {
882 free_backref_edge(cache, edge);
883 err = -ENOMEM;
884 goto out;
885 }
886 upper->bytenr = key.offset;
887 upper->level = cur->level + 1;
888 /*
889 * backrefs for the upper level block isn't
890 * cached, add the block to pending list
891 */
892 list_add_tail(&edge->list[UPPER], &list);
893 } else {
894 upper = rb_entry(rb_node, struct backref_node,
895 rb_node);
896 ASSERT(upper->checked);
897 INIT_LIST_HEAD(&edge->list[UPPER]);
898 }
899 list_add_tail(&edge->list[LOWER], &cur->upper);
900 edge->node[LOWER] = cur;
901 edge->node[UPPER] = upper;
902
903 goto next;
904 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
905 err = -EINVAL;
906 btrfs_print_v0_err(rc->extent_root->fs_info);
907 btrfs_handle_fs_error(rc->extent_root->fs_info, err,
908 NULL);
909 goto out;
910 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
911 goto next;
912 }
913
914 /*
915 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
916 * means the root objectid. We need to search the tree to get
917 * its parent bytenr.
918 */
919 root = read_fs_root(rc->extent_root->fs_info, key.offset);
920 if (IS_ERR(root)) {
921 err = PTR_ERR(root);
922 goto out;
923 }
924
925 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
926 cur->cowonly = 1;
927
928 if (btrfs_root_level(&root->root_item) == cur->level) {
929 /* tree root */
930 ASSERT(btrfs_root_bytenr(&root->root_item) ==
931 cur->bytenr);
932 if (should_ignore_root(root)) {
933 btrfs_put_root(root);
934 list_add(&cur->list, &useless);
935 } else {
936 cur->root = root;
937 }
938 break;
939 }
940
941 level = cur->level + 1;
942
943 /* Search the tree to find parent blocks referring the block. */
944 path2->search_commit_root = 1;
945 path2->skip_locking = 1;
946 path2->lowest_level = level;
947 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
948 path2->lowest_level = 0;
949 if (ret < 0) {492 if (ret < 0) {
950 btrfs_put_root(root);
951 err = ret;493 err = ret;
952 goto out;494 goto out;
953 }495 }
954 if (ret > 0 && path2->slots[level] > 0)496 edge = list_first_entry_or_null(&cache->pending_edge,
955 path2->slots[level]--;497 struct btrfs_backref_edge, list[UPPER]);
956 498 /*
957 eb = path2->nodes[level];499 * The pending list isn't empty, take the first block to
958 if (btrfs_node_blockptr(eb, path2->slots[level]) !=500 * process
959 cur->bytenr) {501 */
960 btrfs_err(root->fs_info,502 if (edge) {
961 "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",503 list_del_init(&edge->list[UPPER]);
962 cur->bytenr, level - 1,504 cur = edge->node[UPPER];
963 root->root_key.objectid,
964 node_key->objectid, node_key->type,
965 node_key->offset);
966 btrfs_put_root(root);
967 err = -ENOENT;
968 goto out;
969 }
970 lower = cur;
971 need_check = true;
972
973 /* Add all nodes and edges in the path */
974 for (; level < BTRFS_MAX_LEVEL; level++) {
975 if (!path2->nodes[level]) {
976 ASSERT(btrfs_root_bytenr(&root->root_item) ==
977 lower->bytenr);
978 if (should_ignore_root(root)) {
979 btrfs_put_root(root);
980 list_add(&lower->list, &useless);
981 } else {
982 lower->root = root;
983 }
984 break;
985 }
986
987 edge = alloc_backref_edge(cache);
988 if (!edge) {
989 btrfs_put_root(root);
990 err = -ENOMEM;
991 goto out;
992 }
993
994 eb = path2->nodes[level];
995 rb_node = tree_search(&cache->rb_root, eb->start);
996 if (!rb_node) {
997 upper = alloc_backref_node(cache);
998 if (!upper) {
999 btrfs_put_root(root);
1000 free_backref_edge(cache, edge);
1001 err = -ENOMEM;
1002 goto out;
1003 }
1004 upper->bytenr = eb->start;
1005 upper->owner = btrfs_header_owner(eb);
1006 upper->level = lower->level + 1;
1007 if (!test_bit(BTRFS_ROOT_REF_COWS,
1008 &root->state))
1009 upper->cowonly = 1;
1010
1011 /*
1012 * if we know the block isn't shared
1013 * we can void checking its backrefs.
1014 */
1015 if (btrfs_block_can_be_shared(root, eb))
1016 upper->checked = 0;
1017 else
1018 upper->checked = 1;
1019
1020 /*
1021 * add the block to pending list if we
1022 * need check its backrefs, we only do this once
1023 * while walking up a tree as we will catch
1024 * anything else later on.
1025 */
1026 if (!upper->checked && need_check) {
1027 need_check = false;
1028 list_add_tail(&edge->list[UPPER],
1029 &list);
1030 } else {
1031 if (upper->checked)
1032 need_check = true;
1033 INIT_LIST_HEAD(&edge->list[UPPER]);
1034 }
1035 } else {
1036 upper = rb_entry(rb_node, struct backref_node,
1037 rb_node);
1038 ASSERT(upper->checked);
1039 INIT_LIST_HEAD(&edge->list[UPPER]);
1040 if (!upper->owner)
1041 upper->owner = btrfs_header_owner(eb);
1042 }
1043 list_add_tail(&edge->list[LOWER], &lower->upper);
1044 edge->node[LOWER] = lower;
1045 edge->node[UPPER] = upper;
1046
1047 if (rb_node) {
1048 btrfs_put_root(root);
1049 break;
1050 }
1051 lower = upper;
1052 upper = NULL;
1053 }
1054 btrfs_release_path(path2);
1055next:
1056 if (ptr < end) {
1057 ptr += btrfs_extent_inline_ref_size(key.type);
1058 if (ptr >= end) {
1059 WARN_ON(ptr > end);
1060 ptr = 0;
1061 end = 0;
1062 }
1063 }
1064 if (ptr >= end)
1065 path1->slots[0]++;
1066 }
1067 btrfs_release_path(path1);
1068
1069 cur->checked = 1;
1070 WARN_ON(exist);
1071
1072 /* the pending list isn't empty, take the first block to process */
1073 if (!list_empty(&list)) {
1074 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1075 list_del_init(&edge->list[UPPER]);
1076 cur = edge->node[UPPER];
1077 goto again;
1078 }
1079
1080 /*
1081 * everything goes well, connect backref nodes and insert backref nodes
1082 * into the cache.
1083 */
1084 ASSERT(node->checked);
1085 cowonly = node->cowonly;
1086 if (!cowonly) {
1087 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1088 &node->rb_node);
1089 if (rb_node)
1090 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1091 list_add_tail(&node->lower, &cache->leaves);
1092 }
1093
1094 list_for_each_entry(edge, &node->upper, list[LOWER])
1095 list_add_tail(&edge->list[UPPER], &list);
1096
1097 while (!list_empty(&list)) {
1098 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1099 list_del_init(&edge->list[UPPER]);
1100 upper = edge->node[UPPER];
1101 if (upper->detached) {
1102 list_del(&edge->list[LOWER]);
1103 lower = edge->node[LOWER];
1104 free_backref_edge(cache, edge);
1105 if (list_empty(&lower->upper))
1106 list_add(&lower->list, &useless);
1107 continue;
1108 }
1109
1110 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1111 if (upper->lowest) {
1112 list_del_init(&upper->lower);
1113 upper->lowest = 0;
1114 }
1115
1116 list_add_tail(&edge->list[UPPER], &upper->lower);
1117 continue;
1118 }
1119
1120 if (!upper->checked) {
1121 /*
1122 * Still want to blow up for developers since this is a
1123 * logic bug.
1124 */
1125 ASSERT(0);
1126 err = -EINVAL;
1127 goto out;
1128 }
1129 if (cowonly != upper->cowonly) {
1130 ASSERT(0);
1131 err = -EINVAL;
1132 goto out;
1133 }
1134
1135 if (!cowonly) {
1136 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1137 &upper->rb_node);
1138 if (rb_node)
1139 backref_tree_panic(rb_node, -EEXIST,
1140 upper->bytenr);
1141 }505 }
506 } while (edge);
1142 507
1143 list_add_tail(&edge->list[UPPER], &upper->lower);508 /* Finish the upper linkage of newly added edges/nodes */
1144 509 ret = btrfs_backref_finish_upper_links(cache, node);
1145 list_for_each_entry(edge, &upper->upper, list[LOWER])510 if (ret < 0) {
1146 list_add_tail(&edge->list[UPPER], &list);511 err = ret;
512 goto out;
1147 }513 }
1148 /*
1149 * process useless backref nodes. backref nodes for tree leaves
1150 * are deleted from the cache. backref nodes for upper level
1151 * tree blocks are left in the cache to avoid unnecessary backref
1152 * lookup.
1153 */
1154 while (!list_empty(&useless)) {
1155 upper = list_entry(useless.next, struct backref_node, list);
1156 list_del_init(&upper->list);
1157 ASSERT(list_empty(&upper->upper));
1158 if (upper == node)
1159 node = NULL;
1160 if (upper->lowest) {
1161 list_del_init(&upper->lower);
1162 upper->lowest = 0;
1163 }
1164 while (!list_empty(&upper->lower)) {
1165 edge = list_entry(upper->lower.next,
1166 struct backref_edge, list[UPPER]);
1167 list_del(&edge->list[UPPER]);
1168 list_del(&edge->list[LOWER]);
1169 lower = edge->node[LOWER];
1170 free_backref_edge(cache, edge);
1171 514
1172 if (list_empty(&lower->upper))515 if (handle_useless_nodes(rc, node))
1173 list_add(&lower->list, &useless);516 node = NULL;
1174 }
1175 __mark_block_processed(rc, upper);
1176 if (upper->level > 0) {
1177 list_add(&upper->list, &cache->detached);
1178 upper->detached = 1;
1179 } else {
1180 rb_erase(&upper->rb_node, &cache->rb_root);
1181 free_backref_node(cache, upper);
1182 }
1183 }
1184out:517out:
1185 btrfs_free_path(path1);518 btrfs_backref_iter_free(iter);
1186 btrfs_free_path(path2);519 btrfs_free_path(path);
1187 if (err) {520 if (err) {
1188 while (!list_empty(&useless)) {521 btrfs_backref_error_cleanup(cache, node);
1189 lower = list_entry(useless.next,
1190 struct backref_node, list);
1191 list_del_init(&lower->list);
1192 }
1193 while (!list_empty(&list)) {
1194 edge = list_first_entry(&list, struct backref_edge,
1195 list[UPPER]);
1196 list_del(&edge->list[UPPER]);
1197 list_del(&edge->list[LOWER]);
1198 lower = edge->node[LOWER];
1199 upper = edge->node[UPPER];
1200 free_backref_edge(cache, edge);
1201
1202 /*
1203 * Lower is no longer linked to any upper backref nodes
1204 * and isn't in the cache, we can free it ourselves.
1205 */
1206 if (list_empty(&lower->upper) &&
1207 RB_EMPTY_NODE(&lower->rb_node))
1208 list_add(&lower->list, &useless);
1209
1210 if (!RB_EMPTY_NODE(&upper->rb_node))
1211 continue;
1212
1213 /* Add this guy's upper edges to the list to process */
1214 list_for_each_entry(edge, &upper->upper, list[LOWER])
1215 list_add_tail(&edge->list[UPPER], &list);
1216 if (list_empty(&upper->upper))
1217 list_add(&upper->list, &useless);
1218 }
1219
1220 while (!list_empty(&useless)) {
1221 lower = list_entry(useless.next,
1222 struct backref_node, list);
1223 list_del_init(&lower->list);
1224 if (lower == node)
1225 node = NULL;
1226 free_backref_node(cache, lower);
1227 }
1228
1229 remove_backref_node(cache, node);
1230 return ERR_PTR(err);522 return ERR_PTR(err);
1231 }523 }
1232 ASSERT(!node || !node->detached);524 ASSERT(!node || !node->detached);
525 ASSERT(list_empty(&cache->useless_node) &&
526 list_empty(&cache->pending_edge));
1233 return node;527 return node;
1234}528}
1235 529
@@ -1244,19 +538,19 @@ static int clone_backref_node(struct btrfs_trans_handle *trans, · 8 unmodified lines
1244 struct btrfs_root *dest)538 struct btrfs_root *dest)
1245{539{
1246 struct btrfs_root *reloc_root = src->reloc_root;540 struct btrfs_root *reloc_root = src->reloc_root;
1247 struct backref_cache *cache = &rc->backref_cache;541 struct btrfs_backref_cache *cache = &rc->backref_cache;
1248 struct backref_node *node = NULL;542 struct btrfs_backref_node *node = NULL;
1249 struct backref_node *new_node;543 struct btrfs_backref_node *new_node;
1250 struct backref_edge *edge;544 struct btrfs_backref_edge *edge;
1251 struct backref_edge *new_edge;545 struct btrfs_backref_edge *new_edge;
1252 struct rb_node *rb_node;546 struct rb_node *rb_node;
1253 547
1254 if (cache->last_trans > 0)548 if (cache->last_trans > 0)
1255 update_backref_cache(trans, cache);549 update_backref_cache(trans, cache);
1256 550
1257 rb_node = tree_search(&cache->rb_root, src->commit_root->start);551 rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
1258 if (rb_node) {552 if (rb_node) {
1259 node = rb_entry(rb_node, struct backref_node, rb_node);553 node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
1260 if (node->detached)554 if (node->detached)
1261 node = NULL;555 node = NULL;
1262 else556 else
@@ -1264,10 +558,10 @@ static int clone_backref_node(struct btrfs_trans_handle *trans, · 1 unmodified line
1264 }558 }
1265 559
1266 if (!node) {560 if (!node) {
1267 rb_node = tree_search(&cache->rb_root,561 rb_node = rb_simple_search(&cache->rb_root,
1268 reloc_root->commit_root->start);562 reloc_root->commit_root->start);
1269 if (rb_node) {563 if (rb_node) {
1270 node = rb_entry(rb_node, struct backref_node,564 node = rb_entry(rb_node, struct btrfs_backref_node,
1271 rb_node);565 rb_node);
1272 BUG_ON(node->detached);566 BUG_ON(node->detached);
1273 }567 }
@@ -1276,12 +570,11 @@ static int clone_backref_node(struct btrfs_trans_handle *trans, · 2 unmodified lines
1276 if (!node)570 if (!node)
1277 return 0;571 return 0;
1278 572
1279 new_node = alloc_backref_node(cache);573 new_node = btrfs_backref_alloc_node(cache, dest->node->start,
574 node->level);
1280 if (!new_node)575 if (!new_node)
1281 return -ENOMEM;576 return -ENOMEM;
1282 577
1283 new_node->bytenr = dest->node->start;
1284 new_node->level = node->level;
1285 new_node->lowest = node->lowest;578 new_node->lowest = node->lowest;
1286 new_node->checked = 1;579 new_node->checked = 1;
1287 new_node->root = btrfs_grab_root(dest);580 new_node->root = btrfs_grab_root(dest);
@@ -1289,23 +582,21 @@ static int clone_backref_node(struct btrfs_trans_handle *trans, · 1 unmodified line
1289 582
1290 if (!node->lowest) {583 if (!node->lowest) {
1291 list_for_each_entry(edge, &node->lower, list[UPPER]) {584 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1292 new_edge = alloc_backref_edge(cache);585 new_edge = btrfs_backref_alloc_edge(cache);
1293 if (!new_edge)586 if (!new_edge)
1294 goto fail;587 goto fail;
1295 588
1296 new_edge->node[UPPER] = new_node;589 btrfs_backref_link_edge(new_edge, edge->node[LOWER],
1297 new_edge->node[LOWER] = edge->node[LOWER];590 new_node, LINK_UPPER);
1298 list_add_tail(&new_edge->list[UPPER],
1299 &new_node->lower);
1300 }591 }
1301 } else {592 } else {
1302 list_add_tail(&new_node->lower, &cache->leaves);593 list_add_tail(&new_node->lower, &cache->leaves);
1303 }594 }
1304 595
1305 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,596 rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
1306 &new_node->rb_node);597 &new_node->rb_node);
1307 if (rb_node)598 if (rb_node)
1308 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);599 btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
1309 600
1310 if (!new_node->lowest) {601 if (!new_node->lowest) {
1311 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {602 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
@@ -1317,11 +608,11 @@ static int clone_backref_node(struct btrfs_trans_handle *trans, · 5 unmodified lines
1317fail:608fail:
1318 while (!list_empty(&new_node->lower)) {609 while (!list_empty(&new_node->lower)) {
1319 new_edge = list_entry(new_node->lower.next,610 new_edge = list_entry(new_node->lower.next,
1320 struct backref_edge, list[UPPER]);611 struct btrfs_backref_edge, list[UPPER]);
1321 list_del(&new_edge->list[UPPER]);612 list_del(&new_edge->list[UPPER]);
1322 free_backref_edge(cache, new_edge);613 btrfs_backref_free_edge(cache, new_edge);
1323 }614 }
1324 free_backref_node(cache, new_node);615 btrfs_backref_free_node(cache, new_node);
1325 return -ENOMEM;616 return -ENOMEM;
1326}617}
1327 618
@@ -1343,8 +634,8 @@ static int __must_check __add_reloc_root(struct btrfs_root *root) · 15 unmodified lines
1343 node->data = root;634 node->data = root;
1344 635
1345 spin_lock(&rc->reloc_root_tree.lock);636 spin_lock(&rc->reloc_root_tree.lock);
1346 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,637 rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
1347 node->bytenr, &node->rb_node);638 node->bytenr, &node->rb_node);
1348 spin_unlock(&rc->reloc_root_tree.lock);639 spin_unlock(&rc->reloc_root_tree.lock);
1349 if (rb_node) {640 if (rb_node) {
1350 btrfs_panic(fs_info, -EEXIST,641 btrfs_panic(fs_info, -EEXIST,
@@ -1370,8 +661,8 @@ static void __del_reloc_root(struct btrfs_root *root) · 19 unmodified lines
1370 661
1371 if (rc && root->node) {662 if (rc && root->node) {
1372 spin_lock(&rc->reloc_root_tree.lock);663 spin_lock(&rc->reloc_root_tree.lock);
1373 rb_node = tree_search(&rc->reloc_root_tree.rb_root,664 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
1374 root->commit_root->start);665 root->commit_root->start);
1375 if (rb_node) {666 if (rb_node) {
1376 node = rb_entry(rb_node, struct mapping_node, rb_node);667 node = rb_entry(rb_node, struct mapping_node, rb_node);
1377 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);668 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
@@ -1414,8 +705,8 @@ static int __update_reloc_root(struct btrfs_root *root) · 36 unmodified lines
1414 struct reloc_control *rc = fs_info->reloc_ctl;705 struct reloc_control *rc = fs_info->reloc_ctl;
1415 706
1416 spin_lock(&rc->reloc_root_tree.lock);707 spin_lock(&rc->reloc_root_tree.lock);
1417 rb_node = tree_search(&rc->reloc_root_tree.rb_root,708 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
1418 root->commit_root->start);709 root->commit_root->start);
1419 if (rb_node) {710 if (rb_node) {
1420 node = rb_entry(rb_node, struct mapping_node, rb_node);711 node = rb_entry(rb_node, struct mapping_node, rb_node);
1421 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);712 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
@@ -1428,11 +719,11 @@ static int __update_reloc_root(struct btrfs_root *root) · 6 unmodified lines
1428 719
1429 spin_lock(&rc->reloc_root_tree.lock);720 spin_lock(&rc->reloc_root_tree.lock);
1430 node->bytenr = root->node->start;721 node->bytenr = root->node->start;
1431 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,722 rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
1432 node->bytenr, &node->rb_node);723 node->bytenr, &node->rb_node);
1433 spin_unlock(&rc->reloc_root_tree.lock);724 spin_unlock(&rc->reloc_root_tree.lock);
1434 if (rb_node)725 if (rb_node)
1435 backref_tree_panic(rb_node, -EEXIST, node->bytenr);726 btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
1436 return 0;727 return 0;
1437}728}
1438 729
@@ -1505,7 +796,7 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, · 66 unmodified lines
1505 796
1506 reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);797 reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
1507 BUG_ON(IS_ERR(reloc_root));798 BUG_ON(IS_ERR(reloc_root));
1508 set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);799 set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
1509 reloc_root->last_trans = trans->transid;800 reloc_root->last_trans = trans->transid;
1510 return reloc_root;801 return reloc_root;
1511}802}
@@ -1679,14 +970,6 @@ again: · 167 unmodified lines
1679 return NULL;970 return NULL;
1680}971}
1681 972
1682static int in_block_group(u64 bytenr, struct btrfs_block_group *block_group)
1683{
1684 if (bytenr >= block_group->start &&
1685 bytenr < block_group->start + block_group->length)
1686 return 1;
1687 return 0;
1688}
1689
1690/*973/*
1691 * get new location of data974 * get new location of data
1692 */975 */
@@ -1784,7 +1067,8 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 91 unmodified lines
1784 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);1067 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1785 if (bytenr == 0)1068 if (bytenr == 0)
1786 continue;1069 continue;
1787 if (!in_block_group(bytenr, rc->block_group))1070 if (!in_range(bytenr, rc->block_group->start,
1071 rc->block_group->length))
1788 continue;1072 continue;
1789 1073
1790 /*1074 /*
@@ -1940,7 +1224,7 @@ again: · 149 unmodified lines
1940 level = btrfs_header_level(parent);1224 level = btrfs_header_level(parent);
1941 BUG_ON(level < lowest_level);1225 BUG_ON(level < lowest_level);
1942 1226
1943 ret = btrfs_bin_search(parent, &key, level, &slot);1227 ret = btrfs_bin_search(parent, &key, &slot);
1944 if (ret < 0)1228 if (ret < 0)
1945 break;1229 break;
1946 if (ret && slot > 0)1230 if (ret && slot > 0)
@@ -2560,7 +1844,8 @@ again: · 613 unmodified lines
2560 struct btrfs_root, root_list);1844 struct btrfs_root, root_list);
2561 list_del_init(&reloc_root->root_list);1845 list_del_init(&reloc_root->root_list);
2562 1846
2563 root = read_fs_root(fs_info, reloc_root->root_key.offset);1847 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1848 false);
2564 BUG_ON(IS_ERR(root));1849 BUG_ON(IS_ERR(root));
2565 BUG_ON(root->reloc_root != reloc_root);1850 BUG_ON(root->reloc_root != reloc_root);
2566 1851
@@ -2588,13 +1873,10 @@ again: · 21 unmodified lines
2588static noinline_for_stack1873static noinline_for_stack
2589void free_reloc_roots(struct list_head *list)1874void free_reloc_roots(struct list_head *list)
2590{1875{
2591 struct btrfs_root *reloc_root;1876 struct btrfs_root *reloc_root, *tmp;
2592 1877
2593 while (!list_empty(list)) {1878 list_for_each_entry_safe(reloc_root, tmp, list, root_list)
2594 reloc_root = list_entry(list->next, struct btrfs_root,
2595 root_list);
2596 __del_reloc_root(reloc_root);1879 __del_reloc_root(reloc_root);
2597 }
2598}1880}
2599 1881
2600static noinline_for_stack1882static noinline_for_stack
@@ -2624,12 +1906,11 @@ again: · 23 unmodified lines
2624 reloc_root = list_entry(reloc_roots.next,1906 reloc_root = list_entry(reloc_roots.next,
2625 struct btrfs_root, root_list);1907 struct btrfs_root, root_list);
2626 1908
1909 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1910 false);
2627 if (btrfs_root_refs(&reloc_root->root_item) > 0) {1911 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2628 root = read_fs_root(fs_info,
2629 reloc_root->root_key.offset);
2630 BUG_ON(IS_ERR(root));1912 BUG_ON(IS_ERR(root));
2631 BUG_ON(root->reloc_root != reloc_root);1913 BUG_ON(root->reloc_root != reloc_root);
2632
2633 ret = merge_reloc_root(rc, root);1914 ret = merge_reloc_root(rc, root);
2634 btrfs_put_root(root);1915 btrfs_put_root(root);
2635 if (ret) {1916 if (ret) {
@@ -2639,6 +1920,16 @@ again: · 3 unmodified lines
2639 goto out;1920 goto out;
2640 }1921 }
2641 } else {1922 } else {
1923 if (!IS_ERR(root)) {
1924 if (root->reloc_root == reloc_root) {
1925 root->reloc_root = NULL;
1926 btrfs_put_root(reloc_root);
1927 }
1928 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
1929 &root->state);
1930 btrfs_put_root(root);
1931 }
1932
2642 list_del_init(&reloc_root->root_list);1933 list_del_init(&reloc_root->root_list);
2643 /* Don't forget to queue this reloc root for cleanup */1934 /* Don't forget to queue this reloc root for cleanup */
2644 list_add_tail(&reloc_root->reloc_dirty_list,1935 list_add_tail(&reloc_root->reloc_dirty_list,
@@ -2653,15 +1944,13 @@ again: · 8 unmodified lines
2653out:1944out:
2654 if (ret) {1945 if (ret) {
2655 btrfs_handle_fs_error(fs_info, ret, NULL);1946 btrfs_handle_fs_error(fs_info, ret, NULL);
2656 if (!list_empty(&reloc_roots))1947 free_reloc_roots(&reloc_roots);
2657 free_reloc_roots(&reloc_roots);
2658 1948
2659 /* new reloc root may be added */1949 /* new reloc root may be added */
2660 mutex_lock(&fs_info->reloc_mutex);1950 mutex_lock(&fs_info->reloc_mutex);
2661 list_splice_init(&rc->reloc_roots, &reloc_roots);1951 list_splice_init(&rc->reloc_roots, &reloc_roots);
2662 mutex_unlock(&fs_info->reloc_mutex);1952 mutex_unlock(&fs_info->reloc_mutex);
2663 if (!list_empty(&reloc_roots))1953 free_reloc_roots(&reloc_roots);
2664 free_reloc_roots(&reloc_roots);
2665 }1954 }
2666 1955
2667 /*1956 /*
@@ -2702,7 +1991,7 @@ static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, · 34 unmodified lines
2702 if (reloc_root->last_trans == trans->transid)1991 if (reloc_root->last_trans == trans->transid)
2703 return 0;1992 return 0;
2704 1993
2705 root = read_fs_root(fs_info, reloc_root->root_key.offset);1994 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
2706 BUG_ON(IS_ERR(root));1995 BUG_ON(IS_ERR(root));
2707 BUG_ON(root->reloc_root != reloc_root);1996 BUG_ON(root->reloc_root != reloc_root);
2708 ret = btrfs_record_root_in_trans(trans, root);1997 ret = btrfs_record_root_in_trans(trans, root);
@@ -2714,10 +2003,10 @@ static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, · 5 unmodified lines
2714static noinline_for_stack2003static noinline_for_stack
2715struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,2004struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2716 struct reloc_control *rc,2005 struct reloc_control *rc,
2717 struct backref_node *node,2006 struct btrfs_backref_node *node,
2718 struct backref_edge *edges[])2007 struct btrfs_backref_edge *edges[])
2719{2008{
2720 struct backref_node *next;2009 struct btrfs_backref_node *next;
2721 struct btrfs_root *root;2010 struct btrfs_root *root;
2722 int index = 0;2011 int index = 0;
2723 2012
@@ -2727,7 +2016,7 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, · 3 unmodified lines
2727 next = walk_up_backref(next, edges, &index);2016 next = walk_up_backref(next, edges, &index);
2728 root = next->root;2017 root = next->root;
2729 BUG_ON(!root);2018 BUG_ON(!root);
2730 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));2019 BUG_ON(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state));
2731 2020
2732 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {2021 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2733 record_reloc_root_in_trans(trans, root);2022 record_reloc_root_in_trans(trans, root);
@@ -2746,7 +2035,7 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, · 12 unmodified lines
2746 ASSERT(next->root);2035 ASSERT(next->root);
2747 list_add_tail(&next->list,2036 list_add_tail(&next->list,
2748 &rc->backref_cache.changed);2037 &rc->backref_cache.changed);
2749 __mark_block_processed(rc, next);2038 mark_block_processed(rc, next);
2750 break;2039 break;
2751 }2040 }
2752 2041
@@ -2771,18 +2060,21 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, · 18 unmodified lines
2771}2060}
2772 2061
2773/*2062/*
2774 * select a tree root for relocation. return NULL if the block2063 * Select a tree root for relocation.
2775 * is reference counted. we should use do_relocation() in this2064 *
2776 * case. return a tree root pointer if the block isn't reference2065 * Return NULL if the block is not shareable. We should use do_relocation() in
2777 * counted. return -ENOENT if the block is root of reloc tree.2066 * this case.
2067 *
2068 * Return a tree root pointer if the block is shareable.
2069 * Return -ENOENT if the block is root of reloc tree.
2778 */2070 */
2779static noinline_for_stack2071static noinline_for_stack
2780struct btrfs_root *select_one_root(struct backref_node *node)2072struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
2781{2073{
2782 struct backref_node *next;2074 struct btrfs_backref_node *next;
2783 struct btrfs_root *root;2075 struct btrfs_root *root;
2784 struct btrfs_root *fs_root = NULL;2076 struct btrfs_root *fs_root = NULL;
2785 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];2077 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2786 int index = 0;2078 int index = 0;
2787 2079
2788 next = node;2080 next = node;
@@ -2792,8 +2084,8 @@ struct btrfs_root *select_one_root(struct backref_node *node) · 3 unmodified lines
2792 root = next->root;2084 root = next->root;
2793 BUG_ON(!root);2085 BUG_ON(!root);
2794 2086
2795 /* no other choice for non-references counted tree */2087 /* No other choice for non-shareable tree */
2796 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))2088 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2797 return root;2089 return root;
2798 2090
2799 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)2091 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
@@ -2814,12 +2106,12 @@ struct btrfs_root *select_one_root(struct backref_node *node) · 14 unmodified lines
2814 2106
2815static noinline_for_stack2107static noinline_for_stack
2816u64 calcu_metadata_size(struct reloc_control *rc,2108u64 calcu_metadata_size(struct reloc_control *rc,
2817 struct backref_node *node, int reserve)2109 struct btrfs_backref_node *node, int reserve)
2818{2110{
2819 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;2111 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2820 struct backref_node *next = node;2112 struct btrfs_backref_node *next = node;
2821 struct backref_edge *edge;2113 struct btrfs_backref_edge *edge;
2822 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];2114 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2823 u64 num_bytes = 0;2115 u64 num_bytes = 0;
2824 int index = 0;2116 int index = 0;
2825 2117
@@ -2837,7 +2129,7 @@ u64 calcu_metadata_size(struct reloc_control *rc, · 11 unmodified lines
2837 break;2129 break;
2838 2130
2839 edge = list_entry(next->upper.next,2131 edge = list_entry(next->upper.next,
2840 struct backref_edge, list[LOWER]);2132 struct btrfs_backref_edge, list[LOWER]);
2841 edges[index++] = edge;2133 edges[index++] = edge;
2842 next = edge->node[UPPER];2134 next = edge->node[UPPER];
2843 }2135 }
@@ -2848,7 +2140,7 @@ u64 calcu_metadata_size(struct reloc_control *rc, · 4 unmodified lines
2848 2140
2849static int reserve_metadata_space(struct btrfs_trans_handle *trans,2141static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2850 struct reloc_control *rc,2142 struct reloc_control *rc,
2851 struct backref_node *node)2143 struct btrfs_backref_node *node)
2852{2144{
2853 struct btrfs_root *root = rc->extent_root;2145 struct btrfs_root *root = rc->extent_root;
2854 struct btrfs_fs_info *fs_info = root->fs_info;2146 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -2896,14 +2188,14 @@ static int reserve_metadata_space(struct btrfs_trans_handle *trans, · 41 unmodified lines
2896 */2188 */
2897static int do_relocation(struct btrfs_trans_handle *trans,2189static int do_relocation(struct btrfs_trans_handle *trans,
2898 struct reloc_control *rc,2190 struct reloc_control *rc,
2899 struct backref_node *node,2191 struct btrfs_backref_node *node,
2900 struct btrfs_key *key,2192 struct btrfs_key *key,
2901 struct btrfs_path *path, int lowest)2193 struct btrfs_path *path, int lowest)
2902{2194{
2903 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;2195 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2904 struct backref_node *upper;2196 struct btrfs_backref_node *upper;
2905 struct backref_edge *edge;2197 struct btrfs_backref_edge *edge;
2906 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];2198 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2907 struct btrfs_root *root;2199 struct btrfs_root *root;
2908 struct extent_buffer *eb;2200 struct extent_buffer *eb;
2909 u32 blocksize;2201 u32 blocksize;
@@ -2929,8 +2221,7 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 19 unmodified lines
2929 2221
2930 if (upper->eb && !upper->locked) {2222 if (upper->eb && !upper->locked) {
2931 if (!lowest) {2223 if (!lowest) {
2932 ret = btrfs_bin_search(upper->eb, key,2224 ret = btrfs_bin_search(upper->eb, key, &slot);
2933 upper->level, &slot);
2934 if (ret < 0) {2225 if (ret < 0) {
2935 err = ret;2226 err = ret;
2936 goto next;2227 goto next;
@@ -2940,7 +2231,7 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 3 unmodified lines
2940 if (node->eb->start == bytenr)2231 if (node->eb->start == bytenr)
2941 goto next;2232 goto next;
2942 }2233 }
2943 drop_node_buffer(upper);2234 btrfs_backref_drop_node_buffer(upper);
2944 }2235 }
2945 2236
2946 if (!upper->eb) {2237 if (!upper->eb) {
@@ -2968,8 +2259,7 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 21 unmodified lines
2968 slot = path->slots[upper->level];2259 slot = path->slots[upper->level];
2969 btrfs_release_path(path);2260 btrfs_release_path(path);
2970 } else {2261 } else {
2971 ret = btrfs_bin_search(upper->eb, key, upper->level,2262 ret = btrfs_bin_search(upper->eb, key, &slot);
2972 &slot);
2973 if (ret < 0) {2263 if (ret < 0) {
2974 err = ret;2264 err = ret;
2975 goto next;2265 goto next;
@@ -3039,15 +2329,15 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 63 unmodified lines
3039 }2329 }
3040next:2330next:
3041 if (!upper->pending)2331 if (!upper->pending)
3042 drop_node_buffer(upper);2332 btrfs_backref_drop_node_buffer(upper);
3043 else2333 else
3044 unlock_node_buffer(upper);2334 btrfs_backref_unlock_node_buffer(upper);
3045 if (err)2335 if (err)
3046 break;2336 break;
3047 }2337 }
3048 2338
3049 if (!err && node->pending) {2339 if (!err && node->pending) {
3050 drop_node_buffer(node);2340 btrfs_backref_drop_node_buffer(node);
3051 list_move_tail(&node->list, &rc->backref_cache.changed);2341 list_move_tail(&node->list, &rc->backref_cache.changed);
3052 node->pending = 0;2342 node->pending = 0;
3053 }2343 }
@@ -3059,7 +2349,7 @@ next: · 5 unmodified lines
3059 2349
3060static int link_to_upper(struct btrfs_trans_handle *trans,2350static int link_to_upper(struct btrfs_trans_handle *trans,
3061 struct reloc_control *rc,2351 struct reloc_control *rc,
3062 struct backref_node *node,2352 struct btrfs_backref_node *node,
3063 struct btrfs_path *path)2353 struct btrfs_path *path)
3064{2354{
3065 struct btrfs_key key;2355 struct btrfs_key key;
@@ -3073,15 +2363,15 @@ static int finish_pending_nodes(struct btrfs_trans_handle *trans, · 7 unmodified lines
3073 struct btrfs_path *path, int err)2363 struct btrfs_path *path, int err)
3074{2364{
3075 LIST_HEAD(list);2365 LIST_HEAD(list);
3076 struct backref_cache *cache = &rc->backref_cache;2366 struct btrfs_backref_cache *cache = &rc->backref_cache;
3077 struct backref_node *node;2367 struct btrfs_backref_node *node;
3078 int level;2368 int level;
3079 int ret;2369 int ret;
3080 2370
3081 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {2371 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
3082 while (!list_empty(&cache->pending[level])) {2372 while (!list_empty(&cache->pending[level])) {
3083 node = list_entry(cache->pending[level].next,2373 node = list_entry(cache->pending[level].next,
3084 struct backref_node, list);2374 struct btrfs_backref_node, list);
3085 list_move_tail(&node->list, &list);2375 list_move_tail(&node->list, &list);
3086 BUG_ON(!node->pending);2376 BUG_ON(!node->pending);
3087 2377
@@ -3096,35 +2386,16 @@ static int finish_pending_nodes(struct btrfs_trans_handle *trans, · 8 unmodified lines
3096 return err;2386 return err;
3097}2387}
3098 2388
3099static void mark_block_processed(struct reloc_control *rc,
3100 u64 bytenr, u32 blocksize)
3101{
3102 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
3103 EXTENT_DIRTY);
3104}
3105
3106static void __mark_block_processed(struct reloc_control *rc,
3107 struct backref_node *node)
3108{
3109 u32 blocksize;
3110 if (node->level == 0 ||
3111 in_block_group(node->bytenr, rc->block_group)) {
3112 blocksize = rc->extent_root->fs_info->nodesize;
3113 mark_block_processed(rc, node->bytenr, blocksize);
3114 }
3115 node->processed = 1;
3116}
3117
3118/*2389/*
3119 * mark a block and all blocks directly/indirectly reference the block2390 * mark a block and all blocks directly/indirectly reference the block
3120 * as processed.2391 * as processed.
3121 */2392 */
3122static void update_processed_blocks(struct reloc_control *rc,2393static void update_processed_blocks(struct reloc_control *rc,
3123 struct backref_node *node)2394 struct btrfs_backref_node *node)
3124{2395{
3125 struct backref_node *next = node;2396 struct btrfs_backref_node *next = node;
3126 struct backref_edge *edge;2397 struct btrfs_backref_edge *edge;
3127 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];2398 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
3128 int index = 0;2399 int index = 0;
3129 2400
3130 while (next) {2401 while (next) {
@@ -3133,13 +2404,13 @@ static void update_processed_blocks(struct reloc_control *rc, · 2 unmodified lines
3133 if (next->processed)2404 if (next->processed)
3134 break;2405 break;
3135 2406
3136 __mark_block_processed(rc, next);2407 mark_block_processed(rc, next);
3137 2408
3138 if (list_empty(&next->upper))2409 if (list_empty(&next->upper))
3139 break;2410 break;
3140 2411
3141 edge = list_entry(next->upper.next,2412 edge = list_entry(next->upper.next,
3142 struct backref_edge, list[LOWER]);2413 struct btrfs_backref_edge, list[LOWER]);
3143 edges[index++] = edge;2414 edges[index++] = edge;
3144 next = edge->node[UPPER];2415 next = edge->node[UPPER];
3145 }2416 }
@@ -3184,7 +2455,7 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info, · 38 unmodified lines
3184 */2455 */
3185static int relocate_tree_block(struct btrfs_trans_handle *trans,2456static int relocate_tree_block(struct btrfs_trans_handle *trans,
3186 struct reloc_control *rc,2457 struct reloc_control *rc,
3187 struct backref_node *node,2458 struct btrfs_backref_node *node,
3188 struct btrfs_key *key,2459 struct btrfs_key *key,
3189 struct btrfs_path *path)2460 struct btrfs_path *path)
3190{2461{
@@ -3210,7 +2481,7 @@ static int relocate_tree_block(struct btrfs_trans_handle *trans, · 19 unmodified lines
3210 }2481 }
3211 2482
3212 if (root) {2483 if (root) {
3213 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {2484 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
3214 BUG_ON(node->new_bytenr);2485 BUG_ON(node->new_bytenr);
3215 BUG_ON(!list_empty(&node->list));2486 BUG_ON(!list_empty(&node->list));
3216 btrfs_record_root_in_trans(trans, root);2487 btrfs_record_root_in_trans(trans, root);
@@ -3234,7 +2505,7 @@ static int relocate_tree_block(struct btrfs_trans_handle *trans, · 17 unmodified lines
3234 }2505 }
3235out:2506out:
3236 if (ret || node->level == 0 || node->cowonly)2507 if (ret || node->level == 0 || node->cowonly)
3237 remove_backref_node(&rc->backref_cache, node);2508 btrfs_backref_cleanup_node(&rc->backref_cache, node);
3238 return ret;2509 return ret;
3239}2510}
3240 2511
@@ -3246,7 +2517,7 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, · 5 unmodified lines
3246 struct reloc_control *rc, struct rb_root *blocks)2517 struct reloc_control *rc, struct rb_root *blocks)
3247{2518{
3248 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;2519 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3249 struct backref_node *node;2520 struct btrfs_backref_node *node;
3250 struct btrfs_path *path;2521 struct btrfs_path *path;
3251 struct tree_block *block;2522 struct tree_block *block;
3252 struct tree_block *next;2523 struct tree_block *next;
@@ -3613,9 +2884,10 @@ static int add_tree_block(struct reloc_control *rc, · 360 unmodified lines
3613 block->level = level;2884 block->level = level;
3614 block->key_ready = 0;2885 block->key_ready = 0;
3615 2886
3616 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);2887 rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
3617 if (rb_node)2888 if (rb_node)
3618 backref_tree_panic(rb_node, -EEXIST, block->bytenr);2889 btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
2890 -EEXIST);
3619 2891
3620 return 0;2892 return 0;
3621}2893}
@@ -3636,7 +2908,7 @@ static int __add_tree_block(struct reloc_control *rc, · 14 unmodified lines
3636 if (tree_block_processed(bytenr, rc))2908 if (tree_block_processed(bytenr, rc))
3637 return 0;2909 return 0;
3638 2910
3639 if (tree_search(blocks, bytenr))2911 if (rb_simple_search(blocks, bytenr))
3640 return 0;2912 return 0;
3641 2913
3642 path = btrfs_alloc_path();2914 path = btrfs_alloc_path();
@@ -3698,7 +2970,6 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info, · 55 unmodified lines
3698 struct inode *inode,2970 struct inode *inode,
3699 u64 ino)2971 u64 ino)
3700{2972{
3701 struct btrfs_key key;
3702 struct btrfs_root *root = fs_info->tree_root;2973 struct btrfs_root *root = fs_info->tree_root;
3703 struct btrfs_trans_handle *trans;2974 struct btrfs_trans_handle *trans;
3704 int ret = 0;2975 int ret = 0;
@@ -3706,11 +2977,7 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info, · 1 unmodified line
3706 if (inode)2977 if (inode)
3707 goto truncate;2978 goto truncate;
3708 2979
3709 key.objectid = ino;2980 inode = btrfs_iget(fs_info->sb, ino, root);
3710 key.type = BTRFS_INODE_ITEM_KEY;
3711 key.offset = 0;
3712
3713 inode = btrfs_iget(fs_info->sb, &key, root);
3714 if (IS_ERR(inode))2981 if (IS_ERR(inode))
3715 return -ENOENT;2982 return -ENOENT;
3716 2983
@@ -4122,7 +3389,7 @@ restart: · 405 unmodified lines
4122 rc->create_reloc_tree = 0;3389 rc->create_reloc_tree = 0;
4123 set_reloc_control(rc);3390 set_reloc_control(rc);
4124 3391
4125 backref_cache_cleanup(&rc->backref_cache);3392 btrfs_backref_release_cache(&rc->backref_cache);
4126 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);3393 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
4127 3394
4128 /*3395 /*
@@ -4198,14 +3465,10 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, · 69 unmodified lines
4198 struct inode *inode = NULL;3465 struct inode *inode = NULL;
4199 struct btrfs_trans_handle *trans;3466 struct btrfs_trans_handle *trans;
4200 struct btrfs_root *root;3467 struct btrfs_root *root;
4201 struct btrfs_key key;
4202 u64 objectid;3468 u64 objectid;
4203 int err = 0;3469 int err = 0;
4204 3470
4205 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);3471 root = btrfs_grab_root(fs_info->data_reloc_root);
4206 if (IS_ERR(root))
4207 return ERR_CAST(root);
4208
4209 trans = btrfs_start_transaction(root, 6);3472 trans = btrfs_start_transaction(root, 6);
4210 if (IS_ERR(trans)) {3473 if (IS_ERR(trans)) {
4211 btrfs_put_root(root);3474 btrfs_put_root(root);
@@ -4219,10 +3482,7 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, · 7 unmodified lines
4219 err = __insert_orphan_inode(trans, root, objectid);3482 err = __insert_orphan_inode(trans, root, objectid);
4220 BUG_ON(err);3483 BUG_ON(err);
4221 3484
4222 key.objectid = objectid;3485 inode = btrfs_iget(fs_info->sb, objectid, root);
4223 key.type = BTRFS_INODE_ITEM_KEY;
4224 key.offset = 0;
4225 inode = btrfs_iget(fs_info->sb, &key, root);
4226 BUG_ON(IS_ERR(inode));3486 BUG_ON(IS_ERR(inode));
4227 BTRFS_I(inode)->index_cnt = group->start;3487 BTRFS_I(inode)->index_cnt = group->start;
4228 3488
@@ -4249,7 +3509,7 @@ static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) · 20 unmodified lines
4249 3509
4250 INIT_LIST_HEAD(&rc->reloc_roots);3510 INIT_LIST_HEAD(&rc->reloc_roots);
4251 INIT_LIST_HEAD(&rc->dirty_subvol_roots);3511 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
4252 backref_cache_init(&rc->backref_cache);3512 btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
4253 mapping_tree_init(&rc->reloc_root_tree);3513 mapping_tree_init(&rc->reloc_root_tree);
4254 extent_io_tree_init(fs_info, &rc->processed_blocks,3514 extent_io_tree_init(fs_info, &rc->processed_blocks,
4255 IO_TREE_RELOC_BLOCKS, NULL);3515 IO_TREE_RELOC_BLOCKS, NULL);
@@ -4494,12 +3754,12 @@ int btrfs_recover_relocation(struct btrfs_root *root) · 238 unmodified lines
4494 goto out;3754 goto out;
4495 }3755 }
4496 3756
4497 set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);3757 set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
4498 list_add(&reloc_root->root_list, &reloc_roots);3758 list_add(&reloc_root->root_list, &reloc_roots);
4499 3759
4500 if (btrfs_root_refs(&reloc_root->root_item) > 0) {3760 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4501 fs_root = read_fs_root(fs_info,3761 fs_root = btrfs_get_fs_root(fs_info,
4502 reloc_root->root_key.offset);3762 reloc_root->root_key.offset, false);
4503 if (IS_ERR(fs_root)) {3763 if (IS_ERR(fs_root)) {
4504 ret = PTR_ERR(fs_root);3764 ret = PTR_ERR(fs_root);
4505 if (ret != -ENOENT) {3765 if (ret != -ENOENT) {
@@ -4555,7 +3815,8 @@ int btrfs_recover_relocation(struct btrfs_root *root) · 49 unmodified lines
4555 continue;3815 continue;
4556 }3816 }
4557 3817
4558 fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);3818 fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
3819 false);
4559 if (IS_ERR(fs_root)) {3820 if (IS_ERR(fs_root)) {
4560 err = PTR_ERR(fs_root);3821 err = PTR_ERR(fs_root);
4561 list_add_tail(&reloc_root->root_list, &reloc_roots);3822 list_add_tail(&reloc_root->root_list, &reloc_roots);
@@ -4591,20 +3852,16 @@ out_unset: · 29 unmodified lines
4591 unset_reloc_control(rc);3852 unset_reloc_control(rc);
4592 free_reloc_control(rc);3853 free_reloc_control(rc);
4593out:3854out:
4594 if (!list_empty(&reloc_roots))3855 free_reloc_roots(&reloc_roots);
4595 free_reloc_roots(&reloc_roots);
4596 3856
4597 btrfs_free_path(path);3857 btrfs_free_path(path);
4598 3858
4599 if (err == 0) {3859 if (err == 0) {
4600 /* cleanup orphan inode in data relocation tree */3860 /* cleanup orphan inode in data relocation tree */
4601 fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);3861 fs_root = btrfs_grab_root(fs_info->data_reloc_root);
4602 if (IS_ERR(fs_root)) {3862 ASSERT(fs_root);
4603 err = PTR_ERR(fs_root);3863 err = btrfs_orphan_cleanup(fs_root);
4604 } else {3864 btrfs_put_root(fs_root);
4605 err = btrfs_orphan_cleanup(fs_root);
4606 btrfs_put_root(fs_root);
4607 }
4608 }3865 }
4609 return err;3866 return err;
4610}3867}
@@ -4666,7 +3923,7 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, · 55 unmodified lines
4666{3923{
4667 struct btrfs_fs_info *fs_info = root->fs_info;3924 struct btrfs_fs_info *fs_info = root->fs_info;
4668 struct reloc_control *rc;3925 struct reloc_control *rc;
4669 struct backref_node *node;3926 struct btrfs_backref_node *node;
4670 int first_cow = 0;3927 int first_cow = 0;
4671 int level;3928 int level;
4672 int ret = 0;3929 int ret = 0;
@@ -4691,7 +3948,7 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, · 18 unmodified lines
4691 BUG_ON(node->bytenr != buf->start &&3948 BUG_ON(node->bytenr != buf->start &&
4692 node->new_bytenr != buf->start);3949 node->new_bytenr != buf->start);
4693 3950
4694 drop_node_buffer(node);3951 btrfs_backref_drop_node_buffer(node);
4695 atomic_inc(&cow->refs);3952 atomic_inc(&cow->refs);
4696 node->eb = cow;3953 node->eb = cow;
4697 node->new_bytenr = cow->start;3954 node->new_bytenr = cow->start;
@@ -4703,7 +3960,7 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, · 5 unmodified lines
4703 }3960 }
4704 3961
4705 if (first_cow)3962 if (first_cow)
4706 __mark_block_processed(rc, node);3963 mark_block_processed(rc, node);
4707 3964
4708 if (first_cow && level > 0)3965 if (first_cow && level > 0)
4709 rc->nodes_relocated += buf->len;3966 rc->nodes_relocated += buf->len;
fs/btrfs/root-tree.c+5−7
@@ -210,7 +210,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · 209 unmodified lines
210 struct extent_buffer *leaf;210 struct extent_buffer *leaf;
211 struct btrfs_path *path;211 struct btrfs_path *path;
212 struct btrfs_key key;212 struct btrfs_key key;
213 struct btrfs_key root_key;
214 struct btrfs_root *root;213 struct btrfs_root *root;
215 int err = 0;214 int err = 0;
216 int ret;215 int ret;
@@ -223,10 +222,9 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · 6 unmodified lines
223 key.type = BTRFS_ORPHAN_ITEM_KEY;222 key.type = BTRFS_ORPHAN_ITEM_KEY;
224 key.offset = 0;223 key.offset = 0;
225 224
226 root_key.type = BTRFS_ROOT_ITEM_KEY;
227 root_key.offset = (u64)-1;
228
229 while (1) {225 while (1) {
226 u64 root_objectid;
227
230 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);228 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
231 if (ret < 0) {229 if (ret < 0) {
232 err = ret;230 err = ret;
@@ -250,10 +248,10 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · 17 unmodified lines
250 key.type != BTRFS_ORPHAN_ITEM_KEY)248 key.type != BTRFS_ORPHAN_ITEM_KEY)
251 break;249 break;
252 250
253 root_key.objectid = key.offset;251 root_objectid = key.offset;
254 key.offset++;252 key.offset++;
255 253
256 root = btrfs_get_fs_root(fs_info, &root_key, false);254 root = btrfs_get_fs_root(fs_info, root_objectid, false);
257 err = PTR_ERR_OR_ZERO(root);255 err = PTR_ERR_OR_ZERO(root);
258 if (err && err != -ENOENT) {256 if (err && err != -ENOENT) {
259 break;257 break;
@@ -270,7 +268,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · 10 unmodified lines
270 break;268 break;
271 }269 }
272 err = btrfs_del_orphan_item(trans, tree_root,270 err = btrfs_del_orphan_item(trans, tree_root,
273 root_key.objectid);271 root_objectid);
274 btrfs_end_transaction(trans);272 btrfs_end_transaction(trans);
275 if (err) {273 if (err) {
276 btrfs_handle_fs_error(fs_info, err,274 btrfs_handle_fs_error(fs_info, err,
fs/btrfs/scrub.c+45−14
@@ -647,13 +647,9 @@ static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, · 646 unmodified lines
647 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;647 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
648 struct inode_fs_paths *ipath = NULL;648 struct inode_fs_paths *ipath = NULL;
649 struct btrfs_root *local_root;649 struct btrfs_root *local_root;
650 struct btrfs_key root_key;
651 struct btrfs_key key;650 struct btrfs_key key;
652 651
653 root_key.objectid = root;652 local_root = btrfs_get_fs_root(fs_info, root, true);
654 root_key.type = BTRFS_ROOT_ITEM_KEY;
655 root_key.offset = (u64)-1;
656 local_root = btrfs_get_fs_root(fs_info, &root_key, true);
657 if (IS_ERR(local_root)) {653 if (IS_ERR(local_root)) {
658 ret = PTR_ERR(local_root);654 ret = PTR_ERR(local_root);
659 goto err;655 goto err;
@@ -3046,7 +3042,8 @@ out: · 2386 unmodified lines
3046static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,3042static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
3047 struct map_lookup *map,3043 struct map_lookup *map,
3048 struct btrfs_device *scrub_dev,3044 struct btrfs_device *scrub_dev,
3049 int num, u64 base, u64 length)3045 int num, u64 base, u64 length,
3046 struct btrfs_block_group *cache)
3050{3047{
3051 struct btrfs_path *path, *ppath;3048 struct btrfs_path *path, *ppath;
3052 struct btrfs_fs_info *fs_info = sctx->fs_info;3049 struct btrfs_fs_info *fs_info = sctx->fs_info;
@@ -3284,6 +3281,20 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, · 231 unmodified lines
3284 break;3281 break;
3285 }3282 }
3286 3283
3284 /*
3285 * If our block group was removed in the meanwhile, just
3286 * stop scrubbing since there is no point in continuing.
3287 * Continuing would prevent reusing its device extents
3288 * for new block groups for a long time.
3289 */
3290 spin_lock(&cache->lock);
3291 if (cache->removed) {
3292 spin_unlock(&cache->lock);
3293 ret = 0;
3294 goto out;
3295 }
3296 spin_unlock(&cache->lock);
3297
3287 extent = btrfs_item_ptr(l, slot,3298 extent = btrfs_item_ptr(l, slot,
3288 struct btrfs_extent_item);3299 struct btrfs_extent_item);
3289 flags = btrfs_extent_flags(l, extent);3300 flags = btrfs_extent_flags(l, extent);
@@ -3328,13 +3339,14 @@ again: · 38 unmodified lines
3328 &extent_dev,3339 &extent_dev,
3329 &extent_mirror_num);3340 &extent_mirror_num);
3330 3341
3331 ret = btrfs_lookup_csums_range(csum_root,3342 if (flags & BTRFS_EXTENT_FLAG_DATA) {
3332 extent_logical,3343 ret = btrfs_lookup_csums_range(csum_root,
3333 extent_logical +3344 extent_logical,
3334 extent_len - 1,3345 extent_logical + extent_len - 1,
3335 &sctx->csum_list, 1);3346 &sctx->csum_list, 1);
3336 if (ret)3347 if (ret)
3337 goto out;3348 goto out;
3349 }
3338 3350
3339 ret = scrub_extent(sctx, map, extent_logical, extent_len,3351 ret = scrub_extent(sctx, map, extent_logical, extent_len,
3340 extent_physical, extent_dev, flags,3352 extent_physical, extent_dev, flags,
@@ -3457,7 +3469,7 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, · 116 unmodified lines
3457 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&3469 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
3458 map->stripes[i].physical == dev_offset) {3470 map->stripes[i].physical == dev_offset) {
3459 ret = scrub_stripe(sctx, map, scrub_dev, i,3471 ret = scrub_stripe(sctx, map, scrub_dev, i,
3460 chunk_offset, length);3472 chunk_offset, length, cache);
3461 if (ret)3473 if (ret)
3462 goto out;3474 goto out;
3463 }3475 }
@@ -3554,6 +3566,23 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx, · 90 unmodified lines
3554 if (!cache)3566 if (!cache)
3555 goto skip;3567 goto skip;
3556 3568
3569 /*
3570 * Make sure that while we are scrubbing the corresponding block
3571 * group doesn't get its logical address and its device extents
3572 * reused for another block group, which can possibly be of a
3573 * different type and different profile. We do this to prevent
3574 * false error detections and crashes due to bogus attempts to
3575 * repair extents.
3576 */
3577 spin_lock(&cache->lock);
3578 if (cache->removed) {
3579 spin_unlock(&cache->lock);
3580 btrfs_put_block_group(cache);
3581 goto skip;
3582 }
3583 btrfs_freeze_block_group(cache);
3584 spin_unlock(&cache->lock);
3585
3557 /*3586 /*
3558 * we need call btrfs_inc_block_group_ro() with scrubs_paused,3587 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3559 * to avoid deadlock caused by:3588 * to avoid deadlock caused by:
@@ -3609,6 +3638,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx, · 49 unmodified lines
3609 } else {3638 } else {
3610 btrfs_warn(fs_info,3639 btrfs_warn(fs_info,
3611 "failed setting block group ro: %d", ret);3640 "failed setting block group ro: %d", ret);
3641 btrfs_unfreeze_block_group(cache);
3612 btrfs_put_block_group(cache);3642 btrfs_put_block_group(cache);
3613 scrub_pause_off(fs_info);3643 scrub_pause_off(fs_info);
3614 break;3644 break;
@@ -3695,6 +3725,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx, · 80 unmodified lines
3695 spin_unlock(&cache->lock);3725 spin_unlock(&cache->lock);
3696 }3726 }
3697 3727
3728 btrfs_unfreeze_block_group(cache);
3698 btrfs_put_block_group(cache);3729 btrfs_put_block_group(cache);
3699 if (ret)3730 if (ret)
3700 break;3731 break;
fs/btrfs/send.c+72−17
@@ -23,6 +23,7 @@ · 22 unmodified lines
23#include "btrfs_inode.h"23#include "btrfs_inode.h"
24#include "transaction.h"24#include "transaction.h"
25#include "compression.h"25#include "compression.h"
26#include "xattr.h"
26 27
27/*28/*
28 * Maximum number of references an extent can have in order for us to attempt to29 * Maximum number of references an extent can have in order for us to attempt to
@@ -4545,6 +4546,10 @@ static int __process_new_xattr(int num, struct btrfs_key *di_key, · 4516 unmodified lines
4545 struct fs_path *p;4546 struct fs_path *p;
4546 struct posix_acl_xattr_header dummy_acl;4547 struct posix_acl_xattr_header dummy_acl;
4547 4548
4549 /* Capabilities are emitted by finish_inode_if_needed */
4550 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4551 return 0;
4552
4548 p = fs_path_alloc();4553 p = fs_path_alloc();
4549 if (!p)4554 if (!p)
4550 return -ENOMEM;4555 return -ENOMEM;
@@ -4801,17 +4806,12 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len) · 250 unmodified lines
4801 struct inode *inode;4806 struct inode *inode;
4802 struct page *page;4807 struct page *page;
4803 char *addr;4808 char *addr;
4804 struct btrfs_key key;
4805 pgoff_t index = offset >> PAGE_SHIFT;4809 pgoff_t index = offset >> PAGE_SHIFT;
4806 pgoff_t last_index;4810 pgoff_t last_index;
4807 unsigned pg_offset = offset_in_page(offset);4811 unsigned pg_offset = offset_in_page(offset);
4808 ssize_t ret = 0;4812 ssize_t ret = 0;
4809 4813
4810 key.objectid = sctx->cur_ino;4814 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
4811 key.type = BTRFS_INODE_ITEM_KEY;
4812 key.offset = 0;
4813
4814 inode = btrfs_iget(fs_info->sb, &key, root);
4815 if (IS_ERR(inode))4815 if (IS_ERR(inode))
4816 return PTR_ERR(inode);4816 return PTR_ERR(inode);
4817 4817
@@ -5107,6 +5107,64 @@ static int send_extent_data(struct send_ctx *sctx, · 289 unmodified lines
5107 return 0;5107 return 0;
5108}5108}
5109 5109
5110/*
5111 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5112 * found, call send_set_xattr function to emit it.
5113 *
5114 * Return 0 if there isn't a capability, or when the capability was emitted
5115 * successfully, or < 0 if an error occurred.
5116 */
5117static int send_capabilities(struct send_ctx *sctx)
5118{
5119 struct fs_path *fspath = NULL;
5120 struct btrfs_path *path;
5121 struct btrfs_dir_item *di;
5122 struct extent_buffer *leaf;
5123 unsigned long data_ptr;
5124 char *buf = NULL;
5125 int buf_len;
5126 int ret = 0;
5127
5128 path = alloc_path_for_send();
5129 if (!path)
5130 return -ENOMEM;
5131
5132 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5133 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5134 if (!di) {
5135 /* There is no xattr for this inode */
5136 goto out;
5137 } else if (IS_ERR(di)) {
5138 ret = PTR_ERR(di);
5139 goto out;
5140 }
5141
5142 leaf = path->nodes[0];
5143 buf_len = btrfs_dir_data_len(leaf, di);
5144
5145 fspath = fs_path_alloc();
5146 buf = kmalloc(buf_len, GFP_KERNEL);
5147 if (!fspath || !buf) {
5148 ret = -ENOMEM;
5149 goto out;
5150 }
5151
5152 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5153 if (ret < 0)
5154 goto out;
5155
5156 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5157 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5158
5159 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5160 strlen(XATTR_NAME_CAPS), buf, buf_len);
5161out:
5162 kfree(buf);
5163 fs_path_free(fspath);
5164 btrfs_free_path(path);
5165 return ret;
5166}
5167
5110static int clone_range(struct send_ctx *sctx,5168static int clone_range(struct send_ctx *sctx,
5111 struct clone_root *clone_root,5169 struct clone_root *clone_root,
5112 const u64 disk_byte,5170 const u64 disk_byte,
@@ -5972,6 +6030,10 @@ static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) · 859 unmodified lines
5972 goto out;6030 goto out;
5973 }6031 }
5974 6032
6033 ret = send_capabilities(sctx);
6034 if (ret < 0)
6035 goto out;
6036
5975 /*6037 /*
5976 * If other directory inodes depended on our current directory6038 * If other directory inodes depended on our current directory
5977 * inode's move/rename, now do their move/rename operations.6039 * inode's move/rename, now do their move/rename operations.
@@ -7021,7 +7083,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg) · 1043 unmodified lines
7021 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;7083 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7022 struct btrfs_fs_info *fs_info = send_root->fs_info;7084 struct btrfs_fs_info *fs_info = send_root->fs_info;
7023 struct btrfs_root *clone_root;7085 struct btrfs_root *clone_root;
7024 struct btrfs_key key;
7025 struct send_ctx *sctx = NULL;7086 struct send_ctx *sctx = NULL;
7026 u32 i;7087 u32 i;
7027 u64 *clone_sources_tmp = NULL;7088 u64 *clone_sources_tmp = NULL;
@@ -7143,11 +7204,8 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg) · 115 unmodified lines
7143 }7204 }
7144 7205
7145 for (i = 0; i < arg->clone_sources_count; i++) {7206 for (i = 0; i < arg->clone_sources_count; i++) {
7146 key.objectid = clone_sources_tmp[i];7207 clone_root = btrfs_get_fs_root(fs_info,
7147 key.type = BTRFS_ROOT_ITEM_KEY;7208 clone_sources_tmp[i], true);
7148 key.offset = (u64)-1;
7149
7150 clone_root = btrfs_get_fs_root(fs_info, &key, true);
7151 if (IS_ERR(clone_root)) {7209 if (IS_ERR(clone_root)) {
7152 ret = PTR_ERR(clone_root);7210 ret = PTR_ERR(clone_root);
7153 goto out;7211 goto out;
@@ -7178,11 +7236,8 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg) · 24 unmodified lines
7178 }7236 }
7179 7237
7180 if (arg->parent_root) {7238 if (arg->parent_root) {
7181 key.objectid = arg->parent_root;7239 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7182 key.type = BTRFS_ROOT_ITEM_KEY;7240 true);
7183 key.offset = (u64)-1;
7184
7185 sctx->parent_root = btrfs_get_fs_root(fs_info, &key, true);
7186 if (IS_ERR(sctx->parent_root)) {7241 if (IS_ERR(sctx->parent_root)) {
7187 ret = PTR_ERR(sctx->parent_root);7242 ret = PTR_ERR(sctx->parent_root);
7188 goto out;7243 goto out;
fs/btrfs/space-info.c+73−8
@@ -626,6 +626,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info, · 625 unmodified lines
626 struct reserve_ticket *ticket = NULL;626 struct reserve_ticket *ticket = NULL;
627 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;627 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
628 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;628 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
629 struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
629 struct btrfs_trans_handle *trans;630 struct btrfs_trans_handle *trans;
630 u64 bytes_needed;631 u64 bytes_needed;
631 u64 reclaim_bytes = 0;632 u64 reclaim_bytes = 0;
@@ -688,6 +689,11 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info, · 56 unmodified lines
688 spin_lock(&delayed_refs_rsv->lock);689 spin_lock(&delayed_refs_rsv->lock);
689 reclaim_bytes += delayed_refs_rsv->reserved;690 reclaim_bytes += delayed_refs_rsv->reserved;
690 spin_unlock(&delayed_refs_rsv->lock);691 spin_unlock(&delayed_refs_rsv->lock);
692
693 spin_lock(&trans_rsv->lock);
694 reclaim_bytes += trans_rsv->reserved;
695 spin_unlock(&trans_rsv->lock);
696
691 if (reclaim_bytes >= bytes_needed)697 if (reclaim_bytes >= bytes_needed)
692 goto commit;698 goto commit;
693 bytes_needed -= reclaim_bytes;699 bytes_needed -= reclaim_bytes;
@@ -856,6 +862,34 @@ static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info, · 162 unmodified lines
856 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));862 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
857}863}
858 864
865static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info,
866 struct btrfs_space_info *space_info,
867 struct reserve_ticket *ticket)
868{
869 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
870 u64 min_bytes;
871
872 if (global_rsv->space_info != space_info)
873 return false;
874
875 spin_lock(&global_rsv->lock);
876 min_bytes = div_factor(global_rsv->size, 1);
877 if (global_rsv->reserved < min_bytes + ticket->bytes) {
878 spin_unlock(&global_rsv->lock);
879 return false;
880 }
881 global_rsv->reserved -= ticket->bytes;
882 ticket->bytes = 0;
883 list_del_init(&ticket->list);
884 wake_up(&ticket->wait);
885 space_info->tickets_id++;
886 if (global_rsv->reserved < global_rsv->size)
887 global_rsv->full = 0;
888 spin_unlock(&global_rsv->lock);
889
890 return true;
891}
892
859/*893/*
860 * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets894 * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets
861 * @fs_info - fs_info for this fs895 * @fs_info - fs_info for this fs
@@ -888,6 +922,10 @@ static bool maybe_fail_all_tickets(struct btrfs_fs_info *fs_info, · 26 unmodified lines
888 ticket = list_first_entry(&space_info->tickets,922 ticket = list_first_entry(&space_info->tickets,
889 struct reserve_ticket, list);923 struct reserve_ticket, list);
890 924
925 if (ticket->steal &&
926 steal_from_global_rsv(fs_info, space_info, ticket))
927 return true;
928
891 /*929 /*
892 * may_commit_transaction will avoid committing the transaction930 * may_commit_transaction will avoid committing the transaction
893 * if it doesn't feel like the space reclaimed by the commit931 * if it doesn't feel like the space reclaimed by the commit
@@ -1104,6 +1142,7 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info, · 210 unmodified lines
1104 1142
1105 switch (flush) {1143 switch (flush) {
1106 case BTRFS_RESERVE_FLUSH_ALL:1144 case BTRFS_RESERVE_FLUSH_ALL:
1145 case BTRFS_RESERVE_FLUSH_ALL_STEAL:
1107 wait_reserve_ticket(fs_info, space_info, ticket);1146 wait_reserve_ticket(fs_info, space_info, ticket);
1108 break;1147 break;
1109 case BTRFS_RESERVE_FLUSH_LIMIT:1148 case BTRFS_RESERVE_FLUSH_LIMIT:
@@ -1125,11 +1164,17 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info, · 15 unmodified lines
1125 ret = ticket->error;1164 ret = ticket->error;
1126 if (ticket->bytes || ticket->error) {1165 if (ticket->bytes || ticket->error) {
1127 /*1166 /*
1128 * Need to delete here for priority tickets. For regular tickets1167 * We were a priority ticket, so we need to delete ourselves
1129 * either the async reclaim job deletes the ticket from the list1168 * from the list. Because we could have other priority tickets
1130 * or we delete it ourselves at wait_reserve_ticket().1169 * behind us that require less space, run
1170 * btrfs_try_granting_tickets() to see if their reservations can
1171 * now be made.
1131 */1172 */
1132 remove_ticket(space_info, ticket);1173 if (!list_empty(&ticket->list)) {
1174 remove_ticket(space_info, ticket);
1175 btrfs_try_granting_tickets(fs_info, space_info);
1176 }
1177
1133 if (!ret)1178 if (!ret)
1134 ret = -ENOSPC;1179 ret = -ENOSPC;
1135 }1180 }
@@ -1145,6 +1190,16 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info, · 9 unmodified lines
1145 return ret;1190 return ret;
1146}1191}
1147 1192
1193/*
1194 * This returns true if this flush state will go through the ordinary flushing
1195 * code.
1196 */
1197static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)
1198{
1199 return (flush == BTRFS_RESERVE_FLUSH_ALL) ||
1200 (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
1201}
1202
1148/**1203/**
1149 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space1204 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
1150 * @root - the root we're allocating for1205 * @root - the root we're allocating for
@@ -1175,8 +1230,17 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info, · 24 unmodified lines
1175 spin_lock(&space_info->lock);1230 spin_lock(&space_info->lock);
1176 ret = -ENOSPC;1231 ret = -ENOSPC;
1177 used = btrfs_space_info_used(space_info, true);1232 used = btrfs_space_info_used(space_info, true);
1178 pending_tickets = !list_empty(&space_info->tickets) ||1233
1179 !list_empty(&space_info->priority_tickets);1234 /*
1235 * We don't want NO_FLUSH allocations to jump everybody, they can
1236 * generally handle ENOSPC in a different way, so treat them the same as
1237 * normal flushers when it comes to skipping pending tickets.
1238 */
1239 if (is_normal_flushing(flush) || (flush == BTRFS_RESERVE_NO_FLUSH))
1240 pending_tickets = !list_empty(&space_info->tickets) ||
1241 !list_empty(&space_info->priority_tickets);
1242 else
1243 pending_tickets = !list_empty(&space_info->priority_tickets);
1180 1244
1181 /*1245 /*
1182 * Carry on if we have enough space (short-circuit) OR call1246 * Carry on if we have enough space (short-circuit) OR call
@@ -1198,12 +1262,13 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info, · 15 unmodified lines
1198 * the list and we will do our own flushing further down.1262 * the list and we will do our own flushing further down.
1199 */1263 */
1200 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {1264 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
1201 ASSERT(space_info->reclaim_size >= 0);
1202 ticket.bytes = orig_bytes;1265 ticket.bytes = orig_bytes;
1203 ticket.error = 0;1266 ticket.error = 0;
1204 space_info->reclaim_size += ticket.bytes;1267 space_info->reclaim_size += ticket.bytes;
1205 init_waitqueue_head(&ticket.wait);1268 init_waitqueue_head(&ticket.wait);
1206 if (flush == BTRFS_RESERVE_FLUSH_ALL) {1269 ticket.steal = (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
1270 if (flush == BTRFS_RESERVE_FLUSH_ALL ||
1271 flush == BTRFS_RESERVE_FLUSH_ALL_STEAL) {
1207 list_add_tail(&ticket.list, &space_info->tickets);1272 list_add_tail(&ticket.list, &space_info->tickets);
1208 if (!space_info->flush) {1273 if (!space_info->flush) {
1209 space_info->flush = 1;1274 space_info->flush = 1;
fs/btrfs/space-info.h+1−0
@@ -78,6 +78,7 @@ struct btrfs_space_info { · 77 unmodified lines
78struct reserve_ticket {78struct reserve_ticket {
79 u64 bytes;79 u64 bytes;
80 int error;80 int error;
81 bool steal;
81 struct list_head list;82 struct list_head list;
82 wait_queue_head_t wait;83 wait_queue_head_t wait;
83};84};
fs/btrfs/struct-funcs.c+112−111
@@ -17,151 +17,152 @@ static inline void put_unaligned_le8(u8 val, void *p) · 16 unmodified lines
17 *(u8 *)p = val;17 *(u8 *)p = val;
18}18}
19 19
20static bool check_setget_bounds(const struct extent_buffer *eb,
21 const void *ptr, unsigned off, int size)
22{
23 const unsigned long member_offset = (unsigned long)ptr + off;
24
25 if (member_offset > eb->len) {
26 btrfs_warn(eb->fs_info,
27 "bad eb member start: ptr 0x%lx start %llu member offset %lu size %d",
28 (unsigned long)ptr, eb->start, member_offset, size);
29 return false;
30 }
31 if (member_offset + size > eb->len) {
32 btrfs_warn(eb->fs_info,
33 "bad eb member end: ptr 0x%lx start %llu member offset %lu size %d",
34 (unsigned long)ptr, eb->start, member_offset, size);
35 return false;
36 }
37
38 return true;
39}
40
20/*41/*
21 * this is some deeply nasty code.42 * Macro templates that define helpers to read/write extent buffer data of a
43 * given size, that are also used via ctree.h for access to item members by
44 * specialized helpers.
22 *45 *
23 * The end result is that anyone who #includes ctree.h gets a46 * Generic helpers:
24 * declaration for the btrfs_set_foo functions and btrfs_foo functions,47 * - btrfs_set_8 (for 8/16/32/64)
25 * which are wrappers of btrfs_set_token_#bits functions and48 * - btrfs_get_8 (for 8/16/32/64)
26 * btrfs_get_token_#bits functions, which are defined in this file.
27 *49 *
28 * These setget functions do all the extent_buffer related mapping50 * Generic helpers with a token (cached address of the most recently accessed
29 * required to efficiently read and write specific fields in the extent51 * page):
30 * buffers. Every pointer to metadata items in btrfs is really just52 * - btrfs_set_token_8 (for 8/16/32/64)
31 * an unsigned long offset into the extent buffer which has been53 * - btrfs_get_token_8 (for 8/16/32/64)
32 * cast to a specific type. This gives us all the gcc type checking.
33 *54 *
34 * The extent buffer api is used to do the page spanning work required to55 * The set/get functions handle data spanning two pages transparently, in case
35 * have a metadata blocksize different from the page size.56 * metadata block size is larger than page. Every pointer to metadata items is
57 * an offset into the extent buffer page array, cast to a specific type. This
58 * gives us all the type checking.
36 *59 *
37 * There are 2 variants defined, one with a token pointer and one without.60 * The extent buffer pages stored in the array pages do not form a contiguous
61 * phyusical range, but the API functions assume the linear offset to the range
62 * from 0 to metadata node size.
38 */63 */
39 64
40#define DEFINE_BTRFS_SETGET_BITS(bits) \65#define DEFINE_BTRFS_SETGET_BITS(bits) \
41u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \66u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
42 const void *ptr, unsigned long off, \67 const void *ptr, unsigned long off) \
43 struct btrfs_map_token *token) \
44{ \68{ \
45 unsigned long part_offset = (unsigned long)ptr; \69 const unsigned long member_offset = (unsigned long)ptr + off; \
46 unsigned long offset = part_offset + off; \70 const unsigned long idx = member_offset >> PAGE_SHIFT; \
47 void *p; \71 const unsigned long oip = offset_in_page(member_offset); \
48 int err; \72 const int size = sizeof(u##bits); \
49 char *kaddr; \73 u8 lebytes[sizeof(u##bits)]; \
50 unsigned long map_start; \74 const int part = PAGE_SIZE - oip; \
51 unsigned long map_len; \
52 int size = sizeof(u##bits); \
53 u##bits res; \
54 \75 \
55 ASSERT(token); \76 ASSERT(token); \
56 ASSERT(token->eb == eb); \77 ASSERT(token->kaddr); \
57 \78 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
58 if (token->kaddr && token->offset <= offset && \79 if (token->offset <= member_offset && \
59 (token->offset + PAGE_SIZE >= offset + size)) { \80 member_offset + size <= token->offset + PAGE_SIZE) { \
60 kaddr = token->kaddr; \81 return get_unaligned_le##bits(token->kaddr + oip); \
61 p = kaddr + part_offset - token->offset; \
62 res = get_unaligned_le##bits(p + off); \
63 return res; \
64 } \82 } \
65 err = map_private_extent_buffer(eb, offset, size, \83 token->kaddr = page_address(token->eb->pages[idx]); \
66 &kaddr, &map_start, &map_len); \84 token->offset = idx << PAGE_SHIFT; \
67 if (err) { \85 if (oip + size <= PAGE_SIZE) \
68 __le##bits leres; \86 return get_unaligned_le##bits(token->kaddr + oip); \
69 \87 \
70 read_extent_buffer(eb, &leres, offset, size); \88 memcpy(lebytes, token->kaddr + oip, part); \
71 return le##bits##_to_cpu(leres); \89 token->kaddr = page_address(token->eb->pages[idx + 1]); \
72 } \90 token->offset = (idx + 1) << PAGE_SHIFT; \
73 p = kaddr + part_offset - map_start; \91 memcpy(lebytes + part, token->kaddr, size - part); \
74 res = get_unaligned_le##bits(p + off); \92 return get_unaligned_le##bits(lebytes); \
75 token->kaddr = kaddr; \
76 token->offset = map_start; \
77 return res; \
78} \93} \
79u##bits btrfs_get_##bits(const struct extent_buffer *eb, \94u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
80 const void *ptr, unsigned long off) \95 const void *ptr, unsigned long off) \
81{ \96{ \
82 unsigned long part_offset = (unsigned long)ptr; \97 const unsigned long member_offset = (unsigned long)ptr + off; \
83 unsigned long offset = part_offset + off; \98 const unsigned long oip = offset_in_page(member_offset); \
84 void *p; \99 const unsigned long idx = member_offset >> PAGE_SHIFT; \
85 int err; \100 char *kaddr = page_address(eb->pages[idx]); \
86 char *kaddr; \101 const int size = sizeof(u##bits); \
87 unsigned long map_start; \102 const int part = PAGE_SIZE - oip; \
88 unsigned long map_len; \103 u8 lebytes[sizeof(u##bits)]; \
89 int size = sizeof(u##bits); \
90 u##bits res; \
91 \104 \
92 err = map_private_extent_buffer(eb, offset, size, \105 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
93 &kaddr, &map_start, &map_len); \106 if (oip + size <= PAGE_SIZE) \
94 if (err) { \107 return get_unaligned_le##bits(kaddr + oip); \
95 __le##bits leres; \
96 \108 \
97 read_extent_buffer(eb, &leres, offset, size); \109 memcpy(lebytes, kaddr + oip, part); \
98 return le##bits##_to_cpu(leres); \110 kaddr = page_address(eb->pages[idx + 1]); \
99 } \111 memcpy(lebytes + part, kaddr, size - part); \
100 p = kaddr + part_offset - map_start; \112 return get_unaligned_le##bits(lebytes); \
101 res = get_unaligned_le##bits(p + off); \
102 return res; \
103} \113} \
104void btrfs_set_token_##bits(struct extent_buffer *eb, \114void btrfs_set_token_##bits(struct btrfs_map_token *token, \
105 const void *ptr, unsigned long off, \115 const void *ptr, unsigned long off, \
106 u##bits val, \116 u##bits val) \
107 struct btrfs_map_token *token) \
108{ \117{ \
109 unsigned long part_offset = (unsigned long)ptr; \118 const unsigned long member_offset = (unsigned long)ptr + off; \
110 unsigned long offset = part_offset + off; \119 const unsigned long idx = member_offset >> PAGE_SHIFT; \
111 void *p; \120 const unsigned long oip = offset_in_page(member_offset); \
112 int err; \121 const int size = sizeof(u##bits); \
113 char *kaddr; \122 u8 lebytes[sizeof(u##bits)]; \
114 unsigned long map_start; \123 const int part = PAGE_SIZE - oip; \
115 unsigned long map_len; \
116 int size = sizeof(u##bits); \
117 \124 \
118 ASSERT(token); \125 ASSERT(token); \
119 ASSERT(token->eb == eb); \126 ASSERT(token->kaddr); \
120 \127 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
121 if (token->kaddr && token->offset <= offset && \128 if (token->offset <= member_offset && \
122 (token->offset + PAGE_SIZE >= offset + size)) { \129 member_offset + size <= token->offset + PAGE_SIZE) { \
123 kaddr = token->kaddr; \130 put_unaligned_le##bits(val, token->kaddr + oip); \
124 p = kaddr + part_offset - token->offset; \
125 put_unaligned_le##bits(val, p + off); \
126 return; \131 return; \
127 } \132 } \
128 err = map_private_extent_buffer(eb, offset, size, \133 token->kaddr = page_address(token->eb->pages[idx]); \
129 &kaddr, &map_start, &map_len); \134 token->offset = idx << PAGE_SHIFT; \
130 if (err) { \135 if (oip + size <= PAGE_SIZE) { \
131 __le##bits val2; \136 put_unaligned_le##bits(val, token->kaddr + oip); \
132 \
133 val2 = cpu_to_le##bits(val); \
134 write_extent_buffer(eb, &val2, offset, size); \
135 return; \137 return; \
136 } \138 } \
137 p = kaddr + part_offset - map_start; \139 put_unaligned_le##bits(val, lebytes); \
138 put_unaligned_le##bits(val, p + off); \140 memcpy(token->kaddr + oip, lebytes, part); \
139 token->kaddr = kaddr; \141 token->kaddr = page_address(token->eb->pages[idx + 1]); \
140 token->offset = map_start; \142 token->offset = (idx + 1) << PAGE_SHIFT; \
143 memcpy(token->kaddr, lebytes + part, size - part); \
141} \144} \
142void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \145void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
143 unsigned long off, u##bits val) \146 unsigned long off, u##bits val) \
144{ \147{ \
145 unsigned long part_offset = (unsigned long)ptr; \148 const unsigned long member_offset = (unsigned long)ptr + off; \
146 unsigned long offset = part_offset + off; \149 const unsigned long oip = offset_in_page(member_offset); \
147 void *p; \150 const unsigned long idx = member_offset >> PAGE_SHIFT; \
148 int err; \151 char *kaddr = page_address(eb->pages[idx]); \
149 char *kaddr; \152 const int size = sizeof(u##bits); \
150 unsigned long map_start; \153 const int part = PAGE_SIZE - oip; \
151 unsigned long map_len; \154 u8 lebytes[sizeof(u##bits)]; \
152 int size = sizeof(u##bits); \
153 \
154 err = map_private_extent_buffer(eb, offset, size, \
155 &kaddr, &map_start, &map_len); \
156 if (err) { \
157 __le##bits val2; \
158 \155 \
159 val2 = cpu_to_le##bits(val); \156 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
160 write_extent_buffer(eb, &val2, offset, size); \157 if (oip + size <= PAGE_SIZE) { \
158 put_unaligned_le##bits(val, kaddr + oip); \
161 return; \159 return; \
162 } \160 } \
163 p = kaddr + part_offset - map_start; \161 \
164 put_unaligned_le##bits(val, p + off); \162 put_unaligned_le##bits(val, lebytes); \
163 memcpy(kaddr + oip, lebytes, part); \
164 kaddr = page_address(eb->pages[idx + 1]); \
165 memcpy(kaddr, lebytes + part, size - part); \
165}166}
166 167
167DEFINE_BTRFS_SETGET_BITS(8)168DEFINE_BTRFS_SETGET_BITS(8)
fs/btrfs/super.c+20−18
@@ -72,23 +72,32 @@ const char * __attribute_const__ btrfs_decode_error(int errno) · 71 unmodified lines
72 char *errstr = "unknown";72 char *errstr = "unknown";
73 73
74 switch (errno) {74 switch (errno) {
75 case -EIO:75 case -ENOENT: /* -2 */
76 errstr = "No such entry";
77 break;
78 case -EIO: /* -5 */
76 errstr = "IO failure";79 errstr = "IO failure";
77 break;80 break;
78 case -ENOMEM:81 case -ENOMEM: /* -12*/
79 errstr = "Out of memory";82 errstr = "Out of memory";
80 break;83 break;
81 case -EROFS:84 case -EEXIST: /* -17 */
82 errstr = "Readonly filesystem";
83 break;
84 case -EEXIST:
85 errstr = "Object already exists";85 errstr = "Object already exists";
86 break;86 break;
87 case -ENOSPC:87 case -ENOSPC: /* -28 */
88 errstr = "No space left";88 errstr = "No space left";
89 break;89 break;
90 case -ENOENT:90 case -EROFS: /* -30 */
91 errstr = "No such entry";91 errstr = "Readonly filesystem";
92 break;
93 case -EOPNOTSUPP: /* -95 */
94 errstr = "Operation not supported";
95 break;
96 case -EUCLEAN: /* -117 */
97 errstr = "Filesystem corrupted";
98 break;
99 case -EDQUOT: /* -122 */
100 errstr = "Quota exceeded";
92 break;101 break;
93 }102 }
94 103
@@ -1093,10 +1102,7 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, · 998 unmodified lines
1093 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);1102 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1094 btrfs_release_path(path);1103 btrfs_release_path(path);
1095 1104
1096 key.objectid = subvol_objectid;1105 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1097 key.type = BTRFS_ROOT_ITEM_KEY;
1098 key.offset = (u64)-1;
1099 fs_root = btrfs_get_fs_root(fs_info, &key, true);
1100 if (IS_ERR(fs_root)) {1106 if (IS_ERR(fs_root)) {
1101 ret = PTR_ERR(fs_root);1107 ret = PTR_ERR(fs_root);
1102 fs_root = NULL;1108 fs_root = NULL;
@@ -1211,7 +1217,6 @@ static int btrfs_fill_super(struct super_block *sb, · 108 unmodified lines
1211{1217{
1212 struct inode *inode;1218 struct inode *inode;
1213 struct btrfs_fs_info *fs_info = btrfs_sb(sb);1219 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1214 struct btrfs_key key;
1215 int err;1220 int err;
1216 1221
1217 sb->s_maxbytes = MAX_LFS_FILESIZE;1222 sb->s_maxbytes = MAX_LFS_FILESIZE;
@@ -1239,10 +1244,7 @@ static int btrfs_fill_super(struct super_block *sb, · 21 unmodified lines
1239 return err;1244 return err;
1240 }1245 }
1241 1246
1242 key.objectid = BTRFS_FIRST_FREE_OBJECTID;1247 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1243 key.type = BTRFS_INODE_ITEM_KEY;
1244 key.offset = 0;
1245 inode = btrfs_iget(sb, &key, fs_info->fs_root);
1246 if (IS_ERR(inode)) {1248 if (IS_ERR(inode)) {
1247 err = PTR_ERR(inode);1249 err = PTR_ERR(inode);
1248 goto fail_close;1250 goto fail_close;
fs/btrfs/transaction.c+32−46
@@ -21,6 +21,7 @@ · 20 unmodified lines
21#include "dev-replace.h"21#include "dev-replace.h"
22#include "qgroup.h"22#include "qgroup.h"
23#include "block-group.h"23#include "block-group.h"
24#include "space-info.h"
24 25
25#define BTRFS_ROOT_TRANS_TAG 026#define BTRFS_ROOT_TRANS_TAG 0
26 27
@@ -141,7 +142,7 @@ void btrfs_put_transaction(struct btrfs_transaction *transaction) · 114 unmodified lines
141 struct btrfs_block_group,142 struct btrfs_block_group,
142 bg_list);143 bg_list);
143 list_del_init(&cache->bg_list);144 list_del_init(&cache->bg_list);
144 btrfs_put_block_group_trimming(cache);145 btrfs_unfreeze_block_group(cache);
145 btrfs_put_block_group(cache);146 btrfs_put_block_group(cache);
146 }147 }
147 WARN_ON(!list_empty(&transaction->dev_update_list));148 WARN_ON(!list_empty(&transaction->dev_update_list));
@@ -348,10 +349,10 @@ loop: · 200 unmodified lines
348}349}
349 350
350/*351/*
351 * this does all the record keeping required to make sure that a reference352 * This does all the record keeping required to make sure that a shareable root
352 * counted root is properly recorded in a given transaction. This is required353 * is properly recorded in a given transaction. This is required to make sure
353 * to make sure the old root from before we joined the transaction is deleted354 * the old root from before we joined the transaction is deleted when the
354 * when the transaction commits355 * transaction commits.
355 */356 */
356static int record_root_in_trans(struct btrfs_trans_handle *trans,357static int record_root_in_trans(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root,358 struct btrfs_root *root,
@@ -359,7 +360,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, · 1 unmodified line
359{360{
360 struct btrfs_fs_info *fs_info = root->fs_info;361 struct btrfs_fs_info *fs_info = root->fs_info;
361 362
362 if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&363 if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
363 root->last_trans < trans->transid) || force) {364 root->last_trans < trans->transid) || force) {
364 WARN_ON(root == fs_info->extent_root);365 WARN_ON(root == fs_info->extent_root);
365 WARN_ON(!force && root->commit_root != root->node);366 WARN_ON(!force && root->commit_root != root->node);
@@ -438,7 +439,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, · 72 unmodified lines
438{439{
439 struct btrfs_fs_info *fs_info = root->fs_info;440 struct btrfs_fs_info *fs_info = root->fs_info;
440 441
441 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))442 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
442 return 0;443 return 0;
443 444
444 /*445 /*
@@ -503,7 +504,7 @@ static inline bool need_reserve_reloc_root(struct btrfs_root *root) · 58 unmodified lines
503 struct btrfs_fs_info *fs_info = root->fs_info;504 struct btrfs_fs_info *fs_info = root->fs_info;
504 505
505 if (!fs_info->reloc_ctl ||506 if (!fs_info->reloc_ctl ||
506 !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||507 !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
507 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||508 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
508 root->reloc_root)509 root->reloc_root)
509 return false;510 return false;
@@ -523,6 +524,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 13 unmodified lines
523 u64 num_bytes = 0;524 u64 num_bytes = 0;
524 u64 qgroup_reserved = 0;525 u64 qgroup_reserved = 0;
525 bool reloc_reserved = false;526 bool reloc_reserved = false;
527 bool do_chunk_alloc = false;
526 int ret;528 int ret;
527 529
528 /* Send isn't supposed to start transactions. */530 /* Send isn't supposed to start transactions. */
@@ -563,7 +565,8 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 34 unmodified lines
563 * refill that amount for whatever is missing in the reserve.565 * refill that amount for whatever is missing in the reserve.
564 */566 */
565 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);567 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
566 if (delayed_refs_rsv->full == 0) {568 if (flush == BTRFS_RESERVE_FLUSH_ALL &&
569 delayed_refs_rsv->full == 0) {
567 delayed_refs_bytes = num_bytes;570 delayed_refs_bytes = num_bytes;
568 num_bytes <<= 1;571 num_bytes <<= 1;
569 }572 }
@@ -584,6 +587,9 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 14 unmodified lines
584 delayed_refs_bytes);587 delayed_refs_bytes);
585 num_bytes -= delayed_refs_bytes;588 num_bytes -= delayed_refs_bytes;
586 }589 }
590
591 if (rsv->space_info->force_alloc)
592 do_chunk_alloc = true;
587 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&593 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
588 !delayed_refs_rsv->full) {594 !delayed_refs_rsv->full) {
589 /*595 /*
@@ -665,6 +671,19 @@ got_it: · 75 unmodified lines
665 if (!current->journal_info)671 if (!current->journal_info)
666 current->journal_info = h;672 current->journal_info = h;
667 673
674 /*
675 * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
676 * ALLOC_FORCE the first run through, and then we won't allocate for
677 * anybody else who races in later. We don't care about the return
678 * value here.
679 */
680 if (do_chunk_alloc && num_bytes) {
681 u64 flags = h->block_rsv->space_info->flags;
682
683 btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
684 CHUNK_ALLOC_NO_FORCE);
685 }
686
668 /*687 /*
669 * btrfs_record_root_in_trans() needs to alloc new extents, and may688 * btrfs_record_root_in_trans() needs to alloc new extents, and may
670 * call btrfs_join_transaction() while we're also starting a689 * call btrfs_join_transaction() while we're also starting a
@@ -699,43 +718,10 @@ struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, · 28 unmodified lines
699 718
700struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(719struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
701 struct btrfs_root *root,720 struct btrfs_root *root,
702 unsigned int num_items,721 unsigned int num_items)
703 int min_factor)
704{722{
705 struct btrfs_fs_info *fs_info = root->fs_info;723 return start_transaction(root, num_items, TRANS_START,
706 struct btrfs_trans_handle *trans;724 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
707 u64 num_bytes;
708 int ret;
709
710 /*
711 * We have two callers: unlink and block group removal. The
712 * former should succeed even if we will temporarily exceed
713 * quota and the latter operates on the extent root so
714 * qgroup enforcement is ignored anyway.
715 */
716 trans = start_transaction(root, num_items, TRANS_START,
717 BTRFS_RESERVE_FLUSH_ALL, false);
718 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
719 return trans;
720
721 trans = btrfs_start_transaction(root, 0);
722 if (IS_ERR(trans))
723 return trans;
724
725 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
726 ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
727 num_bytes, min_factor);
728 if (ret) {
729 btrfs_end_transaction(trans);
730 return ERR_PTR(ret);
731 }
732
733 trans->block_rsv = &fs_info->trans_block_rsv;
734 trans->bytes_reserved = num_bytes;
735 trace_btrfs_space_reservation(fs_info, "transaction",
736 trans->transid, num_bytes, 1);
737
738 return trans;
739}725}
740 726
741struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)727struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
@@ -1644,7 +1630,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 902 unmodified lines
1644 }1630 }
1645 1631
1646 key.offset = (u64)-1;1632 key.offset = (u64)-1;
1647 pending->snap = btrfs_get_fs_root(fs_info, &key, true);1633 pending->snap = btrfs_get_fs_root(fs_info, objectid, true);
1648 if (IS_ERR(pending->snap)) {1634 if (IS_ERR(pending->snap)) {
1649 ret = PTR_ERR(pending->snap);1635 ret = PTR_ERR(pending->snap);
1650 btrfs_abort_transaction(trans, ret);1636 btrfs_abort_transaction(trans, ret);
fs/btrfs/transaction.h+1−2
@@ -193,8 +193,7 @@ struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, · 192 unmodified lines
193 unsigned int num_items);193 unsigned int num_items);
194struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(194struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
195 struct btrfs_root *root,195 struct btrfs_root *root,
196 unsigned int num_items,196 unsigned int num_items);
197 int min_factor);
198struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);197struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
199struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);198struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
200struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);199struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
fs/btrfs/tree-checker.c+0−4
@@ -957,10 +957,6 @@ static int check_dev_item(struct extent_buffer *leaf, · 956 unmodified lines
957 return 0;957 return 0;
958}958}
959 959
960/* Inode item error output has the same format as dir_item_err() */
961#define inode_item_err(eb, slot, fmt, ...) \
962 dir_item_err(eb, slot, fmt, __VA_ARGS__)
963
964static int check_inode_item(struct extent_buffer *leaf,960static int check_inode_item(struct extent_buffer *leaf,
965 struct btrfs_key *key, int slot)961 struct btrfs_key *key, int slot)
966{962{
fs/btrfs/tree-defrag.c+1−1
@@ -35,7 +35,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, · 34 unmodified lines
35 goto out;35 goto out;
36 }36 }
37 37
38 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))38 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
39 goto out;39 goto out;
40 40
41 path = btrfs_alloc_path();41 path = btrfs_alloc_path();
fs/btrfs/tree-log.c+90−102
@@ -505,13 +505,8 @@ insert: · 504 unmodified lines
505 */505 */
506 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&506 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
507 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&507 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
508 ino_size != 0) {508 ino_size != 0)
509 struct btrfs_map_token token;509 btrfs_set_inode_size(dst_eb, dst_item, ino_size);
510
511 btrfs_init_map_token(&token, dst_eb);
512 btrfs_set_token_inode_size(dst_eb, dst_item,
513 ino_size, &token);
514 }
515 goto no_copy;510 goto no_copy;
516 }511 }
517 512
@@ -555,13 +550,9 @@ no_copy: · 37 unmodified lines
555static noinline struct inode *read_one_inode(struct btrfs_root *root,550static noinline struct inode *read_one_inode(struct btrfs_root *root,
556 u64 objectid)551 u64 objectid)
557{552{
558 struct btrfs_key key;
559 struct inode *inode;553 struct inode *inode;
560 554
561 key.objectid = objectid;555 inode = btrfs_iget(root->fs_info->sb, objectid, root);
562 key.type = BTRFS_INODE_ITEM_KEY;
563 key.offset = 0;
564 inode = btrfs_iget(root->fs_info->sb, &key, root);
565 if (IS_ERR(inode))556 if (IS_ERR(inode))
566 inode = NULL;557 inode = NULL;
567 return inode;558 return inode;
@@ -3299,6 +3290,7 @@ static void free_log_tree(struct btrfs_trans_handle *trans, · 2731 unmodified lines
3299 3290
3300 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,3291 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3301 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);3292 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3293 extent_io_tree_release(&log->log_csum_range);
3302 btrfs_put_root(log);3294 btrfs_put_root(log);
3303}3295}
3304 3296
@@ -3816,8 +3808,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans, · 511 unmodified lines
3816 3808
3817 found_key.offset = 0;3809 found_key.offset = 0;
3818 found_key.type = 0;3810 found_key.type = 0;
3819 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,3811 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
3820 &start_slot);
3821 if (ret < 0)3812 if (ret < 0)
3822 break;3813 break;
3823 3814
@@ -3853,44 +3844,41 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 29 unmodified lines
3853 * just to say 'this inode exists' and a logging3844 * just to say 'this inode exists' and a logging
3854 * to say 'update this inode with these values'3845 * to say 'update this inode with these values'
3855 */3846 */
3856 btrfs_set_token_inode_generation(leaf, item, 0, &token);3847 btrfs_set_token_inode_generation(&token, item, 0);
3857 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);3848 btrfs_set_token_inode_size(&token, item, logged_isize);
3858 } else {3849 } else {
3859 btrfs_set_token_inode_generation(leaf, item,3850 btrfs_set_token_inode_generation(&token, item,
3860 BTRFS_I(inode)->generation,3851 BTRFS_I(inode)->generation);
3861 &token);3852 btrfs_set_token_inode_size(&token, item, inode->i_size);
3862 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);3853 }
3863 }3854
3864 3855 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3865 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);3856 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3866 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);3857 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3867 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);3858 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3868 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);3859
3869 3860 btrfs_set_token_timespec_sec(&token, &item->atime,
3870 btrfs_set_token_timespec_sec(leaf, &item->atime,3861 inode->i_atime.tv_sec);
3871 inode->i_atime.tv_sec, &token);3862 btrfs_set_token_timespec_nsec(&token, &item->atime,
3872 btrfs_set_token_timespec_nsec(leaf, &item->atime,3863 inode->i_atime.tv_nsec);
3873 inode->i_atime.tv_nsec, &token);3864
3874 3865 btrfs_set_token_timespec_sec(&token, &item->mtime,
3875 btrfs_set_token_timespec_sec(leaf, &item->mtime,3866 inode->i_mtime.tv_sec);
3876 inode->i_mtime.tv_sec, &token);3867 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3877 btrfs_set_token_timespec_nsec(leaf, &item->mtime,3868 inode->i_mtime.tv_nsec);
3878 inode->i_mtime.tv_nsec, &token);3869
3879 3870 btrfs_set_token_timespec_sec(&token, &item->ctime,
3880 btrfs_set_token_timespec_sec(leaf, &item->ctime,3871 inode->i_ctime.tv_sec);
3881 inode->i_ctime.tv_sec, &token);3872 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3882 btrfs_set_token_timespec_nsec(leaf, &item->ctime,3873 inode->i_ctime.tv_nsec);
3883 inode->i_ctime.tv_nsec, &token);3874
3884 3875 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3885 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),3876
3886 &token);3877 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3887 3878 btrfs_set_token_inode_transid(&token, item, trans->transid);
3888 btrfs_set_token_inode_sequence(leaf, item,3879 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3889 inode_peek_iversion(inode), &token);3880 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3890 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);3881 btrfs_set_token_inode_block_group(&token, item, 0);
3891 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3892 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3893 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3894}3882}
3895 3883
3896static int log_inode_item(struct btrfs_trans_handle *trans,3884static int log_inode_item(struct btrfs_trans_handle *trans,
@@ -3916,8 +3904,20 @@ static int log_csums(struct btrfs_trans_handle *trans, · 19 unmodified lines
3916 struct btrfs_root *log_root,3904 struct btrfs_root *log_root,
3917 struct btrfs_ordered_sum *sums)3905 struct btrfs_ordered_sum *sums)
3918{3906{
3907 const u64 lock_end = sums->bytenr + sums->len - 1;
3908 struct extent_state *cached_state = NULL;
3919 int ret;3909 int ret;
3920 3910
3911 /*
3912 * Serialize logging for checksums. This is to avoid racing with the
3913 * same checksum being logged by another task that is logging another
3914 * file which happens to refer to the same extent as well. Such races
3915 * can leave checksum items in the log with overlapping ranges.
3916 */
3917 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
3918 lock_end, &cached_state);
3919 if (ret)
3920 return ret;
3921 /*3921 /*
3922 * Due to extent cloning, we might have logged a csum item that covers a3922 * Due to extent cloning, we might have logged a csum item that covers a
3923 * subrange of a cloned extent, and later we can end up logging a csum3923 * subrange of a cloned extent, and later we can end up logging a csum
@@ -3928,10 +3928,13 @@ static int log_csums(struct btrfs_trans_handle *trans, · 4 unmodified lines
3928 * trim and adjust) any existing csum items in the log for this range.3928 * trim and adjust) any existing csum items in the log for this range.
3929 */3929 */
3930 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);3930 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
3931 if (ret)3931 if (!ret)
3932 return ret;3932 ret = btrfs_csum_file_blocks(trans, log_root, sums);
3933 3933
3934 return btrfs_csum_file_blocks(trans, log_root, sums);3934 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
3935 &cached_state);
3936
3937 return ret;
3935}3938}
3936 3939
3937static noinline int copy_items(struct btrfs_trans_handle *trans,3940static noinline int copy_items(struct btrfs_trans_handle *trans,
@@ -4164,43 +4167,35 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 226 unmodified lines
4164 fi = btrfs_item_ptr(leaf, path->slots[0],4167 fi = btrfs_item_ptr(leaf, path->slots[0],
4165 struct btrfs_file_extent_item);4168 struct btrfs_file_extent_item);
4166 4169
4167 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,4170 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
4168 &token);
4169 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))4171 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4170 btrfs_set_token_file_extent_type(leaf, fi,4172 btrfs_set_token_file_extent_type(&token, fi,
4171 BTRFS_FILE_EXTENT_PREALLOC,4173 BTRFS_FILE_EXTENT_PREALLOC);
4172 &token);
4173 else4174 else
4174 btrfs_set_token_file_extent_type(leaf, fi,4175 btrfs_set_token_file_extent_type(&token, fi,
4175 BTRFS_FILE_EXTENT_REG,4176 BTRFS_FILE_EXTENT_REG);
4176 &token);
4177 4177
4178 block_len = max(em->block_len, em->orig_block_len);4178 block_len = max(em->block_len, em->orig_block_len);
4179 if (em->compress_type != BTRFS_COMPRESS_NONE) {4179 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4180 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,4180 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4181 em->block_start,4181 em->block_start);
4182 &token);4182 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
4183 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4184 &token);
4185 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {4183 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4186 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,4184 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4187 em->block_start -4185 em->block_start -
4188 extent_offset, &token);4186 extent_offset);
4189 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,4187 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
4190 &token);
4191 } else {4188 } else {
4192 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);4189 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4193 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,4190 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
4194 &token);4191 }
4195 }4192
4196 4193 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4197 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);4194 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4198 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);4195 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4199 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);4196 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4200 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,4197 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4201 &token);4198 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
4202 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4203 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4204 btrfs_mark_buffer_dirty(leaf);4199 btrfs_mark_buffer_dirty(leaf);
4205 4200
4206 btrfs_release_path(path);4201 btrfs_release_path(path);
@@ -4336,12 +4331,9 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, · 129 unmodified lines
4336 }4331 }
4337 }4332 }
4338 }4333 }
4339 if (ins_nr > 0) {4334 if (ins_nr > 0)
4340 ret = copy_items(trans, inode, dst_path, path,4335 ret = copy_items(trans, inode, dst_path, path,
4341 start_slot, ins_nr, 1, 0);4336 start_slot, ins_nr, 1, 0);
4342 if (ret > 0)
4343 ret = 0;
4344 }
4345out:4337out:
4346 btrfs_release_path(path);4338 btrfs_release_path(path);
4347 btrfs_free_path(dst_path);4339 btrfs_free_path(dst_path);
@@ -4835,10 +4827,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans, · 487 unmodified lines
4835 4827
4836 btrfs_release_path(path);4828 btrfs_release_path(path);
4837 4829
4838 key.objectid = ino;4830 inode = btrfs_iget(fs_info->sb, ino, root);
4839 key.type = BTRFS_INODE_ITEM_KEY;
4840 key.offset = 0;
4841 inode = btrfs_iget(fs_info->sb, &key, root);
4842 /*4831 /*
4843 * If the other inode that had a conflicting dir entry was4832 * If the other inode that had a conflicting dir entry was
4844 * deleted in the current transaction, we need to log its parent4833 * deleted in the current transaction, we need to log its parent
@@ -4847,8 +4836,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans, · 2 unmodified lines
4847 if (IS_ERR(inode)) {4836 if (IS_ERR(inode)) {
4848 ret = PTR_ERR(inode);4837 ret = PTR_ERR(inode);
4849 if (ret == -ENOENT) {4838 if (ret == -ENOENT) {
4850 key.objectid = parent;4839 inode = btrfs_iget(fs_info->sb, parent, root);
4851 inode = btrfs_iget(fs_info->sb, &key, root);
4852 if (IS_ERR(inode)) {4840 if (IS_ERR(inode)) {
4853 ret = PTR_ERR(inode);4841 ret = PTR_ERR(inode);
4854 } else {4842 } else {
@@ -5587,7 +5575,7 @@ process_leaf: · 732 unmodified lines
5587 continue;5575 continue;
5588 5576
5589 btrfs_release_path(path);5577 btrfs_release_path(path);
5590 di_inode = btrfs_iget(fs_info->sb, &di_key, root);5578 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
5591 if (IS_ERR(di_inode)) {5579 if (IS_ERR(di_inode)) {
5592 ret = PTR_ERR(di_inode);5580 ret = PTR_ERR(di_inode);
5593 goto next_dir_inode;5581 goto next_dir_inode;
@@ -5713,7 +5701,8 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, · 119 unmodified lines
5713 cur_offset = item_size;5701 cur_offset = item_size;
5714 }5702 }
5715 5703
5716 dir_inode = btrfs_iget(fs_info->sb, &inode_key, root);5704 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
5705 root);
5717 /*5706 /*
5718 * If the parent inode was deleted, return an error to5707 * If the parent inode was deleted, return an error to
5719 * fallback to a transaction commit. This is to prevent5708 * fallback to a transaction commit. This is to prevent
@@ -5780,14 +5769,17 @@ static int log_new_ancestors(struct btrfs_trans_handle *trans, · 60 unmodified lines
5780 int slot = path->slots[0];5769 int slot = path->slots[0];
5781 struct btrfs_key search_key;5770 struct btrfs_key search_key;
5782 struct inode *inode;5771 struct inode *inode;
5772 u64 ino;
5783 int ret = 0;5773 int ret = 0;
5784 5774
5785 btrfs_release_path(path);5775 btrfs_release_path(path);
5786 5776
5777 ino = found_key.offset;
5778
5787 search_key.objectid = found_key.offset;5779 search_key.objectid = found_key.offset;
5788 search_key.type = BTRFS_INODE_ITEM_KEY;5780 search_key.type = BTRFS_INODE_ITEM_KEY;
5789 search_key.offset = 0;5781 search_key.offset = 0;
5790 inode = btrfs_iget(fs_info->sb, &search_key, root);5782 inode = btrfs_iget(fs_info->sb, ino, root);
5791 if (IS_ERR(inode))5783 if (IS_ERR(inode))
5792 return PTR_ERR(inode);5784 return PTR_ERR(inode);
5793 5785
@@ -6132,7 +6124,6 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree) · 338 unmodified lines
6132 struct btrfs_trans_handle *trans;6124 struct btrfs_trans_handle *trans;
6133 struct btrfs_key key;6125 struct btrfs_key key;
6134 struct btrfs_key found_key;6126 struct btrfs_key found_key;
6135 struct btrfs_key tmp_key;
6136 struct btrfs_root *log;6127 struct btrfs_root *log;
6137 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;6128 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6138 struct walk_control wc = {6129 struct walk_control wc = {
@@ -6194,11 +6185,8 @@ again: · 55 unmodified lines
6194 goto error;6185 goto error;
6195 }6186 }
6196 6187
6197 tmp_key.objectid = found_key.offset;6188 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6198 tmp_key.type = BTRFS_ROOT_ITEM_KEY;6189 true);
6199 tmp_key.offset = (u64)-1;
6200
6201 wc.replay_dest = btrfs_get_fs_root(fs_info, &tmp_key, true);
6202 if (IS_ERR(wc.replay_dest)) {6190 if (IS_ERR(wc.replay_dest)) {
6203 ret = PTR_ERR(wc.replay_dest);6191 ret = PTR_ERR(wc.replay_dest);
6204 6192
fs/btrfs/uuid-tree.c+1−5
@@ -257,7 +257,6 @@ out: · 256 unmodified lines
257static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,257static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
258 u8 *uuid, u8 type, u64 subvolid)258 u8 *uuid, u8 type, u64 subvolid)
259{259{
260 struct btrfs_key key;
261 int ret = 0;260 int ret = 0;
262 struct btrfs_root *subvol_root;261 struct btrfs_root *subvol_root;
263 262
@@ -265,10 +264,7 @@ static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, · 1 unmodified line
265 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)264 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
266 goto out;265 goto out;
267 266
268 key.objectid = subvolid;267 subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
269 key.type = BTRFS_ROOT_ITEM_KEY;
270 key.offset = (u64)-1;
271 subvol_root = btrfs_get_fs_root(fs_info, &key, true);
272 if (IS_ERR(subvol_root)) {268 if (IS_ERR(subvol_root)) {
273 ret = PTR_ERR(subvol_root);269 ret = PTR_ERR(subvol_root);
274 if (ret == -ENOENT)270 if (ret == -ENOENT)
fs/btrfs/volumes.c+44−36
@@ -280,10 +280,9 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, · 279 unmodified lines
280 * ============280 * ============
281 *281 *
282 * uuid_mutex282 * uuid_mutex
283 * volume_mutex283 * device_list_mutex
284 * device_list_mutex284 * chunk_mutex
285 * chunk_mutex285 * balance_mutex
286 * balance_mutex
287 *286 *
288 *287 *
289 * Exclusive operations, BTRFS_FS_EXCL_OP288 * Exclusive operations, BTRFS_FS_EXCL_OP
@@ -1042,6 +1041,8 @@ again: · 752 unmodified lines
1042 &device->dev_state)) {1041 &device->dev_state)) {
1043 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,1042 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1044 &device->dev_state) &&1043 &device->dev_state) &&
1044 !test_bit(BTRFS_DEV_STATE_MISSING,
1045 &device->dev_state) &&
1045 (!latest_dev ||1046 (!latest_dev ||
1046 device->generation > latest_dev->generation)) {1047 device->generation > latest_dev->generation)) {
1047 latest_dev = device;1048 latest_dev = device;
@@ -1185,7 +1186,6 @@ static int open_fs_devices(struct btrfs_fs_devices *fs_devices, · 137 unmodified lines
1185{1186{
1186 struct btrfs_device *device;1187 struct btrfs_device *device;
1187 struct btrfs_device *latest_dev = NULL;1188 struct btrfs_device *latest_dev = NULL;
1188 int ret = 0;
1189 1189
1190 flags |= FMODE_EXCL;1190 flags |= FMODE_EXCL;
1191 1191
@@ -1198,16 +1198,15 @@ static int open_fs_devices(struct btrfs_fs_devices *fs_devices, · 6 unmodified lines
1198 device->generation > latest_dev->generation)1198 device->generation > latest_dev->generation)
1199 latest_dev = device;1199 latest_dev = device;
1200 }1200 }
1201 if (fs_devices->open_devices == 0) {1201 if (fs_devices->open_devices == 0)
1202 ret = -EINVAL;1202 return -EINVAL;
1203 goto out;1203
1204 }
1205 fs_devices->opened = 1;1204 fs_devices->opened = 1;
1206 fs_devices->latest_bdev = latest_dev->bdev;1205 fs_devices->latest_bdev = latest_dev->bdev;
1207 fs_devices->total_rw_bytes = 0;1206 fs_devices->total_rw_bytes = 0;
1208 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;1207 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
1209out:1208
1210 return ret;1209 return 0;
1211}1210}
1212 1211
1213static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)1212static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
@@ -1251,49 +1250,48 @@ void btrfs_release_disk_super(struct btrfs_super_block *super) · 37 unmodified lines
1251 put_page(page);1250 put_page(page);
1252}1251}
1253 1252
1254static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,1253static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
1255 struct page **page,1254 u64 bytenr)
1256 struct btrfs_super_block **disk_super)
1257{1255{
1256 struct btrfs_super_block *disk_super;
1257 struct page *page;
1258 void *p;1258 void *p;
1259 pgoff_t index;1259 pgoff_t index;
1260 1260
1261 /* make sure our super fits in the device */1261 /* make sure our super fits in the device */
1262 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))1262 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1263 return 1;1263 return ERR_PTR(-EINVAL);
1264 1264
1265 /* make sure our super fits in the page */1265 /* make sure our super fits in the page */
1266 if (sizeof(**disk_super) > PAGE_SIZE)1266 if (sizeof(*disk_super) > PAGE_SIZE)
1267 return 1;1267 return ERR_PTR(-EINVAL);
1268 1268
1269 /* make sure our super doesn't straddle pages on disk */1269 /* make sure our super doesn't straddle pages on disk */
1270 index = bytenr >> PAGE_SHIFT;1270 index = bytenr >> PAGE_SHIFT;
1271 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)1271 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1272 return 1;1272 return ERR_PTR(-EINVAL);
1273 1273
1274 /* pull in the page with our super */1274 /* pull in the page with our super */
1275 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,1275 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
1276 index, GFP_KERNEL);
1277 1276
1278 if (IS_ERR(*page))1277 if (IS_ERR(page))
1279 return 1;1278 return ERR_CAST(page);
1280 1279
1281 p = page_address(*page);1280 p = page_address(page);
1282 1281
1283 /* align our pointer to the offset of the super block */1282 /* align our pointer to the offset of the super block */
1284 *disk_super = p + offset_in_page(bytenr);1283 disk_super = p + offset_in_page(bytenr);
1285 1284
1286 if (btrfs_super_bytenr(*disk_super) != bytenr ||1285 if (btrfs_super_bytenr(disk_super) != bytenr ||
1287 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {1286 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1288 btrfs_release_disk_super(p);1287 btrfs_release_disk_super(p);
1289 return 1;1288 return ERR_PTR(-EINVAL);
1290 }1289 }
1291 1290
1292 if ((*disk_super)->label[0] &&1291 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1293 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])1292 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
1294 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1295 1293
1296 return 0;1294 return disk_super;
1297}1295}
1298 1296
1299int btrfs_forget_devices(const char *path)1297int btrfs_forget_devices(const char *path)
@@ -1319,7 +1317,6 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, · 19 unmodified lines
1319 bool new_device_added = false;1317 bool new_device_added = false;
1320 struct btrfs_device *device = NULL;1318 struct btrfs_device *device = NULL;
1321 struct block_device *bdev;1319 struct block_device *bdev;
1322 struct page *page;
1323 u64 bytenr;1320 u64 bytenr;
1324 1321
1325 lockdep_assert_held(&uuid_mutex);1322 lockdep_assert_held(&uuid_mutex);
@@ -1337,8 +1334,9 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, · 11 unmodified lines
1337 if (IS_ERR(bdev))1334 if (IS_ERR(bdev))
1338 return ERR_CAST(bdev);1335 return ERR_CAST(bdev);
1339 1336
1340 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {1337 disk_super = btrfs_read_disk_super(bdev, bytenr);
1341 device = ERR_PTR(-EINVAL);1338 if (IS_ERR(disk_super)) {
1339 device = ERR_CAST(disk_super);
1342 goto error_bdev_put;1340 goto error_bdev_put;
1343 }1341 }
1344 1342
@@ -2663,8 +2661,18 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · 1318 unmodified lines
2663 ret = btrfs_commit_transaction(trans);2661 ret = btrfs_commit_transaction(trans);
2664 }2662 }
2665 2663
2666 /* Update ctime/mtime for libblkid */2664 /*
2665 * Now that we have written a new super block to this device, check all
2666 * other fs_devices list if device_path alienates any other scanned
2667 * device.
2668 * We can ignore the return value as it typically returns -EINVAL and
2669 * only succeeds if the device was an alien.
2670 */
2671 btrfs_forget_devices(device_path);
2672
2673 /* Update ctime/mtime for blkid or udev */
2667 update_dev_time(device_path);2674 update_dev_time(device_path);
2675
2668 return ret;2676 return ret;
2669 2677
2670error_sysfs:2678error_sysfs:
fs/direct-io.c+0−19
@@ -386,25 +386,6 @@ static void dio_bio_end_io(struct bio *bio) · 385 unmodified lines
386 spin_unlock_irqrestore(&dio->bio_lock, flags);386 spin_unlock_irqrestore(&dio->bio_lock, flags);
387}387}
388 388
389/**
390 * dio_end_io - handle the end io action for the given bio
391 * @bio: The direct io bio thats being completed
392 *
393 * This is meant to be called by any filesystem that uses their own dio_submit_t
394 * so that the DIO specific endio actions are dealt with after the filesystem
395 * has done it's completion work.
396 */
397void dio_end_io(struct bio *bio)
398{
399 struct dio *dio = bio->bi_private;
400
401 if (dio->is_async)
402 dio_bio_end_aio(bio);
403 else
404 dio_bio_end_io(bio);
405}
406EXPORT_SYMBOL_GPL(dio_end_io);
407
408static inline void389static inline void
409dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,390dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
410 struct block_device *bdev,391 struct block_device *bdev,
fs/iomap/direct-io.c+10−7
@@ -59,7 +59,7 @@ int iomap_dio_iopoll(struct kiocb *kiocb, bool spin) · 58 unmodified lines
59EXPORT_SYMBOL_GPL(iomap_dio_iopoll);59EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
60 60
61static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,61static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
62 struct bio *bio)62 struct bio *bio, loff_t pos)
63{63{
64 atomic_inc(&dio->ref);64 atomic_inc(&dio->ref);
65 65
@@ -67,7 +67,12 @@ static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap, · 1 unmodified line
67 bio_set_polled(bio, dio->iocb);67 bio_set_polled(bio, dio->iocb);
68 68
69 dio->submit.last_queue = bdev_get_queue(iomap->bdev);69 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
70 dio->submit.cookie = submit_bio(bio);70 if (dio->dops && dio->dops->submit_io)
71 dio->submit.cookie = dio->dops->submit_io(
72 file_inode(dio->iocb->ki_filp),
73 iomap, bio, pos);
74 else
75 dio->submit.cookie = submit_bio(bio);
71}76}
72 77
73static ssize_t iomap_dio_complete(struct iomap_dio *dio)78static ssize_t iomap_dio_complete(struct iomap_dio *dio)
@@ -191,7 +196,7 @@ iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos, · 117 unmodified lines
191 get_page(page);196 get_page(page);
192 __bio_add_page(bio, page, len, 0);197 __bio_add_page(bio, page, len, 0);
193 bio_set_op_attrs(bio, REQ_OP_WRITE, flags);198 bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
194 iomap_dio_submit_bio(dio, iomap, bio);199 iomap_dio_submit_bio(dio, iomap, bio, pos);
195}200}
196 201
197static loff_t202static loff_t
@@ -299,11 +304,11 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length, · 101 unmodified lines
299 }304 }
300 305
301 dio->size += n;306 dio->size += n;
302 pos += n;
303 copied += n;307 copied += n;
304 308
305 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);309 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
306 iomap_dio_submit_bio(dio, iomap, bio);310 iomap_dio_submit_bio(dio, iomap, bio, pos);
311 pos += n;
307 } while (nr_pages);312 } while (nr_pages);
308 313
309 /*314 /*
@@ -411,8 +416,6 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · 101 unmodified lines
411 struct blk_plug plug;416 struct blk_plug plug;
412 struct iomap_dio *dio;417 struct iomap_dio *dio;
413 418
414 lockdep_assert_held(&inode->i_rwsem);
415
416 if (!count)419 if (!count)
417 return 0;420 return 0;
418 421
include/linux/bio.h+8−0
@@ -169,6 +169,14 @@ static inline void bio_advance_iter(const struct bio *bio, · 168 unmodified lines
169#define bio_for_each_bvec(bvl, bio, iter) \169#define bio_for_each_bvec(bvl, bio, iter) \
170 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)170 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
171 171
172/*
173 * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
174 * same reasons as bio_for_each_segment_all().
175 */
176#define bio_for_each_bvec_all(bvl, bio, i) \
177 for (i = 0, bvl = bio_first_bvec_all(bio); \
178 i < (bio)->bi_vcnt; i++, bvl++) \
179
172#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)180#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
173 181
174static inline unsigned bio_segments(struct bio *bio)182static inline unsigned bio_segments(struct bio *bio)
include/linux/fs.h+2−2
@@ -3148,6 +3148,8 @@ extern int generic_file_rw_checks(struct file *file_in, struct file *file_out); · 3147 unmodified lines
3148extern int generic_copy_file_checks(struct file *file_in, loff_t pos_in,3148extern int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
3149 struct file *file_out, loff_t pos_out,3149 struct file *file_out, loff_t pos_out,
3150 size_t *count, unsigned int flags);3150 size_t *count, unsigned int flags);
3151extern ssize_t generic_file_buffered_read(struct kiocb *iocb,
3152 struct iov_iter *to, ssize_t already_read);
3151extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);3153extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
3152extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);3154extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
3153extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);3155extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
@@ -3209,8 +3211,6 @@ enum { · 55 unmodified lines
3209 DIO_SKIP_HOLES = 0x02,3211 DIO_SKIP_HOLES = 0x02,
3210};3212};
3211 3213
3212void dio_end_io(struct bio *bio);
3213
3214ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,3214ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
3215 struct block_device *bdev, struct iov_iter *iter,3215 struct block_device *bdev, struct iov_iter *iter,
3216 get_block_t get_block,3216 get_block_t get_block,
include/linux/iomap.h+2−0
@@ -251,6 +251,8 @@ int iomap_writepages(struct address_space *mapping, · 250 unmodified lines
251struct iomap_dio_ops {251struct iomap_dio_ops {
252 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,252 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
253 unsigned flags);253 unsigned flags);
254 blk_qc_t (*submit_io)(struct inode *inode, struct iomap *iomap,
255 struct bio *bio, loff_t file_offset);
254};256};
255 257
256ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,258ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
include/trace/events/btrfs.h+1−0
@@ -89,6 +89,7 @@ TRACE_DEFINE_ENUM(COMMIT_TRANS); · 88 unmodified lines
89 { IO_TREE_TRANS_DIRTY_PAGES, "TRANS_DIRTY_PAGES" }, \89 { IO_TREE_TRANS_DIRTY_PAGES, "TRANS_DIRTY_PAGES" }, \
90 { IO_TREE_ROOT_DIRTY_LOG_PAGES, "ROOT_DIRTY_LOG_PAGES" }, \90 { IO_TREE_ROOT_DIRTY_LOG_PAGES, "ROOT_DIRTY_LOG_PAGES" }, \
91 { IO_TREE_INODE_FILE_EXTENT, "INODE_FILE_EXTENT" }, \91 { IO_TREE_INODE_FILE_EXTENT, "INODE_FILE_EXTENT" }, \
92 { IO_TREE_LOG_CSUM_RANGE, "LOG_CSUM_RANGE" }, \
92 { IO_TREE_SELFTEST, "SELFTEST" })93 { IO_TREE_SELFTEST, "SELFTEST" })
93 94
94#define BTRFS_GROUP_FLAGS \95#define BTRFS_GROUP_FLAGS \
include/uapi/linux/btrfs_tree.h+0−9
@@ -519,15 +519,6 @@ struct btrfs_extent_inline_ref { · 518 unmodified lines
519 __le64 offset;519 __le64 offset;
520} __attribute__ ((__packed__));520} __attribute__ ((__packed__));
521 521
522/* old style backrefs item */
523struct btrfs_extent_ref_v0 {
524 __le64 root;
525 __le64 generation;
526 __le64 objectid;
527 __le32 count;
528} __attribute__ ((__packed__));
529
530
531/* dev extents record free space on individual devices. The owner522/* dev extents record free space on individual devices. The owner
532 * field points back to the chunk allocation mapping tree that allocated523 * field points back to the chunk allocation mapping tree that allocated
533 * the extent. The chunk tree uuid field is a way to double check the owner524 * the extent. The chunk tree uuid field is a way to double check the owner
mm/filemap.c+2−1
@@ -1991,7 +1991,7 @@ static void shrink_readahead_size_eio(struct file_ra_state *ra) · 1990 unmodified lines
1991 * * total number of bytes copied, including those the were already @written1991 * * total number of bytes copied, including those the were already @written
1992 * * negative error code if nothing was copied1992 * * negative error code if nothing was copied
1993 */1993 */
1994static ssize_t generic_file_buffered_read(struct kiocb *iocb,1994ssize_t generic_file_buffered_read(struct kiocb *iocb,
1995 struct iov_iter *iter, ssize_t written)1995 struct iov_iter *iter, ssize_t written)
1996{1996{
1997 struct file *filp = iocb->ki_filp;1997 struct file *filp = iocb->ki_filp;
@@ -2243,6 +2243,7 @@ out: · 245 unmodified lines
2243 file_accessed(filp);2243 file_accessed(filp);
2244 return written ? written : error;2244 return written ? written : error;
2245}2245}
2246EXPORT_SYMBOL_GPL(generic_file_buffered_read);
2246 2247
2247/**2248/**
2248 * generic_file_read_iter - generic filesystem read routine2249 * generic_file_read_iter - generic filesystem read routine