sys/vfs/hammer2/hammer2_xops.c
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 | /* * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@dragonflybsd.org> * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Per-node backend for kernel filesystem interface. * * This executes a VOP concurrently on multiple nodes, each node via its own * thread, and competes to advance the original request. The original * request is retired the moment all requirements are met, even if the * operation is still in-progress on some nodes. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/proc.h> #include <sys/mount.h> #include "hammer2.h" /* * Determine if the specified directory is empty. * * Returns 0 on success. * * Returns HAMMER_ERROR_EAGAIN if caller must re-lookup the entry and * retry. (occurs if we race a ripup on oparent or ochain). * * Or returns a permanent HAMMER2_ERROR_* error mask. * * The caller must pass in an exclusively locked oparent and ochain. This * function will handle the case where the chain is a directory entry or * the inode itself. The original oparent,ochain will be locked upon return. * * This function will unlock the underlying oparent,ochain temporarily when * doing an inode lookup to avoid deadlocks. The caller MUST handle the EAGAIN * result as this means that oparent is no longer the parent of ochain, or * that ochain was destroyed while it was unlocked. */ static int checkdirempty(hammer2_chain_t *oparent, hammer2_chain_t *ochain, int clindex) { hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; hammer2_key_t inum; int error; int didunlock; error = 0; didunlock = 0; /* * Find the inode, set it up as a locked 'chain'. ochain can be the * inode itself, or it can be a directory entry. */ if (ochain->bref.type == HAMMER2_BREF_TYPE_DIRENT) { inum = ochain->bref.embed.dirent.inum; hammer2_chain_unlock(ochain); hammer2_chain_unlock(oparent); parent = NULL; chain = NULL; error = hammer2_chain_inode_find(ochain->pmp, inum, clindex, 0, &parent, &chain); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } didunlock = 1; } else { /* * The directory entry *is* the directory inode */ chain = hammer2_chain_lookup_init(ochain, 0); } /* * Determine if the directory is empty or not by checking its * visible namespace (the area which contains directory entries). */ if (error == 0) { parent = chain; chain = NULL; if (parent) { chain = hammer2_chain_lookup(&parent, &key_next, HAMMER2_DIRHASH_VISIBLE, HAMMER2_KEY_MAX, &error, 0); } if (chain) { error = HAMMER2_ERROR_ENOTEMPTY; hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } hammer2_chain_lookup_done(parent); } else { if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); chain = NULL; /* safety */ } } if (didunlock) { hammer2_chain_lock(oparent, HAMMER2_RESOLVE_ALWAYS); hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS); if ((ochain->flags & HAMMER2_CHAIN_DELETED) || (oparent->flags & HAMMER2_CHAIN_DELETED) || ochain->parent != oparent) { kprintf("hammer2: debug: CHECKDIR inum %jd RETRY\n", inum); error = HAMMER2_ERROR_EAGAIN; } } return error; } /* * Backend for hammer2_vfs_root() * * This is called when a newly mounted PFS has not yet synchronized * to the inode_tid and modify_tid. */ void hammer2_xop_ipcluster(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_ipcluster_t *xop = &arg->xop_ipcluster; hammer2_chain_t *chain; int error; chain = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); if (chain) error = chain->error; else error = HAMMER2_ERROR_EIO; hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Backend for hammer2_vop_readdir() */ void hammer2_xop_readdir(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_readdir_t *xop = &arg->xop_readdir; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; hammer2_key_t lkey; int error = 0; lkey = xop->lkey; if (hammer2_debug & 0x0020) kprintf("xop_readdir: %p lkey=%016jx\n", xop, lkey); /* * The inode's chain is the iterator. If we cannot acquire it our * contribution ends here. */ parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); if (parent == NULL) { kprintf("xop_readdir: NULL parent\n"); goto done; } /* * Directory scan [re]start and loop, the feed inherits the chain's * lock so do not unlock it on the iteration. */ chain = hammer2_chain_lookup(&parent, &key_next, lkey, lkey, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); if (chain == NULL) { chain = hammer2_chain_lookup(&parent, &key_next, lkey, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); } while (chain) { error = hammer2_xop_feed(&xop->head, chain, clindex, 0); if (error) goto break2; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); } break2: if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } hammer2_chain_unlock(parent); hammer2_chain_drop(parent); done: hammer2_xop_feed(&xop->head, NULL, clindex, error); } /* * Backend for hammer2_vop_nresolve() */ void hammer2_xop_nresolve(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_nresolve_t *xop = &arg->xop_nresolve; hammer2_chain_t *parent; hammer2_chain_t *chain; const char *name; size_t name_len; hammer2_key_t key_next; hammer2_key_t lhc; int error; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); if (parent == NULL) { kprintf("xop_nresolve: NULL parent\n"); chain = NULL; error = HAMMER2_ERROR_EIO; goto done; } name = xop->head.name1; name_len = xop->head.name1_len; /* * Lookup the directory entry */ lhc = hammer2_dirhash(name, name_len); chain = hammer2_chain_lookup(&parent, &key_next, lhc, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); while (chain) { if (hammer2_chain_dirent_test(chain, name, name_len)) break; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); } /* * Locate the target inode for a directory entry */ if (chain && chain->error == 0) { if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) { lhc = chain->bref.embed.dirent.inum; error = hammer2_chain_inode_find(chain->pmp, lhc, clindex, HAMMER2_LOOKUP_SHARED, &parent, &chain); } } else if (chain && error == 0) { error = chain->error; } done: hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } } /* * Backend for hammer2_vop_nremove(), hammer2_vop_nrmdir(), and * backend for pfs_delete. * * This function locates and removes a directory entry, and will lookup * and return the underlying inode. For directory entries the underlying * inode is not removed. If the directory entry is the actual inode itself, * it may be conditonally removed and returned. * * WARNING! Any target inode's nlinks may not be synchronized to the * in-memory inode. The frontend's hammer2_inode_unlink_finisher() * is responsible for the final disposition of the actual inode. */ void hammer2_xop_unlink(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_unlink_t *xop = &arg->xop_unlink; hammer2_chain_t *parent; hammer2_chain_t *chain; const char *name; size_t name_len; hammer2_key_t key_next; hammer2_key_t lhc; int error; again: /* * Requires exclusive lock */ parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); chain = NULL; if (parent == NULL) { kprintf("xop_unlink: NULL parent\n"); error = HAMMER2_ERROR_EIO; goto done; } name = xop->head.name1; name_len = xop->head.name1_len; /* * Lookup the directory entry */ lhc = hammer2_dirhash(name, name_len); chain = hammer2_chain_lookup(&parent, &key_next, lhc, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); while (chain) { if (hammer2_chain_dirent_test(chain, name, name_len)) break; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); } /* * The directory entry will either be a BREF_TYPE_DIRENT or a * BREF_TYPE_INODE. We always permanently delete DIRENTs, but * must go by xop->dopermanent for BREF_TYPE_INODE. * * Note that the target chain's nlinks may not be synchronized with * the in-memory hammer2_inode_t structure, so we don't try to do * anything fancy here. The frontend deals with nlinks * synchronization. */ if (chain && chain->error == 0) { int dopermanent = xop->dopermanent & H2DOPERM_PERMANENT; int doforce = xop->dopermanent & H2DOPERM_FORCE; uint8_t type; /* * If the directory entry is the actual inode then use its * type for the directory typing tests, otherwise if it is * a directory entry, pull the type field from the entry. * * Directory entries are always permanently deleted * (because they aren't the actual inode). */ if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) { type = chain->bref.embed.dirent.type; dopermanent |= HAMMER2_DELETE_PERMANENT; } else { type = chain->data->ipdata.meta.type; } /* * Check directory typing and delete the entry. * * Unfortunately, checkdirempty() may have to unlock (parent). * If it no longer matches chain->parent after re-locking, * EAGAIN is returned. */ if (type == HAMMER2_OBJTYPE_DIRECTORY && doforce) { /* * If doforce then execute the operation even if * the directory is not empty or errored. We * ignore chain->error here, allowing an errored * chain (aka directory entry) to still be deleted. */ error = hammer2_chain_delete(parent, chain, xop->head.mtid, dopermanent); } else if (type == HAMMER2_OBJTYPE_DIRECTORY && xop->isdir == 0) { error = HAMMER2_ERROR_EISDIR; } else if (type == HAMMER2_OBJTYPE_DIRECTORY && (error = checkdirempty(parent, chain, clindex)) != 0) { /* * error may be EAGAIN or ENOTEMPTY */ if (error == HAMMER2_ERROR_EAGAIN) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); hammer2_chain_unlock(parent); hammer2_chain_drop(parent); goto again; } } else if (type != HAMMER2_OBJTYPE_DIRECTORY && xop->isdir >= 1) { error = HAMMER2_ERROR_ENOTDIR; } else { /* * Delete the directory entry. chain might also * be a directly-embedded inode. * * Allow the deletion to proceed even if the chain * is errored. Give priority to error-on-delete over * chain->error. */ error = hammer2_chain_delete(parent, chain, xop->head.mtid, dopermanent); if (error == 0) error = chain->error; } } else { if (chain && error == 0) error = chain->error; } /* * If chain is a directory entry we must resolve it. We do not try * to manipulate the contents as it might not be synchronized with * the frontend hammer2_inode_t, nor do we try to lookup the * frontend hammer2_inode_t here (we are the backend!). */ if (chain && chain->bref.type == HAMMER2_BREF_TYPE_DIRENT && (xop->dopermanent & H2DOPERM_IGNINO) == 0) { int error2; lhc = chain->bref.embed.dirent.inum; error2 = hammer2_chain_inode_find(chain->pmp, lhc, clindex, 0, &parent, &chain); if (error2) { kprintf("xop_unlink: %016jx %p failed\n", lhc, chain); error2 = 0; /* silently ignore */ } if (error == 0) error = error2; } /* * Return the inode target for further action. Typically used by * hammer2_inode_unlink_finisher(). */ done: hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); chain = NULL; } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); parent = NULL; } } /* * Backend for hammer2_vop_nrename() * * This handles the backend rename operation. Typically this renames * directory entries but can also be used to rename embedded inodes. * * NOTE! The frontend is responsible for updating the inode meta-data in * the file being renamed and for decrementing the target-replaced * inode's nlinks, if present. */ void hammer2_xop_nrename(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_nrename_t *xop = &arg->xop_nrename; hammer2_pfs_t *pmp; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_chain_t *tmp; hammer2_inode_t *ip; hammer2_key_t key_next; int error; /* * If ip4 is non-NULL we must check to see if the entry being * overwritten is a non-empty directory. */ ip = xop->head.ip4; if (ip) { uint8_t type; chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS); if (chain == NULL) { error = HAMMER2_ERROR_EIO; parent = NULL; goto done; } type = chain->data->ipdata.meta.type; if (type == HAMMER2_OBJTYPE_DIRECTORY && (error = checkdirempty(NULL, chain, clindex)) != 0) { KKASSERT(error != HAMMER2_ERROR_EAGAIN); parent = NULL; goto done; } hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } /* * We need the precise parent chain to issue the deletion. * * If this is a directory entry we must locate the underlying * inode. If it is an embedded inode we can act directly on it. */ ip = xop->head.ip2; pmp = ip->pmp; chain = NULL; error = 0; if (xop->ip_key & HAMMER2_DIRHASH_VISIBLE) { /* * Find ip's direct parent chain. */ chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS); if (chain == NULL) { error = HAMMER2_ERROR_EIO; parent = NULL; goto done; } if (ip->flags & HAMMER2_INODE_CREATING) { parent = NULL; } else { parent = hammer2_chain_getparent(chain, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } } } else { /* * The directory entry for the head.ip1 inode * is in fdip, do a namespace search. */ hammer2_key_t lhc; const char *name; size_t name_len; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { kprintf("xop_nrename: NULL parent\n"); error = HAMMER2_ERROR_EIO; goto done; } name = xop->head.name1; name_len = xop->head.name1_len; /* * Lookup the directory entry */ lhc = hammer2_dirhash(name, name_len); chain = hammer2_chain_lookup(&parent, &key_next, lhc, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); while (chain) { if (hammer2_chain_dirent_test(chain, name, name_len)) break; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); } } if (chain == NULL) { /* XXX shouldn't happen, but does under fsstress */ kprintf("xop_nrename: \"%s\" -> \"%s\" ENOENT\n", xop->head.name1, xop->head.name2); if (error == 0) error = HAMMER2_ERROR_ENOENT; goto done; } if (chain->error) { error = chain->error; goto done; } /* * Delete it, then create it in the new namespace. * * An error can occur if the chain being deleted requires * modification and the media is full. */ error = hammer2_chain_delete(parent, chain, xop->head.mtid, 0); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); parent = NULL; /* safety */ } else { kprintf("hammer2_xop_nrename() (debugging): parent was NULL\n"); error = EINVAL; } if (error) goto done; /* * Adjust fields in the deleted chain appropriate for the rename * operation. * * NOTE! For embedded inodes, the frontend will officially replicate * the field adjustments, but we also do it here to maintain * consistency in case of a crash. */ if (chain->bref.key != xop->lhc || xop->head.name1_len != xop->head.name2_len || bcmp(xop->head.name1, xop->head.name2, xop->head.name1_len) != 0) { if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) { hammer2_inode_data_t *wipdata; error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { wipdata = &chain->data->ipdata; bzero(wipdata->filename, sizeof(wipdata->filename)); bcopy(xop->head.name2, wipdata->filename, xop->head.name2_len); wipdata->meta.name_key = xop->lhc; wipdata->meta.name_len = xop->head.name2_len; } } if (chain->bref.type == HAMMER2_BREF_TYPE_DIRENT) { if (xop->head.name2_len <= sizeof(chain->bref.check.buf)) { /* * Remove any related data buffer, we can * embed the filename in the bref itself. */ error = hammer2_chain_resize( chain, xop->head.mtid, 0, 0, 0); if (error == 0) { error = hammer2_chain_modify( chain, xop->head.mtid, 0, 0); } if (error == 0) { bzero(chain->bref.check.buf, sizeof(chain->bref.check.buf)); bcopy(xop->head.name2, chain->bref.check.buf, xop->head.name2_len); } } else { /* * Associate a data buffer with the bref. * Zero it for consistency. Note that the * data buffer is not 64KB so use chain->bytes * instead of sizeof(). */ error = hammer2_chain_resize( chain, xop->head.mtid, 0, hammer2_getradix(HAMMER2_ALLOC_MIN), 0); if (error == 0) { error = hammer2_chain_modify( chain, xop->head.mtid, 0, 0); } if (error == 0) { bzero(chain->data->buf, chain->bytes); bcopy(xop->head.name2, chain->data->buf, xop->head.name2_len); } } if (error == 0) { chain->bref.embed.dirent.namlen = xop->head.name2_len; } } } /* * The frontend will replicate this operation and is the real final * authority, but adjust the inode's iparent field too if the inode * is embedded in the directory. */ if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data->ipdata.meta.iparent != xop->head.ip3->meta.inum) { hammer2_inode_data_t *wipdata; error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { wipdata = &chain->data->ipdata; wipdata->meta.iparent = xop->head.ip3->meta.inum; } } /* * Destroy any matching target(s) before creating the new entry. * This will result in some ping-ponging of the directory key * iterator but that is ok. */ parent = hammer2_inode_chain(xop->head.ip3, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } /* * Delete all matching directory entries. That is, get rid of * multiple duplicates if present, as a self-healing mechanism. */ if (error == 0) { tmp = hammer2_chain_lookup(&parent, &key_next, xop->lhc & ~HAMMER2_DIRHASH_LOMASK, xop->lhc | HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); while (tmp) { int e2; if (hammer2_chain_dirent_test(tmp, xop->head.name2, xop->head.name2_len)) { e2 = hammer2_chain_delete(parent, tmp, xop->head.mtid, 0); if (error == 0 && e2) error = e2; } tmp = hammer2_chain_next(&parent, tmp, &key_next, key_next, xop->lhc | HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS); } } if (error == 0) { /* * A relookup is required before the create to properly * position the parent chain. */ tmp = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, 0); KKASSERT(tmp == NULL); error = hammer2_chain_create(&parent, &chain, NULL, pmp, HAMMER2_METH_DEFAULT, xop->lhc, 0, HAMMER2_BREF_TYPE_INODE, HAMMER2_INODE_BYTES, xop->head.mtid, 0, 0); } done: hammer2_xop_feed(&xop->head, NULL, clindex, error); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Directory collision resolver scan helper (backend, threaded). * * Used by the inode create code to locate an unused lhc. */ void hammer2_xop_scanlhc(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_scanlhc_t *xop = &arg->xop_scanlhc; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error = 0; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); if (parent == NULL) { kprintf("xop_scanlhc: NULL parent\n"); chain = NULL; error = HAMMER2_ERROR_EIO; goto done; } /* * Lookup all possibly conflicting directory entries, the feed * inherits the chain's lock so do not unlock it on the iteration. */ chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); while (chain) { error = hammer2_xop_feed(&xop->head, chain, clindex, 0); if (error) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); chain = NULL; /* safety */ goto done; } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, xop->lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); } done: hammer2_xop_feed(&xop->head, NULL, clindex, error); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } } /* * Generic lookup of a specific key. */ void hammer2_xop_lookup(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_lookup_t *xop = &arg->xop_lookup; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error = 0; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); chain = NULL; if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } /* * Lookup all possibly conflicting directory entries, the feed * inherits the chain's lock so do not unlock it on the iteration. */ chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, HAMMER2_LOOKUP_ALWAYS | HAMMER2_LOOKUP_SHARED); if (error == 0) { if (chain) error = chain->error; else error = HAMMER2_ERROR_ENOENT; } hammer2_xop_feed(&xop->head, chain, clindex, error); done: if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } } void hammer2_xop_delete(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_lookup_t *xop = &arg->xop_lookup; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error = 0; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); chain = NULL; if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } /* * Lookup all possibly conflicting directory entries, the feed * inherits the chain's lock so do not unlock it on the iteration. */ chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, HAMMER2_LOOKUP_NODATA); if (error == 0) { if (chain) error = chain->error; else error = HAMMER2_ERROR_ENOENT; } if (chain) { error = hammer2_chain_delete(parent, chain, xop->head.mtid, HAMMER2_DELETE_PERMANENT); } hammer2_xop_feed(&xop->head, NULL, clindex, error); done: if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } } /* * Generic scan * * WARNING! Fed chains must be locked shared so ownership can be transfered * and to prevent frontend/backend stalls that would occur with an * exclusive lock. The shared lock also allows chain->data to be * retained. */ void hammer2_xop_scanall(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_scanall_t *xop = &arg->xop_scanall; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error = 0; /* * Assert required flags. */ KKASSERT(xop->resolve_flags & HAMMER2_RESOLVE_SHARED); KKASSERT(xop->lookup_flags & HAMMER2_LOOKUP_SHARED); /* * The inode's chain is the iterator. If we cannot acquire it our * contribution ends here. */ parent = hammer2_inode_chain(xop->head.ip1, clindex, xop->resolve_flags); if (parent == NULL) { kprintf("xop_scanall: NULL parent\n"); goto done; } /* * Generic scan of exact records. Note that indirect blocks are * automatically recursed and will not be returned. */ chain = hammer2_chain_lookup(&parent, &key_next, xop->key_beg, xop->key_end, &error, xop->lookup_flags); while (chain) { error = hammer2_xop_feed(&xop->head, chain, clindex, 0); if (error) goto break2; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, xop->key_end, &error, xop->lookup_flags); } break2: if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } hammer2_chain_unlock(parent); hammer2_chain_drop(parent); done: hammer2_xop_feed(&xop->head, NULL, clindex, error); } /************************************************************************ * INODE LAYER XOPS * ************************************************************************ * */ /* * Helper to create a directory entry. */ void hammer2_xop_inode_mkdirent(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_mkdirent_t *xop = &arg->xop_mkdirent; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; size_t data_len; int error; if (hammer2_debug & 0x0001) kprintf("xop_inode_mkdirent: lhc %016jx clindex %d\n", xop->lhc, clindex); parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; chain = NULL; goto fail; } chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, 0); if (chain) { error = HAMMER2_ERROR_EEXIST; goto fail; } /* * We may be able to embed the directory entry directly in the * blockref. */ if (xop->dirent.namlen <= sizeof(chain->bref.check.buf)) data_len = 0; else data_len = HAMMER2_ALLOC_MIN; error = hammer2_chain_create(&parent, &chain, NULL, xop->head.ip1->pmp, HAMMER2_METH_DEFAULT, xop->lhc, 0, HAMMER2_BREF_TYPE_DIRENT, data_len, xop->head.mtid, 0, 0); if (error == 0) { /* * WARNING: chain->data->buf is sized to chain->bytes, * do not use sizeof(chain->data->buf), which * will be much larger. */ error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { chain->bref.embed.dirent = xop->dirent; if (xop->dirent.namlen <= sizeof(chain->bref.check.buf)) bcopy(xop->head.name1, chain->bref.check.buf, xop->dirent.namlen); else bcopy(xop->head.name1, chain->data->buf, xop->dirent.namlen); } } fail: if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Inode create helper (threaded, backend) * * Used by ncreate, nmknod, nsymlink, nmkdir. * Used by nlink and rename to create HARDLINK pointers. * * Frontend holds the parent directory ip locked exclusively. We * create the inode and feed the exclusively locked chain to the * frontend. */ void hammer2_xop_inode_create(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_create_t *xop = &arg->xop_create; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error; if (hammer2_debug & 0x0001) kprintf("xop_inode_create: lhc %016jx clindex %d\n", xop->lhc, clindex); parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; chain = NULL; goto fail; } chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, 0); if (chain) { error = HAMMER2_ERROR_EEXIST; goto fail; } error = hammer2_chain_create(&parent, &chain, NULL, xop->head.ip1->pmp, HAMMER2_METH_DEFAULT, xop->lhc, 0, HAMMER2_BREF_TYPE_INODE, HAMMER2_INODE_BYTES, xop->head.mtid, 0, xop->flags); if (error == 0) { error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { chain->data->ipdata.meta = xop->meta; if (xop->head.name1) { bcopy(xop->head.name1, chain->data->ipdata.filename, xop->head.name1_len); chain->data->ipdata.meta.name_len = xop->head.name1_len; } chain->data->ipdata.meta.name_key = xop->lhc; } } fail: if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Create inode as above but leave it detached from the hierarchy. */ void hammer2_xop_inode_create_det(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_create_t *xop = &arg->xop_create; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_chain_t *null_parent; hammer2_key_t key_next; hammer2_inode_t *pip; hammer2_inode_t *iroot; int error; if (hammer2_debug & 0x0001) kprintf("xop_inode_create_det: lhc %016jx clindex %d\n", xop->lhc, clindex); pip = xop->head.ip1; iroot = pip->pmp->iroot; parent = hammer2_inode_chain(iroot, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; chain = NULL; goto fail; } chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, 0); if (chain) { error = HAMMER2_ERROR_EEXIST; goto fail; } /* * Create as a detached chain with no parent. We must specify * methods */ null_parent = NULL; error = hammer2_chain_create(&null_parent, &chain, parent->hmp, pip->pmp, HAMMER2_ENC_COMP(pip->meta.comp_algo) + HAMMER2_ENC_CHECK(pip->meta.check_algo), xop->lhc, 0, HAMMER2_BREF_TYPE_INODE, HAMMER2_INODE_BYTES, xop->head.mtid, 0, xop->flags); if (error == 0) { error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { chain->data->ipdata.meta = xop->meta; if (xop->head.name1) { bcopy(xop->head.name1, chain->data->ipdata.filename, xop->head.name1_len); chain->data->ipdata.meta.name_len = xop->head.name1_len; } chain->data->ipdata.meta.name_key = xop->lhc; } } fail: if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Take a detached chain and insert it into the topology */ void hammer2_xop_inode_create_ins(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_create_t *xop = &arg->xop_create; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error; if (hammer2_debug & 0x0001) kprintf("xop_inode_create_ins: lhc %016jx clindex %d\n", xop->lhc, clindex); /* * (parent) will be the insertion point for inode under iroot */ parent = hammer2_inode_chain(xop->head.ip1->pmp->iroot, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; chain = NULL; goto fail; } chain = hammer2_chain_lookup(&parent, &key_next, xop->lhc, xop->lhc, &error, 0); if (chain) { error = HAMMER2_ERROR_EEXIST; goto fail; } /* * (chain) is the detached inode that is being inserted */ chain = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); if (chain == NULL) { error = HAMMER2_ERROR_EIO; chain = NULL; goto fail; } /* * This create call will insert the non-NULL chain into parent. * Most of the auxillary fields are ignored since the chain already * exists. */ error = hammer2_chain_create(&parent, &chain, NULL, xop->head.ip1->pmp, HAMMER2_METH_DEFAULT, xop->lhc, 0, HAMMER2_BREF_TYPE_INODE, HAMMER2_INODE_BYTES, xop->head.mtid, 0, xop->flags); #if 0 if (error == 0) { error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error == 0) { chain->data->ipdata.meta = xop->meta; if (xop->head.name1) { bcopy(xop->head.name1, chain->data->ipdata.filename, xop->head.name1_len); chain->data->ipdata.meta.name_len = xop->head.name1_len; } chain->data->ipdata.meta.name_key = xop->lhc; } } #endif fail: if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Inode delete helper (backend, threaded) * * Generally used by hammer2_run_sideq() */ void hammer2_xop_inode_destroy(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_destroy_t *xop = &arg->xop_destroy; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_inode_t *ip; int error; /* * We need the precise parent chain to issue the deletion. */ ip = xop->head.ip1; chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS); if (chain == NULL) { parent = NULL; error = HAMMER2_ERROR_EIO; goto done; } if (ip->flags & HAMMER2_INODE_CREATING) { /* * Inode's chains are not linked into the media topology * because it is a new inode (which is now being destroyed). */ parent = NULL; } else { /* * Inode's chains are linked into the media topology */ parent = hammer2_chain_getparent(chain, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } } KKASSERT(chain->parent == parent); /* * We have the correct parent, we can issue the deletion. */ hammer2_chain_delete(parent, chain, xop->head.mtid, 0); error = 0; done: hammer2_xop_feed(&xop->head, NULL, clindex, error); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } void hammer2_xop_inode_unlinkall(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_unlinkall_t *xop = &arg->xop_unlinkall; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; int error; /* * We need the precise parent chain to issue the deletion. */ parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); chain = NULL; if (parent == NULL) { error = 0; goto done; } chain = hammer2_chain_lookup(&parent, &key_next, xop->key_beg, xop->key_end, &error, HAMMER2_LOOKUP_ALWAYS); while (chain) { hammer2_chain_delete(parent, chain, xop->head.mtid, HAMMER2_DELETE_PERMANENT); hammer2_xop_feed(&xop->head, chain, clindex, chain->error); /* depend on function to unlock the shared lock */ chain = hammer2_chain_next(&parent, chain, &key_next, key_next, xop->key_end, &error, HAMMER2_LOOKUP_ALWAYS); } done: if (error == 0) error = HAMMER2_ERROR_ENOENT; hammer2_xop_feed(&xop->head, NULL, clindex, error); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } void hammer2_xop_inode_connect(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_connect_t *xop = &arg->xop_connect; hammer2_inode_data_t *wipdata; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_pfs_t *pmp; hammer2_key_t key_dummy; int error; /* * Get directory, then issue a lookup to prime the parent chain * for the create. The lookup is expected to fail. */ pmp = xop->head.ip1->pmp; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); if (parent == NULL) { chain = NULL; error = HAMMER2_ERROR_EIO; goto fail; } chain = hammer2_chain_lookup(&parent, &key_dummy, xop->lhc, xop->lhc, &error, 0); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); chain = NULL; error = HAMMER2_ERROR_EEXIST; goto fail; } if (error) goto fail; /* * Adjust the filename in the inode, set the name key. * * NOTE: Frontend must also adjust ip2->meta on success, we can't * do it here. */ chain = hammer2_inode_chain(xop->head.ip2, clindex, HAMMER2_RESOLVE_ALWAYS); error = hammer2_chain_modify(chain, xop->head.mtid, 0, 0); if (error) goto fail; wipdata = &chain->data->ipdata; hammer2_inode_modify(xop->head.ip2); if (xop->head.name1) { bzero(wipdata->filename, sizeof(wipdata->filename)); bcopy(xop->head.name1, wipdata->filename, xop->head.name1_len); wipdata->meta.name_len = xop->head.name1_len; } wipdata->meta.name_key = xop->lhc; /* * Reconnect the chain to the new parent directory */ error = hammer2_chain_create(&parent, &chain, NULL, pmp, HAMMER2_METH_DEFAULT, xop->lhc, 0, HAMMER2_BREF_TYPE_INODE, HAMMER2_INODE_BYTES, xop->head.mtid, 0, 0); /* * Feed result back. */ fail: hammer2_xop_feed(&xop->head, NULL, clindex, error); if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } } /* * Synchronize the in-memory inode with the chain. This does not flush * the chain to disk. Instead, it makes front-end inode changes visible * in the chain topology, thus visible to the backend. This is done in an * ad-hoc manner outside of the filesystem vfs_sync, and in a controlled * manner inside the vfs_sync. */ void hammer2_xop_inode_chain_sync(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_fsync_t *xop = &arg->xop_fsync; hammer2_chain_t *parent; hammer2_chain_t *chain; int error; parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); chain = NULL; if (parent == NULL) { error = HAMMER2_ERROR_EIO; goto done; } if (parent->error) { error = parent->error; goto done; } error = 0; if ((xop->ipflags & HAMMER2_INODE_RESIZED) == 0) { /* osize must be ignored */ } else if (xop->meta.size < xop->osize) { /* * We must delete any chains beyond the EOF. The chain * straddling the EOF will be pending in the bioq. */ hammer2_key_t lbase; hammer2_key_t key_next; lbase = (xop->meta.size + HAMMER2_PBUFMASK64) & ~HAMMER2_PBUFMASK64; chain = hammer2_chain_lookup(&parent, &key_next, lbase, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NODIRECT); while (chain) { /* * Degenerate embedded case, nothing to loop on */ switch (chain->bref.type) { case HAMMER2_BREF_TYPE_DIRENT: case HAMMER2_BREF_TYPE_INODE: KKASSERT(0); break; case HAMMER2_BREF_TYPE_DATA: hammer2_chain_delete(parent, chain, xop->head.mtid, HAMMER2_DELETE_PERMANENT); break; } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_NODATA | HAMMER2_LOOKUP_NODIRECT); } /* * Reset to point at inode for following code, if necessary. */ if (parent->bref.type != HAMMER2_BREF_TYPE_INODE) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); parent = hammer2_inode_chain(xop->head.ip1, clindex, HAMMER2_RESOLVE_ALWAYS); kprintf("xop_inode_chain_sync: TRUNCATE RESET on '%s'\n", parent->data->ipdata.filename); } } /* * Sync the inode meta-data, potentially clear the blockset area * of direct data so it can be used for blockrefs. */ if (error == 0) { error = hammer2_chain_modify(parent, xop->head.mtid, 0, 0); if (error == 0) { parent->data->ipdata.meta = xop->meta; if (xop->clear_directdata) { bzero(&parent->data->ipdata.u.blockset, sizeof(parent->data->ipdata.u.blockset)); } } } done: if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_xop_feed(&xop->head, NULL, clindex, error); } /* * Backend for hammer2_vop_bmap() */ void hammer2_xop_bmap(hammer2_xop_t *arg, void *scratch, int clindex) { hammer2_xop_bmap_t *xop = &arg->xop_bmap; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_inode_t *ip; hammer2_key_t lbase; hammer2_key_t key_dummy; int error = 0; ip = xop->head.ip1; chain = NULL; parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); if (parent == NULL) { kprintf("xop_bmap: NULL parent\n"); error = HAMMER2_ERROR_EIO; goto done; } xop->offset = HAMMER2_OFF_MASK; lbase = xop->loffset & ~HAMMER2_OFF_MASK_RADIX; chain = hammer2_chain_lookup(&parent, &key_dummy, lbase, lbase, &error, HAMMER2_LOOKUP_SHARED); if (error == 0) { if (chain) { error = chain->error; if (error == 0) xop->offset = chain->bref.data_off; } else { error = HAMMER2_ERROR_ENOENT; } } done: hammer2_xop_feed(&xop->head, chain, clindex, error); if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } } |