Loading...
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 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 | /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* @(#)hfs_readwrite.c 1.0 * * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved * (c) 1998 Apple Computer, Inc. All Rights Reserved * * * hfs_readwrite.c -- vnode operations to deal with reading and writing files. * * MODIFICATION HISTORY: * 9-Nov-1999 Scott Roberts hfs_allocate now returns sizes based on allocation block boundaries (#2398794) * 3-Feb-1999 Pat Dirks Merged in Joe's change to hfs_truncate to skip vinvalbuf if LEOF isn't changing (#2302796) * Removed superfluous (and potentially dangerous) second call to vinvalbuf() in hfs_truncate. * 2-Dec-1998 Pat Dirks Added support for read/write bootstrap ioctls. * 10-Nov-1998 Pat Dirks Changed read/write/truncate logic to optimize block sizes for first extents of a file. * Changed hfs_strategy to correct I/O sizes from cluser code I/O requests in light of * different block sizing. Changed bexpand to handle RELEASE_BUFFER flag. * 22-Sep-1998 Don Brady Changed truncate zero-fill to use bwrite after several bawrites have been queued. * 11-Sep-1998 Pat Dirks Fixed buffering logic to not rely on B_CACHE, which is set for empty buffers that * have been pre-read by cluster_read (use b_validend > 0 instead). * 27-Aug-1998 Pat Dirks Changed hfs_truncate to use cluster_write in place of bawrite where possible. * 25-Aug-1998 Pat Dirks Changed hfs_write to do small device-block aligned writes into buffers without doing * read-ahead of the buffer. Added bexpand to deal with incomplete [dirty] buffers. * Fixed can_cluster macro to use MAXPHYSIO instead of MAXBSIZE. * 19-Aug-1998 Don Brady Remove optimization in hfs_truncate that prevented extra physical blocks from * being truncated (radar #2265750). Also set fcb->fcbEOF before calling vinvalbuf. * 7-Jul-1998 Pat Dirks Added code to honor IO_NOZEROFILL in hfs_truncate. * 16-Jul-1998 Don Brady In hfs_bmap use MAXPHYSIO instead of MAXBSIZE when calling MapFileBlockC (radar #2263753). * 16-Jul-1998 Don Brady Fix error handling in hfs_allocate (radar #2252265). * 04-Jul-1998 chw Synchronized options in hfs_allocate with flags in call to ExtendFileC * 25-Jun-1998 Don Brady Add missing blockNo incrementing to zero fill loop in hfs_truncate. * 22-Jun-1998 Don Brady Add bp = NULL assignment after brelse in hfs_read. * 4-Jun-1998 Pat Dirks Split off from hfs_vnodeops.c */ #include <sys/param.h> #include <sys/systm.h> #include <sys/resourcevar.h> #include <sys/kernel.h> #include <sys/fcntl.h> #include <sys/stat.h> #include <sys/buf.h> #include <sys/proc.h> //#include <mach/machine/vm_types.h> #include <sys/vnode.h> #include <sys/uio.h> #include <miscfs/specfs/specdev.h> #include <sys/ubc.h> #include <vm/vm_pageout.h> #include <sys/kdebug.h> #include "hfs.h" #include "hfs_dbg.h" #include "hfs_endian.h" #include "hfscommon/headers/FileMgrInternal.h" #include "hfscommon/headers/BTreesInternal.h" #define can_cluster(size) ((((size & (4096-1))) == 0) && (size <= (MAXPHYSIO/2))) enum { MAXHFSFILESIZE = 0x7FFFFFFF /* this needs to go in the mount structure */ }; extern u_int32_t GetLogicalBlockSize(struct vnode *vp); #if DBG_VOP_TEST_LOCKS extern void DbgVopTest(int maxSlots, int retval, VopDbgStoreRec *VopDbgStore, char *funcname); #endif #if HFS_DIAGNOSTIC void debug_check_blocksizes(struct vnode *vp); #endif /***************************************************************************** * * Operations on vnodes * *****************************************************************************/ /* #% read vp L L L # vop_read { IN struct vnode *vp; INOUT struct uio *uio; IN int ioflag; IN struct ucred *cred; */ int hfs_read(ap) struct vop_read_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; struct ucred *a_cred; } */ *ap; { register struct vnode *vp; struct hfsnode *hp; register struct uio *uio; struct buf *bp; daddr_t logBlockNo; u_long fragSize, moveSize, startOffset, ioxfersize; int devBlockSize = 0; off_t bytesRemaining; int retval; u_short mode; FCB *fcb; DBG_FUNC_NAME("hfs_read"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); vp = ap->a_vp; hp = VTOH(vp); fcb = HTOFCB(hp); mode = hp->h_meta->h_mode; uio = ap->a_uio; #if HFS_DIAGNOSTIC if (uio->uio_rw != UIO_READ) panic("%s: mode", funcname); #endif /* Can only read files */ if (ap->a_vp->v_type != VREG && ap->a_vp->v_type != VLNK) { DBG_VOP_LOCKS_TEST(EISDIR); return (EISDIR); } DBG_RW(("\tfile size Ox%X\n", (u_int)fcb->fcbEOF)); DBG_RW(("\tstarting at offset Ox%X of file, length Ox%X\n", (u_int)uio->uio_offset, (u_int)uio->uio_resid)); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif /* * If they didn't ask for any data, then we are done. */ if (uio->uio_resid == 0) { DBG_VOP_LOCKS_TEST(E_NONE); return (E_NONE); } /* cant read from a negative offset */ if (uio->uio_offset < 0) { DBG_VOP_LOCKS_TEST(EINVAL); return (EINVAL); } if (uio->uio_offset > fcb->fcbEOF) { if ( (!ISHFSPLUS(VTOVCB(vp))) && (uio->uio_offset > (off_t)MAXHFSFILESIZE)) retval = EFBIG; else retval = E_NONE; DBG_VOP_LOCKS_TEST(retval); return (retval); } VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 12)) | DBG_FUNC_START, (int)uio->uio_offset, uio->uio_resid, (int)fcb->fcbEOF, (int)fcb->fcbPLen, 0); if (UBCISVALID(vp)) retval = cluster_read(vp, uio, (off_t)fcb->fcbEOF, devBlockSize, 0); else { for (retval = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { if ((bytesRemaining = (fcb->fcbEOF - uio->uio_offset)) <= 0) break; logBlockNo = (daddr_t)(uio->uio_offset / PAGE_SIZE_64); startOffset = (u_long) (uio->uio_offset & PAGE_MASK_64); fragSize = PAGE_SIZE; if (((logBlockNo * PAGE_SIZE) + fragSize) < fcb->fcbEOF) ioxfersize = fragSize; else { ioxfersize = fcb->fcbEOF - (logBlockNo * PAGE_SIZE); ioxfersize = (ioxfersize + (devBlockSize - 1)) & ~(devBlockSize - 1); } DBG_RW(("\tat logBlockNo Ox%X, with Ox%lX left to read\n", logBlockNo, (UInt32)uio->uio_resid)); moveSize = ioxfersize; DBG_RW(("\tmoveSize = Ox%lX; ioxfersize = Ox%lX; startOffset = Ox%lX.\n", moveSize, ioxfersize, startOffset)); DBG_ASSERT(moveSize >= startOffset); moveSize -= startOffset; if (bytesRemaining < moveSize) moveSize = bytesRemaining; if (uio->uio_resid < moveSize) { moveSize = uio->uio_resid; DBG_RW(("\treducing moveSize to Ox%lX (uio->uio_resid).\n", moveSize)); }; if (moveSize == 0) { break; }; DBG_RW(("\tat logBlockNo Ox%X, extent of Ox%lX, xfer of Ox%lX; moveSize = Ox%lX\n", logBlockNo, fragSize, ioxfersize, moveSize)); if (( uio->uio_offset + fragSize) >= fcb->fcbEOF) { retval = bread(vp, logBlockNo, ioxfersize, NOCRED, &bp); } else if (logBlockNo - 1 == vp->v_lastr && !(vp->v_flag & VRAOFF)) { daddr_t nextLogBlockNo = logBlockNo + 1; int nextsize; if (((nextLogBlockNo * PAGE_SIZE) + (daddr_t)fragSize) < fcb->fcbEOF) nextsize = fragSize; else { nextsize = fcb->fcbEOF - (nextLogBlockNo * PAGE_SIZE); nextsize = (nextsize + (devBlockSize - 1)) & ~(devBlockSize - 1); } retval = breadn(vp, logBlockNo, ioxfersize, &nextLogBlockNo, &nextsize, 1, NOCRED, &bp); } else { retval = bread(vp, logBlockNo, ioxfersize, NOCRED, &bp); }; if (retval != E_NONE) { if (bp) { brelse(bp); bp = NULL; } break; }; vp->v_lastr = logBlockNo; /* * We should only get non-zero b_resid when an I/O retval * has occurred, which should cause us to break above. * However, if the short read did not cause an retval, * then we want to ensure that we do not uiomove bad * or uninitialized data. */ ioxfersize -= bp->b_resid; if (ioxfersize < moveSize) { /* XXX PPD This should take the offset into account, too! */ if (ioxfersize == 0) break; moveSize = ioxfersize; } if ((startOffset + moveSize) > bp->b_bcount) panic("hfs_read: bad startOffset or moveSize\n"); DBG_RW(("\tcopying Ox%lX bytes from %lX; resid = Ox%lX...\n", moveSize, (char *)bp->b_data + startOffset, bp->b_resid)); if ((retval = uiomove((caddr_t)bp->b_data + startOffset, (int)moveSize, uio))) break; if (S_ISREG(mode) && (((startOffset + moveSize) == fragSize) || (uio->uio_offset == fcb->fcbEOF))) { bp->b_flags |= B_AGE; }; DBG_ASSERT(bp->b_bcount == bp->b_validend); brelse(bp); /* Start of loop resets bp to NULL before reaching outside this block... */ } if (bp != NULL) { DBG_ASSERT(bp->b_bcount == bp->b_validend); brelse(bp); }; } if (HTOVCB(hp)->vcbSigWord == kHFSPlusSigWord) hp->h_nodeflags |= IN_ACCESS; DBG_VOP_LOCKS_TEST(retval); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 12)) | DBG_FUNC_END, (int)uio->uio_offset, uio->uio_resid, (int)fcb->fcbEOF, (int)fcb->fcbPLen, 0); return (retval); } /* * Write data to a file or directory. #% write vp L L L # vop_write { IN struct vnode *vp; INOUT struct uio *uio; IN int ioflag; IN struct ucred *cred; */ int hfs_write(ap) struct vop_write_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; struct ucred *a_cred; } */ *ap; { struct hfsnode *hp = VTOH(ap->a_vp); struct uio *uio = ap->a_uio; struct vnode *vp = ap->a_vp ; struct vnode *dev; struct buf *bp; struct proc *p, *cp; struct timeval tv; FCB *fcb = HTOFCB(hp); ExtendedVCB *vcb = HTOVCB(hp); int devBlockSize = 0; daddr_t logBlockNo; long fragSize; off_t origFileSize, currOffset, writelimit, bytesToAdd; off_t actualBytesAdded; u_long blkoffset, resid, xfersize, clearSize; int flags, ioflag; int retval; DBG_FUNC_NAME("hfs_write"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_RW(("\thfsnode 0x%x (%s)\n", (u_int)hp, H_NAME(hp))); DBG_RW(("\tstarting at offset Ox%lX of file, length Ox%lX\n", (UInt32)uio->uio_offset, (UInt32)uio->uio_resid)); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); dev = hp->h_meta->h_devvp; #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif if (uio->uio_offset < 0) { DBG_VOP_LOCKS_TEST(EINVAL); return (EINVAL); } if (uio->uio_resid == 0) { DBG_VOP_LOCKS_TEST(E_NONE); return (E_NONE); } if (ap->a_vp->v_type != VREG && ap->a_vp->v_type != VLNK) { /* Can only write files */ DBG_VOP_LOCKS_TEST(EISDIR); return (EISDIR); }; #if HFS_DIAGNOSTIC if (uio->uio_rw != UIO_WRITE) panic("%s: mode", funcname); #endif ioflag = ap->a_ioflag; uio = ap->a_uio; vp = ap->a_vp; if (ioflag & IO_APPEND) uio->uio_offset = fcb->fcbEOF; if ((hp->h_meta->h_pflags & APPEND) && uio->uio_offset != fcb->fcbEOF) return (EPERM); writelimit = uio->uio_offset + uio->uio_resid; /* * Maybe this should be above the vnode op call, but so long as * file servers have no limits, I don't think it matters. */ p = uio->uio_procp; if (vp->v_type == VREG && p && writelimit > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { psignal(p, SIGXFSZ); return (EFBIG); }; VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); resid = uio->uio_resid; origFileSize = fcb->fcbEOF; flags = ioflag & IO_SYNC ? B_SYNC : 0; DBG_RW(("\tLEOF is 0x%lX, PEOF is 0x%lX.\n", fcb->fcbEOF, fcb->fcbPLen)); /* NOTE: In the following loop there are two positions tracked: currOffset is the current I/O starting offset. currOffset is never >LEOF; the LEOF is nudged along with currOffset as data is zeroed or written. uio->uio_offset is the start of the current I/O operation. It may be arbitrarily beyond currOffset. The following is true at all times: currOffset <= LEOF <= uio->uio_offset <= writelimit */ currOffset = MIN(uio->uio_offset, fcb->fcbEOF); DBG_RW(("\tstarting I/O loop at 0x%lX.\n", (u_long)currOffset)); cp = current_proc(); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 0)) | DBG_FUNC_START, (int)uio->uio_offset, uio->uio_resid, (int)fcb->fcbEOF, (int)fcb->fcbPLen, 0); retval = 0; /* Now test if we need to extend the file */ /* Doing so will adjust the fcbPLen for us */ while (writelimit > (off_t)fcb->fcbPLen) { bytesToAdd = writelimit - fcb->fcbPLen; DBG_RW(("\textending file by 0x%lX bytes; 0x%lX blocks free", (unsigned long)bytesToAdd, (unsigned long)vcb->freeBlocks)); /* lock extents b-tree (also protects volume bitmap) */ retval = hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_EXCLUSIVE, cp); if (retval != E_NONE) break; retval = MacToVFSError( ExtendFileC (vcb, fcb, bytesToAdd, 0, kEFContigBit, &actualBytesAdded)); (void) hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_RELEASE, cp); DBG_VOP_CONT(("\tactual bytes added = 0x%lX bytes, retval = %d...\n", actualBytesAdded, retval)); if ((actualBytesAdded == 0) && (retval == E_NONE)) retval = ENOSPC; if (retval != E_NONE) break; KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 0)) | DBG_FUNC_NONE, (int)uio->uio_offset, uio->uio_resid, (int)fcb->fcbEOF, (int)fcb->fcbPLen, 0); }; if (UBCISVALID(vp) && retval == E_NONE) { off_t filesize; off_t zero_off; off_t tail_off; off_t inval_start; off_t inval_end; off_t io_start, io_end; int lflag; struct rl_entry *invalid_range; if (writelimit > fcb->fcbEOF) filesize = writelimit; else filesize = fcb->fcbEOF; lflag = (ioflag & IO_SYNC); if (uio->uio_offset <= fcb->fcbEOF) { zero_off = uio->uio_offset & ~PAGE_MASK_64; /* Check to see whether the area between the zero_offset and the start of the transfer to see whether is invalid and should be zero-filled as part of the transfer: */ if (rl_scan(&hp->h_invalidranges, zero_off, uio->uio_offset - 1, &invalid_range) != RL_NOOVERLAP) { lflag |= IO_HEADZEROFILL; }; } else { off_t eof_page_base = fcb->fcbEOF & ~PAGE_MASK_64; /* The bytes between fcb->fcbEOF and uio->uio_offset must never be read without being zeroed. The current last block is filled with zeroes if it holds valid data but in all cases merely do a little bookkeeping to track the area from the end of the current last page to the start of the area actually written. For the same reason only the bytes up to the start of the page where this write will start is invalidated; any remainder before uio->uio_offset is explicitly zeroed as part of the cluster_write. Note that inval_start, the start of the page after the current EOF, may be past the start of the write, in which case the zeroing will be handled by the cluser_write of the actual data. */ inval_start = (fcb->fcbEOF + (PAGE_SIZE_64 - 1)) & ~PAGE_MASK_64; inval_end = uio->uio_offset & ~PAGE_MASK_64; zero_off = fcb->fcbEOF; if ((fcb->fcbEOF & PAGE_MASK_64) && (rl_scan(&hp->h_invalidranges, eof_page_base, fcb->fcbEOF - 1, &invalid_range) != RL_NOOVERLAP)) { /* The page containing the EOF is not valid, so the entire page must be made inaccessible now. If the write starts on a page beyond the page containing the eof (inval_end > eof_page_base), add the whole page to the range to be invalidated. Otherwise (i.e. if the write starts on the same page), zero-fill the entire page explicitly now: */ if (inval_end > eof_page_base) { inval_start = eof_page_base; } else { zero_off = eof_page_base; }; }; if (inval_start < inval_end) { /* There's some range of data that's going to be marked invalid */ if (zero_off < inval_start) { /* The pages between inval_start and inval_end are going to be invalidated, and the actual write will start on a page past inval_end. Now's the last chance to zero-fill the page containing the EOF: */ retval = cluster_write(vp, (struct uio *) 0, fcb->fcbEOF, inval_start, zero_off, (off_t)0, devBlockSize, lflag | IO_HEADZEROFILL); if (retval) goto ioerr_exit; }; /* Mark the remaining area of the newly allocated space as invalid: */ rl_add(inval_start, inval_end - 1 , &hp->h_invalidranges); zero_off = fcb->fcbEOF = inval_end; }; if (uio->uio_offset > zero_off) lflag |= IO_HEADZEROFILL; }; /* Check to see whether the area between the end of the write and the end of the page it falls in is invalid and should be zero-filled as part of the transfer: */ tail_off = (writelimit + (PAGE_SIZE_64 - 1)) & ~PAGE_MASK_64; if (tail_off > filesize) tail_off = filesize; if (tail_off > writelimit) { if (rl_scan(&hp->h_invalidranges, writelimit, tail_off - 1, &invalid_range) != RL_NOOVERLAP) { lflag |= IO_TAILZEROFILL; }; }; /* * if the write starts beyond the current EOF (possibly advanced in the * zeroing of the last block, above), then we'll zero fill from the current EOF * to where the write begins: * * NOTE: If (and ONLY if) the portion of the file about to be written is * before the current EOF it might be marked as invalid now and must be * made readable (removed from the invalid ranges) before cluster_write * tries to write it: */ io_start = (lflag & IO_HEADZEROFILL) ? zero_off : uio->uio_offset; io_end = (lflag & IO_TAILZEROFILL) ? tail_off : writelimit; if (io_start < fcb->fcbEOF) { rl_remove(io_start, io_end - 1, &hp->h_invalidranges); }; retval = cluster_write(vp, uio, fcb->fcbEOF, filesize, zero_off, tail_off, devBlockSize, lflag); if (uio->uio_offset > fcb->fcbEOF) { fcb->fcbEOF = uio->uio_offset; ubc_setsize(vp, (off_t)fcb->fcbEOF); /* XXX check errors */ } if (resid > uio->uio_resid) hp->h_nodeflags |= IN_CHANGE | IN_UPDATE; } else { while (retval == E_NONE && uio->uio_resid > 0) { logBlockNo = currOffset / PAGE_SIZE; blkoffset = currOffset & PAGE_MASK; if (((off_t)(fcb->fcbPLen) - currOffset) < PAGE_SIZE_64) fragSize = (off_t)(fcb->fcbPLen) - ((off_t)logBlockNo * PAGE_SIZE_64); else fragSize = PAGE_SIZE; xfersize = fragSize - blkoffset; DBG_RW(("\tcurrOffset = Ox%lX, logBlockNo = Ox%X, blkoffset = Ox%lX, xfersize = Ox%lX, fragSize = Ox%lX.\n", (unsigned long)currOffset, logBlockNo, blkoffset, xfersize, fragSize)); /* Make any adjustments for boundary conditions */ if (currOffset + (off_t)xfersize > writelimit) { xfersize = writelimit - currOffset; DBG_RW(("\ttrimming xfersize to 0x%lX to match writelimit (uio_resid)...\n", xfersize)); }; /* * There is no need to read into bp if: * We start on a block boundary and will overwrite the whole block * * OR */ if ((blkoffset == 0) && (xfersize >= fragSize)) { DBG_RW(("\tRequesting %ld-byte block Ox%lX w/o read...\n", fragSize, (long)logBlockNo)); bp = getblk(vp, logBlockNo, fragSize, 0, 0, BLK_READ); retval = 0; if (bp->b_blkno == -1) { brelse(bp); retval = EIO; /* XXX */ break; } } else { if (currOffset == fcb->fcbEOF && blkoffset == 0) { bp = getblk(vp, logBlockNo, fragSize, 0, 0, BLK_READ); retval = 0; if (bp->b_blkno == -1) { brelse(bp); retval = EIO; /* XXX */ break; } } else { /* * This I/O transfer is not sufficiently aligned, so read the affected block into a buffer: */ DBG_VOP(("\tRequesting block Ox%X, size = 0x%08lX...\n", logBlockNo, fragSize)); retval = bread(vp, logBlockNo, fragSize, ap->a_cred, &bp); if (retval != E_NONE) { if (bp) brelse(bp); break; } } } /* See if we are starting to write within file boundaries: If not, then we need to present a "hole" for the area between the current EOF and the start of the current I/O operation: Note that currOffset is only less than uio_offset if uio_offset > LEOF... */ if (uio->uio_offset > currOffset) { clearSize = MIN(uio->uio_offset - currOffset, xfersize); DBG_RW(("\tzeroing Ox%lX bytes Ox%lX bytes into block Ox%X...\n", clearSize, blkoffset, logBlockNo)); bzero(bp->b_data + blkoffset, clearSize); currOffset += clearSize; blkoffset += clearSize; xfersize -= clearSize; }; if (xfersize > 0) { DBG_RW(("\tCopying Ox%lX bytes Ox%lX bytes into block Ox%X... ioflag == 0x%X\n", xfersize, blkoffset, logBlockNo, ioflag)); retval = uiomove((caddr_t)bp->b_data + blkoffset, (int)xfersize, uio); currOffset += xfersize; }; DBG_ASSERT((bp->b_bcount % devBlockSize) == 0); if (ioflag & IO_SYNC) { (void)VOP_BWRITE(bp); //DBG_RW(("\tissuing bwrite\n")); } else if ((xfersize + blkoffset) == fragSize) { //DBG_RW(("\tissuing bawrite\n")); bp->b_flags |= B_AGE; bawrite(bp); } else { //DBG_RW(("\tissuing bdwrite\n")); bdwrite(bp); }; /* Update the EOF if we just extended the file (the PEOF has already been moved out and the block mapping table has been updated): */ if (currOffset > fcb->fcbEOF) { DBG_VOP(("\textending EOF to 0x%lX...\n", (UInt32)fcb->fcbEOF)); fcb->fcbEOF = currOffset; if (UBCISVALID(vp)) ubc_setsize(vp, (off_t)fcb->fcbEOF); /* XXX check errors */ }; if (retval || (resid == 0)) break; hp->h_nodeflags |= IN_CHANGE | IN_UPDATE; }; }; ioerr_exit: /* * If we successfully wrote any data, and we are not the superuser * we clear the setuid and setgid bits as a precaution against * tampering. */ if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0) hp->h_meta->h_mode &= ~(ISUID | ISGID); if (retval) { if (ioflag & IO_UNIT) { (void)VOP_TRUNCATE(vp, origFileSize, ioflag & IO_SYNC, ap->a_cred, uio->uio_procp); uio->uio_offset -= resid - uio->uio_resid; uio->uio_resid = resid; } } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) { tv = time; retval = VOP_UPDATE(vp, &tv, &tv, 1); } #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 0)) | DBG_FUNC_END, (int)uio->uio_offset, uio->uio_resid, (int)fcb->fcbEOF, (int)fcb->fcbPLen, 0); DBG_VOP_LOCKS_TEST(retval); return (retval); } /* #% ioctl vp U U U # vop_ioctl { IN struct vnode *vp; IN u_long command; IN caddr_t data; IN int fflag; IN struct ucred *cred; IN struct proc *p; */ /* ARGSUSED */ int hfs_ioctl(ap) struct vop_ioctl_args /* { struct vnode *a_vp; int a_command; caddr_t a_data; int a_fflag; struct ucred *a_cred; struct proc *a_p; } */ *ap; { DBG_FUNC_NAME("hfs_ioctl"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_UNLOCKED, VOPDBG_UNLOCKED, VOPDBG_UNLOCKED, VOPDBG_POS); switch (ap->a_command) { case 1: { register struct hfsnode *hp; register struct vnode *vp; register struct radvisory *ra; FCB *fcb; int devBlockSize = 0; int error; vp = ap->a_vp; VOP_LEASE(vp, ap->a_p, ap->a_cred, LEASE_READ); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_p); ra = (struct radvisory *)(ap->a_data); hp = VTOH(vp); fcb = HTOFCB(hp); if (ra->ra_offset >= fcb->fcbEOF) { VOP_UNLOCK(vp, 0, ap->a_p); DBG_VOP_LOCKS_TEST(EFBIG); return (EFBIG); } VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); error = advisory_read(vp, fcb->fcbEOF, ra->ra_offset, ra->ra_count, devBlockSize); VOP_UNLOCK(vp, 0, ap->a_p); DBG_VOP_LOCKS_TEST(error); return (error); } case 2: /* F_READBOOTBLOCKS */ case 3: /* F_WRITEBOOTBLOCKS */ { struct vnode *vp = ap->a_vp; struct hfsnode *hp = VTOH(vp); struct fbootstraptransfer *btd = (struct fbootstraptransfer *)ap->a_data; int devBlockSize; int error; struct iovec aiov; struct uio auio; u_long blockNumber; u_long blockOffset; u_long xfersize; struct buf *bp; if ((vp->v_flag & VROOT) == 0) return EINVAL; if (btd->fbt_offset + btd->fbt_length > 1024) return EINVAL; aiov.iov_base = btd->fbt_buffer; aiov.iov_len = btd->fbt_length; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = btd->fbt_offset; auio.uio_resid = btd->fbt_length; auio.uio_segflg = UIO_USERSPACE; auio.uio_rw = (ap->a_command == 3) ? UIO_WRITE : UIO_READ; /* F_WRITEBOOTSTRAP / F_READBOOTSTRAP */ auio.uio_procp = ap->a_p; VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); while (auio.uio_resid > 0) { blockNumber = auio.uio_offset / devBlockSize; error = bread(hp->h_meta->h_devvp, blockNumber, devBlockSize, ap->a_cred, &bp); if (error) { if (bp) brelse(bp); return error; }; blockOffset = auio.uio_offset % devBlockSize; xfersize = devBlockSize - blockOffset; error = uiomove((caddr_t)bp->b_data + blockOffset, (int)xfersize, &auio); if (error) { brelse(bp); return error; }; if (auio.uio_rw == UIO_WRITE) { error = VOP_BWRITE(bp); if (error) return error; } else { brelse(bp); }; }; }; return 0; case _IOC(IOC_OUT,'h', 4, 0): /* Create date in local time */ { *(time_t *)(ap->a_data) = to_bsd_time(VTOVCB(ap->a_vp)->localCreateDate); return 0; } default: DBG_VOP_LOCKS_TEST(ENOTTY); return (ENOTTY); } /* Should never get here */ return 0; } /* ARGSUSED */ int hfs_select(ap) struct vop_select_args /* { struct vnode *a_vp; int a_which; int a_fflags; struct ucred *a_cred; void *a_wql; struct proc *a_p; } */ *ap; { DBG_FUNC_NAME("hfs_select"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS); /* * We should really check to see if I/O is possible. */ DBG_VOP_LOCKS_TEST(1); return (1); } /* * Mmap a file * * NB Currently unsupported. # XXX - not used # vop_mmap { IN struct vnode *vp; IN int fflags; IN struct ucred *cred; IN struct proc *p; */ /* ARGSUSED */ int hfs_mmap(ap) struct vop_mmap_args /* { struct vnode *a_vp; int a_fflags; struct ucred *a_cred; struct proc *a_p; } */ *ap; { DBG_FUNC_NAME("hfs_mmap"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS); DBG_VOP_LOCKS_TEST(EINVAL); return (EINVAL); } /* * Seek on a file * * Nothing to do, so just return. # XXX - not used # Needs work: Is newoff right? What's it mean? # vop_seek { IN struct vnode *vp; IN off_t oldoff; IN off_t newoff; IN struct ucred *cred; */ /* ARGSUSED */ int hfs_seek(ap) struct vop_seek_args /* { struct vnode *a_vp; off_t a_oldoff; off_t a_newoff; struct ucred *a_cred; } */ *ap; { DBG_FUNC_NAME("hfs_seek"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS); DBG_VOP_LOCKS_TEST(E_NONE); return (E_NONE); } /* * Bmap converts a the logical block number of a file to its physical block * number on the disk. */ /* * vp - address of vnode file the file * bn - which logical block to convert to a physical block number. * vpp - returns the vnode for the block special file holding the filesystem * containing the file of interest * bnp - address of where to return the filesystem physical block number #% bmap vp L L L #% bmap vpp - U - # vop_bmap { IN struct vnode *vp; IN daddr_t bn; OUT struct vnode **vpp; IN daddr_t *bnp; OUT int *runp; */ /* * Converts a logical block number to a physical block, and optionally returns * the amount of remaining blocks in a run. The logical block is based on hfsNode.logBlockSize. * The physical block number is based on the device block size, currently its 512. * The block run is returned in logical blocks, and is the REMAINING amount of blocks */ int hfs_bmap(ap) struct vop_bmap_args /* { struct vnode *a_vp; daddr_t a_bn; struct vnode **a_vpp; daddr_t *a_bnp; int *a_runp; } */ *ap; { struct hfsnode *hp = VTOH(ap->a_vp); struct hfsmount *hfsmp = VTOHFS(ap->a_vp); int retval = E_NONE; daddr_t logBlockSize; size_t bytesContAvail = 0; off_t blockposition; struct proc *p = NULL; int lockExtBtree; struct rl_entry *invalid_range; enum rl_overlaptype overlaptype; #define DEBUG_BMAP 0 #if DEBUG_BMAP DBG_FUNC_NAME("hfs_bmap"); DBG_VOP_LOCKS_DECL(2); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); if (ap->a_vpp != NULL) { DBG_VOP_LOCKS_INIT(1,*ap->a_vpp, VOPDBG_IGNORE, VOPDBG_UNLOCKED, VOPDBG_IGNORE, VOPDBG_POS); } else { DBG_VOP_LOCKS_INIT(1,NULL, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS); }; #endif DBG_IO(("\tMapped blk %d --> ", ap->a_bn)); /* * Check for underlying vnode requests and ensure that logical * to physical mapping is requested. */ if (ap->a_vpp != NULL) *ap->a_vpp = VTOH(ap->a_vp)->h_meta->h_devvp; if (ap->a_bnp == NULL) return (0); logBlockSize = GetLogicalBlockSize(ap->a_vp); blockposition = (off_t)(ap->a_bn * logBlockSize); lockExtBtree = hasOverflowExtents(hp); if (lockExtBtree) { p = current_proc(); retval = hfs_metafilelocking(hfsmp, kHFSExtentsFileID, LK_EXCLUSIVE | LK_CANRECURSE, p); if (retval) return (retval); } retval = MacToVFSError( MapFileBlockC (HFSTOVCB(hfsmp), HTOFCB(hp), MAXPHYSIO, blockposition, ap->a_bnp, &bytesContAvail)); if (lockExtBtree) (void) hfs_metafilelocking(hfsmp, kHFSExtentsFileID, LK_RELEASE, p); if (retval == E_NONE) { /* Adjust the mapping information for invalid file ranges: */ overlaptype = rl_scan(&hp->h_invalidranges, blockposition, blockposition + MAXPHYSIO - 1, &invalid_range); if (overlaptype != RL_NOOVERLAP) { switch(overlaptype) { case RL_MATCHINGOVERLAP: case RL_OVERLAPCONTAINSRANGE: case RL_OVERLAPSTARTSBEFORE: /* There's no valid block for this byte offset: */ *ap->a_bnp = (daddr_t)-1; bytesContAvail = invalid_range->rl_end + 1 - blockposition; break; case RL_OVERLAPISCONTAINED: case RL_OVERLAPENDSAFTER: /* The range of interest hits an invalid block before the end: */ if (invalid_range->rl_start == blockposition) { /* There's actually no valid information to be had starting here: */ *ap->a_bnp = (daddr_t)-1; if ((HTOFCB(hp)->fcbEOF > (invalid_range->rl_end + 1)) && (invalid_range->rl_end + 1 - blockposition < bytesContAvail)) { bytesContAvail = invalid_range->rl_end + 1 - blockposition; }; } else { bytesContAvail = invalid_range->rl_start - blockposition; }; break; }; if (bytesContAvail > MAXPHYSIO) bytesContAvail = MAXPHYSIO; }; /* Figure out how many read ahead blocks there are */ if (ap->a_runp != NULL) { if (can_cluster(logBlockSize)) { /* Make sure this result never goes negative: */ *ap->a_runp = (bytesContAvail < logBlockSize) ? 0 : (bytesContAvail / logBlockSize) - 1; } else { *ap->a_runp = 0; }; }; }; DBG_IO(("%d:%d.\n", *ap->a_bnp, (bytesContAvail < logBlockSize) ? 0 : (bytesContAvail / logBlockSize) - 1)); #if DEBUG_BMAP DBG_VOP_LOCKS_TEST(retval); #endif if (ap->a_runp) { DBG_ASSERT((*ap->a_runp * logBlockSize) < bytesContAvail); /* At least *ap->a_runp blocks left and ... */ if (can_cluster(logBlockSize)) { DBG_ASSERT(bytesContAvail - (*ap->a_runp * logBlockSize) < (2*logBlockSize)); /* ... at most 1 logical block accounted for by current block */ /* ... plus some sub-logical block sized piece */ }; }; return (retval); } /* blktooff converts logical block number to file offset */ int hfs_blktooff(ap) struct vop_blktooff_args /* { struct vnode *a_vp; daddr_t a_lblkno; off_t *a_offset; } */ *ap; { if (ap->a_vp == NULL) return (EINVAL); *ap->a_offset = (off_t)ap->a_lblkno * PAGE_SIZE_64; return(0); } int hfs_offtoblk(ap) struct vop_offtoblk_args /* { struct vnode *a_vp; off_t a_offset; daddr_t *a_lblkno; } */ *ap; { long lbsize, boff; if (ap->a_vp == NULL) return (EINVAL); *ap->a_lblkno = ap->a_offset / PAGE_SIZE_64; return(0); } int hfs_cmap(ap) struct vop_cmap_args /* { struct vnode *a_vp; off_t a_foffset; size_t a_size; daddr_t *a_bpn; size_t *a_run; void *a_poff; } */ *ap; { struct hfsnode *hp = VTOH(ap->a_vp); struct hfsmount *hfsmp = VTOHFS(ap->a_vp); FCB *fcb = HTOFCB(hp); size_t bytesContAvail = 0; int retval = E_NONE; int lockExtBtree; struct proc *p = NULL; struct rl_entry *invalid_range; enum rl_overlaptype overlaptype; off_t limit; #define DEBUG_CMAP 0 #if DEBUG_CMAP DBG_FUNC_NAME("hfs_cmap"); DBG_VOP_LOCKS_DECL(2); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp); DBG_VOP_LOCKS_INIT(0, ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); #endif DBG_IO(("\tMapped offset %qx --> ", ap->a_foffset)); /* * Check for underlying vnode requests and ensure that logical * to physical mapping is requested. */ if (ap->a_bpn == NULL) { return (0); }; if (lockExtBtree = hasOverflowExtents(hp)) { p = current_proc(); if (retval = hfs_metafilelocking(hfsmp, kHFSExtentsFileID, LK_EXCLUSIVE | LK_CANRECURSE, p)) { return (retval); }; } retval = MacToVFSError( MapFileBlockC (HFSTOVCB(hfsmp), fcb, ap->a_size, ap->a_foffset, ap->a_bpn, &bytesContAvail)); if (lockExtBtree) (void) hfs_metafilelocking(hfsmp, kHFSExtentsFileID, LK_RELEASE, p); if (retval == E_NONE) { /* Adjust the mapping information for invalid file ranges: */ overlaptype = rl_scan(&hp->h_invalidranges, ap->a_foffset, ap->a_foffset + (off_t)bytesContAvail - 1, &invalid_range); if (overlaptype != RL_NOOVERLAP) { switch(overlaptype) { case RL_MATCHINGOVERLAP: case RL_OVERLAPCONTAINSRANGE: case RL_OVERLAPSTARTSBEFORE: /* There's no valid block for this byte offset: */ *ap->a_bpn = (daddr_t)-1; /* There's no point limiting the amount to be returned if the invalid range that was hit extends all the way to the EOF (i.e. there's no valid bytes between the end of this range and the file's EOF): */ if ((fcb->fcbEOF > (invalid_range->rl_end + 1)) && (invalid_range->rl_end + 1 - ap->a_foffset < bytesContAvail)) { bytesContAvail = invalid_range->rl_end + 1 - ap->a_foffset; }; break; case RL_OVERLAPISCONTAINED: case RL_OVERLAPENDSAFTER: /* The range of interest hits an invalid block before the end: */ if (invalid_range->rl_start == ap->a_foffset) { /* There's actually no valid information to be had starting here: */ *ap->a_bpn = (daddr_t)-1; if ((fcb->fcbEOF > (invalid_range->rl_end + 1)) && (invalid_range->rl_end + 1 - ap->a_foffset < bytesContAvail)) { bytesContAvail = invalid_range->rl_end + 1 - ap->a_foffset; }; } else { bytesContAvail = invalid_range->rl_start - ap->a_foffset; }; break; }; if (bytesContAvail > ap->a_size) bytesContAvail = ap->a_size; }; if (ap->a_run) *ap->a_run = bytesContAvail; }; if (ap->a_poff) *(int *)ap->a_poff = 0; DBG_IO(("%d:%d.\n", *ap->a_bpn, bytesContAvail)); #if DEBUG_BMAP DBG_VOP_LOCKS_TEST(retval); #endif return (retval); } /* * Calculate the logical to physical mapping if not done already, * then call the device strategy routine. # #vop_strategy { # IN struct buf *bp; */ int hfs_strategy(ap) struct vop_strategy_args /* { struct buf *a_bp; } */ *ap; { register struct buf *bp = ap->a_bp; register struct vnode *vp = bp->b_vp; register struct hfsnode *hp; int retval = 0; DBG_FUNC_NAME("hfs_strategy"); // DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n")); hp = VTOH(vp); if ( !(bp->b_flags & B_VECTORLIST)) { if (vp->v_type == VBLK || vp->v_type == VCHR) panic("hfs_strategy: device vnode passed!"); if (bp->b_flags & B_PAGELIST) { /* * if we have a page list associated with this bp, * then go through cluster_bp since it knows how to * deal with a page request that might span non-contiguous * physical blocks on the disk... */ retval = cluster_bp(bp); vp = hp->h_meta->h_devvp; bp->b_dev = vp->v_rdev; return (retval); } /* * If we don't already know the filesystem relative block number * then get it using VOP_BMAP(). If VOP_BMAP() returns the block * number as -1 then we've got a hole in the file. Although HFS * filesystems don't create files with holes, invalidating of * subranges of the file (lazy zero filling) may create such a * situation. */ if (bp->b_blkno == bp->b_lblkno) { if ((retval = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL))) { bp->b_error = retval; bp->b_flags |= B_ERROR; biodone(bp); return (retval); } if ((long)bp->b_blkno == -1) clrbuf(bp); } if ((long)bp->b_blkno == -1) { biodone(bp); return (0); } if (bp->b_validend == 0) { /* Record the exact size of the I/O transfer about to be made: */ DBG_ASSERT(bp->b_validoff == 0); bp->b_validend = bp->b_bcount; DBG_ASSERT(bp->b_dirtyoff == 0); }; } vp = hp->h_meta->h_devvp; bp->b_dev = vp->v_rdev; DBG_IO(("\t\t>>>%s: continuing w/ vp: 0x%x with logBlk Ox%X and phyBlk Ox%X\n", funcname, (u_int)vp, bp->b_lblkno, bp->b_blkno)); return VOCALL (vp->v_op, VOFFSET(vop_strategy), ap); } /* #% reallocblks vp L L L # vop_reallocblks { IN struct vnode *vp; IN struct cluster_save *buflist; */ int hfs_reallocblks(ap) struct vop_reallocblks_args /* { struct vnode *a_vp; struct cluster_save *a_buflist; } */ *ap; { DBG_FUNC_NAME("hfs_reallocblks"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); /* Currently no support for clustering */ /* XXX */ DBG_VOP_LOCKS_TEST(ENOSPC); return (ENOSPC); } /* # #% truncate vp L L L # vop_truncate { IN struct vnode *vp; IN off_t length; IN int flags; (IO_SYNC) IN struct ucred *cred; IN struct proc *p; }; * Truncate the hfsnode hp to at most length size, freeing (or adding) the * disk blocks. */ int hfs_truncate(ap) struct vop_truncate_args /* { struct vnode *a_vp; off_t a_length; int a_flags; struct ucred *a_cred; struct proc *a_p; } */ *ap; { register struct vnode *vp = ap->a_vp; register struct hfsnode *hp = VTOH(vp); off_t length = ap->a_length; long vflags; struct timeval tv; int retval; FCB *fcb; off_t bytesToAdd; off_t actualBytesAdded; DBG_FUNC_NAME("hfs_truncate"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); #if HFS_DIAGNOSTIC debug_check_blocksizes(ap->a_vp); #endif fcb = HTOFCB(hp); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 7)) | DBG_FUNC_START, (int)length, fcb->fcbEOF, fcb->fcbPLen, 0, 0); if (length < 0) { DBG_VOP_LOCKS_TEST(EINVAL); return (EINVAL); } if ((!ISHFSPLUS(VTOVCB(vp))) && (length > (off_t)MAXHFSFILESIZE)) { DBG_VOP_LOCKS_TEST(EFBIG); return (EFBIG); } if (vp->v_type != VREG && vp->v_type != VLNK) { DBG_VOP_LOCKS_TEST(EISDIR); return (EISDIR); /* hfs doesn't support truncating of directories */ } tv = time; retval = E_NONE; DBG_RW(("%s: truncate from Ox%lX to Ox%X bytes\n", funcname, fcb->fcbPLen, length)); /* * we cannot just check if fcb->fcbEOF == length (as an optimization) * since there may be extra physical blocks that also need truncation */ /* * Lengthen the size of the file. We must ensure that the * last byte of the file is allocated. Since the smallest * value of fcbEOF is 0, length will be at least 1. */ if (length > fcb->fcbEOF) { off_t filePosition; daddr_t logBlockNo; long logBlockSize; long blkOffset; off_t bytestoclear; int blockZeroCount; struct buf *bp=NULL; /* * If we don't have enough physical space then * we need to extend the physical size. */ if (length > fcb->fcbPLen) { /* lock extents b-tree (also protects volume bitmap) */ retval = hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_EXCLUSIVE, ap->a_p); if (retval) goto Err_Exit; while ((length > fcb->fcbPLen) && (retval == E_NONE)) { bytesToAdd = length - fcb->fcbPLen; retval = MacToVFSError( ExtendFileC (HTOVCB(hp), fcb, bytesToAdd, 0, kEFAllMask, /* allocate all requested bytes or none */ &actualBytesAdded)); if (actualBytesAdded == 0 && retval == E_NONE) { if (length > fcb->fcbPLen) length = fcb->fcbPLen; break; } } (void) hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_RELEASE, ap->a_p); if (retval) goto Err_Exit; DBG_ASSERT(length <= fcb->fcbPLen); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 7)) | DBG_FUNC_NONE, (int)length, fcb->fcbEOF, fcb->fcbPLen, 0, 0); } if (! (ap->a_flags & IO_NOZEROFILL)) { if (UBCISVALID(vp) && retval == E_NONE) { struct rl_entry *invalid_range; int devBlockSize; off_t zero_limit; zero_limit = (fcb->fcbEOF + (PAGE_SIZE_64 - 1)) & ~PAGE_MASK_64; if (length < zero_limit) zero_limit = length; if (length > fcb->fcbEOF) { /* Extending the file: time to fill out the current last page w. zeroes? */ if ((fcb->fcbEOF & PAGE_MASK_64) && (rl_scan(&hp->h_invalidranges, fcb->fcbEOF & ~PAGE_MASK_64, fcb->fcbEOF - 1, &invalid_range) == RL_NOOVERLAP)) { /* There's some valid data at the start of the (current) last page of the file, so zero out the remainder of that page to ensure the entire page contains valid data. Since there is no invalid range possible past the (current) eof, there's no need to remove anything from the invalid range list before calling cluster_write(): */ VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); retval = cluster_write(vp, (struct uio *) 0, fcb->fcbEOF, zero_limit, fcb->fcbEOF, (off_t)0, devBlockSize, (ap->a_flags & IO_SYNC) | IO_HEADZEROFILL); if (retval) goto Err_Exit; /* Merely invalidate the remaining area, if necessary: */ if (length > zero_limit) rl_add(zero_limit, length - 1, &hp->h_invalidranges); } else { /* The page containing the (current) eof is invalid: just add the remainder of the page to the invalid list, along with the area being newly allocated: */ rl_add(fcb->fcbEOF, length - 1, &hp->h_invalidranges); }; } } else { #if 0 /* * zero out any new logical space... */ bytestoclear = length - fcb->fcbEOF; filePosition = fcb->fcbEOF; while (bytestoclear > 0) { logBlockNo = (daddr_t)(filePosition / PAGE_SIZE_64); blkOffset = (long)(filePosition & PAGE_MASK_64); if (((off_t)(fcb->fcbPLen) - ((off_t)logBlockNo * (off_t)PAGE_SIZE)) < PAGE_SIZE_64) logBlockSize = (off_t)(fcb->fcbPLen) - ((off_t)logBlockNo * PAGE_SIZE_64); else logBlockSize = PAGE_SIZE; if (logBlockSize < blkOffset) panic("hfs_truncate: bad logBlockSize computed\n"); blockZeroCount = MIN(bytestoclear, logBlockSize - blkOffset); if (blkOffset == 0 && ((bytestoclear >= logBlockSize) || filePosition >= fcb->fcbEOF)) { bp = getblk(vp, logBlockNo, logBlockSize, 0, 0, BLK_WRITE); retval = 0; } else { retval = bread(vp, logBlockNo, logBlockSize, ap->a_cred, &bp); if (retval) { brelse(bp); goto Err_Exit; } } bzero((char *)bp->b_data + blkOffset, blockZeroCount); bp->b_flags |= B_DIRTY | B_AGE; if (ap->a_flags & IO_SYNC) VOP_BWRITE(bp); else if (logBlockNo % 32) bawrite(bp); else VOP_BWRITE(bp); /* wait after we issue 32 requests */ bytestoclear -= blockZeroCount; filePosition += blockZeroCount; } #else panic("hfs_truncate: invoked on non-UBC object?!"); #endif }; } fcb->fcbEOF = length; if (UBCISVALID(vp)) ubc_setsize(vp, (off_t)fcb->fcbEOF); /* XXX check errors */ } else { /* Shorten the size of the file */ if (fcb->fcbEOF > length) { /* * Any buffers that are past the truncation point need to be * invalidated (to maintain buffer cache consistency). For * simplicity, we invalidate all the buffers by calling vinvalbuf. */ if (UBCISVALID(vp)) ubc_setsize(vp, (off_t)length); /* XXX check errors */ vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA; retval = vinvalbuf(vp, vflags, ap->a_cred, ap->a_p, 0, 0); /* Any space previously marked as invalid is now irrelevant: */ rl_remove(length, fcb->fcbEOF - 1, &hp->h_invalidranges); } /* * For a TBE process the deallocation of the file blocks is * delayed until the file is closed. And hfs_close calls * truncate with the IO_NDELAY flag set. So when IO_NDELAY * isn't set, we make sure this isn't a TBE process. */ if ((ap->a_flags & IO_NDELAY) || (!ISSET(ap->a_p->p_flag, P_TBE))) { /* lock extents b-tree (also protects volume bitmap) */ retval = hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_EXCLUSIVE, ap->a_p); if (retval) goto Err_Exit; retval = MacToVFSError( TruncateFileC( HTOVCB(hp), fcb, length, false)); (void) hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_RELEASE, ap->a_p); if (retval) goto Err_Exit; } fcb->fcbEOF = length; if (fcb->fcbFlags & fcbModifiedMask) hp->h_nodeflags |= IN_MODIFIED; } hp->h_nodeflags |= IN_CHANGE | IN_UPDATE; retval = VOP_UPDATE(vp, &tv, &tv, MNT_WAIT); if (retval) { DBG_ERR(("Could not update truncate")); KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 7)) | DBG_FUNC_NONE, -1, -1, -1, retval, 0); } Err_Exit:; #if HFS_DIAGNOSTIC debug_check_blocksizes(ap->a_vp); #endif KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 7)) | DBG_FUNC_END, (int)length, fcb->fcbEOF, fcb->fcbPLen, retval, 0); DBG_VOP_LOCKS_TEST(retval); return (retval); } /* # #% allocate vp L L L # vop_allocate { IN struct vnode *vp; IN off_t length; IN int flags; OUT off_t *bytesallocated; IN off_t offset; IN struct ucred *cred; IN struct proc *p; }; * allocate the hfsnode hp to at most length size */ int hfs_allocate(ap) struct vop_allocate_args /* { struct vnode *a_vp; off_t a_length; u_int32_t a_flags; off_t *a_bytesallocated; off_t a_offset; struct ucred *a_cred; struct proc *a_p; } */ *ap; { register struct vnode *vp = ap->a_vp; register struct hfsnode *hp = VTOH(vp); off_t length = ap->a_length; off_t startingPEOF; off_t moreBytesRequested; off_t actualBytesAdded; long vflags; struct timeval tv; int retval, retval2; FCB *fcb; UInt32 blockHint; UInt32 extendFlags =0; /* For call to ExtendFileC */ DBG_FUNC_NAME("hfs_allocate"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(ap->a_vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,ap->a_vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); /* Set the number of bytes allocated to 0 so that the caller will know that we did nothing. ExtendFileC will fill this in for us if we actually allocate space */ *(ap->a_bytesallocated) = 0; fcb = HTOFCB(hp); /* Now for some error checking */ if (length < (off_t)0) { DBG_VOP_LOCKS_TEST(EINVAL); return (EINVAL); } if (vp->v_type != VREG && vp->v_type != VLNK) { DBG_VOP_LOCKS_TEST(EISDIR); return (EISDIR); /* hfs doesn't support truncating of directories */ } if ((ap->a_flags & ALLOCATEFROMVOL) && (length <= fcb->fcbPLen)) return (EINVAL); /* Fill in the flags word for the call to Extend the file */ if (ap->a_flags & ALLOCATECONTIG) { extendFlags |= kEFContigMask; } if (ap->a_flags & ALLOCATEALL) { extendFlags |= kEFAllMask; } tv = time; retval = E_NONE; blockHint = 0; startingPEOF = fcb->fcbPLen; if (ap->a_flags & ALLOCATEFROMPEOF) { length += fcb->fcbPLen; } if (ap->a_flags & ALLOCATEFROMVOL) blockHint = ap->a_offset / HTOVCB(hp)->blockSize; /* If no changes are necesary, then we're done */ if (fcb->fcbPLen == length) goto Std_Exit; /* * Lengthen the size of the file. We must ensure that the * last byte of the file is allocated. Since the smallest * value of fcbPLen is 0, length will be at least 1. */ if (length > fcb->fcbPLen) { moreBytesRequested = length - fcb->fcbPLen; /* lock extents b-tree (also protects volume bitmap) */ retval = hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_EXCLUSIVE, ap->a_p); if (retval) goto Err_Exit; retval = MacToVFSError( ExtendFileC(HTOVCB(hp), fcb, moreBytesRequested, blockHint, extendFlags, &actualBytesAdded)); *(ap->a_bytesallocated) = actualBytesAdded; (void) hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_RELEASE, ap->a_p); DBG_ASSERT(length <= fcb->fcbPLen); /* * if we get an error and no changes were made then exit * otherwise we must do the VOP_UPDATE to reflect the changes */ if (retval && (startingPEOF == fcb->fcbPLen)) goto Err_Exit; /* * Adjust actualBytesAdded to be allocation block aligned, not * clump size aligned. * NOTE: So what we are reporting does not affect reality * until the file is closed, when we truncate the file to allocation * block size. */ if ((actualBytesAdded != 0) && (moreBytesRequested < actualBytesAdded)) *(ap->a_bytesallocated) = roundup(moreBytesRequested, (off_t)VTOVCB(vp)->blockSize); } else { /* Shorten the size of the file */ if (fcb->fcbEOF > length) { /* * Any buffers that are past the truncation point need to be * invalidated (to maintain buffer cache consistency). For * simplicity, we invalidate all the buffers by calling vinvalbuf. */ vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA; (void) vinvalbuf(vp, vflags, ap->a_cred, ap->a_p, 0, 0); } /* lock extents b-tree (also protects volume bitmap) */ retval = hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_EXCLUSIVE, ap->a_p); if (retval) goto Err_Exit; retval = MacToVFSError( TruncateFileC( HTOVCB(hp), fcb, length, false)); (void) hfs_metafilelocking(HTOHFS(hp), kHFSExtentsFileID, LK_RELEASE, ap->a_p); /* * if we get an error and no changes were made then exit * otherwise we must do the VOP_UPDATE to reflect the changes */ if (retval && (startingPEOF == fcb->fcbPLen)) goto Err_Exit; if (fcb->fcbFlags & fcbModifiedMask) hp->h_nodeflags |= IN_MODIFIED; DBG_ASSERT(length <= fcb->fcbPLen) // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG if (fcb->fcbEOF > fcb->fcbPLen) { fcb->fcbEOF = fcb->fcbPLen; if (UBCISVALID(vp)) ubc_setsize(vp, (off_t)fcb->fcbEOF); /* XXX check errors */ } } Std_Exit: hp->h_nodeflags |= IN_CHANGE | IN_UPDATE; retval2 = VOP_UPDATE(vp, &tv, &tv, MNT_WAIT); if (retval == 0) retval = retval2; Err_Exit: DBG_VOP_LOCKS_TEST(retval); return (retval); } /* pagein for HFS filesystem, similar to hfs_read(), but without cluster_read() */ int hfs_pagein(ap) struct vop_pagein_args /* { struct vnode *a_vp, upl_t a_pl, vm_offset_t a_pl_offset, off_t a_f_offset, size_t a_size, struct ucred *a_cred, int a_flags } */ *ap; { register struct vnode *vp; struct hfsnode *hp; FCB *fcb; int devBlockSize = 0; int retval; DBG_FUNC_NAME("hfs_pagein"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(vp);DBG_VOP_CONT(("\n")); DBG_VOP_LOCKS_INIT(0,vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); vp = ap->a_vp; hp = VTOH(vp); fcb = HTOFCB(hp); if (vp->v_type != VREG && vp->v_type != VLNK) panic("hfs_pagein: vp not UBC type\n"); DBG_VOP(("\tfile size Ox%X\n", (u_int)fcb->fcbEOF)); DBG_VOP(("\tstarting at offset Ox%X of file, length Ox%X\n", (u_int)ap->a_f_offset, (u_int)ap->a_size)); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); retval = cluster_pagein(vp, ap->a_pl, ap->a_pl_offset, ap->a_f_offset, ap->a_size, (off_t)fcb->fcbEOF, devBlockSize, ap->a_flags); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif DBG_VOP_LOCKS_TEST(retval); return (retval); } /* * pageout for HFS filesystem. */ int hfs_pageout(ap) struct vop_pageout_args /* { struct vnode *a_vp, upl_t a_pl, vm_offset_t a_pl_offset, off_t a_f_offset, size_t a_size, struct ucred *a_cred, int a_flags } */ *ap; { struct vnode *vp = ap->a_vp; struct hfsnode *hp = VTOH(vp); FCB *fcb = HTOFCB(hp); int retval; int devBlockSize = 0; off_t end_of_range; DBG_FUNC_NAME("hfs_pageout"); DBG_VOP_LOCKS_DECL(1); DBG_VOP_PRINT_FUNCNAME(); DBG_VOP_PRINT_VNODE_INFO(vp);DBG_VOP_CONT(("\n")); DBG_VOP(("\thfsnode 0x%x (%s)\n", (u_int)hp, H_NAME(hp))); DBG_VOP(("\tstarting at offset Ox%lX of file, length Ox%lX\n", (UInt32)ap->a_f_offset, (UInt32)ap->a_size)); DBG_VOP_LOCKS_INIT(0, vp, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_LOCKED, VOPDBG_POS); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif if (UBCINVALID(vp)) panic("hfs_pageout: Not a VREG: vp=%x", vp); VOP_DEVBLOCKSIZE(hp->h_meta->h_devvp, &devBlockSize); end_of_range = ap->a_f_offset + ap->a_size - 1; if (end_of_range >= (off_t)fcb->fcbEOF) end_of_range = (off_t)(fcb->fcbEOF - 1); if (ap->a_f_offset < (off_t)fcb->fcbEOF) rl_remove(ap->a_f_offset, end_of_range, &hp->h_invalidranges); retval = cluster_pageout(vp, ap->a_pl, ap->a_pl_offset, ap->a_f_offset, ap->a_size, (off_t)fcb->fcbEOF, devBlockSize, ap->a_flags); /* * If we successfully wrote any data, and we are not the superuser * we clear the setuid and setgid bits as a precaution against * tampering. */ if (retval == 0 && ap->a_cred && ap->a_cred->cr_uid != 0) hp->h_meta->h_mode &= ~(ISUID | ISGID); #if HFS_DIAGNOSTIC debug_check_blocksizes(vp); #endif DBG_VOP_LOCKS_TEST(retval); return (retval); } /* * Intercept B-Tree node writes to unswap them if necessary. # #vop_bwrite { # IN struct buf *bp; */ int hfs_bwrite(ap) struct vop_bwrite_args /* { struct buf *a_bp; } */ *ap; { register struct buf *bp = ap->a_bp; register struct vnode *vp = bp->b_vp; BlockDescriptor block; int retval = 0; DBG_FUNC_NAME("hfs_bwrite"); #if BYTE_ORDER == LITTLE_ENDIAN /* Trap B-Tree writes */ if ((H_FILEID(VTOH(vp)) == kHFSExtentsFileID) || (H_FILEID(VTOH(vp)) == kHFSCatalogFileID)) { /* Swap if the B-Tree node is in native byte order */ if (((UInt16 *)((char *)bp->b_data + bp->b_bcount - 2))[0] == 0x000e) { /* Prepare the block pointer */ block.blockHeader = bp; block.buffer = bp->b_data + IOBYTEOFFSETFORBLK(bp->b_blkno, VTOHFS(vp)->hfs_phys_block_size); block.blockReadFromDisk = (bp->b_flags & B_CACHE) == 0; /* not found in cache ==> came from disk */ block.blockSize = bp->b_bcount; /* Endian un-swap B-Tree node */ SWAP_BT_NODE (&block, ISHFSPLUS (VTOVCB(vp)), H_FILEID(VTOH(vp)), 1); } /* We don't check to make sure that it's 0x0e00 because it could be all zeros */ } #endif retval = vn_bwrite (ap); return (retval); } |