1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
|
/*
* Copyright (C) 2012 Alexander Block. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "kerncompat.h"
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/xattr.h>
#include <linux/fs.h>
#if HAVE_LINUX_FSVERITY_H
#include <linux/fsverity.h>
#endif
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <endian.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <zlib.h>
#if COMPRESSION_LZO
#include <lzo/lzoconf.h>
#include <lzo/lzo1x.h>
#endif
#if COMPRESSION_ZSTD
#include <zstd.h>
#endif
#include "kernel-shared/uapi/btrfs.h"
#include "common/defs.h"
#include "common/messages.h"
#include "common/utils.h"
#include "common/send-stream.h"
#include "common/send-utils.h"
#include "common/help.h"
#include "common/path-utils.h"
#include "common/string-utils.h"
#include "cmds/commands.h"
#include "cmds/receive-dump.h"
struct btrfs_receive
{
int mnt_fd;
int dest_dir_fd;
int write_fd;
char write_path[PATH_MAX];
char *root_path;
char *dest_dir_path; /* relative to root_path */
char full_subvol_path[PATH_MAX];
char *full_root_path;
struct subvol_info cur_subvol;
/*
* Substitute for cur_subvol::path which is a pointer and we cannot
* change it to an array as it's a public API.
*/
char cur_subvol_path[PATH_MAX];
bool dest_dir_chroot;
bool honor_end_cmd;
bool force_decompress;
#if COMPRESSION_ZSTD
/* Reuse stream objects for encoded_write decompression fallback */
ZSTD_DStream *zstd_dstream;
#endif
z_stream *zlib_stream;
};
static int finish_subvol(struct btrfs_receive *rctx)
{
int ret;
int subvol_fd = -1;
struct btrfs_ioctl_received_subvol_args rs_args;
char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
u64 flags;
if (rctx->cur_subvol_path[0] == 0)
return 0;
subvol_fd = openat(rctx->mnt_fd, rctx->cur_subvol_path,
O_RDONLY | O_NOATIME);
if (subvol_fd < 0) {
ret = -errno;
error("cannot open %s: %m", rctx->cur_subvol_path);
goto out;
}
memset(&rs_args, 0, sizeof(rs_args));
memcpy(rs_args.uuid, rctx->cur_subvol.received_uuid, BTRFS_UUID_SIZE);
rs_args.stransid = rctx->cur_subvol.stransid;
if (bconf.verbose >= 2) {
uuid_unparse((u8*)rs_args.uuid, uuid_str);
fprintf(stderr, "BTRFS_IOC_SET_RECEIVED_SUBVOL uuid=%s, "
"stransid=%llu\n", uuid_str, rs_args.stransid);
}
ret = ioctl(subvol_fd, BTRFS_IOC_SET_RECEIVED_SUBVOL, &rs_args);
if (ret < 0) {
ret = -errno;
error("ioctl BTRFS_IOC_SET_RECEIVED_SUBVOL failed: %m");
goto out;
}
rctx->cur_subvol.rtransid = rs_args.rtransid;
ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
if (ret < 0) {
ret = -errno;
error("ioctl BTRFS_IOC_SUBVOL_GETFLAGS failed: %m");
goto out;
}
flags |= BTRFS_SUBVOL_RDONLY;
ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
if (ret < 0) {
ret = -errno;
error("failed to make subvolume read only: %m");
goto out;
}
ret = 0;
out:
if (rctx->cur_subvol_path[0]) {
rctx->cur_subvol_path[0] = 0;
}
if (subvol_fd != -1)
close(subvol_fd);
return ret;
}
static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
void *user)
{
int ret;
struct btrfs_receive *rctx = user;
struct btrfs_ioctl_vol_args args_v1;
char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
ret = finish_subvol(rctx);
if (ret < 0)
goto out;
if (rctx->cur_subvol.path) {
error("subvol: another one already started, path ptr: %s",
rctx->cur_subvol.path);
ret = -EINVAL;
goto out;
}
if (rctx->cur_subvol_path[0]) {
error("subvol: another one already started, path buf: %s",
rctx->cur_subvol_path);
ret = -EINVAL;
goto out;
}
if (*rctx->dest_dir_path == 0) {
strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path);
if (ret < 0) {
error("subvol: path invalid: %s", path);
goto out;
}
}
ret = path_cat3_out(rctx->full_subvol_path, rctx->root_path,
rctx->dest_dir_path, path);
if (ret < 0) {
error("subvol: path invalid: %s", path);
goto out;
}
if (bconf.verbose > BTRFS_BCONF_QUIET)
fprintf(stderr, "At subvol %s\n", path);
memcpy(rctx->cur_subvol.received_uuid, uuid, BTRFS_UUID_SIZE);
rctx->cur_subvol.stransid = ctransid;
if (bconf.verbose >= 2) {
uuid_unparse((u8*)rctx->cur_subvol.received_uuid, uuid_str);
fprintf(stderr, "receiving subvol %s uuid=%s, stransid=%llu\n",
path, uuid_str,
rctx->cur_subvol.stransid);
}
memset(&args_v1, 0, sizeof(args_v1));
strncpy_null(args_v1.name, path, sizeof(args_v1.name));
ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1);
if (ret < 0) {
ret = -errno;
error("creating subvolume %s failed: %m", path);
goto out;
}
out:
return ret;
}
/*
* Search for the @subvol_uuid, try received_uuid and subvolume as a fallback
* if it's not found.
*/
static struct subvol_info *search_source_subvol(int mnt_fd, const u8 *subvol_uuid,
u64 transid)
{
struct subvol_info *found;
found = subvol_uuid_search(mnt_fd, 0, subvol_uuid, transid, NULL,
subvol_search_by_received_uuid);
if (IS_ERR_OR_NULL(found)) {
found = subvol_uuid_search(mnt_fd, 0, subvol_uuid, transid,
NULL, subvol_search_by_uuid);
}
return found;
}
static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
const u8 *parent_uuid, u64 parent_ctransid,
void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
struct btrfs_ioctl_vol_args_v2 args_v2;
struct subvol_info *parent_subvol = NULL;
ret = finish_subvol(rctx);
if (ret < 0)
goto out;
if (rctx->cur_subvol.path) {
error("snapshot: another one already started, path ptr: %s",
rctx->cur_subvol.path);
ret = -EINVAL;
goto out;
}
if (rctx->cur_subvol_path[0]) {
error("snapshot: another one already started, path buf: %s",
rctx->cur_subvol_path);
ret = -EINVAL;
goto out;
}
if (*rctx->dest_dir_path == 0) {
strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path);
if (ret < 0) {
error("snapshot: path invalid: %s", path);
goto out;
}
}
ret = path_cat3_out(rctx->full_subvol_path, rctx->root_path,
rctx->dest_dir_path, path);
if (ret < 0) {
error("snapshot: path invalid: %s", path);
goto out;
}
pr_verbose(1, "At snapshot %s\n", path);
memcpy(rctx->cur_subvol.received_uuid, uuid, BTRFS_UUID_SIZE);
rctx->cur_subvol.stransid = ctransid;
if (bconf.verbose >= 2) {
uuid_unparse((u8*)rctx->cur_subvol.received_uuid, uuid_str);
fprintf(stderr, "receiving snapshot %s uuid=%s, "
"ctransid=%llu ", path, uuid_str,
rctx->cur_subvol.stransid);
uuid_unparse(parent_uuid, uuid_str);
fprintf(stderr, "parent_uuid=%s, parent_ctransid=%llu\n",
uuid_str, parent_ctransid);
}
memset(&args_v2, 0, sizeof(args_v2));
strncpy_null(args_v2.name, path, sizeof(args_v2.name));
parent_subvol = search_source_subvol(rctx->mnt_fd, parent_uuid,
parent_ctransid);
if (IS_ERR_OR_NULL(parent_subvol)) {
if (!parent_subvol)
ret = -ENOENT;
else
ret = PTR_ERR(parent_subvol);
uuid_unparse(parent_uuid, uuid_str);
error("snapshot receive: cannot find parent subvolume %s", uuid_str);
goto out;
}
/*
* The path is resolved from the root subvol, but we could be in some
* subvolume under the root subvolume, so try and adjust the path to be
* relative to our root path.
*/
if (rctx->full_root_path) {
size_t root_len;
size_t sub_len;
root_len = strlen(rctx->full_root_path);
sub_len = strlen(parent_subvol->path);
/* First make sure the parent subvol is actually in our path */
if (strstr(parent_subvol->path, rctx->full_root_path) != parent_subvol->path ||
(sub_len > root_len && parent_subvol->path[root_len] != '/')) {
error(
"parent subvol is not reachable from inside the root subvol");
ret = -ENOENT;
goto out;
}
if (sub_len == root_len) {
parent_subvol->path[0] = '.';
parent_subvol->path[1] = '\0';
} else {
/*
* root path is foo/bar
* subvol path is foo/bar/baz
*
* we need to have baz be the path, so we need to move
* the bit after foo/bar/, so path + root_len + 1, and
* move the part we care about, so sub_len - root_len -
* 1.
*/
memmove(parent_subvol->path,
parent_subvol->path + root_len + 1,
sub_len - root_len - 1);
parent_subvol->path[sub_len - root_len - 1] = '\0';
}
}
if (*parent_subvol->path == 0)
args_v2.fd = dup(rctx->mnt_fd);
else
args_v2.fd = openat(rctx->mnt_fd, parent_subvol->path,
O_RDONLY | O_NOATIME);
if (args_v2.fd < 0) {
ret = -errno;
if (errno != ENOENT)
error("cannot open %s: %m", parent_subvol->path);
else
fprintf(stderr,
"It seems that you have changed your default "
"subvolume or you specify other subvolume to\n"
"mount btrfs, try to remount this btrfs filesystem "
"with fs tree, and run btrfs receive again!\n");
goto out;
}
ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SNAP_CREATE_V2, &args_v2);
close(args_v2.fd);
if (ret < 0) {
ret = -errno;
error("creating snapshot %s -> %s failed: %m",
parent_subvol->path, path);
goto out;
}
out:
if (!IS_ERR_OR_NULL(parent_subvol)) {
free(parent_subvol->path);
free(parent_subvol);
}
return ret;
}
static int process_mkfile(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("mkfile: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "mkfile %s\n", path);
ret = creat(full_path, 0600);
if (ret < 0) {
ret = -errno;
error("mkfile %s failed: %m", path);
goto out;
}
close(ret);
ret = 0;
out:
return ret;
}
static int process_mkdir(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("mkdir: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "mkdir %s\n", path);
ret = mkdir(full_path, 0700);
if (ret < 0) {
ret = -errno;
error("mkdir %s failed: %m", path);
}
out:
return ret;
}
static int process_mknod(const char *path, u64 mode, u64 dev, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("mknod: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "mknod %s mode=%llu, dev=%llu\n",
path, mode, dev);
ret = mknod(full_path, mode & S_IFMT, dev);
if (ret < 0) {
ret = -errno;
error("mknod %s failed: %m", path);
}
out:
return ret;
}
static int process_mkfifo(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("mkfifo: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "mkfifo %s\n", path);
ret = mkfifo(full_path, 0600);
if (ret < 0) {
ret = -errno;
error("mkfifo %s failed: %m", path);
}
out:
return ret;
}
static int process_mksock(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("mksock: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "mksock %s\n", path);
ret = mknod(full_path, 0600 | S_IFSOCK, 0);
if (ret < 0) {
ret = -errno;
error("mknod %s failed: %m", path);
}
out:
return ret;
}
static int process_symlink(const char *path, const char *lnk, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("symlink: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "symlink %s -> %s\n", path, lnk);
ret = symlink(lnk, full_path);
if (ret < 0) {
ret = -errno;
error("symlink %s -> %s failed: %m", path, lnk);
}
out:
return ret;
}
static int process_rename(const char *from, const char *to, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_from[PATH_MAX];
char full_to[PATH_MAX];
ret = path_cat_out(full_from, rctx->full_subvol_path, from);
if (ret < 0) {
error("rename: source path invalid: %s", from);
goto out;
}
ret = path_cat_out(full_to, rctx->full_subvol_path, to);
if (ret < 0) {
error("rename: target path invalid: %s", to);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "rename %s -> %s\n", from, to);
ret = rename(full_from, full_to);
if (ret < 0) {
ret = -errno;
error("rename %s -> %s failed: %m", from, to);
}
out:
return ret;
}
static int process_link(const char *path, const char *lnk, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
char full_link_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("link: source path invalid: %s", full_path);
goto out;
}
ret = path_cat_out(full_link_path, rctx->full_subvol_path, lnk);
if (ret < 0) {
error("link: target path invalid: %s", full_link_path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "link %s -> %s\n", path, lnk);
ret = link(full_link_path, full_path);
if (ret < 0) {
ret = -errno;
error("link %s -> %s failed: %m", path, lnk);
}
out:
return ret;
}
static int process_unlink(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("unlink: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "unlink %s\n", path);
ret = unlink(full_path);
if (ret < 0) {
ret = -errno;
error("unlink %s failed: %m", path);
}
out:
return ret;
}
static int process_rmdir(const char *path, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("rmdir: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "rmdir %s\n", path);
ret = rmdir(full_path);
if (ret < 0) {
ret = -errno;
error("rmdir %s failed: %m", path);
}
out:
return ret;
}
static int open_inode_for_write(struct btrfs_receive *rctx, const char *path)
{
int ret = 0;
if (rctx->write_fd != -1) {
if (strcmp(rctx->write_path, path) == 0)
goto out;
close(rctx->write_fd);
rctx->write_fd = -1;
}
rctx->write_fd = open(path, O_RDWR);
if (rctx->write_fd < 0) {
ret = -errno;
error("cannot open %s: %m", path);
goto out;
}
strncpy_null(rctx->write_path, path, sizeof(rctx->write_path));
out:
return ret;
}
static void close_inode_for_write(struct btrfs_receive *rctx)
{
if(rctx->write_fd == -1)
return;
close(rctx->write_fd);
rctx->write_fd = -1;
rctx->write_path[0] = 0;
}
static int process_write(const char *path, const void *data, u64 offset,
u64 len, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
u64 pos = 0;
int w;
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("write: path invalid: %s", path);
goto out;
}
ret = open_inode_for_write(rctx, full_path);
if (ret < 0)
goto out;
if (bconf.verbose >= 2)
fprintf(stderr, "write %s - offset=%llu length=%llu\n",
path, offset, len);
while (pos < len) {
w = pwrite(rctx->write_fd, (char*)data + pos, len - pos,
offset + pos);
if (w < 0) {
ret = -errno;
error("writing to %s failed: %m", path);
goto out;
}
pos += w;
}
out:
return ret;
}
static int process_clone(const char *path, u64 offset, u64 len,
const u8 *clone_uuid, u64 clone_ctransid,
const char *clone_path, u64 clone_offset,
void *user)
{
int ret;
struct btrfs_receive *rctx = user;
struct btrfs_ioctl_clone_range_args clone_args;
struct subvol_info *si = NULL;
char full_path[PATH_MAX];
const char *subvol_path;
char full_clone_path[PATH_MAX];
int clone_fd = -1;
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("clone: source path invalid: %s", path);
goto out;
}
ret = open_inode_for_write(rctx, full_path);
if (ret < 0)
goto out;
if (memcmp(clone_uuid, rctx->cur_subvol.received_uuid,
BTRFS_UUID_SIZE) == 0) {
subvol_path = rctx->cur_subvol_path;
} else {
si = search_source_subvol(rctx->mnt_fd, clone_uuid,
clone_ctransid);
if (IS_ERR_OR_NULL(si)) {
char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
if (!si)
ret = -ENOENT;
else
ret = PTR_ERR(si);
uuid_unparse(clone_uuid, uuid_str);
error("clone: cannot find source subvol %s", uuid_str);
goto out;
}
/* strip the subvolume that we are receiving to from the start of subvol_path */
if (rctx->full_root_path) {
size_t root_len = strlen(rctx->full_root_path);
size_t sub_len = strlen(si->path);
if (sub_len > root_len &&
strstr(si->path, rctx->full_root_path) == si->path &&
si->path[root_len] == '/') {
subvol_path = si->path + root_len + 1;
} else {
error("clone: source subvol path %s unreachable from %s",
si->path, rctx->full_root_path);
goto out;
}
} else {
subvol_path = si->path;
}
}
ret = path_cat_out(full_clone_path, subvol_path, clone_path);
if (ret < 0) {
error("clone: target path invalid: %s", clone_path);
goto out;
}
clone_fd = openat(rctx->mnt_fd, full_clone_path, O_RDONLY | O_NOATIME);
if (clone_fd < 0) {
ret = -errno;
error("cannot open %s: %m", full_clone_path);
goto out;
}
if (bconf.verbose >= 2)
fprintf(stderr,
"clone %s - source=%s source offset=%llu offset=%llu length=%llu\n",
path, clone_path, clone_offset, offset, len);
clone_args.src_fd = clone_fd;
clone_args.src_offset = clone_offset;
clone_args.src_length = len;
clone_args.dest_offset = offset;
ret = ioctl(rctx->write_fd, BTRFS_IOC_CLONE_RANGE, &clone_args);
if (ret < 0) {
ret = -errno;
error("failed to clone extents to %s: %m", path);
goto out;
}
out:
if (!IS_ERR_OR_NULL(si)) {
free(si->path);
free(si);
}
if (clone_fd != -1)
close(clone_fd);
return ret;
}
static int process_set_xattr(const char *path, const char *name,
const void *data, int len, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("set_xattr: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3) {
fprintf(stderr, "set_xattr %s - name=%s data_len=%d "
"data=%.*s\n", path, name, len,
len, (char*)data);
}
ret = lsetxattr(full_path, name, data, len, 0);
if (ret < 0) {
ret = -errno;
error("lsetxattr %s %s=%.*s failed: %m",
path, name, len, (char*)data);
goto out;
}
out:
return ret;
}
static int process_remove_xattr(const char *path, const char *name, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("remove_xattr: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3) {
fprintf(stderr, "remove_xattr %s - name=%s\n",
path, name);
}
ret = lremovexattr(full_path, name);
if (ret < 0) {
ret = -errno;
error("lremovexattr %s %s failed: %m", path, name);
goto out;
}
out:
return ret;
}
static int process_truncate(const char *path, u64 size, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("truncate: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "truncate %s size=%llu\n", path, size);
ret = truncate(full_path, size);
if (ret < 0) {
ret = -errno;
error("truncate %s failed: %m", path);
goto out;
}
out:
return ret;
}
static int process_chmod(const char *path, u64 mode, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("chmod: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "chmod %s - mode=0%o\n", path, (int)mode);
ret = chmod(full_path, mode);
if (ret < 0) {
ret = -errno;
error("chmod %s failed: %m", path);
goto out;
}
out:
return ret;
}
static int process_chown(const char *path, u64 uid, u64 gid, void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("chown: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "chown %s - uid=%llu, gid=%llu\n", path,
uid, gid);
ret = lchown(full_path, uid, gid);
if (ret < 0) {
ret = -errno;
error("chown %s failed: %m", path);
goto out;
}
out:
return ret;
}
static int process_utimes(const char *path, struct timespec *at,
struct timespec *mt, struct timespec *ct,
void *user)
{
int ret = 0;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
struct timespec tv[2];
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("utimes: path invalid: %s", path);
goto out;
}
if (bconf.verbose >= 3)
fprintf(stderr, "utimes %s\n", path);
tv[0] = *at;
tv[1] = *mt;
ret = utimensat(AT_FDCWD, full_path, tv, AT_SYMLINK_NOFOLLOW);
if (ret < 0) {
ret = -errno;
error("utimes %s failed: %m", path);
goto out;
}
out:
return ret;
}
static int process_update_extent(const char *path, u64 offset, u64 len,
void *user)
{
if (bconf.verbose >= 3)
fprintf(stderr, "update_extent %s: offset=%llu, len=%llu\n",
path, offset, len);
/*
* Sent with BTRFS_SEND_FLAG_NO_FILE_DATA, nothing to do.
*/
return 0;
}
static int decompress_zlib(struct btrfs_receive *rctx, const char *encoded_data,
u64 encoded_len, char *unencoded_data,
u64 unencoded_len)
{
bool init = false;
int ret;
if (!rctx->zlib_stream) {
init = true;
rctx->zlib_stream = malloc(sizeof(z_stream));
if (!rctx->zlib_stream) {
error_msg(ERROR_MSG_MEMORY, "zlib stream: %m");
return -ENOMEM;
}
}
rctx->zlib_stream->next_in = (void *)encoded_data;
rctx->zlib_stream->avail_in = encoded_len;
rctx->zlib_stream->next_out = (void *)unencoded_data;
rctx->zlib_stream->avail_out = unencoded_len;
if (init) {
rctx->zlib_stream->zalloc = Z_NULL;
rctx->zlib_stream->zfree = Z_NULL;
rctx->zlib_stream->opaque = Z_NULL;
ret = inflateInit(rctx->zlib_stream);
} else {
ret = inflateReset(rctx->zlib_stream);
}
if (ret != Z_OK) {
error("zlib inflate init failed: %d", ret);
return -EIO;
}
while (rctx->zlib_stream->avail_in > 0 &&
rctx->zlib_stream->avail_out > 0) {
ret = inflate(rctx->zlib_stream, Z_FINISH);
if (ret == Z_STREAM_END) {
break;
} else if (ret != Z_OK) {
error("zlib inflate failed: %d", ret);
return -EIO;
}
}
return 0;
}
#if COMPRESSION_ZSTD
static int decompress_zstd(struct btrfs_receive *rctx, const char *encoded_buf,
u64 encoded_len, char *unencoded_buf,
u64 unencoded_len)
{
ZSTD_inBuffer in_buf = {
.src = encoded_buf,
.size = encoded_len
};
ZSTD_outBuffer out_buf = {
.dst = unencoded_buf,
.size = unencoded_len
};
size_t ret;
if (!rctx->zstd_dstream) {
rctx->zstd_dstream = ZSTD_createDStream();
if (!rctx->zstd_dstream) {
error("failed to create zstd dstream");
return -ENOMEM;
}
}
ret = ZSTD_initDStream(rctx->zstd_dstream);
if (ZSTD_isError(ret)) {
error("failed to init zstd stream: %s", ZSTD_getErrorName(ret));
return -EIO;
}
while (in_buf.pos < in_buf.size && out_buf.pos < out_buf.size) {
ret = ZSTD_decompressStream(rctx->zstd_dstream, &out_buf, &in_buf);
if (ret == 0) {
break;
} else if (ZSTD_isError(ret)) {
error("failed to decompress zstd stream: %s",
ZSTD_getErrorName(ret));
return -EIO;
}
}
/*
* Zero out the unused part of the output buffer.
* At least with zstd 1.5.2, the decompression can leave some garbage
* at/beyond the offset out_buf.pos.
*/
if (out_buf.pos < out_buf.size)
memset(unencoded_buf + out_buf.pos, 0, out_buf.size - out_buf.pos);
return 0;
}
#endif
#if COMPRESSION_LZO
static int decompress_lzo(const char *encoded_data, u64 encoded_len,
char *unencoded_data, u64 unencoded_len,
unsigned int sector_size)
{
uint32_t total_len;
size_t in_pos, out_pos;
if (encoded_len < 4) {
error("lzo header is truncated");
return -EIO;
}
memcpy(&total_len, encoded_data, 4);
total_len = le32toh(total_len);
if (total_len > encoded_len) {
error("lzo header is invalid");
return -EIO;
}
in_pos = 4;
out_pos = 0;
while (in_pos < total_len && out_pos < unencoded_len) {
size_t sector_remaining;
uint32_t src_len;
lzo_uint dst_len;
int ret;
sector_remaining = -in_pos % sector_size;
if (sector_remaining < 4) {
if (total_len - in_pos <= sector_remaining)
break;
in_pos += sector_remaining;
}
if (total_len - in_pos < 4) {
error("lzo segment header is truncated");
return -EIO;
}
memcpy(&src_len, encoded_data + in_pos, 4);
src_len = le32toh(src_len);
in_pos += 4;
if (src_len > total_len - in_pos) {
error("lzo segment header is invalid");
return -EIO;
}
dst_len = sector_size;
ret = lzo1x_decompress_safe((void *)(encoded_data + in_pos),
src_len,
(void *)(unencoded_data + out_pos),
&dst_len, NULL);
if (ret != LZO_E_OK) {
error("lzo1x_decompress_safe failed: %d", ret);
return -EIO;
}
in_pos += src_len;
out_pos += dst_len;
}
return 0;
}
#endif
static int decompress_and_write(struct btrfs_receive *rctx,
const char *encoded_data, u64 offset,
u64 encoded_len, u64 unencoded_file_len,
u64 unencoded_len, u64 unencoded_offset,
u32 compression)
{
int ret = 0;
char *unencoded_data;
int sector_shift = 0;
u64 written = 0;
unencoded_data = calloc(unencoded_len, 1);
if (!unencoded_data) {
error_msg(ERROR_MSG_MEMORY, "unencoded data: %m");
return -errno;
}
switch (compression) {
case BTRFS_ENCODED_IO_COMPRESSION_ZLIB:
ret = decompress_zlib(rctx, encoded_data, encoded_len,
unencoded_data, unencoded_len);
if (ret)
goto out;
break;
case BTRFS_ENCODED_IO_COMPRESSION_ZSTD:
#if COMPRESSION_ZSTD
ret = decompress_zstd(rctx, encoded_data, encoded_len,
unencoded_data, unencoded_len);
if (ret)
goto out;
break;
#else
error("ZSTD compression for stream not compiled in");
ret = -EOPNOTSUPP;
goto out;
#endif
case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K:
case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K:
case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K:
case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K:
case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K:
#if COMPRESSION_LZO
sector_shift =
compression - BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12;
ret = decompress_lzo(encoded_data, encoded_len, unencoded_data,
unencoded_len, 1U << sector_shift);
if (ret)
goto out;
break;
#else
error("LZO compression for stream not compiled in");
ret = -EOPNOTSUPP;
goto out;
#endif
default:
error("unknown compression: %d", compression);
ret = -EOPNOTSUPP;
goto out;
}
while (written < unencoded_file_len) {
ssize_t w;
w = pwrite(rctx->write_fd, unencoded_data + unencoded_offset,
unencoded_file_len - written, offset);
if (w < 0) {
ret = -errno;
error("writing unencoded data failed: %m");
goto out;
}
written += w;
offset += w;
unencoded_offset += w;
}
out:
free(unencoded_data);
return ret;
}
static int process_encoded_write(const char *path, const void *data, u64 offset,
u64 len, u64 unencoded_file_len,
u64 unencoded_len, u64 unencoded_offset,
u32 compression, u32 encryption, void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
struct iovec iov = { (char *)data, len };
struct btrfs_ioctl_encoded_io_args encoded = {
.iov = &iov,
.iovcnt = 1,
.offset = offset,
.len = unencoded_file_len,
.unencoded_len = unencoded_len,
.unencoded_offset = unencoded_offset,
.compression = compression,
.encryption = encryption,
};
if (bconf.verbose >= 3)
fprintf(stderr,
"encoded_write %s - offset=%llu, len=%llu, unencoded_offset=%llu, unencoded_file_len=%llu, unencoded_len=%llu, compression=%u, encryption=%u\n",
path, offset, len, unencoded_offset, unencoded_file_len,
unencoded_len, compression, encryption);
if (encryption) {
error("encoded_write: encryption not supported");
return -EOPNOTSUPP;
}
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("encoded_write: path invalid: %s", path);
return ret;
}
ret = open_inode_for_write(rctx, full_path);
if (ret < 0)
return ret;
if (!rctx->force_decompress) {
ret = ioctl(rctx->write_fd, BTRFS_IOC_ENCODED_WRITE, &encoded);
if (ret >= 0)
return 0;
/* Fall back for these errors, fail hard for anything else. */
if (errno != ENOSPC && errno != ENOTTY && errno != EINVAL) {
ret = -errno;
error("encoded_write: writing to %s failed: %m", path);
return ret;
}
if (bconf.verbose >= 3)
fprintf(stderr,
"encoded_write %s - falling back to decompress and write due to errno %d (\"%m\")\n",
path, errno);
}
return decompress_and_write(rctx, data, offset, len, unencoded_file_len,
unencoded_len, unencoded_offset,
compression);
}
static int process_fallocate(const char *path, int mode, u64 offset, u64 len,
void *user)
{
int ret;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
if (bconf.verbose >= 3)
fprintf(stderr,
"fallocate %s - offset=%llu, len=%llu, mode=%d\n",
path, offset, len, mode);
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("fallocate: path invalid: %s", path);
return ret;
}
ret = open_inode_for_write(rctx, full_path);
if (ret < 0)
return ret;
ret = fallocate(rctx->write_fd, mode, offset, len);
if (ret < 0) {
ret = -errno;
error("fallocate: fallocate on %s failed: %m", path);
return ret;
}
return 0;
}
static int process_fileattr(const char *path, u64 attr, void *user)
{
/*
* Not yet supported, ignored for now, just like in send stream v1.
* The content of 'attr' matches the flags in the btrfs inode item,
* we can't apply them directly with FS_IOC_SETFLAGS, as we need to
* convert them from BTRFS_INODE_* flags to FS_* flags. Plus some
* flags are special and must be applied in a special way.
* The commented code below therefore does not work.
*/
/* int ret; */
/* struct btrfs_receive *rctx = user; */
/* char full_path[PATH_MAX]; */
/* ret = path_cat_out(full_path, rctx->full_subvol_path, path); */
/* if (ret < 0) { */
/* error("fileattr: path invalid: %s", path); */
/* return ret; */
/* } */
/* ret = open_inode_for_write(rctx, full_path); */
/* if (ret < 0) */
/* return ret; */
/* ret = ioctl(rctx->write_fd, FS_IOC_SETFLAGS, &attr); */
/* if (ret < 0) { */
/* ret = -errno; */
/* error("fileattr: set file attributes on %s failed: %m", path); */
/* return ret; */
/* } */
return 0;
}
#if HAVE_LINUX_FSVERITY_H
static int process_enable_verity(const char *path, u8 algorithm, u32 block_size,
int salt_len, char *salt,
int sig_len, char *sig, void *user)
{
int ret;
int ioctl_fd;
struct btrfs_receive *rctx = user;
char full_path[PATH_MAX];
struct fsverity_enable_arg verity_args = {
.version = 1,
.hash_algorithm = algorithm,
.block_size = block_size,
};
if (salt_len) {
verity_args.salt_size = salt_len;
verity_args.salt_ptr = (__u64)(uintptr_t)salt;
}
if (sig_len) {
verity_args.sig_size = sig_len;
verity_args.sig_ptr = (__u64)(uintptr_t)sig;
}
ret = path_cat_out(full_path, rctx->full_subvol_path, path);
if (ret < 0) {
error("write: path invalid: %s", path);
goto out;
}
ioctl_fd = open(full_path, O_RDONLY);
if (ioctl_fd < 0) {
ret = -errno;
error("cannot open %s for verity ioctl: %m", full_path);
goto out;
}
/*
* Enabling verity denies write access, so it cannot be done with an
* open writeable file descriptor.
*/
close_inode_for_write(rctx);
ret = ioctl(ioctl_fd, FS_IOC_ENABLE_VERITY, &verity_args);
if (ret < 0) {
ret = -errno;
error("failed to enable verity on %s: %d", full_path, ret);
}
close(ioctl_fd);
out:
return ret;
}
#else
static int process_enable_verity(const char *path, u8 algorithm, u32 block_size,
int salt_len, char *salt,
int sig_len, char *sig, void *user)
{
error("fs-verity for stream not compiled in");
return -EOPNOTSUPP;
}
#endif
static struct btrfs_send_ops send_ops = {
.subvol = process_subvol,
.snapshot = process_snapshot,
.mkfile = process_mkfile,
.mkdir = process_mkdir,
.mknod = process_mknod,
.mkfifo = process_mkfifo,
.mksock = process_mksock,
.symlink = process_symlink,
.rename = process_rename,
.link = process_link,
.unlink = process_unlink,
.rmdir = process_rmdir,
.write = process_write,
.clone = process_clone,
.set_xattr = process_set_xattr,
.remove_xattr = process_remove_xattr,
.truncate = process_truncate,
.chmod = process_chmod,
.chown = process_chown,
.utimes = process_utimes,
.update_extent = process_update_extent,
.encoded_write = process_encoded_write,
.fallocate = process_fallocate,
.fileattr = process_fileattr,
.enable_verity = process_enable_verity,
};
static int do_receive(struct btrfs_receive *rctx, const char *tomnt,
char *realmnt, int r_fd, u64 max_errors)
{
u64 subvol_id;
int ret;
char *dest_dir_full_path;
char root_subvol_path[PATH_MAX];
bool end = false;
int iterations = 0;
dest_dir_full_path = realpath(tomnt, NULL);
if (!dest_dir_full_path) {
ret = -errno;
error("realpath(%s) failed: %m", tomnt);
goto out;
}
rctx->dest_dir_fd = open(dest_dir_full_path, O_RDONLY | O_NOATIME);
if (rctx->dest_dir_fd < 0) {
ret = -errno;
error("cannot open destination directory %s: %m",
dest_dir_full_path);
goto out;
}
if (realmnt[0]) {
rctx->root_path = realmnt;
} else {
ret = find_mount_root(dest_dir_full_path, &rctx->root_path);
if (ret < 0) {
errno = -ret;
error("failed to determine mount point for %s: %m",
dest_dir_full_path);
ret = -EINVAL;
goto out;
}
if (ret > 0) {
error("%s doesn't belong to btrfs mount point",
dest_dir_full_path);
ret = -EINVAL;
goto out;
}
}
rctx->mnt_fd = open(rctx->root_path, O_RDONLY | O_NOATIME);
if (rctx->mnt_fd < 0) {
ret = -errno;
error("cannot open %s: %m", rctx->root_path);
goto out;
}
/*
* If we use -m or a default subvol we want to resolve the path to the
* subvolume we're sitting in so that we can adjust the paths of any
* subvols we want to receive in.
*/
ret = lookup_path_rootid(rctx->mnt_fd, &subvol_id);
if (ret) {
errno = -ret;
error("cannot resolve rootid for path: %m");
goto out;
}
root_subvol_path[0] = 0;
ret = btrfs_subvolid_resolve(rctx->mnt_fd, root_subvol_path,
PATH_MAX, subvol_id);
if (ret) {
error("cannot resolve our subvol path");
goto out;
}
/*
* Ok we're inside of a subvol off of the root subvol, we need to
* actually set full_root_path.
*/
if (*root_subvol_path)
rctx->full_root_path = root_subvol_path;
if (rctx->dest_dir_chroot) {
if (chroot(dest_dir_full_path)) {
ret = -errno;
error("failed to chroot to %s: %m", dest_dir_full_path);
goto out;
}
if (chdir("/")) {
ret = -errno;
error("failed to chdir to / after chroot: %m");
goto out;
}
if (bconf.verbose > BTRFS_BCONF_QUIET)
fprintf(stderr, "Chroot to %s\n", dest_dir_full_path);
rctx->root_path = strdup("/");
rctx->dest_dir_path = rctx->root_path;
} else {
/*
* find_mount_root returns a root_path that is a subpath of
* dest_dir_full_path. Now get the other part of root_path,
* which is the destination dir relative to root_path.
*/
rctx->dest_dir_path = dest_dir_full_path + strlen(rctx->root_path);
while (rctx->dest_dir_path[0] == '/')
rctx->dest_dir_path++;
}
while (!end) {
ret = btrfs_read_and_process_send_stream(r_fd, &send_ops,
rctx,
rctx->honor_end_cmd,
max_errors);
if (ret < 0) {
if (ret != -ENODATA)
goto out;
/* Empty stream is invalid */
if (iterations == 0) {
error("empty stream is not considered valid");
ret = -EINVAL;
goto out;
}
ret = 1;
}
if (ret > 0)
end = true;
close_inode_for_write(rctx);
ret = finish_subvol(rctx);
if (ret < 0)
goto out;
iterations++;
}
ret = 0;
out:
if (rctx->write_fd != -1) {
close(rctx->write_fd);
rctx->write_fd = -1;
}
if (rctx->root_path != realmnt)
free(rctx->root_path);
rctx->root_path = NULL;
rctx->dest_dir_path = NULL;
free(dest_dir_full_path);
if (rctx->mnt_fd != -1) {
close(rctx->mnt_fd);
rctx->mnt_fd = -1;
}
if (rctx->dest_dir_fd != -1) {
close(rctx->dest_dir_fd);
rctx->dest_dir_fd = -1;
}
#if COMPRESSION_ZSTD
if (rctx->zstd_dstream)
ZSTD_freeDStream(rctx->zstd_dstream);
#endif
if (rctx->zlib_stream) {
inflateEnd(rctx->zlib_stream);
free(rctx->zlib_stream);
}
return ret;
}
static const char * const cmd_receive_usage[] = {
"btrfs receive [options] <mount>\n"
"btrfs receive --dump [options]",
"Receive subvolumes from a stream",
"Receives one or more subvolumes that were previously",
"sent with btrfs send. The received subvolumes are stored",
"into MOUNT.",
"The receive will fail in case the receiving subvolume",
"already exists. It will also fail in case a previously",
"received subvolume has been changed after it was received.",
"After receiving a subvolume, it is immediately set to",
"read-only.",
"",
OPTLINE("-q|--quiet", "suppress all messages, except errors"),
OPTLINE("-f FILE", "read the stream from FILE instead of stdin"),
OPTLINE("-e", "terminate after receiving an <end cmd> marker in the stream. "
"Without this option the receiver side terminates only in case "
"of an error on end of file."),
OPTLINE("-C|--chroot", "confine the process to <mount> using chroot"),
OPTLINE("-E|--max-errors NERR", "terminate as soon as NERR errors occur while "
"stream processing commands from the stream. "
"Default value is 1. A value of 0 means no limit."),
OPTLINE("-m ROOTMOUNT", "the root mount point of the destination filesystem. "
"If /proc is not accessible, use this to tell us where "
"this file system is mounted."),
OPTLINE("--force-decompress", "if the stream contains compressed data, always "
"decompress it instead of writing it with encoded I/O"),
OPTLINE("--dump", "dump stream metadata, one line per operation, "
"does not require the MOUNT parameter"),
OPTLINE("-v", "deprecated, alias for global -v option"),
HELPINFO_INSERT_GLOBALS,
HELPINFO_INSERT_VERBOSE,
HELPINFO_INSERT_QUIET,
"",
"Compression support: zlib"
#if COMPRESSION_LZO
", lzo"
#endif
#if COMPRESSION_ZSTD
", zstd"
#endif
,
"Feature support:"
#if HAVE_LINUX_FSVERITY_H
" fsverity"
#endif
,
NULL
};
static int cmd_receive(const struct cmd_struct *cmd, int argc, char **argv)
{
char *tomnt = NULL;
char fromfile[PATH_MAX];
char realmnt[PATH_MAX];
struct btrfs_receive rctx;
int receive_fd = fileno(stdin);
u64 max_errors = 1;
bool dump = false;
int ret = 0;
memset(&rctx, 0, sizeof(rctx));
rctx.mnt_fd = -1;
rctx.write_fd = -1;
rctx.dest_dir_fd = -1;
rctx.dest_dir_chroot = false;
realmnt[0] = 0;
fromfile[0] = 0;
/*
* Init global verbose to default, if it is unset.
* Default is 1 for historical reasons, changing may break scripts that
* expect the 'At subvol' message.
* As default is 1, which means the effective verbose for receive is 2
* which global verbose is unaware. So adjust global verbose value here.
*/
if (bconf.verbose == BTRFS_BCONF_UNSET)
bconf.verbose = 1;
else if (bconf.verbose > BTRFS_BCONF_QUIET)
bconf.verbose++;
optind = 0;
while (1) {
int c;
enum {
GETOPT_VAL_DUMP = GETOPT_VAL_FIRST,
GETOPT_VAL_FORCE_DECOMPRESS,
};
static const struct option long_opts[] = {
{ "max-errors", required_argument, NULL, 'E' },
{ "chroot", no_argument, NULL, 'C' },
{ "dump", no_argument, NULL, GETOPT_VAL_DUMP },
{ "quiet", no_argument, NULL, 'q' },
{ "force-decompress", no_argument, NULL, GETOPT_VAL_FORCE_DECOMPRESS },
{ NULL, 0, NULL, 0 }
};
c = getopt_long(argc, argv, "Cevqf:m:E:", long_opts, NULL);
if (c < 0)
break;
switch (c) {
case 'v':
bconf_be_verbose();
break;
case 'q':
bconf_be_quiet();
break;
case 'f':
if (arg_copy_path(fromfile, optarg, sizeof(fromfile))) {
error("input file path too long (%zu)",
strlen(optarg));
ret = 1;
goto out;
}
break;
case 'e':
rctx.honor_end_cmd = true;
break;
case 'C':
rctx.dest_dir_chroot = true;
break;
case 'E':
max_errors = arg_strtou64(optarg);
break;
case 'm':
if (arg_copy_path(realmnt, optarg, sizeof(realmnt))) {
error("mount point path too long (%zu)",
strlen(optarg));
ret = 1;
goto out;
}
break;
case GETOPT_VAL_DUMP:
dump = true;
break;
case GETOPT_VAL_FORCE_DECOMPRESS:
rctx.force_decompress = true;
break;
default:
usage_unknown_option(cmd, argv);
}
}
if (dump && check_argc_exact(argc - optind, 0))
usage(cmd, 1);
if (!dump && check_argc_exact(argc - optind, 1))
usage(cmd, 1);
tomnt = argv[optind];
if (fromfile[0]) {
receive_fd = open(fromfile, O_RDONLY | O_NOATIME);
if (receive_fd < 0) {
error("cannot open %s: %m", fromfile);
goto out;
}
}
if (dump) {
struct btrfs_dump_send_args dump_args;
dump_args.root_path[0] = '.';
dump_args.root_path[1] = '\0';
dump_args.full_subvol_path[0] = '.';
dump_args.full_subvol_path[1] = '\0';
ret = btrfs_read_and_process_send_stream(receive_fd,
&btrfs_print_send_ops, &dump_args, 0, max_errors);
if (ret < 0) {
errno = -ret;
error("failed to dump the send stream: %m");
}
} else {
ret = do_receive(&rctx, tomnt, realmnt, receive_fd, max_errors);
}
if (receive_fd != fileno(stdin))
close(receive_fd);
out:
return !!ret;
}
DEFINE_SIMPLE_COMMAND(receive, "receive");
|