Commit | Line | Data |
---|---|---|
31db9f7c AB |
1 | /* |
2 | * Copyright (C) 2012 Alexander Block. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
19 | #include <linux/bsearch.h> | |
20 | #include <linux/fs.h> | |
21 | #include <linux/file.h> | |
22 | #include <linux/sort.h> | |
23 | #include <linux/mount.h> | |
24 | #include <linux/xattr.h> | |
25 | #include <linux/posix_acl_xattr.h> | |
26 | #include <linux/radix-tree.h> | |
a1857ebe | 27 | #include <linux/vmalloc.h> |
ed84885d | 28 | #include <linux/string.h> |
31db9f7c AB |
29 | |
30 | #include "send.h" | |
31 | #include "backref.h" | |
0b947aff | 32 | #include "hash.h" |
31db9f7c AB |
33 | #include "locking.h" |
34 | #include "disk-io.h" | |
35 | #include "btrfs_inode.h" | |
36 | #include "transaction.h" | |
37 | ||
38 | static int g_verbose = 0; | |
39 | ||
40 | #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__) | |
41 | ||
42 | /* | |
43 | * A fs_path is a helper to dynamically build path names with unknown size. | |
44 | * It reallocates the internal buffer on demand. | |
45 | * It allows fast adding of path elements on the right side (normal path) and | |
46 | * fast adding to the left side (reversed path). A reversed path can also be | |
47 | * unreversed if needed. | |
48 | */ | |
49 | struct fs_path { | |
50 | union { | |
51 | struct { | |
52 | char *start; | |
53 | char *end; | |
31db9f7c AB |
54 | |
55 | char *buf; | |
1f5a7ff9 DS |
56 | unsigned short buf_len:15; |
57 | unsigned short reversed:1; | |
31db9f7c AB |
58 | char inline_buf[]; |
59 | }; | |
60 | char pad[PAGE_SIZE]; | |
61 | }; | |
62 | }; | |
63 | #define FS_PATH_INLINE_SIZE \ | |
64 | (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) | |
65 | ||
66 | ||
67 | /* reused for each extent */ | |
68 | struct clone_root { | |
69 | struct btrfs_root *root; | |
70 | u64 ino; | |
71 | u64 offset; | |
72 | ||
73 | u64 found_refs; | |
74 | }; | |
75 | ||
76 | #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 | |
77 | #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) | |
78 | ||
79 | struct send_ctx { | |
80 | struct file *send_filp; | |
81 | loff_t send_off; | |
82 | char *send_buf; | |
83 | u32 send_size; | |
84 | u32 send_max_size; | |
85 | u64 total_send_size; | |
86 | u64 cmd_send_size[BTRFS_SEND_C_MAX + 1]; | |
cb95e7bf | 87 | u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
31db9f7c | 88 | |
31db9f7c AB |
89 | struct btrfs_root *send_root; |
90 | struct btrfs_root *parent_root; | |
91 | struct clone_root *clone_roots; | |
92 | int clone_roots_cnt; | |
93 | ||
94 | /* current state of the compare_tree call */ | |
95 | struct btrfs_path *left_path; | |
96 | struct btrfs_path *right_path; | |
97 | struct btrfs_key *cmp_key; | |
98 | ||
99 | /* | |
100 | * infos of the currently processed inode. In case of deleted inodes, | |
101 | * these are the values from the deleted inode. | |
102 | */ | |
103 | u64 cur_ino; | |
104 | u64 cur_inode_gen; | |
105 | int cur_inode_new; | |
106 | int cur_inode_new_gen; | |
107 | int cur_inode_deleted; | |
31db9f7c AB |
108 | u64 cur_inode_size; |
109 | u64 cur_inode_mode; | |
16e7549f | 110 | u64 cur_inode_last_extent; |
31db9f7c AB |
111 | |
112 | u64 send_progress; | |
113 | ||
114 | struct list_head new_refs; | |
115 | struct list_head deleted_refs; | |
116 | ||
117 | struct radix_tree_root name_cache; | |
118 | struct list_head name_cache_list; | |
119 | int name_cache_size; | |
120 | ||
31db9f7c | 121 | char *read_buf; |
9f03740a FDBM |
122 | |
123 | /* | |
124 | * We process inodes by their increasing order, so if before an | |
125 | * incremental send we reverse the parent/child relationship of | |
126 | * directories such that a directory with a lower inode number was | |
127 | * the parent of a directory with a higher inode number, and the one | |
128 | * becoming the new parent got renamed too, we can't rename/move the | |
129 | * directory with lower inode number when we finish processing it - we | |
130 | * must process the directory with higher inode number first, then | |
131 | * rename/move it and then rename/move the directory with lower inode | |
132 | * number. Example follows. | |
133 | * | |
134 | * Tree state when the first send was performed: | |
135 | * | |
136 | * . | |
137 | * |-- a (ino 257) | |
138 | * |-- b (ino 258) | |
139 | * | | |
140 | * | | |
141 | * |-- c (ino 259) | |
142 | * | |-- d (ino 260) | |
143 | * | | |
144 | * |-- c2 (ino 261) | |
145 | * | |
146 | * Tree state when the second (incremental) send is performed: | |
147 | * | |
148 | * . | |
149 | * |-- a (ino 257) | |
150 | * |-- b (ino 258) | |
151 | * |-- c2 (ino 261) | |
152 | * |-- d2 (ino 260) | |
153 | * |-- cc (ino 259) | |
154 | * | |
155 | * The sequence of steps that lead to the second state was: | |
156 | * | |
157 | * mv /a/b/c/d /a/b/c2/d2 | |
158 | * mv /a/b/c /a/b/c2/d2/cc | |
159 | * | |
160 | * "c" has lower inode number, but we can't move it (2nd mv operation) | |
161 | * before we move "d", which has higher inode number. | |
162 | * | |
163 | * So we just memorize which move/rename operations must be performed | |
164 | * later when their respective parent is processed and moved/renamed. | |
165 | */ | |
166 | ||
167 | /* Indexed by parent directory inode number. */ | |
168 | struct rb_root pending_dir_moves; | |
169 | ||
170 | /* | |
171 | * Reverse index, indexed by the inode number of a directory that | |
172 | * is waiting for the move/rename of its immediate parent before its | |
173 | * own move/rename can be performed. | |
174 | */ | |
175 | struct rb_root waiting_dir_moves; | |
176 | }; | |
177 | ||
178 | struct pending_dir_move { | |
179 | struct rb_node node; | |
180 | struct list_head list; | |
181 | u64 parent_ino; | |
182 | u64 ino; | |
183 | u64 gen; | |
184 | struct list_head update_refs; | |
185 | }; | |
186 | ||
187 | struct waiting_dir_move { | |
188 | struct rb_node node; | |
189 | u64 ino; | |
31db9f7c AB |
190 | }; |
191 | ||
192 | struct name_cache_entry { | |
193 | struct list_head list; | |
7e0926fe AB |
194 | /* |
195 | * radix_tree has only 32bit entries but we need to handle 64bit inums. | |
196 | * We use the lower 32bit of the 64bit inum to store it in the tree. If | |
197 | * more then one inum would fall into the same entry, we use radix_list | |
198 | * to store the additional entries. radix_list is also used to store | |
199 | * entries where two entries have the same inum but different | |
200 | * generations. | |
201 | */ | |
202 | struct list_head radix_list; | |
31db9f7c AB |
203 | u64 ino; |
204 | u64 gen; | |
205 | u64 parent_ino; | |
206 | u64 parent_gen; | |
207 | int ret; | |
208 | int need_later_update; | |
209 | int name_len; | |
210 | char name[]; | |
211 | }; | |
212 | ||
9f03740a FDBM |
213 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
214 | ||
16e7549f JB |
215 | static int need_send_hole(struct send_ctx *sctx) |
216 | { | |
217 | return (sctx->parent_root && !sctx->cur_inode_new && | |
218 | !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && | |
219 | S_ISREG(sctx->cur_inode_mode)); | |
220 | } | |
221 | ||
31db9f7c AB |
222 | static void fs_path_reset(struct fs_path *p) |
223 | { | |
224 | if (p->reversed) { | |
225 | p->start = p->buf + p->buf_len - 1; | |
226 | p->end = p->start; | |
227 | *p->start = 0; | |
228 | } else { | |
229 | p->start = p->buf; | |
230 | p->end = p->start; | |
231 | *p->start = 0; | |
232 | } | |
233 | } | |
234 | ||
924794c9 | 235 | static struct fs_path *fs_path_alloc(void) |
31db9f7c AB |
236 | { |
237 | struct fs_path *p; | |
238 | ||
239 | p = kmalloc(sizeof(*p), GFP_NOFS); | |
240 | if (!p) | |
241 | return NULL; | |
242 | p->reversed = 0; | |
31db9f7c AB |
243 | p->buf = p->inline_buf; |
244 | p->buf_len = FS_PATH_INLINE_SIZE; | |
245 | fs_path_reset(p); | |
246 | return p; | |
247 | } | |
248 | ||
924794c9 | 249 | static struct fs_path *fs_path_alloc_reversed(void) |
31db9f7c AB |
250 | { |
251 | struct fs_path *p; | |
252 | ||
924794c9 | 253 | p = fs_path_alloc(); |
31db9f7c AB |
254 | if (!p) |
255 | return NULL; | |
256 | p->reversed = 1; | |
257 | fs_path_reset(p); | |
258 | return p; | |
259 | } | |
260 | ||
924794c9 | 261 | static void fs_path_free(struct fs_path *p) |
31db9f7c AB |
262 | { |
263 | if (!p) | |
264 | return; | |
265 | if (p->buf != p->inline_buf) { | |
e25a8122 | 266 | if (is_vmalloc_addr(p->buf)) |
31db9f7c AB |
267 | vfree(p->buf); |
268 | else | |
269 | kfree(p->buf); | |
270 | } | |
271 | kfree(p); | |
272 | } | |
273 | ||
274 | static int fs_path_len(struct fs_path *p) | |
275 | { | |
276 | return p->end - p->start; | |
277 | } | |
278 | ||
279 | static int fs_path_ensure_buf(struct fs_path *p, int len) | |
280 | { | |
281 | char *tmp_buf; | |
282 | int path_len; | |
283 | int old_buf_len; | |
284 | ||
285 | len++; | |
286 | ||
287 | if (p->buf_len >= len) | |
288 | return 0; | |
289 | ||
290 | path_len = p->end - p->start; | |
291 | old_buf_len = p->buf_len; | |
292 | len = PAGE_ALIGN(len); | |
293 | ||
294 | if (p->buf == p->inline_buf) { | |
8be04b93 | 295 | tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN); |
31db9f7c AB |
296 | if (!tmp_buf) { |
297 | tmp_buf = vmalloc(len); | |
298 | if (!tmp_buf) | |
299 | return -ENOMEM; | |
31db9f7c AB |
300 | } |
301 | memcpy(tmp_buf, p->buf, p->buf_len); | |
302 | p->buf = tmp_buf; | |
303 | p->buf_len = len; | |
304 | } else { | |
e25a8122 | 305 | if (is_vmalloc_addr(p->buf)) { |
31db9f7c AB |
306 | tmp_buf = vmalloc(len); |
307 | if (!tmp_buf) | |
308 | return -ENOMEM; | |
309 | memcpy(tmp_buf, p->buf, p->buf_len); | |
310 | vfree(p->buf); | |
311 | } else { | |
312 | tmp_buf = krealloc(p->buf, len, GFP_NOFS); | |
313 | if (!tmp_buf) { | |
314 | tmp_buf = vmalloc(len); | |
315 | if (!tmp_buf) | |
316 | return -ENOMEM; | |
317 | memcpy(tmp_buf, p->buf, p->buf_len); | |
318 | kfree(p->buf); | |
31db9f7c AB |
319 | } |
320 | } | |
321 | p->buf = tmp_buf; | |
322 | p->buf_len = len; | |
323 | } | |
324 | if (p->reversed) { | |
325 | tmp_buf = p->buf + old_buf_len - path_len - 1; | |
326 | p->end = p->buf + p->buf_len - 1; | |
327 | p->start = p->end - path_len; | |
328 | memmove(p->start, tmp_buf, path_len + 1); | |
329 | } else { | |
330 | p->start = p->buf; | |
331 | p->end = p->start + path_len; | |
332 | } | |
333 | return 0; | |
334 | } | |
335 | ||
b23ab57d DS |
336 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
337 | char **prepared) | |
31db9f7c AB |
338 | { |
339 | int ret; | |
340 | int new_len; | |
341 | ||
342 | new_len = p->end - p->start + name_len; | |
343 | if (p->start != p->end) | |
344 | new_len++; | |
345 | ret = fs_path_ensure_buf(p, new_len); | |
346 | if (ret < 0) | |
347 | goto out; | |
348 | ||
349 | if (p->reversed) { | |
350 | if (p->start != p->end) | |
351 | *--p->start = '/'; | |
352 | p->start -= name_len; | |
b23ab57d | 353 | *prepared = p->start; |
31db9f7c AB |
354 | } else { |
355 | if (p->start != p->end) | |
356 | *p->end++ = '/'; | |
b23ab57d | 357 | *prepared = p->end; |
31db9f7c AB |
358 | p->end += name_len; |
359 | *p->end = 0; | |
360 | } | |
361 | ||
362 | out: | |
363 | return ret; | |
364 | } | |
365 | ||
366 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) | |
367 | { | |
368 | int ret; | |
b23ab57d | 369 | char *prepared; |
31db9f7c | 370 | |
b23ab57d | 371 | ret = fs_path_prepare_for_add(p, name_len, &prepared); |
31db9f7c AB |
372 | if (ret < 0) |
373 | goto out; | |
b23ab57d | 374 | memcpy(prepared, name, name_len); |
31db9f7c AB |
375 | |
376 | out: | |
377 | return ret; | |
378 | } | |
379 | ||
380 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) | |
381 | { | |
382 | int ret; | |
b23ab57d | 383 | char *prepared; |
31db9f7c | 384 | |
b23ab57d | 385 | ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
31db9f7c AB |
386 | if (ret < 0) |
387 | goto out; | |
b23ab57d | 388 | memcpy(prepared, p2->start, p2->end - p2->start); |
31db9f7c AB |
389 | |
390 | out: | |
391 | return ret; | |
392 | } | |
393 | ||
394 | static int fs_path_add_from_extent_buffer(struct fs_path *p, | |
395 | struct extent_buffer *eb, | |
396 | unsigned long off, int len) | |
397 | { | |
398 | int ret; | |
b23ab57d | 399 | char *prepared; |
31db9f7c | 400 | |
b23ab57d | 401 | ret = fs_path_prepare_for_add(p, len, &prepared); |
31db9f7c AB |
402 | if (ret < 0) |
403 | goto out; | |
404 | ||
b23ab57d | 405 | read_extent_buffer(eb, prepared, off, len); |
31db9f7c AB |
406 | |
407 | out: | |
408 | return ret; | |
409 | } | |
410 | ||
31db9f7c AB |
411 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
412 | { | |
413 | int ret; | |
414 | ||
415 | p->reversed = from->reversed; | |
416 | fs_path_reset(p); | |
417 | ||
418 | ret = fs_path_add_path(p, from); | |
419 | ||
420 | return ret; | |
421 | } | |
422 | ||
423 | ||
424 | static void fs_path_unreverse(struct fs_path *p) | |
425 | { | |
426 | char *tmp; | |
427 | int len; | |
428 | ||
429 | if (!p->reversed) | |
430 | return; | |
431 | ||
432 | tmp = p->start; | |
433 | len = p->end - p->start; | |
434 | p->start = p->buf; | |
435 | p->end = p->start + len; | |
436 | memmove(p->start, tmp, len + 1); | |
437 | p->reversed = 0; | |
438 | } | |
439 | ||
440 | static struct btrfs_path *alloc_path_for_send(void) | |
441 | { | |
442 | struct btrfs_path *path; | |
443 | ||
444 | path = btrfs_alloc_path(); | |
445 | if (!path) | |
446 | return NULL; | |
447 | path->search_commit_root = 1; | |
448 | path->skip_locking = 1; | |
449 | return path; | |
450 | } | |
451 | ||
48a3b636 | 452 | static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off) |
31db9f7c AB |
453 | { |
454 | int ret; | |
455 | mm_segment_t old_fs; | |
456 | u32 pos = 0; | |
457 | ||
458 | old_fs = get_fs(); | |
459 | set_fs(KERNEL_DS); | |
460 | ||
461 | while (pos < len) { | |
1bcea355 | 462 | ret = vfs_write(filp, (char *)buf + pos, len - pos, off); |
31db9f7c AB |
463 | /* TODO handle that correctly */ |
464 | /*if (ret == -ERESTARTSYS) { | |
465 | continue; | |
466 | }*/ | |
467 | if (ret < 0) | |
468 | goto out; | |
469 | if (ret == 0) { | |
470 | ret = -EIO; | |
471 | goto out; | |
472 | } | |
473 | pos += ret; | |
474 | } | |
475 | ||
476 | ret = 0; | |
477 | ||
478 | out: | |
479 | set_fs(old_fs); | |
480 | return ret; | |
481 | } | |
482 | ||
483 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) | |
484 | { | |
485 | struct btrfs_tlv_header *hdr; | |
486 | int total_len = sizeof(*hdr) + len; | |
487 | int left = sctx->send_max_size - sctx->send_size; | |
488 | ||
489 | if (unlikely(left < total_len)) | |
490 | return -EOVERFLOW; | |
491 | ||
492 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); | |
493 | hdr->tlv_type = cpu_to_le16(attr); | |
494 | hdr->tlv_len = cpu_to_le16(len); | |
495 | memcpy(hdr + 1, data, len); | |
496 | sctx->send_size += total_len; | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
95bc79d5 DS |
501 | #define TLV_PUT_DEFINE_INT(bits) \ |
502 | static int tlv_put_u##bits(struct send_ctx *sctx, \ | |
503 | u##bits attr, u##bits value) \ | |
504 | { \ | |
505 | __le##bits __tmp = cpu_to_le##bits(value); \ | |
506 | return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ | |
507 | } | |
31db9f7c | 508 | |
95bc79d5 | 509 | TLV_PUT_DEFINE_INT(64) |
31db9f7c AB |
510 | |
511 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, | |
512 | const char *str, int len) | |
513 | { | |
514 | if (len == -1) | |
515 | len = strlen(str); | |
516 | return tlv_put(sctx, attr, str, len); | |
517 | } | |
518 | ||
519 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, | |
520 | const u8 *uuid) | |
521 | { | |
522 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); | |
523 | } | |
524 | ||
31db9f7c AB |
525 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
526 | struct extent_buffer *eb, | |
527 | struct btrfs_timespec *ts) | |
528 | { | |
529 | struct btrfs_timespec bts; | |
530 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); | |
531 | return tlv_put(sctx, attr, &bts, sizeof(bts)); | |
532 | } | |
533 | ||
534 | ||
535 | #define TLV_PUT(sctx, attrtype, attrlen, data) \ | |
536 | do { \ | |
537 | ret = tlv_put(sctx, attrtype, attrlen, data); \ | |
538 | if (ret < 0) \ | |
539 | goto tlv_put_failure; \ | |
540 | } while (0) | |
541 | ||
542 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ | |
543 | do { \ | |
544 | ret = tlv_put_u##bits(sctx, attrtype, value); \ | |
545 | if (ret < 0) \ | |
546 | goto tlv_put_failure; \ | |
547 | } while (0) | |
548 | ||
549 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) | |
550 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) | |
551 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) | |
552 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) | |
553 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ | |
554 | do { \ | |
555 | ret = tlv_put_string(sctx, attrtype, str, len); \ | |
556 | if (ret < 0) \ | |
557 | goto tlv_put_failure; \ | |
558 | } while (0) | |
559 | #define TLV_PUT_PATH(sctx, attrtype, p) \ | |
560 | do { \ | |
561 | ret = tlv_put_string(sctx, attrtype, p->start, \ | |
562 | p->end - p->start); \ | |
563 | if (ret < 0) \ | |
564 | goto tlv_put_failure; \ | |
565 | } while(0) | |
566 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ | |
567 | do { \ | |
568 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ | |
569 | if (ret < 0) \ | |
570 | goto tlv_put_failure; \ | |
571 | } while (0) | |
31db9f7c AB |
572 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
573 | do { \ | |
574 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ | |
575 | if (ret < 0) \ | |
576 | goto tlv_put_failure; \ | |
577 | } while (0) | |
578 | ||
579 | static int send_header(struct send_ctx *sctx) | |
580 | { | |
581 | struct btrfs_stream_header hdr; | |
582 | ||
583 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); | |
584 | hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); | |
585 | ||
1bcea355 AJ |
586 | return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
587 | &sctx->send_off); | |
31db9f7c AB |
588 | } |
589 | ||
590 | /* | |
591 | * For each command/item we want to send to userspace, we call this function. | |
592 | */ | |
593 | static int begin_cmd(struct send_ctx *sctx, int cmd) | |
594 | { | |
595 | struct btrfs_cmd_header *hdr; | |
596 | ||
fae7f21c | 597 | if (WARN_ON(!sctx->send_buf)) |
31db9f7c | 598 | return -EINVAL; |
31db9f7c AB |
599 | |
600 | BUG_ON(sctx->send_size); | |
601 | ||
602 | sctx->send_size += sizeof(*hdr); | |
603 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; | |
604 | hdr->cmd = cpu_to_le16(cmd); | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
609 | static int send_cmd(struct send_ctx *sctx) | |
610 | { | |
611 | int ret; | |
612 | struct btrfs_cmd_header *hdr; | |
613 | u32 crc; | |
614 | ||
615 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; | |
616 | hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); | |
617 | hdr->crc = 0; | |
618 | ||
0b947aff | 619 | crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
31db9f7c AB |
620 | hdr->crc = cpu_to_le32(crc); |
621 | ||
1bcea355 AJ |
622 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
623 | &sctx->send_off); | |
31db9f7c AB |
624 | |
625 | sctx->total_send_size += sctx->send_size; | |
626 | sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; | |
627 | sctx->send_size = 0; | |
628 | ||
629 | return ret; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Sends a move instruction to user space | |
634 | */ | |
635 | static int send_rename(struct send_ctx *sctx, | |
636 | struct fs_path *from, struct fs_path *to) | |
637 | { | |
638 | int ret; | |
639 | ||
640 | verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start); | |
641 | ||
642 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); | |
643 | if (ret < 0) | |
644 | goto out; | |
645 | ||
646 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); | |
647 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); | |
648 | ||
649 | ret = send_cmd(sctx); | |
650 | ||
651 | tlv_put_failure: | |
652 | out: | |
653 | return ret; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Sends a link instruction to user space | |
658 | */ | |
659 | static int send_link(struct send_ctx *sctx, | |
660 | struct fs_path *path, struct fs_path *lnk) | |
661 | { | |
662 | int ret; | |
663 | ||
664 | verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start); | |
665 | ||
666 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); | |
667 | if (ret < 0) | |
668 | goto out; | |
669 | ||
670 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
671 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); | |
672 | ||
673 | ret = send_cmd(sctx); | |
674 | ||
675 | tlv_put_failure: | |
676 | out: | |
677 | return ret; | |
678 | } | |
679 | ||
680 | /* | |
681 | * Sends an unlink instruction to user space | |
682 | */ | |
683 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) | |
684 | { | |
685 | int ret; | |
686 | ||
687 | verbose_printk("btrfs: send_unlink %s\n", path->start); | |
688 | ||
689 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); | |
690 | if (ret < 0) | |
691 | goto out; | |
692 | ||
693 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
694 | ||
695 | ret = send_cmd(sctx); | |
696 | ||
697 | tlv_put_failure: | |
698 | out: | |
699 | return ret; | |
700 | } | |
701 | ||
702 | /* | |
703 | * Sends a rmdir instruction to user space | |
704 | */ | |
705 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) | |
706 | { | |
707 | int ret; | |
708 | ||
709 | verbose_printk("btrfs: send_rmdir %s\n", path->start); | |
710 | ||
711 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); | |
712 | if (ret < 0) | |
713 | goto out; | |
714 | ||
715 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
716 | ||
717 | ret = send_cmd(sctx); | |
718 | ||
719 | tlv_put_failure: | |
720 | out: | |
721 | return ret; | |
722 | } | |
723 | ||
724 | /* | |
725 | * Helper function to retrieve some fields from an inode item. | |
726 | */ | |
727 | static int get_inode_info(struct btrfs_root *root, | |
728 | u64 ino, u64 *size, u64 *gen, | |
85a7b33b AB |
729 | u64 *mode, u64 *uid, u64 *gid, |
730 | u64 *rdev) | |
31db9f7c AB |
731 | { |
732 | int ret; | |
733 | struct btrfs_inode_item *ii; | |
734 | struct btrfs_key key; | |
735 | struct btrfs_path *path; | |
736 | ||
737 | path = alloc_path_for_send(); | |
738 | if (!path) | |
739 | return -ENOMEM; | |
740 | ||
741 | key.objectid = ino; | |
742 | key.type = BTRFS_INODE_ITEM_KEY; | |
743 | key.offset = 0; | |
744 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
745 | if (ret < 0) | |
746 | goto out; | |
747 | if (ret) { | |
748 | ret = -ENOENT; | |
749 | goto out; | |
750 | } | |
751 | ||
752 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
753 | struct btrfs_inode_item); | |
754 | if (size) | |
755 | *size = btrfs_inode_size(path->nodes[0], ii); | |
756 | if (gen) | |
757 | *gen = btrfs_inode_generation(path->nodes[0], ii); | |
758 | if (mode) | |
759 | *mode = btrfs_inode_mode(path->nodes[0], ii); | |
760 | if (uid) | |
761 | *uid = btrfs_inode_uid(path->nodes[0], ii); | |
762 | if (gid) | |
763 | *gid = btrfs_inode_gid(path->nodes[0], ii); | |
85a7b33b AB |
764 | if (rdev) |
765 | *rdev = btrfs_inode_rdev(path->nodes[0], ii); | |
31db9f7c AB |
766 | |
767 | out: | |
768 | btrfs_free_path(path); | |
769 | return ret; | |
770 | } | |
771 | ||
772 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, | |
773 | struct fs_path *p, | |
774 | void *ctx); | |
775 | ||
776 | /* | |
96b5bd77 JS |
777 | * Helper function to iterate the entries in ONE btrfs_inode_ref or |
778 | * btrfs_inode_extref. | |
31db9f7c AB |
779 | * The iterate callback may return a non zero value to stop iteration. This can |
780 | * be a negative value for error codes or 1 to simply stop it. | |
781 | * | |
96b5bd77 | 782 | * path must point to the INODE_REF or INODE_EXTREF when called. |
31db9f7c | 783 | */ |
924794c9 | 784 | static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, |
31db9f7c AB |
785 | struct btrfs_key *found_key, int resolve, |
786 | iterate_inode_ref_t iterate, void *ctx) | |
787 | { | |
96b5bd77 | 788 | struct extent_buffer *eb = path->nodes[0]; |
31db9f7c AB |
789 | struct btrfs_item *item; |
790 | struct btrfs_inode_ref *iref; | |
96b5bd77 | 791 | struct btrfs_inode_extref *extref; |
31db9f7c AB |
792 | struct btrfs_path *tmp_path; |
793 | struct fs_path *p; | |
96b5bd77 | 794 | u32 cur = 0; |
31db9f7c | 795 | u32 total; |
96b5bd77 | 796 | int slot = path->slots[0]; |
31db9f7c AB |
797 | u32 name_len; |
798 | char *start; | |
799 | int ret = 0; | |
96b5bd77 | 800 | int num = 0; |
31db9f7c | 801 | int index; |
96b5bd77 JS |
802 | u64 dir; |
803 | unsigned long name_off; | |
804 | unsigned long elem_size; | |
805 | unsigned long ptr; | |
31db9f7c | 806 | |
924794c9 | 807 | p = fs_path_alloc_reversed(); |
31db9f7c AB |
808 | if (!p) |
809 | return -ENOMEM; | |
810 | ||
811 | tmp_path = alloc_path_for_send(); | |
812 | if (!tmp_path) { | |
924794c9 | 813 | fs_path_free(p); |
31db9f7c AB |
814 | return -ENOMEM; |
815 | } | |
816 | ||
31db9f7c | 817 | |
96b5bd77 JS |
818 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
819 | ptr = (unsigned long)btrfs_item_ptr(eb, slot, | |
820 | struct btrfs_inode_ref); | |
dd3cc16b | 821 | item = btrfs_item_nr(slot); |
96b5bd77 JS |
822 | total = btrfs_item_size(eb, item); |
823 | elem_size = sizeof(*iref); | |
824 | } else { | |
825 | ptr = btrfs_item_ptr_offset(eb, slot); | |
826 | total = btrfs_item_size_nr(eb, slot); | |
827 | elem_size = sizeof(*extref); | |
828 | } | |
829 | ||
31db9f7c AB |
830 | while (cur < total) { |
831 | fs_path_reset(p); | |
832 | ||
96b5bd77 JS |
833 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
834 | iref = (struct btrfs_inode_ref *)(ptr + cur); | |
835 | name_len = btrfs_inode_ref_name_len(eb, iref); | |
836 | name_off = (unsigned long)(iref + 1); | |
837 | index = btrfs_inode_ref_index(eb, iref); | |
838 | dir = found_key->offset; | |
839 | } else { | |
840 | extref = (struct btrfs_inode_extref *)(ptr + cur); | |
841 | name_len = btrfs_inode_extref_name_len(eb, extref); | |
842 | name_off = (unsigned long)&extref->name; | |
843 | index = btrfs_inode_extref_index(eb, extref); | |
844 | dir = btrfs_inode_extref_parent(eb, extref); | |
845 | } | |
846 | ||
31db9f7c | 847 | if (resolve) { |
96b5bd77 JS |
848 | start = btrfs_ref_to_path(root, tmp_path, name_len, |
849 | name_off, eb, dir, | |
850 | p->buf, p->buf_len); | |
31db9f7c AB |
851 | if (IS_ERR(start)) { |
852 | ret = PTR_ERR(start); | |
853 | goto out; | |
854 | } | |
855 | if (start < p->buf) { | |
856 | /* overflow , try again with larger buffer */ | |
857 | ret = fs_path_ensure_buf(p, | |
858 | p->buf_len + p->buf - start); | |
859 | if (ret < 0) | |
860 | goto out; | |
96b5bd77 JS |
861 | start = btrfs_ref_to_path(root, tmp_path, |
862 | name_len, name_off, | |
863 | eb, dir, | |
864 | p->buf, p->buf_len); | |
31db9f7c AB |
865 | if (IS_ERR(start)) { |
866 | ret = PTR_ERR(start); | |
867 | goto out; | |
868 | } | |
869 | BUG_ON(start < p->buf); | |
870 | } | |
871 | p->start = start; | |
872 | } else { | |
96b5bd77 JS |
873 | ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
874 | name_len); | |
31db9f7c AB |
875 | if (ret < 0) |
876 | goto out; | |
877 | } | |
878 | ||
96b5bd77 JS |
879 | cur += elem_size + name_len; |
880 | ret = iterate(num, dir, index, p, ctx); | |
31db9f7c AB |
881 | if (ret) |
882 | goto out; | |
31db9f7c AB |
883 | num++; |
884 | } | |
885 | ||
886 | out: | |
887 | btrfs_free_path(tmp_path); | |
924794c9 | 888 | fs_path_free(p); |
31db9f7c AB |
889 | return ret; |
890 | } | |
891 | ||
892 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, | |
893 | const char *name, int name_len, | |
894 | const char *data, int data_len, | |
895 | u8 type, void *ctx); | |
896 | ||
897 | /* | |
898 | * Helper function to iterate the entries in ONE btrfs_dir_item. | |
899 | * The iterate callback may return a non zero value to stop iteration. This can | |
900 | * be a negative value for error codes or 1 to simply stop it. | |
901 | * | |
902 | * path must point to the dir item when called. | |
903 | */ | |
924794c9 | 904 | static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
31db9f7c AB |
905 | struct btrfs_key *found_key, |
906 | iterate_dir_item_t iterate, void *ctx) | |
907 | { | |
908 | int ret = 0; | |
909 | struct extent_buffer *eb; | |
910 | struct btrfs_item *item; | |
911 | struct btrfs_dir_item *di; | |
31db9f7c AB |
912 | struct btrfs_key di_key; |
913 | char *buf = NULL; | |
914 | char *buf2 = NULL; | |
915 | int buf_len; | |
916 | int buf_virtual = 0; | |
917 | u32 name_len; | |
918 | u32 data_len; | |
919 | u32 cur; | |
920 | u32 len; | |
921 | u32 total; | |
922 | int slot; | |
923 | int num; | |
924 | u8 type; | |
925 | ||
926 | buf_len = PAGE_SIZE; | |
927 | buf = kmalloc(buf_len, GFP_NOFS); | |
928 | if (!buf) { | |
929 | ret = -ENOMEM; | |
930 | goto out; | |
931 | } | |
932 | ||
31db9f7c AB |
933 | eb = path->nodes[0]; |
934 | slot = path->slots[0]; | |
dd3cc16b | 935 | item = btrfs_item_nr(slot); |
31db9f7c AB |
936 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
937 | cur = 0; | |
938 | len = 0; | |
939 | total = btrfs_item_size(eb, item); | |
940 | ||
941 | num = 0; | |
942 | while (cur < total) { | |
943 | name_len = btrfs_dir_name_len(eb, di); | |
944 | data_len = btrfs_dir_data_len(eb, di); | |
945 | type = btrfs_dir_type(eb, di); | |
946 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); | |
947 | ||
948 | if (name_len + data_len > buf_len) { | |
949 | buf_len = PAGE_ALIGN(name_len + data_len); | |
950 | if (buf_virtual) { | |
951 | buf2 = vmalloc(buf_len); | |
952 | if (!buf2) { | |
953 | ret = -ENOMEM; | |
954 | goto out; | |
955 | } | |
956 | vfree(buf); | |
957 | } else { | |
958 | buf2 = krealloc(buf, buf_len, GFP_NOFS); | |
959 | if (!buf2) { | |
960 | buf2 = vmalloc(buf_len); | |
961 | if (!buf2) { | |
962 | ret = -ENOMEM; | |
963 | goto out; | |
964 | } | |
965 | kfree(buf); | |
966 | buf_virtual = 1; | |
967 | } | |
968 | } | |
969 | ||
970 | buf = buf2; | |
971 | buf2 = NULL; | |
972 | } | |
973 | ||
974 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), | |
975 | name_len + data_len); | |
976 | ||
977 | len = sizeof(*di) + name_len + data_len; | |
978 | di = (struct btrfs_dir_item *)((char *)di + len); | |
979 | cur += len; | |
980 | ||
981 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, | |
982 | data_len, type, ctx); | |
983 | if (ret < 0) | |
984 | goto out; | |
985 | if (ret) { | |
986 | ret = 0; | |
987 | goto out; | |
988 | } | |
989 | ||
990 | num++; | |
991 | } | |
992 | ||
993 | out: | |
31db9f7c AB |
994 | if (buf_virtual) |
995 | vfree(buf); | |
996 | else | |
997 | kfree(buf); | |
998 | return ret; | |
999 | } | |
1000 | ||
1001 | static int __copy_first_ref(int num, u64 dir, int index, | |
1002 | struct fs_path *p, void *ctx) | |
1003 | { | |
1004 | int ret; | |
1005 | struct fs_path *pt = ctx; | |
1006 | ||
1007 | ret = fs_path_copy(pt, p); | |
1008 | if (ret < 0) | |
1009 | return ret; | |
1010 | ||
1011 | /* we want the first only */ | |
1012 | return 1; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * Retrieve the first path of an inode. If an inode has more then one | |
1017 | * ref/hardlink, this is ignored. | |
1018 | */ | |
924794c9 | 1019 | static int get_inode_path(struct btrfs_root *root, |
31db9f7c AB |
1020 | u64 ino, struct fs_path *path) |
1021 | { | |
1022 | int ret; | |
1023 | struct btrfs_key key, found_key; | |
1024 | struct btrfs_path *p; | |
1025 | ||
1026 | p = alloc_path_for_send(); | |
1027 | if (!p) | |
1028 | return -ENOMEM; | |
1029 | ||
1030 | fs_path_reset(path); | |
1031 | ||
1032 | key.objectid = ino; | |
1033 | key.type = BTRFS_INODE_REF_KEY; | |
1034 | key.offset = 0; | |
1035 | ||
1036 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); | |
1037 | if (ret < 0) | |
1038 | goto out; | |
1039 | if (ret) { | |
1040 | ret = 1; | |
1041 | goto out; | |
1042 | } | |
1043 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); | |
1044 | if (found_key.objectid != ino || | |
96b5bd77 JS |
1045 | (found_key.type != BTRFS_INODE_REF_KEY && |
1046 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { | |
31db9f7c AB |
1047 | ret = -ENOENT; |
1048 | goto out; | |
1049 | } | |
1050 | ||
924794c9 TI |
1051 | ret = iterate_inode_ref(root, p, &found_key, 1, |
1052 | __copy_first_ref, path); | |
31db9f7c AB |
1053 | if (ret < 0) |
1054 | goto out; | |
1055 | ret = 0; | |
1056 | ||
1057 | out: | |
1058 | btrfs_free_path(p); | |
1059 | return ret; | |
1060 | } | |
1061 | ||
1062 | struct backref_ctx { | |
1063 | struct send_ctx *sctx; | |
1064 | ||
1065 | /* number of total found references */ | |
1066 | u64 found; | |
1067 | ||
1068 | /* | |
1069 | * used for clones found in send_root. clones found behind cur_objectid | |
1070 | * and cur_offset are not considered as allowed clones. | |
1071 | */ | |
1072 | u64 cur_objectid; | |
1073 | u64 cur_offset; | |
1074 | ||
1075 | /* may be truncated in case it's the last extent in a file */ | |
1076 | u64 extent_len; | |
1077 | ||
1078 | /* Just to check for bugs in backref resolving */ | |
ee849c04 | 1079 | int found_itself; |
31db9f7c AB |
1080 | }; |
1081 | ||
1082 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) | |
1083 | { | |
995e01b7 | 1084 | u64 root = (u64)(uintptr_t)key; |
31db9f7c AB |
1085 | struct clone_root *cr = (struct clone_root *)elt; |
1086 | ||
1087 | if (root < cr->root->objectid) | |
1088 | return -1; | |
1089 | if (root > cr->root->objectid) | |
1090 | return 1; | |
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | static int __clone_root_cmp_sort(const void *e1, const void *e2) | |
1095 | { | |
1096 | struct clone_root *cr1 = (struct clone_root *)e1; | |
1097 | struct clone_root *cr2 = (struct clone_root *)e2; | |
1098 | ||
1099 | if (cr1->root->objectid < cr2->root->objectid) | |
1100 | return -1; | |
1101 | if (cr1->root->objectid > cr2->root->objectid) | |
1102 | return 1; | |
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | /* | |
1107 | * Called for every backref that is found for the current extent. | |
766702ef | 1108 | * Results are collected in sctx->clone_roots->ino/offset/found_refs |
31db9f7c AB |
1109 | */ |
1110 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) | |
1111 | { | |
1112 | struct backref_ctx *bctx = ctx_; | |
1113 | struct clone_root *found; | |
1114 | int ret; | |
1115 | u64 i_size; | |
1116 | ||
1117 | /* First check if the root is in the list of accepted clone sources */ | |
995e01b7 | 1118 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
31db9f7c AB |
1119 | bctx->sctx->clone_roots_cnt, |
1120 | sizeof(struct clone_root), | |
1121 | __clone_root_cmp_bsearch); | |
1122 | if (!found) | |
1123 | return 0; | |
1124 | ||
1125 | if (found->root == bctx->sctx->send_root && | |
1126 | ino == bctx->cur_objectid && | |
1127 | offset == bctx->cur_offset) { | |
ee849c04 | 1128 | bctx->found_itself = 1; |
31db9f7c AB |
1129 | } |
1130 | ||
1131 | /* | |
766702ef | 1132 | * There are inodes that have extents that lie behind its i_size. Don't |
31db9f7c AB |
1133 | * accept clones from these extents. |
1134 | */ | |
85a7b33b AB |
1135 | ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL, |
1136 | NULL); | |
31db9f7c AB |
1137 | if (ret < 0) |
1138 | return ret; | |
1139 | ||
1140 | if (offset + bctx->extent_len > i_size) | |
1141 | return 0; | |
1142 | ||
1143 | /* | |
1144 | * Make sure we don't consider clones from send_root that are | |
1145 | * behind the current inode/offset. | |
1146 | */ | |
1147 | if (found->root == bctx->sctx->send_root) { | |
1148 | /* | |
1149 | * TODO for the moment we don't accept clones from the inode | |
1150 | * that is currently send. We may change this when | |
1151 | * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same | |
1152 | * file. | |
1153 | */ | |
1154 | if (ino >= bctx->cur_objectid) | |
1155 | return 0; | |
e938c8ad AB |
1156 | #if 0 |
1157 | if (ino > bctx->cur_objectid) | |
1158 | return 0; | |
1159 | if (offset + bctx->extent_len > bctx->cur_offset) | |
31db9f7c | 1160 | return 0; |
e938c8ad | 1161 | #endif |
31db9f7c AB |
1162 | } |
1163 | ||
1164 | bctx->found++; | |
1165 | found->found_refs++; | |
1166 | if (ino < found->ino) { | |
1167 | found->ino = ino; | |
1168 | found->offset = offset; | |
1169 | } else if (found->ino == ino) { | |
1170 | /* | |
1171 | * same extent found more then once in the same file. | |
1172 | */ | |
1173 | if (found->offset > offset + bctx->extent_len) | |
1174 | found->offset = offset; | |
1175 | } | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | /* | |
766702ef AB |
1181 | * Given an inode, offset and extent item, it finds a good clone for a clone |
1182 | * instruction. Returns -ENOENT when none could be found. The function makes | |
1183 | * sure that the returned clone is usable at the point where sending is at the | |
1184 | * moment. This means, that no clones are accepted which lie behind the current | |
1185 | * inode+offset. | |
1186 | * | |
31db9f7c AB |
1187 | * path must point to the extent item when called. |
1188 | */ | |
1189 | static int find_extent_clone(struct send_ctx *sctx, | |
1190 | struct btrfs_path *path, | |
1191 | u64 ino, u64 data_offset, | |
1192 | u64 ino_size, | |
1193 | struct clone_root **found) | |
1194 | { | |
1195 | int ret; | |
1196 | int extent_type; | |
1197 | u64 logical; | |
74dd17fb | 1198 | u64 disk_byte; |
31db9f7c AB |
1199 | u64 num_bytes; |
1200 | u64 extent_item_pos; | |
69917e43 | 1201 | u64 flags = 0; |
31db9f7c AB |
1202 | struct btrfs_file_extent_item *fi; |
1203 | struct extent_buffer *eb = path->nodes[0]; | |
35075bb0 | 1204 | struct backref_ctx *backref_ctx = NULL; |
31db9f7c AB |
1205 | struct clone_root *cur_clone_root; |
1206 | struct btrfs_key found_key; | |
1207 | struct btrfs_path *tmp_path; | |
74dd17fb | 1208 | int compressed; |
31db9f7c AB |
1209 | u32 i; |
1210 | ||
1211 | tmp_path = alloc_path_for_send(); | |
1212 | if (!tmp_path) | |
1213 | return -ENOMEM; | |
1214 | ||
35075bb0 AB |
1215 | backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS); |
1216 | if (!backref_ctx) { | |
1217 | ret = -ENOMEM; | |
1218 | goto out; | |
1219 | } | |
1220 | ||
31db9f7c AB |
1221 | if (data_offset >= ino_size) { |
1222 | /* | |
1223 | * There may be extents that lie behind the file's size. | |
1224 | * I at least had this in combination with snapshotting while | |
1225 | * writing large files. | |
1226 | */ | |
1227 | ret = 0; | |
1228 | goto out; | |
1229 | } | |
1230 | ||
1231 | fi = btrfs_item_ptr(eb, path->slots[0], | |
1232 | struct btrfs_file_extent_item); | |
1233 | extent_type = btrfs_file_extent_type(eb, fi); | |
1234 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1235 | ret = -ENOENT; | |
1236 | goto out; | |
1237 | } | |
74dd17fb | 1238 | compressed = btrfs_file_extent_compression(eb, fi); |
31db9f7c AB |
1239 | |
1240 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); | |
74dd17fb CM |
1241 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
1242 | if (disk_byte == 0) { | |
31db9f7c AB |
1243 | ret = -ENOENT; |
1244 | goto out; | |
1245 | } | |
74dd17fb | 1246 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
31db9f7c | 1247 | |
69917e43 LB |
1248 | ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path, |
1249 | &found_key, &flags); | |
31db9f7c AB |
1250 | btrfs_release_path(tmp_path); |
1251 | ||
1252 | if (ret < 0) | |
1253 | goto out; | |
69917e43 | 1254 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
31db9f7c AB |
1255 | ret = -EIO; |
1256 | goto out; | |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * Setup the clone roots. | |
1261 | */ | |
1262 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1263 | cur_clone_root = sctx->clone_roots + i; | |
1264 | cur_clone_root->ino = (u64)-1; | |
1265 | cur_clone_root->offset = 0; | |
1266 | cur_clone_root->found_refs = 0; | |
1267 | } | |
1268 | ||
35075bb0 AB |
1269 | backref_ctx->sctx = sctx; |
1270 | backref_ctx->found = 0; | |
1271 | backref_ctx->cur_objectid = ino; | |
1272 | backref_ctx->cur_offset = data_offset; | |
1273 | backref_ctx->found_itself = 0; | |
1274 | backref_ctx->extent_len = num_bytes; | |
31db9f7c AB |
1275 | |
1276 | /* | |
1277 | * The last extent of a file may be too large due to page alignment. | |
1278 | * We need to adjust extent_len in this case so that the checks in | |
1279 | * __iterate_backrefs work. | |
1280 | */ | |
1281 | if (data_offset + num_bytes >= ino_size) | |
35075bb0 | 1282 | backref_ctx->extent_len = ino_size - data_offset; |
31db9f7c AB |
1283 | |
1284 | /* | |
1285 | * Now collect all backrefs. | |
1286 | */ | |
74dd17fb CM |
1287 | if (compressed == BTRFS_COMPRESS_NONE) |
1288 | extent_item_pos = logical - found_key.objectid; | |
1289 | else | |
1290 | extent_item_pos = 0; | |
31db9f7c AB |
1291 | ret = iterate_extent_inodes(sctx->send_root->fs_info, |
1292 | found_key.objectid, extent_item_pos, 1, | |
35075bb0 | 1293 | __iterate_backrefs, backref_ctx); |
74dd17fb | 1294 | |
31db9f7c AB |
1295 | if (ret < 0) |
1296 | goto out; | |
1297 | ||
35075bb0 | 1298 | if (!backref_ctx->found_itself) { |
31db9f7c AB |
1299 | /* found a bug in backref code? */ |
1300 | ret = -EIO; | |
efe120a0 | 1301 | btrfs_err(sctx->send_root->fs_info, "did not find backref in " |
31db9f7c | 1302 | "send_root. inode=%llu, offset=%llu, " |
74dd17fb CM |
1303 | "disk_byte=%llu found extent=%llu\n", |
1304 | ino, data_offset, disk_byte, found_key.objectid); | |
31db9f7c AB |
1305 | goto out; |
1306 | } | |
1307 | ||
1308 | verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, " | |
1309 | "ino=%llu, " | |
1310 | "num_bytes=%llu, logical=%llu\n", | |
1311 | data_offset, ino, num_bytes, logical); | |
1312 | ||
35075bb0 | 1313 | if (!backref_ctx->found) |
31db9f7c AB |
1314 | verbose_printk("btrfs: no clones found\n"); |
1315 | ||
1316 | cur_clone_root = NULL; | |
1317 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1318 | if (sctx->clone_roots[i].found_refs) { | |
1319 | if (!cur_clone_root) | |
1320 | cur_clone_root = sctx->clone_roots + i; | |
1321 | else if (sctx->clone_roots[i].root == sctx->send_root) | |
1322 | /* prefer clones from send_root over others */ | |
1323 | cur_clone_root = sctx->clone_roots + i; | |
31db9f7c AB |
1324 | } |
1325 | ||
1326 | } | |
1327 | ||
1328 | if (cur_clone_root) { | |
93de4ba8 FDBM |
1329 | if (compressed != BTRFS_COMPRESS_NONE) { |
1330 | /* | |
1331 | * Offsets given by iterate_extent_inodes() are relative | |
1332 | * to the start of the extent, we need to add logical | |
1333 | * offset from the file extent item. | |
1334 | * (See why at backref.c:check_extent_in_eb()) | |
1335 | */ | |
1336 | cur_clone_root->offset += btrfs_file_extent_offset(eb, | |
1337 | fi); | |
1338 | } | |
31db9f7c AB |
1339 | *found = cur_clone_root; |
1340 | ret = 0; | |
1341 | } else { | |
1342 | ret = -ENOENT; | |
1343 | } | |
1344 | ||
1345 | out: | |
1346 | btrfs_free_path(tmp_path); | |
35075bb0 | 1347 | kfree(backref_ctx); |
31db9f7c AB |
1348 | return ret; |
1349 | } | |
1350 | ||
924794c9 | 1351 | static int read_symlink(struct btrfs_root *root, |
31db9f7c AB |
1352 | u64 ino, |
1353 | struct fs_path *dest) | |
1354 | { | |
1355 | int ret; | |
1356 | struct btrfs_path *path; | |
1357 | struct btrfs_key key; | |
1358 | struct btrfs_file_extent_item *ei; | |
1359 | u8 type; | |
1360 | u8 compression; | |
1361 | unsigned long off; | |
1362 | int len; | |
1363 | ||
1364 | path = alloc_path_for_send(); | |
1365 | if (!path) | |
1366 | return -ENOMEM; | |
1367 | ||
1368 | key.objectid = ino; | |
1369 | key.type = BTRFS_EXTENT_DATA_KEY; | |
1370 | key.offset = 0; | |
1371 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1372 | if (ret < 0) | |
1373 | goto out; | |
1374 | BUG_ON(ret); | |
1375 | ||
1376 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1377 | struct btrfs_file_extent_item); | |
1378 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
1379 | compression = btrfs_file_extent_compression(path->nodes[0], ei); | |
1380 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); | |
1381 | BUG_ON(compression); | |
1382 | ||
1383 | off = btrfs_file_extent_inline_start(ei); | |
514ac8ad | 1384 | len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei); |
31db9f7c AB |
1385 | |
1386 | ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); | |
31db9f7c AB |
1387 | |
1388 | out: | |
1389 | btrfs_free_path(path); | |
1390 | return ret; | |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * Helper function to generate a file name that is unique in the root of | |
1395 | * send_root and parent_root. This is used to generate names for orphan inodes. | |
1396 | */ | |
1397 | static int gen_unique_name(struct send_ctx *sctx, | |
1398 | u64 ino, u64 gen, | |
1399 | struct fs_path *dest) | |
1400 | { | |
1401 | int ret = 0; | |
1402 | struct btrfs_path *path; | |
1403 | struct btrfs_dir_item *di; | |
1404 | char tmp[64]; | |
1405 | int len; | |
1406 | u64 idx = 0; | |
1407 | ||
1408 | path = alloc_path_for_send(); | |
1409 | if (!path) | |
1410 | return -ENOMEM; | |
1411 | ||
1412 | while (1) { | |
f74b86d8 | 1413 | len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
31db9f7c | 1414 | ino, gen, idx); |
64792f25 | 1415 | ASSERT(len < sizeof(tmp)); |
31db9f7c AB |
1416 | |
1417 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, | |
1418 | path, BTRFS_FIRST_FREE_OBJECTID, | |
1419 | tmp, strlen(tmp), 0); | |
1420 | btrfs_release_path(path); | |
1421 | if (IS_ERR(di)) { | |
1422 | ret = PTR_ERR(di); | |
1423 | goto out; | |
1424 | } | |
1425 | if (di) { | |
1426 | /* not unique, try again */ | |
1427 | idx++; | |
1428 | continue; | |
1429 | } | |
1430 | ||
1431 | if (!sctx->parent_root) { | |
1432 | /* unique */ | |
1433 | ret = 0; | |
1434 | break; | |
1435 | } | |
1436 | ||
1437 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, | |
1438 | path, BTRFS_FIRST_FREE_OBJECTID, | |
1439 | tmp, strlen(tmp), 0); | |
1440 | btrfs_release_path(path); | |
1441 | if (IS_ERR(di)) { | |
1442 | ret = PTR_ERR(di); | |
1443 | goto out; | |
1444 | } | |
1445 | if (di) { | |
1446 | /* not unique, try again */ | |
1447 | idx++; | |
1448 | continue; | |
1449 | } | |
1450 | /* unique */ | |
1451 | break; | |
1452 | } | |
1453 | ||
1454 | ret = fs_path_add(dest, tmp, strlen(tmp)); | |
1455 | ||
1456 | out: | |
1457 | btrfs_free_path(path); | |
1458 | return ret; | |
1459 | } | |
1460 | ||
1461 | enum inode_state { | |
1462 | inode_state_no_change, | |
1463 | inode_state_will_create, | |
1464 | inode_state_did_create, | |
1465 | inode_state_will_delete, | |
1466 | inode_state_did_delete, | |
1467 | }; | |
1468 | ||
1469 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) | |
1470 | { | |
1471 | int ret; | |
1472 | int left_ret; | |
1473 | int right_ret; | |
1474 | u64 left_gen; | |
1475 | u64 right_gen; | |
1476 | ||
1477 | ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, | |
85a7b33b | 1478 | NULL, NULL); |
31db9f7c AB |
1479 | if (ret < 0 && ret != -ENOENT) |
1480 | goto out; | |
1481 | left_ret = ret; | |
1482 | ||
1483 | if (!sctx->parent_root) { | |
1484 | right_ret = -ENOENT; | |
1485 | } else { | |
1486 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, | |
85a7b33b | 1487 | NULL, NULL, NULL, NULL); |
31db9f7c AB |
1488 | if (ret < 0 && ret != -ENOENT) |
1489 | goto out; | |
1490 | right_ret = ret; | |
1491 | } | |
1492 | ||
1493 | if (!left_ret && !right_ret) { | |
e938c8ad | 1494 | if (left_gen == gen && right_gen == gen) { |
31db9f7c | 1495 | ret = inode_state_no_change; |
e938c8ad | 1496 | } else if (left_gen == gen) { |
31db9f7c AB |
1497 | if (ino < sctx->send_progress) |
1498 | ret = inode_state_did_create; | |
1499 | else | |
1500 | ret = inode_state_will_create; | |
1501 | } else if (right_gen == gen) { | |
1502 | if (ino < sctx->send_progress) | |
1503 | ret = inode_state_did_delete; | |
1504 | else | |
1505 | ret = inode_state_will_delete; | |
1506 | } else { | |
1507 | ret = -ENOENT; | |
1508 | } | |
1509 | } else if (!left_ret) { | |
1510 | if (left_gen == gen) { | |
1511 | if (ino < sctx->send_progress) | |
1512 | ret = inode_state_did_create; | |
1513 | else | |
1514 | ret = inode_state_will_create; | |
1515 | } else { | |
1516 | ret = -ENOENT; | |
1517 | } | |
1518 | } else if (!right_ret) { | |
1519 | if (right_gen == gen) { | |
1520 | if (ino < sctx->send_progress) | |
1521 | ret = inode_state_did_delete; | |
1522 | else | |
1523 | ret = inode_state_will_delete; | |
1524 | } else { | |
1525 | ret = -ENOENT; | |
1526 | } | |
1527 | } else { | |
1528 | ret = -ENOENT; | |
1529 | } | |
1530 | ||
1531 | out: | |
1532 | return ret; | |
1533 | } | |
1534 | ||
1535 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) | |
1536 | { | |
1537 | int ret; | |
1538 | ||
1539 | ret = get_cur_inode_state(sctx, ino, gen); | |
1540 | if (ret < 0) | |
1541 | goto out; | |
1542 | ||
1543 | if (ret == inode_state_no_change || | |
1544 | ret == inode_state_did_create || | |
1545 | ret == inode_state_will_delete) | |
1546 | ret = 1; | |
1547 | else | |
1548 | ret = 0; | |
1549 | ||
1550 | out: | |
1551 | return ret; | |
1552 | } | |
1553 | ||
1554 | /* | |
1555 | * Helper function to lookup a dir item in a dir. | |
1556 | */ | |
1557 | static int lookup_dir_item_inode(struct btrfs_root *root, | |
1558 | u64 dir, const char *name, int name_len, | |
1559 | u64 *found_inode, | |
1560 | u8 *found_type) | |
1561 | { | |
1562 | int ret = 0; | |
1563 | struct btrfs_dir_item *di; | |
1564 | struct btrfs_key key; | |
1565 | struct btrfs_path *path; | |
1566 | ||
1567 | path = alloc_path_for_send(); | |
1568 | if (!path) | |
1569 | return -ENOMEM; | |
1570 | ||
1571 | di = btrfs_lookup_dir_item(NULL, root, path, | |
1572 | dir, name, name_len, 0); | |
1573 | if (!di) { | |
1574 | ret = -ENOENT; | |
1575 | goto out; | |
1576 | } | |
1577 | if (IS_ERR(di)) { | |
1578 | ret = PTR_ERR(di); | |
1579 | goto out; | |
1580 | } | |
1581 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
1582 | *found_inode = key.objectid; | |
1583 | *found_type = btrfs_dir_type(path->nodes[0], di); | |
1584 | ||
1585 | out: | |
1586 | btrfs_free_path(path); | |
1587 | return ret; | |
1588 | } | |
1589 | ||
766702ef AB |
1590 | /* |
1591 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, | |
1592 | * generation of the parent dir and the name of the dir entry. | |
1593 | */ | |
924794c9 | 1594 | static int get_first_ref(struct btrfs_root *root, u64 ino, |
31db9f7c AB |
1595 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
1596 | { | |
1597 | int ret; | |
1598 | struct btrfs_key key; | |
1599 | struct btrfs_key found_key; | |
1600 | struct btrfs_path *path; | |
31db9f7c | 1601 | int len; |
96b5bd77 | 1602 | u64 parent_dir; |
31db9f7c AB |
1603 | |
1604 | path = alloc_path_for_send(); | |
1605 | if (!path) | |
1606 | return -ENOMEM; | |
1607 | ||
1608 | key.objectid = ino; | |
1609 | key.type = BTRFS_INODE_REF_KEY; | |
1610 | key.offset = 0; | |
1611 | ||
1612 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
1613 | if (ret < 0) | |
1614 | goto out; | |
1615 | if (!ret) | |
1616 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1617 | path->slots[0]); | |
96b5bd77 JS |
1618 | if (ret || found_key.objectid != ino || |
1619 | (found_key.type != BTRFS_INODE_REF_KEY && | |
1620 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { | |
31db9f7c AB |
1621 | ret = -ENOENT; |
1622 | goto out; | |
1623 | } | |
1624 | ||
96b5bd77 JS |
1625 | if (key.type == BTRFS_INODE_REF_KEY) { |
1626 | struct btrfs_inode_ref *iref; | |
1627 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1628 | struct btrfs_inode_ref); | |
1629 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); | |
1630 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], | |
1631 | (unsigned long)(iref + 1), | |
1632 | len); | |
1633 | parent_dir = found_key.offset; | |
1634 | } else { | |
1635 | struct btrfs_inode_extref *extref; | |
1636 | extref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1637 | struct btrfs_inode_extref); | |
1638 | len = btrfs_inode_extref_name_len(path->nodes[0], extref); | |
1639 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], | |
1640 | (unsigned long)&extref->name, len); | |
1641 | parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); | |
1642 | } | |
31db9f7c AB |
1643 | if (ret < 0) |
1644 | goto out; | |
1645 | btrfs_release_path(path); | |
1646 | ||
96b5bd77 | 1647 | ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL, |
85a7b33b | 1648 | NULL, NULL); |
31db9f7c AB |
1649 | if (ret < 0) |
1650 | goto out; | |
1651 | ||
96b5bd77 | 1652 | *dir = parent_dir; |
31db9f7c AB |
1653 | |
1654 | out: | |
1655 | btrfs_free_path(path); | |
1656 | return ret; | |
1657 | } | |
1658 | ||
924794c9 | 1659 | static int is_first_ref(struct btrfs_root *root, |
31db9f7c AB |
1660 | u64 ino, u64 dir, |
1661 | const char *name, int name_len) | |
1662 | { | |
1663 | int ret; | |
1664 | struct fs_path *tmp_name; | |
1665 | u64 tmp_dir; | |
1666 | u64 tmp_dir_gen; | |
1667 | ||
924794c9 | 1668 | tmp_name = fs_path_alloc(); |
31db9f7c AB |
1669 | if (!tmp_name) |
1670 | return -ENOMEM; | |
1671 | ||
924794c9 | 1672 | ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name); |
31db9f7c AB |
1673 | if (ret < 0) |
1674 | goto out; | |
1675 | ||
b9291aff | 1676 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
31db9f7c AB |
1677 | ret = 0; |
1678 | goto out; | |
1679 | } | |
1680 | ||
e938c8ad | 1681 | ret = !memcmp(tmp_name->start, name, name_len); |
31db9f7c AB |
1682 | |
1683 | out: | |
924794c9 | 1684 | fs_path_free(tmp_name); |
31db9f7c AB |
1685 | return ret; |
1686 | } | |
1687 | ||
766702ef AB |
1688 | /* |
1689 | * Used by process_recorded_refs to determine if a new ref would overwrite an | |
1690 | * already existing ref. In case it detects an overwrite, it returns the | |
1691 | * inode/gen in who_ino/who_gen. | |
1692 | * When an overwrite is detected, process_recorded_refs does proper orphanizing | |
1693 | * to make sure later references to the overwritten inode are possible. | |
1694 | * Orphanizing is however only required for the first ref of an inode. | |
1695 | * process_recorded_refs does an additional is_first_ref check to see if | |
1696 | * orphanizing is really required. | |
1697 | */ | |
31db9f7c AB |
1698 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
1699 | const char *name, int name_len, | |
1700 | u64 *who_ino, u64 *who_gen) | |
1701 | { | |
1702 | int ret = 0; | |
ebdad913 | 1703 | u64 gen; |
31db9f7c AB |
1704 | u64 other_inode = 0; |
1705 | u8 other_type = 0; | |
1706 | ||
1707 | if (!sctx->parent_root) | |
1708 | goto out; | |
1709 | ||
1710 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1711 | if (ret <= 0) | |
1712 | goto out; | |
1713 | ||
ebdad913 JB |
1714 | /* |
1715 | * If we have a parent root we need to verify that the parent dir was | |
1716 | * not delted and then re-created, if it was then we have no overwrite | |
1717 | * and we can just unlink this entry. | |
1718 | */ | |
1719 | if (sctx->parent_root) { | |
1720 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, | |
1721 | NULL, NULL, NULL); | |
1722 | if (ret < 0 && ret != -ENOENT) | |
1723 | goto out; | |
1724 | if (ret) { | |
1725 | ret = 0; | |
1726 | goto out; | |
1727 | } | |
1728 | if (gen != dir_gen) | |
1729 | goto out; | |
1730 | } | |
1731 | ||
31db9f7c AB |
1732 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
1733 | &other_inode, &other_type); | |
1734 | if (ret < 0 && ret != -ENOENT) | |
1735 | goto out; | |
1736 | if (ret) { | |
1737 | ret = 0; | |
1738 | goto out; | |
1739 | } | |
1740 | ||
766702ef AB |
1741 | /* |
1742 | * Check if the overwritten ref was already processed. If yes, the ref | |
1743 | * was already unlinked/moved, so we can safely assume that we will not | |
1744 | * overwrite anything at this point in time. | |
1745 | */ | |
31db9f7c AB |
1746 | if (other_inode > sctx->send_progress) { |
1747 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, | |
85a7b33b | 1748 | who_gen, NULL, NULL, NULL, NULL); |
31db9f7c AB |
1749 | if (ret < 0) |
1750 | goto out; | |
1751 | ||
1752 | ret = 1; | |
1753 | *who_ino = other_inode; | |
1754 | } else { | |
1755 | ret = 0; | |
1756 | } | |
1757 | ||
1758 | out: | |
1759 | return ret; | |
1760 | } | |
1761 | ||
766702ef AB |
1762 | /* |
1763 | * Checks if the ref was overwritten by an already processed inode. This is | |
1764 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and | |
1765 | * thus the orphan name needs be used. | |
1766 | * process_recorded_refs also uses it to avoid unlinking of refs that were | |
1767 | * overwritten. | |
1768 | */ | |
31db9f7c AB |
1769 | static int did_overwrite_ref(struct send_ctx *sctx, |
1770 | u64 dir, u64 dir_gen, | |
1771 | u64 ino, u64 ino_gen, | |
1772 | const char *name, int name_len) | |
1773 | { | |
1774 | int ret = 0; | |
1775 | u64 gen; | |
1776 | u64 ow_inode; | |
1777 | u8 other_type; | |
1778 | ||
1779 | if (!sctx->parent_root) | |
1780 | goto out; | |
1781 | ||
1782 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1783 | if (ret <= 0) | |
1784 | goto out; | |
1785 | ||
1786 | /* check if the ref was overwritten by another ref */ | |
1787 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, | |
1788 | &ow_inode, &other_type); | |
1789 | if (ret < 0 && ret != -ENOENT) | |
1790 | goto out; | |
1791 | if (ret) { | |
1792 | /* was never and will never be overwritten */ | |
1793 | ret = 0; | |
1794 | goto out; | |
1795 | } | |
1796 | ||
1797 | ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, | |
85a7b33b | 1798 | NULL, NULL); |
31db9f7c AB |
1799 | if (ret < 0) |
1800 | goto out; | |
1801 | ||
1802 | if (ow_inode == ino && gen == ino_gen) { | |
1803 | ret = 0; | |
1804 | goto out; | |
1805 | } | |
1806 | ||
1807 | /* we know that it is or will be overwritten. check this now */ | |
1808 | if (ow_inode < sctx->send_progress) | |
1809 | ret = 1; | |
1810 | else | |
1811 | ret = 0; | |
1812 | ||
1813 | out: | |
1814 | return ret; | |
1815 | } | |
1816 | ||
766702ef AB |
1817 | /* |
1818 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode | |
1819 | * that got overwritten. This is used by process_recorded_refs to determine | |
1820 | * if it has to use the path as returned by get_cur_path or the orphan name. | |
1821 | */ | |
31db9f7c AB |
1822 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
1823 | { | |
1824 | int ret = 0; | |
1825 | struct fs_path *name = NULL; | |
1826 | u64 dir; | |
1827 | u64 dir_gen; | |
1828 | ||
1829 | if (!sctx->parent_root) | |
1830 | goto out; | |
1831 | ||
924794c9 | 1832 | name = fs_path_alloc(); |
31db9f7c AB |
1833 | if (!name) |
1834 | return -ENOMEM; | |
1835 | ||
924794c9 | 1836 | ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
31db9f7c AB |
1837 | if (ret < 0) |
1838 | goto out; | |
1839 | ||
1840 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, | |
1841 | name->start, fs_path_len(name)); | |
31db9f7c AB |
1842 | |
1843 | out: | |
924794c9 | 1844 | fs_path_free(name); |
31db9f7c AB |
1845 | return ret; |
1846 | } | |
1847 | ||
766702ef AB |
1848 | /* |
1849 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, | |
1850 | * so we need to do some special handling in case we have clashes. This function | |
1851 | * takes care of this with the help of name_cache_entry::radix_list. | |
5dc67d0b | 1852 | * In case of error, nce is kfreed. |
766702ef | 1853 | */ |
31db9f7c AB |
1854 | static int name_cache_insert(struct send_ctx *sctx, |
1855 | struct name_cache_entry *nce) | |
1856 | { | |
1857 | int ret = 0; | |
7e0926fe AB |
1858 | struct list_head *nce_head; |
1859 | ||
1860 | nce_head = radix_tree_lookup(&sctx->name_cache, | |
1861 | (unsigned long)nce->ino); | |
1862 | if (!nce_head) { | |
1863 | nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); | |
cfa7a9cc TI |
1864 | if (!nce_head) { |
1865 | kfree(nce); | |
31db9f7c | 1866 | return -ENOMEM; |
cfa7a9cc | 1867 | } |
7e0926fe | 1868 | INIT_LIST_HEAD(nce_head); |
31db9f7c | 1869 | |
7e0926fe | 1870 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
5dc67d0b AB |
1871 | if (ret < 0) { |
1872 | kfree(nce_head); | |
1873 | kfree(nce); | |
31db9f7c | 1874 | return ret; |
5dc67d0b | 1875 | } |
31db9f7c | 1876 | } |
7e0926fe | 1877 | list_add_tail(&nce->radix_list, nce_head); |
31db9f7c AB |
1878 | list_add_tail(&nce->list, &sctx->name_cache_list); |
1879 | sctx->name_cache_size++; | |
1880 | ||
1881 | return ret; | |
1882 | } | |
1883 | ||
1884 | static void name_cache_delete(struct send_ctx *sctx, | |
1885 | struct name_cache_entry *nce) | |
1886 | { | |
7e0926fe | 1887 | struct list_head *nce_head; |
31db9f7c | 1888 | |
7e0926fe AB |
1889 | nce_head = radix_tree_lookup(&sctx->name_cache, |
1890 | (unsigned long)nce->ino); | |
57fb8910 DS |
1891 | if (!nce_head) { |
1892 | btrfs_err(sctx->send_root->fs_info, | |
1893 | "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", | |
1894 | nce->ino, sctx->name_cache_size); | |
1895 | } | |
31db9f7c | 1896 | |
7e0926fe | 1897 | list_del(&nce->radix_list); |
31db9f7c | 1898 | list_del(&nce->list); |
31db9f7c | 1899 | sctx->name_cache_size--; |
7e0926fe | 1900 | |
57fb8910 DS |
1901 | /* |
1902 | * We may not get to the final release of nce_head if the lookup fails | |
1903 | */ | |
1904 | if (nce_head && list_empty(nce_head)) { | |
7e0926fe AB |
1905 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
1906 | kfree(nce_head); | |
1907 | } | |
31db9f7c AB |
1908 | } |
1909 | ||
1910 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, | |
1911 | u64 ino, u64 gen) | |
1912 | { | |
7e0926fe AB |
1913 | struct list_head *nce_head; |
1914 | struct name_cache_entry *cur; | |
31db9f7c | 1915 | |
7e0926fe AB |
1916 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
1917 | if (!nce_head) | |
31db9f7c AB |
1918 | return NULL; |
1919 | ||
7e0926fe AB |
1920 | list_for_each_entry(cur, nce_head, radix_list) { |
1921 | if (cur->ino == ino && cur->gen == gen) | |
1922 | return cur; | |
1923 | } | |
31db9f7c AB |
1924 | return NULL; |
1925 | } | |
1926 | ||
766702ef AB |
1927 | /* |
1928 | * Removes the entry from the list and adds it back to the end. This marks the | |
1929 | * entry as recently used so that name_cache_clean_unused does not remove it. | |
1930 | */ | |
31db9f7c AB |
1931 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
1932 | { | |
1933 | list_del(&nce->list); | |
1934 | list_add_tail(&nce->list, &sctx->name_cache_list); | |
1935 | } | |
1936 | ||
766702ef AB |
1937 | /* |
1938 | * Remove some entries from the beginning of name_cache_list. | |
1939 | */ | |
31db9f7c AB |
1940 | static void name_cache_clean_unused(struct send_ctx *sctx) |
1941 | { | |
1942 | struct name_cache_entry *nce; | |
1943 | ||
1944 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) | |
1945 | return; | |
1946 | ||
1947 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { | |
1948 | nce = list_entry(sctx->name_cache_list.next, | |
1949 | struct name_cache_entry, list); | |
1950 | name_cache_delete(sctx, nce); | |
1951 | kfree(nce); | |
1952 | } | |
1953 | } | |
1954 | ||
1955 | static void name_cache_free(struct send_ctx *sctx) | |
1956 | { | |
1957 | struct name_cache_entry *nce; | |
31db9f7c | 1958 | |
e938c8ad AB |
1959 | while (!list_empty(&sctx->name_cache_list)) { |
1960 | nce = list_entry(sctx->name_cache_list.next, | |
1961 | struct name_cache_entry, list); | |
31db9f7c | 1962 | name_cache_delete(sctx, nce); |
17589bd9 | 1963 | kfree(nce); |
31db9f7c AB |
1964 | } |
1965 | } | |
1966 | ||
766702ef AB |
1967 | /* |
1968 | * Used by get_cur_path for each ref up to the root. | |
1969 | * Returns 0 if it succeeded. | |
1970 | * Returns 1 if the inode is not existent or got overwritten. In that case, the | |
1971 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 | |
1972 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. | |
1973 | * Returns <0 in case of error. | |
1974 | */ | |
31db9f7c AB |
1975 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
1976 | u64 ino, u64 gen, | |
9f03740a | 1977 | int skip_name_cache, |
31db9f7c AB |
1978 | u64 *parent_ino, |
1979 | u64 *parent_gen, | |
1980 | struct fs_path *dest) | |
1981 | { | |
1982 | int ret; | |
1983 | int nce_ret; | |
1984 | struct btrfs_path *path = NULL; | |
1985 | struct name_cache_entry *nce = NULL; | |
1986 | ||
9f03740a FDBM |
1987 | if (skip_name_cache) |
1988 | goto get_ref; | |
766702ef AB |
1989 | /* |
1990 | * First check if we already did a call to this function with the same | |
1991 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes | |
1992 | * return the cached result. | |
1993 | */ | |
31db9f7c AB |
1994 | nce = name_cache_search(sctx, ino, gen); |
1995 | if (nce) { | |
1996 | if (ino < sctx->send_progress && nce->need_later_update) { | |
1997 | name_cache_delete(sctx, nce); | |
1998 | kfree(nce); | |
1999 | nce = NULL; | |
2000 | } else { | |
2001 | name_cache_used(sctx, nce); | |
2002 | *parent_ino = nce->parent_ino; | |
2003 | *parent_gen = nce->parent_gen; | |
2004 | ret = fs_path_add(dest, nce->name, nce->name_len); | |
2005 | if (ret < 0) | |
2006 | goto out; | |
2007 | ret = nce->ret; | |
2008 | goto out; | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | path = alloc_path_for_send(); | |
2013 | if (!path) | |
2014 | return -ENOMEM; | |
2015 | ||
766702ef AB |
2016 | /* |
2017 | * If the inode is not existent yet, add the orphan name and return 1. | |
2018 | * This should only happen for the parent dir that we determine in | |
2019 | * __record_new_ref | |
2020 | */ | |
31db9f7c AB |
2021 | ret = is_inode_existent(sctx, ino, gen); |
2022 | if (ret < 0) | |
2023 | goto out; | |
2024 | ||
2025 | if (!ret) { | |
2026 | ret = gen_unique_name(sctx, ino, gen, dest); | |
2027 | if (ret < 0) | |
2028 | goto out; | |
2029 | ret = 1; | |
2030 | goto out_cache; | |
2031 | } | |
2032 | ||
9f03740a | 2033 | get_ref: |
766702ef AB |
2034 | /* |
2035 | * Depending on whether the inode was already processed or not, use | |
2036 | * send_root or parent_root for ref lookup. | |
2037 | */ | |
9f03740a | 2038 | if (ino < sctx->send_progress && !skip_name_cache) |
924794c9 TI |
2039 | ret = get_first_ref(sctx->send_root, ino, |
2040 | parent_ino, parent_gen, dest); | |
31db9f7c | 2041 | else |
924794c9 TI |
2042 | ret = get_first_ref(sctx->parent_root, ino, |
2043 | parent_ino, parent_gen, dest); | |
31db9f7c AB |
2044 | if (ret < 0) |
2045 | goto out; | |
2046 | ||
766702ef AB |
2047 | /* |
2048 | * Check if the ref was overwritten by an inode's ref that was processed | |
2049 | * earlier. If yes, treat as orphan and return 1. | |
2050 | */ | |
31db9f7c AB |
2051 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
2052 | dest->start, dest->end - dest->start); | |
2053 | if (ret < 0) | |
2054 | goto out; | |
2055 | if (ret) { | |
2056 | fs_path_reset(dest); | |
2057 | ret = gen_unique_name(sctx, ino, gen, dest); | |
2058 | if (ret < 0) | |
2059 | goto out; | |
2060 | ret = 1; | |
2061 | } | |
9f03740a FDBM |
2062 | if (skip_name_cache) |
2063 | goto out; | |
31db9f7c AB |
2064 | |
2065 | out_cache: | |
766702ef AB |
2066 | /* |
2067 | * Store the result of the lookup in the name cache. | |
2068 | */ | |
31db9f7c AB |
2069 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS); |
2070 | if (!nce) { | |
2071 | ret = -ENOMEM; | |
2072 | goto out; | |
2073 | } | |
2074 | ||
2075 | nce->ino = ino; | |
2076 | nce->gen = gen; | |
2077 | nce->parent_ino = *parent_ino; | |
2078 | nce->parent_gen = *parent_gen; | |
2079 | nce->name_len = fs_path_len(dest); | |
2080 | nce->ret = ret; | |
2081 | strcpy(nce->name, dest->start); | |
31db9f7c AB |
2082 | |
2083 | if (ino < sctx->send_progress) | |
2084 | nce->need_later_update = 0; | |
2085 | else | |
2086 | nce->need_later_update = 1; | |
2087 | ||
2088 | nce_ret = name_cache_insert(sctx, nce); | |
2089 | if (nce_ret < 0) | |
2090 | ret = nce_ret; | |
2091 | name_cache_clean_unused(sctx); | |
2092 | ||
2093 | out: | |
2094 | btrfs_free_path(path); | |
2095 | return ret; | |
2096 | } | |
2097 | ||
2098 | /* | |
2099 | * Magic happens here. This function returns the first ref to an inode as it | |
2100 | * would look like while receiving the stream at this point in time. | |
2101 | * We walk the path up to the root. For every inode in between, we check if it | |
2102 | * was already processed/sent. If yes, we continue with the parent as found | |
2103 | * in send_root. If not, we continue with the parent as found in parent_root. | |
2104 | * If we encounter an inode that was deleted at this point in time, we use the | |
2105 | * inodes "orphan" name instead of the real name and stop. Same with new inodes | |
2106 | * that were not created yet and overwritten inodes/refs. | |
2107 | * | |
2108 | * When do we have have orphan inodes: | |
2109 | * 1. When an inode is freshly created and thus no valid refs are available yet | |
2110 | * 2. When a directory lost all it's refs (deleted) but still has dir items | |
2111 | * inside which were not processed yet (pending for move/delete). If anyone | |
2112 | * tried to get the path to the dir items, it would get a path inside that | |
2113 | * orphan directory. | |
2114 | * 3. When an inode is moved around or gets new links, it may overwrite the ref | |
2115 | * of an unprocessed inode. If in that case the first ref would be | |
2116 | * overwritten, the overwritten inode gets "orphanized". Later when we | |
2117 | * process this overwritten inode, it is restored at a new place by moving | |
2118 | * the orphan inode. | |
2119 | * | |
2120 | * sctx->send_progress tells this function at which point in time receiving | |
2121 | * would be. | |
2122 | */ | |
2123 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, | |
2124 | struct fs_path *dest) | |
2125 | { | |
2126 | int ret = 0; | |
2127 | struct fs_path *name = NULL; | |
2128 | u64 parent_inode = 0; | |
2129 | u64 parent_gen = 0; | |
2130 | int stop = 0; | |
9f03740a | 2131 | int skip_name_cache = 0; |
31db9f7c | 2132 | |
924794c9 | 2133 | name = fs_path_alloc(); |
31db9f7c AB |
2134 | if (!name) { |
2135 | ret = -ENOMEM; | |
2136 | goto out; | |
2137 | } | |
2138 | ||
9f03740a FDBM |
2139 | if (is_waiting_for_move(sctx, ino)) |
2140 | skip_name_cache = 1; | |
2141 | ||
31db9f7c AB |
2142 | dest->reversed = 1; |
2143 | fs_path_reset(dest); | |
2144 | ||
2145 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { | |
2146 | fs_path_reset(name); | |
2147 | ||
9f03740a | 2148 | ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache, |
31db9f7c AB |
2149 | &parent_inode, &parent_gen, name); |
2150 | if (ret < 0) | |
2151 | goto out; | |
2152 | if (ret) | |
2153 | stop = 1; | |
2154 | ||
9f03740a | 2155 | if (!skip_name_cache && |
03cb4fb9 | 2156 | is_waiting_for_move(sctx, parent_inode)) |
9f03740a | 2157 | skip_name_cache = 1; |
9f03740a | 2158 | |
31db9f7c AB |
2159 | ret = fs_path_add_path(dest, name); |
2160 | if (ret < 0) | |
2161 | goto out; | |
2162 | ||
2163 | ino = parent_inode; | |
2164 | gen = parent_gen; | |
2165 | } | |
2166 | ||
2167 | out: | |
924794c9 | 2168 | fs_path_free(name); |
31db9f7c AB |
2169 | if (!ret) |
2170 | fs_path_unreverse(dest); | |
2171 | return ret; | |
2172 | } | |
2173 | ||
31db9f7c AB |
2174 | /* |
2175 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace | |
2176 | */ | |
2177 | static int send_subvol_begin(struct send_ctx *sctx) | |
2178 | { | |
2179 | int ret; | |
2180 | struct btrfs_root *send_root = sctx->send_root; | |
2181 | struct btrfs_root *parent_root = sctx->parent_root; | |
2182 | struct btrfs_path *path; | |
2183 | struct btrfs_key key; | |
2184 | struct btrfs_root_ref *ref; | |
2185 | struct extent_buffer *leaf; | |
2186 | char *name = NULL; | |
2187 | int namelen; | |
2188 | ||
ffcfaf81 | 2189 | path = btrfs_alloc_path(); |
31db9f7c AB |
2190 | if (!path) |
2191 | return -ENOMEM; | |
2192 | ||
2193 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS); | |
2194 | if (!name) { | |
2195 | btrfs_free_path(path); | |
2196 | return -ENOMEM; | |
2197 | } | |
2198 | ||
2199 | key.objectid = send_root->objectid; | |
2200 | key.type = BTRFS_ROOT_BACKREF_KEY; | |
2201 | key.offset = 0; | |
2202 | ||
2203 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, | |
2204 | &key, path, 1, 0); | |
2205 | if (ret < 0) | |
2206 | goto out; | |
2207 | if (ret) { | |
2208 | ret = -ENOENT; | |
2209 | goto out; | |
2210 | } | |
2211 | ||
2212 | leaf = path->nodes[0]; | |
2213 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2214 | if (key.type != BTRFS_ROOT_BACKREF_KEY || | |
2215 | key.objectid != send_root->objectid) { | |
2216 | ret = -ENOENT; | |
2217 | goto out; | |
2218 | } | |
2219 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
2220 | namelen = btrfs_root_ref_name_len(leaf, ref); | |
2221 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); | |
2222 | btrfs_release_path(path); | |
2223 | ||
31db9f7c AB |
2224 | if (parent_root) { |
2225 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); | |
2226 | if (ret < 0) | |
2227 | goto out; | |
2228 | } else { | |
2229 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); | |
2230 | if (ret < 0) | |
2231 | goto out; | |
2232 | } | |
2233 | ||
2234 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); | |
2235 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, | |
2236 | sctx->send_root->root_item.uuid); | |
2237 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, | |
5a0f4e2c | 2238 | le64_to_cpu(sctx->send_root->root_item.ctransid)); |
31db9f7c AB |
2239 | if (parent_root) { |
2240 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
2241 | sctx->parent_root->root_item.uuid); | |
2242 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, | |
5a0f4e2c | 2243 | le64_to_cpu(sctx->parent_root->root_item.ctransid)); |
31db9f7c AB |
2244 | } |
2245 | ||
2246 | ret = send_cmd(sctx); | |
2247 | ||
2248 | tlv_put_failure: | |
2249 | out: | |
2250 | btrfs_free_path(path); | |
2251 | kfree(name); | |
2252 | return ret; | |
2253 | } | |
2254 | ||
2255 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) | |
2256 | { | |
2257 | int ret = 0; | |
2258 | struct fs_path *p; | |
2259 | ||
2260 | verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size); | |
2261 | ||
924794c9 | 2262 | p = fs_path_alloc(); |
31db9f7c AB |
2263 | if (!p) |
2264 | return -ENOMEM; | |
2265 | ||
2266 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); | |
2267 | if (ret < 0) | |
2268 | goto out; | |
2269 | ||
2270 | ret = get_cur_path(sctx, ino, gen, p); | |
2271 | if (ret < 0) | |
2272 | goto out; | |
2273 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2274 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); | |
2275 | ||
2276 | ret = send_cmd(sctx); | |
2277 | ||
2278 | tlv_put_failure: | |
2279 | out: | |
924794c9 | 2280 | fs_path_free(p); |
31db9f7c AB |
2281 | return ret; |
2282 | } | |
2283 | ||
2284 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) | |
2285 | { | |
2286 | int ret = 0; | |
2287 | struct fs_path *p; | |
2288 | ||
2289 | verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode); | |
2290 | ||
924794c9 | 2291 | p = fs_path_alloc(); |
31db9f7c AB |
2292 | if (!p) |
2293 | return -ENOMEM; | |
2294 | ||
2295 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); | |
2296 | if (ret < 0) | |
2297 | goto out; | |
2298 | ||
2299 | ret = get_cur_path(sctx, ino, gen, p); | |
2300 | if (ret < 0) | |
2301 | goto out; | |
2302 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2303 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); | |
2304 | ||
2305 | ret = send_cmd(sctx); | |
2306 | ||
2307 | tlv_put_failure: | |
2308 | out: | |
924794c9 | 2309 | fs_path_free(p); |
31db9f7c AB |
2310 | return ret; |
2311 | } | |
2312 | ||
2313 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) | |
2314 | { | |
2315 | int ret = 0; | |
2316 | struct fs_path *p; | |
2317 | ||
2318 | verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid); | |
2319 | ||
924794c9 | 2320 | p = fs_path_alloc(); |
31db9f7c AB |
2321 | if (!p) |
2322 | return -ENOMEM; | |
2323 | ||
2324 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); | |
2325 | if (ret < 0) | |
2326 | goto out; | |
2327 | ||
2328 | ret = get_cur_path(sctx, ino, gen, p); | |
2329 | if (ret < 0) | |
2330 | goto out; | |
2331 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2332 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); | |
2333 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); | |
2334 | ||
2335 | ret = send_cmd(sctx); | |
2336 | ||
2337 | tlv_put_failure: | |
2338 | out: | |
924794c9 | 2339 | fs_path_free(p); |
31db9f7c AB |
2340 | return ret; |
2341 | } | |
2342 | ||
2343 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) | |
2344 | { | |
2345 | int ret = 0; | |
2346 | struct fs_path *p = NULL; | |
2347 | struct btrfs_inode_item *ii; | |
2348 | struct btrfs_path *path = NULL; | |
2349 | struct extent_buffer *eb; | |
2350 | struct btrfs_key key; | |
2351 | int slot; | |
2352 | ||
2353 | verbose_printk("btrfs: send_utimes %llu\n", ino); | |
2354 | ||
924794c9 | 2355 | p = fs_path_alloc(); |
31db9f7c AB |
2356 | if (!p) |
2357 | return -ENOMEM; | |
2358 | ||
2359 | path = alloc_path_for_send(); | |
2360 | if (!path) { | |
2361 | ret = -ENOMEM; | |
2362 | goto out; | |
2363 | } | |
2364 | ||
2365 | key.objectid = ino; | |
2366 | key.type = BTRFS_INODE_ITEM_KEY; | |
2367 | key.offset = 0; | |
2368 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); | |
2369 | if (ret < 0) | |
2370 | goto out; | |
2371 | ||
2372 | eb = path->nodes[0]; | |
2373 | slot = path->slots[0]; | |
2374 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
2375 | ||
2376 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); | |
2377 | if (ret < 0) | |
2378 | goto out; | |
2379 | ||
2380 | ret = get_cur_path(sctx, ino, gen, p); | |
2381 | if (ret < 0) | |
2382 | goto out; | |
2383 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2384 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, | |
2385 | btrfs_inode_atime(ii)); | |
2386 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, | |
2387 | btrfs_inode_mtime(ii)); | |
2388 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, | |
2389 | btrfs_inode_ctime(ii)); | |
766702ef | 2390 | /* TODO Add otime support when the otime patches get into upstream */ |
31db9f7c AB |
2391 | |
2392 | ret = send_cmd(sctx); | |
2393 | ||
2394 | tlv_put_failure: | |
2395 | out: | |
924794c9 | 2396 | fs_path_free(p); |
31db9f7c AB |
2397 | btrfs_free_path(path); |
2398 | return ret; | |
2399 | } | |
2400 | ||
2401 | /* | |
2402 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have | |
2403 | * a valid path yet because we did not process the refs yet. So, the inode | |
2404 | * is created as orphan. | |
2405 | */ | |
1f4692da | 2406 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
31db9f7c AB |
2407 | { |
2408 | int ret = 0; | |
31db9f7c | 2409 | struct fs_path *p; |
31db9f7c | 2410 | int cmd; |
1f4692da | 2411 | u64 gen; |
31db9f7c | 2412 | u64 mode; |
1f4692da | 2413 | u64 rdev; |
31db9f7c | 2414 | |
1f4692da | 2415 | verbose_printk("btrfs: send_create_inode %llu\n", ino); |
31db9f7c | 2416 | |
924794c9 | 2417 | p = fs_path_alloc(); |
31db9f7c AB |
2418 | if (!p) |
2419 | return -ENOMEM; | |
2420 | ||
1f4692da AB |
2421 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL, |
2422 | NULL, &rdev); | |
2423 | if (ret < 0) | |
2424 | goto out; | |
31db9f7c | 2425 | |
e938c8ad | 2426 | if (S_ISREG(mode)) { |
31db9f7c | 2427 | cmd = BTRFS_SEND_C_MKFILE; |
e938c8ad | 2428 | } else if (S_ISDIR(mode)) { |
31db9f7c | 2429 | cmd = BTRFS_SEND_C_MKDIR; |
e938c8ad | 2430 | } else if (S_ISLNK(mode)) { |
31db9f7c | 2431 | cmd = BTRFS_SEND_C_SYMLINK; |
e938c8ad | 2432 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
31db9f7c | 2433 | cmd = BTRFS_SEND_C_MKNOD; |
e938c8ad | 2434 | } else if (S_ISFIFO(mode)) { |
31db9f7c | 2435 | cmd = BTRFS_SEND_C_MKFIFO; |
e938c8ad | 2436 | } else if (S_ISSOCK(mode)) { |
31db9f7c | 2437 | cmd = BTRFS_SEND_C_MKSOCK; |
e938c8ad | 2438 | } else { |
31db9f7c AB |
2439 | printk(KERN_WARNING "btrfs: unexpected inode type %o", |
2440 | (int)(mode & S_IFMT)); | |
2441 | ret = -ENOTSUPP; | |
2442 | goto out; | |
2443 | } | |
2444 | ||
2445 | ret = begin_cmd(sctx, cmd); | |
2446 | if (ret < 0) | |
2447 | goto out; | |
2448 | ||
1f4692da | 2449 | ret = gen_unique_name(sctx, ino, gen, p); |
31db9f7c AB |
2450 | if (ret < 0) |
2451 | goto out; | |
2452 | ||
2453 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
1f4692da | 2454 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
31db9f7c AB |
2455 | |
2456 | if (S_ISLNK(mode)) { | |
2457 | fs_path_reset(p); | |
924794c9 | 2458 | ret = read_symlink(sctx->send_root, ino, p); |
31db9f7c AB |
2459 | if (ret < 0) |
2460 | goto out; | |
2461 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); | |
2462 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || | |
2463 | S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
d79e5043 AJ |
2464 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
2465 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); | |
31db9f7c AB |
2466 | } |
2467 | ||
2468 | ret = send_cmd(sctx); | |
2469 | if (ret < 0) | |
2470 | goto out; | |
2471 | ||
2472 | ||
2473 | tlv_put_failure: | |
2474 | out: | |
924794c9 | 2475 | fs_path_free(p); |
31db9f7c AB |
2476 | return ret; |
2477 | } | |
2478 | ||
1f4692da AB |
2479 | /* |
2480 | * We need some special handling for inodes that get processed before the parent | |
2481 | * directory got created. See process_recorded_refs for details. | |
2482 | * This function does the check if we already created the dir out of order. | |
2483 | */ | |
2484 | static int did_create_dir(struct send_ctx *sctx, u64 dir) | |
2485 | { | |
2486 | int ret = 0; | |
2487 | struct btrfs_path *path = NULL; | |
2488 | struct btrfs_key key; | |
2489 | struct btrfs_key found_key; | |
2490 | struct btrfs_key di_key; | |
2491 | struct extent_buffer *eb; | |
2492 | struct btrfs_dir_item *di; | |
2493 | int slot; | |
2494 | ||
2495 | path = alloc_path_for_send(); | |
2496 | if (!path) { | |
2497 | ret = -ENOMEM; | |
2498 | goto out; | |
2499 | } | |
2500 | ||
2501 | key.objectid = dir; | |
2502 | key.type = BTRFS_DIR_INDEX_KEY; | |
2503 | key.offset = 0; | |
dff6d0ad FDBM |
2504 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
2505 | if (ret < 0) | |
2506 | goto out; | |
2507 | ||
1f4692da | 2508 | while (1) { |
dff6d0ad FDBM |
2509 | eb = path->nodes[0]; |
2510 | slot = path->slots[0]; | |
2511 | if (slot >= btrfs_header_nritems(eb)) { | |
2512 | ret = btrfs_next_leaf(sctx->send_root, path); | |
2513 | if (ret < 0) { | |
2514 | goto out; | |
2515 | } else if (ret > 0) { | |
2516 | ret = 0; | |
2517 | break; | |
2518 | } | |
2519 | continue; | |
1f4692da | 2520 | } |
dff6d0ad FDBM |
2521 | |
2522 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
2523 | if (found_key.objectid != key.objectid || | |
1f4692da AB |
2524 | found_key.type != key.type) { |
2525 | ret = 0; | |
2526 | goto out; | |
2527 | } | |
2528 | ||
2529 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); | |
2530 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); | |
2531 | ||
a0525414 JB |
2532 | if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
2533 | di_key.objectid < sctx->send_progress) { | |
1f4692da AB |
2534 | ret = 1; |
2535 | goto out; | |
2536 | } | |
2537 | ||
dff6d0ad | 2538 | path->slots[0]++; |
1f4692da AB |
2539 | } |
2540 | ||
2541 | out: | |
2542 | btrfs_free_path(path); | |
2543 | return ret; | |
2544 | } | |
2545 | ||
2546 | /* | |
2547 | * Only creates the inode if it is: | |
2548 | * 1. Not a directory | |
2549 | * 2. Or a directory which was not created already due to out of order | |
2550 | * directories. See did_create_dir and process_recorded_refs for details. | |
2551 | */ | |
2552 | static int send_create_inode_if_needed(struct send_ctx *sctx) | |
2553 | { | |
2554 | int ret; | |
2555 | ||
2556 | if (S_ISDIR(sctx->cur_inode_mode)) { | |
2557 | ret = did_create_dir(sctx, sctx->cur_ino); | |
2558 | if (ret < 0) | |
2559 | goto out; | |
2560 | if (ret) { | |
2561 | ret = 0; | |
2562 | goto out; | |
2563 | } | |
2564 | } | |
2565 | ||
2566 | ret = send_create_inode(sctx, sctx->cur_ino); | |
2567 | if (ret < 0) | |
2568 | goto out; | |
2569 | ||
2570 | out: | |
2571 | return ret; | |
2572 | } | |
2573 | ||
31db9f7c AB |
2574 | struct recorded_ref { |
2575 | struct list_head list; | |
2576 | char *dir_path; | |
2577 | char *name; | |
2578 | struct fs_path *full_path; | |
2579 | u64 dir; | |
2580 | u64 dir_gen; | |
2581 | int dir_path_len; | |
2582 | int name_len; | |
2583 | }; | |
2584 | ||
2585 | /* | |
2586 | * We need to process new refs before deleted refs, but compare_tree gives us | |
2587 | * everything mixed. So we first record all refs and later process them. | |
2588 | * This function is a helper to record one ref. | |
2589 | */ | |
2590 | static int record_ref(struct list_head *head, u64 dir, | |
2591 | u64 dir_gen, struct fs_path *path) | |
2592 | { | |
2593 | struct recorded_ref *ref; | |
31db9f7c AB |
2594 | |
2595 | ref = kmalloc(sizeof(*ref), GFP_NOFS); | |
2596 | if (!ref) | |
2597 | return -ENOMEM; | |
2598 | ||
2599 | ref->dir = dir; | |
2600 | ref->dir_gen = dir_gen; | |
2601 | ref->full_path = path; | |
2602 | ||
ed84885d AS |
2603 | ref->name = (char *)kbasename(ref->full_path->start); |
2604 | ref->name_len = ref->full_path->end - ref->name; | |
2605 | ref->dir_path = ref->full_path->start; | |
2606 | if (ref->name == ref->full_path->start) | |
31db9f7c | 2607 | ref->dir_path_len = 0; |
ed84885d | 2608 | else |
31db9f7c AB |
2609 | ref->dir_path_len = ref->full_path->end - |
2610 | ref->full_path->start - 1 - ref->name_len; | |
31db9f7c AB |
2611 | |
2612 | list_add_tail(&ref->list, head); | |
2613 | return 0; | |
2614 | } | |
2615 | ||
ba5e8f2e JB |
2616 | static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
2617 | { | |
2618 | struct recorded_ref *new; | |
2619 | ||
2620 | new = kmalloc(sizeof(*ref), GFP_NOFS); | |
2621 | if (!new) | |
2622 | return -ENOMEM; | |
2623 | ||
2624 | new->dir = ref->dir; | |
2625 | new->dir_gen = ref->dir_gen; | |
2626 | new->full_path = NULL; | |
2627 | INIT_LIST_HEAD(&new->list); | |
2628 | list_add_tail(&new->list, list); | |
2629 | return 0; | |
2630 | } | |
2631 | ||
924794c9 | 2632 | static void __free_recorded_refs(struct list_head *head) |
31db9f7c AB |
2633 | { |
2634 | struct recorded_ref *cur; | |
31db9f7c | 2635 | |
e938c8ad AB |
2636 | while (!list_empty(head)) { |
2637 | cur = list_entry(head->next, struct recorded_ref, list); | |
924794c9 | 2638 | fs_path_free(cur->full_path); |
e938c8ad | 2639 | list_del(&cur->list); |
31db9f7c AB |
2640 | kfree(cur); |
2641 | } | |
31db9f7c AB |
2642 | } |
2643 | ||
2644 | static void free_recorded_refs(struct send_ctx *sctx) | |
2645 | { | |
924794c9 TI |
2646 | __free_recorded_refs(&sctx->new_refs); |
2647 | __free_recorded_refs(&sctx->deleted_refs); | |
31db9f7c AB |
2648 | } |
2649 | ||
2650 | /* | |
766702ef | 2651 | * Renames/moves a file/dir to its orphan name. Used when the first |
31db9f7c AB |
2652 | * ref of an unprocessed inode gets overwritten and for all non empty |
2653 | * directories. | |
2654 | */ | |
2655 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, | |
2656 | struct fs_path *path) | |
2657 | { | |
2658 | int ret; | |
2659 | struct fs_path *orphan; | |
2660 | ||
924794c9 | 2661 | orphan = fs_path_alloc(); |
31db9f7c AB |
2662 | if (!orphan) |
2663 | return -ENOMEM; | |
2664 | ||
2665 | ret = gen_unique_name(sctx, ino, gen, orphan); | |
2666 | if (ret < 0) | |
2667 | goto out; | |
2668 | ||
2669 | ret = send_rename(sctx, path, orphan); | |
2670 | ||
2671 | out: | |
924794c9 | 2672 | fs_path_free(orphan); |
31db9f7c AB |
2673 | return ret; |
2674 | } | |
2675 | ||
2676 | /* | |
2677 | * Returns 1 if a directory can be removed at this point in time. | |
2678 | * We check this by iterating all dir items and checking if the inode behind | |
2679 | * the dir item was already processed. | |
2680 | */ | |
2681 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress) | |
2682 | { | |
2683 | int ret = 0; | |
2684 | struct btrfs_root *root = sctx->parent_root; | |
2685 | struct btrfs_path *path; | |
2686 | struct btrfs_key key; | |
2687 | struct btrfs_key found_key; | |
2688 | struct btrfs_key loc; | |
2689 | struct btrfs_dir_item *di; | |
2690 | ||
6d85ed05 AB |
2691 | /* |
2692 | * Don't try to rmdir the top/root subvolume dir. | |
2693 | */ | |
2694 | if (dir == BTRFS_FIRST_FREE_OBJECTID) | |
2695 | return 0; | |
2696 | ||
31db9f7c AB |
2697 | path = alloc_path_for_send(); |
2698 | if (!path) | |
2699 | return -ENOMEM; | |
2700 | ||
2701 | key.objectid = dir; | |
2702 | key.type = BTRFS_DIR_INDEX_KEY; | |
2703 | key.offset = 0; | |
dff6d0ad FDBM |
2704 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2705 | if (ret < 0) | |
2706 | goto out; | |
31db9f7c AB |
2707 | |
2708 | while (1) { | |
dff6d0ad FDBM |
2709 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
2710 | ret = btrfs_next_leaf(root, path); | |
2711 | if (ret < 0) | |
2712 | goto out; | |
2713 | else if (ret > 0) | |
2714 | break; | |
2715 | continue; | |
31db9f7c | 2716 | } |
dff6d0ad FDBM |
2717 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
2718 | path->slots[0]); | |
2719 | if (found_key.objectid != key.objectid || | |
2720 | found_key.type != key.type) | |
31db9f7c | 2721 | break; |
31db9f7c AB |
2722 | |
2723 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2724 | struct btrfs_dir_item); | |
2725 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); | |
2726 | ||
2727 | if (loc.objectid > send_progress) { | |
2728 | ret = 0; | |
2729 | goto out; | |
2730 | } | |
2731 | ||
dff6d0ad | 2732 | path->slots[0]++; |
31db9f7c AB |
2733 | } |
2734 | ||
2735 | ret = 1; | |
2736 | ||
2737 | out: | |
2738 | btrfs_free_path(path); | |
2739 | return ret; | |
2740 | } | |
2741 | ||
9f03740a FDBM |
2742 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
2743 | { | |
2744 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; | |
2745 | struct waiting_dir_move *entry; | |
2746 | ||
2747 | while (n) { | |
2748 | entry = rb_entry(n, struct waiting_dir_move, node); | |
2749 | if (ino < entry->ino) | |
2750 | n = n->rb_left; | |
2751 | else if (ino > entry->ino) | |
2752 | n = n->rb_right; | |
2753 | else | |
2754 | return 1; | |
2755 | } | |
2756 | return 0; | |
2757 | } | |
2758 | ||
2759 | static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino) | |
2760 | { | |
2761 | struct rb_node **p = &sctx->waiting_dir_moves.rb_node; | |
2762 | struct rb_node *parent = NULL; | |
2763 | struct waiting_dir_move *entry, *dm; | |
2764 | ||
2765 | dm = kmalloc(sizeof(*dm), GFP_NOFS); | |
2766 | if (!dm) | |
2767 | return -ENOMEM; | |
2768 | dm->ino = ino; | |
2769 | ||
2770 | while (*p) { | |
2771 | parent = *p; | |
2772 | entry = rb_entry(parent, struct waiting_dir_move, node); | |
2773 | if (ino < entry->ino) { | |
2774 | p = &(*p)->rb_left; | |
2775 | } else if (ino > entry->ino) { | |
2776 | p = &(*p)->rb_right; | |
2777 | } else { | |
2778 | kfree(dm); | |
2779 | return -EEXIST; | |
2780 | } | |
2781 | } | |
2782 | ||
2783 | rb_link_node(&dm->node, parent, p); | |
2784 | rb_insert_color(&dm->node, &sctx->waiting_dir_moves); | |
2785 | return 0; | |
2786 | } | |
2787 | ||
9f03740a FDBM |
2788 | static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
2789 | { | |
2790 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; | |
2791 | struct waiting_dir_move *entry; | |
2792 | ||
2793 | while (n) { | |
2794 | entry = rb_entry(n, struct waiting_dir_move, node); | |
2795 | if (ino < entry->ino) { | |
2796 | n = n->rb_left; | |
2797 | } else if (ino > entry->ino) { | |
2798 | n = n->rb_right; | |
2799 | } else { | |
2800 | rb_erase(&entry->node, &sctx->waiting_dir_moves); | |
2801 | kfree(entry); | |
2802 | return 0; | |
2803 | } | |
2804 | } | |
2805 | return -ENOENT; | |
2806 | } | |
2807 | ||
9f03740a FDBM |
2808 | static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino) |
2809 | { | |
2810 | struct rb_node **p = &sctx->pending_dir_moves.rb_node; | |
2811 | struct rb_node *parent = NULL; | |
2812 | struct pending_dir_move *entry, *pm; | |
2813 | struct recorded_ref *cur; | |
2814 | int exists = 0; | |
2815 | int ret; | |
2816 | ||
2817 | pm = kmalloc(sizeof(*pm), GFP_NOFS); | |
2818 | if (!pm) | |
2819 | return -ENOMEM; | |
2820 | pm->parent_ino = parent_ino; | |
2821 | pm->ino = sctx->cur_ino; | |
2822 | pm->gen = sctx->cur_inode_gen; | |
2823 | INIT_LIST_HEAD(&pm->list); | |
2824 | INIT_LIST_HEAD(&pm->update_refs); | |
2825 | RB_CLEAR_NODE(&pm->node); | |
2826 | ||
2827 | while (*p) { | |
2828 | parent = *p; | |
2829 | entry = rb_entry(parent, struct pending_dir_move, node); | |
2830 | if (parent_ino < entry->parent_ino) { | |
2831 | p = &(*p)->rb_left; | |
2832 | } else if (parent_ino > entry->parent_ino) { | |
2833 | p = &(*p)->rb_right; | |
2834 | } else { | |
2835 | exists = 1; | |
2836 | break; | |
2837 | } | |
2838 | } | |
2839 | ||
2840 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
2841 | ret = dup_ref(cur, &pm->update_refs); | |
2842 | if (ret < 0) | |
2843 | goto out; | |
2844 | } | |
2845 | list_for_each_entry(cur, &sctx->new_refs, list) { | |
2846 | ret = dup_ref(cur, &pm->update_refs); | |
2847 | if (ret < 0) | |
2848 | goto out; | |
2849 | } | |
2850 | ||
2851 | ret = add_waiting_dir_move(sctx, pm->ino); | |
2852 | if (ret) | |
2853 | goto out; | |
2854 | ||
2855 | if (exists) { | |
2856 | list_add_tail(&pm->list, &entry->list); | |
2857 | } else { | |
2858 | rb_link_node(&pm->node, parent, p); | |
2859 | rb_insert_color(&pm->node, &sctx->pending_dir_moves); | |
2860 | } | |
2861 | ret = 0; | |
2862 | out: | |
2863 | if (ret) { | |
2864 | __free_recorded_refs(&pm->update_refs); | |
2865 | kfree(pm); | |
2866 | } | |
2867 | return ret; | |
2868 | } | |
2869 | ||
2870 | static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, | |
2871 | u64 parent_ino) | |
2872 | { | |
2873 | struct rb_node *n = sctx->pending_dir_moves.rb_node; | |
2874 | struct pending_dir_move *entry; | |
2875 | ||
2876 | while (n) { | |
2877 | entry = rb_entry(n, struct pending_dir_move, node); | |
2878 | if (parent_ino < entry->parent_ino) | |
2879 | n = n->rb_left; | |
2880 | else if (parent_ino > entry->parent_ino) | |
2881 | n = n->rb_right; | |
2882 | else | |
2883 | return entry; | |
2884 | } | |
2885 | return NULL; | |
2886 | } | |
2887 | ||
2888 | static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) | |
2889 | { | |
2890 | struct fs_path *from_path = NULL; | |
2891 | struct fs_path *to_path = NULL; | |
2892 | u64 orig_progress = sctx->send_progress; | |
2893 | struct recorded_ref *cur; | |
2894 | int ret; | |
2895 | ||
2896 | from_path = fs_path_alloc(); | |
2897 | if (!from_path) | |
2898 | return -ENOMEM; | |
2899 | ||
2900 | sctx->send_progress = pm->ino; | |
2901 | ret = get_cur_path(sctx, pm->ino, pm->gen, from_path); | |
2902 | if (ret < 0) | |
2903 | goto out; | |
2904 | ||
2905 | to_path = fs_path_alloc(); | |
2906 | if (!to_path) { | |
2907 | ret = -ENOMEM; | |
2908 | goto out; | |
2909 | } | |
2910 | ||
2911 | sctx->send_progress = sctx->cur_ino + 1; | |
6cc98d90 JB |
2912 | ret = del_waiting_dir_move(sctx, pm->ino); |
2913 | ASSERT(ret == 0); | |
2914 | ||
9f03740a FDBM |
2915 | ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); |
2916 | if (ret < 0) | |
2917 | goto out; | |
2918 | ||
2919 | ret = send_rename(sctx, from_path, to_path); | |
2920 | if (ret < 0) | |
2921 | goto out; | |
2922 | ||
2923 | ret = send_utimes(sctx, pm->ino, pm->gen); | |
2924 | if (ret < 0) | |
2925 | goto out; | |
2926 | ||
2927 | /* | |
2928 | * After rename/move, need to update the utimes of both new parent(s) | |
2929 | * and old parent(s). | |
2930 | */ | |
2931 | list_for_each_entry(cur, &pm->update_refs, list) { | |
2932 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); | |
2933 | if (ret < 0) | |
2934 | goto out; | |
2935 | } | |
2936 | ||
2937 | out: | |
2938 | fs_path_free(from_path); | |
2939 | fs_path_free(to_path); | |
2940 | sctx->send_progress = orig_progress; | |
2941 | ||
2942 | return ret; | |
2943 | } | |
2944 | ||
2945 | static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) | |
2946 | { | |
2947 | if (!list_empty(&m->list)) | |
2948 | list_del(&m->list); | |
2949 | if (!RB_EMPTY_NODE(&m->node)) | |
2950 | rb_erase(&m->node, &sctx->pending_dir_moves); | |
2951 | __free_recorded_refs(&m->update_refs); | |
2952 | kfree(m); | |
2953 | } | |
2954 | ||
2955 | static void tail_append_pending_moves(struct pending_dir_move *moves, | |
2956 | struct list_head *stack) | |
2957 | { | |
2958 | if (list_empty(&moves->list)) { | |
2959 | list_add_tail(&moves->list, stack); | |
2960 | } else { | |
2961 | LIST_HEAD(list); | |
2962 | list_splice_init(&moves->list, &list); | |
2963 | list_add_tail(&moves->list, stack); | |
2964 | list_splice_tail(&list, stack); | |
2965 | } | |
2966 | } | |
2967 | ||
2968 | static int apply_children_dir_moves(struct send_ctx *sctx) | |
2969 | { | |
2970 | struct pending_dir_move *pm; | |
2971 | struct list_head stack; | |
2972 | u64 parent_ino = sctx->cur_ino; | |
2973 | int ret = 0; | |
2974 | ||
2975 | pm = get_pending_dir_moves(sctx, parent_ino); | |
2976 | if (!pm) | |
2977 | return 0; | |
2978 | ||
2979 | INIT_LIST_HEAD(&stack); | |
2980 | tail_append_pending_moves(pm, &stack); | |
2981 | ||
2982 | while (!list_empty(&stack)) { | |
2983 | pm = list_first_entry(&stack, struct pending_dir_move, list); | |
2984 | parent_ino = pm->ino; | |
2985 | ret = apply_dir_move(sctx, pm); | |
2986 | free_pending_move(sctx, pm); | |
2987 | if (ret) | |
2988 | goto out; | |
2989 | pm = get_pending_dir_moves(sctx, parent_ino); | |
2990 | if (pm) | |
2991 | tail_append_pending_moves(pm, &stack); | |
2992 | } | |
2993 | return 0; | |
2994 | ||
2995 | out: | |
2996 | while (!list_empty(&stack)) { | |
2997 | pm = list_first_entry(&stack, struct pending_dir_move, list); | |
2998 | free_pending_move(sctx, pm); | |
2999 | } | |
3000 | return ret; | |
3001 | } | |
3002 | ||
3003 | static int wait_for_parent_move(struct send_ctx *sctx, | |
3004 | struct recorded_ref *parent_ref) | |
3005 | { | |
3006 | int ret; | |
3007 | u64 ino = parent_ref->dir; | |
3008 | u64 parent_ino_before, parent_ino_after; | |
3009 | u64 new_gen, old_gen; | |
3010 | struct fs_path *path_before = NULL; | |
3011 | struct fs_path *path_after = NULL; | |
3012 | int len1, len2; | |
3013 | ||
3014 | if (parent_ref->dir <= sctx->cur_ino) | |
3015 | return 0; | |
3016 | ||
3017 | if (is_waiting_for_move(sctx, ino)) | |
3018 | return 1; | |
3019 | ||
3020 | ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen, | |
3021 | NULL, NULL, NULL, NULL); | |
3022 | if (ret == -ENOENT) | |
3023 | return 0; | |
3024 | else if (ret < 0) | |
3025 | return ret; | |
3026 | ||
3027 | ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen, | |
3028 | NULL, NULL, NULL, NULL); | |
3029 | if (ret < 0) | |
3030 | return ret; | |
3031 | ||
3032 | if (new_gen != old_gen) | |
3033 | return 0; | |
3034 | ||
3035 | path_before = fs_path_alloc(); | |
3036 | if (!path_before) | |
3037 | return -ENOMEM; | |
3038 | ||
3039 | ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, | |
3040 | NULL, path_before); | |
3041 | if (ret == -ENOENT) { | |
3042 | ret = 0; | |
3043 | goto out; | |
3044 | } else if (ret < 0) { | |
3045 | goto out; | |
3046 | } | |
3047 | ||
3048 | path_after = fs_path_alloc(); | |
3049 | if (!path_after) { | |
3050 | ret = -ENOMEM; | |
3051 | goto out; | |
3052 | } | |
3053 | ||
3054 | ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, | |
3055 | NULL, path_after); | |
3056 | if (ret == -ENOENT) { | |
3057 | ret = 0; | |
3058 | goto out; | |
3059 | } else if (ret < 0) { | |
3060 | goto out; | |
3061 | } | |
3062 | ||
3063 | len1 = fs_path_len(path_before); | |
3064 | len2 = fs_path_len(path_after); | |
5ed7f9ff FDBM |
3065 | if (parent_ino_before != parent_ino_after || len1 != len2 || |
3066 | memcmp(path_before->start, path_after->start, len1)) { | |
9f03740a FDBM |
3067 | ret = 1; |
3068 | goto out; | |
3069 | } | |
3070 | ret = 0; | |
3071 | ||
3072 | out: | |
3073 | fs_path_free(path_before); | |
3074 | fs_path_free(path_after); | |
3075 | ||
3076 | return ret; | |
3077 | } | |
3078 | ||
31db9f7c AB |
3079 | /* |
3080 | * This does all the move/link/unlink/rmdir magic. | |
3081 | */ | |
9f03740a | 3082 | static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) |
31db9f7c AB |
3083 | { |
3084 | int ret = 0; | |
3085 | struct recorded_ref *cur; | |
1f4692da | 3086 | struct recorded_ref *cur2; |
ba5e8f2e | 3087 | struct list_head check_dirs; |
31db9f7c | 3088 | struct fs_path *valid_path = NULL; |
b24baf69 | 3089 | u64 ow_inode = 0; |
31db9f7c AB |
3090 | u64 ow_gen; |
3091 | int did_overwrite = 0; | |
3092 | int is_orphan = 0; | |
3093 | ||
3094 | verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino); | |
3095 | ||
6d85ed05 AB |
3096 | /* |
3097 | * This should never happen as the root dir always has the same ref | |
3098 | * which is always '..' | |
3099 | */ | |
3100 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); | |
ba5e8f2e | 3101 | INIT_LIST_HEAD(&check_dirs); |
6d85ed05 | 3102 | |
924794c9 | 3103 | valid_path = fs_path_alloc(); |
31db9f7c AB |
3104 | if (!valid_path) { |
3105 | ret = -ENOMEM; | |
3106 | goto out; | |
3107 | } | |
3108 | ||
31db9f7c AB |
3109 | /* |
3110 | * First, check if the first ref of the current inode was overwritten | |
3111 | * before. If yes, we know that the current inode was already orphanized | |
3112 | * and thus use the orphan name. If not, we can use get_cur_path to | |
3113 | * get the path of the first ref as it would like while receiving at | |
3114 | * this point in time. | |
3115 | * New inodes are always orphan at the beginning, so force to use the | |
3116 | * orphan name in this case. | |
3117 | * The first ref is stored in valid_path and will be updated if it | |
3118 | * gets moved around. | |
3119 | */ | |
3120 | if (!sctx->cur_inode_new) { | |
3121 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, | |
3122 | sctx->cur_inode_gen); | |
3123 | if (ret < 0) | |
3124 | goto out; | |
3125 | if (ret) | |
3126 | did_overwrite = 1; | |
3127 | } | |
3128 | if (sctx->cur_inode_new || did_overwrite) { | |
3129 | ret = gen_unique_name(sctx, sctx->cur_ino, | |
3130 | sctx->cur_inode_gen, valid_path); | |
3131 | if (ret < 0) | |
3132 | goto out; | |
3133 | is_orphan = 1; | |
3134 | } else { | |
3135 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
3136 | valid_path); | |
3137 | if (ret < 0) | |
3138 | goto out; | |
3139 | } | |
3140 | ||
3141 | list_for_each_entry(cur, &sctx->new_refs, list) { | |
1f4692da AB |
3142 | /* |
3143 | * We may have refs where the parent directory does not exist | |
3144 | * yet. This happens if the parent directories inum is higher | |
3145 | * the the current inum. To handle this case, we create the | |
3146 | * parent directory out of order. But we need to check if this | |
3147 | * did already happen before due to other refs in the same dir. | |
3148 | */ | |
3149 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); | |
3150 | if (ret < 0) | |
3151 | goto out; | |
3152 | if (ret == inode_state_will_create) { | |
3153 | ret = 0; | |
3154 | /* | |
3155 | * First check if any of the current inodes refs did | |
3156 | * already create the dir. | |
3157 | */ | |
3158 | list_for_each_entry(cur2, &sctx->new_refs, list) { | |
3159 | if (cur == cur2) | |
3160 | break; | |
3161 | if (cur2->dir == cur->dir) { | |
3162 | ret = 1; | |
3163 | break; | |
3164 | } | |
3165 | } | |
3166 | ||
3167 | /* | |
3168 | * If that did not happen, check if a previous inode | |
3169 | * did already create the dir. | |
3170 | */ | |
3171 | if (!ret) | |
3172 | ret = did_create_dir(sctx, cur->dir); | |
3173 | if (ret < 0) | |
3174 | goto out; | |
3175 | if (!ret) { | |
3176 | ret = send_create_inode(sctx, cur->dir); | |
3177 | if (ret < 0) | |
3178 | goto out; | |
3179 | } | |
3180 | } | |
3181 | ||
31db9f7c AB |
3182 | /* |
3183 | * Check if this new ref would overwrite the first ref of | |
3184 | * another unprocessed inode. If yes, orphanize the | |
3185 | * overwritten inode. If we find an overwritten ref that is | |
3186 | * not the first ref, simply unlink it. | |
3187 | */ | |
3188 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
3189 | cur->name, cur->name_len, | |
3190 | &ow_inode, &ow_gen); | |
3191 | if (ret < 0) | |
3192 | goto out; | |
3193 | if (ret) { | |
924794c9 TI |
3194 | ret = is_first_ref(sctx->parent_root, |
3195 | ow_inode, cur->dir, cur->name, | |
3196 | cur->name_len); | |
31db9f7c AB |
3197 | if (ret < 0) |
3198 | goto out; | |
3199 | if (ret) { | |
3200 | ret = orphanize_inode(sctx, ow_inode, ow_gen, | |
3201 | cur->full_path); | |
3202 | if (ret < 0) | |
3203 | goto out; | |
3204 | } else { | |
3205 | ret = send_unlink(sctx, cur->full_path); | |
3206 | if (ret < 0) | |
3207 | goto out; | |
3208 | } | |
3209 | } | |
3210 | ||
3211 | /* | |
3212 | * link/move the ref to the new place. If we have an orphan | |
3213 | * inode, move it and update valid_path. If not, link or move | |
3214 | * it depending on the inode mode. | |
3215 | */ | |
1f4692da | 3216 | if (is_orphan) { |
31db9f7c AB |
3217 | ret = send_rename(sctx, valid_path, cur->full_path); |
3218 | if (ret < 0) | |
3219 | goto out; | |
3220 | is_orphan = 0; | |
3221 | ret = fs_path_copy(valid_path, cur->full_path); | |
3222 | if (ret < 0) | |
3223 | goto out; | |
3224 | } else { | |
3225 | if (S_ISDIR(sctx->cur_inode_mode)) { | |
3226 | /* | |
3227 | * Dirs can't be linked, so move it. For moved | |
3228 | * dirs, we always have one new and one deleted | |
3229 | * ref. The deleted ref is ignored later. | |
3230 | */ | |
d86477b3 FDBM |
3231 | ret = wait_for_parent_move(sctx, cur); |
3232 | if (ret < 0) | |
3233 | goto out; | |
3234 | if (ret) { | |
9f03740a FDBM |
3235 | ret = add_pending_dir_move(sctx, |
3236 | cur->dir); | |
3237 | *pending_move = 1; | |
3238 | } else { | |
3239 | ret = send_rename(sctx, valid_path, | |
3240 | cur->full_path); | |
3241 | if (!ret) | |
3242 | ret = fs_path_copy(valid_path, | |
3243 | cur->full_path); | |
3244 | } | |
31db9f7c AB |
3245 | if (ret < 0) |
3246 | goto out; | |
3247 | } else { | |
3248 | ret = send_link(sctx, cur->full_path, | |
3249 | valid_path); | |
3250 | if (ret < 0) | |
3251 | goto out; | |
3252 | } | |
3253 | } | |
ba5e8f2e | 3254 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
3255 | if (ret < 0) |
3256 | goto out; | |
3257 | } | |
3258 | ||
3259 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { | |
3260 | /* | |
3261 | * Check if we can already rmdir the directory. If not, | |
3262 | * orphanize it. For every dir item inside that gets deleted | |
3263 | * later, we do this check again and rmdir it then if possible. | |
3264 | * See the use of check_dirs for more details. | |
3265 | */ | |
3266 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino); | |
3267 | if (ret < 0) | |
3268 | goto out; | |
3269 | if (ret) { | |
3270 | ret = send_rmdir(sctx, valid_path); | |
3271 | if (ret < 0) | |
3272 | goto out; | |
3273 | } else if (!is_orphan) { | |
3274 | ret = orphanize_inode(sctx, sctx->cur_ino, | |
3275 | sctx->cur_inode_gen, valid_path); | |
3276 | if (ret < 0) | |
3277 | goto out; | |
3278 | is_orphan = 1; | |
3279 | } | |
3280 | ||
3281 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
ba5e8f2e | 3282 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
3283 | if (ret < 0) |
3284 | goto out; | |
3285 | } | |
ccf1626b AB |
3286 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
3287 | !list_empty(&sctx->deleted_refs)) { | |
3288 | /* | |
3289 | * We have a moved dir. Add the old parent to check_dirs | |
3290 | */ | |
3291 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, | |
3292 | list); | |
ba5e8f2e | 3293 | ret = dup_ref(cur, &check_dirs); |
ccf1626b AB |
3294 | if (ret < 0) |
3295 | goto out; | |
31db9f7c AB |
3296 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
3297 | /* | |
3298 | * We have a non dir inode. Go through all deleted refs and | |
3299 | * unlink them if they were not already overwritten by other | |
3300 | * inodes. | |
3301 | */ | |
3302 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
3303 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
3304 | sctx->cur_ino, sctx->cur_inode_gen, | |
3305 | cur->name, cur->name_len); | |
3306 | if (ret < 0) | |
3307 | goto out; | |
3308 | if (!ret) { | |
1f4692da AB |
3309 | ret = send_unlink(sctx, cur->full_path); |
3310 | if (ret < 0) | |
3311 | goto out; | |
31db9f7c | 3312 | } |
ba5e8f2e | 3313 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
3314 | if (ret < 0) |
3315 | goto out; | |
3316 | } | |
31db9f7c AB |
3317 | /* |
3318 | * If the inode is still orphan, unlink the orphan. This may | |
3319 | * happen when a previous inode did overwrite the first ref | |
3320 | * of this inode and no new refs were added for the current | |
766702ef AB |
3321 | * inode. Unlinking does not mean that the inode is deleted in |
3322 | * all cases. There may still be links to this inode in other | |
3323 | * places. | |
31db9f7c | 3324 | */ |
1f4692da | 3325 | if (is_orphan) { |
31db9f7c AB |
3326 | ret = send_unlink(sctx, valid_path); |
3327 | if (ret < 0) | |
3328 | goto out; | |
3329 | } | |
3330 | } | |
3331 | ||
3332 | /* | |
3333 | * We did collect all parent dirs where cur_inode was once located. We | |
3334 | * now go through all these dirs and check if they are pending for | |
3335 | * deletion and if it's finally possible to perform the rmdir now. | |
3336 | * We also update the inode stats of the parent dirs here. | |
3337 | */ | |
ba5e8f2e | 3338 | list_for_each_entry(cur, &check_dirs, list) { |
766702ef AB |
3339 | /* |
3340 | * In case we had refs into dirs that were not processed yet, | |
3341 | * we don't need to do the utime and rmdir logic for these dirs. | |
3342 | * The dir will be processed later. | |
3343 | */ | |
ba5e8f2e | 3344 | if (cur->dir > sctx->cur_ino) |
31db9f7c AB |
3345 | continue; |
3346 | ||
ba5e8f2e | 3347 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
31db9f7c AB |
3348 | if (ret < 0) |
3349 | goto out; | |
3350 | ||
3351 | if (ret == inode_state_did_create || | |
3352 | ret == inode_state_no_change) { | |
3353 | /* TODO delayed utimes */ | |
ba5e8f2e | 3354 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
31db9f7c AB |
3355 | if (ret < 0) |
3356 | goto out; | |
3357 | } else if (ret == inode_state_did_delete) { | |
ba5e8f2e | 3358 | ret = can_rmdir(sctx, cur->dir, sctx->cur_ino); |
31db9f7c AB |
3359 | if (ret < 0) |
3360 | goto out; | |
3361 | if (ret) { | |
ba5e8f2e JB |
3362 | ret = get_cur_path(sctx, cur->dir, |
3363 | cur->dir_gen, valid_path); | |
31db9f7c AB |
3364 | if (ret < 0) |
3365 | goto out; | |
3366 | ret = send_rmdir(sctx, valid_path); | |
3367 | if (ret < 0) | |
3368 | goto out; | |
3369 | } | |
3370 | } | |
3371 | } | |
3372 | ||
31db9f7c AB |
3373 | ret = 0; |
3374 | ||
3375 | out: | |
ba5e8f2e | 3376 | __free_recorded_refs(&check_dirs); |
31db9f7c | 3377 | free_recorded_refs(sctx); |
924794c9 | 3378 | fs_path_free(valid_path); |
31db9f7c AB |
3379 | return ret; |
3380 | } | |
3381 | ||
3382 | static int __record_new_ref(int num, u64 dir, int index, | |
3383 | struct fs_path *name, | |
3384 | void *ctx) | |
3385 | { | |
3386 | int ret = 0; | |
3387 | struct send_ctx *sctx = ctx; | |
3388 | struct fs_path *p; | |
3389 | u64 gen; | |
3390 | ||
924794c9 | 3391 | p = fs_path_alloc(); |
31db9f7c AB |
3392 | if (!p) |
3393 | return -ENOMEM; | |
3394 | ||
3395 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL, | |
85a7b33b | 3396 | NULL, NULL); |
31db9f7c AB |
3397 | if (ret < 0) |
3398 | goto out; | |
3399 | ||
31db9f7c AB |
3400 | ret = get_cur_path(sctx, dir, gen, p); |
3401 | if (ret < 0) | |
3402 | goto out; | |
3403 | ret = fs_path_add_path(p, name); | |
3404 | if (ret < 0) | |
3405 | goto out; | |
3406 | ||
3407 | ret = record_ref(&sctx->new_refs, dir, gen, p); | |
3408 | ||
3409 | out: | |
3410 | if (ret) | |
924794c9 | 3411 | fs_path_free(p); |
31db9f7c AB |
3412 | return ret; |
3413 | } | |
3414 | ||
3415 | static int __record_deleted_ref(int num, u64 dir, int index, | |
3416 | struct fs_path *name, | |
3417 | void *ctx) | |
3418 | { | |
3419 | int ret = 0; | |
3420 | struct send_ctx *sctx = ctx; | |
3421 | struct fs_path *p; | |
3422 | u64 gen; | |
3423 | ||
924794c9 | 3424 | p = fs_path_alloc(); |
31db9f7c AB |
3425 | if (!p) |
3426 | return -ENOMEM; | |
3427 | ||
3428 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL, | |
85a7b33b | 3429 | NULL, NULL); |
31db9f7c AB |
3430 | if (ret < 0) |
3431 | goto out; | |
3432 | ||
3433 | ret = get_cur_path(sctx, dir, gen, p); | |
3434 | if (ret < 0) | |
3435 | goto out; | |
3436 | ret = fs_path_add_path(p, name); | |
3437 | if (ret < 0) | |
3438 | goto out; | |
3439 | ||
3440 | ret = record_ref(&sctx->deleted_refs, dir, gen, p); | |
3441 | ||
3442 | out: | |
3443 | if (ret) | |
924794c9 | 3444 | fs_path_free(p); |
31db9f7c AB |
3445 | return ret; |
3446 | } | |
3447 | ||
3448 | static int record_new_ref(struct send_ctx *sctx) | |
3449 | { | |
3450 | int ret; | |
3451 | ||
924794c9 TI |
3452 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
3453 | sctx->cmp_key, 0, __record_new_ref, sctx); | |
31db9f7c AB |
3454 | if (ret < 0) |
3455 | goto out; | |
3456 | ret = 0; | |
3457 | ||
3458 | out: | |
3459 | return ret; | |
3460 | } | |
3461 | ||
3462 | static int record_deleted_ref(struct send_ctx *sctx) | |
3463 | { | |
3464 | int ret; | |
3465 | ||
924794c9 TI |
3466 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
3467 | sctx->cmp_key, 0, __record_deleted_ref, sctx); | |
31db9f7c AB |
3468 | if (ret < 0) |
3469 | goto out; | |
3470 | ret = 0; | |
3471 | ||
3472 | out: | |
3473 | return ret; | |
3474 | } | |
3475 | ||
3476 | struct find_ref_ctx { | |
3477 | u64 dir; | |
ba5e8f2e JB |
3478 | u64 dir_gen; |
3479 | struct btrfs_root *root; | |
31db9f7c AB |
3480 | struct fs_path *name; |
3481 | int found_idx; | |
3482 | }; | |
3483 | ||
3484 | static int __find_iref(int num, u64 dir, int index, | |
3485 | struct fs_path *name, | |
3486 | void *ctx_) | |
3487 | { | |
3488 | struct find_ref_ctx *ctx = ctx_; | |
ba5e8f2e JB |
3489 | u64 dir_gen; |
3490 | int ret; | |
31db9f7c AB |
3491 | |
3492 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && | |
3493 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { | |
ba5e8f2e JB |
3494 | /* |
3495 | * To avoid doing extra lookups we'll only do this if everything | |
3496 | * else matches. | |
3497 | */ | |
3498 | ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL, | |
3499 | NULL, NULL, NULL); | |
3500 | if (ret) | |
3501 | return ret; | |
3502 | if (dir_gen != ctx->dir_gen) | |
3503 | return 0; | |
31db9f7c AB |
3504 | ctx->found_idx = num; |
3505 | return 1; | |
3506 | } | |
3507 | return 0; | |
3508 | } | |
3509 | ||
924794c9 | 3510 | static int find_iref(struct btrfs_root *root, |
31db9f7c AB |
3511 | struct btrfs_path *path, |
3512 | struct btrfs_key *key, | |
ba5e8f2e | 3513 | u64 dir, u64 dir_gen, struct fs_path *name) |
31db9f7c AB |
3514 | { |
3515 | int ret; | |
3516 | struct find_ref_ctx ctx; | |
3517 | ||
3518 | ctx.dir = dir; | |
3519 | ctx.name = name; | |
ba5e8f2e | 3520 | ctx.dir_gen = dir_gen; |
31db9f7c | 3521 | ctx.found_idx = -1; |
ba5e8f2e | 3522 | ctx.root = root; |
31db9f7c | 3523 | |
924794c9 | 3524 | ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx); |
31db9f7c AB |
3525 | if (ret < 0) |
3526 | return ret; | |
3527 | ||
3528 | if (ctx.found_idx == -1) | |
3529 | return -ENOENT; | |
3530 | ||
3531 | return ctx.found_idx; | |
3532 | } | |
3533 | ||
3534 | static int __record_changed_new_ref(int num, u64 dir, int index, | |
3535 | struct fs_path *name, | |
3536 | void *ctx) | |
3537 | { | |
ba5e8f2e | 3538 | u64 dir_gen; |
31db9f7c AB |
3539 | int ret; |
3540 | struct send_ctx *sctx = ctx; | |
3541 | ||
ba5e8f2e JB |
3542 | ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, |
3543 | NULL, NULL, NULL); | |
3544 | if (ret) | |
3545 | return ret; | |
3546 | ||
924794c9 | 3547 | ret = find_iref(sctx->parent_root, sctx->right_path, |
ba5e8f2e | 3548 | sctx->cmp_key, dir, dir_gen, name); |
31db9f7c AB |
3549 | if (ret == -ENOENT) |
3550 | ret = __record_new_ref(num, dir, index, name, sctx); | |
3551 | else if (ret > 0) | |
3552 | ret = 0; | |
3553 | ||
3554 | return ret; | |
3555 | } | |
3556 | ||
3557 | static int __record_changed_deleted_ref(int num, u64 dir, int index, | |
3558 | struct fs_path *name, | |
3559 | void *ctx) | |
3560 | { | |
ba5e8f2e | 3561 | u64 dir_gen; |
31db9f7c AB |
3562 | int ret; |
3563 | struct send_ctx *sctx = ctx; | |
3564 | ||
ba5e8f2e JB |
3565 | ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, |
3566 | NULL, NULL, NULL); | |
3567 | if (ret) | |
3568 | return ret; | |
3569 | ||
924794c9 | 3570 | ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, |
ba5e8f2e | 3571 | dir, dir_gen, name); |
31db9f7c AB |
3572 | if (ret == -ENOENT) |
3573 | ret = __record_deleted_ref(num, dir, index, name, sctx); | |
3574 | else if (ret > 0) | |
3575 | ret = 0; | |
3576 | ||
3577 | return ret; | |
3578 | } | |
3579 | ||
3580 | static int record_changed_ref(struct send_ctx *sctx) | |
3581 | { | |
3582 | int ret = 0; | |
3583 | ||
924794c9 | 3584 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
31db9f7c AB |
3585 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); |
3586 | if (ret < 0) | |
3587 | goto out; | |
924794c9 | 3588 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
31db9f7c AB |
3589 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); |
3590 | if (ret < 0) | |
3591 | goto out; | |
3592 | ret = 0; | |
3593 | ||
3594 | out: | |
3595 | return ret; | |
3596 | } | |
3597 | ||
3598 | /* | |
3599 | * Record and process all refs at once. Needed when an inode changes the | |
3600 | * generation number, which means that it was deleted and recreated. | |
3601 | */ | |
3602 | static int process_all_refs(struct send_ctx *sctx, | |
3603 | enum btrfs_compare_tree_result cmd) | |
3604 | { | |
3605 | int ret; | |
3606 | struct btrfs_root *root; | |
3607 | struct btrfs_path *path; | |
3608 | struct btrfs_key key; | |
3609 | struct btrfs_key found_key; | |
3610 | struct extent_buffer *eb; | |
3611 | int slot; | |
3612 | iterate_inode_ref_t cb; | |
9f03740a | 3613 | int pending_move = 0; |
31db9f7c AB |
3614 | |
3615 | path = alloc_path_for_send(); | |
3616 | if (!path) | |
3617 | return -ENOMEM; | |
3618 | ||
3619 | if (cmd == BTRFS_COMPARE_TREE_NEW) { | |
3620 | root = sctx->send_root; | |
3621 | cb = __record_new_ref; | |
3622 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { | |
3623 | root = sctx->parent_root; | |
3624 | cb = __record_deleted_ref; | |
3625 | } else { | |
4d1a63b2 DS |
3626 | btrfs_err(sctx->send_root->fs_info, |
3627 | "Wrong command %d in process_all_refs", cmd); | |
3628 | ret = -EINVAL; | |
3629 | goto out; | |
31db9f7c AB |
3630 | } |
3631 | ||
3632 | key.objectid = sctx->cmp_key->objectid; | |
3633 | key.type = BTRFS_INODE_REF_KEY; | |
3634 | key.offset = 0; | |
dff6d0ad FDBM |
3635 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
3636 | if (ret < 0) | |
3637 | goto out; | |
31db9f7c | 3638 | |
dff6d0ad | 3639 | while (1) { |
31db9f7c AB |
3640 | eb = path->nodes[0]; |
3641 | slot = path->slots[0]; | |
dff6d0ad FDBM |
3642 | if (slot >= btrfs_header_nritems(eb)) { |
3643 | ret = btrfs_next_leaf(root, path); | |
3644 | if (ret < 0) | |
3645 | goto out; | |
3646 | else if (ret > 0) | |
3647 | break; | |
3648 | continue; | |
3649 | } | |
3650 | ||
31db9f7c AB |
3651 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
3652 | ||
3653 | if (found_key.objectid != key.objectid || | |
96b5bd77 JS |
3654 | (found_key.type != BTRFS_INODE_REF_KEY && |
3655 | found_key.type != BTRFS_INODE_EXTREF_KEY)) | |
31db9f7c | 3656 | break; |
31db9f7c | 3657 | |
924794c9 | 3658 | ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); |
31db9f7c AB |
3659 | if (ret < 0) |
3660 | goto out; | |
3661 | ||
dff6d0ad | 3662 | path->slots[0]++; |
31db9f7c | 3663 | } |
e938c8ad | 3664 | btrfs_release_path(path); |
31db9f7c | 3665 | |
9f03740a FDBM |
3666 | ret = process_recorded_refs(sctx, &pending_move); |
3667 | /* Only applicable to an incremental send. */ | |
3668 | ASSERT(pending_move == 0); | |
31db9f7c AB |
3669 | |
3670 | out: | |
3671 | btrfs_free_path(path); | |
3672 | return ret; | |
3673 | } | |
3674 | ||
3675 | static int send_set_xattr(struct send_ctx *sctx, | |
3676 | struct fs_path *path, | |
3677 | const char *name, int name_len, | |
3678 | const char *data, int data_len) | |
3679 | { | |
3680 | int ret = 0; | |
3681 | ||
3682 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); | |
3683 | if (ret < 0) | |
3684 | goto out; | |
3685 | ||
3686 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
3687 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
3688 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); | |
3689 | ||
3690 | ret = send_cmd(sctx); | |
3691 | ||
3692 | tlv_put_failure: | |
3693 | out: | |
3694 | return ret; | |
3695 | } | |
3696 | ||
3697 | static int send_remove_xattr(struct send_ctx *sctx, | |
3698 | struct fs_path *path, | |
3699 | const char *name, int name_len) | |
3700 | { | |
3701 | int ret = 0; | |
3702 | ||
3703 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); | |
3704 | if (ret < 0) | |
3705 | goto out; | |
3706 | ||
3707 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
3708 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
3709 | ||
3710 | ret = send_cmd(sctx); | |
3711 | ||
3712 | tlv_put_failure: | |
3713 | out: | |
3714 | return ret; | |
3715 | } | |
3716 | ||
3717 | static int __process_new_xattr(int num, struct btrfs_key *di_key, | |
3718 | const char *name, int name_len, | |
3719 | const char *data, int data_len, | |
3720 | u8 type, void *ctx) | |
3721 | { | |
3722 | int ret; | |
3723 | struct send_ctx *sctx = ctx; | |
3724 | struct fs_path *p; | |
3725 | posix_acl_xattr_header dummy_acl; | |
3726 | ||
924794c9 | 3727 | p = fs_path_alloc(); |
31db9f7c AB |
3728 | if (!p) |
3729 | return -ENOMEM; | |
3730 | ||
3731 | /* | |
3732 | * This hack is needed because empty acl's are stored as zero byte | |
3733 | * data in xattrs. Problem with that is, that receiving these zero byte | |
3734 | * acl's will fail later. To fix this, we send a dummy acl list that | |
3735 | * only contains the version number and no entries. | |
3736 | */ | |
3737 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || | |
3738 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { | |
3739 | if (data_len == 0) { | |
3740 | dummy_acl.a_version = | |
3741 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); | |
3742 | data = (char *)&dummy_acl; | |
3743 | data_len = sizeof(dummy_acl); | |
3744 | } | |
3745 | } | |
3746 | ||
3747 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3748 | if (ret < 0) | |
3749 | goto out; | |
3750 | ||
3751 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); | |
3752 | ||
3753 | out: | |
924794c9 | 3754 | fs_path_free(p); |
31db9f7c AB |
3755 | return ret; |
3756 | } | |
3757 | ||
3758 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, | |
3759 | const char *name, int name_len, | |
3760 | const char *data, int data_len, | |
3761 | u8 type, void *ctx) | |
3762 | { | |
3763 | int ret; | |
3764 | struct send_ctx *sctx = ctx; | |
3765 | struct fs_path *p; | |
3766 | ||
924794c9 | 3767 | p = fs_path_alloc(); |
31db9f7c AB |
3768 | if (!p) |
3769 | return -ENOMEM; | |
3770 | ||
3771 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3772 | if (ret < 0) | |
3773 | goto out; | |
3774 | ||
3775 | ret = send_remove_xattr(sctx, p, name, name_len); | |
3776 | ||
3777 | out: | |
924794c9 | 3778 | fs_path_free(p); |
31db9f7c AB |
3779 | return ret; |
3780 | } | |
3781 | ||
3782 | static int process_new_xattr(struct send_ctx *sctx) | |
3783 | { | |
3784 | int ret = 0; | |
3785 | ||
924794c9 TI |
3786 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
3787 | sctx->cmp_key, __process_new_xattr, sctx); | |
31db9f7c AB |
3788 | |
3789 | return ret; | |
3790 | } | |
3791 | ||
3792 | static int process_deleted_xattr(struct send_ctx *sctx) | |
3793 | { | |
3794 | int ret; | |
3795 | ||
924794c9 TI |
3796 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
3797 | sctx->cmp_key, __process_deleted_xattr, sctx); | |
31db9f7c AB |
3798 | |
3799 | return ret; | |
3800 | } | |
3801 | ||
3802 | struct find_xattr_ctx { | |
3803 | const char *name; | |
3804 | int name_len; | |
3805 | int found_idx; | |
3806 | char *found_data; | |
3807 | int found_data_len; | |
3808 | }; | |
3809 | ||
3810 | static int __find_xattr(int num, struct btrfs_key *di_key, | |
3811 | const char *name, int name_len, | |
3812 | const char *data, int data_len, | |
3813 | u8 type, void *vctx) | |
3814 | { | |
3815 | struct find_xattr_ctx *ctx = vctx; | |
3816 | ||
3817 | if (name_len == ctx->name_len && | |
3818 | strncmp(name, ctx->name, name_len) == 0) { | |
3819 | ctx->found_idx = num; | |
3820 | ctx->found_data_len = data_len; | |
a5959bc0 | 3821 | ctx->found_data = kmemdup(data, data_len, GFP_NOFS); |
31db9f7c AB |
3822 | if (!ctx->found_data) |
3823 | return -ENOMEM; | |
31db9f7c AB |
3824 | return 1; |
3825 | } | |
3826 | return 0; | |
3827 | } | |
3828 | ||
924794c9 | 3829 | static int find_xattr(struct btrfs_root *root, |
31db9f7c AB |
3830 | struct btrfs_path *path, |
3831 | struct btrfs_key *key, | |
3832 | const char *name, int name_len, | |
3833 | char **data, int *data_len) | |
3834 | { | |
3835 | int ret; | |
3836 | struct find_xattr_ctx ctx; | |
3837 | ||
3838 | ctx.name = name; | |
3839 | ctx.name_len = name_len; | |
3840 | ctx.found_idx = -1; | |
3841 | ctx.found_data = NULL; | |
3842 | ctx.found_data_len = 0; | |
3843 | ||
924794c9 | 3844 | ret = iterate_dir_item(root, path, key, __find_xattr, &ctx); |
31db9f7c AB |
3845 | if (ret < 0) |
3846 | return ret; | |
3847 | ||
3848 | if (ctx.found_idx == -1) | |
3849 | return -ENOENT; | |
3850 | if (data) { | |
3851 | *data = ctx.found_data; | |
3852 | *data_len = ctx.found_data_len; | |
3853 | } else { | |
3854 | kfree(ctx.found_data); | |
3855 | } | |
3856 | return ctx.found_idx; | |
3857 | } | |
3858 | ||
3859 | ||
3860 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, | |
3861 | const char *name, int name_len, | |
3862 | const char *data, int data_len, | |
3863 | u8 type, void *ctx) | |
3864 | { | |
3865 | int ret; | |
3866 | struct send_ctx *sctx = ctx; | |
3867 | char *found_data = NULL; | |
3868 | int found_data_len = 0; | |
31db9f7c | 3869 | |
924794c9 TI |
3870 | ret = find_xattr(sctx->parent_root, sctx->right_path, |
3871 | sctx->cmp_key, name, name_len, &found_data, | |
3872 | &found_data_len); | |
31db9f7c AB |
3873 | if (ret == -ENOENT) { |
3874 | ret = __process_new_xattr(num, di_key, name, name_len, data, | |
3875 | data_len, type, ctx); | |
3876 | } else if (ret >= 0) { | |
3877 | if (data_len != found_data_len || | |
3878 | memcmp(data, found_data, data_len)) { | |
3879 | ret = __process_new_xattr(num, di_key, name, name_len, | |
3880 | data, data_len, type, ctx); | |
3881 | } else { | |
3882 | ret = 0; | |
3883 | } | |
3884 | } | |
3885 | ||
3886 | kfree(found_data); | |
31db9f7c AB |
3887 | return ret; |
3888 | } | |
3889 | ||
3890 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, | |
3891 | const char *name, int name_len, | |
3892 | const char *data, int data_len, | |
3893 | u8 type, void *ctx) | |
3894 | { | |
3895 | int ret; | |
3896 | struct send_ctx *sctx = ctx; | |
3897 | ||
924794c9 TI |
3898 | ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, |
3899 | name, name_len, NULL, NULL); | |
31db9f7c AB |
3900 | if (ret == -ENOENT) |
3901 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, | |
3902 | data_len, type, ctx); | |
3903 | else if (ret >= 0) | |
3904 | ret = 0; | |
3905 | ||
3906 | return ret; | |
3907 | } | |
3908 | ||
3909 | static int process_changed_xattr(struct send_ctx *sctx) | |
3910 | { | |
3911 | int ret = 0; | |
3912 | ||
924794c9 | 3913 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
31db9f7c AB |
3914 | sctx->cmp_key, __process_changed_new_xattr, sctx); |
3915 | if (ret < 0) | |
3916 | goto out; | |
924794c9 | 3917 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
31db9f7c AB |
3918 | sctx->cmp_key, __process_changed_deleted_xattr, sctx); |
3919 | ||
3920 | out: | |
3921 | return ret; | |
3922 | } | |
3923 | ||
3924 | static int process_all_new_xattrs(struct send_ctx *sctx) | |
3925 | { | |
3926 | int ret; | |
3927 | struct btrfs_root *root; | |
3928 | struct btrfs_path *path; | |
3929 | struct btrfs_key key; | |
3930 | struct btrfs_key found_key; | |
3931 | struct extent_buffer *eb; | |
3932 | int slot; | |
3933 | ||
3934 | path = alloc_path_for_send(); | |
3935 | if (!path) | |
3936 | return -ENOMEM; | |
3937 | ||
3938 | root = sctx->send_root; | |
3939 | ||
3940 | key.objectid = sctx->cmp_key->objectid; | |
3941 | key.type = BTRFS_XATTR_ITEM_KEY; | |
3942 | key.offset = 0; | |
dff6d0ad FDBM |
3943 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
3944 | if (ret < 0) | |
3945 | goto out; | |
31db9f7c | 3946 | |
dff6d0ad | 3947 | while (1) { |
31db9f7c AB |
3948 | eb = path->nodes[0]; |
3949 | slot = path->slots[0]; | |
dff6d0ad FDBM |
3950 | if (slot >= btrfs_header_nritems(eb)) { |
3951 | ret = btrfs_next_leaf(root, path); | |
3952 | if (ret < 0) { | |
3953 | goto out; | |
3954 | } else if (ret > 0) { | |
3955 | ret = 0; | |
3956 | break; | |
3957 | } | |
3958 | continue; | |
3959 | } | |
31db9f7c | 3960 | |
dff6d0ad | 3961 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
31db9f7c AB |
3962 | if (found_key.objectid != key.objectid || |
3963 | found_key.type != key.type) { | |
3964 | ret = 0; | |
3965 | goto out; | |
3966 | } | |
3967 | ||
924794c9 TI |
3968 | ret = iterate_dir_item(root, path, &found_key, |
3969 | __process_new_xattr, sctx); | |
31db9f7c AB |
3970 | if (ret < 0) |
3971 | goto out; | |
3972 | ||
dff6d0ad | 3973 | path->slots[0]++; |
31db9f7c AB |
3974 | } |
3975 | ||
3976 | out: | |
3977 | btrfs_free_path(path); | |
3978 | return ret; | |
3979 | } | |
3980 | ||
ed259095 JB |
3981 | static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len) |
3982 | { | |
3983 | struct btrfs_root *root = sctx->send_root; | |
3984 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3985 | struct inode *inode; | |
3986 | struct page *page; | |
3987 | char *addr; | |
3988 | struct btrfs_key key; | |
3989 | pgoff_t index = offset >> PAGE_CACHE_SHIFT; | |
3990 | pgoff_t last_index; | |
3991 | unsigned pg_offset = offset & ~PAGE_CACHE_MASK; | |
3992 | ssize_t ret = 0; | |
3993 | ||
3994 | key.objectid = sctx->cur_ino; | |
3995 | key.type = BTRFS_INODE_ITEM_KEY; | |
3996 | key.offset = 0; | |
3997 | ||
3998 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
3999 | if (IS_ERR(inode)) | |
4000 | return PTR_ERR(inode); | |
4001 | ||
4002 | if (offset + len > i_size_read(inode)) { | |
4003 | if (offset > i_size_read(inode)) | |
4004 | len = 0; | |
4005 | else | |
4006 | len = offset - i_size_read(inode); | |
4007 | } | |
4008 | if (len == 0) | |
4009 | goto out; | |
4010 | ||
4011 | last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT; | |
4012 | while (index <= last_index) { | |
4013 | unsigned cur_len = min_t(unsigned, len, | |
4014 | PAGE_CACHE_SIZE - pg_offset); | |
4015 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
4016 | if (!page) { | |
4017 | ret = -ENOMEM; | |
4018 | break; | |
4019 | } | |
4020 | ||
4021 | if (!PageUptodate(page)) { | |
4022 | btrfs_readpage(NULL, page); | |
4023 | lock_page(page); | |
4024 | if (!PageUptodate(page)) { | |
4025 | unlock_page(page); | |
4026 | page_cache_release(page); | |
4027 | ret = -EIO; | |
4028 | break; | |
4029 | } | |
4030 | } | |
4031 | ||
4032 | addr = kmap(page); | |
4033 | memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len); | |
4034 | kunmap(page); | |
4035 | unlock_page(page); | |
4036 | page_cache_release(page); | |
4037 | index++; | |
4038 | pg_offset = 0; | |
4039 | len -= cur_len; | |
4040 | ret += cur_len; | |
4041 | } | |
4042 | out: | |
4043 | iput(inode); | |
4044 | return ret; | |
4045 | } | |
4046 | ||
31db9f7c AB |
4047 | /* |
4048 | * Read some bytes from the current inode/file and send a write command to | |
4049 | * user space. | |
4050 | */ | |
4051 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) | |
4052 | { | |
4053 | int ret = 0; | |
4054 | struct fs_path *p; | |
ed259095 | 4055 | ssize_t num_read = 0; |
31db9f7c | 4056 | |
924794c9 | 4057 | p = fs_path_alloc(); |
31db9f7c AB |
4058 | if (!p) |
4059 | return -ENOMEM; | |
4060 | ||
31db9f7c AB |
4061 | verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len); |
4062 | ||
ed259095 JB |
4063 | num_read = fill_read_buf(sctx, offset, len); |
4064 | if (num_read <= 0) { | |
4065 | if (num_read < 0) | |
4066 | ret = num_read; | |
31db9f7c | 4067 | goto out; |
ed259095 | 4068 | } |
31db9f7c AB |
4069 | |
4070 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); | |
4071 | if (ret < 0) | |
4072 | goto out; | |
4073 | ||
4074 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4075 | if (ret < 0) | |
4076 | goto out; | |
4077 | ||
4078 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
4079 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
e938c8ad | 4080 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read); |
31db9f7c AB |
4081 | |
4082 | ret = send_cmd(sctx); | |
4083 | ||
4084 | tlv_put_failure: | |
4085 | out: | |
924794c9 | 4086 | fs_path_free(p); |
31db9f7c AB |
4087 | if (ret < 0) |
4088 | return ret; | |
e938c8ad | 4089 | return num_read; |
31db9f7c AB |
4090 | } |
4091 | ||
4092 | /* | |
4093 | * Send a clone command to user space. | |
4094 | */ | |
4095 | static int send_clone(struct send_ctx *sctx, | |
4096 | u64 offset, u32 len, | |
4097 | struct clone_root *clone_root) | |
4098 | { | |
4099 | int ret = 0; | |
31db9f7c AB |
4100 | struct fs_path *p; |
4101 | u64 gen; | |
4102 | ||
4103 | verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, " | |
4104 | "clone_inode=%llu, clone_offset=%llu\n", offset, len, | |
4105 | clone_root->root->objectid, clone_root->ino, | |
4106 | clone_root->offset); | |
4107 | ||
924794c9 | 4108 | p = fs_path_alloc(); |
31db9f7c AB |
4109 | if (!p) |
4110 | return -ENOMEM; | |
4111 | ||
4112 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); | |
4113 | if (ret < 0) | |
4114 | goto out; | |
4115 | ||
4116 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4117 | if (ret < 0) | |
4118 | goto out; | |
4119 | ||
4120 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
4121 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); | |
4122 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
4123 | ||
e938c8ad | 4124 | if (clone_root->root == sctx->send_root) { |
31db9f7c | 4125 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
85a7b33b | 4126 | &gen, NULL, NULL, NULL, NULL); |
31db9f7c AB |
4127 | if (ret < 0) |
4128 | goto out; | |
4129 | ret = get_cur_path(sctx, clone_root->ino, gen, p); | |
4130 | } else { | |
924794c9 | 4131 | ret = get_inode_path(clone_root->root, clone_root->ino, p); |
31db9f7c AB |
4132 | } |
4133 | if (ret < 0) | |
4134 | goto out; | |
4135 | ||
4136 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
e938c8ad | 4137 | clone_root->root->root_item.uuid); |
31db9f7c | 4138 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
5a0f4e2c | 4139 | le64_to_cpu(clone_root->root->root_item.ctransid)); |
31db9f7c AB |
4140 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
4141 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, | |
4142 | clone_root->offset); | |
4143 | ||
4144 | ret = send_cmd(sctx); | |
4145 | ||
4146 | tlv_put_failure: | |
4147 | out: | |
924794c9 | 4148 | fs_path_free(p); |
31db9f7c AB |
4149 | return ret; |
4150 | } | |
4151 | ||
cb95e7bf MF |
4152 | /* |
4153 | * Send an update extent command to user space. | |
4154 | */ | |
4155 | static int send_update_extent(struct send_ctx *sctx, | |
4156 | u64 offset, u32 len) | |
4157 | { | |
4158 | int ret = 0; | |
4159 | struct fs_path *p; | |
4160 | ||
924794c9 | 4161 | p = fs_path_alloc(); |
cb95e7bf MF |
4162 | if (!p) |
4163 | return -ENOMEM; | |
4164 | ||
4165 | ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); | |
4166 | if (ret < 0) | |
4167 | goto out; | |
4168 | ||
4169 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4170 | if (ret < 0) | |
4171 | goto out; | |
4172 | ||
4173 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
4174 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
4175 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); | |
4176 | ||
4177 | ret = send_cmd(sctx); | |
4178 | ||
4179 | tlv_put_failure: | |
4180 | out: | |
924794c9 | 4181 | fs_path_free(p); |
cb95e7bf MF |
4182 | return ret; |
4183 | } | |
4184 | ||
16e7549f JB |
4185 | static int send_hole(struct send_ctx *sctx, u64 end) |
4186 | { | |
4187 | struct fs_path *p = NULL; | |
4188 | u64 offset = sctx->cur_inode_last_extent; | |
4189 | u64 len; | |
4190 | int ret = 0; | |
4191 | ||
4192 | p = fs_path_alloc(); | |
4193 | if (!p) | |
4194 | return -ENOMEM; | |
4195 | memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE); | |
4196 | while (offset < end) { | |
4197 | len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE); | |
4198 | ||
4199 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); | |
4200 | if (ret < 0) | |
4201 | break; | |
4202 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4203 | if (ret < 0) | |
4204 | break; | |
4205 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
4206 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
4207 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len); | |
4208 | ret = send_cmd(sctx); | |
4209 | if (ret < 0) | |
4210 | break; | |
4211 | offset += len; | |
4212 | } | |
4213 | tlv_put_failure: | |
4214 | fs_path_free(p); | |
4215 | return ret; | |
4216 | } | |
4217 | ||
31db9f7c AB |
4218 | static int send_write_or_clone(struct send_ctx *sctx, |
4219 | struct btrfs_path *path, | |
4220 | struct btrfs_key *key, | |
4221 | struct clone_root *clone_root) | |
4222 | { | |
4223 | int ret = 0; | |
4224 | struct btrfs_file_extent_item *ei; | |
4225 | u64 offset = key->offset; | |
4226 | u64 pos = 0; | |
4227 | u64 len; | |
4228 | u32 l; | |
4229 | u8 type; | |
28e5dd8f | 4230 | u64 bs = sctx->send_root->fs_info->sb->s_blocksize; |
31db9f7c AB |
4231 | |
4232 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4233 | struct btrfs_file_extent_item); | |
4234 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
74dd17fb | 4235 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
514ac8ad CM |
4236 | len = btrfs_file_extent_inline_len(path->nodes[0], |
4237 | path->slots[0], ei); | |
74dd17fb CM |
4238 | /* |
4239 | * it is possible the inline item won't cover the whole page, | |
4240 | * but there may be items after this page. Make | |
4241 | * sure to send the whole thing | |
4242 | */ | |
4243 | len = PAGE_CACHE_ALIGN(len); | |
4244 | } else { | |
31db9f7c | 4245 | len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
74dd17fb | 4246 | } |
31db9f7c AB |
4247 | |
4248 | if (offset + len > sctx->cur_inode_size) | |
4249 | len = sctx->cur_inode_size - offset; | |
4250 | if (len == 0) { | |
4251 | ret = 0; | |
4252 | goto out; | |
4253 | } | |
4254 | ||
28e5dd8f | 4255 | if (clone_root && IS_ALIGNED(offset + len, bs)) { |
cb95e7bf MF |
4256 | ret = send_clone(sctx, offset, len, clone_root); |
4257 | } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) { | |
4258 | ret = send_update_extent(sctx, offset, len); | |
4259 | } else { | |
31db9f7c AB |
4260 | while (pos < len) { |
4261 | l = len - pos; | |
4262 | if (l > BTRFS_SEND_READ_SIZE) | |
4263 | l = BTRFS_SEND_READ_SIZE; | |
4264 | ret = send_write(sctx, pos + offset, l); | |
4265 | if (ret < 0) | |
4266 | goto out; | |
4267 | if (!ret) | |
4268 | break; | |
4269 | pos += ret; | |
4270 | } | |
4271 | ret = 0; | |
31db9f7c | 4272 | } |
31db9f7c AB |
4273 | out: |
4274 | return ret; | |
4275 | } | |
4276 | ||
4277 | static int is_extent_unchanged(struct send_ctx *sctx, | |
4278 | struct btrfs_path *left_path, | |
4279 | struct btrfs_key *ekey) | |
4280 | { | |
4281 | int ret = 0; | |
4282 | struct btrfs_key key; | |
4283 | struct btrfs_path *path = NULL; | |
4284 | struct extent_buffer *eb; | |
4285 | int slot; | |
4286 | struct btrfs_key found_key; | |
4287 | struct btrfs_file_extent_item *ei; | |
4288 | u64 left_disknr; | |
4289 | u64 right_disknr; | |
4290 | u64 left_offset; | |
4291 | u64 right_offset; | |
4292 | u64 left_offset_fixed; | |
4293 | u64 left_len; | |
4294 | u64 right_len; | |
74dd17fb CM |
4295 | u64 left_gen; |
4296 | u64 right_gen; | |
31db9f7c AB |
4297 | u8 left_type; |
4298 | u8 right_type; | |
4299 | ||
4300 | path = alloc_path_for_send(); | |
4301 | if (!path) | |
4302 | return -ENOMEM; | |
4303 | ||
4304 | eb = left_path->nodes[0]; | |
4305 | slot = left_path->slots[0]; | |
31db9f7c AB |
4306 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
4307 | left_type = btrfs_file_extent_type(eb, ei); | |
31db9f7c AB |
4308 | |
4309 | if (left_type != BTRFS_FILE_EXTENT_REG) { | |
4310 | ret = 0; | |
4311 | goto out; | |
4312 | } | |
74dd17fb CM |
4313 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
4314 | left_len = btrfs_file_extent_num_bytes(eb, ei); | |
4315 | left_offset = btrfs_file_extent_offset(eb, ei); | |
4316 | left_gen = btrfs_file_extent_generation(eb, ei); | |
31db9f7c AB |
4317 | |
4318 | /* | |
4319 | * Following comments will refer to these graphics. L is the left | |
4320 | * extents which we are checking at the moment. 1-8 are the right | |
4321 | * extents that we iterate. | |
4322 | * | |
4323 | * |-----L-----| | |
4324 | * |-1-|-2a-|-3-|-4-|-5-|-6-| | |
4325 | * | |
4326 | * |-----L-----| | |
4327 | * |--1--|-2b-|...(same as above) | |
4328 | * | |
4329 | * Alternative situation. Happens on files where extents got split. | |
4330 | * |-----L-----| | |
4331 | * |-----------7-----------|-6-| | |
4332 | * | |
4333 | * Alternative situation. Happens on files which got larger. | |
4334 | * |-----L-----| | |
4335 | * |-8-| | |
4336 | * Nothing follows after 8. | |
4337 | */ | |
4338 | ||
4339 | key.objectid = ekey->objectid; | |
4340 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4341 | key.offset = ekey->offset; | |
4342 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); | |
4343 | if (ret < 0) | |
4344 | goto out; | |
4345 | if (ret) { | |
4346 | ret = 0; | |
4347 | goto out; | |
4348 | } | |
4349 | ||
4350 | /* | |
4351 | * Handle special case where the right side has no extents at all. | |
4352 | */ | |
4353 | eb = path->nodes[0]; | |
4354 | slot = path->slots[0]; | |
4355 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
4356 | if (found_key.objectid != key.objectid || | |
4357 | found_key.type != key.type) { | |
57cfd462 JB |
4358 | /* If we're a hole then just pretend nothing changed */ |
4359 | ret = (left_disknr) ? 0 : 1; | |
31db9f7c AB |
4360 | goto out; |
4361 | } | |
4362 | ||
4363 | /* | |
4364 | * We're now on 2a, 2b or 7. | |
4365 | */ | |
4366 | key = found_key; | |
4367 | while (key.offset < ekey->offset + left_len) { | |
4368 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
4369 | right_type = btrfs_file_extent_type(eb, ei); | |
31db9f7c AB |
4370 | if (right_type != BTRFS_FILE_EXTENT_REG) { |
4371 | ret = 0; | |
4372 | goto out; | |
4373 | } | |
4374 | ||
007d31f7 JB |
4375 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
4376 | right_len = btrfs_file_extent_num_bytes(eb, ei); | |
4377 | right_offset = btrfs_file_extent_offset(eb, ei); | |
4378 | right_gen = btrfs_file_extent_generation(eb, ei); | |
4379 | ||
31db9f7c AB |
4380 | /* |
4381 | * Are we at extent 8? If yes, we know the extent is changed. | |
4382 | * This may only happen on the first iteration. | |
4383 | */ | |
d8347fa4 | 4384 | if (found_key.offset + right_len <= ekey->offset) { |
57cfd462 JB |
4385 | /* If we're a hole just pretend nothing changed */ |
4386 | ret = (left_disknr) ? 0 : 1; | |
31db9f7c AB |
4387 | goto out; |
4388 | } | |
4389 | ||
4390 | left_offset_fixed = left_offset; | |
4391 | if (key.offset < ekey->offset) { | |
4392 | /* Fix the right offset for 2a and 7. */ | |
4393 | right_offset += ekey->offset - key.offset; | |
4394 | } else { | |
4395 | /* Fix the left offset for all behind 2a and 2b */ | |
4396 | left_offset_fixed += key.offset - ekey->offset; | |
4397 | } | |
4398 | ||
4399 | /* | |
4400 | * Check if we have the same extent. | |
4401 | */ | |
3954096d | 4402 | if (left_disknr != right_disknr || |
74dd17fb CM |
4403 | left_offset_fixed != right_offset || |
4404 | left_gen != right_gen) { | |
31db9f7c AB |
4405 | ret = 0; |
4406 | goto out; | |
4407 | } | |
4408 | ||
4409 | /* | |
4410 | * Go to the next extent. | |
4411 | */ | |
4412 | ret = btrfs_next_item(sctx->parent_root, path); | |
4413 | if (ret < 0) | |
4414 | goto out; | |
4415 | if (!ret) { | |
4416 | eb = path->nodes[0]; | |
4417 | slot = path->slots[0]; | |
4418 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
4419 | } | |
4420 | if (ret || found_key.objectid != key.objectid || | |
4421 | found_key.type != key.type) { | |
4422 | key.offset += right_len; | |
4423 | break; | |
adaa4b8e JS |
4424 | } |
4425 | if (found_key.offset != key.offset + right_len) { | |
4426 | ret = 0; | |
4427 | goto out; | |
31db9f7c AB |
4428 | } |
4429 | key = found_key; | |
4430 | } | |
4431 | ||
4432 | /* | |
4433 | * We're now behind the left extent (treat as unchanged) or at the end | |
4434 | * of the right side (treat as changed). | |
4435 | */ | |
4436 | if (key.offset >= ekey->offset + left_len) | |
4437 | ret = 1; | |
4438 | else | |
4439 | ret = 0; | |
4440 | ||
4441 | ||
4442 | out: | |
4443 | btrfs_free_path(path); | |
4444 | return ret; | |
4445 | } | |
4446 | ||
16e7549f JB |
4447 | static int get_last_extent(struct send_ctx *sctx, u64 offset) |
4448 | { | |
4449 | struct btrfs_path *path; | |
4450 | struct btrfs_root *root = sctx->send_root; | |
4451 | struct btrfs_file_extent_item *fi; | |
4452 | struct btrfs_key key; | |
4453 | u64 extent_end; | |
4454 | u8 type; | |
4455 | int ret; | |
4456 | ||
4457 | path = alloc_path_for_send(); | |
4458 | if (!path) | |
4459 | return -ENOMEM; | |
4460 | ||
4461 | sctx->cur_inode_last_extent = 0; | |
4462 | ||
4463 | key.objectid = sctx->cur_ino; | |
4464 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4465 | key.offset = offset; | |
4466 | ret = btrfs_search_slot_for_read(root, &key, path, 0, 1); | |
4467 | if (ret < 0) | |
4468 | goto out; | |
4469 | ret = 0; | |
4470 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4471 | if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
4472 | goto out; | |
4473 | ||
4474 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4475 | struct btrfs_file_extent_item); | |
4476 | type = btrfs_file_extent_type(path->nodes[0], fi); | |
4477 | if (type == BTRFS_FILE_EXTENT_INLINE) { | |
514ac8ad CM |
4478 | u64 size = btrfs_file_extent_inline_len(path->nodes[0], |
4479 | path->slots[0], fi); | |
16e7549f JB |
4480 | extent_end = ALIGN(key.offset + size, |
4481 | sctx->send_root->sectorsize); | |
4482 | } else { | |
4483 | extent_end = key.offset + | |
4484 | btrfs_file_extent_num_bytes(path->nodes[0], fi); | |
4485 | } | |
4486 | sctx->cur_inode_last_extent = extent_end; | |
4487 | out: | |
4488 | btrfs_free_path(path); | |
4489 | return ret; | |
4490 | } | |
4491 | ||
4492 | static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, | |
4493 | struct btrfs_key *key) | |
4494 | { | |
4495 | struct btrfs_file_extent_item *fi; | |
4496 | u64 extent_end; | |
4497 | u8 type; | |
4498 | int ret = 0; | |
4499 | ||
4500 | if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) | |
4501 | return 0; | |
4502 | ||
4503 | if (sctx->cur_inode_last_extent == (u64)-1) { | |
4504 | ret = get_last_extent(sctx, key->offset - 1); | |
4505 | if (ret) | |
4506 | return ret; | |
4507 | } | |
4508 | ||
4509 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4510 | struct btrfs_file_extent_item); | |
4511 | type = btrfs_file_extent_type(path->nodes[0], fi); | |
4512 | if (type == BTRFS_FILE_EXTENT_INLINE) { | |
514ac8ad CM |
4513 | u64 size = btrfs_file_extent_inline_len(path->nodes[0], |
4514 | path->slots[0], fi); | |
16e7549f JB |
4515 | extent_end = ALIGN(key->offset + size, |
4516 | sctx->send_root->sectorsize); | |
4517 | } else { | |
4518 | extent_end = key->offset + | |
4519 | btrfs_file_extent_num_bytes(path->nodes[0], fi); | |
4520 | } | |
bf54f412 FDBM |
4521 | |
4522 | if (path->slots[0] == 0 && | |
4523 | sctx->cur_inode_last_extent < key->offset) { | |
4524 | /* | |
4525 | * We might have skipped entire leafs that contained only | |
4526 | * file extent items for our current inode. These leafs have | |
4527 | * a generation number smaller (older) than the one in the | |
4528 | * current leaf and the leaf our last extent came from, and | |
4529 | * are located between these 2 leafs. | |
4530 | */ | |
4531 | ret = get_last_extent(sctx, key->offset - 1); | |
4532 | if (ret) | |
4533 | return ret; | |
4534 | } | |
4535 | ||
16e7549f JB |
4536 | if (sctx->cur_inode_last_extent < key->offset) |
4537 | ret = send_hole(sctx, key->offset); | |
4538 | sctx->cur_inode_last_extent = extent_end; | |
4539 | return ret; | |
4540 | } | |
4541 | ||
31db9f7c AB |
4542 | static int process_extent(struct send_ctx *sctx, |
4543 | struct btrfs_path *path, | |
4544 | struct btrfs_key *key) | |
4545 | { | |
31db9f7c | 4546 | struct clone_root *found_clone = NULL; |
57cfd462 | 4547 | int ret = 0; |
31db9f7c AB |
4548 | |
4549 | if (S_ISLNK(sctx->cur_inode_mode)) | |
4550 | return 0; | |
4551 | ||
4552 | if (sctx->parent_root && !sctx->cur_inode_new) { | |
4553 | ret = is_extent_unchanged(sctx, path, key); | |
4554 | if (ret < 0) | |
4555 | goto out; | |
4556 | if (ret) { | |
4557 | ret = 0; | |
16e7549f | 4558 | goto out_hole; |
31db9f7c | 4559 | } |
57cfd462 JB |
4560 | } else { |
4561 | struct btrfs_file_extent_item *ei; | |
4562 | u8 type; | |
4563 | ||
4564 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4565 | struct btrfs_file_extent_item); | |
4566 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
4567 | if (type == BTRFS_FILE_EXTENT_PREALLOC || | |
4568 | type == BTRFS_FILE_EXTENT_REG) { | |
4569 | /* | |
4570 | * The send spec does not have a prealloc command yet, | |
4571 | * so just leave a hole for prealloc'ed extents until | |
4572 | * we have enough commands queued up to justify rev'ing | |
4573 | * the send spec. | |
4574 | */ | |
4575 | if (type == BTRFS_FILE_EXTENT_PREALLOC) { | |
4576 | ret = 0; | |
4577 | goto out; | |
4578 | } | |
4579 | ||
4580 | /* Have a hole, just skip it. */ | |
4581 | if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) { | |
4582 | ret = 0; | |
4583 | goto out; | |
4584 | } | |
4585 | } | |
31db9f7c AB |
4586 | } |
4587 | ||
4588 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, | |
4589 | sctx->cur_inode_size, &found_clone); | |
4590 | if (ret != -ENOENT && ret < 0) | |
4591 | goto out; | |
4592 | ||
4593 | ret = send_write_or_clone(sctx, path, key, found_clone); | |
16e7549f JB |
4594 | if (ret) |
4595 | goto out; | |
4596 | out_hole: | |
4597 | ret = maybe_send_hole(sctx, path, key); | |
31db9f7c AB |
4598 | out: |
4599 | return ret; | |
4600 | } | |
4601 | ||
4602 | static int process_all_extents(struct send_ctx *sctx) | |
4603 | { | |
4604 | int ret; | |
4605 | struct btrfs_root *root; | |
4606 | struct btrfs_path *path; | |
4607 | struct btrfs_key key; | |
4608 | struct btrfs_key found_key; | |
4609 | struct extent_buffer *eb; | |
4610 | int slot; | |
4611 | ||
4612 | root = sctx->send_root; | |
4613 | path = alloc_path_for_send(); | |
4614 | if (!path) | |
4615 | return -ENOMEM; | |
4616 | ||
4617 | key.objectid = sctx->cmp_key->objectid; | |
4618 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4619 | key.offset = 0; | |
7fdd29d0 FDBM |
4620 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
4621 | if (ret < 0) | |
4622 | goto out; | |
31db9f7c | 4623 | |
7fdd29d0 | 4624 | while (1) { |
31db9f7c AB |
4625 | eb = path->nodes[0]; |
4626 | slot = path->slots[0]; | |
7fdd29d0 FDBM |
4627 | |
4628 | if (slot >= btrfs_header_nritems(eb)) { | |
4629 | ret = btrfs_next_leaf(root, path); | |
4630 | if (ret < 0) { | |
4631 | goto out; | |
4632 | } else if (ret > 0) { | |
4633 | ret = 0; | |
4634 | break; | |
4635 | } | |
4636 | continue; | |
4637 | } | |
4638 | ||
31db9f7c AB |
4639 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
4640 | ||
4641 | if (found_key.objectid != key.objectid || | |
4642 | found_key.type != key.type) { | |
4643 | ret = 0; | |
4644 | goto out; | |
4645 | } | |
4646 | ||
4647 | ret = process_extent(sctx, path, &found_key); | |
4648 | if (ret < 0) | |
4649 | goto out; | |
4650 | ||
7fdd29d0 | 4651 | path->slots[0]++; |
31db9f7c AB |
4652 | } |
4653 | ||
4654 | out: | |
4655 | btrfs_free_path(path); | |
4656 | return ret; | |
4657 | } | |
4658 | ||
9f03740a FDBM |
4659 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, |
4660 | int *pending_move, | |
4661 | int *refs_processed) | |
31db9f7c AB |
4662 | { |
4663 | int ret = 0; | |
4664 | ||
4665 | if (sctx->cur_ino == 0) | |
4666 | goto out; | |
4667 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && | |
96b5bd77 | 4668 | sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) |
31db9f7c AB |
4669 | goto out; |
4670 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) | |
4671 | goto out; | |
4672 | ||
9f03740a | 4673 | ret = process_recorded_refs(sctx, pending_move); |
e479d9bb AB |
4674 | if (ret < 0) |
4675 | goto out; | |
4676 | ||
9f03740a | 4677 | *refs_processed = 1; |
31db9f7c AB |
4678 | out: |
4679 | return ret; | |
4680 | } | |
4681 | ||
4682 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) | |
4683 | { | |
4684 | int ret = 0; | |
4685 | u64 left_mode; | |
4686 | u64 left_uid; | |
4687 | u64 left_gid; | |
4688 | u64 right_mode; | |
4689 | u64 right_uid; | |
4690 | u64 right_gid; | |
4691 | int need_chmod = 0; | |
4692 | int need_chown = 0; | |
9f03740a FDBM |
4693 | int pending_move = 0; |
4694 | int refs_processed = 0; | |
31db9f7c | 4695 | |
9f03740a FDBM |
4696 | ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, |
4697 | &refs_processed); | |
31db9f7c AB |
4698 | if (ret < 0) |
4699 | goto out; | |
4700 | ||
9f03740a FDBM |
4701 | /* |
4702 | * We have processed the refs and thus need to advance send_progress. | |
4703 | * Now, calls to get_cur_xxx will take the updated refs of the current | |
4704 | * inode into account. | |
4705 | * | |
4706 | * On the other hand, if our current inode is a directory and couldn't | |
4707 | * be moved/renamed because its parent was renamed/moved too and it has | |
4708 | * a higher inode number, we can only move/rename our current inode | |
4709 | * after we moved/renamed its parent. Therefore in this case operate on | |
4710 | * the old path (pre move/rename) of our current inode, and the | |
4711 | * move/rename will be performed later. | |
4712 | */ | |
4713 | if (refs_processed && !pending_move) | |
4714 | sctx->send_progress = sctx->cur_ino + 1; | |
4715 | ||
31db9f7c AB |
4716 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
4717 | goto out; | |
4718 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) | |
4719 | goto out; | |
4720 | ||
4721 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, | |
85a7b33b | 4722 | &left_mode, &left_uid, &left_gid, NULL); |
31db9f7c AB |
4723 | if (ret < 0) |
4724 | goto out; | |
4725 | ||
e2d044fe AL |
4726 | if (!sctx->parent_root || sctx->cur_inode_new) { |
4727 | need_chown = 1; | |
4728 | if (!S_ISLNK(sctx->cur_inode_mode)) | |
31db9f7c | 4729 | need_chmod = 1; |
e2d044fe AL |
4730 | } else { |
4731 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, | |
4732 | NULL, NULL, &right_mode, &right_uid, | |
4733 | &right_gid, NULL); | |
4734 | if (ret < 0) | |
4735 | goto out; | |
31db9f7c | 4736 | |
e2d044fe AL |
4737 | if (left_uid != right_uid || left_gid != right_gid) |
4738 | need_chown = 1; | |
4739 | if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) | |
4740 | need_chmod = 1; | |
31db9f7c AB |
4741 | } |
4742 | ||
4743 | if (S_ISREG(sctx->cur_inode_mode)) { | |
16e7549f JB |
4744 | if (need_send_hole(sctx)) { |
4745 | if (sctx->cur_inode_last_extent == (u64)-1) { | |
4746 | ret = get_last_extent(sctx, (u64)-1); | |
4747 | if (ret) | |
4748 | goto out; | |
4749 | } | |
4750 | if (sctx->cur_inode_last_extent < | |
4751 | sctx->cur_inode_size) { | |
4752 | ret = send_hole(sctx, sctx->cur_inode_size); | |
4753 | if (ret) | |
4754 | goto out; | |
4755 | } | |
4756 | } | |
31db9f7c AB |
4757 | ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
4758 | sctx->cur_inode_size); | |
4759 | if (ret < 0) | |
4760 | goto out; | |
4761 | } | |
4762 | ||
4763 | if (need_chown) { | |
4764 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
4765 | left_uid, left_gid); | |
4766 | if (ret < 0) | |
4767 | goto out; | |
4768 | } | |
4769 | if (need_chmod) { | |
4770 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
4771 | left_mode); | |
4772 | if (ret < 0) | |
4773 | goto out; | |
4774 | } | |
4775 | ||
4776 | /* | |
9f03740a FDBM |
4777 | * If other directory inodes depended on our current directory |
4778 | * inode's move/rename, now do their move/rename operations. | |
31db9f7c | 4779 | */ |
9f03740a FDBM |
4780 | if (!is_waiting_for_move(sctx, sctx->cur_ino)) { |
4781 | ret = apply_children_dir_moves(sctx); | |
4782 | if (ret) | |
4783 | goto out; | |
4784 | } | |
4785 | ||
4786 | /* | |
4787 | * Need to send that every time, no matter if it actually | |
4788 | * changed between the two trees as we have done changes to | |
4789 | * the inode before. | |
4790 | */ | |
4791 | sctx->send_progress = sctx->cur_ino + 1; | |
31db9f7c AB |
4792 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); |
4793 | if (ret < 0) | |
4794 | goto out; | |
4795 | ||
4796 | out: | |
4797 | return ret; | |
4798 | } | |
4799 | ||
4800 | static int changed_inode(struct send_ctx *sctx, | |
4801 | enum btrfs_compare_tree_result result) | |
4802 | { | |
4803 | int ret = 0; | |
4804 | struct btrfs_key *key = sctx->cmp_key; | |
4805 | struct btrfs_inode_item *left_ii = NULL; | |
4806 | struct btrfs_inode_item *right_ii = NULL; | |
4807 | u64 left_gen = 0; | |
4808 | u64 right_gen = 0; | |
4809 | ||
31db9f7c AB |
4810 | sctx->cur_ino = key->objectid; |
4811 | sctx->cur_inode_new_gen = 0; | |
16e7549f | 4812 | sctx->cur_inode_last_extent = (u64)-1; |
e479d9bb AB |
4813 | |
4814 | /* | |
4815 | * Set send_progress to current inode. This will tell all get_cur_xxx | |
4816 | * functions that the current inode's refs are not updated yet. Later, | |
4817 | * when process_recorded_refs is finished, it is set to cur_ino + 1. | |
4818 | */ | |
31db9f7c AB |
4819 | sctx->send_progress = sctx->cur_ino; |
4820 | ||
4821 | if (result == BTRFS_COMPARE_TREE_NEW || | |
4822 | result == BTRFS_COMPARE_TREE_CHANGED) { | |
4823 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], | |
4824 | sctx->left_path->slots[0], | |
4825 | struct btrfs_inode_item); | |
4826 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], | |
4827 | left_ii); | |
4828 | } else { | |
4829 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
4830 | sctx->right_path->slots[0], | |
4831 | struct btrfs_inode_item); | |
4832 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
4833 | right_ii); | |
4834 | } | |
4835 | if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
4836 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
4837 | sctx->right_path->slots[0], | |
4838 | struct btrfs_inode_item); | |
4839 | ||
4840 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
4841 | right_ii); | |
6d85ed05 AB |
4842 | |
4843 | /* | |
4844 | * The cur_ino = root dir case is special here. We can't treat | |
4845 | * the inode as deleted+reused because it would generate a | |
4846 | * stream that tries to delete/mkdir the root dir. | |
4847 | */ | |
4848 | if (left_gen != right_gen && | |
4849 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) | |
31db9f7c AB |
4850 | sctx->cur_inode_new_gen = 1; |
4851 | } | |
4852 | ||
4853 | if (result == BTRFS_COMPARE_TREE_NEW) { | |
4854 | sctx->cur_inode_gen = left_gen; | |
4855 | sctx->cur_inode_new = 1; | |
4856 | sctx->cur_inode_deleted = 0; | |
4857 | sctx->cur_inode_size = btrfs_inode_size( | |
4858 | sctx->left_path->nodes[0], left_ii); | |
4859 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4860 | sctx->left_path->nodes[0], left_ii); | |
4861 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) | |
1f4692da | 4862 | ret = send_create_inode_if_needed(sctx); |
31db9f7c AB |
4863 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
4864 | sctx->cur_inode_gen = right_gen; | |
4865 | sctx->cur_inode_new = 0; | |
4866 | sctx->cur_inode_deleted = 1; | |
4867 | sctx->cur_inode_size = btrfs_inode_size( | |
4868 | sctx->right_path->nodes[0], right_ii); | |
4869 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4870 | sctx->right_path->nodes[0], right_ii); | |
4871 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
766702ef AB |
4872 | /* |
4873 | * We need to do some special handling in case the inode was | |
4874 | * reported as changed with a changed generation number. This | |
4875 | * means that the original inode was deleted and new inode | |
4876 | * reused the same inum. So we have to treat the old inode as | |
4877 | * deleted and the new one as new. | |
4878 | */ | |
31db9f7c | 4879 | if (sctx->cur_inode_new_gen) { |
766702ef AB |
4880 | /* |
4881 | * First, process the inode as if it was deleted. | |
4882 | */ | |
31db9f7c AB |
4883 | sctx->cur_inode_gen = right_gen; |
4884 | sctx->cur_inode_new = 0; | |
4885 | sctx->cur_inode_deleted = 1; | |
4886 | sctx->cur_inode_size = btrfs_inode_size( | |
4887 | sctx->right_path->nodes[0], right_ii); | |
4888 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4889 | sctx->right_path->nodes[0], right_ii); | |
4890 | ret = process_all_refs(sctx, | |
4891 | BTRFS_COMPARE_TREE_DELETED); | |
4892 | if (ret < 0) | |
4893 | goto out; | |
4894 | ||
766702ef AB |
4895 | /* |
4896 | * Now process the inode as if it was new. | |
4897 | */ | |
31db9f7c AB |
4898 | sctx->cur_inode_gen = left_gen; |
4899 | sctx->cur_inode_new = 1; | |
4900 | sctx->cur_inode_deleted = 0; | |
4901 | sctx->cur_inode_size = btrfs_inode_size( | |
4902 | sctx->left_path->nodes[0], left_ii); | |
4903 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4904 | sctx->left_path->nodes[0], left_ii); | |
1f4692da | 4905 | ret = send_create_inode_if_needed(sctx); |
31db9f7c AB |
4906 | if (ret < 0) |
4907 | goto out; | |
4908 | ||
4909 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); | |
4910 | if (ret < 0) | |
4911 | goto out; | |
e479d9bb AB |
4912 | /* |
4913 | * Advance send_progress now as we did not get into | |
4914 | * process_recorded_refs_if_needed in the new_gen case. | |
4915 | */ | |
4916 | sctx->send_progress = sctx->cur_ino + 1; | |
766702ef AB |
4917 | |
4918 | /* | |
4919 | * Now process all extents and xattrs of the inode as if | |
4920 | * they were all new. | |
4921 | */ | |
31db9f7c AB |
4922 | ret = process_all_extents(sctx); |
4923 | if (ret < 0) | |
4924 | goto out; | |
4925 | ret = process_all_new_xattrs(sctx); | |
4926 | if (ret < 0) | |
4927 | goto out; | |
4928 | } else { | |
4929 | sctx->cur_inode_gen = left_gen; | |
4930 | sctx->cur_inode_new = 0; | |
4931 | sctx->cur_inode_new_gen = 0; | |
4932 | sctx->cur_inode_deleted = 0; | |
4933 | sctx->cur_inode_size = btrfs_inode_size( | |
4934 | sctx->left_path->nodes[0], left_ii); | |
4935 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4936 | sctx->left_path->nodes[0], left_ii); | |
4937 | } | |
4938 | } | |
4939 | ||
4940 | out: | |
4941 | return ret; | |
4942 | } | |
4943 | ||
766702ef AB |
4944 | /* |
4945 | * We have to process new refs before deleted refs, but compare_trees gives us | |
4946 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs | |
4947 | * first and later process them in process_recorded_refs. | |
4948 | * For the cur_inode_new_gen case, we skip recording completely because | |
4949 | * changed_inode did already initiate processing of refs. The reason for this is | |
4950 | * that in this case, compare_tree actually compares the refs of 2 different | |
4951 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all | |
4952 | * refs of the right tree as deleted and all refs of the left tree as new. | |
4953 | */ | |
31db9f7c AB |
4954 | static int changed_ref(struct send_ctx *sctx, |
4955 | enum btrfs_compare_tree_result result) | |
4956 | { | |
4957 | int ret = 0; | |
4958 | ||
4959 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
4960 | ||
4961 | if (!sctx->cur_inode_new_gen && | |
4962 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
4963 | if (result == BTRFS_COMPARE_TREE_NEW) | |
4964 | ret = record_new_ref(sctx); | |
4965 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
4966 | ret = record_deleted_ref(sctx); | |
4967 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
4968 | ret = record_changed_ref(sctx); | |
4969 | } | |
4970 | ||
4971 | return ret; | |
4972 | } | |
4973 | ||
766702ef AB |
4974 | /* |
4975 | * Process new/deleted/changed xattrs. We skip processing in the | |
4976 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
4977 | * of xattrs. The reason is the same as in changed_ref | |
4978 | */ | |
31db9f7c AB |
4979 | static int changed_xattr(struct send_ctx *sctx, |
4980 | enum btrfs_compare_tree_result result) | |
4981 | { | |
4982 | int ret = 0; | |
4983 | ||
4984 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
4985 | ||
4986 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
4987 | if (result == BTRFS_COMPARE_TREE_NEW) | |
4988 | ret = process_new_xattr(sctx); | |
4989 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
4990 | ret = process_deleted_xattr(sctx); | |
4991 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
4992 | ret = process_changed_xattr(sctx); | |
4993 | } | |
4994 | ||
4995 | return ret; | |
4996 | } | |
4997 | ||
766702ef AB |
4998 | /* |
4999 | * Process new/deleted/changed extents. We skip processing in the | |
5000 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
5001 | * of extents. The reason is the same as in changed_ref | |
5002 | */ | |
31db9f7c AB |
5003 | static int changed_extent(struct send_ctx *sctx, |
5004 | enum btrfs_compare_tree_result result) | |
5005 | { | |
5006 | int ret = 0; | |
5007 | ||
5008 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
5009 | ||
5010 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
5011 | if (result != BTRFS_COMPARE_TREE_DELETED) | |
5012 | ret = process_extent(sctx, sctx->left_path, | |
5013 | sctx->cmp_key); | |
5014 | } | |
5015 | ||
5016 | return ret; | |
5017 | } | |
5018 | ||
ba5e8f2e JB |
5019 | static int dir_changed(struct send_ctx *sctx, u64 dir) |
5020 | { | |
5021 | u64 orig_gen, new_gen; | |
5022 | int ret; | |
5023 | ||
5024 | ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, | |
5025 | NULL, NULL); | |
5026 | if (ret) | |
5027 | return ret; | |
5028 | ||
5029 | ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, | |
5030 | NULL, NULL, NULL); | |
5031 | if (ret) | |
5032 | return ret; | |
5033 | ||
5034 | return (orig_gen != new_gen) ? 1 : 0; | |
5035 | } | |
5036 | ||
5037 | static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, | |
5038 | struct btrfs_key *key) | |
5039 | { | |
5040 | struct btrfs_inode_extref *extref; | |
5041 | struct extent_buffer *leaf; | |
5042 | u64 dirid = 0, last_dirid = 0; | |
5043 | unsigned long ptr; | |
5044 | u32 item_size; | |
5045 | u32 cur_offset = 0; | |
5046 | int ref_name_len; | |
5047 | int ret = 0; | |
5048 | ||
5049 | /* Easy case, just check this one dirid */ | |
5050 | if (key->type == BTRFS_INODE_REF_KEY) { | |
5051 | dirid = key->offset; | |
5052 | ||
5053 | ret = dir_changed(sctx, dirid); | |
5054 | goto out; | |
5055 | } | |
5056 | ||
5057 | leaf = path->nodes[0]; | |
5058 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
5059 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
5060 | while (cur_offset < item_size) { | |
5061 | extref = (struct btrfs_inode_extref *)(ptr + | |
5062 | cur_offset); | |
5063 | dirid = btrfs_inode_extref_parent(leaf, extref); | |
5064 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); | |
5065 | cur_offset += ref_name_len + sizeof(*extref); | |
5066 | if (dirid == last_dirid) | |
5067 | continue; | |
5068 | ret = dir_changed(sctx, dirid); | |
5069 | if (ret) | |
5070 | break; | |
5071 | last_dirid = dirid; | |
5072 | } | |
5073 | out: | |
5074 | return ret; | |
5075 | } | |
5076 | ||
766702ef AB |
5077 | /* |
5078 | * Updates compare related fields in sctx and simply forwards to the actual | |
5079 | * changed_xxx functions. | |
5080 | */ | |
31db9f7c AB |
5081 | static int changed_cb(struct btrfs_root *left_root, |
5082 | struct btrfs_root *right_root, | |
5083 | struct btrfs_path *left_path, | |
5084 | struct btrfs_path *right_path, | |
5085 | struct btrfs_key *key, | |
5086 | enum btrfs_compare_tree_result result, | |
5087 | void *ctx) | |
5088 | { | |
5089 | int ret = 0; | |
5090 | struct send_ctx *sctx = ctx; | |
5091 | ||
ba5e8f2e | 5092 | if (result == BTRFS_COMPARE_TREE_SAME) { |
16e7549f JB |
5093 | if (key->type == BTRFS_INODE_REF_KEY || |
5094 | key->type == BTRFS_INODE_EXTREF_KEY) { | |
5095 | ret = compare_refs(sctx, left_path, key); | |
5096 | if (!ret) | |
5097 | return 0; | |
5098 | if (ret < 0) | |
5099 | return ret; | |
5100 | } else if (key->type == BTRFS_EXTENT_DATA_KEY) { | |
5101 | return maybe_send_hole(sctx, left_path, key); | |
5102 | } else { | |
ba5e8f2e | 5103 | return 0; |
16e7549f | 5104 | } |
ba5e8f2e JB |
5105 | result = BTRFS_COMPARE_TREE_CHANGED; |
5106 | ret = 0; | |
5107 | } | |
5108 | ||
31db9f7c AB |
5109 | sctx->left_path = left_path; |
5110 | sctx->right_path = right_path; | |
5111 | sctx->cmp_key = key; | |
5112 | ||
5113 | ret = finish_inode_if_needed(sctx, 0); | |
5114 | if (ret < 0) | |
5115 | goto out; | |
5116 | ||
2981e225 AB |
5117 | /* Ignore non-FS objects */ |
5118 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || | |
5119 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) | |
5120 | goto out; | |
5121 | ||
31db9f7c AB |
5122 | if (key->type == BTRFS_INODE_ITEM_KEY) |
5123 | ret = changed_inode(sctx, result); | |
96b5bd77 JS |
5124 | else if (key->type == BTRFS_INODE_REF_KEY || |
5125 | key->type == BTRFS_INODE_EXTREF_KEY) | |
31db9f7c AB |
5126 | ret = changed_ref(sctx, result); |
5127 | else if (key->type == BTRFS_XATTR_ITEM_KEY) | |
5128 | ret = changed_xattr(sctx, result); | |
5129 | else if (key->type == BTRFS_EXTENT_DATA_KEY) | |
5130 | ret = changed_extent(sctx, result); | |
5131 | ||
5132 | out: | |
5133 | return ret; | |
5134 | } | |
5135 | ||
5136 | static int full_send_tree(struct send_ctx *sctx) | |
5137 | { | |
5138 | int ret; | |
31db9f7c AB |
5139 | struct btrfs_root *send_root = sctx->send_root; |
5140 | struct btrfs_key key; | |
5141 | struct btrfs_key found_key; | |
5142 | struct btrfs_path *path; | |
5143 | struct extent_buffer *eb; | |
5144 | int slot; | |
5145 | u64 start_ctransid; | |
5146 | u64 ctransid; | |
5147 | ||
5148 | path = alloc_path_for_send(); | |
5149 | if (!path) | |
5150 | return -ENOMEM; | |
5151 | ||
5f3ab90a | 5152 | spin_lock(&send_root->root_item_lock); |
31db9f7c | 5153 | start_ctransid = btrfs_root_ctransid(&send_root->root_item); |
5f3ab90a | 5154 | spin_unlock(&send_root->root_item_lock); |
31db9f7c AB |
5155 | |
5156 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; | |
5157 | key.type = BTRFS_INODE_ITEM_KEY; | |
5158 | key.offset = 0; | |
5159 | ||
31db9f7c | 5160 | /* |
766702ef AB |
5161 | * Make sure the tree has not changed after re-joining. We detect this |
5162 | * by comparing start_ctransid and ctransid. They should always match. | |
31db9f7c | 5163 | */ |
5f3ab90a | 5164 | spin_lock(&send_root->root_item_lock); |
31db9f7c | 5165 | ctransid = btrfs_root_ctransid(&send_root->root_item); |
5f3ab90a | 5166 | spin_unlock(&send_root->root_item_lock); |
31db9f7c AB |
5167 | |
5168 | if (ctransid != start_ctransid) { | |
efe120a0 | 5169 | WARN(1, KERN_WARNING "BTRFS: the root that you're trying to " |
31db9f7c AB |
5170 | "send was modified in between. This is " |
5171 | "probably a bug.\n"); | |
5172 | ret = -EIO; | |
5173 | goto out; | |
5174 | } | |
5175 | ||
5176 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); | |
5177 | if (ret < 0) | |
5178 | goto out; | |
5179 | if (ret) | |
5180 | goto out_finish; | |
5181 | ||
5182 | while (1) { | |
31db9f7c AB |
5183 | eb = path->nodes[0]; |
5184 | slot = path->slots[0]; | |
5185 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
5186 | ||
5187 | ret = changed_cb(send_root, NULL, path, NULL, | |
5188 | &found_key, BTRFS_COMPARE_TREE_NEW, sctx); | |
5189 | if (ret < 0) | |
5190 | goto out; | |
5191 | ||
5192 | key.objectid = found_key.objectid; | |
5193 | key.type = found_key.type; | |
5194 | key.offset = found_key.offset + 1; | |
5195 | ||
5196 | ret = btrfs_next_item(send_root, path); | |
5197 | if (ret < 0) | |
5198 | goto out; | |
5199 | if (ret) { | |
5200 | ret = 0; | |
5201 | break; | |
5202 | } | |
5203 | } | |
5204 | ||
5205 | out_finish: | |
5206 | ret = finish_inode_if_needed(sctx, 1); | |
5207 | ||
5208 | out: | |
5209 | btrfs_free_path(path); | |
31db9f7c AB |
5210 | return ret; |
5211 | } | |
5212 | ||
5213 | static int send_subvol(struct send_ctx *sctx) | |
5214 | { | |
5215 | int ret; | |
5216 | ||
c2c71324 SB |
5217 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { |
5218 | ret = send_header(sctx); | |
5219 | if (ret < 0) | |
5220 | goto out; | |
5221 | } | |
31db9f7c AB |
5222 | |
5223 | ret = send_subvol_begin(sctx); | |
5224 | if (ret < 0) | |
5225 | goto out; | |
5226 | ||
5227 | if (sctx->parent_root) { | |
5228 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, | |
5229 | changed_cb, sctx); | |
5230 | if (ret < 0) | |
5231 | goto out; | |
5232 | ret = finish_inode_if_needed(sctx, 1); | |
5233 | if (ret < 0) | |
5234 | goto out; | |
5235 | } else { | |
5236 | ret = full_send_tree(sctx); | |
5237 | if (ret < 0) | |
5238 | goto out; | |
5239 | } | |
5240 | ||
5241 | out: | |
31db9f7c AB |
5242 | free_recorded_refs(sctx); |
5243 | return ret; | |
5244 | } | |
5245 | ||
66ef7d65 DS |
5246 | static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) |
5247 | { | |
5248 | spin_lock(&root->root_item_lock); | |
5249 | root->send_in_progress--; | |
5250 | /* | |
5251 | * Not much left to do, we don't know why it's unbalanced and | |
5252 | * can't blindly reset it to 0. | |
5253 | */ | |
5254 | if (root->send_in_progress < 0) | |
5255 | btrfs_err(root->fs_info, | |
5256 | "send_in_progres unbalanced %d root %llu\n", | |
5257 | root->send_in_progress, root->root_key.objectid); | |
5258 | spin_unlock(&root->root_item_lock); | |
5259 | } | |
5260 | ||
31db9f7c AB |
5261 | long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) |
5262 | { | |
5263 | int ret = 0; | |
5264 | struct btrfs_root *send_root; | |
5265 | struct btrfs_root *clone_root; | |
5266 | struct btrfs_fs_info *fs_info; | |
5267 | struct btrfs_ioctl_send_args *arg = NULL; | |
5268 | struct btrfs_key key; | |
31db9f7c AB |
5269 | struct send_ctx *sctx = NULL; |
5270 | u32 i; | |
5271 | u64 *clone_sources_tmp = NULL; | |
2c686537 | 5272 | int clone_sources_to_rollback = 0; |
896c14f9 | 5273 | int sort_clone_roots = 0; |
18f687d5 | 5274 | int index; |
31db9f7c AB |
5275 | |
5276 | if (!capable(CAP_SYS_ADMIN)) | |
5277 | return -EPERM; | |
5278 | ||
496ad9aa | 5279 | send_root = BTRFS_I(file_inode(mnt_file))->root; |
31db9f7c AB |
5280 | fs_info = send_root->fs_info; |
5281 | ||
2c686537 DS |
5282 | /* |
5283 | * The subvolume must remain read-only during send, protect against | |
5284 | * making it RW. | |
5285 | */ | |
5286 | spin_lock(&send_root->root_item_lock); | |
5287 | send_root->send_in_progress++; | |
5288 | spin_unlock(&send_root->root_item_lock); | |
5289 | ||
139f807a JB |
5290 | /* |
5291 | * This is done when we lookup the root, it should already be complete | |
5292 | * by the time we get here. | |
5293 | */ | |
5294 | WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE); | |
5295 | ||
2c686537 DS |
5296 | /* |
5297 | * Userspace tools do the checks and warn the user if it's | |
5298 | * not RO. | |
5299 | */ | |
5300 | if (!btrfs_root_readonly(send_root)) { | |
5301 | ret = -EPERM; | |
5302 | goto out; | |
5303 | } | |
5304 | ||
31db9f7c AB |
5305 | arg = memdup_user(arg_, sizeof(*arg)); |
5306 | if (IS_ERR(arg)) { | |
5307 | ret = PTR_ERR(arg); | |
5308 | arg = NULL; | |
5309 | goto out; | |
5310 | } | |
5311 | ||
5312 | if (!access_ok(VERIFY_READ, arg->clone_sources, | |
700ff4f0 DC |
5313 | sizeof(*arg->clone_sources) * |
5314 | arg->clone_sources_count)) { | |
31db9f7c AB |
5315 | ret = -EFAULT; |
5316 | goto out; | |
5317 | } | |
5318 | ||
c2c71324 | 5319 | if (arg->flags & ~BTRFS_SEND_FLAG_MASK) { |
cb95e7bf MF |
5320 | ret = -EINVAL; |
5321 | goto out; | |
5322 | } | |
5323 | ||
31db9f7c AB |
5324 | sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS); |
5325 | if (!sctx) { | |
5326 | ret = -ENOMEM; | |
5327 | goto out; | |
5328 | } | |
5329 | ||
5330 | INIT_LIST_HEAD(&sctx->new_refs); | |
5331 | INIT_LIST_HEAD(&sctx->deleted_refs); | |
5332 | INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS); | |
5333 | INIT_LIST_HEAD(&sctx->name_cache_list); | |
5334 | ||
cb95e7bf MF |
5335 | sctx->flags = arg->flags; |
5336 | ||
31db9f7c | 5337 | sctx->send_filp = fget(arg->send_fd); |
ecc7ada7 TI |
5338 | if (!sctx->send_filp) { |
5339 | ret = -EBADF; | |
31db9f7c AB |
5340 | goto out; |
5341 | } | |
5342 | ||
31db9f7c AB |
5343 | sctx->send_root = send_root; |
5344 | sctx->clone_roots_cnt = arg->clone_sources_count; | |
5345 | ||
5346 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; | |
5347 | sctx->send_buf = vmalloc(sctx->send_max_size); | |
5348 | if (!sctx->send_buf) { | |
5349 | ret = -ENOMEM; | |
5350 | goto out; | |
5351 | } | |
5352 | ||
5353 | sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE); | |
5354 | if (!sctx->read_buf) { | |
5355 | ret = -ENOMEM; | |
5356 | goto out; | |
5357 | } | |
5358 | ||
9f03740a FDBM |
5359 | sctx->pending_dir_moves = RB_ROOT; |
5360 | sctx->waiting_dir_moves = RB_ROOT; | |
5361 | ||
31db9f7c AB |
5362 | sctx->clone_roots = vzalloc(sizeof(struct clone_root) * |
5363 | (arg->clone_sources_count + 1)); | |
5364 | if (!sctx->clone_roots) { | |
5365 | ret = -ENOMEM; | |
5366 | goto out; | |
5367 | } | |
5368 | ||
5369 | if (arg->clone_sources_count) { | |
5370 | clone_sources_tmp = vmalloc(arg->clone_sources_count * | |
5371 | sizeof(*arg->clone_sources)); | |
5372 | if (!clone_sources_tmp) { | |
5373 | ret = -ENOMEM; | |
5374 | goto out; | |
5375 | } | |
5376 | ||
5377 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, | |
5378 | arg->clone_sources_count * | |
5379 | sizeof(*arg->clone_sources)); | |
5380 | if (ret) { | |
5381 | ret = -EFAULT; | |
5382 | goto out; | |
5383 | } | |
5384 | ||
5385 | for (i = 0; i < arg->clone_sources_count; i++) { | |
5386 | key.objectid = clone_sources_tmp[i]; | |
5387 | key.type = BTRFS_ROOT_ITEM_KEY; | |
5388 | key.offset = (u64)-1; | |
18f687d5 WS |
5389 | |
5390 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
5391 | ||
31db9f7c | 5392 | clone_root = btrfs_read_fs_root_no_name(fs_info, &key); |
31db9f7c | 5393 | if (IS_ERR(clone_root)) { |
18f687d5 | 5394 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
31db9f7c AB |
5395 | ret = PTR_ERR(clone_root); |
5396 | goto out; | |
5397 | } | |
2c686537 DS |
5398 | clone_sources_to_rollback = i + 1; |
5399 | spin_lock(&clone_root->root_item_lock); | |
5400 | clone_root->send_in_progress++; | |
5401 | if (!btrfs_root_readonly(clone_root)) { | |
5402 | spin_unlock(&clone_root->root_item_lock); | |
18f687d5 | 5403 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
2c686537 DS |
5404 | ret = -EPERM; |
5405 | goto out; | |
5406 | } | |
5407 | spin_unlock(&clone_root->root_item_lock); | |
18f687d5 WS |
5408 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
5409 | ||
31db9f7c AB |
5410 | sctx->clone_roots[i].root = clone_root; |
5411 | } | |
5412 | vfree(clone_sources_tmp); | |
5413 | clone_sources_tmp = NULL; | |
5414 | } | |
5415 | ||
5416 | if (arg->parent_root) { | |
5417 | key.objectid = arg->parent_root; | |
5418 | key.type = BTRFS_ROOT_ITEM_KEY; | |
5419 | key.offset = (u64)-1; | |
18f687d5 WS |
5420 | |
5421 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
5422 | ||
31db9f7c | 5423 | sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key); |
b1b19596 | 5424 | if (IS_ERR(sctx->parent_root)) { |
18f687d5 | 5425 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
b1b19596 | 5426 | ret = PTR_ERR(sctx->parent_root); |
31db9f7c AB |
5427 | goto out; |
5428 | } | |
18f687d5 | 5429 | |
2c686537 DS |
5430 | spin_lock(&sctx->parent_root->root_item_lock); |
5431 | sctx->parent_root->send_in_progress++; | |
5432 | if (!btrfs_root_readonly(sctx->parent_root)) { | |
5433 | spin_unlock(&sctx->parent_root->root_item_lock); | |
18f687d5 | 5434 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
2c686537 DS |
5435 | ret = -EPERM; |
5436 | goto out; | |
5437 | } | |
5438 | spin_unlock(&sctx->parent_root->root_item_lock); | |
18f687d5 WS |
5439 | |
5440 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
31db9f7c AB |
5441 | } |
5442 | ||
5443 | /* | |
5444 | * Clones from send_root are allowed, but only if the clone source | |
5445 | * is behind the current send position. This is checked while searching | |
5446 | * for possible clone sources. | |
5447 | */ | |
5448 | sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root; | |
5449 | ||
5450 | /* We do a bsearch later */ | |
5451 | sort(sctx->clone_roots, sctx->clone_roots_cnt, | |
5452 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, | |
5453 | NULL); | |
896c14f9 | 5454 | sort_clone_roots = 1; |
31db9f7c AB |
5455 | |
5456 | ret = send_subvol(sctx); | |
5457 | if (ret < 0) | |
5458 | goto out; | |
5459 | ||
c2c71324 SB |
5460 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { |
5461 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); | |
5462 | if (ret < 0) | |
5463 | goto out; | |
5464 | ret = send_cmd(sctx); | |
5465 | if (ret < 0) | |
5466 | goto out; | |
5467 | } | |
31db9f7c AB |
5468 | |
5469 | out: | |
9f03740a FDBM |
5470 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); |
5471 | while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { | |
5472 | struct rb_node *n; | |
5473 | struct pending_dir_move *pm; | |
5474 | ||
5475 | n = rb_first(&sctx->pending_dir_moves); | |
5476 | pm = rb_entry(n, struct pending_dir_move, node); | |
5477 | while (!list_empty(&pm->list)) { | |
5478 | struct pending_dir_move *pm2; | |
5479 | ||
5480 | pm2 = list_first_entry(&pm->list, | |
5481 | struct pending_dir_move, list); | |
5482 | free_pending_move(sctx, pm2); | |
5483 | } | |
5484 | free_pending_move(sctx, pm); | |
5485 | } | |
5486 | ||
5487 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); | |
5488 | while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { | |
5489 | struct rb_node *n; | |
5490 | struct waiting_dir_move *dm; | |
5491 | ||
5492 | n = rb_first(&sctx->waiting_dir_moves); | |
5493 | dm = rb_entry(n, struct waiting_dir_move, node); | |
5494 | rb_erase(&dm->node, &sctx->waiting_dir_moves); | |
5495 | kfree(dm); | |
5496 | } | |
5497 | ||
896c14f9 WS |
5498 | if (sort_clone_roots) { |
5499 | for (i = 0; i < sctx->clone_roots_cnt; i++) | |
5500 | btrfs_root_dec_send_in_progress( | |
5501 | sctx->clone_roots[i].root); | |
5502 | } else { | |
5503 | for (i = 0; sctx && i < clone_sources_to_rollback; i++) | |
5504 | btrfs_root_dec_send_in_progress( | |
5505 | sctx->clone_roots[i].root); | |
5506 | ||
5507 | btrfs_root_dec_send_in_progress(send_root); | |
5508 | } | |
66ef7d65 DS |
5509 | if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) |
5510 | btrfs_root_dec_send_in_progress(sctx->parent_root); | |
2c686537 | 5511 | |
31db9f7c AB |
5512 | kfree(arg); |
5513 | vfree(clone_sources_tmp); | |
5514 | ||
5515 | if (sctx) { | |
5516 | if (sctx->send_filp) | |
5517 | fput(sctx->send_filp); | |
5518 | ||
5519 | vfree(sctx->clone_roots); | |
5520 | vfree(sctx->send_buf); | |
5521 | vfree(sctx->read_buf); | |
5522 | ||
5523 | name_cache_free(sctx); | |
5524 | ||
5525 | kfree(sctx); | |
5526 | } | |
5527 | ||
5528 | return ret; | |
5529 | } |