2 * Copyright (C) 2012 Alexander Block. All rights reserved.
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.
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.
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.
19 #include <uuid/uuid.h>
23 #include "send-stream.h"
27 struct btrfs_send_stream {
29 char read_buf[BTRFS_SEND_BUF_SIZE];
32 struct btrfs_cmd_header *cmd_hdr;
33 struct btrfs_tlv_header *cmd_attrs[BTRFS_SEND_A_MAX + 1];
36 struct btrfs_send_ops *ops;
40 static int read_buf(struct btrfs_send_stream *sctx, char *buf, size_t len)
46 ret = read(sctx->fd, buf + pos, len - pos);
49 error("read from stream failed: %s",
67 * Reads a single command from kernel space and decodes the TLV's into
70 static int read_cmd(struct btrfs_send_stream *sctx)
79 struct btrfs_tlv_header *tlv_hdr;
83 memset(sctx->cmd_attrs, 0, sizeof(sctx->cmd_attrs));
85 ASSERT(sizeof(*sctx->cmd_hdr) <= sizeof(sctx->read_buf));
86 ret = read_buf(sctx, sctx->read_buf, sizeof(*sctx->cmd_hdr));
91 error("unexpected EOF in stream");
95 sctx->cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf;
96 cmd = le16_to_cpu(sctx->cmd_hdr->cmd);
97 cmd_len = le32_to_cpu(sctx->cmd_hdr->len);
99 if (cmd_len + sizeof(*sctx->cmd_hdr) >= sizeof(sctx->read_buf)) {
101 error("command length %d too big for buffer %zu",
102 cmd_len, sizeof(sctx->read_buf));
106 data = sctx->read_buf + sizeof(*sctx->cmd_hdr);
107 ret = read_buf(sctx, data, cmd_len);
112 error("unexpected EOF in stream");
116 crc = le32_to_cpu(sctx->cmd_hdr->crc);
117 sctx->cmd_hdr->crc = 0;
119 crc2 = crc32c(0, (unsigned char*)sctx->read_buf,
120 sizeof(*sctx->cmd_hdr) + cmd_len);
124 error("crc32 mismatch in command");
129 while (pos < cmd_len) {
130 tlv_hdr = (struct btrfs_tlv_header *)data;
131 tlv_type = le16_to_cpu(tlv_hdr->tlv_type);
132 tlv_len = le16_to_cpu(tlv_hdr->tlv_len);
134 if (tlv_type <= 0 || tlv_type > BTRFS_SEND_A_MAX ||
135 tlv_len < 0 || tlv_len > BTRFS_SEND_BUF_SIZE) {
136 error("invalid tlv in cmd tlv_type = %d, tlv_len = %d",
142 sctx->cmd_attrs[tlv_type] = tlv_hdr;
144 data += sizeof(*tlv_hdr) + tlv_len;
145 pos += sizeof(*tlv_hdr) + tlv_len;
155 static int tlv_get(struct btrfs_send_stream *sctx, int attr, void **data, int *len)
158 struct btrfs_tlv_header *h;
160 if (attr <= 0 || attr > BTRFS_SEND_A_MAX) {
161 error("invalid attribute requested, attr = %d", attr);
166 h = sctx->cmd_attrs[attr];
168 error("attribute %d requested but not present", attr);
173 *len = le16_to_cpu(h->tlv_len);
182 #define __TLV_GOTO_FAIL(expr) \
183 if ((ret = expr) < 0) \
186 #define __TLV_DO_WHILE_GOTO_FAIL(expr) \
188 __TLV_GOTO_FAIL(expr) \
192 #define TLV_GET(s, attr, data, len) \
193 __TLV_DO_WHILE_GOTO_FAIL(tlv_get(s, attr, data, len))
195 #define TLV_CHECK_LEN(expected, got) \
197 if (expected != got) { \
198 error("invalid size for attribute, " \
199 "expected = %d, got = %d", \
200 (int)expected, (int)got); \
202 goto tlv_get_failed; \
206 #define TLV_GET_INT(s, attr, bits, v) \
210 TLV_GET(s, attr, (void**)&__tmp, &__len); \
211 TLV_CHECK_LEN(sizeof(*__tmp), __len); \
212 *v = get_unaligned_le##bits(__tmp); \
215 #define TLV_GET_U8(s, attr, v) TLV_GET_INT(s, attr, 8, v)
216 #define TLV_GET_U16(s, attr, v) TLV_GET_INT(s, attr, 16, v)
217 #define TLV_GET_U32(s, attr, v) TLV_GET_INT(s, attr, 32, v)
218 #define TLV_GET_U64(s, attr, v) TLV_GET_INT(s, attr, 64, v)
220 static int tlv_get_string(struct btrfs_send_stream *sctx, int attr, char **str)
226 TLV_GET(sctx, attr, &data, &len);
228 *str = malloc(len + 1);
232 memcpy(*str, data, len);
239 #define TLV_GET_STRING(s, attr, str) \
240 __TLV_DO_WHILE_GOTO_FAIL(tlv_get_string(s, attr, str))
242 static int tlv_get_timespec(struct btrfs_send_stream *sctx,
243 int attr, struct timespec *ts)
247 struct btrfs_timespec *bts;
249 TLV_GET(sctx, attr, (void**)&bts, &len);
250 TLV_CHECK_LEN(sizeof(*bts), len);
252 ts->tv_sec = le64_to_cpu(bts->sec);
253 ts->tv_nsec = le32_to_cpu(bts->nsec);
259 #define TLV_GET_TIMESPEC(s, attr, ts) \
260 __TLV_DO_WHILE_GOTO_FAIL(tlv_get_timespec(s, attr, ts))
262 static int tlv_get_uuid(struct btrfs_send_stream *sctx, int attr, u8 *uuid)
268 TLV_GET(sctx, attr, &data, &len);
269 TLV_CHECK_LEN(BTRFS_UUID_SIZE, len);
270 memcpy(uuid, data, BTRFS_UUID_SIZE);
277 #define TLV_GET_UUID(s, attr, uuid) \
278 __TLV_DO_WHILE_GOTO_FAIL(tlv_get_uuid(s, attr, uuid))
280 static int read_and_process_cmd(struct btrfs_send_stream *sctx)
284 char *path_to = NULL;
285 char *clone_path = NULL;
286 char *xattr_name = NULL;
287 void *xattr_data = NULL;
292 u8 uuid[BTRFS_UUID_SIZE];
293 u8 clone_uuid[BTRFS_UUID_SIZE];
305 ret = read_cmd(sctx);
310 case BTRFS_SEND_C_SUBVOL:
311 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
312 TLV_GET_UUID(sctx, BTRFS_SEND_A_UUID, uuid);
313 TLV_GET_U64(sctx, BTRFS_SEND_A_CTRANSID, &ctransid);
314 ret = sctx->ops->subvol(path, uuid, ctransid, sctx->user);
316 case BTRFS_SEND_C_SNAPSHOT:
317 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
318 TLV_GET_UUID(sctx, BTRFS_SEND_A_UUID, uuid);
319 TLV_GET_U64(sctx, BTRFS_SEND_A_CTRANSID, &ctransid);
320 TLV_GET_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, clone_uuid);
321 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, &clone_ctransid);
322 ret = sctx->ops->snapshot(path, uuid, ctransid, clone_uuid,
323 clone_ctransid, sctx->user);
325 case BTRFS_SEND_C_MKFILE:
326 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
327 ret = sctx->ops->mkfile(path, sctx->user);
329 case BTRFS_SEND_C_MKDIR:
330 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
331 ret = sctx->ops->mkdir(path, sctx->user);
333 case BTRFS_SEND_C_MKNOD:
334 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
335 TLV_GET_U64(sctx, BTRFS_SEND_A_MODE, &mode);
336 TLV_GET_U64(sctx, BTRFS_SEND_A_RDEV, &dev);
337 ret = sctx->ops->mknod(path, mode, dev, sctx->user);
339 case BTRFS_SEND_C_MKFIFO:
340 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
341 ret = sctx->ops->mkfifo(path, sctx->user);
343 case BTRFS_SEND_C_MKSOCK:
344 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
345 ret = sctx->ops->mksock(path, sctx->user);
347 case BTRFS_SEND_C_SYMLINK:
348 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
349 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_LINK, &path_to);
350 ret = sctx->ops->symlink(path, path_to, sctx->user);
352 case BTRFS_SEND_C_RENAME:
353 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
354 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_TO, &path_to);
355 ret = sctx->ops->rename(path, path_to, sctx->user);
357 case BTRFS_SEND_C_LINK:
358 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
359 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH_LINK, &path_to);
360 ret = sctx->ops->link(path, path_to, sctx->user);
362 case BTRFS_SEND_C_UNLINK:
363 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
364 ret = sctx->ops->unlink(path, sctx->user);
366 case BTRFS_SEND_C_RMDIR:
367 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
368 ret = sctx->ops->rmdir(path, sctx->user);
370 case BTRFS_SEND_C_WRITE:
371 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
372 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
373 TLV_GET(sctx, BTRFS_SEND_A_DATA, &data, &len);
374 ret = sctx->ops->write(path, data, offset, len, sctx->user);
376 case BTRFS_SEND_C_CLONE:
377 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
378 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
379 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_LEN, &len);
380 TLV_GET_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, clone_uuid);
381 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, &clone_ctransid);
382 TLV_GET_STRING(sctx, BTRFS_SEND_A_CLONE_PATH, &clone_path);
383 TLV_GET_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, &clone_offset);
384 ret = sctx->ops->clone(path, offset, len, clone_uuid,
385 clone_ctransid, clone_path, clone_offset,
388 case BTRFS_SEND_C_SET_XATTR:
389 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
390 TLV_GET_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, &xattr_name);
391 TLV_GET(sctx, BTRFS_SEND_A_XATTR_DATA, &xattr_data, &xattr_len);
392 ret = sctx->ops->set_xattr(path, xattr_name, xattr_data,
393 xattr_len, sctx->user);
395 case BTRFS_SEND_C_REMOVE_XATTR:
396 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
397 TLV_GET_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, &xattr_name);
398 ret = sctx->ops->remove_xattr(path, xattr_name, sctx->user);
400 case BTRFS_SEND_C_TRUNCATE:
401 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
402 TLV_GET_U64(sctx, BTRFS_SEND_A_SIZE, &tmp);
403 ret = sctx->ops->truncate(path, tmp, sctx->user);
405 case BTRFS_SEND_C_CHMOD:
406 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
407 TLV_GET_U64(sctx, BTRFS_SEND_A_MODE, &tmp);
408 ret = sctx->ops->chmod(path, tmp, sctx->user);
410 case BTRFS_SEND_C_CHOWN:
411 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
412 TLV_GET_U64(sctx, BTRFS_SEND_A_UID, &tmp);
413 TLV_GET_U64(sctx, BTRFS_SEND_A_GID, &tmp2);
414 ret = sctx->ops->chown(path, tmp, tmp2, sctx->user);
416 case BTRFS_SEND_C_UTIMES:
417 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
418 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, &at);
419 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, &mt);
420 TLV_GET_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, &ct);
421 ret = sctx->ops->utimes(path, &at, &mt, &ct, sctx->user);
423 case BTRFS_SEND_C_UPDATE_EXTENT:
424 TLV_GET_STRING(sctx, BTRFS_SEND_A_PATH, &path);
425 TLV_GET_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, &offset);
426 TLV_GET_U64(sctx, BTRFS_SEND_A_SIZE, &tmp);
427 ret = sctx->ops->update_extent(path, offset, tmp, sctx->user);
429 case BTRFS_SEND_C_END:
444 * If max_errors is 0, then don't stop processing the stream if one of the
445 * callbacks in btrfs_send_ops structure returns an error. If greater than
446 * zero, stop after max_errors errors happened.
448 int btrfs_read_and_process_send_stream(int fd,
449 struct btrfs_send_ops *ops, void *user,
454 struct btrfs_send_stream sctx;
455 struct btrfs_stream_header hdr;
463 ret = read_buf(&sctx, (char*)&hdr, sizeof(hdr));
471 if (strcmp(hdr.magic, BTRFS_SEND_STREAM_MAGIC)) {
473 error("unexpected header");
477 sctx.version = le32_to_cpu(hdr.version);
478 if (sctx.version > BTRFS_SEND_STREAM_VERSION) {
480 error("stream version %d not supported, please use newer version",
486 ret = read_and_process_cmd(&sctx);
490 if (max_errors > 0 && errors >= max_errors)
492 } else if (ret > 0) {
500 if (last_err && !ret)