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