d2de9d39d2962939580b980116f64e6ca9e30902
[platform/upstream/btrfs-progs.git] / send-stream.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 #include <uuid/uuid.h>
20 #include <unistd.h>
21
22 #include "send.h"
23 #include "send-stream.h"
24 #include "crc32c.h"
25 #include "utils.h"
26
27 struct btrfs_send_stream {
28         int fd;
29         char read_buf[BTRFS_SEND_BUF_SIZE];
30
31         int cmd;
32         struct btrfs_cmd_header *cmd_hdr;
33         struct btrfs_tlv_header *cmd_attrs[BTRFS_SEND_A_MAX + 1];
34         u32 version;
35
36         struct btrfs_send_ops *ops;
37         void *user;
38 };
39
40 /*
41  * Read len bytes to buf.
42  * Return:
43  *   0 - success
44  * < 0 - negative errno in case of error
45  * > 0 - no data read, EOF
46  */
47 static int read_buf(struct btrfs_send_stream *sctx, char *buf, size_t len)
48 {
49         int ret;
50         size_t pos = 0;
51
52         while (pos < len) {
53                 ssize_t rbytes;
54
55                 rbytes = read(sctx->fd, buf + pos, len - pos);
56                 if (rbytes < 0) {
57                         ret = -errno;
58                         error("read from stream failed: %s",
59                                         strerror(-ret));
60                         goto out;
61                 }
62                 if (rbytes == 0) {
63                         ret = 1;
64                         goto out;
65                 }
66                 pos += rbytes;
67         }
68
69         ret = 0;
70
71 out:
72         return ret;
73 }
74
75 /*
76  * Reads a single command from kernel space and decodes the TLV's into
77  * sctx->cmd_attrs
78  *
79  * Returns:
80  *   0 - success
81  * < 0 - an error in the command
82  */
83 static int read_cmd(struct btrfs_send_stream *sctx)
84 {
85         int ret;
86         u16 cmd;
87         u32 cmd_len;
88         char *data;
89         u32 pos;
90         u32 crc;
91         u32 crc2;
92
93         memset(sctx->cmd_attrs, 0, sizeof(sctx->cmd_attrs));
94
95         ASSERT(sizeof(*sctx->cmd_hdr) <= sizeof(sctx->read_buf));
96         ret = read_buf(sctx, sctx->read_buf, sizeof(*sctx->cmd_hdr));
97         if (ret < 0)
98                 goto out;
99         if (ret) {
100                 ret = -EINVAL;
101                 error("unexpected EOF in stream");
102                 goto out;
103         }
104
105         sctx->cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf;
106         cmd = le16_to_cpu(sctx->cmd_hdr->cmd);
107         cmd_len = le32_to_cpu(sctx->cmd_hdr->len);
108
109         if (cmd_len + sizeof(*sctx->cmd_hdr) >= sizeof(sctx->read_buf)) {
110                 ret = -EINVAL;
111                 error("command length %u too big for buffer %zu",
112                                 cmd_len, sizeof(sctx->read_buf));
113                 goto out;
114         }
115
116         data = sctx->read_buf + sizeof(*sctx->cmd_hdr);
117         ret = read_buf(sctx, data, cmd_len);
118         if (ret < 0)
119                 goto out;
120         if (ret) {
121                 ret = -EINVAL;
122                 error("unexpected EOF in stream");
123                 goto out;
124         }
125
126         crc = le32_to_cpu(sctx->cmd_hdr->crc);
127         sctx->cmd_hdr->crc = 0;
128
129         crc2 = crc32c(0, (unsigned char*)sctx->read_buf,
130                         sizeof(*sctx->cmd_hdr) + cmd_len);
131
132         if (crc != crc2) {
133                 ret = -EINVAL;
134                 error("crc32 mismatch in command");
135                 goto out;
136         }
137
138         pos = 0;
139         while (pos < cmd_len) {
140                 struct btrfs_tlv_header *tlv_hdr;
141                 u16 tlv_type;
142                 u16 tlv_len;
143
144                 tlv_hdr = (struct btrfs_tlv_header *)data;
145                 tlv_type = le16_to_cpu(tlv_hdr->tlv_type);
146                 tlv_len = le16_to_cpu(tlv_hdr->tlv_len);
147
148                 if (tlv_type == 0 || tlv_type > BTRFS_SEND_A_MAX
149                     || tlv_len > BTRFS_SEND_BUF_SIZE) {
150                         error("invalid tlv in cmd tlv_type = %hu, tlv_len = %hu",
151                                         tlv_type, tlv_len);
152                         ret = -EINVAL;
153                         goto out;
154                 }
155
156                 sctx->cmd_attrs[tlv_type] = tlv_hdr;
157
158                 data += sizeof(*tlv_hdr) + tlv_len;
159                 pos += sizeof(*tlv_hdr) + tlv_len;
160         }
161
162         sctx->cmd = cmd;
163         ret = 0;
164
165 out:
166         return ret;
167 }
168
169 static int tlv_get(struct btrfs_send_stream *sctx, int attr, void **data, int *len)
170 {
171         int ret;
172         struct btrfs_tlv_header *hdr;
173
174         if (attr <= 0 || attr > BTRFS_SEND_A_MAX) {
175                 error("invalid attribute requested, attr = %d", attr);
176                 ret = -EINVAL;
177                 goto out;
178         }
179
180         hdr = sctx->cmd_attrs[attr];
181         if (!hdr) {
182                 error("attribute %d requested but not present", attr);
183                 ret = -ENOENT;
184                 goto out;
185         }
186
187         *len = le16_to_cpu(hdr->tlv_len);
188         *data = hdr + 1;
189
190         ret = 0;
191
192 out:
193         return ret;
194 }
195
196 #define __TLV_GOTO_FAIL(expr) \
197         if ((ret = expr) < 0) \
198                 goto tlv_get_failed;
199
200 #define __TLV_DO_WHILE_GOTO_FAIL(expr) \
201         do { \
202                 __TLV_GOTO_FAIL(expr) \
203         } while (0)
204
205
206 #define TLV_GET(s, attr, data, len) \
207         __TLV_DO_WHILE_GOTO_FAIL(tlv_get(s, attr, data, len))
208
209 #define TLV_CHECK_LEN(expected, got) \
210         do { \
211                 if (expected != got) { \
212                         error("invalid size for attribute, " \
213                                         "expected = %d, got = %d", \
214                                         (int)expected, (int)got); \
215                         ret = -EINVAL; \
216                         goto tlv_get_failed; \
217                 } \
218         } while (0)
219
220 #define TLV_GET_INT(s, attr, bits, v) \
221         do { \
222                 __le##bits *__tmp; \
223                 int __len; \
224                 TLV_GET(s, attr, (void**)&__tmp, &__len); \
225                 TLV_CHECK_LEN(sizeof(*__tmp), __len); \
226                 *v = get_unaligned_le##bits(__tmp); \
227         } while (0)
228
229 #define TLV_GET_U8(s, attr, v) TLV_GET_INT(s, attr, 8, v)
230 #define TLV_GET_U16(s, attr, v) TLV_GET_INT(s, attr, 16, v)
231 #define TLV_GET_U32(s, attr, v) TLV_GET_INT(s, attr, 32, v)
232 #define TLV_GET_U64(s, attr, v) TLV_GET_INT(s, attr, 64, v)
233
234 static int tlv_get_string(struct btrfs_send_stream *sctx, int attr, char **str)
235 {
236         int ret;
237         void *data;
238         int len = 0;
239
240         TLV_GET(sctx, attr, &data, &len);
241
242         *str = malloc(len + 1);
243         if (!*str)
244                 return -ENOMEM;
245
246         memcpy(*str, data, len);
247         (*str)[len] = 0;
248         ret = 0;
249
250 tlv_get_failed:
251         return ret;
252 }
253 #define TLV_GET_STRING(s, attr, str) \
254         __TLV_DO_WHILE_GOTO_FAIL(tlv_get_string(s, attr, str))
255
256 static int tlv_get_timespec(struct btrfs_send_stream *sctx,
257                             int attr, struct timespec *ts)
258 {
259         int ret;
260         int len;
261         struct btrfs_timespec *bts;
262
263         TLV_GET(sctx, attr, (void**)&bts, &len);
264         TLV_CHECK_LEN(sizeof(*bts), len);
265
266         ts->tv_sec = le64_to_cpu(bts->sec);
267         ts->tv_nsec = le32_to_cpu(bts->nsec);
268         ret = 0;
269
270 tlv_get_failed:
271         return ret;
272 }
273 #define TLV_GET_TIMESPEC(s, attr, ts) \
274         __TLV_DO_WHILE_GOTO_FAIL(tlv_get_timespec(s, attr, ts))
275
276 static int tlv_get_uuid(struct btrfs_send_stream *sctx, int attr, u8 *uuid)
277 {
278         int ret;
279         int len;
280         void *data;
281
282         TLV_GET(sctx, attr, &data, &len);
283         TLV_CHECK_LEN(BTRFS_UUID_SIZE, len);
284         memcpy(uuid, data, BTRFS_UUID_SIZE);
285
286         ret = 0;
287
288 tlv_get_failed:
289         return ret;
290 }
291 #define TLV_GET_UUID(s, attr, uuid) \
292         __TLV_DO_WHILE_GOTO_FAIL(tlv_get_uuid(s, attr, uuid))
293
294 static int read_and_process_cmd(struct btrfs_send_stream *sctx)
295 {
296         int ret;
297         char *path = NULL;
298         char *path_to = NULL;
299         char *clone_path = NULL;
300         char *xattr_name = NULL;
301         void *xattr_data = NULL;
302         void *data = NULL;
303         struct timespec at;
304         struct timespec ct;
305         struct timespec mt;
306         u8 uuid[BTRFS_UUID_SIZE];
307         u8 clone_uuid[BTRFS_UUID_SIZE];
308         u64 tmp;
309         u64 tmp2;
310         u64 ctransid;
311         u64 clone_ctransid;
312         u64 mode;
313         u64 dev;
314         u64 clone_offset;
315         u64 offset;
316         int len;
317         int xattr_len;
318
319         ret = read_cmd(sctx);
320         if (ret)
321                 goto out;
322
323         switch (sctx->cmd) {
324         case BTRFS_SEND_C_SUBVOL:
325                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
326                 TLV_GET_UUID(sctx, BTRFS_SEND_A_UUID, uuid);
327                 TLV_GET_U64(sctx, BTRFS_SEND_A_CTRANSID, &ctransid);
328                 ret = sctx->ops->subvol(path, uuid, ctransid, sctx->user);
329                 break;
330         case BTRFS_SEND_C_SNAPSHOT:
331                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
332                 TLV_GET_UUID(sctx, BTRFS_SEND_A_UUID, uuid);
333                 TLV_GET_U64(sctx, BTRFS_SEND_A_CTRANSID, &ctransid);
334                 TLV_GET_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, clone_uuid);
335                 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, &clone_ctransid);
336                 ret = sctx->ops->snapshot(path, uuid, ctransid, clone_uuid,
337                                 clone_ctransid, sctx->user);
338                 break;
339         case BTRFS_SEND_C_MKFILE:
340                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
341                 ret = sctx->ops->mkfile(path, sctx->user);
342                 break;
343         case BTRFS_SEND_C_MKDIR:
344                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
345                 ret = sctx->ops->mkdir(path, sctx->user);
346                 break;
347         case BTRFS_SEND_C_MKNOD:
348                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
349                 TLV_GET_U64(sctx, BTRFS_SEND_A_MODE, &mode);
350                 TLV_GET_U64(sctx, BTRFS_SEND_A_RDEV, &dev);
351                 ret = sctx->ops->mknod(path, mode, dev, sctx->user);
352                 break;
353         case BTRFS_SEND_C_MKFIFO:
354                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
355                 ret = sctx->ops->mkfifo(path, sctx->user);
356                 break;
357         case BTRFS_SEND_C_MKSOCK:
358                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
359                 ret = sctx->ops->mksock(path, sctx->user);
360                 break;
361         case BTRFS_SEND_C_SYMLINK:
362                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
363                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_LINK, &path_to);
364                 ret = sctx->ops->symlink(path, path_to, sctx->user);
365                 break;
366         case BTRFS_SEND_C_RENAME:
367                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
368                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_TO, &path_to);
369                 ret = sctx->ops->rename(path, path_to, sctx->user);
370                 break;
371         case BTRFS_SEND_C_LINK:
372                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
373                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_LINK, &path_to);
374                 ret = sctx->ops->link(path, path_to, sctx->user);
375                 break;
376         case BTRFS_SEND_C_UNLINK:
377                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
378                 ret = sctx->ops->unlink(path, sctx->user);
379                 break;
380         case BTRFS_SEND_C_RMDIR:
381                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
382                 ret = sctx->ops->rmdir(path, sctx->user);
383                 break;
384         case BTRFS_SEND_C_WRITE:
385                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
386                 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
387                 TLV_GET(sctx, BTRFS_SEND_A_DATA, &data, &len);
388                 ret = sctx->ops->write(path, data, offset, len, sctx->user);
389                 break;
390         case BTRFS_SEND_C_CLONE:
391                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
392                 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
393                 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_LEN, &len);
394                 TLV_GET_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, clone_uuid);
395                 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, &clone_ctransid);
396                 TLV_GET_STRING(sctx, BTRFS_SEND_A_CLONE_PATH, &clone_path);
397                 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, &clone_offset);
398                 ret = sctx->ops->clone(path, offset, len, clone_uuid,
399                                 clone_ctransid, clone_path, clone_offset,
400                                 sctx->user);
401                 break;
402         case BTRFS_SEND_C_SET_XATTR:
403                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
404                 TLV_GET_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, &xattr_name);
405                 TLV_GET(sctx, BTRFS_SEND_A_XATTR_DATA, &xattr_data, &xattr_len);
406                 ret = sctx->ops->set_xattr(path, xattr_name, xattr_data,
407                                 xattr_len, sctx->user);
408                 break;
409         case BTRFS_SEND_C_REMOVE_XATTR:
410                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
411                 TLV_GET_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, &xattr_name);
412                 ret = sctx->ops->remove_xattr(path, xattr_name, sctx->user);
413                 break;
414         case BTRFS_SEND_C_TRUNCATE:
415                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
416                 TLV_GET_U64(sctx, BTRFS_SEND_A_SIZE, &tmp);
417                 ret = sctx->ops->truncate(path, tmp, sctx->user);
418                 break;
419         case BTRFS_SEND_C_CHMOD:
420                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
421                 TLV_GET_U64(sctx, BTRFS_SEND_A_MODE, &tmp);
422                 ret = sctx->ops->chmod(path, tmp, sctx->user);
423                 break;
424         case BTRFS_SEND_C_CHOWN:
425                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
426                 TLV_GET_U64(sctx, BTRFS_SEND_A_UID, &tmp);
427                 TLV_GET_U64(sctx, BTRFS_SEND_A_GID, &tmp2);
428                 ret = sctx->ops->chown(path, tmp, tmp2, sctx->user);
429                 break;
430         case BTRFS_SEND_C_UTIMES:
431                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
432                 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, &at);
433                 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, &mt);
434                 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, &ct);
435                 ret = sctx->ops->utimes(path, &at, &mt, &ct, sctx->user);
436                 break;
437         case BTRFS_SEND_C_UPDATE_EXTENT:
438                 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
439                 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
440                 TLV_GET_U64(sctx, BTRFS_SEND_A_SIZE, &tmp);
441                 ret = sctx->ops->update_extent(path, offset, tmp, sctx->user);
442                 break;
443         case BTRFS_SEND_C_END:
444                 ret = 1;
445                 break;
446         }
447
448 tlv_get_failed:
449 out:
450         free(path);
451         free(path_to);
452         free(clone_path);
453         free(xattr_name);
454         return ret;
455 }
456
457 /*
458  * If max_errors is 0, then don't stop processing the stream if one of the
459  * callbacks in btrfs_send_ops structure returns an error. If greater than
460  * zero, stop after max_errors errors happened.
461  */
462 int btrfs_read_and_process_send_stream(int fd,
463                                        struct btrfs_send_ops *ops, void *user,
464                                        int honor_end_cmd,
465                                        u64 max_errors)
466 {
467         int ret;
468         struct btrfs_send_stream sctx;
469         struct btrfs_stream_header hdr;
470         u64 errors = 0;
471         int last_err = 0;
472
473         sctx.fd = fd;
474         sctx.ops = ops;
475         sctx.user = user;
476
477         ret = read_buf(&sctx, (char*)&hdr, sizeof(hdr));
478         if (ret < 0)
479                 goto out;
480         if (ret) {
481                 ret = 1;
482                 goto out;
483         }
484
485         if (strcmp(hdr.magic, BTRFS_SEND_STREAM_MAGIC)) {
486                 ret = -EINVAL;
487                 error("unexpected header");
488                 goto out;
489         }
490
491         sctx.version = le32_to_cpu(hdr.version);
492         if (sctx.version > BTRFS_SEND_STREAM_VERSION) {
493                 ret = -EINVAL;
494                 error("stream version %d not supported, please use newer version",
495                                 sctx.version);
496                 goto out;
497         }
498
499         while (1) {
500                 ret = read_and_process_cmd(&sctx);
501                 if (ret < 0) {
502                         last_err = ret;
503                         errors++;
504                         if (max_errors > 0 && errors >= max_errors)
505                                 goto out;
506                 } else if (ret > 0) {
507                         if (!honor_end_cmd)
508                                 ret = 0;
509                         goto out;
510                 }
511         }
512
513 out:
514         if (last_err && !ret)
515                 ret = last_err;
516
517         return ret;
518 }