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 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
|
/*
* QEMU Xen emulation: The actual implementation of XenStore
*
* Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors: David Woodhouse <dwmw2@infradead.org>, Paul Durrant <paul@xen.org>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qom/object.h"
#include "hw/xen/xen.h"
#include "xen_xenstore.h"
#include "xenstore_impl.h"
#include "hw/xen/interface/io/xs_wire.h"
#define XS_MAX_WATCHES 128
#define XS_MAX_DOMAIN_NODES 1000
#define XS_MAX_NODE_SIZE 2048
#define XS_MAX_TRANSACTIONS 10
#define XS_MAX_PERMS_PER_NODE 5
#define XS_VALID_CHARS "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"0123456789-/_"
typedef struct XsNode {
uint32_t ref;
GByteArray *content;
GList *perms;
GHashTable *children;
uint64_t gencnt;
bool deleted_in_tx;
bool modified_in_tx;
unsigned int serialized_tx;
#ifdef XS_NODE_UNIT_TEST
gchar *name; /* debug only */
#endif
} XsNode;
typedef struct XsWatch {
struct XsWatch *next;
xs_impl_watch_fn *cb;
void *cb_opaque;
char *token;
unsigned int dom_id;
int rel_prefix;
} XsWatch;
typedef struct XsTransaction {
XsNode *root;
unsigned int nr_nodes;
unsigned int base_tx;
unsigned int tx_id;
unsigned int dom_id;
} XsTransaction;
struct XenstoreImplState {
XsNode *root;
unsigned int nr_nodes;
GHashTable *watches;
unsigned int nr_domu_watches;
GHashTable *transactions;
unsigned int nr_domu_transactions;
unsigned int root_tx;
unsigned int last_tx;
bool serialized;
};
static void nobble_tx(gpointer key, gpointer value, gpointer user_data)
{
unsigned int *new_tx_id = user_data;
XsTransaction *tx = value;
if (tx->base_tx == *new_tx_id) {
/* Transactions based on XBT_NULL will always fail */
tx->base_tx = XBT_NULL;
}
}
static inline unsigned int next_tx(struct XenstoreImplState *s)
{
unsigned int tx_id;
/* Find the next TX id which isn't either XBT_NULL or in use. */
do {
tx_id = ++s->last_tx;
} while (tx_id == XBT_NULL || tx_id == s->root_tx ||
g_hash_table_lookup(s->transactions, GINT_TO_POINTER(tx_id)));
/*
* It is vanishingly unlikely, but ensure that no outstanding transaction
* is based on the (previous incarnation of the) newly-allocated TX id.
*/
g_hash_table_foreach(s->transactions, nobble_tx, &tx_id);
return tx_id;
}
static inline XsNode *xs_node_new(void)
{
XsNode *n = g_new0(XsNode, 1);
n->ref = 1;
#ifdef XS_NODE_UNIT_TEST
nr_xs_nodes++;
xs_node_list = g_list_prepend(xs_node_list, n);
#endif
return n;
}
static inline XsNode *xs_node_ref(XsNode *n)
{
/* With just 10 transactions, it can never get anywhere near this. */
g_assert(n->ref < INT_MAX);
g_assert(n->ref);
n->ref++;
return n;
}
static inline void xs_node_unref(XsNode *n)
{
if (!n) {
return;
}
g_assert(n->ref);
if (--n->ref) {
return;
}
if (n->content) {
g_byte_array_unref(n->content);
}
if (n->perms) {
g_list_free_full(n->perms, g_free);
}
if (n->children) {
g_hash_table_unref(n->children);
}
#ifdef XS_NODE_UNIT_TEST
g_free(n->name);
nr_xs_nodes--;
xs_node_list = g_list_remove(xs_node_list, n);
#endif
g_free(n);
}
char *xs_perm_as_string(unsigned int perm, unsigned int domid)
{
char letter;
switch (perm) {
case XS_PERM_READ | XS_PERM_WRITE:
letter = 'b';
break;
case XS_PERM_READ:
letter = 'r';
break;
case XS_PERM_WRITE:
letter = 'w';
break;
case XS_PERM_NONE:
default:
letter = 'n';
break;
}
return g_strdup_printf("%c%u", letter, domid);
}
static gpointer do_perm_copy(gconstpointer src, gpointer user_data)
{
return g_strdup(src);
}
static XsNode *xs_node_create(const char *name, GList *perms)
{
XsNode *n = xs_node_new();
#ifdef XS_NODE_UNIT_TEST
if (name) {
n->name = g_strdup(name);
}
#endif
n->perms = g_list_copy_deep(perms, do_perm_copy, NULL);
return n;
}
/* For copying from one hash table to another using g_hash_table_foreach() */
static void do_child_insert(gpointer key, gpointer value, gpointer user_data)
{
g_hash_table_insert(user_data, g_strdup(key), xs_node_ref(value));
}
static XsNode *xs_node_copy(XsNode *old)
{
XsNode *n = xs_node_new();
n->gencnt = old->gencnt;
#ifdef XS_NODE_UNIT_TEST
if (n->name) {
n->name = g_strdup(old->name);
}
#endif
assert(old);
if (old->children) {
n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)xs_node_unref);
g_hash_table_foreach(old->children, do_child_insert, n->children);
}
if (old->perms) {
n->perms = g_list_copy_deep(old->perms, do_perm_copy, NULL);
}
if (old->content) {
n->content = g_byte_array_ref(old->content);
}
return n;
}
/* Returns true if it made a change to the hash table */
static bool xs_node_add_child(XsNode *n, const char *path_elem, XsNode *child)
{
assert(!strchr(path_elem, '/'));
if (!child) {
assert(n->children);
return g_hash_table_remove(n->children, path_elem);
}
#ifdef XS_NODE_UNIT_TEST
g_free(child->name);
child->name = g_strdup(path_elem);
#endif
if (!n->children) {
n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)xs_node_unref);
}
/*
* The documentation for g_hash_table_insert() says that it "returns a
* boolean value to indicate whether the newly added value was already
* in the hash table or not."
*
* It could perhaps be clearer that returning TRUE means it wasn't,
*/
return g_hash_table_insert(n->children, g_strdup(path_elem), child);
}
struct walk_op {
struct XenstoreImplState *s;
char path[XENSTORE_ABS_PATH_MAX + 2]; /* Two NUL terminators */
int (*op_fn)(XsNode **n, struct walk_op *op);
void *op_opaque;
void *op_opaque2;
GList *watches;
unsigned int dom_id;
unsigned int tx_id;
/* The number of nodes which will exist in the tree if this op succeeds. */
unsigned int new_nr_nodes;
/*
* This is maintained on the way *down* the walk to indicate
* whether nodes can be modified in place or whether COW is
* required. It starts off being true, as we're always going to
* replace the root node. If we walk into a shared subtree it
* becomes false. If we start *creating* new nodes for a write,
* it becomes true again.
*
* Do not use it on the way back up.
*/
bool inplace;
bool mutating;
bool create_dirs;
bool in_transaction;
/* Tracking during recursion so we know which is first. */
bool deleted_in_tx;
};
static void fire_watches(struct walk_op *op, bool parents)
{
GList *l = NULL;
XsWatch *w;
if (!op->mutating || op->in_transaction) {
return;
}
if (parents) {
l = op->watches;
}
w = g_hash_table_lookup(op->s->watches, op->path);
while (w || l) {
if (!w) {
/* Fire the parent nodes from 'op' if asked to */
w = l->data;
l = l->next;
continue;
}
assert(strlen(op->path) > w->rel_prefix);
w->cb(w->cb_opaque, op->path + w->rel_prefix, w->token);
w = w->next;
}
}
static int xs_node_add_content(XsNode **n, struct walk_op *op)
{
GByteArray *data = op->op_opaque;
if (op->dom_id) {
/*
* The real XenStored includes permissions and names of child nodes
* in the calculated datasize but life's too short. For a single
* tenant internal XenStore, we don't have to be quite as pedantic.
*/
if (data->len > XS_MAX_NODE_SIZE) {
return E2BIG;
}
}
/* We *are* the node to be written. Either this or a copy. */
if (!op->inplace) {
XsNode *old = *n;
*n = xs_node_copy(old);
xs_node_unref(old);
}
if ((*n)->content) {
g_byte_array_unref((*n)->content);
}
(*n)->content = g_byte_array_ref(data);
if (op->tx_id != XBT_NULL) {
(*n)->modified_in_tx = true;
}
return 0;
}
static int xs_node_get_content(XsNode **n, struct walk_op *op)
{
GByteArray *data = op->op_opaque;
GByteArray *node_data;
assert(op->inplace);
assert(*n);
node_data = (*n)->content;
if (node_data) {
g_byte_array_append(data, node_data->data, node_data->len);
}
return 0;
}
static int node_rm_recurse(gpointer key, gpointer value, gpointer user_data)
{
struct walk_op *op = user_data;
int path_len = strlen(op->path);
int key_len = strlen(key);
XsNode *n = value;
bool this_inplace = op->inplace;
if (n->ref != 1) {
op->inplace = 0;
}
assert(key_len + path_len + 2 <= sizeof(op->path));
op->path[path_len] = '/';
memcpy(op->path + path_len + 1, key, key_len + 1);
if (n->children) {
g_hash_table_foreach_remove(n->children, node_rm_recurse, op);
}
op->new_nr_nodes--;
/*
* Fire watches on *this* node but not the parents because they are
* going to be deleted too, so the watch will fire for them anyway.
*/
fire_watches(op, false);
op->path[path_len] = '\0';
/*
* Actually deleting the child here is just an optimisation; if we
* don't then the final unref on the topmost victim will just have
* to cascade down again repeating all the g_hash_table_foreach()
* calls.
*/
return this_inplace;
}
static XsNode *xs_node_copy_deleted(XsNode *old, struct walk_op *op);
static void copy_deleted_recurse(gpointer key, gpointer value,
gpointer user_data)
{
struct walk_op *op = user_data;
GHashTable *siblings = op->op_opaque2;
XsNode *n = xs_node_copy_deleted(value, op);
/*
* Reinsert the deleted_in_tx copy of the node into the parent's
* 'children' hash table. Having stashed it from op->op_opaque2
* before the recursive call to xs_node_copy_deleted() scribbled
* over it.
*/
g_hash_table_insert(siblings, g_strdup(key), n);
}
static XsNode *xs_node_copy_deleted(XsNode *old, struct walk_op *op)
{
XsNode *n = xs_node_new();
n->gencnt = old->gencnt;
#ifdef XS_NODE_UNIT_TEST
if (old->name) {
n->name = g_strdup(old->name);
}
#endif
if (old->children) {
n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)xs_node_unref);
op->op_opaque2 = n->children;
g_hash_table_foreach(old->children, copy_deleted_recurse, op);
}
if (old->perms) {
n->perms = g_list_copy_deep(old->perms, do_perm_copy, NULL);
}
n->deleted_in_tx = true;
/* If it gets resurrected we only fire a watch if it lost its content */
if (old->content) {
n->modified_in_tx = true;
}
op->new_nr_nodes--;
return n;
}
static int xs_node_rm(XsNode **n, struct walk_op *op)
{
bool this_inplace = op->inplace;
if (op->tx_id != XBT_NULL) {
/* It's not trivial to do inplace handling for this one */
XsNode *old = *n;
*n = xs_node_copy_deleted(old, op);
xs_node_unref(old);
return 0;
}
/* Fire watches for, and count, nodes in the subtree which get deleted */
if ((*n)->children) {
g_hash_table_foreach_remove((*n)->children, node_rm_recurse, op);
}
op->new_nr_nodes--;
if (this_inplace) {
xs_node_unref(*n);
}
*n = NULL;
return 0;
}
static int xs_node_get_perms(XsNode **n, struct walk_op *op)
{
GList **perms = op->op_opaque;
assert(op->inplace);
assert(*n);
*perms = g_list_copy_deep((*n)->perms, do_perm_copy, NULL);
return 0;
}
static void parse_perm(const char *perm, char *letter, unsigned int *dom_id)
{
unsigned int n = sscanf(perm, "%c%u", letter, dom_id);
assert(n == 2);
}
static bool can_access(unsigned int dom_id, GList *perms, const char *letters)
{
unsigned int i, n;
char perm_letter;
unsigned int perm_dom_id;
bool access;
if (dom_id == 0) {
return true;
}
n = g_list_length(perms);
assert(n >= 1);
/*
* The dom_id of the first perm is the owner, and the owner always has
* read-write access.
*/
parse_perm(g_list_nth_data(perms, 0), &perm_letter, &perm_dom_id);
if (dom_id == perm_dom_id) {
return true;
}
/*
* The letter of the first perm specified the default access for all other
* domains.
*/
access = !!strchr(letters, perm_letter);
for (i = 1; i < n; i++) {
parse_perm(g_list_nth_data(perms, i), &perm_letter, &perm_dom_id);
if (dom_id != perm_dom_id) {
continue;
}
access = !!strchr(letters, perm_letter);
}
return access;
}
static int xs_node_set_perms(XsNode **n, struct walk_op *op)
{
GList *perms = op->op_opaque;
if (op->dom_id) {
unsigned int perm_dom_id;
char perm_letter;
/* A guest may not change permissions on nodes it does not own */
if (!can_access(op->dom_id, (*n)->perms, "")) {
return EPERM;
}
/* A guest may not change the owner of a node it owns. */
parse_perm(perms->data, &perm_letter, &perm_dom_id);
if (perm_dom_id != op->dom_id) {
return EPERM;
}
if (g_list_length(perms) > XS_MAX_PERMS_PER_NODE) {
return ENOSPC;
}
}
/* We *are* the node to be written. Either this or a copy. */
if (!op->inplace) {
XsNode *old = *n;
*n = xs_node_copy(old);
xs_node_unref(old);
}
if ((*n)->perms) {
g_list_free_full((*n)->perms, g_free);
}
(*n)->perms = g_list_copy_deep(perms, do_perm_copy, NULL);
if (op->tx_id != XBT_NULL) {
(*n)->modified_in_tx = true;
}
return 0;
}
/*
* Passed a full reference in *n which it may free if it needs to COW.
*
* When changing the tree, the op->inplace flag indicates whether this
* node may be modified in place (i.e. it and all its parents had a
* refcount of one). If walking down the tree we find a node whose
* refcount is higher, we must clear op->inplace and COW from there
* down. Unless we are creating new nodes as scaffolding for a write
* (which works like 'mkdir -p' does). In which case those newly
* created nodes can (and must) be modified in place again.
*/
static int xs_node_walk(XsNode **n, struct walk_op *op)
{
char *child_name = NULL;
size_t namelen;
XsNode *old = *n, *child = NULL;
bool stole_child = false;
bool this_inplace;
XsWatch *watch;
int err;
namelen = strlen(op->path);
watch = g_hash_table_lookup(op->s->watches, op->path);
/* Is there a child, or do we hit the double-NUL termination? */
if (op->path[namelen + 1]) {
char *slash;
child_name = op->path + namelen + 1;
slash = strchr(child_name, '/');
if (slash) {
*slash = '\0';
}
op->path[namelen] = '/';
}
/* If we walk into a subtree which is shared, we must COW */
if (op->mutating && old->ref != 1) {
op->inplace = false;
}
if (!child_name) {
const char *letters = op->mutating ? "wb" : "rb";
if (!can_access(op->dom_id, old->perms, letters)) {
err = EACCES;
goto out;
}
/* This is the actual node on which the operation shall be performed */
err = op->op_fn(n, op);
if (!err) {
fire_watches(op, true);
}
goto out;
}
/* op->inplace will be further modified during the recursion */
this_inplace = op->inplace;
if (old && old->children) {
child = g_hash_table_lookup(old->children, child_name);
/* This is a *weak* reference to 'child', owned by the hash table */
}
if (child) {
if (child->deleted_in_tx) {
assert(child->ref == 1);
/* Cannot actually set child->deleted_in_tx = false until later */
}
xs_node_ref(child);
/*
* Now we own it too. But if we can modify inplace, that's going to
* foil the check and force it to COW. We want to be the *only* owner
* so that it can be modified in place, so remove it from the hash
* table in that case. We'll add it (or its replacement) back later.
*/
if (op->mutating && this_inplace) {
g_hash_table_remove(old->children, child_name);
stole_child = true;
}
} else if (op->create_dirs) {
assert(op->mutating);
if (!can_access(op->dom_id, old->perms, "wb")) {
err = EACCES;
goto out;
}
if (op->dom_id && op->new_nr_nodes >= XS_MAX_DOMAIN_NODES) {
err = ENOSPC;
goto out;
}
child = xs_node_create(child_name, old->perms);
op->new_nr_nodes++;
/*
* If we're creating a new child, we can clearly modify it (and its
* children) in place from here on down.
*/
op->inplace = true;
} else {
err = ENOENT;
goto out;
}
/*
* If there's a watch on this node, add it to the list to be fired
* (with the correct full pathname for the modified node) at the end.
*/
if (watch) {
op->watches = g_list_append(op->watches, watch);
}
/*
* Except for the temporary child-stealing as noted, our node has not
* changed yet. We don't yet know the overall operation will complete.
*/
err = xs_node_walk(&child, op);
if (watch) {
op->watches = g_list_remove(op->watches, watch);
}
if (err || !op->mutating) {
if (stole_child) {
/* Put it back as it was. */
g_hash_table_replace(old->children, g_strdup(child_name), child);
} else {
xs_node_unref(child);
}
goto out;
}
/*
* Now we know the operation has completed successfully and we're on
* the way back up. Make the change, substituting 'child' in the
* node at our level.
*/
if (!this_inplace) {
*n = xs_node_copy(old);
xs_node_unref(old);
}
/*
* If we resurrected a deleted_in_tx node, we can mark it as no longer
* deleted now that we know the overall operation has succeeded.
*/
if (op->create_dirs && child && child->deleted_in_tx) {
op->new_nr_nodes++;
child->deleted_in_tx = false;
}
/*
* The child may be NULL here, for a remove operation. Either way,
* xs_node_add_child() will do the right thing and return a value
* indicating whether it changed the parent's hash table or not.
*
* We bump the parent gencnt if it adds a child that we *didn't*
* steal from it in the first place, or if child==NULL and was
* thus removed (whether we stole it earlier and didn't put it
* back, or xs_node_add_child() actually removed it now).
*/
if ((xs_node_add_child(*n, child_name, child) && !stole_child) || !child) {
(*n)->gencnt++;
}
out:
op->path[namelen] = '\0';
if (!namelen) {
assert(!op->watches);
/*
* On completing the recursion back up the path walk and reaching the
* top, assign the new node count if the operation was successful. If
* the main tree was changed, bump its tx ID so that outstanding
* transactions correctly fail. But don't bump it every time; only
* if it makes a difference.
*/
if (!err && op->mutating) {
if (!op->in_transaction) {
if (op->s->root_tx != op->s->last_tx) {
op->s->root_tx = next_tx(op->s);
}
op->s->nr_nodes = op->new_nr_nodes;
} else {
XsTransaction *tx = g_hash_table_lookup(op->s->transactions,
GINT_TO_POINTER(op->tx_id));
assert(tx);
tx->nr_nodes = op->new_nr_nodes;
}
}
}
return err;
}
static void append_directory_item(gpointer key, gpointer value,
gpointer user_data)
{
GList **items = user_data;
*items = g_list_insert_sorted(*items, g_strdup(key), (GCompareFunc)strcmp);
}
/* Populates items with char * names which caller must free. */
static int xs_node_directory(XsNode **n, struct walk_op *op)
{
GList **items = op->op_opaque;
assert(op->inplace);
assert(*n);
if ((*n)->children) {
g_hash_table_foreach((*n)->children, append_directory_item, items);
}
if (op->op_opaque2) {
*(uint64_t *)op->op_opaque2 = (*n)->gencnt;
}
return 0;
}
static int validate_path(char *outpath, const char *userpath,
unsigned int dom_id)
{
size_t i, pathlen = strlen(userpath);
if (!pathlen || userpath[pathlen] == '/' || strstr(userpath, "//")) {
return EINVAL;
}
for (i = 0; i < pathlen; i++) {
if (!strchr(XS_VALID_CHARS, userpath[i])) {
return EINVAL;
}
}
if (userpath[0] == '/') {
if (pathlen > XENSTORE_ABS_PATH_MAX) {
return E2BIG;
}
memcpy(outpath, userpath, pathlen + 1);
} else {
if (pathlen > XENSTORE_REL_PATH_MAX) {
return E2BIG;
}
snprintf(outpath, XENSTORE_ABS_PATH_MAX, "/local/domain/%u/%s", dom_id,
userpath);
}
return 0;
}
static int init_walk_op(XenstoreImplState *s, struct walk_op *op,
xs_transaction_t tx_id, unsigned int dom_id,
const char *path, XsNode ***rootp)
{
int ret = validate_path(op->path, path, dom_id);
if (ret) {
return ret;
}
/*
* We use *two* NUL terminators at the end of the path, as during the walk
* we will temporarily turn each '/' into a NUL to allow us to use that
* path element for the lookup.
*/
op->path[strlen(op->path) + 1] = '\0';
op->watches = NULL;
op->path[0] = '\0';
op->inplace = true;
op->mutating = false;
op->create_dirs = false;
op->in_transaction = false;
op->dom_id = dom_id;
op->tx_id = tx_id;
op->s = s;
if (tx_id == XBT_NULL) {
*rootp = &s->root;
op->new_nr_nodes = s->nr_nodes;
} else {
XsTransaction *tx = g_hash_table_lookup(s->transactions,
GINT_TO_POINTER(tx_id));
if (!tx) {
return ENOENT;
}
*rootp = &tx->root;
op->new_nr_nodes = tx->nr_nodes;
op->in_transaction = true;
}
return 0;
}
int xs_impl_read(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path, GByteArray *data)
{
/*
* The data GByteArray shall exist, and will be freed by caller.
* Just g_byte_array_append() to it.
*/
struct walk_op op;
XsNode **n;
int ret;
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_get_content;
op.op_opaque = data;
return xs_node_walk(n, &op);
}
int xs_impl_write(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path, GByteArray *data)
{
/*
* The data GByteArray shall exist, will be freed by caller. You are
* free to use g_byte_array_steal() and keep the data. Or just ref it.
*/
struct walk_op op;
XsNode **n;
int ret;
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_add_content;
op.op_opaque = data;
op.mutating = true;
op.create_dirs = true;
return xs_node_walk(n, &op);
}
int xs_impl_directory(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path,
uint64_t *gencnt, GList **items)
{
/*
* The items are (char *) to be freed by caller. Although it's consumed
* immediately so if you want to change it to (const char *) and keep
* them, go ahead and change the caller.
*/
struct walk_op op;
XsNode **n;
int ret;
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_directory;
op.op_opaque = items;
op.op_opaque2 = gencnt;
return xs_node_walk(n, &op);
}
int xs_impl_transaction_start(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t *tx_id)
{
XsTransaction *tx;
if (*tx_id != XBT_NULL) {
return EINVAL;
}
if (dom_id && s->nr_domu_transactions >= XS_MAX_TRANSACTIONS) {
return ENOSPC;
}
tx = g_new0(XsTransaction, 1);
tx->nr_nodes = s->nr_nodes;
tx->tx_id = next_tx(s);
tx->base_tx = s->root_tx;
tx->root = xs_node_ref(s->root);
tx->dom_id = dom_id;
g_hash_table_insert(s->transactions, GINT_TO_POINTER(tx->tx_id), tx);
if (dom_id) {
s->nr_domu_transactions++;
}
*tx_id = tx->tx_id;
return 0;
}
static gboolean tx_commit_walk(gpointer key, gpointer value,
gpointer user_data)
{
struct walk_op *op = user_data;
int path_len = strlen(op->path);
int key_len = strlen(key);
bool fire_parents = true;
XsWatch *watch;
XsNode *n = value;
if (n->ref != 1) {
return false;
}
if (n->deleted_in_tx) {
/*
* We fire watches on our parents if we are the *first* node
* to be deleted (the topmost one). This matches the behaviour
* when deleting in the live tree.
*/
fire_parents = !op->deleted_in_tx;
/* Only used on the way down so no need to clear it later */
op->deleted_in_tx = true;
}
assert(key_len + path_len + 2 <= sizeof(op->path));
op->path[path_len] = '/';
memcpy(op->path + path_len + 1, key, key_len + 1);
watch = g_hash_table_lookup(op->s->watches, op->path);
if (watch) {
op->watches = g_list_append(op->watches, watch);
}
if (n->children) {
g_hash_table_foreach_remove(n->children, tx_commit_walk, op);
}
if (watch) {
op->watches = g_list_remove(op->watches, watch);
}
/*
* Don't fire watches if this node was only copied because a
* descendent was changed. The modified_in_tx flag indicates the
* ones which were really changed.
*/
if (n->modified_in_tx || n->deleted_in_tx) {
fire_watches(op, fire_parents);
n->modified_in_tx = false;
}
op->path[path_len] = '\0';
/* Deleted nodes really do get expunged when we commit */
return n->deleted_in_tx;
}
static int transaction_commit(XenstoreImplState *s, XsTransaction *tx)
{
struct walk_op op;
XsNode **n;
int ret;
if (s->root_tx != tx->base_tx) {
return EAGAIN;
}
xs_node_unref(s->root);
s->root = tx->root;
tx->root = NULL;
s->root_tx = tx->tx_id;
s->nr_nodes = tx->nr_nodes;
ret = init_walk_op(s, &op, XBT_NULL, tx->dom_id, "/", &n);
/*
* There are two reasons why init_walk_op() may fail: an invalid tx_id,
* or an invalid path. We pass XBT_NULL and "/", and it cannot fail.
* If it does, the world is broken. And returning 'ret' would be weird
* because the transaction *was* committed, and all this tree walk is
* trying to do is fire the resulting watches on newly-committed nodes.
*/
g_assert(!ret);
op.deleted_in_tx = false;
op.mutating = true;
/*
* Walk the new root and fire watches on any node which has a
* refcount of one (which is therefore unique to this transaction).
*/
if (s->root->children) {
g_hash_table_foreach_remove(s->root->children, tx_commit_walk, &op);
}
return 0;
}
int xs_impl_transaction_end(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, bool commit)
{
int ret = 0;
XsTransaction *tx = g_hash_table_lookup(s->transactions,
GINT_TO_POINTER(tx_id));
if (!tx || tx->dom_id != dom_id) {
return ENOENT;
}
if (commit) {
ret = transaction_commit(s, tx);
}
g_hash_table_remove(s->transactions, GINT_TO_POINTER(tx_id));
if (dom_id) {
assert(s->nr_domu_transactions);
s->nr_domu_transactions--;
}
return ret;
}
int xs_impl_rm(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path)
{
struct walk_op op;
XsNode **n;
int ret;
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_rm;
op.mutating = true;
return xs_node_walk(n, &op);
}
int xs_impl_get_perms(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path, GList **perms)
{
struct walk_op op;
XsNode **n;
int ret;
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_get_perms;
op.op_opaque = perms;
return xs_node_walk(n, &op);
}
static void is_valid_perm(gpointer data, gpointer user_data)
{
char *perm = data;
bool *valid = user_data;
char letter;
unsigned int dom_id;
if (!*valid) {
return;
}
if (sscanf(perm, "%c%u", &letter, &dom_id) != 2) {
*valid = false;
return;
}
switch (letter) {
case 'n':
case 'r':
case 'w':
case 'b':
break;
default:
*valid = false;
break;
}
}
int xs_impl_set_perms(XenstoreImplState *s, unsigned int dom_id,
xs_transaction_t tx_id, const char *path, GList *perms)
{
struct walk_op op;
XsNode **n;
bool valid = true;
int ret;
if (!g_list_length(perms)) {
return EINVAL;
}
g_list_foreach(perms, is_valid_perm, &valid);
if (!valid) {
return EINVAL;
}
ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
if (ret) {
return ret;
}
op.op_fn = xs_node_set_perms;
op.op_opaque = perms;
op.mutating = true;
return xs_node_walk(n, &op);
}
static int do_xs_impl_watch(XenstoreImplState *s, unsigned int dom_id,
const char *path, const char *token,
xs_impl_watch_fn fn, void *opaque)
{
char abspath[XENSTORE_ABS_PATH_MAX + 1];
XsWatch *w, *l;
int ret;
ret = validate_path(abspath, path, dom_id);
if (ret) {
return ret;
}
/* Check for duplicates */
l = w = g_hash_table_lookup(s->watches, abspath);
while (w) {
if (!g_strcmp0(token, w->token) && opaque == w->cb_opaque &&
fn == w->cb && dom_id == w->dom_id) {
return EEXIST;
}
w = w->next;
}
if (dom_id && s->nr_domu_watches >= XS_MAX_WATCHES) {
return E2BIG;
}
w = g_new0(XsWatch, 1);
w->token = g_strdup(token);
w->cb = fn;
w->cb_opaque = opaque;
w->dom_id = dom_id;
w->rel_prefix = strlen(abspath) - strlen(path);
/* l was looked up above when checking for duplicates */
if (l) {
w->next = l->next;
l->next = w;
} else {
g_hash_table_insert(s->watches, g_strdup(abspath), w);
}
if (dom_id) {
s->nr_domu_watches++;
}
return 0;
}
int xs_impl_watch(XenstoreImplState *s, unsigned int dom_id, const char *path,
const char *token, xs_impl_watch_fn fn, void *opaque)
{
int ret = do_xs_impl_watch(s, dom_id, path, token, fn, opaque);
if (!ret) {
/* A new watch should fire immediately */
fn(opaque, path, token);
}
return ret;
}
static XsWatch *free_watch(XenstoreImplState *s, XsWatch *w)
{
XsWatch *next = w->next;
if (w->dom_id) {
assert(s->nr_domu_watches);
s->nr_domu_watches--;
}
g_free(w->token);
g_free(w);
return next;
}
int xs_impl_unwatch(XenstoreImplState *s, unsigned int dom_id,
const char *path, const char *token,
xs_impl_watch_fn fn, void *opaque)
{
char abspath[XENSTORE_ABS_PATH_MAX + 1];
XsWatch *w, **l;
int ret;
ret = validate_path(abspath, path, dom_id);
if (ret) {
return ret;
}
w = g_hash_table_lookup(s->watches, abspath);
if (!w) {
return ENOENT;
}
/*
* The hash table contains the first element of a list of
* watches. Removing the first element in the list is a
* special case because we have to update the hash table to
* point to the next (or remove it if there's nothing left).
*/
if (!g_strcmp0(token, w->token) && fn == w->cb && opaque == w->cb_opaque &&
dom_id == w->dom_id) {
if (w->next) {
/* Insert the previous 'next' into the hash table */
g_hash_table_insert(s->watches, g_strdup(abspath), w->next);
} else {
/* Nothing left; remove from hash table */
g_hash_table_remove(s->watches, abspath);
}
free_watch(s, w);
return 0;
}
/*
* We're all done messing with the hash table because the element
* it points to has survived the cull. Now it's just a simple
* linked list removal operation.
*/
for (l = &w->next; *l; l = &w->next) {
w = *l;
if (!g_strcmp0(token, w->token) && fn == w->cb &&
opaque != w->cb_opaque && dom_id == w->dom_id) {
*l = free_watch(s, w);
return 0;
}
}
return ENOENT;
}
int xs_impl_reset_watches(XenstoreImplState *s, unsigned int dom_id)
{
char **watch_paths;
guint nr_watch_paths;
guint i;
watch_paths = (char **)g_hash_table_get_keys_as_array(s->watches,
&nr_watch_paths);
for (i = 0; i < nr_watch_paths; i++) {
XsWatch *w1 = g_hash_table_lookup(s->watches, watch_paths[i]);
XsWatch *w2, *w, **l;
/*
* w1 is the original list. The hash table has this pointer.
* w2 is the head of our newly-filtered list.
* w and l are temporary for processing. w is somewhat redundant
* with *l but makes my eyes bleed less.
*/
w = w2 = w1;
l = &w;
while (w) {
if (w->dom_id == dom_id) {
/* If we're freeing the head of the list, bump w2 */
if (w2 == w) {
w2 = w->next;
}
*l = free_watch(s, w);
} else {
l = &w->next;
}
w = *l;
}
/*
* If the head of the list survived the cull, we don't need to
* touch the hash table and we're done with this path. Else...
*/
if (w1 != w2) {
g_hash_table_steal(s->watches, watch_paths[i]);
/*
* It was already freed. (Don't worry, this whole thing is
* single-threaded and nobody saw it in the meantime). And
* having *stolen* it, we now own the watch_paths[i] string
* so if we don't give it back to the hash table, we need
* to free it.
*/
if (w2) {
g_hash_table_insert(s->watches, watch_paths[i], w2);
} else {
g_free(watch_paths[i]);
}
}
}
g_free(watch_paths);
return 0;
}
static void xs_tx_free(void *_tx)
{
XsTransaction *tx = _tx;
if (tx->root) {
xs_node_unref(tx->root);
}
g_free(tx);
}
XenstoreImplState *xs_impl_create(unsigned int dom_id)
{
XenstoreImplState *s = g_new0(XenstoreImplState, 1);
GList *perms;
s->watches = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
s->transactions = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, xs_tx_free);
perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, 0));
s->root = xs_node_create("/", perms);
g_list_free_full(perms, g_free);
s->nr_nodes = 1;
s->root_tx = s->last_tx = 1;
return s;
}
static void clear_serialized_tx(gpointer key, gpointer value, gpointer opaque)
{
XsNode *n = value;
n->serialized_tx = XBT_NULL;
if (n->children) {
g_hash_table_foreach(n->children, clear_serialized_tx, NULL);
}
}
static void clear_tx_serialized_tx(gpointer key, gpointer value,
gpointer opaque)
{
XsTransaction *t = value;
clear_serialized_tx(NULL, t->root, NULL);
}
static void write_be32(GByteArray *save, uint32_t val)
{
uint32_t be = htonl(val);
g_byte_array_append(save, (void *)&be, sizeof(be));
}
struct save_state {
GByteArray *bytes;
unsigned int tx_id;
};
#define MODIFIED_IN_TX (1U << 0)
#define DELETED_IN_TX (1U << 1)
#define NODE_REF (1U << 2)
static void save_node(gpointer key, gpointer value, gpointer opaque)
{
struct save_state *ss = opaque;
XsNode *n = value;
char *name = key;
uint8_t flag = 0;
/* Child nodes (i.e. anything but the root) have a name */
if (name) {
g_byte_array_append(ss->bytes, key, strlen(key) + 1);
}
/*
* If we already wrote this node, refer to the previous copy.
* There's no rename/move in XenStore, so all we need to find
* it is the tx_id of the transaction in which it exists. Which
* may be the root tx.
*/
if (n->serialized_tx != XBT_NULL) {
flag = NODE_REF;
g_byte_array_append(ss->bytes, &flag, 1);
write_be32(ss->bytes, n->serialized_tx);
} else {
GList *l;
n->serialized_tx = ss->tx_id;
if (n->modified_in_tx) {
flag |= MODIFIED_IN_TX;
}
if (n->deleted_in_tx) {
flag |= DELETED_IN_TX;
}
g_byte_array_append(ss->bytes, &flag, 1);
if (n->content) {
write_be32(ss->bytes, n->content->len);
g_byte_array_append(ss->bytes, n->content->data, n->content->len);
} else {
write_be32(ss->bytes, 0);
}
for (l = n->perms; l; l = l->next) {
g_byte_array_append(ss->bytes, l->data, strlen(l->data) + 1);
}
/* NUL termination after perms */
g_byte_array_append(ss->bytes, (void *)"", 1);
if (n->children) {
g_hash_table_foreach(n->children, save_node, ss);
}
/* NUL termination after children (child name is NUL) */
g_byte_array_append(ss->bytes, (void *)"", 1);
}
}
static void save_tree(struct save_state *ss, uint32_t tx_id, XsNode *root)
{
write_be32(ss->bytes, tx_id);
ss->tx_id = tx_id;
save_node(NULL, root, ss);
}
static void save_tx(gpointer key, gpointer value, gpointer opaque)
{
uint32_t tx_id = GPOINTER_TO_INT(key);
struct save_state *ss = opaque;
XsTransaction *n = value;
write_be32(ss->bytes, n->base_tx);
write_be32(ss->bytes, n->dom_id);
save_tree(ss, tx_id, n->root);
}
static void save_watch(gpointer key, gpointer value, gpointer opaque)
{
struct save_state *ss = opaque;
XsWatch *w = value;
/* We only save the *guest* watches. */
if (w->dom_id) {
gpointer relpath = key + w->rel_prefix;
g_byte_array_append(ss->bytes, relpath, strlen(relpath) + 1);
g_byte_array_append(ss->bytes, (void *)w->token, strlen(w->token) + 1);
}
}
GByteArray *xs_impl_serialize(XenstoreImplState *s)
{
struct save_state ss;
ss.bytes = g_byte_array_new();
/*
* node = flags [ real_node / node_ref ]
* flags = uint8_t (MODIFIED_IN_TX | DELETED_IN_TX | NODE_REF)
* node_ref = tx_id (in which the original version of this node exists)
* real_node = content perms child* NUL
* content = len data
* len = uint32_t
* data = uint8_t{len}
* perms = perm* NUL
* perm = asciiz
* child = name node
* name = asciiz
*
* tree = tx_id node
* tx_id = uint32_t
*
* transaction = base_tx_id dom_id tree
* base_tx_id = uint32_t
* dom_id = uint32_t
*
* tx_list = tree transaction* XBT_NULL
*
* watch = path token
* path = asciiz
* token = asciiz
*
* watch_list = watch* NUL
*
* xs_serialize_stream = last_tx tx_list watch_list
* last_tx = uint32_t
*/
/* Clear serialized_tx in every node. */
if (s->serialized) {
clear_serialized_tx(NULL, s->root, NULL);
g_hash_table_foreach(s->transactions, clear_tx_serialized_tx, NULL);
}
s->serialized = true;
write_be32(ss.bytes, s->last_tx);
save_tree(&ss, s->root_tx, s->root);
g_hash_table_foreach(s->transactions, save_tx, &ss);
write_be32(ss.bytes, XBT_NULL);
g_hash_table_foreach(s->watches, save_watch, &ss);
g_byte_array_append(ss.bytes, (void *)"", 1);
return ss.bytes;
}
struct unsave_state {
char path[XENSTORE_ABS_PATH_MAX + 1];
XenstoreImplState *s;
GByteArray *bytes;
uint8_t *d;
size_t l;
bool root_walk;
};
static int consume_be32(struct unsave_state *us, unsigned int *val)
{
uint32_t d;
if (us->l < sizeof(d)) {
return -EINVAL;
}
memcpy(&d, us->d, sizeof(d));
*val = ntohl(d);
us->d += sizeof(d);
us->l -= sizeof(d);
return 0;
}
static int consume_string(struct unsave_state *us, char **str, size_t *len)
{
size_t l;
if (!us->l) {
return -EINVAL;
}
l = strnlen((void *)us->d, us->l);
if (l == us->l) {
return -EINVAL;
}
if (str) {
*str = (void *)us->d;
}
if (len) {
*len = l;
}
us->d += l + 1;
us->l -= l + 1;
return 0;
}
static XsNode *lookup_node(XsNode *n, char *path)
{
char *slash = strchr(path, '/');
XsNode *child;
if (path[0] == '\0') {
return n;
}
if (slash) {
*slash = '\0';
}
if (!n->children) {
return NULL;
}
child = g_hash_table_lookup(n->children, path);
if (!slash) {
return child;
}
*slash = '/';
if (!child) {
return NULL;
}
return lookup_node(child, slash + 1);
}
static XsNode *lookup_tx_node(struct unsave_state *us, unsigned int tx_id)
{
XsTransaction *t;
if (tx_id == us->s->root_tx) {
return lookup_node(us->s->root, us->path + 1);
}
t = g_hash_table_lookup(us->s->transactions, GINT_TO_POINTER(tx_id));
if (!t) {
return NULL;
}
g_assert(t->root);
return lookup_node(t->root, us->path + 1);
}
static void count_child_nodes(gpointer key, gpointer value, gpointer user_data)
{
unsigned int *nr_nodes = user_data;
XsNode *n = value;
(*nr_nodes)++;
if (n->children) {
g_hash_table_foreach(n->children, count_child_nodes, nr_nodes);
}
}
static int consume_node(struct unsave_state *us, XsNode **nodep,
unsigned int *nr_nodes)
{
XsNode *n = NULL;
uint8_t flags;
int ret;
if (us->l < 1) {
return -EINVAL;
}
flags = us->d[0];
us->d++;
us->l--;
if (flags == NODE_REF) {
unsigned int tx;
ret = consume_be32(us, &tx);
if (ret) {
return ret;
}
n = lookup_tx_node(us, tx);
if (!n) {
return -EINVAL;
}
n->ref++;
if (n->children) {
g_hash_table_foreach(n->children, count_child_nodes, nr_nodes);
}
} else {
uint32_t datalen;
if (flags & ~(DELETED_IN_TX | MODIFIED_IN_TX)) {
return -EINVAL;
}
n = xs_node_new();
if (flags & DELETED_IN_TX) {
n->deleted_in_tx = true;
}
if (flags & MODIFIED_IN_TX) {
n->modified_in_tx = true;
}
ret = consume_be32(us, &datalen);
if (ret) {
xs_node_unref(n);
return -EINVAL;
}
if (datalen) {
if (datalen > us->l) {
xs_node_unref(n);
return -EINVAL;
}
GByteArray *node_data = g_byte_array_new();
g_byte_array_append(node_data, us->d, datalen);
us->d += datalen;
us->l -= datalen;
n->content = node_data;
if (us->root_walk) {
n->modified_in_tx = true;
}
}
while (1) {
char *perm = NULL;
size_t permlen = 0;
ret = consume_string(us, &perm, &permlen);
if (ret) {
xs_node_unref(n);
return ret;
}
if (!permlen) {
break;
}
n->perms = g_list_append(n->perms, g_strdup(perm));
}
/* Now children */
while (1) {
size_t childlen;
char *childname;
char *pathend;
XsNode *child = NULL;
ret = consume_string(us, &childname, &childlen);
if (ret) {
xs_node_unref(n);
return ret;
}
if (!childlen) {
break;
}
pathend = us->path + strlen(us->path);
strncat(us->path, "/", sizeof(us->path) - 1);
strncat(us->path, childname, sizeof(us->path) - 1);
ret = consume_node(us, &child, nr_nodes);
*pathend = '\0';
if (ret) {
xs_node_unref(n);
return ret;
}
g_assert(child);
xs_node_add_child(n, childname, child);
}
/*
* If the node has no data and no children we still want to fire
* a watch on it.
*/
if (us->root_walk && !n->children) {
n->modified_in_tx = true;
}
}
if (!n->deleted_in_tx) {
(*nr_nodes)++;
}
*nodep = n;
return 0;
}
static int consume_tree(struct unsave_state *us, XsTransaction *t)
{
int ret;
ret = consume_be32(us, &t->tx_id);
if (ret) {
return ret;
}
if (t->tx_id > us->s->last_tx) {
return -EINVAL;
}
us->path[0] = '\0';
return consume_node(us, &t->root, &t->nr_nodes);
}
int xs_impl_deserialize(XenstoreImplState *s, GByteArray *bytes,
unsigned int dom_id, xs_impl_watch_fn watch_fn,
void *watch_opaque)
{
struct unsave_state us;
XsTransaction base_t = { 0 };
int ret;
us.s = s;
us.bytes = bytes;
us.d = bytes->data;
us.l = bytes->len;
xs_impl_reset_watches(s, dom_id);
g_hash_table_remove_all(s->transactions);
xs_node_unref(s->root);
s->root = NULL;
s->root_tx = s->last_tx = XBT_NULL;
ret = consume_be32(&us, &s->last_tx);
if (ret) {
return ret;
}
/*
* Consume the base tree into a transaction so that watches can be
* fired as we commit it. By setting us.root_walk we cause the nodes
* to be marked as 'modified_in_tx' as they are created, so that the
* watches are triggered on them.
*/
base_t.dom_id = dom_id;
base_t.base_tx = XBT_NULL;
us.root_walk = true;
ret = consume_tree(&us, &base_t);
if (ret) {
return ret;
}
us.root_walk = false;
/*
* Commit the transaction now while the refcount on all nodes is 1.
* Note that we haven't yet reinstated the *guest* watches but that's
* OK because we don't want the guest to see any changes. Even any
* backend nodes which get recreated should be *precisely* as they
* were before the migration. Back ends may have been instantiated
* already, and will see the frontend magically blink into existence
* now (well, from the aio_bh which fires the watches). It's their
* responsibility to rebuild everything precisely as it was before.
*/
ret = transaction_commit(s, &base_t);
if (ret) {
return ret;
}
while (1) {
unsigned int base_tx;
XsTransaction *t;
ret = consume_be32(&us, &base_tx);
if (ret) {
return ret;
}
if (base_tx == XBT_NULL) {
break;
}
t = g_new0(XsTransaction, 1);
t->base_tx = base_tx;
ret = consume_be32(&us, &t->dom_id);
if (!ret) {
ret = consume_tree(&us, t);
}
if (ret) {
g_free(t);
return ret;
}
g_assert(t->root);
if (t->dom_id) {
s->nr_domu_transactions++;
}
g_hash_table_insert(s->transactions, GINT_TO_POINTER(t->tx_id), t);
}
while (1) {
char *path, *token;
size_t pathlen, toklen;
ret = consume_string(&us, &path, &pathlen);
if (ret) {
return ret;
}
if (!pathlen) {
break;
}
ret = consume_string(&us, &token, &toklen);
if (ret) {
return ret;
}
if (!watch_fn) {
continue;
}
ret = do_xs_impl_watch(s, dom_id, path, token, watch_fn, watch_opaque);
if (ret) {
return ret;
}
}
if (us.l) {
return -EINVAL;
}
return 0;
}
|