| // SPDX-License-Identifier: GPL-2.0 |
| /* |
| * Copyright (C) 2012 Alexander Block. All rights reserved. |
| */ |
| |
| #include <linux/bsearch.h> |
| #include <linux/fs.h> |
| #include <linux/file.h> |
| #include <linux/sort.h> |
| #include <linux/mount.h> |
| #include <linux/xattr.h> |
| #include <linux/posix_acl_xattr.h> |
| #include <linux/radix-tree.h> |
| #include <linux/vmalloc.h> |
| #include <linux/string.h> |
| #include <linux/compat.h> |
| #include <linux/crc32c.h> |
| |
| #include "send.h" |
| #include "backref.h" |
| #include "locking.h" |
| #include "disk-io.h" |
| #include "btrfs_inode.h" |
| #include "transaction.h" |
| #include "compression.h" |
| |
| /* |
| * Maximum number of references an extent can have in order for us to attempt to |
| * issue clone operations instead of write operations. This currently exists to |
| * avoid hitting limitations of the backreference walking code (taking a lot of |
| * time and using too much memory for extents with large number of references). |
| */ |
| #define SEND_MAX_EXTENT_REFS 64 |
| |
| /* |
| * A fs_path is a helper to dynamically build path names with unknown size. |
| * It reallocates the internal buffer on demand. |
| * It allows fast adding of path elements on the right side (normal path) and |
| * fast adding to the left side (reversed path). A reversed path can also be |
| * unreversed if needed. |
| */ |
| struct fs_path { |
| union { |
| struct { |
| char *start; |
| char *end; |
| |
| char *buf; |
| unsigned short buf_len:15; |
| unsigned short reversed:1; |
| char inline_buf[]; |
| }; |
| /* |
| * Average path length does not exceed 200 bytes, we'll have |
| * better packing in the slab and higher chance to satisfy |
| * a allocation later during send. |
| */ |
| char pad[256]; |
| }; |
| }; |
| #define FS_PATH_INLINE_SIZE \ |
| (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) |
| |
| |
| /* reused for each extent */ |
| struct clone_root { |
| struct btrfs_root *root; |
| u64 ino; |
| u64 offset; |
| |
| u64 found_refs; |
| }; |
| |
| #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 |
| #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) |
| |
| struct send_ctx { |
| struct file *send_filp; |
| loff_t send_off; |
| char *send_buf; |
| u32 send_size; |
| u32 send_max_size; |
| u64 total_send_size; |
| u64 cmd_send_size[BTRFS_SEND_C_MAX + 1]; |
| u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
| |
| struct btrfs_root *send_root; |
| struct btrfs_root *parent_root; |
| struct clone_root *clone_roots; |
| int clone_roots_cnt; |
| |
| /* current state of the compare_tree call */ |
| struct btrfs_path *left_path; |
| struct btrfs_path *right_path; |
| struct btrfs_key *cmp_key; |
| |
| /* |
| * infos of the currently processed inode. In case of deleted inodes, |
| * these are the values from the deleted inode. |
| */ |
| u64 cur_ino; |
| u64 cur_inode_gen; |
| int cur_inode_new; |
| int cur_inode_new_gen; |
| int cur_inode_deleted; |
| u64 cur_inode_size; |
| u64 cur_inode_mode; |
| u64 cur_inode_rdev; |
| u64 cur_inode_last_extent; |
| u64 cur_inode_next_write_offset; |
| bool ignore_cur_inode; |
| |
| u64 send_progress; |
| |
| struct list_head new_refs; |
| struct list_head deleted_refs; |
| |
| struct radix_tree_root name_cache; |
| struct list_head name_cache_list; |
| int name_cache_size; |
| |
| struct file_ra_state ra; |
| |
| char *read_buf; |
| |
| /* |
| * We process inodes by their increasing order, so if before an |
| * incremental send we reverse the parent/child relationship of |
| * directories such that a directory with a lower inode number was |
| * the parent of a directory with a higher inode number, and the one |
| * becoming the new parent got renamed too, we can't rename/move the |
| * directory with lower inode number when we finish processing it - we |
| * must process the directory with higher inode number first, then |
| * rename/move it and then rename/move the directory with lower inode |
| * number. Example follows. |
| * |
| * Tree state when the first send was performed: |
| * |
| * . |
| * |-- a (ino 257) |
| * |-- b (ino 258) |
| * | |
| * | |
| * |-- c (ino 259) |
| * | |-- d (ino 260) |
| * | |
| * |-- c2 (ino 261) |
| * |
| * Tree state when the second (incremental) send is performed: |
| * |
| * . |
| * |-- a (ino 257) |
| * |-- b (ino 258) |
| * |-- c2 (ino 261) |
| * |-- d2 (ino 260) |
| * |-- cc (ino 259) |
| * |
| * The sequence of steps that lead to the second state was: |
| * |
| * mv /a/b/c/d /a/b/c2/d2 |
| * mv /a/b/c /a/b/c2/d2/cc |
| * |
| * "c" has lower inode number, but we can't move it (2nd mv operation) |
| * before we move "d", which has higher inode number. |
| * |
| * So we just memorize which move/rename operations must be performed |
| * later when their respective parent is processed and moved/renamed. |
| */ |
| |
| /* Indexed by parent directory inode number. */ |
| struct rb_root pending_dir_moves; |
| |
| /* |
| * Reverse index, indexed by the inode number of a directory that |
| * is waiting for the move/rename of its immediate parent before its |
| * own move/rename can be performed. |
| */ |
| struct rb_root waiting_dir_moves; |
| |
| /* |
| * A directory that is going to be rm'ed might have a child directory |
| * which is in the pending directory moves index above. In this case, |
| * the directory can only be removed after the move/rename of its child |
| * is performed. Example: |
| * |
| * Parent snapshot: |
| * |
| * . (ino 256) |
| * |-- a/ (ino 257) |
| * |-- b/ (ino 258) |
| * |-- c/ (ino 259) |
| * | |-- x/ (ino 260) |
| * | |
| * |-- y/ (ino 261) |
| * |
| * Send snapshot: |
| * |
| * . (ino 256) |
| * |-- a/ (ino 257) |
| * |-- b/ (ino 258) |
| * |-- YY/ (ino 261) |
| * |-- x/ (ino 260) |
| * |
| * Sequence of steps that lead to the send snapshot: |
| * rm -f /a/b/c/foo.txt |
| * mv /a/b/y /a/b/YY |
| * mv /a/b/c/x /a/b/YY |
| * rmdir /a/b/c |
| * |
| * When the child is processed, its move/rename is delayed until its |
| * parent is processed (as explained above), but all other operations |
| * like update utimes, chown, chgrp, etc, are performed and the paths |
| * that it uses for those operations must use the orphanized name of |
| * its parent (the directory we're going to rm later), so we need to |
| * memorize that name. |
| * |
| * Indexed by the inode number of the directory to be deleted. |
| */ |
| struct rb_root orphan_dirs; |
| }; |
| |
| struct pending_dir_move { |
| struct rb_node node; |
| struct list_head list; |
| u64 parent_ino; |
| u64 ino; |
| u64 gen; |
| struct list_head update_refs; |
| }; |
| |
| struct waiting_dir_move { |
| struct rb_node node; |
| u64 ino; |
| /* |
| * There might be some directory that could not be removed because it |
| * was waiting for this directory inode to be moved first. Therefore |
| * after this directory is moved, we can try to rmdir the ino rmdir_ino. |
| */ |
| u64 rmdir_ino; |
| bool orphanized; |
| }; |
| |
| struct orphan_dir_info { |
| struct rb_node node; |
| u64 ino; |
| u64 gen; |
| u64 last_dir_index_offset; |
| }; |
| |
| struct name_cache_entry { |
| struct list_head list; |
| /* |
| * radix_tree has only 32bit entries but we need to handle 64bit inums. |
| * We use the lower 32bit of the 64bit inum to store it in the tree. If |
| * more then one inum would fall into the same entry, we use radix_list |
| * to store the additional entries. radix_list is also used to store |
| * entries where two entries have the same inum but different |
| * generations. |
| */ |
| struct list_head radix_list; |
| u64 ino; |
| u64 gen; |
| u64 parent_ino; |
| u64 parent_gen; |
| int ret; |
| int need_later_update; |
| int name_len; |
| char name[]; |
| }; |
| |
| __cold |
| static void inconsistent_snapshot_error(struct send_ctx *sctx, |
| enum btrfs_compare_tree_result result, |
| const char *what) |
| { |
| const char *result_string; |
| |
| switch (result) { |
| case BTRFS_COMPARE_TREE_NEW: |
| result_string = "new"; |
| break; |
| case BTRFS_COMPARE_TREE_DELETED: |
| result_string = "deleted"; |
| break; |
| case BTRFS_COMPARE_TREE_CHANGED: |
| result_string = "updated"; |
| break; |
| case BTRFS_COMPARE_TREE_SAME: |
| ASSERT(0); |
| result_string = "unchanged"; |
| break; |
| default: |
| ASSERT(0); |
| result_string = "unexpected"; |
| } |
| |
| btrfs_err(sctx->send_root->fs_info, |
| "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu", |
| result_string, what, sctx->cmp_key->objectid, |
| sctx->send_root->root_key.objectid, |
| (sctx->parent_root ? |
| sctx->parent_root->root_key.objectid : 0)); |
| } |
| |
| static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
| |
| static struct waiting_dir_move * |
| get_waiting_dir_move(struct send_ctx *sctx, u64 ino); |
| |
| static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino); |
| |
| static int need_send_hole(struct send_ctx *sctx) |
| { |
| return (sctx->parent_root && !sctx->cur_inode_new && |
| !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && |
| S_ISREG(sctx->cur_inode_mode)); |
| } |
| |
| static void fs_path_reset(struct fs_path *p) |
| { |
| if (p->reversed) { |
| p->start = p->buf + p->buf_len - 1; |
| p->end = p->start; |
| *p->start = 0; |
| } else { |
| p->start = p->buf; |
| p->end = p->start; |
| *p->start = 0; |
| } |
| } |
| |
| static struct fs_path *fs_path_alloc(void) |
| { |
| struct fs_path *p; |
| |
| p = kmalloc(sizeof(*p), GFP_KERNEL); |
| if (!p) |
| return NULL; |
| p->reversed = 0; |
| p->buf = p->inline_buf; |
| p->buf_len = FS_PATH_INLINE_SIZE; |
| fs_path_reset(p); |
| return p; |
| } |
| |
| static struct fs_path *fs_path_alloc_reversed(void) |
| { |
| struct fs_path *p; |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return NULL; |
| p->reversed = 1; |
| fs_path_reset(p); |
| return p; |
| } |
| |
| static void fs_path_free(struct fs_path *p) |
| { |
| if (!p) |
| return; |
| if (p->buf != p->inline_buf) |
| kfree(p->buf); |
| kfree(p); |
| } |
| |
| static int fs_path_len(struct fs_path *p) |
| { |
| return p->end - p->start; |
| } |
| |
| static int fs_path_ensure_buf(struct fs_path *p, int len) |
| { |
| char *tmp_buf; |
| int path_len; |
| int old_buf_len; |
| |
| len++; |
| |
| if (p->buf_len >= len) |
| return 0; |
| |
| if (len > PATH_MAX) { |
| WARN_ON(1); |
| return -ENOMEM; |
| } |
| |
| path_len = p->end - p->start; |
| old_buf_len = p->buf_len; |
| |
| /* |
| * First time the inline_buf does not suffice |
| */ |
| if (p->buf == p->inline_buf) { |
| tmp_buf = kmalloc(len, GFP_KERNEL); |
| if (tmp_buf) |
| memcpy(tmp_buf, p->buf, old_buf_len); |
| } else { |
| tmp_buf = krealloc(p->buf, len, GFP_KERNEL); |
| } |
| if (!tmp_buf) |
| return -ENOMEM; |
| p->buf = tmp_buf; |
| /* |
| * The real size of the buffer is bigger, this will let the fast path |
| * happen most of the time |
| */ |
| p->buf_len = ksize(p->buf); |
| |
| if (p->reversed) { |
| tmp_buf = p->buf + old_buf_len - path_len - 1; |
| p->end = p->buf + p->buf_len - 1; |
| p->start = p->end - path_len; |
| memmove(p->start, tmp_buf, path_len + 1); |
| } else { |
| p->start = p->buf; |
| p->end = p->start + path_len; |
| } |
| return 0; |
| } |
| |
| static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
| char **prepared) |
| { |
| int ret; |
| int new_len; |
| |
| new_len = p->end - p->start + name_len; |
| if (p->start != p->end) |
| new_len++; |
| ret = fs_path_ensure_buf(p, new_len); |
| if (ret < 0) |
| goto out; |
| |
| if (p->reversed) { |
| if (p->start != p->end) |
| *--p->start = '/'; |
| p->start -= name_len; |
| *prepared = p->start; |
| } else { |
| if (p->start != p->end) |
| *p->end++ = '/'; |
| *prepared = p->end; |
| p->end += name_len; |
| *p->end = 0; |
| } |
| |
| out: |
| return ret; |
| } |
| |
| static int fs_path_add(struct fs_path *p, const char *name, int name_len) |
| { |
| int ret; |
| char *prepared; |
| |
| ret = fs_path_prepare_for_add(p, name_len, &prepared); |
| if (ret < 0) |
| goto out; |
| memcpy(prepared, name, name_len); |
| |
| out: |
| return ret; |
| } |
| |
| static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) |
| { |
| int ret; |
| char *prepared; |
| |
| ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
| if (ret < 0) |
| goto out; |
| memcpy(prepared, p2->start, p2->end - p2->start); |
| |
| out: |
| return ret; |
| } |
| |
| static int fs_path_add_from_extent_buffer(struct fs_path *p, |
| struct extent_buffer *eb, |
| unsigned long off, int len) |
| { |
| int ret; |
| char *prepared; |
| |
| ret = fs_path_prepare_for_add(p, len, &prepared); |
| if (ret < 0) |
| goto out; |
| |
| read_extent_buffer(eb, prepared, off, len); |
| |
| out: |
| return ret; |
| } |
| |
| static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
| { |
| int ret; |
| |
| p->reversed = from->reversed; |
| fs_path_reset(p); |
| |
| ret = fs_path_add_path(p, from); |
| |
| return ret; |
| } |
| |
| |
| static void fs_path_unreverse(struct fs_path *p) |
| { |
| char *tmp; |
| int len; |
| |
| if (!p->reversed) |
| return; |
| |
| tmp = p->start; |
| len = p->end - p->start; |
| p->start = p->buf; |
| p->end = p->start + len; |
| memmove(p->start, tmp, len + 1); |
| p->reversed = 0; |
| } |
| |
| static struct btrfs_path *alloc_path_for_send(void) |
| { |
| struct btrfs_path *path; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return NULL; |
| path->search_commit_root = 1; |
| path->skip_locking = 1; |
| path->need_commit_sem = 1; |
| return path; |
| } |
| |
| static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off) |
| { |
| int ret; |
| u32 pos = 0; |
| |
| while (pos < len) { |
| ret = kernel_write(filp, buf + pos, len - pos, off); |
| /* TODO handle that correctly */ |
| /*if (ret == -ERESTARTSYS) { |
| continue; |
| }*/ |
| if (ret < 0) |
| return ret; |
| if (ret == 0) { |
| return -EIO; |
| } |
| pos += ret; |
| } |
| |
| return 0; |
| } |
| |
| static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) |
| { |
| struct btrfs_tlv_header *hdr; |
| int total_len = sizeof(*hdr) + len; |
| int left = sctx->send_max_size - sctx->send_size; |
| |
| if (unlikely(left < total_len)) |
| return -EOVERFLOW; |
| |
| hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); |
| hdr->tlv_type = cpu_to_le16(attr); |
| hdr->tlv_len = cpu_to_le16(len); |
| memcpy(hdr + 1, data, len); |
| sctx->send_size += total_len; |
| |
| return 0; |
| } |
| |
| #define TLV_PUT_DEFINE_INT(bits) \ |
| static int tlv_put_u##bits(struct send_ctx *sctx, \ |
| u##bits attr, u##bits value) \ |
| { \ |
| __le##bits __tmp = cpu_to_le##bits(value); \ |
| return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ |
| } |
| |
| TLV_PUT_DEFINE_INT(64) |
| |
| static int tlv_put_string(struct send_ctx *sctx, u16 attr, |
| const char *str, int len) |
| { |
| if (len == -1) |
| len = strlen(str); |
| return tlv_put(sctx, attr, str, len); |
| } |
| |
| static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, |
| const u8 *uuid) |
| { |
| return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); |
| } |
| |
| static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
| struct extent_buffer *eb, |
| struct btrfs_timespec *ts) |
| { |
| struct btrfs_timespec bts; |
| read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); |
| return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| } |
| |
| |
| #define TLV_PUT(sctx, attrtype, data, attrlen) \ |
| do { \ |
| ret = tlv_put(sctx, attrtype, data, attrlen); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while (0) |
| |
| #define TLV_PUT_INT(sctx, attrtype, bits, value) \ |
| do { \ |
| ret = tlv_put_u##bits(sctx, attrtype, value); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while (0) |
| |
| #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) |
| #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) |
| #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) |
| #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) |
| #define TLV_PUT_STRING(sctx, attrtype, str, len) \ |
| do { \ |
| ret = tlv_put_string(sctx, attrtype, str, len); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while (0) |
| #define TLV_PUT_PATH(sctx, attrtype, p) \ |
| do { \ |
| ret = tlv_put_string(sctx, attrtype, p->start, \ |
| p->end - p->start); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while(0) |
| #define TLV_PUT_UUID(sctx, attrtype, uuid) \ |
| do { \ |
| ret = tlv_put_uuid(sctx, attrtype, uuid); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while (0) |
| #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
| do { \ |
| ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ |
| if (ret < 0) \ |
| goto tlv_put_failure; \ |
| } while (0) |
| |
| static int send_header(struct send_ctx *sctx) |
| { |
| struct btrfs_stream_header hdr; |
| |
| strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); |
| hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); |
| |
| return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
| &sctx->send_off); |
| } |
| |
| /* |
| * For each command/item we want to send to userspace, we call this function. |
| */ |
| static int begin_cmd(struct send_ctx *sctx, int cmd) |
| { |
| struct btrfs_cmd_header *hdr; |
| |
| if (WARN_ON(!sctx->send_buf)) |
| return -EINVAL; |
| |
| BUG_ON(sctx->send_size); |
| |
| sctx->send_size += sizeof(*hdr); |
| hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| hdr->cmd = cpu_to_le16(cmd); |
| |
| return 0; |
| } |
| |
| static int send_cmd(struct send_ctx *sctx) |
| { |
| int ret; |
| struct btrfs_cmd_header *hdr; |
| u32 crc; |
| |
| hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); |
| hdr->crc = 0; |
| |
| crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
| hdr->crc = cpu_to_le32(crc); |
| |
| ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
| &sctx->send_off); |
| |
| sctx->total_send_size += sctx->send_size; |
| sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; |
| sctx->send_size = 0; |
| |
| return ret; |
| } |
| |
| /* |
| * Sends a move instruction to user space |
| */ |
| static int send_rename(struct send_ctx *sctx, |
| struct fs_path *from, struct fs_path *to) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret; |
| |
| btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start); |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); |
| if (ret < 0) |
| goto out; |
| |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| return ret; |
| } |
| |
| /* |
| * Sends a link instruction to user space |
| */ |
| static int send_link(struct send_ctx *sctx, |
| struct fs_path *path, struct fs_path *lnk) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret; |
| |
| btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start); |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); |
| if (ret < 0) |
| goto out; |
| |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| return ret; |
| } |
| |
| /* |
| * Sends an unlink instruction to user space |
| */ |
| static int send_unlink(struct send_ctx *sctx, struct fs_path *path) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret; |
| |
| btrfs_debug(fs_info, "send_unlink %s", path->start); |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); |
| if (ret < 0) |
| goto out; |
| |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| return ret; |
| } |
| |
| /* |
| * Sends a rmdir instruction to user space |
| */ |
| static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret; |
| |
| btrfs_debug(fs_info, "send_rmdir %s", path->start); |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); |
| if (ret < 0) |
| goto out; |
| |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| return ret; |
| } |
| |
| /* |
| * Helper function to retrieve some fields from an inode item. |
| */ |
| static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path, |
| u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid, |
| u64 *gid, u64 *rdev) |
| { |
| int ret; |
| struct btrfs_inode_item *ii; |
| struct btrfs_key key; |
| |
| key.objectid = ino; |
| key.type = BTRFS_INODE_ITEM_KEY; |
| key.offset = 0; |
| ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| if (ret) { |
| if (ret > 0) |
| ret = -ENOENT; |
| return ret; |
| } |
| |
| ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| struct btrfs_inode_item); |
| if (size) |
| *size = btrfs_inode_size(path->nodes[0], ii); |
| if (gen) |
| *gen = btrfs_inode_generation(path->nodes[0], ii); |
| if (mode) |
| *mode = btrfs_inode_mode(path->nodes[0], ii); |
| if (uid) |
| *uid = btrfs_inode_uid(path->nodes[0], ii); |
| if (gid) |
| *gid = btrfs_inode_gid(path->nodes[0], ii); |
| if (rdev) |
| *rdev = btrfs_inode_rdev(path->nodes[0], ii); |
| |
| return ret; |
| } |
| |
| static int get_inode_info(struct btrfs_root *root, |
| u64 ino, u64 *size, u64 *gen, |
| u64 *mode, u64 *uid, u64 *gid, |
| u64 *rdev) |
| { |
| struct btrfs_path *path; |
| int ret; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid, |
| rdev); |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, |
| struct fs_path *p, |
| void *ctx); |
| |
| /* |
| * Helper function to iterate the entries in ONE btrfs_inode_ref or |
| * btrfs_inode_extref. |
| * The iterate callback may return a non zero value to stop iteration. This can |
| * be a negative value for error codes or 1 to simply stop it. |
| * |
| * path must point to the INODE_REF or INODE_EXTREF when called. |
| */ |
| static int 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) |
| { |
| struct extent_buffer *eb = path->nodes[0]; |
| struct btrfs_item *item; |
| struct btrfs_inode_ref *iref; |
| struct btrfs_inode_extref *extref; |
| struct btrfs_path *tmp_path; |
| struct fs_path *p; |
| u32 cur = 0; |
| u32 total; |
| int slot = path->slots[0]; |
| u32 name_len; |
| char *start; |
| int ret = 0; |
| int num = 0; |
| int index; |
| u64 dir; |
| unsigned long name_off; |
| unsigned long elem_size; |
| unsigned long ptr; |
| |
| p = fs_path_alloc_reversed(); |
| if (!p) |
| return -ENOMEM; |
| |
| tmp_path = alloc_path_for_send(); |
| if (!tmp_path) { |
| fs_path_free(p); |
| return -ENOMEM; |
| } |
| |
| |
| if (found_key->type == BTRFS_INODE_REF_KEY) { |
| ptr = (unsigned long)btrfs_item_ptr(eb, slot, |
| struct btrfs_inode_ref); |
| item = btrfs_item_nr(slot); |
| total = btrfs_item_size(eb, item); |
| elem_size = sizeof(*iref); |
| } else { |
| ptr = btrfs_item_ptr_offset(eb, slot); |
| total = btrfs_item_size_nr(eb, slot); |
| elem_size = sizeof(*extref); |
| } |
| |
| while (cur < total) { |
| fs_path_reset(p); |
| |
| if (found_key->type == BTRFS_INODE_REF_KEY) { |
| iref = (struct btrfs_inode_ref *)(ptr + cur); |
| name_len = btrfs_inode_ref_name_len(eb, iref); |
| name_off = (unsigned long)(iref + 1); |
| index = btrfs_inode_ref_index(eb, iref); |
| dir = found_key->offset; |
| } else { |
| extref = (struct btrfs_inode_extref *)(ptr + cur); |
| name_len = btrfs_inode_extref_name_len(eb, extref); |
| name_off = (unsigned long)&extref->name; |
| index = btrfs_inode_extref_index(eb, extref); |
| dir = btrfs_inode_extref_parent(eb, extref); |
| } |
| |
| if (resolve) { |
| start = btrfs_ref_to_path(root, tmp_path, name_len, |
| name_off, eb, dir, |
| p->buf, p->buf_len); |
| if (IS_ERR(start)) { |
| ret = PTR_ERR(start); |
| goto out; |
| } |
| if (start < p->buf) { |
| /* overflow , try again with larger buffer */ |
| ret = fs_path_ensure_buf(p, |
| p->buf_len + p->buf - start); |
| if (ret < 0) |
| goto out; |
| start = btrfs_ref_to_path(root, tmp_path, |
| name_len, name_off, |
| eb, dir, |
| p->buf, p->buf_len); |
| if (IS_ERR(start)) { |
| ret = PTR_ERR(start); |
| goto out; |
| } |
| BUG_ON(start < p->buf); |
| } |
| p->start = start; |
| } else { |
| ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
| name_len); |
| if (ret < 0) |
| goto out; |
| } |
| |
| cur += elem_size + name_len; |
| ret = iterate(num, dir, index, p, ctx); |
| if (ret) |
| goto out; |
| num++; |
| } |
| |
| out: |
| btrfs_free_path(tmp_path); |
| fs_path_free(p); |
| return ret; |
| } |
| |
| typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, |
| const char *name, int name_len, |
| const char *data, int data_len, |
| u8 type, void *ctx); |
| |
| /* |
| * Helper function to iterate the entries in ONE btrfs_dir_item. |
| * The iterate callback may return a non zero value to stop iteration. This can |
| * be a negative value for error codes or 1 to simply stop it. |
| * |
| * path must point to the dir item when called. |
| */ |
| static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
| iterate_dir_item_t iterate, void *ctx) |
| { |
| int ret = 0; |
| struct extent_buffer *eb; |
| struct btrfs_item *item; |
| struct btrfs_dir_item *di; |
| struct btrfs_key di_key; |
| char *buf = NULL; |
| int buf_len; |
| u32 name_len; |
| u32 data_len; |
| u32 cur; |
| u32 len; |
| u32 total; |
| int slot; |
| int num; |
| u8 type; |
| |
| /* |
| * Start with a small buffer (1 page). If later we end up needing more |
| * space, which can happen for xattrs on a fs with a leaf size greater |
| * then the page size, attempt to increase the buffer. Typically xattr |
| * values are small. |
| */ |
| buf_len = PATH_MAX; |
| buf = kmalloc(buf_len, GFP_KERNEL); |
| if (!buf) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| eb = path->nodes[0]; |
| slot = path->slots[0]; |
| item = btrfs_item_nr(slot); |
| di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| cur = 0; |
| len = 0; |
| total = btrfs_item_size(eb, item); |
| |
| num = 0; |
| while (cur < total) { |
| name_len = btrfs_dir_name_len(eb, di); |
| data_len = btrfs_dir_data_len(eb, di); |
| type = btrfs_dir_type(eb, di); |
| btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| |
| if (type == BTRFS_FT_XATTR) { |
| if (name_len > XATTR_NAME_MAX) { |
| ret = -ENAMETOOLONG; |
| goto out; |
| } |
| if (name_len + data_len > |
| BTRFS_MAX_XATTR_SIZE(root->fs_info)) { |
| ret = -E2BIG; |
| goto out; |
| } |
| } else { |
| /* |
| * Path too long |
| */ |
| if (name_len + data_len > PATH_MAX) { |
| ret = -ENAMETOOLONG; |
| goto out; |
| } |
| } |
| |
| if (name_len + data_len > buf_len) { |
| buf_len = name_len + data_len; |
| if (is_vmalloc_addr(buf)) { |
| vfree(buf); |
| buf = NULL; |
| } else { |
| char *tmp = krealloc(buf, buf_len, |
| GFP_KERNEL | __GFP_NOWARN); |
| |
| if (!tmp) |
| kfree(buf); |
| buf = tmp; |
| } |
| if (!buf) { |
| buf = kvmalloc(buf_len, GFP_KERNEL); |
| if (!buf) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| } |
| } |
| |
| read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
| name_len + data_len); |
| |
| len = sizeof(*di) + name_len + data_len; |
| di = (struct btrfs_dir_item *)((char *)di + len); |
| cur += len; |
| |
| ret = iterate(num, &di_key, buf, name_len, buf + name_len, |
| data_len, type, ctx); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| ret = 0; |
| goto out; |
| } |
| |
| num++; |
| } |
| |
| out: |
| kvfree(buf); |
| return ret; |
| } |
| |
| static int __copy_first_ref(int num, u64 dir, int index, |
| struct fs_path *p, void *ctx) |
| { |
| int ret; |
| struct fs_path *pt = ctx; |
| |
| ret = fs_path_copy(pt, p); |
| if (ret < 0) |
| return ret; |
| |
| /* we want the first only */ |
| return 1; |
| } |
| |
| /* |
| * Retrieve the first path of an inode. If an inode has more then one |
| * ref/hardlink, this is ignored. |
| */ |
| static int get_inode_path(struct btrfs_root *root, |
| u64 ino, struct fs_path *path) |
| { |
| int ret; |
| struct btrfs_key key, found_key; |
| struct btrfs_path *p; |
| |
| p = alloc_path_for_send(); |
| if (!p) |
| return -ENOMEM; |
| |
| fs_path_reset(path); |
| |
| key.objectid = ino; |
| key.type = BTRFS_INODE_REF_KEY; |
| key.offset = 0; |
| |
| ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| ret = 1; |
| goto out; |
| } |
| btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); |
| if (found_key.objectid != ino || |
| (found_key.type != BTRFS_INODE_REF_KEY && |
| found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| ret = iterate_inode_ref(root, p, &found_key, 1, |
| __copy_first_ref, path); |
| if (ret < 0) |
| goto out; |
| ret = 0; |
| |
| out: |
| btrfs_free_path(p); |
| return ret; |
| } |
| |
| struct backref_ctx { |
| struct send_ctx *sctx; |
| |
| struct btrfs_path *path; |
| /* number of total found references */ |
| u64 found; |
| |
| /* |
| * used for clones found in send_root. clones found behind cur_objectid |
| * and cur_offset are not considered as allowed clones. |
| */ |
| u64 cur_objectid; |
| u64 cur_offset; |
| |
| /* may be truncated in case it's the last extent in a file */ |
| u64 extent_len; |
| |
| /* data offset in the file extent item */ |
| u64 data_offset; |
| |
| /* Just to check for bugs in backref resolving */ |
| int found_itself; |
| }; |
| |
| static int __clone_root_cmp_bsearch(const void *key, const void *elt) |
| { |
| u64 root = (u64)(uintptr_t)key; |
| struct clone_root *cr = (struct clone_root *)elt; |
| |
| if (root < cr->root->objectid) |
| return -1; |
| if (root > cr->root->objectid) |
| return 1; |
| return 0; |
| } |
| |
| static int __clone_root_cmp_sort(const void *e1, const void *e2) |
| { |
| struct clone_root *cr1 = (struct clone_root *)e1; |
| struct clone_root *cr2 = (struct clone_root *)e2; |
| |
| if (cr1->root->objectid < cr2->root->objectid) |
| return -1; |
| if (cr1->root->objectid > cr2->root->objectid) |
| return 1; |
| return 0; |
| } |
| |
| /* |
| * Called for every backref that is found for the current extent. |
| * Results are collected in sctx->clone_roots->ino/offset/found_refs |
| */ |
| static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) |
| { |
| struct backref_ctx *bctx = ctx_; |
| struct clone_root *found; |
| int ret; |
| u64 i_size; |
| |
| /* First check if the root is in the list of accepted clone sources */ |
| found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
| bctx->sctx->clone_roots_cnt, |
| sizeof(struct clone_root), |
| __clone_root_cmp_bsearch); |
| if (!found) |
| return 0; |
| |
| if (found->root == bctx->sctx->send_root && |
| ino == bctx->cur_objectid && |
| offset == bctx->cur_offset) { |
| bctx->found_itself = 1; |
| } |
| |
| /* |
| * There are inodes that have extents that lie behind its i_size. Don't |
| * accept clones from these extents. |
| */ |
| ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL, |
| NULL, NULL, NULL); |
| btrfs_release_path(bctx->path); |
| if (ret < 0) |
| return ret; |
| |
| if (offset + bctx->data_offset + bctx->extent_len > i_size) |
| return 0; |
| |
| /* |
| * Make sure we don't consider clones from send_root that are |
| * behind the current inode/offset. |
| */ |
| if (found->root == bctx->sctx->send_root) { |
| /* |
| * TODO for the moment we don't accept clones from the inode |
| * that is currently send. We may change this when |
| * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same |
| * file. |
| */ |
| if (ino >= bctx->cur_objectid) |
| return 0; |
| } |
| |
| bctx->found++; |
| found->found_refs++; |
| if (ino < found->ino) { |
| found->ino = ino; |
| found->offset = offset; |
| } else if (found->ino == ino) { |
| /* |
| * same extent found more then once in the same file. |
| */ |
| if (found->offset > offset + bctx->extent_len) |
| found->offset = offset; |
| } |
| |
| return 0; |
| } |
| |
| /* |
| * Given an inode, offset and extent item, it finds a good clone for a clone |
| * instruction. Returns -ENOENT when none could be found. The function makes |
| * sure that the returned clone is usable at the point where sending is at the |
| * moment. This means, that no clones are accepted which lie behind the current |
| * inode+offset. |
| * |
| * path must point to the extent item when called. |
| */ |
| static int find_extent_clone(struct send_ctx *sctx, |
| struct btrfs_path *path, |
| u64 ino, u64 data_offset, |
| u64 ino_size, |
| struct clone_root **found) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret; |
| int extent_type; |
| u64 logical; |
| u64 disk_byte; |
| u64 num_bytes; |
| u64 extent_item_pos; |
| u64 flags = 0; |
| struct btrfs_file_extent_item *fi; |
| struct extent_buffer *eb = path->nodes[0]; |
| struct backref_ctx *backref_ctx = NULL; |
| struct clone_root *cur_clone_root; |
| struct btrfs_key found_key; |
| struct btrfs_path *tmp_path; |
| struct btrfs_extent_item *ei; |
| int compressed; |
| u32 i; |
| |
| tmp_path = alloc_path_for_send(); |
| if (!tmp_path) |
| return -ENOMEM; |
| |
| /* We only use this path under the commit sem */ |
| tmp_path->need_commit_sem = 0; |
| |
| backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL); |
| if (!backref_ctx) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| backref_ctx->path = tmp_path; |
| |
| if (data_offset >= ino_size) { |
| /* |
| * There may be extents that lie behind the file's size. |
| * I at least had this in combination with snapshotting while |
| * writing large files. |
| */ |
| ret = 0; |
| goto out; |
| } |
| |
| fi = btrfs_item_ptr(eb, path->slots[0], |
| struct btrfs_file_extent_item); |
| extent_type = btrfs_file_extent_type(eb, fi); |
| if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| ret = -ENOENT; |
| goto out; |
| } |
| compressed = btrfs_file_extent_compression(eb, fi); |
| |
| num_bytes = btrfs_file_extent_num_bytes(eb, fi); |
| disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| if (disk_byte == 0) { |
| ret = -ENOENT; |
| goto out; |
| } |
| logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
| |
| down_read(&fs_info->commit_root_sem); |
| ret = extent_from_logical(fs_info, disk_byte, tmp_path, |
| &found_key, &flags); |
| up_read(&fs_info->commit_root_sem); |
| |
| if (ret < 0) |
| goto out; |
| if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| ret = -EIO; |
| goto out; |
| } |
| |
| ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0], |
| struct btrfs_extent_item); |
| /* |
| * Backreference walking (iterate_extent_inodes() below) is currently |
| * too expensive when an extent has a large number of references, both |
| * in time spent and used memory. So for now just fallback to write |
| * operations instead of clone operations when an extent has more than |
| * a certain amount of references. |
| */ |
| if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) { |
| ret = -ENOENT; |
| goto out; |
| } |
| btrfs_release_path(tmp_path); |
| |
| /* |
| * Setup the clone roots. |
| */ |
| for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| cur_clone_root = sctx->clone_roots + i; |
| cur_clone_root->ino = (u64)-1; |
| cur_clone_root->offset = 0; |
| cur_clone_root->found_refs = 0; |
| } |
| |
| backref_ctx->sctx = sctx; |
| backref_ctx->found = 0; |
| backref_ctx->cur_objectid = ino; |
| backref_ctx->cur_offset = data_offset; |
| backref_ctx->found_itself = 0; |
| backref_ctx->extent_len = num_bytes; |
| /* |
| * For non-compressed extents iterate_extent_inodes() gives us extent |
| * offsets that already take into account the data offset, but not for |
| * compressed extents, since the offset is logical and not relative to |
| * the physical extent locations. We must take this into account to |
| * avoid sending clone offsets that go beyond the source file's size, |
| * which would result in the clone ioctl failing with -EINVAL on the |
| * receiving end. |
| */ |
| if (compressed == BTRFS_COMPRESS_NONE) |
| backref_ctx->data_offset = 0; |
| else |
| backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi); |
| |
| /* |
| * The last extent of a file may be too large due to page alignment. |
| * We need to adjust extent_len in this case so that the checks in |
| * __iterate_backrefs work. |
| */ |
| if (data_offset + num_bytes >= ino_size) |
| backref_ctx->extent_len = ino_size - data_offset; |
| |
| /* |
| * Now collect all backrefs. |
| */ |
| if (compressed == BTRFS_COMPRESS_NONE) |
| extent_item_pos = logical - found_key.objectid; |
| else |
| extent_item_pos = 0; |
| ret = iterate_extent_inodes(fs_info, found_key.objectid, |
| extent_item_pos, 1, __iterate_backrefs, |
| backref_ctx, false); |
| |
| if (ret < 0) |
| goto out; |
| |
| if (!backref_ctx->found_itself) { |
| /* found a bug in backref code? */ |
| ret = -EIO; |
| btrfs_err(fs_info, |
| "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu", |
| ino, data_offset, disk_byte, found_key.objectid); |
| goto out; |
| } |
| |
| btrfs_debug(fs_info, |
| "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu", |
| data_offset, ino, num_bytes, logical); |
| |
| if (!backref_ctx->found) |
| btrfs_debug(fs_info, "no clones found"); |
| |
| cur_clone_root = NULL; |
| for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| if (sctx->clone_roots[i].found_refs) { |
| if (!cur_clone_root) |
| cur_clone_root = sctx->clone_roots + i; |
| else if (sctx->clone_roots[i].root == sctx->send_root) |
| /* prefer clones from send_root over others */ |
| cur_clone_root = sctx->clone_roots + i; |
| } |
| |
| } |
| |
| if (cur_clone_root) { |
| *found = cur_clone_root; |
| ret = 0; |
| } else { |
| ret = -ENOENT; |
| } |
| |
| out: |
| btrfs_free_path(tmp_path); |
| kfree(backref_ctx); |
| return ret; |
| } |
| |
| static int read_symlink(struct btrfs_root *root, |
| u64 ino, |
| struct fs_path *dest) |
| { |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| struct btrfs_file_extent_item *ei; |
| u8 type; |
| u8 compression; |
| unsigned long off; |
| int len; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = ino; |
| key.type = BTRFS_EXTENT_DATA_KEY; |
| key.offset = 0; |
| ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| /* |
| * An empty symlink inode. Can happen in rare error paths when |
| * creating a symlink (transaction committed before the inode |
| * eviction handler removed the symlink inode items and a crash |
| * happened in between or the subvol was snapshoted in between). |
| * Print an informative message to dmesg/syslog so that the user |
| * can delete the symlink. |
| */ |
| btrfs_err(root->fs_info, |
| "Found empty symlink inode %llu at root %llu", |
| ino, root->root_key.objectid); |
| ret = -EIO; |
| goto out; |
| } |
| |
| ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| struct btrfs_file_extent_item); |
| type = btrfs_file_extent_type(path->nodes[0], ei); |
| compression = btrfs_file_extent_compression(path->nodes[0], ei); |
| BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); |
| BUG_ON(compression); |
| |
| off = btrfs_file_extent_inline_start(ei); |
| len = btrfs_file_extent_ram_bytes(path->nodes[0], ei); |
| |
| ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * Helper function to generate a file name that is unique in the root of |
| * send_root and parent_root. This is used to generate names for orphan inodes. |
| */ |
| static int gen_unique_name(struct send_ctx *sctx, |
| u64 ino, u64 gen, |
| struct fs_path *dest) |
| { |
| int ret = 0; |
| struct btrfs_path *path; |
| struct btrfs_dir_item *di; |
| char tmp[64]; |
| int len; |
| u64 idx = 0; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| |
| while (1) { |
| len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
| ino, gen, idx); |
| ASSERT(len < sizeof(tmp)); |
| |
| di = btrfs_lookup_dir_item(NULL, sctx->send_root, |
| path, BTRFS_FIRST_FREE_OBJECTID, |
| tmp, strlen(tmp), 0); |
| btrfs_release_path(path); |
| if (IS_ERR(di)) { |
| ret = PTR_ERR(di); |
| goto out; |
| } |
| if (di) { |
| /* not unique, try again */ |
| idx++; |
| continue; |
| } |
| |
| if (!sctx->parent_root) { |
| /* unique */ |
| ret = 0; |
| break; |
| } |
| |
| di = btrfs_lookup_dir_item(NULL, sctx->parent_root, |
| path, BTRFS_FIRST_FREE_OBJECTID, |
| tmp, strlen(tmp), 0); |
| btrfs_release_path(path); |
| if (IS_ERR(di)) { |
| ret = PTR_ERR(di); |
| goto out; |
| } |
| if (di) { |
| /* not unique, try again */ |
| idx++; |
| continue; |
| } |
| /* unique */ |
| break; |
| } |
| |
| ret = fs_path_add(dest, tmp, strlen(tmp)); |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| enum inode_state { |
| inode_state_no_change, |
| inode_state_will_create, |
| inode_state_did_create, |
| inode_state_will_delete, |
| inode_state_did_delete, |
| }; |
| |
| static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) |
| { |
| int ret; |
| int left_ret; |
| int right_ret; |
| u64 left_gen; |
| u64 right_gen; |
| |
| ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, |
| NULL, NULL); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| left_ret = ret; |
| |
| if (!sctx->parent_root) { |
| right_ret = -ENOENT; |
| } else { |
| ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, |
| NULL, NULL, NULL, NULL); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| right_ret = ret; |
| } |
| |
| if (!left_ret && !right_ret) { |
| if (left_gen == gen && right_gen == gen) { |
| ret = inode_state_no_change; |
| } else if (left_gen == gen) { |
| if (ino < sctx->send_progress) |
| ret = inode_state_did_create; |
| else |
| ret = inode_state_will_create; |
| } else if (right_gen == gen) { |
| if (ino < sctx->send_progress) |
| ret = inode_state_did_delete; |
| else |
| ret = inode_state_will_delete; |
| } else { |
| ret = -ENOENT; |
| } |
| } else if (!left_ret) { |
| if (left_gen == gen) { |
| if (ino < sctx->send_progress) |
| ret = inode_state_did_create; |
| else |
| ret = inode_state_will_create; |
| } else { |
| ret = -ENOENT; |
| } |
| } else if (!right_ret) { |
| if (right_gen == gen) { |
| if (ino < sctx->send_progress) |
| ret = inode_state_did_delete; |
| else |
| ret = inode_state_will_delete; |
| } else { |
| ret = -ENOENT; |
| } |
| } else { |
| ret = -ENOENT; |
| } |
| |
| out: |
| return ret; |
| } |
| |
| static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) |
| { |
| int ret; |
| |
| if (ino == BTRFS_FIRST_FREE_OBJECTID) |
| return 1; |
| |
| ret = get_cur_inode_state(sctx, ino, gen); |
| if (ret < 0) |
| goto out; |
| |
| if (ret == inode_state_no_change || |
| ret == inode_state_did_create || |
| ret == inode_state_will_delete) |
| ret = 1; |
| else |
| ret = 0; |
| |
| out: |
| return ret; |
| } |
| |
| /* |
| * Helper function to lookup a dir item in a dir. |
| */ |
| static int lookup_dir_item_inode(struct btrfs_root *root, |
| u64 dir, const char *name, int name_len, |
| u64 *found_inode, |
| u8 *found_type) |
| { |
| int ret = 0; |
| struct btrfs_dir_item *di; |
| struct btrfs_key key; |
| struct btrfs_path *path; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| |
| di = btrfs_lookup_dir_item(NULL, root, path, |
| dir, name, name_len, 0); |
| if (!di) { |
| ret = -ENOENT; |
| goto out; |
| } |
| if (IS_ERR(di)) { |
| ret = PTR_ERR(di); |
| goto out; |
| } |
| btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| if (key.type == BTRFS_ROOT_ITEM_KEY) { |
| ret = -ENOENT; |
| goto out; |
| } |
| *found_inode = key.objectid; |
| *found_type = btrfs_dir_type(path->nodes[0], di); |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, |
| * generation of the parent dir and the name of the dir entry. |
| */ |
| static int get_first_ref(struct btrfs_root *root, u64 ino, |
| u64 *dir, u64 *dir_gen, struct fs_path *name) |
| { |
| int ret; |
| struct btrfs_key key; |
| struct btrfs_key found_key; |
| struct btrfs_path *path; |
| int len; |
| u64 parent_dir; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = ino; |
| key.type = BTRFS_INODE_REF_KEY; |
| key.offset = 0; |
| |
| ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| if (ret < 0) |
| goto out; |
| if (!ret) |
| btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| path->slots[0]); |
| if (ret || found_key.objectid != ino || |
| (found_key.type != BTRFS_INODE_REF_KEY && |
| found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| if (found_key.type == BTRFS_INODE_REF_KEY) { |
| struct btrfs_inode_ref *iref; |
| iref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| struct btrfs_inode_ref); |
| len = btrfs_inode_ref_name_len(path->nodes[0], iref); |
| ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| (unsigned long)(iref + 1), |
| len); |
| parent_dir = found_key.offset; |
| } else { |
| struct btrfs_inode_extref *extref; |
| extref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| struct btrfs_inode_extref); |
| len = btrfs_inode_extref_name_len(path->nodes[0], extref); |
| ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| (unsigned long)&extref->name, len); |
| parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); |
| } |
| if (ret < 0) |
| goto out; |
| btrfs_release_path(path); |
| |
| if (dir_gen) { |
| ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, |
| NULL, NULL, NULL); |
| if (ret < 0) |
| goto out; |
| } |
| |
| *dir = parent_dir; |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| static int is_first_ref(struct btrfs_root *root, |
| u64 ino, u64 dir, |
| const char *name, int name_len) |
| { |
| int ret; |
| struct fs_path *tmp_name; |
| u64 tmp_dir; |
| |
| tmp_name = fs_path_alloc(); |
| if (!tmp_name) |
| return -ENOMEM; |
| |
| ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name); |
| if (ret < 0) |
| goto out; |
| |
| if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
| ret = 0; |
| goto out; |
| } |
| |
| ret = !memcmp(tmp_name->start, name, name_len); |
| |
| out: |
| fs_path_free(tmp_name); |
| return ret; |
| } |
| |
| /* |
| * Used by process_recorded_refs to determine if a new ref would overwrite an |
| * already existing ref. In case it detects an overwrite, it returns the |
| * inode/gen in who_ino/who_gen. |
| * When an overwrite is detected, process_recorded_refs does proper orphanizing |
| * to make sure later references to the overwritten inode are possible. |
| * Orphanizing is however only required for the first ref of an inode. |
| * process_recorded_refs does an additional is_first_ref check to see if |
| * orphanizing is really required. |
| */ |
| static int 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) |
| { |
| int ret = 0; |
| u64 gen; |
| u64 other_inode = 0; |
| u8 other_type = 0; |
| |
| if (!sctx->parent_root) |
| goto out; |
| |
| ret = is_inode_existent(sctx, dir, dir_gen); |
| if (ret <= 0) |
| goto out; |
| |
| /* |
| * If we have a parent root we need to verify that the parent dir was |
| * not deleted and then re-created, if it was then we have no overwrite |
| * and we can just unlink this entry. |
| */ |
| if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { |
| ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, |
| NULL, NULL, NULL); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| if (ret) { |
| ret = 0; |
| goto out; |
| } |
| if (gen != dir_gen) |
| goto out; |
| } |
| |
| ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
| &other_inode, &other_type); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| if (ret) { |
| ret = 0; |
| goto out; |
| } |
| |
| /* |
| * Check if the overwritten ref was already processed. If yes, the ref |
| * was already unlinked/moved, so we can safely assume that we will not |
| * overwrite anything at this point in time. |
| */ |
| if (other_inode > sctx->send_progress || |
| is_waiting_for_move(sctx, other_inode)) { |
| ret = get_inode_info(sctx->parent_root, other_inode, NULL, |
| who_gen, who_mode, NULL, NULL, NULL); |
| if (ret < 0) |
| goto out; |
| |
| ret = 1; |
| *who_ino = other_inode; |
| } else { |
| ret = 0; |
| } |
| |
| out: |
| return ret; |
| } |
| |
| /* |
| * Checks if the ref was overwritten by an already processed inode. This is |
| * used by __get_cur_name_and_parent to find out if the ref was orphanized and |
| * thus the orphan name needs be used. |
| * process_recorded_refs also uses it to avoid unlinking of refs that were |
| * overwritten. |
| */ |
| static int did_overwrite_ref(struct send_ctx *sctx, |
| u64 dir, u64 dir_gen, |
| u64 ino, u64 ino_gen, |
| const char *name, int name_len) |
| { |
| int ret = 0; |
| u64 gen; |
| u64 ow_inode; |
| u8 other_type; |
| |
| if (!sctx->parent_root) |
| goto out; |
| |
| ret = is_inode_existent(sctx, dir, dir_gen); |
| if (ret <= 0) |
| goto out; |
| |
| if (dir != BTRFS_FIRST_FREE_OBJECTID) { |
| ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, |
| NULL, NULL, NULL); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| if (ret) { |
| ret = 0; |
| goto out; |
| } |
| if (gen != dir_gen) |
| goto out; |
| } |
| |
| /* check if the ref was overwritten by another ref */ |
| ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, |
| &ow_inode, &other_type); |
| if (ret < 0 && ret != -ENOENT) |
| goto out; |
| if (ret) { |
| /* was never and will never be overwritten */ |
| ret = 0; |
| goto out; |
| } |
| |
| ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, |
| NULL, NULL); |
| if (ret < 0) |
| goto out; |
| |
| if (ow_inode == ino && gen == ino_gen) { |
| ret = 0; |
| goto out; |
| } |
| |
| /* |
| * We know that it is or will be overwritten. Check this now. |
| * The current inode being processed might have been the one that caused |
| * inode 'ino' to be orphanized, therefore check if ow_inode matches |
| * the current inode being processed. |
| */ |
| if ((ow_inode < sctx->send_progress) || |
| (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && |
| gen == sctx->cur_inode_gen)) |
| ret = 1; |
| else |
| ret = 0; |
| |
| out: |
| return ret; |
| } |
| |
| /* |
| * Same as did_overwrite_ref, but also checks if it is the first ref of an inode |
| * that got overwritten. This is used by process_recorded_refs to determine |
| * if it has to use the path as returned by get_cur_path or the orphan name. |
| */ |
| static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
| { |
| int ret = 0; |
| struct fs_path *name = NULL; |
| u64 dir; |
| u64 dir_gen; |
| |
| if (!sctx->parent_root) |
| goto out; |
| |
| name = fs_path_alloc(); |
| if (!name) |
| return -ENOMEM; |
| |
| ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
| if (ret < 0) |
| goto out; |
| |
| ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, |
| name->start, fs_path_len(name)); |
| |
| out: |
| fs_path_free(name); |
| return ret; |
| } |
| |
| /* |
| * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, |
| * so we need to do some special handling in case we have clashes. This function |
| * takes care of this with the help of name_cache_entry::radix_list. |
| * In case of error, nce is kfreed. |
| */ |
| static int name_cache_insert(struct send_ctx *sctx, |
| struct name_cache_entry *nce) |
| { |
| int ret = 0; |
| struct list_head *nce_head; |
| |
| nce_head = radix_tree_lookup(&sctx->name_cache, |
| (unsigned long)nce->ino); |
| if (!nce_head) { |
| nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL); |
| if (!nce_head) { |
| kfree(nce); |
| return -ENOMEM; |
| } |
| INIT_LIST_HEAD(nce_head); |
| |
| ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
| if (ret < 0) { |
| kfree(nce_head); |
| kfree(nce); |
| return ret; |
| } |
| } |
| list_add_tail(&nce->radix_list, nce_head); |
| list_add_tail(&nce->list, &sctx->name_cache_list); |
| sctx->name_cache_size++; |
| |
| return ret; |
| } |
| |
| static void name_cache_delete(struct send_ctx *sctx, |
| struct name_cache_entry *nce) |
| { |
| struct list_head *nce_head; |
| |
| nce_head = radix_tree_lookup(&sctx->name_cache, |
| (unsigned long)nce->ino); |
| if (!nce_head) { |
| btrfs_err(sctx->send_root->fs_info, |
| "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", |
| nce->ino, sctx->name_cache_size); |
| } |
| |
| list_del(&nce->radix_list); |
| list_del(&nce->list); |
| sctx->name_cache_size--; |
| |
| /* |
| * We may not get to the final release of nce_head if the lookup fails |
| */ |
| if (nce_head && list_empty(nce_head)) { |
| radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
| kfree(nce_head); |
| } |
| } |
| |
| static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, |
| u64 ino, u64 gen) |
| { |
| struct list_head *nce_head; |
| struct name_cache_entry *cur; |
| |
| nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
| if (!nce_head) |
| return NULL; |
| |
| list_for_each_entry(cur, nce_head, radix_list) { |
| if (cur->ino == ino && cur->gen == gen) |
| return cur; |
| } |
| return NULL; |
| } |
| |
| /* |
| * Removes the entry from the list and adds it back to the end. This marks the |
| * entry as recently used so that name_cache_clean_unused does not remove it. |
| */ |
| static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
| { |
| list_del(&nce->list); |
| list_add_tail(&nce->list, &sctx->name_cache_list); |
| } |
| |
| /* |
| * Remove some entries from the beginning of name_cache_list. |
| */ |
| static void name_cache_clean_unused(struct send_ctx *sctx) |
| { |
| struct name_cache_entry *nce; |
| |
| if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) |
| return; |
| |
| while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { |
| nce = list_entry(sctx->name_cache_list.next, |
| struct name_cache_entry, list); |
| name_cache_delete(sctx, nce); |
| kfree(nce); |
| } |
| } |
| |
| static void name_cache_free(struct send_ctx *sctx) |
| { |
| struct name_cache_entry *nce; |
| |
| while (!list_empty(&sctx->name_cache_list)) { |
| nce = list_entry(sctx->name_cache_list.next, |
| struct name_cache_entry, list); |
| name_cache_delete(sctx, nce); |
| kfree(nce); |
| } |
| } |
| |
| /* |
| * Used by get_cur_path for each ref up to the root. |
| * Returns 0 if it succeeded. |
| * Returns 1 if the inode is not existent or got overwritten. In that case, the |
| * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 |
| * is returned, parent_ino/parent_gen are not guaranteed to be valid. |
| * Returns <0 in case of error. |
| */ |
| static int __get_cur_name_and_parent(struct send_ctx *sctx, |
| u64 ino, u64 gen, |
| u64 *parent_ino, |
| u64 *parent_gen, |
| struct fs_path *dest) |
| { |
| int ret; |
| int nce_ret; |
| struct name_cache_entry *nce = NULL; |
| |
| /* |
| * First check if we already did a call to this function with the same |
| * ino/gen. If yes, check if the cache entry is still up-to-date. If yes |
| * return the cached result. |
| */ |
| nce = name_cache_search(sctx, ino, gen); |
| if (nce) { |
| if (ino < sctx->send_progress && nce->need_later_update) { |
| name_cache_delete(sctx, nce); |
| kfree(nce); |
| nce = NULL; |
| } else { |
| name_cache_used(sctx, nce); |
| *parent_ino = nce->parent_ino; |
| *parent_gen = nce->parent_gen; |
| ret = fs_path_add(dest, nce->name, nce->name_len); |
| if (ret < 0) |
| goto out; |
| ret = nce->ret; |
| goto out; |
| } |
| } |
| |
| /* |
| * If the inode is not existent yet, add the orphan name and return 1. |
| * This should only happen for the parent dir that we determine in |
| * __record_new_ref |
| */ |
| ret = is_inode_existent(sctx, ino, gen); |
| if (ret < 0) |
| goto out; |
| |
| if (!ret) { |
| ret = gen_unique_name(sctx, ino, gen, dest); |
| if (ret < 0) |
| goto out; |
| ret = 1; |
| goto out_cache; |
| } |
| |
| /* |
| * Depending on whether the inode was already processed or not, use |
| * send_root or parent_root for ref lookup. |
| */ |
| if (ino < sctx->send_progress) |
| ret = get_first_ref(sctx->send_root, ino, |
| parent_ino, parent_gen, dest); |
| else |
| ret = get_first_ref(sctx->parent_root, ino, |
| parent_ino, parent_gen, dest); |
| if (ret < 0) |
| goto out; |
| |
| /* |
| * Check if the ref was overwritten by an inode's ref that was processed |
| * earlier. If yes, treat as orphan and return 1. |
| */ |
| ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
| dest->start, dest->end - dest->start); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| fs_path_reset(dest); |
| ret = gen_unique_name(sctx, ino, gen, dest); |
| if (ret < 0) |
| goto out; |
| ret = 1; |
| } |
| |
| out_cache: |
| /* |
| * Store the result of the lookup in the name cache. |
| */ |
| nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL); |
| if (!nce) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| nce->ino = ino; |
| nce->gen = gen; |
| nce->parent_ino = *parent_ino; |
| nce->parent_gen = *parent_gen; |
| nce->name_len = fs_path_len(dest); |
| nce->ret = ret; |
| strcpy(nce->name, dest->start); |
| |
| if (ino < sctx->send_progress) |
| nce->need_later_update = 0; |
| else |
| nce->need_later_update = 1; |
| |
| nce_ret = name_cache_insert(sctx, nce); |
| if (nce_ret < 0) |
| ret = nce_ret; |
| name_cache_clean_unused(sctx); |
| |
| out: |
| return ret; |
| } |
| |
| /* |
| * Magic happens here. This function returns the first ref to an inode as it |
| * would look like while receiving the stream at this point in time. |
| * We walk the path up to the root. For every inode in between, we check if it |
| * was already processed/sent. If yes, we continue with the parent as found |
| * in send_root. If not, we continue with the parent as found in parent_root. |
| * If we encounter an inode that was deleted at this point in time, we use the |
| * inodes "orphan" name instead of the real name and stop. Same with new inodes |
| * that were not created yet and overwritten inodes/refs. |
| * |
| * When do we have have orphan inodes: |
| * 1. When an inode is freshly created and thus no valid refs are available yet |
| * 2. When a directory lost all it's refs (deleted) but still has dir items |
| * inside which were not processed yet (pending for move/delete). If anyone |
| * tried to get the path to the dir items, it would get a path inside that |
| * orphan directory. |
| * 3. When an inode is moved around or gets new links, it may overwrite the ref |
| * of an unprocessed inode. If in that case the first ref would be |
| * overwritten, the overwritten inode gets "orphanized". Later when we |
| * process this overwritten inode, it is restored at a new place by moving |
| * the orphan inode. |
| * |
| * sctx->send_progress tells this function at which point in time receiving |
| * would be. |
| */ |
| static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, |
| struct fs_path *dest) |
| { |
| int ret = 0; |
| struct fs_path *name = NULL; |
| u64 parent_inode = 0; |
| u64 parent_gen = 0; |
| int stop = 0; |
| |
| name = fs_path_alloc(); |
| if (!name) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| dest->reversed = 1; |
| fs_path_reset(dest); |
| |
| while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { |
| struct waiting_dir_move *wdm; |
| |
| fs_path_reset(name); |
| |
| if (is_waiting_for_rm(sctx, ino)) { |
| ret = gen_unique_name(sctx, ino, gen, name); |
| if (ret < 0) |
| goto out; |
| ret = fs_path_add_path(dest, name); |
| break; |
| } |
| |
| wdm = get_waiting_dir_move(sctx, ino); |
| if (wdm && wdm->orphanized) { |
| ret = gen_unique_name(sctx, ino, gen, name); |
| stop = 1; |
| } else if (wdm) { |
| ret = get_first_ref(sctx->parent_root, ino, |
| &parent_inode, &parent_gen, name); |
| } else { |
| ret = __get_cur_name_and_parent(sctx, ino, gen, |
| &parent_inode, |
| &parent_gen, name); |
| if (ret) |
| stop = 1; |
| } |
| |
| if (ret < 0) |
| goto out; |
| |
| ret = fs_path_add_path(dest, name); |
| if (ret < 0) |
| goto out; |
| |
| ino = parent_inode; |
| gen = parent_gen; |
| } |
| |
| out: |
| fs_path_free(name); |
| if (!ret) |
| fs_path_unreverse(dest); |
| return ret; |
| } |
| |
| /* |
| * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace |
| */ |
| static int send_subvol_begin(struct send_ctx *sctx) |
| { |
| int ret; |
| struct btrfs_root *send_root = sctx->send_root; |
| struct btrfs_root *parent_root = sctx->parent_root; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| struct btrfs_root_ref *ref; |
| struct extent_buffer *leaf; |
| char *name = NULL; |
| int namelen; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); |
| if (!name) { |
| btrfs_free_path(path); |
| return -ENOMEM; |
| } |
| |
| key.objectid = send_root->objectid; |
| key.type = BTRFS_ROOT_BACKREF_KEY; |
| key.offset = 0; |
| |
| ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, |
| &key, path, 1, 0); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| leaf = path->nodes[0]; |
| btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| if (key.type != BTRFS_ROOT_BACKREF_KEY || |
| key.objectid != send_root->objectid) { |
| ret = -ENOENT; |
| goto out; |
| } |
| ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| namelen = btrfs_root_ref_name_len(leaf, ref); |
| read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); |
| btrfs_release_path(path); |
| |
| if (parent_root) { |
| ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); |
| if (ret < 0) |
| goto out; |
| } else { |
| ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); |
| if (ret < 0) |
| goto out; |
| } |
| |
| TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); |
| |
| if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) |
| TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| sctx->send_root->root_item.received_uuid); |
| else |
| TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| sctx->send_root->root_item.uuid); |
| |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
| le64_to_cpu(sctx->send_root->root_item.ctransid)); |
| if (parent_root) { |
| if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid)) |
| TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| parent_root->root_item.received_uuid); |
| else |
| TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| parent_root->root_item.uuid); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
| le64_to_cpu(sctx->parent_root->root_item.ctransid)); |
| } |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| btrfs_free_path(path); |
| kfree(name); |
| return ret; |
| } |
| |
| static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret = 0; |
| struct fs_path *p; |
| |
| btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size); |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return -ENOMEM; |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); |
| if (ret < 0) |
| goto out; |
| |
| ret = get_cur_path(sctx, ino, gen, p); |
| if (ret < 0) |
| goto out; |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| fs_path_free(p); |
| return ret; |
| } |
| |
| static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret = 0; |
| struct fs_path *p; |
| |
| btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode); |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return -ENOMEM; |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); |
| if (ret < 0) |
| goto out; |
| |
| ret = get_cur_path(sctx, ino, gen, p); |
| if (ret < 0) |
| goto out; |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| fs_path_free(p); |
| return ret; |
| } |
| |
| static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret = 0; |
| struct fs_path *p; |
| |
| btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu", |
| ino, uid, gid); |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return -ENOMEM; |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); |
| if (ret < 0) |
| goto out; |
| |
| ret = get_cur_path(sctx, ino, gen, p); |
| if (ret < 0) |
| goto out; |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| fs_path_free(p); |
| return ret; |
| } |
| |
| static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret = 0; |
| struct fs_path *p = NULL; |
| struct btrfs_inode_item *ii; |
| struct btrfs_path *path = NULL; |
| struct extent_buffer *eb; |
| struct btrfs_key key; |
| int slot; |
| |
| btrfs_debug(fs_info, "send_utimes %llu", ino); |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return -ENOMEM; |
| |
| path = alloc_path_for_send(); |
| if (!path) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| key.objectid = ino; |
| key.type = BTRFS_INODE_ITEM_KEY; |
| key.offset = 0; |
| ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| if (ret > 0) |
| ret = -ENOENT; |
| if (ret < 0) |
| goto out; |
| |
| eb = path->nodes[0]; |
| slot = path->slots[0]; |
| ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| |
| ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); |
| if (ret < 0) |
| goto out; |
| |
| ret = get_cur_path(sctx, ino, gen, p); |
| if (ret < 0) |
| goto out; |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); |
| TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); |
| TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); |
| /* TODO Add otime support when the otime patches get into upstream */ |
| |
| ret = send_cmd(sctx); |
| |
| tlv_put_failure: |
| out: |
| fs_path_free(p); |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have |
| * a valid path yet because we did not process the refs yet. So, the inode |
| * is created as orphan. |
| */ |
| static int send_create_inode(struct send_ctx *sctx, u64 ino) |
| { |
| struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
| int ret = 0; |
| struct fs_path *p; |
| int cmd; |
| u64 gen; |
| u64 mode; |
| u64 rdev; |
| |
| btrfs_debug(fs_info, "send_create_inode %llu", ino); |
| |
| p = fs_path_alloc(); |
| if (!p) |
| return -ENOMEM; |
| |
| if (ino != sctx->cur_ino) { |
| ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, |
| NULL, NULL, &rdev); |
| if (ret < 0) |
| goto out; |
| } else { |
| gen = sctx->cur_inode_gen; |
| mode = sctx->cur_inode_mode; |
| rdev = sctx->cur_inode_rdev; |
| } |
| |
| if (S_ISREG(mode)) { |
| cmd = BTRFS_SEND_C_MKFILE; |
| } else if (S_ISDIR(mode)) { |
| cmd = BTRFS_SEND_C_MKDIR; |
| } else if (S_ISLNK(mode)) { |
| cmd = BTRFS_SEND_C_SYMLINK; |
| } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
| cmd = BTRFS_SEND_C_MKNOD; |
| } else if (S_ISFIFO(mode)) { |
| cmd = BTRFS_SEND_C_MKFIFO; |
| } else if (S_ISSOCK(mode)) { |
| cmd = BTRFS_SEND_C_MKSOCK; |
| } else { |
| btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", |
| (int)(mode & S_IFMT)); |
| ret = -EOPNOTSUPP; |
| goto out; |
| } |
| |
| ret = begin_cmd(sctx, cmd); |
| if (ret < 0) |
| goto out; |
| |
| ret = gen_unique_name(sctx, ino, gen, p); |
| if (ret < 0) |
| goto out; |
| |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
| |
| if (S_ISLNK(mode)) { |
| fs_path_reset(p); |
| ret = read_symlink(sctx->send_root, ino, p); |
| if (ret < 0) |
| goto out; |
| TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); |
| } else if (S_ISCHR(mode) || S_ISBLK(mode) || |
| S_ISFIFO(mode) || S_ISSOCK(mode)) { |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
| TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); |
| } |
| |
| ret = send_cmd(sctx); |
| if (ret < 0) |
| goto out; |
| |
| |
| tlv_put_failure: |
| out: |
| fs_path_free(p); |
| return ret; |
| } |
| |
| /* |
| * We need some special handling for inodes that get processed before the parent |
| * directory got created. See process_recorded_refs for details. |
| * This function does the check if we already created the dir out of order. |
| */ |
| static int did_create_dir(struct send_ctx *sctx, u64 dir) |
| { |
| int ret = 0; |
| struct btrfs_path *path = NULL; |
| struct btrfs_key key; |
| struct btrfs_key found_key; |
| struct btrfs_key di_key; |
| struct extent_buffer *eb; |
| struct btrfs_dir_item *di; |
| int slot; |
| |
| path = alloc_path_for_send(); |
| if (!path) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| |
| key.objectid = dir; |
| key.type = BTRFS_DIR_INDEX_KEY; |
| key.offset = 0; |
| ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| if (ret < 0) |
| goto out; |
| |
| while (1) { |
| eb = path->nodes[0]; |
| slot = path->slots[0]; |
| if (slot >= btrfs_header_nritems(eb)) { |
| ret = btrfs_next_leaf(sctx->send_root, path); |
| if (ret < 0) { |
| goto out; |
| } else if (ret > 0) { |
| ret = 0; |
| break; |
| } |
| continue; |
| } |
| |
| btrfs_item_key_to_cpu(eb, &found_key, slot); |
| if (found_key.objectid != key.objectid || |
| found_key.type != key.type) { |
| ret = 0; |
| goto out; |
| } |
| |
| di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| |
| if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
| di_key.objectid < sctx->send_progress) { |
| ret = 1; |
| goto out; |
| } |
| |
| path->slots[0]++; |
| } |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * Only creates the inode if it is: |
| * 1. Not a directory |
| * 2. Or a directory which was not created already due to out of order |
| * directories. See did_create_dir and process_recorded_refs for details. |
| */ |
| static int send_create_inode_if_needed(struct send_ctx *sctx) |
| { |
| int ret; |
| |
| if (S_ISDIR(sctx->cur_inode_mode)) { |
| ret = did_create_dir(sctx, sctx->cur_ino); |
| if (ret < 0) |
| goto out; |
| if (ret) { |
| ret = 0; |
| goto out; |
| } |
| } |
| |
| ret = send_create_inode(sctx, sctx->cur_ino); |
| if (ret < 0) |
| goto out; |
| |
| out: |
| return ret; |
| } |
| |
| struct recorded_ref { |
| struct list_head list; |
| char *name; |
| struct fs_path *full_path; |
| u64 dir; |
| u64 dir_gen; |
| int name_len; |
| }; |
| |
| static void set_ref_path(struct recorded_ref *ref, struct fs_path *path) |
| { |
| ref->full_path = path; |
| ref->name = (char *)kbasename(ref->full_path->start); |
| ref->name_len = ref->full_path->end - ref->name; |
| } |
| |
| /* |
| * We need to process new refs before deleted refs, but compare_tree gives us |
| * everything mixed. So we first record all refs and later process them. |
| * This function is a helper to record one ref. |
| */ |
| static int __record_ref(struct list_head *head, u64 dir, |
| u64 dir_gen, struct fs_path *path) |
| { |
| struct recorded_ref *ref; |
| |
| ref = kmalloc(sizeof(*ref), GFP_KERNEL); |
| if (!ref) |
| return -ENOMEM; |
| |
| ref->dir = dir; |
| ref->dir_gen = dir_gen; |
| set_ref_path(ref, path); |
| list_add_tail(&ref->list, head); |
| return 0; |
| } |
| |
| static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
| { |
| struct recorded_ref *new; |
| |
| new = kmalloc(sizeof(*ref), GFP_KERNEL); |
| if (!new) |
| return -ENOMEM; |
| |
| new->dir = ref->dir; |
| new->dir_gen = ref->dir_gen; |
| new->full_path = NULL; |
| INIT_LIST_HEAD(&new->list); |
| list_add_tail(&new->list, list); |
| return 0; |
| } |
| |
| static void __free_recorded_refs(struct list_head *head) |
| { |
| struct recorded_ref *cur; |
| |
| while (!list_empty(head)) { |
| cur = list_entry(head->next, struct recorded_ref, list); |
| fs_path_free(cur->full_path); |
| list_del(&cur->list); |
| kfree(cur); |
| } |
| } |
| |
| static void free_recorded_refs(struct send_ctx *sctx) |
| { |
| __free_recorded_refs(&sctx->new_refs); |
| __free_recorded_refs(&sctx->deleted_refs); |
| } |
| |
| /* |
| * Renames/moves a file/dir to its orphan name. Used when the first |
| * ref of an unprocessed inode gets overwritten and for all non empty |
| * directories. |
| */ |
| static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, |
| struct fs_path *path) |
| { |
| int ret; |
| struct fs_path *orphan; |
| |
| orphan = fs_path_alloc(); |
| if (!orphan) |
| return -ENOMEM; |
| |
| ret = gen_unique_name(sctx, ino, gen, orphan); |
| if (ret < 0) |
| goto out; |
| |
| ret = send_rename(sctx, path, orphan); |
| |
| out: |
| fs_path_free(orphan); |
| return ret; |
| } |
| |
| static struct orphan_dir_info * |
| add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| { |
| struct rb_node **p = &sctx->orphan_dirs.rb_node; |
| struct rb_node *parent = NULL; |
| struct orphan_dir_info *entry, *odi; |
| |
| while (*p) { |
| parent = *p; |
| entry = rb_entry(parent, struct orphan_dir_info, node); |
| if (dir_ino < entry->ino) { |
| p = &(*p)->rb_left; |
| } else if (dir_ino > entry->ino) { |
| p = &(*p)->rb_right; |
| } else { |
| return entry; |
| } |
| } |
| |
| odi = kmalloc(sizeof(*odi), GFP_KERNEL); |
| if (!odi) |
| return ERR_PTR(-ENOMEM); |
| odi->ino = dir_ino; |
| odi->gen = 0; |
| odi->last_dir_index_offset = 0; |
| |
| rb_link_node(&odi->node, parent, p); |
| rb_insert_color(&odi->node, &sctx->orphan_dirs); |
| return odi; |
| } |
| |
| static struct orphan_dir_info * |
| get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| { |
| struct rb_node *n = sctx->orphan_dirs.rb_node; |
| struct orphan_dir_info *entry; |
| |
| while (n) { |
| entry = rb_entry(n, struct orphan_dir_info, node); |
| if (dir_ino < entry->ino) |
| n = n->rb_left; |
| else if (dir_ino > entry->ino) |
| n = n->rb_right; |
| else |
| return entry; |
| } |
| return NULL; |
| } |
| |
| static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino) |
| { |
| struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino); |
| |
| return odi != NULL; |
| } |
| |
| static void free_orphan_dir_info(struct send_ctx *sctx, |
| struct orphan_dir_info *odi) |
| { |
| if (!odi) |
| return; |
| rb_erase(&odi->node, &sctx->orphan_dirs); |
| kfree(odi); |
| } |
| |
| /* |
| * Returns 1 if a directory can be removed at this point in time. |
| * We check this by iterating all dir items and checking if the inode behind |
| * the dir item was already processed. |
| */ |
| static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| u64 send_progress) |
| { |
| int ret = 0; |
| struct btrfs_root *root = sctx->parent_root; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| struct btrfs_key found_key; |
| struct btrfs_key loc; |
| struct btrfs_dir_item *di; |
| struct orphan_dir_info *odi = NULL; |
| |
| /* |
| * Don't try to rmdir the top/root subvolume dir. |
| */ |
| if (dir == BTRFS_FIRST_FREE_OBJECTID) |
| return 0; |
| |
| path = alloc_path_for_send(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = dir; |
| key.type = BTRFS_DIR_INDEX_KEY; |
| key.offset = 0; |
| |
| odi = get_orphan_dir_info(sctx, dir); |
| if (odi) |
| key.offset = odi->last_dir_index_offset; |
| |
| ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| if (ret < 0) |
| goto out; |
| |
| while (1) { |
| struct waiting_dir_move *dm; |
| |
| if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| ret = btrfs_next_leaf(root, path); |
| if (ret < 0) |
| goto out; |
| else if (ret > 0) |
| break; |
| continue; |
| } |
| btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| path->slots[0]); |
| if (found_key.objectid != key.objectid || |
| found_key.type != key.type) |
| break; |
| |
| di = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| struct btrfs_dir_item); |
| btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); |
| |
| dm = get_waiting_dir_move(sctx, loc.objectid); |
| if (dm) { |
| odi = add_orphan_dir_info(sctx, dir); |
| if (IS_ERR(odi)) { |
| ret = PTR_ERR(odi); |
| goto out; |
| } |
| odi->gen = dir_gen; |
| odi->last_dir_index_offset = found_key.offset; |
| dm->rmdir_ino = dir; |
| ret = 0; |
| goto out; |
| } |
| |
| if (loc.objectid > send_progress) { |
| odi = add_orphan_dir_info(sctx, dir); |
| if (IS_ERR(odi)) { |
| ret = PTR_ERR(odi); |
| goto out; |
| } |
| odi->gen = dir_gen; |
| odi->last_dir_index_offset = found_key.offset; |
| ret = 0; |
| goto out; |
| } |
| |
| path->slots[0]++; |
| } |
| free_orphan_dir_info(sctx, odi); |
| |
| ret = 1; |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
| { |
| struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); |
| |
| return entry != NULL; |
| } |
| |
| static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) |
| { |
| struct rb_node **p = &sctx->waiting_dir_moves.rb_node; |
| struct rb_node *parent = NULL; |
| struct waiting_dir_move *entry, *dm; |
| |
| dm = kmalloc(sizeof(*dm), GFP_KERNEL); |
| if (!dm) |
| return -ENOMEM; |
| dm->ino = ino; |
| dm->rmdir_ino = 0; |
| dm->orphanized = orphanized; |
| |
| while (*p) { |
| parent = *p; |
| entry = rb_entry(parent, struct waiting_dir_move, node); |
| if (ino < entry->ino) { |
| p = &(*p)->rb_left; |
| } else if (ino > entry->ino) { |
| p = &(*p)->rb_right; |
| } else { |
| kfree(dm); |
| return -EEXIST; |
| } |
| } |
| |
| rb_link_node(&dm->node, parent, p); |
| rb_insert_color(&dm->node, &sctx->waiting_dir_moves); |
| return 0; |
| } |
| |
| static struct waiting_dir_move * |
| get_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
| { |
| struct rb_node *n = sctx->waiting_dir_moves.rb_node; |
| struct waiting_dir_move *entry; |
| |
| while (n) { |
| entry = rb_entry(n, struct waiting_dir_move, node); |
| if (ino < entry->ino) |
| n = n->rb_left; |
| else if (ino > entry->ino) |
| n = n->rb_right; |
| else |
| return entry; |
| } |
| return NULL; |
| } |
| |
| static void free_waiting_dir_move(struct send_ctx *sctx, |
| struct waiting_dir_move *dm) |
| { |
| if (!dm) |
| return; |
| rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| kfree(dm); |
| } |
| |
| static int 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) |
| { |
| struct rb_node **p = &sctx->pending_dir_moves.rb_node; |
| struct rb_node *parent = NULL; |
| struct pending_dir_move *entry = NULL, *pm; |
| struct recorded_ref *cur; |
| int exists = 0; |
| int ret; |
| |
| pm = kmalloc(sizeof(*pm), GFP_KERNEL); |
| if (!pm) |
| return -ENOMEM; |
| pm->parent_ino = parent_ino; |
| pm->ino = ino; |
| pm->gen = ino_gen; |
| INIT_LIST_HEAD(&pm->list); |
| INIT_LIST_HEAD(&pm->update_refs); |
| RB_CLEAR_NODE(&pm->node); |
| |
| while (*p) { |
| parent = *p; |
| entry = rb_entry(parent, struct pending_dir_move, node); |
| if (parent_ino < entry->parent_ino) { |
| p = &(*p)->rb_left; |
| } else if (parent_ino > entry->parent_ino) { |
| p = &(*p)->rb_right; |
| } else { |
| exists = 1; |
| break; |
| } |
| } |
| |
| list_for_each_entry(cur, deleted_refs, list) { |
| ret = dup_ref(cur, &pm->update_refs); |
| if (ret < 0) |
| goto out; |
| } |
| list_for_each_entry(cur, new_refs, list) { |
| ret = dup_ref(cur, &pm->update_refs); |
| if (ret < 0) |
| goto out; |
| } |
| |
| ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); |
| if (ret) |
| goto out; |
| |
| if (exists) { |
|