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