1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
231db9f7cSAlexander Block /*
331db9f7cSAlexander Block * Copyright (C) 2012 Alexander Block. All rights reserved.
431db9f7cSAlexander Block */
531db9f7cSAlexander Block
631db9f7cSAlexander Block #include <linux/bsearch.h>
731db9f7cSAlexander Block #include <linux/fs.h>
831db9f7cSAlexander Block #include <linux/file.h>
931db9f7cSAlexander Block #include <linux/sort.h>
1031db9f7cSAlexander Block #include <linux/mount.h>
1131db9f7cSAlexander Block #include <linux/xattr.h>
1231db9f7cSAlexander Block #include <linux/posix_acl_xattr.h>
135b8418b8SDavid Sterba #include <linux/radix-tree.h>
14a1857ebeSStephen Rothwell #include <linux/vmalloc.h>
15ed84885dSAndy Shevchenko #include <linux/string.h>
162351f431SJosef Bacik #include <linux/compat.h>
179678c543SNikolay Borisov #include <linux/crc32c.h>
1838622010SBoris Burkov #include <linux/fsverity.h>
1931db9f7cSAlexander Block
2031db9f7cSAlexander Block #include "send.h"
218234d3f6SDavid Sterba #include "ctree.h"
2231db9f7cSAlexander Block #include "backref.h"
2331db9f7cSAlexander Block #include "locking.h"
2431db9f7cSAlexander Block #include "disk-io.h"
2531db9f7cSAlexander Block #include "btrfs_inode.h"
2631db9f7cSAlexander Block #include "transaction.h"
27ebb8765bSAnand Jain #include "compression.h"
2889efda52SMarcos Paulo de Souza #include "xattr.h"
29d96b3424SFilipe Manana #include "print-tree.h"
3007e81dc9SJosef Bacik #include "accessors.h"
31f2b39277SJosef Bacik #include "dir-item.h"
327c8ede16SJosef Bacik #include "file-item.h"
337572dec8SJosef Bacik #include "ioctl.h"
345c11adccSJosef Bacik #include "verity.h"
3590b90d4aSFilipe Manana #include "lru_cache.h"
3631db9f7cSAlexander Block
3731db9f7cSAlexander Block /*
38fd0ddbe2SFilipe Manana * Maximum number of references an extent can have in order for us to attempt to
39fd0ddbe2SFilipe Manana * issue clone operations instead of write operations. This currently exists to
40fd0ddbe2SFilipe Manana * avoid hitting limitations of the backreference walking code (taking a lot of
41fd0ddbe2SFilipe Manana * time and using too much memory for extents with large number of references).
42fd0ddbe2SFilipe Manana */
43e2a04165SFilipe Manana #define SEND_MAX_EXTENT_REFS 1024
44fd0ddbe2SFilipe Manana
45fd0ddbe2SFilipe Manana /*
4631db9f7cSAlexander Block * A fs_path is a helper to dynamically build path names with unknown size.
4731db9f7cSAlexander Block * It reallocates the internal buffer on demand.
4831db9f7cSAlexander Block * It allows fast adding of path elements on the right side (normal path) and
4931db9f7cSAlexander Block * fast adding to the left side (reversed path). A reversed path can also be
5031db9f7cSAlexander Block * unreversed if needed.
5131db9f7cSAlexander Block */
5231db9f7cSAlexander Block struct fs_path {
5331db9f7cSAlexander Block union {
5431db9f7cSAlexander Block struct {
5531db9f7cSAlexander Block char *start;
5631db9f7cSAlexander Block char *end;
5731db9f7cSAlexander Block
5831db9f7cSAlexander Block char *buf;
591f5a7ff9SDavid Sterba unsigned short buf_len:15;
601f5a7ff9SDavid Sterba unsigned short reversed:1;
6131db9f7cSAlexander Block char inline_buf[];
6231db9f7cSAlexander Block };
63ace01050SDavid Sterba /*
64ace01050SDavid Sterba * Average path length does not exceed 200 bytes, we'll have
65ace01050SDavid Sterba * better packing in the slab and higher chance to satisfy
66ace01050SDavid Sterba * a allocation later during send.
67ace01050SDavid Sterba */
68ace01050SDavid Sterba char pad[256];
6931db9f7cSAlexander Block };
7031db9f7cSAlexander Block };
7131db9f7cSAlexander Block #define FS_PATH_INLINE_SIZE \
7231db9f7cSAlexander Block (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
7331db9f7cSAlexander Block
7431db9f7cSAlexander Block
7531db9f7cSAlexander Block /* reused for each extent */
7631db9f7cSAlexander Block struct clone_root {
7731db9f7cSAlexander Block struct btrfs_root *root;
7831db9f7cSAlexander Block u64 ino;
7931db9f7cSAlexander Block u64 offset;
80c7499a64SFilipe Manana u64 num_bytes;
8188ffb665SFilipe Manana bool found_ref;
8231db9f7cSAlexander Block };
8331db9f7cSAlexander Block
84c48545deSFilipe Manana #define SEND_MAX_NAME_CACHE_SIZE 256
8531db9f7cSAlexander Block
8666d04209SFilipe Manana /*
87ace79df8SFilipe Manana * Limit the root_ids array of struct backref_cache_entry to 17 elements.
88ace79df8SFilipe Manana * This makes the size of a cache entry to be exactly 192 bytes on x86_64, which
89ace79df8SFilipe Manana * can be satisfied from the kmalloc-192 slab, without wasting any space.
9066d04209SFilipe Manana * The most common case is to have a single root for cloning, which corresponds
91ace79df8SFilipe Manana * to the send root. Having the user specify more than 16 clone roots is not
9266d04209SFilipe Manana * common, and in such rare cases we simply don't use caching if the number of
93ace79df8SFilipe Manana * cloning roots that lead down to a leaf is more than 17.
9466d04209SFilipe Manana */
95ace79df8SFilipe Manana #define SEND_MAX_BACKREF_CACHE_ROOTS 17
9666d04209SFilipe Manana
9766d04209SFilipe Manana /*
9866d04209SFilipe Manana * Max number of entries in the cache.
99ace79df8SFilipe Manana * With SEND_MAX_BACKREF_CACHE_ROOTS as 17, the size in bytes, excluding
100ace79df8SFilipe Manana * maple tree's internal nodes, is 24K.
10166d04209SFilipe Manana */
10266d04209SFilipe Manana #define SEND_MAX_BACKREF_CACHE_SIZE 128
10366d04209SFilipe Manana
10466d04209SFilipe Manana /*
10566d04209SFilipe Manana * A backref cache entry maps a leaf to a list of IDs of roots from which the
10666d04209SFilipe Manana * leaf is accessible and we can use for clone operations.
10766d04209SFilipe Manana * With SEND_MAX_BACKREF_CACHE_ROOTS as 12, each cache entry is 128 bytes (on
10866d04209SFilipe Manana * x86_64).
10966d04209SFilipe Manana */
11066d04209SFilipe Manana struct backref_cache_entry {
11190b90d4aSFilipe Manana struct btrfs_lru_cache_entry entry;
11266d04209SFilipe Manana u64 root_ids[SEND_MAX_BACKREF_CACHE_ROOTS];
11366d04209SFilipe Manana /* Number of valid elements in the root_ids array. */
11466d04209SFilipe Manana int num_roots;
11566d04209SFilipe Manana };
11666d04209SFilipe Manana
11790b90d4aSFilipe Manana /* See the comment at lru_cache.h about struct btrfs_lru_cache_entry. */
11890b90d4aSFilipe Manana static_assert(offsetof(struct backref_cache_entry, entry) == 0);
11990b90d4aSFilipe Manana
120e8a7f49dSFilipe Manana /*
121e8a7f49dSFilipe Manana * Max number of entries in the cache that stores directories that were already
122e8a7f49dSFilipe Manana * created. The cache uses raw struct btrfs_lru_cache_entry entries, so it uses
1230da0c560SFilipe Manana * at most 4096 bytes - sizeof(struct btrfs_lru_cache_entry) is 48 bytes, but
124e8a7f49dSFilipe Manana * the kmalloc-64 slab is used, so we get 4096 bytes (64 bytes * 64).
125e8a7f49dSFilipe Manana */
126e8a7f49dSFilipe Manana #define SEND_MAX_DIR_CREATED_CACHE_SIZE 64
127e8a7f49dSFilipe Manana
1283e49363bSFilipe Manana /*
1293e49363bSFilipe Manana * Max number of entries in the cache that stores directories that were already
1303e49363bSFilipe Manana * created. The cache uses raw struct btrfs_lru_cache_entry entries, so it uses
1313e49363bSFilipe Manana * at most 4096 bytes - sizeof(struct btrfs_lru_cache_entry) is 48 bytes, but
1323e49363bSFilipe Manana * the kmalloc-64 slab is used, so we get 4096 bytes (64 bytes * 64).
1333e49363bSFilipe Manana */
1343e49363bSFilipe Manana #define SEND_MAX_DIR_UTIMES_CACHE_SIZE 64
1353e49363bSFilipe Manana
13631db9f7cSAlexander Block struct send_ctx {
13731db9f7cSAlexander Block struct file *send_filp;
13831db9f7cSAlexander Block loff_t send_off;
13931db9f7cSAlexander Block char *send_buf;
14031db9f7cSAlexander Block u32 send_size;
14131db9f7cSAlexander Block u32 send_max_size;
142356bbbb6SOmar Sandoval /*
143356bbbb6SOmar Sandoval * Whether BTRFS_SEND_A_DATA attribute was already added to current
144356bbbb6SOmar Sandoval * command (since protocol v2, data must be the last attribute).
145356bbbb6SOmar Sandoval */
146356bbbb6SOmar Sandoval bool put_data;
147a4b333f2SOmar Sandoval struct page **send_buf_pages;
148cb95e7bfSMark Fasheh u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
149e77fbf99SDavid Sterba /* Protocol version compatibility requested */
150e77fbf99SDavid Sterba u32 proto;
15131db9f7cSAlexander Block
15231db9f7cSAlexander Block struct btrfs_root *send_root;
15331db9f7cSAlexander Block struct btrfs_root *parent_root;
15431db9f7cSAlexander Block struct clone_root *clone_roots;
15531db9f7cSAlexander Block int clone_roots_cnt;
15631db9f7cSAlexander Block
15731db9f7cSAlexander Block /* current state of the compare_tree call */
15831db9f7cSAlexander Block struct btrfs_path *left_path;
15931db9f7cSAlexander Block struct btrfs_path *right_path;
16031db9f7cSAlexander Block struct btrfs_key *cmp_key;
16131db9f7cSAlexander Block
16231db9f7cSAlexander Block /*
163d96b3424SFilipe Manana * Keep track of the generation of the last transaction that was used
164d96b3424SFilipe Manana * for relocating a block group. This is periodically checked in order
165d96b3424SFilipe Manana * to detect if a relocation happened since the last check, so that we
166d96b3424SFilipe Manana * don't operate on stale extent buffers for nodes (level >= 1) or on
167d96b3424SFilipe Manana * stale disk_bytenr values of file extent items.
168d96b3424SFilipe Manana */
169d96b3424SFilipe Manana u64 last_reloc_trans;
170d96b3424SFilipe Manana
171d96b3424SFilipe Manana /*
17231db9f7cSAlexander Block * infos of the currently processed inode. In case of deleted inodes,
17331db9f7cSAlexander Block * these are the values from the deleted inode.
17431db9f7cSAlexander Block */
17531db9f7cSAlexander Block u64 cur_ino;
17631db9f7cSAlexander Block u64 cur_inode_gen;
17731db9f7cSAlexander Block u64 cur_inode_size;
17831db9f7cSAlexander Block u64 cur_inode_mode;
179644d1940SLiu Bo u64 cur_inode_rdev;
18016e7549fSJosef Bacik u64 cur_inode_last_extent;
181ffa7c429SFilipe Manana u64 cur_inode_next_write_offset;
1829555e1f1SDavid Sterba bool cur_inode_new;
1839555e1f1SDavid Sterba bool cur_inode_new_gen;
1849555e1f1SDavid Sterba bool cur_inode_deleted;
18546b2f459SFilipe Manana bool ignore_cur_inode;
18638622010SBoris Burkov bool cur_inode_needs_verity;
18738622010SBoris Burkov void *verity_descriptor;
18831db9f7cSAlexander Block
18931db9f7cSAlexander Block u64 send_progress;
19031db9f7cSAlexander Block
19131db9f7cSAlexander Block struct list_head new_refs;
19231db9f7cSAlexander Block struct list_head deleted_refs;
19331db9f7cSAlexander Block
194c48545deSFilipe Manana struct btrfs_lru_cache name_cache;
19531db9f7cSAlexander Block
196521b6803SFilipe Manana /*
197521b6803SFilipe Manana * The inode we are currently processing. It's not NULL only when we
198521b6803SFilipe Manana * need to issue write commands for data extents from this inode.
199521b6803SFilipe Manana */
200521b6803SFilipe Manana struct inode *cur_inode;
2012131bcd3SLiu Bo struct file_ra_state ra;
202152555b3SFilipe Manana u64 page_cache_clear_start;
203152555b3SFilipe Manana bool clean_page_cache;
2042131bcd3SLiu Bo
2059f03740aSFilipe David Borba Manana /*
2069f03740aSFilipe David Borba Manana * We process inodes by their increasing order, so if before an
2079f03740aSFilipe David Borba Manana * incremental send we reverse the parent/child relationship of
2089f03740aSFilipe David Borba Manana * directories such that a directory with a lower inode number was
2099f03740aSFilipe David Borba Manana * the parent of a directory with a higher inode number, and the one
2109f03740aSFilipe David Borba Manana * becoming the new parent got renamed too, we can't rename/move the
2119f03740aSFilipe David Borba Manana * directory with lower inode number when we finish processing it - we
2129f03740aSFilipe David Borba Manana * must process the directory with higher inode number first, then
2139f03740aSFilipe David Borba Manana * rename/move it and then rename/move the directory with lower inode
2149f03740aSFilipe David Borba Manana * number. Example follows.
2159f03740aSFilipe David Borba Manana *
2169f03740aSFilipe David Borba Manana * Tree state when the first send was performed:
2179f03740aSFilipe David Borba Manana *
2189f03740aSFilipe David Borba Manana * .
2199f03740aSFilipe David Borba Manana * |-- a (ino 257)
2209f03740aSFilipe David Borba Manana * |-- b (ino 258)
2219f03740aSFilipe David Borba Manana * |
2229f03740aSFilipe David Borba Manana * |
2239f03740aSFilipe David Borba Manana * |-- c (ino 259)
2249f03740aSFilipe David Borba Manana * | |-- d (ino 260)
2259f03740aSFilipe David Borba Manana * |
2269f03740aSFilipe David Borba Manana * |-- c2 (ino 261)
2279f03740aSFilipe David Borba Manana *
2289f03740aSFilipe David Borba Manana * Tree state when the second (incremental) send is performed:
2299f03740aSFilipe David Borba Manana *
2309f03740aSFilipe David Borba Manana * .
2319f03740aSFilipe David Borba Manana * |-- a (ino 257)
2329f03740aSFilipe David Borba Manana * |-- b (ino 258)
2339f03740aSFilipe David Borba Manana * |-- c2 (ino 261)
2349f03740aSFilipe David Borba Manana * |-- d2 (ino 260)
2359f03740aSFilipe David Borba Manana * |-- cc (ino 259)
2369f03740aSFilipe David Borba Manana *
2379f03740aSFilipe David Borba Manana * The sequence of steps that lead to the second state was:
2389f03740aSFilipe David Borba Manana *
2399f03740aSFilipe David Borba Manana * mv /a/b/c/d /a/b/c2/d2
2409f03740aSFilipe David Borba Manana * mv /a/b/c /a/b/c2/d2/cc
2419f03740aSFilipe David Borba Manana *
2429f03740aSFilipe David Borba Manana * "c" has lower inode number, but we can't move it (2nd mv operation)
2439f03740aSFilipe David Borba Manana * before we move "d", which has higher inode number.
2449f03740aSFilipe David Borba Manana *
2459f03740aSFilipe David Borba Manana * So we just memorize which move/rename operations must be performed
2469f03740aSFilipe David Borba Manana * later when their respective parent is processed and moved/renamed.
2479f03740aSFilipe David Borba Manana */
2489f03740aSFilipe David Borba Manana
2499f03740aSFilipe David Borba Manana /* Indexed by parent directory inode number. */
2509f03740aSFilipe David Borba Manana struct rb_root pending_dir_moves;
2519f03740aSFilipe David Borba Manana
2529f03740aSFilipe David Borba Manana /*
2539f03740aSFilipe David Borba Manana * Reverse index, indexed by the inode number of a directory that
2549f03740aSFilipe David Borba Manana * is waiting for the move/rename of its immediate parent before its
2559f03740aSFilipe David Borba Manana * own move/rename can be performed.
2569f03740aSFilipe David Borba Manana */
2579f03740aSFilipe David Borba Manana struct rb_root waiting_dir_moves;
2589dc44214SFilipe Manana
2599dc44214SFilipe Manana /*
2609dc44214SFilipe Manana * A directory that is going to be rm'ed might have a child directory
2619dc44214SFilipe Manana * which is in the pending directory moves index above. In this case,
2629dc44214SFilipe Manana * the directory can only be removed after the move/rename of its child
2639dc44214SFilipe Manana * is performed. Example:
2649dc44214SFilipe Manana *
2659dc44214SFilipe Manana * Parent snapshot:
2669dc44214SFilipe Manana *
2679dc44214SFilipe Manana * . (ino 256)
2689dc44214SFilipe Manana * |-- a/ (ino 257)
2699dc44214SFilipe Manana * |-- b/ (ino 258)
2709dc44214SFilipe Manana * |-- c/ (ino 259)
2719dc44214SFilipe Manana * | |-- x/ (ino 260)
2729dc44214SFilipe Manana * |
2739dc44214SFilipe Manana * |-- y/ (ino 261)
2749dc44214SFilipe Manana *
2759dc44214SFilipe Manana * Send snapshot:
2769dc44214SFilipe Manana *
2779dc44214SFilipe Manana * . (ino 256)
2789dc44214SFilipe Manana * |-- a/ (ino 257)
2799dc44214SFilipe Manana * |-- b/ (ino 258)
2809dc44214SFilipe Manana * |-- YY/ (ino 261)
2819dc44214SFilipe Manana * |-- x/ (ino 260)
2829dc44214SFilipe Manana *
2839dc44214SFilipe Manana * Sequence of steps that lead to the send snapshot:
2849dc44214SFilipe Manana * rm -f /a/b/c/foo.txt
2859dc44214SFilipe Manana * mv /a/b/y /a/b/YY
2869dc44214SFilipe Manana * mv /a/b/c/x /a/b/YY
2879dc44214SFilipe Manana * rmdir /a/b/c
2889dc44214SFilipe Manana *
2899dc44214SFilipe Manana * When the child is processed, its move/rename is delayed until its
2909dc44214SFilipe Manana * parent is processed (as explained above), but all other operations
2919dc44214SFilipe Manana * like update utimes, chown, chgrp, etc, are performed and the paths
2929dc44214SFilipe Manana * that it uses for those operations must use the orphanized name of
2939dc44214SFilipe Manana * its parent (the directory we're going to rm later), so we need to
2949dc44214SFilipe Manana * memorize that name.
2959dc44214SFilipe Manana *
2969dc44214SFilipe Manana * Indexed by the inode number of the directory to be deleted.
2979dc44214SFilipe Manana */
2989dc44214SFilipe Manana struct rb_root orphan_dirs;
2993aa5bd36SBingJing Chang
3003aa5bd36SBingJing Chang struct rb_root rbtree_new_refs;
3013aa5bd36SBingJing Chang struct rb_root rbtree_deleted_refs;
30266d04209SFilipe Manana
30390b90d4aSFilipe Manana struct btrfs_lru_cache backref_cache;
30490b90d4aSFilipe Manana u64 backref_cache_last_reloc_trans;
305e8a7f49dSFilipe Manana
306e8a7f49dSFilipe Manana struct btrfs_lru_cache dir_created_cache;
3073e49363bSFilipe Manana struct btrfs_lru_cache dir_utimes_cache;
3089f03740aSFilipe David Borba Manana };
3099f03740aSFilipe David Borba Manana
3109f03740aSFilipe David Borba Manana struct pending_dir_move {
3119f03740aSFilipe David Borba Manana struct rb_node node;
3129f03740aSFilipe David Borba Manana struct list_head list;
3139f03740aSFilipe David Borba Manana u64 parent_ino;
3149f03740aSFilipe David Borba Manana u64 ino;
3159f03740aSFilipe David Borba Manana u64 gen;
3169f03740aSFilipe David Borba Manana struct list_head update_refs;
3179f03740aSFilipe David Borba Manana };
3189f03740aSFilipe David Borba Manana
3199f03740aSFilipe David Borba Manana struct waiting_dir_move {
3209f03740aSFilipe David Borba Manana struct rb_node node;
3219f03740aSFilipe David Borba Manana u64 ino;
3229dc44214SFilipe Manana /*
3239dc44214SFilipe Manana * There might be some directory that could not be removed because it
3249dc44214SFilipe Manana * was waiting for this directory inode to be moved first. Therefore
3259dc44214SFilipe Manana * after this directory is moved, we can try to rmdir the ino rmdir_ino.
3269dc44214SFilipe Manana */
3279dc44214SFilipe Manana u64 rmdir_ino;
3280b3f407eSFilipe Manana u64 rmdir_gen;
3298b191a68SFilipe Manana bool orphanized;
3309dc44214SFilipe Manana };
3319dc44214SFilipe Manana
3329dc44214SFilipe Manana struct orphan_dir_info {
3339dc44214SFilipe Manana struct rb_node node;
3349dc44214SFilipe Manana u64 ino;
3359dc44214SFilipe Manana u64 gen;
3360f96f517SRobbie Ko u64 last_dir_index_offset;
337474e4761SFilipe Manana u64 dir_high_seq_ino;
33831db9f7cSAlexander Block };
33931db9f7cSAlexander Block
34031db9f7cSAlexander Block struct name_cache_entry {
3417e0926feSAlexander Block /*
342c48545deSFilipe Manana * The key in the entry is an inode number, and the generation matches
343c48545deSFilipe Manana * the inode's generation.
3447e0926feSAlexander Block */
345c48545deSFilipe Manana struct btrfs_lru_cache_entry entry;
34631db9f7cSAlexander Block u64 parent_ino;
34731db9f7cSAlexander Block u64 parent_gen;
34831db9f7cSAlexander Block int ret;
34931db9f7cSAlexander Block int need_later_update;
35031db9f7cSAlexander Block int name_len;
35131db9f7cSAlexander Block char name[];
35231db9f7cSAlexander Block };
35331db9f7cSAlexander Block
354c48545deSFilipe Manana /* See the comment at lru_cache.h about struct btrfs_lru_cache_entry. */
355c48545deSFilipe Manana static_assert(offsetof(struct name_cache_entry, entry) == 0);
356c48545deSFilipe Manana
35718d0f5c6SDavid Sterba #define ADVANCE 1
35818d0f5c6SDavid Sterba #define ADVANCE_ONLY_NEXT -1
35918d0f5c6SDavid Sterba
36018d0f5c6SDavid Sterba enum btrfs_compare_tree_result {
36118d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_NEW,
36218d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_DELETED,
36318d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_CHANGED,
36418d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_SAME,
36518d0f5c6SDavid Sterba };
36618d0f5c6SDavid Sterba
367e67c718bSDavid Sterba __cold
inconsistent_snapshot_error(struct send_ctx * sctx,enum btrfs_compare_tree_result result,const char * what)36895155585SFilipe Manana static void inconsistent_snapshot_error(struct send_ctx *sctx,
36995155585SFilipe Manana enum btrfs_compare_tree_result result,
37095155585SFilipe Manana const char *what)
37195155585SFilipe Manana {
37295155585SFilipe Manana const char *result_string;
37395155585SFilipe Manana
37495155585SFilipe Manana switch (result) {
37595155585SFilipe Manana case BTRFS_COMPARE_TREE_NEW:
37695155585SFilipe Manana result_string = "new";
37795155585SFilipe Manana break;
37895155585SFilipe Manana case BTRFS_COMPARE_TREE_DELETED:
37995155585SFilipe Manana result_string = "deleted";
38095155585SFilipe Manana break;
38195155585SFilipe Manana case BTRFS_COMPARE_TREE_CHANGED:
38295155585SFilipe Manana result_string = "updated";
38395155585SFilipe Manana break;
38495155585SFilipe Manana case BTRFS_COMPARE_TREE_SAME:
38595155585SFilipe Manana ASSERT(0);
38695155585SFilipe Manana result_string = "unchanged";
38795155585SFilipe Manana break;
38895155585SFilipe Manana default:
38995155585SFilipe Manana ASSERT(0);
39095155585SFilipe Manana result_string = "unexpected";
39195155585SFilipe Manana }
39295155585SFilipe Manana
39395155585SFilipe Manana btrfs_err(sctx->send_root->fs_info,
39495155585SFilipe Manana "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
39595155585SFilipe Manana result_string, what, sctx->cmp_key->objectid,
39695155585SFilipe Manana sctx->send_root->root_key.objectid,
39795155585SFilipe Manana (sctx->parent_root ?
39895155585SFilipe Manana sctx->parent_root->root_key.objectid : 0));
39995155585SFilipe Manana }
40095155585SFilipe Manana
401e77fbf99SDavid Sterba __maybe_unused
proto_cmd_ok(const struct send_ctx * sctx,int cmd)402e77fbf99SDavid Sterba static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
403e77fbf99SDavid Sterba {
404e77fbf99SDavid Sterba switch (sctx->proto) {
40554cab6afSOmar Sandoval case 1: return cmd <= BTRFS_SEND_C_MAX_V1;
40654cab6afSOmar Sandoval case 2: return cmd <= BTRFS_SEND_C_MAX_V2;
407c86eab81SDavid Sterba case 3: return cmd <= BTRFS_SEND_C_MAX_V3;
408e77fbf99SDavid Sterba default: return false;
409e77fbf99SDavid Sterba }
410e77fbf99SDavid Sterba }
411e77fbf99SDavid Sterba
4129f03740aSFilipe David Borba Manana static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
4139f03740aSFilipe David Borba Manana
4149dc44214SFilipe Manana static struct waiting_dir_move *
4159dc44214SFilipe Manana get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
4169dc44214SFilipe Manana
4170b3f407eSFilipe Manana static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
4189dc44214SFilipe Manana
need_send_hole(struct send_ctx * sctx)41916e7549fSJosef Bacik static int need_send_hole(struct send_ctx *sctx)
42016e7549fSJosef Bacik {
42116e7549fSJosef Bacik return (sctx->parent_root && !sctx->cur_inode_new &&
42216e7549fSJosef Bacik !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
42316e7549fSJosef Bacik S_ISREG(sctx->cur_inode_mode));
42416e7549fSJosef Bacik }
42516e7549fSJosef Bacik
fs_path_reset(struct fs_path * p)42631db9f7cSAlexander Block static void fs_path_reset(struct fs_path *p)
42731db9f7cSAlexander Block {
42831db9f7cSAlexander Block if (p->reversed) {
42931db9f7cSAlexander Block p->start = p->buf + p->buf_len - 1;
43031db9f7cSAlexander Block p->end = p->start;
43131db9f7cSAlexander Block *p->start = 0;
43231db9f7cSAlexander Block } else {
43331db9f7cSAlexander Block p->start = p->buf;
43431db9f7cSAlexander Block p->end = p->start;
43531db9f7cSAlexander Block *p->start = 0;
43631db9f7cSAlexander Block }
43731db9f7cSAlexander Block }
43831db9f7cSAlexander Block
fs_path_alloc(void)439924794c9STsutomu Itoh static struct fs_path *fs_path_alloc(void)
44031db9f7cSAlexander Block {
44131db9f7cSAlexander Block struct fs_path *p;
44231db9f7cSAlexander Block
443e780b0d1SDavid Sterba p = kmalloc(sizeof(*p), GFP_KERNEL);
44431db9f7cSAlexander Block if (!p)
44531db9f7cSAlexander Block return NULL;
44631db9f7cSAlexander Block p->reversed = 0;
44731db9f7cSAlexander Block p->buf = p->inline_buf;
44831db9f7cSAlexander Block p->buf_len = FS_PATH_INLINE_SIZE;
44931db9f7cSAlexander Block fs_path_reset(p);
45031db9f7cSAlexander Block return p;
45131db9f7cSAlexander Block }
45231db9f7cSAlexander Block
fs_path_alloc_reversed(void)453924794c9STsutomu Itoh static struct fs_path *fs_path_alloc_reversed(void)
45431db9f7cSAlexander Block {
45531db9f7cSAlexander Block struct fs_path *p;
45631db9f7cSAlexander Block
457924794c9STsutomu Itoh p = fs_path_alloc();
45831db9f7cSAlexander Block if (!p)
45931db9f7cSAlexander Block return NULL;
46031db9f7cSAlexander Block p->reversed = 1;
46131db9f7cSAlexander Block fs_path_reset(p);
46231db9f7cSAlexander Block return p;
46331db9f7cSAlexander Block }
46431db9f7cSAlexander Block
fs_path_free(struct fs_path * p)465924794c9STsutomu Itoh static void fs_path_free(struct fs_path *p)
46631db9f7cSAlexander Block {
46731db9f7cSAlexander Block if (!p)
46831db9f7cSAlexander Block return;
469ace01050SDavid Sterba if (p->buf != p->inline_buf)
47031db9f7cSAlexander Block kfree(p->buf);
47131db9f7cSAlexander Block kfree(p);
47231db9f7cSAlexander Block }
47331db9f7cSAlexander Block
fs_path_len(struct fs_path * p)47431db9f7cSAlexander Block static int fs_path_len(struct fs_path *p)
47531db9f7cSAlexander Block {
47631db9f7cSAlexander Block return p->end - p->start;
47731db9f7cSAlexander Block }
47831db9f7cSAlexander Block
fs_path_ensure_buf(struct fs_path * p,int len)47931db9f7cSAlexander Block static int fs_path_ensure_buf(struct fs_path *p, int len)
48031db9f7cSAlexander Block {
48131db9f7cSAlexander Block char *tmp_buf;
48231db9f7cSAlexander Block int path_len;
48331db9f7cSAlexander Block int old_buf_len;
48431db9f7cSAlexander Block
48531db9f7cSAlexander Block len++;
48631db9f7cSAlexander Block
48731db9f7cSAlexander Block if (p->buf_len >= len)
48831db9f7cSAlexander Block return 0;
48931db9f7cSAlexander Block
490cfd4a535SChris Mason if (len > PATH_MAX) {
491cfd4a535SChris Mason WARN_ON(1);
492cfd4a535SChris Mason return -ENOMEM;
493cfd4a535SChris Mason }
494cfd4a535SChris Mason
4951b2782c8SDavid Sterba path_len = p->end - p->start;
4961b2782c8SDavid Sterba old_buf_len = p->buf_len;
4971b2782c8SDavid Sterba
498ace01050SDavid Sterba /*
499905889bcSKees Cook * Allocate to the next largest kmalloc bucket size, to let
500905889bcSKees Cook * the fast path happen most of the time.
501905889bcSKees Cook */
502905889bcSKees Cook len = kmalloc_size_roundup(len);
503905889bcSKees Cook /*
504ace01050SDavid Sterba * First time the inline_buf does not suffice
505ace01050SDavid Sterba */
50601a9a8a9SFilipe Manana if (p->buf == p->inline_buf) {
507e780b0d1SDavid Sterba tmp_buf = kmalloc(len, GFP_KERNEL);
50801a9a8a9SFilipe Manana if (tmp_buf)
50901a9a8a9SFilipe Manana memcpy(tmp_buf, p->buf, old_buf_len);
51001a9a8a9SFilipe Manana } else {
511e780b0d1SDavid Sterba tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
51201a9a8a9SFilipe Manana }
5139c9ca00bSDavid Sterba if (!tmp_buf)
514ace01050SDavid Sterba return -ENOMEM;
5159c9ca00bSDavid Sterba p->buf = tmp_buf;
516905889bcSKees Cook p->buf_len = len;
517ace01050SDavid Sterba
51831db9f7cSAlexander Block if (p->reversed) {
51931db9f7cSAlexander Block tmp_buf = p->buf + old_buf_len - path_len - 1;
52031db9f7cSAlexander Block p->end = p->buf + p->buf_len - 1;
52131db9f7cSAlexander Block p->start = p->end - path_len;
52231db9f7cSAlexander Block memmove(p->start, tmp_buf, path_len + 1);
52331db9f7cSAlexander Block } else {
52431db9f7cSAlexander Block p->start = p->buf;
52531db9f7cSAlexander Block p->end = p->start + path_len;
52631db9f7cSAlexander Block }
52731db9f7cSAlexander Block return 0;
52831db9f7cSAlexander Block }
52931db9f7cSAlexander Block
fs_path_prepare_for_add(struct fs_path * p,int name_len,char ** prepared)530b23ab57dSDavid Sterba static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
531b23ab57dSDavid Sterba char **prepared)
53231db9f7cSAlexander Block {
53331db9f7cSAlexander Block int ret;
53431db9f7cSAlexander Block int new_len;
53531db9f7cSAlexander Block
53631db9f7cSAlexander Block new_len = p->end - p->start + name_len;
53731db9f7cSAlexander Block if (p->start != p->end)
53831db9f7cSAlexander Block new_len++;
53931db9f7cSAlexander Block ret = fs_path_ensure_buf(p, new_len);
54031db9f7cSAlexander Block if (ret < 0)
54131db9f7cSAlexander Block goto out;
54231db9f7cSAlexander Block
54331db9f7cSAlexander Block if (p->reversed) {
54431db9f7cSAlexander Block if (p->start != p->end)
54531db9f7cSAlexander Block *--p->start = '/';
54631db9f7cSAlexander Block p->start -= name_len;
547b23ab57dSDavid Sterba *prepared = p->start;
54831db9f7cSAlexander Block } else {
54931db9f7cSAlexander Block if (p->start != p->end)
55031db9f7cSAlexander Block *p->end++ = '/';
551b23ab57dSDavid Sterba *prepared = p->end;
55231db9f7cSAlexander Block p->end += name_len;
55331db9f7cSAlexander Block *p->end = 0;
55431db9f7cSAlexander Block }
55531db9f7cSAlexander Block
55631db9f7cSAlexander Block out:
55731db9f7cSAlexander Block return ret;
55831db9f7cSAlexander Block }
55931db9f7cSAlexander Block
fs_path_add(struct fs_path * p,const char * name,int name_len)56031db9f7cSAlexander Block static int fs_path_add(struct fs_path *p, const char *name, int name_len)
56131db9f7cSAlexander Block {
56231db9f7cSAlexander Block int ret;
563b23ab57dSDavid Sterba char *prepared;
56431db9f7cSAlexander Block
565b23ab57dSDavid Sterba ret = fs_path_prepare_for_add(p, name_len, &prepared);
56631db9f7cSAlexander Block if (ret < 0)
56731db9f7cSAlexander Block goto out;
568b23ab57dSDavid Sterba memcpy(prepared, name, name_len);
56931db9f7cSAlexander Block
57031db9f7cSAlexander Block out:
57131db9f7cSAlexander Block return ret;
57231db9f7cSAlexander Block }
57331db9f7cSAlexander Block
fs_path_add_path(struct fs_path * p,struct fs_path * p2)57431db9f7cSAlexander Block static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
57531db9f7cSAlexander Block {
57631db9f7cSAlexander Block int ret;
577b23ab57dSDavid Sterba char *prepared;
57831db9f7cSAlexander Block
579b23ab57dSDavid Sterba ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
58031db9f7cSAlexander Block if (ret < 0)
58131db9f7cSAlexander Block goto out;
582b23ab57dSDavid Sterba memcpy(prepared, p2->start, p2->end - p2->start);
58331db9f7cSAlexander Block
58431db9f7cSAlexander Block out:
58531db9f7cSAlexander Block return ret;
58631db9f7cSAlexander Block }
58731db9f7cSAlexander Block
fs_path_add_from_extent_buffer(struct fs_path * p,struct extent_buffer * eb,unsigned long off,int len)58831db9f7cSAlexander Block static int fs_path_add_from_extent_buffer(struct fs_path *p,
58931db9f7cSAlexander Block struct extent_buffer *eb,
59031db9f7cSAlexander Block unsigned long off, int len)
59131db9f7cSAlexander Block {
59231db9f7cSAlexander Block int ret;
593b23ab57dSDavid Sterba char *prepared;
59431db9f7cSAlexander Block
595b23ab57dSDavid Sterba ret = fs_path_prepare_for_add(p, len, &prepared);
59631db9f7cSAlexander Block if (ret < 0)
59731db9f7cSAlexander Block goto out;
59831db9f7cSAlexander Block
599b23ab57dSDavid Sterba read_extent_buffer(eb, prepared, off, len);
60031db9f7cSAlexander Block
60131db9f7cSAlexander Block out:
60231db9f7cSAlexander Block return ret;
60331db9f7cSAlexander Block }
60431db9f7cSAlexander Block
fs_path_copy(struct fs_path * p,struct fs_path * from)60531db9f7cSAlexander Block static int fs_path_copy(struct fs_path *p, struct fs_path *from)
60631db9f7cSAlexander Block {
60731db9f7cSAlexander Block p->reversed = from->reversed;
60831db9f7cSAlexander Block fs_path_reset(p);
60931db9f7cSAlexander Block
6100292ecf1SMinghao Chi return fs_path_add_path(p, from);
61131db9f7cSAlexander Block }
61231db9f7cSAlexander Block
fs_path_unreverse(struct fs_path * p)61331db9f7cSAlexander Block static void fs_path_unreverse(struct fs_path *p)
61431db9f7cSAlexander Block {
61531db9f7cSAlexander Block char *tmp;
61631db9f7cSAlexander Block int len;
61731db9f7cSAlexander Block
61831db9f7cSAlexander Block if (!p->reversed)
61931db9f7cSAlexander Block return;
62031db9f7cSAlexander Block
62131db9f7cSAlexander Block tmp = p->start;
62231db9f7cSAlexander Block len = p->end - p->start;
62331db9f7cSAlexander Block p->start = p->buf;
62431db9f7cSAlexander Block p->end = p->start + len;
62531db9f7cSAlexander Block memmove(p->start, tmp, len + 1);
62631db9f7cSAlexander Block p->reversed = 0;
62731db9f7cSAlexander Block }
62831db9f7cSAlexander Block
alloc_path_for_send(void)62931db9f7cSAlexander Block static struct btrfs_path *alloc_path_for_send(void)
63031db9f7cSAlexander Block {
63131db9f7cSAlexander Block struct btrfs_path *path;
63231db9f7cSAlexander Block
63331db9f7cSAlexander Block path = btrfs_alloc_path();
63431db9f7cSAlexander Block if (!path)
63531db9f7cSAlexander Block return NULL;
63631db9f7cSAlexander Block path->search_commit_root = 1;
63731db9f7cSAlexander Block path->skip_locking = 1;
6383f8a18ccSJosef Bacik path->need_commit_sem = 1;
63931db9f7cSAlexander Block return path;
64031db9f7cSAlexander Block }
64131db9f7cSAlexander Block
write_buf(struct file * filp,const void * buf,u32 len,loff_t * off)64248a3b636SEric Sandeen static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
64331db9f7cSAlexander Block {
64431db9f7cSAlexander Block int ret;
64531db9f7cSAlexander Block u32 pos = 0;
64631db9f7cSAlexander Block
64731db9f7cSAlexander Block while (pos < len) {
6488e93157bSChristoph Hellwig ret = kernel_write(filp, buf + pos, len - pos, off);
64931db9f7cSAlexander Block if (ret < 0)
6508e93157bSChristoph Hellwig return ret;
651cec3dad9SDavid Sterba if (ret == 0)
6528e93157bSChristoph Hellwig return -EIO;
65331db9f7cSAlexander Block pos += ret;
65431db9f7cSAlexander Block }
65531db9f7cSAlexander Block
6568e93157bSChristoph Hellwig return 0;
65731db9f7cSAlexander Block }
65831db9f7cSAlexander Block
tlv_put(struct send_ctx * sctx,u16 attr,const void * data,int len)65931db9f7cSAlexander Block static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
66031db9f7cSAlexander Block {
66131db9f7cSAlexander Block struct btrfs_tlv_header *hdr;
66231db9f7cSAlexander Block int total_len = sizeof(*hdr) + len;
66331db9f7cSAlexander Block int left = sctx->send_max_size - sctx->send_size;
66431db9f7cSAlexander Block
665356bbbb6SOmar Sandoval if (WARN_ON_ONCE(sctx->put_data))
666356bbbb6SOmar Sandoval return -EINVAL;
667356bbbb6SOmar Sandoval
66831db9f7cSAlexander Block if (unlikely(left < total_len))
66931db9f7cSAlexander Block return -EOVERFLOW;
67031db9f7cSAlexander Block
67131db9f7cSAlexander Block hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
672e2f896b3SDavid Sterba put_unaligned_le16(attr, &hdr->tlv_type);
673e2f896b3SDavid Sterba put_unaligned_le16(len, &hdr->tlv_len);
67431db9f7cSAlexander Block memcpy(hdr + 1, data, len);
67531db9f7cSAlexander Block sctx->send_size += total_len;
67631db9f7cSAlexander Block
67731db9f7cSAlexander Block return 0;
67831db9f7cSAlexander Block }
67931db9f7cSAlexander Block
68095bc79d5SDavid Sterba #define TLV_PUT_DEFINE_INT(bits) \
68195bc79d5SDavid Sterba static int tlv_put_u##bits(struct send_ctx *sctx, \
68295bc79d5SDavid Sterba u##bits attr, u##bits value) \
68395bc79d5SDavid Sterba { \
68495bc79d5SDavid Sterba __le##bits __tmp = cpu_to_le##bits(value); \
68595bc79d5SDavid Sterba return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
68631db9f7cSAlexander Block }
68731db9f7cSAlexander Block
68838622010SBoris Burkov TLV_PUT_DEFINE_INT(8)
6893ea4dc5bSOmar Sandoval TLV_PUT_DEFINE_INT(32)
69095bc79d5SDavid Sterba TLV_PUT_DEFINE_INT(64)
69131db9f7cSAlexander Block
tlv_put_string(struct send_ctx * sctx,u16 attr,const char * str,int len)69231db9f7cSAlexander Block static int tlv_put_string(struct send_ctx *sctx, u16 attr,
69331db9f7cSAlexander Block const char *str, int len)
69431db9f7cSAlexander Block {
69531db9f7cSAlexander Block if (len == -1)
69631db9f7cSAlexander Block len = strlen(str);
69731db9f7cSAlexander Block return tlv_put(sctx, attr, str, len);
69831db9f7cSAlexander Block }
69931db9f7cSAlexander Block
tlv_put_uuid(struct send_ctx * sctx,u16 attr,const u8 * uuid)70031db9f7cSAlexander Block static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
70131db9f7cSAlexander Block const u8 *uuid)
70231db9f7cSAlexander Block {
70331db9f7cSAlexander Block return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
70431db9f7cSAlexander Block }
70531db9f7cSAlexander Block
tlv_put_btrfs_timespec(struct send_ctx * sctx,u16 attr,struct extent_buffer * eb,struct btrfs_timespec * ts)70631db9f7cSAlexander Block static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
70731db9f7cSAlexander Block struct extent_buffer *eb,
70831db9f7cSAlexander Block struct btrfs_timespec *ts)
70931db9f7cSAlexander Block {
71031db9f7cSAlexander Block struct btrfs_timespec bts;
71131db9f7cSAlexander Block read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
71231db9f7cSAlexander Block return tlv_put(sctx, attr, &bts, sizeof(bts));
71331db9f7cSAlexander Block }
71431db9f7cSAlexander Block
71531db9f7cSAlexander Block
716895a72beSLiu Bo #define TLV_PUT(sctx, attrtype, data, attrlen) \
71731db9f7cSAlexander Block do { \
718895a72beSLiu Bo ret = tlv_put(sctx, attrtype, data, attrlen); \
71931db9f7cSAlexander Block if (ret < 0) \
72031db9f7cSAlexander Block goto tlv_put_failure; \
72131db9f7cSAlexander Block } while (0)
72231db9f7cSAlexander Block
72331db9f7cSAlexander Block #define TLV_PUT_INT(sctx, attrtype, bits, value) \
72431db9f7cSAlexander Block do { \
72531db9f7cSAlexander Block ret = tlv_put_u##bits(sctx, attrtype, value); \
72631db9f7cSAlexander Block if (ret < 0) \
72731db9f7cSAlexander Block goto tlv_put_failure; \
72831db9f7cSAlexander Block } while (0)
72931db9f7cSAlexander Block
73031db9f7cSAlexander Block #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
73131db9f7cSAlexander Block #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
73231db9f7cSAlexander Block #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
73331db9f7cSAlexander Block #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
73431db9f7cSAlexander Block #define TLV_PUT_STRING(sctx, attrtype, str, len) \
73531db9f7cSAlexander Block do { \
73631db9f7cSAlexander Block ret = tlv_put_string(sctx, attrtype, str, len); \
73731db9f7cSAlexander Block if (ret < 0) \
73831db9f7cSAlexander Block goto tlv_put_failure; \
73931db9f7cSAlexander Block } while (0)
74031db9f7cSAlexander Block #define TLV_PUT_PATH(sctx, attrtype, p) \
74131db9f7cSAlexander Block do { \
74231db9f7cSAlexander Block ret = tlv_put_string(sctx, attrtype, p->start, \
74331db9f7cSAlexander Block p->end - p->start); \
74431db9f7cSAlexander Block if (ret < 0) \
74531db9f7cSAlexander Block goto tlv_put_failure; \
74631db9f7cSAlexander Block } while(0)
74731db9f7cSAlexander Block #define TLV_PUT_UUID(sctx, attrtype, uuid) \
74831db9f7cSAlexander Block do { \
74931db9f7cSAlexander Block ret = tlv_put_uuid(sctx, attrtype, uuid); \
75031db9f7cSAlexander Block if (ret < 0) \
75131db9f7cSAlexander Block goto tlv_put_failure; \
75231db9f7cSAlexander Block } while (0)
75331db9f7cSAlexander Block #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
75431db9f7cSAlexander Block do { \
75531db9f7cSAlexander Block ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
75631db9f7cSAlexander Block if (ret < 0) \
75731db9f7cSAlexander Block goto tlv_put_failure; \
75831db9f7cSAlexander Block } while (0)
75931db9f7cSAlexander Block
send_header(struct send_ctx * sctx)76031db9f7cSAlexander Block static int send_header(struct send_ctx *sctx)
76131db9f7cSAlexander Block {
76231db9f7cSAlexander Block struct btrfs_stream_header hdr;
76331db9f7cSAlexander Block
76431db9f7cSAlexander Block strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
765d6815592SOmar Sandoval hdr.version = cpu_to_le32(sctx->proto);
7661bcea355SAnand Jain return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
7671bcea355SAnand Jain &sctx->send_off);
76831db9f7cSAlexander Block }
76931db9f7cSAlexander Block
77031db9f7cSAlexander Block /*
77131db9f7cSAlexander Block * For each command/item we want to send to userspace, we call this function.
77231db9f7cSAlexander Block */
begin_cmd(struct send_ctx * sctx,int cmd)77331db9f7cSAlexander Block static int begin_cmd(struct send_ctx *sctx, int cmd)
77431db9f7cSAlexander Block {
77531db9f7cSAlexander Block struct btrfs_cmd_header *hdr;
77631db9f7cSAlexander Block
777fae7f21cSDulshani Gunawardhana if (WARN_ON(!sctx->send_buf))
77831db9f7cSAlexander Block return -EINVAL;
77931db9f7cSAlexander Block
78031db9f7cSAlexander Block BUG_ON(sctx->send_size);
78131db9f7cSAlexander Block
78231db9f7cSAlexander Block sctx->send_size += sizeof(*hdr);
78331db9f7cSAlexander Block hdr = (struct btrfs_cmd_header *)sctx->send_buf;
784e2f896b3SDavid Sterba put_unaligned_le16(cmd, &hdr->cmd);
78531db9f7cSAlexander Block
78631db9f7cSAlexander Block return 0;
78731db9f7cSAlexander Block }
78831db9f7cSAlexander Block
send_cmd(struct send_ctx * sctx)78931db9f7cSAlexander Block static int send_cmd(struct send_ctx *sctx)
79031db9f7cSAlexander Block {
79131db9f7cSAlexander Block int ret;
79231db9f7cSAlexander Block struct btrfs_cmd_header *hdr;
79331db9f7cSAlexander Block u32 crc;
79431db9f7cSAlexander Block
79531db9f7cSAlexander Block hdr = (struct btrfs_cmd_header *)sctx->send_buf;
796e2f896b3SDavid Sterba put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
797e2f896b3SDavid Sterba put_unaligned_le32(0, &hdr->crc);
79831db9f7cSAlexander Block
79965019df8SJohannes Thumshirn crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
800e2f896b3SDavid Sterba put_unaligned_le32(crc, &hdr->crc);
80131db9f7cSAlexander Block
8021bcea355SAnand Jain ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
8031bcea355SAnand Jain &sctx->send_off);
80431db9f7cSAlexander Block
80531db9f7cSAlexander Block sctx->send_size = 0;
806356bbbb6SOmar Sandoval sctx->put_data = false;
80731db9f7cSAlexander Block
80831db9f7cSAlexander Block return ret;
80931db9f7cSAlexander Block }
81031db9f7cSAlexander Block
81131db9f7cSAlexander Block /*
81231db9f7cSAlexander Block * Sends a move instruction to user space
81331db9f7cSAlexander Block */
send_rename(struct send_ctx * sctx,struct fs_path * from,struct fs_path * to)81431db9f7cSAlexander Block static int send_rename(struct send_ctx *sctx,
81531db9f7cSAlexander Block struct fs_path *from, struct fs_path *to)
81631db9f7cSAlexander Block {
81704ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
81831db9f7cSAlexander Block int ret;
81931db9f7cSAlexander Block
82004ab956eSJeff Mahoney btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
82131db9f7cSAlexander Block
82231db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
82331db9f7cSAlexander Block if (ret < 0)
82431db9f7cSAlexander Block goto out;
82531db9f7cSAlexander Block
82631db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
82731db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
82831db9f7cSAlexander Block
82931db9f7cSAlexander Block ret = send_cmd(sctx);
83031db9f7cSAlexander Block
83131db9f7cSAlexander Block tlv_put_failure:
83231db9f7cSAlexander Block out:
83331db9f7cSAlexander Block return ret;
83431db9f7cSAlexander Block }
83531db9f7cSAlexander Block
83631db9f7cSAlexander Block /*
83731db9f7cSAlexander Block * Sends a link instruction to user space
83831db9f7cSAlexander Block */
send_link(struct send_ctx * sctx,struct fs_path * path,struct fs_path * lnk)83931db9f7cSAlexander Block static int send_link(struct send_ctx *sctx,
84031db9f7cSAlexander Block struct fs_path *path, struct fs_path *lnk)
84131db9f7cSAlexander Block {
84204ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
84331db9f7cSAlexander Block int ret;
84431db9f7cSAlexander Block
84504ab956eSJeff Mahoney btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
84631db9f7cSAlexander Block
84731db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
84831db9f7cSAlexander Block if (ret < 0)
84931db9f7cSAlexander Block goto out;
85031db9f7cSAlexander Block
85131db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
85231db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
85331db9f7cSAlexander Block
85431db9f7cSAlexander Block ret = send_cmd(sctx);
85531db9f7cSAlexander Block
85631db9f7cSAlexander Block tlv_put_failure:
85731db9f7cSAlexander Block out:
85831db9f7cSAlexander Block return ret;
85931db9f7cSAlexander Block }
86031db9f7cSAlexander Block
86131db9f7cSAlexander Block /*
86231db9f7cSAlexander Block * Sends an unlink instruction to user space
86331db9f7cSAlexander Block */
send_unlink(struct send_ctx * sctx,struct fs_path * path)86431db9f7cSAlexander Block static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
86531db9f7cSAlexander Block {
86604ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
86731db9f7cSAlexander Block int ret;
86831db9f7cSAlexander Block
86904ab956eSJeff Mahoney btrfs_debug(fs_info, "send_unlink %s", path->start);
87031db9f7cSAlexander Block
87131db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
87231db9f7cSAlexander Block if (ret < 0)
87331db9f7cSAlexander Block goto out;
87431db9f7cSAlexander Block
87531db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
87631db9f7cSAlexander Block
87731db9f7cSAlexander Block ret = send_cmd(sctx);
87831db9f7cSAlexander Block
87931db9f7cSAlexander Block tlv_put_failure:
88031db9f7cSAlexander Block out:
88131db9f7cSAlexander Block return ret;
88231db9f7cSAlexander Block }
88331db9f7cSAlexander Block
88431db9f7cSAlexander Block /*
88531db9f7cSAlexander Block * Sends a rmdir instruction to user space
88631db9f7cSAlexander Block */
send_rmdir(struct send_ctx * sctx,struct fs_path * path)88731db9f7cSAlexander Block static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
88831db9f7cSAlexander Block {
88904ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
89031db9f7cSAlexander Block int ret;
89131db9f7cSAlexander Block
89204ab956eSJeff Mahoney btrfs_debug(fs_info, "send_rmdir %s", path->start);
89331db9f7cSAlexander Block
89431db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
89531db9f7cSAlexander Block if (ret < 0)
89631db9f7cSAlexander Block goto out;
89731db9f7cSAlexander Block
89831db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
89931db9f7cSAlexander Block
90031db9f7cSAlexander Block ret = send_cmd(sctx);
90131db9f7cSAlexander Block
90231db9f7cSAlexander Block tlv_put_failure:
90331db9f7cSAlexander Block out:
90431db9f7cSAlexander Block return ret;
90531db9f7cSAlexander Block }
90631db9f7cSAlexander Block
9077e93f6dcSBingJing Chang struct btrfs_inode_info {
9087e93f6dcSBingJing Chang u64 size;
9097e93f6dcSBingJing Chang u64 gen;
9107e93f6dcSBingJing Chang u64 mode;
9117e93f6dcSBingJing Chang u64 uid;
9127e93f6dcSBingJing Chang u64 gid;
9137e93f6dcSBingJing Chang u64 rdev;
9147e93f6dcSBingJing Chang u64 fileattr;
9159ed0a72eSBingJing Chang u64 nlink;
9167e93f6dcSBingJing Chang };
9177e93f6dcSBingJing Chang
91831db9f7cSAlexander Block /*
91931db9f7cSAlexander Block * Helper function to retrieve some fields from an inode item.
92031db9f7cSAlexander Block */
get_inode_info(struct btrfs_root * root,u64 ino,struct btrfs_inode_info * info)9217e93f6dcSBingJing Chang static int get_inode_info(struct btrfs_root *root, u64 ino,
9227e93f6dcSBingJing Chang struct btrfs_inode_info *info)
92331db9f7cSAlexander Block {
92431db9f7cSAlexander Block int ret;
9257e93f6dcSBingJing Chang struct btrfs_path *path;
92631db9f7cSAlexander Block struct btrfs_inode_item *ii;
92731db9f7cSAlexander Block struct btrfs_key key;
92831db9f7cSAlexander Block
9297e93f6dcSBingJing Chang path = alloc_path_for_send();
9307e93f6dcSBingJing Chang if (!path)
9317e93f6dcSBingJing Chang return -ENOMEM;
9327e93f6dcSBingJing Chang
93331db9f7cSAlexander Block key.objectid = ino;
93431db9f7cSAlexander Block key.type = BTRFS_INODE_ITEM_KEY;
93531db9f7cSAlexander Block key.offset = 0;
93631db9f7cSAlexander Block ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
93731db9f7cSAlexander Block if (ret) {
9383f8a18ccSJosef Bacik if (ret > 0)
93931db9f7cSAlexander Block ret = -ENOENT;
9407e93f6dcSBingJing Chang goto out;
94131db9f7cSAlexander Block }
94231db9f7cSAlexander Block
9437e93f6dcSBingJing Chang if (!info)
9447e93f6dcSBingJing Chang goto out;
9457e93f6dcSBingJing Chang
94631db9f7cSAlexander Block ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
94731db9f7cSAlexander Block struct btrfs_inode_item);
9487e93f6dcSBingJing Chang info->size = btrfs_inode_size(path->nodes[0], ii);
9497e93f6dcSBingJing Chang info->gen = btrfs_inode_generation(path->nodes[0], ii);
9507e93f6dcSBingJing Chang info->mode = btrfs_inode_mode(path->nodes[0], ii);
9517e93f6dcSBingJing Chang info->uid = btrfs_inode_uid(path->nodes[0], ii);
9527e93f6dcSBingJing Chang info->gid = btrfs_inode_gid(path->nodes[0], ii);
9537e93f6dcSBingJing Chang info->rdev = btrfs_inode_rdev(path->nodes[0], ii);
9549ed0a72eSBingJing Chang info->nlink = btrfs_inode_nlink(path->nodes[0], ii);
95548247359SDavid Sterba /*
95648247359SDavid Sterba * Transfer the unchanged u64 value of btrfs_inode_item::flags, that's
95748247359SDavid Sterba * otherwise logically split to 32/32 parts.
95848247359SDavid Sterba */
9597e93f6dcSBingJing Chang info->fileattr = btrfs_inode_flags(path->nodes[0], ii);
96031db9f7cSAlexander Block
9617e93f6dcSBingJing Chang out:
9627e93f6dcSBingJing Chang btrfs_free_path(path);
9633f8a18ccSJosef Bacik return ret;
9643f8a18ccSJosef Bacik }
9653f8a18ccSJosef Bacik
get_inode_gen(struct btrfs_root * root,u64 ino,u64 * gen)9667e93f6dcSBingJing Chang static int get_inode_gen(struct btrfs_root *root, u64 ino, u64 *gen)
9673f8a18ccSJosef Bacik {
9683f8a18ccSJosef Bacik int ret;
969ab199013SJosef Bacik struct btrfs_inode_info info = { 0 };
9703f8a18ccSJosef Bacik
971ab199013SJosef Bacik ASSERT(gen);
9727e93f6dcSBingJing Chang
9737e93f6dcSBingJing Chang ret = get_inode_info(root, ino, &info);
9747e93f6dcSBingJing Chang *gen = info.gen;
97531db9f7cSAlexander Block return ret;
97631db9f7cSAlexander Block }
97731db9f7cSAlexander Block
97831db9f7cSAlexander Block typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
97931db9f7cSAlexander Block struct fs_path *p,
98031db9f7cSAlexander Block void *ctx);
98131db9f7cSAlexander Block
98231db9f7cSAlexander Block /*
98396b5bd77SJan Schmidt * Helper function to iterate the entries in ONE btrfs_inode_ref or
98496b5bd77SJan Schmidt * btrfs_inode_extref.
98531db9f7cSAlexander Block * The iterate callback may return a non zero value to stop iteration. This can
98631db9f7cSAlexander Block * be a negative value for error codes or 1 to simply stop it.
98731db9f7cSAlexander Block *
98896b5bd77SJan Schmidt * path must point to the INODE_REF or INODE_EXTREF when called.
98931db9f7cSAlexander Block */
iterate_inode_ref(struct btrfs_root * root,struct btrfs_path * path,struct btrfs_key * found_key,int resolve,iterate_inode_ref_t iterate,void * ctx)990924794c9STsutomu Itoh static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
99131db9f7cSAlexander Block struct btrfs_key *found_key, int resolve,
99231db9f7cSAlexander Block iterate_inode_ref_t iterate, void *ctx)
99331db9f7cSAlexander Block {
99496b5bd77SJan Schmidt struct extent_buffer *eb = path->nodes[0];
99531db9f7cSAlexander Block struct btrfs_inode_ref *iref;
99696b5bd77SJan Schmidt struct btrfs_inode_extref *extref;
99731db9f7cSAlexander Block struct btrfs_path *tmp_path;
99831db9f7cSAlexander Block struct fs_path *p;
99996b5bd77SJan Schmidt u32 cur = 0;
100031db9f7cSAlexander Block u32 total;
100196b5bd77SJan Schmidt int slot = path->slots[0];
100231db9f7cSAlexander Block u32 name_len;
100331db9f7cSAlexander Block char *start;
100431db9f7cSAlexander Block int ret = 0;
100596b5bd77SJan Schmidt int num = 0;
100631db9f7cSAlexander Block int index;
100796b5bd77SJan Schmidt u64 dir;
100896b5bd77SJan Schmidt unsigned long name_off;
100996b5bd77SJan Schmidt unsigned long elem_size;
101096b5bd77SJan Schmidt unsigned long ptr;
101131db9f7cSAlexander Block
1012924794c9STsutomu Itoh p = fs_path_alloc_reversed();
101331db9f7cSAlexander Block if (!p)
101431db9f7cSAlexander Block return -ENOMEM;
101531db9f7cSAlexander Block
101631db9f7cSAlexander Block tmp_path = alloc_path_for_send();
101731db9f7cSAlexander Block if (!tmp_path) {
1018924794c9STsutomu Itoh fs_path_free(p);
101931db9f7cSAlexander Block return -ENOMEM;
102031db9f7cSAlexander Block }
102131db9f7cSAlexander Block
102231db9f7cSAlexander Block
102396b5bd77SJan Schmidt if (found_key->type == BTRFS_INODE_REF_KEY) {
102496b5bd77SJan Schmidt ptr = (unsigned long)btrfs_item_ptr(eb, slot,
102596b5bd77SJan Schmidt struct btrfs_inode_ref);
10263212fa14SJosef Bacik total = btrfs_item_size(eb, slot);
102796b5bd77SJan Schmidt elem_size = sizeof(*iref);
102896b5bd77SJan Schmidt } else {
102996b5bd77SJan Schmidt ptr = btrfs_item_ptr_offset(eb, slot);
10303212fa14SJosef Bacik total = btrfs_item_size(eb, slot);
103196b5bd77SJan Schmidt elem_size = sizeof(*extref);
103296b5bd77SJan Schmidt }
103396b5bd77SJan Schmidt
103431db9f7cSAlexander Block while (cur < total) {
103531db9f7cSAlexander Block fs_path_reset(p);
103631db9f7cSAlexander Block
103796b5bd77SJan Schmidt if (found_key->type == BTRFS_INODE_REF_KEY) {
103896b5bd77SJan Schmidt iref = (struct btrfs_inode_ref *)(ptr + cur);
103931db9f7cSAlexander Block name_len = btrfs_inode_ref_name_len(eb, iref);
104096b5bd77SJan Schmidt name_off = (unsigned long)(iref + 1);
104131db9f7cSAlexander Block index = btrfs_inode_ref_index(eb, iref);
104296b5bd77SJan Schmidt dir = found_key->offset;
104396b5bd77SJan Schmidt } else {
104496b5bd77SJan Schmidt extref = (struct btrfs_inode_extref *)(ptr + cur);
104596b5bd77SJan Schmidt name_len = btrfs_inode_extref_name_len(eb, extref);
104696b5bd77SJan Schmidt name_off = (unsigned long)&extref->name;
104796b5bd77SJan Schmidt index = btrfs_inode_extref_index(eb, extref);
104896b5bd77SJan Schmidt dir = btrfs_inode_extref_parent(eb, extref);
104996b5bd77SJan Schmidt }
105096b5bd77SJan Schmidt
105131db9f7cSAlexander Block if (resolve) {
105296b5bd77SJan Schmidt start = btrfs_ref_to_path(root, tmp_path, name_len,
105396b5bd77SJan Schmidt name_off, eb, dir,
105496b5bd77SJan Schmidt p->buf, p->buf_len);
105531db9f7cSAlexander Block if (IS_ERR(start)) {
105631db9f7cSAlexander Block ret = PTR_ERR(start);
105731db9f7cSAlexander Block goto out;
105831db9f7cSAlexander Block }
105931db9f7cSAlexander Block if (start < p->buf) {
106031db9f7cSAlexander Block /* overflow , try again with larger buffer */
106131db9f7cSAlexander Block ret = fs_path_ensure_buf(p,
106231db9f7cSAlexander Block p->buf_len + p->buf - start);
106331db9f7cSAlexander Block if (ret < 0)
106431db9f7cSAlexander Block goto out;
106596b5bd77SJan Schmidt start = btrfs_ref_to_path(root, tmp_path,
106696b5bd77SJan Schmidt name_len, name_off,
106796b5bd77SJan Schmidt eb, dir,
106896b5bd77SJan Schmidt p->buf, p->buf_len);
106931db9f7cSAlexander Block if (IS_ERR(start)) {
107031db9f7cSAlexander Block ret = PTR_ERR(start);
107131db9f7cSAlexander Block goto out;
107231db9f7cSAlexander Block }
1073c1363ed8SDavid Sterba if (unlikely(start < p->buf)) {
1074c1363ed8SDavid Sterba btrfs_err(root->fs_info,
1075c1363ed8SDavid Sterba "send: path ref buffer underflow for key (%llu %u %llu)",
1076c1363ed8SDavid Sterba found_key->objectid,
1077c1363ed8SDavid Sterba found_key->type,
1078c1363ed8SDavid Sterba found_key->offset);
1079c1363ed8SDavid Sterba ret = -EINVAL;
1080c1363ed8SDavid Sterba goto out;
1081c1363ed8SDavid Sterba }
108231db9f7cSAlexander Block }
108331db9f7cSAlexander Block p->start = start;
108431db9f7cSAlexander Block } else {
108596b5bd77SJan Schmidt ret = fs_path_add_from_extent_buffer(p, eb, name_off,
108696b5bd77SJan Schmidt name_len);
108731db9f7cSAlexander Block if (ret < 0)
108831db9f7cSAlexander Block goto out;
108931db9f7cSAlexander Block }
109031db9f7cSAlexander Block
109196b5bd77SJan Schmidt cur += elem_size + name_len;
109296b5bd77SJan Schmidt ret = iterate(num, dir, index, p, ctx);
109331db9f7cSAlexander Block if (ret)
109431db9f7cSAlexander Block goto out;
109531db9f7cSAlexander Block num++;
109631db9f7cSAlexander Block }
109731db9f7cSAlexander Block
109831db9f7cSAlexander Block out:
109931db9f7cSAlexander Block btrfs_free_path(tmp_path);
1100924794c9STsutomu Itoh fs_path_free(p);
110131db9f7cSAlexander Block return ret;
110231db9f7cSAlexander Block }
110331db9f7cSAlexander Block
110431db9f7cSAlexander Block typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
110531db9f7cSAlexander Block const char *name, int name_len,
110631db9f7cSAlexander Block const char *data, int data_len,
1107b1dea4e7SOmar Sandoval void *ctx);
110831db9f7cSAlexander Block
110931db9f7cSAlexander Block /*
111031db9f7cSAlexander Block * Helper function to iterate the entries in ONE btrfs_dir_item.
111131db9f7cSAlexander Block * The iterate callback may return a non zero value to stop iteration. This can
111231db9f7cSAlexander Block * be a negative value for error codes or 1 to simply stop it.
111331db9f7cSAlexander Block *
111431db9f7cSAlexander Block * path must point to the dir item when called.
111531db9f7cSAlexander Block */
iterate_dir_item(struct btrfs_root * root,struct btrfs_path * path,iterate_dir_item_t iterate,void * ctx)1116924794c9STsutomu Itoh static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
111731db9f7cSAlexander Block iterate_dir_item_t iterate, void *ctx)
111831db9f7cSAlexander Block {
111931db9f7cSAlexander Block int ret = 0;
112031db9f7cSAlexander Block struct extent_buffer *eb;
112131db9f7cSAlexander Block struct btrfs_dir_item *di;
112231db9f7cSAlexander Block struct btrfs_key di_key;
112331db9f7cSAlexander Block char *buf = NULL;
11247e3ae33eSFilipe Manana int buf_len;
112531db9f7cSAlexander Block u32 name_len;
112631db9f7cSAlexander Block u32 data_len;
112731db9f7cSAlexander Block u32 cur;
112831db9f7cSAlexander Block u32 len;
112931db9f7cSAlexander Block u32 total;
113031db9f7cSAlexander Block int slot;
113131db9f7cSAlexander Block int num;
113231db9f7cSAlexander Block
11334395e0c4SFilipe Manana /*
11344395e0c4SFilipe Manana * Start with a small buffer (1 page). If later we end up needing more
11354395e0c4SFilipe Manana * space, which can happen for xattrs on a fs with a leaf size greater
11364395e0c4SFilipe Manana * then the page size, attempt to increase the buffer. Typically xattr
11374395e0c4SFilipe Manana * values are small.
11384395e0c4SFilipe Manana */
11397e3ae33eSFilipe Manana buf_len = PATH_MAX;
1140e780b0d1SDavid Sterba buf = kmalloc(buf_len, GFP_KERNEL);
114131db9f7cSAlexander Block if (!buf) {
114231db9f7cSAlexander Block ret = -ENOMEM;
114331db9f7cSAlexander Block goto out;
114431db9f7cSAlexander Block }
114531db9f7cSAlexander Block
114631db9f7cSAlexander Block eb = path->nodes[0];
114731db9f7cSAlexander Block slot = path->slots[0];
114831db9f7cSAlexander Block di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
114931db9f7cSAlexander Block cur = 0;
115031db9f7cSAlexander Block len = 0;
11513212fa14SJosef Bacik total = btrfs_item_size(eb, slot);
115231db9f7cSAlexander Block
115331db9f7cSAlexander Block num = 0;
115431db9f7cSAlexander Block while (cur < total) {
115531db9f7cSAlexander Block name_len = btrfs_dir_name_len(eb, di);
115631db9f7cSAlexander Block data_len = btrfs_dir_data_len(eb, di);
115731db9f7cSAlexander Block btrfs_dir_item_key_to_cpu(eb, di, &di_key);
115831db9f7cSAlexander Block
115994a48aefSOmar Sandoval if (btrfs_dir_ftype(eb, di) == BTRFS_FT_XATTR) {
11607e3ae33eSFilipe Manana if (name_len > XATTR_NAME_MAX) {
11617e3ae33eSFilipe Manana ret = -ENAMETOOLONG;
11627e3ae33eSFilipe Manana goto out;
11637e3ae33eSFilipe Manana }
1164da17066cSJeff Mahoney if (name_len + data_len >
1165da17066cSJeff Mahoney BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
11667e3ae33eSFilipe Manana ret = -E2BIG;
11677e3ae33eSFilipe Manana goto out;
11687e3ae33eSFilipe Manana }
11697e3ae33eSFilipe Manana } else {
1170ace01050SDavid Sterba /*
1171ace01050SDavid Sterba * Path too long
1172ace01050SDavid Sterba */
11734395e0c4SFilipe Manana if (name_len + data_len > PATH_MAX) {
1174ace01050SDavid Sterba ret = -ENAMETOOLONG;
117531db9f7cSAlexander Block goto out;
117631db9f7cSAlexander Block }
11777e3ae33eSFilipe Manana }
117831db9f7cSAlexander Block
11794395e0c4SFilipe Manana if (name_len + data_len > buf_len) {
11804395e0c4SFilipe Manana buf_len = name_len + data_len;
11814395e0c4SFilipe Manana if (is_vmalloc_addr(buf)) {
11824395e0c4SFilipe Manana vfree(buf);
11834395e0c4SFilipe Manana buf = NULL;
11844395e0c4SFilipe Manana } else {
11854395e0c4SFilipe Manana char *tmp = krealloc(buf, buf_len,
1186e780b0d1SDavid Sterba GFP_KERNEL | __GFP_NOWARN);
11874395e0c4SFilipe Manana
11884395e0c4SFilipe Manana if (!tmp)
11894395e0c4SFilipe Manana kfree(buf);
11904395e0c4SFilipe Manana buf = tmp;
11914395e0c4SFilipe Manana }
11924395e0c4SFilipe Manana if (!buf) {
1193f11f7441SDavid Sterba buf = kvmalloc(buf_len, GFP_KERNEL);
11944395e0c4SFilipe Manana if (!buf) {
11954395e0c4SFilipe Manana ret = -ENOMEM;
11964395e0c4SFilipe Manana goto out;
11974395e0c4SFilipe Manana }
11984395e0c4SFilipe Manana }
11994395e0c4SFilipe Manana }
12004395e0c4SFilipe Manana
120131db9f7cSAlexander Block read_extent_buffer(eb, buf, (unsigned long)(di + 1),
120231db9f7cSAlexander Block name_len + data_len);
120331db9f7cSAlexander Block
120431db9f7cSAlexander Block len = sizeof(*di) + name_len + data_len;
120531db9f7cSAlexander Block di = (struct btrfs_dir_item *)((char *)di + len);
120631db9f7cSAlexander Block cur += len;
120731db9f7cSAlexander Block
120831db9f7cSAlexander Block ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1209b1dea4e7SOmar Sandoval data_len, ctx);
121031db9f7cSAlexander Block if (ret < 0)
121131db9f7cSAlexander Block goto out;
121231db9f7cSAlexander Block if (ret) {
121331db9f7cSAlexander Block ret = 0;
121431db9f7cSAlexander Block goto out;
121531db9f7cSAlexander Block }
121631db9f7cSAlexander Block
121731db9f7cSAlexander Block num++;
121831db9f7cSAlexander Block }
121931db9f7cSAlexander Block
122031db9f7cSAlexander Block out:
12214395e0c4SFilipe Manana kvfree(buf);
122231db9f7cSAlexander Block return ret;
122331db9f7cSAlexander Block }
122431db9f7cSAlexander Block
__copy_first_ref(int num,u64 dir,int index,struct fs_path * p,void * ctx)122531db9f7cSAlexander Block static int __copy_first_ref(int num, u64 dir, int index,
122631db9f7cSAlexander Block struct fs_path *p, void *ctx)
122731db9f7cSAlexander Block {
122831db9f7cSAlexander Block int ret;
122931db9f7cSAlexander Block struct fs_path *pt = ctx;
123031db9f7cSAlexander Block
123131db9f7cSAlexander Block ret = fs_path_copy(pt, p);
123231db9f7cSAlexander Block if (ret < 0)
123331db9f7cSAlexander Block return ret;
123431db9f7cSAlexander Block
123531db9f7cSAlexander Block /* we want the first only */
123631db9f7cSAlexander Block return 1;
123731db9f7cSAlexander Block }
123831db9f7cSAlexander Block
123931db9f7cSAlexander Block /*
124031db9f7cSAlexander Block * Retrieve the first path of an inode. If an inode has more then one
124131db9f7cSAlexander Block * ref/hardlink, this is ignored.
124231db9f7cSAlexander Block */
get_inode_path(struct btrfs_root * root,u64 ino,struct fs_path * path)1243924794c9STsutomu Itoh static int get_inode_path(struct btrfs_root *root,
124431db9f7cSAlexander Block u64 ino, struct fs_path *path)
124531db9f7cSAlexander Block {
124631db9f7cSAlexander Block int ret;
124731db9f7cSAlexander Block struct btrfs_key key, found_key;
124831db9f7cSAlexander Block struct btrfs_path *p;
124931db9f7cSAlexander Block
125031db9f7cSAlexander Block p = alloc_path_for_send();
125131db9f7cSAlexander Block if (!p)
125231db9f7cSAlexander Block return -ENOMEM;
125331db9f7cSAlexander Block
125431db9f7cSAlexander Block fs_path_reset(path);
125531db9f7cSAlexander Block
125631db9f7cSAlexander Block key.objectid = ino;
125731db9f7cSAlexander Block key.type = BTRFS_INODE_REF_KEY;
125831db9f7cSAlexander Block key.offset = 0;
125931db9f7cSAlexander Block
126031db9f7cSAlexander Block ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
126131db9f7cSAlexander Block if (ret < 0)
126231db9f7cSAlexander Block goto out;
126331db9f7cSAlexander Block if (ret) {
126431db9f7cSAlexander Block ret = 1;
126531db9f7cSAlexander Block goto out;
126631db9f7cSAlexander Block }
126731db9f7cSAlexander Block btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
126831db9f7cSAlexander Block if (found_key.objectid != ino ||
126996b5bd77SJan Schmidt (found_key.type != BTRFS_INODE_REF_KEY &&
127096b5bd77SJan Schmidt found_key.type != BTRFS_INODE_EXTREF_KEY)) {
127131db9f7cSAlexander Block ret = -ENOENT;
127231db9f7cSAlexander Block goto out;
127331db9f7cSAlexander Block }
127431db9f7cSAlexander Block
1275924794c9STsutomu Itoh ret = iterate_inode_ref(root, p, &found_key, 1,
127631db9f7cSAlexander Block __copy_first_ref, path);
127731db9f7cSAlexander Block if (ret < 0)
127831db9f7cSAlexander Block goto out;
127931db9f7cSAlexander Block ret = 0;
128031db9f7cSAlexander Block
128131db9f7cSAlexander Block out:
128231db9f7cSAlexander Block btrfs_free_path(p);
128331db9f7cSAlexander Block return ret;
128431db9f7cSAlexander Block }
128531db9f7cSAlexander Block
128631db9f7cSAlexander Block struct backref_ctx {
128731db9f7cSAlexander Block struct send_ctx *sctx;
128831db9f7cSAlexander Block
128931db9f7cSAlexander Block /* number of total found references */
129031db9f7cSAlexander Block u64 found;
129131db9f7cSAlexander Block
129231db9f7cSAlexander Block /*
129331db9f7cSAlexander Block * used for clones found in send_root. clones found behind cur_objectid
129431db9f7cSAlexander Block * and cur_offset are not considered as allowed clones.
129531db9f7cSAlexander Block */
129631db9f7cSAlexander Block u64 cur_objectid;
129731db9f7cSAlexander Block u64 cur_offset;
129831db9f7cSAlexander Block
129931db9f7cSAlexander Block /* may be truncated in case it's the last extent in a file */
130031db9f7cSAlexander Block u64 extent_len;
1301f73853c7SFilipe Manana
1302f73853c7SFilipe Manana /* The bytenr the file extent item we are processing refers to. */
1303f73853c7SFilipe Manana u64 bytenr;
1304adf02418SFilipe Manana /* The owner (root id) of the data backref for the current extent. */
1305adf02418SFilipe Manana u64 backref_owner;
1306adf02418SFilipe Manana /* The offset of the data backref for the current extent. */
1307adf02418SFilipe Manana u64 backref_offset;
130831db9f7cSAlexander Block };
130931db9f7cSAlexander Block
__clone_root_cmp_bsearch(const void * key,const void * elt)131031db9f7cSAlexander Block static int __clone_root_cmp_bsearch(const void *key, const void *elt)
131131db9f7cSAlexander Block {
1312995e01b7SJan Schmidt u64 root = (u64)(uintptr_t)key;
1313214cc184SDavid Sterba const struct clone_root *cr = elt;
131431db9f7cSAlexander Block
13154fd786e6SMisono Tomohiro if (root < cr->root->root_key.objectid)
131631db9f7cSAlexander Block return -1;
13174fd786e6SMisono Tomohiro if (root > cr->root->root_key.objectid)
131831db9f7cSAlexander Block return 1;
131931db9f7cSAlexander Block return 0;
132031db9f7cSAlexander Block }
132131db9f7cSAlexander Block
__clone_root_cmp_sort(const void * e1,const void * e2)132231db9f7cSAlexander Block static int __clone_root_cmp_sort(const void *e1, const void *e2)
132331db9f7cSAlexander Block {
1324214cc184SDavid Sterba const struct clone_root *cr1 = e1;
1325214cc184SDavid Sterba const struct clone_root *cr2 = e2;
132631db9f7cSAlexander Block
13274fd786e6SMisono Tomohiro if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
132831db9f7cSAlexander Block return -1;
13294fd786e6SMisono Tomohiro if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
133031db9f7cSAlexander Block return 1;
133131db9f7cSAlexander Block return 0;
133231db9f7cSAlexander Block }
133331db9f7cSAlexander Block
133431db9f7cSAlexander Block /*
133531db9f7cSAlexander Block * Called for every backref that is found for the current extent.
133688ffb665SFilipe Manana * Results are collected in sctx->clone_roots->ino/offset.
133731db9f7cSAlexander Block */
iterate_backrefs(u64 ino,u64 offset,u64 num_bytes,u64 root_id,void * ctx_)133888ffb665SFilipe Manana static int iterate_backrefs(u64 ino, u64 offset, u64 num_bytes, u64 root_id,
1339c7499a64SFilipe Manana void *ctx_)
134031db9f7cSAlexander Block {
134131db9f7cSAlexander Block struct backref_ctx *bctx = ctx_;
134288ffb665SFilipe Manana struct clone_root *clone_root;
134331db9f7cSAlexander Block
134431db9f7cSAlexander Block /* First check if the root is in the list of accepted clone sources */
134588ffb665SFilipe Manana clone_root = bsearch((void *)(uintptr_t)root_id, bctx->sctx->clone_roots,
134631db9f7cSAlexander Block bctx->sctx->clone_roots_cnt,
134731db9f7cSAlexander Block sizeof(struct clone_root),
134831db9f7cSAlexander Block __clone_root_cmp_bsearch);
134988ffb665SFilipe Manana if (!clone_root)
135031db9f7cSAlexander Block return 0;
135131db9f7cSAlexander Block
135288ffb665SFilipe Manana /* This is our own reference, bail out as we can't clone from it. */
135388ffb665SFilipe Manana if (clone_root->root == bctx->sctx->send_root &&
135431db9f7cSAlexander Block ino == bctx->cur_objectid &&
135588ffb665SFilipe Manana offset == bctx->cur_offset)
135688ffb665SFilipe Manana return 0;
135731db9f7cSAlexander Block
135831db9f7cSAlexander Block /*
135931db9f7cSAlexander Block * Make sure we don't consider clones from send_root that are
136031db9f7cSAlexander Block * behind the current inode/offset.
136131db9f7cSAlexander Block */
136288ffb665SFilipe Manana if (clone_root->root == bctx->sctx->send_root) {
136331db9f7cSAlexander Block /*
136411f2069cSFilipe Manana * If the source inode was not yet processed we can't issue a
136511f2069cSFilipe Manana * clone operation, as the source extent does not exist yet at
136611f2069cSFilipe Manana * the destination of the stream.
136731db9f7cSAlexander Block */
136811f2069cSFilipe Manana if (ino > bctx->cur_objectid)
136911f2069cSFilipe Manana return 0;
137011f2069cSFilipe Manana /*
137111f2069cSFilipe Manana * We clone from the inode currently being sent as long as the
137211f2069cSFilipe Manana * source extent is already processed, otherwise we could try
137311f2069cSFilipe Manana * to clone from an extent that does not exist yet at the
137411f2069cSFilipe Manana * destination of the stream.
137511f2069cSFilipe Manana */
137611f2069cSFilipe Manana if (ino == bctx->cur_objectid &&
13779722b101SFilipe Manana offset + bctx->extent_len >
13789722b101SFilipe Manana bctx->sctx->cur_inode_next_write_offset)
137931db9f7cSAlexander Block return 0;
138031db9f7cSAlexander Block }
138131db9f7cSAlexander Block
138231db9f7cSAlexander Block bctx->found++;
138388ffb665SFilipe Manana clone_root->found_ref = true;
1384c7499a64SFilipe Manana
1385c7499a64SFilipe Manana /*
1386c7499a64SFilipe Manana * If the given backref refers to a file extent item with a larger
1387c7499a64SFilipe Manana * number of bytes than what we found before, use the new one so that
1388c7499a64SFilipe Manana * we clone more optimally and end up doing less writes and getting
1389c7499a64SFilipe Manana * less exclusive, non-shared extents at the destination.
1390c7499a64SFilipe Manana */
139188ffb665SFilipe Manana if (num_bytes > clone_root->num_bytes) {
139288ffb665SFilipe Manana clone_root->ino = ino;
139388ffb665SFilipe Manana clone_root->offset = offset;
139488ffb665SFilipe Manana clone_root->num_bytes = num_bytes;
139588ffb665SFilipe Manana
139688ffb665SFilipe Manana /*
139788ffb665SFilipe Manana * Found a perfect candidate, so there's no need to continue
139888ffb665SFilipe Manana * backref walking.
139988ffb665SFilipe Manana */
140088ffb665SFilipe Manana if (num_bytes >= bctx->extent_len)
140188ffb665SFilipe Manana return BTRFS_ITERATE_EXTENT_INODES_STOP;
140231db9f7cSAlexander Block }
140331db9f7cSAlexander Block
140431db9f7cSAlexander Block return 0;
140531db9f7cSAlexander Block }
140631db9f7cSAlexander Block
lookup_backref_cache(u64 leaf_bytenr,void * ctx,const u64 ** root_ids_ret,int * root_count_ret)140766d04209SFilipe Manana static bool lookup_backref_cache(u64 leaf_bytenr, void *ctx,
140866d04209SFilipe Manana const u64 **root_ids_ret, int *root_count_ret)
140966d04209SFilipe Manana {
141088ffb665SFilipe Manana struct backref_ctx *bctx = ctx;
141188ffb665SFilipe Manana struct send_ctx *sctx = bctx->sctx;
141266d04209SFilipe Manana struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
141366d04209SFilipe Manana const u64 key = leaf_bytenr >> fs_info->sectorsize_bits;
141490b90d4aSFilipe Manana struct btrfs_lru_cache_entry *raw_entry;
141566d04209SFilipe Manana struct backref_cache_entry *entry;
141666d04209SFilipe Manana
141790b90d4aSFilipe Manana if (btrfs_lru_cache_size(&sctx->backref_cache) == 0)
141866d04209SFilipe Manana return false;
141966d04209SFilipe Manana
142066d04209SFilipe Manana /*
142166d04209SFilipe Manana * If relocation happened since we first filled the cache, then we must
142266d04209SFilipe Manana * empty the cache and can not use it, because even though we operate on
142366d04209SFilipe Manana * read-only roots, their leaves and nodes may have been reallocated and
142466d04209SFilipe Manana * now be used for different nodes/leaves of the same tree or some other
142566d04209SFilipe Manana * tree.
142666d04209SFilipe Manana *
142766d04209SFilipe Manana * We are called from iterate_extent_inodes() while either holding a
142866d04209SFilipe Manana * transaction handle or holding fs_info->commit_root_sem, so no need
142966d04209SFilipe Manana * to take any lock here.
143066d04209SFilipe Manana */
143190b90d4aSFilipe Manana if (fs_info->last_reloc_trans > sctx->backref_cache_last_reloc_trans) {
143290b90d4aSFilipe Manana btrfs_lru_cache_clear(&sctx->backref_cache);
143366d04209SFilipe Manana return false;
143466d04209SFilipe Manana }
143566d04209SFilipe Manana
14360da0c560SFilipe Manana raw_entry = btrfs_lru_cache_lookup(&sctx->backref_cache, key, 0);
143790b90d4aSFilipe Manana if (!raw_entry)
143866d04209SFilipe Manana return false;
143966d04209SFilipe Manana
144090b90d4aSFilipe Manana entry = container_of(raw_entry, struct backref_cache_entry, entry);
144166d04209SFilipe Manana *root_ids_ret = entry->root_ids;
144266d04209SFilipe Manana *root_count_ret = entry->num_roots;
144366d04209SFilipe Manana
144466d04209SFilipe Manana return true;
144566d04209SFilipe Manana }
144666d04209SFilipe Manana
store_backref_cache(u64 leaf_bytenr,const struct ulist * root_ids,void * ctx)144766d04209SFilipe Manana static void store_backref_cache(u64 leaf_bytenr, const struct ulist *root_ids,
144866d04209SFilipe Manana void *ctx)
144966d04209SFilipe Manana {
145088ffb665SFilipe Manana struct backref_ctx *bctx = ctx;
145188ffb665SFilipe Manana struct send_ctx *sctx = bctx->sctx;
145266d04209SFilipe Manana struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
145366d04209SFilipe Manana struct backref_cache_entry *new_entry;
145466d04209SFilipe Manana struct ulist_iterator uiter;
145566d04209SFilipe Manana struct ulist_node *node;
145666d04209SFilipe Manana int ret;
145766d04209SFilipe Manana
145866d04209SFilipe Manana /*
145966d04209SFilipe Manana * We're called while holding a transaction handle or while holding
146066d04209SFilipe Manana * fs_info->commit_root_sem (at iterate_extent_inodes()), so must do a
146166d04209SFilipe Manana * NOFS allocation.
146266d04209SFilipe Manana */
146366d04209SFilipe Manana new_entry = kmalloc(sizeof(struct backref_cache_entry), GFP_NOFS);
146466d04209SFilipe Manana /* No worries, cache is optional. */
146566d04209SFilipe Manana if (!new_entry)
146666d04209SFilipe Manana return;
146766d04209SFilipe Manana
146890b90d4aSFilipe Manana new_entry->entry.key = leaf_bytenr >> fs_info->sectorsize_bits;
14690da0c560SFilipe Manana new_entry->entry.gen = 0;
147066d04209SFilipe Manana new_entry->num_roots = 0;
147166d04209SFilipe Manana ULIST_ITER_INIT(&uiter);
147266d04209SFilipe Manana while ((node = ulist_next(root_ids, &uiter)) != NULL) {
147366d04209SFilipe Manana const u64 root_id = node->val;
147466d04209SFilipe Manana struct clone_root *root;
147566d04209SFilipe Manana
147666d04209SFilipe Manana root = bsearch((void *)(uintptr_t)root_id, sctx->clone_roots,
147766d04209SFilipe Manana sctx->clone_roots_cnt, sizeof(struct clone_root),
147866d04209SFilipe Manana __clone_root_cmp_bsearch);
147966d04209SFilipe Manana if (!root)
148066d04209SFilipe Manana continue;
148166d04209SFilipe Manana
148266d04209SFilipe Manana /* Too many roots, just exit, no worries as caching is optional. */
148366d04209SFilipe Manana if (new_entry->num_roots >= SEND_MAX_BACKREF_CACHE_ROOTS) {
148466d04209SFilipe Manana kfree(new_entry);
148566d04209SFilipe Manana return;
148666d04209SFilipe Manana }
148766d04209SFilipe Manana
148866d04209SFilipe Manana new_entry->root_ids[new_entry->num_roots] = root_id;
148966d04209SFilipe Manana new_entry->num_roots++;
149066d04209SFilipe Manana }
149166d04209SFilipe Manana
149266d04209SFilipe Manana /*
149366d04209SFilipe Manana * We may have not added any roots to the new cache entry, which means
149466d04209SFilipe Manana * none of the roots is part of the list of roots from which we are
149566d04209SFilipe Manana * allowed to clone. Cache the new entry as it's still useful to avoid
149666d04209SFilipe Manana * backref walking to determine which roots have a path to the leaf.
149790b90d4aSFilipe Manana *
149890b90d4aSFilipe Manana * Also use GFP_NOFS because we're called while holding a transaction
149990b90d4aSFilipe Manana * handle or while holding fs_info->commit_root_sem.
150066d04209SFilipe Manana */
150190b90d4aSFilipe Manana ret = btrfs_lru_cache_store(&sctx->backref_cache, &new_entry->entry,
150290b90d4aSFilipe Manana GFP_NOFS);
150366d04209SFilipe Manana ASSERT(ret == 0 || ret == -ENOMEM);
150466d04209SFilipe Manana if (ret) {
150566d04209SFilipe Manana /* Caching is optional, no worries. */
150666d04209SFilipe Manana kfree(new_entry);
150766d04209SFilipe Manana return;
150866d04209SFilipe Manana }
150966d04209SFilipe Manana
151066d04209SFilipe Manana /*
151166d04209SFilipe Manana * We are called from iterate_extent_inodes() while either holding a
151266d04209SFilipe Manana * transaction handle or holding fs_info->commit_root_sem, so no need
151366d04209SFilipe Manana * to take any lock here.
151466d04209SFilipe Manana */
151590b90d4aSFilipe Manana if (btrfs_lru_cache_size(&sctx->backref_cache) == 1)
151690b90d4aSFilipe Manana sctx->backref_cache_last_reloc_trans = fs_info->last_reloc_trans;
151766d04209SFilipe Manana }
151866d04209SFilipe Manana
check_extent_item(u64 bytenr,const struct btrfs_extent_item * ei,const struct extent_buffer * leaf,void * ctx)1519f73853c7SFilipe Manana static int check_extent_item(u64 bytenr, const struct btrfs_extent_item *ei,
1520f73853c7SFilipe Manana const struct extent_buffer *leaf, void *ctx)
1521f73853c7SFilipe Manana {
1522f73853c7SFilipe Manana const u64 refs = btrfs_extent_refs(leaf, ei);
1523f73853c7SFilipe Manana const struct backref_ctx *bctx = ctx;
1524f73853c7SFilipe Manana const struct send_ctx *sctx = bctx->sctx;
1525f73853c7SFilipe Manana
1526f73853c7SFilipe Manana if (bytenr == bctx->bytenr) {
1527f73853c7SFilipe Manana const u64 flags = btrfs_extent_flags(leaf, ei);
1528f73853c7SFilipe Manana
1529f73853c7SFilipe Manana if (WARN_ON(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
1530f73853c7SFilipe Manana return -EUCLEAN;
1531f73853c7SFilipe Manana
1532f73853c7SFilipe Manana /*
1533f73853c7SFilipe Manana * If we have only one reference and only the send root as a
1534f73853c7SFilipe Manana * clone source - meaning no clone roots were given in the
1535f73853c7SFilipe Manana * struct btrfs_ioctl_send_args passed to the send ioctl - then
1536f73853c7SFilipe Manana * it's our reference and there's no point in doing backref
1537f73853c7SFilipe Manana * walking which is expensive, so exit early.
1538f73853c7SFilipe Manana */
1539f73853c7SFilipe Manana if (refs == 1 && sctx->clone_roots_cnt == 1)
1540f73853c7SFilipe Manana return -ENOENT;
1541f73853c7SFilipe Manana }
1542f73853c7SFilipe Manana
1543f73853c7SFilipe Manana /*
1544f73853c7SFilipe Manana * Backreference walking (iterate_extent_inodes() below) is currently
1545f73853c7SFilipe Manana * too expensive when an extent has a large number of references, both
1546f73853c7SFilipe Manana * in time spent and used memory. So for now just fallback to write
1547f73853c7SFilipe Manana * operations instead of clone operations when an extent has more than
1548f73853c7SFilipe Manana * a certain amount of references.
1549f73853c7SFilipe Manana */
1550f73853c7SFilipe Manana if (refs > SEND_MAX_EXTENT_REFS)
1551f73853c7SFilipe Manana return -ENOENT;
1552f73853c7SFilipe Manana
1553f73853c7SFilipe Manana return 0;
1554f73853c7SFilipe Manana }
1555f73853c7SFilipe Manana
skip_self_data_ref(u64 root,u64 ino,u64 offset,void * ctx)1556adf02418SFilipe Manana static bool skip_self_data_ref(u64 root, u64 ino, u64 offset, void *ctx)
1557adf02418SFilipe Manana {
1558adf02418SFilipe Manana const struct backref_ctx *bctx = ctx;
1559adf02418SFilipe Manana
1560adf02418SFilipe Manana if (ino == bctx->cur_objectid &&
1561adf02418SFilipe Manana root == bctx->backref_owner &&
1562adf02418SFilipe Manana offset == bctx->backref_offset)
1563adf02418SFilipe Manana return true;
1564adf02418SFilipe Manana
1565adf02418SFilipe Manana return false;
1566adf02418SFilipe Manana }
1567adf02418SFilipe Manana
156831db9f7cSAlexander Block /*
1569766702efSAlexander Block * Given an inode, offset and extent item, it finds a good clone for a clone
1570766702efSAlexander Block * instruction. Returns -ENOENT when none could be found. The function makes
1571766702efSAlexander Block * sure that the returned clone is usable at the point where sending is at the
1572766702efSAlexander Block * moment. This means, that no clones are accepted which lie behind the current
1573766702efSAlexander Block * inode+offset.
1574766702efSAlexander Block *
157531db9f7cSAlexander Block * path must point to the extent item when called.
157631db9f7cSAlexander Block */
find_extent_clone(struct send_ctx * sctx,struct btrfs_path * path,u64 ino,u64 data_offset,u64 ino_size,struct clone_root ** found)157731db9f7cSAlexander Block static int find_extent_clone(struct send_ctx *sctx,
157831db9f7cSAlexander Block struct btrfs_path *path,
157931db9f7cSAlexander Block u64 ino, u64 data_offset,
158031db9f7cSAlexander Block u64 ino_size,
158131db9f7cSAlexander Block struct clone_root **found)
158231db9f7cSAlexander Block {
158304ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
158431db9f7cSAlexander Block int ret;
158531db9f7cSAlexander Block int extent_type;
158631db9f7cSAlexander Block u64 logical;
158774dd17fbSChris Mason u64 disk_byte;
158831db9f7cSAlexander Block u64 num_bytes;
158931db9f7cSAlexander Block struct btrfs_file_extent_item *fi;
159031db9f7cSAlexander Block struct extent_buffer *eb = path->nodes[0];
1591dce28150SGoldwyn Rodrigues struct backref_ctx backref_ctx = { 0 };
1592a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx backref_walk_ctx = { 0 };
159331db9f7cSAlexander Block struct clone_root *cur_clone_root;
159474dd17fbSChris Mason int compressed;
159531db9f7cSAlexander Block u32 i;
159631db9f7cSAlexander Block
159731db9f7cSAlexander Block /*
1598d3f41317SFilipe Manana * With fallocate we can get prealloc extents beyond the inode's i_size,
1599d3f41317SFilipe Manana * so we don't do anything here because clone operations can not clone
1600d3f41317SFilipe Manana * to a range beyond i_size without increasing the i_size of the
1601d3f41317SFilipe Manana * destination inode.
160231db9f7cSAlexander Block */
1603d3f41317SFilipe Manana if (data_offset >= ino_size)
160461ce908aSFilipe Manana return 0;
160531db9f7cSAlexander Block
160661ce908aSFilipe Manana fi = btrfs_item_ptr(eb, path->slots[0], struct btrfs_file_extent_item);
160731db9f7cSAlexander Block extent_type = btrfs_file_extent_type(eb, fi);
160861ce908aSFilipe Manana if (extent_type == BTRFS_FILE_EXTENT_INLINE)
160961ce908aSFilipe Manana return -ENOENT;
161031db9f7cSAlexander Block
161174dd17fbSChris Mason disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
161261ce908aSFilipe Manana if (disk_byte == 0)
161361ce908aSFilipe Manana return -ENOENT;
161461ce908aSFilipe Manana
161561ce908aSFilipe Manana compressed = btrfs_file_extent_compression(eb, fi);
161661ce908aSFilipe Manana num_bytes = btrfs_file_extent_num_bytes(eb, fi);
161774dd17fbSChris Mason logical = disk_byte + btrfs_file_extent_offset(eb, fi);
161831db9f7cSAlexander Block
161931db9f7cSAlexander Block /*
162031db9f7cSAlexander Block * Setup the clone roots.
162131db9f7cSAlexander Block */
162231db9f7cSAlexander Block for (i = 0; i < sctx->clone_roots_cnt; i++) {
162331db9f7cSAlexander Block cur_clone_root = sctx->clone_roots + i;
162431db9f7cSAlexander Block cur_clone_root->ino = (u64)-1;
162531db9f7cSAlexander Block cur_clone_root->offset = 0;
1626c7499a64SFilipe Manana cur_clone_root->num_bytes = 0;
162788ffb665SFilipe Manana cur_clone_root->found_ref = false;
162831db9f7cSAlexander Block }
162931db9f7cSAlexander Block
1630dce28150SGoldwyn Rodrigues backref_ctx.sctx = sctx;
1631dce28150SGoldwyn Rodrigues backref_ctx.cur_objectid = ino;
1632dce28150SGoldwyn Rodrigues backref_ctx.cur_offset = data_offset;
1633f73853c7SFilipe Manana backref_ctx.bytenr = disk_byte;
1634adf02418SFilipe Manana /*
1635adf02418SFilipe Manana * Use the header owner and not the send root's id, because in case of a
1636adf02418SFilipe Manana * snapshot we can have shared subtrees.
1637adf02418SFilipe Manana */
1638adf02418SFilipe Manana backref_ctx.backref_owner = btrfs_header_owner(eb);
1639adf02418SFilipe Manana backref_ctx.backref_offset = data_offset - btrfs_file_extent_offset(eb, fi);
164031db9f7cSAlexander Block
164131db9f7cSAlexander Block /*
164231db9f7cSAlexander Block * The last extent of a file may be too large due to page alignment.
164331db9f7cSAlexander Block * We need to adjust extent_len in this case so that the checks in
164488ffb665SFilipe Manana * iterate_backrefs() work.
164531db9f7cSAlexander Block */
164631db9f7cSAlexander Block if (data_offset + num_bytes >= ino_size)
1647dce28150SGoldwyn Rodrigues backref_ctx.extent_len = ino_size - data_offset;
1648344174a1SFilipe Manana else
1649344174a1SFilipe Manana backref_ctx.extent_len = num_bytes;
165031db9f7cSAlexander Block
165131db9f7cSAlexander Block /*
165231db9f7cSAlexander Block * Now collect all backrefs.
165331db9f7cSAlexander Block */
1654f73853c7SFilipe Manana backref_walk_ctx.bytenr = disk_byte;
165574dd17fbSChris Mason if (compressed == BTRFS_COMPRESS_NONE)
1656f73853c7SFilipe Manana backref_walk_ctx.extent_item_pos = btrfs_file_extent_offset(eb, fi);
1657a2c8d27eSFilipe Manana backref_walk_ctx.fs_info = fs_info;
165866d04209SFilipe Manana backref_walk_ctx.cache_lookup = lookup_backref_cache;
165966d04209SFilipe Manana backref_walk_ctx.cache_store = store_backref_cache;
166088ffb665SFilipe Manana backref_walk_ctx.indirect_ref_iterator = iterate_backrefs;
1661f73853c7SFilipe Manana backref_walk_ctx.check_extent_item = check_extent_item;
166288ffb665SFilipe Manana backref_walk_ctx.user_ctx = &backref_ctx;
166374dd17fbSChris Mason
1664adf02418SFilipe Manana /*
1665adf02418SFilipe Manana * If have a single clone root, then it's the send root and we can tell
1666adf02418SFilipe Manana * the backref walking code to skip our own backref and not resolve it,
1667adf02418SFilipe Manana * since we can not use it for cloning - the source and destination
1668adf02418SFilipe Manana * ranges can't overlap and in case the leaf is shared through a subtree
1669adf02418SFilipe Manana * due to snapshots, we can't use those other roots since they are not
1670adf02418SFilipe Manana * in the list of clone roots.
1671adf02418SFilipe Manana */
1672adf02418SFilipe Manana if (sctx->clone_roots_cnt == 1)
1673adf02418SFilipe Manana backref_walk_ctx.skip_data_ref = skip_self_data_ref;
1674adf02418SFilipe Manana
167588ffb665SFilipe Manana ret = iterate_extent_inodes(&backref_walk_ctx, true, iterate_backrefs,
1676a2c8d27eSFilipe Manana &backref_ctx);
167731db9f7cSAlexander Block if (ret < 0)
1678f73853c7SFilipe Manana return ret;
167931db9f7cSAlexander Block
1680d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
1681d96b3424SFilipe Manana if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1682d96b3424SFilipe Manana /*
1683d96b3424SFilipe Manana * A transaction commit for a transaction in which block group
1684d96b3424SFilipe Manana * relocation was done just happened.
1685d96b3424SFilipe Manana * The disk_bytenr of the file extent item we processed is
1686d96b3424SFilipe Manana * possibly stale, referring to the extent's location before
1687d96b3424SFilipe Manana * relocation. So act as if we haven't found any clone sources
1688d96b3424SFilipe Manana * and fallback to write commands, which will read the correct
1689d96b3424SFilipe Manana * data from the new extent location. Otherwise we will fail
1690d96b3424SFilipe Manana * below because we haven't found our own back reference or we
1691d96b3424SFilipe Manana * could be getting incorrect sources in case the old extent
1692d96b3424SFilipe Manana * was already reallocated after the relocation.
1693d96b3424SFilipe Manana */
1694d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
1695f73853c7SFilipe Manana return -ENOENT;
1696d96b3424SFilipe Manana }
1697d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
1698d96b3424SFilipe Manana
169904ab956eSJeff Mahoney btrfs_debug(fs_info,
170004ab956eSJeff Mahoney "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
170131db9f7cSAlexander Block data_offset, ino, num_bytes, logical);
170231db9f7cSAlexander Block
170388ffb665SFilipe Manana if (!backref_ctx.found) {
170404ab956eSJeff Mahoney btrfs_debug(fs_info, "no clones found");
1705f73853c7SFilipe Manana return -ENOENT;
170688ffb665SFilipe Manana }
170731db9f7cSAlexander Block
170831db9f7cSAlexander Block cur_clone_root = NULL;
170931db9f7cSAlexander Block for (i = 0; i < sctx->clone_roots_cnt; i++) {
1710c7499a64SFilipe Manana struct clone_root *clone_root = &sctx->clone_roots[i];
171131db9f7cSAlexander Block
171288ffb665SFilipe Manana if (!clone_root->found_ref)
1713c7499a64SFilipe Manana continue;
1714c7499a64SFilipe Manana
1715c7499a64SFilipe Manana /*
1716c7499a64SFilipe Manana * Choose the root from which we can clone more bytes, to
1717c7499a64SFilipe Manana * minimize write operations and therefore have more extent
1718c7499a64SFilipe Manana * sharing at the destination (the same as in the source).
1719c7499a64SFilipe Manana */
1720c7499a64SFilipe Manana if (!cur_clone_root ||
1721c7499a64SFilipe Manana clone_root->num_bytes > cur_clone_root->num_bytes) {
1722c7499a64SFilipe Manana cur_clone_root = clone_root;
1723c7499a64SFilipe Manana
1724c7499a64SFilipe Manana /*
1725c7499a64SFilipe Manana * We found an optimal clone candidate (any inode from
1726c7499a64SFilipe Manana * any root is fine), so we're done.
1727c7499a64SFilipe Manana */
1728c7499a64SFilipe Manana if (clone_root->num_bytes >= backref_ctx.extent_len)
1729c7499a64SFilipe Manana break;
1730c7499a64SFilipe Manana }
173131db9f7cSAlexander Block }
173231db9f7cSAlexander Block
173331db9f7cSAlexander Block if (cur_clone_root) {
173431db9f7cSAlexander Block *found = cur_clone_root;
173531db9f7cSAlexander Block ret = 0;
173631db9f7cSAlexander Block } else {
173731db9f7cSAlexander Block ret = -ENOENT;
173831db9f7cSAlexander Block }
173931db9f7cSAlexander Block
174031db9f7cSAlexander Block return ret;
174131db9f7cSAlexander Block }
174231db9f7cSAlexander Block
read_symlink(struct btrfs_root * root,u64 ino,struct fs_path * dest)1743924794c9STsutomu Itoh static int read_symlink(struct btrfs_root *root,
174431db9f7cSAlexander Block u64 ino,
174531db9f7cSAlexander Block struct fs_path *dest)
174631db9f7cSAlexander Block {
174731db9f7cSAlexander Block int ret;
174831db9f7cSAlexander Block struct btrfs_path *path;
174931db9f7cSAlexander Block struct btrfs_key key;
175031db9f7cSAlexander Block struct btrfs_file_extent_item *ei;
175131db9f7cSAlexander Block u8 type;
175231db9f7cSAlexander Block u8 compression;
175331db9f7cSAlexander Block unsigned long off;
175431db9f7cSAlexander Block int len;
175531db9f7cSAlexander Block
175631db9f7cSAlexander Block path = alloc_path_for_send();
175731db9f7cSAlexander Block if (!path)
175831db9f7cSAlexander Block return -ENOMEM;
175931db9f7cSAlexander Block
176031db9f7cSAlexander Block key.objectid = ino;
176131db9f7cSAlexander Block key.type = BTRFS_EXTENT_DATA_KEY;
176231db9f7cSAlexander Block key.offset = 0;
176331db9f7cSAlexander Block ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
176431db9f7cSAlexander Block if (ret < 0)
176531db9f7cSAlexander Block goto out;
1766a879719bSFilipe Manana if (ret) {
1767a879719bSFilipe Manana /*
1768a879719bSFilipe Manana * An empty symlink inode. Can happen in rare error paths when
1769a879719bSFilipe Manana * creating a symlink (transaction committed before the inode
1770a879719bSFilipe Manana * eviction handler removed the symlink inode items and a crash
1771a879719bSFilipe Manana * happened in between or the subvol was snapshoted in between).
1772a879719bSFilipe Manana * Print an informative message to dmesg/syslog so that the user
1773a879719bSFilipe Manana * can delete the symlink.
1774a879719bSFilipe Manana */
1775a879719bSFilipe Manana btrfs_err(root->fs_info,
1776a879719bSFilipe Manana "Found empty symlink inode %llu at root %llu",
1777a879719bSFilipe Manana ino, root->root_key.objectid);
1778a879719bSFilipe Manana ret = -EIO;
1779a879719bSFilipe Manana goto out;
1780a879719bSFilipe Manana }
178131db9f7cSAlexander Block
178231db9f7cSAlexander Block ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
178331db9f7cSAlexander Block struct btrfs_file_extent_item);
178431db9f7cSAlexander Block type = btrfs_file_extent_type(path->nodes[0], ei);
17856f3eb72aSFilipe Manana if (unlikely(type != BTRFS_FILE_EXTENT_INLINE)) {
17866f3eb72aSFilipe Manana ret = -EUCLEAN;
17876f3eb72aSFilipe Manana btrfs_crit(root->fs_info,
17886f3eb72aSFilipe Manana "send: found symlink extent that is not inline, ino %llu root %llu extent type %d",
17896f3eb72aSFilipe Manana ino, btrfs_root_id(root), type);
17906f3eb72aSFilipe Manana goto out;
17916f3eb72aSFilipe Manana }
179231db9f7cSAlexander Block compression = btrfs_file_extent_compression(path->nodes[0], ei);
17936f3eb72aSFilipe Manana if (unlikely(compression != BTRFS_COMPRESS_NONE)) {
17946f3eb72aSFilipe Manana ret = -EUCLEAN;
17956f3eb72aSFilipe Manana btrfs_crit(root->fs_info,
17966f3eb72aSFilipe Manana "send: found symlink extent with compression, ino %llu root %llu compression type %d",
17976f3eb72aSFilipe Manana ino, btrfs_root_id(root), compression);
17986f3eb72aSFilipe Manana goto out;
17996f3eb72aSFilipe Manana }
180031db9f7cSAlexander Block
180131db9f7cSAlexander Block off = btrfs_file_extent_inline_start(ei);
1802e41ca589SQu Wenruo len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
180331db9f7cSAlexander Block
180431db9f7cSAlexander Block ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
180531db9f7cSAlexander Block
180631db9f7cSAlexander Block out:
180731db9f7cSAlexander Block btrfs_free_path(path);
180831db9f7cSAlexander Block return ret;
180931db9f7cSAlexander Block }
181031db9f7cSAlexander Block
181131db9f7cSAlexander Block /*
181231db9f7cSAlexander Block * Helper function to generate a file name that is unique in the root of
181331db9f7cSAlexander Block * send_root and parent_root. This is used to generate names for orphan inodes.
181431db9f7cSAlexander Block */
gen_unique_name(struct send_ctx * sctx,u64 ino,u64 gen,struct fs_path * dest)181531db9f7cSAlexander Block static int gen_unique_name(struct send_ctx *sctx,
181631db9f7cSAlexander Block u64 ino, u64 gen,
181731db9f7cSAlexander Block struct fs_path *dest)
181831db9f7cSAlexander Block {
181931db9f7cSAlexander Block int ret = 0;
182031db9f7cSAlexander Block struct btrfs_path *path;
182131db9f7cSAlexander Block struct btrfs_dir_item *di;
182231db9f7cSAlexander Block char tmp[64];
182331db9f7cSAlexander Block int len;
182431db9f7cSAlexander Block u64 idx = 0;
182531db9f7cSAlexander Block
182631db9f7cSAlexander Block path = alloc_path_for_send();
182731db9f7cSAlexander Block if (!path)
182831db9f7cSAlexander Block return -ENOMEM;
182931db9f7cSAlexander Block
183031db9f7cSAlexander Block while (1) {
18316db75318SSweet Tea Dorminy struct fscrypt_str tmp_name;
1832e43eec81SSweet Tea Dorminy
1833f74b86d8SFilipe David Borba Manana len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
183431db9f7cSAlexander Block ino, gen, idx);
183564792f25SDavid Sterba ASSERT(len < sizeof(tmp));
1836e43eec81SSweet Tea Dorminy tmp_name.name = tmp;
1837e43eec81SSweet Tea Dorminy tmp_name.len = strlen(tmp);
183831db9f7cSAlexander Block
183931db9f7cSAlexander Block di = btrfs_lookup_dir_item(NULL, sctx->send_root,
184031db9f7cSAlexander Block path, BTRFS_FIRST_FREE_OBJECTID,
1841e43eec81SSweet Tea Dorminy &tmp_name, 0);
184231db9f7cSAlexander Block btrfs_release_path(path);
184331db9f7cSAlexander Block if (IS_ERR(di)) {
184431db9f7cSAlexander Block ret = PTR_ERR(di);
184531db9f7cSAlexander Block goto out;
184631db9f7cSAlexander Block }
184731db9f7cSAlexander Block if (di) {
184831db9f7cSAlexander Block /* not unique, try again */
184931db9f7cSAlexander Block idx++;
185031db9f7cSAlexander Block continue;
185131db9f7cSAlexander Block }
185231db9f7cSAlexander Block
185331db9f7cSAlexander Block if (!sctx->parent_root) {
185431db9f7cSAlexander Block /* unique */
185531db9f7cSAlexander Block ret = 0;
185631db9f7cSAlexander Block break;
185731db9f7cSAlexander Block }
185831db9f7cSAlexander Block
185931db9f7cSAlexander Block di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
186031db9f7cSAlexander Block path, BTRFS_FIRST_FREE_OBJECTID,
1861e43eec81SSweet Tea Dorminy &tmp_name, 0);
186231db9f7cSAlexander Block btrfs_release_path(path);
186331db9f7cSAlexander Block if (IS_ERR(di)) {
186431db9f7cSAlexander Block ret = PTR_ERR(di);
186531db9f7cSAlexander Block goto out;
186631db9f7cSAlexander Block }
186731db9f7cSAlexander Block if (di) {
186831db9f7cSAlexander Block /* not unique, try again */
186931db9f7cSAlexander Block idx++;
187031db9f7cSAlexander Block continue;
187131db9f7cSAlexander Block }
187231db9f7cSAlexander Block /* unique */
187331db9f7cSAlexander Block break;
187431db9f7cSAlexander Block }
187531db9f7cSAlexander Block
187631db9f7cSAlexander Block ret = fs_path_add(dest, tmp, strlen(tmp));
187731db9f7cSAlexander Block
187831db9f7cSAlexander Block out:
187931db9f7cSAlexander Block btrfs_free_path(path);
188031db9f7cSAlexander Block return ret;
188131db9f7cSAlexander Block }
188231db9f7cSAlexander Block
188331db9f7cSAlexander Block enum inode_state {
188431db9f7cSAlexander Block inode_state_no_change,
188531db9f7cSAlexander Block inode_state_will_create,
188631db9f7cSAlexander Block inode_state_did_create,
188731db9f7cSAlexander Block inode_state_will_delete,
188831db9f7cSAlexander Block inode_state_did_delete,
188931db9f7cSAlexander Block };
189031db9f7cSAlexander Block
get_cur_inode_state(struct send_ctx * sctx,u64 ino,u64 gen,u64 * send_gen,u64 * parent_gen)1891498581f3SFilipe Manana static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen,
1892498581f3SFilipe Manana u64 *send_gen, u64 *parent_gen)
189331db9f7cSAlexander Block {
189431db9f7cSAlexander Block int ret;
189531db9f7cSAlexander Block int left_ret;
189631db9f7cSAlexander Block int right_ret;
189731db9f7cSAlexander Block u64 left_gen;
18988ba7d5f5SGenjian Zhang u64 right_gen = 0;
18999ed0a72eSBingJing Chang struct btrfs_inode_info info;
190031db9f7cSAlexander Block
19019ed0a72eSBingJing Chang ret = get_inode_info(sctx->send_root, ino, &info);
190231db9f7cSAlexander Block if (ret < 0 && ret != -ENOENT)
190331db9f7cSAlexander Block goto out;
19049ed0a72eSBingJing Chang left_ret = (info.nlink == 0) ? -ENOENT : ret;
19059ed0a72eSBingJing Chang left_gen = info.gen;
1906498581f3SFilipe Manana if (send_gen)
1907498581f3SFilipe Manana *send_gen = ((left_ret == -ENOENT) ? 0 : info.gen);
190831db9f7cSAlexander Block
190931db9f7cSAlexander Block if (!sctx->parent_root) {
191031db9f7cSAlexander Block right_ret = -ENOENT;
191131db9f7cSAlexander Block } else {
19129ed0a72eSBingJing Chang ret = get_inode_info(sctx->parent_root, ino, &info);
191331db9f7cSAlexander Block if (ret < 0 && ret != -ENOENT)
191431db9f7cSAlexander Block goto out;
19159ed0a72eSBingJing Chang right_ret = (info.nlink == 0) ? -ENOENT : ret;
19169ed0a72eSBingJing Chang right_gen = info.gen;
1917498581f3SFilipe Manana if (parent_gen)
1918498581f3SFilipe Manana *parent_gen = ((right_ret == -ENOENT) ? 0 : info.gen);
191931db9f7cSAlexander Block }
192031db9f7cSAlexander Block
192131db9f7cSAlexander Block if (!left_ret && !right_ret) {
1922e938c8adSAlexander Block if (left_gen == gen && right_gen == gen) {
192331db9f7cSAlexander Block ret = inode_state_no_change;
1924e938c8adSAlexander Block } else if (left_gen == gen) {
192531db9f7cSAlexander Block if (ino < sctx->send_progress)
192631db9f7cSAlexander Block ret = inode_state_did_create;
192731db9f7cSAlexander Block else
192831db9f7cSAlexander Block ret = inode_state_will_create;
192931db9f7cSAlexander Block } else if (right_gen == gen) {
193031db9f7cSAlexander Block if (ino < sctx->send_progress)
193131db9f7cSAlexander Block ret = inode_state_did_delete;
193231db9f7cSAlexander Block else
193331db9f7cSAlexander Block ret = inode_state_will_delete;
193431db9f7cSAlexander Block } else {
193531db9f7cSAlexander Block ret = -ENOENT;
193631db9f7cSAlexander Block }
193731db9f7cSAlexander Block } else if (!left_ret) {
193831db9f7cSAlexander Block if (left_gen == gen) {
193931db9f7cSAlexander Block if (ino < sctx->send_progress)
194031db9f7cSAlexander Block ret = inode_state_did_create;
194131db9f7cSAlexander Block else
194231db9f7cSAlexander Block ret = inode_state_will_create;
194331db9f7cSAlexander Block } else {
194431db9f7cSAlexander Block ret = -ENOENT;
194531db9f7cSAlexander Block }
194631db9f7cSAlexander Block } else if (!right_ret) {
194731db9f7cSAlexander Block if (right_gen == gen) {
194831db9f7cSAlexander Block if (ino < sctx->send_progress)
194931db9f7cSAlexander Block ret = inode_state_did_delete;
195031db9f7cSAlexander Block else
195131db9f7cSAlexander Block ret = inode_state_will_delete;
195231db9f7cSAlexander Block } else {
195331db9f7cSAlexander Block ret = -ENOENT;
195431db9f7cSAlexander Block }
195531db9f7cSAlexander Block } else {
195631db9f7cSAlexander Block ret = -ENOENT;
195731db9f7cSAlexander Block }
195831db9f7cSAlexander Block
195931db9f7cSAlexander Block out:
196031db9f7cSAlexander Block return ret;
196131db9f7cSAlexander Block }
196231db9f7cSAlexander Block
is_inode_existent(struct send_ctx * sctx,u64 ino,u64 gen,u64 * send_gen,u64 * parent_gen)1963498581f3SFilipe Manana static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen,
1964498581f3SFilipe Manana u64 *send_gen, u64 *parent_gen)
196531db9f7cSAlexander Block {
196631db9f7cSAlexander Block int ret;
196731db9f7cSAlexander Block
19684dd9920dSRobbie Ko if (ino == BTRFS_FIRST_FREE_OBJECTID)
19694dd9920dSRobbie Ko return 1;
19704dd9920dSRobbie Ko
1971498581f3SFilipe Manana ret = get_cur_inode_state(sctx, ino, gen, send_gen, parent_gen);
197231db9f7cSAlexander Block if (ret < 0)
197331db9f7cSAlexander Block goto out;
197431db9f7cSAlexander Block
197531db9f7cSAlexander Block if (ret == inode_state_no_change ||
197631db9f7cSAlexander Block ret == inode_state_did_create ||
197731db9f7cSAlexander Block ret == inode_state_will_delete)
197831db9f7cSAlexander Block ret = 1;
197931db9f7cSAlexander Block else
198031db9f7cSAlexander Block ret = 0;
198131db9f7cSAlexander Block
198231db9f7cSAlexander Block out:
198331db9f7cSAlexander Block return ret;
198431db9f7cSAlexander Block }
198531db9f7cSAlexander Block
198631db9f7cSAlexander Block /*
198731db9f7cSAlexander Block * Helper function to lookup a dir item in a dir.
198831db9f7cSAlexander Block */
lookup_dir_item_inode(struct btrfs_root * root,u64 dir,const char * name,int name_len,u64 * found_inode)198931db9f7cSAlexander Block static int lookup_dir_item_inode(struct btrfs_root *root,
199031db9f7cSAlexander Block u64 dir, const char *name, int name_len,
1991eab67c06SOmar Sandoval u64 *found_inode)
199231db9f7cSAlexander Block {
199331db9f7cSAlexander Block int ret = 0;
199431db9f7cSAlexander Block struct btrfs_dir_item *di;
199531db9f7cSAlexander Block struct btrfs_key key;
199631db9f7cSAlexander Block struct btrfs_path *path;
19976db75318SSweet Tea Dorminy struct fscrypt_str name_str = FSTR_INIT((char *)name, name_len);
199831db9f7cSAlexander Block
199931db9f7cSAlexander Block path = alloc_path_for_send();
200031db9f7cSAlexander Block if (!path)
200131db9f7cSAlexander Block return -ENOMEM;
200231db9f7cSAlexander Block
2003e43eec81SSweet Tea Dorminy di = btrfs_lookup_dir_item(NULL, root, path, dir, &name_str, 0);
20043cf5068fSLiu Bo if (IS_ERR_OR_NULL(di)) {
20053cf5068fSLiu Bo ret = di ? PTR_ERR(di) : -ENOENT;
200631db9f7cSAlexander Block goto out;
200731db9f7cSAlexander Block }
200831db9f7cSAlexander Block btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
20091af56070SFilipe Manana if (key.type == BTRFS_ROOT_ITEM_KEY) {
20101af56070SFilipe Manana ret = -ENOENT;
20111af56070SFilipe Manana goto out;
20121af56070SFilipe Manana }
201331db9f7cSAlexander Block *found_inode = key.objectid;
201431db9f7cSAlexander Block
201531db9f7cSAlexander Block out:
201631db9f7cSAlexander Block btrfs_free_path(path);
201731db9f7cSAlexander Block return ret;
201831db9f7cSAlexander Block }
201931db9f7cSAlexander Block
2020766702efSAlexander Block /*
2021766702efSAlexander Block * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
2022766702efSAlexander Block * generation of the parent dir and the name of the dir entry.
2023766702efSAlexander Block */
get_first_ref(struct btrfs_root * root,u64 ino,u64 * dir,u64 * dir_gen,struct fs_path * name)2024924794c9STsutomu Itoh static int get_first_ref(struct btrfs_root *root, u64 ino,
202531db9f7cSAlexander Block u64 *dir, u64 *dir_gen, struct fs_path *name)
202631db9f7cSAlexander Block {
202731db9f7cSAlexander Block int ret;
202831db9f7cSAlexander Block struct btrfs_key key;
202931db9f7cSAlexander Block struct btrfs_key found_key;
203031db9f7cSAlexander Block struct btrfs_path *path;
203131db9f7cSAlexander Block int len;
203296b5bd77SJan Schmidt u64 parent_dir;
203331db9f7cSAlexander Block
203431db9f7cSAlexander Block path = alloc_path_for_send();
203531db9f7cSAlexander Block if (!path)
203631db9f7cSAlexander Block return -ENOMEM;
203731db9f7cSAlexander Block
203831db9f7cSAlexander Block key.objectid = ino;
203931db9f7cSAlexander Block key.type = BTRFS_INODE_REF_KEY;
204031db9f7cSAlexander Block key.offset = 0;
204131db9f7cSAlexander Block
204231db9f7cSAlexander Block ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
204331db9f7cSAlexander Block if (ret < 0)
204431db9f7cSAlexander Block goto out;
204531db9f7cSAlexander Block if (!ret)
204631db9f7cSAlexander Block btrfs_item_key_to_cpu(path->nodes[0], &found_key,
204731db9f7cSAlexander Block path->slots[0]);
204896b5bd77SJan Schmidt if (ret || found_key.objectid != ino ||
204996b5bd77SJan Schmidt (found_key.type != BTRFS_INODE_REF_KEY &&
205096b5bd77SJan Schmidt found_key.type != BTRFS_INODE_EXTREF_KEY)) {
205131db9f7cSAlexander Block ret = -ENOENT;
205231db9f7cSAlexander Block goto out;
205331db9f7cSAlexander Block }
205431db9f7cSAlexander Block
205551a60253SFilipe Manana if (found_key.type == BTRFS_INODE_REF_KEY) {
205696b5bd77SJan Schmidt struct btrfs_inode_ref *iref;
205731db9f7cSAlexander Block iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
205831db9f7cSAlexander Block struct btrfs_inode_ref);
205931db9f7cSAlexander Block len = btrfs_inode_ref_name_len(path->nodes[0], iref);
206031db9f7cSAlexander Block ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
206196b5bd77SJan Schmidt (unsigned long)(iref + 1),
206296b5bd77SJan Schmidt len);
206396b5bd77SJan Schmidt parent_dir = found_key.offset;
206496b5bd77SJan Schmidt } else {
206596b5bd77SJan Schmidt struct btrfs_inode_extref *extref;
206696b5bd77SJan Schmidt extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
206796b5bd77SJan Schmidt struct btrfs_inode_extref);
206896b5bd77SJan Schmidt len = btrfs_inode_extref_name_len(path->nodes[0], extref);
206996b5bd77SJan Schmidt ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
207096b5bd77SJan Schmidt (unsigned long)&extref->name, len);
207196b5bd77SJan Schmidt parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
207296b5bd77SJan Schmidt }
207331db9f7cSAlexander Block if (ret < 0)
207431db9f7cSAlexander Block goto out;
207531db9f7cSAlexander Block btrfs_release_path(path);
207631db9f7cSAlexander Block
2077b46ab97bSFilipe Manana if (dir_gen) {
20787e93f6dcSBingJing Chang ret = get_inode_gen(root, parent_dir, dir_gen);
207931db9f7cSAlexander Block if (ret < 0)
208031db9f7cSAlexander Block goto out;
2081b46ab97bSFilipe Manana }
208231db9f7cSAlexander Block
208396b5bd77SJan Schmidt *dir = parent_dir;
208431db9f7cSAlexander Block
208531db9f7cSAlexander Block out:
208631db9f7cSAlexander Block btrfs_free_path(path);
208731db9f7cSAlexander Block return ret;
208831db9f7cSAlexander Block }
208931db9f7cSAlexander Block
is_first_ref(struct btrfs_root * root,u64 ino,u64 dir,const char * name,int name_len)2090924794c9STsutomu Itoh static int is_first_ref(struct btrfs_root *root,
209131db9f7cSAlexander Block u64 ino, u64 dir,
209231db9f7cSAlexander Block const char *name, int name_len)
209331db9f7cSAlexander Block {
209431db9f7cSAlexander Block int ret;
209531db9f7cSAlexander Block struct fs_path *tmp_name;
209631db9f7cSAlexander Block u64 tmp_dir;
209731db9f7cSAlexander Block
2098924794c9STsutomu Itoh tmp_name = fs_path_alloc();
209931db9f7cSAlexander Block if (!tmp_name)
210031db9f7cSAlexander Block return -ENOMEM;
210131db9f7cSAlexander Block
2102b46ab97bSFilipe Manana ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
210331db9f7cSAlexander Block if (ret < 0)
210431db9f7cSAlexander Block goto out;
210531db9f7cSAlexander Block
2106b9291affSAlexander Block if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
210731db9f7cSAlexander Block ret = 0;
210831db9f7cSAlexander Block goto out;
210931db9f7cSAlexander Block }
211031db9f7cSAlexander Block
2111e938c8adSAlexander Block ret = !memcmp(tmp_name->start, name, name_len);
211231db9f7cSAlexander Block
211331db9f7cSAlexander Block out:
2114924794c9STsutomu Itoh fs_path_free(tmp_name);
211531db9f7cSAlexander Block return ret;
211631db9f7cSAlexander Block }
211731db9f7cSAlexander Block
2118766702efSAlexander Block /*
2119766702efSAlexander Block * Used by process_recorded_refs to determine if a new ref would overwrite an
2120766702efSAlexander Block * already existing ref. In case it detects an overwrite, it returns the
2121766702efSAlexander Block * inode/gen in who_ino/who_gen.
2122766702efSAlexander Block * When an overwrite is detected, process_recorded_refs does proper orphanizing
2123766702efSAlexander Block * to make sure later references to the overwritten inode are possible.
2124766702efSAlexander Block * Orphanizing is however only required for the first ref of an inode.
2125766702efSAlexander Block * process_recorded_refs does an additional is_first_ref check to see if
2126766702efSAlexander Block * orphanizing is really required.
2127766702efSAlexander Block */
will_overwrite_ref(struct send_ctx * sctx,u64 dir,u64 dir_gen,const char * name,int name_len,u64 * who_ino,u64 * who_gen,u64 * who_mode)212831db9f7cSAlexander Block static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
212931db9f7cSAlexander Block const char *name, int name_len,
2130f5962781SFilipe Manana u64 *who_ino, u64 *who_gen, u64 *who_mode)
213131db9f7cSAlexander Block {
2132b3047a42SFilipe Manana int ret;
2133498581f3SFilipe Manana u64 parent_root_dir_gen;
213431db9f7cSAlexander Block u64 other_inode = 0;
21357e93f6dcSBingJing Chang struct btrfs_inode_info info;
213631db9f7cSAlexander Block
213731db9f7cSAlexander Block if (!sctx->parent_root)
2138b3047a42SFilipe Manana return 0;
213931db9f7cSAlexander Block
2140498581f3SFilipe Manana ret = is_inode_existent(sctx, dir, dir_gen, NULL, &parent_root_dir_gen);
214131db9f7cSAlexander Block if (ret <= 0)
2142b3047a42SFilipe Manana return 0;
214331db9f7cSAlexander Block
2144ebdad913SJosef Bacik /*
2145ebdad913SJosef Bacik * If we have a parent root we need to verify that the parent dir was
214601327610SNicholas D Steeves * not deleted and then re-created, if it was then we have no overwrite
2147ebdad913SJosef Bacik * and we can just unlink this entry.
2148498581f3SFilipe Manana *
2149498581f3SFilipe Manana * @parent_root_dir_gen was set to 0 if the inode does not exist in the
2150498581f3SFilipe Manana * parent root.
2151ebdad913SJosef Bacik */
2152498581f3SFilipe Manana if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID &&
2153498581f3SFilipe Manana parent_root_dir_gen != dir_gen)
2154b3047a42SFilipe Manana return 0;
2155ebdad913SJosef Bacik
215631db9f7cSAlexander Block ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
2157eab67c06SOmar Sandoval &other_inode);
2158b3047a42SFilipe Manana if (ret == -ENOENT)
2159b3047a42SFilipe Manana return 0;
2160b3047a42SFilipe Manana else if (ret < 0)
2161b3047a42SFilipe Manana return ret;
216231db9f7cSAlexander Block
2163766702efSAlexander Block /*
2164766702efSAlexander Block * Check if the overwritten ref was already processed. If yes, the ref
2165766702efSAlexander Block * was already unlinked/moved, so we can safely assume that we will not
2166766702efSAlexander Block * overwrite anything at this point in time.
2167766702efSAlexander Block */
2168801bec36SRobbie Ko if (other_inode > sctx->send_progress ||
2169801bec36SRobbie Ko is_waiting_for_move(sctx, other_inode)) {
21707e93f6dcSBingJing Chang ret = get_inode_info(sctx->parent_root, other_inode, &info);
217131db9f7cSAlexander Block if (ret < 0)
2172b3047a42SFilipe Manana return ret;
217331db9f7cSAlexander Block
217431db9f7cSAlexander Block *who_ino = other_inode;
21757e93f6dcSBingJing Chang *who_gen = info.gen;
21767e93f6dcSBingJing Chang *who_mode = info.mode;
2177b3047a42SFilipe Manana return 1;
217831db9f7cSAlexander Block }
217931db9f7cSAlexander Block
2180b3047a42SFilipe Manana return 0;
218131db9f7cSAlexander Block }
218231db9f7cSAlexander Block
2183766702efSAlexander Block /*
2184766702efSAlexander Block * Checks if the ref was overwritten by an already processed inode. This is
2185766702efSAlexander Block * used by __get_cur_name_and_parent to find out if the ref was orphanized and
2186766702efSAlexander Block * thus the orphan name needs be used.
2187766702efSAlexander Block * process_recorded_refs also uses it to avoid unlinking of refs that were
2188766702efSAlexander Block * overwritten.
2189766702efSAlexander Block */
did_overwrite_ref(struct send_ctx * sctx,u64 dir,u64 dir_gen,u64 ino,u64 ino_gen,const char * name,int name_len)219031db9f7cSAlexander Block static int did_overwrite_ref(struct send_ctx *sctx,
219131db9f7cSAlexander Block u64 dir, u64 dir_gen,
219231db9f7cSAlexander Block u64 ino, u64 ino_gen,
219331db9f7cSAlexander Block const char *name, int name_len)
219431db9f7cSAlexander Block {
2195e739ba30SFilipe Manana int ret;
219631db9f7cSAlexander Block u64 ow_inode;
2197cb689481SFilipe Manana u64 ow_gen = 0;
2198498581f3SFilipe Manana u64 send_root_dir_gen;
219931db9f7cSAlexander Block
220031db9f7cSAlexander Block if (!sctx->parent_root)
2201e739ba30SFilipe Manana return 0;
220231db9f7cSAlexander Block
2203498581f3SFilipe Manana ret = is_inode_existent(sctx, dir, dir_gen, &send_root_dir_gen, NULL);
220431db9f7cSAlexander Block if (ret <= 0)
2205e739ba30SFilipe Manana return ret;
220631db9f7cSAlexander Block
2207498581f3SFilipe Manana /*
2208498581f3SFilipe Manana * @send_root_dir_gen was set to 0 if the inode does not exist in the
2209498581f3SFilipe Manana * send root.
2210498581f3SFilipe Manana */
2211498581f3SFilipe Manana if (dir != BTRFS_FIRST_FREE_OBJECTID && send_root_dir_gen != dir_gen)
2212e739ba30SFilipe Manana return 0;
221301914101SRobbie Ko
221431db9f7cSAlexander Block /* check if the ref was overwritten by another ref */
221531db9f7cSAlexander Block ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
2216eab67c06SOmar Sandoval &ow_inode);
2217e739ba30SFilipe Manana if (ret == -ENOENT) {
221831db9f7cSAlexander Block /* was never and will never be overwritten */
2219e739ba30SFilipe Manana return 0;
2220e739ba30SFilipe Manana } else if (ret < 0) {
2221e739ba30SFilipe Manana return ret;
222231db9f7cSAlexander Block }
222331db9f7cSAlexander Block
2224cb689481SFilipe Manana if (ow_inode == ino) {
2225cb689481SFilipe Manana ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
222631db9f7cSAlexander Block if (ret < 0)
2227e739ba30SFilipe Manana return ret;
222831db9f7cSAlexander Block
2229cb689481SFilipe Manana /* It's the same inode, so no overwrite happened. */
2230cb689481SFilipe Manana if (ow_gen == ino_gen)
2231e739ba30SFilipe Manana return 0;
2232cb689481SFilipe Manana }
223331db9f7cSAlexander Block
22348b191a68SFilipe Manana /*
22358b191a68SFilipe Manana * We know that it is or will be overwritten. Check this now.
22368b191a68SFilipe Manana * The current inode being processed might have been the one that caused
2237b786f16aSFilipe Manana * inode 'ino' to be orphanized, therefore check if ow_inode matches
2238b786f16aSFilipe Manana * the current inode being processed.
22398b191a68SFilipe Manana */
2240cb689481SFilipe Manana if (ow_inode < sctx->send_progress)
2241e739ba30SFilipe Manana return 1;
224231db9f7cSAlexander Block
2243cb689481SFilipe Manana if (ino != sctx->cur_ino && ow_inode == sctx->cur_ino) {
2244cb689481SFilipe Manana if (ow_gen == 0) {
2245cb689481SFilipe Manana ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
2246cb689481SFilipe Manana if (ret < 0)
2247cb689481SFilipe Manana return ret;
2248cb689481SFilipe Manana }
2249cb689481SFilipe Manana if (ow_gen == sctx->cur_inode_gen)
2250cb689481SFilipe Manana return 1;
2251cb689481SFilipe Manana }
2252cb689481SFilipe Manana
2253e739ba30SFilipe Manana return 0;
225431db9f7cSAlexander Block }
225531db9f7cSAlexander Block
2256766702efSAlexander Block /*
2257766702efSAlexander Block * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
2258766702efSAlexander Block * that got overwritten. This is used by process_recorded_refs to determine
2259766702efSAlexander Block * if it has to use the path as returned by get_cur_path or the orphan name.
2260766702efSAlexander Block */
did_overwrite_first_ref(struct send_ctx * sctx,u64 ino,u64 gen)226131db9f7cSAlexander Block static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
226231db9f7cSAlexander Block {
226331db9f7cSAlexander Block int ret = 0;
226431db9f7cSAlexander Block struct fs_path *name = NULL;
226531db9f7cSAlexander Block u64 dir;
226631db9f7cSAlexander Block u64 dir_gen;
226731db9f7cSAlexander Block
226831db9f7cSAlexander Block if (!sctx->parent_root)
226931db9f7cSAlexander Block goto out;
227031db9f7cSAlexander Block
2271924794c9STsutomu Itoh name = fs_path_alloc();
227231db9f7cSAlexander Block if (!name)
227331db9f7cSAlexander Block return -ENOMEM;
227431db9f7cSAlexander Block
2275924794c9STsutomu Itoh ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
227631db9f7cSAlexander Block if (ret < 0)
227731db9f7cSAlexander Block goto out;
227831db9f7cSAlexander Block
227931db9f7cSAlexander Block ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
228031db9f7cSAlexander Block name->start, fs_path_len(name));
228131db9f7cSAlexander Block
228231db9f7cSAlexander Block out:
2283924794c9STsutomu Itoh fs_path_free(name);
228431db9f7cSAlexander Block return ret;
228531db9f7cSAlexander Block }
228631db9f7cSAlexander Block
name_cache_search(struct send_ctx * sctx,u64 ino,u64 gen)2287c48545deSFilipe Manana static inline struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
228831db9f7cSAlexander Block u64 ino, u64 gen)
228931db9f7cSAlexander Block {
2290c48545deSFilipe Manana struct btrfs_lru_cache_entry *entry;
229131db9f7cSAlexander Block
2292c48545deSFilipe Manana entry = btrfs_lru_cache_lookup(&sctx->name_cache, ino, gen);
2293c48545deSFilipe Manana if (!entry)
229431db9f7cSAlexander Block return NULL;
229531db9f7cSAlexander Block
2296c48545deSFilipe Manana return container_of(entry, struct name_cache_entry, entry);
229731db9f7cSAlexander Block }
229831db9f7cSAlexander Block
2299766702efSAlexander Block /*
2300766702efSAlexander Block * Used by get_cur_path for each ref up to the root.
2301766702efSAlexander Block * Returns 0 if it succeeded.
2302766702efSAlexander Block * Returns 1 if the inode is not existent or got overwritten. In that case, the
2303766702efSAlexander Block * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2304766702efSAlexander Block * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2305766702efSAlexander Block * Returns <0 in case of error.
2306766702efSAlexander Block */
__get_cur_name_and_parent(struct send_ctx * sctx,u64 ino,u64 gen,u64 * parent_ino,u64 * parent_gen,struct fs_path * dest)230731db9f7cSAlexander Block static int __get_cur_name_and_parent(struct send_ctx *sctx,
230831db9f7cSAlexander Block u64 ino, u64 gen,
230931db9f7cSAlexander Block u64 *parent_ino,
231031db9f7cSAlexander Block u64 *parent_gen,
231131db9f7cSAlexander Block struct fs_path *dest)
231231db9f7cSAlexander Block {
231331db9f7cSAlexander Block int ret;
231431db9f7cSAlexander Block int nce_ret;
2315c48545deSFilipe Manana struct name_cache_entry *nce;
231631db9f7cSAlexander Block
2317766702efSAlexander Block /*
2318766702efSAlexander Block * First check if we already did a call to this function with the same
2319766702efSAlexander Block * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2320766702efSAlexander Block * return the cached result.
2321766702efSAlexander Block */
232231db9f7cSAlexander Block nce = name_cache_search(sctx, ino, gen);
232331db9f7cSAlexander Block if (nce) {
232431db9f7cSAlexander Block if (ino < sctx->send_progress && nce->need_later_update) {
2325c48545deSFilipe Manana btrfs_lru_cache_remove(&sctx->name_cache, &nce->entry);
232631db9f7cSAlexander Block nce = NULL;
232731db9f7cSAlexander Block } else {
232831db9f7cSAlexander Block *parent_ino = nce->parent_ino;
232931db9f7cSAlexander Block *parent_gen = nce->parent_gen;
233031db9f7cSAlexander Block ret = fs_path_add(dest, nce->name, nce->name_len);
233131db9f7cSAlexander Block if (ret < 0)
233231db9f7cSAlexander Block goto out;
233331db9f7cSAlexander Block ret = nce->ret;
233431db9f7cSAlexander Block goto out;
233531db9f7cSAlexander Block }
233631db9f7cSAlexander Block }
233731db9f7cSAlexander Block
2338766702efSAlexander Block /*
2339766702efSAlexander Block * If the inode is not existent yet, add the orphan name and return 1.
2340766702efSAlexander Block * This should only happen for the parent dir that we determine in
23410d8869fbSFilipe Manana * record_new_ref_if_needed().
2342766702efSAlexander Block */
2343498581f3SFilipe Manana ret = is_inode_existent(sctx, ino, gen, NULL, NULL);
234431db9f7cSAlexander Block if (ret < 0)
234531db9f7cSAlexander Block goto out;
234631db9f7cSAlexander Block
234731db9f7cSAlexander Block if (!ret) {
234831db9f7cSAlexander Block ret = gen_unique_name(sctx, ino, gen, dest);
234931db9f7cSAlexander Block if (ret < 0)
235031db9f7cSAlexander Block goto out;
235131db9f7cSAlexander Block ret = 1;
235231db9f7cSAlexander Block goto out_cache;
235331db9f7cSAlexander Block }
235431db9f7cSAlexander Block
2355766702efSAlexander Block /*
2356766702efSAlexander Block * Depending on whether the inode was already processed or not, use
2357766702efSAlexander Block * send_root or parent_root for ref lookup.
2358766702efSAlexander Block */
2359bf0d1f44SFilipe Manana if (ino < sctx->send_progress)
2360924794c9STsutomu Itoh ret = get_first_ref(sctx->send_root, ino,
236131db9f7cSAlexander Block parent_ino, parent_gen, dest);
236231db9f7cSAlexander Block else
2363924794c9STsutomu Itoh ret = get_first_ref(sctx->parent_root, ino,
236431db9f7cSAlexander Block parent_ino, parent_gen, dest);
236531db9f7cSAlexander Block if (ret < 0)
236631db9f7cSAlexander Block goto out;
236731db9f7cSAlexander Block
2368766702efSAlexander Block /*
2369766702efSAlexander Block * Check if the ref was overwritten by an inode's ref that was processed
2370766702efSAlexander Block * earlier. If yes, treat as orphan and return 1.
2371766702efSAlexander Block */
237231db9f7cSAlexander Block ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
237331db9f7cSAlexander Block dest->start, dest->end - dest->start);
237431db9f7cSAlexander Block if (ret < 0)
237531db9f7cSAlexander Block goto out;
237631db9f7cSAlexander Block if (ret) {
237731db9f7cSAlexander Block fs_path_reset(dest);
237831db9f7cSAlexander Block ret = gen_unique_name(sctx, ino, gen, dest);
237931db9f7cSAlexander Block if (ret < 0)
238031db9f7cSAlexander Block goto out;
238131db9f7cSAlexander Block ret = 1;
238231db9f7cSAlexander Block }
238331db9f7cSAlexander Block
238431db9f7cSAlexander Block out_cache:
2385766702efSAlexander Block /*
2386766702efSAlexander Block * Store the result of the lookup in the name cache.
2387766702efSAlexander Block */
2388e780b0d1SDavid Sterba nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
238931db9f7cSAlexander Block if (!nce) {
239031db9f7cSAlexander Block ret = -ENOMEM;
239131db9f7cSAlexander Block goto out;
239231db9f7cSAlexander Block }
239331db9f7cSAlexander Block
2394c48545deSFilipe Manana nce->entry.key = ino;
2395c48545deSFilipe Manana nce->entry.gen = gen;
239631db9f7cSAlexander Block nce->parent_ino = *parent_ino;
239731db9f7cSAlexander Block nce->parent_gen = *parent_gen;
239831db9f7cSAlexander Block nce->name_len = fs_path_len(dest);
239931db9f7cSAlexander Block nce->ret = ret;
240031db9f7cSAlexander Block strcpy(nce->name, dest->start);
240131db9f7cSAlexander Block
240231db9f7cSAlexander Block if (ino < sctx->send_progress)
240331db9f7cSAlexander Block nce->need_later_update = 0;
240431db9f7cSAlexander Block else
240531db9f7cSAlexander Block nce->need_later_update = 1;
240631db9f7cSAlexander Block
2407c48545deSFilipe Manana nce_ret = btrfs_lru_cache_store(&sctx->name_cache, &nce->entry, GFP_KERNEL);
2408c48545deSFilipe Manana if (nce_ret < 0) {
2409c48545deSFilipe Manana kfree(nce);
241031db9f7cSAlexander Block ret = nce_ret;
2411c48545deSFilipe Manana }
241231db9f7cSAlexander Block
241331db9f7cSAlexander Block out:
241431db9f7cSAlexander Block return ret;
241531db9f7cSAlexander Block }
241631db9f7cSAlexander Block
241731db9f7cSAlexander Block /*
241831db9f7cSAlexander Block * Magic happens here. This function returns the first ref to an inode as it
241931db9f7cSAlexander Block * would look like while receiving the stream at this point in time.
242031db9f7cSAlexander Block * We walk the path up to the root. For every inode in between, we check if it
242131db9f7cSAlexander Block * was already processed/sent. If yes, we continue with the parent as found
242231db9f7cSAlexander Block * in send_root. If not, we continue with the parent as found in parent_root.
242331db9f7cSAlexander Block * If we encounter an inode that was deleted at this point in time, we use the
242431db9f7cSAlexander Block * inodes "orphan" name instead of the real name and stop. Same with new inodes
242531db9f7cSAlexander Block * that were not created yet and overwritten inodes/refs.
242631db9f7cSAlexander Block *
242752042d8eSAndrea Gelmini * When do we have orphan inodes:
242831db9f7cSAlexander Block * 1. When an inode is freshly created and thus no valid refs are available yet
242931db9f7cSAlexander Block * 2. When a directory lost all it's refs (deleted) but still has dir items
243031db9f7cSAlexander Block * inside which were not processed yet (pending for move/delete). If anyone
243131db9f7cSAlexander Block * tried to get the path to the dir items, it would get a path inside that
243231db9f7cSAlexander Block * orphan directory.
243331db9f7cSAlexander Block * 3. When an inode is moved around or gets new links, it may overwrite the ref
243431db9f7cSAlexander Block * of an unprocessed inode. If in that case the first ref would be
243531db9f7cSAlexander Block * overwritten, the overwritten inode gets "orphanized". Later when we
243631db9f7cSAlexander Block * process this overwritten inode, it is restored at a new place by moving
243731db9f7cSAlexander Block * the orphan inode.
243831db9f7cSAlexander Block *
243931db9f7cSAlexander Block * sctx->send_progress tells this function at which point in time receiving
244031db9f7cSAlexander Block * would be.
244131db9f7cSAlexander Block */
get_cur_path(struct send_ctx * sctx,u64 ino,u64 gen,struct fs_path * dest)244231db9f7cSAlexander Block static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
244331db9f7cSAlexander Block struct fs_path *dest)
244431db9f7cSAlexander Block {
244531db9f7cSAlexander Block int ret = 0;
244631db9f7cSAlexander Block struct fs_path *name = NULL;
244731db9f7cSAlexander Block u64 parent_inode = 0;
244831db9f7cSAlexander Block u64 parent_gen = 0;
244931db9f7cSAlexander Block int stop = 0;
245031db9f7cSAlexander Block
2451924794c9STsutomu Itoh name = fs_path_alloc();
245231db9f7cSAlexander Block if (!name) {
245331db9f7cSAlexander Block ret = -ENOMEM;
245431db9f7cSAlexander Block goto out;
245531db9f7cSAlexander Block }
245631db9f7cSAlexander Block
245731db9f7cSAlexander Block dest->reversed = 1;
245831db9f7cSAlexander Block fs_path_reset(dest);
245931db9f7cSAlexander Block
246031db9f7cSAlexander Block while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
24618b191a68SFilipe Manana struct waiting_dir_move *wdm;
24628b191a68SFilipe Manana
246331db9f7cSAlexander Block fs_path_reset(name);
246431db9f7cSAlexander Block
24650b3f407eSFilipe Manana if (is_waiting_for_rm(sctx, ino, gen)) {
24669dc44214SFilipe Manana ret = gen_unique_name(sctx, ino, gen, name);
24679dc44214SFilipe Manana if (ret < 0)
24689dc44214SFilipe Manana goto out;
24699dc44214SFilipe Manana ret = fs_path_add_path(dest, name);
24709dc44214SFilipe Manana break;
24719dc44214SFilipe Manana }
24729dc44214SFilipe Manana
24738b191a68SFilipe Manana wdm = get_waiting_dir_move(sctx, ino);
24748b191a68SFilipe Manana if (wdm && wdm->orphanized) {
24758b191a68SFilipe Manana ret = gen_unique_name(sctx, ino, gen, name);
24768b191a68SFilipe Manana stop = 1;
24778b191a68SFilipe Manana } else if (wdm) {
2478bf0d1f44SFilipe Manana ret = get_first_ref(sctx->parent_root, ino,
247931db9f7cSAlexander Block &parent_inode, &parent_gen, name);
2480bf0d1f44SFilipe Manana } else {
2481bf0d1f44SFilipe Manana ret = __get_cur_name_and_parent(sctx, ino, gen,
2482bf0d1f44SFilipe Manana &parent_inode,
2483bf0d1f44SFilipe Manana &parent_gen, name);
248431db9f7cSAlexander Block if (ret)
248531db9f7cSAlexander Block stop = 1;
2486bf0d1f44SFilipe Manana }
248731db9f7cSAlexander Block
2488bf0d1f44SFilipe Manana if (ret < 0)
2489bf0d1f44SFilipe Manana goto out;
24909f03740aSFilipe David Borba Manana
249131db9f7cSAlexander Block ret = fs_path_add_path(dest, name);
249231db9f7cSAlexander Block if (ret < 0)
249331db9f7cSAlexander Block goto out;
249431db9f7cSAlexander Block
249531db9f7cSAlexander Block ino = parent_inode;
249631db9f7cSAlexander Block gen = parent_gen;
249731db9f7cSAlexander Block }
249831db9f7cSAlexander Block
249931db9f7cSAlexander Block out:
2500924794c9STsutomu Itoh fs_path_free(name);
250131db9f7cSAlexander Block if (!ret)
250231db9f7cSAlexander Block fs_path_unreverse(dest);
250331db9f7cSAlexander Block return ret;
250431db9f7cSAlexander Block }
250531db9f7cSAlexander Block
250631db9f7cSAlexander Block /*
250731db9f7cSAlexander Block * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
250831db9f7cSAlexander Block */
send_subvol_begin(struct send_ctx * sctx)250931db9f7cSAlexander Block static int send_subvol_begin(struct send_ctx *sctx)
251031db9f7cSAlexander Block {
251131db9f7cSAlexander Block int ret;
251231db9f7cSAlexander Block struct btrfs_root *send_root = sctx->send_root;
251331db9f7cSAlexander Block struct btrfs_root *parent_root = sctx->parent_root;
251431db9f7cSAlexander Block struct btrfs_path *path;
251531db9f7cSAlexander Block struct btrfs_key key;
251631db9f7cSAlexander Block struct btrfs_root_ref *ref;
251731db9f7cSAlexander Block struct extent_buffer *leaf;
251831db9f7cSAlexander Block char *name = NULL;
251931db9f7cSAlexander Block int namelen;
252031db9f7cSAlexander Block
2521ffcfaf81SWang Shilong path = btrfs_alloc_path();
252231db9f7cSAlexander Block if (!path)
252331db9f7cSAlexander Block return -ENOMEM;
252431db9f7cSAlexander Block
2525e780b0d1SDavid Sterba name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
252631db9f7cSAlexander Block if (!name) {
252731db9f7cSAlexander Block btrfs_free_path(path);
252831db9f7cSAlexander Block return -ENOMEM;
252931db9f7cSAlexander Block }
253031db9f7cSAlexander Block
25314fd786e6SMisono Tomohiro key.objectid = send_root->root_key.objectid;
253231db9f7cSAlexander Block key.type = BTRFS_ROOT_BACKREF_KEY;
253331db9f7cSAlexander Block key.offset = 0;
253431db9f7cSAlexander Block
253531db9f7cSAlexander Block ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
253631db9f7cSAlexander Block &key, path, 1, 0);
253731db9f7cSAlexander Block if (ret < 0)
253831db9f7cSAlexander Block goto out;
253931db9f7cSAlexander Block if (ret) {
254031db9f7cSAlexander Block ret = -ENOENT;
254131db9f7cSAlexander Block goto out;
254231db9f7cSAlexander Block }
254331db9f7cSAlexander Block
254431db9f7cSAlexander Block leaf = path->nodes[0];
254531db9f7cSAlexander Block btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
254631db9f7cSAlexander Block if (key.type != BTRFS_ROOT_BACKREF_KEY ||
25474fd786e6SMisono Tomohiro key.objectid != send_root->root_key.objectid) {
254831db9f7cSAlexander Block ret = -ENOENT;
254931db9f7cSAlexander Block goto out;
255031db9f7cSAlexander Block }
255131db9f7cSAlexander Block ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
255231db9f7cSAlexander Block namelen = btrfs_root_ref_name_len(leaf, ref);
255331db9f7cSAlexander Block read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
255431db9f7cSAlexander Block btrfs_release_path(path);
255531db9f7cSAlexander Block
255631db9f7cSAlexander Block if (parent_root) {
255731db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
255831db9f7cSAlexander Block if (ret < 0)
255931db9f7cSAlexander Block goto out;
256031db9f7cSAlexander Block } else {
256131db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
256231db9f7cSAlexander Block if (ret < 0)
256331db9f7cSAlexander Block goto out;
256431db9f7cSAlexander Block }
256531db9f7cSAlexander Block
256631db9f7cSAlexander Block TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2567b96b1db0SRobin Ruede
2568b96b1db0SRobin Ruede if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2569b96b1db0SRobin Ruede TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2570b96b1db0SRobin Ruede sctx->send_root->root_item.received_uuid);
2571b96b1db0SRobin Ruede else
257231db9f7cSAlexander Block TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
257331db9f7cSAlexander Block sctx->send_root->root_item.uuid);
2574b96b1db0SRobin Ruede
257531db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
257609e3a288SDavid Sterba btrfs_root_ctransid(&sctx->send_root->root_item));
257731db9f7cSAlexander Block if (parent_root) {
257837b8d27dSJosef Bacik if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
257931db9f7cSAlexander Block TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
258037b8d27dSJosef Bacik parent_root->root_item.received_uuid);
258137b8d27dSJosef Bacik else
258237b8d27dSJosef Bacik TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
258337b8d27dSJosef Bacik parent_root->root_item.uuid);
258431db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
258509e3a288SDavid Sterba btrfs_root_ctransid(&sctx->parent_root->root_item));
258631db9f7cSAlexander Block }
258731db9f7cSAlexander Block
258831db9f7cSAlexander Block ret = send_cmd(sctx);
258931db9f7cSAlexander Block
259031db9f7cSAlexander Block tlv_put_failure:
259131db9f7cSAlexander Block out:
259231db9f7cSAlexander Block btrfs_free_path(path);
259331db9f7cSAlexander Block kfree(name);
259431db9f7cSAlexander Block return ret;
259531db9f7cSAlexander Block }
259631db9f7cSAlexander Block
send_truncate(struct send_ctx * sctx,u64 ino,u64 gen,u64 size)259731db9f7cSAlexander Block static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
259831db9f7cSAlexander Block {
259904ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
260031db9f7cSAlexander Block int ret = 0;
260131db9f7cSAlexander Block struct fs_path *p;
260231db9f7cSAlexander Block
260304ab956eSJeff Mahoney btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
260431db9f7cSAlexander Block
2605924794c9STsutomu Itoh p = fs_path_alloc();
260631db9f7cSAlexander Block if (!p)
260731db9f7cSAlexander Block return -ENOMEM;
260831db9f7cSAlexander Block
260931db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
261031db9f7cSAlexander Block if (ret < 0)
261131db9f7cSAlexander Block goto out;
261231db9f7cSAlexander Block
261331db9f7cSAlexander Block ret = get_cur_path(sctx, ino, gen, p);
261431db9f7cSAlexander Block if (ret < 0)
261531db9f7cSAlexander Block goto out;
261631db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
261731db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
261831db9f7cSAlexander Block
261931db9f7cSAlexander Block ret = send_cmd(sctx);
262031db9f7cSAlexander Block
262131db9f7cSAlexander Block tlv_put_failure:
262231db9f7cSAlexander Block out:
2623924794c9STsutomu Itoh fs_path_free(p);
262431db9f7cSAlexander Block return ret;
262531db9f7cSAlexander Block }
262631db9f7cSAlexander Block
send_chmod(struct send_ctx * sctx,u64 ino,u64 gen,u64 mode)262731db9f7cSAlexander Block static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
262831db9f7cSAlexander Block {
262904ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
263031db9f7cSAlexander Block int ret = 0;
263131db9f7cSAlexander Block struct fs_path *p;
263231db9f7cSAlexander Block
263304ab956eSJeff Mahoney btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
263431db9f7cSAlexander Block
2635924794c9STsutomu Itoh p = fs_path_alloc();
263631db9f7cSAlexander Block if (!p)
263731db9f7cSAlexander Block return -ENOMEM;
263831db9f7cSAlexander Block
263931db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
264031db9f7cSAlexander Block if (ret < 0)
264131db9f7cSAlexander Block goto out;
264231db9f7cSAlexander Block
264331db9f7cSAlexander Block ret = get_cur_path(sctx, ino, gen, p);
264431db9f7cSAlexander Block if (ret < 0)
264531db9f7cSAlexander Block goto out;
264631db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
264731db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
264831db9f7cSAlexander Block
264931db9f7cSAlexander Block ret = send_cmd(sctx);
265031db9f7cSAlexander Block
265131db9f7cSAlexander Block tlv_put_failure:
265231db9f7cSAlexander Block out:
2653924794c9STsutomu Itoh fs_path_free(p);
265431db9f7cSAlexander Block return ret;
265531db9f7cSAlexander Block }
265631db9f7cSAlexander Block
send_fileattr(struct send_ctx * sctx,u64 ino,u64 gen,u64 fileattr)265748247359SDavid Sterba static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr)
265848247359SDavid Sterba {
265948247359SDavid Sterba struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
266048247359SDavid Sterba int ret = 0;
266148247359SDavid Sterba struct fs_path *p;
266248247359SDavid Sterba
266348247359SDavid Sterba if (sctx->proto < 2)
266448247359SDavid Sterba return 0;
266548247359SDavid Sterba
266648247359SDavid Sterba btrfs_debug(fs_info, "send_fileattr %llu fileattr=%llu", ino, fileattr);
266748247359SDavid Sterba
266848247359SDavid Sterba p = fs_path_alloc();
266948247359SDavid Sterba if (!p)
267048247359SDavid Sterba return -ENOMEM;
267148247359SDavid Sterba
267248247359SDavid Sterba ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR);
267348247359SDavid Sterba if (ret < 0)
267448247359SDavid Sterba goto out;
267548247359SDavid Sterba
267648247359SDavid Sterba ret = get_cur_path(sctx, ino, gen, p);
267748247359SDavid Sterba if (ret < 0)
267848247359SDavid Sterba goto out;
267948247359SDavid Sterba TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
268048247359SDavid Sterba TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr);
268148247359SDavid Sterba
268248247359SDavid Sterba ret = send_cmd(sctx);
268348247359SDavid Sterba
268448247359SDavid Sterba tlv_put_failure:
268548247359SDavid Sterba out:
268648247359SDavid Sterba fs_path_free(p);
268748247359SDavid Sterba return ret;
268848247359SDavid Sterba }
268948247359SDavid Sterba
send_chown(struct send_ctx * sctx,u64 ino,u64 gen,u64 uid,u64 gid)269031db9f7cSAlexander Block static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
269131db9f7cSAlexander Block {
269204ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
269331db9f7cSAlexander Block int ret = 0;
269431db9f7cSAlexander Block struct fs_path *p;
269531db9f7cSAlexander Block
269604ab956eSJeff Mahoney btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
269704ab956eSJeff Mahoney ino, uid, gid);
269831db9f7cSAlexander Block
2699924794c9STsutomu Itoh p = fs_path_alloc();
270031db9f7cSAlexander Block if (!p)
270131db9f7cSAlexander Block return -ENOMEM;
270231db9f7cSAlexander Block
270331db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
270431db9f7cSAlexander Block if (ret < 0)
270531db9f7cSAlexander Block goto out;
270631db9f7cSAlexander Block
270731db9f7cSAlexander Block ret = get_cur_path(sctx, ino, gen, p);
270831db9f7cSAlexander Block if (ret < 0)
270931db9f7cSAlexander Block goto out;
271031db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
271131db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
271231db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
271331db9f7cSAlexander Block
271431db9f7cSAlexander Block ret = send_cmd(sctx);
271531db9f7cSAlexander Block
271631db9f7cSAlexander Block tlv_put_failure:
271731db9f7cSAlexander Block out:
2718924794c9STsutomu Itoh fs_path_free(p);
271931db9f7cSAlexander Block return ret;
272031db9f7cSAlexander Block }
272131db9f7cSAlexander Block
send_utimes(struct send_ctx * sctx,u64 ino,u64 gen)272231db9f7cSAlexander Block static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
272331db9f7cSAlexander Block {
272404ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
272531db9f7cSAlexander Block int ret = 0;
272631db9f7cSAlexander Block struct fs_path *p = NULL;
272731db9f7cSAlexander Block struct btrfs_inode_item *ii;
272831db9f7cSAlexander Block struct btrfs_path *path = NULL;
272931db9f7cSAlexander Block struct extent_buffer *eb;
273031db9f7cSAlexander Block struct btrfs_key key;
273131db9f7cSAlexander Block int slot;
273231db9f7cSAlexander Block
273304ab956eSJeff Mahoney btrfs_debug(fs_info, "send_utimes %llu", ino);
273431db9f7cSAlexander Block
2735924794c9STsutomu Itoh p = fs_path_alloc();
273631db9f7cSAlexander Block if (!p)
273731db9f7cSAlexander Block return -ENOMEM;
273831db9f7cSAlexander Block
273931db9f7cSAlexander Block path = alloc_path_for_send();
274031db9f7cSAlexander Block if (!path) {
274131db9f7cSAlexander Block ret = -ENOMEM;
274231db9f7cSAlexander Block goto out;
274331db9f7cSAlexander Block }
274431db9f7cSAlexander Block
274531db9f7cSAlexander Block key.objectid = ino;
274631db9f7cSAlexander Block key.type = BTRFS_INODE_ITEM_KEY;
274731db9f7cSAlexander Block key.offset = 0;
274831db9f7cSAlexander Block ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
274915b253eaSFilipe Manana if (ret > 0)
275015b253eaSFilipe Manana ret = -ENOENT;
275131db9f7cSAlexander Block if (ret < 0)
275231db9f7cSAlexander Block goto out;
275331db9f7cSAlexander Block
275431db9f7cSAlexander Block eb = path->nodes[0];
275531db9f7cSAlexander Block slot = path->slots[0];
275631db9f7cSAlexander Block ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
275731db9f7cSAlexander Block
275831db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
275931db9f7cSAlexander Block if (ret < 0)
276031db9f7cSAlexander Block goto out;
276131db9f7cSAlexander Block
276231db9f7cSAlexander Block ret = get_cur_path(sctx, ino, gen, p);
276331db9f7cSAlexander Block if (ret < 0)
276431db9f7cSAlexander Block goto out;
276531db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2766a937b979SDavid Sterba TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2767a937b979SDavid Sterba TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2768a937b979SDavid Sterba TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
276922a5b2abSDavid Sterba if (sctx->proto >= 2)
277022a5b2abSDavid Sterba TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime);
277131db9f7cSAlexander Block
277231db9f7cSAlexander Block ret = send_cmd(sctx);
277331db9f7cSAlexander Block
277431db9f7cSAlexander Block tlv_put_failure:
277531db9f7cSAlexander Block out:
2776924794c9STsutomu Itoh fs_path_free(p);
277731db9f7cSAlexander Block btrfs_free_path(path);
277831db9f7cSAlexander Block return ret;
277931db9f7cSAlexander Block }
278031db9f7cSAlexander Block
278131db9f7cSAlexander Block /*
27823e49363bSFilipe Manana * If the cache is full, we can't remove entries from it and do a call to
27833e49363bSFilipe Manana * send_utimes() for each respective inode, because we might be finishing
27843e49363bSFilipe Manana * processing an inode that is a directory and it just got renamed, and existing
27853e49363bSFilipe Manana * entries in the cache may refer to inodes that have the directory in their
27863e49363bSFilipe Manana * full path - in which case we would generate outdated paths (pre-rename)
27873e49363bSFilipe Manana * for the inodes that the cache entries point to. Instead of prunning the
27883e49363bSFilipe Manana * cache when inserting, do it after we finish processing each inode at
27893e49363bSFilipe Manana * finish_inode_if_needed().
27903e49363bSFilipe Manana */
cache_dir_utimes(struct send_ctx * sctx,u64 dir,u64 gen)27913e49363bSFilipe Manana static int cache_dir_utimes(struct send_ctx *sctx, u64 dir, u64 gen)
27923e49363bSFilipe Manana {
27933e49363bSFilipe Manana struct btrfs_lru_cache_entry *entry;
27943e49363bSFilipe Manana int ret;
27953e49363bSFilipe Manana
27963e49363bSFilipe Manana entry = btrfs_lru_cache_lookup(&sctx->dir_utimes_cache, dir, gen);
27973e49363bSFilipe Manana if (entry != NULL)
27983e49363bSFilipe Manana return 0;
27993e49363bSFilipe Manana
28003e49363bSFilipe Manana /* Caching is optional, don't fail if we can't allocate memory. */
28013e49363bSFilipe Manana entry = kmalloc(sizeof(*entry), GFP_KERNEL);
28023e49363bSFilipe Manana if (!entry)
28033e49363bSFilipe Manana return send_utimes(sctx, dir, gen);
28043e49363bSFilipe Manana
28053e49363bSFilipe Manana entry->key = dir;
28063e49363bSFilipe Manana entry->gen = gen;
28073e49363bSFilipe Manana
28083e49363bSFilipe Manana ret = btrfs_lru_cache_store(&sctx->dir_utimes_cache, entry, GFP_KERNEL);
28093e49363bSFilipe Manana ASSERT(ret != -EEXIST);
28103e49363bSFilipe Manana if (ret) {
28113e49363bSFilipe Manana kfree(entry);
28123e49363bSFilipe Manana return send_utimes(sctx, dir, gen);
28133e49363bSFilipe Manana }
28143e49363bSFilipe Manana
28153e49363bSFilipe Manana return 0;
28163e49363bSFilipe Manana }
28173e49363bSFilipe Manana
trim_dir_utimes_cache(struct send_ctx * sctx)28183e49363bSFilipe Manana static int trim_dir_utimes_cache(struct send_ctx *sctx)
28193e49363bSFilipe Manana {
28203e49363bSFilipe Manana while (btrfs_lru_cache_size(&sctx->dir_utimes_cache) >
28213e49363bSFilipe Manana SEND_MAX_DIR_UTIMES_CACHE_SIZE) {
28223e49363bSFilipe Manana struct btrfs_lru_cache_entry *lru;
28233e49363bSFilipe Manana int ret;
28243e49363bSFilipe Manana
28253e49363bSFilipe Manana lru = btrfs_lru_cache_lru_entry(&sctx->dir_utimes_cache);
28263e49363bSFilipe Manana ASSERT(lru != NULL);
28273e49363bSFilipe Manana
28283e49363bSFilipe Manana ret = send_utimes(sctx, lru->key, lru->gen);
28293e49363bSFilipe Manana if (ret)
28303e49363bSFilipe Manana return ret;
28313e49363bSFilipe Manana
28323e49363bSFilipe Manana btrfs_lru_cache_remove(&sctx->dir_utimes_cache, lru);
28333e49363bSFilipe Manana }
28343e49363bSFilipe Manana
28353e49363bSFilipe Manana return 0;
28363e49363bSFilipe Manana }
28373e49363bSFilipe Manana
28383e49363bSFilipe Manana /*
283931db9f7cSAlexander Block * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
284031db9f7cSAlexander Block * a valid path yet because we did not process the refs yet. So, the inode
284131db9f7cSAlexander Block * is created as orphan.
284231db9f7cSAlexander Block */
send_create_inode(struct send_ctx * sctx,u64 ino)28431f4692daSAlexander Block static int send_create_inode(struct send_ctx *sctx, u64 ino)
284431db9f7cSAlexander Block {
284504ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
284631db9f7cSAlexander Block int ret = 0;
284731db9f7cSAlexander Block struct fs_path *p;
284831db9f7cSAlexander Block int cmd;
28497e93f6dcSBingJing Chang struct btrfs_inode_info info;
28501f4692daSAlexander Block u64 gen;
285131db9f7cSAlexander Block u64 mode;
28521f4692daSAlexander Block u64 rdev;
285331db9f7cSAlexander Block
285404ab956eSJeff Mahoney btrfs_debug(fs_info, "send_create_inode %llu", ino);
285531db9f7cSAlexander Block
2856924794c9STsutomu Itoh p = fs_path_alloc();
285731db9f7cSAlexander Block if (!p)
285831db9f7cSAlexander Block return -ENOMEM;
285931db9f7cSAlexander Block
2860644d1940SLiu Bo if (ino != sctx->cur_ino) {
28617e93f6dcSBingJing Chang ret = get_inode_info(sctx->send_root, ino, &info);
28621f4692daSAlexander Block if (ret < 0)
28631f4692daSAlexander Block goto out;
28647e93f6dcSBingJing Chang gen = info.gen;
28657e93f6dcSBingJing Chang mode = info.mode;
28667e93f6dcSBingJing Chang rdev = info.rdev;
2867644d1940SLiu Bo } else {
2868644d1940SLiu Bo gen = sctx->cur_inode_gen;
2869644d1940SLiu Bo mode = sctx->cur_inode_mode;
2870644d1940SLiu Bo rdev = sctx->cur_inode_rdev;
2871644d1940SLiu Bo }
287231db9f7cSAlexander Block
2873e938c8adSAlexander Block if (S_ISREG(mode)) {
287431db9f7cSAlexander Block cmd = BTRFS_SEND_C_MKFILE;
2875e938c8adSAlexander Block } else if (S_ISDIR(mode)) {
287631db9f7cSAlexander Block cmd = BTRFS_SEND_C_MKDIR;
2877e938c8adSAlexander Block } else if (S_ISLNK(mode)) {
287831db9f7cSAlexander Block cmd = BTRFS_SEND_C_SYMLINK;
2879e938c8adSAlexander Block } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
288031db9f7cSAlexander Block cmd = BTRFS_SEND_C_MKNOD;
2881e938c8adSAlexander Block } else if (S_ISFIFO(mode)) {
288231db9f7cSAlexander Block cmd = BTRFS_SEND_C_MKFIFO;
2883e938c8adSAlexander Block } else if (S_ISSOCK(mode)) {
288431db9f7cSAlexander Block cmd = BTRFS_SEND_C_MKSOCK;
2885e938c8adSAlexander Block } else {
2886f14d104dSDavid Sterba btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
288731db9f7cSAlexander Block (int)(mode & S_IFMT));
2888ca6842bfSTsutomu Itoh ret = -EOPNOTSUPP;
288931db9f7cSAlexander Block goto out;
289031db9f7cSAlexander Block }
289131db9f7cSAlexander Block
289231db9f7cSAlexander Block ret = begin_cmd(sctx, cmd);
289331db9f7cSAlexander Block if (ret < 0)
289431db9f7cSAlexander Block goto out;
289531db9f7cSAlexander Block
28961f4692daSAlexander Block ret = gen_unique_name(sctx, ino, gen, p);
289731db9f7cSAlexander Block if (ret < 0)
289831db9f7cSAlexander Block goto out;
289931db9f7cSAlexander Block
290031db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
29011f4692daSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
290231db9f7cSAlexander Block
290331db9f7cSAlexander Block if (S_ISLNK(mode)) {
290431db9f7cSAlexander Block fs_path_reset(p);
2905924794c9STsutomu Itoh ret = read_symlink(sctx->send_root, ino, p);
290631db9f7cSAlexander Block if (ret < 0)
290731db9f7cSAlexander Block goto out;
290831db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
290931db9f7cSAlexander Block } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
291031db9f7cSAlexander Block S_ISFIFO(mode) || S_ISSOCK(mode)) {
2911d79e5043SArne Jansen TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2912d79e5043SArne Jansen TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
291331db9f7cSAlexander Block }
291431db9f7cSAlexander Block
291531db9f7cSAlexander Block ret = send_cmd(sctx);
291631db9f7cSAlexander Block if (ret < 0)
291731db9f7cSAlexander Block goto out;
291831db9f7cSAlexander Block
291931db9f7cSAlexander Block
292031db9f7cSAlexander Block tlv_put_failure:
292131db9f7cSAlexander Block out:
2922924794c9STsutomu Itoh fs_path_free(p);
292331db9f7cSAlexander Block return ret;
292431db9f7cSAlexander Block }
292531db9f7cSAlexander Block
cache_dir_created(struct send_ctx * sctx,u64 dir)2926e8a7f49dSFilipe Manana static void cache_dir_created(struct send_ctx *sctx, u64 dir)
2927e8a7f49dSFilipe Manana {
2928e8a7f49dSFilipe Manana struct btrfs_lru_cache_entry *entry;
2929e8a7f49dSFilipe Manana int ret;
2930e8a7f49dSFilipe Manana
2931e8a7f49dSFilipe Manana /* Caching is optional, ignore any failures. */
2932e8a7f49dSFilipe Manana entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2933e8a7f49dSFilipe Manana if (!entry)
2934e8a7f49dSFilipe Manana return;
2935e8a7f49dSFilipe Manana
2936e8a7f49dSFilipe Manana entry->key = dir;
29370da0c560SFilipe Manana entry->gen = 0;
2938e8a7f49dSFilipe Manana ret = btrfs_lru_cache_store(&sctx->dir_created_cache, entry, GFP_KERNEL);
2939e8a7f49dSFilipe Manana if (ret < 0)
2940e8a7f49dSFilipe Manana kfree(entry);
2941e8a7f49dSFilipe Manana }
2942e8a7f49dSFilipe Manana
29431f4692daSAlexander Block /*
29441f4692daSAlexander Block * We need some special handling for inodes that get processed before the parent
29451f4692daSAlexander Block * directory got created. See process_recorded_refs for details.
29461f4692daSAlexander Block * This function does the check if we already created the dir out of order.
29471f4692daSAlexander Block */
did_create_dir(struct send_ctx * sctx,u64 dir)29481f4692daSAlexander Block static int did_create_dir(struct send_ctx *sctx, u64 dir)
29491f4692daSAlexander Block {
29501f4692daSAlexander Block int ret = 0;
29516dcee260SGabriel Niebler int iter_ret = 0;
29521f4692daSAlexander Block struct btrfs_path *path = NULL;
29531f4692daSAlexander Block struct btrfs_key key;
29541f4692daSAlexander Block struct btrfs_key found_key;
29551f4692daSAlexander Block struct btrfs_key di_key;
29561f4692daSAlexander Block struct btrfs_dir_item *di;
29571f4692daSAlexander Block
29580da0c560SFilipe Manana if (btrfs_lru_cache_lookup(&sctx->dir_created_cache, dir, 0))
2959e8a7f49dSFilipe Manana return 1;
2960e8a7f49dSFilipe Manana
29611f4692daSAlexander Block path = alloc_path_for_send();
29626dcee260SGabriel Niebler if (!path)
29636dcee260SGabriel Niebler return -ENOMEM;
29641f4692daSAlexander Block
29651f4692daSAlexander Block key.objectid = dir;
29661f4692daSAlexander Block key.type = BTRFS_DIR_INDEX_KEY;
29671f4692daSAlexander Block key.offset = 0;
2968dff6d0adSFilipe David Borba Manana
29696dcee260SGabriel Niebler btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
29706dcee260SGabriel Niebler struct extent_buffer *eb = path->nodes[0];
2971dff6d0adSFilipe David Borba Manana
2972dff6d0adSFilipe David Borba Manana if (found_key.objectid != key.objectid ||
29731f4692daSAlexander Block found_key.type != key.type) {
29741f4692daSAlexander Block ret = 0;
29756dcee260SGabriel Niebler break;
29761f4692daSAlexander Block }
29771f4692daSAlexander Block
29786dcee260SGabriel Niebler di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item);
29791f4692daSAlexander Block btrfs_dir_item_key_to_cpu(eb, di, &di_key);
29801f4692daSAlexander Block
2981a0525414SJosef Bacik if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2982a0525414SJosef Bacik di_key.objectid < sctx->send_progress) {
29831f4692daSAlexander Block ret = 1;
2984e8a7f49dSFilipe Manana cache_dir_created(sctx, dir);
29856dcee260SGabriel Niebler break;
29861f4692daSAlexander Block }
29871f4692daSAlexander Block }
29886dcee260SGabriel Niebler /* Catch error found during iteration */
29896dcee260SGabriel Niebler if (iter_ret < 0)
29906dcee260SGabriel Niebler ret = iter_ret;
29911f4692daSAlexander Block
29921f4692daSAlexander Block btrfs_free_path(path);
29931f4692daSAlexander Block return ret;
29941f4692daSAlexander Block }
29951f4692daSAlexander Block
29961f4692daSAlexander Block /*
29971f4692daSAlexander Block * Only creates the inode if it is:
29981f4692daSAlexander Block * 1. Not a directory
29991f4692daSAlexander Block * 2. Or a directory which was not created already due to out of order
30001f4692daSAlexander Block * directories. See did_create_dir and process_recorded_refs for details.
30011f4692daSAlexander Block */
send_create_inode_if_needed(struct send_ctx * sctx)30021f4692daSAlexander Block static int send_create_inode_if_needed(struct send_ctx *sctx)
30031f4692daSAlexander Block {
30041f4692daSAlexander Block int ret;
30051f4692daSAlexander Block
30061f4692daSAlexander Block if (S_ISDIR(sctx->cur_inode_mode)) {
30071f4692daSAlexander Block ret = did_create_dir(sctx, sctx->cur_ino);
30081f4692daSAlexander Block if (ret < 0)
30091f4692daSAlexander Block return ret;
30100e3dd5bcSMarcos Paulo de Souza else if (ret > 0)
30110e3dd5bcSMarcos Paulo de Souza return 0;
30120e3dd5bcSMarcos Paulo de Souza }
30130e3dd5bcSMarcos Paulo de Souza
3014e8a7f49dSFilipe Manana ret = send_create_inode(sctx, sctx->cur_ino);
3015e8a7f49dSFilipe Manana
3016e8a7f49dSFilipe Manana if (ret == 0 && S_ISDIR(sctx->cur_inode_mode))
3017e8a7f49dSFilipe Manana cache_dir_created(sctx, sctx->cur_ino);
3018e8a7f49dSFilipe Manana
3019e8a7f49dSFilipe Manana return ret;
30201f4692daSAlexander Block }
30211f4692daSAlexander Block
302231db9f7cSAlexander Block struct recorded_ref {
302331db9f7cSAlexander Block struct list_head list;
302431db9f7cSAlexander Block char *name;
302531db9f7cSAlexander Block struct fs_path *full_path;
302631db9f7cSAlexander Block u64 dir;
302731db9f7cSAlexander Block u64 dir_gen;
302831db9f7cSAlexander Block int name_len;
30293aa5bd36SBingJing Chang struct rb_node node;
30303aa5bd36SBingJing Chang struct rb_root *root;
303131db9f7cSAlexander Block };
303231db9f7cSAlexander Block
recorded_ref_alloc(void)303371ecfc13SBingJing Chang static struct recorded_ref *recorded_ref_alloc(void)
303471ecfc13SBingJing Chang {
303571ecfc13SBingJing Chang struct recorded_ref *ref;
303671ecfc13SBingJing Chang
303771ecfc13SBingJing Chang ref = kzalloc(sizeof(*ref), GFP_KERNEL);
303871ecfc13SBingJing Chang if (!ref)
303971ecfc13SBingJing Chang return NULL;
30403aa5bd36SBingJing Chang RB_CLEAR_NODE(&ref->node);
304171ecfc13SBingJing Chang INIT_LIST_HEAD(&ref->list);
304271ecfc13SBingJing Chang return ref;
304371ecfc13SBingJing Chang }
304471ecfc13SBingJing Chang
recorded_ref_free(struct recorded_ref * ref)304571ecfc13SBingJing Chang static void recorded_ref_free(struct recorded_ref *ref)
304671ecfc13SBingJing Chang {
304771ecfc13SBingJing Chang if (!ref)
304871ecfc13SBingJing Chang return;
30493aa5bd36SBingJing Chang if (!RB_EMPTY_NODE(&ref->node))
30503aa5bd36SBingJing Chang rb_erase(&ref->node, ref->root);
305171ecfc13SBingJing Chang list_del(&ref->list);
305271ecfc13SBingJing Chang fs_path_free(ref->full_path);
305371ecfc13SBingJing Chang kfree(ref);
305471ecfc13SBingJing Chang }
305571ecfc13SBingJing Chang
set_ref_path(struct recorded_ref * ref,struct fs_path * path)3056fdb13889SFilipe Manana static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
3057fdb13889SFilipe Manana {
3058fdb13889SFilipe Manana ref->full_path = path;
3059fdb13889SFilipe Manana ref->name = (char *)kbasename(ref->full_path->start);
3060fdb13889SFilipe Manana ref->name_len = ref->full_path->end - ref->name;
3061fdb13889SFilipe Manana }
3062fdb13889SFilipe Manana
dup_ref(struct recorded_ref * ref,struct list_head * list)3063ba5e8f2eSJosef Bacik static int dup_ref(struct recorded_ref *ref, struct list_head *list)
3064ba5e8f2eSJosef Bacik {
3065ba5e8f2eSJosef Bacik struct recorded_ref *new;
3066ba5e8f2eSJosef Bacik
306771ecfc13SBingJing Chang new = recorded_ref_alloc();
3068ba5e8f2eSJosef Bacik if (!new)
3069ba5e8f2eSJosef Bacik return -ENOMEM;
3070ba5e8f2eSJosef Bacik
3071ba5e8f2eSJosef Bacik new->dir = ref->dir;
3072ba5e8f2eSJosef Bacik new->dir_gen = ref->dir_gen;
3073ba5e8f2eSJosef Bacik list_add_tail(&new->list, list);
3074ba5e8f2eSJosef Bacik return 0;
3075ba5e8f2eSJosef Bacik }
3076ba5e8f2eSJosef Bacik
__free_recorded_refs(struct list_head * head)3077924794c9STsutomu Itoh static void __free_recorded_refs(struct list_head *head)
307831db9f7cSAlexander Block {
307931db9f7cSAlexander Block struct recorded_ref *cur;
308031db9f7cSAlexander Block
3081e938c8adSAlexander Block while (!list_empty(head)) {
3082e938c8adSAlexander Block cur = list_entry(head->next, struct recorded_ref, list);
308371ecfc13SBingJing Chang recorded_ref_free(cur);
308431db9f7cSAlexander Block }
308531db9f7cSAlexander Block }
308631db9f7cSAlexander Block
free_recorded_refs(struct send_ctx * sctx)308731db9f7cSAlexander Block static void free_recorded_refs(struct send_ctx *sctx)
308831db9f7cSAlexander Block {
3089924794c9STsutomu Itoh __free_recorded_refs(&sctx->new_refs);
3090924794c9STsutomu Itoh __free_recorded_refs(&sctx->deleted_refs);
309131db9f7cSAlexander Block }
309231db9f7cSAlexander Block
309331db9f7cSAlexander Block /*
3094766702efSAlexander Block * Renames/moves a file/dir to its orphan name. Used when the first
309531db9f7cSAlexander Block * ref of an unprocessed inode gets overwritten and for all non empty
309631db9f7cSAlexander Block * directories.
309731db9f7cSAlexander Block */
orphanize_inode(struct send_ctx * sctx,u64 ino,u64 gen,struct fs_path * path)309831db9f7cSAlexander Block static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
309931db9f7cSAlexander Block struct fs_path *path)
310031db9f7cSAlexander Block {
310131db9f7cSAlexander Block int ret;
310231db9f7cSAlexander Block struct fs_path *orphan;
310331db9f7cSAlexander Block
3104924794c9STsutomu Itoh orphan = fs_path_alloc();
310531db9f7cSAlexander Block if (!orphan)
310631db9f7cSAlexander Block return -ENOMEM;
310731db9f7cSAlexander Block
310831db9f7cSAlexander Block ret = gen_unique_name(sctx, ino, gen, orphan);
310931db9f7cSAlexander Block if (ret < 0)
311031db9f7cSAlexander Block goto out;
311131db9f7cSAlexander Block
311231db9f7cSAlexander Block ret = send_rename(sctx, path, orphan);
311331db9f7cSAlexander Block
311431db9f7cSAlexander Block out:
3115924794c9STsutomu Itoh fs_path_free(orphan);
311631db9f7cSAlexander Block return ret;
311731db9f7cSAlexander Block }
311831db9f7cSAlexander Block
add_orphan_dir_info(struct send_ctx * sctx,u64 dir_ino,u64 dir_gen)31190b3f407eSFilipe Manana static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
31200b3f407eSFilipe Manana u64 dir_ino, u64 dir_gen)
31219dc44214SFilipe Manana {
31229dc44214SFilipe Manana struct rb_node **p = &sctx->orphan_dirs.rb_node;
31239dc44214SFilipe Manana struct rb_node *parent = NULL;
31249dc44214SFilipe Manana struct orphan_dir_info *entry, *odi;
31259dc44214SFilipe Manana
31269dc44214SFilipe Manana while (*p) {
31279dc44214SFilipe Manana parent = *p;
31289dc44214SFilipe Manana entry = rb_entry(parent, struct orphan_dir_info, node);
31290b3f407eSFilipe Manana if (dir_ino < entry->ino)
31309dc44214SFilipe Manana p = &(*p)->rb_left;
31310b3f407eSFilipe Manana else if (dir_ino > entry->ino)
31329dc44214SFilipe Manana p = &(*p)->rb_right;
31330b3f407eSFilipe Manana else if (dir_gen < entry->gen)
31340b3f407eSFilipe Manana p = &(*p)->rb_left;
31350b3f407eSFilipe Manana else if (dir_gen > entry->gen)
31360b3f407eSFilipe Manana p = &(*p)->rb_right;
31370b3f407eSFilipe Manana else
31389dc44214SFilipe Manana return entry;
31399dc44214SFilipe Manana }
31409dc44214SFilipe Manana
314135c8eda1SRobbie Ko odi = kmalloc(sizeof(*odi), GFP_KERNEL);
314235c8eda1SRobbie Ko if (!odi)
314335c8eda1SRobbie Ko return ERR_PTR(-ENOMEM);
314435c8eda1SRobbie Ko odi->ino = dir_ino;
31450b3f407eSFilipe Manana odi->gen = dir_gen;
31460f96f517SRobbie Ko odi->last_dir_index_offset = 0;
3147474e4761SFilipe Manana odi->dir_high_seq_ino = 0;
314835c8eda1SRobbie Ko
31499dc44214SFilipe Manana rb_link_node(&odi->node, parent, p);
31509dc44214SFilipe Manana rb_insert_color(&odi->node, &sctx->orphan_dirs);
31519dc44214SFilipe Manana return odi;
31529dc44214SFilipe Manana }
31539dc44214SFilipe Manana
get_orphan_dir_info(struct send_ctx * sctx,u64 dir_ino,u64 gen)31540b3f407eSFilipe Manana static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
31550b3f407eSFilipe Manana u64 dir_ino, u64 gen)
31569dc44214SFilipe Manana {
31579dc44214SFilipe Manana struct rb_node *n = sctx->orphan_dirs.rb_node;
31589dc44214SFilipe Manana struct orphan_dir_info *entry;
31599dc44214SFilipe Manana
31609dc44214SFilipe Manana while (n) {
31619dc44214SFilipe Manana entry = rb_entry(n, struct orphan_dir_info, node);
31629dc44214SFilipe Manana if (dir_ino < entry->ino)
31639dc44214SFilipe Manana n = n->rb_left;
31649dc44214SFilipe Manana else if (dir_ino > entry->ino)
31659dc44214SFilipe Manana n = n->rb_right;
31660b3f407eSFilipe Manana else if (gen < entry->gen)
31670b3f407eSFilipe Manana n = n->rb_left;
31680b3f407eSFilipe Manana else if (gen > entry->gen)
31690b3f407eSFilipe Manana n = n->rb_right;
31709dc44214SFilipe Manana else
31719dc44214SFilipe Manana return entry;
31729dc44214SFilipe Manana }
31739dc44214SFilipe Manana return NULL;
31749dc44214SFilipe Manana }
31759dc44214SFilipe Manana
is_waiting_for_rm(struct send_ctx * sctx,u64 dir_ino,u64 gen)31760b3f407eSFilipe Manana static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
31779dc44214SFilipe Manana {
31780b3f407eSFilipe Manana struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
31799dc44214SFilipe Manana
31809dc44214SFilipe Manana return odi != NULL;
31819dc44214SFilipe Manana }
31829dc44214SFilipe Manana
free_orphan_dir_info(struct send_ctx * sctx,struct orphan_dir_info * odi)31839dc44214SFilipe Manana static void free_orphan_dir_info(struct send_ctx *sctx,
31849dc44214SFilipe Manana struct orphan_dir_info *odi)
31859dc44214SFilipe Manana {
31869dc44214SFilipe Manana if (!odi)
31879dc44214SFilipe Manana return;
31889dc44214SFilipe Manana rb_erase(&odi->node, &sctx->orphan_dirs);
31899dc44214SFilipe Manana kfree(odi);
31909dc44214SFilipe Manana }
31919dc44214SFilipe Manana
319231db9f7cSAlexander Block /*
319331db9f7cSAlexander Block * Returns 1 if a directory can be removed at this point in time.
319431db9f7cSAlexander Block * We check this by iterating all dir items and checking if the inode behind
319531db9f7cSAlexander Block * the dir item was already processed.
319631db9f7cSAlexander Block */
can_rmdir(struct send_ctx * sctx,u64 dir,u64 dir_gen)319724970ccbSFilipe Manana static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
319831db9f7cSAlexander Block {
319931db9f7cSAlexander Block int ret = 0;
320018f80f1fSGabriel Niebler int iter_ret = 0;
320131db9f7cSAlexander Block struct btrfs_root *root = sctx->parent_root;
320231db9f7cSAlexander Block struct btrfs_path *path;
320331db9f7cSAlexander Block struct btrfs_key key;
320431db9f7cSAlexander Block struct btrfs_key found_key;
320531db9f7cSAlexander Block struct btrfs_key loc;
320631db9f7cSAlexander Block struct btrfs_dir_item *di;
32070f96f517SRobbie Ko struct orphan_dir_info *odi = NULL;
3208474e4761SFilipe Manana u64 dir_high_seq_ino = 0;
3209474e4761SFilipe Manana u64 last_dir_index_offset = 0;
321031db9f7cSAlexander Block
32116d85ed05SAlexander Block /*
32126d85ed05SAlexander Block * Don't try to rmdir the top/root subvolume dir.
32136d85ed05SAlexander Block */
32146d85ed05SAlexander Block if (dir == BTRFS_FIRST_FREE_OBJECTID)
32156d85ed05SAlexander Block return 0;
32166d85ed05SAlexander Block
3217474e4761SFilipe Manana odi = get_orphan_dir_info(sctx, dir, dir_gen);
3218474e4761SFilipe Manana if (odi && sctx->cur_ino < odi->dir_high_seq_ino)
3219474e4761SFilipe Manana return 0;
3220474e4761SFilipe Manana
322131db9f7cSAlexander Block path = alloc_path_for_send();
322231db9f7cSAlexander Block if (!path)
322331db9f7cSAlexander Block return -ENOMEM;
322431db9f7cSAlexander Block
3225474e4761SFilipe Manana if (!odi) {
3226474e4761SFilipe Manana /*
3227474e4761SFilipe Manana * Find the inode number associated with the last dir index
3228474e4761SFilipe Manana * entry. This is very likely the inode with the highest number
3229474e4761SFilipe Manana * of all inodes that have an entry in the directory. We can
3230474e4761SFilipe Manana * then use it to avoid future calls to can_rmdir(), when
3231474e4761SFilipe Manana * processing inodes with a lower number, from having to search
3232474e4761SFilipe Manana * the parent root b+tree for dir index keys.
3233474e4761SFilipe Manana */
323431db9f7cSAlexander Block key.objectid = dir;
323531db9f7cSAlexander Block key.type = BTRFS_DIR_INDEX_KEY;
3236474e4761SFilipe Manana key.offset = (u64)-1;
32370f96f517SRobbie Ko
3238474e4761SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3239474e4761SFilipe Manana if (ret < 0) {
3240474e4761SFilipe Manana goto out;
3241474e4761SFilipe Manana } else if (ret > 0) {
3242474e4761SFilipe Manana /* Can't happen, the root is never empty. */
3243474e4761SFilipe Manana ASSERT(path->slots[0] > 0);
3244474e4761SFilipe Manana if (WARN_ON(path->slots[0] == 0)) {
3245474e4761SFilipe Manana ret = -EUCLEAN;
3246474e4761SFilipe Manana goto out;
3247474e4761SFilipe Manana }
3248474e4761SFilipe Manana path->slots[0]--;
3249474e4761SFilipe Manana }
3250474e4761SFilipe Manana
3251474e4761SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3252474e4761SFilipe Manana if (key.objectid != dir || key.type != BTRFS_DIR_INDEX_KEY) {
3253474e4761SFilipe Manana /* No index keys, dir can be removed. */
3254474e4761SFilipe Manana ret = 1;
3255474e4761SFilipe Manana goto out;
3256474e4761SFilipe Manana }
3257474e4761SFilipe Manana
3258474e4761SFilipe Manana di = btrfs_item_ptr(path->nodes[0], path->slots[0],
3259474e4761SFilipe Manana struct btrfs_dir_item);
3260474e4761SFilipe Manana btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
3261474e4761SFilipe Manana dir_high_seq_ino = loc.objectid;
3262474e4761SFilipe Manana if (sctx->cur_ino < dir_high_seq_ino) {
3263474e4761SFilipe Manana ret = 0;
3264474e4761SFilipe Manana goto out;
3265474e4761SFilipe Manana }
3266474e4761SFilipe Manana
3267474e4761SFilipe Manana btrfs_release_path(path);
3268474e4761SFilipe Manana }
3269474e4761SFilipe Manana
3270474e4761SFilipe Manana key.objectid = dir;
3271474e4761SFilipe Manana key.type = BTRFS_DIR_INDEX_KEY;
3272474e4761SFilipe Manana key.offset = (odi ? odi->last_dir_index_offset : 0);
32730f96f517SRobbie Ko
327418f80f1fSGabriel Niebler btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
32759dc44214SFilipe Manana struct waiting_dir_move *dm;
32769dc44214SFilipe Manana
3277dff6d0adSFilipe David Borba Manana if (found_key.objectid != key.objectid ||
3278dff6d0adSFilipe David Borba Manana found_key.type != key.type)
327931db9f7cSAlexander Block break;
328031db9f7cSAlexander Block
328131db9f7cSAlexander Block di = btrfs_item_ptr(path->nodes[0], path->slots[0],
328231db9f7cSAlexander Block struct btrfs_dir_item);
328331db9f7cSAlexander Block btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
328431db9f7cSAlexander Block
3285474e4761SFilipe Manana dir_high_seq_ino = max(dir_high_seq_ino, loc.objectid);
3286474e4761SFilipe Manana last_dir_index_offset = found_key.offset;
3287474e4761SFilipe Manana
32889dc44214SFilipe Manana dm = get_waiting_dir_move(sctx, loc.objectid);
32899dc44214SFilipe Manana if (dm) {
32909dc44214SFilipe Manana dm->rmdir_ino = dir;
32910b3f407eSFilipe Manana dm->rmdir_gen = dir_gen;
32929dc44214SFilipe Manana ret = 0;
32939dc44214SFilipe Manana goto out;
32949dc44214SFilipe Manana }
32959dc44214SFilipe Manana
329624970ccbSFilipe Manana if (loc.objectid > sctx->cur_ino) {
329731db9f7cSAlexander Block ret = 0;
329831db9f7cSAlexander Block goto out;
329931db9f7cSAlexander Block }
330018f80f1fSGabriel Niebler }
330118f80f1fSGabriel Niebler if (iter_ret < 0) {
330218f80f1fSGabriel Niebler ret = iter_ret;
330318f80f1fSGabriel Niebler goto out;
330431db9f7cSAlexander Block }
33050f96f517SRobbie Ko free_orphan_dir_info(sctx, odi);
330631db9f7cSAlexander Block
330731db9f7cSAlexander Block ret = 1;
330831db9f7cSAlexander Block
330931db9f7cSAlexander Block out:
331031db9f7cSAlexander Block btrfs_free_path(path);
3311d921b9cfSFilipe Manana
3312d921b9cfSFilipe Manana if (ret)
331331db9f7cSAlexander Block return ret;
3314d921b9cfSFilipe Manana
331578cf1a95SFilipe Manana if (!odi) {
3316d921b9cfSFilipe Manana odi = add_orphan_dir_info(sctx, dir, dir_gen);
3317d921b9cfSFilipe Manana if (IS_ERR(odi))
3318d921b9cfSFilipe Manana return PTR_ERR(odi);
3319d921b9cfSFilipe Manana
3320d921b9cfSFilipe Manana odi->gen = dir_gen;
332178cf1a95SFilipe Manana }
332278cf1a95SFilipe Manana
3323474e4761SFilipe Manana odi->last_dir_index_offset = last_dir_index_offset;
3324474e4761SFilipe Manana odi->dir_high_seq_ino = max(odi->dir_high_seq_ino, dir_high_seq_ino);
3325d921b9cfSFilipe Manana
3326d921b9cfSFilipe Manana return 0;
332731db9f7cSAlexander Block }
332831db9f7cSAlexander Block
is_waiting_for_move(struct send_ctx * sctx,u64 ino)33299f03740aSFilipe David Borba Manana static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
33309f03740aSFilipe David Borba Manana {
33319dc44214SFilipe Manana struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
33329f03740aSFilipe David Borba Manana
33339dc44214SFilipe Manana return entry != NULL;
33349f03740aSFilipe David Borba Manana }
33359f03740aSFilipe David Borba Manana
add_waiting_dir_move(struct send_ctx * sctx,u64 ino,bool orphanized)33368b191a68SFilipe Manana static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
33379f03740aSFilipe David Borba Manana {
33389f03740aSFilipe David Borba Manana struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
33399f03740aSFilipe David Borba Manana struct rb_node *parent = NULL;
33409f03740aSFilipe David Borba Manana struct waiting_dir_move *entry, *dm;
33419f03740aSFilipe David Borba Manana
3342e780b0d1SDavid Sterba dm = kmalloc(sizeof(*dm), GFP_KERNEL);
33439f03740aSFilipe David Borba Manana if (!dm)
33449f03740aSFilipe David Borba Manana return -ENOMEM;
33459f03740aSFilipe David Borba Manana dm->ino = ino;
33469dc44214SFilipe Manana dm->rmdir_ino = 0;
33470b3f407eSFilipe Manana dm->rmdir_gen = 0;
33488b191a68SFilipe Manana dm->orphanized = orphanized;
33499f03740aSFilipe David Borba Manana
33509f03740aSFilipe David Borba Manana while (*p) {
33519f03740aSFilipe David Borba Manana parent = *p;
33529f03740aSFilipe David Borba Manana entry = rb_entry(parent, struct waiting_dir_move, node);
33539f03740aSFilipe David Borba Manana if (ino < entry->ino) {
33549f03740aSFilipe David Borba Manana p = &(*p)->rb_left;
33559f03740aSFilipe David Borba Manana } else if (ino > entry->ino) {
33569f03740aSFilipe David Borba Manana p = &(*p)->rb_right;
33579f03740aSFilipe David Borba Manana } else {
33589f03740aSFilipe David Borba Manana kfree(dm);
33599f03740aSFilipe David Borba Manana return -EEXIST;
33609f03740aSFilipe David Borba Manana }
33619f03740aSFilipe David Borba Manana }
33629f03740aSFilipe David Borba Manana
33639f03740aSFilipe David Borba Manana rb_link_node(&dm->node, parent, p);
33649f03740aSFilipe David Borba Manana rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
33659f03740aSFilipe David Borba Manana return 0;
33669f03740aSFilipe David Borba Manana }
33679f03740aSFilipe David Borba Manana
33689dc44214SFilipe Manana static struct waiting_dir_move *
get_waiting_dir_move(struct send_ctx * sctx,u64 ino)33699dc44214SFilipe Manana get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
33709f03740aSFilipe David Borba Manana {
33719f03740aSFilipe David Borba Manana struct rb_node *n = sctx->waiting_dir_moves.rb_node;
33729f03740aSFilipe David Borba Manana struct waiting_dir_move *entry;
33739f03740aSFilipe David Borba Manana
33749f03740aSFilipe David Borba Manana while (n) {
33759f03740aSFilipe David Borba Manana entry = rb_entry(n, struct waiting_dir_move, node);
33769dc44214SFilipe Manana if (ino < entry->ino)
33779f03740aSFilipe David Borba Manana n = n->rb_left;
33789dc44214SFilipe Manana else if (ino > entry->ino)
33799f03740aSFilipe David Borba Manana n = n->rb_right;
33809dc44214SFilipe Manana else
33819dc44214SFilipe Manana return entry;
33829f03740aSFilipe David Borba Manana }
33839dc44214SFilipe Manana return NULL;
33849f03740aSFilipe David Borba Manana }
33859dc44214SFilipe Manana
free_waiting_dir_move(struct send_ctx * sctx,struct waiting_dir_move * dm)33869dc44214SFilipe Manana static void free_waiting_dir_move(struct send_ctx *sctx,
33879dc44214SFilipe Manana struct waiting_dir_move *dm)
33889dc44214SFilipe Manana {
33899dc44214SFilipe Manana if (!dm)
33909dc44214SFilipe Manana return;
33919dc44214SFilipe Manana rb_erase(&dm->node, &sctx->waiting_dir_moves);
33929dc44214SFilipe Manana kfree(dm);
33939f03740aSFilipe David Borba Manana }
33949f03740aSFilipe David Borba Manana
add_pending_dir_move(struct send_ctx * sctx,u64 ino,u64 ino_gen,u64 parent_ino,struct list_head * new_refs,struct list_head * deleted_refs,const bool is_orphan)3395bfa7e1f8SFilipe Manana static int add_pending_dir_move(struct send_ctx *sctx,
3396bfa7e1f8SFilipe Manana u64 ino,
3397bfa7e1f8SFilipe Manana u64 ino_gen,
3398f959492fSFilipe Manana u64 parent_ino,
3399f959492fSFilipe Manana struct list_head *new_refs,
340084471e24SFilipe Manana struct list_head *deleted_refs,
340184471e24SFilipe Manana const bool is_orphan)
34029f03740aSFilipe David Borba Manana {
34039f03740aSFilipe David Borba Manana struct rb_node **p = &sctx->pending_dir_moves.rb_node;
34049f03740aSFilipe David Borba Manana struct rb_node *parent = NULL;
340573b802f4SChris Mason struct pending_dir_move *entry = NULL, *pm;
34069f03740aSFilipe David Borba Manana struct recorded_ref *cur;
34079f03740aSFilipe David Borba Manana int exists = 0;
34089f03740aSFilipe David Borba Manana int ret;
34099f03740aSFilipe David Borba Manana
3410e780b0d1SDavid Sterba pm = kmalloc(sizeof(*pm), GFP_KERNEL);
34119f03740aSFilipe David Borba Manana if (!pm)
34129f03740aSFilipe David Borba Manana return -ENOMEM;
34139f03740aSFilipe David Borba Manana pm->parent_ino = parent_ino;
3414bfa7e1f8SFilipe Manana pm->ino = ino;
3415bfa7e1f8SFilipe Manana pm->gen = ino_gen;
34169f03740aSFilipe David Borba Manana INIT_LIST_HEAD(&pm->list);
34179f03740aSFilipe David Borba Manana INIT_LIST_HEAD(&pm->update_refs);
34189f03740aSFilipe David Borba Manana RB_CLEAR_NODE(&pm->node);
34199f03740aSFilipe David Borba Manana
34209f03740aSFilipe David Borba Manana while (*p) {
34219f03740aSFilipe David Borba Manana parent = *p;
34229f03740aSFilipe David Borba Manana entry = rb_entry(parent, struct pending_dir_move, node);
34239f03740aSFilipe David Borba Manana if (parent_ino < entry->parent_ino) {
34249f03740aSFilipe David Borba Manana p = &(*p)->rb_left;
34259f03740aSFilipe David Borba Manana } else if (parent_ino > entry->parent_ino) {
34269f03740aSFilipe David Borba Manana p = &(*p)->rb_right;
34279f03740aSFilipe David Borba Manana } else {
34289f03740aSFilipe David Borba Manana exists = 1;
34299f03740aSFilipe David Borba Manana break;
34309f03740aSFilipe David Borba Manana }
34319f03740aSFilipe David Borba Manana }
34329f03740aSFilipe David Borba Manana
3433f959492fSFilipe Manana list_for_each_entry(cur, deleted_refs, list) {
34349f03740aSFilipe David Borba Manana ret = dup_ref(cur, &pm->update_refs);
34359f03740aSFilipe David Borba Manana if (ret < 0)
34369f03740aSFilipe David Borba Manana goto out;
34379f03740aSFilipe David Borba Manana }
3438f959492fSFilipe Manana list_for_each_entry(cur, new_refs, list) {
34399f03740aSFilipe David Borba Manana ret = dup_ref(cur, &pm->update_refs);
34409f03740aSFilipe David Borba Manana if (ret < 0)
34419f03740aSFilipe David Borba Manana goto out;
34429f03740aSFilipe David Borba Manana }
34439f03740aSFilipe David Borba Manana
34448b191a68SFilipe Manana ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
34459f03740aSFilipe David Borba Manana if (ret)
34469f03740aSFilipe David Borba Manana goto out;
34479f03740aSFilipe David Borba Manana
34489f03740aSFilipe David Borba Manana if (exists) {
34499f03740aSFilipe David Borba Manana list_add_tail(&pm->list, &entry->list);
34509f03740aSFilipe David Borba Manana } else {
34519f03740aSFilipe David Borba Manana rb_link_node(&pm->node, parent, p);
34529f03740aSFilipe David Borba Manana rb_insert_color(&pm->node, &sctx->pending_dir_moves);
34539f03740aSFilipe David Borba Manana }
34549f03740aSFilipe David Borba Manana ret = 0;
34559f03740aSFilipe David Borba Manana out:
34569f03740aSFilipe David Borba Manana if (ret) {
34579f03740aSFilipe David Borba Manana __free_recorded_refs(&pm->update_refs);
34589f03740aSFilipe David Borba Manana kfree(pm);
34599f03740aSFilipe David Borba Manana }
34609f03740aSFilipe David Borba Manana return ret;
34619f03740aSFilipe David Borba Manana }
34629f03740aSFilipe David Borba Manana
get_pending_dir_moves(struct send_ctx * sctx,u64 parent_ino)34639f03740aSFilipe David Borba Manana static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
34649f03740aSFilipe David Borba Manana u64 parent_ino)
34659f03740aSFilipe David Borba Manana {
34669f03740aSFilipe David Borba Manana struct rb_node *n = sctx->pending_dir_moves.rb_node;
34679f03740aSFilipe David Borba Manana struct pending_dir_move *entry;
34689f03740aSFilipe David Borba Manana
34699f03740aSFilipe David Borba Manana while (n) {
34709f03740aSFilipe David Borba Manana entry = rb_entry(n, struct pending_dir_move, node);
34719f03740aSFilipe David Borba Manana if (parent_ino < entry->parent_ino)
34729f03740aSFilipe David Borba Manana n = n->rb_left;
34739f03740aSFilipe David Borba Manana else if (parent_ino > entry->parent_ino)
34749f03740aSFilipe David Borba Manana n = n->rb_right;
34759f03740aSFilipe David Borba Manana else
34769f03740aSFilipe David Borba Manana return entry;
34779f03740aSFilipe David Borba Manana }
34789f03740aSFilipe David Borba Manana return NULL;
34799f03740aSFilipe David Borba Manana }
34809f03740aSFilipe David Borba Manana
path_loop(struct send_ctx * sctx,struct fs_path * name,u64 ino,u64 gen,u64 * ancestor_ino)3481801bec36SRobbie Ko static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3482801bec36SRobbie Ko u64 ino, u64 gen, u64 *ancestor_ino)
3483801bec36SRobbie Ko {
3484801bec36SRobbie Ko int ret = 0;
3485801bec36SRobbie Ko u64 parent_inode = 0;
3486801bec36SRobbie Ko u64 parent_gen = 0;
3487801bec36SRobbie Ko u64 start_ino = ino;
3488801bec36SRobbie Ko
3489801bec36SRobbie Ko *ancestor_ino = 0;
3490801bec36SRobbie Ko while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3491801bec36SRobbie Ko fs_path_reset(name);
3492801bec36SRobbie Ko
34930b3f407eSFilipe Manana if (is_waiting_for_rm(sctx, ino, gen))
3494801bec36SRobbie Ko break;
3495801bec36SRobbie Ko if (is_waiting_for_move(sctx, ino)) {
3496801bec36SRobbie Ko if (*ancestor_ino == 0)
3497801bec36SRobbie Ko *ancestor_ino = ino;
3498801bec36SRobbie Ko ret = get_first_ref(sctx->parent_root, ino,
3499801bec36SRobbie Ko &parent_inode, &parent_gen, name);
3500801bec36SRobbie Ko } else {
3501801bec36SRobbie Ko ret = __get_cur_name_and_parent(sctx, ino, gen,
3502801bec36SRobbie Ko &parent_inode,
3503801bec36SRobbie Ko &parent_gen, name);
3504801bec36SRobbie Ko if (ret > 0) {
3505801bec36SRobbie Ko ret = 0;
3506801bec36SRobbie Ko break;
3507801bec36SRobbie Ko }
3508801bec36SRobbie Ko }
3509801bec36SRobbie Ko if (ret < 0)
3510801bec36SRobbie Ko break;
3511801bec36SRobbie Ko if (parent_inode == start_ino) {
3512801bec36SRobbie Ko ret = 1;
3513801bec36SRobbie Ko if (*ancestor_ino == 0)
3514801bec36SRobbie Ko *ancestor_ino = ino;
3515801bec36SRobbie Ko break;
3516801bec36SRobbie Ko }
3517801bec36SRobbie Ko ino = parent_inode;
3518801bec36SRobbie Ko gen = parent_gen;
3519801bec36SRobbie Ko }
3520801bec36SRobbie Ko return ret;
3521801bec36SRobbie Ko }
3522801bec36SRobbie Ko
apply_dir_move(struct send_ctx * sctx,struct pending_dir_move * pm)35239f03740aSFilipe David Borba Manana static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
35249f03740aSFilipe David Borba Manana {
35259f03740aSFilipe David Borba Manana struct fs_path *from_path = NULL;
35269f03740aSFilipe David Borba Manana struct fs_path *to_path = NULL;
35272b863a13SFilipe Manana struct fs_path *name = NULL;
35289f03740aSFilipe David Borba Manana u64 orig_progress = sctx->send_progress;
35299f03740aSFilipe David Borba Manana struct recorded_ref *cur;
35302b863a13SFilipe Manana u64 parent_ino, parent_gen;
35319dc44214SFilipe Manana struct waiting_dir_move *dm = NULL;
35329dc44214SFilipe Manana u64 rmdir_ino = 0;
35330b3f407eSFilipe Manana u64 rmdir_gen;
3534801bec36SRobbie Ko u64 ancestor;
3535801bec36SRobbie Ko bool is_orphan;
35369f03740aSFilipe David Borba Manana int ret;
35379f03740aSFilipe David Borba Manana
35382b863a13SFilipe Manana name = fs_path_alloc();
35399f03740aSFilipe David Borba Manana from_path = fs_path_alloc();
35402b863a13SFilipe Manana if (!name || !from_path) {
35412b863a13SFilipe Manana ret = -ENOMEM;
35422b863a13SFilipe Manana goto out;
35432b863a13SFilipe Manana }
35449f03740aSFilipe David Borba Manana
35459dc44214SFilipe Manana dm = get_waiting_dir_move(sctx, pm->ino);
35469dc44214SFilipe Manana ASSERT(dm);
35479dc44214SFilipe Manana rmdir_ino = dm->rmdir_ino;
35480b3f407eSFilipe Manana rmdir_gen = dm->rmdir_gen;
3549801bec36SRobbie Ko is_orphan = dm->orphanized;
35509dc44214SFilipe Manana free_waiting_dir_move(sctx, dm);
35512b863a13SFilipe Manana
3552801bec36SRobbie Ko if (is_orphan) {
355384471e24SFilipe Manana ret = gen_unique_name(sctx, pm->ino,
355484471e24SFilipe Manana pm->gen, from_path);
355584471e24SFilipe Manana } else {
35562b863a13SFilipe Manana ret = get_first_ref(sctx->parent_root, pm->ino,
35572b863a13SFilipe Manana &parent_ino, &parent_gen, name);
35582b863a13SFilipe Manana if (ret < 0)
35592b863a13SFilipe Manana goto out;
3560c992ec94SFilipe Manana ret = get_cur_path(sctx, parent_ino, parent_gen,
35612b863a13SFilipe Manana from_path);
35622b863a13SFilipe Manana if (ret < 0)
35632b863a13SFilipe Manana goto out;
35642b863a13SFilipe Manana ret = fs_path_add_path(from_path, name);
356584471e24SFilipe Manana }
35662b863a13SFilipe Manana if (ret < 0)
35672b863a13SFilipe Manana goto out;
35682b863a13SFilipe Manana
3569f959492fSFilipe Manana sctx->send_progress = sctx->cur_ino + 1;
3570801bec36SRobbie Ko ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
35717969e77aSFilipe Manana if (ret < 0)
35727969e77aSFilipe Manana goto out;
3573801bec36SRobbie Ko if (ret) {
3574801bec36SRobbie Ko LIST_HEAD(deleted_refs);
3575801bec36SRobbie Ko ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3576801bec36SRobbie Ko ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3577801bec36SRobbie Ko &pm->update_refs, &deleted_refs,
3578801bec36SRobbie Ko is_orphan);
3579801bec36SRobbie Ko if (ret < 0)
3580801bec36SRobbie Ko goto out;
3581801bec36SRobbie Ko if (rmdir_ino) {
3582801bec36SRobbie Ko dm = get_waiting_dir_move(sctx, pm->ino);
3583801bec36SRobbie Ko ASSERT(dm);
3584801bec36SRobbie Ko dm->rmdir_ino = rmdir_ino;
35850b3f407eSFilipe Manana dm->rmdir_gen = rmdir_gen;
3586801bec36SRobbie Ko }
3587801bec36SRobbie Ko goto out;
3588801bec36SRobbie Ko }
3589c992ec94SFilipe Manana fs_path_reset(name);
3590c992ec94SFilipe Manana to_path = name;
35912b863a13SFilipe Manana name = NULL;
35929f03740aSFilipe David Borba Manana ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
35939f03740aSFilipe David Borba Manana if (ret < 0)
35949f03740aSFilipe David Borba Manana goto out;
35959f03740aSFilipe David Borba Manana
35969f03740aSFilipe David Borba Manana ret = send_rename(sctx, from_path, to_path);
35979f03740aSFilipe David Borba Manana if (ret < 0)
35989f03740aSFilipe David Borba Manana goto out;
35999f03740aSFilipe David Borba Manana
36009dc44214SFilipe Manana if (rmdir_ino) {
36019dc44214SFilipe Manana struct orphan_dir_info *odi;
36020f96f517SRobbie Ko u64 gen;
36039dc44214SFilipe Manana
36040b3f407eSFilipe Manana odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
36059dc44214SFilipe Manana if (!odi) {
36069dc44214SFilipe Manana /* already deleted */
36079dc44214SFilipe Manana goto finish;
36089dc44214SFilipe Manana }
36090f96f517SRobbie Ko gen = odi->gen;
36100f96f517SRobbie Ko
361124970ccbSFilipe Manana ret = can_rmdir(sctx, rmdir_ino, gen);
36129dc44214SFilipe Manana if (ret < 0)
36139dc44214SFilipe Manana goto out;
36149dc44214SFilipe Manana if (!ret)
36159dc44214SFilipe Manana goto finish;
36169dc44214SFilipe Manana
36179dc44214SFilipe Manana name = fs_path_alloc();
36189dc44214SFilipe Manana if (!name) {
36199dc44214SFilipe Manana ret = -ENOMEM;
36209dc44214SFilipe Manana goto out;
36219dc44214SFilipe Manana }
36220f96f517SRobbie Ko ret = get_cur_path(sctx, rmdir_ino, gen, name);
36239dc44214SFilipe Manana if (ret < 0)
36249dc44214SFilipe Manana goto out;
36259dc44214SFilipe Manana ret = send_rmdir(sctx, name);
36269dc44214SFilipe Manana if (ret < 0)
36279dc44214SFilipe Manana goto out;
36289dc44214SFilipe Manana }
36299dc44214SFilipe Manana
36309dc44214SFilipe Manana finish:
36313e49363bSFilipe Manana ret = cache_dir_utimes(sctx, pm->ino, pm->gen);
36329f03740aSFilipe David Borba Manana if (ret < 0)
36339f03740aSFilipe David Borba Manana goto out;
36349f03740aSFilipe David Borba Manana
36359f03740aSFilipe David Borba Manana /*
36369f03740aSFilipe David Borba Manana * After rename/move, need to update the utimes of both new parent(s)
36379f03740aSFilipe David Borba Manana * and old parent(s).
36389f03740aSFilipe David Borba Manana */
36399f03740aSFilipe David Borba Manana list_for_each_entry(cur, &pm->update_refs, list) {
3640764433a1SRobbie Ko /*
3641764433a1SRobbie Ko * The parent inode might have been deleted in the send snapshot
3642764433a1SRobbie Ko */
36437e93f6dcSBingJing Chang ret = get_inode_info(sctx->send_root, cur->dir, NULL);
3644764433a1SRobbie Ko if (ret == -ENOENT) {
3645764433a1SRobbie Ko ret = 0;
36469dc44214SFilipe Manana continue;
3647764433a1SRobbie Ko }
3648764433a1SRobbie Ko if (ret < 0)
3649764433a1SRobbie Ko goto out;
3650764433a1SRobbie Ko
36513e49363bSFilipe Manana ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
36529f03740aSFilipe David Borba Manana if (ret < 0)
36539f03740aSFilipe David Borba Manana goto out;
36549f03740aSFilipe David Borba Manana }
36559f03740aSFilipe David Borba Manana
36569f03740aSFilipe David Borba Manana out:
36572b863a13SFilipe Manana fs_path_free(name);
36589f03740aSFilipe David Borba Manana fs_path_free(from_path);
36599f03740aSFilipe David Borba Manana fs_path_free(to_path);
36609f03740aSFilipe David Borba Manana sctx->send_progress = orig_progress;
36619f03740aSFilipe David Borba Manana
36629f03740aSFilipe David Borba Manana return ret;
36639f03740aSFilipe David Borba Manana }
36649f03740aSFilipe David Borba Manana
free_pending_move(struct send_ctx * sctx,struct pending_dir_move * m)36659f03740aSFilipe David Borba Manana static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
36669f03740aSFilipe David Borba Manana {
36679f03740aSFilipe David Borba Manana if (!list_empty(&m->list))
36689f03740aSFilipe David Borba Manana list_del(&m->list);
36699f03740aSFilipe David Borba Manana if (!RB_EMPTY_NODE(&m->node))
36709f03740aSFilipe David Borba Manana rb_erase(&m->node, &sctx->pending_dir_moves);
36719f03740aSFilipe David Borba Manana __free_recorded_refs(&m->update_refs);
36729f03740aSFilipe David Borba Manana kfree(m);
36739f03740aSFilipe David Borba Manana }
36749f03740aSFilipe David Borba Manana
tail_append_pending_moves(struct send_ctx * sctx,struct pending_dir_move * moves,struct list_head * stack)3675a4390aeeSRobbie Ko static void tail_append_pending_moves(struct send_ctx *sctx,
3676a4390aeeSRobbie Ko struct pending_dir_move *moves,
36779f03740aSFilipe David Borba Manana struct list_head *stack)
36789f03740aSFilipe David Borba Manana {
36799f03740aSFilipe David Borba Manana if (list_empty(&moves->list)) {
36809f03740aSFilipe David Borba Manana list_add_tail(&moves->list, stack);
36819f03740aSFilipe David Borba Manana } else {
36829f03740aSFilipe David Borba Manana LIST_HEAD(list);
36839f03740aSFilipe David Borba Manana list_splice_init(&moves->list, &list);
36849f03740aSFilipe David Borba Manana list_add_tail(&moves->list, stack);
36859f03740aSFilipe David Borba Manana list_splice_tail(&list, stack);
36869f03740aSFilipe David Borba Manana }
3687a4390aeeSRobbie Ko if (!RB_EMPTY_NODE(&moves->node)) {
3688a4390aeeSRobbie Ko rb_erase(&moves->node, &sctx->pending_dir_moves);
3689a4390aeeSRobbie Ko RB_CLEAR_NODE(&moves->node);
3690a4390aeeSRobbie Ko }
36919f03740aSFilipe David Borba Manana }
36929f03740aSFilipe David Borba Manana
apply_children_dir_moves(struct send_ctx * sctx)36939f03740aSFilipe David Borba Manana static int apply_children_dir_moves(struct send_ctx *sctx)
36949f03740aSFilipe David Borba Manana {
36959f03740aSFilipe David Borba Manana struct pending_dir_move *pm;
369684af994bSRuan Jinjie LIST_HEAD(stack);
36979f03740aSFilipe David Borba Manana u64 parent_ino = sctx->cur_ino;
36989f03740aSFilipe David Borba Manana int ret = 0;
36999f03740aSFilipe David Borba Manana
37009f03740aSFilipe David Borba Manana pm = get_pending_dir_moves(sctx, parent_ino);
37019f03740aSFilipe David Borba Manana if (!pm)
37029f03740aSFilipe David Borba Manana return 0;
37039f03740aSFilipe David Borba Manana
3704a4390aeeSRobbie Ko tail_append_pending_moves(sctx, pm, &stack);
37059f03740aSFilipe David Borba Manana
37069f03740aSFilipe David Borba Manana while (!list_empty(&stack)) {
37079f03740aSFilipe David Borba Manana pm = list_first_entry(&stack, struct pending_dir_move, list);
37089f03740aSFilipe David Borba Manana parent_ino = pm->ino;
37099f03740aSFilipe David Borba Manana ret = apply_dir_move(sctx, pm);
37109f03740aSFilipe David Borba Manana free_pending_move(sctx, pm);
37119f03740aSFilipe David Borba Manana if (ret)
37129f03740aSFilipe David Borba Manana goto out;
37139f03740aSFilipe David Borba Manana pm = get_pending_dir_moves(sctx, parent_ino);
37149f03740aSFilipe David Borba Manana if (pm)
3715a4390aeeSRobbie Ko tail_append_pending_moves(sctx, pm, &stack);
37169f03740aSFilipe David Borba Manana }
37179f03740aSFilipe David Borba Manana return 0;
37189f03740aSFilipe David Borba Manana
37199f03740aSFilipe David Borba Manana out:
37209f03740aSFilipe David Borba Manana while (!list_empty(&stack)) {
37219f03740aSFilipe David Borba Manana pm = list_first_entry(&stack, struct pending_dir_move, list);
37229f03740aSFilipe David Borba Manana free_pending_move(sctx, pm);
37239f03740aSFilipe David Borba Manana }
37249f03740aSFilipe David Borba Manana return ret;
37259f03740aSFilipe David Borba Manana }
37269f03740aSFilipe David Borba Manana
372784471e24SFilipe Manana /*
372884471e24SFilipe Manana * We might need to delay a directory rename even when no ancestor directory
372984471e24SFilipe Manana * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
373084471e24SFilipe Manana * renamed. This happens when we rename a directory to the old name (the name
373184471e24SFilipe Manana * in the parent root) of some other unrelated directory that got its rename
373284471e24SFilipe Manana * delayed due to some ancestor with higher number that got renamed.
373384471e24SFilipe Manana *
373484471e24SFilipe Manana * Example:
373584471e24SFilipe Manana *
373684471e24SFilipe Manana * Parent snapshot:
373784471e24SFilipe Manana * . (ino 256)
373884471e24SFilipe Manana * |---- a/ (ino 257)
373984471e24SFilipe Manana * | |---- file (ino 260)
374084471e24SFilipe Manana * |
374184471e24SFilipe Manana * |---- b/ (ino 258)
374284471e24SFilipe Manana * |---- c/ (ino 259)
374384471e24SFilipe Manana *
374484471e24SFilipe Manana * Send snapshot:
374584471e24SFilipe Manana * . (ino 256)
374684471e24SFilipe Manana * |---- a/ (ino 258)
374784471e24SFilipe Manana * |---- x/ (ino 259)
374884471e24SFilipe Manana * |---- y/ (ino 257)
374984471e24SFilipe Manana * |----- file (ino 260)
375084471e24SFilipe Manana *
375184471e24SFilipe Manana * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
375284471e24SFilipe Manana * from 'a' to 'x/y' happening first, which in turn depends on the rename of
375384471e24SFilipe Manana * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
375484471e24SFilipe Manana * must issue is:
375584471e24SFilipe Manana *
375684471e24SFilipe Manana * 1 - rename 259 from 'c' to 'x'
375784471e24SFilipe Manana * 2 - rename 257 from 'a' to 'x/y'
375884471e24SFilipe Manana * 3 - rename 258 from 'b' to 'a'
375984471e24SFilipe Manana *
376084471e24SFilipe Manana * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
376184471e24SFilipe Manana * be done right away and < 0 on error.
376284471e24SFilipe Manana */
wait_for_dest_dir_move(struct send_ctx * sctx,struct recorded_ref * parent_ref,const bool is_orphan)376384471e24SFilipe Manana static int wait_for_dest_dir_move(struct send_ctx *sctx,
376484471e24SFilipe Manana struct recorded_ref *parent_ref,
376584471e24SFilipe Manana const bool is_orphan)
376684471e24SFilipe Manana {
37672ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
376884471e24SFilipe Manana struct btrfs_path *path;
376984471e24SFilipe Manana struct btrfs_key key;
377084471e24SFilipe Manana struct btrfs_key di_key;
377184471e24SFilipe Manana struct btrfs_dir_item *di;
377284471e24SFilipe Manana u64 left_gen;
377384471e24SFilipe Manana u64 right_gen;
377484471e24SFilipe Manana int ret = 0;
3775801bec36SRobbie Ko struct waiting_dir_move *wdm;
377684471e24SFilipe Manana
377784471e24SFilipe Manana if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
377884471e24SFilipe Manana return 0;
377984471e24SFilipe Manana
378084471e24SFilipe Manana path = alloc_path_for_send();
378184471e24SFilipe Manana if (!path)
378284471e24SFilipe Manana return -ENOMEM;
378384471e24SFilipe Manana
378484471e24SFilipe Manana key.objectid = parent_ref->dir;
378584471e24SFilipe Manana key.type = BTRFS_DIR_ITEM_KEY;
378684471e24SFilipe Manana key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
378784471e24SFilipe Manana
378884471e24SFilipe Manana ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
378984471e24SFilipe Manana if (ret < 0) {
379084471e24SFilipe Manana goto out;
379184471e24SFilipe Manana } else if (ret > 0) {
379284471e24SFilipe Manana ret = 0;
379384471e24SFilipe Manana goto out;
379484471e24SFilipe Manana }
379584471e24SFilipe Manana
37962ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
37972ff7e61eSJeff Mahoney parent_ref->name_len);
379884471e24SFilipe Manana if (!di) {
379984471e24SFilipe Manana ret = 0;
380084471e24SFilipe Manana goto out;
380184471e24SFilipe Manana }
380284471e24SFilipe Manana /*
380384471e24SFilipe Manana * di_key.objectid has the number of the inode that has a dentry in the
380484471e24SFilipe Manana * parent directory with the same name that sctx->cur_ino is being
380584471e24SFilipe Manana * renamed to. We need to check if that inode is in the send root as
380684471e24SFilipe Manana * well and if it is currently marked as an inode with a pending rename,
380784471e24SFilipe Manana * if it is, we need to delay the rename of sctx->cur_ino as well, so
380884471e24SFilipe Manana * that it happens after that other inode is renamed.
380984471e24SFilipe Manana */
381084471e24SFilipe Manana btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
381184471e24SFilipe Manana if (di_key.type != BTRFS_INODE_ITEM_KEY) {
381284471e24SFilipe Manana ret = 0;
381384471e24SFilipe Manana goto out;
381484471e24SFilipe Manana }
381584471e24SFilipe Manana
38167e93f6dcSBingJing Chang ret = get_inode_gen(sctx->parent_root, di_key.objectid, &left_gen);
381784471e24SFilipe Manana if (ret < 0)
381884471e24SFilipe Manana goto out;
38197e93f6dcSBingJing Chang ret = get_inode_gen(sctx->send_root, di_key.objectid, &right_gen);
382084471e24SFilipe Manana if (ret < 0) {
382184471e24SFilipe Manana if (ret == -ENOENT)
382284471e24SFilipe Manana ret = 0;
382384471e24SFilipe Manana goto out;
382484471e24SFilipe Manana }
382584471e24SFilipe Manana
382684471e24SFilipe Manana /* Different inode, no need to delay the rename of sctx->cur_ino */
382784471e24SFilipe Manana if (right_gen != left_gen) {
382884471e24SFilipe Manana ret = 0;
382984471e24SFilipe Manana goto out;
383084471e24SFilipe Manana }
383184471e24SFilipe Manana
3832801bec36SRobbie Ko wdm = get_waiting_dir_move(sctx, di_key.objectid);
3833801bec36SRobbie Ko if (wdm && !wdm->orphanized) {
383484471e24SFilipe Manana ret = add_pending_dir_move(sctx,
383584471e24SFilipe Manana sctx->cur_ino,
383684471e24SFilipe Manana sctx->cur_inode_gen,
383784471e24SFilipe Manana di_key.objectid,
383884471e24SFilipe Manana &sctx->new_refs,
383984471e24SFilipe Manana &sctx->deleted_refs,
384084471e24SFilipe Manana is_orphan);
384184471e24SFilipe Manana if (!ret)
384284471e24SFilipe Manana ret = 1;
384384471e24SFilipe Manana }
384484471e24SFilipe Manana out:
384584471e24SFilipe Manana btrfs_free_path(path);
384684471e24SFilipe Manana return ret;
384784471e24SFilipe Manana }
384884471e24SFilipe Manana
384980aa6027SFilipe Manana /*
3850ea37d599SFilipe Manana * Check if inode ino2, or any of its ancestors, is inode ino1.
3851ea37d599SFilipe Manana * Return 1 if true, 0 if false and < 0 on error.
3852ea37d599SFilipe Manana */
check_ino_in_path(struct btrfs_root * root,const u64 ino1,const u64 ino1_gen,const u64 ino2,const u64 ino2_gen,struct fs_path * fs_path)3853ea37d599SFilipe Manana static int check_ino_in_path(struct btrfs_root *root,
3854ea37d599SFilipe Manana const u64 ino1,
3855ea37d599SFilipe Manana const u64 ino1_gen,
3856ea37d599SFilipe Manana const u64 ino2,
3857ea37d599SFilipe Manana const u64 ino2_gen,
3858ea37d599SFilipe Manana struct fs_path *fs_path)
3859ea37d599SFilipe Manana {
3860ea37d599SFilipe Manana u64 ino = ino2;
3861ea37d599SFilipe Manana
3862ea37d599SFilipe Manana if (ino1 == ino2)
3863ea37d599SFilipe Manana return ino1_gen == ino2_gen;
3864ea37d599SFilipe Manana
3865ea37d599SFilipe Manana while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3866ea37d599SFilipe Manana u64 parent;
3867ea37d599SFilipe Manana u64 parent_gen;
3868ea37d599SFilipe Manana int ret;
3869ea37d599SFilipe Manana
3870ea37d599SFilipe Manana fs_path_reset(fs_path);
3871ea37d599SFilipe Manana ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3872ea37d599SFilipe Manana if (ret < 0)
3873ea37d599SFilipe Manana return ret;
3874ea37d599SFilipe Manana if (parent == ino1)
3875ea37d599SFilipe Manana return parent_gen == ino1_gen;
3876ea37d599SFilipe Manana ino = parent;
3877ea37d599SFilipe Manana }
3878ea37d599SFilipe Manana return 0;
3879ea37d599SFilipe Manana }
3880ea37d599SFilipe Manana
3881ea37d599SFilipe Manana /*
388235a68080SGabriel Niebler * Check if inode ino1 is an ancestor of inode ino2 in the given root for any
3883ea37d599SFilipe Manana * possible path (in case ino2 is not a directory and has multiple hard links).
388480aa6027SFilipe Manana * Return 1 if true, 0 if false and < 0 on error.
388580aa6027SFilipe Manana */
is_ancestor(struct btrfs_root * root,const u64 ino1,const u64 ino1_gen,const u64 ino2,struct fs_path * fs_path)388680aa6027SFilipe Manana static int is_ancestor(struct btrfs_root *root,
388780aa6027SFilipe Manana const u64 ino1,
388880aa6027SFilipe Manana const u64 ino1_gen,
388980aa6027SFilipe Manana const u64 ino2,
389080aa6027SFilipe Manana struct fs_path *fs_path)
389180aa6027SFilipe Manana {
3892ea37d599SFilipe Manana bool free_fs_path = false;
389372c3668fSFilipe Manana int ret = 0;
389435a68080SGabriel Niebler int iter_ret = 0;
3895ea37d599SFilipe Manana struct btrfs_path *path = NULL;
3896ea37d599SFilipe Manana struct btrfs_key key;
389772c3668fSFilipe Manana
389872c3668fSFilipe Manana if (!fs_path) {
389972c3668fSFilipe Manana fs_path = fs_path_alloc();
390072c3668fSFilipe Manana if (!fs_path)
390172c3668fSFilipe Manana return -ENOMEM;
3902ea37d599SFilipe Manana free_fs_path = true;
390372c3668fSFilipe Manana }
390480aa6027SFilipe Manana
3905ea37d599SFilipe Manana path = alloc_path_for_send();
3906ea37d599SFilipe Manana if (!path) {
3907ea37d599SFilipe Manana ret = -ENOMEM;
3908ea37d599SFilipe Manana goto out;
3909ea37d599SFilipe Manana }
3910ea37d599SFilipe Manana
3911ea37d599SFilipe Manana key.objectid = ino2;
3912ea37d599SFilipe Manana key.type = BTRFS_INODE_REF_KEY;
3913ea37d599SFilipe Manana key.offset = 0;
3914ea37d599SFilipe Manana
391535a68080SGabriel Niebler btrfs_for_each_slot(root, &key, &key, path, iter_ret) {
3916ea37d599SFilipe Manana struct extent_buffer *leaf = path->nodes[0];
3917ea37d599SFilipe Manana int slot = path->slots[0];
3918ea37d599SFilipe Manana u32 cur_offset = 0;
3919ea37d599SFilipe Manana u32 item_size;
3920ea37d599SFilipe Manana
3921ea37d599SFilipe Manana if (key.objectid != ino2)
3922ea37d599SFilipe Manana break;
3923ea37d599SFilipe Manana if (key.type != BTRFS_INODE_REF_KEY &&
3924ea37d599SFilipe Manana key.type != BTRFS_INODE_EXTREF_KEY)
3925ea37d599SFilipe Manana break;
3926ea37d599SFilipe Manana
39273212fa14SJosef Bacik item_size = btrfs_item_size(leaf, slot);
3928ea37d599SFilipe Manana while (cur_offset < item_size) {
392980aa6027SFilipe Manana u64 parent;
393080aa6027SFilipe Manana u64 parent_gen;
393180aa6027SFilipe Manana
3932ea37d599SFilipe Manana if (key.type == BTRFS_INODE_EXTREF_KEY) {
3933ea37d599SFilipe Manana unsigned long ptr;
3934ea37d599SFilipe Manana struct btrfs_inode_extref *extref;
3935ea37d599SFilipe Manana
3936ea37d599SFilipe Manana ptr = btrfs_item_ptr_offset(leaf, slot);
3937ea37d599SFilipe Manana extref = (struct btrfs_inode_extref *)
3938ea37d599SFilipe Manana (ptr + cur_offset);
3939ea37d599SFilipe Manana parent = btrfs_inode_extref_parent(leaf,
3940ea37d599SFilipe Manana extref);
3941ea37d599SFilipe Manana cur_offset += sizeof(*extref);
3942ea37d599SFilipe Manana cur_offset += btrfs_inode_extref_name_len(leaf,
3943ea37d599SFilipe Manana extref);
3944ea37d599SFilipe Manana } else {
3945ea37d599SFilipe Manana parent = key.offset;
3946ea37d599SFilipe Manana cur_offset = item_size;
3947ea37d599SFilipe Manana }
3948ea37d599SFilipe Manana
39497e93f6dcSBingJing Chang ret = get_inode_gen(root, parent, &parent_gen);
3950ea37d599SFilipe Manana if (ret < 0)
3951ea37d599SFilipe Manana goto out;
3952ea37d599SFilipe Manana ret = check_ino_in_path(root, ino1, ino1_gen,
3953ea37d599SFilipe Manana parent, parent_gen, fs_path);
3954ea37d599SFilipe Manana if (ret)
3955ea37d599SFilipe Manana goto out;
3956ea37d599SFilipe Manana }
3957ea37d599SFilipe Manana }
395880aa6027SFilipe Manana ret = 0;
395935a68080SGabriel Niebler if (iter_ret < 0)
396035a68080SGabriel Niebler ret = iter_ret;
396135a68080SGabriel Niebler
396272c3668fSFilipe Manana out:
3963ea37d599SFilipe Manana btrfs_free_path(path);
3964ea37d599SFilipe Manana if (free_fs_path)
396572c3668fSFilipe Manana fs_path_free(fs_path);
396672c3668fSFilipe Manana return ret;
396780aa6027SFilipe Manana }
396880aa6027SFilipe Manana
wait_for_parent_move(struct send_ctx * sctx,struct recorded_ref * parent_ref,const bool is_orphan)39699f03740aSFilipe David Borba Manana static int wait_for_parent_move(struct send_ctx *sctx,
39708b191a68SFilipe Manana struct recorded_ref *parent_ref,
39718b191a68SFilipe Manana const bool is_orphan)
39729f03740aSFilipe David Borba Manana {
3973f959492fSFilipe Manana int ret = 0;
39749f03740aSFilipe David Borba Manana u64 ino = parent_ref->dir;
3975fe9c798dSFilipe Manana u64 ino_gen = parent_ref->dir_gen;
39769f03740aSFilipe David Borba Manana u64 parent_ino_before, parent_ino_after;
39779f03740aSFilipe David Borba Manana struct fs_path *path_before = NULL;
39789f03740aSFilipe David Borba Manana struct fs_path *path_after = NULL;
39799f03740aSFilipe David Borba Manana int len1, len2;
39809f03740aSFilipe David Borba Manana
39819f03740aSFilipe David Borba Manana path_after = fs_path_alloc();
3982f959492fSFilipe Manana path_before = fs_path_alloc();
3983f959492fSFilipe Manana if (!path_after || !path_before) {
39849f03740aSFilipe David Borba Manana ret = -ENOMEM;
39859f03740aSFilipe David Borba Manana goto out;
39869f03740aSFilipe David Borba Manana }
39879f03740aSFilipe David Borba Manana
3988bfa7e1f8SFilipe Manana /*
3989f959492fSFilipe Manana * Our current directory inode may not yet be renamed/moved because some
3990f959492fSFilipe Manana * ancestor (immediate or not) has to be renamed/moved first. So find if
3991f959492fSFilipe Manana * such ancestor exists and make sure our own rename/move happens after
399280aa6027SFilipe Manana * that ancestor is processed to avoid path build infinite loops (done
399380aa6027SFilipe Manana * at get_cur_path()).
3994bfa7e1f8SFilipe Manana */
3995f959492fSFilipe Manana while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3996fe9c798dSFilipe Manana u64 parent_ino_after_gen;
3997fe9c798dSFilipe Manana
3998f959492fSFilipe Manana if (is_waiting_for_move(sctx, ino)) {
399980aa6027SFilipe Manana /*
400080aa6027SFilipe Manana * If the current inode is an ancestor of ino in the
400180aa6027SFilipe Manana * parent root, we need to delay the rename of the
400280aa6027SFilipe Manana * current inode, otherwise don't delayed the rename
400380aa6027SFilipe Manana * because we can end up with a circular dependency
400480aa6027SFilipe Manana * of renames, resulting in some directories never
400580aa6027SFilipe Manana * getting the respective rename operations issued in
400680aa6027SFilipe Manana * the send stream or getting into infinite path build
400780aa6027SFilipe Manana * loops.
400880aa6027SFilipe Manana */
400980aa6027SFilipe Manana ret = is_ancestor(sctx->parent_root,
401080aa6027SFilipe Manana sctx->cur_ino, sctx->cur_inode_gen,
401180aa6027SFilipe Manana ino, path_before);
40124122ea64SFilipe Manana if (ret)
4013f959492fSFilipe Manana break;
4014f959492fSFilipe Manana }
4015bfa7e1f8SFilipe Manana
4016bfa7e1f8SFilipe Manana fs_path_reset(path_before);
4017bfa7e1f8SFilipe Manana fs_path_reset(path_after);
4018bfa7e1f8SFilipe Manana
4019bfa7e1f8SFilipe Manana ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
4020fe9c798dSFilipe Manana &parent_ino_after_gen, path_after);
4021bfa7e1f8SFilipe Manana if (ret < 0)
4022bfa7e1f8SFilipe Manana goto out;
4023bfa7e1f8SFilipe Manana ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
4024bfa7e1f8SFilipe Manana NULL, path_before);
4025f959492fSFilipe Manana if (ret < 0 && ret != -ENOENT) {
4026bfa7e1f8SFilipe Manana goto out;
4027f959492fSFilipe Manana } else if (ret == -ENOENT) {
4028bf8e8ca6SFilipe Manana ret = 0;
4029f959492fSFilipe Manana break;
4030bfa7e1f8SFilipe Manana }
4031bfa7e1f8SFilipe Manana
4032bfa7e1f8SFilipe Manana len1 = fs_path_len(path_before);
4033bfa7e1f8SFilipe Manana len2 = fs_path_len(path_after);
4034f959492fSFilipe Manana if (ino > sctx->cur_ino &&
4035f959492fSFilipe Manana (parent_ino_before != parent_ino_after || len1 != len2 ||
4036f959492fSFilipe Manana memcmp(path_before->start, path_after->start, len1))) {
4037fe9c798dSFilipe Manana u64 parent_ino_gen;
4038fe9c798dSFilipe Manana
40397e93f6dcSBingJing Chang ret = get_inode_gen(sctx->parent_root, ino, &parent_ino_gen);
4040fe9c798dSFilipe Manana if (ret < 0)
4041fe9c798dSFilipe Manana goto out;
4042fe9c798dSFilipe Manana if (ino_gen == parent_ino_gen) {
4043bfa7e1f8SFilipe Manana ret = 1;
4044bfa7e1f8SFilipe Manana break;
4045bfa7e1f8SFilipe Manana }
4046fe9c798dSFilipe Manana }
4047bfa7e1f8SFilipe Manana ino = parent_ino_after;
4048fe9c798dSFilipe Manana ino_gen = parent_ino_after_gen;
4049bfa7e1f8SFilipe Manana }
4050bfa7e1f8SFilipe Manana
40519f03740aSFilipe David Borba Manana out:
40529f03740aSFilipe David Borba Manana fs_path_free(path_before);
40539f03740aSFilipe David Borba Manana fs_path_free(path_after);
40549f03740aSFilipe David Borba Manana
4055f959492fSFilipe Manana if (ret == 1) {
4056f959492fSFilipe Manana ret = add_pending_dir_move(sctx,
4057f959492fSFilipe Manana sctx->cur_ino,
4058f959492fSFilipe Manana sctx->cur_inode_gen,
4059f959492fSFilipe Manana ino,
4060f959492fSFilipe Manana &sctx->new_refs,
406184471e24SFilipe Manana &sctx->deleted_refs,
40628b191a68SFilipe Manana is_orphan);
4063f959492fSFilipe Manana if (!ret)
4064f959492fSFilipe Manana ret = 1;
4065f959492fSFilipe Manana }
4066f959492fSFilipe Manana
40679f03740aSFilipe David Borba Manana return ret;
40689f03740aSFilipe David Borba Manana }
40699f03740aSFilipe David Borba Manana
update_ref_path(struct send_ctx * sctx,struct recorded_ref * ref)4070f5962781SFilipe Manana static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
4071f5962781SFilipe Manana {
4072f5962781SFilipe Manana int ret;
4073f5962781SFilipe Manana struct fs_path *new_path;
4074f5962781SFilipe Manana
4075f5962781SFilipe Manana /*
4076f5962781SFilipe Manana * Our reference's name member points to its full_path member string, so
4077f5962781SFilipe Manana * we use here a new path.
4078f5962781SFilipe Manana */
4079f5962781SFilipe Manana new_path = fs_path_alloc();
4080f5962781SFilipe Manana if (!new_path)
4081f5962781SFilipe Manana return -ENOMEM;
4082f5962781SFilipe Manana
4083f5962781SFilipe Manana ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
4084f5962781SFilipe Manana if (ret < 0) {
4085f5962781SFilipe Manana fs_path_free(new_path);
4086f5962781SFilipe Manana return ret;
4087f5962781SFilipe Manana }
4088f5962781SFilipe Manana ret = fs_path_add(new_path, ref->name, ref->name_len);
4089f5962781SFilipe Manana if (ret < 0) {
4090f5962781SFilipe Manana fs_path_free(new_path);
4091f5962781SFilipe Manana return ret;
4092f5962781SFilipe Manana }
4093f5962781SFilipe Manana
4094f5962781SFilipe Manana fs_path_free(ref->full_path);
4095f5962781SFilipe Manana set_ref_path(ref, new_path);
4096f5962781SFilipe Manana
4097f5962781SFilipe Manana return 0;
4098f5962781SFilipe Manana }
4099f5962781SFilipe Manana
410031db9f7cSAlexander Block /*
41019c2b4e03SFilipe Manana * When processing the new references for an inode we may orphanize an existing
41029c2b4e03SFilipe Manana * directory inode because its old name conflicts with one of the new references
41039c2b4e03SFilipe Manana * of the current inode. Later, when processing another new reference of our
41049c2b4e03SFilipe Manana * inode, we might need to orphanize another inode, but the path we have in the
41059c2b4e03SFilipe Manana * reference reflects the pre-orphanization name of the directory we previously
41069c2b4e03SFilipe Manana * orphanized. For example:
41079c2b4e03SFilipe Manana *
41089c2b4e03SFilipe Manana * parent snapshot looks like:
41099c2b4e03SFilipe Manana *
41109c2b4e03SFilipe Manana * . (ino 256)
41119c2b4e03SFilipe Manana * |----- f1 (ino 257)
41129c2b4e03SFilipe Manana * |----- f2 (ino 258)
41139c2b4e03SFilipe Manana * |----- d1/ (ino 259)
41149c2b4e03SFilipe Manana * |----- d2/ (ino 260)
41159c2b4e03SFilipe Manana *
41169c2b4e03SFilipe Manana * send snapshot looks like:
41179c2b4e03SFilipe Manana *
41189c2b4e03SFilipe Manana * . (ino 256)
41199c2b4e03SFilipe Manana * |----- d1 (ino 258)
41209c2b4e03SFilipe Manana * |----- f2/ (ino 259)
41219c2b4e03SFilipe Manana * |----- f2_link/ (ino 260)
41229c2b4e03SFilipe Manana * | |----- f1 (ino 257)
41239c2b4e03SFilipe Manana * |
41249c2b4e03SFilipe Manana * |----- d2 (ino 258)
41259c2b4e03SFilipe Manana *
41269c2b4e03SFilipe Manana * When processing inode 257 we compute the name for inode 259 as "d1", and we
41279c2b4e03SFilipe Manana * cache it in the name cache. Later when we start processing inode 258, when
41289c2b4e03SFilipe Manana * collecting all its new references we set a full path of "d1/d2" for its new
41299c2b4e03SFilipe Manana * reference with name "d2". When we start processing the new references we
41309c2b4e03SFilipe Manana * start by processing the new reference with name "d1", and this results in
41319c2b4e03SFilipe Manana * orphanizing inode 259, since its old reference causes a conflict. Then we
41329c2b4e03SFilipe Manana * move on the next new reference, with name "d2", and we find out we must
41339c2b4e03SFilipe Manana * orphanize inode 260, as its old reference conflicts with ours - but for the
41349c2b4e03SFilipe Manana * orphanization we use a source path corresponding to the path we stored in the
41359c2b4e03SFilipe Manana * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
41369c2b4e03SFilipe Manana * receiver fail since the path component "d1/" no longer exists, it was renamed
41379c2b4e03SFilipe Manana * to "o259-6-0/" when processing the previous new reference. So in this case we
41389c2b4e03SFilipe Manana * must recompute the path in the new reference and use it for the new
41399c2b4e03SFilipe Manana * orphanization operation.
41409c2b4e03SFilipe Manana */
refresh_ref_path(struct send_ctx * sctx,struct recorded_ref * ref)41419c2b4e03SFilipe Manana static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
41429c2b4e03SFilipe Manana {
41439c2b4e03SFilipe Manana char *name;
41449c2b4e03SFilipe Manana int ret;
41459c2b4e03SFilipe Manana
41469c2b4e03SFilipe Manana name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
41479c2b4e03SFilipe Manana if (!name)
41489c2b4e03SFilipe Manana return -ENOMEM;
41499c2b4e03SFilipe Manana
41509c2b4e03SFilipe Manana fs_path_reset(ref->full_path);
41519c2b4e03SFilipe Manana ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
41529c2b4e03SFilipe Manana if (ret < 0)
41539c2b4e03SFilipe Manana goto out;
41549c2b4e03SFilipe Manana
41559c2b4e03SFilipe Manana ret = fs_path_add(ref->full_path, name, ref->name_len);
41569c2b4e03SFilipe Manana if (ret < 0)
41579c2b4e03SFilipe Manana goto out;
41589c2b4e03SFilipe Manana
41599c2b4e03SFilipe Manana /* Update the reference's base name pointer. */
41609c2b4e03SFilipe Manana set_ref_path(ref, ref->full_path);
41619c2b4e03SFilipe Manana out:
41629c2b4e03SFilipe Manana kfree(name);
41639c2b4e03SFilipe Manana return ret;
41649c2b4e03SFilipe Manana }
41659c2b4e03SFilipe Manana
41669c2b4e03SFilipe Manana /*
416731db9f7cSAlexander Block * This does all the move/link/unlink/rmdir magic.
416831db9f7cSAlexander Block */
process_recorded_refs(struct send_ctx * sctx,int * pending_move)41699f03740aSFilipe David Borba Manana static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
417031db9f7cSAlexander Block {
417104ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
417231db9f7cSAlexander Block int ret = 0;
417331db9f7cSAlexander Block struct recorded_ref *cur;
41741f4692daSAlexander Block struct recorded_ref *cur2;
417584af994bSRuan Jinjie LIST_HEAD(check_dirs);
417631db9f7cSAlexander Block struct fs_path *valid_path = NULL;
4177b24baf69SChris Mason u64 ow_inode = 0;
417831db9f7cSAlexander Block u64 ow_gen;
4179f5962781SFilipe Manana u64 ow_mode;
418031db9f7cSAlexander Block int did_overwrite = 0;
418131db9f7cSAlexander Block int is_orphan = 0;
418229d6d30fSFilipe Manana u64 last_dir_ino_rm = 0;
418384471e24SFilipe Manana bool can_rename = true;
4184f5962781SFilipe Manana bool orphanized_dir = false;
4185fdb13889SFilipe Manana bool orphanized_ancestor = false;
418631db9f7cSAlexander Block
418704ab956eSJeff Mahoney btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
418831db9f7cSAlexander Block
41896d85ed05SAlexander Block /*
41906d85ed05SAlexander Block * This should never happen as the root dir always has the same ref
41916d85ed05SAlexander Block * which is always '..'
41926d85ed05SAlexander Block */
41936d85ed05SAlexander Block BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
41946d85ed05SAlexander Block
4195924794c9STsutomu Itoh valid_path = fs_path_alloc();
419631db9f7cSAlexander Block if (!valid_path) {
419731db9f7cSAlexander Block ret = -ENOMEM;
419831db9f7cSAlexander Block goto out;
419931db9f7cSAlexander Block }
420031db9f7cSAlexander Block
420131db9f7cSAlexander Block /*
420231db9f7cSAlexander Block * First, check if the first ref of the current inode was overwritten
420331db9f7cSAlexander Block * before. If yes, we know that the current inode was already orphanized
420431db9f7cSAlexander Block * and thus use the orphan name. If not, we can use get_cur_path to
420531db9f7cSAlexander Block * get the path of the first ref as it would like while receiving at
420631db9f7cSAlexander Block * this point in time.
420731db9f7cSAlexander Block * New inodes are always orphan at the beginning, so force to use the
420831db9f7cSAlexander Block * orphan name in this case.
420931db9f7cSAlexander Block * The first ref is stored in valid_path and will be updated if it
421031db9f7cSAlexander Block * gets moved around.
421131db9f7cSAlexander Block */
421231db9f7cSAlexander Block if (!sctx->cur_inode_new) {
421331db9f7cSAlexander Block ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
421431db9f7cSAlexander Block sctx->cur_inode_gen);
421531db9f7cSAlexander Block if (ret < 0)
421631db9f7cSAlexander Block goto out;
421731db9f7cSAlexander Block if (ret)
421831db9f7cSAlexander Block did_overwrite = 1;
421931db9f7cSAlexander Block }
422031db9f7cSAlexander Block if (sctx->cur_inode_new || did_overwrite) {
422131db9f7cSAlexander Block ret = gen_unique_name(sctx, sctx->cur_ino,
422231db9f7cSAlexander Block sctx->cur_inode_gen, valid_path);
422331db9f7cSAlexander Block if (ret < 0)
422431db9f7cSAlexander Block goto out;
422531db9f7cSAlexander Block is_orphan = 1;
422631db9f7cSAlexander Block } else {
422731db9f7cSAlexander Block ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
422831db9f7cSAlexander Block valid_path);
422931db9f7cSAlexander Block if (ret < 0)
423031db9f7cSAlexander Block goto out;
423131db9f7cSAlexander Block }
423231db9f7cSAlexander Block
423331db9f7cSAlexander Block /*
423498272bb7SFilipe Manana * Before doing any rename and link operations, do a first pass on the
423598272bb7SFilipe Manana * new references to orphanize any unprocessed inodes that may have a
423698272bb7SFilipe Manana * reference that conflicts with one of the new references of the current
423798272bb7SFilipe Manana * inode. This needs to happen first because a new reference may conflict
423898272bb7SFilipe Manana * with the old reference of a parent directory, so we must make sure
423998272bb7SFilipe Manana * that the path used for link and rename commands don't use an
424098272bb7SFilipe Manana * orphanized name when an ancestor was not yet orphanized.
424198272bb7SFilipe Manana *
424298272bb7SFilipe Manana * Example:
424398272bb7SFilipe Manana *
424498272bb7SFilipe Manana * Parent snapshot:
424598272bb7SFilipe Manana *
424698272bb7SFilipe Manana * . (ino 256)
424798272bb7SFilipe Manana * |----- testdir/ (ino 259)
424898272bb7SFilipe Manana * | |----- a (ino 257)
424998272bb7SFilipe Manana * |
425098272bb7SFilipe Manana * |----- b (ino 258)
425198272bb7SFilipe Manana *
425298272bb7SFilipe Manana * Send snapshot:
425398272bb7SFilipe Manana *
425498272bb7SFilipe Manana * . (ino 256)
425598272bb7SFilipe Manana * |----- testdir_2/ (ino 259)
425698272bb7SFilipe Manana * | |----- a (ino 260)
425798272bb7SFilipe Manana * |
425898272bb7SFilipe Manana * |----- testdir (ino 257)
425998272bb7SFilipe Manana * |----- b (ino 257)
426098272bb7SFilipe Manana * |----- b2 (ino 258)
426198272bb7SFilipe Manana *
426298272bb7SFilipe Manana * Processing the new reference for inode 257 with name "b" may happen
426398272bb7SFilipe Manana * before processing the new reference with name "testdir". If so, we
426498272bb7SFilipe Manana * must make sure that by the time we send a link command to create the
426598272bb7SFilipe Manana * hard link "b", inode 259 was already orphanized, since the generated
426698272bb7SFilipe Manana * path in "valid_path" already contains the orphanized name for 259.
426798272bb7SFilipe Manana * We are processing inode 257, so only later when processing 259 we do
426898272bb7SFilipe Manana * the rename operation to change its temporary (orphanized) name to
426998272bb7SFilipe Manana * "testdir_2".
42701f4692daSAlexander Block */
427198272bb7SFilipe Manana list_for_each_entry(cur, &sctx->new_refs, list) {
4272498581f3SFilipe Manana ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
42731f4692daSAlexander Block if (ret < 0)
42741f4692daSAlexander Block goto out;
427598272bb7SFilipe Manana if (ret == inode_state_will_create)
427698272bb7SFilipe Manana continue;
42771f4692daSAlexander Block
42781f4692daSAlexander Block /*
427998272bb7SFilipe Manana * Check if this new ref would overwrite the first ref of another
428098272bb7SFilipe Manana * unprocessed inode. If yes, orphanize the overwritten inode.
428198272bb7SFilipe Manana * If we find an overwritten ref that is not the first ref,
428298272bb7SFilipe Manana * simply unlink it.
428331db9f7cSAlexander Block */
428431db9f7cSAlexander Block ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
428531db9f7cSAlexander Block cur->name, cur->name_len,
4286f5962781SFilipe Manana &ow_inode, &ow_gen, &ow_mode);
428731db9f7cSAlexander Block if (ret < 0)
428831db9f7cSAlexander Block goto out;
428931db9f7cSAlexander Block if (ret) {
4290924794c9STsutomu Itoh ret = is_first_ref(sctx->parent_root,
429131db9f7cSAlexander Block ow_inode, cur->dir, cur->name,
429231db9f7cSAlexander Block cur->name_len);
429331db9f7cSAlexander Block if (ret < 0)
429431db9f7cSAlexander Block goto out;
429531db9f7cSAlexander Block if (ret) {
42968996a48cSFilipe Manana struct name_cache_entry *nce;
4297801bec36SRobbie Ko struct waiting_dir_move *wdm;
42988996a48cSFilipe Manana
42999c2b4e03SFilipe Manana if (orphanized_dir) {
43009c2b4e03SFilipe Manana ret = refresh_ref_path(sctx, cur);
43019c2b4e03SFilipe Manana if (ret < 0)
43029c2b4e03SFilipe Manana goto out;
43039c2b4e03SFilipe Manana }
43049c2b4e03SFilipe Manana
430531db9f7cSAlexander Block ret = orphanize_inode(sctx, ow_inode, ow_gen,
430631db9f7cSAlexander Block cur->full_path);
430731db9f7cSAlexander Block if (ret < 0)
430831db9f7cSAlexander Block goto out;
4309f5962781SFilipe Manana if (S_ISDIR(ow_mode))
4310f5962781SFilipe Manana orphanized_dir = true;
4311801bec36SRobbie Ko
4312801bec36SRobbie Ko /*
4313801bec36SRobbie Ko * If ow_inode has its rename operation delayed
4314801bec36SRobbie Ko * make sure that its orphanized name is used in
4315801bec36SRobbie Ko * the source path when performing its rename
4316801bec36SRobbie Ko * operation.
4317801bec36SRobbie Ko */
43188c139e1dSFilipe Manana wdm = get_waiting_dir_move(sctx, ow_inode);
43198c139e1dSFilipe Manana if (wdm)
4320801bec36SRobbie Ko wdm->orphanized = true;
4321801bec36SRobbie Ko
43228996a48cSFilipe Manana /*
43238996a48cSFilipe Manana * Make sure we clear our orphanized inode's
43248996a48cSFilipe Manana * name from the name cache. This is because the
43258996a48cSFilipe Manana * inode ow_inode might be an ancestor of some
43268996a48cSFilipe Manana * other inode that will be orphanized as well
43278996a48cSFilipe Manana * later and has an inode number greater than
43288996a48cSFilipe Manana * sctx->send_progress. We need to prevent
43298996a48cSFilipe Manana * future name lookups from using the old name
43308996a48cSFilipe Manana * and get instead the orphan name.
43318996a48cSFilipe Manana */
43328996a48cSFilipe Manana nce = name_cache_search(sctx, ow_inode, ow_gen);
4333c48545deSFilipe Manana if (nce)
4334c48545deSFilipe Manana btrfs_lru_cache_remove(&sctx->name_cache,
4335c48545deSFilipe Manana &nce->entry);
4336801bec36SRobbie Ko
4337801bec36SRobbie Ko /*
4338801bec36SRobbie Ko * ow_inode might currently be an ancestor of
4339801bec36SRobbie Ko * cur_ino, therefore compute valid_path (the
4340801bec36SRobbie Ko * current path of cur_ino) again because it
4341801bec36SRobbie Ko * might contain the pre-orphanization name of
4342801bec36SRobbie Ko * ow_inode, which is no longer valid.
4343801bec36SRobbie Ko */
434472c3668fSFilipe Manana ret = is_ancestor(sctx->parent_root,
434572c3668fSFilipe Manana ow_inode, ow_gen,
434672c3668fSFilipe Manana sctx->cur_ino, NULL);
434772c3668fSFilipe Manana if (ret > 0) {
4348fdb13889SFilipe Manana orphanized_ancestor = true;
4349801bec36SRobbie Ko fs_path_reset(valid_path);
4350801bec36SRobbie Ko ret = get_cur_path(sctx, sctx->cur_ino,
435172c3668fSFilipe Manana sctx->cur_inode_gen,
435272c3668fSFilipe Manana valid_path);
435372c3668fSFilipe Manana }
4354801bec36SRobbie Ko if (ret < 0)
4355801bec36SRobbie Ko goto out;
435631db9f7cSAlexander Block } else {
4357d8ac76cdSFilipe Manana /*
4358d8ac76cdSFilipe Manana * If we previously orphanized a directory that
4359d8ac76cdSFilipe Manana * collided with a new reference that we already
4360d8ac76cdSFilipe Manana * processed, recompute the current path because
4361d8ac76cdSFilipe Manana * that directory may be part of the path.
4362d8ac76cdSFilipe Manana */
4363d8ac76cdSFilipe Manana if (orphanized_dir) {
4364d8ac76cdSFilipe Manana ret = refresh_ref_path(sctx, cur);
4365d8ac76cdSFilipe Manana if (ret < 0)
4366d8ac76cdSFilipe Manana goto out;
4367d8ac76cdSFilipe Manana }
436831db9f7cSAlexander Block ret = send_unlink(sctx, cur->full_path);
436931db9f7cSAlexander Block if (ret < 0)
437031db9f7cSAlexander Block goto out;
437131db9f7cSAlexander Block }
437231db9f7cSAlexander Block }
437331db9f7cSAlexander Block
437498272bb7SFilipe Manana }
437598272bb7SFilipe Manana
437698272bb7SFilipe Manana list_for_each_entry(cur, &sctx->new_refs, list) {
437798272bb7SFilipe Manana /*
437898272bb7SFilipe Manana * We may have refs where the parent directory does not exist
437998272bb7SFilipe Manana * yet. This happens if the parent directories inum is higher
438098272bb7SFilipe Manana * than the current inum. To handle this case, we create the
438198272bb7SFilipe Manana * parent directory out of order. But we need to check if this
438298272bb7SFilipe Manana * did already happen before due to other refs in the same dir.
438398272bb7SFilipe Manana */
4384498581f3SFilipe Manana ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
438598272bb7SFilipe Manana if (ret < 0)
438698272bb7SFilipe Manana goto out;
438798272bb7SFilipe Manana if (ret == inode_state_will_create) {
438898272bb7SFilipe Manana ret = 0;
438998272bb7SFilipe Manana /*
439098272bb7SFilipe Manana * First check if any of the current inodes refs did
439198272bb7SFilipe Manana * already create the dir.
439298272bb7SFilipe Manana */
439398272bb7SFilipe Manana list_for_each_entry(cur2, &sctx->new_refs, list) {
439498272bb7SFilipe Manana if (cur == cur2)
439598272bb7SFilipe Manana break;
439698272bb7SFilipe Manana if (cur2->dir == cur->dir) {
439798272bb7SFilipe Manana ret = 1;
439898272bb7SFilipe Manana break;
439998272bb7SFilipe Manana }
440098272bb7SFilipe Manana }
440198272bb7SFilipe Manana
440298272bb7SFilipe Manana /*
440398272bb7SFilipe Manana * If that did not happen, check if a previous inode
440498272bb7SFilipe Manana * did already create the dir.
440598272bb7SFilipe Manana */
440698272bb7SFilipe Manana if (!ret)
440798272bb7SFilipe Manana ret = did_create_dir(sctx, cur->dir);
440898272bb7SFilipe Manana if (ret < 0)
440998272bb7SFilipe Manana goto out;
441098272bb7SFilipe Manana if (!ret) {
441198272bb7SFilipe Manana ret = send_create_inode(sctx, cur->dir);
441298272bb7SFilipe Manana if (ret < 0)
441398272bb7SFilipe Manana goto out;
4414e8a7f49dSFilipe Manana cache_dir_created(sctx, cur->dir);
441598272bb7SFilipe Manana }
441698272bb7SFilipe Manana }
441798272bb7SFilipe Manana
441884471e24SFilipe Manana if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
441984471e24SFilipe Manana ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
442084471e24SFilipe Manana if (ret < 0)
442184471e24SFilipe Manana goto out;
442284471e24SFilipe Manana if (ret == 1) {
442384471e24SFilipe Manana can_rename = false;
442484471e24SFilipe Manana *pending_move = 1;
442584471e24SFilipe Manana }
442684471e24SFilipe Manana }
442784471e24SFilipe Manana
44288b191a68SFilipe Manana if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
44298b191a68SFilipe Manana can_rename) {
44308b191a68SFilipe Manana ret = wait_for_parent_move(sctx, cur, is_orphan);
44318b191a68SFilipe Manana if (ret < 0)
44328b191a68SFilipe Manana goto out;
44338b191a68SFilipe Manana if (ret == 1) {
44348b191a68SFilipe Manana can_rename = false;
44358b191a68SFilipe Manana *pending_move = 1;
44368b191a68SFilipe Manana }
44378b191a68SFilipe Manana }
44388b191a68SFilipe Manana
443931db9f7cSAlexander Block /*
444031db9f7cSAlexander Block * link/move the ref to the new place. If we have an orphan
444131db9f7cSAlexander Block * inode, move it and update valid_path. If not, link or move
444231db9f7cSAlexander Block * it depending on the inode mode.
444331db9f7cSAlexander Block */
444484471e24SFilipe Manana if (is_orphan && can_rename) {
444531db9f7cSAlexander Block ret = send_rename(sctx, valid_path, cur->full_path);
444631db9f7cSAlexander Block if (ret < 0)
444731db9f7cSAlexander Block goto out;
444831db9f7cSAlexander Block is_orphan = 0;
444931db9f7cSAlexander Block ret = fs_path_copy(valid_path, cur->full_path);
445031db9f7cSAlexander Block if (ret < 0)
445131db9f7cSAlexander Block goto out;
445284471e24SFilipe Manana } else if (can_rename) {
445331db9f7cSAlexander Block if (S_ISDIR(sctx->cur_inode_mode)) {
445431db9f7cSAlexander Block /*
445531db9f7cSAlexander Block * Dirs can't be linked, so move it. For moved
445631db9f7cSAlexander Block * dirs, we always have one new and one deleted
445731db9f7cSAlexander Block * ref. The deleted ref is ignored later.
445831db9f7cSAlexander Block */
445931db9f7cSAlexander Block ret = send_rename(sctx, valid_path,
446031db9f7cSAlexander Block cur->full_path);
44619f03740aSFilipe David Borba Manana if (!ret)
44629f03740aSFilipe David Borba Manana ret = fs_path_copy(valid_path,
44639f03740aSFilipe David Borba Manana cur->full_path);
446431db9f7cSAlexander Block if (ret < 0)
446531db9f7cSAlexander Block goto out;
446631db9f7cSAlexander Block } else {
4467f5962781SFilipe Manana /*
4468f5962781SFilipe Manana * We might have previously orphanized an inode
4469f5962781SFilipe Manana * which is an ancestor of our current inode,
4470f5962781SFilipe Manana * so our reference's full path, which was
4471f5962781SFilipe Manana * computed before any such orphanizations, must
4472f5962781SFilipe Manana * be updated.
4473f5962781SFilipe Manana */
4474f5962781SFilipe Manana if (orphanized_dir) {
4475f5962781SFilipe Manana ret = update_ref_path(sctx, cur);
4476f5962781SFilipe Manana if (ret < 0)
4477f5962781SFilipe Manana goto out;
4478f5962781SFilipe Manana }
447931db9f7cSAlexander Block ret = send_link(sctx, cur->full_path,
448031db9f7cSAlexander Block valid_path);
448131db9f7cSAlexander Block if (ret < 0)
448231db9f7cSAlexander Block goto out;
448331db9f7cSAlexander Block }
448431db9f7cSAlexander Block }
4485ba5e8f2eSJosef Bacik ret = dup_ref(cur, &check_dirs);
448631db9f7cSAlexander Block if (ret < 0)
448731db9f7cSAlexander Block goto out;
448831db9f7cSAlexander Block }
448931db9f7cSAlexander Block
449031db9f7cSAlexander Block if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
449131db9f7cSAlexander Block /*
449231db9f7cSAlexander Block * Check if we can already rmdir the directory. If not,
449331db9f7cSAlexander Block * orphanize it. For every dir item inside that gets deleted
449431db9f7cSAlexander Block * later, we do this check again and rmdir it then if possible.
449531db9f7cSAlexander Block * See the use of check_dirs for more details.
449631db9f7cSAlexander Block */
449724970ccbSFilipe Manana ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen);
449831db9f7cSAlexander Block if (ret < 0)
449931db9f7cSAlexander Block goto out;
450031db9f7cSAlexander Block if (ret) {
450131db9f7cSAlexander Block ret = send_rmdir(sctx, valid_path);
450231db9f7cSAlexander Block if (ret < 0)
450331db9f7cSAlexander Block goto out;
450431db9f7cSAlexander Block } else if (!is_orphan) {
450531db9f7cSAlexander Block ret = orphanize_inode(sctx, sctx->cur_ino,
450631db9f7cSAlexander Block sctx->cur_inode_gen, valid_path);
450731db9f7cSAlexander Block if (ret < 0)
450831db9f7cSAlexander Block goto out;
450931db9f7cSAlexander Block is_orphan = 1;
451031db9f7cSAlexander Block }
451131db9f7cSAlexander Block
451231db9f7cSAlexander Block list_for_each_entry(cur, &sctx->deleted_refs, list) {
4513ba5e8f2eSJosef Bacik ret = dup_ref(cur, &check_dirs);
451431db9f7cSAlexander Block if (ret < 0)
451531db9f7cSAlexander Block goto out;
451631db9f7cSAlexander Block }
4517ccf1626bSAlexander Block } else if (S_ISDIR(sctx->cur_inode_mode) &&
4518ccf1626bSAlexander Block !list_empty(&sctx->deleted_refs)) {
4519ccf1626bSAlexander Block /*
4520ccf1626bSAlexander Block * We have a moved dir. Add the old parent to check_dirs
4521ccf1626bSAlexander Block */
4522ccf1626bSAlexander Block cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4523ccf1626bSAlexander Block list);
4524ba5e8f2eSJosef Bacik ret = dup_ref(cur, &check_dirs);
4525ccf1626bSAlexander Block if (ret < 0)
4526ccf1626bSAlexander Block goto out;
452731db9f7cSAlexander Block } else if (!S_ISDIR(sctx->cur_inode_mode)) {
452831db9f7cSAlexander Block /*
452931db9f7cSAlexander Block * We have a non dir inode. Go through all deleted refs and
453031db9f7cSAlexander Block * unlink them if they were not already overwritten by other
453131db9f7cSAlexander Block * inodes.
453231db9f7cSAlexander Block */
453331db9f7cSAlexander Block list_for_each_entry(cur, &sctx->deleted_refs, list) {
453431db9f7cSAlexander Block ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
453531db9f7cSAlexander Block sctx->cur_ino, sctx->cur_inode_gen,
453631db9f7cSAlexander Block cur->name, cur->name_len);
453731db9f7cSAlexander Block if (ret < 0)
453831db9f7cSAlexander Block goto out;
453931db9f7cSAlexander Block if (!ret) {
4540fdb13889SFilipe Manana /*
4541fdb13889SFilipe Manana * If we orphanized any ancestor before, we need
4542fdb13889SFilipe Manana * to recompute the full path for deleted names,
4543fdb13889SFilipe Manana * since any such path was computed before we
4544fdb13889SFilipe Manana * processed any references and orphanized any
4545fdb13889SFilipe Manana * ancestor inode.
4546fdb13889SFilipe Manana */
4547fdb13889SFilipe Manana if (orphanized_ancestor) {
4548f5962781SFilipe Manana ret = update_ref_path(sctx, cur);
4549f5962781SFilipe Manana if (ret < 0)
4550fdb13889SFilipe Manana goto out;
4551fdb13889SFilipe Manana }
455231db9f7cSAlexander Block ret = send_unlink(sctx, cur->full_path);
455331db9f7cSAlexander Block if (ret < 0)
455431db9f7cSAlexander Block goto out;
455531db9f7cSAlexander Block }
4556ba5e8f2eSJosef Bacik ret = dup_ref(cur, &check_dirs);
455731db9f7cSAlexander Block if (ret < 0)
455831db9f7cSAlexander Block goto out;
455931db9f7cSAlexander Block }
456031db9f7cSAlexander Block /*
456131db9f7cSAlexander Block * If the inode is still orphan, unlink the orphan. This may
456231db9f7cSAlexander Block * happen when a previous inode did overwrite the first ref
456331db9f7cSAlexander Block * of this inode and no new refs were added for the current
4564766702efSAlexander Block * inode. Unlinking does not mean that the inode is deleted in
4565766702efSAlexander Block * all cases. There may still be links to this inode in other
4566766702efSAlexander Block * places.
456731db9f7cSAlexander Block */
45681f4692daSAlexander Block if (is_orphan) {
456931db9f7cSAlexander Block ret = send_unlink(sctx, valid_path);
457031db9f7cSAlexander Block if (ret < 0)
457131db9f7cSAlexander Block goto out;
457231db9f7cSAlexander Block }
457331db9f7cSAlexander Block }
457431db9f7cSAlexander Block
457531db9f7cSAlexander Block /*
457631db9f7cSAlexander Block * We did collect all parent dirs where cur_inode was once located. We
457731db9f7cSAlexander Block * now go through all these dirs and check if they are pending for
457831db9f7cSAlexander Block * deletion and if it's finally possible to perform the rmdir now.
457931db9f7cSAlexander Block * We also update the inode stats of the parent dirs here.
458031db9f7cSAlexander Block */
4581ba5e8f2eSJosef Bacik list_for_each_entry(cur, &check_dirs, list) {
4582766702efSAlexander Block /*
4583766702efSAlexander Block * In case we had refs into dirs that were not processed yet,
4584766702efSAlexander Block * we don't need to do the utime and rmdir logic for these dirs.
4585766702efSAlexander Block * The dir will be processed later.
4586766702efSAlexander Block */
4587ba5e8f2eSJosef Bacik if (cur->dir > sctx->cur_ino)
458831db9f7cSAlexander Block continue;
458931db9f7cSAlexander Block
4590498581f3SFilipe Manana ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
459131db9f7cSAlexander Block if (ret < 0)
459231db9f7cSAlexander Block goto out;
459331db9f7cSAlexander Block
459431db9f7cSAlexander Block if (ret == inode_state_did_create ||
459531db9f7cSAlexander Block ret == inode_state_no_change) {
45963e49363bSFilipe Manana ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
459731db9f7cSAlexander Block if (ret < 0)
459831db9f7cSAlexander Block goto out;
459929d6d30fSFilipe Manana } else if (ret == inode_state_did_delete &&
460029d6d30fSFilipe Manana cur->dir != last_dir_ino_rm) {
460124970ccbSFilipe Manana ret = can_rmdir(sctx, cur->dir, cur->dir_gen);
460231db9f7cSAlexander Block if (ret < 0)
460331db9f7cSAlexander Block goto out;
460431db9f7cSAlexander Block if (ret) {
4605ba5e8f2eSJosef Bacik ret = get_cur_path(sctx, cur->dir,
4606ba5e8f2eSJosef Bacik cur->dir_gen, valid_path);
460731db9f7cSAlexander Block if (ret < 0)
460831db9f7cSAlexander Block goto out;
460931db9f7cSAlexander Block ret = send_rmdir(sctx, valid_path);
461031db9f7cSAlexander Block if (ret < 0)
461131db9f7cSAlexander Block goto out;
461229d6d30fSFilipe Manana last_dir_ino_rm = cur->dir;
461331db9f7cSAlexander Block }
461431db9f7cSAlexander Block }
461531db9f7cSAlexander Block }
461631db9f7cSAlexander Block
461731db9f7cSAlexander Block ret = 0;
461831db9f7cSAlexander Block
461931db9f7cSAlexander Block out:
4620ba5e8f2eSJosef Bacik __free_recorded_refs(&check_dirs);
462131db9f7cSAlexander Block free_recorded_refs(sctx);
4622924794c9STsutomu Itoh fs_path_free(valid_path);
462331db9f7cSAlexander Block return ret;
462431db9f7cSAlexander Block }
462531db9f7cSAlexander Block
rbtree_ref_comp(const void * k,const struct rb_node * node)46263aa5bd36SBingJing Chang static int rbtree_ref_comp(const void *k, const struct rb_node *node)
46273aa5bd36SBingJing Chang {
46283aa5bd36SBingJing Chang const struct recorded_ref *data = k;
46293aa5bd36SBingJing Chang const struct recorded_ref *ref = rb_entry(node, struct recorded_ref, node);
46303aa5bd36SBingJing Chang int result;
46313aa5bd36SBingJing Chang
46323aa5bd36SBingJing Chang if (data->dir > ref->dir)
46333aa5bd36SBingJing Chang return 1;
46343aa5bd36SBingJing Chang if (data->dir < ref->dir)
46353aa5bd36SBingJing Chang return -1;
46363aa5bd36SBingJing Chang if (data->dir_gen > ref->dir_gen)
46373aa5bd36SBingJing Chang return 1;
46383aa5bd36SBingJing Chang if (data->dir_gen < ref->dir_gen)
46393aa5bd36SBingJing Chang return -1;
46403aa5bd36SBingJing Chang if (data->name_len > ref->name_len)
46413aa5bd36SBingJing Chang return 1;
46423aa5bd36SBingJing Chang if (data->name_len < ref->name_len)
46433aa5bd36SBingJing Chang return -1;
46443aa5bd36SBingJing Chang result = strcmp(data->name, ref->name);
46453aa5bd36SBingJing Chang if (result > 0)
46463aa5bd36SBingJing Chang return 1;
46473aa5bd36SBingJing Chang if (result < 0)
46483aa5bd36SBingJing Chang return -1;
46493aa5bd36SBingJing Chang return 0;
46503aa5bd36SBingJing Chang }
46513aa5bd36SBingJing Chang
rbtree_ref_less(struct rb_node * node,const struct rb_node * parent)46523aa5bd36SBingJing Chang static bool rbtree_ref_less(struct rb_node *node, const struct rb_node *parent)
46533aa5bd36SBingJing Chang {
46543aa5bd36SBingJing Chang const struct recorded_ref *entry = rb_entry(node, struct recorded_ref, node);
46553aa5bd36SBingJing Chang
46563aa5bd36SBingJing Chang return rbtree_ref_comp(entry, parent) < 0;
46573aa5bd36SBingJing Chang }
46583aa5bd36SBingJing Chang
record_ref_in_tree(struct rb_root * root,struct list_head * refs,struct fs_path * name,u64 dir,u64 dir_gen,struct send_ctx * sctx)46593aa5bd36SBingJing Chang static int record_ref_in_tree(struct rb_root *root, struct list_head *refs,
46603aa5bd36SBingJing Chang struct fs_path *name, u64 dir, u64 dir_gen,
46613aa5bd36SBingJing Chang struct send_ctx *sctx)
46623aa5bd36SBingJing Chang {
46633aa5bd36SBingJing Chang int ret = 0;
46643aa5bd36SBingJing Chang struct fs_path *path = NULL;
46653aa5bd36SBingJing Chang struct recorded_ref *ref = NULL;
46663aa5bd36SBingJing Chang
46673aa5bd36SBingJing Chang path = fs_path_alloc();
46683aa5bd36SBingJing Chang if (!path) {
46693aa5bd36SBingJing Chang ret = -ENOMEM;
46703aa5bd36SBingJing Chang goto out;
46713aa5bd36SBingJing Chang }
46723aa5bd36SBingJing Chang
46733aa5bd36SBingJing Chang ref = recorded_ref_alloc();
46743aa5bd36SBingJing Chang if (!ref) {
46753aa5bd36SBingJing Chang ret = -ENOMEM;
46763aa5bd36SBingJing Chang goto out;
46773aa5bd36SBingJing Chang }
46783aa5bd36SBingJing Chang
46793aa5bd36SBingJing Chang ret = get_cur_path(sctx, dir, dir_gen, path);
46803aa5bd36SBingJing Chang if (ret < 0)
46813aa5bd36SBingJing Chang goto out;
46823aa5bd36SBingJing Chang ret = fs_path_add_path(path, name);
46833aa5bd36SBingJing Chang if (ret < 0)
46843aa5bd36SBingJing Chang goto out;
46853aa5bd36SBingJing Chang
46863aa5bd36SBingJing Chang ref->dir = dir;
46873aa5bd36SBingJing Chang ref->dir_gen = dir_gen;
46883aa5bd36SBingJing Chang set_ref_path(ref, path);
46893aa5bd36SBingJing Chang list_add_tail(&ref->list, refs);
46903aa5bd36SBingJing Chang rb_add(&ref->node, root, rbtree_ref_less);
46913aa5bd36SBingJing Chang ref->root = root;
46923aa5bd36SBingJing Chang out:
46933aa5bd36SBingJing Chang if (ret) {
46943aa5bd36SBingJing Chang if (path && (!ref || !ref->full_path))
46953aa5bd36SBingJing Chang fs_path_free(path);
46963aa5bd36SBingJing Chang recorded_ref_free(ref);
46973aa5bd36SBingJing Chang }
46983aa5bd36SBingJing Chang return ret;
46993aa5bd36SBingJing Chang }
47003aa5bd36SBingJing Chang
record_new_ref_if_needed(int num,u64 dir,int index,struct fs_path * name,void * ctx)47013aa5bd36SBingJing Chang static int record_new_ref_if_needed(int num, u64 dir, int index,
47023aa5bd36SBingJing Chang struct fs_path *name, void *ctx)
47033aa5bd36SBingJing Chang {
47043aa5bd36SBingJing Chang int ret = 0;
47053aa5bd36SBingJing Chang struct send_ctx *sctx = ctx;
47063aa5bd36SBingJing Chang struct rb_node *node = NULL;
47073aa5bd36SBingJing Chang struct recorded_ref data;
47083aa5bd36SBingJing Chang struct recorded_ref *ref;
47093aa5bd36SBingJing Chang u64 dir_gen;
47103aa5bd36SBingJing Chang
47117e93f6dcSBingJing Chang ret = get_inode_gen(sctx->send_root, dir, &dir_gen);
47123aa5bd36SBingJing Chang if (ret < 0)
47133aa5bd36SBingJing Chang goto out;
47143aa5bd36SBingJing Chang
47153aa5bd36SBingJing Chang data.dir = dir;
47163aa5bd36SBingJing Chang data.dir_gen = dir_gen;
47173aa5bd36SBingJing Chang set_ref_path(&data, name);
47183aa5bd36SBingJing Chang node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp);
47193aa5bd36SBingJing Chang if (node) {
47203aa5bd36SBingJing Chang ref = rb_entry(node, struct recorded_ref, node);
47213aa5bd36SBingJing Chang recorded_ref_free(ref);
47223aa5bd36SBingJing Chang } else {
47233aa5bd36SBingJing Chang ret = record_ref_in_tree(&sctx->rbtree_new_refs,
47243aa5bd36SBingJing Chang &sctx->new_refs, name, dir, dir_gen,
47253aa5bd36SBingJing Chang sctx);
47263aa5bd36SBingJing Chang }
47273aa5bd36SBingJing Chang out:
47283aa5bd36SBingJing Chang return ret;
47293aa5bd36SBingJing Chang }
47303aa5bd36SBingJing Chang
record_deleted_ref_if_needed(int num,u64 dir,int index,struct fs_path * name,void * ctx)47313aa5bd36SBingJing Chang static int record_deleted_ref_if_needed(int num, u64 dir, int index,
47323aa5bd36SBingJing Chang struct fs_path *name, void *ctx)
47333aa5bd36SBingJing Chang {
47343aa5bd36SBingJing Chang int ret = 0;
47353aa5bd36SBingJing Chang struct send_ctx *sctx = ctx;
47363aa5bd36SBingJing Chang struct rb_node *node = NULL;
47373aa5bd36SBingJing Chang struct recorded_ref data;
47383aa5bd36SBingJing Chang struct recorded_ref *ref;
47393aa5bd36SBingJing Chang u64 dir_gen;
47403aa5bd36SBingJing Chang
47417e93f6dcSBingJing Chang ret = get_inode_gen(sctx->parent_root, dir, &dir_gen);
47423aa5bd36SBingJing Chang if (ret < 0)
47433aa5bd36SBingJing Chang goto out;
47443aa5bd36SBingJing Chang
47453aa5bd36SBingJing Chang data.dir = dir;
47463aa5bd36SBingJing Chang data.dir_gen = dir_gen;
47473aa5bd36SBingJing Chang set_ref_path(&data, name);
47483aa5bd36SBingJing Chang node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp);
47493aa5bd36SBingJing Chang if (node) {
47503aa5bd36SBingJing Chang ref = rb_entry(node, struct recorded_ref, node);
47513aa5bd36SBingJing Chang recorded_ref_free(ref);
47523aa5bd36SBingJing Chang } else {
47533aa5bd36SBingJing Chang ret = record_ref_in_tree(&sctx->rbtree_deleted_refs,
47543aa5bd36SBingJing Chang &sctx->deleted_refs, name, dir,
47553aa5bd36SBingJing Chang dir_gen, sctx);
47563aa5bd36SBingJing Chang }
47573aa5bd36SBingJing Chang out:
47583aa5bd36SBingJing Chang return ret;
47593aa5bd36SBingJing Chang }
47603aa5bd36SBingJing Chang
record_new_ref(struct send_ctx * sctx)476131db9f7cSAlexander Block static int record_new_ref(struct send_ctx *sctx)
476231db9f7cSAlexander Block {
476331db9f7cSAlexander Block int ret;
476431db9f7cSAlexander Block
4765924794c9STsutomu Itoh ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
47663aa5bd36SBingJing Chang sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
476731db9f7cSAlexander Block if (ret < 0)
476831db9f7cSAlexander Block goto out;
476931db9f7cSAlexander Block ret = 0;
477031db9f7cSAlexander Block
477131db9f7cSAlexander Block out:
477231db9f7cSAlexander Block return ret;
477331db9f7cSAlexander Block }
477431db9f7cSAlexander Block
record_deleted_ref(struct send_ctx * sctx)477531db9f7cSAlexander Block static int record_deleted_ref(struct send_ctx *sctx)
477631db9f7cSAlexander Block {
477731db9f7cSAlexander Block int ret;
477831db9f7cSAlexander Block
4779924794c9STsutomu Itoh ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
47803aa5bd36SBingJing Chang sctx->cmp_key, 0, record_deleted_ref_if_needed,
47813aa5bd36SBingJing Chang sctx);
478231db9f7cSAlexander Block if (ret < 0)
478331db9f7cSAlexander Block goto out;
478431db9f7cSAlexander Block ret = 0;
478531db9f7cSAlexander Block
478631db9f7cSAlexander Block out:
478731db9f7cSAlexander Block return ret;
478831db9f7cSAlexander Block }
478931db9f7cSAlexander Block
record_changed_ref(struct send_ctx * sctx)479031db9f7cSAlexander Block static int record_changed_ref(struct send_ctx *sctx)
479131db9f7cSAlexander Block {
479231db9f7cSAlexander Block int ret = 0;
479331db9f7cSAlexander Block
4794924794c9STsutomu Itoh ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
47950d8869fbSFilipe Manana sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
479631db9f7cSAlexander Block if (ret < 0)
479731db9f7cSAlexander Block goto out;
4798924794c9STsutomu Itoh ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
47990d8869fbSFilipe Manana sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx);
480031db9f7cSAlexander Block if (ret < 0)
480131db9f7cSAlexander Block goto out;
480231db9f7cSAlexander Block ret = 0;
480331db9f7cSAlexander Block
480431db9f7cSAlexander Block out:
480531db9f7cSAlexander Block return ret;
480631db9f7cSAlexander Block }
480731db9f7cSAlexander Block
480831db9f7cSAlexander Block /*
480931db9f7cSAlexander Block * Record and process all refs at once. Needed when an inode changes the
481031db9f7cSAlexander Block * generation number, which means that it was deleted and recreated.
481131db9f7cSAlexander Block */
process_all_refs(struct send_ctx * sctx,enum btrfs_compare_tree_result cmd)481231db9f7cSAlexander Block static int process_all_refs(struct send_ctx *sctx,
481331db9f7cSAlexander Block enum btrfs_compare_tree_result cmd)
481431db9f7cSAlexander Block {
4815649b9635SGabriel Niebler int ret = 0;
4816649b9635SGabriel Niebler int iter_ret = 0;
481731db9f7cSAlexander Block struct btrfs_root *root;
481831db9f7cSAlexander Block struct btrfs_path *path;
481931db9f7cSAlexander Block struct btrfs_key key;
482031db9f7cSAlexander Block struct btrfs_key found_key;
482131db9f7cSAlexander Block iterate_inode_ref_t cb;
48229f03740aSFilipe David Borba Manana int pending_move = 0;
482331db9f7cSAlexander Block
482431db9f7cSAlexander Block path = alloc_path_for_send();
482531db9f7cSAlexander Block if (!path)
482631db9f7cSAlexander Block return -ENOMEM;
482731db9f7cSAlexander Block
482831db9f7cSAlexander Block if (cmd == BTRFS_COMPARE_TREE_NEW) {
482931db9f7cSAlexander Block root = sctx->send_root;
48300d8869fbSFilipe Manana cb = record_new_ref_if_needed;
483131db9f7cSAlexander Block } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
483231db9f7cSAlexander Block root = sctx->parent_root;
48330d8869fbSFilipe Manana cb = record_deleted_ref_if_needed;
483431db9f7cSAlexander Block } else {
48354d1a63b2SDavid Sterba btrfs_err(sctx->send_root->fs_info,
48364d1a63b2SDavid Sterba "Wrong command %d in process_all_refs", cmd);
48374d1a63b2SDavid Sterba ret = -EINVAL;
48384d1a63b2SDavid Sterba goto out;
483931db9f7cSAlexander Block }
484031db9f7cSAlexander Block
484131db9f7cSAlexander Block key.objectid = sctx->cmp_key->objectid;
484231db9f7cSAlexander Block key.type = BTRFS_INODE_REF_KEY;
484331db9f7cSAlexander Block key.offset = 0;
4844649b9635SGabriel Niebler btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
484531db9f7cSAlexander Block if (found_key.objectid != key.objectid ||
484696b5bd77SJan Schmidt (found_key.type != BTRFS_INODE_REF_KEY &&
484796b5bd77SJan Schmidt found_key.type != BTRFS_INODE_EXTREF_KEY))
484831db9f7cSAlexander Block break;
484931db9f7cSAlexander Block
4850924794c9STsutomu Itoh ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
485131db9f7cSAlexander Block if (ret < 0)
485231db9f7cSAlexander Block goto out;
4853649b9635SGabriel Niebler }
4854649b9635SGabriel Niebler /* Catch error found during iteration */
4855649b9635SGabriel Niebler if (iter_ret < 0) {
4856649b9635SGabriel Niebler ret = iter_ret;
4857649b9635SGabriel Niebler goto out;
485831db9f7cSAlexander Block }
4859e938c8adSAlexander Block btrfs_release_path(path);
486031db9f7cSAlexander Block
48613dc09ec8SJosef Bacik /*
48623dc09ec8SJosef Bacik * We don't actually care about pending_move as we are simply
48633dc09ec8SJosef Bacik * re-creating this inode and will be rename'ing it into place once we
48643dc09ec8SJosef Bacik * rename the parent directory.
48653dc09ec8SJosef Bacik */
48669f03740aSFilipe David Borba Manana ret = process_recorded_refs(sctx, &pending_move);
486731db9f7cSAlexander Block out:
486831db9f7cSAlexander Block btrfs_free_path(path);
486931db9f7cSAlexander Block return ret;
487031db9f7cSAlexander Block }
487131db9f7cSAlexander Block
send_set_xattr(struct send_ctx * sctx,struct fs_path * path,const char * name,int name_len,const char * data,int data_len)487231db9f7cSAlexander Block static int send_set_xattr(struct send_ctx *sctx,
487331db9f7cSAlexander Block struct fs_path *path,
487431db9f7cSAlexander Block const char *name, int name_len,
487531db9f7cSAlexander Block const char *data, int data_len)
487631db9f7cSAlexander Block {
487731db9f7cSAlexander Block int ret = 0;
487831db9f7cSAlexander Block
487931db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
488031db9f7cSAlexander Block if (ret < 0)
488131db9f7cSAlexander Block goto out;
488231db9f7cSAlexander Block
488331db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
488431db9f7cSAlexander Block TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
488531db9f7cSAlexander Block TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
488631db9f7cSAlexander Block
488731db9f7cSAlexander Block ret = send_cmd(sctx);
488831db9f7cSAlexander Block
488931db9f7cSAlexander Block tlv_put_failure:
489031db9f7cSAlexander Block out:
489131db9f7cSAlexander Block return ret;
489231db9f7cSAlexander Block }
489331db9f7cSAlexander Block
send_remove_xattr(struct send_ctx * sctx,struct fs_path * path,const char * name,int name_len)489431db9f7cSAlexander Block static int send_remove_xattr(struct send_ctx *sctx,
489531db9f7cSAlexander Block struct fs_path *path,
489631db9f7cSAlexander Block const char *name, int name_len)
489731db9f7cSAlexander Block {
489831db9f7cSAlexander Block int ret = 0;
489931db9f7cSAlexander Block
490031db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
490131db9f7cSAlexander Block if (ret < 0)
490231db9f7cSAlexander Block goto out;
490331db9f7cSAlexander Block
490431db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
490531db9f7cSAlexander Block TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
490631db9f7cSAlexander Block
490731db9f7cSAlexander Block ret = send_cmd(sctx);
490831db9f7cSAlexander Block
490931db9f7cSAlexander Block tlv_put_failure:
491031db9f7cSAlexander Block out:
491131db9f7cSAlexander Block return ret;
491231db9f7cSAlexander Block }
491331db9f7cSAlexander Block
__process_new_xattr(int num,struct btrfs_key * di_key,const char * name,int name_len,const char * data,int data_len,void * ctx)491431db9f7cSAlexander Block static int __process_new_xattr(int num, struct btrfs_key *di_key,
4915b1dea4e7SOmar Sandoval const char *name, int name_len, const char *data,
4916b1dea4e7SOmar Sandoval int data_len, void *ctx)
491731db9f7cSAlexander Block {
491831db9f7cSAlexander Block int ret;
491931db9f7cSAlexander Block struct send_ctx *sctx = ctx;
492031db9f7cSAlexander Block struct fs_path *p;
49212211d5baSAndreas Gruenbacher struct posix_acl_xattr_header dummy_acl;
492231db9f7cSAlexander Block
492389efda52SMarcos Paulo de Souza /* Capabilities are emitted by finish_inode_if_needed */
492489efda52SMarcos Paulo de Souza if (!strncmp(name, XATTR_NAME_CAPS, name_len))
492589efda52SMarcos Paulo de Souza return 0;
492689efda52SMarcos Paulo de Souza
4927924794c9STsutomu Itoh p = fs_path_alloc();
492831db9f7cSAlexander Block if (!p)
492931db9f7cSAlexander Block return -ENOMEM;
493031db9f7cSAlexander Block
493131db9f7cSAlexander Block /*
493201327610SNicholas D Steeves * This hack is needed because empty acls are stored as zero byte
493331db9f7cSAlexander Block * data in xattrs. Problem with that is, that receiving these zero byte
493401327610SNicholas D Steeves * acls will fail later. To fix this, we send a dummy acl list that
493531db9f7cSAlexander Block * only contains the version number and no entries.
493631db9f7cSAlexander Block */
493731db9f7cSAlexander Block if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
493831db9f7cSAlexander Block !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
493931db9f7cSAlexander Block if (data_len == 0) {
494031db9f7cSAlexander Block dummy_acl.a_version =
494131db9f7cSAlexander Block cpu_to_le32(POSIX_ACL_XATTR_VERSION);
494231db9f7cSAlexander Block data = (char *)&dummy_acl;
494331db9f7cSAlexander Block data_len = sizeof(dummy_acl);
494431db9f7cSAlexander Block }
494531db9f7cSAlexander Block }
494631db9f7cSAlexander Block
494731db9f7cSAlexander Block ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
494831db9f7cSAlexander Block if (ret < 0)
494931db9f7cSAlexander Block goto out;
495031db9f7cSAlexander Block
495131db9f7cSAlexander Block ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
495231db9f7cSAlexander Block
495331db9f7cSAlexander Block out:
4954924794c9STsutomu Itoh fs_path_free(p);
495531db9f7cSAlexander Block return ret;
495631db9f7cSAlexander Block }
495731db9f7cSAlexander Block
__process_deleted_xattr(int num,struct btrfs_key * di_key,const char * name,int name_len,const char * data,int data_len,void * ctx)495831db9f7cSAlexander Block static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
495931db9f7cSAlexander Block const char *name, int name_len,
4960b1dea4e7SOmar Sandoval const char *data, int data_len, void *ctx)
496131db9f7cSAlexander Block {
496231db9f7cSAlexander Block int ret;
496331db9f7cSAlexander Block struct send_ctx *sctx = ctx;
496431db9f7cSAlexander Block struct fs_path *p;
496531db9f7cSAlexander Block
4966924794c9STsutomu Itoh p = fs_path_alloc();
496731db9f7cSAlexander Block if (!p)
496831db9f7cSAlexander Block return -ENOMEM;
496931db9f7cSAlexander Block
497031db9f7cSAlexander Block ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
497131db9f7cSAlexander Block if (ret < 0)
497231db9f7cSAlexander Block goto out;
497331db9f7cSAlexander Block
497431db9f7cSAlexander Block ret = send_remove_xattr(sctx, p, name, name_len);
497531db9f7cSAlexander Block
497631db9f7cSAlexander Block out:
4977924794c9STsutomu Itoh fs_path_free(p);
497831db9f7cSAlexander Block return ret;
497931db9f7cSAlexander Block }
498031db9f7cSAlexander Block
process_new_xattr(struct send_ctx * sctx)498131db9f7cSAlexander Block static int process_new_xattr(struct send_ctx *sctx)
498231db9f7cSAlexander Block {
498331db9f7cSAlexander Block int ret = 0;
498431db9f7cSAlexander Block
4985924794c9STsutomu Itoh ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4986a0357511SNikolay Borisov __process_new_xattr, sctx);
498731db9f7cSAlexander Block
498831db9f7cSAlexander Block return ret;
498931db9f7cSAlexander Block }
499031db9f7cSAlexander Block
process_deleted_xattr(struct send_ctx * sctx)499131db9f7cSAlexander Block static int process_deleted_xattr(struct send_ctx *sctx)
499231db9f7cSAlexander Block {
4993e2c89907SMasahiro Yamada return iterate_dir_item(sctx->parent_root, sctx->right_path,
4994a0357511SNikolay Borisov __process_deleted_xattr, sctx);
499531db9f7cSAlexander Block }
499631db9f7cSAlexander Block
499731db9f7cSAlexander Block struct find_xattr_ctx {
499831db9f7cSAlexander Block const char *name;
499931db9f7cSAlexander Block int name_len;
500031db9f7cSAlexander Block int found_idx;
500131db9f7cSAlexander Block char *found_data;
500231db9f7cSAlexander Block int found_data_len;
500331db9f7cSAlexander Block };
500431db9f7cSAlexander Block
__find_xattr(int num,struct btrfs_key * di_key,const char * name,int name_len,const char * data,int data_len,void * vctx)5005b1dea4e7SOmar Sandoval static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
5006b1dea4e7SOmar Sandoval int name_len, const char *data, int data_len, void *vctx)
500731db9f7cSAlexander Block {
500831db9f7cSAlexander Block struct find_xattr_ctx *ctx = vctx;
500931db9f7cSAlexander Block
501031db9f7cSAlexander Block if (name_len == ctx->name_len &&
501131db9f7cSAlexander Block strncmp(name, ctx->name, name_len) == 0) {
501231db9f7cSAlexander Block ctx->found_idx = num;
501331db9f7cSAlexander Block ctx->found_data_len = data_len;
5014e780b0d1SDavid Sterba ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
501531db9f7cSAlexander Block if (!ctx->found_data)
501631db9f7cSAlexander Block return -ENOMEM;
501731db9f7cSAlexander Block return 1;
501831db9f7cSAlexander Block }
501931db9f7cSAlexander Block return 0;
502031db9f7cSAlexander Block }
502131db9f7cSAlexander Block
find_xattr(struct btrfs_root * root,struct btrfs_path * path,struct btrfs_key * key,const char * name,int name_len,char ** data,int * data_len)5022924794c9STsutomu Itoh static int find_xattr(struct btrfs_root *root,
502331db9f7cSAlexander Block struct btrfs_path *path,
502431db9f7cSAlexander Block struct btrfs_key *key,
502531db9f7cSAlexander Block const char *name, int name_len,
502631db9f7cSAlexander Block char **data, int *data_len)
502731db9f7cSAlexander Block {
502831db9f7cSAlexander Block int ret;
502931db9f7cSAlexander Block struct find_xattr_ctx ctx;
503031db9f7cSAlexander Block
503131db9f7cSAlexander Block ctx.name = name;
503231db9f7cSAlexander Block ctx.name_len = name_len;
503331db9f7cSAlexander Block ctx.found_idx = -1;
503431db9f7cSAlexander Block ctx.found_data = NULL;
503531db9f7cSAlexander Block ctx.found_data_len = 0;
503631db9f7cSAlexander Block
5037a0357511SNikolay Borisov ret = iterate_dir_item(root, path, __find_xattr, &ctx);
503831db9f7cSAlexander Block if (ret < 0)
503931db9f7cSAlexander Block return ret;
504031db9f7cSAlexander Block
504131db9f7cSAlexander Block if (ctx.found_idx == -1)
504231db9f7cSAlexander Block return -ENOENT;
504331db9f7cSAlexander Block if (data) {
504431db9f7cSAlexander Block *data = ctx.found_data;
504531db9f7cSAlexander Block *data_len = ctx.found_data_len;
504631db9f7cSAlexander Block } else {
504731db9f7cSAlexander Block kfree(ctx.found_data);
504831db9f7cSAlexander Block }
504931db9f7cSAlexander Block return ctx.found_idx;
505031db9f7cSAlexander Block }
505131db9f7cSAlexander Block
505231db9f7cSAlexander Block
__process_changed_new_xattr(int num,struct btrfs_key * di_key,const char * name,int name_len,const char * data,int data_len,void * ctx)505331db9f7cSAlexander Block static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
505431db9f7cSAlexander Block const char *name, int name_len,
505531db9f7cSAlexander Block const char *data, int data_len,
5056b1dea4e7SOmar Sandoval void *ctx)
505731db9f7cSAlexander Block {
505831db9f7cSAlexander Block int ret;
505931db9f7cSAlexander Block struct send_ctx *sctx = ctx;
506031db9f7cSAlexander Block char *found_data = NULL;
506131db9f7cSAlexander Block int found_data_len = 0;
506231db9f7cSAlexander Block
5063924794c9STsutomu Itoh ret = find_xattr(sctx->parent_root, sctx->right_path,
506431db9f7cSAlexander Block sctx->cmp_key, name, name_len, &found_data,
506531db9f7cSAlexander Block &found_data_len);
506631db9f7cSAlexander Block if (ret == -ENOENT) {
506731db9f7cSAlexander Block ret = __process_new_xattr(num, di_key, name, name_len, data,
5068b1dea4e7SOmar Sandoval data_len, ctx);
506931db9f7cSAlexander Block } else if (ret >= 0) {
507031db9f7cSAlexander Block if (data_len != found_data_len ||
507131db9f7cSAlexander Block memcmp(data, found_data, data_len)) {
507231db9f7cSAlexander Block ret = __process_new_xattr(num, di_key, name, name_len,
5073b1dea4e7SOmar Sandoval data, data_len, ctx);
507431db9f7cSAlexander Block } else {
507531db9f7cSAlexander Block ret = 0;
507631db9f7cSAlexander Block }
507731db9f7cSAlexander Block }
507831db9f7cSAlexander Block
507931db9f7cSAlexander Block kfree(found_data);
508031db9f7cSAlexander Block return ret;
508131db9f7cSAlexander Block }
508231db9f7cSAlexander Block
__process_changed_deleted_xattr(int num,struct btrfs_key * di_key,const char * name,int name_len,const char * data,int data_len,void * ctx)508331db9f7cSAlexander Block static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
508431db9f7cSAlexander Block const char *name, int name_len,
508531db9f7cSAlexander Block const char *data, int data_len,
5086b1dea4e7SOmar Sandoval void *ctx)
508731db9f7cSAlexander Block {
508831db9f7cSAlexander Block int ret;
508931db9f7cSAlexander Block struct send_ctx *sctx = ctx;
509031db9f7cSAlexander Block
5091924794c9STsutomu Itoh ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
509231db9f7cSAlexander Block name, name_len, NULL, NULL);
509331db9f7cSAlexander Block if (ret == -ENOENT)
509431db9f7cSAlexander Block ret = __process_deleted_xattr(num, di_key, name, name_len, data,
5095b1dea4e7SOmar Sandoval data_len, ctx);
509631db9f7cSAlexander Block else if (ret >= 0)
509731db9f7cSAlexander Block ret = 0;
509831db9f7cSAlexander Block
509931db9f7cSAlexander Block return ret;
510031db9f7cSAlexander Block }
510131db9f7cSAlexander Block
process_changed_xattr(struct send_ctx * sctx)510231db9f7cSAlexander Block static int process_changed_xattr(struct send_ctx *sctx)
510331db9f7cSAlexander Block {
510431db9f7cSAlexander Block int ret = 0;
510531db9f7cSAlexander Block
5106924794c9STsutomu Itoh ret = iterate_dir_item(sctx->send_root, sctx->left_path,
5107a0357511SNikolay Borisov __process_changed_new_xattr, sctx);
510831db9f7cSAlexander Block if (ret < 0)
510931db9f7cSAlexander Block goto out;
5110924794c9STsutomu Itoh ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
5111a0357511SNikolay Borisov __process_changed_deleted_xattr, sctx);
511231db9f7cSAlexander Block
511331db9f7cSAlexander Block out:
511431db9f7cSAlexander Block return ret;
511531db9f7cSAlexander Block }
511631db9f7cSAlexander Block
process_all_new_xattrs(struct send_ctx * sctx)511731db9f7cSAlexander Block static int process_all_new_xattrs(struct send_ctx *sctx)
511831db9f7cSAlexander Block {
511969e43177SGabriel Niebler int ret = 0;
512069e43177SGabriel Niebler int iter_ret = 0;
512131db9f7cSAlexander Block struct btrfs_root *root;
512231db9f7cSAlexander Block struct btrfs_path *path;
512331db9f7cSAlexander Block struct btrfs_key key;
512431db9f7cSAlexander Block struct btrfs_key found_key;
512531db9f7cSAlexander Block
512631db9f7cSAlexander Block path = alloc_path_for_send();
512731db9f7cSAlexander Block if (!path)
512831db9f7cSAlexander Block return -ENOMEM;
512931db9f7cSAlexander Block
513031db9f7cSAlexander Block root = sctx->send_root;
513131db9f7cSAlexander Block
513231db9f7cSAlexander Block key.objectid = sctx->cmp_key->objectid;
513331db9f7cSAlexander Block key.type = BTRFS_XATTR_ITEM_KEY;
513431db9f7cSAlexander Block key.offset = 0;
513569e43177SGabriel Niebler btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
513631db9f7cSAlexander Block if (found_key.objectid != key.objectid ||
513731db9f7cSAlexander Block found_key.type != key.type) {
513831db9f7cSAlexander Block ret = 0;
513969e43177SGabriel Niebler break;
514031db9f7cSAlexander Block }
514131db9f7cSAlexander Block
5142a0357511SNikolay Borisov ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
514331db9f7cSAlexander Block if (ret < 0)
514469e43177SGabriel Niebler break;
514531db9f7cSAlexander Block }
514669e43177SGabriel Niebler /* Catch error found during iteration */
514769e43177SGabriel Niebler if (iter_ret < 0)
514869e43177SGabriel Niebler ret = iter_ret;
514931db9f7cSAlexander Block
515031db9f7cSAlexander Block btrfs_free_path(path);
515131db9f7cSAlexander Block return ret;
515231db9f7cSAlexander Block }
515331db9f7cSAlexander Block
send_verity(struct send_ctx * sctx,struct fs_path * path,struct fsverity_descriptor * desc)515438622010SBoris Burkov static int send_verity(struct send_ctx *sctx, struct fs_path *path,
515538622010SBoris Burkov struct fsverity_descriptor *desc)
515638622010SBoris Burkov {
515738622010SBoris Burkov int ret;
515838622010SBoris Burkov
515938622010SBoris Burkov ret = begin_cmd(sctx, BTRFS_SEND_C_ENABLE_VERITY);
516038622010SBoris Burkov if (ret < 0)
516138622010SBoris Burkov goto out;
516238622010SBoris Burkov
516338622010SBoris Burkov TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
516438622010SBoris Burkov TLV_PUT_U8(sctx, BTRFS_SEND_A_VERITY_ALGORITHM,
516538622010SBoris Burkov le8_to_cpu(desc->hash_algorithm));
516638622010SBoris Burkov TLV_PUT_U32(sctx, BTRFS_SEND_A_VERITY_BLOCK_SIZE,
516738622010SBoris Burkov 1U << le8_to_cpu(desc->log_blocksize));
516838622010SBoris Burkov TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SALT_DATA, desc->salt,
516938622010SBoris Burkov le8_to_cpu(desc->salt_size));
517038622010SBoris Burkov TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SIG_DATA, desc->signature,
517138622010SBoris Burkov le32_to_cpu(desc->sig_size));
517238622010SBoris Burkov
517338622010SBoris Burkov ret = send_cmd(sctx);
517438622010SBoris Burkov
517538622010SBoris Burkov tlv_put_failure:
517638622010SBoris Burkov out:
517738622010SBoris Burkov return ret;
517838622010SBoris Burkov }
517938622010SBoris Burkov
process_verity(struct send_ctx * sctx)518038622010SBoris Burkov static int process_verity(struct send_ctx *sctx)
518138622010SBoris Burkov {
518238622010SBoris Burkov int ret = 0;
518338622010SBoris Burkov struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
518438622010SBoris Burkov struct inode *inode;
518538622010SBoris Burkov struct fs_path *p;
518638622010SBoris Burkov
518738622010SBoris Burkov inode = btrfs_iget(fs_info->sb, sctx->cur_ino, sctx->send_root);
518838622010SBoris Burkov if (IS_ERR(inode))
518938622010SBoris Burkov return PTR_ERR(inode);
519038622010SBoris Burkov
519138622010SBoris Burkov ret = btrfs_get_verity_descriptor(inode, NULL, 0);
519238622010SBoris Burkov if (ret < 0)
519338622010SBoris Burkov goto iput;
519438622010SBoris Burkov
519538622010SBoris Burkov if (ret > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
519638622010SBoris Burkov ret = -EMSGSIZE;
519738622010SBoris Burkov goto iput;
519838622010SBoris Burkov }
519938622010SBoris Burkov if (!sctx->verity_descriptor) {
520038622010SBoris Burkov sctx->verity_descriptor = kvmalloc(FS_VERITY_MAX_DESCRIPTOR_SIZE,
520138622010SBoris Burkov GFP_KERNEL);
520238622010SBoris Burkov if (!sctx->verity_descriptor) {
520338622010SBoris Burkov ret = -ENOMEM;
520438622010SBoris Burkov goto iput;
520538622010SBoris Burkov }
520638622010SBoris Burkov }
520738622010SBoris Burkov
520838622010SBoris Burkov ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret);
520938622010SBoris Burkov if (ret < 0)
521038622010SBoris Burkov goto iput;
521138622010SBoris Burkov
521238622010SBoris Burkov p = fs_path_alloc();
521338622010SBoris Burkov if (!p) {
521438622010SBoris Burkov ret = -ENOMEM;
521538622010SBoris Burkov goto iput;
521638622010SBoris Burkov }
521738622010SBoris Burkov ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
521838622010SBoris Burkov if (ret < 0)
521938622010SBoris Burkov goto free_path;
522038622010SBoris Burkov
522138622010SBoris Burkov ret = send_verity(sctx, p, sctx->verity_descriptor);
522238622010SBoris Burkov if (ret < 0)
522338622010SBoris Burkov goto free_path;
522438622010SBoris Burkov
522538622010SBoris Burkov free_path:
522638622010SBoris Burkov fs_path_free(p);
522738622010SBoris Burkov iput:
522838622010SBoris Burkov iput(inode);
522938622010SBoris Burkov return ret;
523038622010SBoris Burkov }
523138622010SBoris Burkov
max_send_read_size(const struct send_ctx * sctx)52328c7d9fe0SOmar Sandoval static inline u64 max_send_read_size(const struct send_ctx *sctx)
52338c7d9fe0SOmar Sandoval {
52348c7d9fe0SOmar Sandoval return sctx->send_max_size - SZ_16K;
52358c7d9fe0SOmar Sandoval }
52368c7d9fe0SOmar Sandoval
put_data_header(struct send_ctx * sctx,u32 len)52378c7d9fe0SOmar Sandoval static int put_data_header(struct send_ctx *sctx, u32 len)
52388c7d9fe0SOmar Sandoval {
5239356bbbb6SOmar Sandoval if (WARN_ON_ONCE(sctx->put_data))
5240356bbbb6SOmar Sandoval return -EINVAL;
5241356bbbb6SOmar Sandoval sctx->put_data = true;
5242356bbbb6SOmar Sandoval if (sctx->proto >= 2) {
5243356bbbb6SOmar Sandoval /*
5244356bbbb6SOmar Sandoval * Since v2, the data attribute header doesn't include a length,
5245356bbbb6SOmar Sandoval * it is implicitly to the end of the command.
5246356bbbb6SOmar Sandoval */
5247356bbbb6SOmar Sandoval if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len)
5248356bbbb6SOmar Sandoval return -EOVERFLOW;
5249356bbbb6SOmar Sandoval put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size);
5250356bbbb6SOmar Sandoval sctx->send_size += sizeof(__le16);
5251356bbbb6SOmar Sandoval } else {
52528c7d9fe0SOmar Sandoval struct btrfs_tlv_header *hdr;
52538c7d9fe0SOmar Sandoval
52548c7d9fe0SOmar Sandoval if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
52558c7d9fe0SOmar Sandoval return -EOVERFLOW;
52568c7d9fe0SOmar Sandoval hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
52578c7d9fe0SOmar Sandoval put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
52588c7d9fe0SOmar Sandoval put_unaligned_le16(len, &hdr->tlv_len);
52598c7d9fe0SOmar Sandoval sctx->send_size += sizeof(*hdr);
5260356bbbb6SOmar Sandoval }
52618c7d9fe0SOmar Sandoval return 0;
52628c7d9fe0SOmar Sandoval }
52638c7d9fe0SOmar Sandoval
put_file_data(struct send_ctx * sctx,u64 offset,u32 len)52648c7d9fe0SOmar Sandoval static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
5265ed259095SJosef Bacik {
5266ed259095SJosef Bacik struct btrfs_root *root = sctx->send_root;
5267ed259095SJosef Bacik struct btrfs_fs_info *fs_info = root->fs_info;
5268ed259095SJosef Bacik struct page *page;
526909cbfeafSKirill A. Shutemov pgoff_t index = offset >> PAGE_SHIFT;
5270ed259095SJosef Bacik pgoff_t last_index;
52717073017aSJohannes Thumshirn unsigned pg_offset = offset_in_page(offset);
52728c7d9fe0SOmar Sandoval int ret;
52738c7d9fe0SOmar Sandoval
52748c7d9fe0SOmar Sandoval ret = put_data_header(sctx, len);
52758c7d9fe0SOmar Sandoval if (ret)
52768c7d9fe0SOmar Sandoval return ret;
5277ed259095SJosef Bacik
527809cbfeafSKirill A. Shutemov last_index = (offset + len - 1) >> PAGE_SHIFT;
52792131bcd3SLiu Bo
5280ed259095SJosef Bacik while (index <= last_index) {
5281ed259095SJosef Bacik unsigned cur_len = min_t(unsigned, len,
528209cbfeafSKirill A. Shutemov PAGE_SIZE - pg_offset);
5283eef16ba2SKuanling Huang
5284521b6803SFilipe Manana page = find_lock_page(sctx->cur_inode->i_mapping, index);
5285eef16ba2SKuanling Huang if (!page) {
5286521b6803SFilipe Manana page_cache_sync_readahead(sctx->cur_inode->i_mapping,
5287521b6803SFilipe Manana &sctx->ra, NULL, index,
5288521b6803SFilipe Manana last_index + 1 - index);
5289eef16ba2SKuanling Huang
5290521b6803SFilipe Manana page = find_or_create_page(sctx->cur_inode->i_mapping,
5291521b6803SFilipe Manana index, GFP_KERNEL);
5292ed259095SJosef Bacik if (!page) {
5293ed259095SJosef Bacik ret = -ENOMEM;
5294ed259095SJosef Bacik break;
5295ed259095SJosef Bacik }
5296eef16ba2SKuanling Huang }
5297eef16ba2SKuanling Huang
5298521b6803SFilipe Manana if (PageReadahead(page))
5299521b6803SFilipe Manana page_cache_async_readahead(sctx->cur_inode->i_mapping,
5300fdaf9a58SLinus Torvalds &sctx->ra, NULL, page_folio(page),
5301fdaf9a58SLinus Torvalds index, last_index + 1 - index);
5302ed259095SJosef Bacik
5303ed259095SJosef Bacik if (!PageUptodate(page)) {
5304fb12489bSMatthew Wilcox (Oracle) btrfs_read_folio(NULL, page_folio(page));
5305ed259095SJosef Bacik lock_page(page);
5306ed259095SJosef Bacik if (!PageUptodate(page)) {
5307ed259095SJosef Bacik unlock_page(page);
53082e7be9dbSDāvis Mosāns btrfs_err(fs_info,
53092e7be9dbSDāvis Mosāns "send: IO error at offset %llu for inode %llu root %llu",
53102e7be9dbSDāvis Mosāns page_offset(page), sctx->cur_ino,
53112e7be9dbSDāvis Mosāns sctx->send_root->root_key.objectid);
531209cbfeafSKirill A. Shutemov put_page(page);
5313ed259095SJosef Bacik ret = -EIO;
5314ed259095SJosef Bacik break;
5315ed259095SJosef Bacik }
5316ed259095SJosef Bacik }
5317ed259095SJosef Bacik
53183590ec58SIra Weiny memcpy_from_page(sctx->send_buf + sctx->send_size, page,
53193590ec58SIra Weiny pg_offset, cur_len);
5320ed259095SJosef Bacik unlock_page(page);
532109cbfeafSKirill A. Shutemov put_page(page);
5322ed259095SJosef Bacik index++;
5323ed259095SJosef Bacik pg_offset = 0;
5324ed259095SJosef Bacik len -= cur_len;
53258c7d9fe0SOmar Sandoval sctx->send_size += cur_len;
5326ed259095SJosef Bacik }
5327521b6803SFilipe Manana
5328ed259095SJosef Bacik return ret;
5329ed259095SJosef Bacik }
5330ed259095SJosef Bacik
533131db9f7cSAlexander Block /*
533231db9f7cSAlexander Block * Read some bytes from the current inode/file and send a write command to
533331db9f7cSAlexander Block * user space.
533431db9f7cSAlexander Block */
send_write(struct send_ctx * sctx,u64 offset,u32 len)533531db9f7cSAlexander Block static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
533631db9f7cSAlexander Block {
533704ab956eSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
533831db9f7cSAlexander Block int ret = 0;
533931db9f7cSAlexander Block struct fs_path *p;
534031db9f7cSAlexander Block
5341924794c9STsutomu Itoh p = fs_path_alloc();
534231db9f7cSAlexander Block if (!p)
534331db9f7cSAlexander Block return -ENOMEM;
534431db9f7cSAlexander Block
534504ab956eSJeff Mahoney btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
534631db9f7cSAlexander Block
534731db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
534831db9f7cSAlexander Block if (ret < 0)
534931db9f7cSAlexander Block goto out;
535031db9f7cSAlexander Block
535131db9f7cSAlexander Block ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
535231db9f7cSAlexander Block if (ret < 0)
535331db9f7cSAlexander Block goto out;
535431db9f7cSAlexander Block
535531db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
535631db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
53578c7d9fe0SOmar Sandoval ret = put_file_data(sctx, offset, len);
53588c7d9fe0SOmar Sandoval if (ret < 0)
53598c7d9fe0SOmar Sandoval goto out;
536031db9f7cSAlexander Block
536131db9f7cSAlexander Block ret = send_cmd(sctx);
536231db9f7cSAlexander Block
536331db9f7cSAlexander Block tlv_put_failure:
536431db9f7cSAlexander Block out:
5365924794c9STsutomu Itoh fs_path_free(p);
536631db9f7cSAlexander Block return ret;
536731db9f7cSAlexander Block }
536831db9f7cSAlexander Block
536931db9f7cSAlexander Block /*
537031db9f7cSAlexander Block * Send a clone command to user space.
537131db9f7cSAlexander Block */
send_clone(struct send_ctx * sctx,u64 offset,u32 len,struct clone_root * clone_root)537231db9f7cSAlexander Block static int send_clone(struct send_ctx *sctx,
537331db9f7cSAlexander Block u64 offset, u32 len,
537431db9f7cSAlexander Block struct clone_root *clone_root)
537531db9f7cSAlexander Block {
537631db9f7cSAlexander Block int ret = 0;
537731db9f7cSAlexander Block struct fs_path *p;
537831db9f7cSAlexander Block u64 gen;
537931db9f7cSAlexander Block
538004ab956eSJeff Mahoney btrfs_debug(sctx->send_root->fs_info,
538104ab956eSJeff Mahoney "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
53824fd786e6SMisono Tomohiro offset, len, clone_root->root->root_key.objectid,
53834fd786e6SMisono Tomohiro clone_root->ino, clone_root->offset);
538431db9f7cSAlexander Block
5385924794c9STsutomu Itoh p = fs_path_alloc();
538631db9f7cSAlexander Block if (!p)
538731db9f7cSAlexander Block return -ENOMEM;
538831db9f7cSAlexander Block
538931db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
539031db9f7cSAlexander Block if (ret < 0)
539131db9f7cSAlexander Block goto out;
539231db9f7cSAlexander Block
539331db9f7cSAlexander Block ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
539431db9f7cSAlexander Block if (ret < 0)
539531db9f7cSAlexander Block goto out;
539631db9f7cSAlexander Block
539731db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
539831db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
539931db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
540031db9f7cSAlexander Block
5401e938c8adSAlexander Block if (clone_root->root == sctx->send_root) {
54027e93f6dcSBingJing Chang ret = get_inode_gen(sctx->send_root, clone_root->ino, &gen);
540331db9f7cSAlexander Block if (ret < 0)
540431db9f7cSAlexander Block goto out;
540531db9f7cSAlexander Block ret = get_cur_path(sctx, clone_root->ino, gen, p);
540631db9f7cSAlexander Block } else {
5407924794c9STsutomu Itoh ret = get_inode_path(clone_root->root, clone_root->ino, p);
540831db9f7cSAlexander Block }
540931db9f7cSAlexander Block if (ret < 0)
541031db9f7cSAlexander Block goto out;
541131db9f7cSAlexander Block
541237b8d27dSJosef Bacik /*
541337b8d27dSJosef Bacik * If the parent we're using has a received_uuid set then use that as
541437b8d27dSJosef Bacik * our clone source as that is what we will look for when doing a
541537b8d27dSJosef Bacik * receive.
541637b8d27dSJosef Bacik *
541737b8d27dSJosef Bacik * This covers the case that we create a snapshot off of a received
541837b8d27dSJosef Bacik * subvolume and then use that as the parent and try to receive on a
541937b8d27dSJosef Bacik * different host.
542037b8d27dSJosef Bacik */
542137b8d27dSJosef Bacik if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
542237b8d27dSJosef Bacik TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
542337b8d27dSJosef Bacik clone_root->root->root_item.received_uuid);
542437b8d27dSJosef Bacik else
542531db9f7cSAlexander Block TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5426e938c8adSAlexander Block clone_root->root->root_item.uuid);
542731db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
542809e3a288SDavid Sterba btrfs_root_ctransid(&clone_root->root->root_item));
542931db9f7cSAlexander Block TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
543031db9f7cSAlexander Block TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
543131db9f7cSAlexander Block clone_root->offset);
543231db9f7cSAlexander Block
543331db9f7cSAlexander Block ret = send_cmd(sctx);
543431db9f7cSAlexander Block
543531db9f7cSAlexander Block tlv_put_failure:
543631db9f7cSAlexander Block out:
5437924794c9STsutomu Itoh fs_path_free(p);
543831db9f7cSAlexander Block return ret;
543931db9f7cSAlexander Block }
544031db9f7cSAlexander Block
5441cb95e7bfSMark Fasheh /*
5442cb95e7bfSMark Fasheh * Send an update extent command to user space.
5443cb95e7bfSMark Fasheh */
send_update_extent(struct send_ctx * sctx,u64 offset,u32 len)5444cb95e7bfSMark Fasheh static int send_update_extent(struct send_ctx *sctx,
5445cb95e7bfSMark Fasheh u64 offset, u32 len)
5446cb95e7bfSMark Fasheh {
5447cb95e7bfSMark Fasheh int ret = 0;
5448cb95e7bfSMark Fasheh struct fs_path *p;
5449cb95e7bfSMark Fasheh
5450924794c9STsutomu Itoh p = fs_path_alloc();
5451cb95e7bfSMark Fasheh if (!p)
5452cb95e7bfSMark Fasheh return -ENOMEM;
5453cb95e7bfSMark Fasheh
5454cb95e7bfSMark Fasheh ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5455cb95e7bfSMark Fasheh if (ret < 0)
5456cb95e7bfSMark Fasheh goto out;
5457cb95e7bfSMark Fasheh
5458cb95e7bfSMark Fasheh ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5459cb95e7bfSMark Fasheh if (ret < 0)
5460cb95e7bfSMark Fasheh goto out;
5461cb95e7bfSMark Fasheh
5462cb95e7bfSMark Fasheh TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5463cb95e7bfSMark Fasheh TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5464cb95e7bfSMark Fasheh TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5465cb95e7bfSMark Fasheh
5466cb95e7bfSMark Fasheh ret = send_cmd(sctx);
5467cb95e7bfSMark Fasheh
5468cb95e7bfSMark Fasheh tlv_put_failure:
5469cb95e7bfSMark Fasheh out:
5470924794c9STsutomu Itoh fs_path_free(p);
5471cb95e7bfSMark Fasheh return ret;
5472cb95e7bfSMark Fasheh }
5473cb95e7bfSMark Fasheh
send_hole(struct send_ctx * sctx,u64 end)547416e7549fSJosef Bacik static int send_hole(struct send_ctx *sctx, u64 end)
547516e7549fSJosef Bacik {
547616e7549fSJosef Bacik struct fs_path *p = NULL;
54778c7d9fe0SOmar Sandoval u64 read_size = max_send_read_size(sctx);
547816e7549fSJosef Bacik u64 offset = sctx->cur_inode_last_extent;
547916e7549fSJosef Bacik int ret = 0;
548016e7549fSJosef Bacik
548122d3151cSFilipe Manana /*
548222d3151cSFilipe Manana * A hole that starts at EOF or beyond it. Since we do not yet support
548322d3151cSFilipe Manana * fallocate (for extent preallocation and hole punching), sending a
548422d3151cSFilipe Manana * write of zeroes starting at EOF or beyond would later require issuing
548522d3151cSFilipe Manana * a truncate operation which would undo the write and achieve nothing.
548622d3151cSFilipe Manana */
548722d3151cSFilipe Manana if (offset >= sctx->cur_inode_size)
548822d3151cSFilipe Manana return 0;
548922d3151cSFilipe Manana
54906b1f72e5SFilipe Manana /*
54916b1f72e5SFilipe Manana * Don't go beyond the inode's i_size due to prealloc extents that start
54926b1f72e5SFilipe Manana * after the i_size.
54936b1f72e5SFilipe Manana */
54946b1f72e5SFilipe Manana end = min_t(u64, end, sctx->cur_inode_size);
54956b1f72e5SFilipe Manana
5496d4dfc0f4SFilipe Manana if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5497d4dfc0f4SFilipe Manana return send_update_extent(sctx, offset, end - offset);
5498d4dfc0f4SFilipe Manana
549916e7549fSJosef Bacik p = fs_path_alloc();
550016e7549fSJosef Bacik if (!p)
550116e7549fSJosef Bacik return -ENOMEM;
5502c715e155SFilipe Manana ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5503c715e155SFilipe Manana if (ret < 0)
5504c715e155SFilipe Manana goto tlv_put_failure;
550516e7549fSJosef Bacik while (offset < end) {
55068c7d9fe0SOmar Sandoval u64 len = min(end - offset, read_size);
550716e7549fSJosef Bacik
550816e7549fSJosef Bacik ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
550916e7549fSJosef Bacik if (ret < 0)
551016e7549fSJosef Bacik break;
551116e7549fSJosef Bacik TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
551216e7549fSJosef Bacik TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
55138c7d9fe0SOmar Sandoval ret = put_data_header(sctx, len);
55148c7d9fe0SOmar Sandoval if (ret < 0)
55158c7d9fe0SOmar Sandoval break;
55168c7d9fe0SOmar Sandoval memset(sctx->send_buf + sctx->send_size, 0, len);
55178c7d9fe0SOmar Sandoval sctx->send_size += len;
551816e7549fSJosef Bacik ret = send_cmd(sctx);
551916e7549fSJosef Bacik if (ret < 0)
552016e7549fSJosef Bacik break;
552116e7549fSJosef Bacik offset += len;
552216e7549fSJosef Bacik }
5523ffa7c429SFilipe Manana sctx->cur_inode_next_write_offset = offset;
552416e7549fSJosef Bacik tlv_put_failure:
552516e7549fSJosef Bacik fs_path_free(p);
552616e7549fSJosef Bacik return ret;
552716e7549fSJosef Bacik }
552816e7549fSJosef Bacik
send_encoded_inline_extent(struct send_ctx * sctx,struct btrfs_path * path,u64 offset,u64 len)55293ea4dc5bSOmar Sandoval static int send_encoded_inline_extent(struct send_ctx *sctx,
55303ea4dc5bSOmar Sandoval struct btrfs_path *path, u64 offset,
55313ea4dc5bSOmar Sandoval u64 len)
55323ea4dc5bSOmar Sandoval {
55333ea4dc5bSOmar Sandoval struct btrfs_root *root = sctx->send_root;
55343ea4dc5bSOmar Sandoval struct btrfs_fs_info *fs_info = root->fs_info;
55353ea4dc5bSOmar Sandoval struct inode *inode;
55363ea4dc5bSOmar Sandoval struct fs_path *fspath;
55373ea4dc5bSOmar Sandoval struct extent_buffer *leaf = path->nodes[0];
55383ea4dc5bSOmar Sandoval struct btrfs_key key;
55393ea4dc5bSOmar Sandoval struct btrfs_file_extent_item *ei;
55403ea4dc5bSOmar Sandoval u64 ram_bytes;
55413ea4dc5bSOmar Sandoval size_t inline_size;
55423ea4dc5bSOmar Sandoval int ret;
55433ea4dc5bSOmar Sandoval
55443ea4dc5bSOmar Sandoval inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
55453ea4dc5bSOmar Sandoval if (IS_ERR(inode))
55463ea4dc5bSOmar Sandoval return PTR_ERR(inode);
55473ea4dc5bSOmar Sandoval
55483ea4dc5bSOmar Sandoval fspath = fs_path_alloc();
55493ea4dc5bSOmar Sandoval if (!fspath) {
55503ea4dc5bSOmar Sandoval ret = -ENOMEM;
55513ea4dc5bSOmar Sandoval goto out;
55523ea4dc5bSOmar Sandoval }
55533ea4dc5bSOmar Sandoval
55543ea4dc5bSOmar Sandoval ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
55553ea4dc5bSOmar Sandoval if (ret < 0)
55563ea4dc5bSOmar Sandoval goto out;
55573ea4dc5bSOmar Sandoval
55583ea4dc5bSOmar Sandoval ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
55593ea4dc5bSOmar Sandoval if (ret < 0)
55603ea4dc5bSOmar Sandoval goto out;
55613ea4dc5bSOmar Sandoval
55623ea4dc5bSOmar Sandoval btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
55633ea4dc5bSOmar Sandoval ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
55643ea4dc5bSOmar Sandoval ram_bytes = btrfs_file_extent_ram_bytes(leaf, ei);
55653ea4dc5bSOmar Sandoval inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
55663ea4dc5bSOmar Sandoval
55673ea4dc5bSOmar Sandoval TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
55683ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
55693ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
55703ea4dc5bSOmar Sandoval min(key.offset + ram_bytes - offset, len));
55713ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes);
55723ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset);
55733ea4dc5bSOmar Sandoval ret = btrfs_encoded_io_compression_from_extent(fs_info,
55743ea4dc5bSOmar Sandoval btrfs_file_extent_compression(leaf, ei));
55753ea4dc5bSOmar Sandoval if (ret < 0)
55763ea4dc5bSOmar Sandoval goto out;
55773ea4dc5bSOmar Sandoval TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
55783ea4dc5bSOmar Sandoval
55793ea4dc5bSOmar Sandoval ret = put_data_header(sctx, inline_size);
55803ea4dc5bSOmar Sandoval if (ret < 0)
55813ea4dc5bSOmar Sandoval goto out;
55823ea4dc5bSOmar Sandoval read_extent_buffer(leaf, sctx->send_buf + sctx->send_size,
55833ea4dc5bSOmar Sandoval btrfs_file_extent_inline_start(ei), inline_size);
55843ea4dc5bSOmar Sandoval sctx->send_size += inline_size;
55853ea4dc5bSOmar Sandoval
55863ea4dc5bSOmar Sandoval ret = send_cmd(sctx);
55873ea4dc5bSOmar Sandoval
55883ea4dc5bSOmar Sandoval tlv_put_failure:
55893ea4dc5bSOmar Sandoval out:
55903ea4dc5bSOmar Sandoval fs_path_free(fspath);
55913ea4dc5bSOmar Sandoval iput(inode);
55923ea4dc5bSOmar Sandoval return ret;
55933ea4dc5bSOmar Sandoval }
55943ea4dc5bSOmar Sandoval
send_encoded_extent(struct send_ctx * sctx,struct btrfs_path * path,u64 offset,u64 len)55953ea4dc5bSOmar Sandoval static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
55963ea4dc5bSOmar Sandoval u64 offset, u64 len)
55973ea4dc5bSOmar Sandoval {
55983ea4dc5bSOmar Sandoval struct btrfs_root *root = sctx->send_root;
55993ea4dc5bSOmar Sandoval struct btrfs_fs_info *fs_info = root->fs_info;
56003ea4dc5bSOmar Sandoval struct inode *inode;
56013ea4dc5bSOmar Sandoval struct fs_path *fspath;
56023ea4dc5bSOmar Sandoval struct extent_buffer *leaf = path->nodes[0];
56033ea4dc5bSOmar Sandoval struct btrfs_key key;
56043ea4dc5bSOmar Sandoval struct btrfs_file_extent_item *ei;
56053ea4dc5bSOmar Sandoval u64 disk_bytenr, disk_num_bytes;
56063ea4dc5bSOmar Sandoval u32 data_offset;
56073ea4dc5bSOmar Sandoval struct btrfs_cmd_header *hdr;
56083ea4dc5bSOmar Sandoval u32 crc;
56093ea4dc5bSOmar Sandoval int ret;
56103ea4dc5bSOmar Sandoval
56113ea4dc5bSOmar Sandoval inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
56123ea4dc5bSOmar Sandoval if (IS_ERR(inode))
56133ea4dc5bSOmar Sandoval return PTR_ERR(inode);
56143ea4dc5bSOmar Sandoval
56153ea4dc5bSOmar Sandoval fspath = fs_path_alloc();
56163ea4dc5bSOmar Sandoval if (!fspath) {
56173ea4dc5bSOmar Sandoval ret = -ENOMEM;
56183ea4dc5bSOmar Sandoval goto out;
56193ea4dc5bSOmar Sandoval }
56203ea4dc5bSOmar Sandoval
56213ea4dc5bSOmar Sandoval ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
56223ea4dc5bSOmar Sandoval if (ret < 0)
56233ea4dc5bSOmar Sandoval goto out;
56243ea4dc5bSOmar Sandoval
56253ea4dc5bSOmar Sandoval ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
56263ea4dc5bSOmar Sandoval if (ret < 0)
56273ea4dc5bSOmar Sandoval goto out;
56283ea4dc5bSOmar Sandoval
56293ea4dc5bSOmar Sandoval btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
56303ea4dc5bSOmar Sandoval ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
56313ea4dc5bSOmar Sandoval disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
56323ea4dc5bSOmar Sandoval disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, ei);
56333ea4dc5bSOmar Sandoval
56343ea4dc5bSOmar Sandoval TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
56353ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
56363ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
56373ea4dc5bSOmar Sandoval min(key.offset + btrfs_file_extent_num_bytes(leaf, ei) - offset,
56383ea4dc5bSOmar Sandoval len));
56393ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN,
56403ea4dc5bSOmar Sandoval btrfs_file_extent_ram_bytes(leaf, ei));
56413ea4dc5bSOmar Sandoval TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET,
56423ea4dc5bSOmar Sandoval offset - key.offset + btrfs_file_extent_offset(leaf, ei));
56433ea4dc5bSOmar Sandoval ret = btrfs_encoded_io_compression_from_extent(fs_info,
56443ea4dc5bSOmar Sandoval btrfs_file_extent_compression(leaf, ei));
56453ea4dc5bSOmar Sandoval if (ret < 0)
56463ea4dc5bSOmar Sandoval goto out;
56473ea4dc5bSOmar Sandoval TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
56483ea4dc5bSOmar Sandoval TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0);
56493ea4dc5bSOmar Sandoval
56503ea4dc5bSOmar Sandoval ret = put_data_header(sctx, disk_num_bytes);
56513ea4dc5bSOmar Sandoval if (ret < 0)
56523ea4dc5bSOmar Sandoval goto out;
56533ea4dc5bSOmar Sandoval
56543ea4dc5bSOmar Sandoval /*
56553ea4dc5bSOmar Sandoval * We want to do I/O directly into the send buffer, so get the next page
56563ea4dc5bSOmar Sandoval * boundary in the send buffer. This means that there may be a gap
56573ea4dc5bSOmar Sandoval * between the beginning of the command and the file data.
56583ea4dc5bSOmar Sandoval */
5659ce394a7fSYushan Zhou data_offset = PAGE_ALIGN(sctx->send_size);
56603ea4dc5bSOmar Sandoval if (data_offset > sctx->send_max_size ||
56613ea4dc5bSOmar Sandoval sctx->send_max_size - data_offset < disk_num_bytes) {
56623ea4dc5bSOmar Sandoval ret = -EOVERFLOW;
56633ea4dc5bSOmar Sandoval goto out;
56643ea4dc5bSOmar Sandoval }
56653ea4dc5bSOmar Sandoval
56663ea4dc5bSOmar Sandoval /*
56673ea4dc5bSOmar Sandoval * Note that send_buf is a mapping of send_buf_pages, so this is really
56683ea4dc5bSOmar Sandoval * reading into send_buf.
56693ea4dc5bSOmar Sandoval */
56703ea4dc5bSOmar Sandoval ret = btrfs_encoded_read_regular_fill_pages(BTRFS_I(inode), offset,
56713ea4dc5bSOmar Sandoval disk_bytenr, disk_num_bytes,
56723ea4dc5bSOmar Sandoval sctx->send_buf_pages +
56733ea4dc5bSOmar Sandoval (data_offset >> PAGE_SHIFT));
56743ea4dc5bSOmar Sandoval if (ret)
56753ea4dc5bSOmar Sandoval goto out;
56763ea4dc5bSOmar Sandoval
56773ea4dc5bSOmar Sandoval hdr = (struct btrfs_cmd_header *)sctx->send_buf;
56783ea4dc5bSOmar Sandoval hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
56793ea4dc5bSOmar Sandoval hdr->crc = 0;
56803ea4dc5bSOmar Sandoval crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size);
56813ea4dc5bSOmar Sandoval crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
56823ea4dc5bSOmar Sandoval hdr->crc = cpu_to_le32(crc);
56833ea4dc5bSOmar Sandoval
56843ea4dc5bSOmar Sandoval ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
56853ea4dc5bSOmar Sandoval &sctx->send_off);
56863ea4dc5bSOmar Sandoval if (!ret) {
56873ea4dc5bSOmar Sandoval ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset,
56883ea4dc5bSOmar Sandoval disk_num_bytes, &sctx->send_off);
56893ea4dc5bSOmar Sandoval }
56903ea4dc5bSOmar Sandoval sctx->send_size = 0;
56913ea4dc5bSOmar Sandoval sctx->put_data = false;
56923ea4dc5bSOmar Sandoval
56933ea4dc5bSOmar Sandoval tlv_put_failure:
56943ea4dc5bSOmar Sandoval out:
56953ea4dc5bSOmar Sandoval fs_path_free(fspath);
56963ea4dc5bSOmar Sandoval iput(inode);
56973ea4dc5bSOmar Sandoval return ret;
56983ea4dc5bSOmar Sandoval }
56993ea4dc5bSOmar Sandoval
send_extent_data(struct send_ctx * sctx,struct btrfs_path * path,const u64 offset,const u64 len)57003ea4dc5bSOmar Sandoval static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
57013ea4dc5bSOmar Sandoval const u64 offset, const u64 len)
5702d906d49fSFilipe Manana {
5703152555b3SFilipe Manana const u64 end = offset + len;
57043ea4dc5bSOmar Sandoval struct extent_buffer *leaf = path->nodes[0];
57053ea4dc5bSOmar Sandoval struct btrfs_file_extent_item *ei;
57068c7d9fe0SOmar Sandoval u64 read_size = max_send_read_size(sctx);
5707d906d49fSFilipe Manana u64 sent = 0;
5708d906d49fSFilipe Manana
5709d906d49fSFilipe Manana if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5710d906d49fSFilipe Manana return send_update_extent(sctx, offset, len);
5711d906d49fSFilipe Manana
57123ea4dc5bSOmar Sandoval ei = btrfs_item_ptr(leaf, path->slots[0],
57133ea4dc5bSOmar Sandoval struct btrfs_file_extent_item);
57143ea4dc5bSOmar Sandoval if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) &&
57153ea4dc5bSOmar Sandoval btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) {
57163ea4dc5bSOmar Sandoval bool is_inline = (btrfs_file_extent_type(leaf, ei) ==
57173ea4dc5bSOmar Sandoval BTRFS_FILE_EXTENT_INLINE);
57183ea4dc5bSOmar Sandoval
57193ea4dc5bSOmar Sandoval /*
57203ea4dc5bSOmar Sandoval * Send the compressed extent unless the compressed data is
57213ea4dc5bSOmar Sandoval * larger than the decompressed data. This can happen if we're
57223ea4dc5bSOmar Sandoval * not sending the entire extent, either because it has been
57233ea4dc5bSOmar Sandoval * partially overwritten/truncated or because this is a part of
57243ea4dc5bSOmar Sandoval * the extent that we couldn't clone in clone_range().
57253ea4dc5bSOmar Sandoval */
57263ea4dc5bSOmar Sandoval if (is_inline &&
57273ea4dc5bSOmar Sandoval btrfs_file_extent_inline_item_len(leaf,
57283ea4dc5bSOmar Sandoval path->slots[0]) <= len) {
57293ea4dc5bSOmar Sandoval return send_encoded_inline_extent(sctx, path, offset,
57303ea4dc5bSOmar Sandoval len);
57313ea4dc5bSOmar Sandoval } else if (!is_inline &&
57323ea4dc5bSOmar Sandoval btrfs_file_extent_disk_num_bytes(leaf, ei) <= len) {
57333ea4dc5bSOmar Sandoval return send_encoded_extent(sctx, path, offset, len);
57343ea4dc5bSOmar Sandoval }
57353ea4dc5bSOmar Sandoval }
57363ea4dc5bSOmar Sandoval
5737521b6803SFilipe Manana if (sctx->cur_inode == NULL) {
5738521b6803SFilipe Manana struct btrfs_root *root = sctx->send_root;
5739521b6803SFilipe Manana
5740521b6803SFilipe Manana sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root);
5741521b6803SFilipe Manana if (IS_ERR(sctx->cur_inode)) {
5742521b6803SFilipe Manana int err = PTR_ERR(sctx->cur_inode);
5743521b6803SFilipe Manana
5744521b6803SFilipe Manana sctx->cur_inode = NULL;
5745521b6803SFilipe Manana return err;
5746521b6803SFilipe Manana }
5747521b6803SFilipe Manana memset(&sctx->ra, 0, sizeof(struct file_ra_state));
5748521b6803SFilipe Manana file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
5749152555b3SFilipe Manana
5750152555b3SFilipe Manana /*
5751152555b3SFilipe Manana * It's very likely there are no pages from this inode in the page
5752152555b3SFilipe Manana * cache, so after reading extents and sending their data, we clean
5753152555b3SFilipe Manana * the page cache to avoid trashing the page cache (adding pressure
5754152555b3SFilipe Manana * to the page cache and forcing eviction of other data more useful
5755152555b3SFilipe Manana * for applications).
5756152555b3SFilipe Manana *
5757152555b3SFilipe Manana * We decide if we should clean the page cache simply by checking
5758152555b3SFilipe Manana * if the inode's mapping nrpages is 0 when we first open it, and
5759152555b3SFilipe Manana * not by using something like filemap_range_has_page() before
5760152555b3SFilipe Manana * reading an extent because when we ask the readahead code to
5761152555b3SFilipe Manana * read a given file range, it may (and almost always does) read
5762152555b3SFilipe Manana * pages from beyond that range (see the documentation for
5763152555b3SFilipe Manana * page_cache_sync_readahead()), so it would not be reliable,
5764152555b3SFilipe Manana * because after reading the first extent future calls to
5765152555b3SFilipe Manana * filemap_range_has_page() would return true because the readahead
5766152555b3SFilipe Manana * on the previous extent resulted in reading pages of the current
5767152555b3SFilipe Manana * extent as well.
5768152555b3SFilipe Manana */
5769152555b3SFilipe Manana sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
5770152555b3SFilipe Manana sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
5771521b6803SFilipe Manana }
5772521b6803SFilipe Manana
5773d906d49fSFilipe Manana while (sent < len) {
57748c7d9fe0SOmar Sandoval u64 size = min(len - sent, read_size);
5775d906d49fSFilipe Manana int ret;
5776d906d49fSFilipe Manana
5777d906d49fSFilipe Manana ret = send_write(sctx, offset + sent, size);
5778d906d49fSFilipe Manana if (ret < 0)
5779d906d49fSFilipe Manana return ret;
5780a9b2e0deSOmar Sandoval sent += size;
5781d906d49fSFilipe Manana }
5782152555b3SFilipe Manana
5783ce394a7fSYushan Zhou if (sctx->clean_page_cache && PAGE_ALIGNED(end)) {
5784152555b3SFilipe Manana /*
5785152555b3SFilipe Manana * Always operate only on ranges that are a multiple of the page
5786152555b3SFilipe Manana * size. This is not only to prevent zeroing parts of a page in
5787152555b3SFilipe Manana * the case of subpage sector size, but also to guarantee we evict
5788152555b3SFilipe Manana * pages, as passing a range that is smaller than page size does
5789152555b3SFilipe Manana * not evict the respective page (only zeroes part of its content).
5790152555b3SFilipe Manana *
5791152555b3SFilipe Manana * Always start from the end offset of the last range cleared.
5792152555b3SFilipe Manana * This is because the readahead code may (and very often does)
5793152555b3SFilipe Manana * reads pages beyond the range we request for readahead. So if
5794152555b3SFilipe Manana * we have an extent layout like this:
5795152555b3SFilipe Manana *
5796152555b3SFilipe Manana * [ extent A ] [ extent B ] [ extent C ]
5797152555b3SFilipe Manana *
5798152555b3SFilipe Manana * When we ask page_cache_sync_readahead() to read extent A, it
5799152555b3SFilipe Manana * may also trigger reads for pages of extent B. If we are doing
5800152555b3SFilipe Manana * an incremental send and extent B has not changed between the
5801152555b3SFilipe Manana * parent and send snapshots, some or all of its pages may end
5802152555b3SFilipe Manana * up being read and placed in the page cache. So when truncating
5803152555b3SFilipe Manana * the page cache we always start from the end offset of the
5804152555b3SFilipe Manana * previously processed extent up to the end of the current
5805152555b3SFilipe Manana * extent.
5806152555b3SFilipe Manana */
5807152555b3SFilipe Manana truncate_inode_pages_range(&sctx->cur_inode->i_data,
5808152555b3SFilipe Manana sctx->page_cache_clear_start,
5809152555b3SFilipe Manana end - 1);
5810152555b3SFilipe Manana sctx->page_cache_clear_start = end;
5811152555b3SFilipe Manana }
5812152555b3SFilipe Manana
5813d906d49fSFilipe Manana return 0;
5814d906d49fSFilipe Manana }
5815d906d49fSFilipe Manana
581689efda52SMarcos Paulo de Souza /*
581789efda52SMarcos Paulo de Souza * Search for a capability xattr related to sctx->cur_ino. If the capability is
581889efda52SMarcos Paulo de Souza * found, call send_set_xattr function to emit it.
581989efda52SMarcos Paulo de Souza *
582089efda52SMarcos Paulo de Souza * Return 0 if there isn't a capability, or when the capability was emitted
582189efda52SMarcos Paulo de Souza * successfully, or < 0 if an error occurred.
582289efda52SMarcos Paulo de Souza */
send_capabilities(struct send_ctx * sctx)582389efda52SMarcos Paulo de Souza static int send_capabilities(struct send_ctx *sctx)
582489efda52SMarcos Paulo de Souza {
582589efda52SMarcos Paulo de Souza struct fs_path *fspath = NULL;
582689efda52SMarcos Paulo de Souza struct btrfs_path *path;
582789efda52SMarcos Paulo de Souza struct btrfs_dir_item *di;
582889efda52SMarcos Paulo de Souza struct extent_buffer *leaf;
582989efda52SMarcos Paulo de Souza unsigned long data_ptr;
583089efda52SMarcos Paulo de Souza char *buf = NULL;
583189efda52SMarcos Paulo de Souza int buf_len;
583289efda52SMarcos Paulo de Souza int ret = 0;
583389efda52SMarcos Paulo de Souza
583489efda52SMarcos Paulo de Souza path = alloc_path_for_send();
583589efda52SMarcos Paulo de Souza if (!path)
583689efda52SMarcos Paulo de Souza return -ENOMEM;
583789efda52SMarcos Paulo de Souza
583889efda52SMarcos Paulo de Souza di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
583989efda52SMarcos Paulo de Souza XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
584089efda52SMarcos Paulo de Souza if (!di) {
584189efda52SMarcos Paulo de Souza /* There is no xattr for this inode */
584289efda52SMarcos Paulo de Souza goto out;
584389efda52SMarcos Paulo de Souza } else if (IS_ERR(di)) {
584489efda52SMarcos Paulo de Souza ret = PTR_ERR(di);
584589efda52SMarcos Paulo de Souza goto out;
584689efda52SMarcos Paulo de Souza }
584789efda52SMarcos Paulo de Souza
584889efda52SMarcos Paulo de Souza leaf = path->nodes[0];
584989efda52SMarcos Paulo de Souza buf_len = btrfs_dir_data_len(leaf, di);
585089efda52SMarcos Paulo de Souza
585189efda52SMarcos Paulo de Souza fspath = fs_path_alloc();
585289efda52SMarcos Paulo de Souza buf = kmalloc(buf_len, GFP_KERNEL);
585389efda52SMarcos Paulo de Souza if (!fspath || !buf) {
585489efda52SMarcos Paulo de Souza ret = -ENOMEM;
585589efda52SMarcos Paulo de Souza goto out;
585689efda52SMarcos Paulo de Souza }
585789efda52SMarcos Paulo de Souza
585889efda52SMarcos Paulo de Souza ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
585989efda52SMarcos Paulo de Souza if (ret < 0)
586089efda52SMarcos Paulo de Souza goto out;
586189efda52SMarcos Paulo de Souza
586289efda52SMarcos Paulo de Souza data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
586389efda52SMarcos Paulo de Souza read_extent_buffer(leaf, buf, data_ptr, buf_len);
586489efda52SMarcos Paulo de Souza
586589efda52SMarcos Paulo de Souza ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
586689efda52SMarcos Paulo de Souza strlen(XATTR_NAME_CAPS), buf, buf_len);
586789efda52SMarcos Paulo de Souza out:
586889efda52SMarcos Paulo de Souza kfree(buf);
586989efda52SMarcos Paulo de Souza fs_path_free(fspath);
587089efda52SMarcos Paulo de Souza btrfs_free_path(path);
587189efda52SMarcos Paulo de Souza return ret;
587289efda52SMarcos Paulo de Souza }
587389efda52SMarcos Paulo de Souza
clone_range(struct send_ctx * sctx,struct btrfs_path * dst_path,struct clone_root * clone_root,const u64 disk_byte,u64 data_offset,u64 offset,u64 len)58743ea4dc5bSOmar Sandoval static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
58753ea4dc5bSOmar Sandoval struct clone_root *clone_root, const u64 disk_byte,
58763ea4dc5bSOmar Sandoval u64 data_offset, u64 offset, u64 len)
5877d906d49fSFilipe Manana {
5878d906d49fSFilipe Manana struct btrfs_path *path;
5879d906d49fSFilipe Manana struct btrfs_key key;
5880d906d49fSFilipe Manana int ret;
58817e93f6dcSBingJing Chang struct btrfs_inode_info info;
5882431d3988SAustin Kim u64 clone_src_i_size = 0;
5883d906d49fSFilipe Manana
588472610b1bSFilipe Manana /*
588572610b1bSFilipe Manana * Prevent cloning from a zero offset with a length matching the sector
588672610b1bSFilipe Manana * size because in some scenarios this will make the receiver fail.
588772610b1bSFilipe Manana *
588872610b1bSFilipe Manana * For example, if in the source filesystem the extent at offset 0
588972610b1bSFilipe Manana * has a length of sectorsize and it was written using direct IO, then
589072610b1bSFilipe Manana * it can never be an inline extent (even if compression is enabled).
589172610b1bSFilipe Manana * Then this extent can be cloned in the original filesystem to a non
589272610b1bSFilipe Manana * zero file offset, but it may not be possible to clone in the
589372610b1bSFilipe Manana * destination filesystem because it can be inlined due to compression
589472610b1bSFilipe Manana * on the destination filesystem (as the receiver's write operations are
589572610b1bSFilipe Manana * always done using buffered IO). The same happens when the original
589672610b1bSFilipe Manana * filesystem does not have compression enabled but the destination
589772610b1bSFilipe Manana * filesystem has.
589872610b1bSFilipe Manana */
589972610b1bSFilipe Manana if (clone_root->offset == 0 &&
590072610b1bSFilipe Manana len == sctx->send_root->fs_info->sectorsize)
59013ea4dc5bSOmar Sandoval return send_extent_data(sctx, dst_path, offset, len);
590272610b1bSFilipe Manana
5903d906d49fSFilipe Manana path = alloc_path_for_send();
5904d906d49fSFilipe Manana if (!path)
5905d906d49fSFilipe Manana return -ENOMEM;
5906d906d49fSFilipe Manana
5907d906d49fSFilipe Manana /*
5908040ee612SRobbie Ko * There are inodes that have extents that lie behind its i_size. Don't
5909040ee612SRobbie Ko * accept clones from these extents.
5910040ee612SRobbie Ko */
59117e93f6dcSBingJing Chang ret = get_inode_info(clone_root->root, clone_root->ino, &info);
5912040ee612SRobbie Ko btrfs_release_path(path);
5913040ee612SRobbie Ko if (ret < 0)
5914040ee612SRobbie Ko goto out;
59157e93f6dcSBingJing Chang clone_src_i_size = info.size;
5916040ee612SRobbie Ko
5917040ee612SRobbie Ko /*
5918d906d49fSFilipe Manana * We can't send a clone operation for the entire range if we find
5919d906d49fSFilipe Manana * extent items in the respective range in the source file that
5920d906d49fSFilipe Manana * refer to different extents or if we find holes.
5921d906d49fSFilipe Manana * So check for that and do a mix of clone and regular write/copy
5922d906d49fSFilipe Manana * operations if needed.
5923d906d49fSFilipe Manana *
5924d906d49fSFilipe Manana * Example:
5925d906d49fSFilipe Manana *
5926d906d49fSFilipe Manana * mkfs.btrfs -f /dev/sda
5927d906d49fSFilipe Manana * mount /dev/sda /mnt
5928d906d49fSFilipe Manana * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5929d906d49fSFilipe Manana * cp --reflink=always /mnt/foo /mnt/bar
5930d906d49fSFilipe Manana * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5931d906d49fSFilipe Manana * btrfs subvolume snapshot -r /mnt /mnt/snap
5932d906d49fSFilipe Manana *
5933d906d49fSFilipe Manana * If when we send the snapshot and we are processing file bar (which
5934d906d49fSFilipe Manana * has a higher inode number than foo) we blindly send a clone operation
5935d906d49fSFilipe Manana * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5936d906d49fSFilipe Manana * a file bar that matches the content of file foo - iow, doesn't match
5937d906d49fSFilipe Manana * the content from bar in the original filesystem.
5938d906d49fSFilipe Manana */
5939d906d49fSFilipe Manana key.objectid = clone_root->ino;
5940d906d49fSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY;
5941d906d49fSFilipe Manana key.offset = clone_root->offset;
5942d906d49fSFilipe Manana ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5943d906d49fSFilipe Manana if (ret < 0)
5944d906d49fSFilipe Manana goto out;
5945d906d49fSFilipe Manana if (ret > 0 && path->slots[0] > 0) {
5946d906d49fSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5947d906d49fSFilipe Manana if (key.objectid == clone_root->ino &&
5948d906d49fSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY)
5949d906d49fSFilipe Manana path->slots[0]--;
5950d906d49fSFilipe Manana }
5951d906d49fSFilipe Manana
5952d906d49fSFilipe Manana while (true) {
5953d906d49fSFilipe Manana struct extent_buffer *leaf = path->nodes[0];
5954d906d49fSFilipe Manana int slot = path->slots[0];
5955d906d49fSFilipe Manana struct btrfs_file_extent_item *ei;
5956d906d49fSFilipe Manana u8 type;
5957d906d49fSFilipe Manana u64 ext_len;
5958d906d49fSFilipe Manana u64 clone_len;
5959040ee612SRobbie Ko u64 clone_data_offset;
5960a11452a3SFilipe Manana bool crossed_src_i_size = false;
5961d906d49fSFilipe Manana
5962d906d49fSFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
5963d906d49fSFilipe Manana ret = btrfs_next_leaf(clone_root->root, path);
5964d906d49fSFilipe Manana if (ret < 0)
5965d906d49fSFilipe Manana goto out;
5966d906d49fSFilipe Manana else if (ret > 0)
5967d906d49fSFilipe Manana break;
5968d906d49fSFilipe Manana continue;
5969d906d49fSFilipe Manana }
5970d906d49fSFilipe Manana
5971d906d49fSFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
5972d906d49fSFilipe Manana
5973d906d49fSFilipe Manana /*
5974d906d49fSFilipe Manana * We might have an implicit trailing hole (NO_HOLES feature
5975d906d49fSFilipe Manana * enabled). We deal with it after leaving this loop.
5976d906d49fSFilipe Manana */
5977d906d49fSFilipe Manana if (key.objectid != clone_root->ino ||
5978d906d49fSFilipe Manana key.type != BTRFS_EXTENT_DATA_KEY)
5979d906d49fSFilipe Manana break;
5980d906d49fSFilipe Manana
5981d906d49fSFilipe Manana ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5982d906d49fSFilipe Manana type = btrfs_file_extent_type(leaf, ei);
5983d906d49fSFilipe Manana if (type == BTRFS_FILE_EXTENT_INLINE) {
5984e41ca589SQu Wenruo ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
598509cbfeafSKirill A. Shutemov ext_len = PAGE_ALIGN(ext_len);
5986d906d49fSFilipe Manana } else {
5987d906d49fSFilipe Manana ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5988d906d49fSFilipe Manana }
5989d906d49fSFilipe Manana
5990d906d49fSFilipe Manana if (key.offset + ext_len <= clone_root->offset)
5991d906d49fSFilipe Manana goto next;
5992d906d49fSFilipe Manana
5993d906d49fSFilipe Manana if (key.offset > clone_root->offset) {
5994d906d49fSFilipe Manana /* Implicit hole, NO_HOLES feature enabled. */
5995d906d49fSFilipe Manana u64 hole_len = key.offset - clone_root->offset;
5996d906d49fSFilipe Manana
5997d906d49fSFilipe Manana if (hole_len > len)
5998d906d49fSFilipe Manana hole_len = len;
59993ea4dc5bSOmar Sandoval ret = send_extent_data(sctx, dst_path, offset,
60003ea4dc5bSOmar Sandoval hole_len);
6001d906d49fSFilipe Manana if (ret < 0)
6002d906d49fSFilipe Manana goto out;
6003d906d49fSFilipe Manana
6004d906d49fSFilipe Manana len -= hole_len;
6005d906d49fSFilipe Manana if (len == 0)
6006d906d49fSFilipe Manana break;
6007d906d49fSFilipe Manana offset += hole_len;
6008d906d49fSFilipe Manana clone_root->offset += hole_len;
6009d906d49fSFilipe Manana data_offset += hole_len;
6010d906d49fSFilipe Manana }
6011d906d49fSFilipe Manana
6012d906d49fSFilipe Manana if (key.offset >= clone_root->offset + len)
6013d906d49fSFilipe Manana break;
6014d906d49fSFilipe Manana
6015040ee612SRobbie Ko if (key.offset >= clone_src_i_size)
6016040ee612SRobbie Ko break;
6017040ee612SRobbie Ko
6018a11452a3SFilipe Manana if (key.offset + ext_len > clone_src_i_size) {
6019040ee612SRobbie Ko ext_len = clone_src_i_size - key.offset;
6020a11452a3SFilipe Manana crossed_src_i_size = true;
6021a11452a3SFilipe Manana }
6022040ee612SRobbie Ko
6023040ee612SRobbie Ko clone_data_offset = btrfs_file_extent_offset(leaf, ei);
6024040ee612SRobbie Ko if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
6025040ee612SRobbie Ko clone_root->offset = key.offset;
6026040ee612SRobbie Ko if (clone_data_offset < data_offset &&
6027040ee612SRobbie Ko clone_data_offset + ext_len > data_offset) {
6028040ee612SRobbie Ko u64 extent_offset;
6029040ee612SRobbie Ko
6030040ee612SRobbie Ko extent_offset = data_offset - clone_data_offset;
6031040ee612SRobbie Ko ext_len -= extent_offset;
6032040ee612SRobbie Ko clone_data_offset += extent_offset;
6033040ee612SRobbie Ko clone_root->offset += extent_offset;
6034040ee612SRobbie Ko }
6035040ee612SRobbie Ko }
6036040ee612SRobbie Ko
6037d906d49fSFilipe Manana clone_len = min_t(u64, ext_len, len);
6038d906d49fSFilipe Manana
6039d906d49fSFilipe Manana if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
60403c850b45SFilipe Manana clone_data_offset == data_offset) {
60413c850b45SFilipe Manana const u64 src_end = clone_root->offset + clone_len;
60423c850b45SFilipe Manana const u64 sectorsize = SZ_64K;
60433c850b45SFilipe Manana
60443c850b45SFilipe Manana /*
60453c850b45SFilipe Manana * We can't clone the last block, when its size is not
60463c850b45SFilipe Manana * sector size aligned, into the middle of a file. If we
60473c850b45SFilipe Manana * do so, the receiver will get a failure (-EINVAL) when
60483c850b45SFilipe Manana * trying to clone or will silently corrupt the data in
60493c850b45SFilipe Manana * the destination file if it's on a kernel without the
60503c850b45SFilipe Manana * fix introduced by commit ac765f83f1397646
60513c850b45SFilipe Manana * ("Btrfs: fix data corruption due to cloning of eof
60523c850b45SFilipe Manana * block).
60533c850b45SFilipe Manana *
60543c850b45SFilipe Manana * So issue a clone of the aligned down range plus a
60553c850b45SFilipe Manana * regular write for the eof block, if we hit that case.
60563c850b45SFilipe Manana *
60573c850b45SFilipe Manana * Also, we use the maximum possible sector size, 64K,
60583c850b45SFilipe Manana * because we don't know what's the sector size of the
60593c850b45SFilipe Manana * filesystem that receives the stream, so we have to
60603c850b45SFilipe Manana * assume the largest possible sector size.
60613c850b45SFilipe Manana */
60623c850b45SFilipe Manana if (src_end == clone_src_i_size &&
60633c850b45SFilipe Manana !IS_ALIGNED(src_end, sectorsize) &&
60643c850b45SFilipe Manana offset + clone_len < sctx->cur_inode_size) {
60653c850b45SFilipe Manana u64 slen;
60663c850b45SFilipe Manana
60673c850b45SFilipe Manana slen = ALIGN_DOWN(src_end - clone_root->offset,
60683c850b45SFilipe Manana sectorsize);
60693c850b45SFilipe Manana if (slen > 0) {
60703c850b45SFilipe Manana ret = send_clone(sctx, offset, slen,
60713c850b45SFilipe Manana clone_root);
60723c850b45SFilipe Manana if (ret < 0)
60733c850b45SFilipe Manana goto out;
60743c850b45SFilipe Manana }
60753ea4dc5bSOmar Sandoval ret = send_extent_data(sctx, dst_path,
60763ea4dc5bSOmar Sandoval offset + slen,
60773c850b45SFilipe Manana clone_len - slen);
60783c850b45SFilipe Manana } else {
60793c850b45SFilipe Manana ret = send_clone(sctx, offset, clone_len,
60803c850b45SFilipe Manana clone_root);
60813c850b45SFilipe Manana }
6082a11452a3SFilipe Manana } else if (crossed_src_i_size && clone_len < len) {
6083a11452a3SFilipe Manana /*
6084a11452a3SFilipe Manana * If we are at i_size of the clone source inode and we
6085a11452a3SFilipe Manana * can not clone from it, terminate the loop. This is
6086a11452a3SFilipe Manana * to avoid sending two write operations, one with a
6087a11452a3SFilipe Manana * length matching clone_len and the final one after
6088a11452a3SFilipe Manana * this loop with a length of len - clone_len.
6089a11452a3SFilipe Manana *
6090a11452a3SFilipe Manana * When using encoded writes (BTRFS_SEND_FLAG_COMPRESSED
6091a11452a3SFilipe Manana * was passed to the send ioctl), this helps avoid
6092a11452a3SFilipe Manana * sending an encoded write for an offset that is not
6093a11452a3SFilipe Manana * sector size aligned, in case the i_size of the source
6094a11452a3SFilipe Manana * inode is not sector size aligned. That will make the
6095a11452a3SFilipe Manana * receiver fallback to decompression of the data and
6096a11452a3SFilipe Manana * writing it using regular buffered IO, therefore while
6097a11452a3SFilipe Manana * not incorrect, it's not optimal due decompression and
6098a11452a3SFilipe Manana * possible re-compression at the receiver.
6099a11452a3SFilipe Manana */
6100a11452a3SFilipe Manana break;
61013c850b45SFilipe Manana } else {
61023ea4dc5bSOmar Sandoval ret = send_extent_data(sctx, dst_path, offset,
61033ea4dc5bSOmar Sandoval clone_len);
61043c850b45SFilipe Manana }
6105d906d49fSFilipe Manana
6106d906d49fSFilipe Manana if (ret < 0)
6107d906d49fSFilipe Manana goto out;
6108d906d49fSFilipe Manana
6109d906d49fSFilipe Manana len -= clone_len;
6110d906d49fSFilipe Manana if (len == 0)
6111d906d49fSFilipe Manana break;
6112d906d49fSFilipe Manana offset += clone_len;
6113d906d49fSFilipe Manana clone_root->offset += clone_len;
6114518837e6SFilipe Manana
6115518837e6SFilipe Manana /*
6116518837e6SFilipe Manana * If we are cloning from the file we are currently processing,
6117518837e6SFilipe Manana * and using the send root as the clone root, we must stop once
6118518837e6SFilipe Manana * the current clone offset reaches the current eof of the file
6119518837e6SFilipe Manana * at the receiver, otherwise we would issue an invalid clone
6120518837e6SFilipe Manana * operation (source range going beyond eof) and cause the
6121518837e6SFilipe Manana * receiver to fail. So if we reach the current eof, bail out
6122518837e6SFilipe Manana * and fallback to a regular write.
6123518837e6SFilipe Manana */
6124518837e6SFilipe Manana if (clone_root->root == sctx->send_root &&
6125518837e6SFilipe Manana clone_root->ino == sctx->cur_ino &&
6126518837e6SFilipe Manana clone_root->offset >= sctx->cur_inode_next_write_offset)
6127518837e6SFilipe Manana break;
6128518837e6SFilipe Manana
6129d906d49fSFilipe Manana data_offset += clone_len;
6130d906d49fSFilipe Manana next:
6131d906d49fSFilipe Manana path->slots[0]++;
6132d906d49fSFilipe Manana }
6133d906d49fSFilipe Manana
6134d906d49fSFilipe Manana if (len > 0)
61353ea4dc5bSOmar Sandoval ret = send_extent_data(sctx, dst_path, offset, len);
6136d906d49fSFilipe Manana else
6137d906d49fSFilipe Manana ret = 0;
6138d906d49fSFilipe Manana out:
6139d906d49fSFilipe Manana btrfs_free_path(path);
6140d906d49fSFilipe Manana return ret;
6141d906d49fSFilipe Manana }
6142d906d49fSFilipe Manana
send_write_or_clone(struct send_ctx * sctx,struct btrfs_path * path,struct btrfs_key * key,struct clone_root * clone_root)614331db9f7cSAlexander Block static int send_write_or_clone(struct send_ctx *sctx,
614431db9f7cSAlexander Block struct btrfs_path *path,
614531db9f7cSAlexander Block struct btrfs_key *key,
614631db9f7cSAlexander Block struct clone_root *clone_root)
614731db9f7cSAlexander Block {
614831db9f7cSAlexander Block int ret = 0;
614931db9f7cSAlexander Block u64 offset = key->offset;
6150c9a949afSOmar Sandoval u64 end;
615128e5dd8fSFilipe David Borba Manana u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
615231db9f7cSAlexander Block
6153c9a949afSOmar Sandoval end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
6154c9a949afSOmar Sandoval if (offset >= end)
6155c9a949afSOmar Sandoval return 0;
615631db9f7cSAlexander Block
6157c9a949afSOmar Sandoval if (clone_root && IS_ALIGNED(end, bs)) {
6158c9a949afSOmar Sandoval struct btrfs_file_extent_item *ei;
6159d906d49fSFilipe Manana u64 disk_byte;
6160d906d49fSFilipe Manana u64 data_offset;
6161d906d49fSFilipe Manana
6162c9a949afSOmar Sandoval ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
6163c9a949afSOmar Sandoval struct btrfs_file_extent_item);
6164d906d49fSFilipe Manana disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
6165d906d49fSFilipe Manana data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
61663ea4dc5bSOmar Sandoval ret = clone_range(sctx, path, clone_root, disk_byte,
61673ea4dc5bSOmar Sandoval data_offset, offset, end - offset);
6168cb95e7bfSMark Fasheh } else {
61693ea4dc5bSOmar Sandoval ret = send_extent_data(sctx, path, offset, end - offset);
617031db9f7cSAlexander Block }
6171c9a949afSOmar Sandoval sctx->cur_inode_next_write_offset = end;
617231db9f7cSAlexander Block return ret;
617331db9f7cSAlexander Block }
617431db9f7cSAlexander Block
is_extent_unchanged(struct send_ctx * sctx,struct btrfs_path * left_path,struct btrfs_key * ekey)617531db9f7cSAlexander Block static int is_extent_unchanged(struct send_ctx *sctx,
617631db9f7cSAlexander Block struct btrfs_path *left_path,
617731db9f7cSAlexander Block struct btrfs_key *ekey)
617831db9f7cSAlexander Block {
617931db9f7cSAlexander Block int ret = 0;
618031db9f7cSAlexander Block struct btrfs_key key;
618131db9f7cSAlexander Block struct btrfs_path *path = NULL;
618231db9f7cSAlexander Block struct extent_buffer *eb;
618331db9f7cSAlexander Block int slot;
618431db9f7cSAlexander Block struct btrfs_key found_key;
618531db9f7cSAlexander Block struct btrfs_file_extent_item *ei;
618631db9f7cSAlexander Block u64 left_disknr;
618731db9f7cSAlexander Block u64 right_disknr;
618831db9f7cSAlexander Block u64 left_offset;
618931db9f7cSAlexander Block u64 right_offset;
619031db9f7cSAlexander Block u64 left_offset_fixed;
619131db9f7cSAlexander Block u64 left_len;
619231db9f7cSAlexander Block u64 right_len;
619374dd17fbSChris Mason u64 left_gen;
619474dd17fbSChris Mason u64 right_gen;
619531db9f7cSAlexander Block u8 left_type;
619631db9f7cSAlexander Block u8 right_type;
619731db9f7cSAlexander Block
619831db9f7cSAlexander Block path = alloc_path_for_send();
619931db9f7cSAlexander Block if (!path)
620031db9f7cSAlexander Block return -ENOMEM;
620131db9f7cSAlexander Block
620231db9f7cSAlexander Block eb = left_path->nodes[0];
620331db9f7cSAlexander Block slot = left_path->slots[0];
620431db9f7cSAlexander Block ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
620531db9f7cSAlexander Block left_type = btrfs_file_extent_type(eb, ei);
620631db9f7cSAlexander Block
620731db9f7cSAlexander Block if (left_type != BTRFS_FILE_EXTENT_REG) {
620831db9f7cSAlexander Block ret = 0;
620931db9f7cSAlexander Block goto out;
621031db9f7cSAlexander Block }
621174dd17fbSChris Mason left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
621274dd17fbSChris Mason left_len = btrfs_file_extent_num_bytes(eb, ei);
621374dd17fbSChris Mason left_offset = btrfs_file_extent_offset(eb, ei);
621474dd17fbSChris Mason left_gen = btrfs_file_extent_generation(eb, ei);
621531db9f7cSAlexander Block
621631db9f7cSAlexander Block /*
621731db9f7cSAlexander Block * Following comments will refer to these graphics. L is the left
621831db9f7cSAlexander Block * extents which we are checking at the moment. 1-8 are the right
621931db9f7cSAlexander Block * extents that we iterate.
622031db9f7cSAlexander Block *
622131db9f7cSAlexander Block * |-----L-----|
622231db9f7cSAlexander Block * |-1-|-2a-|-3-|-4-|-5-|-6-|
622331db9f7cSAlexander Block *
622431db9f7cSAlexander Block * |-----L-----|
622531db9f7cSAlexander Block * |--1--|-2b-|...(same as above)
622631db9f7cSAlexander Block *
622731db9f7cSAlexander Block * Alternative situation. Happens on files where extents got split.
622831db9f7cSAlexander Block * |-----L-----|
622931db9f7cSAlexander Block * |-----------7-----------|-6-|
623031db9f7cSAlexander Block *
623131db9f7cSAlexander Block * Alternative situation. Happens on files which got larger.
623231db9f7cSAlexander Block * |-----L-----|
623331db9f7cSAlexander Block * |-8-|
623431db9f7cSAlexander Block * Nothing follows after 8.
623531db9f7cSAlexander Block */
623631db9f7cSAlexander Block
623731db9f7cSAlexander Block key.objectid = ekey->objectid;
623831db9f7cSAlexander Block key.type = BTRFS_EXTENT_DATA_KEY;
623931db9f7cSAlexander Block key.offset = ekey->offset;
624031db9f7cSAlexander Block ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
624131db9f7cSAlexander Block if (ret < 0)
624231db9f7cSAlexander Block goto out;
624331db9f7cSAlexander Block if (ret) {
624431db9f7cSAlexander Block ret = 0;
624531db9f7cSAlexander Block goto out;
624631db9f7cSAlexander Block }
624731db9f7cSAlexander Block
624831db9f7cSAlexander Block /*
624931db9f7cSAlexander Block * Handle special case where the right side has no extents at all.
625031db9f7cSAlexander Block */
625131db9f7cSAlexander Block eb = path->nodes[0];
625231db9f7cSAlexander Block slot = path->slots[0];
625331db9f7cSAlexander Block btrfs_item_key_to_cpu(eb, &found_key, slot);
625431db9f7cSAlexander Block if (found_key.objectid != key.objectid ||
625531db9f7cSAlexander Block found_key.type != key.type) {
625657cfd462SJosef Bacik /* If we're a hole then just pretend nothing changed */
625757cfd462SJosef Bacik ret = (left_disknr) ? 0 : 1;
625831db9f7cSAlexander Block goto out;
625931db9f7cSAlexander Block }
626031db9f7cSAlexander Block
626131db9f7cSAlexander Block /*
626231db9f7cSAlexander Block * We're now on 2a, 2b or 7.
626331db9f7cSAlexander Block */
626431db9f7cSAlexander Block key = found_key;
626531db9f7cSAlexander Block while (key.offset < ekey->offset + left_len) {
626631db9f7cSAlexander Block ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
626731db9f7cSAlexander Block right_type = btrfs_file_extent_type(eb, ei);
6268e1cbfd7bSFilipe Manana if (right_type != BTRFS_FILE_EXTENT_REG &&
6269e1cbfd7bSFilipe Manana right_type != BTRFS_FILE_EXTENT_INLINE) {
627031db9f7cSAlexander Block ret = 0;
627131db9f7cSAlexander Block goto out;
627231db9f7cSAlexander Block }
627331db9f7cSAlexander Block
6274e1cbfd7bSFilipe Manana if (right_type == BTRFS_FILE_EXTENT_INLINE) {
6275e41ca589SQu Wenruo right_len = btrfs_file_extent_ram_bytes(eb, ei);
6276e1cbfd7bSFilipe Manana right_len = PAGE_ALIGN(right_len);
6277e1cbfd7bSFilipe Manana } else {
6278007d31f7SJosef Bacik right_len = btrfs_file_extent_num_bytes(eb, ei);
6279e1cbfd7bSFilipe Manana }
6280007d31f7SJosef Bacik
628131db9f7cSAlexander Block /*
628231db9f7cSAlexander Block * Are we at extent 8? If yes, we know the extent is changed.
628331db9f7cSAlexander Block * This may only happen on the first iteration.
628431db9f7cSAlexander Block */
6285d8347fa4SAlexander Block if (found_key.offset + right_len <= ekey->offset) {
628657cfd462SJosef Bacik /* If we're a hole just pretend nothing changed */
628757cfd462SJosef Bacik ret = (left_disknr) ? 0 : 1;
628831db9f7cSAlexander Block goto out;
628931db9f7cSAlexander Block }
629031db9f7cSAlexander Block
6291e1cbfd7bSFilipe Manana /*
6292e1cbfd7bSFilipe Manana * We just wanted to see if when we have an inline extent, what
6293e1cbfd7bSFilipe Manana * follows it is a regular extent (wanted to check the above
6294e1cbfd7bSFilipe Manana * condition for inline extents too). This should normally not
6295e1cbfd7bSFilipe Manana * happen but it's possible for example when we have an inline
6296e1cbfd7bSFilipe Manana * compressed extent representing data with a size matching
6297e1cbfd7bSFilipe Manana * the page size (currently the same as sector size).
6298e1cbfd7bSFilipe Manana */
6299e1cbfd7bSFilipe Manana if (right_type == BTRFS_FILE_EXTENT_INLINE) {
6300e1cbfd7bSFilipe Manana ret = 0;
6301e1cbfd7bSFilipe Manana goto out;
6302e1cbfd7bSFilipe Manana }
6303e1cbfd7bSFilipe Manana
630424e52b11SFilipe Manana right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
630524e52b11SFilipe Manana right_offset = btrfs_file_extent_offset(eb, ei);
630624e52b11SFilipe Manana right_gen = btrfs_file_extent_generation(eb, ei);
630724e52b11SFilipe Manana
630831db9f7cSAlexander Block left_offset_fixed = left_offset;
630931db9f7cSAlexander Block if (key.offset < ekey->offset) {
631031db9f7cSAlexander Block /* Fix the right offset for 2a and 7. */
631131db9f7cSAlexander Block right_offset += ekey->offset - key.offset;
631231db9f7cSAlexander Block } else {
631331db9f7cSAlexander Block /* Fix the left offset for all behind 2a and 2b */
631431db9f7cSAlexander Block left_offset_fixed += key.offset - ekey->offset;
631531db9f7cSAlexander Block }
631631db9f7cSAlexander Block
631731db9f7cSAlexander Block /*
631831db9f7cSAlexander Block * Check if we have the same extent.
631931db9f7cSAlexander Block */
63203954096dSAlexander Block if (left_disknr != right_disknr ||
632174dd17fbSChris Mason left_offset_fixed != right_offset ||
632274dd17fbSChris Mason left_gen != right_gen) {
632331db9f7cSAlexander Block ret = 0;
632431db9f7cSAlexander Block goto out;
632531db9f7cSAlexander Block }
632631db9f7cSAlexander Block
632731db9f7cSAlexander Block /*
632831db9f7cSAlexander Block * Go to the next extent.
632931db9f7cSAlexander Block */
633031db9f7cSAlexander Block ret = btrfs_next_item(sctx->parent_root, path);
633131db9f7cSAlexander Block if (ret < 0)
633231db9f7cSAlexander Block goto out;
633331db9f7cSAlexander Block if (!ret) {
633431db9f7cSAlexander Block eb = path->nodes[0];
633531db9f7cSAlexander Block slot = path->slots[0];
633631db9f7cSAlexander Block btrfs_item_key_to_cpu(eb, &found_key, slot);
633731db9f7cSAlexander Block }
633831db9f7cSAlexander Block if (ret || found_key.objectid != key.objectid ||
633931db9f7cSAlexander Block found_key.type != key.type) {
634031db9f7cSAlexander Block key.offset += right_len;
634131db9f7cSAlexander Block break;
634231db9f7cSAlexander Block }
6343adaa4b8eSJan Schmidt if (found_key.offset != key.offset + right_len) {
6344adaa4b8eSJan Schmidt ret = 0;
6345adaa4b8eSJan Schmidt goto out;
634631db9f7cSAlexander Block }
634731db9f7cSAlexander Block key = found_key;
634831db9f7cSAlexander Block }
634931db9f7cSAlexander Block
635031db9f7cSAlexander Block /*
635131db9f7cSAlexander Block * We're now behind the left extent (treat as unchanged) or at the end
635231db9f7cSAlexander Block * of the right side (treat as changed).
635331db9f7cSAlexander Block */
635431db9f7cSAlexander Block if (key.offset >= ekey->offset + left_len)
635531db9f7cSAlexander Block ret = 1;
635631db9f7cSAlexander Block else
635731db9f7cSAlexander Block ret = 0;
635831db9f7cSAlexander Block
635931db9f7cSAlexander Block
636031db9f7cSAlexander Block out:
636131db9f7cSAlexander Block btrfs_free_path(path);
636231db9f7cSAlexander Block return ret;
636331db9f7cSAlexander Block }
636431db9f7cSAlexander Block
get_last_extent(struct send_ctx * sctx,u64 offset)636516e7549fSJosef Bacik static int get_last_extent(struct send_ctx *sctx, u64 offset)
636616e7549fSJosef Bacik {
636716e7549fSJosef Bacik struct btrfs_path *path;
636816e7549fSJosef Bacik struct btrfs_root *root = sctx->send_root;
636916e7549fSJosef Bacik struct btrfs_key key;
637016e7549fSJosef Bacik int ret;
637116e7549fSJosef Bacik
637216e7549fSJosef Bacik path = alloc_path_for_send();
637316e7549fSJosef Bacik if (!path)
637416e7549fSJosef Bacik return -ENOMEM;
637516e7549fSJosef Bacik
637616e7549fSJosef Bacik sctx->cur_inode_last_extent = 0;
637716e7549fSJosef Bacik
637816e7549fSJosef Bacik key.objectid = sctx->cur_ino;
637916e7549fSJosef Bacik key.type = BTRFS_EXTENT_DATA_KEY;
638016e7549fSJosef Bacik key.offset = offset;
638116e7549fSJosef Bacik ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
638216e7549fSJosef Bacik if (ret < 0)
638316e7549fSJosef Bacik goto out;
638416e7549fSJosef Bacik ret = 0;
638516e7549fSJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
638616e7549fSJosef Bacik if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
638716e7549fSJosef Bacik goto out;
638816e7549fSJosef Bacik
6389a5eeb3d1SFilipe Manana sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
639016e7549fSJosef Bacik out:
639116e7549fSJosef Bacik btrfs_free_path(path);
639216e7549fSJosef Bacik return ret;
639316e7549fSJosef Bacik }
639416e7549fSJosef Bacik
range_is_hole_in_parent(struct send_ctx * sctx,const u64 start,const u64 end)639582bfb2e7SFilipe Manana static int range_is_hole_in_parent(struct send_ctx *sctx,
639682bfb2e7SFilipe Manana const u64 start,
639782bfb2e7SFilipe Manana const u64 end)
639882bfb2e7SFilipe Manana {
639982bfb2e7SFilipe Manana struct btrfs_path *path;
640082bfb2e7SFilipe Manana struct btrfs_key key;
640182bfb2e7SFilipe Manana struct btrfs_root *root = sctx->parent_root;
640282bfb2e7SFilipe Manana u64 search_start = start;
640382bfb2e7SFilipe Manana int ret;
640482bfb2e7SFilipe Manana
640582bfb2e7SFilipe Manana path = alloc_path_for_send();
640682bfb2e7SFilipe Manana if (!path)
640782bfb2e7SFilipe Manana return -ENOMEM;
640882bfb2e7SFilipe Manana
640982bfb2e7SFilipe Manana key.objectid = sctx->cur_ino;
641082bfb2e7SFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY;
641182bfb2e7SFilipe Manana key.offset = search_start;
641282bfb2e7SFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
641382bfb2e7SFilipe Manana if (ret < 0)
641482bfb2e7SFilipe Manana goto out;
641582bfb2e7SFilipe Manana if (ret > 0 && path->slots[0] > 0)
641682bfb2e7SFilipe Manana path->slots[0]--;
641782bfb2e7SFilipe Manana
641882bfb2e7SFilipe Manana while (search_start < end) {
641982bfb2e7SFilipe Manana struct extent_buffer *leaf = path->nodes[0];
642082bfb2e7SFilipe Manana int slot = path->slots[0];
642182bfb2e7SFilipe Manana struct btrfs_file_extent_item *fi;
642282bfb2e7SFilipe Manana u64 extent_end;
642382bfb2e7SFilipe Manana
642482bfb2e7SFilipe Manana if (slot >= btrfs_header_nritems(leaf)) {
642582bfb2e7SFilipe Manana ret = btrfs_next_leaf(root, path);
642682bfb2e7SFilipe Manana if (ret < 0)
642782bfb2e7SFilipe Manana goto out;
642882bfb2e7SFilipe Manana else if (ret > 0)
642982bfb2e7SFilipe Manana break;
643082bfb2e7SFilipe Manana continue;
643182bfb2e7SFilipe Manana }
643282bfb2e7SFilipe Manana
643382bfb2e7SFilipe Manana btrfs_item_key_to_cpu(leaf, &key, slot);
643482bfb2e7SFilipe Manana if (key.objectid < sctx->cur_ino ||
643582bfb2e7SFilipe Manana key.type < BTRFS_EXTENT_DATA_KEY)
643682bfb2e7SFilipe Manana goto next;
643782bfb2e7SFilipe Manana if (key.objectid > sctx->cur_ino ||
643882bfb2e7SFilipe Manana key.type > BTRFS_EXTENT_DATA_KEY ||
643982bfb2e7SFilipe Manana key.offset >= end)
644082bfb2e7SFilipe Manana break;
644182bfb2e7SFilipe Manana
644282bfb2e7SFilipe Manana fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6443a5eeb3d1SFilipe Manana extent_end = btrfs_file_extent_end(path);
644482bfb2e7SFilipe Manana if (extent_end <= start)
644582bfb2e7SFilipe Manana goto next;
644682bfb2e7SFilipe Manana if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
644782bfb2e7SFilipe Manana search_start = extent_end;
644882bfb2e7SFilipe Manana goto next;
644982bfb2e7SFilipe Manana }
645082bfb2e7SFilipe Manana ret = 0;
645182bfb2e7SFilipe Manana goto out;
645282bfb2e7SFilipe Manana next:
645382bfb2e7SFilipe Manana path->slots[0]++;
645482bfb2e7SFilipe Manana }
645582bfb2e7SFilipe Manana ret = 1;
645682bfb2e7SFilipe Manana out:
645782bfb2e7SFilipe Manana btrfs_free_path(path);
645882bfb2e7SFilipe Manana return ret;
645982bfb2e7SFilipe Manana }
646082bfb2e7SFilipe Manana
maybe_send_hole(struct send_ctx * sctx,struct btrfs_path * path,struct btrfs_key * key)646116e7549fSJosef Bacik static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
646216e7549fSJosef Bacik struct btrfs_key *key)
646316e7549fSJosef Bacik {
646416e7549fSJosef Bacik int ret = 0;
646516e7549fSJosef Bacik
646616e7549fSJosef Bacik if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
646716e7549fSJosef Bacik return 0;
646816e7549fSJosef Bacik
646916e7549fSJosef Bacik if (sctx->cur_inode_last_extent == (u64)-1) {
647016e7549fSJosef Bacik ret = get_last_extent(sctx, key->offset - 1);
647116e7549fSJosef Bacik if (ret)
647216e7549fSJosef Bacik return ret;
647316e7549fSJosef Bacik }
647416e7549fSJosef Bacik
6475bf54f412SFilipe David Borba Manana if (path->slots[0] == 0 &&
6476bf54f412SFilipe David Borba Manana sctx->cur_inode_last_extent < key->offset) {
6477bf54f412SFilipe David Borba Manana /*
6478bf54f412SFilipe David Borba Manana * We might have skipped entire leafs that contained only
6479bf54f412SFilipe David Borba Manana * file extent items for our current inode. These leafs have
6480bf54f412SFilipe David Borba Manana * a generation number smaller (older) than the one in the
6481bf54f412SFilipe David Borba Manana * current leaf and the leaf our last extent came from, and
6482bf54f412SFilipe David Borba Manana * are located between these 2 leafs.
6483bf54f412SFilipe David Borba Manana */
6484bf54f412SFilipe David Borba Manana ret = get_last_extent(sctx, key->offset - 1);
6485bf54f412SFilipe David Borba Manana if (ret)
6486bf54f412SFilipe David Borba Manana return ret;
6487bf54f412SFilipe David Borba Manana }
6488bf54f412SFilipe David Borba Manana
648982bfb2e7SFilipe Manana if (sctx->cur_inode_last_extent < key->offset) {
649082bfb2e7SFilipe Manana ret = range_is_hole_in_parent(sctx,
649182bfb2e7SFilipe Manana sctx->cur_inode_last_extent,
649282bfb2e7SFilipe Manana key->offset);
649382bfb2e7SFilipe Manana if (ret < 0)
649482bfb2e7SFilipe Manana return ret;
649582bfb2e7SFilipe Manana else if (ret == 0)
649616e7549fSJosef Bacik ret = send_hole(sctx, key->offset);
649782bfb2e7SFilipe Manana else
649882bfb2e7SFilipe Manana ret = 0;
649982bfb2e7SFilipe Manana }
6500a5eeb3d1SFilipe Manana sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
650116e7549fSJosef Bacik return ret;
650216e7549fSJosef Bacik }
650316e7549fSJosef Bacik
process_extent(struct send_ctx * sctx,struct btrfs_path * path,struct btrfs_key * key)650431db9f7cSAlexander Block static int process_extent(struct send_ctx *sctx,
650531db9f7cSAlexander Block struct btrfs_path *path,
650631db9f7cSAlexander Block struct btrfs_key *key)
650731db9f7cSAlexander Block {
650831db9f7cSAlexander Block struct clone_root *found_clone = NULL;
650957cfd462SJosef Bacik int ret = 0;
651031db9f7cSAlexander Block
651131db9f7cSAlexander Block if (S_ISLNK(sctx->cur_inode_mode))
651231db9f7cSAlexander Block return 0;
651331db9f7cSAlexander Block
651431db9f7cSAlexander Block if (sctx->parent_root && !sctx->cur_inode_new) {
651531db9f7cSAlexander Block ret = is_extent_unchanged(sctx, path, key);
651631db9f7cSAlexander Block if (ret < 0)
651731db9f7cSAlexander Block goto out;
651831db9f7cSAlexander Block if (ret) {
651931db9f7cSAlexander Block ret = 0;
652016e7549fSJosef Bacik goto out_hole;
652131db9f7cSAlexander Block }
652257cfd462SJosef Bacik } else {
652357cfd462SJosef Bacik struct btrfs_file_extent_item *ei;
652457cfd462SJosef Bacik u8 type;
652557cfd462SJosef Bacik
652657cfd462SJosef Bacik ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
652757cfd462SJosef Bacik struct btrfs_file_extent_item);
652857cfd462SJosef Bacik type = btrfs_file_extent_type(path->nodes[0], ei);
652957cfd462SJosef Bacik if (type == BTRFS_FILE_EXTENT_PREALLOC ||
653057cfd462SJosef Bacik type == BTRFS_FILE_EXTENT_REG) {
653157cfd462SJosef Bacik /*
653257cfd462SJosef Bacik * The send spec does not have a prealloc command yet,
653357cfd462SJosef Bacik * so just leave a hole for prealloc'ed extents until
653457cfd462SJosef Bacik * we have enough commands queued up to justify rev'ing
653557cfd462SJosef Bacik * the send spec.
653657cfd462SJosef Bacik */
653757cfd462SJosef Bacik if (type == BTRFS_FILE_EXTENT_PREALLOC) {
653857cfd462SJosef Bacik ret = 0;
653957cfd462SJosef Bacik goto out;
654057cfd462SJosef Bacik }
654157cfd462SJosef Bacik
654257cfd462SJosef Bacik /* Have a hole, just skip it. */
654357cfd462SJosef Bacik if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
654457cfd462SJosef Bacik ret = 0;
654557cfd462SJosef Bacik goto out;
654657cfd462SJosef Bacik }
654757cfd462SJosef Bacik }
654831db9f7cSAlexander Block }
654931db9f7cSAlexander Block
655031db9f7cSAlexander Block ret = find_extent_clone(sctx, path, key->objectid, key->offset,
655131db9f7cSAlexander Block sctx->cur_inode_size, &found_clone);
655231db9f7cSAlexander Block if (ret != -ENOENT && ret < 0)
655331db9f7cSAlexander Block goto out;
655431db9f7cSAlexander Block
655531db9f7cSAlexander Block ret = send_write_or_clone(sctx, path, key, found_clone);
655616e7549fSJosef Bacik if (ret)
655716e7549fSJosef Bacik goto out;
655816e7549fSJosef Bacik out_hole:
655916e7549fSJosef Bacik ret = maybe_send_hole(sctx, path, key);
656031db9f7cSAlexander Block out:
656131db9f7cSAlexander Block return ret;
656231db9f7cSAlexander Block }
656331db9f7cSAlexander Block
process_all_extents(struct send_ctx * sctx)656431db9f7cSAlexander Block static int process_all_extents(struct send_ctx *sctx)
656531db9f7cSAlexander Block {
65669930e9d4SGabriel Niebler int ret = 0;
65679930e9d4SGabriel Niebler int iter_ret = 0;
656831db9f7cSAlexander Block struct btrfs_root *root;
656931db9f7cSAlexander Block struct btrfs_path *path;
657031db9f7cSAlexander Block struct btrfs_key key;
657131db9f7cSAlexander Block struct btrfs_key found_key;
657231db9f7cSAlexander Block
657331db9f7cSAlexander Block root = sctx->send_root;
657431db9f7cSAlexander Block path = alloc_path_for_send();
657531db9f7cSAlexander Block if (!path)
657631db9f7cSAlexander Block return -ENOMEM;
657731db9f7cSAlexander Block
657831db9f7cSAlexander Block key.objectid = sctx->cmp_key->objectid;
657931db9f7cSAlexander Block key.type = BTRFS_EXTENT_DATA_KEY;
658031db9f7cSAlexander Block key.offset = 0;
65819930e9d4SGabriel Niebler btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
658231db9f7cSAlexander Block if (found_key.objectid != key.objectid ||
658331db9f7cSAlexander Block found_key.type != key.type) {
658431db9f7cSAlexander Block ret = 0;
65859930e9d4SGabriel Niebler break;
658631db9f7cSAlexander Block }
658731db9f7cSAlexander Block
658831db9f7cSAlexander Block ret = process_extent(sctx, path, &found_key);
658931db9f7cSAlexander Block if (ret < 0)
65909930e9d4SGabriel Niebler break;
659131db9f7cSAlexander Block }
65929930e9d4SGabriel Niebler /* Catch error found during iteration */
65939930e9d4SGabriel Niebler if (iter_ret < 0)
65949930e9d4SGabriel Niebler ret = iter_ret;
659531db9f7cSAlexander Block
659631db9f7cSAlexander Block btrfs_free_path(path);
659731db9f7cSAlexander Block return ret;
659831db9f7cSAlexander Block }
659931db9f7cSAlexander Block
process_recorded_refs_if_needed(struct send_ctx * sctx,int at_end,int * pending_move,int * refs_processed)66009f03740aSFilipe David Borba Manana static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
66019f03740aSFilipe David Borba Manana int *pending_move,
66029f03740aSFilipe David Borba Manana int *refs_processed)
660331db9f7cSAlexander Block {
660431db9f7cSAlexander Block int ret = 0;
660531db9f7cSAlexander Block
660631db9f7cSAlexander Block if (sctx->cur_ino == 0)
660731db9f7cSAlexander Block goto out;
660831db9f7cSAlexander Block if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
660996b5bd77SJan Schmidt sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
661031db9f7cSAlexander Block goto out;
661131db9f7cSAlexander Block if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
661231db9f7cSAlexander Block goto out;
661331db9f7cSAlexander Block
66149f03740aSFilipe David Borba Manana ret = process_recorded_refs(sctx, pending_move);
6615e479d9bbSAlexander Block if (ret < 0)
6616e479d9bbSAlexander Block goto out;
6617e479d9bbSAlexander Block
66189f03740aSFilipe David Borba Manana *refs_processed = 1;
661931db9f7cSAlexander Block out:
662031db9f7cSAlexander Block return ret;
662131db9f7cSAlexander Block }
662231db9f7cSAlexander Block
finish_inode_if_needed(struct send_ctx * sctx,int at_end)662331db9f7cSAlexander Block static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
662431db9f7cSAlexander Block {
662531db9f7cSAlexander Block int ret = 0;
66267e93f6dcSBingJing Chang struct btrfs_inode_info info;
662731db9f7cSAlexander Block u64 left_mode;
662831db9f7cSAlexander Block u64 left_uid;
662931db9f7cSAlexander Block u64 left_gid;
663048247359SDavid Sterba u64 left_fileattr;
663131db9f7cSAlexander Block u64 right_mode;
663231db9f7cSAlexander Block u64 right_uid;
663331db9f7cSAlexander Block u64 right_gid;
663448247359SDavid Sterba u64 right_fileattr;
663531db9f7cSAlexander Block int need_chmod = 0;
663631db9f7cSAlexander Block int need_chown = 0;
663748247359SDavid Sterba bool need_fileattr = false;
6638ffa7c429SFilipe Manana int need_truncate = 1;
66399f03740aSFilipe David Borba Manana int pending_move = 0;
66409f03740aSFilipe David Borba Manana int refs_processed = 0;
664131db9f7cSAlexander Block
664246b2f459SFilipe Manana if (sctx->ignore_cur_inode)
664346b2f459SFilipe Manana return 0;
664446b2f459SFilipe Manana
66459f03740aSFilipe David Borba Manana ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
66469f03740aSFilipe David Borba Manana &refs_processed);
664731db9f7cSAlexander Block if (ret < 0)
664831db9f7cSAlexander Block goto out;
664931db9f7cSAlexander Block
66509f03740aSFilipe David Borba Manana /*
66519f03740aSFilipe David Borba Manana * We have processed the refs and thus need to advance send_progress.
66529f03740aSFilipe David Borba Manana * Now, calls to get_cur_xxx will take the updated refs of the current
66539f03740aSFilipe David Borba Manana * inode into account.
66549f03740aSFilipe David Borba Manana *
66559f03740aSFilipe David Borba Manana * On the other hand, if our current inode is a directory and couldn't
66569f03740aSFilipe David Borba Manana * be moved/renamed because its parent was renamed/moved too and it has
66579f03740aSFilipe David Borba Manana * a higher inode number, we can only move/rename our current inode
66589f03740aSFilipe David Borba Manana * after we moved/renamed its parent. Therefore in this case operate on
66599f03740aSFilipe David Borba Manana * the old path (pre move/rename) of our current inode, and the
66609f03740aSFilipe David Borba Manana * move/rename will be performed later.
66619f03740aSFilipe David Borba Manana */
66629f03740aSFilipe David Borba Manana if (refs_processed && !pending_move)
66639f03740aSFilipe David Borba Manana sctx->send_progress = sctx->cur_ino + 1;
66649f03740aSFilipe David Borba Manana
666531db9f7cSAlexander Block if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
666631db9f7cSAlexander Block goto out;
666731db9f7cSAlexander Block if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
666831db9f7cSAlexander Block goto out;
66697e93f6dcSBingJing Chang ret = get_inode_info(sctx->send_root, sctx->cur_ino, &info);
667031db9f7cSAlexander Block if (ret < 0)
667131db9f7cSAlexander Block goto out;
66727e93f6dcSBingJing Chang left_mode = info.mode;
66737e93f6dcSBingJing Chang left_uid = info.uid;
66747e93f6dcSBingJing Chang left_gid = info.gid;
66757e93f6dcSBingJing Chang left_fileattr = info.fileattr;
667631db9f7cSAlexander Block
667731db9f7cSAlexander Block if (!sctx->parent_root || sctx->cur_inode_new) {
667831db9f7cSAlexander Block need_chown = 1;
6679e2d044feSAlex Lyakas if (!S_ISLNK(sctx->cur_inode_mode))
6680e2d044feSAlex Lyakas need_chmod = 1;
6681ffa7c429SFilipe Manana if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6682ffa7c429SFilipe Manana need_truncate = 0;
668331db9f7cSAlexander Block } else {
6684ffa7c429SFilipe Manana u64 old_size;
6685ffa7c429SFilipe Manana
66867e93f6dcSBingJing Chang ret = get_inode_info(sctx->parent_root, sctx->cur_ino, &info);
668731db9f7cSAlexander Block if (ret < 0)
668831db9f7cSAlexander Block goto out;
66897e93f6dcSBingJing Chang old_size = info.size;
66907e93f6dcSBingJing Chang right_mode = info.mode;
66917e93f6dcSBingJing Chang right_uid = info.uid;
66927e93f6dcSBingJing Chang right_gid = info.gid;
66937e93f6dcSBingJing Chang right_fileattr = info.fileattr;
669431db9f7cSAlexander Block
669531db9f7cSAlexander Block if (left_uid != right_uid || left_gid != right_gid)
669631db9f7cSAlexander Block need_chown = 1;
6697e2d044feSAlex Lyakas if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
669831db9f7cSAlexander Block need_chmod = 1;
669948247359SDavid Sterba if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr)
670048247359SDavid Sterba need_fileattr = true;
6701ffa7c429SFilipe Manana if ((old_size == sctx->cur_inode_size) ||
6702ffa7c429SFilipe Manana (sctx->cur_inode_size > old_size &&
6703ffa7c429SFilipe Manana sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6704ffa7c429SFilipe Manana need_truncate = 0;
670531db9f7cSAlexander Block }
670631db9f7cSAlexander Block
670731db9f7cSAlexander Block if (S_ISREG(sctx->cur_inode_mode)) {
670816e7549fSJosef Bacik if (need_send_hole(sctx)) {
6709766b5e5aSFilipe Manana if (sctx->cur_inode_last_extent == (u64)-1 ||
6710766b5e5aSFilipe Manana sctx->cur_inode_last_extent <
6711766b5e5aSFilipe Manana sctx->cur_inode_size) {
671216e7549fSJosef Bacik ret = get_last_extent(sctx, (u64)-1);
671316e7549fSJosef Bacik if (ret)
671416e7549fSJosef Bacik goto out;
671516e7549fSJosef Bacik }
6716202e4f4bSFilipe Manana if (sctx->cur_inode_last_extent < sctx->cur_inode_size) {
6717202e4f4bSFilipe Manana ret = range_is_hole_in_parent(sctx,
6718202e4f4bSFilipe Manana sctx->cur_inode_last_extent,
6719202e4f4bSFilipe Manana sctx->cur_inode_size);
6720202e4f4bSFilipe Manana if (ret < 0) {
672116e7549fSJosef Bacik goto out;
6722202e4f4bSFilipe Manana } else if (ret == 0) {
6723202e4f4bSFilipe Manana ret = send_hole(sctx, sctx->cur_inode_size);
6724202e4f4bSFilipe Manana if (ret < 0)
6725202e4f4bSFilipe Manana goto out;
6726202e4f4bSFilipe Manana } else {
6727202e4f4bSFilipe Manana /* Range is already a hole, skip. */
6728202e4f4bSFilipe Manana ret = 0;
6729202e4f4bSFilipe Manana }
673016e7549fSJosef Bacik }
673116e7549fSJosef Bacik }
6732ffa7c429SFilipe Manana if (need_truncate) {
6733ffa7c429SFilipe Manana ret = send_truncate(sctx, sctx->cur_ino,
6734ffa7c429SFilipe Manana sctx->cur_inode_gen,
673531db9f7cSAlexander Block sctx->cur_inode_size);
673631db9f7cSAlexander Block if (ret < 0)
673731db9f7cSAlexander Block goto out;
673831db9f7cSAlexander Block }
6739ffa7c429SFilipe Manana }
674031db9f7cSAlexander Block
674131db9f7cSAlexander Block if (need_chown) {
674231db9f7cSAlexander Block ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
674331db9f7cSAlexander Block left_uid, left_gid);
674431db9f7cSAlexander Block if (ret < 0)
674531db9f7cSAlexander Block goto out;
674631db9f7cSAlexander Block }
674731db9f7cSAlexander Block if (need_chmod) {
674831db9f7cSAlexander Block ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
674931db9f7cSAlexander Block left_mode);
675031db9f7cSAlexander Block if (ret < 0)
675131db9f7cSAlexander Block goto out;
675231db9f7cSAlexander Block }
675348247359SDavid Sterba if (need_fileattr) {
675448247359SDavid Sterba ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen,
675548247359SDavid Sterba left_fileattr);
675648247359SDavid Sterba if (ret < 0)
675748247359SDavid Sterba goto out;
675848247359SDavid Sterba }
6759c86eab81SDavid Sterba
6760c86eab81SDavid Sterba if (proto_cmd_ok(sctx, BTRFS_SEND_C_ENABLE_VERITY)
6761c86eab81SDavid Sterba && sctx->cur_inode_needs_verity) {
676238622010SBoris Burkov ret = process_verity(sctx);
676338622010SBoris Burkov if (ret < 0)
676438622010SBoris Burkov goto out;
676538622010SBoris Burkov }
676631db9f7cSAlexander Block
676789efda52SMarcos Paulo de Souza ret = send_capabilities(sctx);
676889efda52SMarcos Paulo de Souza if (ret < 0)
676989efda52SMarcos Paulo de Souza goto out;
677089efda52SMarcos Paulo de Souza
677131db9f7cSAlexander Block /*
67729f03740aSFilipe David Borba Manana * If other directory inodes depended on our current directory
67739f03740aSFilipe David Borba Manana * inode's move/rename, now do their move/rename operations.
677431db9f7cSAlexander Block */
67759f03740aSFilipe David Borba Manana if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
67769f03740aSFilipe David Borba Manana ret = apply_children_dir_moves(sctx);
67779f03740aSFilipe David Borba Manana if (ret)
67789f03740aSFilipe David Borba Manana goto out;
67799f03740aSFilipe David Borba Manana /*
67809f03740aSFilipe David Borba Manana * Need to send that every time, no matter if it actually
67819f03740aSFilipe David Borba Manana * changed between the two trees as we have done changes to
6782fcbd2154SFilipe Manana * the inode before. If our inode is a directory and it's
6783fcbd2154SFilipe Manana * waiting to be moved/renamed, we will send its utimes when
6784fcbd2154SFilipe Manana * it's moved/renamed, therefore we don't need to do it here.
67859f03740aSFilipe David Borba Manana */
67869f03740aSFilipe David Borba Manana sctx->send_progress = sctx->cur_ino + 1;
67873e49363bSFilipe Manana
67883e49363bSFilipe Manana /*
67893e49363bSFilipe Manana * If the current inode is a non-empty directory, delay issuing
67903e49363bSFilipe Manana * the utimes command for it, as it's very likely we have inodes
67913e49363bSFilipe Manana * with an higher number inside it. We want to issue the utimes
67923e49363bSFilipe Manana * command only after adding all dentries to it.
67933e49363bSFilipe Manana */
67943e49363bSFilipe Manana if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_size > 0)
67953e49363bSFilipe Manana ret = cache_dir_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
67963e49363bSFilipe Manana else
679731db9f7cSAlexander Block ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
67983e49363bSFilipe Manana
679931db9f7cSAlexander Block if (ret < 0)
680031db9f7cSAlexander Block goto out;
6801fcbd2154SFilipe Manana }
680231db9f7cSAlexander Block
680331db9f7cSAlexander Block out:
68043e49363bSFilipe Manana if (!ret)
68053e49363bSFilipe Manana ret = trim_dir_utimes_cache(sctx);
68063e49363bSFilipe Manana
680731db9f7cSAlexander Block return ret;
680831db9f7cSAlexander Block }
680931db9f7cSAlexander Block
close_current_inode(struct send_ctx * sctx)6810152555b3SFilipe Manana static void close_current_inode(struct send_ctx *sctx)
6811152555b3SFilipe Manana {
6812152555b3SFilipe Manana u64 i_size;
6813152555b3SFilipe Manana
6814152555b3SFilipe Manana if (sctx->cur_inode == NULL)
6815152555b3SFilipe Manana return;
6816152555b3SFilipe Manana
6817152555b3SFilipe Manana i_size = i_size_read(sctx->cur_inode);
6818152555b3SFilipe Manana
6819152555b3SFilipe Manana /*
6820152555b3SFilipe Manana * If we are doing an incremental send, we may have extents between the
6821152555b3SFilipe Manana * last processed extent and the i_size that have not been processed
6822152555b3SFilipe Manana * because they haven't changed but we may have read some of their pages
6823152555b3SFilipe Manana * through readahead, see the comments at send_extent_data().
6824152555b3SFilipe Manana */
6825152555b3SFilipe Manana if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
6826152555b3SFilipe Manana truncate_inode_pages_range(&sctx->cur_inode->i_data,
6827152555b3SFilipe Manana sctx->page_cache_clear_start,
6828152555b3SFilipe Manana round_up(i_size, PAGE_SIZE) - 1);
6829152555b3SFilipe Manana
6830152555b3SFilipe Manana iput(sctx->cur_inode);
6831152555b3SFilipe Manana sctx->cur_inode = NULL;
6832152555b3SFilipe Manana }
6833152555b3SFilipe Manana
changed_inode(struct send_ctx * sctx,enum btrfs_compare_tree_result result)683431db9f7cSAlexander Block static int changed_inode(struct send_ctx *sctx,
683531db9f7cSAlexander Block enum btrfs_compare_tree_result result)
683631db9f7cSAlexander Block {
683731db9f7cSAlexander Block int ret = 0;
683831db9f7cSAlexander Block struct btrfs_key *key = sctx->cmp_key;
683931db9f7cSAlexander Block struct btrfs_inode_item *left_ii = NULL;
684031db9f7cSAlexander Block struct btrfs_inode_item *right_ii = NULL;
684131db9f7cSAlexander Block u64 left_gen = 0;
684231db9f7cSAlexander Block u64 right_gen = 0;
684331db9f7cSAlexander Block
6844152555b3SFilipe Manana close_current_inode(sctx);
6845521b6803SFilipe Manana
684631db9f7cSAlexander Block sctx->cur_ino = key->objectid;
68479555e1f1SDavid Sterba sctx->cur_inode_new_gen = false;
684816e7549fSJosef Bacik sctx->cur_inode_last_extent = (u64)-1;
6849ffa7c429SFilipe Manana sctx->cur_inode_next_write_offset = 0;
685046b2f459SFilipe Manana sctx->ignore_cur_inode = false;
6851e479d9bbSAlexander Block
6852e479d9bbSAlexander Block /*
6853e479d9bbSAlexander Block * Set send_progress to current inode. This will tell all get_cur_xxx
6854e479d9bbSAlexander Block * functions that the current inode's refs are not updated yet. Later,
6855e479d9bbSAlexander Block * when process_recorded_refs is finished, it is set to cur_ino + 1.
6856e479d9bbSAlexander Block */
685731db9f7cSAlexander Block sctx->send_progress = sctx->cur_ino;
685831db9f7cSAlexander Block
685931db9f7cSAlexander Block if (result == BTRFS_COMPARE_TREE_NEW ||
686031db9f7cSAlexander Block result == BTRFS_COMPARE_TREE_CHANGED) {
686131db9f7cSAlexander Block left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
686231db9f7cSAlexander Block sctx->left_path->slots[0],
686331db9f7cSAlexander Block struct btrfs_inode_item);
686431db9f7cSAlexander Block left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
686531db9f7cSAlexander Block left_ii);
686631db9f7cSAlexander Block } else {
686731db9f7cSAlexander Block right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
686831db9f7cSAlexander Block sctx->right_path->slots[0],
686931db9f7cSAlexander Block struct btrfs_inode_item);
687031db9f7cSAlexander Block right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
687131db9f7cSAlexander Block right_ii);
687231db9f7cSAlexander Block }
687331db9f7cSAlexander Block if (result == BTRFS_COMPARE_TREE_CHANGED) {
687431db9f7cSAlexander Block right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
687531db9f7cSAlexander Block sctx->right_path->slots[0],
687631db9f7cSAlexander Block struct btrfs_inode_item);
687731db9f7cSAlexander Block
687831db9f7cSAlexander Block right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
687931db9f7cSAlexander Block right_ii);
68806d85ed05SAlexander Block
68816d85ed05SAlexander Block /*
68826d85ed05SAlexander Block * The cur_ino = root dir case is special here. We can't treat
68836d85ed05SAlexander Block * the inode as deleted+reused because it would generate a
68846d85ed05SAlexander Block * stream that tries to delete/mkdir the root dir.
68856d85ed05SAlexander Block */
68866d85ed05SAlexander Block if (left_gen != right_gen &&
68876d85ed05SAlexander Block sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
68889555e1f1SDavid Sterba sctx->cur_inode_new_gen = true;
688931db9f7cSAlexander Block }
689031db9f7cSAlexander Block
689146b2f459SFilipe Manana /*
689246b2f459SFilipe Manana * Normally we do not find inodes with a link count of zero (orphans)
689346b2f459SFilipe Manana * because the most common case is to create a snapshot and use it
689446b2f459SFilipe Manana * for a send operation. However other less common use cases involve
689546b2f459SFilipe Manana * using a subvolume and send it after turning it to RO mode just
689646b2f459SFilipe Manana * after deleting all hard links of a file while holding an open
689746b2f459SFilipe Manana * file descriptor against it or turning a RO snapshot into RW mode,
689846b2f459SFilipe Manana * keep an open file descriptor against a file, delete it and then
689946b2f459SFilipe Manana * turn the snapshot back to RO mode before using it for a send
69009ed0a72eSBingJing Chang * operation. The former is what the receiver operation does.
69019ed0a72eSBingJing Chang * Therefore, if we want to send these snapshots soon after they're
69029ed0a72eSBingJing Chang * received, we need to handle orphan inodes as well. Moreover, orphans
69039ed0a72eSBingJing Chang * can appear not only in the send snapshot but also in the parent
69049ed0a72eSBingJing Chang * snapshot. Here are several cases:
69059ed0a72eSBingJing Chang *
69069ed0a72eSBingJing Chang * Case 1: BTRFS_COMPARE_TREE_NEW
69079ed0a72eSBingJing Chang * | send snapshot | action
69089ed0a72eSBingJing Chang * --------------------------------
69099ed0a72eSBingJing Chang * nlink | 0 | ignore
69109ed0a72eSBingJing Chang *
69119ed0a72eSBingJing Chang * Case 2: BTRFS_COMPARE_TREE_DELETED
69129ed0a72eSBingJing Chang * | parent snapshot | action
69139ed0a72eSBingJing Chang * ----------------------------------
69149ed0a72eSBingJing Chang * nlink | 0 | as usual
69159ed0a72eSBingJing Chang * Note: No unlinks will be sent because there're no paths for it.
69169ed0a72eSBingJing Chang *
69179ed0a72eSBingJing Chang * Case 3: BTRFS_COMPARE_TREE_CHANGED
69189ed0a72eSBingJing Chang * | | parent snapshot | send snapshot | action
69199ed0a72eSBingJing Chang * -----------------------------------------------------------------------
69209ed0a72eSBingJing Chang * subcase 1 | nlink | 0 | 0 | ignore
69219ed0a72eSBingJing Chang * subcase 2 | nlink | >0 | 0 | new_gen(deletion)
69229ed0a72eSBingJing Chang * subcase 3 | nlink | 0 | >0 | new_gen(creation)
69239ed0a72eSBingJing Chang *
692446b2f459SFilipe Manana */
69259ed0a72eSBingJing Chang if (result == BTRFS_COMPARE_TREE_NEW) {
69269ed0a72eSBingJing Chang if (btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii) == 0) {
692746b2f459SFilipe Manana sctx->ignore_cur_inode = true;
692846b2f459SFilipe Manana goto out;
692946b2f459SFilipe Manana }
693031db9f7cSAlexander Block sctx->cur_inode_gen = left_gen;
69319555e1f1SDavid Sterba sctx->cur_inode_new = true;
69329555e1f1SDavid Sterba sctx->cur_inode_deleted = false;
693331db9f7cSAlexander Block sctx->cur_inode_size = btrfs_inode_size(
693431db9f7cSAlexander Block sctx->left_path->nodes[0], left_ii);
693531db9f7cSAlexander Block sctx->cur_inode_mode = btrfs_inode_mode(
693631db9f7cSAlexander Block sctx->left_path->nodes[0], left_ii);
6937644d1940SLiu Bo sctx->cur_inode_rdev = btrfs_inode_rdev(
6938644d1940SLiu Bo sctx->left_path->nodes[0], left_ii);
693931db9f7cSAlexander Block if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
69401f4692daSAlexander Block ret = send_create_inode_if_needed(sctx);
694131db9f7cSAlexander Block } else if (result == BTRFS_COMPARE_TREE_DELETED) {
694231db9f7cSAlexander Block sctx->cur_inode_gen = right_gen;
69439555e1f1SDavid Sterba sctx->cur_inode_new = false;
69449555e1f1SDavid Sterba sctx->cur_inode_deleted = true;
694531db9f7cSAlexander Block sctx->cur_inode_size = btrfs_inode_size(
694631db9f7cSAlexander Block sctx->right_path->nodes[0], right_ii);
694731db9f7cSAlexander Block sctx->cur_inode_mode = btrfs_inode_mode(
694831db9f7cSAlexander Block sctx->right_path->nodes[0], right_ii);
694931db9f7cSAlexander Block } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
69509ed0a72eSBingJing Chang u32 new_nlinks, old_nlinks;
69519ed0a72eSBingJing Chang
69529ed0a72eSBingJing Chang new_nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
69539ed0a72eSBingJing Chang old_nlinks = btrfs_inode_nlink(sctx->right_path->nodes[0], right_ii);
69549ed0a72eSBingJing Chang if (new_nlinks == 0 && old_nlinks == 0) {
69559ed0a72eSBingJing Chang sctx->ignore_cur_inode = true;
69569ed0a72eSBingJing Chang goto out;
69579ed0a72eSBingJing Chang } else if (new_nlinks == 0 || old_nlinks == 0) {
69589ed0a72eSBingJing Chang sctx->cur_inode_new_gen = 1;
69599ed0a72eSBingJing Chang }
6960766702efSAlexander Block /*
6961766702efSAlexander Block * We need to do some special handling in case the inode was
6962766702efSAlexander Block * reported as changed with a changed generation number. This
6963766702efSAlexander Block * means that the original inode was deleted and new inode
6964766702efSAlexander Block * reused the same inum. So we have to treat the old inode as
6965766702efSAlexander Block * deleted and the new one as new.
6966766702efSAlexander Block */
696731db9f7cSAlexander Block if (sctx->cur_inode_new_gen) {
6968766702efSAlexander Block /*
6969766702efSAlexander Block * First, process the inode as if it was deleted.
6970766702efSAlexander Block */
69719b8be45fSBingJing Chang if (old_nlinks > 0) {
697231db9f7cSAlexander Block sctx->cur_inode_gen = right_gen;
69739555e1f1SDavid Sterba sctx->cur_inode_new = false;
69749555e1f1SDavid Sterba sctx->cur_inode_deleted = true;
697531db9f7cSAlexander Block sctx->cur_inode_size = btrfs_inode_size(
697631db9f7cSAlexander Block sctx->right_path->nodes[0], right_ii);
697731db9f7cSAlexander Block sctx->cur_inode_mode = btrfs_inode_mode(
697831db9f7cSAlexander Block sctx->right_path->nodes[0], right_ii);
697931db9f7cSAlexander Block ret = process_all_refs(sctx,
698031db9f7cSAlexander Block BTRFS_COMPARE_TREE_DELETED);
698131db9f7cSAlexander Block if (ret < 0)
698231db9f7cSAlexander Block goto out;
69839b8be45fSBingJing Chang }
698431db9f7cSAlexander Block
6985766702efSAlexander Block /*
6986766702efSAlexander Block * Now process the inode as if it was new.
6987766702efSAlexander Block */
69889ed0a72eSBingJing Chang if (new_nlinks > 0) {
698931db9f7cSAlexander Block sctx->cur_inode_gen = left_gen;
69909555e1f1SDavid Sterba sctx->cur_inode_new = true;
69919555e1f1SDavid Sterba sctx->cur_inode_deleted = false;
699231db9f7cSAlexander Block sctx->cur_inode_size = btrfs_inode_size(
69939ed0a72eSBingJing Chang sctx->left_path->nodes[0],
69949ed0a72eSBingJing Chang left_ii);
699531db9f7cSAlexander Block sctx->cur_inode_mode = btrfs_inode_mode(
69969ed0a72eSBingJing Chang sctx->left_path->nodes[0],
69979ed0a72eSBingJing Chang left_ii);
6998644d1940SLiu Bo sctx->cur_inode_rdev = btrfs_inode_rdev(
69999ed0a72eSBingJing Chang sctx->left_path->nodes[0],
70009ed0a72eSBingJing Chang left_ii);
70011f4692daSAlexander Block ret = send_create_inode_if_needed(sctx);
700231db9f7cSAlexander Block if (ret < 0)
700331db9f7cSAlexander Block goto out;
700431db9f7cSAlexander Block
700531db9f7cSAlexander Block ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
700631db9f7cSAlexander Block if (ret < 0)
700731db9f7cSAlexander Block goto out;
7008e479d9bbSAlexander Block /*
70099ed0a72eSBingJing Chang * Advance send_progress now as we did not get
70109ed0a72eSBingJing Chang * into process_recorded_refs_if_needed in the
70119ed0a72eSBingJing Chang * new_gen case.
7012e479d9bbSAlexander Block */
7013e479d9bbSAlexander Block sctx->send_progress = sctx->cur_ino + 1;
7014766702efSAlexander Block
7015766702efSAlexander Block /*
70169ed0a72eSBingJing Chang * Now process all extents and xattrs of the
70179ed0a72eSBingJing Chang * inode as if they were all new.
7018766702efSAlexander Block */
701931db9f7cSAlexander Block ret = process_all_extents(sctx);
702031db9f7cSAlexander Block if (ret < 0)
702131db9f7cSAlexander Block goto out;
702231db9f7cSAlexander Block ret = process_all_new_xattrs(sctx);
702331db9f7cSAlexander Block if (ret < 0)
702431db9f7cSAlexander Block goto out;
70259ed0a72eSBingJing Chang }
702631db9f7cSAlexander Block } else {
702731db9f7cSAlexander Block sctx->cur_inode_gen = left_gen;
70289555e1f1SDavid Sterba sctx->cur_inode_new = false;
70299555e1f1SDavid Sterba sctx->cur_inode_new_gen = false;
70309555e1f1SDavid Sterba sctx->cur_inode_deleted = false;
703131db9f7cSAlexander Block sctx->cur_inode_size = btrfs_inode_size(
703231db9f7cSAlexander Block sctx->left_path->nodes[0], left_ii);
703331db9f7cSAlexander Block sctx->cur_inode_mode = btrfs_inode_mode(
703431db9f7cSAlexander Block sctx->left_path->nodes[0], left_ii);
703531db9f7cSAlexander Block }
703631db9f7cSAlexander Block }
703731db9f7cSAlexander Block
703831db9f7cSAlexander Block out:
703931db9f7cSAlexander Block return ret;
704031db9f7cSAlexander Block }
704131db9f7cSAlexander Block
7042766702efSAlexander Block /*
7043766702efSAlexander Block * We have to process new refs before deleted refs, but compare_trees gives us
7044766702efSAlexander Block * the new and deleted refs mixed. To fix this, we record the new/deleted refs
7045766702efSAlexander Block * first and later process them in process_recorded_refs.
7046766702efSAlexander Block * For the cur_inode_new_gen case, we skip recording completely because
7047766702efSAlexander Block * changed_inode did already initiate processing of refs. The reason for this is
7048766702efSAlexander Block * that in this case, compare_tree actually compares the refs of 2 different
7049766702efSAlexander Block * inodes. To fix this, process_all_refs is used in changed_inode to handle all
7050766702efSAlexander Block * refs of the right tree as deleted and all refs of the left tree as new.
7051766702efSAlexander Block */
changed_ref(struct send_ctx * sctx,enum btrfs_compare_tree_result result)705231db9f7cSAlexander Block static int changed_ref(struct send_ctx *sctx,
705331db9f7cSAlexander Block enum btrfs_compare_tree_result result)
705431db9f7cSAlexander Block {
705531db9f7cSAlexander Block int ret = 0;
705631db9f7cSAlexander Block
705795155585SFilipe Manana if (sctx->cur_ino != sctx->cmp_key->objectid) {
705895155585SFilipe Manana inconsistent_snapshot_error(sctx, result, "reference");
705995155585SFilipe Manana return -EIO;
706095155585SFilipe Manana }
706131db9f7cSAlexander Block
706231db9f7cSAlexander Block if (!sctx->cur_inode_new_gen &&
706331db9f7cSAlexander Block sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
706431db9f7cSAlexander Block if (result == BTRFS_COMPARE_TREE_NEW)
706531db9f7cSAlexander Block ret = record_new_ref(sctx);
706631db9f7cSAlexander Block else if (result == BTRFS_COMPARE_TREE_DELETED)
706731db9f7cSAlexander Block ret = record_deleted_ref(sctx);
706831db9f7cSAlexander Block else if (result == BTRFS_COMPARE_TREE_CHANGED)
706931db9f7cSAlexander Block ret = record_changed_ref(sctx);
707031db9f7cSAlexander Block }
707131db9f7cSAlexander Block
707231db9f7cSAlexander Block return ret;
707331db9f7cSAlexander Block }
707431db9f7cSAlexander Block
7075766702efSAlexander Block /*
7076766702efSAlexander Block * Process new/deleted/changed xattrs. We skip processing in the
7077766702efSAlexander Block * cur_inode_new_gen case because changed_inode did already initiate processing
7078766702efSAlexander Block * of xattrs. The reason is the same as in changed_ref
7079766702efSAlexander Block */
changed_xattr(struct send_ctx * sctx,enum btrfs_compare_tree_result result)708031db9f7cSAlexander Block static int changed_xattr(struct send_ctx *sctx,
708131db9f7cSAlexander Block enum btrfs_compare_tree_result result)
708231db9f7cSAlexander Block {
708331db9f7cSAlexander Block int ret = 0;
708431db9f7cSAlexander Block
708595155585SFilipe Manana if (sctx->cur_ino != sctx->cmp_key->objectid) {
708695155585SFilipe Manana inconsistent_snapshot_error(sctx, result, "xattr");
708795155585SFilipe Manana return -EIO;
708895155585SFilipe Manana }
708931db9f7cSAlexander Block
709031db9f7cSAlexander Block if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
709131db9f7cSAlexander Block if (result == BTRFS_COMPARE_TREE_NEW)
709231db9f7cSAlexander Block ret = process_new_xattr(sctx);
709331db9f7cSAlexander Block else if (result == BTRFS_COMPARE_TREE_DELETED)
709431db9f7cSAlexander Block ret = process_deleted_xattr(sctx);
709531db9f7cSAlexander Block else if (result == BTRFS_COMPARE_TREE_CHANGED)
709631db9f7cSAlexander Block ret = process_changed_xattr(sctx);
709731db9f7cSAlexander Block }
709831db9f7cSAlexander Block
709931db9f7cSAlexander Block return ret;
710031db9f7cSAlexander Block }
710131db9f7cSAlexander Block
7102766702efSAlexander Block /*
7103766702efSAlexander Block * Process new/deleted/changed extents. We skip processing in the
7104766702efSAlexander Block * cur_inode_new_gen case because changed_inode did already initiate processing
7105766702efSAlexander Block * of extents. The reason is the same as in changed_ref
7106766702efSAlexander Block */
changed_extent(struct send_ctx * sctx,enum btrfs_compare_tree_result result)710731db9f7cSAlexander Block static int changed_extent(struct send_ctx *sctx,
710831db9f7cSAlexander Block enum btrfs_compare_tree_result result)
710931db9f7cSAlexander Block {
711031db9f7cSAlexander Block int ret = 0;
711131db9f7cSAlexander Block
7112d5e84fd8SFilipe Manana /*
7113b4f9a1a8SFilipe Manana * We have found an extent item that changed without the inode item
7114b4f9a1a8SFilipe Manana * having changed. This can happen either after relocation (where the
7115b4f9a1a8SFilipe Manana * disk_bytenr of an extent item is replaced at
7116b4f9a1a8SFilipe Manana * relocation.c:replace_file_extents()) or after deduplication into a
7117b4f9a1a8SFilipe Manana * file in both the parent and send snapshots (where an extent item can
7118b4f9a1a8SFilipe Manana * get modified or replaced with a new one). Note that deduplication
7119b4f9a1a8SFilipe Manana * updates the inode item, but it only changes the iversion (sequence
7120b4f9a1a8SFilipe Manana * field in the inode item) of the inode, so if a file is deduplicated
7121b4f9a1a8SFilipe Manana * the same amount of times in both the parent and send snapshots, its
71221a9fd417SDavid Sterba * iversion becomes the same in both snapshots, whence the inode item is
7123b4f9a1a8SFilipe Manana * the same on both snapshots.
7124d5e84fd8SFilipe Manana */
7125b4f9a1a8SFilipe Manana if (sctx->cur_ino != sctx->cmp_key->objectid)
7126d5e84fd8SFilipe Manana return 0;
712731db9f7cSAlexander Block
712831db9f7cSAlexander Block if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
712931db9f7cSAlexander Block if (result != BTRFS_COMPARE_TREE_DELETED)
713031db9f7cSAlexander Block ret = process_extent(sctx, sctx->left_path,
713131db9f7cSAlexander Block sctx->cmp_key);
713231db9f7cSAlexander Block }
713331db9f7cSAlexander Block
713431db9f7cSAlexander Block return ret;
713531db9f7cSAlexander Block }
713631db9f7cSAlexander Block
changed_verity(struct send_ctx * sctx,enum btrfs_compare_tree_result result)713738622010SBoris Burkov static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result)
713838622010SBoris Burkov {
713938622010SBoris Burkov int ret = 0;
714038622010SBoris Burkov
714138622010SBoris Burkov if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
714238622010SBoris Burkov if (result == BTRFS_COMPARE_TREE_NEW)
714338622010SBoris Burkov sctx->cur_inode_needs_verity = true;
714438622010SBoris Burkov }
714538622010SBoris Burkov return ret;
714638622010SBoris Burkov }
714738622010SBoris Burkov
dir_changed(struct send_ctx * sctx,u64 dir)7148ba5e8f2eSJosef Bacik static int dir_changed(struct send_ctx *sctx, u64 dir)
7149ba5e8f2eSJosef Bacik {
7150ba5e8f2eSJosef Bacik u64 orig_gen, new_gen;
7151ba5e8f2eSJosef Bacik int ret;
7152ba5e8f2eSJosef Bacik
71537e93f6dcSBingJing Chang ret = get_inode_gen(sctx->send_root, dir, &new_gen);
7154ba5e8f2eSJosef Bacik if (ret)
7155ba5e8f2eSJosef Bacik return ret;
7156ba5e8f2eSJosef Bacik
71577e93f6dcSBingJing Chang ret = get_inode_gen(sctx->parent_root, dir, &orig_gen);
7158ba5e8f2eSJosef Bacik if (ret)
7159ba5e8f2eSJosef Bacik return ret;
7160ba5e8f2eSJosef Bacik
7161ba5e8f2eSJosef Bacik return (orig_gen != new_gen) ? 1 : 0;
7162ba5e8f2eSJosef Bacik }
7163ba5e8f2eSJosef Bacik
compare_refs(struct send_ctx * sctx,struct btrfs_path * path,struct btrfs_key * key)7164ba5e8f2eSJosef Bacik static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
7165ba5e8f2eSJosef Bacik struct btrfs_key *key)
7166ba5e8f2eSJosef Bacik {
7167ba5e8f2eSJosef Bacik struct btrfs_inode_extref *extref;
7168ba5e8f2eSJosef Bacik struct extent_buffer *leaf;
7169ba5e8f2eSJosef Bacik u64 dirid = 0, last_dirid = 0;
7170ba5e8f2eSJosef Bacik unsigned long ptr;
7171ba5e8f2eSJosef Bacik u32 item_size;
7172ba5e8f2eSJosef Bacik u32 cur_offset = 0;
7173ba5e8f2eSJosef Bacik int ref_name_len;
7174ba5e8f2eSJosef Bacik int ret = 0;
7175ba5e8f2eSJosef Bacik
7176ba5e8f2eSJosef Bacik /* Easy case, just check this one dirid */
7177ba5e8f2eSJosef Bacik if (key->type == BTRFS_INODE_REF_KEY) {
7178ba5e8f2eSJosef Bacik dirid = key->offset;
7179ba5e8f2eSJosef Bacik
7180ba5e8f2eSJosef Bacik ret = dir_changed(sctx, dirid);
7181ba5e8f2eSJosef Bacik goto out;
7182ba5e8f2eSJosef Bacik }
7183ba5e8f2eSJosef Bacik
7184ba5e8f2eSJosef Bacik leaf = path->nodes[0];
71853212fa14SJosef Bacik item_size = btrfs_item_size(leaf, path->slots[0]);
7186ba5e8f2eSJosef Bacik ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
7187ba5e8f2eSJosef Bacik while (cur_offset < item_size) {
7188ba5e8f2eSJosef Bacik extref = (struct btrfs_inode_extref *)(ptr +
7189ba5e8f2eSJosef Bacik cur_offset);
7190ba5e8f2eSJosef Bacik dirid = btrfs_inode_extref_parent(leaf, extref);
7191ba5e8f2eSJosef Bacik ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
7192ba5e8f2eSJosef Bacik cur_offset += ref_name_len + sizeof(*extref);
7193ba5e8f2eSJosef Bacik if (dirid == last_dirid)
7194ba5e8f2eSJosef Bacik continue;
7195ba5e8f2eSJosef Bacik ret = dir_changed(sctx, dirid);
7196ba5e8f2eSJosef Bacik if (ret)
7197ba5e8f2eSJosef Bacik break;
7198ba5e8f2eSJosef Bacik last_dirid = dirid;
7199ba5e8f2eSJosef Bacik }
7200ba5e8f2eSJosef Bacik out:
7201ba5e8f2eSJosef Bacik return ret;
7202ba5e8f2eSJosef Bacik }
7203ba5e8f2eSJosef Bacik
7204766702efSAlexander Block /*
7205766702efSAlexander Block * Updates compare related fields in sctx and simply forwards to the actual
7206766702efSAlexander Block * changed_xxx functions.
7207766702efSAlexander Block */
changed_cb(struct btrfs_path * left_path,struct btrfs_path * right_path,struct btrfs_key * key,enum btrfs_compare_tree_result result,struct send_ctx * sctx)7208ee8c494fSNikolay Borisov static int changed_cb(struct btrfs_path *left_path,
720931db9f7cSAlexander Block struct btrfs_path *right_path,
721031db9f7cSAlexander Block struct btrfs_key *key,
721131db9f7cSAlexander Block enum btrfs_compare_tree_result result,
721288980383SRoman Anasal struct send_ctx *sctx)
721331db9f7cSAlexander Block {
721431db9f7cSAlexander Block int ret = 0;
721531db9f7cSAlexander Block
7216d96b3424SFilipe Manana /*
7217d96b3424SFilipe Manana * We can not hold the commit root semaphore here. This is because in
7218d96b3424SFilipe Manana * the case of sending and receiving to the same filesystem, using a
7219d96b3424SFilipe Manana * pipe, could result in a deadlock:
7220d96b3424SFilipe Manana *
7221d96b3424SFilipe Manana * 1) The task running send blocks on the pipe because it's full;
7222d96b3424SFilipe Manana *
7223d96b3424SFilipe Manana * 2) The task running receive, which is the only consumer of the pipe,
7224d96b3424SFilipe Manana * is waiting for a transaction commit (for example due to a space
7225d96b3424SFilipe Manana * reservation when doing a write or triggering a transaction commit
7226d96b3424SFilipe Manana * when creating a subvolume);
7227d96b3424SFilipe Manana *
7228d96b3424SFilipe Manana * 3) The transaction is waiting to write lock the commit root semaphore,
7229d96b3424SFilipe Manana * but can not acquire it since it's being held at 1).
7230d96b3424SFilipe Manana *
7231d96b3424SFilipe Manana * Down this call chain we write to the pipe through kernel_write().
7232d96b3424SFilipe Manana * The same type of problem can also happen when sending to a file that
7233d96b3424SFilipe Manana * is stored in the same filesystem - when reserving space for a write
7234d96b3424SFilipe Manana * into the file, we can trigger a transaction commit.
7235d96b3424SFilipe Manana *
7236d96b3424SFilipe Manana * Our caller has supplied us with clones of leaves from the send and
7237d96b3424SFilipe Manana * parent roots, so we're safe here from a concurrent relocation and
7238d96b3424SFilipe Manana * further reallocation of metadata extents while we are here. Below we
7239d96b3424SFilipe Manana * also assert that the leaves are clones.
7240d96b3424SFilipe Manana */
7241d96b3424SFilipe Manana lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
7242d96b3424SFilipe Manana
7243d96b3424SFilipe Manana /*
7244d96b3424SFilipe Manana * We always have a send root, so left_path is never NULL. We will not
7245d96b3424SFilipe Manana * have a leaf when we have reached the end of the send root but have
7246d96b3424SFilipe Manana * not yet reached the end of the parent root.
7247d96b3424SFilipe Manana */
7248d96b3424SFilipe Manana if (left_path->nodes[0])
7249d96b3424SFilipe Manana ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
7250d96b3424SFilipe Manana &left_path->nodes[0]->bflags));
7251d96b3424SFilipe Manana /*
7252d96b3424SFilipe Manana * When doing a full send we don't have a parent root, so right_path is
7253d96b3424SFilipe Manana * NULL. When doing an incremental send, we may have reached the end of
7254d96b3424SFilipe Manana * the parent root already, so we don't have a leaf at right_path.
7255d96b3424SFilipe Manana */
7256d96b3424SFilipe Manana if (right_path && right_path->nodes[0])
7257d96b3424SFilipe Manana ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
7258d96b3424SFilipe Manana &right_path->nodes[0]->bflags));
7259d96b3424SFilipe Manana
7260ba5e8f2eSJosef Bacik if (result == BTRFS_COMPARE_TREE_SAME) {
726116e7549fSJosef Bacik if (key->type == BTRFS_INODE_REF_KEY ||
726216e7549fSJosef Bacik key->type == BTRFS_INODE_EXTREF_KEY) {
7263ba5e8f2eSJosef Bacik ret = compare_refs(sctx, left_path, key);
7264ba5e8f2eSJosef Bacik if (!ret)
7265ba5e8f2eSJosef Bacik return 0;
7266ba5e8f2eSJosef Bacik if (ret < 0)
7267ba5e8f2eSJosef Bacik return ret;
726816e7549fSJosef Bacik } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
726916e7549fSJosef Bacik return maybe_send_hole(sctx, left_path, key);
727016e7549fSJosef Bacik } else {
727116e7549fSJosef Bacik return 0;
727216e7549fSJosef Bacik }
7273ba5e8f2eSJosef Bacik result = BTRFS_COMPARE_TREE_CHANGED;
7274ba5e8f2eSJosef Bacik ret = 0;
7275ba5e8f2eSJosef Bacik }
7276ba5e8f2eSJosef Bacik
727731db9f7cSAlexander Block sctx->left_path = left_path;
727831db9f7cSAlexander Block sctx->right_path = right_path;
727931db9f7cSAlexander Block sctx->cmp_key = key;
728031db9f7cSAlexander Block
728131db9f7cSAlexander Block ret = finish_inode_if_needed(sctx, 0);
728231db9f7cSAlexander Block if (ret < 0)
728331db9f7cSAlexander Block goto out;
728431db9f7cSAlexander Block
72852981e225SAlexander Block /* Ignore non-FS objects */
72862981e225SAlexander Block if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
72872981e225SAlexander Block key->objectid == BTRFS_FREE_SPACE_OBJECTID)
72882981e225SAlexander Block goto out;
72892981e225SAlexander Block
729046b2f459SFilipe Manana if (key->type == BTRFS_INODE_ITEM_KEY) {
729131db9f7cSAlexander Block ret = changed_inode(sctx, result);
729246b2f459SFilipe Manana } else if (!sctx->ignore_cur_inode) {
729346b2f459SFilipe Manana if (key->type == BTRFS_INODE_REF_KEY ||
729496b5bd77SJan Schmidt key->type == BTRFS_INODE_EXTREF_KEY)
729531db9f7cSAlexander Block ret = changed_ref(sctx, result);
729631db9f7cSAlexander Block else if (key->type == BTRFS_XATTR_ITEM_KEY)
729731db9f7cSAlexander Block ret = changed_xattr(sctx, result);
729831db9f7cSAlexander Block else if (key->type == BTRFS_EXTENT_DATA_KEY)
729931db9f7cSAlexander Block ret = changed_extent(sctx, result);
730038622010SBoris Burkov else if (key->type == BTRFS_VERITY_DESC_ITEM_KEY &&
730138622010SBoris Burkov key->offset == 0)
730238622010SBoris Burkov ret = changed_verity(sctx, result);
730346b2f459SFilipe Manana }
730431db9f7cSAlexander Block
730531db9f7cSAlexander Block out:
730631db9f7cSAlexander Block return ret;
730731db9f7cSAlexander Block }
730831db9f7cSAlexander Block
search_key_again(const struct send_ctx * sctx,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * key)7309d96b3424SFilipe Manana static int search_key_again(const struct send_ctx *sctx,
7310d96b3424SFilipe Manana struct btrfs_root *root,
7311d96b3424SFilipe Manana struct btrfs_path *path,
7312d96b3424SFilipe Manana const struct btrfs_key *key)
7313d96b3424SFilipe Manana {
7314d96b3424SFilipe Manana int ret;
7315d96b3424SFilipe Manana
7316d96b3424SFilipe Manana if (!path->need_commit_sem)
7317d96b3424SFilipe Manana lockdep_assert_held_read(&root->fs_info->commit_root_sem);
7318d96b3424SFilipe Manana
7319d96b3424SFilipe Manana /*
7320d96b3424SFilipe Manana * Roots used for send operations are readonly and no one can add,
7321d96b3424SFilipe Manana * update or remove keys from them, so we should be able to find our
7322d96b3424SFilipe Manana * key again. The only exception is deduplication, which can operate on
7323d96b3424SFilipe Manana * readonly roots and add, update or remove keys to/from them - but at
7324d96b3424SFilipe Manana * the moment we don't allow it to run in parallel with send.
7325d96b3424SFilipe Manana */
7326d96b3424SFilipe Manana ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7327d96b3424SFilipe Manana ASSERT(ret <= 0);
7328d96b3424SFilipe Manana if (ret > 0) {
7329d96b3424SFilipe Manana btrfs_print_tree(path->nodes[path->lowest_level], false);
7330d96b3424SFilipe Manana btrfs_err(root->fs_info,
7331d96b3424SFilipe Manana "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d",
7332d96b3424SFilipe Manana key->objectid, key->type, key->offset,
7333d96b3424SFilipe Manana (root == sctx->parent_root ? "parent" : "send"),
7334d96b3424SFilipe Manana root->root_key.objectid, path->lowest_level,
7335d96b3424SFilipe Manana path->slots[path->lowest_level]);
7336d96b3424SFilipe Manana return -EUCLEAN;
7337d96b3424SFilipe Manana }
7338d96b3424SFilipe Manana
7339d96b3424SFilipe Manana return ret;
7340d96b3424SFilipe Manana }
7341d96b3424SFilipe Manana
full_send_tree(struct send_ctx * sctx)734231db9f7cSAlexander Block static int full_send_tree(struct send_ctx *sctx)
734331db9f7cSAlexander Block {
734431db9f7cSAlexander Block int ret;
734531db9f7cSAlexander Block struct btrfs_root *send_root = sctx->send_root;
734631db9f7cSAlexander Block struct btrfs_key key;
7347d96b3424SFilipe Manana struct btrfs_fs_info *fs_info = send_root->fs_info;
734831db9f7cSAlexander Block struct btrfs_path *path;
734931db9f7cSAlexander Block
735031db9f7cSAlexander Block path = alloc_path_for_send();
735131db9f7cSAlexander Block if (!path)
735231db9f7cSAlexander Block return -ENOMEM;
7353ace75066SFilipe Manana path->reada = READA_FORWARD_ALWAYS;
735431db9f7cSAlexander Block
735531db9f7cSAlexander Block key.objectid = BTRFS_FIRST_FREE_OBJECTID;
735631db9f7cSAlexander Block key.type = BTRFS_INODE_ITEM_KEY;
735731db9f7cSAlexander Block key.offset = 0;
735831db9f7cSAlexander Block
7359d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
7360d96b3424SFilipe Manana sctx->last_reloc_trans = fs_info->last_reloc_trans;
7361d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
7362d96b3424SFilipe Manana
736331db9f7cSAlexander Block ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
736431db9f7cSAlexander Block if (ret < 0)
736531db9f7cSAlexander Block goto out;
736631db9f7cSAlexander Block if (ret)
736731db9f7cSAlexander Block goto out_finish;
736831db9f7cSAlexander Block
736931db9f7cSAlexander Block while (1) {
7370d96b3424SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
737131db9f7cSAlexander Block
7372ca5d2ba1SFilipe Manana ret = changed_cb(path, NULL, &key,
7373ee8c494fSNikolay Borisov BTRFS_COMPARE_TREE_NEW, sctx);
737431db9f7cSAlexander Block if (ret < 0)
737531db9f7cSAlexander Block goto out;
737631db9f7cSAlexander Block
7377d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
7378d96b3424SFilipe Manana if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7379d96b3424SFilipe Manana sctx->last_reloc_trans = fs_info->last_reloc_trans;
7380d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
7381d96b3424SFilipe Manana /*
7382d96b3424SFilipe Manana * A transaction used for relocating a block group was
7383d96b3424SFilipe Manana * committed or is about to finish its commit. Release
7384d96b3424SFilipe Manana * our path (leaf) and restart the search, so that we
7385d96b3424SFilipe Manana * avoid operating on any file extent items that are
7386d96b3424SFilipe Manana * stale, with a disk_bytenr that reflects a pre
7387d96b3424SFilipe Manana * relocation value. This way we avoid as much as
7388d96b3424SFilipe Manana * possible to fallback to regular writes when checking
7389d96b3424SFilipe Manana * if we can clone file ranges.
7390d96b3424SFilipe Manana */
7391d96b3424SFilipe Manana btrfs_release_path(path);
7392d96b3424SFilipe Manana ret = search_key_again(sctx, send_root, path, &key);
7393d96b3424SFilipe Manana if (ret < 0)
7394d96b3424SFilipe Manana goto out;
7395d96b3424SFilipe Manana } else {
7396d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
7397d96b3424SFilipe Manana }
7398d96b3424SFilipe Manana
739931db9f7cSAlexander Block ret = btrfs_next_item(send_root, path);
740031db9f7cSAlexander Block if (ret < 0)
740131db9f7cSAlexander Block goto out;
740231db9f7cSAlexander Block if (ret) {
740331db9f7cSAlexander Block ret = 0;
740431db9f7cSAlexander Block break;
740531db9f7cSAlexander Block }
740631db9f7cSAlexander Block }
740731db9f7cSAlexander Block
740831db9f7cSAlexander Block out_finish:
740931db9f7cSAlexander Block ret = finish_inode_if_needed(sctx, 1);
741031db9f7cSAlexander Block
741131db9f7cSAlexander Block out:
741231db9f7cSAlexander Block btrfs_free_path(path);
741331db9f7cSAlexander Block return ret;
741431db9f7cSAlexander Block }
741531db9f7cSAlexander Block
replace_node_with_clone(struct btrfs_path * path,int level)7416d96b3424SFilipe Manana static int replace_node_with_clone(struct btrfs_path *path, int level)
7417d96b3424SFilipe Manana {
7418d96b3424SFilipe Manana struct extent_buffer *clone;
7419d96b3424SFilipe Manana
7420d96b3424SFilipe Manana clone = btrfs_clone_extent_buffer(path->nodes[level]);
7421d96b3424SFilipe Manana if (!clone)
7422d96b3424SFilipe Manana return -ENOMEM;
7423d96b3424SFilipe Manana
7424d96b3424SFilipe Manana free_extent_buffer(path->nodes[level]);
7425d96b3424SFilipe Manana path->nodes[level] = clone;
7426d96b3424SFilipe Manana
7427d96b3424SFilipe Manana return 0;
7428d96b3424SFilipe Manana }
7429d96b3424SFilipe Manana
tree_move_down(struct btrfs_path * path,int * level,u64 reada_min_gen)74302ce73c63SFilipe Manana static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
743118d0f5c6SDavid Sterba {
743218d0f5c6SDavid Sterba struct extent_buffer *eb;
74332ce73c63SFilipe Manana struct extent_buffer *parent = path->nodes[*level];
74342ce73c63SFilipe Manana int slot = path->slots[*level];
74352ce73c63SFilipe Manana const int nritems = btrfs_header_nritems(parent);
74362ce73c63SFilipe Manana u64 reada_max;
74372ce73c63SFilipe Manana u64 reada_done = 0;
743818d0f5c6SDavid Sterba
7439d96b3424SFilipe Manana lockdep_assert_held_read(&parent->fs_info->commit_root_sem);
7440d96b3424SFilipe Manana
744118d0f5c6SDavid Sterba BUG_ON(*level == 0);
74422ce73c63SFilipe Manana eb = btrfs_read_node_slot(parent, slot);
744318d0f5c6SDavid Sterba if (IS_ERR(eb))
744418d0f5c6SDavid Sterba return PTR_ERR(eb);
744518d0f5c6SDavid Sterba
74462ce73c63SFilipe Manana /*
74472ce73c63SFilipe Manana * Trigger readahead for the next leaves we will process, so that it is
74482ce73c63SFilipe Manana * very likely that when we need them they are already in memory and we
74492ce73c63SFilipe Manana * will not block on disk IO. For nodes we only do readahead for one,
74502ce73c63SFilipe Manana * since the time window between processing nodes is typically larger.
74512ce73c63SFilipe Manana */
74522ce73c63SFilipe Manana reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
74532ce73c63SFilipe Manana
74542ce73c63SFilipe Manana for (slot++; slot < nritems && reada_done < reada_max; slot++) {
74552ce73c63SFilipe Manana if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
74562ce73c63SFilipe Manana btrfs_readahead_node_child(parent, slot);
74572ce73c63SFilipe Manana reada_done += eb->fs_info->nodesize;
74582ce73c63SFilipe Manana }
74592ce73c63SFilipe Manana }
74602ce73c63SFilipe Manana
746118d0f5c6SDavid Sterba path->nodes[*level - 1] = eb;
746218d0f5c6SDavid Sterba path->slots[*level - 1] = 0;
746318d0f5c6SDavid Sterba (*level)--;
7464d96b3424SFilipe Manana
7465d96b3424SFilipe Manana if (*level == 0)
7466d96b3424SFilipe Manana return replace_node_with_clone(path, 0);
7467d96b3424SFilipe Manana
746818d0f5c6SDavid Sterba return 0;
746918d0f5c6SDavid Sterba }
747018d0f5c6SDavid Sterba
tree_move_next_or_upnext(struct btrfs_path * path,int * level,int root_level)747118d0f5c6SDavid Sterba static int tree_move_next_or_upnext(struct btrfs_path *path,
747218d0f5c6SDavid Sterba int *level, int root_level)
747318d0f5c6SDavid Sterba {
747418d0f5c6SDavid Sterba int ret = 0;
747518d0f5c6SDavid Sterba int nritems;
747618d0f5c6SDavid Sterba nritems = btrfs_header_nritems(path->nodes[*level]);
747718d0f5c6SDavid Sterba
747818d0f5c6SDavid Sterba path->slots[*level]++;
747918d0f5c6SDavid Sterba
748018d0f5c6SDavid Sterba while (path->slots[*level] >= nritems) {
7481d96b3424SFilipe Manana if (*level == root_level) {
7482d96b3424SFilipe Manana path->slots[*level] = nritems - 1;
748318d0f5c6SDavid Sterba return -1;
7484d96b3424SFilipe Manana }
748518d0f5c6SDavid Sterba
748618d0f5c6SDavid Sterba /* move upnext */
748718d0f5c6SDavid Sterba path->slots[*level] = 0;
748818d0f5c6SDavid Sterba free_extent_buffer(path->nodes[*level]);
748918d0f5c6SDavid Sterba path->nodes[*level] = NULL;
749018d0f5c6SDavid Sterba (*level)++;
749118d0f5c6SDavid Sterba path->slots[*level]++;
749218d0f5c6SDavid Sterba
749318d0f5c6SDavid Sterba nritems = btrfs_header_nritems(path->nodes[*level]);
749418d0f5c6SDavid Sterba ret = 1;
749518d0f5c6SDavid Sterba }
749618d0f5c6SDavid Sterba return ret;
749718d0f5c6SDavid Sterba }
749818d0f5c6SDavid Sterba
749918d0f5c6SDavid Sterba /*
750018d0f5c6SDavid Sterba * Returns 1 if it had to move up and next. 0 is returned if it moved only next
750118d0f5c6SDavid Sterba * or down.
750218d0f5c6SDavid Sterba */
tree_advance(struct btrfs_path * path,int * level,int root_level,int allow_down,struct btrfs_key * key,u64 reada_min_gen)750318d0f5c6SDavid Sterba static int tree_advance(struct btrfs_path *path,
750418d0f5c6SDavid Sterba int *level, int root_level,
750518d0f5c6SDavid Sterba int allow_down,
75062ce73c63SFilipe Manana struct btrfs_key *key,
75072ce73c63SFilipe Manana u64 reada_min_gen)
750818d0f5c6SDavid Sterba {
750918d0f5c6SDavid Sterba int ret;
751018d0f5c6SDavid Sterba
751118d0f5c6SDavid Sterba if (*level == 0 || !allow_down) {
751218d0f5c6SDavid Sterba ret = tree_move_next_or_upnext(path, level, root_level);
751318d0f5c6SDavid Sterba } else {
75142ce73c63SFilipe Manana ret = tree_move_down(path, level, reada_min_gen);
751518d0f5c6SDavid Sterba }
7516d96b3424SFilipe Manana
7517d96b3424SFilipe Manana /*
7518d96b3424SFilipe Manana * Even if we have reached the end of a tree, ret is -1, update the key
7519d96b3424SFilipe Manana * anyway, so that in case we need to restart due to a block group
7520d96b3424SFilipe Manana * relocation, we can assert that the last key of the root node still
7521d96b3424SFilipe Manana * exists in the tree.
7522d96b3424SFilipe Manana */
752318d0f5c6SDavid Sterba if (*level == 0)
752418d0f5c6SDavid Sterba btrfs_item_key_to_cpu(path->nodes[*level], key,
752518d0f5c6SDavid Sterba path->slots[*level]);
752618d0f5c6SDavid Sterba else
752718d0f5c6SDavid Sterba btrfs_node_key_to_cpu(path->nodes[*level], key,
752818d0f5c6SDavid Sterba path->slots[*level]);
7529d96b3424SFilipe Manana
753018d0f5c6SDavid Sterba return ret;
753118d0f5c6SDavid Sterba }
753218d0f5c6SDavid Sterba
tree_compare_item(struct btrfs_path * left_path,struct btrfs_path * right_path,char * tmp_buf)753318d0f5c6SDavid Sterba static int tree_compare_item(struct btrfs_path *left_path,
753418d0f5c6SDavid Sterba struct btrfs_path *right_path,
753518d0f5c6SDavid Sterba char *tmp_buf)
753618d0f5c6SDavid Sterba {
753718d0f5c6SDavid Sterba int cmp;
753818d0f5c6SDavid Sterba int len1, len2;
753918d0f5c6SDavid Sterba unsigned long off1, off2;
754018d0f5c6SDavid Sterba
75413212fa14SJosef Bacik len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]);
75423212fa14SJosef Bacik len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]);
754318d0f5c6SDavid Sterba if (len1 != len2)
754418d0f5c6SDavid Sterba return 1;
754518d0f5c6SDavid Sterba
754618d0f5c6SDavid Sterba off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
754718d0f5c6SDavid Sterba off2 = btrfs_item_ptr_offset(right_path->nodes[0],
754818d0f5c6SDavid Sterba right_path->slots[0]);
754918d0f5c6SDavid Sterba
755018d0f5c6SDavid Sterba read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
755118d0f5c6SDavid Sterba
755218d0f5c6SDavid Sterba cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
755318d0f5c6SDavid Sterba if (cmp)
755418d0f5c6SDavid Sterba return 1;
755518d0f5c6SDavid Sterba return 0;
755618d0f5c6SDavid Sterba }
755718d0f5c6SDavid Sterba
755818d0f5c6SDavid Sterba /*
7559d96b3424SFilipe Manana * A transaction used for relocating a block group was committed or is about to
7560d96b3424SFilipe Manana * finish its commit. Release our paths and restart the search, so that we are
7561d96b3424SFilipe Manana * not using stale extent buffers:
7562d96b3424SFilipe Manana *
7563d96b3424SFilipe Manana * 1) For levels > 0, we are only holding references of extent buffers, without
7564d96b3424SFilipe Manana * any locks on them, which does not prevent them from having been relocated
7565d96b3424SFilipe Manana * and reallocated after the last time we released the commit root semaphore.
7566d96b3424SFilipe Manana * The exception are the root nodes, for which we always have a clone, see
7567d96b3424SFilipe Manana * the comment at btrfs_compare_trees();
7568d96b3424SFilipe Manana *
7569d96b3424SFilipe Manana * 2) For leaves, level 0, we are holding copies (clones) of extent buffers, so
7570d96b3424SFilipe Manana * we are safe from the concurrent relocation and reallocation. However they
7571d96b3424SFilipe Manana * can have file extent items with a pre relocation disk_bytenr value, so we
7572d96b3424SFilipe Manana * restart the start from the current commit roots and clone the new leaves so
7573d96b3424SFilipe Manana * that we get the post relocation disk_bytenr values. Not doing so, could
7574d96b3424SFilipe Manana * make us clone the wrong data in case there are new extents using the old
7575d96b3424SFilipe Manana * disk_bytenr that happen to be shared.
7576d96b3424SFilipe Manana */
restart_after_relocation(struct btrfs_path * left_path,struct btrfs_path * right_path,const struct btrfs_key * left_key,const struct btrfs_key * right_key,int left_level,int right_level,const struct send_ctx * sctx)7577d96b3424SFilipe Manana static int restart_after_relocation(struct btrfs_path *left_path,
7578d96b3424SFilipe Manana struct btrfs_path *right_path,
7579d96b3424SFilipe Manana const struct btrfs_key *left_key,
7580d96b3424SFilipe Manana const struct btrfs_key *right_key,
7581d96b3424SFilipe Manana int left_level,
7582d96b3424SFilipe Manana int right_level,
7583d96b3424SFilipe Manana const struct send_ctx *sctx)
7584d96b3424SFilipe Manana {
7585d96b3424SFilipe Manana int root_level;
7586d96b3424SFilipe Manana int ret;
7587d96b3424SFilipe Manana
7588d96b3424SFilipe Manana lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
7589d96b3424SFilipe Manana
7590d96b3424SFilipe Manana btrfs_release_path(left_path);
7591d96b3424SFilipe Manana btrfs_release_path(right_path);
7592d96b3424SFilipe Manana
7593d96b3424SFilipe Manana /*
7594d96b3424SFilipe Manana * Since keys can not be added or removed to/from our roots because they
7595d96b3424SFilipe Manana * are readonly and we do not allow deduplication to run in parallel
7596d96b3424SFilipe Manana * (which can add, remove or change keys), the layout of the trees should
7597d96b3424SFilipe Manana * not change.
7598d96b3424SFilipe Manana */
7599d96b3424SFilipe Manana left_path->lowest_level = left_level;
7600d96b3424SFilipe Manana ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
7601d96b3424SFilipe Manana if (ret < 0)
7602d96b3424SFilipe Manana return ret;
7603d96b3424SFilipe Manana
7604d96b3424SFilipe Manana right_path->lowest_level = right_level;
7605d96b3424SFilipe Manana ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
7606d96b3424SFilipe Manana if (ret < 0)
7607d96b3424SFilipe Manana return ret;
7608d96b3424SFilipe Manana
7609d96b3424SFilipe Manana /*
7610d96b3424SFilipe Manana * If the lowest level nodes are leaves, clone them so that they can be
7611d96b3424SFilipe Manana * safely used by changed_cb() while not under the protection of the
7612d96b3424SFilipe Manana * commit root semaphore, even if relocation and reallocation happens in
7613d96b3424SFilipe Manana * parallel.
7614d96b3424SFilipe Manana */
7615d96b3424SFilipe Manana if (left_level == 0) {
7616d96b3424SFilipe Manana ret = replace_node_with_clone(left_path, 0);
7617d96b3424SFilipe Manana if (ret < 0)
7618d96b3424SFilipe Manana return ret;
7619d96b3424SFilipe Manana }
7620d96b3424SFilipe Manana
7621d96b3424SFilipe Manana if (right_level == 0) {
7622d96b3424SFilipe Manana ret = replace_node_with_clone(right_path, 0);
7623d96b3424SFilipe Manana if (ret < 0)
7624d96b3424SFilipe Manana return ret;
7625d96b3424SFilipe Manana }
7626d96b3424SFilipe Manana
7627d96b3424SFilipe Manana /*
7628d96b3424SFilipe Manana * Now clone the root nodes (unless they happen to be the leaves we have
7629d96b3424SFilipe Manana * already cloned). This is to protect against concurrent snapshotting of
7630d96b3424SFilipe Manana * the send and parent roots (see the comment at btrfs_compare_trees()).
7631d96b3424SFilipe Manana */
7632d96b3424SFilipe Manana root_level = btrfs_header_level(sctx->send_root->commit_root);
7633d96b3424SFilipe Manana if (root_level > 0) {
7634d96b3424SFilipe Manana ret = replace_node_with_clone(left_path, root_level);
7635d96b3424SFilipe Manana if (ret < 0)
7636d96b3424SFilipe Manana return ret;
7637d96b3424SFilipe Manana }
7638d96b3424SFilipe Manana
7639d96b3424SFilipe Manana root_level = btrfs_header_level(sctx->parent_root->commit_root);
7640d96b3424SFilipe Manana if (root_level > 0) {
7641d96b3424SFilipe Manana ret = replace_node_with_clone(right_path, root_level);
7642d96b3424SFilipe Manana if (ret < 0)
7643d96b3424SFilipe Manana return ret;
7644d96b3424SFilipe Manana }
7645d96b3424SFilipe Manana
7646d96b3424SFilipe Manana return 0;
7647d96b3424SFilipe Manana }
7648d96b3424SFilipe Manana
7649d96b3424SFilipe Manana /*
765018d0f5c6SDavid Sterba * This function compares two trees and calls the provided callback for
765118d0f5c6SDavid Sterba * every changed/new/deleted item it finds.
765218d0f5c6SDavid Sterba * If shared tree blocks are encountered, whole subtrees are skipped, making
765318d0f5c6SDavid Sterba * the compare pretty fast on snapshotted subvolumes.
765418d0f5c6SDavid Sterba *
765518d0f5c6SDavid Sterba * This currently works on commit roots only. As commit roots are read only,
765618d0f5c6SDavid Sterba * we don't do any locking. The commit roots are protected with transactions.
765718d0f5c6SDavid Sterba * Transactions are ended and rejoined when a commit is tried in between.
765818d0f5c6SDavid Sterba *
765918d0f5c6SDavid Sterba * This function checks for modifications done to the trees while comparing.
766018d0f5c6SDavid Sterba * If it detects a change, it aborts immediately.
766118d0f5c6SDavid Sterba */
btrfs_compare_trees(struct btrfs_root * left_root,struct btrfs_root * right_root,struct send_ctx * sctx)766218d0f5c6SDavid Sterba static int btrfs_compare_trees(struct btrfs_root *left_root,
766388980383SRoman Anasal struct btrfs_root *right_root, struct send_ctx *sctx)
766418d0f5c6SDavid Sterba {
766518d0f5c6SDavid Sterba struct btrfs_fs_info *fs_info = left_root->fs_info;
766618d0f5c6SDavid Sterba int ret;
766718d0f5c6SDavid Sterba int cmp;
766818d0f5c6SDavid Sterba struct btrfs_path *left_path = NULL;
766918d0f5c6SDavid Sterba struct btrfs_path *right_path = NULL;
767018d0f5c6SDavid Sterba struct btrfs_key left_key;
767118d0f5c6SDavid Sterba struct btrfs_key right_key;
767218d0f5c6SDavid Sterba char *tmp_buf = NULL;
767318d0f5c6SDavid Sterba int left_root_level;
767418d0f5c6SDavid Sterba int right_root_level;
767518d0f5c6SDavid Sterba int left_level;
767618d0f5c6SDavid Sterba int right_level;
7677d96b3424SFilipe Manana int left_end_reached = 0;
7678d96b3424SFilipe Manana int right_end_reached = 0;
7679d96b3424SFilipe Manana int advance_left = 0;
7680d96b3424SFilipe Manana int advance_right = 0;
768118d0f5c6SDavid Sterba u64 left_blockptr;
768218d0f5c6SDavid Sterba u64 right_blockptr;
768318d0f5c6SDavid Sterba u64 left_gen;
768418d0f5c6SDavid Sterba u64 right_gen;
76852ce73c63SFilipe Manana u64 reada_min_gen;
768618d0f5c6SDavid Sterba
768718d0f5c6SDavid Sterba left_path = btrfs_alloc_path();
768818d0f5c6SDavid Sterba if (!left_path) {
768918d0f5c6SDavid Sterba ret = -ENOMEM;
769018d0f5c6SDavid Sterba goto out;
769118d0f5c6SDavid Sterba }
769218d0f5c6SDavid Sterba right_path = btrfs_alloc_path();
769318d0f5c6SDavid Sterba if (!right_path) {
769418d0f5c6SDavid Sterba ret = -ENOMEM;
769518d0f5c6SDavid Sterba goto out;
769618d0f5c6SDavid Sterba }
769718d0f5c6SDavid Sterba
769818d0f5c6SDavid Sterba tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
769918d0f5c6SDavid Sterba if (!tmp_buf) {
770018d0f5c6SDavid Sterba ret = -ENOMEM;
770118d0f5c6SDavid Sterba goto out;
770218d0f5c6SDavid Sterba }
770318d0f5c6SDavid Sterba
770418d0f5c6SDavid Sterba left_path->search_commit_root = 1;
770518d0f5c6SDavid Sterba left_path->skip_locking = 1;
770618d0f5c6SDavid Sterba right_path->search_commit_root = 1;
770718d0f5c6SDavid Sterba right_path->skip_locking = 1;
770818d0f5c6SDavid Sterba
770918d0f5c6SDavid Sterba /*
771018d0f5c6SDavid Sterba * Strategy: Go to the first items of both trees. Then do
771118d0f5c6SDavid Sterba *
771218d0f5c6SDavid Sterba * If both trees are at level 0
771318d0f5c6SDavid Sterba * Compare keys of current items
771418d0f5c6SDavid Sterba * If left < right treat left item as new, advance left tree
771518d0f5c6SDavid Sterba * and repeat
771618d0f5c6SDavid Sterba * If left > right treat right item as deleted, advance right tree
771718d0f5c6SDavid Sterba * and repeat
771818d0f5c6SDavid Sterba * If left == right do deep compare of items, treat as changed if
771918d0f5c6SDavid Sterba * needed, advance both trees and repeat
772018d0f5c6SDavid Sterba * If both trees are at the same level but not at level 0
772118d0f5c6SDavid Sterba * Compare keys of current nodes/leafs
772218d0f5c6SDavid Sterba * If left < right advance left tree and repeat
772318d0f5c6SDavid Sterba * If left > right advance right tree and repeat
772418d0f5c6SDavid Sterba * If left == right compare blockptrs of the next nodes/leafs
772518d0f5c6SDavid Sterba * If they match advance both trees but stay at the same level
772618d0f5c6SDavid Sterba * and repeat
772718d0f5c6SDavid Sterba * If they don't match advance both trees while allowing to go
772818d0f5c6SDavid Sterba * deeper and repeat
772918d0f5c6SDavid Sterba * If tree levels are different
773018d0f5c6SDavid Sterba * Advance the tree that needs it and repeat
773118d0f5c6SDavid Sterba *
773218d0f5c6SDavid Sterba * Advancing a tree means:
773318d0f5c6SDavid Sterba * If we are at level 0, try to go to the next slot. If that's not
773418d0f5c6SDavid Sterba * possible, go one level up and repeat. Stop when we found a level
773518d0f5c6SDavid Sterba * where we could go to the next slot. We may at this point be on a
773618d0f5c6SDavid Sterba * node or a leaf.
773718d0f5c6SDavid Sterba *
773818d0f5c6SDavid Sterba * If we are not at level 0 and not on shared tree blocks, go one
773918d0f5c6SDavid Sterba * level deeper.
774018d0f5c6SDavid Sterba *
774118d0f5c6SDavid Sterba * If we are not at level 0 and on shared tree blocks, go one slot to
774218d0f5c6SDavid Sterba * the right if possible or go up and right.
774318d0f5c6SDavid Sterba */
774418d0f5c6SDavid Sterba
774518d0f5c6SDavid Sterba down_read(&fs_info->commit_root_sem);
774618d0f5c6SDavid Sterba left_level = btrfs_header_level(left_root->commit_root);
774718d0f5c6SDavid Sterba left_root_level = left_level;
7748d96b3424SFilipe Manana /*
7749d96b3424SFilipe Manana * We clone the root node of the send and parent roots to prevent races
7750d96b3424SFilipe Manana * with snapshot creation of these roots. Snapshot creation COWs the
7751d96b3424SFilipe Manana * root node of a tree, so after the transaction is committed the old
7752d96b3424SFilipe Manana * extent can be reallocated while this send operation is still ongoing.
7753d96b3424SFilipe Manana * So we clone them, under the commit root semaphore, to be race free.
7754d96b3424SFilipe Manana */
775518d0f5c6SDavid Sterba left_path->nodes[left_level] =
775618d0f5c6SDavid Sterba btrfs_clone_extent_buffer(left_root->commit_root);
775718d0f5c6SDavid Sterba if (!left_path->nodes[left_level]) {
775818d0f5c6SDavid Sterba ret = -ENOMEM;
7759d96b3424SFilipe Manana goto out_unlock;
776018d0f5c6SDavid Sterba }
776118d0f5c6SDavid Sterba
776218d0f5c6SDavid Sterba right_level = btrfs_header_level(right_root->commit_root);
776318d0f5c6SDavid Sterba right_root_level = right_level;
776418d0f5c6SDavid Sterba right_path->nodes[right_level] =
776518d0f5c6SDavid Sterba btrfs_clone_extent_buffer(right_root->commit_root);
776618d0f5c6SDavid Sterba if (!right_path->nodes[right_level]) {
776718d0f5c6SDavid Sterba ret = -ENOMEM;
7768d96b3424SFilipe Manana goto out_unlock;
776918d0f5c6SDavid Sterba }
77702ce73c63SFilipe Manana /*
77712ce73c63SFilipe Manana * Our right root is the parent root, while the left root is the "send"
77722ce73c63SFilipe Manana * root. We know that all new nodes/leaves in the left root must have
77732ce73c63SFilipe Manana * a generation greater than the right root's generation, so we trigger
77742ce73c63SFilipe Manana * readahead for those nodes and leaves of the left root, as we know we
77752ce73c63SFilipe Manana * will need to read them at some point.
77762ce73c63SFilipe Manana */
77772ce73c63SFilipe Manana reada_min_gen = btrfs_header_generation(right_root->commit_root);
777818d0f5c6SDavid Sterba
777918d0f5c6SDavid Sterba if (left_level == 0)
778018d0f5c6SDavid Sterba btrfs_item_key_to_cpu(left_path->nodes[left_level],
778118d0f5c6SDavid Sterba &left_key, left_path->slots[left_level]);
778218d0f5c6SDavid Sterba else
778318d0f5c6SDavid Sterba btrfs_node_key_to_cpu(left_path->nodes[left_level],
778418d0f5c6SDavid Sterba &left_key, left_path->slots[left_level]);
778518d0f5c6SDavid Sterba if (right_level == 0)
778618d0f5c6SDavid Sterba btrfs_item_key_to_cpu(right_path->nodes[right_level],
778718d0f5c6SDavid Sterba &right_key, right_path->slots[right_level]);
778818d0f5c6SDavid Sterba else
778918d0f5c6SDavid Sterba btrfs_node_key_to_cpu(right_path->nodes[right_level],
779018d0f5c6SDavid Sterba &right_key, right_path->slots[right_level]);
779118d0f5c6SDavid Sterba
7792d96b3424SFilipe Manana sctx->last_reloc_trans = fs_info->last_reloc_trans;
779318d0f5c6SDavid Sterba
779418d0f5c6SDavid Sterba while (1) {
7795d96b3424SFilipe Manana if (need_resched() ||
7796d96b3424SFilipe Manana rwsem_is_contended(&fs_info->commit_root_sem)) {
7797d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
77986af112b1SNikolay Borisov cond_resched();
7799d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
7800d96b3424SFilipe Manana }
7801d96b3424SFilipe Manana
7802d96b3424SFilipe Manana if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7803d96b3424SFilipe Manana ret = restart_after_relocation(left_path, right_path,
7804d96b3424SFilipe Manana &left_key, &right_key,
7805d96b3424SFilipe Manana left_level, right_level,
7806d96b3424SFilipe Manana sctx);
7807d96b3424SFilipe Manana if (ret < 0)
7808d96b3424SFilipe Manana goto out_unlock;
7809d96b3424SFilipe Manana sctx->last_reloc_trans = fs_info->last_reloc_trans;
7810d96b3424SFilipe Manana }
7811d96b3424SFilipe Manana
781218d0f5c6SDavid Sterba if (advance_left && !left_end_reached) {
781318d0f5c6SDavid Sterba ret = tree_advance(left_path, &left_level,
781418d0f5c6SDavid Sterba left_root_level,
781518d0f5c6SDavid Sterba advance_left != ADVANCE_ONLY_NEXT,
78162ce73c63SFilipe Manana &left_key, reada_min_gen);
781718d0f5c6SDavid Sterba if (ret == -1)
781818d0f5c6SDavid Sterba left_end_reached = ADVANCE;
781918d0f5c6SDavid Sterba else if (ret < 0)
7820d96b3424SFilipe Manana goto out_unlock;
782118d0f5c6SDavid Sterba advance_left = 0;
782218d0f5c6SDavid Sterba }
782318d0f5c6SDavid Sterba if (advance_right && !right_end_reached) {
782418d0f5c6SDavid Sterba ret = tree_advance(right_path, &right_level,
782518d0f5c6SDavid Sterba right_root_level,
782618d0f5c6SDavid Sterba advance_right != ADVANCE_ONLY_NEXT,
78272ce73c63SFilipe Manana &right_key, reada_min_gen);
782818d0f5c6SDavid Sterba if (ret == -1)
782918d0f5c6SDavid Sterba right_end_reached = ADVANCE;
783018d0f5c6SDavid Sterba else if (ret < 0)
7831d96b3424SFilipe Manana goto out_unlock;
783218d0f5c6SDavid Sterba advance_right = 0;
783318d0f5c6SDavid Sterba }
783418d0f5c6SDavid Sterba
783518d0f5c6SDavid Sterba if (left_end_reached && right_end_reached) {
783618d0f5c6SDavid Sterba ret = 0;
7837d96b3424SFilipe Manana goto out_unlock;
783818d0f5c6SDavid Sterba } else if (left_end_reached) {
783918d0f5c6SDavid Sterba if (right_level == 0) {
7840d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
784118d0f5c6SDavid Sterba ret = changed_cb(left_path, right_path,
784218d0f5c6SDavid Sterba &right_key,
784318d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_DELETED,
784488980383SRoman Anasal sctx);
784518d0f5c6SDavid Sterba if (ret < 0)
784618d0f5c6SDavid Sterba goto out;
7847d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
784818d0f5c6SDavid Sterba }
784918d0f5c6SDavid Sterba advance_right = ADVANCE;
785018d0f5c6SDavid Sterba continue;
785118d0f5c6SDavid Sterba } else if (right_end_reached) {
785218d0f5c6SDavid Sterba if (left_level == 0) {
7853d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
785418d0f5c6SDavid Sterba ret = changed_cb(left_path, right_path,
785518d0f5c6SDavid Sterba &left_key,
785618d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_NEW,
785788980383SRoman Anasal sctx);
785818d0f5c6SDavid Sterba if (ret < 0)
785918d0f5c6SDavid Sterba goto out;
7860d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
786118d0f5c6SDavid Sterba }
786218d0f5c6SDavid Sterba advance_left = ADVANCE;
786318d0f5c6SDavid Sterba continue;
786418d0f5c6SDavid Sterba }
786518d0f5c6SDavid Sterba
786618d0f5c6SDavid Sterba if (left_level == 0 && right_level == 0) {
7867d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
786818d0f5c6SDavid Sterba cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
786918d0f5c6SDavid Sterba if (cmp < 0) {
787018d0f5c6SDavid Sterba ret = changed_cb(left_path, right_path,
787118d0f5c6SDavid Sterba &left_key,
787218d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_NEW,
787388980383SRoman Anasal sctx);
787418d0f5c6SDavid Sterba advance_left = ADVANCE;
787518d0f5c6SDavid Sterba } else if (cmp > 0) {
787618d0f5c6SDavid Sterba ret = changed_cb(left_path, right_path,
787718d0f5c6SDavid Sterba &right_key,
787818d0f5c6SDavid Sterba BTRFS_COMPARE_TREE_DELETED,
787988980383SRoman Anasal sctx);
788018d0f5c6SDavid Sterba advance_right = ADVANCE;
788118d0f5c6SDavid Sterba } else {
788218d0f5c6SDavid Sterba enum btrfs_compare_tree_result result;
788318d0f5c6SDavid Sterba
788418d0f5c6SDavid Sterba WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
788518d0f5c6SDavid Sterba ret = tree_compare_item(left_path, right_path,
788618d0f5c6SDavid Sterba tmp_buf);
788718d0f5c6SDavid Sterba if (ret)
788818d0f5c6SDavid Sterba result = BTRFS_COMPARE_TREE_CHANGED;
788918d0f5c6SDavid Sterba else
789018d0f5c6SDavid Sterba result = BTRFS_COMPARE_TREE_SAME;
789118d0f5c6SDavid Sterba ret = changed_cb(left_path, right_path,
789288980383SRoman Anasal &left_key, result, sctx);
789318d0f5c6SDavid Sterba advance_left = ADVANCE;
789418d0f5c6SDavid Sterba advance_right = ADVANCE;
789518d0f5c6SDavid Sterba }
7896d96b3424SFilipe Manana
7897d96b3424SFilipe Manana if (ret < 0)
7898d96b3424SFilipe Manana goto out;
7899d96b3424SFilipe Manana down_read(&fs_info->commit_root_sem);
790018d0f5c6SDavid Sterba } else if (left_level == right_level) {
790118d0f5c6SDavid Sterba cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
790218d0f5c6SDavid Sterba if (cmp < 0) {
790318d0f5c6SDavid Sterba advance_left = ADVANCE;
790418d0f5c6SDavid Sterba } else if (cmp > 0) {
790518d0f5c6SDavid Sterba advance_right = ADVANCE;
790618d0f5c6SDavid Sterba } else {
790718d0f5c6SDavid Sterba left_blockptr = btrfs_node_blockptr(
790818d0f5c6SDavid Sterba left_path->nodes[left_level],
790918d0f5c6SDavid Sterba left_path->slots[left_level]);
791018d0f5c6SDavid Sterba right_blockptr = btrfs_node_blockptr(
791118d0f5c6SDavid Sterba right_path->nodes[right_level],
791218d0f5c6SDavid Sterba right_path->slots[right_level]);
791318d0f5c6SDavid Sterba left_gen = btrfs_node_ptr_generation(
791418d0f5c6SDavid Sterba left_path->nodes[left_level],
791518d0f5c6SDavid Sterba left_path->slots[left_level]);
791618d0f5c6SDavid Sterba right_gen = btrfs_node_ptr_generation(
791718d0f5c6SDavid Sterba right_path->nodes[right_level],
791818d0f5c6SDavid Sterba right_path->slots[right_level]);
791918d0f5c6SDavid Sterba if (left_blockptr == right_blockptr &&
792018d0f5c6SDavid Sterba left_gen == right_gen) {
792118d0f5c6SDavid Sterba /*
792218d0f5c6SDavid Sterba * As we're on a shared block, don't
792318d0f5c6SDavid Sterba * allow to go deeper.
792418d0f5c6SDavid Sterba */
792518d0f5c6SDavid Sterba advance_left = ADVANCE_ONLY_NEXT;
792618d0f5c6SDavid Sterba advance_right = ADVANCE_ONLY_NEXT;
792718d0f5c6SDavid Sterba } else {
792818d0f5c6SDavid Sterba advance_left = ADVANCE;
792918d0f5c6SDavid Sterba advance_right = ADVANCE;
793018d0f5c6SDavid Sterba }
793118d0f5c6SDavid Sterba }
793218d0f5c6SDavid Sterba } else if (left_level < right_level) {
793318d0f5c6SDavid Sterba advance_right = ADVANCE;
793418d0f5c6SDavid Sterba } else {
793518d0f5c6SDavid Sterba advance_left = ADVANCE;
793618d0f5c6SDavid Sterba }
793718d0f5c6SDavid Sterba }
793818d0f5c6SDavid Sterba
7939d96b3424SFilipe Manana out_unlock:
7940d96b3424SFilipe Manana up_read(&fs_info->commit_root_sem);
794118d0f5c6SDavid Sterba out:
794218d0f5c6SDavid Sterba btrfs_free_path(left_path);
794318d0f5c6SDavid Sterba btrfs_free_path(right_path);
794418d0f5c6SDavid Sterba kvfree(tmp_buf);
794518d0f5c6SDavid Sterba return ret;
794618d0f5c6SDavid Sterba }
794718d0f5c6SDavid Sterba
send_subvol(struct send_ctx * sctx)794831db9f7cSAlexander Block static int send_subvol(struct send_ctx *sctx)
794931db9f7cSAlexander Block {
795031db9f7cSAlexander Block int ret;
795131db9f7cSAlexander Block
7952c2c71324SStefan Behrens if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
795331db9f7cSAlexander Block ret = send_header(sctx);
795431db9f7cSAlexander Block if (ret < 0)
795531db9f7cSAlexander Block goto out;
7956c2c71324SStefan Behrens }
795731db9f7cSAlexander Block
795831db9f7cSAlexander Block ret = send_subvol_begin(sctx);
795931db9f7cSAlexander Block if (ret < 0)
796031db9f7cSAlexander Block goto out;
796131db9f7cSAlexander Block
796231db9f7cSAlexander Block if (sctx->parent_root) {
79631b51d6fcSDavid Sterba ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
796431db9f7cSAlexander Block if (ret < 0)
796531db9f7cSAlexander Block goto out;
796631db9f7cSAlexander Block ret = finish_inode_if_needed(sctx, 1);
796731db9f7cSAlexander Block if (ret < 0)
796831db9f7cSAlexander Block goto out;
796931db9f7cSAlexander Block } else {
797031db9f7cSAlexander Block ret = full_send_tree(sctx);
797131db9f7cSAlexander Block if (ret < 0)
797231db9f7cSAlexander Block goto out;
797331db9f7cSAlexander Block }
797431db9f7cSAlexander Block
797531db9f7cSAlexander Block out:
797631db9f7cSAlexander Block free_recorded_refs(sctx);
797731db9f7cSAlexander Block return ret;
797831db9f7cSAlexander Block }
797931db9f7cSAlexander Block
7980e5fa8f86SFilipe Manana /*
7981e5fa8f86SFilipe Manana * If orphan cleanup did remove any orphans from a root, it means the tree
7982e5fa8f86SFilipe Manana * was modified and therefore the commit root is not the same as the current
7983e5fa8f86SFilipe Manana * root anymore. This is a problem, because send uses the commit root and
7984e5fa8f86SFilipe Manana * therefore can see inode items that don't exist in the current root anymore,
7985e5fa8f86SFilipe Manana * and for example make calls to btrfs_iget, which will do tree lookups based
7986e5fa8f86SFilipe Manana * on the current root and not on the commit root. Those lookups will fail,
7987e5fa8f86SFilipe Manana * returning a -ESTALE error, and making send fail with that error. So make
7988e5fa8f86SFilipe Manana * sure a send does not see any orphans we have just removed, and that it will
7989e5fa8f86SFilipe Manana * see the same inodes regardless of whether a transaction commit happened
7990e5fa8f86SFilipe Manana * before it started (meaning that the commit root will be the same as the
7991e5fa8f86SFilipe Manana * current root) or not.
7992e5fa8f86SFilipe Manana */
ensure_commit_roots_uptodate(struct send_ctx * sctx)7993e5fa8f86SFilipe Manana static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7994e5fa8f86SFilipe Manana {
7995e5fa8f86SFilipe Manana int i;
7996e5fa8f86SFilipe Manana struct btrfs_trans_handle *trans = NULL;
7997e5fa8f86SFilipe Manana
7998e5fa8f86SFilipe Manana again:
7999e5fa8f86SFilipe Manana if (sctx->parent_root &&
8000e5fa8f86SFilipe Manana sctx->parent_root->node != sctx->parent_root->commit_root)
8001e5fa8f86SFilipe Manana goto commit_trans;
8002e5fa8f86SFilipe Manana
8003e5fa8f86SFilipe Manana for (i = 0; i < sctx->clone_roots_cnt; i++)
8004e5fa8f86SFilipe Manana if (sctx->clone_roots[i].root->node !=
8005e5fa8f86SFilipe Manana sctx->clone_roots[i].root->commit_root)
8006e5fa8f86SFilipe Manana goto commit_trans;
8007e5fa8f86SFilipe Manana
8008e5fa8f86SFilipe Manana if (trans)
80093a45bb20SJeff Mahoney return btrfs_end_transaction(trans);
8010e5fa8f86SFilipe Manana
8011e5fa8f86SFilipe Manana return 0;
8012e5fa8f86SFilipe Manana
8013e5fa8f86SFilipe Manana commit_trans:
8014e5fa8f86SFilipe Manana /* Use any root, all fs roots will get their commit roots updated. */
8015e5fa8f86SFilipe Manana if (!trans) {
8016e5fa8f86SFilipe Manana trans = btrfs_join_transaction(sctx->send_root);
8017e5fa8f86SFilipe Manana if (IS_ERR(trans))
8018e5fa8f86SFilipe Manana return PTR_ERR(trans);
8019e5fa8f86SFilipe Manana goto again;
8020e5fa8f86SFilipe Manana }
8021e5fa8f86SFilipe Manana
80223a45bb20SJeff Mahoney return btrfs_commit_transaction(trans);
8023e5fa8f86SFilipe Manana }
8024e5fa8f86SFilipe Manana
80259f89d5deSFilipe Manana /*
80269f89d5deSFilipe Manana * Make sure any existing dellaloc is flushed for any root used by a send
80279f89d5deSFilipe Manana * operation so that we do not miss any data and we do not race with writeback
80289f89d5deSFilipe Manana * finishing and changing a tree while send is using the tree. This could
80299f89d5deSFilipe Manana * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
80309f89d5deSFilipe Manana * a send operation then uses the subvolume.
80319f89d5deSFilipe Manana * After flushing delalloc ensure_commit_roots_uptodate() must be called.
80329f89d5deSFilipe Manana */
flush_delalloc_roots(struct send_ctx * sctx)80339f89d5deSFilipe Manana static int flush_delalloc_roots(struct send_ctx *sctx)
80349f89d5deSFilipe Manana {
80359f89d5deSFilipe Manana struct btrfs_root *root = sctx->parent_root;
80369f89d5deSFilipe Manana int ret;
80379f89d5deSFilipe Manana int i;
80389f89d5deSFilipe Manana
80399f89d5deSFilipe Manana if (root) {
8040f9baa501SFilipe Manana ret = btrfs_start_delalloc_snapshot(root, false);
80419f89d5deSFilipe Manana if (ret)
80429f89d5deSFilipe Manana return ret;
80439f89d5deSFilipe Manana btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
80449f89d5deSFilipe Manana }
80459f89d5deSFilipe Manana
80469f89d5deSFilipe Manana for (i = 0; i < sctx->clone_roots_cnt; i++) {
80479f89d5deSFilipe Manana root = sctx->clone_roots[i].root;
8048f9baa501SFilipe Manana ret = btrfs_start_delalloc_snapshot(root, false);
80499f89d5deSFilipe Manana if (ret)
80509f89d5deSFilipe Manana return ret;
80519f89d5deSFilipe Manana btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
80529f89d5deSFilipe Manana }
80539f89d5deSFilipe Manana
80549f89d5deSFilipe Manana return 0;
80559f89d5deSFilipe Manana }
80569f89d5deSFilipe Manana
btrfs_root_dec_send_in_progress(struct btrfs_root * root)805766ef7d65SDavid Sterba static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
805866ef7d65SDavid Sterba {
805966ef7d65SDavid Sterba spin_lock(&root->root_item_lock);
806066ef7d65SDavid Sterba root->send_in_progress--;
806166ef7d65SDavid Sterba /*
806266ef7d65SDavid Sterba * Not much left to do, we don't know why it's unbalanced and
806366ef7d65SDavid Sterba * can't blindly reset it to 0.
806466ef7d65SDavid Sterba */
806566ef7d65SDavid Sterba if (root->send_in_progress < 0)
806666ef7d65SDavid Sterba btrfs_err(root->fs_info,
8067f5686e3aSColin Ian King "send_in_progress unbalanced %d root %llu",
806866ef7d65SDavid Sterba root->send_in_progress, root->root_key.objectid);
806966ef7d65SDavid Sterba spin_unlock(&root->root_item_lock);
807066ef7d65SDavid Sterba }
807166ef7d65SDavid Sterba
dedupe_in_progress_warn(const struct btrfs_root * root)807262d54f3aSFilipe Manana static void dedupe_in_progress_warn(const struct btrfs_root *root)
807362d54f3aSFilipe Manana {
807462d54f3aSFilipe Manana btrfs_warn_rl(root->fs_info,
807562d54f3aSFilipe Manana "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
807662d54f3aSFilipe Manana root->root_key.objectid, root->dedupe_in_progress);
807762d54f3aSFilipe Manana }
807862d54f3aSFilipe Manana
btrfs_ioctl_send(struct inode * inode,struct btrfs_ioctl_send_args * arg)80799ad12305SSahil Kang long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)
808031db9f7cSAlexander Block {
808131db9f7cSAlexander Block int ret = 0;
80829ad12305SSahil Kang struct btrfs_root *send_root = BTRFS_I(inode)->root;
80830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = send_root->fs_info;
808431db9f7cSAlexander Block struct btrfs_root *clone_root;
808531db9f7cSAlexander Block struct send_ctx *sctx = NULL;
808631db9f7cSAlexander Block u32 i;
808731db9f7cSAlexander Block u64 *clone_sources_tmp = NULL;
80882c686537SDavid Sterba int clone_sources_to_rollback = 0;
8089bae12df9SDenis Efremov size_t alloc_size;
8090896c14f9SWang Shilong int sort_clone_roots = 0;
80913e49363bSFilipe Manana struct btrfs_lru_cache_entry *entry;
80923e49363bSFilipe Manana struct btrfs_lru_cache_entry *tmp;
809331db9f7cSAlexander Block
809431db9f7cSAlexander Block if (!capable(CAP_SYS_ADMIN))
809531db9f7cSAlexander Block return -EPERM;
809631db9f7cSAlexander Block
8097139f807aSJosef Bacik /*
80982c686537SDavid Sterba * The subvolume must remain read-only during send, protect against
8099521e0546SDavid Sterba * making it RW. This also protects against deletion.
81002c686537SDavid Sterba */
81012c686537SDavid Sterba spin_lock(&send_root->root_item_lock);
810262d54f3aSFilipe Manana if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
810362d54f3aSFilipe Manana dedupe_in_progress_warn(send_root);
810462d54f3aSFilipe Manana spin_unlock(&send_root->root_item_lock);
810562d54f3aSFilipe Manana return -EAGAIN;
810662d54f3aSFilipe Manana }
81072c686537SDavid Sterba send_root->send_in_progress++;
81082c686537SDavid Sterba spin_unlock(&send_root->root_item_lock);
81092c686537SDavid Sterba
81102c686537SDavid Sterba /*
81112c686537SDavid Sterba * Userspace tools do the checks and warn the user if it's
81122c686537SDavid Sterba * not RO.
81132c686537SDavid Sterba */
81142c686537SDavid Sterba if (!btrfs_root_readonly(send_root)) {
81152c686537SDavid Sterba ret = -EPERM;
81162c686537SDavid Sterba goto out;
81172c686537SDavid Sterba }
81182c686537SDavid Sterba
8119457ae726SDan Carpenter /*
8120457ae726SDan Carpenter * Check that we don't overflow at later allocations, we request
8121457ae726SDan Carpenter * clone_sources_count + 1 items, and compare to unsigned long inside
812233e17b3fSDavid Sterba * access_ok. Also set an upper limit for allocation size so this can't
812333e17b3fSDavid Sterba * easily exhaust memory. Max number of clone sources is about 200K.
8124457ae726SDan Carpenter */
812533e17b3fSDavid Sterba if (arg->clone_sources_count > SZ_8M / sizeof(struct clone_root)) {
8126f5ecec3cSDan Carpenter ret = -EINVAL;
8127f5ecec3cSDan Carpenter goto out;
8128f5ecec3cSDan Carpenter }
8129f5ecec3cSDan Carpenter
8130c2c71324SStefan Behrens if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
81317efadbcbSDavid Sterba ret = -EOPNOTSUPP;
8132cb95e7bfSMark Fasheh goto out;
8133cb95e7bfSMark Fasheh }
8134cb95e7bfSMark Fasheh
8135e780b0d1SDavid Sterba sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
813631db9f7cSAlexander Block if (!sctx) {
813731db9f7cSAlexander Block ret = -ENOMEM;
813831db9f7cSAlexander Block goto out;
813931db9f7cSAlexander Block }
814031db9f7cSAlexander Block
814131db9f7cSAlexander Block INIT_LIST_HEAD(&sctx->new_refs);
814231db9f7cSAlexander Block INIT_LIST_HEAD(&sctx->deleted_refs);
814331db9f7cSAlexander Block
8144c48545deSFilipe Manana btrfs_lru_cache_init(&sctx->name_cache, SEND_MAX_NAME_CACHE_SIZE);
814590b90d4aSFilipe Manana btrfs_lru_cache_init(&sctx->backref_cache, SEND_MAX_BACKREF_CACHE_SIZE);
8146e8a7f49dSFilipe Manana btrfs_lru_cache_init(&sctx->dir_created_cache,
8147e8a7f49dSFilipe Manana SEND_MAX_DIR_CREATED_CACHE_SIZE);
81483e49363bSFilipe Manana /*
81493e49363bSFilipe Manana * This cache is periodically trimmed to a fixed size elsewhere, see
81503e49363bSFilipe Manana * cache_dir_utimes() and trim_dir_utimes_cache().
81513e49363bSFilipe Manana */
81523e49363bSFilipe Manana btrfs_lru_cache_init(&sctx->dir_utimes_cache, 0);
815366d04209SFilipe Manana
8154d307d2f3SFilipe Manana sctx->pending_dir_moves = RB_ROOT;
8155d307d2f3SFilipe Manana sctx->waiting_dir_moves = RB_ROOT;
8156d307d2f3SFilipe Manana sctx->orphan_dirs = RB_ROOT;
8157d307d2f3SFilipe Manana sctx->rbtree_new_refs = RB_ROOT;
8158d307d2f3SFilipe Manana sctx->rbtree_deleted_refs = RB_ROOT;
8159d307d2f3SFilipe Manana
8160cb95e7bfSMark Fasheh sctx->flags = arg->flags;
8161cb95e7bfSMark Fasheh
8162e77fbf99SDavid Sterba if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
8163e77fbf99SDavid Sterba if (arg->version > BTRFS_SEND_STREAM_VERSION) {
8164e77fbf99SDavid Sterba ret = -EPROTO;
8165e77fbf99SDavid Sterba goto out;
8166e77fbf99SDavid Sterba }
8167e77fbf99SDavid Sterba /* Zero means "use the highest version" */
8168e77fbf99SDavid Sterba sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
8169e77fbf99SDavid Sterba } else {
8170e77fbf99SDavid Sterba sctx->proto = 1;
8171e77fbf99SDavid Sterba }
8172d6815592SOmar Sandoval if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) {
8173d6815592SOmar Sandoval ret = -EINVAL;
8174d6815592SOmar Sandoval goto out;
8175d6815592SOmar Sandoval }
8176e77fbf99SDavid Sterba
817731db9f7cSAlexander Block sctx->send_filp = fget(arg->send_fd);
8178da2dbbb7SJann Horn if (!sctx->send_filp || !(sctx->send_filp->f_mode & FMODE_WRITE)) {
8179ecc7ada7STsutomu Itoh ret = -EBADF;
818031db9f7cSAlexander Block goto out;
818131db9f7cSAlexander Block }
818231db9f7cSAlexander Block
818331db9f7cSAlexander Block sctx->send_root = send_root;
8184521e0546SDavid Sterba /*
8185521e0546SDavid Sterba * Unlikely but possible, if the subvolume is marked for deletion but
8186521e0546SDavid Sterba * is slow to remove the directory entry, send can still be started
8187521e0546SDavid Sterba */
8188521e0546SDavid Sterba if (btrfs_root_dead(sctx->send_root)) {
8189521e0546SDavid Sterba ret = -EPERM;
8190521e0546SDavid Sterba goto out;
8191521e0546SDavid Sterba }
8192521e0546SDavid Sterba
819331db9f7cSAlexander Block sctx->clone_roots_cnt = arg->clone_sources_count;
819431db9f7cSAlexander Block
8195a4b333f2SOmar Sandoval if (sctx->proto >= 2) {
8196a4b333f2SOmar Sandoval u32 send_buf_num_pages;
8197356bbbb6SOmar Sandoval
8198875c627cSWang Yugui sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V2;
8199a4b333f2SOmar Sandoval sctx->send_buf = vmalloc(sctx->send_max_size);
8200a4b333f2SOmar Sandoval if (!sctx->send_buf) {
8201a4b333f2SOmar Sandoval ret = -ENOMEM;
8202a4b333f2SOmar Sandoval goto out;
8203a4b333f2SOmar Sandoval }
8204a4b333f2SOmar Sandoval send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT;
8205a4b333f2SOmar Sandoval sctx->send_buf_pages = kcalloc(send_buf_num_pages,
8206a4b333f2SOmar Sandoval sizeof(*sctx->send_buf_pages),
8207a4b333f2SOmar Sandoval GFP_KERNEL);
8208a4b333f2SOmar Sandoval if (!sctx->send_buf_pages) {
8209a4b333f2SOmar Sandoval ret = -ENOMEM;
8210a4b333f2SOmar Sandoval goto out;
8211a4b333f2SOmar Sandoval }
8212a4b333f2SOmar Sandoval for (i = 0; i < send_buf_num_pages; i++) {
8213a4b333f2SOmar Sandoval sctx->send_buf_pages[i] =
8214a4b333f2SOmar Sandoval vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT));
8215a4b333f2SOmar Sandoval }
8216a4b333f2SOmar Sandoval } else {
8217a4b333f2SOmar Sandoval sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1;
8218752ade68SMichal Hocko sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
8219a4b333f2SOmar Sandoval }
822031db9f7cSAlexander Block if (!sctx->send_buf) {
822131db9f7cSAlexander Block ret = -ENOMEM;
822231db9f7cSAlexander Block goto out;
822331db9f7cSAlexander Block }
822431db9f7cSAlexander Block
82250b76a4f7SDmitry Antipov sctx->clone_roots = kvcalloc(arg->clone_sources_count + 1,
82260b76a4f7SDmitry Antipov sizeof(*sctx->clone_roots),
8227bae12df9SDenis Efremov GFP_KERNEL);
822831db9f7cSAlexander Block if (!sctx->clone_roots) {
822931db9f7cSAlexander Block ret = -ENOMEM;
823031db9f7cSAlexander Block goto out;
823131db9f7cSAlexander Block }
823231db9f7cSAlexander Block
8233bae12df9SDenis Efremov alloc_size = array_size(sizeof(*arg->clone_sources),
8234bae12df9SDenis Efremov arg->clone_sources_count);
8235e55d1153SDavid Sterba
823631db9f7cSAlexander Block if (arg->clone_sources_count) {
8237752ade68SMichal Hocko clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
823831db9f7cSAlexander Block if (!clone_sources_tmp) {
823931db9f7cSAlexander Block ret = -ENOMEM;
824031db9f7cSAlexander Block goto out;
824131db9f7cSAlexander Block }
824231db9f7cSAlexander Block
824331db9f7cSAlexander Block ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
8244e55d1153SDavid Sterba alloc_size);
824531db9f7cSAlexander Block if (ret) {
824631db9f7cSAlexander Block ret = -EFAULT;
824731db9f7cSAlexander Block goto out;
824831db9f7cSAlexander Block }
824931db9f7cSAlexander Block
825031db9f7cSAlexander Block for (i = 0; i < arg->clone_sources_count; i++) {
825156e9357aSDavid Sterba clone_root = btrfs_get_fs_root(fs_info,
825256e9357aSDavid Sterba clone_sources_tmp[i], true);
825331db9f7cSAlexander Block if (IS_ERR(clone_root)) {
825431db9f7cSAlexander Block ret = PTR_ERR(clone_root);
825531db9f7cSAlexander Block goto out;
825631db9f7cSAlexander Block }
82572c686537SDavid Sterba spin_lock(&clone_root->root_item_lock);
82585cc2b17eSFilipe Manana if (!btrfs_root_readonly(clone_root) ||
82595cc2b17eSFilipe Manana btrfs_root_dead(clone_root)) {
82602c686537SDavid Sterba spin_unlock(&clone_root->root_item_lock);
826100246528SJosef Bacik btrfs_put_root(clone_root);
82622c686537SDavid Sterba ret = -EPERM;
82632c686537SDavid Sterba goto out;
82642c686537SDavid Sterba }
826562d54f3aSFilipe Manana if (clone_root->dedupe_in_progress) {
826662d54f3aSFilipe Manana dedupe_in_progress_warn(clone_root);
826762d54f3aSFilipe Manana spin_unlock(&clone_root->root_item_lock);
826800246528SJosef Bacik btrfs_put_root(clone_root);
826962d54f3aSFilipe Manana ret = -EAGAIN;
827062d54f3aSFilipe Manana goto out;
827162d54f3aSFilipe Manana }
82722f1f465aSFilipe Manana clone_root->send_in_progress++;
82732c686537SDavid Sterba spin_unlock(&clone_root->root_item_lock);
827418f687d5SWang Shilong
827531db9f7cSAlexander Block sctx->clone_roots[i].root = clone_root;
82762f1f465aSFilipe Manana clone_sources_to_rollback = i + 1;
827731db9f7cSAlexander Block }
82782f91306aSDavid Sterba kvfree(clone_sources_tmp);
827931db9f7cSAlexander Block clone_sources_tmp = NULL;
828031db9f7cSAlexander Block }
828131db9f7cSAlexander Block
828231db9f7cSAlexander Block if (arg->parent_root) {
828356e9357aSDavid Sterba sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
828456e9357aSDavid Sterba true);
8285b1b19596SStefan Behrens if (IS_ERR(sctx->parent_root)) {
8286b1b19596SStefan Behrens ret = PTR_ERR(sctx->parent_root);
828731db9f7cSAlexander Block goto out;
828831db9f7cSAlexander Block }
828918f687d5SWang Shilong
82902c686537SDavid Sterba spin_lock(&sctx->parent_root->root_item_lock);
82912c686537SDavid Sterba sctx->parent_root->send_in_progress++;
8292521e0546SDavid Sterba if (!btrfs_root_readonly(sctx->parent_root) ||
8293521e0546SDavid Sterba btrfs_root_dead(sctx->parent_root)) {
82942c686537SDavid Sterba spin_unlock(&sctx->parent_root->root_item_lock);
82952c686537SDavid Sterba ret = -EPERM;
82962c686537SDavid Sterba goto out;
82972c686537SDavid Sterba }
829862d54f3aSFilipe Manana if (sctx->parent_root->dedupe_in_progress) {
829962d54f3aSFilipe Manana dedupe_in_progress_warn(sctx->parent_root);
830062d54f3aSFilipe Manana spin_unlock(&sctx->parent_root->root_item_lock);
830162d54f3aSFilipe Manana ret = -EAGAIN;
830262d54f3aSFilipe Manana goto out;
830362d54f3aSFilipe Manana }
83042c686537SDavid Sterba spin_unlock(&sctx->parent_root->root_item_lock);
830531db9f7cSAlexander Block }
830631db9f7cSAlexander Block
830731db9f7cSAlexander Block /*
830831db9f7cSAlexander Block * Clones from send_root are allowed, but only if the clone source
830931db9f7cSAlexander Block * is behind the current send position. This is checked while searching
831031db9f7cSAlexander Block * for possible clone sources.
831131db9f7cSAlexander Block */
83126f9a3da5SJosef Bacik sctx->clone_roots[sctx->clone_roots_cnt++].root =
831300246528SJosef Bacik btrfs_grab_root(sctx->send_root);
831431db9f7cSAlexander Block
831531db9f7cSAlexander Block /* We do a bsearch later */
831631db9f7cSAlexander Block sort(sctx->clone_roots, sctx->clone_roots_cnt,
831731db9f7cSAlexander Block sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
831831db9f7cSAlexander Block NULL);
8319896c14f9SWang Shilong sort_clone_roots = 1;
832031db9f7cSAlexander Block
83219f89d5deSFilipe Manana ret = flush_delalloc_roots(sctx);
83229f89d5deSFilipe Manana if (ret)
83239f89d5deSFilipe Manana goto out;
83249f89d5deSFilipe Manana
8325e5fa8f86SFilipe Manana ret = ensure_commit_roots_uptodate(sctx);
8326e5fa8f86SFilipe Manana if (ret)
8327e5fa8f86SFilipe Manana goto out;
8328e5fa8f86SFilipe Manana
832931db9f7cSAlexander Block ret = send_subvol(sctx);
833031db9f7cSAlexander Block if (ret < 0)
833131db9f7cSAlexander Block goto out;
833231db9f7cSAlexander Block
83333e49363bSFilipe Manana btrfs_lru_cache_for_each_entry_safe(&sctx->dir_utimes_cache, entry, tmp) {
83343e49363bSFilipe Manana ret = send_utimes(sctx, entry->key, entry->gen);
83353e49363bSFilipe Manana if (ret < 0)
83363e49363bSFilipe Manana goto out;
83373e49363bSFilipe Manana btrfs_lru_cache_remove(&sctx->dir_utimes_cache, entry);
83383e49363bSFilipe Manana }
83393e49363bSFilipe Manana
8340c2c71324SStefan Behrens if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
834131db9f7cSAlexander Block ret = begin_cmd(sctx, BTRFS_SEND_C_END);
834231db9f7cSAlexander Block if (ret < 0)
834331db9f7cSAlexander Block goto out;
834431db9f7cSAlexander Block ret = send_cmd(sctx);
834531db9f7cSAlexander Block if (ret < 0)
834631db9f7cSAlexander Block goto out;
8347c2c71324SStefan Behrens }
834831db9f7cSAlexander Block
834931db9f7cSAlexander Block out:
83509f03740aSFilipe David Borba Manana WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
83519f03740aSFilipe David Borba Manana while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
83529f03740aSFilipe David Borba Manana struct rb_node *n;
83539f03740aSFilipe David Borba Manana struct pending_dir_move *pm;
83549f03740aSFilipe David Borba Manana
83559f03740aSFilipe David Borba Manana n = rb_first(&sctx->pending_dir_moves);
83569f03740aSFilipe David Borba Manana pm = rb_entry(n, struct pending_dir_move, node);
83579f03740aSFilipe David Borba Manana while (!list_empty(&pm->list)) {
83589f03740aSFilipe David Borba Manana struct pending_dir_move *pm2;
83599f03740aSFilipe David Borba Manana
83609f03740aSFilipe David Borba Manana pm2 = list_first_entry(&pm->list,
83619f03740aSFilipe David Borba Manana struct pending_dir_move, list);
83629f03740aSFilipe David Borba Manana free_pending_move(sctx, pm2);
83639f03740aSFilipe David Borba Manana }
83649f03740aSFilipe David Borba Manana free_pending_move(sctx, pm);
83659f03740aSFilipe David Borba Manana }
83669f03740aSFilipe David Borba Manana
83679f03740aSFilipe David Borba Manana WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
83689f03740aSFilipe David Borba Manana while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
83699f03740aSFilipe David Borba Manana struct rb_node *n;
83709f03740aSFilipe David Borba Manana struct waiting_dir_move *dm;
83719f03740aSFilipe David Borba Manana
83729f03740aSFilipe David Borba Manana n = rb_first(&sctx->waiting_dir_moves);
83739f03740aSFilipe David Borba Manana dm = rb_entry(n, struct waiting_dir_move, node);
83749f03740aSFilipe David Borba Manana rb_erase(&dm->node, &sctx->waiting_dir_moves);
83759f03740aSFilipe David Borba Manana kfree(dm);
83769f03740aSFilipe David Borba Manana }
83779f03740aSFilipe David Borba Manana
83789dc44214SFilipe Manana WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
83799dc44214SFilipe Manana while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
83809dc44214SFilipe Manana struct rb_node *n;
83819dc44214SFilipe Manana struct orphan_dir_info *odi;
83829dc44214SFilipe Manana
83839dc44214SFilipe Manana n = rb_first(&sctx->orphan_dirs);
83849dc44214SFilipe Manana odi = rb_entry(n, struct orphan_dir_info, node);
83859dc44214SFilipe Manana free_orphan_dir_info(sctx, odi);
83869dc44214SFilipe Manana }
83879dc44214SFilipe Manana
8388896c14f9SWang Shilong if (sort_clone_roots) {
83896f9a3da5SJosef Bacik for (i = 0; i < sctx->clone_roots_cnt; i++) {
8390896c14f9SWang Shilong btrfs_root_dec_send_in_progress(
8391896c14f9SWang Shilong sctx->clone_roots[i].root);
839200246528SJosef Bacik btrfs_put_root(sctx->clone_roots[i].root);
83936f9a3da5SJosef Bacik }
8394896c14f9SWang Shilong } else {
83956f9a3da5SJosef Bacik for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
8396896c14f9SWang Shilong btrfs_root_dec_send_in_progress(
8397896c14f9SWang Shilong sctx->clone_roots[i].root);
839800246528SJosef Bacik btrfs_put_root(sctx->clone_roots[i].root);
83996f9a3da5SJosef Bacik }
8400896c14f9SWang Shilong
8401896c14f9SWang Shilong btrfs_root_dec_send_in_progress(send_root);
8402896c14f9SWang Shilong }
84036f9a3da5SJosef Bacik if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
840466ef7d65SDavid Sterba btrfs_root_dec_send_in_progress(sctx->parent_root);
840500246528SJosef Bacik btrfs_put_root(sctx->parent_root);
84066f9a3da5SJosef Bacik }
84072c686537SDavid Sterba
84082f91306aSDavid Sterba kvfree(clone_sources_tmp);
840931db9f7cSAlexander Block
841031db9f7cSAlexander Block if (sctx) {
841131db9f7cSAlexander Block if (sctx->send_filp)
841231db9f7cSAlexander Block fput(sctx->send_filp);
841331db9f7cSAlexander Block
8414c03d01f3SDavid Sterba kvfree(sctx->clone_roots);
8415a4b333f2SOmar Sandoval kfree(sctx->send_buf_pages);
84166ff48ce0SDavid Sterba kvfree(sctx->send_buf);
841738622010SBoris Burkov kvfree(sctx->verity_descriptor);
841831db9f7cSAlexander Block
8419152555b3SFilipe Manana close_current_inode(sctx);
8420521b6803SFilipe Manana
8421c48545deSFilipe Manana btrfs_lru_cache_clear(&sctx->name_cache);
842290b90d4aSFilipe Manana btrfs_lru_cache_clear(&sctx->backref_cache);
8423e8a7f49dSFilipe Manana btrfs_lru_cache_clear(&sctx->dir_created_cache);
84243e49363bSFilipe Manana btrfs_lru_cache_clear(&sctx->dir_utimes_cache);
842566d04209SFilipe Manana
842631db9f7cSAlexander Block kfree(sctx);
842731db9f7cSAlexander Block }
842831db9f7cSAlexander Block
842931db9f7cSAlexander Block return ret;
843031db9f7cSAlexander Block }
8431