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 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481
|
/* $NetBSD$ */
/*
* File "udf.c" is part of the UDFclient toolkit.
* File $Id: udf.c,v 1.309 2024/10/14 14:09:19 reinoud Exp $ $Name: $
*
* Copyright (c) 2003, 2004, 2005, 2006, 2011, 2020
* Reinoud Zandijk <reinoud@netbsd.org>
* All rights reserved.
*
* The UDFclient toolkit is distributed under the Clarified Artistic Licence.
* A copy of the licence is included in the distribution as
* `LICENCE.clearified.artistic' and a copy of the licence can also be
* requested at the GNU foundantion's website.
*
* Visit the UDFclient toolkit homepage http://www.13thmonkey.org/udftoolkit/
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <time.h>
#include "uscsilib.h"
/* for locals */
#include "udf.h"
#include "udf_bswap.h"
#include "udf_discop.h"
#include "udf_unix.h"
#include "uio.h"
#include "dirhash.h"
#include <pthread.h>
/* for scsilib */
const char *dvname="UDF device";
#ifndef MAX
# define MAX(a,b) ((a)>(b)?(a):(b))
# define MIN(a,b) ((a)<(b)?(a):(b))
#endif
/* #define DEBUG(a) { a; } */
#define DEBUG(a) if (0) { a; }
/* global settings */
int udf_verbose = UDF_VERBLEV_ACTIONS;
/* static structures shared over all programs */
struct discslist udf_discs_list;
struct volumeset_list udf_volumeset_list;
struct mountables_list udf_mountables;
#define UDF_INODE_NUM_GUESS 2048
/* predefines */
int udf_validate_tag_and_crc_sums(union dscrptr *dscr);
void udf_node_mark_dirty(struct udf_node *udf_node);
static void udf_set_imp_id(struct regid *regid);
static void udf_set_app_id(struct regid *regid);
static void udf_node_unmark_dirty(struct udf_node *udf_node);
static void udf_init_desc_tag(struct desc_tag *tag, uint16_t id, uint16_t dscr_ver, uint16_t serial_num);
static int udf_translate_icb_filetype_to_dirent_filetype(int udf_filetype);
static int udf_remove_directory_prim(struct udf_node *dir_node, struct udf_node *udf_node, char *componentname);
static int udf_remove_directory_entry(struct udf_node *dir_node, struct udf_node *udf_node, char *componentname);
/* external dumpers */
extern void udf_dump_descriptor(union dscrptr *dscrpt);
extern void udf_dump_file_entry(struct file_entry *fe);
extern void udf_dump_extfile_entry(struct extfile_entry *efe);
extern void udf_dump_alloc_extent(struct alloc_ext_entry *ext, int addr_type);
extern void udf_dump_vat_table(struct udf_part_mapping *udf_part_mapping);
extern void udf_dump_disc_anchors(struct udf_discinfo *disc);
extern void udf_dump_alive_sets(void);
extern void udf_dump_root_dir(struct udf_mountpoint *mountpoint);
extern void udf_dump_timestamp(char *dscr, struct timestamp *t);
/******************************************************************************************
*
* Filename space conversion
*
******************************************************************************************/
void udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp) {
uint16_t raw_name[1024], unix_name[1024];
uint16_t *inchp, ch;
uint8_t *outchp;
int ucode_chars, nice_uchars;
assert(sizeof(char) == sizeof(uint8_t));
outchp = (uint8_t *) result;
if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) {
*raw_name = *unix_name = 0;
ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
ucode_chars = UnicodeLength((unicode_t *) raw_name, ucode_chars);
nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
ch = *inchp;
/* sloppy unicode -> latin */
*outchp++ = ch & 255;
if (!ch) break;
}
*outchp++ = 0;
} else {
/* assume 8bit char length byte latin-1 */
assert(*id == 8);
strncpy(result, id+1, len);
}
}
void unix_to_udf_name(char *result, char *name, uint8_t *result_len, struct charspec *chsp) {
uint16_t raw_name[1024];
int udf_chars, name_len;
char *inchp;
uint16_t *outchp;
/* convert latin-1 or whatever to unicode-16 */
*raw_name = 0;
name_len = 0;
inchp = name;
outchp = raw_name;
while (*inchp) {
*outchp++ = (uint16_t) (*inchp++);
name_len++;
}
if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result);
} else {
/* assume 8bit char length byte latin-1 */
*result++ = 8; udf_chars = 1;
strcpy(result, name + 1);
udf_chars += strlen(name);
}
*result_len = udf_chars;
}
static char *udf_get_compound_name(struct udf_mountpoint *mountpoint) {
static char compound[128+128+32+32+1];
struct charspec *charspec;
struct udf_log_vol *udf_log_vol;
struct udf_pri_vol *udf_pri_vol;
char *unix_name;
udf_log_vol = mountpoint->udf_log_vol;
udf_pri_vol = udf_log_vol->primary;
charspec = &udf_pri_vol->pri_vol->desc_charset;
assert(charspec->type == 0);
assert(strcmp((const char *) charspec->inf, "OSTA Compressed Unicode")==0);
unix_name = compound;
udf_to_unix_name(unix_name, udf_pri_vol->pri_vol->volset_id, 128, charspec);
strcat(unix_name, ":");
unix_name += strlen(unix_name);
udf_to_unix_name(unix_name, udf_pri_vol->pri_vol->vol_id, 32, charspec);
strcat(unix_name, ":");
unix_name += strlen(unix_name);
udf_to_unix_name(unix_name, udf_log_vol->log_vol->logvol_id, 128, charspec);
strcat(unix_name, ":");
unix_name += strlen(unix_name);
udf_to_unix_name(unix_name, mountpoint->fileset_desc->fileset_id, 32, charspec);
return compound;
}
/******************************************************************************************
*
* Dump helpers for printing out information during parse
*
******************************************************************************************/
void udf_dump_long_ad(char *prefix, struct long_ad *adr) {
printf("%s at sector %d within partion space %d for %d bytes\n", prefix,
udf_rw32(adr->loc.lb_num), udf_rw16(adr->loc.part_num),
udf_rw32(adr->len)
);
}
void udf_dump_id(char *prefix, int len, char *id, struct charspec *chsp) {
uint16_t raw_name[1024];
uint16_t *pos, ch;
int ucode_chars;
if (prefix) printf("%s ", prefix);
if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
/* print the identifier using the OSTA compressed unicode */
printf("`");
ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
for (pos = raw_name; ucode_chars > 0; pos++, ucode_chars--) {
ch = *pos; /* OSTA code decompresses to machine endian */
if (!ch) break;
if ((ch < 32) || (ch > 255)) {
printf("[%d]", ch);
} else {
printf("%c", ch & 255);
}
}
printf("`");
} else {
printf("(roughly) `%s`", id+1);
}
if (prefix) printf("\n");
}
void udf_dump_volume_name(char *prefix, struct udf_log_vol *udf_log_vol) {
if (prefix) printf("%s%s", prefix, udf_log_vol->primary->udf_session->session_offset?" (local) ":" ");
udf_dump_id(NULL, 128, udf_log_vol->primary->pri_vol->volset_id, &udf_log_vol->primary->pri_vol->desc_charset);
printf(":");
udf_dump_id(NULL, 32, udf_log_vol->primary->pri_vol->vol_id, &udf_log_vol->primary->pri_vol->desc_charset);
printf(":");
udf_dump_id(NULL, 128, udf_log_vol->log_vol->logvol_id, &udf_log_vol->log_vol->desc_charset);
if (prefix) printf("\n");
}
/******************************************************************************************
*
* UDF tag checkers and size calculator
*
******************************************************************************************/
/* not used extensively enough yet */
int udf_check_tag(union dscrptr *dscr) {
struct desc_tag *tag = &dscr->tag;
uint8_t *pos, sum, cnt;
/* check TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for(cnt = 0; cnt < 16; cnt++) {
if (cnt != 4) sum += *pos;
pos++;
}
if (sum != tag->cksum) {
/* bad tag header checksum; this is not a valid tag */
DEBUG(printf("Bad checksum\n"));
return EINVAL;
}
return 0;
}
int udf_check_tag_payload(union dscrptr *dscr) {
struct desc_tag *tag = &dscr->tag;
uint16_t crc;
/* check payload CRC if applicable */
if (udf_rw16(tag->desc_crc_len) == 0) return 0;
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, udf_rw16(tag->desc_crc_len));
if (crc != udf_rw16(tag->desc_crc)) {
DEBUG(printf("ERROR: CRC bad read 0x%0x calc 0x%0x\n", udf_rw16(tag->desc_crc), crc));
/* bad payload CRC; this is a broken tag */
return EINVAL;
}
return 0;
}
int udf_validate_tag_sum(union dscrptr *dscr) {
struct desc_tag *tag = &dscr->tag;
uint8_t *pos, sum, cnt;
/* calculate TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for(cnt = 0; cnt < 16; cnt++) {
if (cnt != 4) sum += *pos;
pos++;
}
tag->cksum = sum; /* 8 bit */
return 0;
}
/* assumes sector number of descriptor to be allready present */
int udf_validate_tag_and_crc_sums(union dscrptr *dscr) {
struct desc_tag *tag = &dscr->tag;
uint16_t crc;
/* check payload CRC if applicable */
if (udf_rw16(tag->desc_crc_len) > 0) {
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, udf_rw16(tag->desc_crc_len));
tag->desc_crc = udf_rw16(crc);
}
/* calculate TAG header checksum */
return udf_validate_tag_sum(dscr);
}
int udf_check_tag_presence(union dscrptr *dscr, int TAG) {
struct desc_tag *tag = &dscr->tag;
int error;
error = udf_check_tag(dscr);
if (error) return error;
if (udf_rw16(tag->id) != TAG) {
DEBUG(fprintf(stderr, "looking for tag %d but found %d\n", TAG, udf_rw16(tag->id)));
return ENOENT;
}
return 0;
}
/*
* for malloc() purposes ... rather have an upperlimit than an exact size
*/
uint64_t udf_calc_tag_malloc_size(union dscrptr *dscr, uint32_t udf_sector_size) {
uint32_t size, tag_id;
tag_id = udf_rw16(dscr->tag.id);
switch (tag_id) {
case TAGID_LOGVOL :
size = sizeof(struct logvol_desc) - 1; /* maps[1] */
size += udf_rw32(dscr->lvd.mt_l);
break;
case TAGID_UNALLOC_SPACE :
size = sizeof(struct unalloc_sp_desc) - sizeof(struct extent_ad); /* alloc_desc[1] */
size += udf_rw32(dscr->usd.alloc_desc_num) * sizeof(struct extent_ad);
break;
case TAGID_FID :
size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
size = (size + 3) & ~3;
return size; /* RETURN !! */
case TAGID_LOGVOL_INTEGRITY :
size = sizeof(struct logvol_int_desc) - sizeof(uint32_t); /* tables[1] */
size += udf_rw32(dscr->lvid.l_iu);
size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
break;
case TAGID_SPACE_BITMAP :
size = sizeof(struct space_bitmap_desc) - 1; /* data[1] */
size += udf_rw32(dscr->sbd.num_bytes);
break;
case TAGID_SPARING_TABLE :
size = sizeof(struct udf_sparing_table) - sizeof(struct spare_map_entry); /* entries[1] */
size += udf_rw16(dscr->spt.rt_l) * sizeof(struct spare_map_entry);
break;
case TAGID_FENTRY :
size = sizeof(struct file_entry);
size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1; /* data[0] */
break;
case TAGID_EXTFENTRY :
size = sizeof(struct extfile_entry);
size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1; /* data[0] */
break;
case TAGID_FSD :
size = sizeof(struct fileset_desc);
break;
default :
size = sizeof(union dscrptr);
break;
}
if ((size == 0) || (udf_sector_size == 0)) return 0;
return ((size + udf_sector_size -1) / udf_sector_size) * udf_sector_size;
}
/* explicit only for FID's */
static int
udf_fidsize(struct fileid_desc *fid)
{
int size;
size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
size = (size + 3) & ~3;
return size;
}
/******************************************************************************************
*
* Logical to physical adres transformation
*
******************************************************************************************/
/* convert (udf_log_vol, vpart_num) to a udf_partion structure */
int udf_logvol_vpart_to_partition(struct udf_log_vol *udf_log_vol, uint32_t vpart_num, struct udf_part_mapping **udf_part_mapping_ptr, struct udf_partition **udf_partition_ptr) {
struct udf_volumeset *udf_volumeset;
struct udf_partition *udf_partition;
struct udf_part_mapping *udf_part_mapping;
uint32_t part_num;
int found;
assert(udf_log_vol);
assert(!SLIST_EMPTY(&udf_log_vol->part_mappings));
/* clear result */
if (udf_part_mapping_ptr) *udf_part_mapping_ptr = NULL;
if (udf_partition_ptr) *udf_partition_ptr = NULL;
/* map the requested partition map to the physical udf partition */
found = 0;
SLIST_FOREACH(udf_part_mapping, &udf_log_vol->part_mappings, next_mapping) {
if (udf_part_mapping->udf_virt_part_num == vpart_num) {
found = 1;
break;
}
}
if (!found) {
printf("\t\t\tVirtual partition number %d not found!\n", vpart_num);
return EINVAL;
}
assert(udf_part_mapping); /* call me paranoid */
part_num = udf_part_mapping->udf_phys_part_num;
/* search for the physical partition information */
udf_volumeset = udf_log_vol->primary->volumeset;
SLIST_FOREACH(udf_partition, &udf_volumeset->parts, next_partition) {
if (udf_rw16(udf_partition->partition->part_num) == part_num) break;
}
if (!udf_partition) {
printf("\t\t\tNo information known about partition %d yet!\n", part_num);
printf("\t\t\t\tPlease insert volume %d of this volumeset and try again\n", udf_part_mapping->vol_seq_num);
return ENOENT;
}
if (udf_part_mapping_ptr) *udf_part_mapping_ptr = udf_part_mapping;
if (udf_partition_ptr) *udf_partition_ptr = udf_partition;
return 0;
}
/* no recursive translations yet (UDF OK) */
/* All translation is done in 64 bits to prevent bitrot and returns the PARTITION relative address */
int udf_vpartoff_to_sessionoff(struct udf_log_vol *udf_log_vol, struct udf_part_mapping *udf_part_mapping, struct udf_partition *udf_partition, uint64_t offset, uint64_t *ses_off, uint64_t *trans_valid_len) {
struct spare_map_entry *sp_entry;
struct udf_node *udf_node;
struct udf_allocentry *alloc_entry;
uint64_t part_start, part_length;
uint64_t eff_sector, eff_offset;
uint64_t trans_sector;
uint64_t cur_offset;
uint32_t len, lb_num, block_offset;
uint32_t entry, entries;
uint32_t sector_size, lb_size;
uint64_t packet_num, packet_rlb;
uint64_t packet_len;
uint32_t vat_entries, *vat_pos;
int flags;
assert(udf_part_mapping);
assert(udf_partition);
assert(ses_off);
assert(trans_valid_len);
/* not ok, but rather this than a dangling random value */
*ses_off = UINT_MAX;
*trans_valid_len = 0;
lb_size = udf_log_vol->lb_size;
sector_size = udf_log_vol->sector_size;
part_start = (uint64_t) udf_rw32(udf_partition->partition->start_loc) * sector_size;
part_length = (uint64_t) udf_rw32(udf_partition->partition->part_len) * sector_size;
/* get the offset (in bytes) in the partition and check its validity */
if (offset >= part_length) {
printf("\t\toffset %"PRIu64" is outside partition %d!\n", offset, udf_rw16(udf_partition->partition->part_num));
return EFAULT;
}
/* do the address translations based on the partition mapping type */
/* translation of virt/sparable etc. is assumed to be done in logical block sizes */
switch (udf_part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_PHYSICAL :
/* nothing to be done; physical is logical */
*ses_off = part_start + offset; /* 1:1 */
*trans_valid_len = part_length - offset; /* rest of partition */
return 0;
case UDF_PART_MAPPING_VIRTUAL :
vat_entries = udf_part_mapping->vat_entries;
vat_pos = (uint32_t *) udf_part_mapping->vat_translation;
/* this translation is dependent on logical sector numbers */
eff_sector = offset / lb_size;
eff_offset = offset % lb_size;
/* TODO check range for logical sector against VAT length */
assert(eff_sector < vat_entries);
trans_sector = vat_pos[eff_sector];
*ses_off = part_start + (trans_sector * lb_size) + eff_offset; /* trans sectors are in lb->lb ? */
*trans_valid_len = lb_size - eff_offset; /* maximum one logical sector */
return 0;
case UDF_PART_MAPPING_SPARABLE :
/* this translation is dependent on logical sector numbers */
*ses_off = part_start + offset; /* 1:1 */
eff_sector = offset / lb_size;
eff_offset = offset % lb_size;
/* transform on packet-length base */
packet_len = udf_rw16(udf_part_mapping->udf_pmap->pms.packet_len); /* in lb */
entries = udf_rw16(udf_part_mapping->sparing_table->rt_l);
packet_num = (eff_sector / packet_len) * packet_len;
packet_rlb = eff_sector % packet_len; /* within packet */
/* translate this packet; source is in partition, destination is absolute disc address */
sp_entry = &udf_part_mapping->sparing_table->entries[0];
for (entry = 0; entry < entries; entry++) {
if (udf_rw32(sp_entry->org) - packet_num == 0) {
/* mappings contain absolute disc addresses, so no partition offsets please */
*ses_off = (uint64_t) (udf_rw32(sp_entry->map) + packet_rlb) * lb_size + eff_offset;
break;
}
sp_entry++;
}
*trans_valid_len = (packet_len - packet_rlb) * lb_size; /* maximum one packet */
return 0;
case UDF_PART_MAPPING_META :
/* We follow the allocation entries to calculate our offset */
udf_node = udf_part_mapping->meta_file;
assert(udf_node->addr_type != UDF_ICB_INTERN_ALLOC);
/* find sector in the allocation space */
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
cur_offset = 0;
TAILQ_FOREACH(alloc_entry, &udf_node->alloc_entries, next_alloc) {
len = alloc_entry->len;
lb_num = alloc_entry->lb_num;
/* vpart_num = alloc_entry->vpart_num; */
flags = alloc_entry->flags;
/* check overlap with this alloc entry */
if (cur_offset + len > offset) {
assert(((offset - cur_offset) % lb_size) == 0); /* ought to be on sector boundary */
if (flags != UDF_EXT_ALLOCATED)
break;
block_offset = offset - cur_offset;
*ses_off = part_start + lb_num * lb_size + block_offset; /* 1:1 within the block */
*trans_valid_len = len - block_offset; /* rest of this chunk */
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
return 0;
}
cur_offset += len;
} /* FOREACH */
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
printf("\t\toffset %"PRIu64" is not translated within current metadata partition %d file descriptor!\n", offset, udf_rw16(udf_partition->partition->part_num));
return EFAULT;
case UDF_PART_MAPPING_ERROR :
default :
break;
}
printf("Unsupported or bad mapping %d; can't translate\n", udf_part_mapping->udf_part_mapping_type);
return EFAULT;
}
/******************************************************************************************
*
* udf_node creator, destructor and syncer
*
******************************************************************************************/
static mode_t udf_perm_to_unix_mode(uint32_t perm) {
mode_t mode;
mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
return mode;
}
static uint32_t unix_mode_to_udf_perm(mode_t mode) {
uint32_t perm;
perm = ((mode & S_IRWXO) );
perm |= ((mode & S_IRWXG) << 2);
perm |= ((mode & S_IRWXU) << 4);
perm |= ((mode & S_IWOTH) << 3);
perm |= ((mode & S_IWGRP) << 5);
perm |= ((mode & S_IWUSR) << 7);
return perm;
}
/*
* Fill in timestamp structure based on clock_gettime(). Time is reported back as a time_t
* accompanied with a nano second field.
*
* The husec, usec and csec could be relaxed in type.
*/
static void udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp) {
struct tm tm;
uint64_t husec, usec, csec;
bzero(timestamp, sizeof(struct timestamp));
gmtime_r(×pec->tv_sec, &tm);
/*
* Time type and time zone : see ECMA 1/7.3, UDF 2., 2.1.4.1, 3.1.1.
*
* Lower 12 bits are two complement signed timezone offset if bit 12
* (method 1) is clear. Otherwise if bit 12 is set, specify timezone
* offset to -2047 i.e. unsigned `zero'
*/
timestamp->type_tz = udf_rw16((1<<12) + 0); /* has to be method 1 for CUT/GMT */
timestamp->year = udf_rw16(tm.tm_year + 1900);
timestamp->month = tm.tm_mon + 1; /* `tm' structure uses 0..11 for months */
timestamp->day = tm.tm_mday;
timestamp->hour = tm.tm_hour;
timestamp->minute = tm.tm_min;
timestamp->second = tm.tm_sec;
usec = (timespec->tv_nsec + 500) / 1000; /* round (if possible) */
husec = usec / 100;
usec -= husec * 100; /* we only want 0-99 in usec */
csec = husec / 100; /* we get 0-99 in csec */
husec -= csec * 100; /* we only want 0-99 in husec */
timestamp->centisec = csec;
timestamp->hund_usec = husec;
timestamp->usec = usec;
}
void udf_set_timestamp_now(struct timestamp *timestamp) {
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
udf_timespec_to_timestamp(&now, timestamp);
}
/* implemented as a seperate function to allow tuning */
void udf_set_timespec_now(struct timespec *timespec) {
clock_gettime(CLOCK_REALTIME, timespec);
}
int udf_insanetimespec(struct timespec *check) {
struct timespec now;
struct tm tm;
gmtime_r(&check->tv_sec, &tm);
/* since our converters can only deal with timestamps after 1970 we
* reject earlier */
if (tm.tm_year < 1970) return 1;
/* don't accept values from the future; FFS or NFS might not mind, but
* UDF does! */
clock_gettime(CLOCK_REALTIME, &now);
if (now.tv_sec < check->tv_sec)
return 1;
if ((now.tv_sec == check->tv_sec) && (now.tv_nsec < check->tv_nsec))
return 1;
return 0;
}
/*
* Timestamp to timespec conversion code is taken with small modifications
* from FreeBSD /sys/fs/udf by Scott Long <scottl@freebsd.org>
*/
static int mon_lens[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static int
udf_isaleapyear(int year)
{
int i;
i = (year % 4) ? 0 : 1;
i &= (year % 100) ? 1 : 0;
i |= (year % 400) ? 0 : 1;
return i;
}
static void udf_timestamp_to_timespec(struct timestamp *timestamp, struct timespec *timespec) {
uint32_t usecs, secs, nsecs;
uint16_t tz;
int i, lpyear, daysinyear, year;
timespec->tv_sec = secs = 0;
timespec->tv_nsec = nsecs = 0;
/*
* DirectCD seems to like using bogus year values.
* Distrust time->month especially, since it will be used for an array
* index.
*/
year = udf_rw16(timestamp->year);
if ((year < 1970) || (timestamp->month > 12)) {
return;
}
/* Calculate the time and day */
usecs = timestamp->usec + 100*timestamp->hund_usec + 10000*timestamp->centisec;
nsecs = usecs * 1000;
secs = timestamp->second;
secs += timestamp->minute * 60;
secs += timestamp->hour * 3600;
secs += (timestamp->day-1) * 3600 * 24; /* day : 1-31 */
/* Calclulate the month */
lpyear = udf_isaleapyear(year);
for (i = 1; i < timestamp->month; i++)
secs += mon_lens[lpyear][i-1] * 3600 * 24; /* month: 1-12 */
for (i = 1970; i < year; i++) {
daysinyear = udf_isaleapyear(i) + 365 ;
secs += daysinyear * 3600 * 24;
}
/*
* Calculate the time zone. The timezone is 12 bit signed 2's
* compliment, so we gotta do some extra magic to handle it right.
*/
tz = udf_rw16(timestamp->type_tz);
tz &= 0x0fff; /* only lower 12 bits are significant */
if (tz & 0x0800) /* sign extention */
tz |= 0xf000;
/* TODO check timezone conversion */
#if 1
/* check if we are specified a timezone to convert */
if (udf_rw16(timestamp->type_tz) & 0x1000)
if ((int16_t) tz != -2047)
secs -= (int16_t) tz * 60;
#endif
timespec->tv_sec = secs;
timespec->tv_nsec = nsecs;
}
static void udf_node_get_fileinfo(struct udf_node *udf_node, union dscrptr *dscrptr) {
struct stat *stat;
struct file_entry *file_entry;
struct extfile_entry *extfile_entry;
struct timestamp *atime, *mtime, *ctime, *attrtime;
uint64_t inf_len, unique_id;
uint32_t uid, gid, udf_perm;
uint16_t fe_tag;
uint16_t udf_icbtag_flags, serial_num, link_cnt;
uint8_t filetype;
assert(udf_node);
assert(dscrptr);
stat = &udf_node->stat;
/* check if its an normal file entry or a extended file entry ICB */
fe_tag = udf_rw16(dscrptr->tag.id);
if (fe_tag == TAGID_FENTRY) {
file_entry = &dscrptr->fe;
#if 0
prev_direct_entries = udf_rw32(file_entry->icbtag.prev_num_dirs);
strat_param16 = udf_rw16(* (uint16_t *) (file_entry->icbtag.strat_param));
entries = udf_rw16(file_entry->icbtag.max_num_entries);
strategy = udf_rw16(file_entry->icbtag.strat_type);
data_length = udf_rw32(file_entry->l_ad);
addr_type = udf_rw16(file_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
pos = &file_entry->data[0] + udf_rw32(file_entry->l_ea);
#endif
filetype = file_entry->icbtag.file_type;
inf_len = udf_rw64(file_entry->inf_len);
uid = udf_rw32(file_entry->uid);
gid = udf_rw32(file_entry->gid);
udf_perm = udf_rw32(file_entry->perm);
serial_num = udf_rw16(file_entry->tag.serial_num);
udf_icbtag_flags = udf_rw16(file_entry->icbtag.flags);
link_cnt = udf_rw16(file_entry->link_cnt);
unique_id = udf_rw64(file_entry->unique_id);
atime = &file_entry->atime;
mtime = &file_entry->mtime;
ctime = &file_entry->mtime; /* XXX assumption */
attrtime = &file_entry->attrtime;
} else if (fe_tag == TAGID_EXTFENTRY) {
extfile_entry = &dscrptr->efe;
#if 0
prev_direct_entries = udf_rw32(extfile_entry->icbtag.prev_num_dirs);
strat_param16 = udf_rw16(* (uint16_t *) (extfile_entry->icbtag.strat_param));
entries = udf_rw16(extfile_entry->icbtag.max_num_entries);
strategy = udf_rw16(extfile_entry->icbtag.strat_type);
data_length = udf_rw32(extfile_entry->l_ad);
addr_type = udf_rw16(extfile_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
pos = &extfile_entry->data[0] + udf_rw32(extfile_entry->l_ea);
#endif
filetype = extfile_entry->icbtag.file_type;
inf_len = udf_rw64(extfile_entry->inf_len);
uid = udf_rw32(extfile_entry->uid);
gid = udf_rw32(extfile_entry->gid);
udf_perm = udf_rw32(extfile_entry->perm);
serial_num = udf_rw16(extfile_entry->tag.serial_num);
udf_icbtag_flags = udf_rw16(extfile_entry->icbtag.flags);
link_cnt = udf_rw16(extfile_entry->link_cnt); /* how many FID's are linked to this (ext)fentry */
unique_id = udf_rw64(extfile_entry->unique_id); /* unique file ID */
atime = &extfile_entry->atime;
mtime = &extfile_entry->mtime;
ctime = &extfile_entry->ctime;
attrtime = &extfile_entry->attrtime;
} else {
printf("udf_node_set_file_info : help! i can't be here!!! i got a %d tag\n", fe_tag);
udf_dump_descriptor(dscrptr);
return;
}
/* fill in (parts of) the stat structure */
/* XXX important info missing still like access mode, times etc. XXX */
udf_node->udf_filetype = filetype;
udf_node->serial_num = serial_num;
udf_node->udf_icbtag_flags = udf_icbtag_flags;
udf_node->link_cnt = link_cnt; /* how many FID's are linked to this (ext)fentry */
udf_node->unique_id = unique_id; /* unique file ID */
/* fill in stat basics */
bzero(stat, sizeof(struct stat));
stat->st_ino = unique_id; /* lowest 32 bit(!) only */
stat->st_mode = udf_perm_to_unix_mode(udf_perm); /* CONVERT from udf_perm */
stat->st_mode |= (udf_translate_icb_filetype_to_dirent_filetype(filetype) & DT_DIR) ? S_IFDIR : S_IFREG;
stat->st_uid = uid;
stat->st_gid = gid;
/* ... times */
udf_timestamp_to_timespec(atime, &stat->st_atimespec);
udf_timestamp_to_timespec(mtime, &stat->st_mtimespec);
udf_timestamp_to_timespec(attrtime, &stat->st_ctimespec);
#ifndef NO_STAT_BIRTHTIME
udf_timestamp_to_timespec(ctime, &stat->st_birthtimespec);
#endif
/* ... sizes */
stat->st_size = inf_len;
stat->st_blksize = udf_node->udf_log_vol->lb_size;
/* special: updatables */
stat->st_nlink = link_cnt;
stat->st_blocks = (stat->st_size + 512 -1)/512; /* blocks are hardcoded 512 bytes/sector in stat :-/ */
return;
}
static void udf_node_set_fileinfo(struct udf_node *udf_node, union dscrptr *dscrptr) {
struct stat *stat;
struct file_entry *file_entry;
struct extfile_entry *extfile_entry;
struct timestamp *atime, *mtime, *ctime, *attrtime;
uint64_t inf_len, unique_id;
uint32_t uid, gid, udf_perm;
uint16_t fe_tag, serial_num, link_cnt;
uint8_t filetype;
assert(udf_node);
assert(dscrptr);
stat = &udf_node->stat;
/* set (parts of) the stat structure */
/* XXX important info missing still like times etc. XXX */
uid = stat->st_uid;
gid = stat->st_gid;
inf_len = stat->st_size;
udf_perm = unix_mode_to_udf_perm(stat->st_mode); /* conversion to UDF perm. */
filetype = udf_node->udf_filetype;
unique_id = udf_node->unique_id;
serial_num = udf_node->serial_num;
link_cnt = udf_node->link_cnt;
/* check if its to be written in an normal file entry or a extended file entry ICB */
fe_tag = udf_rw16(dscrptr->tag.id);
if (fe_tag == TAGID_FENTRY) {
file_entry = &dscrptr->fe;
#if 0
prev_direct_entries = udf_rw32(file_entry->icbtag.prev_num_dirs);
strat_param16 = udf_rw16(* (uint16_t *) (file_entry->icbtag.strat_param));
entries = udf_rw16(file_entry->icbtag.max_num_entries);
strategy = udf_rw16(file_entry->icbtag.strat_type);
data_length = udf_rw32(file_entry->l_ad);
addr_type = udf_rw16(file_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
pos = &file_entry->data[0] + udf_rw32(file_entry->l_ea);
#endif
file_entry->icbtag.file_type = filetype;
file_entry->inf_len = udf_rw64(inf_len);
file_entry->uid = udf_rw32(uid);
file_entry->gid = udf_rw32(gid);
file_entry->perm = udf_rw32(udf_perm);
file_entry->tag.serial_num = udf_rw16(serial_num);
file_entry->link_cnt = udf_rw16(link_cnt);
file_entry->unique_id = udf_rw64(unique_id);
atime = &file_entry->atime;
mtime = &file_entry->mtime;
ctime = mtime; /* XXX assumption */
attrtime = &file_entry->attrtime;
} else if (fe_tag == TAGID_EXTFENTRY) {
extfile_entry = &dscrptr->efe;
#if 0
prev_direct_entries = udf_rw32(extfile_entry->icbtag.prev_num_dirs);
strat_param16 = udf_rw16(* (uint16_t *) (extfile_entry->icbtag.strat_param));
entries = udf_rw16(extfile_entry->icbtag.max_num_entries);
strategy = udf_rw16(extfile_entry->icbtag.strat_type);
data_length = udf_rw32(extfile_entry->l_ad);
addr_type = udf_rw16(extfile_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
pos = &extfile_entry->data[0] + udf_rw32(extfile_entry->l_ea);
#endif
extfile_entry->icbtag.file_type = filetype;
extfile_entry->inf_len = udf_rw64(inf_len);
extfile_entry->uid = udf_rw32(uid);
extfile_entry->gid = udf_rw32(gid);
extfile_entry->perm = udf_rw32(udf_perm);
extfile_entry->tag.serial_num = udf_rw16(serial_num);
extfile_entry->link_cnt = udf_rw16(link_cnt); /* how many FID's are linked to this (ext)fentry */
extfile_entry->unique_id = udf_rw64(unique_id); /* unique file ID */
atime = &extfile_entry->atime;
mtime = &extfile_entry->mtime;
ctime = &extfile_entry->ctime;
attrtime = &extfile_entry->attrtime;
} else {
printf("udf_node_set_file_info : help! i can't be here!!! i got a %d tag\n", fe_tag);
udf_dump_descriptor(dscrptr);
return;
}
/* FILL in {atime, mtime, attrtime} TIMES! */
udf_timespec_to_timestamp(&stat->st_atimespec, atime);
udf_timespec_to_timestamp(&stat->st_mtimespec, mtime);
udf_timespec_to_timestamp(&stat->st_ctimespec, attrtime);
#ifndef NO_STAT_BIRTHTIME
udf_timespec_to_timestamp(&stat->st_birthtimespec, ctime);
#else
memcpy(ctime, mtime, sizeof(*ctime));
#endif
return;
}
/* with 32 bits ino_t, a maximum of about 8 TB discs are supported (1<<32) * 2KB*/
ino_t udf_calc_hash(struct long_ad *icbptr) {
/* TODO unique file-id would be better */
return (ino_t) udf_rw32(icbptr->loc.lb_num);
}
void udf_insert_node_in_hash(struct udf_node *udf_node) {
struct udf_log_vol *log_vol;
uint32_t bucket;
ino_t hashkey;
struct long_ad icb;
icb.loc.lb_num = udf_rw32(TAILQ_FIRST(&udf_node->dscr_allocs)->lb_num);
log_vol = udf_node->udf_log_vol;
hashkey = udf_calc_hash(&icb);
udf_node->hashkey = hashkey;
bucket = hashkey & UDF_INODE_HASHMASK;
LIST_INSERT_HEAD(&log_vol->udf_nodes[bucket], udf_node, next_node);
}
/* dispose udf_node's administration */
void udf_dispose_udf_node(struct udf_node *udf_node) {
struct udf_allocentry *alloc_entry;
struct udf_buf *buf_entry;
struct udf_node *lookup;
uint32_t bucket;
ino_t hashkey;
if (!udf_node) return;
/* XXX locks? XXX */
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
if (udf_node->dirty) {
DEBUG(printf("Warning: disposing dirty node\n"));
udf_node_unmark_dirty(udf_node);
}
/* free all associated buffers */
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
/* due to this `trick' we don't need to use a marker */
while ((buf_entry = TAILQ_FIRST(&udf_node->vn_bufs))) {
udf_mark_buf_clean(udf_node, buf_entry); /* its destroyed so not dirty */
udf_mark_buf_allocated(udf_node, buf_entry); /* i.e. taken care of */
udf_detach_buf_from_node(udf_node, buf_entry);
udf_free_buf_entry(buf_entry);
}
/* free in-node filedata blob if present */
if (udf_node->intern_data)
free(udf_node->intern_data);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
/* free our extended attribute administration if present */
if (udf_node->extattrfile_icb)
free(udf_node->extattrfile_icb);
if (udf_node->streamdir_icb)
free(udf_node->streamdir_icb);
if (udf_node->extended_attr)
free(udf_node->extended_attr);
/* free dscr_allocs queue */
while ((alloc_entry = TAILQ_FIRST(&udf_node->dscr_allocs))) {
TAILQ_REMOVE(&udf_node->dscr_allocs, alloc_entry, next_alloc);
free(alloc_entry);
}
/* free allocation queue */
while ((alloc_entry = TAILQ_FIRST(&udf_node->alloc_entries))) {
TAILQ_REMOVE(&udf_node->alloc_entries, alloc_entry, next_alloc);
free(alloc_entry);
}
/* if its part of a logical volume, delete it in its hash table */
if (udf_node->udf_log_vol) {
hashkey = udf_node->hashkey;
bucket = hashkey & UDF_INODE_HASHMASK;
LIST_FOREACH(lookup, &udf_node->udf_log_vol->udf_nodes[bucket], next_node) {
/* hashkey doesn't matter; just remove same udf_node pointer */
if (lookup == udf_node) {
assert(lookup->hashkey == hashkey);
DEBUG(printf("removal of udf_node from the hash table\n"));
LIST_REMOVE(lookup, next_node);
break;
}
}
}
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
/* free the node */
free(udf_node);
}
uint64_t udf_increment_unique_id(struct udf_log_vol *udf_log_vol) {
uint64_t unique_id, next_unique_id;
/* lock? */
unique_id = udf_log_vol->next_unique_id;
/* increment according to UDF 3/3.2.1.1 */
next_unique_id = unique_id + 1;
if (((next_unique_id << 32) >> 32) < 16) next_unique_id |= 16;
udf_log_vol->next_unique_id = next_unique_id;
DEBUG(printf("next unique_id <-- %"PRIu64"\n", udf_log_vol->next_unique_id));
return unique_id;
}
int udf_init_udf_node(struct udf_mountpoint *mountpoint, struct udf_log_vol *udf_log_vol, char *what, struct udf_node **udf_nodeptr) {
struct udf_node *udf_node;
uint32_t lb_size, data_space_avail;
int descr_ver;
what = what; /* not used yet */
assert(udf_log_vol);
lb_size = udf_log_vol->lb_size;
/* get ourselves some space */
udf_node = calloc(1, sizeof(struct udf_node));
if (!udf_node) return ENOMEM;
/* setup basic udf_node */
udf_node->addr_type = UDF_ICB_LONG_ALLOC;
udf_node->icb_len = sizeof(struct long_ad);
udf_node->serial_num = 1;
udf_node->udf_icbtag_flags = UDF_ICB_INTERN_ALLOC;
udf_node->link_cnt = 1; /* how many FID's are linked to this node */
udf_node->unique_id = 0; /* unique file ID unknown */
/* get internal space available for internal nodes */
/* TODO keep in mind the extended attributes! XXX */
descr_ver = udf_rw16(udf_log_vol->log_vol->tag.descriptor_ver);
if (descr_ver == 2) {
/* TODO reserve some space for the icb creation time */
data_space_avail = lb_size - sizeof(struct file_entry) - 0; /* udf_rw32(file_entry->l_ea) */
} else {
data_space_avail = lb_size - sizeof(struct extfile_entry) - 0; /* udf_rw32(extfile_entry->l_ea) */
}
udf_node->intern_free = data_space_avail;
udf_node->intern_data = NULL;
udf_node->intern_len = 0;
/* finalise udf_node */
udf_node->mountpoint = mountpoint;
udf_node->udf_log_vol = udf_log_vol;
TAILQ_INIT(&udf_node->dscr_allocs);
TAILQ_INIT(&udf_node->alloc_entries);
TAILQ_INIT(&udf_node->vn_bufs);
UDF_MUTEX_INIT(&udf_node->alloc_mutex);
UDF_MUTEX_INIT(&udf_node->buf_mutex);
/* XXX udf_node_lock NOT USED XXX */
/* pthread_rwlock_init(&udf_node->udf_node_lock, NULL); */
*udf_nodeptr = udf_node;
return 0;
}
int udf_allocate_udf_node_on_disc(struct udf_node *udf_node) {
struct udf_allocentry *alloc_entry;
uint32_t lb_num, lb_size;
uint16_t vpart_num;
int error;
assert(udf_node);
assert(udf_node->udf_log_vol);
assert(udf_node->udf_log_vol->log_vol);
lb_size = udf_node->udf_log_vol->lb_size;
assert(lb_size);
/* pre-allocate node; its needed in directory linkage for now */
error = udf_allocate_lbs(udf_node->udf_log_vol, UDF_C_NODE, /*num lb */ 1, "New FID", &vpart_num, &lb_num, NULL);
if (error) return error;
alloc_entry = calloc(1, sizeof(struct udf_allocentry));
if (!alloc_entry) {
return ENOMEM;
}
alloc_entry->len = lb_size;
alloc_entry->vpart_num = vpart_num;
alloc_entry->lb_num = lb_num;
alloc_entry->flags = 0;
TAILQ_INSERT_TAIL(&udf_node->dscr_allocs, alloc_entry, next_alloc);
assert(error == 0);
return error;
}
/* note: the udf_node (inode) is not stored in a hashtable! hash value is still invalid */
/* TODO remember our extended attributes and remember our streamdir long_ad */
int udf_readin_anon_udf_node(struct udf_log_vol *udf_log_vol, union dscrptr *given_dscrptr, struct long_ad *udf_icbptr, char *what, struct udf_node **udf_nodeptr) {
union dscrptr *dscrptr;
struct udf_node *udf_node;
struct udf_allocentry *alloc_entry;
struct udf_allocentry *cur_alloc, *next_alloc;
struct file_entry *file_entry;
struct extfile_entry *extfile_entry;
struct alloc_ext_entry *alloc_ext_entry;
struct long_ad *l_ad;
struct short_ad *s_ad;
uint64_t inf_len, calculated_len;
uint32_t lb_size, entries;
uint64_t data_length;
uint32_t data_space_avail;
uint32_t fe_tag;
uint64_t len;
uint32_t lb_num, vpart_num;
uint32_t icb_len;
int16_t addr_type;
uint8_t *pos;
uint8_t flags;
int error, advance_sector;
if ((udf_icbptr->loc.lb_num == 0) && (udf_icbptr->loc.part_num == 0) && (udf_icbptr->len == 0)) return ENOENT;
DEBUG(printf("udf_readin_anon_udf_node for %s\n", what));
error = udf_init_udf_node(/*mountpoint*/ NULL, udf_log_vol, what, &udf_node);
DEBUG(
if (error) printf("While reading in `anononymous' udf_node : got error %s\n", strerror(error));
);
if (error) return error;
*udf_nodeptr = udf_node;
assert(udf_log_vol);
lb_size = udf_log_vol->lb_size;
/* read in descriptor if not provided to us */
dscrptr = NULL;
len = lb_size; /* nodes are defined in lb_size only (?) */
lb_num = udf_rw32(udf_icbptr->loc.lb_num);
vpart_num = udf_rw16(udf_icbptr->loc.part_num);
if (!given_dscrptr) {
error = udf_read_logvol_descriptor(udf_log_vol, vpart_num, lb_num, what, &dscrptr, NULL);
if (error) {
*udf_nodeptr = NULL;
return error;
}
} else {
dscrptr = given_dscrptr;
}
fe_tag = udf_rw16(dscrptr->tag.id);
if (fe_tag != TAGID_FENTRY && fe_tag != TAGID_EXTFENTRY) {
/* something wrong with this address; it doesn't start with a file entry */
printf("UDF: bad udf_node for %s; got a %d tag\n", what, fe_tag);
if (dscrptr != given_dscrptr) free(dscrptr);
udf_dispose_udf_node(udf_node);
*udf_nodeptr = NULL;
return EFAULT;
}
/* get as much info as possible */
udf_node_get_fileinfo(udf_node, dscrptr);
/* reset pending write count */
udf_node->v_numoutput = 0;
/* initialise various variables for extracting allocation information */
inf_len = 0; data_length = 0; lb_num = 0; len = 0; vpart_num = 0; pos = NULL;
data_space_avail = 0;
next_alloc = calloc(1, sizeof(struct udf_allocentry));
if (!next_alloc) {
if (dscrptr != given_dscrptr) free(dscrptr);
udf_dispose_udf_node(udf_node);
*udf_nodeptr = NULL;
return ENOMEM;
}
next_alloc->len = lb_size;
next_alloc->lb_num = udf_rw32(udf_icbptr->loc.lb_num);
next_alloc->vpart_num = udf_rw16(udf_icbptr->loc.part_num);
entries = 1;
calculated_len = 0;
error = 0;
addr_type = -1;
do {
cur_alloc = next_alloc;
next_alloc = calloc(1, sizeof(struct udf_allocentry));
if (!next_alloc) {
if (dscrptr != given_dscrptr) free(dscrptr);
udf_dispose_udf_node(udf_node);
*udf_nodeptr = NULL;
return ENOMEM;
}
memcpy(next_alloc, cur_alloc, sizeof(struct udf_allocentry));
TAILQ_INSERT_TAIL(&udf_node->dscr_allocs, cur_alloc, next_alloc);
/* process this allocation descriptor */
/* note that we don't store the file descriptors -> XXX impl. use stuff gets lost here */
fe_tag = udf_rw16(dscrptr->tag.id);
switch (fe_tag) {
case TAGID_FENTRY :
/* allocation descriptors follow this tag */
file_entry = &dscrptr->fe;
entries = udf_rw16(file_entry->icbtag.max_num_entries);
data_length = udf_rw32(file_entry->l_ad);
pos = &file_entry->data[0] + udf_rw32(file_entry->l_ea);
inf_len = udf_rw64(file_entry->inf_len);
addr_type = udf_rw16(file_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
data_space_avail = lb_size - sizeof(struct file_entry) - udf_rw32(file_entry->l_ea);
/* process extended attributes */
/* keep a trace of the descriptors in this udf_node */
UDF_VERBOSE_MAX(udf_dump_file_entry(file_entry));
break;
case TAGID_EXTFENTRY :
/* allocation descriptors follow this tag */
extfile_entry = &dscrptr->efe;
entries = udf_rw16(extfile_entry->icbtag.max_num_entries);
data_length = udf_rw32(extfile_entry->l_ad);
pos = &extfile_entry->data[0] + udf_rw32(extfile_entry->l_ea);
inf_len = udf_rw64(extfile_entry->inf_len);
addr_type = udf_rw16(extfile_entry->icbtag.flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
data_space_avail = lb_size - sizeof(struct extfile_entry) - udf_rw32(extfile_entry->l_ea);
/* process extended attributes */
/* keep a trace of the descriptors in this udf_node */
UDF_VERBOSE_MAX(udf_dump_extfile_entry(extfile_entry));
break;
case TAGID_ALLOCEXTENT :
/* allocation descriptors follow this tag; treat as if continuation of a (ext)file entry*/
alloc_ext_entry = &dscrptr->aee;
data_length = udf_rw32(alloc_ext_entry->l_ad);
pos = &alloc_ext_entry->data[0];
assert(addr_type >= 0);
/* keep a trace of the descriptors in this udf_node */
UDF_VERBOSE_MAX(udf_dump_alloc_extent(alloc_ext_entry, addr_type));
break;
case TAGID_INDIRECTENTRY :
printf("create_anon_udf_node called with indirect entry; following chain\n");
l_ad = &dscrptr->inde.indirect_icb;
next_alloc->len = udf_rw32(l_ad->len);
next_alloc->lb_num = udf_rw32(l_ad->loc.lb_num);
next_alloc->vpart_num = udf_rw16(l_ad->loc.part_num);
entries = 1; /* at least one more entry */
advance_sector = 0; /* don't advance to next sector */
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
break;
default:
printf("read_file_part_extents: i can't be here! (tag = %d) in ICB hiargy of %s\n", fe_tag, what);
udf_dump_descriptor(dscrptr);
return EFAULT;
}
if (fe_tag != TAGID_INDIRECTENTRY) {
udf_node->addr_type = addr_type;
if (addr_type == UDF_ICB_INTERN_ALLOC) {
udf_node->intern_len = inf_len;
udf_node->intern_free = data_space_avail;
udf_node->intern_data = calloc(1, udf_node->intern_free);
if (udf_node->intern_data) {
memcpy(udf_node->intern_data, pos, inf_len);
} else {
error = ENOMEM;
}
if (dscrptr != given_dscrptr) free(dscrptr);
return error;
}
icb_len = 0;
switch (udf_node->addr_type) {
case UDF_ICB_SHORT_ALLOC :
icb_len = sizeof(struct short_ad);
break;
case UDF_ICB_LONG_ALLOC :
icb_len = sizeof(struct long_ad);
break;
default :
printf("UDF encountered an unknown allocation type %d\n", udf_node->addr_type);
break;
}
udf_node->icb_len = icb_len;
advance_sector = 1;
while (icb_len && data_length) {
switch (udf_node->addr_type) {
case UDF_ICB_SHORT_ALLOC :
s_ad = (struct short_ad *) pos;
len = udf_rw32(s_ad->len);
lb_num = udf_rw32(s_ad->lb_num);
vpart_num = cur_alloc->vpart_num;
break;
case UDF_ICB_LONG_ALLOC :
l_ad = (struct long_ad *) pos;
len = udf_rw32(l_ad->len);
lb_num = udf_rw32(l_ad->loc.lb_num);
vpart_num = udf_rw16(l_ad->loc.part_num);
if (l_ad->impl.im_used.flags & UDF_ADIMP_FLAGS_EXTENT_ERASED) {
printf("UDF: got a `extent erased' flag in a file's long_ad; ignoring\n");
}
break;
default :
printf("UDF encountered an unknown allocation type %d\n", udf_node->addr_type);
break;
}
/* ecma-167 48.14.1.1 */
flags = (uint8_t) ((uint32_t) (len >> 30) & 3);
len = len & ((1<<30)-1);
if (flags == UDF_SPACE_REDIRECT) {
/* fill in next extent */
next_alloc->len = len;
next_alloc->lb_num = lb_num;
next_alloc->vpart_num = vpart_num;
advance_sector = 0; /* don't advance to next sector */
icb_len = data_length; /* must be last */
DEBUG(printf("Continuing extent flagged at vpart = %d, lb_num = %d, len = %d\n", (int) vpart_num, (int) lb_num, (int) len));
} else {
if (len) {
alloc_entry = calloc(1, sizeof(struct udf_allocentry));
if (!alloc_entry) {
if (dscrptr != given_dscrptr) free(dscrptr);
return ENOMEM;
}
alloc_entry->len = len;
alloc_entry->lb_num = lb_num;
alloc_entry->vpart_num = vpart_num;
alloc_entry->flags = flags;
TAILQ_INSERT_TAIL(&udf_node->alloc_entries, alloc_entry, next_alloc);
}
calculated_len += len;
}
data_length -= icb_len;
pos += icb_len;
} /* while */
if (advance_sector) {
/* Note: UDF descriptor length is maximised to one sector */
next_alloc->lb_num++; /* advance one sector */
entries--;
}
} /* indirect ICB check */
if (entries) {
/* load in new dscrptr */
if (dscrptr != given_dscrptr) free(dscrptr);
error = udf_read_logvol_descriptor(udf_log_vol, next_alloc->vpart_num, next_alloc->lb_num, what, &dscrptr, NULL);
}
} while (entries && !error);
/* error from reading next sector in extent is not considered an error */
if (dscrptr != given_dscrptr && dscrptr) free(dscrptr);
if (calculated_len != (uint64_t) udf_node->stat.st_size) {
printf("UDF: create node length mismatch; stated as %g but calculated as %g bytes. fixing\n", (double) udf_node->stat.st_size, (double) calculated_len);
udf_node->stat.st_size = calculated_len;
}
return 0;
}
int udf_readin_udf_node(struct udf_node *dir_node, struct long_ad *udf_icbptr, struct fileid_desc *fid, struct udf_node **res_sub_node) {
struct udf_node *sub_node;
char *fid_name;
char entry_name[NAME_MAX];
uint32_t bucket;
ino_t hashkey;
int error;
assert(dir_node);
assert(udf_icbptr);
assert(fid);
assert(res_sub_node);
/* check if its allready in the logical volume's udf_node cache (inodes) */
hashkey = udf_calc_hash(udf_icbptr);
bucket = hashkey & UDF_INODE_HASHMASK;
LIST_FOREACH(sub_node, &dir_node->udf_log_vol->udf_nodes[bucket], next_node) {
if (sub_node->hashkey == hashkey) {
*res_sub_node = sub_node;
DEBUG(printf("found node in hashlist\n"));
return 0;
}
}
/* dump the FID we are trying to read in */
UDF_VERBOSE_MAX(udf_dump_descriptor((union dscrptr *) fid));
fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
udf_to_unix_name(entry_name, fid_name, fid->l_fi, &dir_node->udf_log_vol->log_vol->desc_charset);
/* build missing vnode */
error = udf_readin_anon_udf_node(dir_node->udf_log_vol, NULL, udf_icbptr, entry_name, &sub_node);
if (error)
return error;
if (!sub_node) {
printf("sub_node = NULL? and no error? \n");
}
assert(sub_node);
/* link this UDF node to the mountpoint and remember its hash-key */
sub_node->mountpoint = dir_node->mountpoint;
sub_node->hashkey = hashkey;
/* XXX use file version number and filechar from fid ? */
sub_node->file_version_num = udf_rw16(fid->file_version_num); /* user set */
sub_node->udf_filechar = fid->file_char;
/* insert/replace in mountpoint's udfnode hashtable with optional check for doubles */
if (0) {
struct udf_node *lookup;
LIST_FOREACH(lookup, &dir_node->udf_log_vol->udf_nodes[bucket], next_node) {
if (lookup->hashkey == sub_node->hashkey) printf("DOUBLE hashnode?\n");
}
}
LIST_INSERT_HEAD(&dir_node->udf_log_vol->udf_nodes[bucket], sub_node, next_node);
DEBUG(printf("inserting hash node for hash = %d\n", (int) hashkey));
*res_sub_node = sub_node;
return 0;
}
void udf_syncnode_callback(int reason, struct udf_wrcallback *wrcallback, int error, uint8_t *sectordata) {
/* struct udf_node *udf_node = (struct udf_node *) wrcallback->structure; */
wrcallback = wrcallback; /* unused for now */
sectordata = sectordata;
if (reason == UDF_WRCALLBACK_REASON_PENDING) {
/* what to do? */
return;
}
if (reason == UDF_WRCALLBACK_REASON_ANULATE) {
/* what to do? */
return;
}
assert(reason == UDF_WRCALLBACK_REASON_WRITTEN);
if (error) {
printf("UDF error: syncnode writing failed, not fixing yet!\n");
return;
}
}
/* XXX This mark node dirty code will simplify if we store nodes in buffers? XXX */
void udf_node_mark_dirty(struct udf_node *udf_node) {
struct udf_allocentry *alloc_entry, *my_entry, *tail_entry;
struct udf_node *search_node, *tail_node;
if (udf_node->dirty) return;
my_entry = TAILQ_FIRST(&udf_node->dscr_allocs);
assert(my_entry);
/* dscr locks ? */
UDF_MUTEX_LOCK(&udf_node->udf_log_vol->dirty_nodes_mutex);
if (1) {
/* insertion sort :-S */
tail_node = TAILQ_LAST(&udf_node->udf_log_vol->dirty_nodes, udf_node_list);
if (!tail_node) {
TAILQ_INSERT_TAIL(&udf_node->udf_log_vol->dirty_nodes, udf_node, next_dirty);
} else {
tail_entry = TAILQ_FIRST(&tail_node->dscr_allocs);
if (tail_entry->lb_num < my_entry->lb_num) {
TAILQ_INSERT_TAIL(&udf_node->udf_log_vol->dirty_nodes, udf_node, next_dirty);
} else {
/* find my place; could be done smarter */
TAILQ_FOREACH(search_node, &udf_node->udf_log_vol->dirty_nodes, next_dirty) {
alloc_entry = TAILQ_FIRST(&tail_node->dscr_allocs);
if (alloc_entry->lb_num > my_entry->lb_num) {
TAILQ_INSERT_BEFORE(search_node, udf_node, next_dirty);
break; /* foreach */
}
}
}
}
} else {
/* dumb */
TAILQ_INSERT_TAIL(&udf_node->udf_log_vol->dirty_nodes, udf_node, next_dirty);
}
UDF_MUTEX_UNLOCK(&udf_node->udf_log_vol->dirty_nodes_mutex);
udf_node->dirty = 1;
}
static void udf_node_unmark_dirty(struct udf_node *udf_node) {
if (!udf_node->dirty) return;
/* remove me just in case; why were we called otherwise? */
UDF_MUTEX_LOCK(&udf_node->udf_log_vol->dirty_nodes_mutex);
TAILQ_REMOVE(&udf_node->udf_log_vol->dirty_nodes, udf_node, next_dirty);
UDF_MUTEX_UNLOCK(&udf_node->udf_log_vol->dirty_nodes_mutex);
udf_node->dirty = 0;
}
/* VOP_FSYNC : data FDATASYNC */
int udf_sync_udf_node(struct udf_node *udf_node, char *why) {
DEBUG(printf("Syncing udf_node `%"PRIu64"` because of %s\n", udf_node->unique_id, why));
DEBUG(printf("sync udf node: dirty = %d, v_numoutput = %d\n", udf_node->dirty, udf_node->v_numoutput));
if (!udf_node->dirty) {
/* Not dirty??!!! */
udf_node_unmark_dirty(udf_node);
return 0;
}
if (!udf_node->udf_log_vol->writable) {
fprintf(stderr, "encountered a dirty node on a read-only filingsystem!\n");
exit(1);
}
/*
* We are really syncing disc but we are only continueing when the
* node itself is clean... not breaking the semantics.
*/
/* XXX flushall flag magic XXX */
udf_bufcache->flushall = 1;
udf_purgethread_kick("Sync node");
fflush(stdout);
/* wait until all dirty bufs associated with this node are processed */
if (!udf_node->dirty) return 0;
if (udf_node->v_numoutput) {
usleep(100);
}
if (!udf_node->v_numoutput) return 0;
UDF_VERBOSE(printf("(wait on node)"));
while (udf_node->v_numoutput) {
usleep(100);
}
return 0;
}
extern void udf_merge_allocentry_queue(struct udf_alloc_entries *queue, uint32_t lb_size);
/* VOP_FSYNC : metadata FFILESYNC */
/* writeout the udf_node back to disc */
/* TODO don't forget to writeout our extended attributes and write out the link to the associated streamdir as well */
int udf_writeout_udf_node(struct udf_node *udf_node, char *why) {
struct udf_wrcallback wr_callback;
struct udf_allocentry *dscr_entry, *next_dscr_entry, *alloc_entry;
union dscrptr *dscrptr;
struct file_entry *fe;
struct extfile_entry *efe;
struct alloc_ext_entry *aee;
struct lb_addr parent_icb;
struct icb_tag *icbtag;
struct long_ad *l_ad;
struct short_ad *s_ad;
uint32_t *l_adptr; /* points to length of alloc. descr. */
uint8_t *pos;
char *what;
uint32_t alloc_entries;
uint64_t rest; /* in bytes */
uint64_t len;
uint64_t logblks_rec;
uint32_t lb_size, descr_ver;
if (!udf_node->udf_log_vol->writable) {
fprintf(stderr, "encountered a dirty node on a read-only filingsystem!\n");
exit(1);
}
lb_size = udf_node->udf_log_vol->lb_size;
/* assure all file data is written out! and clean up */
udf_sync_udf_node(udf_node, why);
/* XXX node lock? XXX */
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
/* clean up allocentry queue so we get a nice clean layout */
udf_merge_allocentry_queue(&udf_node->alloc_entries, lb_size);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
/* allocate for descriptor */
dscrptr = calloc(1, lb_size); /* not a bit biggish? */
if (!dscrptr) return ENOMEM;
/* calculate logical blocks recorded; zero for interns [UDF 2.3.6.5, ECMA 4/14.9.11, 4/14.6.8] */
logblks_rec = 0;
if (udf_node->addr_type != UDF_ICB_INTERN_ALLOC) {
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
TAILQ_FOREACH(alloc_entry, &udf_node->alloc_entries, next_alloc) {
if (alloc_entry->flags == UDF_SPACE_ALLOCATED) {
logblks_rec += (alloc_entry->len + lb_size-1) / lb_size;
}
}
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
}
/* fill in (ext)fentry descriptor */
/* copy descriptor version from the logvol's */
bzero(&parent_icb, sizeof(struct lb_addr));
descr_ver = udf_rw16(udf_node->udf_log_vol->log_vol->tag.descriptor_ver);
if (descr_ver == 3) {
efe = &dscrptr->efe;
efe->tag.id = udf_rw16(TAGID_EXTFENTRY);
efe->tag.descriptor_ver = udf_rw16(descr_ver);
efe->ckpoint = udf_rw32(1); /* [ECMA 4/14.17.17] */
udf_set_imp_id(&efe->imp_id);
efe->logblks_rec = udf_rw64(logblks_rec);
/* set additional fileinfo (access, uid/gid) etc. */
udf_node_set_fileinfo(udf_node, dscrptr);
efe->obj_size = efe->inf_len; /* not true if there are streams [ECMA 4/48.17.11] */
icbtag = &efe->icbtag;
} else if (descr_ver == 2) {
fe = &dscrptr->fe;
fe->tag.id = udf_rw16(TAGID_FENTRY);
fe->tag.descriptor_ver = udf_rw16(descr_ver);
fe->ckpoint = udf_rw32(1); /* [ECMA 4/14.17.17] */
udf_set_imp_id(&fe->imp_id);
fe->logblks_rec = udf_rw64(logblks_rec);
/* set additional fileinfo (access, uid/gid) etc. */
udf_node_set_fileinfo(udf_node, dscrptr);
/* fe->obj_size doesn't exist */
icbtag = &fe->icbtag;
} else {
printf("UDF: i don't know this descriptor version %d\n", descr_ver);
free(dscrptr);
return EBADF;
}
/* update derived info */
udf_node->udf_icbtag_flags = (udf_node->udf_icbtag_flags & ~UDF_ICB_TAG_FLAGS_ALLOC_MASK) | udf_node->addr_type;
/* strategy 4 has no parent nodes */
/* XXX NO extended attributes recorded YET (!!) XXX (like device nodes !!! ) */
dscr_entry = TAILQ_FIRST(&udf_node->dscr_allocs);
/* ensure allocation of (ext) file descriptor */
if (!dscr_entry) {
/* have to allocate one and add to queue ! */
printf("UDF: XXX no allocation of file descriptor entry yet in sync_udf_node\n");
return ENOENT;
}
/* XXX implement alloc entry walker? XXX */
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
what = "UDF sync: File descriptor";
alloc_entry = TAILQ_FIRST(&udf_node->alloc_entries);
do {
assert(dscr_entry->len <= lb_size);
if (icbtag) {
/* fill in ICB fields */ /* XXX we're only writing out strategy type 4 !!! XXX */
icbtag->prev_num_dirs = udf_rw32(0); /* prev_alloc_entries = 0 due to strategy type 4 XXX */
icbtag->strat_type = udf_rw16(4); /* default UDF strategy type 4 XXX what about 4096? */
icbtag->parent_icb = parent_icb;
icbtag->file_type = udf_node->udf_filetype;
icbtag->flags = udf_rw16(udf_node->udf_icbtag_flags);
icbtag->max_num_entries = udf_rw16(1); /* due to strategy type 4 ! XXX */
}
pos = 0;
rest = 0;
l_adptr = NULL; /* invalid */
switch (udf_rw16(dscrptr->tag.id)) {
case TAGID_FENTRY :
/* not used now */
fe = &dscrptr->fe;
pos = &fe->data[0] + udf_rw32(fe->l_ea);
rest = dscr_entry->len - sizeof(struct file_entry) - udf_rw32(fe->l_ea);
l_adptr = &fe->l_ad;
break;
case TAGID_EXTFENTRY :
efe = &dscrptr->efe;
pos = &efe->data[0] + udf_rw32(efe->l_ea);
rest = dscr_entry->len - sizeof(struct extfile_entry) - udf_rw32(efe->l_ea);
l_adptr = &efe->l_ad;
break;
case TAGID_ALLOCEXTENT :
aee = &dscrptr->aee;
pos = &aee->data[0];
rest = dscr_entry->len - sizeof(struct alloc_ext_entry);
l_adptr = &aee->l_ad;
break;
case TAGID_INDIRECTENTRY :
/* do we even do these in strat 4 ? */
printf("UDF: sanity check; request for writeout of indirect entry\n");
free(dscrptr);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
return ENOENT; /* panic really */
}
assert(l_adptr);
/* fill in remaining allocation entries */
/* remember to keep one SPARE for extending */
alloc_entries = 0;
DEBUG(printf("Allocated at "));
if (udf_node->addr_type == UDF_ICB_INTERN_ALLOC) {
/* internal allocation -> copy in data */
assert(udf_node->intern_len <= rest);
bzero(pos, rest);
memcpy(pos, udf_node->intern_data, udf_node->intern_len);
/* make sure the length for allocation entries is the same as the data it contains (4/8.8.2, 4/14.6.8) */
*l_adptr = udf_rw32(udf_node->intern_len);
/* alloc_entry is zero, so it'll fall trough */
pos += udf_node->intern_len; /* advance pos for length calculation */
alloc_entry = NULL; /* not applicable */
}
while ((rest > 2*udf_node->icb_len) && alloc_entry) {
assert(udf_node->icb_len);
DEBUG(printf("[%p, lb_num = %d, len = %d] ", alloc_entry, (uint32_t) alloc_entry->lb_num, (uint32_t) alloc_entry->len));
/* XXX assumption: UDF_SPACE_* is equal to UDF flags XXX */
len = alloc_entry->len | (((uint32_t) alloc_entry->flags) << 30);
switch (udf_node->addr_type) {
case UDF_ICB_SHORT_ALLOC :
s_ad = (struct short_ad *) pos;
s_ad->len = udf_rw32(len);
s_ad->lb_num = udf_rw32(alloc_entry->lb_num);
assert(alloc_entry->vpart_num == 0);
if (alloc_entry->len == 0) s_ad->lb_num = udf_rw32(0);
if (alloc_entry->flags == UDF_SPACE_FREE) s_ad->lb_num = udf_rw32(0);
break;
case UDF_ICB_LONG_ALLOC :
l_ad = (struct long_ad *) pos;
l_ad->len = udf_rw32(len);
l_ad->loc.lb_num = udf_rw32(alloc_entry->lb_num);
l_ad->loc.part_num = udf_rw16(alloc_entry->vpart_num);
l_ad->impl.im_used.unique_id = udf_rw64(udf_node->unique_id);
if (alloc_entry->len == 0) l_ad->loc.lb_num = udf_rw32(0);
if (alloc_entry->flags == UDF_SPACE_FREE) l_ad->loc.lb_num = udf_rw32(0);
break;
}
alloc_entries++;
*l_adptr = udf_rw32(alloc_entries * udf_node->icb_len);
alloc_entry = TAILQ_NEXT(alloc_entry, next_alloc);
pos += udf_node->icb_len;
rest -= udf_node->icb_len;
}
DEBUG(printf("\nend alloc\n\n"));
next_dscr_entry = TAILQ_NEXT(dscr_entry, next_alloc);
if (alloc_entry) {
/* overflow */
/* ensure allocation of (ext) file descriptor */
if (!next_dscr_entry) {
/* have to allocate one and add to queue ! */
printf("UDF: XXX no allocation of allocation extent descriptor yet in sync_udf_node\n");
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
return ENOENT;
}
/* flag next extent being specified */
len = next_dscr_entry->len | ((uint32_t) UDF_SPACE_REDIRECT << 30);
switch (udf_node->addr_type) {
case UDF_ICB_SHORT_ALLOC :
s_ad = (struct short_ad *) pos;
s_ad->len = udf_rw32(len);
s_ad->lb_num = udf_rw32(next_dscr_entry->lb_num);
assert(next_dscr_entry->vpart_num == 0);
break;
case UDF_ICB_LONG_ALLOC :
l_ad = (struct long_ad *) pos;
l_ad->len = udf_rw32(len);
l_ad->loc.lb_num = udf_rw32(next_dscr_entry->lb_num);
l_ad->loc.part_num = udf_rw16(next_dscr_entry->vpart_num);
l_ad->impl.im_used.unique_id = udf_rw64(udf_node->unique_id);
break;
}
alloc_entries++;
pos += udf_node->icb_len;
rest -= udf_node->icb_len;
}
/* manage lengths */
dscrptr->tag.desc_crc_len = udf_rw16((pos - (uint8_t *) dscrptr) - UDF_DESC_TAG_LENGTH);
/* writeout */
wr_callback.function = udf_syncnode_callback;
#if 0
wr_callback.structure = (void *) udf_node;
wr_callback.vpart_num = dscr_entry->vpart_num;
wr_callback.lb_num = dscr_entry->lb_num;
wr_callback.offset = 0; /* ? */
wr_callback.length = dscr_entry->len;
#endif
UDF_VERBOSE_MAX(
udf_validate_tag_and_crc_sums(dscrptr); /* for dumping */
udf_dump_descriptor(dscrptr);
);
errno = udf_write_logvol_descriptor(udf_node->udf_log_vol, dscr_entry->vpart_num, dscr_entry->lb_num, what, dscrptr, &wr_callback);
/* advance */
if (alloc_entry) {
dscr_entry = next_dscr_entry;
assert(dscr_entry);
/* allocate and initialise new allocation extent descriptor */
/* also flag no icbtag follows */
bzero(dscrptr, lb_size);
aee = &dscrptr->aee;
aee->tag.id = udf_rw16(TAGID_ALLOCEXTENT);
aee->tag.descriptor_ver = udf_node->udf_log_vol->log_vol->tag.descriptor_ver;
icbtag = NULL;
what = "UDF sync: allocation extent";
}
} while (alloc_entry);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
/* XXX Free extra non used allocation extent descriptors XXX */
/* Mark me clean */
if (udf_node->mountpoint) {
/* remove me just in case; why were we called otherwise? */
udf_node_unmark_dirty(udf_node);
}
return 0;
}
/******************************************************************************************
*
* Space bitmap reader and writer
*
******************************************************************************************/
/* tested OK */
int udf_read_in_space_bitmap(struct udf_alloc_entries *queue, struct space_bitmap_desc *sbd, uint32_t lb_size, uint64_t *freespace) {
struct udf_allocentry *alloc_entry;
uint64_t bits, from, now, start, end;
uint8_t byte, bit, bitpos, state, *pos;
int cnt;
assert(udf_rw16(sbd->tag.id) == TAGID_SPACE_BITMAP);
DEBUG(printf("processing space bitmap : \n"););
bits = udf_rw32(sbd->num_bits);
/*
* Mark the disc as completely full;
* Bugallert: use `udf_mark_allocentry_queue()' for the extent might
* not fit in just one alloc entry for bigger discs
*/
assert(TAILQ_EMPTY(queue));
udf_mark_allocentry_queue(queue, lb_size, 0, bits * lb_size, UDF_SPACE_ALLOCATED, NULL, NULL);
pos = sbd->data;
from = 0; now = 0; bitpos = 0; byte = *pos; state = byte & 1;
*freespace = 0;
while (now < bits) {
if (bitpos == 0) {
byte = *pos++;
}
bit = byte & 1;
if (bit != state) {
if (state) {
start = from;
end = now-1;
/* printf("[%08d - %08d]", start, end); */
udf_mark_allocentry_queue(queue, lb_size, start*lb_size, (end-start+1)*lb_size, UDF_SPACE_FREE, NULL, NULL);
*freespace += (end-start+1)*lb_size;
}
from = now;
state = bit;
}
byte >>= 1;
bitpos = (bitpos+1) & 7;
now++;
}
if (state) {
start = from;
end = now;
/* printf("[%08d - %08d]", start, end); */
udf_mark_allocentry_queue(queue, lb_size, start*lb_size, (end-start)*lb_size, UDF_SPACE_FREE, NULL, NULL);
*freespace += (end-start)*lb_size;
}
UDF_VERBOSE_TABLES(
printf("\t\tFree space found on this partition");
cnt = 0;
start = 0;
TAILQ_FOREACH(alloc_entry, queue, next_alloc) {
if (alloc_entry->flags == UDF_SPACE_ALLOCATED) {
/* printf("... "); */
} else {
if (cnt == 0) printf("\n\t\t\t");
printf("[%08"PRIu64" - %08"PRIu64"] ", start / lb_size, ((start + alloc_entry->len) / lb_size)-1);
cnt++; if (cnt > 4) cnt = 0;
}
start += alloc_entry->len;
}
printf("\n");
);
/* merge is not nessisary */
return 0;
}
/* inverse of readin space bitmap; it synchronises the bitmap with the queue */
/* tested OK */
int udf_sync_space_bitmap(struct udf_alloc_entries *queue, struct space_bitmap_desc *sbd, uint32_t lb_size) {
struct udf_allocentry *alloc_entry;
uint32_t start, bits, total_bits;
uint32_t cnt, byte;
uint8_t bit, bitmask, setting;
uint8_t *pos;
/* merge it just in case */
udf_merge_allocentry_queue(queue, lb_size);
total_bits = udf_rw32(sbd->num_bits);
DEBUG(printf("SYNC SPACE BITMAP DEBUG: total bits = %d\n", total_bits));
alloc_entry = TAILQ_FIRST(queue);
start = alloc_entry->lb_num;
assert(start == 0);
TAILQ_FOREACH(alloc_entry, queue, next_alloc) {
DEBUG(printf(" [%d : %d + %d]", alloc_entry->flags, alloc_entry->lb_num, alloc_entry->len));
bits = alloc_entry->len / lb_size;
assert(bits*lb_size == alloc_entry->len);
byte = start / 8;
bit = start - byte*8;
pos = sbd->data + byte;
if (byte*8 + bit + bits > total_bits) { /* XXX > or >= ? */
/* this should NEVER happen */
printf("UDF: not enough space writing back space bitmap! HELP!\n");
return EBADF;
}
cnt = 0;
setting = (alloc_entry->flags != UDF_SPACE_FREE) ? 0 : 255;
while (cnt < bits) {
bitmask = (1 << bit);
/* simple sanity check */
if (byte*8 + bit >= total_bits) {
printf("IEEEE!!!! too big; %d instead of %d\n", (byte*8 + bit), total_bits);
}
*pos = (*pos & (~bitmask)) | (setting ? bitmask: 0);
cnt++;
bit++;
if (bit == 8) {
/* byte transition */
byte++; bit = 0;
pos++;
#if 0
/* speedup by doing bytes at a time */
while (bits-cnt > 8) {
*pos = setting;
cnt += 8;
pos++; byte++;
}
#endif
}
}
start += bits;
}
DEBUG(printf("\n\n"));
return 0;
}
/******************************************************************************************
*
* Filepart readers and writers
*
******************************************************************************************/
/* part of VOP_STRATEGY */
/* internal function; reads in a new buffer */
/* !!! bufcache lock ought to be held on entry !!! */
int udf_readin_file_buffer(struct udf_node *udf_node, char *what, uint32_t sector, int cache_flags, struct udf_buf **buf_entry_p) {
struct udf_allocentry *alloc_entry;
struct udf_buf *buf_entry;
uint64_t cur_offset;
uint64_t overlap_length, overlap_sectors, transfer_length;
uint32_t lb_size;
uint32_t len, lb_num, vpart_num;
int32_t error;
uint8_t flags;
assert(udf_node);
assert(buf_entry_p);
assert(udf_bufcache->bufcache_lock.locked);
error = udf_get_buf_entry(udf_node, buf_entry_p);
if (error) return error;
buf_entry = *buf_entry_p;
lb_size = udf_node->udf_log_vol->lb_size;
/* internal node? */
if (udf_node->addr_type == UDF_ICB_INTERN_ALLOC) {
buf_entry->b_lblk = 0;
buf_entry->b_flags = 0; /* not dirty, not needing alloc */
buf_entry->b_bcount = udf_node->intern_len;
buf_entry->b_resid = lb_size - udf_node->intern_len;
memcpy(buf_entry->b_data, udf_node->intern_data, udf_node->intern_len);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_attach_buf_to_node(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
return error;
}
/* `normal' node */
/* find sector in the allocation space */
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
cur_offset = 0;
error = EIO; /* until proven readable */
TAILQ_FOREACH(alloc_entry, &udf_node->alloc_entries, next_alloc) {
len = alloc_entry->len;
lb_num = alloc_entry->lb_num;
vpart_num = alloc_entry->vpart_num;
flags = alloc_entry->flags;
/* check overlap with this alloc entry */
if (cur_offset + len > sector * lb_size) {
/* found an extent that fits */
assert(((sector * lb_size - cur_offset) % lb_size) == 0); /* ought to be on sector boundary */
lb_num += sector - (cur_offset / lb_size);
overlap_length = cur_offset + len - sector * lb_size;
overlap_sectors = (overlap_length + lb_size -1) / lb_size;
transfer_length = MIN(lb_size, overlap_length); /* max one logical sector */
/* fill in new buf info */
buf_entry->b_lblk = sector;
buf_entry->b_bcount = transfer_length;
buf_entry->b_resid = lb_size - transfer_length;
/* sector transfered; mark valid */
buf_entry->b_flags = 0; /* not dirty and valid */
switch (flags) {
case UDF_SPACE_ALLOCATED :
/* on disc or in the write queue/cache to be written out; read one block at a time */
error = udf_read_logvol_sector(udf_node->udf_log_vol, vpart_num, lb_num, what, buf_entry->b_data, overlap_sectors, cache_flags);
break;
case UDF_SPACE_FREE :
/* fall trough */
case UDF_SPACE_ALLOCATED_BUT_NOT_USED :
error = 0;
bzero(buf_entry->b_data, lb_size);
break;
default :
fprintf(stderr, "Got an redirect flag, can't happen\n");
break;
}
if (error) {
fprintf(stderr, "\tgot error from read_logvol_sector : %s\n", strerror(error));
break; /* FOREACH */
}
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_attach_buf_to_node(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
return 0;
} /* looking for overlap */
cur_offset += len;
} /* FOREACH */
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
*buf_entry_p = NULL;
udf_mark_buf_clean(udf_node, buf_entry); /* its destroyed so not dirty */
udf_mark_buf_allocated(udf_node, buf_entry); /* i.e. taken care of */
udf_free_buf_entry(buf_entry);
return error;
}
/* internal function; shedule writing out of a buffer */
/* part of VOP_STRATEGY */
int udf_writeout_file_buffer(struct udf_node *udf_node, char *what, int cache_flags, struct udf_buf *buf_entry) {
struct udf_allocentry *from_alloc, *to_alloc, *alloc_entry;
uint32_t lb_num, lb_size, lblk;
uint16_t vpart_num;
int error;
what = what; /* not used now */
if (!udf_node->udf_log_vol->writable) {
/* ieek */
fprintf(stderr, "write request from non writable file buffer?\n");
}
/* first get logical block offset and block size */
lblk = buf_entry->b_lblk;
lb_size = udf_node->udf_log_vol->lb_size;
error = 0;
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
/* maybe a bit too strict locking; code is not winning the beauty contest */
/* check if we can convert/save disc space */
if (udf_node->stat.st_size <= udf_node->intern_free) {
/* convert to internal alloc with backup storage (writing there) */
if (udf_node->addr_type != UDF_ICB_INTERN_ALLOC) {
DEBUG(printf("CONVERTING to intern alloc\n"));
/* first free all previously allocated sectors (if any) */
error = udf_node_release_extent(udf_node, 0, udf_node->stat.st_size);
}
assert(!error);
if (!udf_node->intern_data) {
udf_node->intern_data = calloc(1, udf_node->intern_free);
}
if (udf_node->intern_data) {
assert(buf_entry->b_bcount <= udf_node->intern_free);
memcpy(udf_node->intern_data, buf_entry->b_data, buf_entry->b_bcount);
udf_node->intern_len = buf_entry->b_bcount;
udf_node->addr_type = UDF_ICB_INTERN_ALLOC;
/* we don't write here: mark buffer clean */
udf_mark_buf_clean(udf_node, buf_entry);
udf_mark_buf_allocated(udf_node, buf_entry); /* signal its allocated */
buf_entry->b_flags &= ~(B_ERROR);
/* check if all buffers are gone now and mark node for writeout to indicate state change */
assert(udf_node->v_numoutput == 0);
udf_node_mark_dirty(udf_node);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
return 0;
}
/* fall trough ... for some reason it isn't converted */
} else {
if (udf_node->addr_type == UDF_ICB_INTERN_ALLOC) {
DEBUG(printf("CONVERTING to normal alloc\n"));
/* won't fit anymore, have to turn it into a `normal' alloced */
udf_node->intern_len = 0;
if (udf_node->intern_data)
free(udf_node->intern_data);
udf_node->intern_data = NULL;
udf_node->icb_len = sizeof(struct long_ad);
udf_node->addr_type = UDF_ICB_LONG_ALLOC;
udf_node_mark_dirty(udf_node);
/* mark needalloc to make sure it gets an address */
udf_mark_buf_needing_allocate(udf_node, buf_entry); /* signal it needs allocation */
}
}
/* merge queue first to get better performance */
udf_merge_allocentry_queue(&udf_node->alloc_entries, lb_size);
/* get extent that it represents */
udf_mark_allocentry_queue(&udf_node->alloc_entries, lb_size, (uint64_t) lblk*lb_size, buf_entry->b_bcount, UDF_SPACE_ALLOCATED, &from_alloc, &to_alloc);
alloc_entry = from_alloc;
if (buf_entry->b_flags & B_NEEDALLOC) {
/* need allocation */
error = udf_node_allocate_lbs(udf_node, /* num lb */ 1, &vpart_num, &lb_num, NULL);
assert(!error);
udf_mark_buf_allocated(udf_node, buf_entry);
alloc_entry->lb_num = lb_num;
alloc_entry->vpart_num = vpart_num;
}
assert(TAILQ_NEXT(alloc_entry, next_alloc) == to_alloc || (alloc_entry == to_alloc));
lb_num = alloc_entry->lb_num;
vpart_num = alloc_entry->vpart_num;
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
/* printf("writing out filebuffer vpart %d, lb_num %d for %s\n", vpart_num, lb_num, what); */
error = udf_write_logvol_sector(udf_node->udf_log_vol, vpart_num, lb_num, "File contents", buf_entry->b_data, cache_flags, NULL);
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
if (error) {
printf("YIKES error during writing of logvol_sector\n");
udf_mark_buf_needing_allocate(udf_node, buf_entry);
buf_entry->b_flags |= B_ERROR;
/* buf_entry->b_errno = error; */
} else {
udf_mark_buf_clean(udf_node, buf_entry);
buf_entry->b_flags &= ~(B_ERROR);
}
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
return error;
}
/* VOP_READ */
int udf_read_file_part_uio(struct udf_node *udf_node, char *what, int content, struct uio *data_uio) {
struct udf_buf *buf_entry;
off_t offset;
uint64_t sector, lb_size, data_length;
uint8_t *base;
int error, short_buf;
if (!udf_node) return EINVAL;
/* NOTE: we are NOT marking the node dirty only by reading it */
udf_set_timespec_now(&udf_node->stat.st_atimespec);
if (udf_node->stat.st_size == 0) {
if (data_uio->uio_resid) return EIO; /* reading past end by default */
return 0;
}
lb_size = udf_node->udf_log_vol->lb_size;
error = 0;
while (data_uio->uio_resid) {
error = 0;
short_buf = 0;
sector = data_uio->uio_offset / lb_size;
/* lookup sector in buffer set */
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
udf_lookup_node_buf(udf_node, sector, &buf_entry);
if (!buf_entry || (buf_entry && (buf_entry->b_lblk != sector))) {
/* `page in' sector from file */
error = udf_readin_file_buffer(udf_node, what, sector, content, &buf_entry);
}
if (!error && buf_entry) {
offset = data_uio->uio_offset - (off_t) sector*lb_size;
base = buf_entry->b_data;
if (offset >= 0) {
data_length = buf_entry->b_bcount - offset;
data_length = MIN(data_length, data_uio->uio_resid);
uiomove(base + offset, data_length, data_uio);
}
short_buf = (buf_entry->b_bcount < lb_size); /* short buf -> none will follow */
}
assert(!error || (error && !buf_entry));
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
if (error) break; /* while */
if (data_uio->uio_resid == 0) return 0; /* finished? */
if (short_buf) break; /* while */
} /* while */
if (data_uio->uio_resid) {
printf("UDF: WARNING file is truncated; missing %d bytes while reading %s\n",
(int) data_uio->uio_resid, what);
return EIO;
}
return error;
}
/* XXX meaning is to be changed; kept for reference still XXX */
void udf_filepart_write_callback(int reason, struct udf_wrcallback *wrcallback, int error, uint8_t *sectordata) {
/* struct udf_node *udf_node = (struct udf_node *) wrcallback->structure; */
wrcallback = wrcallback; /* unused for now */
sectordata = sectordata;
if (reason == UDF_WRCALLBACK_REASON_PENDING) {
/* what to do? */
return;
}
if (reason == UDF_WRCALLBACK_REASON_ANULATE) {
/* what to do? */
return;
}
assert(reason == UDF_WRCALLBACK_REASON_WRITTEN);
if (error) {
printf("UDF error: file part write failed, not fixing yet!\n");
return;
}
}
/* VOP_TRUNCATE */
/* doesn't lock udf_node */
int udf_truncate_node(struct udf_node *udf_node, uint64_t length /* ,ioflags */) {
struct udf_log_vol *udf_log_vol;
struct udf_allocentry *alloc_entry, *cut_point;
struct udf_buf *buf_entry, *marker;
uint32_t lb_size;
uint64_t cur_extent, block_extent, new_extent, too_much;
uint32_t last_sector;
int32_t error;
if (!udf_node) return EINVAL;
if (udf_open_logvol(udf_node->udf_log_vol))
return EROFS;
udf_log_vol = udf_node->udf_log_vol;
lb_size = udf_log_vol->lb_size;
/* we might change this node AND access times ... */
if (!udf_node->dirty) {
/* mark node as dirty */
udf_node_mark_dirty(udf_node);
}
/* XXX lock node !!! XXX */
/* merge known allocation entry queue */
new_extent = length;
block_extent = (uint64_t) lb_size * ((new_extent + lb_size-1) / lb_size); /* inclusive */
too_much = block_extent - new_extent;
assert(block_extent >= new_extent);
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
udf_merge_allocentry_queue(&udf_node->alloc_entries, lb_size);
/* extend the file when nessisary */
if (new_extent > (uint64_t) udf_node->stat.st_size) {
if (udf_node->addr_type == UDF_ICB_INTERN_ALLOC) {
/* XXX to seperate function XXX */
/* grow intern node by converting it to a normal buffer first */
/* First lookup if the node already had a buffer associated */
udf_lookup_node_buf(udf_node, 0, &buf_entry);
if (!buf_entry) {
error = udf_get_buf_entry(udf_node, &buf_entry);
if (error) {
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
return error;
}
buf_entry->b_lblk = 0;
buf_entry->b_flags = 0;
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_attach_buf_to_node(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
} else {
DEBUG(printf("found buffer associated with intern node!\n"));
}
buf_entry->b_bcount = MIN(lb_size, new_extent);
buf_entry->b_resid = lb_size - buf_entry->b_bcount;
memcpy(buf_entry->b_data, udf_node->intern_data, udf_node->intern_len);
memset(buf_entry->b_data + udf_node->intern_len, 0, lb_size - udf_node->intern_len);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_mark_buf_dirty(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
udf_node->intern_len = 0;
if (udf_node->intern_data)
free(udf_node->intern_data);
udf_node->intern_data = NULL;
udf_node->icb_len = sizeof(struct long_ad);
udf_node->addr_type = UDF_ICB_LONG_ALLOC;
}
udf_cut_allocentry_queue(&udf_node->alloc_entries, lb_size, block_extent);
if (new_extent < block_extent) {
alloc_entry = TAILQ_LAST(&udf_node->alloc_entries, udf_alloc_entries);
assert(alloc_entry->len > too_much);
alloc_entry->len -= too_much;
}
/* XXX Andrey suggested buffer extension? XXX */
udf_node->stat.st_size = new_extent;
}
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
/* `free' the extent when shrinking the extent */
if (new_extent < (uint64_t) udf_node->stat.st_size) {
DEBUG(printf("TRIMMING file %"PRIu64" from %d to %d\n", udf_node->unique_id, (int32_t) udf_node->stat.st_size, (uint32_t) length));
/* free file buffers that are not needed anymore */
/* XXX NetBSD kernel : uvm_vnp_setsize() XXX */
marker = calloc(1, sizeof(struct udf_buf));
if (!marker) return ENOMEM;
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
/* use marker as we are going to delete items in the list we are traversing */
last_sector = new_extent / lb_size;
TAILQ_INSERT_HEAD(&udf_node->vn_bufs, marker, b_vnbufs);
while ((buf_entry = TAILQ_NEXT(marker, b_vnbufs))) {
/* advance marker */
TAILQ_REMOVE(&udf_node->vn_bufs, marker, b_vnbufs);
TAILQ_INSERT_AFTER(&udf_node->vn_bufs, buf_entry, marker, b_vnbufs);
/* process buf_entry */
if (buf_entry->b_lblk > last_sector) {
udf_mark_buf_clean(udf_node, buf_entry); /* its destroyed so not dirty */
udf_mark_buf_allocated(udf_node, buf_entry); /* i.e. taken care of */
udf_detach_buf_from_node(udf_node, buf_entry);
udf_free_buf_entry(buf_entry);
}
/* trim last buffer entry */
/* XXX NetBSD kernel : uvm_vnp_zerorange(), vtruncbuf() XXX */
if (buf_entry->b_lblk == last_sector) {
buf_entry->b_bcount = udf_node->stat.st_size % lb_size;
buf_entry->b_resid = buf_entry->b_bufsize - buf_entry->b_bcount;
/* still data to record? */
if (buf_entry->b_bcount == 0) {
/* marker not an issue here */
udf_mark_buf_clean(udf_node, buf_entry); /* its destroyed so not dirty */
udf_mark_buf_allocated(udf_node, buf_entry); /* i.e. taken care of */
udf_detach_buf_from_node(udf_node, buf_entry);
udf_free_buf_entry(buf_entry);
}
}
}
TAILQ_REMOVE(&udf_node->vn_bufs, marker, b_vnbufs);
free(marker);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
if (udf_node->addr_type == UDF_ICB_INTERN_ALLOC) {
/* shrink intern node */
too_much = udf_node->stat.st_size - new_extent;
udf_node->intern_len -= too_much;
udf_node->intern_free += too_much;
memset(udf_node->intern_data + new_extent, 0, too_much);
} else {
/* shrink alloc entries _after_ deleting bufs to make sure we are not crossed */
error = udf_node_release_extent(udf_node, block_extent, udf_node->stat.st_size);
/* after `cleanup', trim now unused entries */
udf_merge_allocentry_queue(&udf_node->alloc_entries, lb_size);
if (new_extent == 0) {
/* remove all; most common case too */
while ((alloc_entry = TAILQ_FIRST(&udf_node->alloc_entries))) {
TAILQ_REMOVE(&udf_node->alloc_entries, alloc_entry, next_alloc);
free(alloc_entry);
}
cur_extent = 0;
} else {
/* move code to udf_allocentries.c ? */
udf_cut_allocentry_queue(&udf_node->alloc_entries, lb_size, block_extent);
/* find cut-point */
cur_extent = 0;
TAILQ_FOREACH(alloc_entry, &udf_node->alloc_entries, next_alloc) {
cur_extent += alloc_entry->len;
if (cur_extent == block_extent) break;
}
cut_point = alloc_entry; /* all after this point need to be deleted */
assert(cut_point);
while ((alloc_entry = TAILQ_NEXT(cut_point, next_alloc))) {
TAILQ_REMOVE(&udf_node->alloc_entries, alloc_entry, next_alloc);
free(alloc_entry);
}
/* clip last entry to the correct size */
if (new_extent < block_extent) {
assert(too_much == block_extent - new_extent);
alloc_entry = TAILQ_LAST(&udf_node->alloc_entries, udf_alloc_entries);
assert(alloc_entry->len > too_much);
alloc_entry->len -= too_much;
cur_extent -= too_much;
}
}
assert(cur_extent == new_extent);
}
udf_node->stat.st_size = new_extent;
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
}
return 0;
}
/* VOP_WRITE */
int udf_write_file_part_uio(struct udf_node *udf_node, char *what, int content, struct uio *data_uio) {
struct udf_buf *buf_entry;
off_t offset;
uint64_t new_possible_extent;
uint64_t start_extent, end_extent;
uint64_t lb_size, sector, data_length;
uint8_t *base;
int error, appending, allocated;
if (!udf_node) return EINVAL;
if (udf_open_logvol(udf_node->udf_log_vol))
return EROFS;
/* write chanches both modification and file status times */
udf_set_timespec_now(&udf_node->stat.st_ctimespec);
udf_set_timespec_now(&udf_node->stat.st_mtimespec);
/* Zero length write -> finished */
if (data_uio->uio_resid == 0) return 0;
/* TODO lock node directly to avoid multiple writers entering */
/* pthread_rwlock_wrlock(&udf_node->udf_node_lock); */
if (!udf_node->dirty) {
udf_node_mark_dirty(udf_node);
}
/* auto-extent file */
appending = 0;
allocated = 0;
new_possible_extent = data_uio->uio_offset + data_uio->uio_resid;
if (new_possible_extent >= (uint64_t) udf_node->stat.st_size) {
/* BUGALERT: extra space can be pre-allocated AFTER file length */
error = udf_truncate_node(udf_node, new_possible_extent);
appending = 1;
}
lb_size = udf_node->udf_log_vol->lb_size;
while (data_uio->uio_resid) {
error = 0;
sector = data_uio->uio_offset / lb_size;
/* lookup sector in buffer set */
UDF_MUTEX_LOCK(&udf_bufcache->bufcache_lock);
udf_lookup_node_buf(udf_node, sector, &buf_entry);
if (!buf_entry || (buf_entry && (buf_entry->b_lblk != sector))) {
/* not found in cache; `page in' sector from file BUT ONLY if we don't completely overwrite it anyway */
if ((data_uio->uio_resid < lb_size) && (!appending)) {
DEBUG(printf("Reading in file buffer for %s for %"PRIu64" bytes\n", what, (uint64_t) data_uio->uio_resid));
error = udf_readin_file_buffer(udf_node, what, sector, content, &buf_entry);
}
/* check if the extent is allocated; check assumption that the size of a buffer is a lb_size */
if (buf_entry)
assert(buf_entry->b_bufsize == lb_size);
UDF_MUTEX_LOCK(&udf_node->alloc_mutex);
start_extent = (uint64_t) lb_size * sector;
end_extent = MIN((uint64_t) udf_node->stat.st_size, start_extent + lb_size);
error = udf_extent_properties(&udf_node->alloc_entries, lb_size, start_extent, end_extent, &allocated);
UDF_MUTEX_UNLOCK(&udf_node->alloc_mutex);
/* check free space if space is to allocated */
if (!buf_entry || !allocated) {
/* be coulant on metadata here to avoid not to easy to solve resource problems */
if (content == UDF_C_USERDATA) {
/* check for space no space anymore for userdata (bit on the safe side really) */
assert(udf_node->udf_log_vol);
if (!udf_confirm_freespace(udf_node->udf_log_vol, content, lb_size)) {
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
return ENOSPC;
}
}
}
DEBUG(if (allocated) printf("Writing pre-allocated buffer\n"));
if (!buf_entry) {
/* create new buffer */
error = udf_get_buf_entry(udf_node, &buf_entry);
if (error) {
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
return error;
}
/* don't forget to set the relative block number! */
buf_entry->b_lblk = sector;
/* add it to the buffer list */
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_attach_buf_to_node(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
}
assert(buf_entry);
if (allocated) {
/*
* we could free the old allocated extent and mark it needing allocation
* again
*/
}
if (!allocated) {
/* mark it needs to be allocated, locks are not very nice here */
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_mark_buf_needing_allocate(udf_node, buf_entry);
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
}
}
if (error) {
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
break; /* while */
}
assert(buf_entry);
offset = data_uio->uio_offset - (off_t) sector*lb_size;
base = buf_entry->b_data;
assert(offset >= 0);
if (offset >= 0) {
UDF_MUTEX_LOCK(&udf_node->buf_mutex);
udf_mark_buf_dirty(udf_node, buf_entry);
data_length = buf_entry->b_bufsize - offset;
data_length = MIN(data_length, data_uio->uio_resid);
uiomove(base + offset, data_length, data_uio);
buf_entry->b_bcount = MAX(buf_entry->b_bcount, offset + data_length);
buf_entry->b_resid = buf_entry->b_bufsize - buf_entry->b_bcount;
UDF_MUTEX_UNLOCK(&udf_node->buf_mutex);
}
UDF_MUTEX_UNLOCK(&udf_bufcache->bufcache_lock);
if (data_uio->uio_resid == 0) return 0; /* finished? */
} /* while */
return 0;
}
/******************************************************************************************
*
* UDF volume and descriptor logic
*
******************************************************************************************/
struct udf_volumeset *udf_search_volumeset(char *volset_id) {
struct udf_volumeset *volumeset;
struct udf_pri_vol *primary;
/* XXX this is a bit ugly XXX */
SLIST_FOREACH(volumeset, &udf_volumeset_list, next_volumeset) {
primary = STAILQ_FIRST(&volumeset->primaries);
assert(primary->pri_vol);
if (memcmp(primary->pri_vol->volset_id, volset_id, 128) == 0) return volumeset;
}
return NULL;
}
struct udf_pri_vol *udf_search_primary(struct udf_volumeset *set, char *id) {
struct udf_pri_vol *primary;
STAILQ_FOREACH(primary, &set->primaries, next_primary) {
assert(primary->pri_vol);
if (memcmp(primary->pri_vol->vol_id, id, 32) == 0) return primary;
}
return NULL;
}
struct udf_log_vol *udf_search_logical_volume_in_primary(struct udf_pri_vol *primary, char *logvol_id) {
struct udf_log_vol *here;
SLIST_FOREACH(here, &primary->log_vols, next_logvol) {
if (memcmp(here->log_vol->logvol_id, logvol_id, 128) == 0) return here;
}
return NULL;
}
/* called not very often ... ; free incomming when not needed */
int udf_proc_pri_vol(struct udf_session *udf_session, struct udf_pri_vol **current, struct pri_vol_desc *incomming) {
struct udf_volumeset *volset;
struct udf_pri_vol *primary;
assert(current);
volset = udf_search_volumeset(incomming->volset_id);
if (!volset) {
/* create a volume set */
volset = calloc(1, sizeof(struct udf_volumeset));
if (!volset) {
free(incomming);
return ENOMEM;
}
/* populate and link in */
volset->max_partnum = 0;
STAILQ_INIT(&volset->primaries);
SLIST_INSERT_HEAD(&udf_volumeset_list, volset, next_volumeset);
}
assert(volset);
primary = udf_search_primary(volset, incomming->vol_id);
*current = primary;
if (!primary) {
/* create a primary volume */
primary = calloc(1, sizeof(struct udf_pri_vol));
if (!primary) {
free(incomming);
return ENOMEM;
}
/* add to volset's primaries list */
STAILQ_INSERT_TAIL(&volset->primaries, primary, next_primary);
*current = primary;
} else {
/* mark as current */
/* ok ... we now need to check if this new descriptor is a newer version */
if (udf_rw32(incomming->seq_num) <= udf_rw32(primary->pri_vol->seq_num)) {
if (udf_session->session_num <= (*current)->udf_session->session_num) {
DEBUG(printf("UDF: DISCARDING primary descriptor for its the same but higher session number\n"));
/* its an older one; ignore */
free(incomming);
return 0;
}
}
DEBUG(printf("UPDATING primary descriptor for it has a higher session number\n"));
}
/* update the primary volume descriptor */
if (primary->pri_vol) free(primary->pri_vol);
primary->volumeset = volset;
primary->pri_vol = incomming;
primary->udf_session = udf_session;
return 0;
}
/* not called often ; free lvid when not used */
int udf_proc_logvol_integrity(struct udf_log_vol *udf_log_vol, struct logvol_int_desc *new_lvid) {
struct udf_logvol_info *impl;
uint64_t psize, pfree;
uint32_t *free_space_pos, *size_pos;
uint32_t lb_size, part_map;
uint32_t integrity;
int error, tagid;
error = udf_check_tag((union dscrptr *) new_lvid);
if (error) {
return error; /* return error on faulty tag */
}
tagid = udf_rw16(new_lvid->tag.id);
/* getting a terminator tag or zero is an OK condition */
if ((tagid == TAGID_TERM) || (tagid == 0)) {
return 0;
}
/* not getting an logical volume itegrity volume descriptor is an error now */
if (tagid != TAGID_LOGVOL_INTEGRITY) {
printf("IEE! got a %d tag while searching for a logical volume integrity descriptor\n", tagid);
return EINVAL; /* XXX error code? XXX */
}
/* check CRC on the contents of the logvol integrity */
error = udf_check_tag_payload((union dscrptr *) new_lvid);
if (error) {
return error;
}
/* check logvol integrity validity */
integrity = udf_rw32(new_lvid->integrity_type);
if ((integrity != UDF_INTEGRITY_OPEN) && (integrity != UDF_INTEGRITY_CLOSED))
return EINVAL;
/* allways go for the next in line; silly but thats Ecma-167/UDF */
/* process information contained in logical volume integrity descriptor */
udf_log_vol->logvol_state = integrity;
udf_log_vol->integrity_serial = udf_rw16(new_lvid->tag.serial_num);
impl = (struct udf_logvol_info *) (new_lvid->tables + 2*udf_rw32(new_lvid->num_part));
udf_log_vol->min_udf_readver = udf_rw16(impl->min_udf_readver);
udf_log_vol->min_udf_writever = udf_rw16(impl->min_udf_writever);
udf_log_vol->max_udf_writever = udf_rw16(impl->max_udf_writever);
udf_log_vol->num_files = udf_rw32(impl->num_files);
udf_log_vol->num_directories = udf_rw32(impl->num_directories);
udf_log_vol->next_unique_id = udf_rw64(new_lvid->lvint_next_unique_id);
/* calculate free space from this integrity descritor */
lb_size = udf_log_vol->lb_size;
/* init start positions */
free_space_pos = &new_lvid->tables[0];
size_pos = &new_lvid->tables[udf_log_vol->num_part_mappings];
/* init counters */
udf_log_vol->total_space = udf_log_vol->free_space = udf_log_vol->await_alloc_space = 0;
for (part_map = 0; part_map < udf_log_vol->num_part_mappings; part_map++) {
psize = udf_rw32(*size_pos); size_pos++;
pfree = udf_rw32(*free_space_pos); free_space_pos++;
if (pfree != UINT_MAX) {
/* if UINT_MAX, its not applicable like virtual space partitions */
udf_log_vol->total_space += (uint64_t) psize * lb_size;
udf_log_vol->free_space += (uint64_t) pfree * lb_size;
}
}
UDF_VERBOSE(
if (udf_log_vol->logvol_state == UDF_INTEGRITY_OPEN) {
udf_dump_timestamp("\t\t\t\tmarked open at ", &new_lvid->time);
} else {
udf_dump_timestamp("\t\t\t\tmarked closed at ", &new_lvid->time);
}
);
return 0;
}
void udf_derive_new_logvol_integrity(struct udf_log_vol *udf_log_vol) {
udf_log_vol->logvol_state = UDF_INTEGRITY_OPEN;
udf_log_vol->integrity_serial = 1;
/* analyse the log vol to check out minimum and maximum read/write versions */
if (udf_rw16(udf_log_vol->log_vol->tag.descriptor_ver) == 2) {
udf_log_vol->min_udf_readver = 0x0102;
udf_log_vol->min_udf_writever = 0x0150;
udf_log_vol->max_udf_writever = 0x0150;
} else {
udf_log_vol->min_udf_readver = 0x0201;
udf_log_vol->min_udf_writever = 0x0201;
udf_log_vol->max_udf_writever = 0x0201; /* 2.50? */
}
udf_log_vol->num_files = 0;
udf_log_vol->num_directories = 0;
udf_log_vol->next_unique_id = 16; /* zero first, rest 15/16+ minimum */
}
int udf_proc_logvol_integrity_sequence(struct udf_log_vol *udf_log_vol) {
union dscrptr *dscr;
uint32_t sector, lvid_len, num_sectors;
uint32_t lb_size;
int length;
int error;
sector = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.loc);
length = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.len);
lb_size = udf_log_vol->lb_size;
/* go for the default `open' integrity first as initialisation */
udf_derive_new_logvol_integrity(udf_log_vol);
if (!length) {
fprintf(stderr, "UDF: no volume integrity descriptor sequence space defined... OK for Ecma-167, not for UDF; rejecting\n");
return EBADF;
}
error = 0;
while (length > 0) {
UDF_VERBOSE_TABLES(
printf("\t\tLogical volume integrity extent at sector %d for %d bytes\n", sector, length)
);
error = udf_read_session_descriptor(udf_log_vol->primary->udf_session, sector, "Logical volume integrity descriptor (LVID)", &dscr, &lvid_len);
if (error) {
if (dscr) free(dscr);
dscr = NULL;
break;
}
UDF_VERBOSE_MAX(udf_dump_descriptor(dscr));
error = udf_proc_logvol_integrity(udf_log_vol, &dscr->lvid);
if (error) break;
if ((udf_rw16(dscr->tag.id) == TAGID_TERM) || (udf_rw16(dscr->tag.id) == 0)) break;
num_sectors = (lvid_len + lb_size-1) / lb_size;
length -= num_sectors * lb_size;
sector += num_sectors;
if (udf_rw32(dscr->lvid.next_extent.len)) {
sector = udf_rw32(dscr->lvid.next_extent.loc);
length = udf_rw32(dscr->lvid.next_extent.len);
}
/* free consumed descriptor */
free(dscr);
dscr = NULL;
}
/* free dangling descriptor */
if (dscr) free(dscr);
/* either an error has occured or we have processed all descriptors */
if (error) {
fprintf(stderr, "WARNING: integrity sequence ended with a bad descriptor; creating new\n");
udf_derive_new_logvol_integrity(udf_log_vol);
return ENOENT;
}
/*
* If its marked closed we hope/assume all is fine otherwise it may be
* marked closed later on when we are using a VAT and its found and
* correct
*/
return 0;
}
/* Add partition mapping to specified logvol descriptor */
void udf_add_physical_to_logvol(struct logvol_desc *logvol, uint16_t vol_seq_num, uint16_t phys_part_num) {
union udf_pmap *pmap;
uint8_t *pmap_pos;
pmap_pos = logvol->maps + udf_rw32(logvol->mt_l);
pmap = (union udf_pmap *) pmap_pos;
pmap->pm1.type = 1;
pmap->pm1.len = sizeof(struct part_map_1);
pmap->pm1.vol_seq_num = udf_rw16(vol_seq_num);
pmap->pm1.part_num = udf_rw16(phys_part_num);
/* increment partition mapping count */
logvol->n_pm = udf_rw32(udf_rw32(logvol->n_pm) + 1);
logvol->mt_l = udf_rw32(udf_rw32(logvol->mt_l) + sizeof(struct part_map_1));
logvol->tag.desc_crc_len = udf_rw16(udf_rw16(logvol->tag.desc_crc_len) + sizeof(struct part_map_1));
}
#if 0
/* not yet */
void udf_add_sparable_to_logvol(struct logvol_desc *logvol, uint16_t vol_seq_num, uint16_t phys_part_num, uint16_t packet_len, ) {
union udf_pmap *pmap;
uint8_t *pmap_pos;
pmap_pos = logvol->maps + udf_rw32(logvol->mt_l);
pmap = (union udf_pmap *) pmap_pos;
pmap->pm1.type = 1;
pmap->pm1.len = sizeof(struct part_map_1);
pmap->pm1.vol_seq_num = vol_seq_num;
pmap->pm1.part_num = phys_part_num;
/* increment partition mapping count */
logvol->n_pm = udf_rw32(udf_rw32(logvol->n_pm) + 1);
logvol->mt_l = udf_rw32(udf_rw32(logvol->mt_l) + sizeof(struct part_map_1));
logvol->tag.desc_crc_len = udf_rw16(udf_rw16(logvol->tag.desc_crc_len) + sizeof(struct part_map_1));
}
#endif
/* not called often; free incomming when not needed */
int udf_proc_log_vol(struct udf_pri_vol *primary, struct udf_log_vol **current, struct logvol_desc *incomming) {
struct udf_log_vol *logical;
struct udf_part_mapping *part_mapping, *data_part_mapping, *search_part_mapping;
union udf_pmap *pmap;
uint32_t part_cnt, pmap_type, pmap_size;
uint32_t data_part_num;
uint8_t *pmap_pos;
logical = udf_search_logical_volume_in_primary(primary, incomming->logvol_id);
if (!logical) {
/* create a logical volume */
logical = calloc(1, sizeof(struct udf_log_vol));
if (!logical) {
free(incomming);
return ENOMEM;
}
/* link in */
SLIST_INSERT_HEAD(&primary->log_vols, logical, next_logvol);
} else {
/* ok ... we now need to check if this new descriptor is a newer version */
if (udf_rw32(incomming->seq_num) < udf_rw32(logical->log_vol->seq_num)) {
/* its an older one; ignore */
free(incomming);
return 0;
}
}
/* update the logical volume descriptor and its mappings; first delete old partition mappings allocated before */
logical->primary = primary;
if (current) *current = logical;
part_mapping = SLIST_FIRST(&logical->part_mappings);
while ((part_mapping = SLIST_FIRST(&logical->part_mappings))) {
/* TODO cleanup old cruft ? (XXX while mounted? i don't think so!) */
/*
free(part_mapping->sparing_table);
free(part_mapping->vat_file_entry);
free(part_mapping->vat);
free(part_mapping->meta_file);
free(part_mapping->meta_mirror_file);
free(part_mapping->meta_bitmap_file);
*/
SLIST_REMOVE_HEAD(&logical->part_mappings, next_mapping);
free(part_mapping);
}
SLIST_INIT(&logical->part_mappings);
/* use the new logical volume and preprocess it */
if (logical->log_vol) free(logical->log_vol);
logical->log_vol = incomming;
logical->lb_size = udf_rw32(incomming->lb_size);
logical->sector_size = primary->udf_session->disc->sector_size;
/* build up the partion mappings */
logical->num_part_mappings = udf_rw32(incomming->n_pm);
/* process partition mappings */
pmap_pos = &logical->log_vol->maps[0];
for (part_cnt = 0; part_cnt < logical->num_part_mappings; part_cnt++) {
/* get a new part_mapping structure */
part_mapping = calloc(1, sizeof(struct udf_part_mapping));
assert(part_mapping); /* XXX check with partition mapping destructor etc XXX */
/*
* BUG alert: add to *tail* of list for dependencies sake.
* Since this is the only place that its needed, I decided
* against changing the SLIST to a TAILQ.
*/
if (SLIST_EMPTY(&logical->part_mappings))
SLIST_INSERT_HEAD(&logical->part_mappings, part_mapping, next_mapping);
else {
search_part_mapping = SLIST_FIRST(&logical->part_mappings);
while (SLIST_NEXT(search_part_mapping, next_mapping))
search_part_mapping = SLIST_NEXT(search_part_mapping, next_mapping);
SLIST_INSERT_AFTER(search_part_mapping, part_mapping, next_mapping);
}
/* process */
pmap = (union udf_pmap *) pmap_pos;
pmap_type = pmap->data[0];
pmap_size = pmap->data[1];
part_mapping->udf_virt_part_num = part_cnt;
part_mapping->udf_pmap = pmap;
switch (pmap_type) {
case 1:
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_PHYSICAL;
part_mapping->vol_seq_num = udf_rw16(pmap->pm1.vol_seq_num);
part_mapping->udf_phys_part_num = udf_rw16(pmap->pm1.part_num);
break;
case 2:
part_mapping->vol_seq_num = udf_rw16(pmap->pm2.vol_seq_num);
part_mapping->udf_phys_part_num = udf_rw16(pmap->pm2.part_num);
if (strncmp((char *) pmap->pm2.part_id.id, "*UDF Virtual Partition", UDF_REGID_ID_SIZE) == 0) {
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_VIRTUAL;
break;
}
if (strncmp((char *) pmap->pm2.part_id.id, "*UDF Sparable Partition", UDF_REGID_ID_SIZE) == 0) {
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_SPARABLE;
break;
}
if (strncmp((char *) pmap->pm2.part_id.id, "*UDF Metadata Partition", UDF_REGID_ID_SIZE) == 0) {
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_META;
break;
}
printf("HELP ... found unsupported type 2 partition mapping id `%s`; marking broken\n", pmap->pm2.part_id.id);
default:
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_ERROR;
}
pmap_pos += pmap_size; /* variable length array :( */
}
/* flag all partion mappings data and metadata writable */
SLIST_FOREACH(part_mapping, &logical->part_mappings, next_mapping) {
part_mapping->data_writable = 1;
part_mapping->metadata_writable = 1;
}
/* update writable flags depending on mapping type */
SLIST_FOREACH(part_mapping, &logical->part_mappings, next_mapping) {
switch (part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_ERROR :
part_mapping->data_writable = 0;
part_mapping->metadata_writable = 0;
break;
case UDF_PART_MAPPING_PHYSICAL :
break;
case UDF_PART_MAPPING_VIRTUAL :
case UDF_PART_MAPPING_META :
/*
* These are special in that there is a special metadata partition where no data
* is meant to be written on and vice versa
*/
/* find the associated data partition */
data_part_num = part_mapping->udf_phys_part_num;
SLIST_FOREACH(data_part_mapping, &logical->part_mappings, next_mapping) {
if (data_part_mapping->udf_phys_part_num == data_part_num) {
if (data_part_mapping != part_mapping) {
data_part_mapping->metadata_writable = 0;
break;
}
}
}
part_mapping->data_writable = 0;
break;
case UDF_PART_MAPPING_SPARABLE :
break;
}
}
TAILQ_INIT(&logical->dirty_nodes);
UDF_MUTEX_INIT(&logical->dirty_nodes_mutex);
return 0;
}
/* not called often; free incomming when not needed */
int udf_proc_part(struct udf_pri_vol *primary, struct udf_partition **current, struct part_desc *incomming) {
struct udf_partition *udf_partition;
struct udf_volumeset *udf_volset;
uint32_t new_part_num, sector_size;
assert(primary);
assert(primary->pri_vol);
udf_volset = udf_search_volumeset(primary->pri_vol->volset_id);
assert(udf_volset);
new_part_num = udf_rw16(incomming->part_num);
/* check if its a partition type we recognize */
if (strncmp((char *) incomming->contents.id, "+NSR0", 5) != 0) {
fprintf(stderr, "Unrecognized partition content type %s encountered; ignoring\n", incomming->contents.id);
free(incomming);
return 0;
}
/* look if we allready got it */
SLIST_FOREACH(udf_partition, &udf_volset->parts, next_partition) {
if (udf_rw16(udf_partition->partition->part_num) == new_part_num) break;
}
/* we have space... now check if this is a newer one than the one known */
if (udf_partition) {
if (udf_rw32(incomming->seq_num) < udf_rw32(udf_partition->partition->seq_num)) {
/* its an older version */
free(incomming);
return 0;
}
} else {
/* get us a new udf_partition */
udf_partition = calloc(1, sizeof(struct udf_partition));
if (!udf_partition) {
free(incomming);
return ENOMEM;
}
/* link it in */
SLIST_INSERT_HEAD(&udf_volset->parts, udf_partition, next_partition);
}
assert(udf_partition);
/* copy this new partition descriptor in the list */
if (udf_partition->partition) free(udf_partition->partition);
udf_partition->partition = incomming;
udf_partition->udf_session = primary->udf_session;
udf_volset->max_partnum = MAX(udf_volset->max_partnum, new_part_num+1); /* REVIEW why +1? */
/* initialise */
sector_size = primary->udf_session->disc->sector_size;
UDF_MUTEX_INIT(&udf_partition->partition_space_mutex);
TAILQ_INIT(&udf_partition->unalloc_space_queue);
TAILQ_INIT(&udf_partition->freed_space_queue);
udf_partition->part_offset = udf_rw32(incomming->start_loc) * sector_size;
udf_partition->part_length = udf_rw32(incomming->part_len) * sector_size;
/* udf_partition->access_type = udf_rw32(incomming->access_type); */
udf_partition->free_unalloc_space = udf_partition->free_freed_space = 0;
if (current) *current = udf_partition;
return 0;
}
/* not called often; free incomming when not needed */
int udf_proc_filesetdesc(struct udf_log_vol *udf_log_vol, struct fileset_desc *incomming) {
struct udf_mountpoint *mp;
if (udf_rw16(incomming->tag.id) != TAGID_FSD) {
printf("IEEE! Encountered a non TAGID_FSD in this fileset descriptor sequence!!!\n");
free(incomming);
return EFAULT;
}
/* lookup fileset descriptor in this logical volume; interestingly fileset_num is KEY! */
SLIST_FOREACH(mp, &udf_log_vol->mountpoints, logvol_next) {
if (mp->fileset_desc->fileset_num == incomming->fileset_num) break;
}
if (!mp) {
/* add a new mountpoint! */
mp = calloc(1, sizeof(struct udf_mountpoint));
if (!mp) {
free(incomming);
return ENOMEM;
}
mp->fileset_desc = incomming;
/* insert into udf_log_vol and into mountables list */
SLIST_INSERT_HEAD(&udf_log_vol->mountpoints, mp, logvol_next);
SLIST_INSERT_HEAD(&udf_mountables, mp, all_next);
} else {
/* should we update mountpoint? */
if (udf_rw32(incomming->fileset_desc_num) <= udf_rw32(mp->fileset_desc->fileset_desc_num)) {
/* we allready got a newer one */
free(incomming);
return 0;
}
fprintf(stderr, "UDF DEBUG: would be updating mountpoint... HELP!\n");
/* FIXME delete all inode hash entries */
/* XXX how to do that? inodes OK but associated vnodes? XXX */
#if 0
if (!SLIST_EMPTY(&mp->inodes)) {
printf("UDF: asked to delete mountpoint with inodes in hashtable!\n");
printf("Can't cope with that... aborting\n");
exit(1);
}
#endif
/* free old information (allready in lists though!) */
free(mp->fileset_desc);
free(mp->mount_name);
}
mp->udf_log_vol = udf_log_vol;
mp->fileset_desc = incomming;
mp->mount_name = strdup(udf_get_compound_name(mp));
return 0;
}
int udf_retrieve_volume_space(struct udf_discinfo *disc, struct udf_session *udf_session, struct extent_ad *extent) {
struct udf_pri_vol *udf_pri_vol;
struct udf_log_vol *udf_log_vol;
union dscrptr *dscr;
uint32_t sector, length, dscr_len, num_sectors;
uint32_t sector_size;
int tag_id;
int error;
udf_pri_vol = NULL;
sector = udf_rw32(extent->loc);
length = udf_rw32(extent->len);
sector_size = disc->sector_size;
error = 0; /* XXX zero length area's possible? XXX */
while (length) {
error = udf_read_session_descriptor(udf_session, sector, "volume descriptor", &dscr, &dscr_len);
if (error) {
if (dscr) free(dscr);
break;
}
tag_id = udf_rw16(dscr->tag.id);
num_sectors = (dscr_len + sector_size-1) / sector_size;
/* proc volume descriptor starting at sector `volume_sector' */
UDF_VERBOSE_MAX(udf_dump_descriptor(dscr));
switch (tag_id) {
case TAGID_PRI_VOL :
error = udf_proc_pri_vol(udf_session, &udf_pri_vol, &dscr->pvd);
break;
case TAGID_PARTITION :
error = udf_proc_part(udf_pri_vol, NULL, &dscr->pd);
break;
case TAGID_LOGVOL :
error = udf_proc_log_vol(udf_pri_vol, &udf_log_vol, &dscr->lvd);
if (!error) {
/* first create empty integrity descriptor then modify it on input (for sanity) */
udf_derive_new_logvol_integrity(udf_log_vol);
}
break;
case TAGID_TERM :
free(dscr);
return 0; /* terminator */
case TAGID_UNALLOC_SPACE :
/* unallocated space descriptor */
/* Specifies space that is not claimed yet in partitions (!) */
UDF_VERBOSE(printf("\t\t`unallocated space descriptor' ignored\n"));
break;
case TAGID_IMP_VOL :
/* implemenation use volume descriptor */
/* Specifies information relevant for the implementator */
UDF_VERBOSE_MAX(printf("\t\t`implementation use volume descriptor' ignored\n"));
break;
case TAGID_VOL :
fprintf(stderr, "UDF : untested volume space extender encountered\n");
break;
default :
printf("XXX Unhandled volume sequence %d; freeing\n", tag_id);
free(dscr);
break;
}
length -= num_sectors * sector_size;
sector += num_sectors;
if (tag_id == TAGID_VOL) {
sector = udf_rw32(dscr->vdp.next_vds_ex.loc);
length = udf_rw32(dscr->vdp.next_vds_ex.len);
free(dscr);
}
}
return error;
}
int udf_get_filelength(union dscrptr *dscr, uint64_t *length) {
int32_t fe_tag;
fe_tag = udf_rw16(dscr->tag.id);
if (fe_tag == TAGID_FENTRY) {
*length = udf_rw64(dscr->fe.inf_len);
return 0;
} else if (fe_tag == TAGID_EXTFENTRY) {
*length = udf_rw64(dscr->efe.inf_len);
return 0;
}
return ENOENT;
}
/* can be passed either a file_entry or an extfil_entry trough fentry! */
int udf_check_for_vat(struct udf_log_vol *udf_log_vol, struct udf_part_mapping *part_mapping, uint32_t vat_lb, union dscrptr *dscr) {
struct udf_part_mapping *s_part_mapping;
struct udf_node *vat_udf_node;
struct long_ad udf_icbptr;
struct regid *regid;
struct uio vat_uio;
struct iovec vat_iovec;
struct icb_tag *icbtag;
struct timestamp *mtime;
uint64_t vat_length, vat_entries;
uint32_t *vat_pos, vpart_num;
uint8_t *vat;
int error, found;
/* prepare a `uio' structure for reading in complete VAT file */
error = udf_get_filelength(dscr, &vat_length);
if (error) return error;
if (vat_length == 0)
return EFAULT;
vat = malloc(vat_length);
if (!vat)
return ENOMEM;
/* move to uio_newuio(struct uio *uio) with fixed length uio_iovcnt? */
bzero(&vat_uio, sizeof(struct uio));
vat_uio.uio_rw = UIO_WRITE; /* WRITE into this space */
vat_uio.uio_iovcnt = 1;
vat_uio.uio_iov = &vat_iovec;
vat_uio.uio_offset = 0; /* begin at the start */
vat_uio.uio_resid = vat_length;
/* fill in IO vector */
vat_uio.uio_iov->iov_base = vat;
vat_uio.uio_iov->iov_len = vat_length;
/* find our virtual partition number corresponding to our physical partition number; this sucks */
found = 0;
vpart_num = 0;
SLIST_FOREACH(s_part_mapping, &udf_log_vol->part_mappings, next_mapping) {
if (s_part_mapping->udf_phys_part_num == part_mapping->udf_phys_part_num) {
if (s_part_mapping->udf_part_mapping_type == UDF_PART_MAPPING_PHYSICAL) {
/* found it ! */
found = 1;
vpart_num = s_part_mapping->udf_virt_part_num;
}
}
}
if (!found) {
printf("Can't find accompanied physical volume\n");
return ENOENT;
}
/* prepare udf_icbptr file node for easy file reading */
udf_icbptr.loc.part_num = vpart_num;
udf_icbptr.loc.lb_num = udf_rw32(vat_lb);
udf_icbptr.len = udf_log_vol->lb_size; /* not used, but may not be zero */
/*
* this udf_node creation and disposing may look a bit inefficient but
* its beneficiary for normal file access. its only used once for
* reading in the VAT.
*/
/* create the udf_vat_node; anonymous since it can't be in a mountpoint */
error = udf_readin_anon_udf_node(udf_log_vol, dscr, &udf_icbptr, "VAT", &vat_udf_node);
if (!error) {
DEBUG(printf("READ FILE PART UIO for VAT\n"));
error = udf_read_file_part_uio(vat_udf_node, "VAT contents", 0, &vat_uio);
DEBUG(printf("vat_uio rest %d\n", (uint32_t) vat_uio.uio_resid));
}
/* XXX allow for SHORT VAT's ? XXX */
if (!error) {
if (vat_uio.uio_resid) {
fprintf(stderr, "Warning: VAT file can't be read in completely\n");
}
part_mapping->vat_udf_node = vat_udf_node;
part_mapping->vat = (struct udf_vat *) vat;
part_mapping->vat_length = vat_length;
/* extract next unique file ID from the VAT file entry's unique ID incremented by one */
udf_log_vol->next_unique_id = vat_udf_node->unique_id; /* ok? */
udf_increment_unique_id(udf_log_vol);
/* fentry is confirmed to be either an file_entry or an extfile_entry here */
if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
icbtag = &dscr->fe.icbtag;
mtime = &dscr->fe.mtime;
} else {
icbtag = &dscr->efe.icbtag;
mtime = &dscr->efe.mtime;
}
if (icbtag->file_type == UDF_ICB_FILETYPE_VAT) {
/* we are in UDF 2.00+ userland */
part_mapping->vat_translation = ((uint8_t *) part_mapping->vat) + udf_rw16(part_mapping->vat->header_len);
part_mapping->vat_entries = (vat_length - udf_rw16(part_mapping->vat->header_len))/4;
udf_log_vol->num_files = udf_rw32(part_mapping->vat->num_files);
udf_log_vol->num_directories = udf_rw32(part_mapping->vat->num_directories);
udf_log_vol->min_udf_readver = udf_rw16(part_mapping->vat->min_udf_readver);
udf_log_vol->min_udf_writever = udf_rw16(part_mapping->vat->min_udf_writever);
udf_log_vol->max_udf_writever = udf_rw16(part_mapping->vat->max_udf_writever);
/* TODO update logvol name */
} else {
/* still in the old UDF 1.50 userland; update? its notoriously broken */
/* check the old UDF 1.50 VAT */
DEBUG(printf("CHECK UDF 1.50 VAT\n"));
vat_pos = (uint32_t *) vat;
vat_entries = (vat_length-36)/4; /* definition */
regid = (struct regid *) (vat_pos + vat_entries);
error = (strncmp((char *) regid->id, "*UDF Virtual Alloc Tbl", 22) == 0) ? 0 : ENOENT;
if (!error) {
part_mapping->vat_entries = vat_entries;
part_mapping->vat_translation = vat;
part_mapping->vat = NULL;
/* num files/dirs? */
}
}
if (!error) {
UDF_VERBOSE(udf_dump_timestamp("\t\t\t\tmarked closed at ", mtime));
}
}
/* clean up uio structure */
if (error) {
if (vat) free(vat);
if (vat_udf_node) udf_dispose_udf_node(vat_udf_node);
part_mapping->vat_udf_node = NULL;
}
return error;
}
int udf_retrieve_supporting_tables(struct udf_log_vol *udf_log_vol) {
struct udf_partition *udf_partition;
struct udf_part_mapping *part_mapping, *s_part_mapping;
struct udf_session *udf_session;
struct long_ad udf_icbptr;
union dscrptr *possible_vat_fe;
union dscrptr *sparing_table_dscr;
uint32_t spar_loc;
uint64_t first_vat_loc, vat_loc, last_vat_loc;
uint32_t sector_size, lb_size;
uint32_t part_num, spar_num, data_part_num, vpart_num;
int session_num;
int error;
/*
* if there are any virtual or sparable partition in this logical
* volume, try to find their supporting tables so we can find the rest
*/
lb_size = udf_log_vol->lb_size;
sector_size = udf_log_vol->sector_size;
SLIST_FOREACH(part_mapping, &udf_log_vol->part_mappings, next_mapping) {
part_num = part_mapping->udf_virt_part_num;
udf_logvol_vpart_to_partition(udf_log_vol, part_num, NULL, &udf_partition);
UDF_VERBOSE_TABLES(printf("\tFor partition mapping %d->%d\n", part_num, part_mapping->udf_phys_part_num));
switch (part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_ERROR :
/* nothing to be done for these */
break;
case UDF_PART_MAPPING_PHYSICAL :
/* nothing to be done for these; no supporting tables */
break;
case UDF_PART_MAPPING_VIRTUAL :
/*
* we have to find a good VAT at the END of the session. Since VAT's are
* only to be used on WORM's and need to written as last, the strategy is
* to go for the predicted end of this session and walk UP
*/
udf_session = udf_log_vol->primary->udf_session;
session_num = udf_session->session_num;
UDF_VERBOSE_TABLES(printf("\t\tSearching for the VAT :\n"));
if (udf_session->session_length == 0) {
UDF_VERBOSE(
printf("\t\tThis virtual partition is inaccessible since its its size is not known;\n");
printf("\t\tTry to insert the disc in a CD or DVD recordable device to access it.\n");
);
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_ERROR;
continue;
}
if (udf_session->disc->next_writable[session_num]) {
last_vat_loc = udf_session->disc->next_writable[session_num];
} else {
last_vat_loc = udf_session->disc->session_end[session_num];
}
last_vat_loc += udf_session->disc->blockingnr;
/* give some extra slack since sizes are not allways given up correctly */
first_vat_loc = last_vat_loc - 256; /* 8 blocks of 32 */
first_vat_loc = MAX(first_vat_loc, (uint64_t) udf_session->disc->session_start[session_num]);
/* try to find the fileid for the VAT; NOTE that we are reading backwards :( */
vat_loc = last_vat_loc;
do {
DEBUG(
printf("Trying VAT at sector %d in session\n", (int) vat_loc)
);
error = udf_read_session_descriptor(udf_session, vat_loc, "VAT file entry", &possible_vat_fe, NULL);
if (!error) {
error = udf_check_tag_presence(possible_vat_fe, TAGID_FENTRY);
if (error)
error = udf_check_tag_presence(possible_vat_fe, TAGID_EXTFENTRY);
}
if (!error) error = udf_check_tag_payload( possible_vat_fe);
if (!error) error = udf_check_for_vat(udf_log_vol, part_mapping, vat_loc, possible_vat_fe);
if (!error) {
break;
} else {
if (possible_vat_fe) free(possible_vat_fe);
vat_loc--;
if (vat_loc < first_vat_loc) error = EIO;
}
} while (error != EIO);
if (error) {
printf("WARNING: was looking for a VAT but didnt find it; marking logical volume broken\n");
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_ERROR;
udf_log_vol->logvol_state = UDF_INTEGRITY_OPEN;
udf_log_vol->broken = 1;
continue;
}
UDF_VERBOSE_TABLES(printf("\t\t\tfound VAT file-entry at device logical sector %d\n", (uint32_t) vat_loc));
UDF_VERBOSE_TABLES(printf("\t\t\tFound %d byte VAT descriptor+table\n", (uint32_t) part_mapping->vat_length));
UDF_VERBOSE_TABLES(udf_dump_descriptor(possible_vat_fe));
UDF_VERBOSE_MAX(udf_dump_vat_table(part_mapping));
if (part_mapping->vat_translation) {
/* the presence of a correct VAT means the logical volume is in a closed state */
udf_log_vol->logvol_state = UDF_INTEGRITY_CLOSED;
UDF_VERBOSE(printf("\t\t\t\tmarked closed due to presence of VAT\n"));
/* XXX update `free' space by requesting the device's free space? XXX */
udf_log_vol->free_space = udf_partition->part_offset + udf_partition->part_length - vat_loc*sector_size;
}
if (!udf_session->disc->sequential) {
UDF_VERBOSE(printf("\t\t\t\tenabling sequential media emulation\n"));
udf_session->disc->sequential = 1;
}
break;
case UDF_PART_MAPPING_SPARABLE :
/* we have to find a good sparing table; address are in device logical blocks */
udf_session = udf_log_vol->primary->udf_session;
for(spar_num = 0; spar_num < part_mapping->udf_pmap->pms.n_st; spar_num++) {
spar_loc = udf_rw32(part_mapping->udf_pmap->pms.st_loc[spar_num]);
/* fetch spar_loc's table ; on THIS session. */
error = udf_read_session_descriptor(udf_session, spar_loc, "Sparing table", &sparing_table_dscr, NULL);
if (!error) error = udf_check_tag_presence(sparing_table_dscr, TAGID_SPARING_TABLE);
if (!error) {
UDF_VERBOSE_TABLES(printf("\t\tFound the sparing table\n"));
part_mapping->sparing_table = &sparing_table_dscr->spt;
break;
} else {
if (sparing_table_dscr) free(sparing_table_dscr);
}
}
if (!part_mapping->sparing_table) {
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_ERROR;
}
UDF_VERBOSE_TABLES(udf_dump_descriptor((union dscrptr *) part_mapping->sparing_table));
break;
case UDF_PART_MAPPING_META :
/*
* set up common locator parts; the files are located inside the `part_num' partion where
* this partition is a added layer on.
*/
/* find the associated data partition */
data_part_num = udf_rw16(part_mapping->udf_pmap->pmm.part_num);
/* find our virtual partition number corresponding to our physical partition number; this sucks */
vpart_num = data_part_num;
SLIST_FOREACH(s_part_mapping, &udf_log_vol->part_mappings, next_mapping) {
if (s_part_mapping->udf_phys_part_num == data_part_num) {
if (s_part_mapping->udf_part_mapping_type != UDF_PART_MAPPING_META) {
/* found it ! */
vpart_num = s_part_mapping->udf_virt_part_num;
break;
}
}
}
udf_icbptr.loc.part_num = vpart_num;
udf_icbptr.len = lb_size; /* defined as maximum size */
UDF_VERBOSE_TABLES(printf("Reading metadata partition filedescriptor\n"));
udf_icbptr.loc.lb_num = udf_rw32(part_mapping->udf_pmap->pmm.meta_file_lbn);
error = udf_readin_anon_udf_node(udf_log_vol, NULL, &udf_icbptr, "Metadata partition file descriptor", &part_mapping->meta_file);
if (error == 0)
UDF_VERBOSE_TABLES(udf_dump_descriptor((union dscrptr *) part_mapping->meta_file));
udf_icbptr.loc.lb_num = udf_rw32(part_mapping->udf_pmap->pmm.meta_mirror_file_lbn);
if ((error == 0) && (udf_icbptr.loc.lb_num != (uint32_t) -1)) {
UDF_VERBOSE_TABLES(printf("Reading metadata partition mirror filedescriptor\n"));
error = udf_readin_anon_udf_node(udf_log_vol, NULL, &udf_icbptr, "Metadata partition mirror file descriptor", &part_mapping->meta_mirror_file);
if (error == 0)
UDF_VERBOSE_TABLES(udf_dump_descriptor((union dscrptr *) part_mapping->meta_mirror_file));
error = 0; /* XXX ignoring error code for now */
}
udf_icbptr.loc.lb_num = udf_rw32(part_mapping->udf_pmap->pmm.meta_bitmap_file_lbn);
if ((error == 0) && (udf_icbptr.loc.lb_num != (uint32_t) -1)) {
UDF_VERBOSE_TABLES(printf("Reading metadata partition bitmap filedescriptor\n"));
error = udf_readin_anon_udf_node(udf_log_vol, NULL, &udf_icbptr, "Metadata partition bitmap file descriptor", &part_mapping->meta_bitmap_file);
if (error == 0)
UDF_VERBOSE_TABLES(udf_dump_descriptor((union dscrptr *) part_mapping->meta_bitmap_file));
}
/* if something is wrong, then mark it as a broken partition */
if (error) {
/* TODO handle read-errors on the meta data and meta data mirror file descriptors. */
part_mapping->udf_part_mapping_type = UDF_PART_MAPPING_ERROR;
}
break;
}
}
UDF_VERBOSE_TABLES(printf("\n"));
if (udf_log_vol->broken) return EIO;
return 0;
}
int udf_retrieve_space_tables(struct udf_log_vol *udf_log_vol) {
struct udf_partition *udf_partition;
struct udf_part_mapping *part_mapping;
struct part_hdr_desc *part_hdr_desc;
union dscrptr *dscrptr;
uint32_t sector;
uint32_t lb_size;
uint64_t length;
int vpart_num, ppart_num;
int error;
lb_size = udf_log_vol->lb_size;
SLIST_FOREACH(part_mapping, &udf_log_vol->part_mappings, next_mapping) {
vpart_num = part_mapping->udf_virt_part_num;
ppart_num = part_mapping->udf_phys_part_num;
UDF_VERBOSE_TABLES(printf("\tFor partition mapping %d->%d\n", vpart_num, ppart_num));
if ((part_mapping->udf_part_mapping_type != UDF_PART_MAPPING_PHYSICAL) &&
(part_mapping->udf_part_mapping_type != UDF_PART_MAPPING_SPARABLE)) {
UDF_VERBOSE_TABLES(printf("\t\tDon't know how to load space tables for type %d\n", part_mapping->udf_part_mapping_type));
continue;
}
/* retrieve and process unallocated- and freed-space information for all used partitions of the logvol */
error = udf_logvol_vpart_to_partition(udf_log_vol, vpart_num, NULL, &udf_partition);
assert(udf_partition);
part_hdr_desc = &udf_partition->partition->pd_part_hdr;
sector = udf_rw32(part_hdr_desc->unalloc_space_table.lb_num);
length = udf_rw32(part_hdr_desc->unalloc_space_table.len); /* needed? */
if (length) {
error = udf_read_logvol_descriptor(udf_log_vol, vpart_num, sector, "Unallocated space table", &dscrptr, NULL);
UDF_VERBOSE_MAX(printf("\tUnalloced space table\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
/* udf_process_space_table(&udf_partition->unalloc_space, dscrptr); */
free(dscrptr);
}
sector = udf_rw32(part_hdr_desc->unalloc_space_bitmap.lb_num);
length = udf_rw32(part_hdr_desc->unalloc_space_bitmap.len);
if (length && (udf_partition->unalloc_space_bitmap == 0)) {
error = udf_read_logvol_descriptor(udf_log_vol, vpart_num, sector, "Unallocated space bitmap", &dscrptr, NULL);
if (!error) {
UDF_VERBOSE_MAX(printf("\tUnalloced space bitmap\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
udf_read_in_space_bitmap(&udf_partition->unalloc_space_queue, &dscrptr->sbd, lb_size, &udf_partition->free_unalloc_space);
UDF_VERBOSE_TABLES(printf("\t\tPhysical partition's unallocated space : %"PRIu64"\n", udf_partition->free_unalloc_space));
udf_partition->unalloc_space_bitmap = &dscrptr->sbd;
} else {
printf("While reading in unallocated space bitmap : %s\n", strerror(error));
udf_partition->unalloc_space_bitmap = NULL;
/* TODO mark read-only logvol */
}
}
sector = udf_rw32(part_hdr_desc->freed_space_table.lb_num);
length = udf_rw32(part_hdr_desc->freed_space_table.len);
if (length) {
error = udf_read_logvol_descriptor(udf_log_vol, vpart_num, sector, "Freed space table", &dscrptr, NULL);
UDF_VERBOSE_MAX(printf("\tFreed space table\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
/* udf_process_space_table(&udf_partition->freed_space, dscrptr); */
free(dscrptr);
}
sector = udf_rw32(part_hdr_desc->freed_space_bitmap.lb_num);
length = udf_rw32(part_hdr_desc->freed_space_bitmap.len);
if (length && (udf_partition->freed_space_bitmap == NULL)) {
error = udf_read_logvol_descriptor(udf_log_vol, vpart_num, sector, "Freed space bitmap", &dscrptr, NULL);
if (!error) {
UDF_VERBOSE_MAX(printf("\tFreed space bitmap\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
udf_read_in_space_bitmap(&udf_partition->freed_space_queue, &dscrptr->sbd, lb_size, &udf_partition->free_freed_space);
UDF_VERBOSE_TABLES(printf("\t\tPhysical partition's freed space : %"PRIu64"\n", udf_partition->free_unalloc_space));
udf_partition->freed_space_bitmap = &dscrptr->sbd;
} else {
printf("While reading in freed space bitmap : %s\n", strerror(error));
udf_partition->freed_space_bitmap = NULL;
/* TODO mark read-only logvol */
}
}
}
UDF_VERBOSE_TABLES(printf("\n"));
return 0;
}
/*
* Fileset descriptors are on a logical volume's partitions; since virtual
* partitions are then also possible its OK to use the VAT for redefining the
* fileset descriptors.
*/
int udf_retrieve_fileset_descriptor(struct udf_log_vol *udf_log_vol) {
struct udf_mountpoint *mountable;
struct long_ad *fsd_loc;
struct fileset_desc *new_fsd;
struct udf_node *vnode;
uint32_t part_num, lb_num;
int32_t length;
int32_t error;
error = 0; /* flag OK */
fsd_loc = &udf_log_vol->log_vol->_lvd_use.fsd_loc;
part_num = udf_rw16(fsd_loc->loc.part_num);
lb_num = udf_rw32(fsd_loc->loc.lb_num);
length = udf_rw32(fsd_loc->len);
while ((length > 0) && !error) {
UDF_VERBOSE_TABLES(
printf("\tFileset descriptor extent at sector %d within partion %d for %d bytes\n", lb_num, part_num, length)
);
/* only go for ONE fsb at a time */
error = udf_read_logvol_descriptor(udf_log_vol, part_num, lb_num, "Fileset descriptor", (union dscrptr **) &new_fsd, NULL);
if (!error) error = udf_check_tag((union dscrptr *) new_fsd);
/* TODO need a clearer handling unrecorded blocks here */
if (error || (!new_fsd) || (new_fsd && (udf_rw16(new_fsd->tag.id) == TAGID_TERM))) {
/* end of sequence */
UDF_VERBOSE_TABLES(
printf("\t\t(Terminator) ");
if (!new_fsd || error) printf("; unrecorded"); else printf("; explicit");
printf("\n");
);
/* clear error to indicate end of sequence and free possible read in descriptor */
error = 0;
if (new_fsd) free(new_fsd);
break;
}
UDF_VERBOSE_MAX(udf_dump_descriptor((union dscrptr *) new_fsd));
udf_proc_filesetdesc(udf_log_vol, new_fsd);
if (udf_rw32(new_fsd->next_ex.len) == 0) {
/* next entry */
lb_num += 1;
length -= udf_log_vol->lb_size;
} else {
/* follow the next extent */
fsd_loc = &new_fsd->next_ex;
part_num = udf_rw16(fsd_loc->loc.part_num);
lb_num = udf_rw32(fsd_loc->loc.lb_num);
length = udf_rw32(fsd_loc->len);
}
}
UDF_VERBOSE_TABLES(printf("\n"));
if (error) return error;
/* if no error occured, create rootdir udf_nodes */
SLIST_FOREACH(mountable, &udf_log_vol->mountpoints, logvol_next) {
/* errors are OK */
udf_readin_anon_udf_node(udf_log_vol, NULL, &mountable->fileset_desc->rootdir_icb, "Rootdir", &mountable->rootdir_node);
udf_readin_anon_udf_node(udf_log_vol, NULL, &mountable->fileset_desc->streamdir_icb, "Streamdir", &mountable->streamdir_node);
/* keep names the same ? (duplicate code ahead ... ) */
if (mountable->rootdir_node) {
vnode = mountable->rootdir_node;
vnode->mountpoint = mountable;
vnode->stat.st_uid = vnode->stat.st_gid = UINT_MAX;
vnode->stat.st_mode = 0777 | S_IFDIR;
udf_insert_node_in_hash(vnode);
}
if (mountable->streamdir_node) {
vnode = mountable->streamdir_node;
vnode->mountpoint = mountable;
vnode->stat.st_uid = vnode->stat.st_gid = UINT_MAX;
vnode->stat.st_mode = 0777 | S_IFDIR;
udf_insert_node_in_hash(vnode);
}
}
return 0;
}
int udf_check_writable_filesets(struct udf_log_vol *udf_log_vol, int mnt_flags) {
struct udf_mountpoint *mp;
struct udf_part_mapping *udf_part_mapping;
int writable;
writable = 1;
if (mnt_flags & UDF_MNT_RDONLY)
writable = 0;
if (mnt_flags & UDF_MNT_FORCE)
writable = 1;
if (udf_log_vol->logvol_state == UDF_INTEGRITY_OPEN) {
if (!(mnt_flags & UDF_MNT_FORCE)) {
/* we explicitly DISABLE writing */
/* XXX do we even reach here? XXX */
if (udf_verbose) {
printf("\t\t\t\tmounting READ-ONLY due to open integrity\n");
} else {
printf("WARNING: mounting logical volume READ-ONLY due to open integrity\n");
}
writable = 0;
} else {
printf("WARNING: ignoring open integrity\n");
}
}
SLIST_FOREACH(udf_part_mapping, &udf_log_vol->part_mappings, next_mapping) {
if (udf_part_mapping->udf_part_mapping_type == UDF_PART_MAPPING_META) {
writable = 0;
fprintf(stderr, "\t\t\t\t*** marked read-only due to read-only support for Metadata partition ***\n");
}
}
/* follow all mountpoints of this logical volume and set if they are writable */
SLIST_FOREACH(mp, &udf_log_vol->mountpoints, logvol_next) {
mp->writable = writable;
}
udf_log_vol->writable = writable;
/* WAS: */
/* udf_log_vol->primary->udf_session->writable = mark; */
return 0;
}
/*
* udf_eliminate_predescessor_volumesets()
*
* We are faced with a curious problem : we are to examine the partitions and
* determine which are successors of eachother. This is propably most
* relevant only on WORM media though. We could consider the following rules :
* 1) `glue' according to strict UDF level 1 rules ?
* 2) use heuristics info i.e. NERO, DirectCD and mkisofs quirks ?
* 3) use overlapping partitions to detect relationships ?
*
* Using 1 would imply no multi-volume discs and thus glue everything but that
* could easily be wrong. Selecting by volumeset names is not possible for
* Nero f.e. just creates random volumeset names every session and uses no
* volume version information and thus also violates the UDF rules.
*
* Using 2 would be tricky; we know a few programs but what if more are
* developped? We then would be at loss.
*
* Using 3 would imply some calculation but is fail-safe in both supporting
* multiple volumes on one disc (they seperate) and in supporting
* multi-session WORM media for these will refer to eachother. Offcource NERO
* could faul this by just extending the zero partion to the whole disc in its
* ignorance and thus create false overlapping over other independent
* sessions. This is to be investigated. I don't know how NERO will react on
* this situation.
*
* Propably method 3 would be good to try :
*
* Follow the disc and check for all sessions in order to mark the ones with
* overlapping partitions as `inactive' and keep the latest one active.
* Sessions with the `local' quirk are seperate allmost by default; should
* be change the offsets? would not be too difficult but possible.
*/
void udf_eliminate_predescessor_volumesets(struct udf_discinfo *disc) {
struct udf_volumeset *anc_vol_set;
struct udf_volumeset *sib_vol_set;
struct pri_vol_desc *anc_pri_vol;
struct pri_vol_desc *sib_pri_vol;
struct udf_partition *anc_part;
struct udf_partition *sib_part;
int anc_partnum;
int sib_partnum;
uint32_t anc_start, anc_end;
uint32_t sib_start, sib_end;
uint32_t overlap_start, overlap_end;
uint32_t anc_session;
uint32_t sib_session;
SLIST_FOREACH(anc_vol_set, &udf_volumeset_list, next_volumeset) {
anc_pri_vol = STAILQ_FIRST(&anc_vol_set->primaries)->pri_vol;
sib_vol_set = SLIST_NEXT(anc_vol_set, next_volumeset);
while (sib_vol_set) {
sib_pri_vol = STAILQ_FIRST(&sib_vol_set->primaries)->pri_vol;
DEBUG(
printf("checking volset %s with volset %s\n", anc_pri_vol->volset_id+1, sib_pri_vol->volset_id+1)
);
/* compare these two volume sets but only process partitions on _this_ disc */
SLIST_FOREACH(anc_part, &anc_vol_set->parts, next_partition) {
if (anc_part->udf_session->disc != disc) continue;
anc_session = anc_part->udf_session->session_num;
anc_start = 0;
#if 0
if (disc->session_quirks[anc_session] & CD_SESS_QUIRK_SESSION_LOCAL)
anc_start += disc->session_start[anc_session];
#endif
anc_start += udf_rw32(anc_part->partition->start_loc);
anc_end = anc_start + udf_rw32(anc_part->partition->part_len);
SLIST_FOREACH(sib_part, &sib_vol_set->parts, next_partition) {
if (sib_part->udf_session->disc != disc) continue;
sib_session = sib_part->udf_session->session_num;
#if 0
/* can `session local' volumes even be considered part/successor ? */
if (disc->session_quirks[sib_session] & CD_SESS_QUIRK_SESSION_LOCAL) continue;
#endif
sib_start = 0;
#if 0
if (disc->session_quirks[sib_session] & CD_SESS_QUIRK_SESSION_LOCAL)
sib_start += disc->session_start[sib_session];
#endif
sib_start += udf_rw32(sib_part->partition->start_loc);
sib_end = sib_start + udf_rw32(sib_part->partition->part_len);
DEBUG(
anc_partnum = udf_rw16(anc_part->partition->part_num);
sib_partnum = udf_rw16(sib_part->partition->part_num);
printf("\t\tchecking partition %d with partition %d ([%d-%d] x [%d-%d])\n", anc_partnum, sib_partnum, anc_start, anc_end, sib_start, sib_end)
);
overlap_start = MAX(sib_start, anc_start);
overlap_end = MIN(sib_end, sib_end);
if (overlap_start < overlap_end) {
DEBUG(
printf("\t\t\tOVERLAP!\n")
);
if (sib_session < anc_session) {
/* the sibbling is older */
UDF_VERBOSE_TABLES(
printf("\tVolume set ");
udf_dump_id(NULL, 128, anc_pri_vol->vol_id, &anc_pri_vol->desc_charset);
printf(" is a newer version of volume set ");
udf_dump_id(NULL, 128, sib_pri_vol->vol_id, &sib_pri_vol->desc_charset);
printf("\n");
);
sib_vol_set->obsolete = 1;
break;
}
} /* overlap */
if (sib_vol_set->obsolete) break;
} /* sibling partition */
if (sib_vol_set->obsolete) break;
} /* ancestor partition */
sib_vol_set = SLIST_NEXT(sib_vol_set, next_volumeset);
} /* sibling volume set */
} /* ancestor volume set */
}
int udf_add_session_to_discinfo(struct udf_discinfo *disc, int session, struct anchor_vdp *avdp, int error) {
struct udf_session *udf_session;
udf_session = calloc(1, sizeof(struct udf_session));
assert(udf_session);
if (!error) {
memcpy(&udf_session->anchor, avdp, sizeof(struct anchor_vdp));
}
udf_session->disc = disc;
udf_session->session_num = session;
udf_session->session_offset = 0;
udf_session->session_length = disc->session_end[session] - disc->session_start[session];
disc->session_quirks[session] = 0;
/* writable session administration */
udf_session->writable = 0; /* default off */
error = udf_init_session_caches(udf_session);
if (!error) {
/* detect quirks */
/* XXX session local disabled due to wrong heuristic XXX */
#if 0
if (disc->session_start[session] > 0) {
if ((udf_session->anchor.main_vds_ex.loc < disc->session_start[session])) {
disc->session_quirks[session] |= CD_SESS_QUIRK_SESSION_LOCAL;
udf_session->session_offset = disc->session_start[session];
}
}
#endif
}
/* add to tail of session list */
STAILQ_INSERT_TAIL(&disc->sessions, udf_session, next_session);
disc->num_udf_sessions++;
/* record status of this volume */
disc->session_is_UDF[session] = error ? 0 : 1;
return error;
}
int udf_get_anchors(struct udf_discinfo *disc) {
uint8_t *sector;
union dscrptr *dscr;
uint32_t session_start, session_end;
int session, error;
/* Get all anchors */
STAILQ_INIT(&disc->sessions);
sector = NULL;
for (session = 0; session < disc->num_sessions; session++) {
/* check for anchors ; no volume recognition data ? */
session_start = disc->session_start[session];
session_end = disc->session_end [session]-1;
sector = calloc(1, disc->sector_size);
if (!sector) return ENOMEM;
dscr = (union dscrptr *) sector;
error = udf_read_physical_sectors(disc, session_end, 1, "Anchor", sector);
if (!error) error = udf_check_tag_presence(dscr, TAGID_ANCHOR);
if (!error) UDF_VERBOSE_TABLES(printf("Accepting anchor at session end (%d)\n", session_end));
if (error) {
error = udf_read_physical_sectors(disc, session_end - 256, 1, "Anchor", sector);
if (!error) error = udf_check_tag_presence(dscr, TAGID_ANCHOR);
if (!error) UDF_VERBOSE_TABLES(printf("Accepting anchor at session end - 256 (%d)\n", session_end - 256));
if (error) {
error = udf_read_physical_sectors(disc, session_start + 256, 1, "Anchor", sector);
if (!error) error = udf_check_tag_presence(dscr, TAGID_ANCHOR);
if (!error) UDF_VERBOSE_TABLES(printf("Accepting anchor at session sector 256 (%d)\n", session_start + 256));
if (error) {
/* unclosed CD recordable case due to track reservation for iso9660 filesystems */
error = udf_read_physical_sectors(disc, session_start + 512, 1, "Anchor", sector);
if (!error) error = udf_check_tag_presence(dscr, TAGID_ANCHOR);
if (!error) UDF_VERBOSE_TABLES(printf("Accepting anchor at session sector 512 (%d)\n", session_start + 512));
}
}
}
if (!error) {
udf_add_session_to_discinfo(disc, session, (struct anchor_vdp *) sector, error);
} else {
free(sector);
}
}
return 0;
}
int udf_get_volumeset_space(struct udf_discinfo *disc) {
struct udf_session *udf_session;
int one_good_found;
int error;
/* Rip all volume spaces */
one_good_found = 0;
UDF_VERBOSE(printf("\tretrieving volume space\n"));
STAILQ_FOREACH(udf_session, &disc->sessions, next_session) {
UDF_VERBOSE_MAX(printf("Session %d volumes : \n", udf_session->session_num));
error = udf_retrieve_volume_space(disc, udf_session, &udf_session->anchor.main_vds_ex);
if (error) {
printf("\nError retrieving session %d's volume space; prosessing reserve\n", udf_session->session_num);
error = udf_retrieve_volume_space(disc, udf_session, &udf_session->anchor.reserve_vds_ex);
}
if (!error)
one_good_found = 1;
}
return one_good_found ? 0 : ENOENT;
}
int udf_get_logical_volumes_supporting_tables(struct udf_discinfo *disc, int mnt_flags) {
struct udf_volumeset *udf_volumeset;
struct udf_pri_vol *udf_pri_vol;
struct udf_log_vol *udf_log_vol;
int logvolint_error;
int one_good_found;
int error;
one_good_found = 0;
SLIST_FOREACH(udf_volumeset, &udf_volumeset_list, next_volumeset) {
if (!udf_volumeset->obsolete) {
STAILQ_FOREACH(udf_pri_vol, &udf_volumeset->primaries, next_primary) {
if (udf_pri_vol->udf_session->disc == disc) {
SLIST_FOREACH(udf_log_vol, &udf_pri_vol->log_vols, next_logvol) {
/* retrieving logical volume integrity sequence */
UDF_VERBOSE(udf_dump_volume_name("\t\tLogical volume ", udf_log_vol));
UDF_VERBOSE(printf("\t\t\tintegrity\n"));
logvolint_error = udf_proc_logvol_integrity_sequence(udf_log_vol);
/* load in supporting tables */
UDF_VERBOSE(printf("\t\t\tsupporting tables\n"));
error = udf_retrieve_supporting_tables(udf_log_vol);
/* if the state is still marked `open', its dirty and we mount read-only for safety */
if (logvolint_error) {
printf("\t\t\t*** marked read-only due to logvol integrity error ***\n");
mnt_flags |= UDF_MNT_RDONLY;
}
if (udf_log_vol->logvol_state == UDF_INTEGRITY_OPEN) {
printf("\t\t\t*** marked read-only due to open logical volume ***\n");
mnt_flags |= UDF_MNT_RDONLY;
}
/* get fileset descriptors */
UDF_VERBOSE(printf("\t\t\tfileset(s)\n"));
if (!error) error = udf_retrieve_fileset_descriptor(udf_log_vol);
/* check if the logical volume is writable */
UDF_VERBOSE(printf("\t\t\tchecking writable filesets\n"));
if (!error) error = udf_check_writable_filesets(udf_log_vol, mnt_flags);
/* load in free/used space tables for writable volsets */
UDF_VERBOSE(printf("\t\t\tused/freed space tables\n"));
if (!error) error = udf_retrieve_space_tables(udf_log_vol);
if (error) {
udf_log_vol->broken = 1;
} else {
one_good_found = 1;
}
} /* logical */
} /* disc */
} /* primary */
} /* if */
} /* volumeset */
return one_good_found? 0 : ENOENT;
}
/******************************************************************************************
*
* Disc sync
*
******************************************************************************************/
void udf_sync_tables_callback(int reason, struct udf_wrcallback *wrcallback, int error, uint8_t *sectordata) {
/* struct udf_node *udf_node = (struct udf_node *) wrcallback->structure; */
wrcallback = wrcallback; /* not used now */
sectordata = sectordata;
if (reason == UDF_WRCALLBACK_REASON_PENDING) {
/* what to do? */
return;
}
if (reason == UDF_WRCALLBACK_REASON_ANULATE) {
/* what to do? */
return;
}
assert(reason == UDF_WRCALLBACK_REASON_WRITTEN);
if (error) {
printf("UDF error: sync tables write errors in syncnode not fixed!\n");
return;
}
}
/* TODO space tables are not coupled on a logical volume but on a partition/disc, so call them on that instead of logvol */
int udf_sync_space_tables(struct udf_log_vol *udf_log_vol) {
struct udf_partition *udf_partition;
struct udf_part_mapping *part_mapping;
struct part_hdr_desc *part_hdr_desc;
struct udf_wrcallback wr_callback;
union dscrptr *dscrptr;
uint64_t length;
uint32_t sector;
uint32_t lb_size, part_len;
uint16_t dscr_ver;
int part_num;
int error;
lb_size = udf_log_vol->lb_size;
wr_callback.function = udf_sync_tables_callback;
SLIST_FOREACH(part_mapping, &udf_log_vol->part_mappings, next_mapping) {
part_num = part_mapping->udf_virt_part_num;
UDF_VERBOSE_TABLES(printf("\tFor partition mapping %d->%d\n", part_num, part_mapping->udf_phys_part_num));
/* retrieve and process unallocated- and freed-space information for all used partitions of the logvol */
error = udf_logvol_vpart_to_partition(udf_log_vol, part_num, NULL, &udf_partition);
assert(udf_partition);
part_hdr_desc = &udf_partition->partition->pd_part_hdr;
// part_start = udf_rw32(udf_partition->partition->start_loc);
part_len = udf_rw32(udf_partition->partition->part_len);
dscr_ver = udf_rw16(udf_partition->partition->tag.descriptor_ver);
sector = udf_rw32(part_hdr_desc->unalloc_space_table.lb_num);
length = udf_rw32(part_hdr_desc->unalloc_space_table.len); /* needed? */
if (length) {
printf("UDF: Can't write space tables yet\n");
#if 0
error = udf_read_logvol_descriptor(udf_log_vol, part_num, sector, "Unallocated space table", &dscrptr, NULL);
UDF_VERBOSE_MAX(printf("\tUnalloced space table\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
//udf_process_space_table(&udf_partition->unalloc_space, dscrptr);
free(dscrptr);
#endif
}
sector = udf_rw32(part_hdr_desc->unalloc_space_bitmap.lb_num);
length = udf_rw32(part_hdr_desc->unalloc_space_bitmap.len);
/* printf("unalloc dscr at partition sector %d\n", sector); */
if (length) {
/* read it in and modify */
dscrptr = (union dscrptr *) udf_partition->unalloc_space_bitmap;
if (!dscrptr) {
printf("Warning: creating empty unallocated space bitmap for partition's is gone\n");
error = udf_create_empty_space_bitmap(lb_size, dscr_ver, /* num_lbs */ part_len, (struct space_bitmap_desc **) &dscrptr);
assert(!error);
assert(udf_calc_tag_malloc_size(dscrptr, lb_size) <= length);
udf_partition->unalloc_space_bitmap = &dscrptr->sbd;
}
udf_sync_space_bitmap(&udf_partition->unalloc_space_queue, &dscrptr->sbd, lb_size);
UDF_VERBOSE_MAX(printf("\tWriteout unallocated space bitmap\n"));
UDF_VERBOSE_MAX(udf_validate_tag_and_crc_sums((union dscrptr *) dscrptr); udf_dump_descriptor(dscrptr));
udf_write_partition_descriptor(udf_partition, sector, "Unallocated space bitmap", dscrptr, &wr_callback); /* SESSION descriptor!! */
}
sector = udf_rw32(part_hdr_desc->freed_space_table.lb_num);
length = udf_rw32(part_hdr_desc->freed_space_table.len);
if (length) {
printf("UDF: Can't write space tables yet\n");
#if 0
error = udf_read_logvol_descriptor(udf_log_vol, part_num, sector, "Freed space table", &dscrptr, NULL);
UDF_VERBOSE_MAX(printf("\tFreed space table\n"));
UDF_VERBOSE_MAX(udf_dump_descriptor(dscrptr));
//udf_process_space_table(&udf_partition->freed_space, dscrptr);
free(dscrptr);
#endif
}
sector = udf_rw32(part_hdr_desc->freed_space_bitmap.lb_num);
length = udf_rw32(part_hdr_desc->freed_space_bitmap.len);
/* printf("freed dscr at partition sector %d\n", sector); */
if (length) {
/* read it in and modify */
dscrptr = (union dscrptr *) udf_partition->freed_space_bitmap;
if (!dscrptr) {
printf("Warning: creating empty freed space bitmap for partition's is gone\n");
error = udf_create_empty_space_bitmap(lb_size, dscr_ver, part_len, (struct space_bitmap_desc **) &dscrptr);
assert(!error);
assert(udf_calc_tag_malloc_size(dscrptr, lb_size) <= length);
udf_partition->freed_space_bitmap = &dscrptr->sbd;
}
udf_sync_space_bitmap(&udf_partition->freed_space_queue, &dscrptr->sbd, lb_size);
UDF_VERBOSE_MAX(printf("\tWriteout freed space bitmap\n"));
UDF_VERBOSE_MAX(udf_validate_tag_and_crc_sums((union dscrptr *) dscrptr); udf_dump_descriptor(dscrptr));
udf_write_partition_descriptor(udf_partition, sector, "Freed space bitmap", dscrptr, &wr_callback); /* SESSION descriptor!! */
}
}
UDF_VERBOSE_TABLES(printf("\n"));
return 0;
}
int udf_writeout_LVID(struct udf_log_vol *udf_log_vol, int type) {
union dscrptr *dscr;
struct logvol_int_desc *intdesc;
struct udf_logvol_info *impl;
struct udf_session *session;
struct udf_partition *udf_partition;
struct udf_part_mapping *part_mapping;
struct desc_tag *terminator;
struct udf_wrcallback wr_callback;
uint32_t sector, lvid_sector, term_sector;
uint32_t part_num, *free_space_pos, *size_pos, lb_size;
uint32_t len, length, lvid_len, num_sectors;
int error, dscr_ver, tagid;
/* create a new `fresh' logvol integrity */
session = udf_log_vol->primary->udf_session;
lb_size = udf_log_vol->lb_size;
num_sectors = lb_size / session->disc->sector_size;
sector = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.loc);
length = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.len);
if (!length)
return ENOENT;
intdesc = calloc(1, udf_log_vol->lb_size);
if (!intdesc)
return ENOMEM;
/* search insertion place */
lvid_sector = 0;
term_sector = 0;
while (length) {
error = udf_read_session_descriptor(udf_log_vol->primary->udf_session, sector, "Logical volume integrity descriptor (LVID)", &dscr, &lvid_len);
/* getting a terminator tag or zero is an OK condition */
if (error) {
tagid = 0;
} else {
tagid = udf_rw16(dscr->tag.id);
}
if ((tagid == TAGID_TERM) || (tagid == 0)) {
lvid_sector = sector;
if (length > lb_size) {
/* space for a terminator */
term_sector = sector + num_sectors;
}
break; /* while */
}
length -= lb_size;
sector += num_sectors;
if (udf_rw32(dscr->lvid.next_extent.len)) {
sector = udf_rw32(dscr->lvid.next_extent.loc);
length = udf_rw32(dscr->lvid.next_extent.len);
}
/* free consumed descriptor */
free(dscr);
dscr = NULL;
}
if (dscr) free(dscr);
if ((!lvid_sector) || (length == 0)) {
sector = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.loc);
length = udf_rw32(udf_log_vol->log_vol->integrity_seq_loc.len);
lvid_sector = sector;
if (length > lb_size) {
/* space for a terminator */
term_sector = lvid_sector + num_sectors;
}
}
assert(lvid_sector);
/* build up integrity descriptor and write it out */
dscr_ver = udf_rw16(udf_log_vol->log_vol->tag.descriptor_ver);
udf_init_desc_tag(&intdesc->tag, TAGID_LOGVOL_INTEGRITY, dscr_ver, udf_log_vol->integrity_serial);
udf_set_timestamp_now(&intdesc->time);
intdesc->integrity_type = udf_rw32(type);
intdesc->lvint_next_unique_id = udf_rw64(udf_log_vol->next_unique_id);
/* calculate and fill in free space */
intdesc->num_part = udf_rw32(udf_log_vol->num_part_mappings);
free_space_pos = &intdesc->tables[0];
size_pos = &intdesc->tables[udf_log_vol->num_part_mappings];
SLIST_FOREACH(part_mapping, &udf_log_vol->part_mappings, next_mapping) {
part_num = part_mapping->udf_virt_part_num;
udf_logvol_vpart_to_partition(udf_log_vol, part_num, NULL, &udf_partition);
assert(udf_partition);
*size_pos++ = udf_partition->partition->part_len;
*free_space_pos++ = udf_rw32(udf_partition->free_unalloc_space / udf_log_vol->lb_size);
}
/* fill in UDF implementation use parameters */
impl = (struct udf_logvol_info *) (&intdesc->tables[2*udf_log_vol->num_part_mappings]);
udf_set_imp_id(&impl->impl_id);
impl->num_files = udf_rw32(udf_log_vol->num_files);
impl->num_directories = udf_rw32(udf_log_vol->num_directories);
impl->min_udf_readver = udf_rw16(udf_log_vol->min_udf_readver);
impl->min_udf_writever = udf_rw16(udf_log_vol->min_udf_writever);
impl->max_udf_writever = udf_rw16(udf_log_vol->max_udf_writever);
intdesc->l_iu = udf_rw32(sizeof(struct udf_logvol_info)); /* ECMA 3/10.10.7, UDF 2.2.6.4. */
len = sizeof(struct logvol_int_desc) - sizeof(uint32_t); /* length of logvol_int_desc without the extra table entry */
len += sizeof(uint32_t) * 2 * udf_log_vol->num_part_mappings; /* size and free space */
len += sizeof(struct udf_logvol_info); /* extra implementation use area */
len -= UDF_DESC_TAG_LENGTH; /* without header */
intdesc->tag.desc_crc_len = udf_rw16(len);
udf_write_session_descriptor(session, lvid_sector, "Logvol integrity descriptor (LVID)", (union dscrptr *) intdesc, &wr_callback);
if (session->disc->rewritable && term_sector) {
/* only when there is space and its a rewritable media add a terminor */
error = udf_create_empty_terminator_descriptor(lb_size, dscr_ver, &terminator);
if (!error) {
udf_write_session_descriptor(session, term_sector, "Logvol integrity sequence descriptor sequence terminator", (union dscrptr *) terminator, &wr_callback);
free(terminator);
}
}
free(intdesc);
return 0;
}
/* mark the logical volume `open'; for non-rewritables (CD-R/DVD+R/DVD-R) this is allmost a no-op */
int udf_open_logvol(struct udf_log_vol *udf_log_vol) {
int error;
if (!udf_log_vol->writable) {
udf_dump_volume_name("\nLogical volume marked read only: ", udf_log_vol);
return EROFS;
}
/* will return many times for each write */
if (udf_log_vol->logvol_state == UDF_INTEGRITY_OPEN)
return 0;
/*
* Opening and closing logical volumes is derived from the state of
* the primaries disc.
*/
udf_dump_volume_name("Opening logical volume", udf_log_vol);
if (!udf_log_vol->primary->udf_session->disc->sequential) {
error = udf_writeout_LVID(udf_log_vol, UDF_INTEGRITY_OPEN);
assert(!error);
/* sync caches to make sure all is written out */
udf_sync_caches(udf_log_vol);
/* FIXME (callback) XXX ought to wait until we get the ALL-OK signal from the writeout-LVID action XXX */
} else {
/* sequential recordable; any write just opens it; the descriptor is allready marked open */
}
/* mark it open */
udf_log_vol->logvol_state = UDF_INTEGRITY_OPEN;
return 0;
}
/* mark the logical volume in a `closed' state; close the integrity when possible for recordables writeout VAT */
int udf_close_logvol(struct udf_log_vol *udf_log_vol) {
int error;
if (udf_log_vol->logvol_state == UDF_INTEGRITY_CLOSED) {
DEBUG(printf("close logvol: integrity allready closed\n"));
return 0;
}
/*
* Opening and closing logical volumes is derived from the state of
* the primaries disc.
*/
udf_dump_volume_name("Closing logical volume", udf_log_vol);
if (!udf_log_vol->primary->udf_session->disc->sequential) {
error = udf_writeout_LVID(udf_log_vol, UDF_INTEGRITY_CLOSED);
assert(!error);
} else {
/* XXX TODO XXX */
fprintf(stderr, "write out virtual sectors, compile VAT and write out VAT : not implemented\n");
return EIO;
}
/* sync caches to make sure all is written out */
udf_sync_caches(udf_log_vol);
/* FIXME (callback) XXX ought to wait until we get the ALL-OK signal from the writeout-LVID action XXX */
/* mark it closed again */
udf_log_vol->logvol_state = UDF_INTEGRITY_CLOSED;
return 0;
}
int udf_sync_logvol(struct udf_log_vol *udf_log_vol) {
struct udf_node *udf_node;
uint32_t num_dirty, count, prnt;
int error;
if (!udf_log_vol->writable)
return 0;
if (udf_log_vol->logvol_state == UDF_INTEGRITY_CLOSED) {
DEBUG(printf("close logvol: its closed so no sync nessisary\n"));
return 0;
}
UDF_VERBOSE(udf_dump_volume_name("\tsyncing ", udf_log_vol));
/* sync all nodes */
/* XXX syncing logvol sequential due to insertion sort in add node XXX */
num_dirty = 0;
TAILQ_FOREACH(udf_node, &udf_log_vol->dirty_nodes, next_dirty) {
num_dirty++;
}
/*
* Purge all data out first, this will speed things up later (not
* strickly nessissary since syncing a node will wait for all the data
* to be written out first anyway
*/
count = num_dirty;
prnt = 0;
UDF_VERBOSE(printf("\t\tsyncing data\n"));
TAILQ_FOREACH(udf_node, &udf_log_vol->dirty_nodes, next_dirty) {
UDF_VERBOSE(printf("\r%8d", count); fflush(stdout));
udf_sync_udf_node(udf_node, "Sync Logvol");
count--;
prnt = 1;
}
if (prnt) UDF_VERBOSE(printf("\r \r"));
/*
* Purge all nodes out... they ought to have no dirty buffers anymore
* but they will write them out if deemed nessisary
*/
count = num_dirty;
prnt = 0;
UDF_VERBOSE(printf("\t\tsyncing nodes\n"));
TAILQ_FOREACH(udf_node, &udf_log_vol->dirty_nodes, next_dirty) {
UDF_VERBOSE(printf("\r%8d", count); fflush(stdout));
DEBUG(printf("N"); fflush(stdout));
udf_writeout_udf_node(udf_node, "Sync Logvol");
count--;
prnt = 1;
}
if (prnt) UDF_VERBOSE(printf("\r \r"));
/* shouldn't be nessisary */
udf_bufcache->flushall = 1;
udf_purgethread_kick("Sync Logvol");
usleep(1);
if (udf_bufcache->lru_len_dirty_metadata + udf_bufcache->lru_len_dirty_data) {
printf("Warning: after syncing logvol dirty counts != 0 (%d, %d); please contact author.\n",
udf_bufcache->lru_len_dirty_metadata, udf_bufcache->lru_len_dirty_data);
}
/* sync free and used space tables for writable volsets */
UDF_VERBOSE(printf("\t\tused/freed space tables\n"));
error = udf_sync_space_tables(udf_log_vol);
/* close logical volume */
udf_close_logvol(udf_log_vol);
return error;
}
/* convenience routine */
int udf_sync_disc(struct udf_discinfo *disc) {
struct udf_volumeset *udf_volumeset;
struct udf_pri_vol *udf_pri_vol;
struct udf_log_vol *udf_log_vol;
SLIST_FOREACH(udf_volumeset, &udf_volumeset_list, next_volumeset) {
if (!udf_volumeset->obsolete) {
STAILQ_FOREACH(udf_pri_vol, &udf_volumeset->primaries, next_primary) {
if (udf_pri_vol->udf_session->disc == disc) {
SLIST_FOREACH(udf_log_vol, &udf_pri_vol->log_vols, next_logvol) {
udf_sync_logvol(udf_log_vol);
} /* logical */
} /* disc */
} /* primary */
} /* if */
} /* volumeset */
return 0;
}
/******************************************************************************************
*
* UDF descriptor buildup and update functions
*
******************************************************************************************/
static void udf_init_desc_tag(struct desc_tag *tag, uint16_t id, uint16_t dscr_ver, uint16_t serial_num) {
bzero(tag, sizeof(struct desc_tag));
tag->id = udf_rw16(id);
tag->descriptor_ver = udf_rw16(dscr_ver);
tag->serial_num = udf_rw16(serial_num);
/* the rest gets filled in when we write */
}
static void udf_osta_charset(struct charspec *charspec) {
bzero(charspec, sizeof(struct charspec));
charspec->type = 0;
strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
}
static void udf_encode_osta_id(char *osta_id, uint16_t len, char *text) {
uint16_t u16_name[1024];
uint8_t *pos;
uint16_t *pos16;
bzero(osta_id, len);
if (!text) return;
bzero(u16_name, sizeof(uint16_t) * 1023);
/* convert ascii to 16 bits unicode */
pos = (uint8_t *) text;
pos16 = u16_name;
while (*pos) {
*pos16 = *pos;
pos++; pos16++;
}
*pos16 = 0;
udf_CompressUnicode(len, 8, (unicode_t *) u16_name, (byte *) osta_id);
/* Ecma 167/7.2.13 states that the length is recorded in the last byte */
osta_id[len-1] = strlen(text)+1;
}
static void udf_set_app_id(struct regid *regid) {
bzero(regid, sizeof(struct regid));
regid->flags = 0; /* not dirty and not protected */
strcpy((char *) regid->id, APP_NAME);
regid->id_suffix[0] = APP_VERSION_MAIN;
regid->id_suffix[1] = APP_VERSION_SUB;
}
static void udf_set_imp_id(struct regid *regid) {
bzero(regid, sizeof(struct regid));
regid->flags = 0; /* not dirty and not protected */
strcpy((char *) regid->id, IMPL_NAME);
regid->id_suffix[0] = 4; /* unix */
regid->id_suffix[1] = 0; /* generic */
#if defined(__ANONYMOUSUDF__)
#elif defined(__NetBSD__)
regid->id_suffix[1] = 8; /* NetBSD */
#elif defined(__FreeBSD__)
regid->id_suffix[1] = 7; /* FreeBSD */
#elif defined(LINUX)
regid->id_suffix[1] = 5; /* Linux */
#endif
}
static void udf_set_entity_id(struct regid *regid, char *name, uint16_t UDF_version) {
uint16_t *ver;
bzero(regid, sizeof(struct regid));
regid->flags = 0; /* not dirty and not protected */
strcpy((char *) regid->id, name);
ver = (uint16_t *) regid->id_suffix;
*ver = udf_rw16(UDF_version);
regid->id_suffix[2] = 4; /* unix */
regid->id_suffix[3] = 0; /* generic */
#if defined(__ANONYMOUSUDF__)
#elif defined(__NetBSD__)
regid->id_suffix[3] = 8; /* NetBSD */
#elif defined(__FreeBSD__)
regid->id_suffix[3] = 7; /* FreeBSD */
#elif defined(LINUX)
regid->id_suffix[3] = 5; /* Linux */
#endif
}
void udf_set_contents_id(struct regid *regid, char *content_id) {
bzero(regid, sizeof(struct regid));
regid->flags = 0;
strcpy((char *) regid->id, content_id);
}
/* XXX creators of empty descriptors could be externalised */
/*
* result can be further processed using modify functions if demanded and then
* processed trough udf_proc_pri_vol
* [ int udf_proc_pri_vol(struct udf_session *udf_session, struct udf_pri_vol **current, struct pri_vol_desc *incomming); ]
*
*/
int udf_create_empty_primary_volume_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint16_t serial, char *volset_id, char *privol_name, int vds_num, int max_vol_seq, struct pri_vol_desc **dscrptr) {
struct pri_vol_desc *dscr;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate an empty primary volume descriptor */
dscr = malloc(sector_size);
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_PRI_VOL, dscr_ver, 1);
dscr->pvd_num = udf_rw32(serial);
udf_encode_osta_id(dscr->vol_id, 32, privol_name);
dscr->vds_num = udf_rw16(vds_num);
dscr->max_vol_seq = udf_rw16(max_vol_seq);
if (max_vol_seq > 1) {
dscr->ichg_lvl = udf_rw16(3); /* signal its a single volume intended to be in a set */
dscr->max_ichg_lvl = udf_rw16(3); /* ,, */
dscr->flags = udf_rw16(1); /* signal relevance volumeset id */
} else {
dscr->ichg_lvl = udf_rw16(2); /* signal its volume intended not to be in a set */
dscr->max_ichg_lvl = udf_rw16(2); /* ,, */
dscr->flags = udf_rw16(0); /* signal relevance volumeset id */
}
dscr->charset_list = udf_rw32(1); /* only CS0 */
dscr->max_charset_list = udf_rw32(1);
udf_encode_osta_id(dscr->volset_id, 128, volset_id);
udf_osta_charset(&dscr->desc_charset);
udf_osta_charset(&dscr->explanatory_charset);
udf_set_app_id(&dscr->app_id);
udf_set_imp_id(&dscr->imp_id);
udf_set_timestamp_now(&dscr->time);
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct pri_vol_desc) - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_partition_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint16_t serial, uint16_t part_num, uint32_t access_type, uint32_t start_loc, uint32_t part_len, uint32_t space_bitmap_size, uint32_t unalloc_space_bitmap, struct part_desc **dscrptr) {
struct part_desc *dscr;
struct part_hdr_desc *part_hdr;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate empty partition descriptor */
dscr = malloc(sector_size); /* only descriptor, no bitmap! */
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_PARTITION, dscr_ver, 1);
dscr->seq_num = udf_rw32(serial);
dscr->flags = udf_rw16(1); /* bit 0 : space is allocated */
dscr->part_num = udf_rw16(part_num);
if (dscr_ver == 2) udf_set_contents_id(&dscr->contents, "+NSR02");
if (dscr_ver == 3) udf_set_contents_id(&dscr->contents, "+NSR03");
part_hdr = &dscr->pd_part_hdr;
part_hdr->unalloc_space_bitmap.len = udf_rw32(space_bitmap_size);
part_hdr->unalloc_space_bitmap.lb_num = udf_rw32(unalloc_space_bitmap);
dscr->access_type = udf_rw32(access_type);
dscr->start_loc = udf_rw32(start_loc);
dscr->part_len = udf_rw32(part_len);
udf_set_imp_id(&dscr->imp_id); /* why is this ignored? */
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct part_desc) - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_unallocated_space_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint16_t serial, struct unalloc_sp_desc **dscrptr) {
struct unalloc_sp_desc *dscr;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate an empty unallocated space descriptor */
dscr = malloc(sector_size);
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_UNALLOC_SPACE, dscr_ver, 1);
dscr->seq_num = udf_rw32(serial);
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct unalloc_sp_desc) - sizeof(struct extent_ad) - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_implementation_use_volume_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint16_t serial, char *logvol_name, struct impvol_desc **dscrptr) {
struct impvol_desc *dscr;
struct udf_lv_info *lv_info;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate an empty implementation use volume descriptor */
dscr = malloc(sector_size);
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_IMP_VOL, dscr_ver, 1);
dscr->seq_num = udf_rw32(serial);
udf_set_entity_id(&dscr->impl_id, "*UDF LV Info", 0x102); /* just pick one; it'll be modifed later */
lv_info = &dscr->_impl_use.lv_info;
udf_osta_charset(&lv_info->lvi_charset);
udf_encode_osta_id(lv_info->logvol_id, 128, logvol_name);
udf_encode_osta_id(lv_info->lvinfo1, 36, NULL);
udf_encode_osta_id(lv_info->lvinfo2, 36, NULL);
udf_encode_osta_id(lv_info->lvinfo3, 36, NULL);
udf_set_imp_id(&lv_info->impl_id);
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct impvol_desc) - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_logical_volume_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint16_t serial, char *logvol_name, uint32_t lb_size, uint32_t integrity_start, uint32_t integrity_length, struct logvol_desc **dscrptr) {
struct logvol_desc *dscr;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate an empty logical volume descriptor */
dscr = malloc(sector_size);
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_LOGVOL, dscr_ver, 1);
dscr->seq_num = udf_rw32(serial);
udf_osta_charset(&dscr->desc_charset);
udf_encode_osta_id(dscr->logvol_id, 128, logvol_name);
dscr->lb_size = udf_rw32(lb_size);
udf_set_contents_id(&dscr->domain_id, "*OSTA UDF Compliant");
/* no fsd yet nor partition mapping */
udf_set_imp_id(&dscr->imp_id);
dscr->integrity_seq_loc.loc = udf_rw32(integrity_start);
dscr->integrity_seq_loc.len = udf_rw32(integrity_length * lb_size);
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct logvol_desc) - 1 - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_space_bitmap(uint32_t sector_size, uint16_t dscr_ver, uint32_t num_lbs, struct space_bitmap_desc **dscrptr) {
struct space_bitmap_desc *dscr;
uint64_t bits;
uint32_t bytes, space_bitmap_size;
assert(dscrptr);
*dscrptr = NULL;
/* reserve space for unallocated space bitmap */
bits = num_lbs;
bytes = (bits + 7)/8;
space_bitmap_size = (bytes + sizeof(struct space_bitmap_desc)-1);
/* round space bitmap size to sector size */
space_bitmap_size = ((space_bitmap_size + sector_size - 1) / sector_size) * sector_size;
/* allocate and populate an empty space bitmap descriptor */
dscr = malloc(space_bitmap_size);
if (!dscr) return ENOMEM;
bzero(dscr, space_bitmap_size);
udf_init_desc_tag(&dscr->tag, TAGID_SPACE_BITMAP, dscr_ver, 1);
/* crc length 8 is recommended, UDF 2.3.1.2, 2.3.8.1, errata DCN-5108 for UDF 2.50 and lower. */
dscr->tag.desc_crc_len = udf_rw16(8);
dscr->num_bits = udf_rw32(bits);
dscr->num_bytes = udf_rw32(bytes);
*dscrptr = dscr;
return 0;
}
/* FIXME: no rootdir setting yet */
/* FIXME: fileset desc. is disc sector size or lb_size ? */
int udf_create_empty_fileset_desc(uint32_t sector_size, uint16_t dscr_ver, uint32_t fileset_num, char *logvol_name, char *fileset_name, struct fileset_desc **dscrptr) {
struct fileset_desc *dscr;
assert(dscrptr);
*dscrptr = NULL;
/* allocate and populate an empty logical volume descriptor */
dscr = malloc(sector_size);
if (!dscr) return ENOMEM;
bzero(dscr, sector_size);
udf_init_desc_tag(&dscr->tag, TAGID_FSD, dscr_ver, 1);
udf_set_timestamp_now(&dscr->time);
dscr->ichg_lvl = udf_rw16(3); /* fixed? */
dscr->max_ichg_lvl = udf_rw16(3); /* fixed? */
dscr->charset_list = udf_rw32(1); /* only CS0 */
dscr->max_charset_list = udf_rw32(1); /* only CS0 */
dscr->fileset_num = udf_rw32(fileset_num); /* key for fileset */
dscr->fileset_desc_num = udf_rw32(0); /* fileset descriptor number as in copy # */
udf_osta_charset(&dscr->logvol_id_charset);
udf_encode_osta_id(dscr->logvol_id, 128, logvol_name);
udf_osta_charset(&dscr->fileset_charset);
udf_encode_osta_id(dscr->fileset_id, 32, fileset_name);
udf_encode_osta_id(dscr->copyright_file_id, 32, NULL);
udf_encode_osta_id(dscr->abstract_file_id, 32, NULL);
udf_set_contents_id(&dscr->domain_id, "*OSTA UDF Compliant");
dscr->tag.desc_crc_len = udf_rw16(sizeof(struct fileset_desc) - UDF_DESC_TAG_LENGTH);
*dscrptr = dscr;
return 0;
}
int udf_create_empty_anchor_volume_descriptor(uint32_t sector_size, uint16_t dscr_ver, uint32_t main_vds_loc, uint32_t reserve_vds_loc, uint32_t length, struct anchor_vdp **vdp) {
assert(vdp);
assert(main_vds_loc - reserve_vds_loc >= length);
*vdp = malloc(sector_size);
if (!*vdp) return ENOMEM;
bzero(*vdp, sector_size);
udf_init_desc_tag(&(*vdp)->tag, TAGID_ANCHOR, dscr_ver, 1);
(*vdp)->main_vds_ex.loc = udf_rw32(main_vds_loc);
(*vdp)->main_vds_ex.len = udf_rw32(length * sector_size);
(*vdp)->reserve_vds_ex.loc = udf_rw32(reserve_vds_loc);
(*vdp)->reserve_vds_ex.len = udf_rw32(length * sector_size);
(*vdp)->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH); /* fixed size in Ecma */
return 0;
}
int udf_create_empty_terminator_descriptor(uint32_t sector_size, uint16_t dscr_ver, struct desc_tag **tag) {
assert(tag);
*tag = malloc(sector_size);
if (!*tag) return ENOMEM;
bzero(*tag, sector_size);
udf_init_desc_tag(*tag, TAGID_TERM, dscr_ver, 1);
(*tag)->desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH); /* fixed size in Ecma */
return 0;
}
/******************************************************************************************
*
* Basic `open' and `close' disc functions
*
******************************************************************************************/
static void udf_process_session_range(struct udf_discinfo *disc, int *enabled, int low, int high) {
int session;
if (!disc) return;
high = MIN(high, disc->num_sessions-1);
session = low;
for (session = low; session <= high; session++) {
enabled[session] = 1;
}
}
/* range is specified in -3,5,7 or 5-6,8- etc */
static int udf_process_session_range_string(struct udf_discinfo *disc, char *range) {
struct udf_session *udf_session, *next_udf_session;
char *pos, *nop;
int low, high, len, session;
int enabled[MAX_SESSIONS];
if (!range) return 0;
DEBUG(printf("UDF range debugging string '%s'\n", range));
if (disc) {
/* disable all */
for (session = 0; session < disc->num_sessions; session++) {
enabled[session] = 0;
}
}
/* parse string */
nop = strdup(range);
pos = range;
if (sscanf(pos, "-%u%n%s", &high, &len, nop) >= 1) {
DEBUG(printf("UDF range match till %d\n", high));
udf_process_session_range(disc, enabled, 0, high);
pos += len;
}
if (*pos && *pos == ',') pos++;
while (*pos) {
if (sscanf(pos, "%u%n%s", &low, &len, nop) >= 1) {
pos += len;
if (*pos == '-') {
pos++;
if (!*pos) {
DEBUG(printf("UDF range match from %d\n", low));
udf_process_session_range(disc, enabled, low, INT_MAX);
free(nop);
return 0;
}
if (sscanf(pos, "%u%n%s", &high, &len, nop) >= 1) {
pos += len;
DEBUG(printf("UDF range match from %d to %d\n", low, high));
udf_process_session_range(disc, enabled, low, high);
}
} else {
if (!*pos || (*pos == ',')) {
DEBUG(printf("UDF range match %d\n", low));
udf_process_session_range(disc, enabled, low, low);
}
}
if (*pos && (*pos != ',')) {
fprintf(stderr, "UDF range matching : ',' expected at %s\n", pos);
free(nop);
return ENOENT;
}
pos++;
} else {
fprintf(stderr, "UDF range matching : number expected at %s\n", pos);
free(nop);
return ENOENT;
}
}
free(nop);
DEBUG(printf("UDF range matching : all ok till the end\n"));
if (!disc) return 0;
udf_session = STAILQ_FIRST(&disc->sessions);
while (udf_session) {
next_udf_session = STAILQ_NEXT(udf_session, next_session);
session = udf_session->session_num;
if (!enabled[session]) {
/* remove this session */
fprintf(stderr, "UDF: disabling UDF session %d on request\n", session);
STAILQ_REMOVE(&disc->sessions, udf_session, udf_session, next_session);
free(udf_session);
disc->session_is_UDF[session] = 0;
}
udf_session = next_udf_session;
}
return 0;
}
int udf_check_session_range(char *range) {
return udf_process_session_range_string(NULL, range);
}
void
udf_init(void)
{
udf_unix_init();
udf_start_unix_thread();
dirhash_init();
SLIST_INIT(&udf_discs_list);
}
int udf_mount_disc(char *devname, char *range, uint32_t sector_size, int mnt_flags, struct udf_discinfo **disc) {
int discop_flags, error;
discop_flags = mnt_flags & UDF_MNT_BSWAP ? UDF_DISCOP_BSWAP : 0;
error = udf_open_disc(devname, discop_flags, disc);
if ((!error) && sector_size)
error = udf_discinfo_alter_perception(*disc, sector_size, 0);
if (error)
return error;
error = udf_get_anchors(*disc);
UDF_VERBOSE(udf_dump_disc_anchors(*disc));
if (range) {
UDF_VERBOSE(printf("Selecting UDF sessions '%s' as specified\n", range));
udf_process_session_range_string(*disc, range);
UDF_VERBOSE(udf_dump_disc_anchors(*disc));
}
/* no UDF partitions so bail out */
if ((*disc)->num_udf_sessions == 0) return 0;
UDF_VERBOSE(printf("Start mounting\n"));
error = udf_get_volumeset_space(*disc);
if (error) return error;
UDF_VERBOSE(printf("\teliminating predescessors\n"));
udf_eliminate_predescessor_volumesets(*disc);
UDF_VERBOSE_TABLES(udf_dump_alive_sets());
UDF_VERBOSE(printf("\tretrieving logical volume dependencies %p\n", *disc));
error = udf_get_logical_volumes_supporting_tables(*disc, mnt_flags);
UDF_VERBOSE_TABLES(udf_dump_alive_sets());
/* insert disc in the disc list */
SLIST_INSERT_HEAD(&udf_discs_list, *disc, next_disc);
return error;
}
int udf_dismount_disc(struct udf_discinfo *disc) {
UDF_VERBOSE(printf("Dismounting disc\n"));
if (!disc->recordable) {
/* easy way out: it was a read-only system */
UDF_VERBOSE(printf("\tdismounting readonly disc\n"));
udf_stop_unix_thread();
udf_close_disc(disc);
return 0;
}
/* Sync disc before closing it */
UDF_VERBOSE(printf("\tsyncing disc\n"));
udf_sync_disc(disc);
/* wait for the disc to idle */
UDF_VERBOSE(printf("\twait for syncing disc to idle\n"));
while (!udf_discinfo_check_disc_ready(disc)) {
sleep(1);
}
/* stop threads and finish writing to it */
udf_stop_unix_thread();
UDF_VERBOSE(printf("\tsignal disc its finished with writing\n"));
udf_discinfo_finish_writing(disc);
/* wait for the disc to idle again */
UDF_VERBOSE(printf("\twait for final disc idling\n"));
while (!udf_discinfo_check_disc_ready(disc)) {
sleep(1);
}
UDF_VERBOSE(printf("\tclose device\n"));
udf_close_disc(disc);
return 0;
}
/******************************************************************************************
*
* Directory and other conversion UDF logic
* Move to udf_unix.c / udf_vnops.c one day?
*
******************************************************************************************/
static int udf_translate_icb_filetype_to_dirent_filetype(int udf_filetype) {
int d_type;
switch (udf_filetype) {
case UDF_ICB_FILETYPE_DIRECTORY :
d_type = DT_DIR;
break;
case UDF_ICB_FILETYPE_STREAMDIR :
d_type = DT_DIR;
break;
case UDF_ICB_FILETYPE_FIFO :
d_type = DT_FIFO;
break;
case UDF_ICB_FILETYPE_CHARDEVICE :
d_type = DT_CHR;
break;
case UDF_ICB_FILETYPE_BLOCKDEVICE :
d_type = DT_BLK;
break;
case UDF_ICB_FILETYPE_RANDOMACCESS :
d_type = DT_REG;
break;
case UDF_ICB_FILETYPE_SYMLINK :
d_type = DT_LNK;
break;
case UDF_ICB_FILETYPE_SOCKET :
d_type = DT_SOCK;
break;
default :
d_type = DT_UNKNOWN;
break;
}
return d_type;
}
/* VOP_GETATTR */
/* allmost NOOP since we remember the stat in the inode */
int udf_getattr(struct udf_node *udf_node, struct stat *stat) {
*stat = udf_node->stat;
/* special: updatables */
stat->st_nlink = udf_node->link_cnt;
stat->st_blocks = (stat->st_size + 512 -1)/512; /* blocks are hardcoded 512 bytes/sector in stat :-/ */
return 0;
}
/* VOP_SETATTR */
/* allmost NOOP since we remember the stat in the inode */
/* note VOP_SETATTR can selectively set attrs */
int udf_setattr(struct udf_node *udf_node, struct stat *stat) {
if (!udf_node) return ENOENT;
if (udf_open_logvol(udf_node->udf_log_vol))
return EROFS;
/* FIXME please don't just copy everything ... XXX */
udf_node->stat = *stat;
/* not attribute change time */
udf_set_timespec_now(&udf_node->stat.st_ctimespec);
udf_node_mark_dirty(udf_node);
return 0;
}
void udf_resync_fid_stream(uint8_t *buffer, uint32_t *pfid_pos, uint32_t max_fid_pos, int *phas_fids) {
struct fileid_desc *fid;
uint32_t fid_pos;
int has_fids;
assert(buffer);
assert(pfid_pos);
assert(phas_fids);
has_fids = 0;
fid_pos = *pfid_pos;
while (!has_fids) {
while (fid_pos <= max_fid_pos) {
fid = (struct fileid_desc *) (buffer + fid_pos);
if (udf_rw16(fid->tag.id) == TAGID_FID)
break;
/* fid's can only exist 4 bytes aligned */
fid_pos += 4;
}
if (fid_pos > max_fid_pos) {
/* shouldn't happen ! to prevent chaos, do nothing */
/* XXX ought to give a warning? XXX */
has_fids = 0;
break;
} else {
/* check if we found a valid FID */
fid = (struct fileid_desc *) (buffer + fid_pos);
has_fids = (udf_check_tag((union dscrptr *) fid) == 0);
if (has_fids) {
assert(udf_rw16(fid->tag.id) == TAGID_FID);
break;
}
}
}
*pfid_pos = fid_pos;
*phas_fids = has_fids;
}
/* read one fid and process it into a dirent and advance to the next */
/* (*fid) has to be allocated a logical block in size, (*dirent) struct dirent length */
int udf_read_fid_stream(struct udf_node *dir_node, uint64_t *offset, struct fileid_desc *fid, struct dirent *dirent) {
struct uio dir_uio;
struct iovec dir_iovec;
char *fid_name;
uint32_t entry_length, lb_size;
int enough, error;
assert(fid);
assert(dirent);
assert(dir_node);
assert(offset);
assert(*offset != 1);
lb_size = dir_node->udf_log_vol->lb_size;
entry_length = 0;
bzero(dirent, sizeof(struct dirent));
bzero(fid, lb_size);
if (*offset >= (uint64_t) dir_node->stat.st_size)
return EINVAL;
bzero(&dir_uio, sizeof(struct uio));
dir_uio.uio_rw = UIO_WRITE; /* write into this space */
dir_uio.uio_iovcnt = 1;
dir_uio.uio_iov = &dir_iovec;
dir_iovec.iov_base = fid;
dir_iovec.iov_len = lb_size;
dir_uio.uio_offset = *offset;
dir_uio.uio_resid = MIN(dir_node->stat.st_size - (*offset), lb_size);
error = udf_read_file_part_uio(dir_node, "file id" /* udf_node->dirent.d_name */, UDF_C_FIDS, &dir_uio);
if (error)
return error;
/*
* Check if we got a whole descriptor.
* XXX Try to `resync' directory stream when something is very wrong.
*
*/
enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE);
if (!enough) {
/* short dir ... */
return EIO;
}
error = udf_check_tag((union dscrptr *) fid);
if (!error) {
entry_length = udf_calc_tag_malloc_size((union dscrptr *) fid, lb_size);
enough = (dir_uio.uio_offset - (*offset) >= entry_length);
}
if (!enough) {
/* short dir ... */
return EIO;
}
if (!error) error = udf_check_tag_payload((union dscrptr *) fid);
if (error) {
printf("BROKEN DIRECTORY ENTRY\n");
#if 0
// udf_dump_desc(&fid->tag);
// udf_dump_fileid(fid);
#endif
/* RESYNC? */
/* TODO: use udf_resync_fid_stream */
return EIO;
}
/* we got a whole and valid descriptor */
/* create resulting dirent structure */
fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
dirent->d_fileno = udf_rw32(fid->icb.impl.im_used.unique_id); /* only 32 bits salvageable */
#if !defined(__DragonFly__)
dirent->d_reclen = sizeof(struct dirent);
#endif
dirent->d_type = DT_UNKNOWN;
udf_to_unix_name(dirent->d_name, fid_name, fid->l_fi, &dir_node->udf_log_vol->log_vol->desc_charset);
#ifndef NO_DIRENT_NAMLEN
dirent->d_namlen = strlen(dirent->d_name);
#endif
if (fid->file_char & UDF_FILE_CHAR_DIR) dirent->d_type = DT_DIR;
if (fid->file_char & UDF_FILE_CHAR_PAR) strcpy(dirent->d_name, "..");
/* advance */
*offset += entry_length;
return error;
}
/* VOP_READDIR */
/* read in dirent's until the result_uio can't hold another */
int udf_readdir(struct udf_node *dir_node, struct uio *result_uio, int *eof_res /* int *cookies, int ncookies */) {
struct fileid_desc *fid;
struct dirent dirent;
uint64_t diroffset, transoffset;
uint32_t lb_size;
int eof;
int error;
assert(eof_res);
if (!dir_node)
return EINVAL;
if (!dir_node->udf_log_vol)
return EINVAL;
assert(result_uio->uio_resid >= sizeof(struct dirent));
lb_size = dir_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
if (!fid) return ENOMEM;
/* check if we ought to insert dummy `.' node */
if (result_uio->uio_offset == 0) {
bzero(&dirent, sizeof(struct dirent));
strcpy(dirent.d_name, ".");
dirent.d_type = DT_DIR;
#ifndef NO_DIRENT_NAMLEN
dirent.d_namlen = 2;
#endif
uiomove(&dirent, sizeof(struct dirent), result_uio);
/* mark with magic value (yeah it suxxs) that we have done the dummy */
result_uio->uio_offset = 1;
}
/* start directory reading */
diroffset = result_uio->uio_offset;
transoffset = diroffset;
while (diroffset < (uint64_t) dir_node->stat.st_size) {
/* read just the offset when its flagged */
if (diroffset == 1) {
diroffset = result_uio->uio_offset = 0;
}
/* read in FIDs */
error = udf_read_fid_stream(dir_node, &diroffset, fid, &dirent);
if (error) {
printf("Error while reading directory file: %s\n", strerror(error));
free(fid);
return error;
}
/* if there is not enough space for the dirent break off read */
if (result_uio->uio_resid < sizeof(struct dirent))
break;
/* remember the last entry we transfered */
transoffset = diroffset;
/* skip deleted entries */
if (fid->file_char & UDF_FILE_CHAR_DEL)
continue;
/* skip not visible entries */
if (fid->file_char & UDF_FILE_CHAR_VIS)
continue;
uiomove(&dirent, sizeof(struct dirent), result_uio);
}
/* pass on last transfered offset */
result_uio->uio_offset = transoffset;
free(fid);
eof = (result_uio->uio_offset >= (int64_t) dir_node->stat.st_size);
if (eof_res) *eof_res = 1;
*eof_res = eof;
return 0;
}
static int
dirhash_fill(struct udf_node *dir_node)
{
struct dirhash *dirh;
struct fileid_desc *fid;
struct dirent *dirent;
uint64_t file_size, pre_diroffset, diroffset;
uint32_t lb_size;
int error;
/* make sure we have a dirhash to work on */
dirh = dir_node->dir_hash;
assert(dirh);
assert(dirh->refcnt > 0);
if (dirh->flags & DIRH_BROKEN)
return EIO;
if (dirh->flags & DIRH_COMPLETE)
return 0;
/* make sure we have a clean dirhash to add to */
dirhash_purge_entries(dirh);
/* get directory filesize */
file_size = dir_node->stat.st_size;
/* allocate temporary space for fid */
lb_size = dir_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
assert(fid);
/* allocate temporary space for dirent */
dirent = malloc(sizeof(struct dirent));
assert(dirent);
error = 0;
diroffset = 0;
while (diroffset < file_size) {
/* transfer a new fid/dirent */
pre_diroffset = diroffset;
error = udf_read_fid_stream(dir_node, &diroffset, fid, dirent);
if (error) {
/* TODO what to do? continue but not add? */
dirh->flags |= DIRH_BROKEN;
dirhash_purge_entries(dirh);
break;
}
if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
/* register deleted extent for reuse */
dirhash_enter_freed(dirh, pre_diroffset,
udf_fidsize(fid));
} else {
/* append to the dirhash */
dirhash_enter(dirh, dirent, pre_diroffset,
udf_fidsize(fid), 0);
/* XXX speedup HACK: preread in our nodes to compensate for too lazy backend */
{
struct udf_node *res_node;
error = udf_readin_udf_node(dir_node, &fid->icb, fid, &res_node);
}
}
}
dirh->flags |= DIRH_COMPLETE;
free(fid);
free(dirent);
return error;
}
/* XXX yes, move namelen to unsigned int */
int udf_lookup_name_in_dir(struct udf_node *dir_node, char *name, int namelen, struct long_ad *icb_loc, struct fileid_desc *fid, int *found) {
struct dirhash *dirh;
struct dirhash_entry *dirh_ep;
struct dirent *dirent;
uint64_t diroffset;
int hit, error;
/* set default return */
*found = 0;
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* allocate temporary space for dirent */
dirent = malloc(sizeof(struct dirent));
if (!dirent)
return ENOMEM;
DEBUG(printf("dirhash_lookup looking for `%*.*s`\n",
namelen, namelen, name));
/* search our dirhash hits */
memset(icb_loc, 0, sizeof(*icb_loc));
dirh_ep = NULL;
for (;;) {
hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
/* if no hit, abort the search */
if (!hit)
break;
/* check this hit */
diroffset = dirh_ep->offset;
/* transfer a new fid/dirent */
error = udf_read_fid_stream(dir_node, &diroffset, fid, dirent);
if (error)
break;
DEBUG(printf("dirhash_lookup\tchecking `%*.*s`\n",
(int) DIRENT_NAMLEN(dirent), (int) DIRENT_NAMLEN(dirent), dirent->d_name));
/* see if its our entry */
assert(DIRENT_NAMLEN(dirent) == (unsigned int) namelen);
if (strncmp(dirent->d_name, name, namelen) == 0) {
*found = 1;
*icb_loc = fid->icb;
break;
}
}
free(dirent);
dirhash_put(dir_node->dir_hash);
return error;
}
static int udf_count_direntries(struct udf_node *dir_node, int count_dotdot, uint32_t *dir_entries) {
struct fileid_desc *fid;
struct dirent dirent;
uint64_t pos;
uint32_t lb_size;
int eof;
int error;
if (!dir_node) return EINVAL;
lb_size = dir_node->udf_log_vol->lb_size;
/* count all directory entries with optional the dotdot too */
/* only defined in directories XXX DT_COMP also possible XXX */
if ((dir_node->stat.st_mode & S_IFDIR) == 0)
return ENOTDIR;
/* get space to read fid in */
fid = malloc(lb_size);
if (!fid) return ENOMEM;
/* start directory reading */
*dir_entries = 0;
pos = 0;
eof = (pos == (uint64_t) dir_node->stat.st_size);
while (!eof) {
/* read in FIDs */
error = udf_read_fid_stream(dir_node, &pos, fid, &dirent);
if (error) {
printf("Error while counting directory entries : %s\n", strerror(error));
free(fid);
return error;
}
/* process this FID/dirent */
if ((fid->file_char & UDF_FILE_CHAR_DEL) == 0) {
if (fid->file_char & UDF_FILE_CHAR_PAR) {
if (count_dotdot) *dir_entries = *dir_entries + 1;
} else {
*dir_entries = *dir_entries + 1;
}
}
/* pos is automatically advanced */
eof = (pos == (uint64_t) dir_node->stat.st_size);
}
/* end of directory */
free(fid);
return 0;
}
static int udf_writeout_fid_info(struct udf_node *dir_node, struct fileid_desc *fid, uint64_t offset, uint16_t fid_len) {
struct uio uio;
struct iovec iovec;
int flags;
bzero(&uio, sizeof(struct uio));
uio.uio_rw = UIO_READ; /* read from this space */
uio.uio_iovcnt = 1;
uio.uio_iov = &iovec;
iovec.iov_base = fid;
iovec.iov_len = fid_len;
uio.uio_offset = offset;
uio.uio_resid = fid_len;
flags = UDF_C_FIDS;
return udf_write_file_part_uio(dir_node, "file id.", flags, &uio);
}
/* search for a space to record the fid in, not checking if it is allready in it ! */
/* ALERT: not to be used to update a fid ... use writeout_fid_info for that */
/* ONLY used by udf_create_directory_entry */
static int udf_insert_fid_info(struct udf_node *dir_node, struct udf_node *udf_node, struct fileid_desc *i_fid, uint16_t fidsize) {
struct dirhash *dirh;
struct dirhash_entry *dirh_ep;
struct fileid_desc *fid;
struct dirent dirent;
uint64_t dir_size, fid_pos, chosen_fid_pos, end_fid_pos;
uint32_t this_fidsize, chosen_size;
uint32_t lb_size, lb_rest;
uint32_t size_diff, chosen_size_diff;
char *fid_name;
int descr_ver, hit, error;
udf_node = udf_node; /* passed only for printing diagnostic info if required */
if (!dir_node)
return EINVAL;
/* only defined in directories XXX DT_COMP also possible XXX */
if ((dir_node->stat.st_mode & S_IFDIR) == 0)
return ENOTDIR;
/* needs to be 4 bytes aligned to be legal! if not, something is seriously wrong so abort */
assert((fidsize & 3) == 0);
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* get info */
lb_size = dir_node->udf_log_vol->lb_size;
dir_size = dir_node->stat.st_size;
descr_ver = udf_rw16(dir_node->udf_log_vol->log_vol->tag.descriptor_ver);
/* get space to read fid in */
fid = malloc(lb_size);
if (!fid)
return ENOMEM;
/* find position that will fit the FID */
chosen_fid_pos = dir_size;
chosen_size = 0;
chosen_size_diff = UINT_MAX;
/* shut up gcc */
#ifndef NO_DIRENT_NAMLEN
dirent.d_namlen = 0;
#endif
/* search our dirhash hits */
error = 0;
dirh_ep = NULL;
for (;;) {
hit = dirhash_lookup_freed(dirh, fidsize, &dirh_ep);
/* if no hit, abort the search */
if (!hit)
break;
/* check this hit for size */
this_fidsize = dirh_ep->entry_size;
/* check this hit */
fid_pos = dirh_ep->offset;
end_fid_pos = fid_pos + this_fidsize;
size_diff = this_fidsize - fidsize;
lb_rest = lb_size - (end_fid_pos % lb_size);
/* select if not splitting the tag and its smaller */
if ((size_diff <= chosen_size_diff) &&
(lb_rest >= sizeof(struct desc_tag)))
{
/* UDF 2.3.4.2+3 specifies rules for iu size */
if ((size_diff == 0) || (size_diff >= 32)) {
chosen_fid_pos = fid_pos;
chosen_size = this_fidsize;
chosen_size_diff = size_diff;
}
}
}
/* extend directory if no other candidate found */
if (chosen_size == 0) {
chosen_fid_pos = dir_size;
chosen_size = fidsize;
/* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
if (dir_node->addr_type == UDF_ICB_INTERN_ALLOC) {
/* pre-grow directory to see if we're to switch */
// udf_grow_node(dir_node, dir_size + chosen_size);
error = udf_truncate_node(dir_node, chosen_fid_pos + chosen_size);
assert(!error);
}
/* make sure the next fid desc_tag won't be splitted */
if (dir_node->addr_type != UDF_ICB_INTERN_ALLOC) {
end_fid_pos = chosen_fid_pos + chosen_size;
lb_rest = lb_size - (end_fid_pos % lb_size);
/* pad with implementation use regid if needed */
if (lb_rest < sizeof(struct desc_tag))
chosen_size += 32;
}
}
chosen_size_diff = chosen_size - fidsize;
/* populate the FID */
memset(fid, 0, lb_size);
udf_init_desc_tag(&fid->tag, TAGID_FID, descr_ver, 1); /* tag serial number */
fid->file_version_num = i_fid->file_version_num;
fid->file_char = i_fid->file_char;
fid->icb = i_fid->icb;
fid->l_iu = udf_rw16(0);
if (chosen_size > fidsize) {
/* insert implementation-use regid to space it correctly */
fid->l_iu = udf_rw16(chosen_size_diff);
/* set implementation use */
udf_set_imp_id((struct regid *) fid->data);
}
/* copy name */
fid->l_fi = i_fid->l_fi;
memcpy(fid->data + udf_rw16(fid->l_iu), i_fid->data, fid->l_fi);
fid->tag.desc_crc_len = chosen_size - UDF_DESC_TAG_LENGTH;
/* writeout modified piece */
udf_validate_tag_and_crc_sums((union dscrptr *) fid);
error = udf_writeout_fid_info(dir_node, fid, chosen_fid_pos, chosen_size);
assert(!error);
/* append to the dirhash */
fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
dirent.d_fileno = udf_rw32(fid->icb.impl.im_used.unique_id); /* only 32 bits salvageable */
#if !defined(__DragonFly__)
dirent.d_reclen = sizeof(struct dirent);
#endif
dirent.d_type = DT_UNKNOWN;
udf_to_unix_name(dirent.d_name, fid_name, fid->l_fi, &dir_node->udf_log_vol->log_vol->desc_charset);
#ifndef NO_DIRENT_NAMLEN
dirent.d_namlen = strlen(dirent.d_name);
#endif
if (fid->file_char & UDF_FILE_CHAR_DIR) dirent.d_type = DT_DIR;
if (fid->file_char & UDF_FILE_CHAR_PAR) strcpy(dirent.d_name, "..");
dirhash_enter(dirh, &dirent, chosen_fid_pos, udf_fidsize(fid), 1);
free(fid);
dirhash_put(dir_node->dir_hash);
return error;
}
/* create a file in the given directory with the given name and attributes using udf's file_char and udf'd filetype */
/* note
* 1) that with `refering' node specified its effectively `link()'
* 2) that with `refering' node specified, `filetype' is discarded as it ought to be the same as the `refering' one
*
* XXX this function needs to be splitted into node creation and directory
* attachment; its now doing both in one go.
*/
int udf_create_directory_entry(struct udf_node *dir_node, char *name, int filetype, int filechar, struct udf_node *refering, struct stat *stat, struct udf_node **new_node) {
struct udf_allocentry *alloc_entry;
struct udf_log_vol *udf_log_vol;
struct udf_node *udf_node;
struct charspec osta_charspec;
struct fileid_desc *fid;
struct long_ad icb_loc;
uint32_t lb_num, lb_size;
uint16_t vpart_num, descr_ver, len;
int found, error;
assert(dir_node);
assert(name);
assert(dir_node->udf_log_vol);
udf_log_vol = dir_node->udf_log_vol;
lb_size = udf_log_vol->lb_size;
descr_ver = udf_rw16(udf_log_vol->log_vol->tag.descriptor_ver);
*new_node = NULL;
/* lookup if it allready exists (sanity mainly) */
fid = malloc(lb_size);
assert(fid);
error = udf_lookup_name_in_dir(dir_node, name, strlen(name), &icb_loc, fid, &found);
if (!error && found) {
/* it existed! allready there */
free(fid);
return EEXIST;
}
if (!refering) {
/*
* Get ourselves an empty node and space to record file
* descriptor in.
*/
error = udf_init_udf_node(dir_node->mountpoint, udf_log_vol, "New direntry", &udf_node);
if (error) {
free(fid);
return error;
}
udf_node->udf_filetype = filetype;
udf_node->udf_filechar = filechar;
udf_node->unique_id = udf_increment_unique_id(udf_log_vol);
/* snif */
error = udf_allocate_udf_node_on_disc(udf_node);
if (error) {
assert(udf_node != dir_node);
udf_dispose_udf_node(udf_node);
free(fid);
return error;
}
udf_node->stat = *stat;
/* note passed creation times; do sanitise them */
#ifndef NO_STAT_BIRTHTIME
if (udf_insanetimespec(&stat->st_birthtimespec))
udf_set_timespec_now(&udf_node->stat.st_birthtimespec);
#endif
if (udf_insanetimespec(&stat->st_ctimespec))
udf_set_timespec_now(&udf_node->stat.st_ctimespec);
if (udf_insanetimespec(&stat->st_atimespec))
udf_set_timespec_now(&udf_node->stat.st_atimespec);
if (udf_insanetimespec(&stat->st_mtimespec))
udf_set_timespec_now(&udf_node->stat.st_mtimespec);
} else {
/* refering->ignore passed stat info */
udf_node = refering;
filetype = udf_node->udf_filetype;
/* linking changes metadata modification */
udf_set_timespec_now(&udf_node->stat.st_ctimespec);
}
alloc_entry = TAILQ_FIRST(&udf_node->dscr_allocs);
vpart_num = alloc_entry->vpart_num;
lb_num = alloc_entry->lb_num;
/* build up new directory entry */
memset(fid, 0, lb_size);
udf_osta_charset(&osta_charspec);
udf_init_desc_tag(&fid->tag, TAGID_FID, descr_ver, 1); /* tag serial number */
if (filechar & UDF_FILE_CHAR_PAR) {
/* parent or `..' is not allowed to have a name length ... wierd but ok */
fid->l_fi = 0;
} else {
unix_to_udf_name((char *) fid->data, name, &fid->l_fi, &osta_charspec);
}
fid->file_version_num = udf_rw16(1); /* new file/dir; version starts at 1 */
fid->file_char = filechar; /* what is it */
fid->l_iu = udf_rw32(0); /* no impl. use */
fid->icb.len = udf_rw32(lb_size); /* fill in location */
fid->icb.loc.part_num = udf_rw16(vpart_num);
fid->icb.loc.lb_num = udf_rw32(lb_num);
/* fill in lower 32 bits of unique ID (UDF 3/3.2.2.1) in the impl use part of the FID's long_ad */
fid->icb.impl.im_used.unique_id = udf_rw32(((udf_node->unique_id << 32) >> 32));
/* calculate minimum size needed for directory entry */
len = UDF_FID_SIZE + fid->l_fi;
len = (len + 3) & ~3;
fid->tag.desc_crc_len = udf_rw16(len - UDF_DESC_TAG_LENGTH);
error = udf_insert_fid_info(dir_node, udf_node, fid, len);
free(fid); /* Ahum... easily forgotten here */
if (error) {
fprintf(stderr, "UDF: fid insertion failed : %s\n", strerror(error));
if (!refering)
udf_dispose_udf_node(udf_node);
return error;
}
if (udf_node) {
/* only insert file in hashlist if its not an explicit reference */
if (!refering) {
udf_insert_node_in_hash(udf_node);
} else {
refering->link_cnt++;
udf_node_mark_dirty(refering);
}
udf_node_mark_dirty(udf_node);
}
*new_node = udf_node;
return error;
}
/*
* Rename file from old_name to new_name. `present' is the file to be replaced
* if found present allready. Care should be taken that the directory tree is
* kept intact. To prevent this no path should be possible from the new parent
* to the node to be renamed if it considers a directory and the new_parent is
* not equal to the old parent.
*/
/*
* VOP_RENAME(struct vnode *fdvp, struct vnode *vp,
* struct componentname *fcnp, struct componentname *tdvp,
* struct vnode *tvp, struct componentname *tcnp
* );
*/
int udf_rename(struct udf_node *old_parent, struct udf_node *rename_me, char *old_name, struct udf_node *new_parent, struct udf_node *present, char *new_name) {
struct udf_node *new_node;
int error;
/* sanity */
if (!old_parent) return ENOENT;
if (!new_parent) return ENOENT;
if (!rename_me) return ENOENT;
if (!(old_parent->stat.st_mode & S_IFDIR)) return ENOTDIR;
if (!(new_parent->stat.st_mode & S_IFDIR)) return ENOTDIR;
if (udf_open_logvol(old_parent->udf_log_vol))
return EROFS;
if (udf_open_logvol(new_parent->udf_log_vol))
return EROFS;
if ((present && (present->stat.st_mode & S_IFDIR)) || (old_parent != new_parent)) {
/* cross directory moves */
fprintf(stderr, "Cross directory renaming is not implemented yet.\n");
return ENOTSUP;
}
/* if it was present, delete old contents; reference counting is done */
if (present) {
/* TODO what about non dir, non file entries? */
if (present->stat.st_mode & S_IFDIR) {
error = udf_remove_directory(new_parent, present, new_name);
} else {
error = udf_remove_file(new_parent, present, new_name);
}
if (error)
return error;
}
/* insert new_name HARD-linked to the `rename_me' node */
error = udf_create_directory_entry(new_parent, new_name, rename_me->udf_filetype, rename_me->udf_filechar, rename_me, NULL, &new_node);
if (error) return error;
/* extra sanity */
if (!new_node) return ENOENT;
/* 3) remove old link and mark directories dirty */
error = udf_remove_directory_entry(old_parent, rename_me, old_name);
udf_node_mark_dirty(old_parent);
udf_node_mark_dirty(new_parent);
return error;
}
/* VOP_CREATE */
int udf_create_file(struct udf_node *dir_node, char *componentname, struct stat *stat, struct udf_node **new_node) {
struct udf_log_vol *udf_log_vol;
struct udf_node *udf_node;
uint32_t lb_size;
int error;
if (!dir_node) return EINVAL;
udf_log_vol = dir_node->udf_log_vol;
if (!udf_log_vol) return EINVAL;
lb_size = udf_log_vol->lb_size;
if (!udf_confirm_freespace(udf_log_vol, UDF_C_NODE, lb_size))
return ENOSPC;
if (udf_open_logvol(dir_node->udf_log_vol))
return EROFS;
error = udf_create_directory_entry(dir_node, componentname, UDF_ICB_FILETYPE_RANDOMACCESS, 0, NULL, stat, new_node);
if ((!error) && (*new_node)) {
udf_node = *new_node;
/* update sizes */
udf_node->stat.st_size = 0;
udf_node->stat.st_blksize = dir_node->udf_log_vol->lb_size;
udf_node->stat.st_blocks = 0; /* not 1? */
udf_node->udf_log_vol->num_files++;
udf_node_mark_dirty(udf_node);
}
return error;
}
/* VOP_MKDIR */
int udf_create_directory(struct udf_node *dir_node, char *componentname, struct stat *stat, struct udf_node **new_node) {
struct udf_log_vol *udf_log_vol;
struct udf_node *udf_node, *dummy_node;
uint32_t lb_size;
int error;
if (!dir_node) return EINVAL;
udf_log_vol = dir_node->udf_log_vol;
if (!udf_log_vol) return EINVAL;
lb_size = udf_log_vol->lb_size;
if (!udf_confirm_freespace(udf_log_vol, UDF_C_NODE, 2*lb_size))
return ENOSPC;
if (udf_open_logvol(dir_node->udf_log_vol))
return EROFS;
stat->st_mode |= S_IFDIR;
error = udf_create_directory_entry(dir_node, componentname, UDF_ICB_FILETYPE_DIRECTORY, UDF_FILE_CHAR_DIR, NULL, stat, new_node);
if ((!error) && (*new_node)) {
udf_node = *new_node;
/* update sizes */
udf_node->stat.st_size = 0;
udf_node->stat.st_blksize = dir_node->udf_log_vol->lb_size;
udf_node->stat.st_blocks = 0; /* not 1? */
udf_node->udf_log_vol->num_directories++;
udf_node_mark_dirty(udf_node);
/* create `..' directory entry */
error = udf_create_directory_entry(udf_node, "..", UDF_ICB_FILETYPE_DIRECTORY, UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR, dir_node, stat, &dummy_node);
if (error) {
/* use of _prim for dir counting might not go well due to aborted creation */
error = udf_remove_directory_prim(dir_node, udf_node, componentname);
}
}
return error;
}
/* really deletes all space referenced to this udf node including descriptor spaces and removes it from the administration */
int udf_unlink_node(struct udf_node *udf_node) {
struct udf_allocentry *alloc_entry;
uint32_t lbnum, len;
uint16_t vpart;
int error;
/* just in case its called from outside */
if (udf_open_logvol(udf_node->udf_log_vol))
return EROFS;
/* unlinking changes metadata modification */
udf_set_timespec_now(&udf_node->stat.st_ctimespec);
udf_node->link_cnt--;
udf_node_mark_dirty(udf_node);
if (udf_node->link_cnt > 0)
return 0;
/* trunc node */
udf_truncate_node(udf_node, (uint64_t) 0); /* get rid of file contents */
/* free descriptors from dscr_allocs queue */
TAILQ_FOREACH(alloc_entry, &udf_node->dscr_allocs, next_alloc) {
vpart = alloc_entry->vpart_num;
lbnum = alloc_entry->lb_num;
/* flags = alloc_entry->flags; */
len = alloc_entry->len;
error = udf_release_lbs(udf_node->udf_log_vol, vpart, lbnum, len);
/* what if an error occures? */
assert(error == 0);
}
/* delete from administration */
udf_dispose_udf_node(udf_node);
return 0;
}
/* NOTE: Dont use the EXTENT erased part; its for non sequential WORM only */
/* UDF 2.3.10.1, ECMA 4/48.14.1.1 */
/* fid->icb.impl.im_used.flags = udf_rw16(UDF_ADIMP_FLAGS_EXTENT_ERASED); */
static int udf_remove_directory_entry(struct udf_node *dir_node, struct udf_node *udf_node, char *name) {
struct dirhash *dirh;
struct dirhash_entry *dirh_ep;
struct fileid_desc *fid;
struct dirent *dirent;
uint64_t diroffset;
uint32_t lb_size, namelen, fidsize;
int hit, found;
int error;
assert(dir_node);
assert(udf_node);
assert(udf_node->udf_log_vol);
assert(name);
namelen = strlen(name);
if (strncmp(name, "..", 3) == 0) {
printf("Asked to remove `..' parent directory identifier; not allowed!\n");
return ENOENT;
}
if (strncmp(name, ".", 2) == 0) {
printf("Asked to remove `.' current directory identifier; not allowed!\n");
return ENOENT;
}
/* only lookup in directories XXX DT_COMP also possible XXX */
if ((dir_node->stat.st_mode & S_IFDIR) == 0)
return ENOTDIR;
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* allocate temporary space for fid */
lb_size = udf_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
dirent = malloc(sizeof(struct dirent));
if (!fid || !dirent) {
error = ENOMEM;
goto error_out;
}
/* search our dirhash hits */
found = 0;
dirh_ep = NULL;
for (;;) {
hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
/* if no hit, abort the search */
if (!hit)
break;
/* check this hit */
diroffset = dirh_ep->offset;
/* transfer a new fid/dirent */
error = udf_read_fid_stream(dir_node, &diroffset, fid, dirent);
if (error)
break;
/* see if its our entry */
assert(DIRENT_NAMLEN(dirent) == namelen);
if (strncmp(dirent->d_name, name, namelen) == 0) {
found = 1;
break;
}
}
if (!found)
error = ENOENT;
if (error)
goto error_out;
/* get size of fid and compensate for the read_fid_stream advance */
fidsize = udf_fidsize(fid);
diroffset -= fidsize;
/* mark fid as deleted */
fid->file_char |= UDF_FILE_CHAR_DEL;
bzero(&fid->icb, sizeof(struct long_ad));
udf_validate_tag_and_crc_sums((union dscrptr *) fid);
udf_writeout_fid_info(dir_node, fid, diroffset, fidsize);
/* remove from the dirhash */
dirhash_mark_freed(dirh, dirh_ep, dirent);
/* delete node and its administration if refcount indicates so */
udf_unlink_node(udf_node);
error_out:
if (fid)
free(fid);
if (dirent)
free(dirent);
dirhash_put(dir_node->dir_hash);
return error;
}
/* VOP_REMOVE */
int udf_remove_file(struct udf_node *dir_node, struct udf_node *udf_node, char *componentname) {
int error;
if (udf_open_logvol(dir_node->udf_log_vol))
return EROFS;
if (udf_node->stat.st_mode & S_IFDIR) {
/* only remove files with this call */
return EISDIR;
}
error = udf_remove_directory_entry(dir_node, udf_node, componentname);
if (!error) {
dir_node->udf_log_vol->num_files--;
} /* else? */
return error;
}
static int udf_remove_directory_prim(struct udf_node *dir_node, struct udf_node *udf_node, char *componentname) {
int error;
if (udf_open_logvol(dir_node->udf_log_vol))
return EROFS;
/* remove the entry */
error = udf_remove_directory_entry(dir_node, udf_node, componentname);
if (!error) {
dir_node->link_cnt--;
udf_node_mark_dirty(dir_node);
dir_node->udf_log_vol->num_directories--;
} else {
/* whoah! something went wrong, mark the .. as present again */
printf("UDF warning: filesystem might by in compromised state\n");
assert(udf_node);
udf_node->link_cnt++;
}
return error;
}
/* VOP_RMDIR */
int udf_remove_directory(struct udf_node *dir_node, struct udf_node *udf_node, char *componentname) {
uint32_t num_nodes;
int error;
if (!(udf_node->stat.st_mode & S_IFDIR)) {
/* only remove directories with this call */
return ENOTDIR;
}
error = udf_count_direntries(udf_node, 0, &num_nodes);
if (error) return error;
if (num_nodes != 0) return ENOTEMPTY;
error = udf_remove_directory_prim(dir_node, udf_node, componentname);
return error;
}
/* end of udf.c */
|