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