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 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
|
/*
* fuse2fs.c - FUSE server for e2fsprogs.
*
* Copyright (C) 2014 Oracle.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "config.h"
#include <pthread.h>
#ifdef __linux__
# include <linux/fs.h>
# include <linux/falloc.h>
# include <linux/xattr.h>
#endif
#include <sys/ioctl.h>
#include <unistd.h>
#ifdef __SET_FOB_FOR_FUSE
# error Do not set magic value __SET_FOB_FOR_FUSE!!!!
#endif
#ifndef _FILE_OFFSET_BITS
/*
* Old versions of libfuse (e.g. Debian 2.9.9 package) required that the build
* system set _FILE_OFFSET_BITS explicitly, even if doing so isn't required to
* get a 64-bit off_t. AC_SYS_LARGEFILE doesn't set any _FILE_OFFSET_BITS if
* it's not required (such as on aarch64), so we must inject it here.
*/
# define __SET_FOB_FOR_FUSE
# define _FILE_OFFSET_BITS 64
#endif /* _FILE_OFFSET_BITS */
#include <fuse.h>
#ifdef __SET_FOB_FOR_FUSE
# undef _FILE_OFFSET_BITS
#endif /* __SET_FOB_FOR_FUSE */
#include <inttypes.h>
#include "ext2fs/ext2fs.h"
#include "ext2fs/ext2_fs.h"
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
# define FUSE_PLATFORM_OPTS ""
#else
# ifdef __linux__
# define FUSE_PLATFORM_OPTS ",use_ino,big_writes"
# else
# define FUSE_PLATFORM_OPTS ",use_ino"
# endif
#endif
#include "../version.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#include <locale.h>
#define _(a) (gettext(a))
#ifdef gettext_noop
#define N_(a) gettext_noop(a)
#else
#define N_(a) (a)
#endif
#define P_(singular, plural, n) (ngettext(singular, plural, n))
#ifndef NLS_CAT_NAME
#define NLS_CAT_NAME "e2fsprogs"
#endif
#ifndef LOCALEDIR
#define LOCALEDIR "/usr/share/locale"
#endif
#else
#define _(a) (a)
#define N_(a) a
#define P_(singular, plural, n) ((n) == 1 ? (singular) : (plural))
#endif
static ext2_filsys global_fs; /* Try not to use this directly */
#undef DEBUG
#ifdef DEBUG
# define dbg_printf(f, a...) do {printf("FUSE2FS-" f, ## a); \
fflush(stdout); \
} while (0)
#else
# define dbg_printf(f, a...)
#endif
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
# ifdef _IOR
# ifdef _IOW
# define SUPPORT_I_FLAGS
# endif
# endif
#endif
#ifdef FALLOC_FL_KEEP_SIZE
# define FL_KEEP_SIZE_FLAG FALLOC_FL_KEEP_SIZE
# define SUPPORT_FALLOCATE
#else
# define FL_KEEP_SIZE_FLAG (0)
#endif
#ifdef FALLOC_FL_PUNCH_HOLE
# define FL_PUNCH_HOLE_FLAG FALLOC_FL_PUNCH_HOLE
#else
# define FL_PUNCH_HOLE_FLAG (0)
#endif
errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
int journal_enable_debug = -1;
#endif
/*
* ext2_file_t contains a struct inode, so we can't leave files open.
* Use this as a proxy instead.
*/
#define FUSE2FS_FILE_MAGIC (0xEF53DEAFUL)
struct fuse2fs_file_handle {
unsigned long magic;
ext2_ino_t ino;
int open_flags;
};
/* Main program context */
#define FUSE2FS_MAGIC (0xEF53DEADUL)
struct fuse2fs {
unsigned long magic;
ext2_filsys fs;
pthread_mutex_t bfl;
char *device;
int ro;
int debug;
int no_default_opts;
int panic_on_error;
int minixdf;
int fakeroot;
int alloc_all_blocks;
int norecovery;
unsigned long offset;
FILE *err_fp;
unsigned int next_generation;
};
#define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \
return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \
} while (0)
#define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \
return translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); \
} while (0)
static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
const char *file, int line);
#define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \
__FILE__, __LINE__)
/* for macosx */
#ifndef W_OK
# define W_OK 2
#endif
#ifndef R_OK
# define R_OK 4
#endif
#define EXT4_EPOCH_BITS 2
#define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
#define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
/*
* Extended fields will fit into an inode if the filesystem was formatted
* with large inodes (-I 256 or larger) and there are not currently any EAs
* consuming all of the available space. For new inodes we always reserve
* enough space for the kernel's known extended fields, but for inodes
* created with an old kernel this might not have been the case. None of
* the extended inode fields is critical for correct filesystem operation.
* This macro checks if a certain field fits in the inode. Note that
* inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
*/
#define EXT4_FITS_IN_INODE(ext4_inode, field) \
((offsetof(typeof(*ext4_inode), field) + \
sizeof((ext4_inode)->field)) \
<= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \
(ext4_inode)->i_extra_isize)) \
static inline __u32 ext4_encode_extra_time(const struct timespec *time)
{
__u32 extra = sizeof(time->tv_sec) > 4 ?
((time->tv_sec - (__s32)time->tv_sec) >> 32) &
EXT4_EPOCH_MASK : 0;
return extra | (time->tv_nsec << EXT4_EPOCH_BITS);
}
static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra)
{
if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) {
__u64 extra_bits = extra & EXT4_EPOCH_MASK;
/*
* Prior to kernel 3.14?, we had a broken decode function,
* wherein we effectively did this:
* if (extra_bits == 3)
* extra_bits = 0;
*/
time->tv_sec += extra_bits << 32;
}
time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
}
#define EXT4_CLAMP_TIMESTAMP(xtime, timespec, raw_inode) \
do { \
if ((timespec)->tv_sec < EXT4_TIMESTAMP_MIN) \
(timespec)->tv_sec = EXT4_TIMESTAMP_MIN; \
if ((timespec)->tv_sec < EXT4_TIMESTAMP_MIN) \
(timespec)->tv_sec = EXT4_TIMESTAMP_MIN; \
\
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) { \
if ((timespec)->tv_sec > EXT4_EXTRA_TIMESTAMP_MAX) \
(timespec)->tv_sec = EXT4_EXTRA_TIMESTAMP_MAX; \
} else { \
if ((timespec)->tv_sec > EXT4_NON_EXTRA_TIMESTAMP_MAX) \
(timespec)->tv_sec = EXT4_NON_EXTRA_TIMESTAMP_MAX; \
} \
} while (0)
#define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \
do { \
typeof(*(timespec)) _ts = *(timespec); \
\
EXT4_CLAMP_TIMESTAMP(xtime, &_ts, raw_inode); \
(raw_inode)->xtime = _ts.tv_sec; \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
(raw_inode)->xtime ## _extra = \
ext4_encode_extra_time(&_ts); \
} while (0)
#define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \
do { \
typeof(*(timespec)) _ts = *(timespec); \
\
EXT4_CLAMP_TIMESTAMP(xtime, &_ts, raw_inode); \
if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
(raw_inode)->xtime = _ts.tv_sec; \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
(raw_inode)->xtime ## _extra = \
ext4_encode_extra_time(&_ts); \
} while (0)
#define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \
do { \
(timespec)->tv_sec = (signed)((raw_inode)->xtime); \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
ext4_decode_extra_time((timespec), \
(raw_inode)->xtime ## _extra); \
else \
(timespec)->tv_nsec = 0; \
} while (0)
#define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \
do { \
if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
(timespec)->tv_sec = \
(signed)((raw_inode)->xtime); \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
ext4_decode_extra_time((timespec), \
raw_inode->xtime ## _extra); \
else \
(timespec)->tv_nsec = 0; \
} while (0)
static void get_now(struct timespec *now)
{
#ifdef CLOCK_REALTIME
if (!clock_gettime(CLOCK_REALTIME, now))
return;
#endif
now->tv_sec = time(NULL);
now->tv_nsec = 0;
}
static void increment_version(struct ext2_inode_large *inode)
{
__u64 ver;
ver = inode->osd1.linux1.l_i_version;
if (EXT4_FITS_IN_INODE(inode, i_version_hi))
ver |= (__u64)inode->i_version_hi << 32;
ver++;
inode->osd1.linux1.l_i_version = ver;
if (EXT4_FITS_IN_INODE(inode, i_version_hi))
inode->i_version_hi = ver >> 32;
}
static void init_times(struct ext2_inode_large *inode)
{
struct timespec now;
get_now(&now);
EXT4_INODE_SET_XTIME(i_atime, &now, inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
increment_version(inode);
}
static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *pinode)
{
errcode_t err;
struct timespec now;
struct ext2_inode_large inode;
get_now(&now);
/* If user already has a inode buffer, just update that */
if (pinode) {
increment_version(pinode);
EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
return 0;
}
/* Otherwise we have to read-modify-write the inode */
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
increment_version(&inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int update_atime(ext2_filsys fs, ext2_ino_t ino)
{
errcode_t err;
struct ext2_inode_large inode, *pinode;
struct timespec atime, mtime, now;
if (!(fs->flags & EXT2_FLAG_RW))
return 0;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
pinode = &inode;
EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
get_now(&now);
/*
* If atime is newer than mtime and atime hasn't been updated in thirty
* seconds, skip the atime update. Same idea as Linux "relatime".
*/
if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30)
return 0;
EXT4_INODE_SET_XTIME(i_atime, &now, &inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *pinode)
{
errcode_t err;
struct ext2_inode_large inode;
struct timespec now;
if (pinode) {
get_now(&now);
EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
increment_version(pinode);
return 0;
}
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
get_now(&now);
EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
increment_version(&inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int ext2_file_type(unsigned int mode)
{
if (LINUX_S_ISREG(mode))
return EXT2_FT_REG_FILE;
if (LINUX_S_ISDIR(mode))
return EXT2_FT_DIR;
if (LINUX_S_ISCHR(mode))
return EXT2_FT_CHRDEV;
if (LINUX_S_ISBLK(mode))
return EXT2_FT_BLKDEV;
if (LINUX_S_ISLNK(mode))
return EXT2_FT_SYMLINK;
if (LINUX_S_ISFIFO(mode))
return EXT2_FT_FIFO;
if (LINUX_S_ISSOCK(mode))
return EXT2_FT_SOCK;
return 0;
}
static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)
{
ext2_filsys fs = ff->fs;
blk64_t reserved;
dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu "
"rsvd=%llu\n", __func__, num, ff->alloc_all_blocks,
ext2fs_blocks_count(fs->super),
ext2fs_free_blocks_count(fs->super),
ext2fs_r_blocks_count(fs->super));
if (num > ext2fs_blocks_count(fs->super))
return 0;
if (ff->alloc_all_blocks)
return 1;
/*
* Different meaning for r_blocks -- libext2fs has bugs where the FS
* can get corrupted if it totally runs out of blocks. Avoid this
* by refusing to allocate any of the reserve blocks to anybody.
*/
reserved = ext2fs_r_blocks_count(fs->super);
if (reserved == 0)
reserved = ext2fs_blocks_count(fs->super) / 10;
return ext2fs_free_blocks_count(fs->super) > reserved + num;
}
static int fs_writeable(ext2_filsys fs)
{
return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
}
static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct ext2_inode inode;
mode_t perms;
errcode_t err;
/* no writing to read-only or broken fs */
if ((mask & W_OK) && !fs_writeable(fs))
return -EROFS;
err = ext2fs_read_inode(fs, ino, &inode);
if (err)
return translate_error(fs, ino, err);
perms = inode.i_mode & 0777;
dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
"uid=%d gid=%d\n", ino,
(mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
(mask & X_OK ? "x" : ""), perms, inode_uid(inode),
inode_gid(inode), ctxt->uid, ctxt->gid);
/* existence check */
if (mask == 0)
return 0;
/* is immutable? */
if ((mask & W_OK) &&
(inode.i_flags & EXT2_IMMUTABLE_FL))
return -EACCES;
/* Figure out what root's allowed to do */
if (ff->fakeroot || ctxt->uid == 0) {
/* Non-file access always ok */
if (!LINUX_S_ISREG(inode.i_mode))
return 0;
/* R/W access to a file always ok */
if (!(mask & X_OK))
return 0;
/* X access to a file ok if a user/group/other can X */
if (perms & 0111)
return 0;
/* Trying to execute a file that's not executable. BZZT! */
return -EACCES;
}
/* allow owner, if perms match */
if (inode_uid(inode) == ctxt->uid) {
if ((mask & (perms >> 6)) == mask)
return 0;
return -EACCES;
}
/* allow group, if perms match */
if (inode_gid(inode) == ctxt->gid) {
if ((mask & (perms >> 3)) == mask)
return 0;
return -EACCES;
}
/* otherwise check other */
if ((mask & perms) == mask)
return 0;
return -EACCES;
}
static void op_destroy(void *p EXT2FS_ATTR((unused)))
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
if (ff->magic != FUSE2FS_MAGIC) {
translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
return;
}
fs = ff->fs;
dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
if (fs->flags & EXT2_FLAG_RW) {
fs->super->s_state |= EXT2_VALID_FS;
if (fs->super->s_error_count)
fs->super->s_state |= EXT2_ERROR_FS;
ext2fs_mark_super_dirty(fs);
err = ext2fs_set_gdt_csum(fs);
if (err)
translate_error(fs, 0, err);
err = ext2fs_flush2(fs, 0);
if (err)
translate_error(fs, 0, err);
}
}
static void *op_init(struct fuse_conn_info *conn
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_config *cfg EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
if (ff->magic != FUSE2FS_MAGIC) {
translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
return NULL;
}
fs = ff->fs;
dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
#ifdef FUSE_CAP_IOCTL_DIR
conn->want |= FUSE_CAP_IOCTL_DIR;
#endif
if (fs->flags & EXT2_FLAG_RW) {
fs->super->s_mnt_count++;
ext2fs_set_tstamp(fs->super, s_mtime, time(NULL));
fs->super->s_state &= ~EXT2_VALID_FS;
ext2fs_mark_super_dirty(fs);
err = ext2fs_flush2(fs, 0);
if (err)
translate_error(fs, 0, err);
}
return ff;
}
static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
{
struct ext2_inode_large inode;
dev_t fakedev = 0;
errcode_t err;
int ret = 0;
struct timespec tv;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
statbuf->st_dev = fakedev;
statbuf->st_ino = ino;
statbuf->st_mode = inode.i_mode;
statbuf->st_nlink = inode.i_links_count;
statbuf->st_uid = inode_uid(inode);
statbuf->st_gid = inode_gid(inode);
statbuf->st_size = EXT2_I_SIZE(&inode);
statbuf->st_blksize = fs->blocksize;
statbuf->st_blocks = ext2fs_get_stat_i_blocks(fs,
(struct ext2_inode *)&inode);
EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
statbuf->st_atime = tv.tv_sec;
EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
statbuf->st_mtime = tv.tv_sec;
EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
statbuf->st_ctime = tv.tv_sec;
if (LINUX_S_ISCHR(inode.i_mode) ||
LINUX_S_ISBLK(inode.i_mode)) {
if (inode.i_block[0])
statbuf->st_rdev = inode.i_block[0];
else
statbuf->st_rdev = inode.i_block[1];
}
return ret;
}
static int op_getattr(const char *path, struct stat *statbuf
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s\n", __func__, path);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
ret = stat_inode(fs, ino, statbuf);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_readlink(const char *path, char *buf, size_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
struct ext2_inode inode;
unsigned int got;
ext2_file_t file;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s\n", __func__, path);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
err = ext2fs_read_inode(fs, ino, &inode);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
if (!LINUX_S_ISLNK(inode.i_mode)) {
ret = -EINVAL;
goto out;
}
len--;
if (inode.i_size < len)
len = inode.i_size;
if (ext2fs_is_fast_symlink(&inode))
memcpy(buf, (char *)inode.i_block, len);
else {
/* big/inline symlink */
err = ext2fs_file_open(fs, ino, 0, &file);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_file_read(file, buf, len, &got);
if (err || got != len) {
ext2fs_file_close(file);
ret = translate_error(fs, ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(file);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
}
buf[len] = 0;
if (fs_writeable(fs)) {
ret = update_atime(fs, ino);
if (ret)
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_mknod(const char *path, mode_t mode, dev_t dev)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t parent, child;
char *temp_path;
errcode_t err;
char *node_name, a;
int filetype;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode,
(unsigned int)dev);
temp_path = strdup(path);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 2)) {
ret = -ENOSPC;
goto out2;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
*node_name = a;
if (LINUX_S_ISCHR(mode))
filetype = EXT2_FT_CHRDEV;
else if (LINUX_S_ISBLK(mode))
filetype = EXT2_FT_BLKDEV;
else if (LINUX_S_ISFIFO(mode))
filetype = EXT2_FT_FIFO;
else if (LINUX_S_ISSOCK(mode))
filetype = EXT2_FT_SOCK;
else {
ret = -EINVAL;
goto out2;
}
err = ext2fs_new_inode(fs, parent, mode, 0, &child);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child,
node_name, parent);
err = ext2fs_link(fs, parent, node_name, child, filetype);
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_link(fs, parent, node_name, child,
filetype);
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
ret = update_mtime(fs, parent, NULL);
if (ret)
goto out2;
memset(&inode, 0, sizeof(inode));
inode.i_mode = mode;
if (dev & ~0xFFFF)
inode.i_block[1] = dev;
else
inode.i_block[0] = dev;
inode.i_links_count = 1;
inode.i_extra_isize = sizeof(struct ext2_inode_large) -
EXT2_GOOD_OLD_INODE_SIZE;
inode.i_uid = ctxt->uid;
ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
inode.i_gid = ctxt->gid;
ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
inode.i_generation = ff->next_generation++;
init_times(&inode);
err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
ext2fs_inode_alloc_stats2(fs, child, 1, 0);
out2:
pthread_mutex_unlock(&ff->bfl);
out:
free(temp_path);
return ret;
}
static int op_mkdir(const char *path, mode_t mode)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t parent, child;
char *temp_path;
errcode_t err;
char *node_name, a;
struct ext2_inode_large inode;
char *block;
blk64_t blk;
int ret = 0;
mode_t parent_sgid;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
temp_path = strdup(path);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out2;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
/* Is the parent dir sgid? */
err = ext2fs_read_inode_full(fs, parent, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
parent_sgid = inode.i_mode & S_ISGID;
*node_name = a;
err = ext2fs_mkdir(fs, parent, 0, node_name);
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_mkdir(fs, parent, 0, node_name);
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
ret = update_mtime(fs, parent, NULL);
if (ret)
goto out2;
/* Still have to update the uid/gid of the dir */
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&child);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
dbg_printf("%s: created ino=%d/path=%s in dir=%d\n", __func__, child,
node_name, parent);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
inode.i_uid = ctxt->uid;
ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
inode.i_gid = ctxt->gid;
ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
inode.i_mode = LINUX_S_IFDIR | (mode & ~S_ISUID) |
parent_sgid;
inode.i_generation = ff->next_generation++;
init_times(&inode);
err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
/* Rewrite the directory block checksum, having set i_generation */
if ((inode.i_flags & EXT4_INLINE_DATA_FL) ||
!ext2fs_has_feature_metadata_csum(fs->super))
goto out2;
err = ext2fs_new_dir_block(fs, child, parent, &block);
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
err = ext2fs_bmap2(fs, child, (struct ext2_inode *)&inode, NULL, 0, 0,
NULL, &blk);
if (err) {
ret = translate_error(fs, child, err);
goto out3;
}
err = ext2fs_write_dir_block4(fs, blk, block, 0, child);
if (err) {
ret = translate_error(fs, child, err);
goto out3;
}
out3:
ext2fs_free_mem(&block);
out2:
pthread_mutex_unlock(&ff->bfl);
out:
free(temp_path);
return ret;
}
static int unlink_file_by_name(ext2_filsys fs, const char *path)
{
errcode_t err;
ext2_ino_t dir;
char *filename = strdup(path);
char *base_name;
int ret;
base_name = strrchr(filename, '/');
if (base_name) {
*base_name++ = '\0';
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, filename,
&dir);
if (err) {
free(filename);
return translate_error(fs, 0, err);
}
} else {
dir = EXT2_ROOT_INO;
base_name = filename;
}
ret = check_inum_access(fs, dir, W_OK);
if (ret) {
free(filename);
return ret;
}
dbg_printf("%s: unlinking name=%s from dir=%d\n", __func__,
base_name, dir);
err = ext2fs_unlink(fs, dir, base_name, 0, 0);
free(filename);
if (err)
return translate_error(fs, dir, err);
return update_mtime(fs, dir, NULL);
}
static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino)
{
ext2_filsys fs = ff->fs;
errcode_t err;
struct ext2_inode_large inode;
int ret = 0;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
dbg_printf("%s: put ino=%d links=%d\n", __func__, ino,
inode.i_links_count);
switch (inode.i_links_count) {
case 0:
return 0; /* XXX: already done? */
case 1:
inode.i_links_count--;
ext2fs_set_dtime(fs, EXT2_INODE(&inode));
break;
default:
inode.i_links_count--;
}
ret = update_ctime(fs, ino, &inode);
if (ret)
goto out;
if (inode.i_links_count)
goto write_out;
/* Nobody holds this file; free its blocks! */
err = ext2fs_free_ext_attr(fs, ino, &inode);
if (err)
goto write_out;
if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
err = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
0, ~0ULL);
if (err) {
ret = translate_error(fs, ino, err);
goto write_out;
}
}
ext2fs_inode_alloc_stats2(fs, ino, -1,
LINUX_S_ISDIR(inode.i_mode));
write_out:
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
out:
return ret;
}
static int __op_unlink(struct fuse2fs *ff, const char *path)
{
ext2_filsys fs = ff->fs;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
ret = unlink_file_by_name(fs, path);
if (ret)
goto out;
ret = remove_inode(ff, ino);
if (ret)
goto out;
out:
return ret;
}
static int op_unlink(const char *path)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
int ret;
FUSE2FS_CHECK_CONTEXT(ff);
pthread_mutex_lock(&ff->bfl);
ret = __op_unlink(ff, path);
pthread_mutex_unlock(&ff->bfl);
return ret;
}
struct rd_struct {
ext2_ino_t parent;
int empty;
};
static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
int entry EXT2FS_ATTR((unused)),
struct ext2_dir_entry *dirent,
int offset EXT2FS_ATTR((unused)),
int blocksize EXT2FS_ATTR((unused)),
char *buf EXT2FS_ATTR((unused)),
void *private)
{
struct rd_struct *rds = (struct rd_struct *) private;
if (dirent->inode == 0)
return 0;
if (((dirent->name_len & 0xFF) == 1) && (dirent->name[0] == '.'))
return 0;
if (((dirent->name_len & 0xFF) == 2) && (dirent->name[0] == '.') &&
(dirent->name[1] == '.')) {
rds->parent = dirent->inode;
return 0;
}
rds->empty = 0;
return 0;
}
static int __op_rmdir(struct fuse2fs *ff, const char *path)
{
ext2_filsys fs = ff->fs;
ext2_ino_t child;
errcode_t err;
struct ext2_inode_large inode;
struct rd_struct rds;
int ret = 0;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &child);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: rmdir path=%s ino=%d\n", __func__, path, child);
rds.parent = 0;
rds.empty = 1;
err = ext2fs_dir_iterate2(fs, child, 0, 0, rmdir_proc, &rds);
if (err) {
ret = translate_error(fs, child, err);
goto out;
}
if (rds.empty == 0) {
ret = -ENOTEMPTY;
goto out;
}
ret = unlink_file_by_name(fs, path);
if (ret)
goto out;
/* Directories have to be "removed" twice. */
ret = remove_inode(ff, child);
if (ret)
goto out;
ret = remove_inode(ff, child);
if (ret)
goto out;
if (rds.parent) {
dbg_printf("%s: decr dir=%d link count\n", __func__,
rds.parent);
err = ext2fs_read_inode_full(fs, rds.parent,
(struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, rds.parent, err);
goto out;
}
if (inode.i_links_count > 1)
inode.i_links_count--;
ret = update_mtime(fs, rds.parent, &inode);
if (ret)
goto out;
err = ext2fs_write_inode_full(fs, rds.parent,
(struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, rds.parent, err);
goto out;
}
}
out:
return ret;
}
static int op_rmdir(const char *path)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
int ret;
FUSE2FS_CHECK_CONTEXT(ff);
pthread_mutex_lock(&ff->bfl);
ret = __op_rmdir(ff, path);
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_symlink(const char *src, const char *dest)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t parent, child;
char *temp_path;
errcode_t err;
char *node_name, a;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: symlink %s to %s\n", __func__, src, dest);
temp_path = strdup(dest);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
*node_name = a;
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
/* Create symlink */
err = ext2fs_symlink(fs, parent, 0, node_name, src);
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_symlink(fs, parent, 0, node_name, src);
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
/* Update parent dir's mtime */
ret = update_mtime(fs, parent, NULL);
if (ret)
goto out2;
/* Still have to update the uid/gid of the symlink */
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&child);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
dbg_printf("%s: symlinking ino=%d/name=%s to dir=%d\n", __func__,
child, node_name, parent);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
inode.i_uid = ctxt->uid;
ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
inode.i_gid = ctxt->gid;
ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
inode.i_generation = ff->next_generation++;
init_times(&inode);
err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
out2:
pthread_mutex_unlock(&ff->bfl);
out:
free(temp_path);
return ret;
}
struct update_dotdot {
ext2_ino_t new_dotdot;
};
static int update_dotdot_helper(ext2_ino_t dir EXT2FS_ATTR((unused)),
int entry EXT2FS_ATTR((unused)),
struct ext2_dir_entry *dirent,
int offset EXT2FS_ATTR((unused)),
int blocksize EXT2FS_ATTR((unused)),
char *buf EXT2FS_ATTR((unused)),
void *priv_data)
{
struct update_dotdot *ud = priv_data;
if (ext2fs_dirent_name_len(dirent) == 2 &&
dirent->name[0] == '.' && dirent->name[1] == '.') {
dirent->inode = ud->new_dotdot;
return DIRENT_CHANGED | DIRENT_ABORT;
}
return 0;
}
static int op_rename(const char *from, const char *to
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, unsigned int flags EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t from_ino, to_ino, to_dir_ino, from_dir_ino;
char *temp_to = NULL, *temp_from = NULL;
char *cp, a;
struct ext2_inode inode;
struct update_dotdot ud;
int ret = 0;
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
/* renameat2 is not supported */
if (flags)
return -ENOSYS;
#endif
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: renaming %s to %s\n", __func__, from, to);
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 5)) {
ret = -ENOSPC;
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, from, &from_ino);
if (err || from_ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, to, &to_ino);
if (err && err != EXT2_ET_FILE_NOT_FOUND) {
ret = translate_error(fs, 0, err);
goto out;
}
if (err == EXT2_ET_FILE_NOT_FOUND)
to_ino = 0;
/* Already the same file? */
if (to_ino != 0 && to_ino == from_ino) {
ret = 0;
goto out;
}
temp_to = strdup(to);
if (!temp_to) {
ret = -ENOMEM;
goto out;
}
temp_from = strdup(from);
if (!temp_from) {
ret = -ENOMEM;
goto out2;
}
/* Find parent dir of the source and check write access */
cp = strrchr(temp_from, '/');
if (!cp) {
ret = -EINVAL;
goto out2;
}
a = *(cp + 1);
*(cp + 1) = 0;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_from,
&from_dir_ino);
*(cp + 1) = a;
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
if (from_dir_ino == 0) {
ret = -ENOENT;
goto out2;
}
ret = check_inum_access(fs, from_dir_ino, W_OK);
if (ret)
goto out2;
/* Find parent dir of the destination and check write access */
cp = strrchr(temp_to, '/');
if (!cp) {
ret = -EINVAL;
goto out2;
}
a = *(cp + 1);
*(cp + 1) = 0;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_to,
&to_dir_ino);
*(cp + 1) = a;
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
if (to_dir_ino == 0) {
ret = -ENOENT;
goto out2;
}
ret = check_inum_access(fs, to_dir_ino, W_OK);
if (ret)
goto out2;
/* If the target exists, unlink it first */
if (to_ino != 0) {
err = ext2fs_read_inode(fs, to_ino, &inode);
if (err) {
ret = translate_error(fs, to_ino, err);
goto out2;
}
dbg_printf("%s: unlinking %s ino=%d\n", __func__,
LINUX_S_ISDIR(inode.i_mode) ? "dir" : "file",
to_ino);
if (LINUX_S_ISDIR(inode.i_mode))
ret = __op_rmdir(ff, to);
else
ret = __op_unlink(ff, to);
if (ret)
goto out2;
}
/* Get ready to do the move */
err = ext2fs_read_inode(fs, from_ino, &inode);
if (err) {
ret = translate_error(fs, from_ino, err);
goto out2;
}
/* Link in the new file */
dbg_printf("%s: linking ino=%d/path=%s to dir=%d\n", __func__,
from_ino, cp + 1, to_dir_ino);
err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
ext2_file_type(inode.i_mode));
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, to_dir_ino);
if (err) {
ret = translate_error(fs, to_dir_ino, err);
goto out2;
}
err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
ext2_file_type(inode.i_mode));
}
if (err) {
ret = translate_error(fs, to_dir_ino, err);
goto out2;
}
/* Update '..' pointer if dir */
err = ext2fs_read_inode(fs, from_ino, &inode);
if (err) {
ret = translate_error(fs, from_ino, err);
goto out2;
}
if (LINUX_S_ISDIR(inode.i_mode)) {
ud.new_dotdot = to_dir_ino;
dbg_printf("%s: updating .. entry for dir=%d\n", __func__,
to_dir_ino);
err = ext2fs_dir_iterate2(fs, from_ino, 0, NULL,
update_dotdot_helper, &ud);
if (err) {
ret = translate_error(fs, from_ino, err);
goto out2;
}
/* Decrease from_dir_ino's links_count */
dbg_printf("%s: moving linkcount from dir=%d to dir=%d\n",
__func__, from_dir_ino, to_dir_ino);
err = ext2fs_read_inode(fs, from_dir_ino, &inode);
if (err) {
ret = translate_error(fs, from_dir_ino, err);
goto out2;
}
inode.i_links_count--;
err = ext2fs_write_inode(fs, from_dir_ino, &inode);
if (err) {
ret = translate_error(fs, from_dir_ino, err);
goto out2;
}
/* Increase to_dir_ino's links_count */
err = ext2fs_read_inode(fs, to_dir_ino, &inode);
if (err) {
ret = translate_error(fs, to_dir_ino, err);
goto out2;
}
inode.i_links_count++;
err = ext2fs_write_inode(fs, to_dir_ino, &inode);
if (err) {
ret = translate_error(fs, to_dir_ino, err);
goto out2;
}
}
/* Update timestamps */
ret = update_ctime(fs, from_ino, NULL);
if (ret)
goto out2;
ret = update_mtime(fs, to_dir_ino, NULL);
if (ret)
goto out2;
/* Remove the old file */
ret = unlink_file_by_name(fs, from);
if (ret)
goto out2;
/* Flush the whole mess out */
err = ext2fs_flush2(fs, 0);
if (err)
ret = translate_error(fs, 0, err);
out2:
free(temp_from);
free(temp_to);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_link(const char *src, const char *dest)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
char *temp_path;
errcode_t err;
char *node_name, a;
ext2_ino_t parent, ino;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: src=%s dest=%s\n", __func__, src, dest);
temp_path = strdup(dest);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 2)) {
ret = -ENOSPC;
goto out2;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
*node_name = a;
if (err) {
err = -ENOENT;
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, src, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out2;
}
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
inode.i_links_count++;
ret = update_ctime(fs, ino, &inode);
if (ret)
goto out2;
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
dbg_printf("%s: linking ino=%d/name=%s to dir=%d\n", __func__, ino,
node_name, parent);
err = ext2fs_link(fs, parent, node_name, ino,
ext2_file_type(inode.i_mode));
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_link(fs, parent, node_name, ino,
ext2_file_type(inode.i_mode));
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
ret = update_mtime(fs, parent, NULL);
if (ret)
goto out2;
out2:
pthread_mutex_unlock(&ff->bfl);
out:
free(temp_path);
return ret;
}
static int op_chmod(const char *path, mode_t mode
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: path=%s mode=0%o ino=%d\n", __func__, path, mode, ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
if (!ff->fakeroot && ctxt->uid != 0 && ctxt->uid != inode_uid(inode)) {
ret = -EPERM;
goto out;
}
/*
* XXX: We should really check that the inode gid is not in /any/
* of the user's groups, but FUSE only tells us about the primary
* group.
*/
if (!ff->fakeroot && ctxt->uid != 0 && ctxt->gid != inode_gid(inode))
mode &= ~S_ISGID;
inode.i_mode &= ~0xFFF;
inode.i_mode |= mode & 0xFFF;
ret = update_ctime(fs, ino, &inode);
if (ret)
goto out;
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_chown(const char *path, uid_t owner, gid_t group
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: path=%s owner=%d group=%d ino=%d\n", __func__,
path, owner, group, ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
/* FUSE seems to feed us ~0 to mean "don't change" */
if (owner != (uid_t) ~0) {
/* Only root gets to change UID. */
if (!ff->fakeroot && ctxt->uid != 0 &&
!(inode_uid(inode) == ctxt->uid && owner == ctxt->uid)) {
ret = -EPERM;
goto out;
}
inode.i_uid = owner;
ext2fs_set_i_uid_high(inode, owner >> 16);
}
if (group != (gid_t) ~0) {
/* Only root or the owner get to change GID. */
if (!ff->fakeroot && ctxt->uid != 0 &&
inode_uid(inode) != ctxt->uid) {
ret = -EPERM;
goto out;
}
/* XXX: We /should/ check group membership but FUSE */
inode.i_gid = group;
ext2fs_set_i_gid_high(inode, group >> 16);
}
ret = update_ctime(fs, ino, &inode);
if (ret)
goto out;
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_truncate(const char *path, off_t len
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
ext2_file_t file;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d len=%jd\n", __func__, ino, len);
ret = check_inum_access(fs, ino, W_OK);
if (ret)
goto out;
err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_file_set_size2(file, len);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(file);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
ret = update_mtime(fs, ino, NULL);
out:
pthread_mutex_unlock(&ff->bfl);
return err;
}
#ifdef __linux__
static void detect_linux_executable_open(int kernel_flags, int *access_check,
int *e2fs_open_flags)
{
/*
* On Linux, execve will bleed __FMODE_EXEC into the file mode flags,
* and FUSE is more than happy to let that slip through.
*/
if (kernel_flags & 0x20) {
*access_check = X_OK;
*e2fs_open_flags &= ~EXT2_FILE_WRITE;
}
}
#else
static void detect_linux_executable_open(int kernel_flags, int *access_check,
int *e2fs_open_flags)
{
/* empty */
}
#endif /* __linux__ */
static int __op_open(struct fuse2fs *ff, const char *path,
struct fuse_file_info *fp)
{
ext2_filsys fs = ff->fs;
errcode_t err;
struct fuse2fs_file_handle *file;
int check = 0, ret = 0;
dbg_printf("%s: path=%s\n", __func__, path);
err = ext2fs_get_mem(sizeof(*file), &file);
if (err)
return translate_error(fs, 0, err);
file->magic = FUSE2FS_FILE_MAGIC;
file->open_flags = 0;
switch (fp->flags & O_ACCMODE) {
case O_RDONLY:
check = R_OK;
break;
case O_WRONLY:
check = W_OK;
file->open_flags |= EXT2_FILE_WRITE;
break;
case O_RDWR:
check = R_OK | W_OK;
file->open_flags |= EXT2_FILE_WRITE;
break;
}
detect_linux_executable_open(fp->flags, &check, &file->open_flags);
if (fp->flags & O_CREAT)
file->open_flags |= EXT2_FILE_CREATE;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &file->ino);
if (err || file->ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d\n", __func__, file->ino);
ret = check_inum_access(fs, file->ino, check);
if (ret) {
/*
* In a regular (Linux) fs driver, the kernel will open
* binaries for reading if the user has --x privileges (i.e.
* execute without read). Since the kernel doesn't have any
* way to tell us if it's opening a file via execve, we'll
* just assume that allowing access is ok if asking for ro mode
* fails but asking for x mode succeeds. Of course we can
* also employ undocumented hacks (see above).
*/
if (check == R_OK) {
ret = check_inum_access(fs, file->ino, X_OK);
if (ret)
goto out;
} else
goto out;
}
fp->fh = (uintptr_t)file;
out:
if (ret)
ext2fs_free_mem(&file);
return ret;
}
static int op_open(const char *path, struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
int ret;
FUSE2FS_CHECK_CONTEXT(ff);
pthread_mutex_lock(&ff->bfl);
ret = __op_open(ff, path, fp);
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
size_t len, off_t offset,
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
ext2_file_t efp;
errcode_t err;
unsigned int got = 0;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
len);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out2;
}
err = ext2fs_file_read(efp, buf, len, &got);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(efp);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
if (fs_writeable(fs)) {
ret = update_atime(fs, fh->ino);
if (ret)
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return got ? (int) got : ret;
}
static int op_write(const char *path EXT2FS_ATTR((unused)),
const char *buf, size_t len, off_t offset,
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
ext2_file_t efp;
errcode_t err;
unsigned int got = 0;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
len);
pthread_mutex_lock(&ff->bfl);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;
}
if (!fs_can_allocate(ff, len / fs->blocksize)) {
ret = -ENOSPC;
goto out;
}
err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out2;
}
err = ext2fs_file_write(efp, buf, len, &got);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out2;
}
err = ext2fs_file_flush(efp);
if (err) {
got = 0;
ret = translate_error(fs, fh->ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(efp);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
ret = update_mtime(fs, fh->ino, NULL);
if (ret)
goto out;
out:
pthread_mutex_unlock(&ff->bfl);
return got ? (int) got : ret;
}
static int op_release(const char *path EXT2FS_ATTR((unused)),
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
pthread_mutex_lock(&ff->bfl);
if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC);
if (err)
ret = translate_error(fs, fh->ino, err);
}
fp->fh = 0;
pthread_mutex_unlock(&ff->bfl);
ext2fs_free_mem(&fh);
return ret;
}
static int op_fsync(const char *path EXT2FS_ATTR((unused)),
int datasync EXT2FS_ATTR((unused)),
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
/* For now, flush everything, even if it's slow */
pthread_mutex_lock(&ff->bfl);
if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
err = ext2fs_flush2(fs, 0);
if (err)
ret = translate_error(fs, fh->ino, err);
}
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_statfs(const char *path EXT2FS_ATTR((unused)),
struct statvfs *buf)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
uint64_t fsid, *f;
blk64_t overhead, reserved, free;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s\n", __func__, path);
buf->f_bsize = fs->blocksize;
buf->f_frsize = 0;
if (ff->minixdf)
overhead = 0;
else
overhead = fs->desc_blocks +
(blk64_t)fs->group_desc_count *
(fs->inode_blocks_per_group + 2);
reserved = ext2fs_r_blocks_count(fs->super);
if (!reserved)
reserved = ext2fs_blocks_count(fs->super) / 10;
free = ext2fs_free_blocks_count(fs->super);
buf->f_blocks = ext2fs_blocks_count(fs->super) - overhead;
buf->f_bfree = free;
if (free < reserved)
buf->f_bavail = 0;
else
buf->f_bavail = free - reserved;
buf->f_files = fs->super->s_inodes_count;
buf->f_ffree = fs->super->s_free_inodes_count;
buf->f_favail = fs->super->s_free_inodes_count;
f = (uint64_t *)fs->super->s_uuid;
fsid = *f;
f++;
fsid ^= *f;
buf->f_fsid = fsid;
buf->f_flag = 0;
if (fs->flags & EXT2_FLAG_RW)
buf->f_flag |= ST_RDONLY;
buf->f_namemax = EXT2_NAME_LEN;
return 0;
}
static int op_getxattr(const char *path, const char *key, char *value,
size_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
struct ext2_xattr_handle *h;
void *ptr;
size_t plen;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d\n", __func__, ino);
ret = check_inum_access(fs, ino, R_OK);
if (ret)
goto out;
err = ext2fs_xattrs_open(fs, ino, &h);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_xattrs_read(h);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
err = ext2fs_xattr_get(h, key, &ptr, &plen);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
if (!len) {
ret = plen;
} else if (len < plen) {
ret = -ERANGE;
} else {
memcpy(value, ptr, plen);
ret = plen;
}
ext2fs_free_mem(&ptr);
out2:
err = ext2fs_xattrs_close(&h);
if (err)
ret = translate_error(fs, ino, err);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int count_buffer_space(char *name, char *value EXT2FS_ATTR((unused)),
size_t value_len EXT2FS_ATTR((unused)),
void *data)
{
unsigned int *x = data;
*x = *x + strlen(name) + 1;
return 0;
}
static int copy_names(char *name, char *value EXT2FS_ATTR((unused)),
size_t value_len EXT2FS_ATTR((unused)), void *data)
{
char **b = data;
size_t name_len = strlen(name);
memcpy(*b, name, name_len + 1);
*b = *b + name_len + 1;
return 0;
}
static int op_listxattr(const char *path, char *names, size_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
struct ext2_xattr_handle *h;
unsigned int bufsz;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, ino, err);
goto out;
}
dbg_printf("%s: ino=%d\n", __func__, ino);
ret = check_inum_access(fs, ino, R_OK);
if (ret)
goto out;
err = ext2fs_xattrs_open(fs, ino, &h);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_xattrs_read(h);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
/* Count buffer space needed for names */
bufsz = 0;
err = ext2fs_xattrs_iterate(h, count_buffer_space, &bufsz);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
if (len == 0) {
ret = bufsz;
goto out2;
} else if (len < bufsz) {
ret = -ERANGE;
goto out2;
}
/* Copy names out */
memset(names, 0, len);
err = ext2fs_xattrs_iterate(h, copy_names, &names);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
ret = bufsz;
out2:
err = ext2fs_xattrs_close(&h);
if (err)
ret = translate_error(fs, ino, err);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
const char *key, const char *value,
size_t len, int flags EXT2FS_ATTR((unused)))
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
struct ext2_xattr_handle *h;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d\n", __func__, ino);
ret = check_inum_access(fs, ino, W_OK);
if (ret == -EACCES) {
ret = -EPERM;
goto out;
} else if (ret)
goto out;
err = ext2fs_xattrs_open(fs, ino, &h);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_xattrs_read(h);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
err = ext2fs_xattr_set(h, key, value, len);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
ret = update_ctime(fs, ino, NULL);
out2:
err = ext2fs_xattrs_close(&h);
if (!ret && err)
ret = translate_error(fs, ino, err);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_removexattr(const char *path, const char *key)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
struct ext2_xattr_handle *h;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
}
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d\n", __func__, ino);
ret = check_inum_access(fs, ino, W_OK);
if (ret)
goto out;
err = ext2fs_xattrs_open(fs, ino, &h);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_xattrs_read(h);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
err = ext2fs_xattr_remove(h, key);
if (err) {
ret = translate_error(fs, ino, err);
goto out2;
}
ret = update_ctime(fs, ino, NULL);
out2:
err = ext2fs_xattrs_close(&h);
if (err)
ret = translate_error(fs, ino, err);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
struct readdir_iter {
void *buf;
fuse_fill_dir_t func;
};
static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
int entry EXT2FS_ATTR((unused)),
struct ext2_dir_entry *dirent,
int offset EXT2FS_ATTR((unused)),
int blocksize EXT2FS_ATTR((unused)),
char *buf EXT2FS_ATTR((unused)), void *data)
{
struct readdir_iter *i = data;
char namebuf[EXT2_NAME_LEN + 1];
int ret;
memcpy(namebuf, dirent->name, dirent->name_len & 0xFF);
namebuf[dirent->name_len & 0xFF] = 0;
ret = i->func(i->buf, namebuf, NULL, 0
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, 0
#endif
);
if (ret)
return DIRENT_ABORT;
return 0;
}
static int op_readdir(const char *path EXT2FS_ATTR((unused)),
void *buf, fuse_fill_dir_t fill_func,
off_t offset EXT2FS_ATTR((unused)),
struct fuse_file_info *fp
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, enum fuse_readdir_flags flags EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
errcode_t err;
struct readdir_iter i;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
pthread_mutex_lock(&ff->bfl);
i.buf = buf;
i.func = fill_func;
err = ext2fs_dir_iterate2(fs, fh->ino, 0, NULL, op_readdir_iter, &i);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
if (fs_writeable(fs)) {
ret = update_atime(fs, fh->ino);
if (ret)
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_access(const char *path, int mask)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s mask=0x%x\n", __func__, path, mask);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
ret = check_inum_access(fs, ino, mask);
if (ret)
goto out;
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t parent, child;
char *temp_path;
errcode_t err;
char *node_name, a;
int filetype;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
temp_path = strdup(path);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out2;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
*node_name = a;
filetype = ext2_file_type(mode);
err = ext2fs_new_inode(fs, parent, mode, 0, &child);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
dbg_printf("%s: creating ino=%d/name=%s in dir=%d\n", __func__, child,
node_name, parent);
err = ext2fs_link(fs, parent, node_name, child, filetype);
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_link(fs, parent, node_name, child,
filetype);
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
ret = update_mtime(fs, parent, NULL);
if (ret)
goto out2;
memset(&inode, 0, sizeof(inode));
inode.i_mode = mode;
inode.i_links_count = 1;
inode.i_extra_isize = sizeof(struct ext2_inode_large) -
EXT2_GOOD_OLD_INODE_SIZE;
inode.i_uid = ctxt->uid;
ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
inode.i_gid = ctxt->gid;
ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
if (ext2fs_has_feature_extents(fs->super)) {
ext2_extent_handle_t handle;
inode.i_flags &= ~EXT4_EXTENTS_FL;
ret = ext2fs_extent_open2(fs, child,
(struct ext2_inode *)&inode, &handle);
if (ret)
return ret;
ext2fs_extent_free(handle);
}
err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
inode.i_generation = ff->next_generation++;
init_times(&inode);
err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, child, err);
goto out2;
}
ext2fs_inode_alloc_stats2(fs, child, 1, 0);
ret = __op_open(ff, path, fp);
if (ret)
goto out2;
out2:
pthread_mutex_unlock(&ff->bfl);
out:
free(temp_path);
return ret;
}
#if FUSE_VERSION < FUSE_MAKE_VERSION(3, 0)
static int op_ftruncate(const char *path EXT2FS_ATTR((unused)),
off_t len, struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
ext2_file_t efp;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d len=%jd\n", __func__, fh->ino, len);
pthread_mutex_lock(&ff->bfl);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;
}
err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
err = ext2fs_file_set_size2(efp, len);
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(efp);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, fh->ino, err);
goto out;
}
ret = update_mtime(fs, fh->ino, NULL);
if (ret)
goto out;
out:
pthread_mutex_unlock(&ff->bfl);
return 0;
}
static int op_fgetattr(const char *path EXT2FS_ATTR((unused)),
struct stat *statbuf,
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
pthread_mutex_lock(&ff->bfl);
ret = stat_inode(fs, fh->ino, statbuf);
pthread_mutex_unlock(&ff->bfl);
return ret;
}
#endif /* FUSE_VERSION < FUSE_MAKE_VERSION(3, 0) */
static int op_utimens(const char *path, const struct timespec ctv[2]
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct timespec tv[2];
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d atime=%lld.%ld mtime=%lld.%ld\n", __func__,
ino,
(long long int)ctv[0].tv_sec, ctv[0].tv_nsec,
(long long int)ctv[1].tv_sec, ctv[1].tv_nsec);
ret = check_inum_access(fs, ino, W_OK);
if (ret)
goto out;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
tv[0] = ctv[0];
tv[1] = ctv[1];
#ifdef UTIME_NOW
if (tv[0].tv_nsec == UTIME_NOW)
get_now(tv);
if (tv[1].tv_nsec == UTIME_NOW)
get_now(tv + 1);
#endif /* UTIME_NOW */
#ifdef UTIME_OMIT
if (tv[0].tv_nsec != UTIME_OMIT)
EXT4_INODE_SET_XTIME(i_atime, &tv[0], &inode);
if (tv[1].tv_nsec != UTIME_OMIT)
EXT4_INODE_SET_XTIME(i_mtime, &tv[1], &inode);
#endif /* UTIME_OMIT */
ret = update_ctime(fs, ino, &inode);
if (ret)
goto out;
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
#ifdef SUPPORT_I_FLAGS
static int ioctl_getflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
void *data)
{
errcode_t err;
struct ext2_inode_large inode;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
*(__u32 *)data = inode.i_flags & EXT2_FL_USER_VISIBLE;
return 0;
}
#define FUSE2FS_MODIFIABLE_IFLAGS \
(EXT2_IMMUTABLE_FL | EXT2_APPEND_FL | EXT2_NODUMP_FL | \
EXT2_NOATIME_FL | EXT3_JOURNAL_DATA_FL | EXT2_DIRSYNC_FL | \
EXT2_TOPDIR_FL)
static int ioctl_setflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
void *data)
{
errcode_t err;
struct ext2_inode_large inode;
int ret;
__u32 flags = *(__u32 *)data;
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
return -EPERM;
if ((inode.i_flags ^ flags) & ~FUSE2FS_MODIFIABLE_IFLAGS)
return -EINVAL;
inode.i_flags = (inode.i_flags & ~FUSE2FS_MODIFIABLE_IFLAGS) |
(flags & FUSE2FS_MODIFIABLE_IFLAGS);
ret = update_ctime(fs, fh->ino, &inode);
if (ret)
return ret;
err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
return 0;
}
static int ioctl_getversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
void *data)
{
errcode_t err;
struct ext2_inode_large inode;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
*(__u32 *)data = inode.i_generation;
return 0;
}
static int ioctl_setversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
void *data)
{
errcode_t err;
struct ext2_inode_large inode;
int ret;
__u32 generation = *(__u32 *)data;
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: ino=%d\n", __func__, fh->ino);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
return -EPERM;
inode.i_generation = generation;
ret = update_ctime(fs, fh->ino, &inode);
if (ret)
return ret;
err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
return 0;
}
#endif /* SUPPORT_I_FLAGS */
#ifdef FITRIM
static int ioctl_fitrim(ext2_filsys fs, struct fuse2fs_file_handle *fh,
void *data)
{
struct fstrim_range *fr = data;
blk64_t start, end, max_blocks, b, cleared;
errcode_t err = 0;
start = fr->start / fs->blocksize;
end = (fr->start + fr->len - 1) / fs->blocksize;
dbg_printf("%s: start=%llu end=%llu\n", __func__, start, end);
if (start < fs->super->s_first_data_block)
start = fs->super->s_first_data_block;
if (start >= ext2fs_blocks_count(fs->super))
start = ext2fs_blocks_count(fs->super) - 1;
if (end < fs->super->s_first_data_block)
end = fs->super->s_first_data_block;
if (end >= ext2fs_blocks_count(fs->super))
end = ext2fs_blocks_count(fs->super) - 1;
cleared = 0;
max_blocks = 2048ULL * 1024 * 1024 / fs->blocksize;
fr->len = 0;
while (start <= end) {
err = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
start, end, &start);
if (err == ENOENT)
return 0;
else if (err)
return translate_error(fs, fh->ino, err);
b = start + max_blocks < end ? start + max_blocks : end;
err = ext2fs_find_first_set_block_bitmap2(fs->block_map,
start, b, &b);
if (err && err != ENOENT)
return translate_error(fs, fh->ino, err);
if (b - start >= fr->minlen) {
err = io_channel_discard(fs->io, start, b - start);
if (err)
return translate_error(fs, fh->ino, err);
cleared += b - start;
fr->len = cleared * fs->blocksize;
}
start = b + 1;
}
return err;
}
#endif /* FITRIM */
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
static int op_ioctl(const char *path EXT2FS_ATTR((unused)),
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
unsigned int cmd,
#else
int cmd,
#endif
void *arg EXT2FS_ATTR((unused)),
struct fuse_file_info *fp,
unsigned int flags EXT2FS_ATTR((unused)), void *data)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
switch ((unsigned long) cmd) {
#ifdef SUPPORT_I_FLAGS
case EXT2_IOC_GETFLAGS:
ret = ioctl_getflags(fs, fh, data);
break;
case EXT2_IOC_SETFLAGS:
ret = ioctl_setflags(fs, fh, data);
break;
case EXT2_IOC_GETVERSION:
ret = ioctl_getversion(fs, fh, data);
break;
case EXT2_IOC_SETVERSION:
ret = ioctl_setversion(fs, fh, data);
break;
#endif
#ifdef FITRIM
case FITRIM:
ret = ioctl_fitrim(fs, fh, data);
break;
#endif
default:
dbg_printf("%s: Unknown ioctl %d\n", __func__, cmd);
ret = -ENOTTY;
}
pthread_mutex_unlock(&ff->bfl);
return ret;
}
#endif /* FUSE 28 */
static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
uint64_t *idx)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
dbg_printf("%s: ino=%d blk=%"PRIu64"\n", __func__, ino, *idx);
err = ext2fs_bmap2(fs, ino, NULL, NULL, 0, *idx, 0, (blk64_t *)idx);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
# ifdef SUPPORT_FALLOCATE
static int fallocate_helper(struct fuse_file_info *fp, int mode, off_t offset,
off_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
struct ext2_inode_large inode;
blk64_t start, end;
__u64 fsize;
errcode_t err;
int flags;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
start = offset / fs->blocksize;
end = (offset + len - 1) / fs->blocksize;
dbg_printf("%s: ino=%d mode=0x%x start=%jd end=%llu\n", __func__,
fh->ino, mode, offset / fs->blocksize, end);
if (!fs_can_allocate(ff, len / fs->blocksize))
return -ENOSPC;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return err;
fsize = EXT2_I_SIZE(&inode);
/* Allocate a bunch of blocks */
flags = (mode & FL_KEEP_SIZE_FLAG ? 0 :
EXT2_FALLOCATE_INIT_BEYOND_EOF);
err = ext2fs_fallocate(fs, flags, fh->ino,
(struct ext2_inode *)&inode,
~0ULL, start, end - start + 1);
if (err && err != EXT2_ET_BLOCK_ALLOC_FAIL)
return translate_error(fs, fh->ino, err);
/* Update i_size */
if (!(mode & FL_KEEP_SIZE_FLAG)) {
if ((__u64) offset + len > fsize) {
err = ext2fs_inode_size_set(fs,
(struct ext2_inode *)&inode,
offset + len);
if (err)
return translate_error(fs, fh->ino, err);
}
}
err = update_mtime(fs, fh->ino, &inode);
if (err)
return err;
err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
return err;
}
static errcode_t clean_block_middle(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *inode, off_t offset,
off_t len, char **buf)
{
blk64_t blk;
off_t residue;
int retflags;
errcode_t err;
residue = offset % fs->blocksize;
if (residue == 0)
return 0;
if (!*buf) {
err = ext2fs_get_mem(fs->blocksize, buf);
if (err)
return err;
}
err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
offset / fs->blocksize, &retflags, &blk);
if (err)
return err;
if (!blk || (retflags & BMAP_RET_UNINIT))
return 0;
err = io_channel_read_blk(fs->io, blk, 1, *buf);
if (err)
return err;
memset(*buf + residue, 0, len);
return io_channel_write_blk(fs->io, blk, 1, *buf);
}
static errcode_t clean_block_edge(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *inode, off_t offset,
int clean_before, char **buf)
{
blk64_t blk;
int retflags;
off_t residue;
errcode_t err;
residue = offset % fs->blocksize;
if (residue == 0)
return 0;
if (!*buf) {
err = ext2fs_get_mem(fs->blocksize, buf);
if (err)
return err;
}
err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
offset / fs->blocksize, &retflags, &blk);
if (err)
return err;
err = io_channel_read_blk(fs->io, blk, 1, *buf);
if (err)
return err;
if (!blk || (retflags & BMAP_RET_UNINIT))
return 0;
if (clean_before)
memset(*buf, 0, residue);
else
memset(*buf + residue, 0, fs->blocksize - residue);
return io_channel_write_blk(fs->io, blk, 1, *buf);
}
static int punch_helper(struct fuse_file_info *fp, int mode, off_t offset,
off_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct fuse2fs_file_handle *fh =
(struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
ext2_filsys fs;
struct ext2_inode_large inode;
blk64_t start, end;
errcode_t err;
char *buf = NULL;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
dbg_printf("%s: offset=%jd len=%jd\n", __func__, offset, len);
/* kernel ext4 punch requires this flag to be set */
if (!(mode & FL_KEEP_SIZE_FLAG))
return -EINVAL;
/* Punch out a bunch of blocks */
start = (offset + fs->blocksize - 1) / fs->blocksize;
end = (offset + len - fs->blocksize) / fs->blocksize;
dbg_printf("%s: ino=%d mode=0x%x start=%llu end=%llu\n", __func__,
fh->ino, mode, start, end);
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
/* Zero everything before the first block and after the last block */
if ((offset / fs->blocksize) == ((offset + len) / fs->blocksize))
err = clean_block_middle(fs, fh->ino, &inode, offset,
len, &buf);
else {
err = clean_block_edge(fs, fh->ino, &inode, offset, 0, &buf);
if (!err)
err = clean_block_edge(fs, fh->ino, &inode,
offset + len, 1, &buf);
}
if (buf)
ext2fs_free_mem(&buf);
if (err)
return translate_error(fs, fh->ino, err);
/* Unmap full blocks in the middle */
if (start <= end) {
err = ext2fs_punch(fs, fh->ino, (struct ext2_inode *)&inode,
NULL, start, end);
if (err)
return translate_error(fs, fh->ino, err);
}
err = update_mtime(fs, fh->ino, &inode);
if (err)
return err;
err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, fh->ino, err);
return 0;
}
static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,
off_t offset, off_t len,
struct fuse_file_info *fp)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs = ff->fs;
int ret;
/* Catch unknown flags */
if (mode & ~(FL_PUNCH_HOLE_FLAG | FL_KEEP_SIZE_FLAG))
return -EOPNOTSUPP;
pthread_mutex_lock(&ff->bfl);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;
}
if (mode & FL_PUNCH_HOLE_FLAG)
ret = punch_helper(fp, mode, offset, len);
else
ret = fallocate_helper(fp, mode, offset, len);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
# endif /* SUPPORT_FALLOCATE */
#endif /* FUSE 29 */
static struct fuse_operations fs_ops = {
.init = op_init,
.destroy = op_destroy,
.getattr = op_getattr,
.readlink = op_readlink,
.mknod = op_mknod,
.mkdir = op_mkdir,
.unlink = op_unlink,
.rmdir = op_rmdir,
.symlink = op_symlink,
.rename = op_rename,
.link = op_link,
.chmod = op_chmod,
.chown = op_chown,
.truncate = op_truncate,
.open = op_open,
.read = op_read,
.write = op_write,
.statfs = op_statfs,
.release = op_release,
.fsync = op_fsync,
.setxattr = op_setxattr,
.getxattr = op_getxattr,
.listxattr = op_listxattr,
.removexattr = op_removexattr,
.opendir = op_open,
.readdir = op_readdir,
.releasedir = op_release,
.fsyncdir = op_fsync,
.access = op_access,
.create = op_create,
#if FUSE_VERSION < FUSE_MAKE_VERSION(3, 0)
.ftruncate = op_ftruncate,
.fgetattr = op_fgetattr,
#endif
.utimens = op_utimens,
#if (FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)) && (FUSE_VERSION < FUSE_MAKE_VERSION(3, 0))
# if defined(UTIME_NOW) || defined(UTIME_OMIT)
.flag_utime_omit_ok = 1,
# endif
#endif
.bmap = op_bmap,
#ifdef SUPERFLUOUS
.lock = op_lock,
.poll = op_poll,
#endif
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
.ioctl = op_ioctl,
#if FUSE_VERSION < FUSE_MAKE_VERSION(3, 0)
.flag_nullpath_ok = 1,
#endif
#endif
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
#if FUSE_VERSION < FUSE_MAKE_VERSION(3, 0)
.flag_nopath = 1,
#endif
# ifdef SUPPORT_FALLOCATE
.fallocate = op_fallocate,
# endif
#endif
};
static int get_random_bytes(void *p, size_t sz)
{
int fd;
ssize_t r;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
perror("/dev/urandom");
return 0;
}
r = read(fd, p, sz);
close(fd);
return (size_t) r == sz;
}
enum {
FUSE2FS_VERSION,
FUSE2FS_HELP,
FUSE2FS_HELPFULL,
};
#define FUSE2FS_OPT(t, p, v) { t, offsetof(struct fuse2fs, p), v }
static struct fuse_opt fuse2fs_opts[] = {
FUSE2FS_OPT("ro", ro, 1),
FUSE2FS_OPT("errors=panic", panic_on_error, 1),
FUSE2FS_OPT("minixdf", minixdf, 1),
FUSE2FS_OPT("fakeroot", fakeroot, 1),
FUSE2FS_OPT("fuse2fs_debug", debug, 1),
FUSE2FS_OPT("no_default_opts", no_default_opts, 1),
FUSE2FS_OPT("norecovery", norecovery, 1),
FUSE2FS_OPT("offset=%lu", offset, 0),
FUSE_OPT_KEY("-V", FUSE2FS_VERSION),
FUSE_OPT_KEY("--version", FUSE2FS_VERSION),
FUSE_OPT_KEY("-h", FUSE2FS_HELP),
FUSE_OPT_KEY("--help", FUSE2FS_HELP),
FUSE_OPT_KEY("--helpfull", FUSE2FS_HELPFULL),
FUSE_OPT_END
};
static int fuse2fs_opt_proc(void *data, const char *arg,
int key, struct fuse_args *outargs)
{
struct fuse2fs *ff = data;
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (!ff->device) {
ff->device = strdup(arg);
return 0;
}
return 1;
case FUSE2FS_HELP:
case FUSE2FS_HELPFULL:
fprintf(stderr,
"usage: %s device/image mountpoint [options]\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
"fuse2fs options:\n"
" -o ro read-only mount\n"
" -o errors=panic dump core on error\n"
" -o minixdf minix-style df\n"
" -o fakeroot pretend to be root for permission checks\n"
" -o no_default_opts do not include default fuse options\n"
" -o offset=<bytes> similar to mount -o offset=<bytes>, mount the partition starting at <bytes>\n"
" -o norecovery don't replay the journal (implies ro)\n"
" -o fuse2fs_debug enable fuse2fs debugging\n"
"\n",
outargs->argv[0]);
if (key == FUSE2FS_HELPFULL) {
fuse_opt_add_arg(outargs, "-h");
fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
} else {
fprintf(stderr, "Try --helpfull to get a list of "
"all flags, including the FUSE options.\n");
}
exit(1);
case FUSE2FS_VERSION:
fprintf(stderr, "fuse2fs %s (%s)\n", E2FSPROGS_VERSION,
E2FSPROGS_DATE);
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
exit(0);
}
return 1;
}
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fuse2fs fctx;
errcode_t err;
char *logfile;
char extra_args[BUFSIZ];
int ret = 0;
int flags = EXT2_FLAG_64BITS | EXT2_FLAG_THREADS | EXT2_FLAG_EXCLUSIVE;
memset(&fctx, 0, sizeof(fctx));
fctx.magic = FUSE2FS_MAGIC;
fuse_opt_parse(&args, &fctx, fuse2fs_opts, fuse2fs_opt_proc);
if (fctx.device == NULL) {
fprintf(stderr, "Missing ext4 device/image\n");
fprintf(stderr, "See '%s -h' for usage\n", argv[0]);
exit(1);
}
if (fctx.norecovery)
fctx.ro = 1;
if (fctx.ro)
printf("%s", _("Mounting read-only.\n"));
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
set_com_err_gettext(gettext);
#endif
add_error_table(&et_ext2_error_table);
/* Set up error logging */
logfile = getenv("FUSE2FS_LOGFILE");
if (logfile) {
fctx.err_fp = fopen(logfile, "a");
if (!fctx.err_fp) {
perror(logfile);
goto out;
}
} else
fctx.err_fp = stderr;
/* Will we allow users to allocate every last block? */
if (getenv("FUSE2FS_ALLOC_ALL_BLOCKS")) {
printf(_("%s: Allowing users to allocate all blocks. "
"This is dangerous!\n"), fctx.device);
fctx.alloc_all_blocks = 1;
}
/* Start up the fs (while we still can use stdout) */
ret = 2;
if (!fctx.ro)
flags |= EXT2_FLAG_RW;
char options[50];
sprintf(options, "offset=%lu", fctx.offset);
err = ext2fs_open2(fctx.device, options, flags, 0, 0, unix_io_manager,
&global_fs);
if (err) {
printf(_("%s: %s.\n"), fctx.device, error_message(err));
printf(_("Please run e2fsck -fy %s.\n"), fctx.device);
goto out;
}
fctx.fs = global_fs;
global_fs->priv_data = &fctx;
ret = 3;
if (ext2fs_has_feature_quota(global_fs->super)) {
printf(_("%s: quotas not supported."), fctx.device);
goto out;
}
if (ext2fs_has_feature_verity(global_fs->super)) {
printf(_("%s: verity not supported."), fctx.device);
goto out;
}
if (ext2fs_has_feature_encrypt(global_fs->super)) {
printf(_("%s: encryption not supported."), fctx.device);
goto out;
}
if (ext2fs_has_feature_casefold(global_fs->super)) {
printf(_("%s: casefolding not supported."), fctx.device);
goto out;
}
if (ext2fs_has_feature_shared_blocks(global_fs->super))
fctx.ro = 1;
if (ext2fs_has_feature_journal_needs_recovery(global_fs->super)) {
if (fctx.norecovery) {
printf(_("%s: mounting read-only without "
"recovering journal\n"),
fctx.device);
} else if (!fctx.ro) {
printf(_("%s: recovering journal\n"), fctx.device);
err = ext2fs_run_ext3_journal(&global_fs);
if (err) {
printf(_("%s: %s.\n"), fctx.device,
error_message(err));
printf(_("Please run e2fsck -fy %s.\n"),
fctx.device);
goto out;
}
ext2fs_clear_feature_journal_needs_recovery(global_fs->super);
ext2fs_mark_super_dirty(global_fs);
} else {
printf("%s", _("Journal needs recovery; running "
"`e2fsck -E journal_only' is required.\n"));
goto out;
}
}
if (!fctx.ro) {
if (ext2fs_has_feature_journal(global_fs->super))
printf(_("%s: Writing to the journal is not supported.\n"),
fctx.device);
err = ext2fs_read_inode_bitmap(global_fs);
if (err) {
translate_error(global_fs, 0, err);
goto out;
}
err = ext2fs_read_block_bitmap(global_fs);
if (err) {
translate_error(global_fs, 0, err);
goto out;
}
}
if (!(global_fs->super->s_state & EXT2_VALID_FS))
printf("%s", _("Warning: Mounting unchecked fs, running e2fsck "
"is recommended.\n"));
if (global_fs->super->s_max_mnt_count > 0 &&
global_fs->super->s_mnt_count >= global_fs->super->s_max_mnt_count)
printf("%s", _("Warning: Maximal mount count reached, running "
"e2fsck is recommended.\n"));
if (global_fs->super->s_checkinterval > 0 &&
(time_t) (global_fs->super->s_lastcheck +
global_fs->super->s_checkinterval) <= time(0))
printf("%s", _("Warning: Check time reached; running e2fsck "
"is recommended.\n"));
if (global_fs->super->s_last_orphan)
printf("%s",
_("Orphans detected; running e2fsck is recommended.\n"));
if (global_fs->super->s_state & EXT2_ERROR_FS) {
printf("%s",
_("Errors detected; running e2fsck is required.\n"));
goto out;
}
/* Initialize generation counter */
get_random_bytes(&fctx.next_generation, sizeof(unsigned int));
/* Set up default fuse parameters */
snprintf(extra_args, BUFSIZ, "-okernel_cache,subtype=ext4,"
"fsname=%s,attr_timeout=0" FUSE_PLATFORM_OPTS,
fctx.device);
if (fctx.no_default_opts == 0)
fuse_opt_add_arg(&args, extra_args);
if (fctx.fakeroot) {
#ifdef HAVE_MOUNT_NODEV
fuse_opt_add_arg(&args,"-onodev");
#endif
#ifdef HAVE_MOUNT_NOSUID
fuse_opt_add_arg(&args,"-onosuid");
#endif
}
if (fctx.debug) {
int i;
printf("fuse arguments:");
for (i = 0; i < args.argc; i++)
printf(" '%s'", args.argv[i]);
printf("\n");
}
pthread_mutex_init(&fctx.bfl, NULL);
fuse_main(args.argc, args.argv, &fs_ops, &fctx);
pthread_mutex_destroy(&fctx.bfl);
ret = 0;
out:
if (global_fs) {
err = ext2fs_close(global_fs);
if (err)
com_err(argv[0], err, "while closing fs");
global_fs = NULL;
}
return ret;
}
static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
const char *file, int line)
{
struct timespec now;
int ret = err;
struct fuse2fs *ff = fs->priv_data;
int is_err = 0;
/* Translate ext2 error to unix error code */
if (err < EXT2_ET_BASE)
goto no_translation;
switch (err) {
case EXT2_ET_NO_MEMORY:
case EXT2_ET_TDB_ERR_OOM:
ret = -ENOMEM;
break;
case EXT2_ET_INVALID_ARGUMENT:
case EXT2_ET_LLSEEK_FAILED:
ret = -EINVAL;
break;
case EXT2_ET_NO_DIRECTORY:
ret = -ENOTDIR;
break;
case EXT2_ET_FILE_NOT_FOUND:
ret = -ENOENT;
break;
case EXT2_ET_DIR_NO_SPACE:
is_err = 1;
/* fallthrough */
case EXT2_ET_TOOSMALL:
case EXT2_ET_BLOCK_ALLOC_FAIL:
case EXT2_ET_INODE_ALLOC_FAIL:
case EXT2_ET_EA_NO_SPACE:
ret = -ENOSPC;
break;
case EXT2_ET_SYMLINK_LOOP:
ret = -EMLINK;
break;
case EXT2_ET_FILE_TOO_BIG:
ret = -EFBIG;
break;
case EXT2_ET_TDB_ERR_EXISTS:
case EXT2_ET_FILE_EXISTS:
ret = -EEXIST;
break;
case EXT2_ET_MMP_FAILED:
case EXT2_ET_MMP_FSCK_ON:
ret = -EBUSY;
break;
case EXT2_ET_EA_KEY_NOT_FOUND:
#ifdef ENODATA
ret = -ENODATA;
#else
ret = -ENOENT;
#endif
break;
/* Sometimes fuse returns a garbage file handle pointer to us... */
case EXT2_ET_MAGIC_EXT2_FILE:
ret = -EFAULT;
break;
case EXT2_ET_UNIMPLEMENTED:
ret = -EOPNOTSUPP;
break;
default:
is_err = 1;
ret = -EIO;
break;
}
no_translation:
if (!is_err)
return ret;
if (ino)
fprintf(ff->err_fp, "FUSE2FS (%s): %s (inode #%d) at %s:%d.\n",
fs->device_name ? fs->device_name : "???",
error_message(err), ino, file, line);
else
fprintf(ff->err_fp, "FUSE2FS (%s): %s at %s:%d.\n",
fs->device_name ? fs->device_name : "???",
error_message(err), file, line);
fflush(ff->err_fp);
/* Make a note in the error log */
get_now(&now);
ext2fs_set_tstamp(fs->super, s_last_error_time, now.tv_sec);
fs->super->s_last_error_ino = ino;
fs->super->s_last_error_line = line;
fs->super->s_last_error_block = err; /* Yeah... */
strncpy((char *)fs->super->s_last_error_func, file,
sizeof(fs->super->s_last_error_func));
if (ext2fs_get_tstamp(fs->super, s_first_error_time) == 0) {
ext2fs_set_tstamp(fs->super, s_first_error_time, now.tv_sec);
fs->super->s_first_error_ino = ino;
fs->super->s_first_error_line = line;
fs->super->s_first_error_block = err;
strncpy((char *)fs->super->s_first_error_func, file,
sizeof(fs->super->s_first_error_func));
}
fs->super->s_error_count++;
ext2fs_mark_super_dirty(fs);
ext2fs_flush(fs);
if (ff->panic_on_error)
abort();
return ret;
}
|