btrfs-progs: check: Fix lowmem false alert on tree reloc tree
[platform/upstream/btrfs-progs.git] / send-dump.c
1 /*
2  * Copyright (C) 2016 Fujitsu.  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.
15  */
16
17 #include <unistd.h>
18 #include <stdint.h>
19 #include <dirent.h>
20 #include <pthread.h>
21 #include <math.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <libgen.h>
27 #include <mntent.h>
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <ctype.h>
32 #include <asm/types.h>
33 #include <uuid/uuid.h>
34 #include "utils.h"
35 #include "commands.h"
36 #include "send-utils.h"
37 #include "send-stream.h"
38 #include "send-dump.h"
39
40 #define PATH_CAT_OR_RET(function_name, outpath, path1, path2, ret)      \
41 ({                                                                      \
42         ret = path_cat_out(outpath, path1, path2);                      \
43         if (ret < 0) {                                                  \
44                 error("%s: path invalid: %s\n", function_name, path2);  \
45                 return ret;                                             \
46         }                                                               \
47 })
48
49 /*
50  * Print path and escape chaacters (in a C way) that could break the line.
51  * Returns the length of the escaped characters. Unprintable characters are
52  * escaped as octals.
53  */
54 static int print_path_escaped(const char *path)
55 {
56         size_t i;
57         size_t path_len = strlen(path);
58         int len = 0;
59
60         for (i = 0; i < path_len; i++) {
61                 char c = path[i];
62
63                 len++;
64                 switch (c) {
65                 case '\a': putchar('\\'); putchar('a'); len++; break;
66                 case '\b': putchar('\\'); putchar('b'); len++; break;
67                 case '\e': putchar('\\'); putchar('e'); len++; break;
68                 case '\f': putchar('\\'); putchar('f'); len++; break;
69                 case '\n': putchar('\\'); putchar('n'); len++; break;
70                 case '\r': putchar('\\'); putchar('r'); len++; break;
71                 case '\t': putchar('\\'); putchar('t'); len++; break;
72                 case '\v': putchar('\\'); putchar('v'); len++; break;
73                 case ' ':  putchar('\\'); putchar(' '); len++; break;
74                 case '\\': putchar('\\'); putchar('\\'); len++; break;
75                 default:
76                           if (!isprint(c)) {
77                                   printf("\\%c%c%c",
78                                                   '0' + ((c & 0300) >> 6),
79                                                   '0' + ((c & 070) >> 3),
80                                                   '0' + (c & 07));
81                                   len += 3;
82                           } else {
83                                   putchar(c);
84                           }
85                 }
86         }
87         return len;
88 }
89
90 /*
91  * Underlying PRINT_DUMP, the only difference is how we handle
92  * the full path.
93  */
94 __attribute__ ((format (printf, 5, 6)))
95 static int __print_dump(int subvol, void *user, const char *path,
96                         const char *title, const char *fmt, ...)
97 {
98         struct btrfs_dump_send_args *r = user;
99         char full_path[PATH_MAX] = {0};
100         char *out_path;
101         va_list args;
102         int ret;
103
104         if (subvol) {
105                 PATH_CAT_OR_RET(title, r->full_subvol_path, r->root_path, path, ret);
106                 out_path = r->full_subvol_path;
107         } else {
108                 PATH_CAT_OR_RET(title, full_path, r->full_subvol_path, path, ret);
109                 out_path = full_path;
110         }
111
112         /* Unified header */
113         printf("%-16s", title);
114         ret = print_path_escaped(out_path);
115         if (!fmt)
116                 return 0;
117         /* Short paths ale aligned to 32 chars */
118         while (ret++ < 32)
119                 putchar(' ');
120         va_start(args, fmt);
121         /* Operation specified ones */
122         vprintf(fmt, args);
123         va_end(args);
124         putchar('\n');
125         return 0;
126 }
127
128 /* For subvolume/snapshot operation only */
129 #define PRINT_DUMP_SUBVOL(user, path, title, fmt, ...) \
130         __print_dump(1, user, path, title, fmt, ##__VA_ARGS__)
131
132 /* For other operations */
133 #define PRINT_DUMP(user, path, title, fmt, ...) \
134         __print_dump(0, user, path, title, fmt, ##__VA_ARGS__)
135
136 static int print_subvol(const char *path, const u8 *uuid, u64 ctransid,
137                         void *user)
138 {
139         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
140
141         uuid_unparse(uuid, uuid_str);
142
143         return PRINT_DUMP_SUBVOL(user, path, "subvol", "uuid=%s transid=%llu",
144                                  uuid_str, ctransid);
145 }
146
147 static int print_snapshot(const char *path, const u8 *uuid, u64 ctransid,
148                           const u8 *parent_uuid, u64 parent_ctransid,
149                           void *user)
150 {
151         char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
152         char parent_uuid_str[BTRFS_UUID_UNPARSED_SIZE];
153         int ret;
154
155         uuid_unparse(uuid, uuid_str);
156         uuid_unparse(parent_uuid, parent_uuid_str);
157
158         ret = PRINT_DUMP_SUBVOL(user, path, "snapshot",
159                 "uuid=%s transid=%llu parent_uuid=%s parent_transid=%llu",
160                                 uuid_str, ctransid, parent_uuid_str,
161                                 parent_ctransid);
162         return ret;
163 }
164
165 static int print_mkfile(const char *path, void *user)
166 {
167         return PRINT_DUMP(user, path, "mkfile", NULL);
168 }
169
170 static int print_mkdir(const char *path, void *user)
171 {
172         return PRINT_DUMP(user, path, "mkdir", NULL);
173 }
174
175 static int print_mknod(const char *path, u64 mode, u64 dev, void *user)
176 {
177         return PRINT_DUMP(user, path, "mknod", "mode=%llo dev=0x%llx", mode,
178                           dev);
179 }
180
181 static int print_mkfifo(const char *path, void *user)
182 {
183         return PRINT_DUMP(user, path, "mkfifo", NULL);
184 }
185
186 static int print_mksock(const char *path, void *user)
187 {
188         return PRINT_DUMP(user, path, "mksock", NULL);
189 }
190
191 static int print_symlink(const char *path, const char *lnk, void *user)
192 {
193         return PRINT_DUMP(user, path, "symlink", "dest=%s", lnk);
194 }
195
196 static int print_rename(const char *from, const char *to, void *user)
197 {
198         struct btrfs_dump_send_args *r = user;
199         char full_to[PATH_MAX];
200         int ret;
201
202         PATH_CAT_OR_RET("rename", full_to, r->full_subvol_path, to, ret);
203         return PRINT_DUMP(user, from, "rename", "dest=%s", full_to);
204 }
205
206 static int print_link(const char *path, const char *lnk, void *user)
207 {
208         return PRINT_DUMP(user, path, "link", "dest=%s", lnk);
209 }
210
211 static int print_unlink(const char *path, void *user)
212 {
213         return PRINT_DUMP(user, path, "unlink", NULL);
214 }
215
216 static int print_rmdir(const char *path, void *user)
217 {
218         return PRINT_DUMP(user, path, "rmdir", NULL);
219 }
220
221 static int print_write(const char *path, const void *data, u64 offset,
222                        u64 len, void *user)
223 {
224         return PRINT_DUMP(user, path, "write", "offset=%llu len=%llu",
225                           offset, len);
226 }
227
228 static int print_clone(const char *path, u64 offset, u64 len,
229                        const u8 *clone_uuid, u64 clone_ctransid,
230                        const char *clone_path, u64 clone_offset,
231                        void *user)
232 {
233         struct btrfs_dump_send_args *r = user;
234         char full_path[PATH_MAX];
235         int ret;
236
237         PATH_CAT_OR_RET("clone", full_path, r->full_subvol_path, clone_path,
238                         ret);
239         return PRINT_DUMP(user, path, "clone",
240                           "offset=%llu len=%llu from=%s clone_offset=%llu",
241                           offset, len, full_path, clone_offset);
242 }
243
244 static int print_set_xattr(const char *path, const char *name,
245                            const void *data, int len, void *user)
246 {
247         return PRINT_DUMP(user, path, "set_xattr", "name=%s data=%.*s len=%d",
248                           name, len, (char *)data, len);
249 }
250
251 static int print_remove_xattr(const char *path, const char *name, void *user)
252 {
253
254         return PRINT_DUMP(user, path, "remove_xattr", "name=%s", name);
255 }
256
257 static int print_truncate(const char *path, u64 size, void *user)
258 {
259         return PRINT_DUMP(user, path, "truncate", "size=%llu", size);
260 }
261
262 static int print_chmod(const char *path, u64 mode, void *user)
263 {
264         return PRINT_DUMP(user, path, "chmod", "mode=%llo", mode);
265 }
266
267 static int print_chown(const char *path, u64 uid, u64 gid, void *user)
268 {
269         return PRINT_DUMP(user, path, "chown", "gid=%llu uid=%llu", gid, uid);
270 }
271
272 static int sprintf_timespec(struct timespec *ts, char *dest, int max_size)
273 {
274         struct tm tm;
275         int ret;
276
277         if (!localtime_r(&ts->tv_sec, &tm)) {
278                 error("failed to convert time %lld.%.9ld to local time",
279                       (long long)ts->tv_sec, ts->tv_nsec);
280                 return -EINVAL;
281         }
282         ret = strftime(dest, max_size, "%FT%T%z", &tm);
283         if (ret == 0) {
284                 error(
285                 "time %lld.%ld is too long to convert into readable string",
286                       (long long)ts->tv_sec, ts->tv_nsec);
287                 return -EINVAL;
288         }
289         return 0;
290 }
291
292 #define TIME_STRING_MAX 64
293 static int print_utimes(const char *path, struct timespec *at,
294                         struct timespec *mt, struct timespec *ct,
295                         void *user)
296 {
297         char at_str[TIME_STRING_MAX];
298         char mt_str[TIME_STRING_MAX];
299         char ct_str[TIME_STRING_MAX];
300
301         if (sprintf_timespec(at, at_str, TIME_STRING_MAX - 1) < 0 ||
302             sprintf_timespec(mt, mt_str, TIME_STRING_MAX - 1) < 0 ||
303             sprintf_timespec(ct, ct_str, TIME_STRING_MAX - 1) < 0)
304                 return -EINVAL;
305         return PRINT_DUMP(user, path, "utimes", "atime=%s mtime=%s ctime=%s",
306                           at_str, mt_str, ct_str);
307 }
308
309 static int print_update_extent(const char *path, u64 offset, u64 len,
310                                void *user)
311 {
312         return PRINT_DUMP(user, path, "update_extent", "offset=%llu len=%llu",
313                           offset, len);
314 }
315
316 struct btrfs_send_ops btrfs_print_send_ops = {
317         .subvol = print_subvol,
318         .snapshot = print_snapshot,
319         .mkfile = print_mkfile,
320         .mkdir = print_mkdir,
321         .mknod = print_mknod,
322         .mkfifo = print_mkfifo,
323         .mksock = print_mksock,
324         .symlink = print_symlink,
325         .rename = print_rename,
326         .link = print_link,
327         .unlink = print_unlink,
328         .rmdir = print_rmdir,
329         .write = print_write,
330         .clone = print_clone,
331         .set_xattr = print_set_xattr,
332         .remove_xattr = print_remove_xattr,
333         .truncate = print_truncate,
334         .chmod = print_chmod,
335         .chown = print_chown,
336         .utimes = print_utimes,
337         .update_extent = print_update_extent
338 };