sys/vfs/hammer/hammer_io.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 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 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 | /* * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * 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. */ /* * IO Primitives and buffer cache management * * All major data-tracking structures in HAMMER contain a struct hammer_io * which is used to manage their backing store. We use filesystem buffers * for backing store and we leave them passively associated with their * HAMMER structures. * * If the kernel tries to destroy a passively associated buf which we cannot * yet let go we set B_LOCKED in the buffer and then actively released it * later when we can. * * The io_token is required for anything which might race bioops and bio_done * callbacks, with one exception: A successful hammer_try_interlock_norefs(). * the fs_token will be held in all other cases. */ #include <sys/buf2.h> #include "hammer.h" static void hammer_io_modify(hammer_io_t io, int count); static void hammer_io_deallocate(struct buf *bp); static void hammer_indirect_callback(struct bio *bio); static void hammer_io_direct_write_complete(struct bio *nbio); static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data); static void hammer_io_set_modlist(hammer_io_t io); static __inline void hammer_io_flush_mark(hammer_volume_t volume); static struct bio_ops hammer_bioops; static int hammer_mod_rb_compare(hammer_io_t io1, hammer_io_t io2) { hammer_off_t io1_offset; hammer_off_t io2_offset; /* * Encoded offsets are neither valid block device offsets * nor valid zone-X offsets. */ io1_offset = HAMMER_ENCODE(0, io1->volume->vol_no, io1->offset); io2_offset = HAMMER_ENCODE(0, io2->volume->vol_no, io2->offset); if (io1_offset < io2_offset) return(-1); if (io1_offset > io2_offset) return(1); return(0); } RB_GENERATE(hammer_mod_rb_tree, hammer_io, rb_node, hammer_mod_rb_compare); /* * Initialize a new, already-zero'd hammer_io structure, or reinitialize * an existing hammer_io structure which may have switched to another type. */ void hammer_io_init(hammer_io_t io, hammer_volume_t volume, hammer_io_type_t type) { io->volume = volume; io->hmp = volume->io.hmp; io->type = type; } hammer_io_type_t hammer_zone_to_iotype(int zone) { hammer_io_type_t iotype; switch(zone) { case HAMMER_ZONE_RAW_VOLUME_INDEX: iotype = HAMMER_IOTYPE_VOLUME; break; case HAMMER_ZONE_RAW_BUFFER_INDEX: case HAMMER_ZONE_FREEMAP_INDEX: case HAMMER_ZONE_BTREE_INDEX: case HAMMER_ZONE_META_INDEX: iotype = HAMMER_IOTYPE_META_BUFFER; break; case HAMMER_ZONE_UNDO_INDEX: iotype = HAMMER_IOTYPE_UNDO_BUFFER; break; case HAMMER_ZONE_LARGE_DATA_INDEX: case HAMMER_ZONE_SMALL_DATA_INDEX: iotype = HAMMER_IOTYPE_DATA_BUFFER; break; default: iotype = HAMMER_IOTYPE_DUMMY; break; } return(iotype); } static const char* hammer_io_to_iostring(hammer_io_t io) { const char *iostr = NULL; switch(io->type) { case HAMMER_IOTYPE_VOLUME: iostr = "volume"; break; case HAMMER_IOTYPE_META_BUFFER: switch(HAMMER_ZONE(HAMMER_ITOB(io)->zoneX_offset)) { case HAMMER_ZONE_RAW_BUFFER: iostr = "meta/raw_buffer"; break; case HAMMER_ZONE_FREEMAP: iostr = "meta/freemap"; break; case HAMMER_ZONE_BTREE: iostr = "meta/btree"; break; case HAMMER_ZONE_META: iostr = "meta/meta"; break; } break; case HAMMER_IOTYPE_UNDO_BUFFER: iostr = "undo"; break; case HAMMER_IOTYPE_DATA_BUFFER: switch(HAMMER_ZONE(HAMMER_ITOB(io)->zoneX_offset)) { case HAMMER_ZONE_LARGE_DATA: iostr = "data/large_data"; break; case HAMMER_ZONE_SMALL_DATA: iostr = "data/small_data"; break; } break; case HAMMER_IOTYPE_DUMMY: iostr = "dummy"; break; default: hpanic("bad io type"); break; } return(iostr); } /* * Helper routine to disassociate a buffer cache buffer from an I/O * structure. The io must be interlocked and marked appropriately for * reclamation. * * The io must be in a released state with the io->bp owned and * locked by the caller of this function. When not called from an * io_deallocate() this cannot race an io_deallocate() since the * kernel would be unable to get the buffer lock in that case. * (The released state in this case means we own the bp, not the * hammer_io structure). * * The io may have 0 or 1 references depending on who called us. The * caller is responsible for dealing with the refs. * * This call can only be made when no action is required on the buffer. * * This function is guaranteed not to race against anything because we * own both the io lock and the bp lock and are interlocked with no * references. */ static void hammer_io_disassociate(hammer_io_t io) { struct buf *bp = io->bp; KKASSERT(io->released); KKASSERT(io->modified == 0); KKASSERT(hammer_buf_peek_io(bp) == io); buf_dep_init(bp); io->bp = NULL; /* * If the buffer was locked someone wanted to get rid of it. */ if (bp->b_flags & B_LOCKED) { atomic_add_int(&hammer_count_io_locked, -1); bp->b_flags &= ~B_LOCKED; } if (io->reclaim) { bp->b_flags |= B_NOCACHE|B_RELBUF; io->reclaim = 0; } switch(io->type) { case HAMMER_IOTYPE_VOLUME: HAMMER_ITOV(io)->ondisk = NULL; break; case HAMMER_IOTYPE_DATA_BUFFER: case HAMMER_IOTYPE_META_BUFFER: case HAMMER_IOTYPE_UNDO_BUFFER: HAMMER_ITOB(io)->ondisk = NULL; break; case HAMMER_IOTYPE_DUMMY: hpanic("bad io type"); break; } } /* * Wait for any physical IO to complete * * XXX we aren't interlocked against a spinlock or anything so there * is a small window in the interlock / io->running == 0 test. */ void hammer_io_wait(hammer_io_t io) { if (io->running) { hammer_mount_t hmp = io->hmp; lwkt_gettoken(&hmp->io_token); while (io->running) { io->waiting = 1; tsleep_interlock(io, 0); if (io->running) tsleep(io, PINTERLOCKED, "hmrflw", hz); } lwkt_reltoken(&hmp->io_token); } } /* * Wait for all currently queued HAMMER-initiated I/Os to complete. * * This is not supposed to count direct I/O's but some can leak * through (for non-full-sized direct I/Os). */ void hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush) { struct hammer_io iodummy; hammer_io_t io; /* * Degenerate case, no I/O is running */ lwkt_gettoken(&hmp->io_token); if (TAILQ_EMPTY(&hmp->iorun_list)) { lwkt_reltoken(&hmp->io_token); if (doflush) hammer_io_flush_sync(hmp); return; } bzero(&iodummy, sizeof(iodummy)); iodummy.type = HAMMER_IOTYPE_DUMMY; /* * Add placemarker and then wait until it becomes the head of * the list. */ TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry); while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) { tsleep(&iodummy, 0, ident, 0); } /* * Chain in case several placemarkers are present. */ TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry); io = TAILQ_FIRST(&hmp->iorun_list); if (io && io->type == HAMMER_IOTYPE_DUMMY) wakeup(io); lwkt_reltoken(&hmp->io_token); if (doflush) hammer_io_flush_sync(hmp); } /* * Clear a flagged error condition on a I/O buffer. The caller must hold * its own ref on the buffer. */ void hammer_io_clear_error(hammer_io_t io) { hammer_mount_t hmp = io->hmp; lwkt_gettoken(&hmp->io_token); if (io->ioerror) { io->ioerror = 0; hammer_rel(&io->lock); KKASSERT(hammer_isactive(&io->lock)); } lwkt_reltoken(&hmp->io_token); } void hammer_io_clear_error_noassert(hammer_io_t io) { hammer_mount_t hmp = io->hmp; lwkt_gettoken(&hmp->io_token); if (io->ioerror) { io->ioerror = 0; hammer_rel(&io->lock); } lwkt_reltoken(&hmp->io_token); } /* * This is an advisory function only which tells the buffer cache * the bp is not a meta-data buffer, even though it is backed by * a block device. * * This is used by HAMMER's reblocking code to avoid trying to * swapcache the filesystem's data when it is read or written * by the reblocking code. * * The caller has a ref on the buffer preventing the bp from * being disassociated from it. */ void hammer_io_notmeta(hammer_buffer_t buffer) { if ((buffer->io.bp->b_flags & B_NOTMETA) == 0) { hammer_mount_t hmp = buffer->io.hmp; lwkt_gettoken(&hmp->io_token); buffer->io.bp->b_flags |= B_NOTMETA; lwkt_reltoken(&hmp->io_token); } } /* * Load bp for a HAMMER structure. The io must be exclusively locked by * the caller. * * This routine is mostly used on meta-data and small-data blocks. Generally * speaking HAMMER assumes some locality of reference and will cluster. * * Note that the caller (hammer_ondisk.c) may place further restrictions * on clusterability via the limit (in bytes). Typically large-data * zones cannot be clustered due to their mixed buffer sizes. This is * not an issue since such clustering occurs in hammer_vnops at the * regular file layer, whereas this is the buffered block device layer. * * No I/O callbacks can occur while we hold the buffer locked. */ int hammer_io_read(struct vnode *devvp, hammer_io_t io, int limit) { struct buf *bp; int error; if ((bp = io->bp) == NULL) { int hce = hammer_cluster_enable; atomic_add_long(&hammer_count_io_running_read, io->bytes); if (hce && limit > io->bytes) { error = cluster_read(devvp, io->offset + limit, io->offset, io->bytes, HAMMER_CLUSTER_SIZE, HAMMER_CLUSTER_SIZE * hce, &io->bp); } else { error = bread(devvp, io->offset, io->bytes, &io->bp); } hammer_stats_disk_read += io->bytes; atomic_add_long(&hammer_count_io_running_read, -io->bytes); /* * The code generally assumes b_ops/b_dep has been set-up, * even if we error out here. */ bp = io->bp; if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IOISSUED)) { hdkprintf("zone2_offset %016jx %s\n", (intmax_t)bp->b_bio2.bio_offset, hammer_io_to_iostring(io)); } bp->b_flags &= ~B_IOISSUED; bp->b_ops = &hammer_bioops; hammer_buf_attach_io(bp, io); /* locked by the io lock */ BUF_KERNPROC(bp); KKASSERT(io->modified == 0); KKASSERT(io->running == 0); KKASSERT(io->waiting == 0); io->released = 0; /* we hold an active lock on bp */ } else { error = 0; } return(error); } /* * Similar to hammer_io_read() but returns a zero'd out buffer instead. * Must be called with the IO exclusively locked. * * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background * I/O by forcing the buffer to not be in a released state before calling * it. * * This function will also mark the IO as modified but it will not * increment the modify_refs count. * * No I/O callbacks can occur while we hold the buffer locked. */ int hammer_io_new(struct vnode *devvp, hammer_io_t io) { struct buf *bp; if ((bp = io->bp) == NULL) { io->bp = getblk(devvp, io->offset, io->bytes, 0, 0); bp = io->bp; bp->b_ops = &hammer_bioops; hammer_buf_attach_io(bp, io); /* locked by the io lock */ io->released = 0; KKASSERT(io->running == 0); io->waiting = 0; BUF_KERNPROC(bp); } else { if (io->released) { regetblk(bp); BUF_KERNPROC(bp); io->released = 0; } } hammer_io_modify(io, 0); vfs_bio_clrbuf(bp); return(0); } /* * Advance the activity count on the underlying buffer because * HAMMER does not getblk/brelse on every access. * * The io->bp cannot go away while the buffer is referenced. */ void hammer_io_advance(hammer_io_t io) { if (io->bp) buf_act_advance(io->bp); } /* * Remove potential device level aliases against buffers managed by high level * vnodes. Aliases can also be created due to mixed buffer sizes or via * direct access to the backing store device. * * This is nasty because the buffers are also VMIO-backed. Even if a buffer * does not exist its backing VM pages might, and we have to invalidate * those as well or a getblk() will reinstate them. * * Buffer cache buffers associated with hammer_buffers cannot be * invalidated. */ int hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset) { hammer_io_t io; hammer_mount_t hmp; hammer_off_t phys_offset; struct buf *bp; int error; hmp = volume->io.hmp; lwkt_gettoken(&hmp->io_token); /* * If a device buffer already exists for the specified physical * offset use that, otherwise instantiate a buffer to cover any * related VM pages, set BNOCACHE, and brelse(). */ phys_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset); if ((bp = findblk(volume->devvp, phys_offset, 0)) != NULL) bremfree(bp); else bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0); if ((io = hammer_buf_peek_io(bp)) != NULL) { #if 0 hammer_ref(&io->lock); hammer_io_clear_modify(io, 1); bundirty(bp); io->released = 0; BUF_KERNPROC(bp); io->reclaim = 1; io->waitdep = 1; /* XXX this is a fs_token field */ KKASSERT(hammer_isactive(&io->lock) == 1); hammer_rel_buffer(HAMMER_ITOB(io), 0); /*hammer_io_deallocate(bp);*/ #endif bqrelse(bp); error = EAGAIN; } else { KKASSERT((bp->b_flags & B_LOCKED) == 0); bundirty(bp); bp->b_flags |= B_NOCACHE|B_RELBUF; brelse(bp); error = 0; } lwkt_reltoken(&hmp->io_token); return(error); } /* * This routine is called on the last reference to a hammer structure. * The io must be interlocked with a refcount of zero. The hammer structure * will remain interlocked on return. * * This routine may return a non-NULL bp to the caller for dispoal. * The caller typically brelse()'s the bp. * * The bp may or may not still be passively associated with the IO. It * will remain passively associated if it is unreleasable (e.g. a modified * meta-data buffer). * * The only requirement here is that modified meta-data and volume-header * buffer may NOT be disassociated from the IO structure, and consequently * we also leave such buffers actively associated with the IO if they already * are (since the kernel can't do anything with them anyway). Only the * flusher is allowed to write such buffers out. Modified pure-data and * undo buffers are returned to the kernel but left passively associated * so we can track when the kernel writes the bp out. */ struct buf * hammer_io_release(hammer_io_t io, int flush) { struct buf *bp; if ((bp = io->bp) == NULL) return(NULL); /* * Try to flush a dirty IO to disk if asked to by the * caller or if the kernel tried to flush the buffer in the past. * * Kernel-initiated flushes are only allowed for pure-data buffers. * meta-data and volume buffers can only be flushed explicitly * by HAMMER. */ if (io->modified) { if (flush) { hammer_io_flush(io, 0); } else if (bp->b_flags & B_LOCKED) { switch(io->type) { case HAMMER_IOTYPE_DATA_BUFFER: hammer_io_flush(io, 0); break; case HAMMER_IOTYPE_UNDO_BUFFER: hammer_io_flush(io, hammer_undo_reclaim(io)); break; default: break; } } /* else no explicit request to flush the buffer */ } /* * Wait for the IO to complete if asked to. This occurs when * the buffer must be disposed of definitively during an umount * or buffer invalidation. */ if (io->waitdep && io->running) { hammer_io_wait(io); } /* * Return control of the buffer to the kernel (with the provisio * that our bioops can override kernel decisions with regards to * the buffer). */ if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) { /* * Always disassociate the bp if an explicit flush * was requested and the IO completed with no error * (so unmount can really clean up the structure). */ if (io->released) { regetblk(bp); BUF_KERNPROC(bp); } else { io->released = 1; } hammer_io_disassociate(io); /* return the bp */ } else if (io->modified) { /* * Only certain IO types can be released to the kernel if * the buffer has been modified. * * volume and meta-data IO types may only be explicitly * flushed by HAMMER. */ switch(io->type) { case HAMMER_IOTYPE_DATA_BUFFER: case HAMMER_IOTYPE_UNDO_BUFFER: if (io->released == 0) { io->released = 1; bp->b_flags |= B_CLUSTEROK; bdwrite(bp); } break; default: break; } bp = NULL; /* bp left associated */ } else if (io->released == 0) { /* * Clean buffers can be generally released to the kernel. * We leave the bp passively associated with the HAMMER * structure and use bioops to disconnect it later on * if the kernel wants to discard the buffer. * * We can steal the structure's ownership of the bp. */ io->released = 1; if (bp->b_flags & B_LOCKED) { hammer_io_disassociate(io); /* return the bp */ } else { if (io->reclaim) { hammer_io_disassociate(io); /* return the bp */ } else { /* return the bp (bp passively associated) */ } } } else { /* * A released buffer is passively associate with our * hammer_io structure. The kernel cannot destroy it * without making a bioops call. If the kernel (B_LOCKED) * or we (reclaim) requested that the buffer be destroyed * we destroy it, otherwise we do a quick get/release to * reset its position in the kernel's LRU list. * * Leaving the buffer passively associated allows us to * use the kernel's LRU buffer flushing mechanisms rather * then rolling our own. * * XXX there are two ways of doing this. We can re-acquire * and passively release to reset the LRU, or not. */ if (io->running == 0) { regetblk(bp); if ((bp->b_flags & B_LOCKED) || io->reclaim) { hammer_io_disassociate(io); /* return the bp */ } else { /* return the bp (bp passively associated) */ } } else { /* * bp is left passively associated but we do not * try to reacquire it. Interactions with the io * structure will occur on completion of the bp's * I/O. */ bp = NULL; } } return(bp); } /* * This routine is called with a locked IO when a flush is desired and * no other references to the structure exists other then ours. This * routine is ONLY called when HAMMER believes it is safe to flush a * potentially modified buffer out. * * The locked io or io reference prevents a flush from being initiated * by the kernel. */ void hammer_io_flush(hammer_io_t io, int reclaim) { struct buf *bp; hammer_mount_t hmp; /* * Degenerate case - nothing to flush if nothing is dirty. */ if (io->modified == 0) return; KKASSERT(io->bp); KKASSERT(io->modify_refs <= 0); /* * Acquire ownership of the bp, particularly before we clear our * modified flag. * * We are going to bawrite() this bp. Don't leave a window where * io->released is set, we actually own the bp rather then our * buffer. * * The io_token should not be required here as only */ hmp = io->hmp; bp = io->bp; if (io->released) { regetblk(bp); /* BUF_KERNPROC(io->bp); */ /* io->released = 0; */ KKASSERT(io->released); KKASSERT(io->bp == bp); } else { io->released = 1; } if (reclaim) { io->reclaim = 1; if ((bp->b_flags & B_LOCKED) == 0) { bp->b_flags |= B_LOCKED; atomic_add_int(&hammer_count_io_locked, 1); } } /* * Acquire exclusive access to the bp and then clear the modified * state of the buffer prior to issuing I/O to interlock any * modifications made while the I/O is in progress. This shouldn't * happen anyway but losing data would be worse. The modified bit * will be rechecked after the IO completes. * * NOTE: This call also finalizes the buffer's content (inval == 0). * * This is only legal when lock.refs == 1 (otherwise we might clear * the modified bit while there are still users of the cluster * modifying the data). * * Do this before potentially blocking so any attempt to modify the * ondisk while we are blocked blocks waiting for us. */ hammer_ref(&io->lock); hammer_io_clear_modify(io, 0); hammer_rel(&io->lock); if (hammer_debug_io & 0x0002) hdkprintf("%016jx\n", bp->b_bio1.bio_offset); /* * Transfer ownership to the kernel and initiate I/O. * * NOTE: We do not hold io_token so an atomic op is required to * update io_running_space. */ io->running = 1; atomic_add_long(&hmp->io_running_space, io->bytes); atomic_add_long(&hammer_count_io_running_write, io->bytes); lwkt_gettoken(&hmp->io_token); TAILQ_INSERT_TAIL(&hmp->iorun_list, io, iorun_entry); lwkt_reltoken(&hmp->io_token); cluster_awrite(bp); hammer_io_flush_mark(io->volume); } /************************************************************************ * BUFFER DIRTYING * ************************************************************************ * * These routines deal with dependancies created when IO buffers get * modified. The caller must call hammer_modify_*() on a referenced * HAMMER structure prior to modifying its on-disk data. * * Any intent to modify an IO buffer acquires the related bp and imposes * various write ordering dependancies. */ /* * Mark a HAMMER structure as undergoing modification. Meta-data buffers * are locked until the flusher can deal with them, pure data buffers * can be written out. * * The referenced io prevents races. */ static void hammer_io_modify(hammer_io_t io, int count) { /* * io->modify_refs must be >= 0 */ while (io->modify_refs < 0) { io->waitmod = 1; tsleep(io, 0, "hmrmod", 0); } /* * Shortcut if nothing to do. */ KKASSERT(hammer_isactive(&io->lock) && io->bp != NULL); io->modify_refs += count; if (io->modified && io->released == 0) return; /* * NOTE: It is important not to set the modified bit * until after we have acquired the bp or we risk * racing against checkwrite. */ hammer_lock_ex(&io->lock); if (io->released) { regetblk(io->bp); BUF_KERNPROC(io->bp); io->released = 0; } if (io->modified == 0) { hammer_io_set_modlist(io); io->modified = 1; } hammer_unlock(&io->lock); } static __inline void hammer_io_modify_done(hammer_io_t io) { KKASSERT(io->modify_refs > 0); --io->modify_refs; if (io->modify_refs == 0 && io->waitmod) { io->waitmod = 0; wakeup(io); } } /* * The write interlock blocks other threads trying to modify a buffer * (they block in hammer_io_modify()) after us, or blocks us while other * threads are in the middle of modifying a buffer. * * The caller also has a ref on the io, however if we are not careful * we will race bioops callbacks (checkwrite). To deal with this * we must at least acquire and release the io_token, and it is probably * better to hold it through the setting of modify_refs. */ void hammer_io_write_interlock(hammer_io_t io) { hammer_mount_t hmp = io->hmp; lwkt_gettoken(&hmp->io_token); while (io->modify_refs != 0) { io->waitmod = 1; tsleep(io, 0, "hmrmod", 0); } io->modify_refs = -1; lwkt_reltoken(&hmp->io_token); } void hammer_io_done_interlock(hammer_io_t io) { KKASSERT(io->modify_refs == -1); io->modify_refs = 0; if (io->waitmod) { io->waitmod = 0; wakeup(io); } } /* * Caller intends to modify a volume's ondisk structure. * * This is only allowed if we are the flusher or we have a ref on the * sync_lock. */ void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume, void *base, int len) { KKASSERT (trans == NULL || trans->sync_lock_refs > 0); hammer_io_modify(&volume->io, 1); if (len) { intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk; KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0); hammer_generate_undo(trans, HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset), base, len); } } /* * Caller intends to modify a buffer's ondisk structure. * * This is only allowed if we are the flusher or we have a ref on the * sync_lock. */ void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer, void *base, int len) { KKASSERT (trans == NULL || trans->sync_lock_refs > 0); hammer_io_modify(&buffer->io, 1); if (len) { intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk; KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0); hammer_generate_undo(trans, buffer->zone2_offset + rel_offset, base, len); } } void hammer_modify_volume_done(hammer_volume_t volume) { hammer_io_modify_done(&volume->io); } void hammer_modify_buffer_done(hammer_buffer_t buffer) { hammer_io_modify_done(&buffer->io); } /* * Mark an entity as not being dirty any more and finalize any * delayed adjustments to the buffer. * * Delayed adjustments are an important performance enhancement, allowing * us to avoid recalculating B-Tree node CRCs over and over again when * making bulk-modifications to the B-Tree. * * If inval is non-zero delayed adjustments are ignored. * * This routine may dereference related btree nodes and cause the * buffer to be dereferenced. The caller must own a reference on io. */ void hammer_io_clear_modify(hammer_io_t io, int inval) { hammer_mount_t hmp; /* * io_token is needed to avoid races on mod_root */ if (io->modified == 0) return; hmp = io->hmp; lwkt_gettoken(&hmp->io_token); if (io->modified == 0) { lwkt_reltoken(&hmp->io_token); return; } /* * Take us off the mod-list and clear the modified bit. */ KKASSERT(io->mod_root != NULL); if (io->mod_root == &io->hmp->volu_root || io->mod_root == &io->hmp->meta_root) { io->hmp->locked_dirty_space -= io->bytes; atomic_add_long(&hammer_count_dirtybufspace, -io->bytes); } RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io); io->mod_root = NULL; io->modified = 0; lwkt_reltoken(&hmp->io_token); /* * If this bit is not set there are no delayed adjustments. */ if (io->gencrc == 0) return; io->gencrc = 0; /* * Finalize requested CRCs. The NEEDSCRC flag also holds a reference * on the node (& underlying buffer). Release the node after clearing * the flag. */ if (io->type == HAMMER_IOTYPE_META_BUFFER) { hammer_buffer_t buffer = HAMMER_ITOB(io); hammer_node_t node; restart: TAILQ_FOREACH(node, &buffer->node_list, entry) { if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0) continue; node->flags &= ~HAMMER_NODE_NEEDSCRC; KKASSERT(node->ondisk); if (inval == 0) hammer_crc_set_btree(hmp->version, node->ondisk); hammer_rel_node(node); goto restart; } } /* caller must still have ref on io */ KKASSERT(hammer_isactive(&io->lock)); } /* * Clear the IO's modify list. Even though the IO is no longer modified * it may still be on the lose_root. This routine is called just before * the governing hammer_buffer is destroyed. * * mod_root requires io_token protection. */ void hammer_io_clear_modlist(hammer_io_t io) { hammer_mount_t hmp = io->hmp; KKASSERT(io->modified == 0); if (io->mod_root) { lwkt_gettoken(&hmp->io_token); if (io->mod_root) { KKASSERT(io->mod_root == &io->hmp->lose_root); RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io); io->mod_root = NULL; } lwkt_reltoken(&hmp->io_token); } } static void hammer_io_set_modlist(hammer_io_t io) { hammer_mount_t hmp = io->hmp; lwkt_gettoken(&hmp->io_token); KKASSERT(io->mod_root == NULL); switch(io->type) { case HAMMER_IOTYPE_VOLUME: io->mod_root = &hmp->volu_root; hmp->locked_dirty_space += io->bytes; atomic_add_long(&hammer_count_dirtybufspace, io->bytes); break; case HAMMER_IOTYPE_META_BUFFER: io->mod_root = &hmp->meta_root; hmp->locked_dirty_space += io->bytes; atomic_add_long(&hammer_count_dirtybufspace, io->bytes); break; case HAMMER_IOTYPE_UNDO_BUFFER: io->mod_root = &hmp->undo_root; break; case HAMMER_IOTYPE_DATA_BUFFER: io->mod_root = &hmp->data_root; break; case HAMMER_IOTYPE_DUMMY: hpanic("bad io type"); break; /* NOT REACHED */ } if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) { hpanic("duplicate entry @ %d:%015jx", io->volume->vol_no, io->offset); /* NOT REACHED */ } lwkt_reltoken(&hmp->io_token); } /************************************************************************ * HAMMER_BIOOPS * ************************************************************************ * */ /* * Pre-IO initiation kernel callback - cluster build only * * bioops callback - hold io_token */ static void hammer_io_start(struct buf *bp) { /* nothing to do, so io_token not needed */ } /* * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT! * * NOTE: HAMMER may modify a data buffer after we have initiated write * I/O. * * NOTE: MPSAFE callback * * bioops callback - hold io_token */ static void hammer_io_complete(struct buf *bp) { hammer_io_t io = hammer_buf_peek_io(bp); hammer_mount_t hmp = io->hmp; hammer_io_t ionext; lwkt_gettoken(&hmp->io_token); KKASSERT(io->released == 1); /* * Deal with people waiting for I/O to drain */ if (io->running) { /* * Deal with critical write errors. Once a critical error * has been flagged in hmp the UNDO FIFO will not be updated. * That way crash recover will give us a consistent * filesystem. * * Because of this we can throw away failed UNDO buffers. If * we throw away META or DATA buffers we risk corrupting * the now read-only version of the filesystem visible to * the user. Clear B_ERROR so the buffer is not re-dirtied * by the kernel and ref the io so it doesn't get thrown * away. */ if (bp->b_flags & B_ERROR) { lwkt_gettoken(&hmp->fs_token); hammer_critical_error(hmp, NULL, bp->b_error, "while flushing meta-data"); lwkt_reltoken(&hmp->fs_token); switch(io->type) { case HAMMER_IOTYPE_UNDO_BUFFER: break; default: if (io->ioerror == 0) { io->ioerror = 1; hammer_ref(&io->lock); } break; } bp->b_flags &= ~B_ERROR; bundirty(bp); #if 0 hammer_io_set_modlist(io); io->modified = 1; #endif } hammer_stats_disk_write += io->bytes; atomic_add_long(&hammer_count_io_running_write, -io->bytes); atomic_add_long(&hmp->io_running_space, -io->bytes); KKASSERT(hmp->io_running_space >= 0); io->running = 0; /* * Remove from iorun list and wakeup any multi-io waiter(s). */ if (TAILQ_FIRST(&hmp->iorun_list) == io) { ionext = TAILQ_NEXT(io, iorun_entry); if (ionext && ionext->type == HAMMER_IOTYPE_DUMMY) wakeup(ionext); } TAILQ_REMOVE(&hmp->iorun_list, io, iorun_entry); } else { hammer_stats_disk_read += io->bytes; } if (io->waiting) { io->waiting = 0; wakeup(io); } /* * If B_LOCKED is set someone wanted to deallocate the bp at some * point, try to do it now. The operation will fail if there are * refs or if hammer_io_deallocate() is unable to gain the * interlock. */ if (bp->b_flags & B_LOCKED) { atomic_add_int(&hammer_count_io_locked, -1); bp->b_flags &= ~B_LOCKED; hammer_io_deallocate(bp); /* structure may be dead now */ } lwkt_reltoken(&hmp->io_token); } /* * Callback from kernel when it wishes to deallocate a passively * associated structure. This mostly occurs with clean buffers * but it may be possible for a holding structure to be marked dirty * while its buffer is passively associated. The caller owns the bp. * * If we cannot disassociate we set B_LOCKED to prevent the buffer * from getting reused. * * WARNING: Because this can be called directly by getnewbuf we cannot * recurse into the tree. If a bp cannot be immediately disassociated * our only recourse is to set B_LOCKED. * * WARNING: This may be called from an interrupt via hammer_io_complete() * * bioops callback - hold io_token */ static void hammer_io_deallocate(struct buf *bp) { hammer_io_t io = hammer_buf_peek_io(bp); hammer_mount_t hmp; hmp = io->hmp; lwkt_gettoken(&hmp->io_token); KKASSERT((bp->b_flags & B_LOCKED) == 0 && io->running == 0); if (hammer_try_interlock_norefs(&io->lock) == 0) { /* * We cannot safely disassociate a bp from a referenced * or interlocked HAMMER structure. */ bp->b_flags |= B_LOCKED; atomic_add_int(&hammer_count_io_locked, 1); } else if (io->modified) { /* * It is not legal to disassociate a modified buffer. This * case really shouldn't ever occur. */ bp->b_flags |= B_LOCKED; atomic_add_int(&hammer_count_io_locked, 1); hammer_put_interlock(&io->lock, 0); } else { /* * Disassociate the BP. If the io has no refs left we * have to add it to the loose list. The kernel has * locked the buffer and therefore our io must be * in a released state. */ hammer_io_disassociate(io); if (io->type != HAMMER_IOTYPE_VOLUME) { KKASSERT(io->bp == NULL); KKASSERT(io->mod_root == NULL); io->mod_root = &hmp->lose_root; if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) { hpanic("duplicate entry @ %d:%015jx", io->volume->vol_no, io->offset); /* NOT REACHED */ } } hammer_put_interlock(&io->lock, 1); } lwkt_reltoken(&hmp->io_token); } /* * bioops callback - hold io_token */ static int hammer_io_fsync(struct vnode *vp) { /* nothing to do, so io_token not needed */ return(0); } /* * NOTE: will not be called unless we tell the kernel about the * bioops. Unused... we use the mount's VFS_SYNC instead. * * bioops callback - hold io_token */ static int hammer_io_sync(struct mount *mp) { /* nothing to do, so io_token not needed */ return(0); } /* * bioops callback - hold io_token */ static void hammer_io_movedeps(struct buf *bp1, struct buf *bp2) { /* nothing to do, so io_token not needed */ } /* * I/O pre-check for reading and writing. HAMMER only uses this for * B_CACHE buffers so checkread just shouldn't happen, but if it does * allow it. * * Writing is a different case. We don't want the kernel to try to write * out a buffer that HAMMER may be modifying passively or which has a * dependancy. In addition, kernel-demanded writes can only proceed for * certain types of buffers (i.e. UNDO and DATA types). Other dirty * buffer types can only be explicitly written by the flusher. * * checkwrite will only be called for bdwrite()n buffers. If we return * success the kernel is guaranteed to initiate the buffer write. * * bioops callback - hold io_token */ static int hammer_io_checkread(struct buf *bp) { /* nothing to do, so io_token not needed */ return(0); } /* * The kernel is asking us whether it can write out a dirty buffer or not. * * bioops callback - hold io_token */ static int hammer_io_checkwrite(struct buf *bp) { hammer_io_t io = hammer_buf_peek_io(bp); hammer_mount_t hmp = io->hmp; /* * This shouldn't happen under normal operation. */ lwkt_gettoken(&hmp->io_token); if (io->type == HAMMER_IOTYPE_VOLUME || io->type == HAMMER_IOTYPE_META_BUFFER) { if (!panicstr) hpanic("illegal buffer"); if ((bp->b_flags & B_LOCKED) == 0) { bp->b_flags |= B_LOCKED; atomic_add_int(&hammer_count_io_locked, 1); } lwkt_reltoken(&hmp->io_token); return(1); } /* * We have to be able to interlock the IO to safely modify any * of its fields without holding the fs_token. If we can't lock * it then we are racing someone. * * Our ownership of the bp lock prevents the io from being ripped * out from under us. */ if (hammer_try_interlock_norefs(&io->lock) == 0) { bp->b_flags |= B_LOCKED; atomic_add_int(&hammer_count_io_locked, 1); lwkt_reltoken(&hmp->io_token); return(1); } /* * The modified bit must be cleared prior to the initiation of * any IO (returning 0 initiates the IO). Because this is a * normal data buffer hammer_io_clear_modify() runs through a * simple degenerate case. * * Return 0 will cause the kernel to initiate the IO, and we * must normally clear the modified bit before we begin. If * the io has modify_refs we do not clear the modified bit, * otherwise we may miss changes. * * Only data and undo buffers can reach here. These buffers do * not have terminal crc functions but we temporarily reference * the IO anyway, just in case. */ if (io->modify_refs == 0 && io->modified) { hammer_ref(&io->lock); hammer_io_clear_modify(io, 0); hammer_rel(&io->lock); } else if (io->modified) { KKASSERT(io->type == HAMMER_IOTYPE_DATA_BUFFER); } /* * The kernel is going to start the IO, set io->running. */ KKASSERT(io->running == 0); io->running = 1; atomic_add_long(&io->hmp->io_running_space, io->bytes); atomic_add_long(&hammer_count_io_running_write, io->bytes); TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry); hammer_put_interlock(&io->lock, 1); lwkt_reltoken(&hmp->io_token); return(0); } /* * Return non-zero if we wish to delay the kernel's attempt to flush * this buffer to disk. * * bioops callback - hold io_token */ static int hammer_io_countdeps(struct buf *bp, int n) { /* nothing to do, so io_token not needed */ return(0); } static struct bio_ops hammer_bioops = { .io_start = hammer_io_start, .io_complete = hammer_io_complete, .io_deallocate = hammer_io_deallocate, .io_fsync = hammer_io_fsync, .io_sync = hammer_io_sync, .io_movedeps = hammer_io_movedeps, .io_countdeps = hammer_io_countdeps, .io_checkread = hammer_io_checkread, .io_checkwrite = hammer_io_checkwrite, }; /************************************************************************ * DIRECT IO OPS * ************************************************************************ * * These functions operate directly on the buffer cache buffer associated * with a front-end vnode rather then a back-end device vnode. */ /* * Read a buffer associated with a front-end vnode directly from the * disk media. The bio may be issued asynchronously. If leaf is non-NULL * we validate the CRC. * * We must check for the presence of a HAMMER buffer to handle the case * where the reblocker has rewritten the data (which it does via the HAMMER * buffer system, not via the high-level vnode buffer cache), but not yet * committed the buffer to the media. */ int hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio, hammer_btree_leaf_elm_t leaf) { hammer_off_t buf_offset; hammer_off_t zone2_offset; hammer_volume_t volume; struct buf *bp; struct bio *nbio; int vol_no; int error; buf_offset = bio->bio_offset; KKASSERT(hammer_is_zone_large_data(buf_offset)); /* * The buffer cache may have an aliased buffer (the reblocker can * write them). If it does we have to sync any dirty data before * we can build our direct-read. This is a non-critical code path. */ bp = bio->bio_buf; hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize); /* * Resolve to a zone-2 offset. The conversion just requires * munging the top 4 bits but we want to abstract it anyway * so the blockmap code can verify the zone assignment. */ zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error); if (error) goto done; KKASSERT(hammer_is_zone_raw_buffer(zone2_offset)); /* * Resolve volume and raw-offset for 3rd level bio. The * offset will be specific to the volume. */ vol_no = HAMMER_VOL_DECODE(zone2_offset); volume = hammer_get_volume(hmp, vol_no, &error); if (error == 0 && zone2_offset >= volume->maxbuf_off) error = EIO; if (error == 0) { /* * 3rd level bio (the caller has already pushed once) */ nbio = push_bio(bio); nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset); hammer_stats_disk_read += bp->b_bufsize; vn_strategy(volume->devvp, nbio); } hammer_rel_volume(volume, 0); done: if (error) { hdkprintf("failed @ %016jx\n", (intmax_t)zone2_offset); bp->b_error = error; bp->b_flags |= B_ERROR; biodone(bio); } return(error); } /* * This works similarly to hammer_io_direct_read() except instead of * directly reading from the device into the bio we instead indirectly * read through the device's buffer cache and then copy the data into * the bio. * * If leaf is non-NULL and validation is enabled, the CRC will be checked. * * This routine also executes asynchronously. It allows hammer strategy * calls to operate asynchronously when in double_buffer mode (in addition * to operating asynchronously when in normal mode). */ int hammer_io_indirect_read(hammer_mount_t hmp, struct bio *bio, hammer_btree_leaf_elm_t leaf) { hammer_off_t buf_offset; hammer_off_t zone2_offset; hammer_volume_t volume; struct buf *bp; int vol_no; int error; buf_offset = bio->bio_offset; KKASSERT(hammer_is_zone_large_data(buf_offset)); /* * The buffer cache may have an aliased buffer (the reblocker can * write them). If it does we have to sync any dirty data before * we can build our direct-read. This is a non-critical code path. */ bp = bio->bio_buf; hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize); /* * Resolve to a zone-2 offset. The conversion just requires * munging the top 4 bits but we want to abstract it anyway * so the blockmap code can verify the zone assignment. */ zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error); if (error) goto done; KKASSERT(hammer_is_zone_raw_buffer(zone2_offset)); /* * Resolve volume and raw-offset for 3rd level bio. The * offset will be specific to the volume. */ vol_no = HAMMER_VOL_DECODE(zone2_offset); volume = hammer_get_volume(hmp, vol_no, &error); if (error == 0 && zone2_offset >= volume->maxbuf_off) error = EIO; if (error == 0) { /* * Convert to the raw volume->devvp offset and acquire * the buf, issuing async I/O if necessary. */ hammer_off_t limit; int hce; buf_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset); if (leaf && hammer_verify_data) { bio->bio_caller_info1.uvalue32 = leaf->data_crc; bio->bio_caller_info2.index = 1; } else { bio->bio_caller_info2.index = 0; } bio->bio_caller_info3.ptr = hmp; hce = hammer_cluster_enable; if (hce > 0) { limit = HAMMER_BIGBLOCK_DOALIGN(zone2_offset); limit -= zone2_offset; cluster_readcb(volume->devvp, limit, buf_offset, bp->b_bufsize, B_NOTMETA, HAMMER_CLUSTER_SIZE, HAMMER_CLUSTER_SIZE * hce, hammer_indirect_callback, bio); } else { breadcb(volume->devvp, buf_offset, bp->b_bufsize, B_NOTMETA, hammer_indirect_callback, bio); } } hammer_rel_volume(volume, 0); done: if (error) { hdkprintf("failed @ %016jx\n", (intmax_t)zone2_offset); bp->b_error = error; bp->b_flags |= B_ERROR; biodone(bio); } return(error); } /* * Indirect callback on completion. bio/bp specify the device-backed * buffer. bio->bio_caller_info1.ptr holds obio. * * obio/obp is the original regular file buffer. obio->bio_caller_info* * contains the crc specification. * * We are responsible for calling bpdone() and bqrelse() on bio/bp, and * for calling biodone() on obio. */ static void hammer_indirect_callback(struct bio *bio) { struct buf *bp = bio->bio_buf; struct buf *obp; struct bio *obio; hammer_mount_t hmp; /* * If BIO_DONE is already set the device buffer was already * fully valid (B_CACHE). If it is not set then I/O was issued * and we have to run I/O completion as the last bio. * * Nobody is waiting for our device I/O to complete, we are * responsible for bqrelse()ing it which means we also have to do * the equivalent of biowait() and clear BIO_DONE (which breadcb() * may have set). * * Any preexisting device buffer should match the requested size, * but due to big-block recycling and other factors there is some * fragility there, so we assert that the device buffer covers * the request. */ if ((bio->bio_flags & BIO_DONE) == 0) bpdone(bp, 0); bio->bio_flags &= ~(BIO_DONE | BIO_SYNC); obio = bio->bio_caller_info1.ptr; obp = obio->bio_buf; hmp = obio->bio_caller_info3.ptr; if (bp->b_flags & B_ERROR) { /* * Error from block device */ obp->b_flags |= B_ERROR; obp->b_error = bp->b_error; } else if (obio->bio_caller_info2.index && obio->bio_caller_info1.uvalue32 != hammer_datacrc(hmp->version, bp->b_data, obp->b_bufsize) && obio->bio_caller_info1.uvalue32 != hammer_datacrc(HAMMER_VOL_VERSION_SIX, bp->b_data, obp->b_bufsize)) { /* * CRC error. First check against current hammer version, * then back-off and check against version 6 (the original * crc). */ obp->b_flags |= B_ERROR; obp->b_error = EIO; } else { /* * Everything is ok */ KKASSERT(bp->b_bufsize >= obp->b_bufsize); bcopy(bp->b_data, obp->b_data, obp->b_bufsize); obp->b_resid = 0; obp->b_flags |= B_AGE; } biodone(obio); bqrelse(bp); } /* * Write a buffer associated with a front-end vnode directly to the * disk media. The bio may be issued asynchronously. * * The BIO is associated with the specified record and RECG_DIRECT_IO * is set. The recorded is added to its object. */ int hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio, hammer_record_t record) { hammer_btree_leaf_elm_t leaf = &record->leaf; hammer_off_t buf_offset; hammer_off_t zone2_offset; hammer_volume_t volume; hammer_buffer_t buffer; struct buf *bp; struct bio *nbio; char *ptr; int vol_no; int error; buf_offset = leaf->data_offset; KKASSERT(hammer_is_zone_record(buf_offset)); KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE); /* * Issue or execute the I/O. The new memory record must replace * the old one before the I/O completes, otherwise a reaquisition of * the buffer will load the old media data instead of the new. */ if ((buf_offset & HAMMER_BUFMASK) == 0 && leaf->data_len >= HAMMER_BUFSIZE) { /* * We are using the vnode's bio to write directly to the * media, any hammer_buffer at the same zone-X offset will * now have stale data. */ zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error); vol_no = HAMMER_VOL_DECODE(zone2_offset); volume = hammer_get_volume(hmp, vol_no, &error); if (error == 0 && zone2_offset >= volume->maxbuf_off) error = EIO; if (error == 0) { bp = bio->bio_buf; KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0); /* * Second level bio - cached zone2 offset. * * (We can put our bio_done function in either the * 2nd or 3rd level). */ nbio = push_bio(bio); nbio->bio_offset = zone2_offset; nbio->bio_done = hammer_io_direct_write_complete; nbio->bio_caller_info1.ptr = record; record->zone2_offset = zone2_offset; record->gflags |= HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_INVAL; /* * Third level bio - raw offset specific to the * correct volume. */ nbio = push_bio(nbio); nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset); hammer_stats_disk_write += bp->b_bufsize; hammer_ip_replace_bulk(hmp, record); vn_strategy(volume->devvp, nbio); hammer_io_flush_mark(volume); } hammer_rel_volume(volume, 0); } else { /* * Must fit in a standard HAMMER buffer. In this case all * consumers use the HAMMER buffer system and RECG_DIRECT_IO * does not need to be set-up. */ KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0); buffer = NULL; ptr = hammer_bread(hmp, buf_offset, &error, &buffer); if (error == 0) { bp = bio->bio_buf; bp->b_flags |= B_AGE; hammer_io_modify(&buffer->io, 1); bcopy(bp->b_data, ptr, leaf->data_len); hammer_io_modify_done(&buffer->io); hammer_rel_buffer(buffer, 0); bp->b_resid = 0; hammer_ip_replace_bulk(hmp, record); biodone(bio); } } if (error) { /* * Major suckage occured. Also note: The record was * never added to the tree so we do not have to worry * about the backend. */ hdkprintf("failed @ %016jx\n", (intmax_t)leaf->data_offset); bp = bio->bio_buf; bp->b_resid = 0; bp->b_error = EIO; bp->b_flags |= B_ERROR; biodone(bio); record->flags |= HAMMER_RECF_DELETED_FE; hammer_rel_mem_record(record); } return(error); } /* * On completion of the BIO this callback must disconnect * it from the hammer_record and chain to the previous bio. * * An I/O error forces the mount to read-only. Data buffers * are not B_LOCKED like meta-data buffers are, so we have to * throw the buffer away to prevent the kernel from retrying. * * NOTE: MPSAFE callback, only modify fields we have explicit * access to (the bp and the record->gflags). */ static void hammer_io_direct_write_complete(struct bio *nbio) { struct bio *obio; struct buf *bp; hammer_record_t record; hammer_mount_t hmp; record = nbio->bio_caller_info1.ptr; KKASSERT(record != NULL); hmp = record->ip->hmp; lwkt_gettoken(&hmp->io_token); bp = nbio->bio_buf; obio = pop_bio(nbio); if (bp->b_flags & B_ERROR) { lwkt_gettoken(&hmp->fs_token); hammer_critical_error(hmp, record->ip, bp->b_error, "while writing bulk data"); lwkt_reltoken(&hmp->fs_token); bp->b_flags |= B_INVAL; } KKASSERT(record->gflags & HAMMER_RECG_DIRECT_IO); if (record->gflags & HAMMER_RECG_DIRECT_WAIT) { record->gflags &= ~(HAMMER_RECG_DIRECT_IO | HAMMER_RECG_DIRECT_WAIT); /* record can disappear once DIRECT_IO flag is cleared */ wakeup(&record->flags); } else { record->gflags &= ~HAMMER_RECG_DIRECT_IO; /* record can disappear once DIRECT_IO flag is cleared */ } lwkt_reltoken(&hmp->io_token); biodone(obio); } /* * This is called before a record is either committed to the B-Tree * or destroyed, to resolve any associated direct-IO. * * (1) We must wait for any direct-IO related to the record to complete. * * (2) We must remove any buffer cache aliases for data accessed via * leaf->data_offset or zone2_offset so non-direct-IO consumers * (the mirroring and reblocking code) do not see stale data. */ void hammer_io_direct_wait(hammer_record_t record) { hammer_mount_t hmp = record->ip->hmp; /* * Wait for I/O to complete */ if (record->gflags & HAMMER_RECG_DIRECT_IO) { lwkt_gettoken(&hmp->io_token); while (record->gflags & HAMMER_RECG_DIRECT_IO) { record->gflags |= HAMMER_RECG_DIRECT_WAIT; tsleep(&record->flags, 0, "hmdiow", 0); } lwkt_reltoken(&hmp->io_token); } /* * Invalidate any related buffer cache aliases associated with the * backing device. This is needed because the buffer cache buffer * for file data is associated with the file vnode, not the backing * device vnode. * * XXX I do not think this case can occur any more now that * reservations ensure that all such buffers are removed before * an area can be reused. */ if (record->gflags & HAMMER_RECG_DIRECT_INVAL) { KKASSERT(record->leaf.data_offset); hammer_del_buffers(hmp, record->leaf.data_offset, record->zone2_offset, record->leaf.data_len, 1); record->gflags &= ~HAMMER_RECG_DIRECT_INVAL; } } /* * This is called to remove the second-level cached zone-2 offset from * frontend buffer cache buffers, now stale due to a data relocation. * These offsets are generated by cluster_read() via VOP_BMAP, or directly * by hammer_vop_strategy_read(). * * This is rather nasty because here we have something like the reblocker * scanning the raw B-Tree with no held references on anything, really, * other then a shared lock on the B-Tree node, and we have to access the * frontend's buffer cache to check for and clean out the association. * Specifically, if the reblocker is moving data on the disk, these cached * offsets will become invalid. * * Only data record types associated with the large-data zone are subject * to direct-io and need to be checked. * */ void hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf) { struct hammer_inode_info iinfo; int zone; if (leaf->base.rec_type != HAMMER_RECTYPE_DATA) return; zone = HAMMER_ZONE_DECODE(leaf->data_offset); if (zone != HAMMER_ZONE_LARGE_DATA_INDEX) return; iinfo.obj_id = leaf->base.obj_id; iinfo.obj_asof = 0; /* unused */ iinfo.obj_localization = leaf->base.localization & HAMMER_LOCALIZE_PSEUDOFS_MASK; iinfo.u.leaf = leaf; hammer_scan_inode_snapshots(hmp, &iinfo, hammer_io_direct_uncache_callback, leaf); } static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data) { hammer_inode_info_t iinfo = data; hammer_off_t file_offset; struct vnode *vp; struct buf *bp; int blksize; if (ip->vp == NULL) return(0); file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len; blksize = iinfo->u.leaf->data_len; KKASSERT((blksize & HAMMER_BUFMASK) == 0); /* * Warning: FINDBLK_TEST return stable storage but not stable * contents. It happens to be ok in this case. */ hammer_ref(&ip->lock); if (hammer_get_vnode(ip, &vp) == 0) { if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL && bp->b_bio2.bio_offset != NOOFFSET) { bp = getblk(ip->vp, file_offset, blksize, 0, 0); bp->b_bio2.bio_offset = NOOFFSET; brelse(bp); } vput(vp); } hammer_rel_inode(ip, 0); return(0); } /* * This function is called when writes may have occured on the volume, * indicating that the device may be holding cached writes. */ static __inline void hammer_io_flush_mark(hammer_volume_t volume) { atomic_set_int(&volume->vol_flags, HAMMER_VOLF_NEEDFLUSH); } /* * This function ensures that the device has flushed any cached writes out. */ void hammer_io_flush_sync(hammer_mount_t hmp) { hammer_volume_t volume; struct buf *bp_base = NULL; struct buf *bp; RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) { if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) { atomic_clear_int(&volume->vol_flags, HAMMER_VOLF_NEEDFLUSH); bp = getpbuf(NULL); bp->b_bio1.bio_offset = 0; bp->b_bufsize = 0; bp->b_bcount = 0; bp->b_cmd = BUF_CMD_FLUSH; bp->b_bio1.bio_caller_info1.cluster_head = bp_base; bp->b_bio1.bio_done = biodone_sync; bp->b_bio1.bio_flags |= BIO_SYNC; bp_base = bp; vn_strategy(volume->devvp, &bp->b_bio1); } } while ((bp = bp_base) != NULL) { bp_base = bp->b_bio1.bio_caller_info1.cluster_head; biowait(&bp->b_bio1, "hmrFLS"); relpbuf(bp, NULL); } } /* * Limit the amount of backlog which we allow to build up */ void hammer_io_limit_backlog(hammer_mount_t hmp) { waitrunningbufspace(); } |