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