btrfs-show-super: don't try to print not-superblocks
[platform/upstream/btrfs-progs.git] / cmds-receive.c
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 #define _GNU_SOURCE
20 #define _POSIX_C_SOURCE 200809
21 #define _XOPEN_SOURCE 700
22 #define _BSD_SOURCE
23
24 #include "kerncompat.h"
25
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <pthread.h>
31 #include <math.h>
32 #include <ftw.h>
33 #include <wait.h>
34 #include <assert.h>
35
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <sys/xattr.h>
42 #include <uuid/uuid.h>
43
44 #include "ctree.h"
45 #include "ioctl.h"
46 #include "commands.h"
47 #include "utils.h"
48 #include "list.h"
49 #include "btrfs-list.h"
50
51 #include "send.h"
52 #include "send-stream.h"
53 #include "send-utils.h"
54
55 static int g_verbose = 0;
56
57 struct btrfs_receive
58 {
59         int mnt_fd;
60         int dest_dir_fd;
61
62         int write_fd;
63         char *write_path;
64
65         char *root_path;
66         char *dest_dir_path; /* relative to root_path */
67         char *full_subvol_path;
68
69         struct subvol_info *cur_subvol;
70
71         struct subvol_uuid_search sus;
72
73         int honor_end_cmd;
74 };
75
76 static int finish_subvol(struct btrfs_receive *r)
77 {
78         int ret;
79         int subvol_fd = -1;
80         struct btrfs_ioctl_received_subvol_args rs_args;
81         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
82         u64 flags;
83
84         if (r->cur_subvol == NULL)
85                 return 0;
86
87         subvol_fd = openat(r->mnt_fd, r->cur_subvol->path,
88                         O_RDONLY | O_NOATIME);
89         if (subvol_fd < 0) {
90                 ret = -errno;
91                 fprintf(stderr, "ERROR: open %s failed. %s\n",
92                                 r->cur_subvol->path, strerror(-ret));
93                 goto out;
94         }
95
96         memset(&rs_args, 0, sizeof(rs_args));
97         memcpy(rs_args.uuid, r->cur_subvol->received_uuid, BTRFS_UUID_SIZE);
98         rs_args.stransid = r->cur_subvol->stransid;
99
100         if (g_verbose >= 1) {
101                 uuid_unparse((u8*)rs_args.uuid, uuid_str);
102                 fprintf(stderr, "BTRFS_IOC_SET_RECEIVED_SUBVOL uuid=%s, "
103                                 "stransid=%llu\n", uuid_str, rs_args.stransid);
104         }
105
106         ret = ioctl(subvol_fd, BTRFS_IOC_SET_RECEIVED_SUBVOL, &rs_args);
107         if (ret < 0) {
108                 ret = -errno;
109                 fprintf(stderr, "ERROR: BTRFS_IOC_SET_RECEIVED_SUBVOL failed. %s\n",
110                                 strerror(-ret));
111                 goto out;
112         }
113         r->cur_subvol->rtransid = rs_args.rtransid;
114
115         ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
116         if (ret < 0) {
117                 ret = -errno;
118                 fprintf(stderr, "ERROR: BTRFS_IOC_SUBVOL_GETFLAGS failed. %s\n",
119                                 strerror(-ret));
120                 goto out;
121         }
122
123         flags |= BTRFS_SUBVOL_RDONLY;
124
125         ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
126         if (ret < 0) {
127                 ret = -errno;
128                 fprintf(stderr, "ERROR: failed to make subvolume read only. "
129                                 "%s\n", strerror(-ret));
130                 goto out;
131         }
132
133         ret = 0;
134
135 out:
136         if (r->cur_subvol) {
137                 free(r->cur_subvol->path);
138                 free(r->cur_subvol);
139                 r->cur_subvol = NULL;
140         }
141         if (subvol_fd != -1)
142                 close(subvol_fd);
143         return ret;
144 }
145
146 static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
147                           void *user)
148 {
149         int ret;
150         struct btrfs_receive *r = user;
151         struct btrfs_ioctl_vol_args args_v1;
152         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
153
154         ret = finish_subvol(r);
155         if (ret < 0)
156                 goto out;
157
158         r->cur_subvol = calloc(1, sizeof(*r->cur_subvol));
159
160         if (strlen(r->dest_dir_path) == 0)
161                 r->cur_subvol->path = strdup(path);
162         else
163                 r->cur_subvol->path = path_cat(r->dest_dir_path, path);
164         free(r->full_subvol_path);
165         r->full_subvol_path = path_cat3(r->root_path, r->dest_dir_path, path);
166
167         fprintf(stderr, "At subvol %s\n", path);
168
169         memcpy(r->cur_subvol->received_uuid, uuid, BTRFS_UUID_SIZE);
170         r->cur_subvol->stransid = ctransid;
171
172         if (g_verbose) {
173                 uuid_unparse((u8*)r->cur_subvol->received_uuid, uuid_str);
174                 fprintf(stderr, "receiving subvol %s uuid=%s, stransid=%llu\n",
175                                 path, uuid_str,
176                                 r->cur_subvol->stransid);
177         }
178
179         memset(&args_v1, 0, sizeof(args_v1));
180         strncpy_null(args_v1.name, path);
181         ret = ioctl(r->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1);
182         if (ret < 0) {
183                 ret = -errno;
184                 fprintf(stderr, "ERROR: creating subvolume %s failed. "
185                                 "%s\n", path, strerror(-ret));
186                 goto out;
187         }
188
189 out:
190         return ret;
191 }
192
193 static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
194                             const u8 *parent_uuid, u64 parent_ctransid,
195                             void *user)
196 {
197         int ret;
198         struct btrfs_receive *r = user;
199         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
200         struct btrfs_ioctl_vol_args_v2 args_v2;
201         struct subvol_info *parent_subvol = NULL;
202
203         ret = finish_subvol(r);
204         if (ret < 0)
205                 goto out;
206
207         r->cur_subvol = calloc(1, sizeof(*r->cur_subvol));
208
209         if (strlen(r->dest_dir_path) == 0)
210                 r->cur_subvol->path = strdup(path);
211         else
212                 r->cur_subvol->path = path_cat(r->dest_dir_path, path);
213         free(r->full_subvol_path);
214         r->full_subvol_path = path_cat3(r->root_path, r->dest_dir_path, path);
215
216         fprintf(stdout, "At snapshot %s\n", path);
217
218         memcpy(r->cur_subvol->received_uuid, uuid, BTRFS_UUID_SIZE);
219         r->cur_subvol->stransid = ctransid;
220
221         if (g_verbose) {
222                 uuid_unparse((u8*)r->cur_subvol->received_uuid, uuid_str);
223                 fprintf(stderr, "receiving snapshot %s uuid=%s, "
224                                 "ctransid=%llu ", path, uuid_str,
225                                 r->cur_subvol->stransid);
226                 uuid_unparse(parent_uuid, uuid_str);
227                 fprintf(stderr, "parent_uuid=%s, parent_ctransid=%llu\n",
228                                 uuid_str, parent_ctransid);
229         }
230
231         memset(&args_v2, 0, sizeof(args_v2));
232         strncpy_null(args_v2.name, path);
233
234         parent_subvol = subvol_uuid_search(&r->sus, 0, parent_uuid,
235                         parent_ctransid, NULL, subvol_search_by_received_uuid);
236         if (!parent_subvol) {
237                 parent_subvol = subvol_uuid_search(&r->sus, 0, parent_uuid,
238                                 parent_ctransid, NULL, subvol_search_by_uuid);
239         }
240         if (!parent_subvol) {
241                 ret = -ENOENT;
242                 fprintf(stderr, "ERROR: could not find parent subvolume\n");
243                 goto out;
244         }
245
246         /*if (rs_args.ctransid > rs_args.rtransid) {
247                 if (!r->force) {
248                         ret = -EINVAL;
249                         fprintf(stderr, "ERROR: subvolume %s was modified after it was received.\n", r->subvol_parent_name);
250                         goto out;
251                 } else {
252                         fprintf(stderr, "WARNING: subvolume %s was modified after it was received.\n", r->subvol_parent_name);
253                 }
254         }*/
255
256         args_v2.fd = openat(r->mnt_fd, parent_subvol->path,
257                         O_RDONLY | O_NOATIME);
258         if (args_v2.fd < 0) {
259                 ret = -errno;
260                 if (errno != ENOENT)
261                         fprintf(stderr, "ERROR: open %s failed. %s\n",
262                                         parent_subvol->path, strerror(-ret));
263                 else
264                         fprintf(stderr,
265                                 "It seems that you have changed your default "
266                                 "subvolume or you specify other subvolume to\n"
267                                 "mount btrfs, try to remount this btrfs filesystem "
268                                 "with fs tree, and run btrfs receive again!\n");
269                 goto out;
270         }
271
272         ret = ioctl(r->dest_dir_fd, BTRFS_IOC_SNAP_CREATE_V2, &args_v2);
273         close(args_v2.fd);
274         if (ret < 0) {
275                 ret = -errno;
276                 fprintf(stderr, "ERROR: creating snapshot %s -> %s "
277                                 "failed. %s\n", parent_subvol->path,
278                                 path, strerror(-ret));
279                 goto out;
280         }
281
282 out:
283         if (parent_subvol) {
284                 free(parent_subvol->path);
285                 free(parent_subvol);
286         }
287         return ret;
288 }
289
290 static int process_mkfile(const char *path, void *user)
291 {
292         int ret;
293         struct btrfs_receive *r = user;
294         char *full_path = path_cat(r->full_subvol_path, path);
295
296         if (g_verbose >= 2)
297                 fprintf(stderr, "mkfile %s\n", path);
298
299         ret = creat(full_path, 0600);
300         if (ret < 0) {
301                 ret = -errno;
302                 fprintf(stderr, "ERROR: mkfile %s failed. %s\n", path,
303                                 strerror(-ret));
304                 goto out;
305         }
306         close(ret);
307         ret = 0;
308
309 out:
310         free(full_path);
311         return ret;
312 }
313
314 static int process_mkdir(const char *path, void *user)
315 {
316         int ret;
317         struct btrfs_receive *r = user;
318         char *full_path = path_cat(r->full_subvol_path, path);
319
320         if (g_verbose >= 2)
321                 fprintf(stderr, "mkdir %s\n", path);
322
323         ret = mkdir(full_path, 0700);
324         if (ret < 0) {
325                 ret = -errno;
326                 fprintf(stderr, "ERROR: mkdir %s failed. %s\n", path,
327                                 strerror(-ret));
328         }
329
330         free(full_path);
331         return ret;
332 }
333
334 static int process_mknod(const char *path, u64 mode, u64 dev, void *user)
335 {
336         int ret;
337         struct btrfs_receive *r = user;
338         char *full_path = path_cat(r->full_subvol_path, path);
339
340         if (g_verbose >= 2)
341                 fprintf(stderr, "mknod %s mode=%llu, dev=%llu\n",
342                                 path, mode, dev);
343
344         ret = mknod(full_path, mode & S_IFMT, dev);
345         if (ret < 0) {
346                 ret = -errno;
347                 fprintf(stderr, "ERROR: mknod %s failed. %s\n", path,
348                                 strerror(-ret));
349         }
350
351         free(full_path);
352         return ret;
353 }
354
355 static int process_mkfifo(const char *path, void *user)
356 {
357         int ret;
358         struct btrfs_receive *r = user;
359         char *full_path = path_cat(r->full_subvol_path, path);
360
361         if (g_verbose >= 2)
362                 fprintf(stderr, "mkfifo %s\n", path);
363
364         ret = mkfifo(full_path, 0600);
365         if (ret < 0) {
366                 ret = -errno;
367                 fprintf(stderr, "ERROR: mkfifo %s failed. %s\n", path,
368                                 strerror(-ret));
369         }
370
371         free(full_path);
372         return ret;
373 }
374
375 static int process_mksock(const char *path, void *user)
376 {
377         int ret;
378         struct btrfs_receive *r = user;
379         char *full_path = path_cat(r->full_subvol_path, path);
380
381         if (g_verbose >= 2)
382                 fprintf(stderr, "mksock %s\n", path);
383
384         ret = mknod(full_path, 0600 | S_IFSOCK, 0);
385         if (ret < 0) {
386                 ret = -errno;
387                 fprintf(stderr, "ERROR: mknod %s failed. %s\n", path,
388                                 strerror(-ret));
389         }
390
391         free(full_path);
392         return ret;
393 }
394
395 static int process_symlink(const char *path, const char *lnk, void *user)
396 {
397         int ret;
398         struct btrfs_receive *r = user;
399         char *full_path = path_cat(r->full_subvol_path, path);
400
401         if (g_verbose >= 2)
402                 fprintf(stderr, "symlink %s -> %s\n", path, lnk);
403
404         ret = symlink(lnk, full_path);
405         if (ret < 0) {
406                 ret = -errno;
407                 fprintf(stderr, "ERROR: symlink %s -> %s failed. %s\n", path,
408                                 lnk, strerror(-ret));
409         }
410
411         free(full_path);
412         return ret;
413 }
414
415 static int process_rename(const char *from, const char *to, void *user)
416 {
417         int ret;
418         struct btrfs_receive *r = user;
419         char *full_from = path_cat(r->full_subvol_path, from);
420         char *full_to = path_cat(r->full_subvol_path, to);
421
422         if (g_verbose >= 2)
423                 fprintf(stderr, "rename %s -> %s\n", from, to);
424
425         ret = rename(full_from, full_to);
426         if (ret < 0) {
427                 ret = -errno;
428                 fprintf(stderr, "ERROR: rename %s -> %s failed. %s\n", from,
429                                 to, strerror(-ret));
430         }
431
432         free(full_from);
433         free(full_to);
434         return ret;
435 }
436
437 static int process_link(const char *path, const char *lnk, void *user)
438 {
439         int ret;
440         struct btrfs_receive *r = user;
441         char *full_path = path_cat(r->full_subvol_path, path);
442         char *full_link_path = path_cat(r->full_subvol_path, lnk);
443
444         if (g_verbose >= 2)
445                 fprintf(stderr, "link %s -> %s\n", path, lnk);
446
447         ret = link(full_link_path, full_path);
448         if (ret < 0) {
449                 ret = -errno;
450                 fprintf(stderr, "ERROR: link %s -> %s failed. %s\n", path,
451                                 lnk, strerror(-ret));
452         }
453
454         free(full_path);
455         free(full_link_path);
456         return ret;
457 }
458
459
460 static int process_unlink(const char *path, void *user)
461 {
462         int ret;
463         struct btrfs_receive *r = user;
464         char *full_path = path_cat(r->full_subvol_path, path);
465
466         if (g_verbose >= 2)
467                 fprintf(stderr, "unlink %s\n", path);
468
469         ret = unlink(full_path);
470         if (ret < 0) {
471                 ret = -errno;
472                 fprintf(stderr, "ERROR: unlink %s failed. %s\n", path,
473                                 strerror(-ret));
474         }
475
476         free(full_path);
477         return ret;
478 }
479
480 static int process_rmdir(const char *path, void *user)
481 {
482         int ret;
483         struct btrfs_receive *r = user;
484         char *full_path = path_cat(r->full_subvol_path, path);
485
486         if (g_verbose >= 2)
487                 fprintf(stderr, "rmdir %s\n", path);
488
489         ret = rmdir(full_path);
490         if (ret < 0) {
491                 ret = -errno;
492                 fprintf(stderr, "ERROR: rmdir %s failed. %s\n", path,
493                                 strerror(-ret));
494         }
495
496         free(full_path);
497         return ret;
498 }
499
500
501 static int open_inode_for_write(struct btrfs_receive *r, const char *path)
502 {
503         int ret = 0;
504
505         if (r->write_fd != -1) {
506                 if (strcmp(r->write_path, path) == 0)
507                         goto out;
508                 close(r->write_fd);
509                 r->write_fd = -1;
510         }
511
512         r->write_fd = open(path, O_RDWR);
513         if (r->write_fd < 0) {
514                 ret = -errno;
515                 fprintf(stderr, "ERROR: open %s failed. %s\n", path,
516                                 strerror(-ret));
517                 goto out;
518         }
519         free(r->write_path);
520         r->write_path = strdup(path);
521
522 out:
523         return ret;
524 }
525
526 static void close_inode_for_write(struct btrfs_receive *r)
527 {
528         if(r->write_fd == -1)
529                 return;
530
531         close(r->write_fd);
532         r->write_fd = -1;
533         r->write_path[0] = 0;
534 }
535
536 static int process_write(const char *path, const void *data, u64 offset,
537                          u64 len, void *user)
538 {
539         int ret = 0;
540         struct btrfs_receive *r = user;
541         char *full_path = path_cat(r->full_subvol_path, path);
542         u64 pos = 0;
543         int w;
544
545         ret = open_inode_for_write(r, full_path);
546         if (ret < 0)
547                 goto out;
548
549         while (pos < len) {
550                 w = pwrite(r->write_fd, (char*)data + pos, len - pos,
551                                 offset + pos);
552                 if (w < 0) {
553                         ret = -errno;
554                         fprintf(stderr, "ERROR: writing to %s failed. %s\n",
555                                         path, strerror(-ret));
556                         goto out;
557                 }
558                 pos += w;
559         }
560
561 out:
562         free(full_path);
563         return ret;
564 }
565
566 static int process_clone(const char *path, u64 offset, u64 len,
567                          const u8 *clone_uuid, u64 clone_ctransid,
568                          const char *clone_path, u64 clone_offset,
569                          void *user)
570 {
571         int ret;
572         struct btrfs_receive *r = user;
573         struct btrfs_ioctl_clone_range_args clone_args;
574         struct subvol_info *si = NULL;
575         char *full_path = path_cat(r->full_subvol_path, path);
576         char *subvol_path = NULL;
577         char *full_clone_path = NULL;
578         int clone_fd = -1;
579
580         ret = open_inode_for_write(r, full_path);
581         if (ret < 0)
582                 goto out;
583
584         si = subvol_uuid_search(&r->sus, 0, clone_uuid, clone_ctransid, NULL,
585                         subvol_search_by_received_uuid);
586         if (!si) {
587                 if (memcmp(clone_uuid, r->cur_subvol->received_uuid,
588                                 BTRFS_UUID_SIZE) == 0) {
589                         /* TODO check generation of extent */
590                         subvol_path = strdup(r->cur_subvol->path);
591                 } else {
592                         ret = -ENOENT;
593                         fprintf(stderr, "ERROR: did not find source subvol.\n");
594                         goto out;
595                 }
596         } else {
597                 /*if (rs_args.ctransid > rs_args.rtransid) {
598                         if (!r->force) {
599                                 ret = -EINVAL;
600                                 fprintf(stderr, "ERROR: subvolume %s was "
601                                                 "modified after it was "
602                                                 "received.\n",
603                                                 r->subvol_parent_name);
604                                 goto out;
605                         } else {
606                                 fprintf(stderr, "WARNING: subvolume %s was "
607                                                 "modified after it was "
608                                                 "received.\n",
609                                                 r->subvol_parent_name);
610                         }
611                 }*/
612                 subvol_path = strdup(si->path);
613         }
614
615         full_clone_path = path_cat3(r->root_path, subvol_path, clone_path);
616
617         clone_fd = open(full_clone_path, O_RDONLY | O_NOATIME);
618         if (clone_fd < 0) {
619                 ret = -errno;
620                 fprintf(stderr, "ERROR: failed to open %s. %s\n",
621                                 full_clone_path, strerror(-ret));
622                 goto out;
623         }
624
625         clone_args.src_fd = clone_fd;
626         clone_args.src_offset = clone_offset;
627         clone_args.src_length = len;
628         clone_args.dest_offset = offset;
629         ret = ioctl(r->write_fd, BTRFS_IOC_CLONE_RANGE, &clone_args);
630         if (ret) {
631                 ret = -errno;
632                 fprintf(stderr, "ERROR: failed to clone extents to %s\n%s\n",
633                                 path, strerror(-ret));
634                 goto out;
635         }
636
637 out:
638         if (si) {
639                 free(si->path);
640                 free(si);
641         }
642         free(full_path);
643         free(full_clone_path);
644         free(subvol_path);
645         if (clone_fd != -1)
646                 close(clone_fd);
647         return ret;
648 }
649
650
651 static int process_set_xattr(const char *path, const char *name,
652                              const void *data, int len, void *user)
653 {
654         int ret = 0;
655         struct btrfs_receive *r = user;
656         char *full_path = path_cat(r->full_subvol_path, path);
657
658         if (g_verbose >= 2) {
659                 fprintf(stderr, "set_xattr %s - name=%s data_len=%d "
660                                 "data=%.*s\n", path, name, len,
661                                 len, (char*)data);
662         }
663
664         ret = lsetxattr(full_path, name, data, len, 0);
665         if (ret < 0) {
666                 ret = -errno;
667                 fprintf(stderr, "ERROR: lsetxattr %s %s=%.*s failed. %s\n",
668                                 path, name, len, (char*)data, strerror(-ret));
669                 goto out;
670         }
671
672 out:
673         free(full_path);
674         return ret;
675 }
676
677 static int process_remove_xattr(const char *path, const char *name, void *user)
678 {
679         int ret = 0;
680         struct btrfs_receive *r = user;
681         char *full_path = path_cat(r->full_subvol_path, path);
682
683         if (g_verbose >= 2) {
684                 fprintf(stderr, "remove_xattr %s - name=%s\n",
685                                 path, name);
686         }
687
688         ret = lremovexattr(full_path, name);
689         if (ret < 0) {
690                 ret = -errno;
691                 fprintf(stderr, "ERROR: lremovexattr %s %s failed. %s\n",
692                                 path, name, strerror(-ret));
693                 goto out;
694         }
695
696 out:
697         free(full_path);
698         return ret;
699 }
700
701 static int process_truncate(const char *path, u64 size, void *user)
702 {
703         int ret = 0;
704         struct btrfs_receive *r = user;
705         char *full_path = path_cat(r->full_subvol_path, path);
706
707         if (g_verbose >= 2)
708                 fprintf(stderr, "truncate %s size=%llu\n", path, size);
709
710         ret = truncate(full_path, size);
711         if (ret < 0) {
712                 ret = -errno;
713                 fprintf(stderr, "ERROR: truncate %s failed. %s\n",
714                                 path, strerror(-ret));
715                 goto out;
716         }
717
718 out:
719         free(full_path);
720         return ret;
721 }
722
723 static int process_chmod(const char *path, u64 mode, void *user)
724 {
725         int ret = 0;
726         struct btrfs_receive *r = user;
727         char *full_path = path_cat(r->full_subvol_path, path);
728
729         if (g_verbose >= 2)
730                 fprintf(stderr, "chmod %s - mode=0%o\n", path, (int)mode);
731
732         ret = chmod(full_path, mode);
733         if (ret < 0) {
734                 ret = -errno;
735                 fprintf(stderr, "ERROR: chmod %s failed. %s\n",
736                                 path, strerror(-ret));
737                 goto out;
738         }
739
740 out:
741         free(full_path);
742         return ret;
743 }
744
745 static int process_chown(const char *path, u64 uid, u64 gid, void *user)
746 {
747         int ret = 0;
748         struct btrfs_receive *r = user;
749         char *full_path = path_cat(r->full_subvol_path, path);
750
751         if (g_verbose >= 2)
752                 fprintf(stderr, "chown %s - uid=%llu, gid=%llu\n", path,
753                                 uid, gid);
754
755         ret = lchown(full_path, uid, gid);
756         if (ret < 0) {
757                 ret = -errno;
758                 fprintf(stderr, "ERROR: chown %s failed. %s\n",
759                                 path, strerror(-ret));
760                 goto out;
761         }
762
763 out:
764         free(full_path);
765         return ret;
766 }
767
768 static int process_utimes(const char *path, struct timespec *at,
769                           struct timespec *mt, struct timespec *ct,
770                           void *user)
771 {
772         int ret = 0;
773         struct btrfs_receive *r = user;
774         char *full_path = path_cat(r->full_subvol_path, path);
775         struct timespec tv[2];
776
777         if (g_verbose >= 2)
778                 fprintf(stderr, "utimes %s\n", path);
779
780         tv[0] = *at;
781         tv[1] = *mt;
782         ret = utimensat(AT_FDCWD, full_path, tv, AT_SYMLINK_NOFOLLOW);
783         if (ret < 0) {
784                 ret = -errno;
785                 fprintf(stderr, "ERROR: utimes %s failed. %s\n",
786                                 path, strerror(-ret));
787                 goto out;
788         }
789
790 out:
791         free(full_path);
792         return ret;
793 }
794
795
796 static struct btrfs_send_ops send_ops = {
797         .subvol = process_subvol,
798         .snapshot = process_snapshot,
799         .mkfile = process_mkfile,
800         .mkdir = process_mkdir,
801         .mknod = process_mknod,
802         .mkfifo = process_mkfifo,
803         .mksock = process_mksock,
804         .symlink = process_symlink,
805         .rename = process_rename,
806         .link = process_link,
807         .unlink = process_unlink,
808         .rmdir = process_rmdir,
809         .write = process_write,
810         .clone = process_clone,
811         .set_xattr = process_set_xattr,
812         .remove_xattr = process_remove_xattr,
813         .truncate = process_truncate,
814         .chmod = process_chmod,
815         .chown = process_chown,
816         .utimes = process_utimes,
817 };
818
819 static int do_receive(struct btrfs_receive *r, const char *tomnt, int r_fd)
820 {
821         int ret;
822         char *dest_dir_full_path;
823         int end = 0;
824
825         dest_dir_full_path = realpath(tomnt, NULL);
826         if (!dest_dir_full_path) {
827                 ret = -errno;
828                 fprintf(stderr, "ERROR: realpath(%s) failed. %s\n", tomnt,
829                         strerror(-ret));
830                 goto out;
831         }
832         r->dest_dir_fd = open(dest_dir_full_path, O_RDONLY | O_NOATIME);
833         if (r->dest_dir_fd < 0) {
834                 ret = -errno;
835                 fprintf(stderr,
836                         "ERROR: failed to open destination directory %s. %s\n",
837                         dest_dir_full_path, strerror(-ret));
838                 goto out;
839         }
840
841         ret = find_mount_root(dest_dir_full_path, &r->root_path);
842         if (ret < 0) {
843                 ret = -EINVAL;
844                 fprintf(stderr, "ERROR: failed to determine mount point "
845                         "for %s\n", dest_dir_full_path);
846                 goto out;
847         }
848         r->mnt_fd = open(r->root_path, O_RDONLY | O_NOATIME);
849         if (r->mnt_fd < 0) {
850                 ret = -errno;
851                 fprintf(stderr, "ERROR: failed to open %s. %s\n", r->root_path,
852                         strerror(-ret));
853                 goto out;
854         }
855
856         /*
857          * find_mount_root returns a root_path that is a subpath of
858          * dest_dir_full_path. Now get the other part of root_path,
859          * which is the destination dir relative to root_path.
860          */
861         r->dest_dir_path = dest_dir_full_path + strlen(r->root_path);
862         while (r->dest_dir_path[0] == '/')
863                 r->dest_dir_path++;
864
865         ret = subvol_uuid_search_init(r->mnt_fd, &r->sus);
866         if (ret < 0)
867                 goto out;
868
869         while (!end) {
870                 ret = btrfs_read_and_process_send_stream(r_fd, &send_ops, r,
871                                                          r->honor_end_cmd);
872                 if (ret < 0)
873                         goto out;
874                 if (ret)
875                         end = 1;
876
877                 close_inode_for_write(r);
878                 ret = finish_subvol(r);
879                 if (ret < 0)
880                         goto out;
881         }
882         ret = 0;
883
884 out:
885         if (r->write_fd != -1) {
886                 close(r->write_fd);
887                 r->write_fd = -1;
888         }
889         free(r->root_path);
890         r->root_path = NULL;
891         free(r->write_path);
892         r->write_path = NULL;
893         free(r->full_subvol_path);
894         r->full_subvol_path = NULL;
895         r->dest_dir_path = NULL;
896         free(dest_dir_full_path);
897         if (r->cur_subvol) {
898                 free(r->cur_subvol->path);
899                 free(r->cur_subvol);
900                 r->cur_subvol = NULL;
901         }
902         subvol_uuid_search_finit(&r->sus);
903         if (r->mnt_fd != -1) {
904                 close(r->mnt_fd);
905                 r->mnt_fd = -1;
906         }
907         if (r->dest_dir_fd != -1) {
908                 close(r->dest_dir_fd);
909                 r->dest_dir_fd = -1;
910         }
911         return ret;
912 }
913
914 int cmd_receive(int argc, char **argv)
915 {
916         int c;
917         char *tomnt = NULL;
918         char *fromfile = NULL;
919         struct btrfs_receive r;
920         int receive_fd = fileno(stdin);
921
922         int ret;
923
924         memset(&r, 0, sizeof(r));
925         r.mnt_fd = -1;
926         r.write_fd = -1;
927         r.dest_dir_fd = -1;
928
929         while ((c = getopt(argc, argv, "evf:")) != -1) {
930                 switch (c) {
931                 case 'v':
932                         g_verbose++;
933                         break;
934                 case 'f':
935                         fromfile = optarg;
936                         break;
937                 case 'e':
938                         r.honor_end_cmd = 1;
939                         break;
940                 case '?':
941                 default:
942                         fprintf(stderr, "ERROR: receive args invalid.\n");
943                         return 1;
944                 }
945         }
946
947         if (check_argc_exact(argc - optind, 1))
948                 usage(cmd_receive_usage);
949
950         tomnt = argv[optind];
951
952         if (fromfile) {
953                 receive_fd = open(fromfile, O_RDONLY | O_NOATIME);
954                 if (receive_fd < 0) {
955                         fprintf(stderr, "ERROR: failed to open %s\n", fromfile);
956                         return 1;
957                 }
958         }
959
960         ret = do_receive(&r, tomnt, receive_fd);
961
962         return !!ret;
963 }
964
965 const char * const cmd_receive_usage[] = {
966         "btrfs receive [-ve] [-f <infile>] <mount>",
967         "Receive subvolumes from stdin.",
968         "Receives one or more subvolumes that were previously ",
969         "sent with btrfs send. The received subvolumes are stored",
970         "into <mount>.",
971         "btrfs receive will fail in case a receiving subvolume",
972         "already exists. It will also fail in case a previously",
973         "received subvolume was changed after it was received.",
974         "After receiving a subvolume, it is immediately set to",
975         "read only.\n",
976         "-v               Enable verbose debug output. Each",
977         "                 occurrence of this option increases the",
978         "                 verbose level more.",
979         "-f <infile>      By default, btrfs receive uses stdin",
980         "                 to receive the subvolumes. Use this",
981         "                 option to specify a file to use instead.",
982         "-e               Terminate after receiving an <end cmd>",
983         "                 in the data stream. Without this option,",
984         "                 the receiver terminates only if an error",
985         "                 is recognized or on EOF.",
986         NULL
987 };