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