]>
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 | { | |
995e01b7 | 1038 | u64 root = (u64)(uintptr_t)key; |
31db9f7c AB |
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 */ | |
995e01b7 | 1072 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
31db9f7c AB |
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; | |
74dd17fb | 1152 | u64 disk_byte; |
31db9f7c AB |
1153 | u64 num_bytes; |
1154 | u64 extent_item_pos; | |
69917e43 | 1155 | u64 flags = 0; |
31db9f7c AB |
1156 | struct btrfs_file_extent_item *fi; |
1157 | struct extent_buffer *eb = path->nodes[0]; | |
35075bb0 | 1158 | struct backref_ctx *backref_ctx = NULL; |
31db9f7c AB |
1159 | struct clone_root *cur_clone_root; |
1160 | struct btrfs_key found_key; | |
1161 | struct btrfs_path *tmp_path; | |
74dd17fb | 1162 | int compressed; |
31db9f7c AB |
1163 | u32 i; |
1164 | ||
1165 | tmp_path = alloc_path_for_send(); | |
1166 | if (!tmp_path) | |
1167 | return -ENOMEM; | |
1168 | ||
35075bb0 AB |
1169 | backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS); |
1170 | if (!backref_ctx) { | |
1171 | ret = -ENOMEM; | |
1172 | goto out; | |
1173 | } | |
1174 | ||
31db9f7c AB |
1175 | if (data_offset >= ino_size) { |
1176 | /* | |
1177 | * There may be extents that lie behind the file's size. | |
1178 | * I at least had this in combination with snapshotting while | |
1179 | * writing large files. | |
1180 | */ | |
1181 | ret = 0; | |
1182 | goto out; | |
1183 | } | |
1184 | ||
1185 | fi = btrfs_item_ptr(eb, path->slots[0], | |
1186 | struct btrfs_file_extent_item); | |
1187 | extent_type = btrfs_file_extent_type(eb, fi); | |
1188 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1189 | ret = -ENOENT; | |
1190 | goto out; | |
1191 | } | |
74dd17fb | 1192 | compressed = btrfs_file_extent_compression(eb, fi); |
31db9f7c AB |
1193 | |
1194 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); | |
74dd17fb CM |
1195 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
1196 | if (disk_byte == 0) { | |
31db9f7c AB |
1197 | ret = -ENOENT; |
1198 | goto out; | |
1199 | } | |
74dd17fb | 1200 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
31db9f7c | 1201 | |
69917e43 LB |
1202 | ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path, |
1203 | &found_key, &flags); | |
31db9f7c AB |
1204 | btrfs_release_path(tmp_path); |
1205 | ||
1206 | if (ret < 0) | |
1207 | goto out; | |
69917e43 | 1208 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
31db9f7c AB |
1209 | ret = -EIO; |
1210 | goto out; | |
1211 | } | |
1212 | ||
1213 | /* | |
1214 | * Setup the clone roots. | |
1215 | */ | |
1216 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1217 | cur_clone_root = sctx->clone_roots + i; | |
1218 | cur_clone_root->ino = (u64)-1; | |
1219 | cur_clone_root->offset = 0; | |
1220 | cur_clone_root->found_refs = 0; | |
1221 | } | |
1222 | ||
35075bb0 AB |
1223 | backref_ctx->sctx = sctx; |
1224 | backref_ctx->found = 0; | |
1225 | backref_ctx->cur_objectid = ino; | |
1226 | backref_ctx->cur_offset = data_offset; | |
1227 | backref_ctx->found_itself = 0; | |
1228 | backref_ctx->extent_len = num_bytes; | |
31db9f7c AB |
1229 | |
1230 | /* | |
1231 | * The last extent of a file may be too large due to page alignment. | |
1232 | * We need to adjust extent_len in this case so that the checks in | |
1233 | * __iterate_backrefs work. | |
1234 | */ | |
1235 | if (data_offset + num_bytes >= ino_size) | |
35075bb0 | 1236 | backref_ctx->extent_len = ino_size - data_offset; |
31db9f7c AB |
1237 | |
1238 | /* | |
1239 | * Now collect all backrefs. | |
1240 | */ | |
74dd17fb CM |
1241 | if (compressed == BTRFS_COMPRESS_NONE) |
1242 | extent_item_pos = logical - found_key.objectid; | |
1243 | else | |
1244 | extent_item_pos = 0; | |
1245 | ||
31db9f7c AB |
1246 | extent_item_pos = logical - found_key.objectid; |
1247 | ret = iterate_extent_inodes(sctx->send_root->fs_info, | |
1248 | found_key.objectid, extent_item_pos, 1, | |
35075bb0 | 1249 | __iterate_backrefs, backref_ctx); |
74dd17fb | 1250 | |
31db9f7c AB |
1251 | if (ret < 0) |
1252 | goto out; | |
1253 | ||
35075bb0 | 1254 | if (!backref_ctx->found_itself) { |
31db9f7c AB |
1255 | /* found a bug in backref code? */ |
1256 | ret = -EIO; | |
1257 | printk(KERN_ERR "btrfs: ERROR did not find backref in " | |
1258 | "send_root. inode=%llu, offset=%llu, " | |
74dd17fb CM |
1259 | "disk_byte=%llu found extent=%llu\n", |
1260 | ino, data_offset, disk_byte, found_key.objectid); | |
31db9f7c AB |
1261 | goto out; |
1262 | } | |
1263 | ||
1264 | verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, " | |
1265 | "ino=%llu, " | |
1266 | "num_bytes=%llu, logical=%llu\n", | |
1267 | data_offset, ino, num_bytes, logical); | |
1268 | ||
35075bb0 | 1269 | if (!backref_ctx->found) |
31db9f7c AB |
1270 | verbose_printk("btrfs: no clones found\n"); |
1271 | ||
1272 | cur_clone_root = NULL; | |
1273 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1274 | if (sctx->clone_roots[i].found_refs) { | |
1275 | if (!cur_clone_root) | |
1276 | cur_clone_root = sctx->clone_roots + i; | |
1277 | else if (sctx->clone_roots[i].root == sctx->send_root) | |
1278 | /* prefer clones from send_root over others */ | |
1279 | cur_clone_root = sctx->clone_roots + i; | |
31db9f7c AB |
1280 | } |
1281 | ||
1282 | } | |
1283 | ||
1284 | if (cur_clone_root) { | |
1285 | *found = cur_clone_root; | |
1286 | ret = 0; | |
1287 | } else { | |
1288 | ret = -ENOENT; | |
1289 | } | |
1290 | ||
1291 | out: | |
1292 | btrfs_free_path(tmp_path); | |
35075bb0 | 1293 | kfree(backref_ctx); |
31db9f7c AB |
1294 | return ret; |
1295 | } | |
1296 | ||
1297 | static int read_symlink(struct send_ctx *sctx, | |
1298 | struct btrfs_root *root, | |
1299 | u64 ino, | |
1300 | struct fs_path *dest) | |
1301 | { | |
1302 | int ret; | |
1303 | struct btrfs_path *path; | |
1304 | struct btrfs_key key; | |
1305 | struct btrfs_file_extent_item *ei; | |
1306 | u8 type; | |
1307 | u8 compression; | |
1308 | unsigned long off; | |
1309 | int len; | |
1310 | ||
1311 | path = alloc_path_for_send(); | |
1312 | if (!path) | |
1313 | return -ENOMEM; | |
1314 | ||
1315 | key.objectid = ino; | |
1316 | key.type = BTRFS_EXTENT_DATA_KEY; | |
1317 | key.offset = 0; | |
1318 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1319 | if (ret < 0) | |
1320 | goto out; | |
1321 | BUG_ON(ret); | |
1322 | ||
1323 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1324 | struct btrfs_file_extent_item); | |
1325 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
1326 | compression = btrfs_file_extent_compression(path->nodes[0], ei); | |
1327 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); | |
1328 | BUG_ON(compression); | |
1329 | ||
1330 | off = btrfs_file_extent_inline_start(ei); | |
1331 | len = btrfs_file_extent_inline_len(path->nodes[0], ei); | |
1332 | ||
1333 | ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); | |
31db9f7c AB |
1334 | |
1335 | out: | |
1336 | btrfs_free_path(path); | |
1337 | return ret; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Helper function to generate a file name that is unique in the root of | |
1342 | * send_root and parent_root. This is used to generate names for orphan inodes. | |
1343 | */ | |
1344 | static int gen_unique_name(struct send_ctx *sctx, | |
1345 | u64 ino, u64 gen, | |
1346 | struct fs_path *dest) | |
1347 | { | |
1348 | int ret = 0; | |
1349 | struct btrfs_path *path; | |
1350 | struct btrfs_dir_item *di; | |
1351 | char tmp[64]; | |
1352 | int len; | |
1353 | u64 idx = 0; | |
1354 | ||
1355 | path = alloc_path_for_send(); | |
1356 | if (!path) | |
1357 | return -ENOMEM; | |
1358 | ||
1359 | while (1) { | |
1360 | len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu", | |
1361 | ino, gen, idx); | |
1362 | if (len >= sizeof(tmp)) { | |
1363 | /* should really not happen */ | |
1364 | ret = -EOVERFLOW; | |
1365 | goto out; | |
1366 | } | |
1367 | ||
1368 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, | |
1369 | path, BTRFS_FIRST_FREE_OBJECTID, | |
1370 | tmp, strlen(tmp), 0); | |
1371 | btrfs_release_path(path); | |
1372 | if (IS_ERR(di)) { | |
1373 | ret = PTR_ERR(di); | |
1374 | goto out; | |
1375 | } | |
1376 | if (di) { | |
1377 | /* not unique, try again */ | |
1378 | idx++; | |
1379 | continue; | |
1380 | } | |
1381 | ||
1382 | if (!sctx->parent_root) { | |
1383 | /* unique */ | |
1384 | ret = 0; | |
1385 | break; | |
1386 | } | |
1387 | ||
1388 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, | |
1389 | path, BTRFS_FIRST_FREE_OBJECTID, | |
1390 | tmp, strlen(tmp), 0); | |
1391 | btrfs_release_path(path); | |
1392 | if (IS_ERR(di)) { | |
1393 | ret = PTR_ERR(di); | |
1394 | goto out; | |
1395 | } | |
1396 | if (di) { | |
1397 | /* not unique, try again */ | |
1398 | idx++; | |
1399 | continue; | |
1400 | } | |
1401 | /* unique */ | |
1402 | break; | |
1403 | } | |
1404 | ||
1405 | ret = fs_path_add(dest, tmp, strlen(tmp)); | |
1406 | ||
1407 | out: | |
1408 | btrfs_free_path(path); | |
1409 | return ret; | |
1410 | } | |
1411 | ||
1412 | enum inode_state { | |
1413 | inode_state_no_change, | |
1414 | inode_state_will_create, | |
1415 | inode_state_did_create, | |
1416 | inode_state_will_delete, | |
1417 | inode_state_did_delete, | |
1418 | }; | |
1419 | ||
1420 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) | |
1421 | { | |
1422 | int ret; | |
1423 | int left_ret; | |
1424 | int right_ret; | |
1425 | u64 left_gen; | |
1426 | u64 right_gen; | |
1427 | ||
1428 | ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, | |
85a7b33b | 1429 | NULL, NULL); |
31db9f7c AB |
1430 | if (ret < 0 && ret != -ENOENT) |
1431 | goto out; | |
1432 | left_ret = ret; | |
1433 | ||
1434 | if (!sctx->parent_root) { | |
1435 | right_ret = -ENOENT; | |
1436 | } else { | |
1437 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, | |
85a7b33b | 1438 | NULL, NULL, NULL, NULL); |
31db9f7c AB |
1439 | if (ret < 0 && ret != -ENOENT) |
1440 | goto out; | |
1441 | right_ret = ret; | |
1442 | } | |
1443 | ||
1444 | if (!left_ret && !right_ret) { | |
e938c8ad | 1445 | if (left_gen == gen && right_gen == gen) { |
31db9f7c | 1446 | ret = inode_state_no_change; |
e938c8ad | 1447 | } else if (left_gen == gen) { |
31db9f7c AB |
1448 | if (ino < sctx->send_progress) |
1449 | ret = inode_state_did_create; | |
1450 | else | |
1451 | ret = inode_state_will_create; | |
1452 | } else if (right_gen == gen) { | |
1453 | if (ino < sctx->send_progress) | |
1454 | ret = inode_state_did_delete; | |
1455 | else | |
1456 | ret = inode_state_will_delete; | |
1457 | } else { | |
1458 | ret = -ENOENT; | |
1459 | } | |
1460 | } else if (!left_ret) { | |
1461 | if (left_gen == gen) { | |
1462 | if (ino < sctx->send_progress) | |
1463 | ret = inode_state_did_create; | |
1464 | else | |
1465 | ret = inode_state_will_create; | |
1466 | } else { | |
1467 | ret = -ENOENT; | |
1468 | } | |
1469 | } else if (!right_ret) { | |
1470 | if (right_gen == gen) { | |
1471 | if (ino < sctx->send_progress) | |
1472 | ret = inode_state_did_delete; | |
1473 | else | |
1474 | ret = inode_state_will_delete; | |
1475 | } else { | |
1476 | ret = -ENOENT; | |
1477 | } | |
1478 | } else { | |
1479 | ret = -ENOENT; | |
1480 | } | |
1481 | ||
1482 | out: | |
1483 | return ret; | |
1484 | } | |
1485 | ||
1486 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) | |
1487 | { | |
1488 | int ret; | |
1489 | ||
1490 | ret = get_cur_inode_state(sctx, ino, gen); | |
1491 | if (ret < 0) | |
1492 | goto out; | |
1493 | ||
1494 | if (ret == inode_state_no_change || | |
1495 | ret == inode_state_did_create || | |
1496 | ret == inode_state_will_delete) | |
1497 | ret = 1; | |
1498 | else | |
1499 | ret = 0; | |
1500 | ||
1501 | out: | |
1502 | return ret; | |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * Helper function to lookup a dir item in a dir. | |
1507 | */ | |
1508 | static int lookup_dir_item_inode(struct btrfs_root *root, | |
1509 | u64 dir, const char *name, int name_len, | |
1510 | u64 *found_inode, | |
1511 | u8 *found_type) | |
1512 | { | |
1513 | int ret = 0; | |
1514 | struct btrfs_dir_item *di; | |
1515 | struct btrfs_key key; | |
1516 | struct btrfs_path *path; | |
1517 | ||
1518 | path = alloc_path_for_send(); | |
1519 | if (!path) | |
1520 | return -ENOMEM; | |
1521 | ||
1522 | di = btrfs_lookup_dir_item(NULL, root, path, | |
1523 | dir, name, name_len, 0); | |
1524 | if (!di) { | |
1525 | ret = -ENOENT; | |
1526 | goto out; | |
1527 | } | |
1528 | if (IS_ERR(di)) { | |
1529 | ret = PTR_ERR(di); | |
1530 | goto out; | |
1531 | } | |
1532 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
1533 | *found_inode = key.objectid; | |
1534 | *found_type = btrfs_dir_type(path->nodes[0], di); | |
1535 | ||
1536 | out: | |
1537 | btrfs_free_path(path); | |
1538 | return ret; | |
1539 | } | |
1540 | ||
766702ef AB |
1541 | /* |
1542 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, | |
1543 | * generation of the parent dir and the name of the dir entry. | |
1544 | */ | |
31db9f7c AB |
1545 | static int get_first_ref(struct send_ctx *sctx, |
1546 | struct btrfs_root *root, u64 ino, | |
1547 | u64 *dir, u64 *dir_gen, struct fs_path *name) | |
1548 | { | |
1549 | int ret; | |
1550 | struct btrfs_key key; | |
1551 | struct btrfs_key found_key; | |
1552 | struct btrfs_path *path; | |
1553 | struct btrfs_inode_ref *iref; | |
1554 | int len; | |
1555 | ||
1556 | path = alloc_path_for_send(); | |
1557 | if (!path) | |
1558 | return -ENOMEM; | |
1559 | ||
1560 | key.objectid = ino; | |
1561 | key.type = BTRFS_INODE_REF_KEY; | |
1562 | key.offset = 0; | |
1563 | ||
1564 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
1565 | if (ret < 0) | |
1566 | goto out; | |
1567 | if (!ret) | |
1568 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1569 | path->slots[0]); | |
1570 | if (ret || found_key.objectid != key.objectid || | |
1571 | found_key.type != key.type) { | |
1572 | ret = -ENOENT; | |
1573 | goto out; | |
1574 | } | |
1575 | ||
1576 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1577 | struct btrfs_inode_ref); | |
1578 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); | |
1579 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], | |
1580 | (unsigned long)(iref + 1), len); | |
1581 | if (ret < 0) | |
1582 | goto out; | |
1583 | btrfs_release_path(path); | |
1584 | ||
1585 | ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL, | |
85a7b33b | 1586 | NULL, NULL); |
31db9f7c AB |
1587 | if (ret < 0) |
1588 | goto out; | |
1589 | ||
1590 | *dir = found_key.offset; | |
1591 | ||
1592 | out: | |
1593 | btrfs_free_path(path); | |
1594 | return ret; | |
1595 | } | |
1596 | ||
1597 | static int is_first_ref(struct send_ctx *sctx, | |
1598 | struct btrfs_root *root, | |
1599 | u64 ino, u64 dir, | |
1600 | const char *name, int name_len) | |
1601 | { | |
1602 | int ret; | |
1603 | struct fs_path *tmp_name; | |
1604 | u64 tmp_dir; | |
1605 | u64 tmp_dir_gen; | |
1606 | ||
1607 | tmp_name = fs_path_alloc(sctx); | |
1608 | if (!tmp_name) | |
1609 | return -ENOMEM; | |
1610 | ||
1611 | ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name); | |
1612 | if (ret < 0) | |
1613 | goto out; | |
1614 | ||
b9291aff | 1615 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
31db9f7c AB |
1616 | ret = 0; |
1617 | goto out; | |
1618 | } | |
1619 | ||
e938c8ad | 1620 | ret = !memcmp(tmp_name->start, name, name_len); |
31db9f7c AB |
1621 | |
1622 | out: | |
1623 | fs_path_free(sctx, tmp_name); | |
1624 | return ret; | |
1625 | } | |
1626 | ||
766702ef AB |
1627 | /* |
1628 | * Used by process_recorded_refs to determine if a new ref would overwrite an | |
1629 | * already existing ref. In case it detects an overwrite, it returns the | |
1630 | * inode/gen in who_ino/who_gen. | |
1631 | * When an overwrite is detected, process_recorded_refs does proper orphanizing | |
1632 | * to make sure later references to the overwritten inode are possible. | |
1633 | * Orphanizing is however only required for the first ref of an inode. | |
1634 | * process_recorded_refs does an additional is_first_ref check to see if | |
1635 | * orphanizing is really required. | |
1636 | */ | |
31db9f7c AB |
1637 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
1638 | const char *name, int name_len, | |
1639 | u64 *who_ino, u64 *who_gen) | |
1640 | { | |
1641 | int ret = 0; | |
1642 | u64 other_inode = 0; | |
1643 | u8 other_type = 0; | |
1644 | ||
1645 | if (!sctx->parent_root) | |
1646 | goto out; | |
1647 | ||
1648 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1649 | if (ret <= 0) | |
1650 | goto out; | |
1651 | ||
1652 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, | |
1653 | &other_inode, &other_type); | |
1654 | if (ret < 0 && ret != -ENOENT) | |
1655 | goto out; | |
1656 | if (ret) { | |
1657 | ret = 0; | |
1658 | goto out; | |
1659 | } | |
1660 | ||
766702ef AB |
1661 | /* |
1662 | * Check if the overwritten ref was already processed. If yes, the ref | |
1663 | * was already unlinked/moved, so we can safely assume that we will not | |
1664 | * overwrite anything at this point in time. | |
1665 | */ | |
31db9f7c AB |
1666 | if (other_inode > sctx->send_progress) { |
1667 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, | |
85a7b33b | 1668 | who_gen, NULL, NULL, NULL, NULL); |
31db9f7c AB |
1669 | if (ret < 0) |
1670 | goto out; | |
1671 | ||
1672 | ret = 1; | |
1673 | *who_ino = other_inode; | |
1674 | } else { | |
1675 | ret = 0; | |
1676 | } | |
1677 | ||
1678 | out: | |
1679 | return ret; | |
1680 | } | |
1681 | ||
766702ef AB |
1682 | /* |
1683 | * Checks if the ref was overwritten by an already processed inode. This is | |
1684 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and | |
1685 | * thus the orphan name needs be used. | |
1686 | * process_recorded_refs also uses it to avoid unlinking of refs that were | |
1687 | * overwritten. | |
1688 | */ | |
31db9f7c AB |
1689 | static int did_overwrite_ref(struct send_ctx *sctx, |
1690 | u64 dir, u64 dir_gen, | |
1691 | u64 ino, u64 ino_gen, | |
1692 | const char *name, int name_len) | |
1693 | { | |
1694 | int ret = 0; | |
1695 | u64 gen; | |
1696 | u64 ow_inode; | |
1697 | u8 other_type; | |
1698 | ||
1699 | if (!sctx->parent_root) | |
1700 | goto out; | |
1701 | ||
1702 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1703 | if (ret <= 0) | |
1704 | goto out; | |
1705 | ||
1706 | /* check if the ref was overwritten by another ref */ | |
1707 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, | |
1708 | &ow_inode, &other_type); | |
1709 | if (ret < 0 && ret != -ENOENT) | |
1710 | goto out; | |
1711 | if (ret) { | |
1712 | /* was never and will never be overwritten */ | |
1713 | ret = 0; | |
1714 | goto out; | |
1715 | } | |
1716 | ||
1717 | ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, | |
85a7b33b | 1718 | NULL, NULL); |
31db9f7c AB |
1719 | if (ret < 0) |
1720 | goto out; | |
1721 | ||
1722 | if (ow_inode == ino && gen == ino_gen) { | |
1723 | ret = 0; | |
1724 | goto out; | |
1725 | } | |
1726 | ||
1727 | /* we know that it is or will be overwritten. check this now */ | |
1728 | if (ow_inode < sctx->send_progress) | |
1729 | ret = 1; | |
1730 | else | |
1731 | ret = 0; | |
1732 | ||
1733 | out: | |
1734 | return ret; | |
1735 | } | |
1736 | ||
766702ef AB |
1737 | /* |
1738 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode | |
1739 | * that got overwritten. This is used by process_recorded_refs to determine | |
1740 | * if it has to use the path as returned by get_cur_path or the orphan name. | |
1741 | */ | |
31db9f7c AB |
1742 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
1743 | { | |
1744 | int ret = 0; | |
1745 | struct fs_path *name = NULL; | |
1746 | u64 dir; | |
1747 | u64 dir_gen; | |
1748 | ||
1749 | if (!sctx->parent_root) | |
1750 | goto out; | |
1751 | ||
1752 | name = fs_path_alloc(sctx); | |
1753 | if (!name) | |
1754 | return -ENOMEM; | |
1755 | ||
1756 | ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name); | |
1757 | if (ret < 0) | |
1758 | goto out; | |
1759 | ||
1760 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, | |
1761 | name->start, fs_path_len(name)); | |
31db9f7c AB |
1762 | |
1763 | out: | |
1764 | fs_path_free(sctx, name); | |
1765 | return ret; | |
1766 | } | |
1767 | ||
766702ef AB |
1768 | /* |
1769 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, | |
1770 | * so we need to do some special handling in case we have clashes. This function | |
1771 | * takes care of this with the help of name_cache_entry::radix_list. | |
5dc67d0b | 1772 | * In case of error, nce is kfreed. |
766702ef | 1773 | */ |
31db9f7c AB |
1774 | static int name_cache_insert(struct send_ctx *sctx, |
1775 | struct name_cache_entry *nce) | |
1776 | { | |
1777 | int ret = 0; | |
7e0926fe AB |
1778 | struct list_head *nce_head; |
1779 | ||
1780 | nce_head = radix_tree_lookup(&sctx->name_cache, | |
1781 | (unsigned long)nce->ino); | |
1782 | if (!nce_head) { | |
1783 | nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); | |
1784 | if (!nce_head) | |
31db9f7c | 1785 | return -ENOMEM; |
7e0926fe | 1786 | INIT_LIST_HEAD(nce_head); |
31db9f7c | 1787 | |
7e0926fe | 1788 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
5dc67d0b AB |
1789 | if (ret < 0) { |
1790 | kfree(nce_head); | |
1791 | kfree(nce); | |
31db9f7c | 1792 | return ret; |
5dc67d0b | 1793 | } |
31db9f7c | 1794 | } |
7e0926fe | 1795 | list_add_tail(&nce->radix_list, nce_head); |
31db9f7c AB |
1796 | list_add_tail(&nce->list, &sctx->name_cache_list); |
1797 | sctx->name_cache_size++; | |
1798 | ||
1799 | return ret; | |
1800 | } | |
1801 | ||
1802 | static void name_cache_delete(struct send_ctx *sctx, | |
1803 | struct name_cache_entry *nce) | |
1804 | { | |
7e0926fe | 1805 | struct list_head *nce_head; |
31db9f7c | 1806 | |
7e0926fe AB |
1807 | nce_head = radix_tree_lookup(&sctx->name_cache, |
1808 | (unsigned long)nce->ino); | |
1809 | BUG_ON(!nce_head); | |
31db9f7c | 1810 | |
7e0926fe | 1811 | list_del(&nce->radix_list); |
31db9f7c | 1812 | list_del(&nce->list); |
31db9f7c | 1813 | sctx->name_cache_size--; |
7e0926fe AB |
1814 | |
1815 | if (list_empty(nce_head)) { | |
1816 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); | |
1817 | kfree(nce_head); | |
1818 | } | |
31db9f7c AB |
1819 | } |
1820 | ||
1821 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, | |
1822 | u64 ino, u64 gen) | |
1823 | { | |
7e0926fe AB |
1824 | struct list_head *nce_head; |
1825 | struct name_cache_entry *cur; | |
31db9f7c | 1826 | |
7e0926fe AB |
1827 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
1828 | if (!nce_head) | |
31db9f7c AB |
1829 | return NULL; |
1830 | ||
7e0926fe AB |
1831 | list_for_each_entry(cur, nce_head, radix_list) { |
1832 | if (cur->ino == ino && cur->gen == gen) | |
1833 | return cur; | |
1834 | } | |
31db9f7c AB |
1835 | return NULL; |
1836 | } | |
1837 | ||
766702ef AB |
1838 | /* |
1839 | * Removes the entry from the list and adds it back to the end. This marks the | |
1840 | * entry as recently used so that name_cache_clean_unused does not remove it. | |
1841 | */ | |
31db9f7c AB |
1842 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
1843 | { | |
1844 | list_del(&nce->list); | |
1845 | list_add_tail(&nce->list, &sctx->name_cache_list); | |
1846 | } | |
1847 | ||
766702ef AB |
1848 | /* |
1849 | * Remove some entries from the beginning of name_cache_list. | |
1850 | */ | |
31db9f7c AB |
1851 | static void name_cache_clean_unused(struct send_ctx *sctx) |
1852 | { | |
1853 | struct name_cache_entry *nce; | |
1854 | ||
1855 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) | |
1856 | return; | |
1857 | ||
1858 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { | |
1859 | nce = list_entry(sctx->name_cache_list.next, | |
1860 | struct name_cache_entry, list); | |
1861 | name_cache_delete(sctx, nce); | |
1862 | kfree(nce); | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | static void name_cache_free(struct send_ctx *sctx) | |
1867 | { | |
1868 | struct name_cache_entry *nce; | |
31db9f7c | 1869 | |
e938c8ad AB |
1870 | while (!list_empty(&sctx->name_cache_list)) { |
1871 | nce = list_entry(sctx->name_cache_list.next, | |
1872 | struct name_cache_entry, list); | |
31db9f7c | 1873 | name_cache_delete(sctx, nce); |
17589bd9 | 1874 | kfree(nce); |
31db9f7c AB |
1875 | } |
1876 | } | |
1877 | ||
766702ef AB |
1878 | /* |
1879 | * Used by get_cur_path for each ref up to the root. | |
1880 | * Returns 0 if it succeeded. | |
1881 | * Returns 1 if the inode is not existent or got overwritten. In that case, the | |
1882 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 | |
1883 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. | |
1884 | * Returns <0 in case of error. | |
1885 | */ | |
31db9f7c AB |
1886 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
1887 | u64 ino, u64 gen, | |
1888 | u64 *parent_ino, | |
1889 | u64 *parent_gen, | |
1890 | struct fs_path *dest) | |
1891 | { | |
1892 | int ret; | |
1893 | int nce_ret; | |
1894 | struct btrfs_path *path = NULL; | |
1895 | struct name_cache_entry *nce = NULL; | |
1896 | ||
766702ef AB |
1897 | /* |
1898 | * First check if we already did a call to this function with the same | |
1899 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes | |
1900 | * return the cached result. | |
1901 | */ | |
31db9f7c AB |
1902 | nce = name_cache_search(sctx, ino, gen); |
1903 | if (nce) { | |
1904 | if (ino < sctx->send_progress && nce->need_later_update) { | |
1905 | name_cache_delete(sctx, nce); | |
1906 | kfree(nce); | |
1907 | nce = NULL; | |
1908 | } else { | |
1909 | name_cache_used(sctx, nce); | |
1910 | *parent_ino = nce->parent_ino; | |
1911 | *parent_gen = nce->parent_gen; | |
1912 | ret = fs_path_add(dest, nce->name, nce->name_len); | |
1913 | if (ret < 0) | |
1914 | goto out; | |
1915 | ret = nce->ret; | |
1916 | goto out; | |
1917 | } | |
1918 | } | |
1919 | ||
1920 | path = alloc_path_for_send(); | |
1921 | if (!path) | |
1922 | return -ENOMEM; | |
1923 | ||
766702ef AB |
1924 | /* |
1925 | * If the inode is not existent yet, add the orphan name and return 1. | |
1926 | * This should only happen for the parent dir that we determine in | |
1927 | * __record_new_ref | |
1928 | */ | |
31db9f7c AB |
1929 | ret = is_inode_existent(sctx, ino, gen); |
1930 | if (ret < 0) | |
1931 | goto out; | |
1932 | ||
1933 | if (!ret) { | |
1934 | ret = gen_unique_name(sctx, ino, gen, dest); | |
1935 | if (ret < 0) | |
1936 | goto out; | |
1937 | ret = 1; | |
1938 | goto out_cache; | |
1939 | } | |
1940 | ||
766702ef AB |
1941 | /* |
1942 | * Depending on whether the inode was already processed or not, use | |
1943 | * send_root or parent_root for ref lookup. | |
1944 | */ | |
31db9f7c AB |
1945 | if (ino < sctx->send_progress) |
1946 | ret = get_first_ref(sctx, sctx->send_root, ino, | |
1947 | parent_ino, parent_gen, dest); | |
1948 | else | |
1949 | ret = get_first_ref(sctx, sctx->parent_root, ino, | |
1950 | parent_ino, parent_gen, dest); | |
1951 | if (ret < 0) | |
1952 | goto out; | |
1953 | ||
766702ef AB |
1954 | /* |
1955 | * Check if the ref was overwritten by an inode's ref that was processed | |
1956 | * earlier. If yes, treat as orphan and return 1. | |
1957 | */ | |
31db9f7c AB |
1958 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
1959 | dest->start, dest->end - dest->start); | |
1960 | if (ret < 0) | |
1961 | goto out; | |
1962 | if (ret) { | |
1963 | fs_path_reset(dest); | |
1964 | ret = gen_unique_name(sctx, ino, gen, dest); | |
1965 | if (ret < 0) | |
1966 | goto out; | |
1967 | ret = 1; | |
1968 | } | |
1969 | ||
1970 | out_cache: | |
766702ef AB |
1971 | /* |
1972 | * Store the result of the lookup in the name cache. | |
1973 | */ | |
31db9f7c AB |
1974 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS); |
1975 | if (!nce) { | |
1976 | ret = -ENOMEM; | |
1977 | goto out; | |
1978 | } | |
1979 | ||
1980 | nce->ino = ino; | |
1981 | nce->gen = gen; | |
1982 | nce->parent_ino = *parent_ino; | |
1983 | nce->parent_gen = *parent_gen; | |
1984 | nce->name_len = fs_path_len(dest); | |
1985 | nce->ret = ret; | |
1986 | strcpy(nce->name, dest->start); | |
31db9f7c AB |
1987 | |
1988 | if (ino < sctx->send_progress) | |
1989 | nce->need_later_update = 0; | |
1990 | else | |
1991 | nce->need_later_update = 1; | |
1992 | ||
1993 | nce_ret = name_cache_insert(sctx, nce); | |
1994 | if (nce_ret < 0) | |
1995 | ret = nce_ret; | |
1996 | name_cache_clean_unused(sctx); | |
1997 | ||
1998 | out: | |
1999 | btrfs_free_path(path); | |
2000 | return ret; | |
2001 | } | |
2002 | ||
2003 | /* | |
2004 | * Magic happens here. This function returns the first ref to an inode as it | |
2005 | * would look like while receiving the stream at this point in time. | |
2006 | * We walk the path up to the root. For every inode in between, we check if it | |
2007 | * was already processed/sent. If yes, we continue with the parent as found | |
2008 | * in send_root. If not, we continue with the parent as found in parent_root. | |
2009 | * If we encounter an inode that was deleted at this point in time, we use the | |
2010 | * inodes "orphan" name instead of the real name and stop. Same with new inodes | |
2011 | * that were not created yet and overwritten inodes/refs. | |
2012 | * | |
2013 | * When do we have have orphan inodes: | |
2014 | * 1. When an inode is freshly created and thus no valid refs are available yet | |
2015 | * 2. When a directory lost all it's refs (deleted) but still has dir items | |
2016 | * inside which were not processed yet (pending for move/delete). If anyone | |
2017 | * tried to get the path to the dir items, it would get a path inside that | |
2018 | * orphan directory. | |
2019 | * 3. When an inode is moved around or gets new links, it may overwrite the ref | |
2020 | * of an unprocessed inode. If in that case the first ref would be | |
2021 | * overwritten, the overwritten inode gets "orphanized". Later when we | |
2022 | * process this overwritten inode, it is restored at a new place by moving | |
2023 | * the orphan inode. | |
2024 | * | |
2025 | * sctx->send_progress tells this function at which point in time receiving | |
2026 | * would be. | |
2027 | */ | |
2028 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, | |
2029 | struct fs_path *dest) | |
2030 | { | |
2031 | int ret = 0; | |
2032 | struct fs_path *name = NULL; | |
2033 | u64 parent_inode = 0; | |
2034 | u64 parent_gen = 0; | |
2035 | int stop = 0; | |
2036 | ||
2037 | name = fs_path_alloc(sctx); | |
2038 | if (!name) { | |
2039 | ret = -ENOMEM; | |
2040 | goto out; | |
2041 | } | |
2042 | ||
2043 | dest->reversed = 1; | |
2044 | fs_path_reset(dest); | |
2045 | ||
2046 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { | |
2047 | fs_path_reset(name); | |
2048 | ||
2049 | ret = __get_cur_name_and_parent(sctx, ino, gen, | |
2050 | &parent_inode, &parent_gen, name); | |
2051 | if (ret < 0) | |
2052 | goto out; | |
2053 | if (ret) | |
2054 | stop = 1; | |
2055 | ||
2056 | ret = fs_path_add_path(dest, name); | |
2057 | if (ret < 0) | |
2058 | goto out; | |
2059 | ||
2060 | ino = parent_inode; | |
2061 | gen = parent_gen; | |
2062 | } | |
2063 | ||
2064 | out: | |
2065 | fs_path_free(sctx, name); | |
2066 | if (!ret) | |
2067 | fs_path_unreverse(dest); | |
2068 | return ret; | |
2069 | } | |
2070 | ||
2071 | /* | |
2072 | * Called for regular files when sending extents data. Opens a struct file | |
2073 | * to read from the file. | |
2074 | */ | |
2075 | static int open_cur_inode_file(struct send_ctx *sctx) | |
2076 | { | |
2077 | int ret = 0; | |
2078 | struct btrfs_key key; | |
e2aed8df | 2079 | struct path path; |
31db9f7c AB |
2080 | struct inode *inode; |
2081 | struct dentry *dentry; | |
2082 | struct file *filp; | |
2083 | int new = 0; | |
2084 | ||
2085 | if (sctx->cur_inode_filp) | |
2086 | goto out; | |
2087 | ||
2088 | key.objectid = sctx->cur_ino; | |
2089 | key.type = BTRFS_INODE_ITEM_KEY; | |
2090 | key.offset = 0; | |
2091 | ||
2092 | inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root, | |
2093 | &new); | |
2094 | if (IS_ERR(inode)) { | |
2095 | ret = PTR_ERR(inode); | |
2096 | goto out; | |
2097 | } | |
2098 | ||
2099 | dentry = d_obtain_alias(inode); | |
2100 | inode = NULL; | |
2101 | if (IS_ERR(dentry)) { | |
2102 | ret = PTR_ERR(dentry); | |
2103 | goto out; | |
2104 | } | |
2105 | ||
e2aed8df LT |
2106 | path.mnt = sctx->mnt; |
2107 | path.dentry = dentry; | |
2108 | filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred()); | |
2109 | dput(dentry); | |
31db9f7c | 2110 | dentry = NULL; |
31db9f7c AB |
2111 | if (IS_ERR(filp)) { |
2112 | ret = PTR_ERR(filp); | |
2113 | goto out; | |
2114 | } | |
2115 | sctx->cur_inode_filp = filp; | |
2116 | ||
2117 | out: | |
2118 | /* | |
2119 | * no xxxput required here as every vfs op | |
2120 | * does it by itself on failure | |
2121 | */ | |
2122 | return ret; | |
2123 | } | |
2124 | ||
2125 | /* | |
2126 | * Closes the struct file that was created in open_cur_inode_file | |
2127 | */ | |
2128 | static int close_cur_inode_file(struct send_ctx *sctx) | |
2129 | { | |
2130 | int ret = 0; | |
2131 | ||
2132 | if (!sctx->cur_inode_filp) | |
2133 | goto out; | |
2134 | ||
2135 | ret = filp_close(sctx->cur_inode_filp, NULL); | |
2136 | sctx->cur_inode_filp = NULL; | |
2137 | ||
2138 | out: | |
2139 | return ret; | |
2140 | } | |
2141 | ||
2142 | /* | |
2143 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace | |
2144 | */ | |
2145 | static int send_subvol_begin(struct send_ctx *sctx) | |
2146 | { | |
2147 | int ret; | |
2148 | struct btrfs_root *send_root = sctx->send_root; | |
2149 | struct btrfs_root *parent_root = sctx->parent_root; | |
2150 | struct btrfs_path *path; | |
2151 | struct btrfs_key key; | |
2152 | struct btrfs_root_ref *ref; | |
2153 | struct extent_buffer *leaf; | |
2154 | char *name = NULL; | |
2155 | int namelen; | |
2156 | ||
2157 | path = alloc_path_for_send(); | |
2158 | if (!path) | |
2159 | return -ENOMEM; | |
2160 | ||
2161 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS); | |
2162 | if (!name) { | |
2163 | btrfs_free_path(path); | |
2164 | return -ENOMEM; | |
2165 | } | |
2166 | ||
2167 | key.objectid = send_root->objectid; | |
2168 | key.type = BTRFS_ROOT_BACKREF_KEY; | |
2169 | key.offset = 0; | |
2170 | ||
2171 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, | |
2172 | &key, path, 1, 0); | |
2173 | if (ret < 0) | |
2174 | goto out; | |
2175 | if (ret) { | |
2176 | ret = -ENOENT; | |
2177 | goto out; | |
2178 | } | |
2179 | ||
2180 | leaf = path->nodes[0]; | |
2181 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2182 | if (key.type != BTRFS_ROOT_BACKREF_KEY || | |
2183 | key.objectid != send_root->objectid) { | |
2184 | ret = -ENOENT; | |
2185 | goto out; | |
2186 | } | |
2187 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
2188 | namelen = btrfs_root_ref_name_len(leaf, ref); | |
2189 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); | |
2190 | btrfs_release_path(path); | |
2191 | ||
31db9f7c AB |
2192 | if (parent_root) { |
2193 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); | |
2194 | if (ret < 0) | |
2195 | goto out; | |
2196 | } else { | |
2197 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); | |
2198 | if (ret < 0) | |
2199 | goto out; | |
2200 | } | |
2201 | ||
2202 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); | |
2203 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, | |
2204 | sctx->send_root->root_item.uuid); | |
2205 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, | |
2206 | sctx->send_root->root_item.ctransid); | |
2207 | if (parent_root) { | |
2208 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
2209 | sctx->parent_root->root_item.uuid); | |
2210 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, | |
2211 | sctx->parent_root->root_item.ctransid); | |
2212 | } | |
2213 | ||
2214 | ret = send_cmd(sctx); | |
2215 | ||
2216 | tlv_put_failure: | |
2217 | out: | |
2218 | btrfs_free_path(path); | |
2219 | kfree(name); | |
2220 | return ret; | |
2221 | } | |
2222 | ||
2223 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) | |
2224 | { | |
2225 | int ret = 0; | |
2226 | struct fs_path *p; | |
2227 | ||
2228 | verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size); | |
2229 | ||
2230 | p = fs_path_alloc(sctx); | |
2231 | if (!p) | |
2232 | return -ENOMEM; | |
2233 | ||
2234 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); | |
2235 | if (ret < 0) | |
2236 | goto out; | |
2237 | ||
2238 | ret = get_cur_path(sctx, ino, gen, p); | |
2239 | if (ret < 0) | |
2240 | goto out; | |
2241 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2242 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); | |
2243 | ||
2244 | ret = send_cmd(sctx); | |
2245 | ||
2246 | tlv_put_failure: | |
2247 | out: | |
2248 | fs_path_free(sctx, p); | |
2249 | return ret; | |
2250 | } | |
2251 | ||
2252 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) | |
2253 | { | |
2254 | int ret = 0; | |
2255 | struct fs_path *p; | |
2256 | ||
2257 | verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode); | |
2258 | ||
2259 | p = fs_path_alloc(sctx); | |
2260 | if (!p) | |
2261 | return -ENOMEM; | |
2262 | ||
2263 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); | |
2264 | if (ret < 0) | |
2265 | goto out; | |
2266 | ||
2267 | ret = get_cur_path(sctx, ino, gen, p); | |
2268 | if (ret < 0) | |
2269 | goto out; | |
2270 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2271 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); | |
2272 | ||
2273 | ret = send_cmd(sctx); | |
2274 | ||
2275 | tlv_put_failure: | |
2276 | out: | |
2277 | fs_path_free(sctx, p); | |
2278 | return ret; | |
2279 | } | |
2280 | ||
2281 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) | |
2282 | { | |
2283 | int ret = 0; | |
2284 | struct fs_path *p; | |
2285 | ||
2286 | verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid); | |
2287 | ||
2288 | p = fs_path_alloc(sctx); | |
2289 | if (!p) | |
2290 | return -ENOMEM; | |
2291 | ||
2292 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); | |
2293 | if (ret < 0) | |
2294 | goto out; | |
2295 | ||
2296 | ret = get_cur_path(sctx, ino, gen, p); | |
2297 | if (ret < 0) | |
2298 | goto out; | |
2299 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2300 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); | |
2301 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); | |
2302 | ||
2303 | ret = send_cmd(sctx); | |
2304 | ||
2305 | tlv_put_failure: | |
2306 | out: | |
2307 | fs_path_free(sctx, p); | |
2308 | return ret; | |
2309 | } | |
2310 | ||
2311 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) | |
2312 | { | |
2313 | int ret = 0; | |
2314 | struct fs_path *p = NULL; | |
2315 | struct btrfs_inode_item *ii; | |
2316 | struct btrfs_path *path = NULL; | |
2317 | struct extent_buffer *eb; | |
2318 | struct btrfs_key key; | |
2319 | int slot; | |
2320 | ||
2321 | verbose_printk("btrfs: send_utimes %llu\n", ino); | |
2322 | ||
2323 | p = fs_path_alloc(sctx); | |
2324 | if (!p) | |
2325 | return -ENOMEM; | |
2326 | ||
2327 | path = alloc_path_for_send(); | |
2328 | if (!path) { | |
2329 | ret = -ENOMEM; | |
2330 | goto out; | |
2331 | } | |
2332 | ||
2333 | key.objectid = ino; | |
2334 | key.type = BTRFS_INODE_ITEM_KEY; | |
2335 | key.offset = 0; | |
2336 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); | |
2337 | if (ret < 0) | |
2338 | goto out; | |
2339 | ||
2340 | eb = path->nodes[0]; | |
2341 | slot = path->slots[0]; | |
2342 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
2343 | ||
2344 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); | |
2345 | if (ret < 0) | |
2346 | goto out; | |
2347 | ||
2348 | ret = get_cur_path(sctx, ino, gen, p); | |
2349 | if (ret < 0) | |
2350 | goto out; | |
2351 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2352 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, | |
2353 | btrfs_inode_atime(ii)); | |
2354 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, | |
2355 | btrfs_inode_mtime(ii)); | |
2356 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, | |
2357 | btrfs_inode_ctime(ii)); | |
766702ef | 2358 | /* TODO Add otime support when the otime patches get into upstream */ |
31db9f7c AB |
2359 | |
2360 | ret = send_cmd(sctx); | |
2361 | ||
2362 | tlv_put_failure: | |
2363 | out: | |
2364 | fs_path_free(sctx, p); | |
2365 | btrfs_free_path(path); | |
2366 | return ret; | |
2367 | } | |
2368 | ||
2369 | /* | |
2370 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have | |
2371 | * a valid path yet because we did not process the refs yet. So, the inode | |
2372 | * is created as orphan. | |
2373 | */ | |
1f4692da | 2374 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
31db9f7c AB |
2375 | { |
2376 | int ret = 0; | |
31db9f7c | 2377 | struct fs_path *p; |
31db9f7c | 2378 | int cmd; |
1f4692da | 2379 | u64 gen; |
31db9f7c | 2380 | u64 mode; |
1f4692da | 2381 | u64 rdev; |
31db9f7c | 2382 | |
1f4692da | 2383 | verbose_printk("btrfs: send_create_inode %llu\n", ino); |
31db9f7c AB |
2384 | |
2385 | p = fs_path_alloc(sctx); | |
2386 | if (!p) | |
2387 | return -ENOMEM; | |
2388 | ||
1f4692da AB |
2389 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL, |
2390 | NULL, &rdev); | |
2391 | if (ret < 0) | |
2392 | goto out; | |
31db9f7c | 2393 | |
e938c8ad | 2394 | if (S_ISREG(mode)) { |
31db9f7c | 2395 | cmd = BTRFS_SEND_C_MKFILE; |
e938c8ad | 2396 | } else if (S_ISDIR(mode)) { |
31db9f7c | 2397 | cmd = BTRFS_SEND_C_MKDIR; |
e938c8ad | 2398 | } else if (S_ISLNK(mode)) { |
31db9f7c | 2399 | cmd = BTRFS_SEND_C_SYMLINK; |
e938c8ad | 2400 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
31db9f7c | 2401 | cmd = BTRFS_SEND_C_MKNOD; |
e938c8ad | 2402 | } else if (S_ISFIFO(mode)) { |
31db9f7c | 2403 | cmd = BTRFS_SEND_C_MKFIFO; |
e938c8ad | 2404 | } else if (S_ISSOCK(mode)) { |
31db9f7c | 2405 | cmd = BTRFS_SEND_C_MKSOCK; |
e938c8ad | 2406 | } else { |
31db9f7c AB |
2407 | printk(KERN_WARNING "btrfs: unexpected inode type %o", |
2408 | (int)(mode & S_IFMT)); | |
2409 | ret = -ENOTSUPP; | |
2410 | goto out; | |
2411 | } | |
2412 | ||
2413 | ret = begin_cmd(sctx, cmd); | |
2414 | if (ret < 0) | |
2415 | goto out; | |
2416 | ||
1f4692da | 2417 | ret = gen_unique_name(sctx, ino, gen, p); |
31db9f7c AB |
2418 | if (ret < 0) |
2419 | goto out; | |
2420 | ||
2421 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
1f4692da | 2422 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
31db9f7c AB |
2423 | |
2424 | if (S_ISLNK(mode)) { | |
2425 | fs_path_reset(p); | |
1f4692da | 2426 | ret = read_symlink(sctx, sctx->send_root, ino, p); |
31db9f7c AB |
2427 | if (ret < 0) |
2428 | goto out; | |
2429 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); | |
2430 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || | |
2431 | S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
1f4692da | 2432 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, rdev); |
31db9f7c AB |
2433 | } |
2434 | ||
2435 | ret = send_cmd(sctx); | |
2436 | if (ret < 0) | |
2437 | goto out; | |
2438 | ||
2439 | ||
2440 | tlv_put_failure: | |
2441 | out: | |
2442 | fs_path_free(sctx, p); | |
2443 | return ret; | |
2444 | } | |
2445 | ||
1f4692da AB |
2446 | /* |
2447 | * We need some special handling for inodes that get processed before the parent | |
2448 | * directory got created. See process_recorded_refs for details. | |
2449 | * This function does the check if we already created the dir out of order. | |
2450 | */ | |
2451 | static int did_create_dir(struct send_ctx *sctx, u64 dir) | |
2452 | { | |
2453 | int ret = 0; | |
2454 | struct btrfs_path *path = NULL; | |
2455 | struct btrfs_key key; | |
2456 | struct btrfs_key found_key; | |
2457 | struct btrfs_key di_key; | |
2458 | struct extent_buffer *eb; | |
2459 | struct btrfs_dir_item *di; | |
2460 | int slot; | |
2461 | ||
2462 | path = alloc_path_for_send(); | |
2463 | if (!path) { | |
2464 | ret = -ENOMEM; | |
2465 | goto out; | |
2466 | } | |
2467 | ||
2468 | key.objectid = dir; | |
2469 | key.type = BTRFS_DIR_INDEX_KEY; | |
2470 | key.offset = 0; | |
2471 | while (1) { | |
2472 | ret = btrfs_search_slot_for_read(sctx->send_root, &key, path, | |
2473 | 1, 0); | |
2474 | if (ret < 0) | |
2475 | goto out; | |
2476 | if (!ret) { | |
2477 | eb = path->nodes[0]; | |
2478 | slot = path->slots[0]; | |
2479 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
2480 | } | |
2481 | if (ret || found_key.objectid != key.objectid || | |
2482 | found_key.type != key.type) { | |
2483 | ret = 0; | |
2484 | goto out; | |
2485 | } | |
2486 | ||
2487 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); | |
2488 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); | |
2489 | ||
2490 | if (di_key.objectid < sctx->send_progress) { | |
2491 | ret = 1; | |
2492 | goto out; | |
2493 | } | |
2494 | ||
2495 | key.offset = found_key.offset + 1; | |
2496 | btrfs_release_path(path); | |
2497 | } | |
2498 | ||
2499 | out: | |
2500 | btrfs_free_path(path); | |
2501 | return ret; | |
2502 | } | |
2503 | ||
2504 | /* | |
2505 | * Only creates the inode if it is: | |
2506 | * 1. Not a directory | |
2507 | * 2. Or a directory which was not created already due to out of order | |
2508 | * directories. See did_create_dir and process_recorded_refs for details. | |
2509 | */ | |
2510 | static int send_create_inode_if_needed(struct send_ctx *sctx) | |
2511 | { | |
2512 | int ret; | |
2513 | ||
2514 | if (S_ISDIR(sctx->cur_inode_mode)) { | |
2515 | ret = did_create_dir(sctx, sctx->cur_ino); | |
2516 | if (ret < 0) | |
2517 | goto out; | |
2518 | if (ret) { | |
2519 | ret = 0; | |
2520 | goto out; | |
2521 | } | |
2522 | } | |
2523 | ||
2524 | ret = send_create_inode(sctx, sctx->cur_ino); | |
2525 | if (ret < 0) | |
2526 | goto out; | |
2527 | ||
2528 | out: | |
2529 | return ret; | |
2530 | } | |
2531 | ||
31db9f7c AB |
2532 | struct recorded_ref { |
2533 | struct list_head list; | |
2534 | char *dir_path; | |
2535 | char *name; | |
2536 | struct fs_path *full_path; | |
2537 | u64 dir; | |
2538 | u64 dir_gen; | |
2539 | int dir_path_len; | |
2540 | int name_len; | |
2541 | }; | |
2542 | ||
2543 | /* | |
2544 | * We need to process new refs before deleted refs, but compare_tree gives us | |
2545 | * everything mixed. So we first record all refs and later process them. | |
2546 | * This function is a helper to record one ref. | |
2547 | */ | |
2548 | static int record_ref(struct list_head *head, u64 dir, | |
2549 | u64 dir_gen, struct fs_path *path) | |
2550 | { | |
2551 | struct recorded_ref *ref; | |
2552 | char *tmp; | |
2553 | ||
2554 | ref = kmalloc(sizeof(*ref), GFP_NOFS); | |
2555 | if (!ref) | |
2556 | return -ENOMEM; | |
2557 | ||
2558 | ref->dir = dir; | |
2559 | ref->dir_gen = dir_gen; | |
2560 | ref->full_path = path; | |
2561 | ||
2562 | tmp = strrchr(ref->full_path->start, '/'); | |
2563 | if (!tmp) { | |
2564 | ref->name_len = ref->full_path->end - ref->full_path->start; | |
2565 | ref->name = ref->full_path->start; | |
2566 | ref->dir_path_len = 0; | |
2567 | ref->dir_path = ref->full_path->start; | |
2568 | } else { | |
2569 | tmp++; | |
2570 | ref->name_len = ref->full_path->end - tmp; | |
2571 | ref->name = tmp; | |
2572 | ref->dir_path = ref->full_path->start; | |
2573 | ref->dir_path_len = ref->full_path->end - | |
2574 | ref->full_path->start - 1 - ref->name_len; | |
2575 | } | |
2576 | ||
2577 | list_add_tail(&ref->list, head); | |
2578 | return 0; | |
2579 | } | |
2580 | ||
2581 | static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head) | |
2582 | { | |
2583 | struct recorded_ref *cur; | |
31db9f7c | 2584 | |
e938c8ad AB |
2585 | while (!list_empty(head)) { |
2586 | cur = list_entry(head->next, struct recorded_ref, list); | |
31db9f7c | 2587 | fs_path_free(sctx, cur->full_path); |
e938c8ad | 2588 | list_del(&cur->list); |
31db9f7c AB |
2589 | kfree(cur); |
2590 | } | |
31db9f7c AB |
2591 | } |
2592 | ||
2593 | static void free_recorded_refs(struct send_ctx *sctx) | |
2594 | { | |
2595 | __free_recorded_refs(sctx, &sctx->new_refs); | |
2596 | __free_recorded_refs(sctx, &sctx->deleted_refs); | |
2597 | } | |
2598 | ||
2599 | /* | |
766702ef | 2600 | * Renames/moves a file/dir to its orphan name. Used when the first |
31db9f7c AB |
2601 | * ref of an unprocessed inode gets overwritten and for all non empty |
2602 | * directories. | |
2603 | */ | |
2604 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, | |
2605 | struct fs_path *path) | |
2606 | { | |
2607 | int ret; | |
2608 | struct fs_path *orphan; | |
2609 | ||
2610 | orphan = fs_path_alloc(sctx); | |
2611 | if (!orphan) | |
2612 | return -ENOMEM; | |
2613 | ||
2614 | ret = gen_unique_name(sctx, ino, gen, orphan); | |
2615 | if (ret < 0) | |
2616 | goto out; | |
2617 | ||
2618 | ret = send_rename(sctx, path, orphan); | |
2619 | ||
2620 | out: | |
2621 | fs_path_free(sctx, orphan); | |
2622 | return ret; | |
2623 | } | |
2624 | ||
2625 | /* | |
2626 | * Returns 1 if a directory can be removed at this point in time. | |
2627 | * We check this by iterating all dir items and checking if the inode behind | |
2628 | * the dir item was already processed. | |
2629 | */ | |
2630 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress) | |
2631 | { | |
2632 | int ret = 0; | |
2633 | struct btrfs_root *root = sctx->parent_root; | |
2634 | struct btrfs_path *path; | |
2635 | struct btrfs_key key; | |
2636 | struct btrfs_key found_key; | |
2637 | struct btrfs_key loc; | |
2638 | struct btrfs_dir_item *di; | |
2639 | ||
6d85ed05 AB |
2640 | /* |
2641 | * Don't try to rmdir the top/root subvolume dir. | |
2642 | */ | |
2643 | if (dir == BTRFS_FIRST_FREE_OBJECTID) | |
2644 | return 0; | |
2645 | ||
31db9f7c AB |
2646 | path = alloc_path_for_send(); |
2647 | if (!path) | |
2648 | return -ENOMEM; | |
2649 | ||
2650 | key.objectid = dir; | |
2651 | key.type = BTRFS_DIR_INDEX_KEY; | |
2652 | key.offset = 0; | |
2653 | ||
2654 | while (1) { | |
2655 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
2656 | if (ret < 0) | |
2657 | goto out; | |
2658 | if (!ret) { | |
2659 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2660 | path->slots[0]); | |
2661 | } | |
2662 | if (ret || found_key.objectid != key.objectid || | |
2663 | found_key.type != key.type) { | |
2664 | break; | |
2665 | } | |
2666 | ||
2667 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2668 | struct btrfs_dir_item); | |
2669 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); | |
2670 | ||
2671 | if (loc.objectid > send_progress) { | |
2672 | ret = 0; | |
2673 | goto out; | |
2674 | } | |
2675 | ||
2676 | btrfs_release_path(path); | |
2677 | key.offset = found_key.offset + 1; | |
2678 | } | |
2679 | ||
2680 | ret = 1; | |
2681 | ||
2682 | out: | |
2683 | btrfs_free_path(path); | |
2684 | return ret; | |
2685 | } | |
2686 | ||
31db9f7c AB |
2687 | /* |
2688 | * This does all the move/link/unlink/rmdir magic. | |
2689 | */ | |
2690 | static int process_recorded_refs(struct send_ctx *sctx) | |
2691 | { | |
2692 | int ret = 0; | |
2693 | struct recorded_ref *cur; | |
1f4692da | 2694 | struct recorded_ref *cur2; |
31db9f7c AB |
2695 | struct ulist *check_dirs = NULL; |
2696 | struct ulist_iterator uit; | |
2697 | struct ulist_node *un; | |
2698 | struct fs_path *valid_path = NULL; | |
b24baf69 | 2699 | u64 ow_inode = 0; |
31db9f7c AB |
2700 | u64 ow_gen; |
2701 | int did_overwrite = 0; | |
2702 | int is_orphan = 0; | |
2703 | ||
2704 | verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino); | |
2705 | ||
6d85ed05 AB |
2706 | /* |
2707 | * This should never happen as the root dir always has the same ref | |
2708 | * which is always '..' | |
2709 | */ | |
2710 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); | |
2711 | ||
31db9f7c AB |
2712 | valid_path = fs_path_alloc(sctx); |
2713 | if (!valid_path) { | |
2714 | ret = -ENOMEM; | |
2715 | goto out; | |
2716 | } | |
2717 | ||
2718 | check_dirs = ulist_alloc(GFP_NOFS); | |
2719 | if (!check_dirs) { | |
2720 | ret = -ENOMEM; | |
2721 | goto out; | |
2722 | } | |
2723 | ||
2724 | /* | |
2725 | * First, check if the first ref of the current inode was overwritten | |
2726 | * before. If yes, we know that the current inode was already orphanized | |
2727 | * and thus use the orphan name. If not, we can use get_cur_path to | |
2728 | * get the path of the first ref as it would like while receiving at | |
2729 | * this point in time. | |
2730 | * New inodes are always orphan at the beginning, so force to use the | |
2731 | * orphan name in this case. | |
2732 | * The first ref is stored in valid_path and will be updated if it | |
2733 | * gets moved around. | |
2734 | */ | |
2735 | if (!sctx->cur_inode_new) { | |
2736 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, | |
2737 | sctx->cur_inode_gen); | |
2738 | if (ret < 0) | |
2739 | goto out; | |
2740 | if (ret) | |
2741 | did_overwrite = 1; | |
2742 | } | |
2743 | if (sctx->cur_inode_new || did_overwrite) { | |
2744 | ret = gen_unique_name(sctx, sctx->cur_ino, | |
2745 | sctx->cur_inode_gen, valid_path); | |
2746 | if (ret < 0) | |
2747 | goto out; | |
2748 | is_orphan = 1; | |
2749 | } else { | |
2750 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
2751 | valid_path); | |
2752 | if (ret < 0) | |
2753 | goto out; | |
2754 | } | |
2755 | ||
2756 | list_for_each_entry(cur, &sctx->new_refs, list) { | |
1f4692da AB |
2757 | /* |
2758 | * We may have refs where the parent directory does not exist | |
2759 | * yet. This happens if the parent directories inum is higher | |
2760 | * the the current inum. To handle this case, we create the | |
2761 | * parent directory out of order. But we need to check if this | |
2762 | * did already happen before due to other refs in the same dir. | |
2763 | */ | |
2764 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); | |
2765 | if (ret < 0) | |
2766 | goto out; | |
2767 | if (ret == inode_state_will_create) { | |
2768 | ret = 0; | |
2769 | /* | |
2770 | * First check if any of the current inodes refs did | |
2771 | * already create the dir. | |
2772 | */ | |
2773 | list_for_each_entry(cur2, &sctx->new_refs, list) { | |
2774 | if (cur == cur2) | |
2775 | break; | |
2776 | if (cur2->dir == cur->dir) { | |
2777 | ret = 1; | |
2778 | break; | |
2779 | } | |
2780 | } | |
2781 | ||
2782 | /* | |
2783 | * If that did not happen, check if a previous inode | |
2784 | * did already create the dir. | |
2785 | */ | |
2786 | if (!ret) | |
2787 | ret = did_create_dir(sctx, cur->dir); | |
2788 | if (ret < 0) | |
2789 | goto out; | |
2790 | if (!ret) { | |
2791 | ret = send_create_inode(sctx, cur->dir); | |
2792 | if (ret < 0) | |
2793 | goto out; | |
2794 | } | |
2795 | } | |
2796 | ||
31db9f7c AB |
2797 | /* |
2798 | * Check if this new ref would overwrite the first ref of | |
2799 | * another unprocessed inode. If yes, orphanize the | |
2800 | * overwritten inode. If we find an overwritten ref that is | |
2801 | * not the first ref, simply unlink it. | |
2802 | */ | |
2803 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
2804 | cur->name, cur->name_len, | |
2805 | &ow_inode, &ow_gen); | |
2806 | if (ret < 0) | |
2807 | goto out; | |
2808 | if (ret) { | |
2809 | ret = is_first_ref(sctx, sctx->parent_root, | |
2810 | ow_inode, cur->dir, cur->name, | |
2811 | cur->name_len); | |
2812 | if (ret < 0) | |
2813 | goto out; | |
2814 | if (ret) { | |
2815 | ret = orphanize_inode(sctx, ow_inode, ow_gen, | |
2816 | cur->full_path); | |
2817 | if (ret < 0) | |
2818 | goto out; | |
2819 | } else { | |
2820 | ret = send_unlink(sctx, cur->full_path); | |
2821 | if (ret < 0) | |
2822 | goto out; | |
2823 | } | |
2824 | } | |
2825 | ||
2826 | /* | |
2827 | * link/move the ref to the new place. If we have an orphan | |
2828 | * inode, move it and update valid_path. If not, link or move | |
2829 | * it depending on the inode mode. | |
2830 | */ | |
1f4692da | 2831 | if (is_orphan) { |
31db9f7c AB |
2832 | ret = send_rename(sctx, valid_path, cur->full_path); |
2833 | if (ret < 0) | |
2834 | goto out; | |
2835 | is_orphan = 0; | |
2836 | ret = fs_path_copy(valid_path, cur->full_path); | |
2837 | if (ret < 0) | |
2838 | goto out; | |
2839 | } else { | |
2840 | if (S_ISDIR(sctx->cur_inode_mode)) { | |
2841 | /* | |
2842 | * Dirs can't be linked, so move it. For moved | |
2843 | * dirs, we always have one new and one deleted | |
2844 | * ref. The deleted ref is ignored later. | |
2845 | */ | |
2846 | ret = send_rename(sctx, valid_path, | |
2847 | cur->full_path); | |
2848 | if (ret < 0) | |
2849 | goto out; | |
2850 | ret = fs_path_copy(valid_path, cur->full_path); | |
2851 | if (ret < 0) | |
2852 | goto out; | |
2853 | } else { | |
2854 | ret = send_link(sctx, cur->full_path, | |
2855 | valid_path); | |
2856 | if (ret < 0) | |
2857 | goto out; | |
2858 | } | |
2859 | } | |
2860 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, | |
2861 | GFP_NOFS); | |
2862 | if (ret < 0) | |
2863 | goto out; | |
2864 | } | |
2865 | ||
2866 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { | |
2867 | /* | |
2868 | * Check if we can already rmdir the directory. If not, | |
2869 | * orphanize it. For every dir item inside that gets deleted | |
2870 | * later, we do this check again and rmdir it then if possible. | |
2871 | * See the use of check_dirs for more details. | |
2872 | */ | |
2873 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino); | |
2874 | if (ret < 0) | |
2875 | goto out; | |
2876 | if (ret) { | |
2877 | ret = send_rmdir(sctx, valid_path); | |
2878 | if (ret < 0) | |
2879 | goto out; | |
2880 | } else if (!is_orphan) { | |
2881 | ret = orphanize_inode(sctx, sctx->cur_ino, | |
2882 | sctx->cur_inode_gen, valid_path); | |
2883 | if (ret < 0) | |
2884 | goto out; | |
2885 | is_orphan = 1; | |
2886 | } | |
2887 | ||
2888 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
2889 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, | |
2890 | GFP_NOFS); | |
2891 | if (ret < 0) | |
2892 | goto out; | |
2893 | } | |
ccf1626b AB |
2894 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
2895 | !list_empty(&sctx->deleted_refs)) { | |
2896 | /* | |
2897 | * We have a moved dir. Add the old parent to check_dirs | |
2898 | */ | |
2899 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, | |
2900 | list); | |
2901 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, | |
2902 | GFP_NOFS); | |
2903 | if (ret < 0) | |
2904 | goto out; | |
31db9f7c AB |
2905 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
2906 | /* | |
2907 | * We have a non dir inode. Go through all deleted refs and | |
2908 | * unlink them if they were not already overwritten by other | |
2909 | * inodes. | |
2910 | */ | |
2911 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
2912 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
2913 | sctx->cur_ino, sctx->cur_inode_gen, | |
2914 | cur->name, cur->name_len); | |
2915 | if (ret < 0) | |
2916 | goto out; | |
2917 | if (!ret) { | |
1f4692da AB |
2918 | ret = send_unlink(sctx, cur->full_path); |
2919 | if (ret < 0) | |
2920 | goto out; | |
31db9f7c AB |
2921 | } |
2922 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, | |
2923 | GFP_NOFS); | |
2924 | if (ret < 0) | |
2925 | goto out; | |
2926 | } | |
2927 | ||
2928 | /* | |
2929 | * If the inode is still orphan, unlink the orphan. This may | |
2930 | * happen when a previous inode did overwrite the first ref | |
2931 | * of this inode and no new refs were added for the current | |
766702ef AB |
2932 | * inode. Unlinking does not mean that the inode is deleted in |
2933 | * all cases. There may still be links to this inode in other | |
2934 | * places. | |
31db9f7c | 2935 | */ |
1f4692da | 2936 | if (is_orphan) { |
31db9f7c AB |
2937 | ret = send_unlink(sctx, valid_path); |
2938 | if (ret < 0) | |
2939 | goto out; | |
2940 | } | |
2941 | } | |
2942 | ||
2943 | /* | |
2944 | * We did collect all parent dirs where cur_inode was once located. We | |
2945 | * now go through all these dirs and check if they are pending for | |
2946 | * deletion and if it's finally possible to perform the rmdir now. | |
2947 | * We also update the inode stats of the parent dirs here. | |
2948 | */ | |
2949 | ULIST_ITER_INIT(&uit); | |
2950 | while ((un = ulist_next(check_dirs, &uit))) { | |
766702ef AB |
2951 | /* |
2952 | * In case we had refs into dirs that were not processed yet, | |
2953 | * we don't need to do the utime and rmdir logic for these dirs. | |
2954 | * The dir will be processed later. | |
2955 | */ | |
31db9f7c AB |
2956 | if (un->val > sctx->cur_ino) |
2957 | continue; | |
2958 | ||
2959 | ret = get_cur_inode_state(sctx, un->val, un->aux); | |
2960 | if (ret < 0) | |
2961 | goto out; | |
2962 | ||
2963 | if (ret == inode_state_did_create || | |
2964 | ret == inode_state_no_change) { | |
2965 | /* TODO delayed utimes */ | |
2966 | ret = send_utimes(sctx, un->val, un->aux); | |
2967 | if (ret < 0) | |
2968 | goto out; | |
2969 | } else if (ret == inode_state_did_delete) { | |
2970 | ret = can_rmdir(sctx, un->val, sctx->cur_ino); | |
2971 | if (ret < 0) | |
2972 | goto out; | |
2973 | if (ret) { | |
2974 | ret = get_cur_path(sctx, un->val, un->aux, | |
2975 | valid_path); | |
2976 | if (ret < 0) | |
2977 | goto out; | |
2978 | ret = send_rmdir(sctx, valid_path); | |
2979 | if (ret < 0) | |
2980 | goto out; | |
2981 | } | |
2982 | } | |
2983 | } | |
2984 | ||
31db9f7c AB |
2985 | ret = 0; |
2986 | ||
2987 | out: | |
2988 | free_recorded_refs(sctx); | |
2989 | ulist_free(check_dirs); | |
2990 | fs_path_free(sctx, valid_path); | |
2991 | return ret; | |
2992 | } | |
2993 | ||
2994 | static int __record_new_ref(int num, u64 dir, int index, | |
2995 | struct fs_path *name, | |
2996 | void *ctx) | |
2997 | { | |
2998 | int ret = 0; | |
2999 | struct send_ctx *sctx = ctx; | |
3000 | struct fs_path *p; | |
3001 | u64 gen; | |
3002 | ||
3003 | p = fs_path_alloc(sctx); | |
3004 | if (!p) | |
3005 | return -ENOMEM; | |
3006 | ||
3007 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL, | |
85a7b33b | 3008 | NULL, NULL); |
31db9f7c AB |
3009 | if (ret < 0) |
3010 | goto out; | |
3011 | ||
31db9f7c AB |
3012 | ret = get_cur_path(sctx, dir, gen, p); |
3013 | if (ret < 0) | |
3014 | goto out; | |
3015 | ret = fs_path_add_path(p, name); | |
3016 | if (ret < 0) | |
3017 | goto out; | |
3018 | ||
3019 | ret = record_ref(&sctx->new_refs, dir, gen, p); | |
3020 | ||
3021 | out: | |
3022 | if (ret) | |
3023 | fs_path_free(sctx, p); | |
3024 | return ret; | |
3025 | } | |
3026 | ||
3027 | static int __record_deleted_ref(int num, u64 dir, int index, | |
3028 | struct fs_path *name, | |
3029 | void *ctx) | |
3030 | { | |
3031 | int ret = 0; | |
3032 | struct send_ctx *sctx = ctx; | |
3033 | struct fs_path *p; | |
3034 | u64 gen; | |
3035 | ||
3036 | p = fs_path_alloc(sctx); | |
3037 | if (!p) | |
3038 | return -ENOMEM; | |
3039 | ||
3040 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL, | |
85a7b33b | 3041 | NULL, NULL); |
31db9f7c AB |
3042 | if (ret < 0) |
3043 | goto out; | |
3044 | ||
3045 | ret = get_cur_path(sctx, dir, gen, p); | |
3046 | if (ret < 0) | |
3047 | goto out; | |
3048 | ret = fs_path_add_path(p, name); | |
3049 | if (ret < 0) | |
3050 | goto out; | |
3051 | ||
3052 | ret = record_ref(&sctx->deleted_refs, dir, gen, p); | |
3053 | ||
3054 | out: | |
3055 | if (ret) | |
3056 | fs_path_free(sctx, p); | |
3057 | return ret; | |
3058 | } | |
3059 | ||
3060 | static int record_new_ref(struct send_ctx *sctx) | |
3061 | { | |
3062 | int ret; | |
3063 | ||
3064 | ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path, | |
3065 | sctx->cmp_key, 0, __record_new_ref, sctx); | |
3066 | if (ret < 0) | |
3067 | goto out; | |
3068 | ret = 0; | |
3069 | ||
3070 | out: | |
3071 | return ret; | |
3072 | } | |
3073 | ||
3074 | static int record_deleted_ref(struct send_ctx *sctx) | |
3075 | { | |
3076 | int ret; | |
3077 | ||
3078 | ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path, | |
3079 | sctx->cmp_key, 0, __record_deleted_ref, sctx); | |
3080 | if (ret < 0) | |
3081 | goto out; | |
3082 | ret = 0; | |
3083 | ||
3084 | out: | |
3085 | return ret; | |
3086 | } | |
3087 | ||
3088 | struct find_ref_ctx { | |
3089 | u64 dir; | |
3090 | struct fs_path *name; | |
3091 | int found_idx; | |
3092 | }; | |
3093 | ||
3094 | static int __find_iref(int num, u64 dir, int index, | |
3095 | struct fs_path *name, | |
3096 | void *ctx_) | |
3097 | { | |
3098 | struct find_ref_ctx *ctx = ctx_; | |
3099 | ||
3100 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && | |
3101 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { | |
3102 | ctx->found_idx = num; | |
3103 | return 1; | |
3104 | } | |
3105 | return 0; | |
3106 | } | |
3107 | ||
3108 | static int find_iref(struct send_ctx *sctx, | |
3109 | struct btrfs_root *root, | |
3110 | struct btrfs_path *path, | |
3111 | struct btrfs_key *key, | |
3112 | u64 dir, struct fs_path *name) | |
3113 | { | |
3114 | int ret; | |
3115 | struct find_ref_ctx ctx; | |
3116 | ||
3117 | ctx.dir = dir; | |
3118 | ctx.name = name; | |
3119 | ctx.found_idx = -1; | |
3120 | ||
3121 | ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx); | |
3122 | if (ret < 0) | |
3123 | return ret; | |
3124 | ||
3125 | if (ctx.found_idx == -1) | |
3126 | return -ENOENT; | |
3127 | ||
3128 | return ctx.found_idx; | |
3129 | } | |
3130 | ||
3131 | static int __record_changed_new_ref(int num, u64 dir, int index, | |
3132 | struct fs_path *name, | |
3133 | void *ctx) | |
3134 | { | |
3135 | int ret; | |
3136 | struct send_ctx *sctx = ctx; | |
3137 | ||
3138 | ret = find_iref(sctx, sctx->parent_root, sctx->right_path, | |
3139 | sctx->cmp_key, dir, name); | |
3140 | if (ret == -ENOENT) | |
3141 | ret = __record_new_ref(num, dir, index, name, sctx); | |
3142 | else if (ret > 0) | |
3143 | ret = 0; | |
3144 | ||
3145 | return ret; | |
3146 | } | |
3147 | ||
3148 | static int __record_changed_deleted_ref(int num, u64 dir, int index, | |
3149 | struct fs_path *name, | |
3150 | void *ctx) | |
3151 | { | |
3152 | int ret; | |
3153 | struct send_ctx *sctx = ctx; | |
3154 | ||
3155 | ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key, | |
3156 | dir, name); | |
3157 | if (ret == -ENOENT) | |
3158 | ret = __record_deleted_ref(num, dir, index, name, sctx); | |
3159 | else if (ret > 0) | |
3160 | ret = 0; | |
3161 | ||
3162 | return ret; | |
3163 | } | |
3164 | ||
3165 | static int record_changed_ref(struct send_ctx *sctx) | |
3166 | { | |
3167 | int ret = 0; | |
3168 | ||
3169 | ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path, | |
3170 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); | |
3171 | if (ret < 0) | |
3172 | goto out; | |
3173 | ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path, | |
3174 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); | |
3175 | if (ret < 0) | |
3176 | goto out; | |
3177 | ret = 0; | |
3178 | ||
3179 | out: | |
3180 | return ret; | |
3181 | } | |
3182 | ||
3183 | /* | |
3184 | * Record and process all refs at once. Needed when an inode changes the | |
3185 | * generation number, which means that it was deleted and recreated. | |
3186 | */ | |
3187 | static int process_all_refs(struct send_ctx *sctx, | |
3188 | enum btrfs_compare_tree_result cmd) | |
3189 | { | |
3190 | int ret; | |
3191 | struct btrfs_root *root; | |
3192 | struct btrfs_path *path; | |
3193 | struct btrfs_key key; | |
3194 | struct btrfs_key found_key; | |
3195 | struct extent_buffer *eb; | |
3196 | int slot; | |
3197 | iterate_inode_ref_t cb; | |
3198 | ||
3199 | path = alloc_path_for_send(); | |
3200 | if (!path) | |
3201 | return -ENOMEM; | |
3202 | ||
3203 | if (cmd == BTRFS_COMPARE_TREE_NEW) { | |
3204 | root = sctx->send_root; | |
3205 | cb = __record_new_ref; | |
3206 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { | |
3207 | root = sctx->parent_root; | |
3208 | cb = __record_deleted_ref; | |
3209 | } else { | |
3210 | BUG(); | |
3211 | } | |
3212 | ||
3213 | key.objectid = sctx->cmp_key->objectid; | |
3214 | key.type = BTRFS_INODE_REF_KEY; | |
3215 | key.offset = 0; | |
3216 | while (1) { | |
3217 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
e938c8ad | 3218 | if (ret < 0) |
31db9f7c | 3219 | goto out; |
e938c8ad | 3220 | if (ret) |
31db9f7c | 3221 | break; |
31db9f7c AB |
3222 | |
3223 | eb = path->nodes[0]; | |
3224 | slot = path->slots[0]; | |
3225 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
3226 | ||
3227 | if (found_key.objectid != key.objectid || | |
e938c8ad | 3228 | found_key.type != key.type) |
31db9f7c | 3229 | break; |
31db9f7c | 3230 | |
2f28f478 AB |
3231 | ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb, |
3232 | sctx); | |
31db9f7c AB |
3233 | btrfs_release_path(path); |
3234 | if (ret < 0) | |
3235 | goto out; | |
3236 | ||
3237 | key.offset = found_key.offset + 1; | |
3238 | } | |
e938c8ad | 3239 | btrfs_release_path(path); |
31db9f7c AB |
3240 | |
3241 | ret = process_recorded_refs(sctx); | |
3242 | ||
3243 | out: | |
3244 | btrfs_free_path(path); | |
3245 | return ret; | |
3246 | } | |
3247 | ||
3248 | static int send_set_xattr(struct send_ctx *sctx, | |
3249 | struct fs_path *path, | |
3250 | const char *name, int name_len, | |
3251 | const char *data, int data_len) | |
3252 | { | |
3253 | int ret = 0; | |
3254 | ||
3255 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); | |
3256 | if (ret < 0) | |
3257 | goto out; | |
3258 | ||
3259 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
3260 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
3261 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); | |
3262 | ||
3263 | ret = send_cmd(sctx); | |
3264 | ||
3265 | tlv_put_failure: | |
3266 | out: | |
3267 | return ret; | |
3268 | } | |
3269 | ||
3270 | static int send_remove_xattr(struct send_ctx *sctx, | |
3271 | struct fs_path *path, | |
3272 | const char *name, int name_len) | |
3273 | { | |
3274 | int ret = 0; | |
3275 | ||
3276 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); | |
3277 | if (ret < 0) | |
3278 | goto out; | |
3279 | ||
3280 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
3281 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
3282 | ||
3283 | ret = send_cmd(sctx); | |
3284 | ||
3285 | tlv_put_failure: | |
3286 | out: | |
3287 | return ret; | |
3288 | } | |
3289 | ||
3290 | static int __process_new_xattr(int num, struct btrfs_key *di_key, | |
3291 | const char *name, int name_len, | |
3292 | const char *data, int data_len, | |
3293 | u8 type, void *ctx) | |
3294 | { | |
3295 | int ret; | |
3296 | struct send_ctx *sctx = ctx; | |
3297 | struct fs_path *p; | |
3298 | posix_acl_xattr_header dummy_acl; | |
3299 | ||
3300 | p = fs_path_alloc(sctx); | |
3301 | if (!p) | |
3302 | return -ENOMEM; | |
3303 | ||
3304 | /* | |
3305 | * This hack is needed because empty acl's are stored as zero byte | |
3306 | * data in xattrs. Problem with that is, that receiving these zero byte | |
3307 | * acl's will fail later. To fix this, we send a dummy acl list that | |
3308 | * only contains the version number and no entries. | |
3309 | */ | |
3310 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || | |
3311 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { | |
3312 | if (data_len == 0) { | |
3313 | dummy_acl.a_version = | |
3314 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); | |
3315 | data = (char *)&dummy_acl; | |
3316 | data_len = sizeof(dummy_acl); | |
3317 | } | |
3318 | } | |
3319 | ||
3320 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3321 | if (ret < 0) | |
3322 | goto out; | |
3323 | ||
3324 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); | |
3325 | ||
3326 | out: | |
3327 | fs_path_free(sctx, p); | |
3328 | return ret; | |
3329 | } | |
3330 | ||
3331 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, | |
3332 | const char *name, int name_len, | |
3333 | const char *data, int data_len, | |
3334 | u8 type, void *ctx) | |
3335 | { | |
3336 | int ret; | |
3337 | struct send_ctx *sctx = ctx; | |
3338 | struct fs_path *p; | |
3339 | ||
3340 | p = fs_path_alloc(sctx); | |
3341 | if (!p) | |
3342 | return -ENOMEM; | |
3343 | ||
3344 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3345 | if (ret < 0) | |
3346 | goto out; | |
3347 | ||
3348 | ret = send_remove_xattr(sctx, p, name, name_len); | |
3349 | ||
3350 | out: | |
3351 | fs_path_free(sctx, p); | |
3352 | return ret; | |
3353 | } | |
3354 | ||
3355 | static int process_new_xattr(struct send_ctx *sctx) | |
3356 | { | |
3357 | int ret = 0; | |
3358 | ||
3359 | ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path, | |
3360 | sctx->cmp_key, __process_new_xattr, sctx); | |
3361 | ||
3362 | return ret; | |
3363 | } | |
3364 | ||
3365 | static int process_deleted_xattr(struct send_ctx *sctx) | |
3366 | { | |
3367 | int ret; | |
3368 | ||
3369 | ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path, | |
3370 | sctx->cmp_key, __process_deleted_xattr, sctx); | |
3371 | ||
3372 | return ret; | |
3373 | } | |
3374 | ||
3375 | struct find_xattr_ctx { | |
3376 | const char *name; | |
3377 | int name_len; | |
3378 | int found_idx; | |
3379 | char *found_data; | |
3380 | int found_data_len; | |
3381 | }; | |
3382 | ||
3383 | static int __find_xattr(int num, struct btrfs_key *di_key, | |
3384 | const char *name, int name_len, | |
3385 | const char *data, int data_len, | |
3386 | u8 type, void *vctx) | |
3387 | { | |
3388 | struct find_xattr_ctx *ctx = vctx; | |
3389 | ||
3390 | if (name_len == ctx->name_len && | |
3391 | strncmp(name, ctx->name, name_len) == 0) { | |
3392 | ctx->found_idx = num; | |
3393 | ctx->found_data_len = data_len; | |
3394 | ctx->found_data = kmalloc(data_len, GFP_NOFS); | |
3395 | if (!ctx->found_data) | |
3396 | return -ENOMEM; | |
3397 | memcpy(ctx->found_data, data, data_len); | |
3398 | return 1; | |
3399 | } | |
3400 | return 0; | |
3401 | } | |
3402 | ||
3403 | static int find_xattr(struct send_ctx *sctx, | |
3404 | struct btrfs_root *root, | |
3405 | struct btrfs_path *path, | |
3406 | struct btrfs_key *key, | |
3407 | const char *name, int name_len, | |
3408 | char **data, int *data_len) | |
3409 | { | |
3410 | int ret; | |
3411 | struct find_xattr_ctx ctx; | |
3412 | ||
3413 | ctx.name = name; | |
3414 | ctx.name_len = name_len; | |
3415 | ctx.found_idx = -1; | |
3416 | ctx.found_data = NULL; | |
3417 | ctx.found_data_len = 0; | |
3418 | ||
3419 | ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx); | |
3420 | if (ret < 0) | |
3421 | return ret; | |
3422 | ||
3423 | if (ctx.found_idx == -1) | |
3424 | return -ENOENT; | |
3425 | if (data) { | |
3426 | *data = ctx.found_data; | |
3427 | *data_len = ctx.found_data_len; | |
3428 | } else { | |
3429 | kfree(ctx.found_data); | |
3430 | } | |
3431 | return ctx.found_idx; | |
3432 | } | |
3433 | ||
3434 | ||
3435 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, | |
3436 | const char *name, int name_len, | |
3437 | const char *data, int data_len, | |
3438 | u8 type, void *ctx) | |
3439 | { | |
3440 | int ret; | |
3441 | struct send_ctx *sctx = ctx; | |
3442 | char *found_data = NULL; | |
3443 | int found_data_len = 0; | |
3444 | struct fs_path *p = NULL; | |
3445 | ||
3446 | ret = find_xattr(sctx, sctx->parent_root, sctx->right_path, | |
3447 | sctx->cmp_key, name, name_len, &found_data, | |
3448 | &found_data_len); | |
3449 | if (ret == -ENOENT) { | |
3450 | ret = __process_new_xattr(num, di_key, name, name_len, data, | |
3451 | data_len, type, ctx); | |
3452 | } else if (ret >= 0) { | |
3453 | if (data_len != found_data_len || | |
3454 | memcmp(data, found_data, data_len)) { | |
3455 | ret = __process_new_xattr(num, di_key, name, name_len, | |
3456 | data, data_len, type, ctx); | |
3457 | } else { | |
3458 | ret = 0; | |
3459 | } | |
3460 | } | |
3461 | ||
3462 | kfree(found_data); | |
3463 | fs_path_free(sctx, p); | |
3464 | return ret; | |
3465 | } | |
3466 | ||
3467 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, | |
3468 | const char *name, int name_len, | |
3469 | const char *data, int data_len, | |
3470 | u8 type, void *ctx) | |
3471 | { | |
3472 | int ret; | |
3473 | struct send_ctx *sctx = ctx; | |
3474 | ||
3475 | ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key, | |
3476 | name, name_len, NULL, NULL); | |
3477 | if (ret == -ENOENT) | |
3478 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, | |
3479 | data_len, type, ctx); | |
3480 | else if (ret >= 0) | |
3481 | ret = 0; | |
3482 | ||
3483 | return ret; | |
3484 | } | |
3485 | ||
3486 | static int process_changed_xattr(struct send_ctx *sctx) | |
3487 | { | |
3488 | int ret = 0; | |
3489 | ||
3490 | ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path, | |
3491 | sctx->cmp_key, __process_changed_new_xattr, sctx); | |
3492 | if (ret < 0) | |
3493 | goto out; | |
3494 | ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path, | |
3495 | sctx->cmp_key, __process_changed_deleted_xattr, sctx); | |
3496 | ||
3497 | out: | |
3498 | return ret; | |
3499 | } | |
3500 | ||
3501 | static int process_all_new_xattrs(struct send_ctx *sctx) | |
3502 | { | |
3503 | int ret; | |
3504 | struct btrfs_root *root; | |
3505 | struct btrfs_path *path; | |
3506 | struct btrfs_key key; | |
3507 | struct btrfs_key found_key; | |
3508 | struct extent_buffer *eb; | |
3509 | int slot; | |
3510 | ||
3511 | path = alloc_path_for_send(); | |
3512 | if (!path) | |
3513 | return -ENOMEM; | |
3514 | ||
3515 | root = sctx->send_root; | |
3516 | ||
3517 | key.objectid = sctx->cmp_key->objectid; | |
3518 | key.type = BTRFS_XATTR_ITEM_KEY; | |
3519 | key.offset = 0; | |
3520 | while (1) { | |
3521 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
3522 | if (ret < 0) | |
3523 | goto out; | |
3524 | if (ret) { | |
3525 | ret = 0; | |
3526 | goto out; | |
3527 | } | |
3528 | ||
3529 | eb = path->nodes[0]; | |
3530 | slot = path->slots[0]; | |
3531 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
3532 | ||
3533 | if (found_key.objectid != key.objectid || | |
3534 | found_key.type != key.type) { | |
3535 | ret = 0; | |
3536 | goto out; | |
3537 | } | |
3538 | ||
3539 | ret = iterate_dir_item(sctx, root, path, &found_key, | |
3540 | __process_new_xattr, sctx); | |
3541 | if (ret < 0) | |
3542 | goto out; | |
3543 | ||
3544 | btrfs_release_path(path); | |
3545 | key.offset = found_key.offset + 1; | |
3546 | } | |
3547 | ||
3548 | out: | |
3549 | btrfs_free_path(path); | |
3550 | return ret; | |
3551 | } | |
3552 | ||
3553 | /* | |
3554 | * Read some bytes from the current inode/file and send a write command to | |
3555 | * user space. | |
3556 | */ | |
3557 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) | |
3558 | { | |
3559 | int ret = 0; | |
3560 | struct fs_path *p; | |
3561 | loff_t pos = offset; | |
e938c8ad | 3562 | int num_read = 0; |
31db9f7c AB |
3563 | mm_segment_t old_fs; |
3564 | ||
3565 | p = fs_path_alloc(sctx); | |
3566 | if (!p) | |
3567 | return -ENOMEM; | |
3568 | ||
3569 | /* | |
3570 | * vfs normally only accepts user space buffers for security reasons. | |
3571 | * we only read from the file and also only provide the read_buf buffer | |
3572 | * to vfs. As this buffer does not come from a user space call, it's | |
3573 | * ok to temporary allow kernel space buffers. | |
3574 | */ | |
3575 | old_fs = get_fs(); | |
3576 | set_fs(KERNEL_DS); | |
3577 | ||
3578 | verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len); | |
3579 | ||
3580 | ret = open_cur_inode_file(sctx); | |
3581 | if (ret < 0) | |
3582 | goto out; | |
3583 | ||
3584 | ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos); | |
3585 | if (ret < 0) | |
3586 | goto out; | |
e938c8ad AB |
3587 | num_read = ret; |
3588 | if (!num_read) | |
31db9f7c AB |
3589 | goto out; |
3590 | ||
3591 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); | |
3592 | if (ret < 0) | |
3593 | goto out; | |
3594 | ||
3595 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3596 | if (ret < 0) | |
3597 | goto out; | |
3598 | ||
3599 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
3600 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
e938c8ad | 3601 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read); |
31db9f7c AB |
3602 | |
3603 | ret = send_cmd(sctx); | |
3604 | ||
3605 | tlv_put_failure: | |
3606 | out: | |
3607 | fs_path_free(sctx, p); | |
3608 | set_fs(old_fs); | |
3609 | if (ret < 0) | |
3610 | return ret; | |
e938c8ad | 3611 | return num_read; |
31db9f7c AB |
3612 | } |
3613 | ||
3614 | /* | |
3615 | * Send a clone command to user space. | |
3616 | */ | |
3617 | static int send_clone(struct send_ctx *sctx, | |
3618 | u64 offset, u32 len, | |
3619 | struct clone_root *clone_root) | |
3620 | { | |
3621 | int ret = 0; | |
31db9f7c AB |
3622 | struct fs_path *p; |
3623 | u64 gen; | |
3624 | ||
3625 | verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, " | |
3626 | "clone_inode=%llu, clone_offset=%llu\n", offset, len, | |
3627 | clone_root->root->objectid, clone_root->ino, | |
3628 | clone_root->offset); | |
3629 | ||
3630 | p = fs_path_alloc(sctx); | |
3631 | if (!p) | |
3632 | return -ENOMEM; | |
3633 | ||
3634 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); | |
3635 | if (ret < 0) | |
3636 | goto out; | |
3637 | ||
3638 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
3639 | if (ret < 0) | |
3640 | goto out; | |
3641 | ||
3642 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
3643 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); | |
3644 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
3645 | ||
e938c8ad | 3646 | if (clone_root->root == sctx->send_root) { |
31db9f7c | 3647 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
85a7b33b | 3648 | &gen, NULL, NULL, NULL, NULL); |
31db9f7c AB |
3649 | if (ret < 0) |
3650 | goto out; | |
3651 | ret = get_cur_path(sctx, clone_root->ino, gen, p); | |
3652 | } else { | |
e938c8ad AB |
3653 | ret = get_inode_path(sctx, clone_root->root, |
3654 | clone_root->ino, p); | |
31db9f7c AB |
3655 | } |
3656 | if (ret < 0) | |
3657 | goto out; | |
3658 | ||
3659 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
e938c8ad | 3660 | clone_root->root->root_item.uuid); |
31db9f7c | 3661 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
e938c8ad | 3662 | clone_root->root->root_item.ctransid); |
31db9f7c AB |
3663 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
3664 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, | |
3665 | clone_root->offset); | |
3666 | ||
3667 | ret = send_cmd(sctx); | |
3668 | ||
3669 | tlv_put_failure: | |
3670 | out: | |
3671 | fs_path_free(sctx, p); | |
3672 | return ret; | |
3673 | } | |
3674 | ||
3675 | static int send_write_or_clone(struct send_ctx *sctx, | |
3676 | struct btrfs_path *path, | |
3677 | struct btrfs_key *key, | |
3678 | struct clone_root *clone_root) | |
3679 | { | |
3680 | int ret = 0; | |
3681 | struct btrfs_file_extent_item *ei; | |
3682 | u64 offset = key->offset; | |
3683 | u64 pos = 0; | |
3684 | u64 len; | |
3685 | u32 l; | |
3686 | u8 type; | |
3687 | ||
3688 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3689 | struct btrfs_file_extent_item); | |
3690 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
74dd17fb | 3691 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
31db9f7c | 3692 | len = btrfs_file_extent_inline_len(path->nodes[0], ei); |
74dd17fb CM |
3693 | /* |
3694 | * it is possible the inline item won't cover the whole page, | |
3695 | * but there may be items after this page. Make | |
3696 | * sure to send the whole thing | |
3697 | */ | |
3698 | len = PAGE_CACHE_ALIGN(len); | |
3699 | } else { | |
31db9f7c | 3700 | len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
74dd17fb | 3701 | } |
31db9f7c AB |
3702 | |
3703 | if (offset + len > sctx->cur_inode_size) | |
3704 | len = sctx->cur_inode_size - offset; | |
3705 | if (len == 0) { | |
3706 | ret = 0; | |
3707 | goto out; | |
3708 | } | |
3709 | ||
3710 | if (!clone_root) { | |
3711 | while (pos < len) { | |
3712 | l = len - pos; | |
3713 | if (l > BTRFS_SEND_READ_SIZE) | |
3714 | l = BTRFS_SEND_READ_SIZE; | |
3715 | ret = send_write(sctx, pos + offset, l); | |
3716 | if (ret < 0) | |
3717 | goto out; | |
3718 | if (!ret) | |
3719 | break; | |
3720 | pos += ret; | |
3721 | } | |
3722 | ret = 0; | |
3723 | } else { | |
3724 | ret = send_clone(sctx, offset, len, clone_root); | |
3725 | } | |
3726 | ||
3727 | out: | |
3728 | return ret; | |
3729 | } | |
3730 | ||
3731 | static int is_extent_unchanged(struct send_ctx *sctx, | |
3732 | struct btrfs_path *left_path, | |
3733 | struct btrfs_key *ekey) | |
3734 | { | |
3735 | int ret = 0; | |
3736 | struct btrfs_key key; | |
3737 | struct btrfs_path *path = NULL; | |
3738 | struct extent_buffer *eb; | |
3739 | int slot; | |
3740 | struct btrfs_key found_key; | |
3741 | struct btrfs_file_extent_item *ei; | |
3742 | u64 left_disknr; | |
3743 | u64 right_disknr; | |
3744 | u64 left_offset; | |
3745 | u64 right_offset; | |
3746 | u64 left_offset_fixed; | |
3747 | u64 left_len; | |
3748 | u64 right_len; | |
74dd17fb CM |
3749 | u64 left_gen; |
3750 | u64 right_gen; | |
31db9f7c AB |
3751 | u8 left_type; |
3752 | u8 right_type; | |
3753 | ||
3754 | path = alloc_path_for_send(); | |
3755 | if (!path) | |
3756 | return -ENOMEM; | |
3757 | ||
3758 | eb = left_path->nodes[0]; | |
3759 | slot = left_path->slots[0]; | |
31db9f7c AB |
3760 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
3761 | left_type = btrfs_file_extent_type(eb, ei); | |
31db9f7c AB |
3762 | |
3763 | if (left_type != BTRFS_FILE_EXTENT_REG) { | |
3764 | ret = 0; | |
3765 | goto out; | |
3766 | } | |
74dd17fb CM |
3767 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
3768 | left_len = btrfs_file_extent_num_bytes(eb, ei); | |
3769 | left_offset = btrfs_file_extent_offset(eb, ei); | |
3770 | left_gen = btrfs_file_extent_generation(eb, ei); | |
31db9f7c AB |
3771 | |
3772 | /* | |
3773 | * Following comments will refer to these graphics. L is the left | |
3774 | * extents which we are checking at the moment. 1-8 are the right | |
3775 | * extents that we iterate. | |
3776 | * | |
3777 | * |-----L-----| | |
3778 | * |-1-|-2a-|-3-|-4-|-5-|-6-| | |
3779 | * | |
3780 | * |-----L-----| | |
3781 | * |--1--|-2b-|...(same as above) | |
3782 | * | |
3783 | * Alternative situation. Happens on files where extents got split. | |
3784 | * |-----L-----| | |
3785 | * |-----------7-----------|-6-| | |
3786 | * | |
3787 | * Alternative situation. Happens on files which got larger. | |
3788 | * |-----L-----| | |
3789 | * |-8-| | |
3790 | * Nothing follows after 8. | |
3791 | */ | |
3792 | ||
3793 | key.objectid = ekey->objectid; | |
3794 | key.type = BTRFS_EXTENT_DATA_KEY; | |
3795 | key.offset = ekey->offset; | |
3796 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); | |
3797 | if (ret < 0) | |
3798 | goto out; | |
3799 | if (ret) { | |
3800 | ret = 0; | |
3801 | goto out; | |
3802 | } | |
3803 | ||
3804 | /* | |
3805 | * Handle special case where the right side has no extents at all. | |
3806 | */ | |
3807 | eb = path->nodes[0]; | |
3808 | slot = path->slots[0]; | |
3809 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
3810 | if (found_key.objectid != key.objectid || | |
3811 | found_key.type != key.type) { | |
3812 | ret = 0; | |
3813 | goto out; | |
3814 | } | |
3815 | ||
3816 | /* | |
3817 | * We're now on 2a, 2b or 7. | |
3818 | */ | |
3819 | key = found_key; | |
3820 | while (key.offset < ekey->offset + left_len) { | |
3821 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
3822 | right_type = btrfs_file_extent_type(eb, ei); | |
3823 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); | |
3824 | right_len = btrfs_file_extent_num_bytes(eb, ei); | |
3825 | right_offset = btrfs_file_extent_offset(eb, ei); | |
74dd17fb | 3826 | right_gen = btrfs_file_extent_generation(eb, ei); |
31db9f7c AB |
3827 | |
3828 | if (right_type != BTRFS_FILE_EXTENT_REG) { | |
3829 | ret = 0; | |
3830 | goto out; | |
3831 | } | |
3832 | ||
3833 | /* | |
3834 | * Are we at extent 8? If yes, we know the extent is changed. | |
3835 | * This may only happen on the first iteration. | |
3836 | */ | |
d8347fa4 | 3837 | if (found_key.offset + right_len <= ekey->offset) { |
31db9f7c AB |
3838 | ret = 0; |
3839 | goto out; | |
3840 | } | |
3841 | ||
3842 | left_offset_fixed = left_offset; | |
3843 | if (key.offset < ekey->offset) { | |
3844 | /* Fix the right offset for 2a and 7. */ | |
3845 | right_offset += ekey->offset - key.offset; | |
3846 | } else { | |
3847 | /* Fix the left offset for all behind 2a and 2b */ | |
3848 | left_offset_fixed += key.offset - ekey->offset; | |
3849 | } | |
3850 | ||
3851 | /* | |
3852 | * Check if we have the same extent. | |
3853 | */ | |
3954096d | 3854 | if (left_disknr != right_disknr || |
74dd17fb CM |
3855 | left_offset_fixed != right_offset || |
3856 | left_gen != right_gen) { | |
31db9f7c AB |
3857 | ret = 0; |
3858 | goto out; | |
3859 | } | |
3860 | ||
3861 | /* | |
3862 | * Go to the next extent. | |
3863 | */ | |
3864 | ret = btrfs_next_item(sctx->parent_root, path); | |
3865 | if (ret < 0) | |
3866 | goto out; | |
3867 | if (!ret) { | |
3868 | eb = path->nodes[0]; | |
3869 | slot = path->slots[0]; | |
3870 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
3871 | } | |
3872 | if (ret || found_key.objectid != key.objectid || | |
3873 | found_key.type != key.type) { | |
3874 | key.offset += right_len; | |
3875 | break; | |
3876 | } else { | |
3877 | if (found_key.offset != key.offset + right_len) { | |
3878 | /* Should really not happen */ | |
3879 | ret = -EIO; | |
3880 | goto out; | |
3881 | } | |
3882 | } | |
3883 | key = found_key; | |
3884 | } | |
3885 | ||
3886 | /* | |
3887 | * We're now behind the left extent (treat as unchanged) or at the end | |
3888 | * of the right side (treat as changed). | |
3889 | */ | |
3890 | if (key.offset >= ekey->offset + left_len) | |
3891 | ret = 1; | |
3892 | else | |
3893 | ret = 0; | |
3894 | ||
3895 | ||
3896 | out: | |
3897 | btrfs_free_path(path); | |
3898 | return ret; | |
3899 | } | |
3900 | ||
3901 | static int process_extent(struct send_ctx *sctx, | |
3902 | struct btrfs_path *path, | |
3903 | struct btrfs_key *key) | |
3904 | { | |
3905 | int ret = 0; | |
3906 | struct clone_root *found_clone = NULL; | |
3907 | ||
3908 | if (S_ISLNK(sctx->cur_inode_mode)) | |
3909 | return 0; | |
3910 | ||
3911 | if (sctx->parent_root && !sctx->cur_inode_new) { | |
3912 | ret = is_extent_unchanged(sctx, path, key); | |
3913 | if (ret < 0) | |
3914 | goto out; | |
3915 | if (ret) { | |
3916 | ret = 0; | |
3917 | goto out; | |
3918 | } | |
3919 | } | |
3920 | ||
3921 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, | |
3922 | sctx->cur_inode_size, &found_clone); | |
3923 | if (ret != -ENOENT && ret < 0) | |
3924 | goto out; | |
3925 | ||
3926 | ret = send_write_or_clone(sctx, path, key, found_clone); | |
3927 | ||
3928 | out: | |
3929 | return ret; | |
3930 | } | |
3931 | ||
3932 | static int process_all_extents(struct send_ctx *sctx) | |
3933 | { | |
3934 | int ret; | |
3935 | struct btrfs_root *root; | |
3936 | struct btrfs_path *path; | |
3937 | struct btrfs_key key; | |
3938 | struct btrfs_key found_key; | |
3939 | struct extent_buffer *eb; | |
3940 | int slot; | |
3941 | ||
3942 | root = sctx->send_root; | |
3943 | path = alloc_path_for_send(); | |
3944 | if (!path) | |
3945 | return -ENOMEM; | |
3946 | ||
3947 | key.objectid = sctx->cmp_key->objectid; | |
3948 | key.type = BTRFS_EXTENT_DATA_KEY; | |
3949 | key.offset = 0; | |
3950 | while (1) { | |
3951 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
3952 | if (ret < 0) | |
3953 | goto out; | |
3954 | if (ret) { | |
3955 | ret = 0; | |
3956 | goto out; | |
3957 | } | |
3958 | ||
3959 | eb = path->nodes[0]; | |
3960 | slot = path->slots[0]; | |
3961 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
3962 | ||
3963 | if (found_key.objectid != key.objectid || | |
3964 | found_key.type != key.type) { | |
3965 | ret = 0; | |
3966 | goto out; | |
3967 | } | |
3968 | ||
3969 | ret = process_extent(sctx, path, &found_key); | |
3970 | if (ret < 0) | |
3971 | goto out; | |
3972 | ||
3973 | btrfs_release_path(path); | |
3974 | key.offset = found_key.offset + 1; | |
3975 | } | |
3976 | ||
3977 | out: | |
3978 | btrfs_free_path(path); | |
3979 | return ret; | |
3980 | } | |
3981 | ||
3982 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end) | |
3983 | { | |
3984 | int ret = 0; | |
3985 | ||
3986 | if (sctx->cur_ino == 0) | |
3987 | goto out; | |
3988 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && | |
3989 | sctx->cmp_key->type <= BTRFS_INODE_REF_KEY) | |
3990 | goto out; | |
3991 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) | |
3992 | goto out; | |
3993 | ||
3994 | ret = process_recorded_refs(sctx); | |
e479d9bb AB |
3995 | if (ret < 0) |
3996 | goto out; | |
3997 | ||
3998 | /* | |
3999 | * We have processed the refs and thus need to advance send_progress. | |
4000 | * Now, calls to get_cur_xxx will take the updated refs of the current | |
4001 | * inode into account. | |
4002 | */ | |
4003 | sctx->send_progress = sctx->cur_ino + 1; | |
31db9f7c AB |
4004 | |
4005 | out: | |
4006 | return ret; | |
4007 | } | |
4008 | ||
4009 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) | |
4010 | { | |
4011 | int ret = 0; | |
4012 | u64 left_mode; | |
4013 | u64 left_uid; | |
4014 | u64 left_gid; | |
4015 | u64 right_mode; | |
4016 | u64 right_uid; | |
4017 | u64 right_gid; | |
4018 | int need_chmod = 0; | |
4019 | int need_chown = 0; | |
4020 | ||
4021 | ret = process_recorded_refs_if_needed(sctx, at_end); | |
4022 | if (ret < 0) | |
4023 | goto out; | |
4024 | ||
4025 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) | |
4026 | goto out; | |
4027 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) | |
4028 | goto out; | |
4029 | ||
4030 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, | |
85a7b33b | 4031 | &left_mode, &left_uid, &left_gid, NULL); |
31db9f7c AB |
4032 | if (ret < 0) |
4033 | goto out; | |
4034 | ||
4035 | if (!S_ISLNK(sctx->cur_inode_mode)) { | |
4036 | if (!sctx->parent_root || sctx->cur_inode_new) { | |
4037 | need_chmod = 1; | |
4038 | need_chown = 1; | |
4039 | } else { | |
4040 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, | |
4041 | NULL, NULL, &right_mode, &right_uid, | |
85a7b33b | 4042 | &right_gid, NULL); |
31db9f7c AB |
4043 | if (ret < 0) |
4044 | goto out; | |
4045 | ||
4046 | if (left_uid != right_uid || left_gid != right_gid) | |
4047 | need_chown = 1; | |
4048 | if (left_mode != right_mode) | |
4049 | need_chmod = 1; | |
4050 | } | |
4051 | } | |
4052 | ||
4053 | if (S_ISREG(sctx->cur_inode_mode)) { | |
4054 | ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
4055 | sctx->cur_inode_size); | |
4056 | if (ret < 0) | |
4057 | goto out; | |
4058 | } | |
4059 | ||
4060 | if (need_chown) { | |
4061 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
4062 | left_uid, left_gid); | |
4063 | if (ret < 0) | |
4064 | goto out; | |
4065 | } | |
4066 | if (need_chmod) { | |
4067 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
4068 | left_mode); | |
4069 | if (ret < 0) | |
4070 | goto out; | |
4071 | } | |
4072 | ||
4073 | /* | |
4074 | * Need to send that every time, no matter if it actually changed | |
4075 | * between the two trees as we have done changes to the inode before. | |
4076 | */ | |
4077 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); | |
4078 | if (ret < 0) | |
4079 | goto out; | |
4080 | ||
4081 | out: | |
4082 | return ret; | |
4083 | } | |
4084 | ||
4085 | static int changed_inode(struct send_ctx *sctx, | |
4086 | enum btrfs_compare_tree_result result) | |
4087 | { | |
4088 | int ret = 0; | |
4089 | struct btrfs_key *key = sctx->cmp_key; | |
4090 | struct btrfs_inode_item *left_ii = NULL; | |
4091 | struct btrfs_inode_item *right_ii = NULL; | |
4092 | u64 left_gen = 0; | |
4093 | u64 right_gen = 0; | |
4094 | ||
4095 | ret = close_cur_inode_file(sctx); | |
4096 | if (ret < 0) | |
4097 | goto out; | |
4098 | ||
4099 | sctx->cur_ino = key->objectid; | |
4100 | sctx->cur_inode_new_gen = 0; | |
e479d9bb AB |
4101 | |
4102 | /* | |
4103 | * Set send_progress to current inode. This will tell all get_cur_xxx | |
4104 | * functions that the current inode's refs are not updated yet. Later, | |
4105 | * when process_recorded_refs is finished, it is set to cur_ino + 1. | |
4106 | */ | |
31db9f7c AB |
4107 | sctx->send_progress = sctx->cur_ino; |
4108 | ||
4109 | if (result == BTRFS_COMPARE_TREE_NEW || | |
4110 | result == BTRFS_COMPARE_TREE_CHANGED) { | |
4111 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], | |
4112 | sctx->left_path->slots[0], | |
4113 | struct btrfs_inode_item); | |
4114 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], | |
4115 | left_ii); | |
4116 | } else { | |
4117 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
4118 | sctx->right_path->slots[0], | |
4119 | struct btrfs_inode_item); | |
4120 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
4121 | right_ii); | |
4122 | } | |
4123 | if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
4124 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
4125 | sctx->right_path->slots[0], | |
4126 | struct btrfs_inode_item); | |
4127 | ||
4128 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
4129 | right_ii); | |
6d85ed05 AB |
4130 | |
4131 | /* | |
4132 | * The cur_ino = root dir case is special here. We can't treat | |
4133 | * the inode as deleted+reused because it would generate a | |
4134 | * stream that tries to delete/mkdir the root dir. | |
4135 | */ | |
4136 | if (left_gen != right_gen && | |
4137 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) | |
31db9f7c AB |
4138 | sctx->cur_inode_new_gen = 1; |
4139 | } | |
4140 | ||
4141 | if (result == BTRFS_COMPARE_TREE_NEW) { | |
4142 | sctx->cur_inode_gen = left_gen; | |
4143 | sctx->cur_inode_new = 1; | |
4144 | sctx->cur_inode_deleted = 0; | |
4145 | sctx->cur_inode_size = btrfs_inode_size( | |
4146 | sctx->left_path->nodes[0], left_ii); | |
4147 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4148 | sctx->left_path->nodes[0], left_ii); | |
4149 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) | |
1f4692da | 4150 | ret = send_create_inode_if_needed(sctx); |
31db9f7c AB |
4151 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
4152 | sctx->cur_inode_gen = right_gen; | |
4153 | sctx->cur_inode_new = 0; | |
4154 | sctx->cur_inode_deleted = 1; | |
4155 | sctx->cur_inode_size = btrfs_inode_size( | |
4156 | sctx->right_path->nodes[0], right_ii); | |
4157 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4158 | sctx->right_path->nodes[0], right_ii); | |
4159 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
766702ef AB |
4160 | /* |
4161 | * We need to do some special handling in case the inode was | |
4162 | * reported as changed with a changed generation number. This | |
4163 | * means that the original inode was deleted and new inode | |
4164 | * reused the same inum. So we have to treat the old inode as | |
4165 | * deleted and the new one as new. | |
4166 | */ | |
31db9f7c | 4167 | if (sctx->cur_inode_new_gen) { |
766702ef AB |
4168 | /* |
4169 | * First, process the inode as if it was deleted. | |
4170 | */ | |
31db9f7c AB |
4171 | sctx->cur_inode_gen = right_gen; |
4172 | sctx->cur_inode_new = 0; | |
4173 | sctx->cur_inode_deleted = 1; | |
4174 | sctx->cur_inode_size = btrfs_inode_size( | |
4175 | sctx->right_path->nodes[0], right_ii); | |
4176 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4177 | sctx->right_path->nodes[0], right_ii); | |
4178 | ret = process_all_refs(sctx, | |
4179 | BTRFS_COMPARE_TREE_DELETED); | |
4180 | if (ret < 0) | |
4181 | goto out; | |
4182 | ||
766702ef AB |
4183 | /* |
4184 | * Now process the inode as if it was new. | |
4185 | */ | |
31db9f7c AB |
4186 | sctx->cur_inode_gen = left_gen; |
4187 | sctx->cur_inode_new = 1; | |
4188 | sctx->cur_inode_deleted = 0; | |
4189 | sctx->cur_inode_size = btrfs_inode_size( | |
4190 | sctx->left_path->nodes[0], left_ii); | |
4191 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4192 | sctx->left_path->nodes[0], left_ii); | |
1f4692da | 4193 | ret = send_create_inode_if_needed(sctx); |
31db9f7c AB |
4194 | if (ret < 0) |
4195 | goto out; | |
4196 | ||
4197 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); | |
4198 | if (ret < 0) | |
4199 | goto out; | |
e479d9bb AB |
4200 | /* |
4201 | * Advance send_progress now as we did not get into | |
4202 | * process_recorded_refs_if_needed in the new_gen case. | |
4203 | */ | |
4204 | sctx->send_progress = sctx->cur_ino + 1; | |
766702ef AB |
4205 | |
4206 | /* | |
4207 | * Now process all extents and xattrs of the inode as if | |
4208 | * they were all new. | |
4209 | */ | |
31db9f7c AB |
4210 | ret = process_all_extents(sctx); |
4211 | if (ret < 0) | |
4212 | goto out; | |
4213 | ret = process_all_new_xattrs(sctx); | |
4214 | if (ret < 0) | |
4215 | goto out; | |
4216 | } else { | |
4217 | sctx->cur_inode_gen = left_gen; | |
4218 | sctx->cur_inode_new = 0; | |
4219 | sctx->cur_inode_new_gen = 0; | |
4220 | sctx->cur_inode_deleted = 0; | |
4221 | sctx->cur_inode_size = btrfs_inode_size( | |
4222 | sctx->left_path->nodes[0], left_ii); | |
4223 | sctx->cur_inode_mode = btrfs_inode_mode( | |
4224 | sctx->left_path->nodes[0], left_ii); | |
4225 | } | |
4226 | } | |
4227 | ||
4228 | out: | |
4229 | return ret; | |
4230 | } | |
4231 | ||
766702ef AB |
4232 | /* |
4233 | * We have to process new refs before deleted refs, but compare_trees gives us | |
4234 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs | |
4235 | * first and later process them in process_recorded_refs. | |
4236 | * For the cur_inode_new_gen case, we skip recording completely because | |
4237 | * changed_inode did already initiate processing of refs. The reason for this is | |
4238 | * that in this case, compare_tree actually compares the refs of 2 different | |
4239 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all | |
4240 | * refs of the right tree as deleted and all refs of the left tree as new. | |
4241 | */ | |
31db9f7c AB |
4242 | static int changed_ref(struct send_ctx *sctx, |
4243 | enum btrfs_compare_tree_result result) | |
4244 | { | |
4245 | int ret = 0; | |
4246 | ||
4247 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
4248 | ||
4249 | if (!sctx->cur_inode_new_gen && | |
4250 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
4251 | if (result == BTRFS_COMPARE_TREE_NEW) | |
4252 | ret = record_new_ref(sctx); | |
4253 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
4254 | ret = record_deleted_ref(sctx); | |
4255 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
4256 | ret = record_changed_ref(sctx); | |
4257 | } | |
4258 | ||
4259 | return ret; | |
4260 | } | |
4261 | ||
766702ef AB |
4262 | /* |
4263 | * Process new/deleted/changed xattrs. We skip processing in the | |
4264 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
4265 | * of xattrs. The reason is the same as in changed_ref | |
4266 | */ | |
31db9f7c AB |
4267 | static int changed_xattr(struct send_ctx *sctx, |
4268 | enum btrfs_compare_tree_result result) | |
4269 | { | |
4270 | int ret = 0; | |
4271 | ||
4272 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
4273 | ||
4274 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
4275 | if (result == BTRFS_COMPARE_TREE_NEW) | |
4276 | ret = process_new_xattr(sctx); | |
4277 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
4278 | ret = process_deleted_xattr(sctx); | |
4279 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
4280 | ret = process_changed_xattr(sctx); | |
4281 | } | |
4282 | ||
4283 | return ret; | |
4284 | } | |
4285 | ||
766702ef AB |
4286 | /* |
4287 | * Process new/deleted/changed extents. We skip processing in the | |
4288 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
4289 | * of extents. The reason is the same as in changed_ref | |
4290 | */ | |
31db9f7c AB |
4291 | static int changed_extent(struct send_ctx *sctx, |
4292 | enum btrfs_compare_tree_result result) | |
4293 | { | |
4294 | int ret = 0; | |
4295 | ||
4296 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); | |
4297 | ||
4298 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
4299 | if (result != BTRFS_COMPARE_TREE_DELETED) | |
4300 | ret = process_extent(sctx, sctx->left_path, | |
4301 | sctx->cmp_key); | |
4302 | } | |
4303 | ||
4304 | return ret; | |
4305 | } | |
4306 | ||
766702ef AB |
4307 | /* |
4308 | * Updates compare related fields in sctx and simply forwards to the actual | |
4309 | * changed_xxx functions. | |
4310 | */ | |
31db9f7c AB |
4311 | static int changed_cb(struct btrfs_root *left_root, |
4312 | struct btrfs_root *right_root, | |
4313 | struct btrfs_path *left_path, | |
4314 | struct btrfs_path *right_path, | |
4315 | struct btrfs_key *key, | |
4316 | enum btrfs_compare_tree_result result, | |
4317 | void *ctx) | |
4318 | { | |
4319 | int ret = 0; | |
4320 | struct send_ctx *sctx = ctx; | |
4321 | ||
4322 | sctx->left_path = left_path; | |
4323 | sctx->right_path = right_path; | |
4324 | sctx->cmp_key = key; | |
4325 | ||
4326 | ret = finish_inode_if_needed(sctx, 0); | |
4327 | if (ret < 0) | |
4328 | goto out; | |
4329 | ||
2981e225 AB |
4330 | /* Ignore non-FS objects */ |
4331 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || | |
4332 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) | |
4333 | goto out; | |
4334 | ||
31db9f7c AB |
4335 | if (key->type == BTRFS_INODE_ITEM_KEY) |
4336 | ret = changed_inode(sctx, result); | |
4337 | else if (key->type == BTRFS_INODE_REF_KEY) | |
4338 | ret = changed_ref(sctx, result); | |
4339 | else if (key->type == BTRFS_XATTR_ITEM_KEY) | |
4340 | ret = changed_xattr(sctx, result); | |
4341 | else if (key->type == BTRFS_EXTENT_DATA_KEY) | |
4342 | ret = changed_extent(sctx, result); | |
4343 | ||
4344 | out: | |
4345 | return ret; | |
4346 | } | |
4347 | ||
4348 | static int full_send_tree(struct send_ctx *sctx) | |
4349 | { | |
4350 | int ret; | |
4351 | struct btrfs_trans_handle *trans = NULL; | |
4352 | struct btrfs_root *send_root = sctx->send_root; | |
4353 | struct btrfs_key key; | |
4354 | struct btrfs_key found_key; | |
4355 | struct btrfs_path *path; | |
4356 | struct extent_buffer *eb; | |
4357 | int slot; | |
4358 | u64 start_ctransid; | |
4359 | u64 ctransid; | |
4360 | ||
4361 | path = alloc_path_for_send(); | |
4362 | if (!path) | |
4363 | return -ENOMEM; | |
4364 | ||
4365 | spin_lock(&send_root->root_times_lock); | |
4366 | start_ctransid = btrfs_root_ctransid(&send_root->root_item); | |
4367 | spin_unlock(&send_root->root_times_lock); | |
4368 | ||
4369 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; | |
4370 | key.type = BTRFS_INODE_ITEM_KEY; | |
4371 | key.offset = 0; | |
4372 | ||
4373 | join_trans: | |
4374 | /* | |
4375 | * We need to make sure the transaction does not get committed | |
4376 | * while we do anything on commit roots. Join a transaction to prevent | |
4377 | * this. | |
4378 | */ | |
4379 | trans = btrfs_join_transaction(send_root); | |
4380 | if (IS_ERR(trans)) { | |
4381 | ret = PTR_ERR(trans); | |
4382 | trans = NULL; | |
4383 | goto out; | |
4384 | } | |
4385 | ||
4386 | /* | |
766702ef AB |
4387 | * Make sure the tree has not changed after re-joining. We detect this |
4388 | * by comparing start_ctransid and ctransid. They should always match. | |
31db9f7c AB |
4389 | */ |
4390 | spin_lock(&send_root->root_times_lock); | |
4391 | ctransid = btrfs_root_ctransid(&send_root->root_item); | |
4392 | spin_unlock(&send_root->root_times_lock); | |
4393 | ||
4394 | if (ctransid != start_ctransid) { | |
4395 | WARN(1, KERN_WARNING "btrfs: the root that you're trying to " | |
4396 | "send was modified in between. This is " | |
4397 | "probably a bug.\n"); | |
4398 | ret = -EIO; | |
4399 | goto out; | |
4400 | } | |
4401 | ||
4402 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); | |
4403 | if (ret < 0) | |
4404 | goto out; | |
4405 | if (ret) | |
4406 | goto out_finish; | |
4407 | ||
4408 | while (1) { | |
4409 | /* | |
4410 | * When someone want to commit while we iterate, end the | |
4411 | * joined transaction and rejoin. | |
4412 | */ | |
4413 | if (btrfs_should_end_transaction(trans, send_root)) { | |
4414 | ret = btrfs_end_transaction(trans, send_root); | |
4415 | trans = NULL; | |
4416 | if (ret < 0) | |
4417 | goto out; | |
4418 | btrfs_release_path(path); | |
4419 | goto join_trans; | |
4420 | } | |
4421 | ||
4422 | eb = path->nodes[0]; | |
4423 | slot = path->slots[0]; | |
4424 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
4425 | ||
4426 | ret = changed_cb(send_root, NULL, path, NULL, | |
4427 | &found_key, BTRFS_COMPARE_TREE_NEW, sctx); | |
4428 | if (ret < 0) | |
4429 | goto out; | |
4430 | ||
4431 | key.objectid = found_key.objectid; | |
4432 | key.type = found_key.type; | |
4433 | key.offset = found_key.offset + 1; | |
4434 | ||
4435 | ret = btrfs_next_item(send_root, path); | |
4436 | if (ret < 0) | |
4437 | goto out; | |
4438 | if (ret) { | |
4439 | ret = 0; | |
4440 | break; | |
4441 | } | |
4442 | } | |
4443 | ||
4444 | out_finish: | |
4445 | ret = finish_inode_if_needed(sctx, 1); | |
4446 | ||
4447 | out: | |
4448 | btrfs_free_path(path); | |
4449 | if (trans) { | |
4450 | if (!ret) | |
4451 | ret = btrfs_end_transaction(trans, send_root); | |
4452 | else | |
4453 | btrfs_end_transaction(trans, send_root); | |
4454 | } | |
4455 | return ret; | |
4456 | } | |
4457 | ||
4458 | static int send_subvol(struct send_ctx *sctx) | |
4459 | { | |
4460 | int ret; | |
4461 | ||
4462 | ret = send_header(sctx); | |
4463 | if (ret < 0) | |
4464 | goto out; | |
4465 | ||
4466 | ret = send_subvol_begin(sctx); | |
4467 | if (ret < 0) | |
4468 | goto out; | |
4469 | ||
4470 | if (sctx->parent_root) { | |
4471 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, | |
4472 | changed_cb, sctx); | |
4473 | if (ret < 0) | |
4474 | goto out; | |
4475 | ret = finish_inode_if_needed(sctx, 1); | |
4476 | if (ret < 0) | |
4477 | goto out; | |
4478 | } else { | |
4479 | ret = full_send_tree(sctx); | |
4480 | if (ret < 0) | |
4481 | goto out; | |
4482 | } | |
4483 | ||
4484 | out: | |
4485 | if (!ret) | |
4486 | ret = close_cur_inode_file(sctx); | |
4487 | else | |
4488 | close_cur_inode_file(sctx); | |
4489 | ||
4490 | free_recorded_refs(sctx); | |
4491 | return ret; | |
4492 | } | |
4493 | ||
4494 | long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) | |
4495 | { | |
4496 | int ret = 0; | |
4497 | struct btrfs_root *send_root; | |
4498 | struct btrfs_root *clone_root; | |
4499 | struct btrfs_fs_info *fs_info; | |
4500 | struct btrfs_ioctl_send_args *arg = NULL; | |
4501 | struct btrfs_key key; | |
4502 | struct file *filp = NULL; | |
4503 | struct send_ctx *sctx = NULL; | |
4504 | u32 i; | |
4505 | u64 *clone_sources_tmp = NULL; | |
4506 | ||
4507 | if (!capable(CAP_SYS_ADMIN)) | |
4508 | return -EPERM; | |
4509 | ||
4510 | send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root; | |
4511 | fs_info = send_root->fs_info; | |
4512 | ||
4513 | arg = memdup_user(arg_, sizeof(*arg)); | |
4514 | if (IS_ERR(arg)) { | |
4515 | ret = PTR_ERR(arg); | |
4516 | arg = NULL; | |
4517 | goto out; | |
4518 | } | |
4519 | ||
4520 | if (!access_ok(VERIFY_READ, arg->clone_sources, | |
4521 | sizeof(*arg->clone_sources * | |
4522 | arg->clone_sources_count))) { | |
4523 | ret = -EFAULT; | |
4524 | goto out; | |
4525 | } | |
4526 | ||
4527 | sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS); | |
4528 | if (!sctx) { | |
4529 | ret = -ENOMEM; | |
4530 | goto out; | |
4531 | } | |
4532 | ||
4533 | INIT_LIST_HEAD(&sctx->new_refs); | |
4534 | INIT_LIST_HEAD(&sctx->deleted_refs); | |
4535 | INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS); | |
4536 | INIT_LIST_HEAD(&sctx->name_cache_list); | |
4537 | ||
4538 | sctx->send_filp = fget(arg->send_fd); | |
4539 | if (IS_ERR(sctx->send_filp)) { | |
4540 | ret = PTR_ERR(sctx->send_filp); | |
4541 | goto out; | |
4542 | } | |
4543 | ||
4544 | sctx->mnt = mnt_file->f_path.mnt; | |
4545 | ||
4546 | sctx->send_root = send_root; | |
4547 | sctx->clone_roots_cnt = arg->clone_sources_count; | |
4548 | ||
4549 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; | |
4550 | sctx->send_buf = vmalloc(sctx->send_max_size); | |
4551 | if (!sctx->send_buf) { | |
4552 | ret = -ENOMEM; | |
4553 | goto out; | |
4554 | } | |
4555 | ||
4556 | sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE); | |
4557 | if (!sctx->read_buf) { | |
4558 | ret = -ENOMEM; | |
4559 | goto out; | |
4560 | } | |
4561 | ||
4562 | sctx->clone_roots = vzalloc(sizeof(struct clone_root) * | |
4563 | (arg->clone_sources_count + 1)); | |
4564 | if (!sctx->clone_roots) { | |
4565 | ret = -ENOMEM; | |
4566 | goto out; | |
4567 | } | |
4568 | ||
4569 | if (arg->clone_sources_count) { | |
4570 | clone_sources_tmp = vmalloc(arg->clone_sources_count * | |
4571 | sizeof(*arg->clone_sources)); | |
4572 | if (!clone_sources_tmp) { | |
4573 | ret = -ENOMEM; | |
4574 | goto out; | |
4575 | } | |
4576 | ||
4577 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, | |
4578 | arg->clone_sources_count * | |
4579 | sizeof(*arg->clone_sources)); | |
4580 | if (ret) { | |
4581 | ret = -EFAULT; | |
4582 | goto out; | |
4583 | } | |
4584 | ||
4585 | for (i = 0; i < arg->clone_sources_count; i++) { | |
4586 | key.objectid = clone_sources_tmp[i]; | |
4587 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4588 | key.offset = (u64)-1; | |
4589 | clone_root = btrfs_read_fs_root_no_name(fs_info, &key); | |
4590 | if (!clone_root) { | |
4591 | ret = -EINVAL; | |
4592 | goto out; | |
4593 | } | |
4594 | if (IS_ERR(clone_root)) { | |
4595 | ret = PTR_ERR(clone_root); | |
4596 | goto out; | |
4597 | } | |
4598 | sctx->clone_roots[i].root = clone_root; | |
4599 | } | |
4600 | vfree(clone_sources_tmp); | |
4601 | clone_sources_tmp = NULL; | |
4602 | } | |
4603 | ||
4604 | if (arg->parent_root) { | |
4605 | key.objectid = arg->parent_root; | |
4606 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4607 | key.offset = (u64)-1; | |
4608 | sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key); | |
4609 | if (!sctx->parent_root) { | |
4610 | ret = -EINVAL; | |
4611 | goto out; | |
4612 | } | |
4613 | } | |
4614 | ||
4615 | /* | |
4616 | * Clones from send_root are allowed, but only if the clone source | |
4617 | * is behind the current send position. This is checked while searching | |
4618 | * for possible clone sources. | |
4619 | */ | |
4620 | sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root; | |
4621 | ||
4622 | /* We do a bsearch later */ | |
4623 | sort(sctx->clone_roots, sctx->clone_roots_cnt, | |
4624 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, | |
4625 | NULL); | |
4626 | ||
4627 | ret = send_subvol(sctx); | |
4628 | if (ret < 0) | |
4629 | goto out; | |
4630 | ||
4631 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); | |
4632 | if (ret < 0) | |
4633 | goto out; | |
4634 | ret = send_cmd(sctx); | |
4635 | if (ret < 0) | |
4636 | goto out; | |
4637 | ||
4638 | out: | |
4639 | if (filp) | |
4640 | fput(filp); | |
4641 | kfree(arg); | |
4642 | vfree(clone_sources_tmp); | |
4643 | ||
4644 | if (sctx) { | |
4645 | if (sctx->send_filp) | |
4646 | fput(sctx->send_filp); | |
4647 | ||
4648 | vfree(sctx->clone_roots); | |
4649 | vfree(sctx->send_buf); | |
4650 | vfree(sctx->read_buf); | |
4651 | ||
4652 | name_cache_free(sctx); | |
4653 | ||
4654 | kfree(sctx); | |
4655 | } | |
4656 | ||
4657 | return ret; | |
4658 | } |