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