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