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
|
/*
* linux/drivers/block/loop.c
*
* Written by Theodore Ts'o, 3/29/93
*
* Copyright 1993 by Theodore Ts'o. Redistribution of this file is
* permitted under the GNU General Public License.
*
* DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
* more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
*
* Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
* Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
*
* Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
*
* Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
*
* Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
*
* Loadable modules and other fixes by AK, 1998
*
* Make real block number available to downstream transfer functions, enables
* CBC (and relatives) mode encryption requiring unique IVs per data block.
* Reed H. Petty, rhp@draper.net
*
* Maximum number of loop devices now dynamic via max_loop module parameter.
* Russell Kroll <rkroll@exploits.org> 19990701
*
* Maximum number of loop devices when compiled-in now selectable by passing
* max_loop=<1-255> to the kernel on boot.
* Erik I. Bols, <eriki@himolde.no>, Oct 31, 1999
*
* Completely rewrite request handling to be make_request_fn style and
* non blocking, pushing work to a helper thread. Lots of fixes from
* Al Viro too.
* Jens Axboe <axboe@suse.de>, Nov 2000
*
* Support up to 256 loop devices
* Heinz Mauelshagen <mge@sistina.com>, Feb 2002
*
* IV is now passed as (512 byte) sector number.
* Jari Ruusu, May 18 2001
*
* External encryption module locking bug fixed.
* Ingo Rohloff <rohloff@in.tum.de>, June 21 2001
*
* Make device backed loop work with swap (pre-allocated buffers + queue rewrite).
* Jari Ruusu, September 2 2001
*
* Ported 'pre-allocated buffers + queue rewrite' to BIO for 2.5 kernels
* Ben Slusky <sluskyb@stwing.org>, March 1 2002
* Jari Ruusu, March 27 2002
*
* File backed code now uses file->f_op->read/write. Based on Andrew Morton's idea.
* Jari Ruusu, May 23 2002
*
* Exported hard sector size correctly, fixed file-backed-loop-on-tmpfs bug,
* plus many more enhancements and optimizations.
* Adam J. Richter <adam@yggdrasil.com>, Aug 2002
*
* Added support for removing offset from IV computations.
* Jari Ruusu, September 21 2003
*
*
* Still To Fix:
* - Advisory locking is ignored here.
* - Should use an own CAP_* category instead of CAP_SYS_ADMIN
*/
#include <linux/version.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/bio.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/major.h>
#include <linux/wait.h>
#include <linux/blkdev.h>
#include <linux/blkpg.h>
#include <linux/init.h>
#ifdef CONFIG_DEVFS_FS
# include <linux/devfs_fs_kernel.h>
#endif
#include <linux/smp_lock.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/loop.h>
#include <linux/suspend.h>
#include <linux/writeback.h>
#include <linux/buffer_head.h> /* for invalidate_bdev() */
#include <linux/completion.h>
#if defined(CONFIG_COMPAT) && defined(HAVE_COMPAT_IOCTL)
# include <linux/compat.h>
#endif
#if LINUX_VERSION_CODE >= 0x20606
# include <linux/mqueue.h>
#endif
#include <asm/uaccess.h>
#if !defined(LO_FLAGS_DO_BMAP)
# define LO_FLAGS_DO_BMAP 0x20000
#endif
#if !defined(LO_FLAGS_READ_ONLY)
# define LO_FLAGS_READ_ONLY 0x40000
#endif
#ifndef LOOP_CHANGE_FD
# define LOOP_CHANGE_FD 0x4C06
#endif
#if !defined(LOOP_MULTI_KEY_SETUP)
# define LOOP_MULTI_KEY_SETUP 0x4C4D
#endif
#if !defined(LOOP_MULTI_KEY_SETUP_V3)
# define LOOP_MULTI_KEY_SETUP_V3 0x4C4E
#endif
#if !defined(LOOP_RECOMPUTE_DEV_SIZE)
# define LOOP_RECOMPUTE_DEV_SIZE 0x4C52
#endif
#if defined(CONFIG_COMPAT) && !defined(HAVE_COMPAT_IOCTL)
# include <linux/ioctl32.h>
# define IOCTL32_COMPATIBLE_PTR ((void*)0)
#endif
#if (LINUX_VERSION_CODE < 0x20609) || defined(QUEUE_FLAG_ORDERED)
# define QUEUE_ORDERED_NONE 0
#endif
static int max_loop = 8;
module_param(max_loop, int, 0);
MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-256)");
static struct gendisk **disks;
/*
* Transfer functions
*/
static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
char *loop_buf, int size, sector_t real_block)
{
/* this code is only called from file backed loop */
/* and that code expects this function to be no-op */
cond_resched();
return 0;
}
static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf,
char *loop_buf, int size, sector_t real_block)
{
char *in, *out, *key;
int i, keysize;
if (cmd == READ) {
in = raw_buf;
out = loop_buf;
} else {
in = loop_buf;
out = raw_buf;
}
key = lo->lo_encrypt_key;
keysize = lo->lo_encrypt_key_size;
for (i = 0; i < size; i++)
*out++ = *in++ ^ key[(i & 511) % keysize];
cond_resched();
return 0;
}
static int xor_init(struct loop_device *lo, struct loop_info64 *info)
{
if (info->lo_encrypt_key_size <= 0)
return -EINVAL;
return 0;
}
static struct loop_func_table none_funcs = {
.number = LO_CRYPT_NONE,
.transfer = (void *)transfer_none,
};
static struct loop_func_table xor_funcs = {
.number = LO_CRYPT_XOR,
.transfer = (void *)transfer_xor,
.init = (void *)xor_init,
};
/* xfer_funcs[0] is special - its release function is never called */
static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
&none_funcs,
&xor_funcs,
};
/*
* First number of 'lo_prealloc' is the default number of RAM pages
* to pre-allocate for each device backed loop. Every (configured)
* device backed loop pre-allocates this amount of RAM pages unless
* later 'lo_prealloc' numbers provide an override. 'lo_prealloc'
* overrides are defined in pairs: loop_index,number_of_pages
*/
static int lo_prealloc[9] = { 125, -1, 0, -1, 0, -1, 0, -1, 0 };
#define LO_PREALLOC_MIN 4 /* minimum user defined pre-allocated RAM pages */
#define LO_PREALLOC_MAX 512 /* maximum user defined pre-allocated RAM pages */
static int dummy1;
#if LINUX_VERSION_CODE >= 0x2060a
module_param_array(lo_prealloc, int, &dummy1, 0);
#else
module_param_array(lo_prealloc, int, dummy1, 0);
#endif
MODULE_PARM_DESC(lo_prealloc, "Number of pre-allocated pages [,index,pages]...");
/*
* This is loop helper thread nice value in range
* from 0 (low priority) to -20 (high priority).
*/
static int lo_nice = -1;
module_param(lo_nice, int, 0);
MODULE_PARM_DESC(lo_nice, "Loop thread scheduler nice (0 ... -20)");
struct loop_bio_extension {
struct bio *bioext_merge;
struct loop_device *bioext_loop;
sector_t bioext_iv;
int bioext_index;
int bioext_size;
};
typedef struct {
struct loop_device lo_orig;
atomic_t lo_pending;
struct bio *lo_bio_que0;
struct bio *lo_bio_que1;
struct bio *lo_bio_que2;
struct bio *lo_bio_free0;
struct bio *lo_bio_free1;
atomic_t lo_bio_barr;
int lo_bio_flsh;
int lo_bio_need;
wait_queue_head_t lo_bio_wait;
sector_t lo_offs_sec;
sector_t lo_iv_remove;
unsigned long lo_bio_flag;
struct completion lo_done;
spinlock_t lo_ioctl_spin;
int lo_ioctl_busy;
wait_queue_head_t lo_ioctl_wait;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
void (*lo_keyscrub_fn)(void *);
void *lo_keyscrub_ptr;
#endif
} LoDevExt;
static struct loop_device **loop_dev_ptr_arr;
#define LDE_lo_pending (((LoDevExt *)lo)->lo_pending)
#define LDE_lo_bio_que0 (((LoDevExt *)lo)->lo_bio_que0)
#define LDE_lo_bio_que1 (((LoDevExt *)lo)->lo_bio_que1)
#define LDE_lo_bio_que2 (((LoDevExt *)lo)->lo_bio_que2)
#define LDE_lo_bio_free0 (((LoDevExt *)lo)->lo_bio_free0)
#define LDE_lo_bio_free1 (((LoDevExt *)lo)->lo_bio_free1)
#define LDE_lo_bio_barr (((LoDevExt *)lo)->lo_bio_barr)
#define LDE_lo_bio_flsh (((LoDevExt *)lo)->lo_bio_flsh)
#define LDE_lo_bio_need (((LoDevExt *)lo)->lo_bio_need)
#define LDE_lo_bio_wait (((LoDevExt *)lo)->lo_bio_wait)
#define LDE_lo_offs_sec (((LoDevExt *)lo)->lo_offs_sec)
#define LDE_lo_iv_remove (((LoDevExt *)lo)->lo_iv_remove)
#define LDE_lo_bio_flag (((LoDevExt *)lo)->lo_bio_flag)
#define LDE_lo_done (((LoDevExt *)lo)->lo_done)
#define LDE_lo_ioctl_spin (((LoDevExt *)lo)->lo_ioctl_spin)
#define LDE_lo_ioctl_busy (((LoDevExt *)lo)->lo_ioctl_busy)
#define LDE_lo_ioctl_wait (((LoDevExt *)lo)->lo_ioctl_wait)
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
#define LDE_lo_keyscrub_fn (((LoDevExt *)lo)->lo_keyscrub_fn)
#define LDE_lo_keyscrub_ptr (((LoDevExt *)lo)->lo_keyscrub_ptr)
#endif
static void loop_prealloc_cleanup(struct loop_device *lo)
{
struct bio *bio;
while ((bio = LDE_lo_bio_free0)) {
LDE_lo_bio_free0 = bio->bi_next;
__free_page(bio->bi_io_vec[0].bv_page);
kfree(bio->bi_private);
bio->bi_next = NULL;
bio_put(bio);
}
while ((bio = LDE_lo_bio_free1)) {
LDE_lo_bio_free1 = bio->bi_next;
/* bi_flags was used for other purpose */
bio->bi_flags = 0;
/* bi_size was used for other purpose */
bio->bi_size = 0;
/* bi_cnt was used for other purpose */
atomic_set(&bio->bi_cnt, 1);
bio->bi_next = NULL;
bio_put(bio);
}
}
static int loop_prealloc_init(struct loop_device *lo, int y)
{
struct bio *bio;
int x;
if(!y) {
y = lo_prealloc[0];
for (x = 1; x < (sizeof(lo_prealloc) / sizeof(int)); x += 2) {
if (lo_prealloc[x + 1] && (lo->lo_number == lo_prealloc[x])) {
y = lo_prealloc[x + 1];
break;
}
}
}
LDE_lo_bio_flsh = (y * 3) / 4;
for (x = 0; x < y; x++) {
bio = bio_alloc(GFP_KERNEL, 1);
if (!bio) {
fail1:
loop_prealloc_cleanup(lo);
return 1;
}
bio->bi_io_vec[0].bv_page = alloc_page(GFP_KERNEL);
if (!bio->bi_io_vec[0].bv_page) {
fail2:
bio->bi_next = NULL;
bio_put(bio);
goto fail1;
}
memset(page_address(bio->bi_io_vec[0].bv_page), 0, PAGE_SIZE);
bio->bi_vcnt = 1;
bio->bi_private = kmalloc(sizeof(struct loop_bio_extension), GFP_KERNEL);
if (!bio->bi_private)
goto fail2;
bio->bi_next = LDE_lo_bio_free0;
LDE_lo_bio_free0 = bio;
bio = bio_alloc(GFP_KERNEL, 1);
if (!bio)
goto fail1;
bio->bi_vcnt = 1;
bio->bi_next = LDE_lo_bio_free1;
LDE_lo_bio_free1 = bio;
}
return 0;
}
static void loop_add_queue_last(struct loop_device *lo, struct bio *bio, struct bio **q)
{
unsigned long flags;
spin_lock_irqsave(&lo->lo_lock, flags);
if (*q) {
bio->bi_next = (*q)->bi_next;
(*q)->bi_next = bio;
} else {
bio->bi_next = bio;
}
*q = bio;
spin_unlock_irqrestore(&lo->lo_lock, flags);
if (waitqueue_active(&LDE_lo_bio_wait))
wake_up_interruptible(&LDE_lo_bio_wait);
}
static void loop_add_queue_first(struct loop_device *lo, struct bio *bio, struct bio **q)
{
spin_lock_irq(&lo->lo_lock);
if (*q) {
bio->bi_next = (*q)->bi_next;
(*q)->bi_next = bio;
} else {
bio->bi_next = bio;
*q = bio;
}
spin_unlock_irq(&lo->lo_lock);
}
static struct bio *loop_get_bio(struct loop_device *lo, int *list_nr)
{
struct bio *bio = NULL, *last;
spin_lock_irq(&lo->lo_lock);
if ((last = LDE_lo_bio_que0)) {
bio = last->bi_next;
if (bio == last)
LDE_lo_bio_que0 = NULL;
else
last->bi_next = bio->bi_next;
bio->bi_next = NULL;
*list_nr = 0;
} else if ((last = LDE_lo_bio_que1)) {
bio = last->bi_next;
if (bio == last)
LDE_lo_bio_que1 = NULL;
else
last->bi_next = bio->bi_next;
bio->bi_next = NULL;
*list_nr = 1;
} else if ((last = LDE_lo_bio_que2)) {
bio = last->bi_next;
if (bio == last)
LDE_lo_bio_que2 = NULL;
else
last->bi_next = bio->bi_next;
bio->bi_next = NULL;
*list_nr = 2;
}
spin_unlock_irq(&lo->lo_lock);
return bio;
}
static void loop_put_buffer(struct loop_device *lo, struct bio *b, int flist)
{
unsigned long flags;
int wk;
spin_lock_irqsave(&lo->lo_lock, flags);
if(!flist) {
b->bi_next = LDE_lo_bio_free0;
LDE_lo_bio_free0 = b;
wk = LDE_lo_bio_need & 1;
} else {
b->bi_next = LDE_lo_bio_free1;
LDE_lo_bio_free1 = b;
wk = LDE_lo_bio_need & 2;
}
spin_unlock_irqrestore(&lo->lo_lock, flags);
if (wk && waitqueue_active(&LDE_lo_bio_wait))
wake_up_interruptible(&LDE_lo_bio_wait);
}
static int loop_end_io_transfer(struct bio *bio, unsigned int bytes_done, int err)
{
struct loop_bio_extension *extension = bio->bi_private;
struct bio *merge = extension->bioext_merge;
struct loop_device *lo = extension->bioext_loop;
struct bio *origbio = merge->bi_private;
if (err) {
merge->bi_size = err; /* used as error code */
if(err == -EIO)
clear_bit(0, &merge->bi_flags);
printk(KERN_ERR "loop%d: loop_end_io_transfer err=%d bi_rw=0x%lx\n", lo->lo_number, err, bio->bi_rw);
}
if (bio->bi_size)
return 1;
if (bio_rw(bio) == WRITE) {
loop_put_buffer(lo, bio, 0);
if (!atomic_dec_and_test(&merge->bi_cnt))
return 0;
origbio->bi_next = NULL;
bio_endio(origbio, origbio->bi_size, test_bit(0, &merge->bi_flags) ? (int)merge->bi_size : -EIO);
loop_put_buffer(lo, merge, 1);
if (atomic_dec_and_test(&LDE_lo_pending))
wake_up_interruptible(&LDE_lo_bio_wait);
} else {
loop_add_queue_last(lo, bio, &LDE_lo_bio_que0);
}
return 0;
}
static struct bio *loop_get_buffer(struct loop_device *lo, struct bio *orig_bio,
int from_thread, struct bio **merge_ptr, int *isBarrBioPtr)
{
struct bio *bio = NULL, *merge = *merge_ptr;
struct loop_bio_extension *extension;
unsigned long flags;
int len;
/*
* If called from make_request and if there are unprocessed
* barrier requests, fail allocation so that request is
* inserted to end of no-merge-allocated list. This guarantees
* FIFO processing order of requests.
*/
if (!from_thread && atomic_read(&LDE_lo_bio_barr))
return NULL;
spin_lock_irqsave(&lo->lo_lock, flags);
if (!merge) {
merge = LDE_lo_bio_free1;
if (merge) {
LDE_lo_bio_free1 = merge->bi_next;
if (from_thread)
LDE_lo_bio_need = 0;
} else {
if (from_thread)
LDE_lo_bio_need = 2;
}
}
/*
* If there are unprocessed barrier requests and a merge-bio was just
* allocated, do not allocate a buffer-bio yet. This causes request
* to be moved from head of no-merge-allocated list to end of
* merge-allocated list. This guarantees FIFO processing order
* of requests.
*/
if (merge && (*merge_ptr || !atomic_read(&LDE_lo_bio_barr))) {
bio = LDE_lo_bio_free0;
if (bio) {
LDE_lo_bio_free0 = bio->bi_next;
if (from_thread)
LDE_lo_bio_need = 0;
} else {
if (from_thread)
LDE_lo_bio_need = 1;
}
}
spin_unlock_irqrestore(&lo->lo_lock, flags);
if (!(*merge_ptr) && merge) {
/*
* initialize "merge-bio" which is used as
* rendezvous point among multiple vecs
*/
*merge_ptr = merge;
merge->bi_sector = orig_bio->bi_sector + LDE_lo_offs_sec;
merge->bi_size = 0; /* used as error code */
set_bit(0, &merge->bi_flags);
merge->bi_idx = orig_bio->bi_idx;
atomic_set(&merge->bi_cnt, orig_bio->bi_vcnt - orig_bio->bi_idx);
merge->bi_private = orig_bio;
}
if (!bio)
return NULL;
/*
* initialize one page "buffer-bio"
*/
bio->bi_sector = merge->bi_sector;
bio->bi_next = NULL;
bio->bi_bdev = lo->lo_device;
bio->bi_flags = (1 << BIO_UPTODATE);
/* read-ahead bit needs to be cleared to work around kernel bug */
/* that causes I/O errors on -EWOULDBLOCK I/O elevator failures */
bio->bi_rw = orig_bio->bi_rw & ~((1 << BIO_RW_BARRIER) | (1 << BIO_RW_AHEAD));
if (bio_barrier(orig_bio)) {
if(merge->bi_idx == (orig_bio->bi_vcnt - 1)) {
#if LINUX_VERSION_CODE >= 0x20609
setBarr2:
orig_bio->bi_hw_front_size = 0;
#endif
*isBarrBioPtr = 1;
setBarr1:
bio->bi_rw |= (1 << BIO_RW_BARRIER);
} else if(merge->bi_idx == orig_bio->bi_idx) {
goto setBarr1;
}
}
#if LINUX_VERSION_CODE >= 0x20609
else if(orig_bio->bi_hw_front_size == 1536) {
goto setBarr2;
}
#endif
#if defined(BIO_RW_SYNC)
bio->bi_rw &= ~(1 << BIO_RW_SYNC);
if (bio_sync(orig_bio) && (merge->bi_idx == (orig_bio->bi_vcnt - 1)))
bio->bi_rw |= (1 << BIO_RW_SYNC);
#endif
bio->bi_vcnt = 1;
bio->bi_idx = 0;
bio->bi_phys_segments = 0;
bio->bi_hw_segments = 0;
bio->bi_size = len = orig_bio->bi_io_vec[merge->bi_idx].bv_len;
#if defined(BIOVEC_VIRT_START_SIZE) || (LINUX_VERSION_CODE >= 0x20608)
bio->bi_hw_front_size = 0;
bio->bi_hw_back_size = 0;
#endif
/* bio->bi_max_vecs not touched */
bio->bi_io_vec[0].bv_len = len;
bio->bi_io_vec[0].bv_offset = 0;
bio->bi_end_io = loop_end_io_transfer;
/* bio->bi_cnt not touched */
/* bio->bi_private not touched */
/* bio->bi_destructor not touched */
/*
* initialize "buffer-bio" extension. This extension is
* permanently glued to above "buffer-bio" via bio->bi_private
*/
extension = bio->bi_private;
extension->bioext_merge = merge;
extension->bioext_loop = lo;
extension->bioext_iv = merge->bi_sector - LDE_lo_iv_remove;
extension->bioext_index = merge->bi_idx;
extension->bioext_size = len;
/*
* prepare "merge-bio" for next vec
*/
merge->bi_sector += len >> 9;
merge->bi_idx++;
return bio;
}
static int figure_loop_size(struct loop_device *lo, struct block_device *bdev)
{
loff_t size, offs;
sector_t x;
int err = 0;
size = i_size_read(lo->lo_backing_file->f_dentry->d_inode->i_mapping->host);
offs = lo->lo_offset;
if (!(lo->lo_flags & LO_FLAGS_DO_BMAP))
offs &= ~((loff_t)511);
if ((offs > 0) && (offs < size)) {
size -= offs;
} else {
if (offs)
err = -EINVAL;
lo->lo_offset = 0;
LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
}
if ((lo->lo_sizelimit > 0) && (lo->lo_sizelimit <= size)) {
size = lo->lo_sizelimit;
} else {
if (lo->lo_sizelimit)
err = -EINVAL;
lo->lo_sizelimit = 0;
}
size >>= 9;
/*
* Unfortunately, if we want to do I/O on the device,
* the number of 512-byte sectors has to fit into a sector_t.
*/
x = (sector_t)size;
if ((loff_t)x != size) {
err = -EFBIG;
size = 0;
}
set_capacity(disks[lo->lo_number], size); /* 512 byte units */
i_size_write(bdev->bd_inode, size << 9); /* byte units */
return err;
}
static inline int lo_do_transfer(struct loop_device *lo, int cmd, char *rbuf,
char *lbuf, int size, sector_t rblock)
{
if (!lo->transfer)
return 0;
/* this ugly cast is needed to work around (possible) kmap damage in function prototype */
/* should be: return lo->transfer(lo, cmd, rbuf, lbuf, size, rblock); */
return ((int (*)(struct loop_device *, int, char *, char *, int, sector_t))lo->transfer)(lo, cmd, rbuf, lbuf, size, rblock);
}
static int loop_file_io(struct file *file, char *buf, int size, loff_t *ppos, int w)
{
mm_segment_t fs;
int x, y, z;
y = 0;
do {
z = size - y;
fs = get_fs();
set_fs(get_ds());
if (w) {
x = file->f_op->write(file, buf + y, z, ppos);
set_fs(fs);
} else {
x = file->f_op->read(file, buf + y, z, ppos);
set_fs(fs);
if (!x)
return 1;
}
if (x < 0) {
if ((x == -EAGAIN) || (x == -ENOMEM) || (x == -ERESTART) || (x == -EINTR)) {
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ / 2);
continue;
}
return 1;
}
y += x;
} while (y < size);
return 0;
}
static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
{
loff_t pos;
struct file *file = lo->lo_backing_file;
char *data, *buf;
unsigned int size, len;
sector_t IV;
struct page *pg;
pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
buf = page_address(LDE_lo_bio_free0->bi_io_vec[0].bv_page);
IV = bio->bi_sector;
if (!LDE_lo_iv_remove)
IV += LDE_lo_offs_sec;
do {
pg = bio->bi_io_vec[bio->bi_idx].bv_page;
len = bio->bi_io_vec[bio->bi_idx].bv_len;
data = kmap(pg) + bio->bi_io_vec[bio->bi_idx].bv_offset;
while (len > 0) {
if (!lo->lo_encryption) {
/* this code relies that NONE transfer is a no-op */
buf = data;
}
size = PAGE_CACHE_SIZE;
if (size > len)
size = len;
if (bio_rw(bio) == WRITE) {
if (lo_do_transfer(lo, WRITE, buf, data, size, IV)) {
printk(KERN_ERR "loop%d: write transfer error, sector %llu\n", lo->lo_number, (unsigned long long)IV);
goto kunmap_and_out;
}
if (loop_file_io(file, buf, size, &pos, 1)) {
printk(KERN_ERR "loop%d: write i/o error, sector %llu\n", lo->lo_number, (unsigned long long)IV);
goto kunmap_and_out;
}
} else {
if (loop_file_io(file, buf, size, &pos, 0)) {
printk(KERN_ERR "loop%d: read i/o error, sector %llu\n", lo->lo_number, (unsigned long long)IV);
goto kunmap_and_out;
}
if (lo_do_transfer(lo, READ, buf, data, size, IV)) {
printk(KERN_ERR "loop%d: read transfer error, sector %llu\n", lo->lo_number, (unsigned long long)IV);
goto kunmap_and_out;
}
flush_dcache_page(pg);
}
data += size;
len -= size;
IV += size >> 9;
}
kunmap(pg);
} while (++bio->bi_idx < bio->bi_vcnt);
return 0;
kunmap_and_out:
kunmap(pg);
return -EIO;
}
#if LINUX_VERSION_CODE >= 0x20609
static int loop_issue_flush(request_queue_t *q, struct gendisk *disk, sector_t *error_sector)
{
struct loop_device *lo = q->queuedata;
struct block_device *bdev;
request_queue_t *bqu;
sector_t sect;
int ret;
if(!lo)
return 0;
if(lo->lo_flags & LO_FLAGS_DO_BMAP)
return 0;
bdev = lo->lo_device;
if(!bdev)
return 0;
bqu = bdev_get_queue(bdev);
if(!bqu)
return 0;
if(!bqu->issue_flush_fn)
return -EOPNOTSUPP;
if(!lo->lo_encryption) {
/* bdev & sector remapped for NONE transfer */
sect = 0;
ret = bqu->issue_flush_fn(bqu, bdev->bd_disk, §);
if(ret && error_sector) {
if(sect >= LDE_lo_offs_sec) {
sect -= LDE_lo_offs_sec;
} else {
sect = 0;
}
*error_sector = sect;
}
return ret;
}
#if !defined(QUEUE_FLAG_ORDERED)
if(bqu->ordered != QUEUE_ORDERED_TAG)
#else
if(!(bqu->queue_flags & (1 << QUEUE_FLAG_ORDERED)))
#endif
return -EOPNOTSUPP;
/* encrypted loop is not flushed now, but next request that */
/* arrives at loop_make_request_real() gets tagged as barrier */
set_bit(0, &LDE_lo_bio_flag);
return 0;
}
#endif
static int loop_make_request_err(request_queue_t *q, struct bio *old_bio)
{
old_bio->bi_next = NULL;
bio_io_error(old_bio, old_bio->bi_size);
return 0;
}
static int loop_make_request_real(request_queue_t *q, struct bio *old_bio)
{
struct bio *new_bio, *merge;
struct loop_device *lo = q->queuedata;
struct loop_bio_extension *extension;
int rw = bio_rw(old_bio), y;
char *md;
set_current_state(TASK_RUNNING);
if (!lo)
goto out;
if ((rw == WRITE) && (lo->lo_flags & LO_FLAGS_READ_ONLY))
goto out;
atomic_inc(&LDE_lo_pending);
/*
* file backed, queue for loop_thread to handle
*/
if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
loop_add_queue_last(lo, old_bio, &LDE_lo_bio_que0);
return 0;
}
/*
* device backed, just remap bdev & sector for NONE transfer
*/
if (!lo->lo_encryption) {
old_bio->bi_sector += LDE_lo_offs_sec;
old_bio->bi_bdev = lo->lo_device;
generic_make_request(old_bio);
if (atomic_dec_and_test(&LDE_lo_pending))
wake_up_interruptible(&LDE_lo_bio_wait);
return 0;
}
/*
* device backed, start reads and writes now if buffer available
*/
merge = NULL;
#if LINUX_VERSION_CODE >= 0x20609
old_bio->bi_hw_front_size = 0;
#endif
if(test_and_clear_bit(0, &LDE_lo_bio_flag) || bio_barrier(old_bio)) {
atomic_inc(&LDE_lo_bio_barr);
#if LINUX_VERSION_CODE >= 0x20609
old_bio->bi_hw_front_size = 1536;
#endif
}
try_next_old_bio_vec:
/* Passing isBarrBioPtras NULL. All barriers are sent from helper thread */
/* If loop_get_buffer() incorrectly attempts to return barrier bio here, */
/* then that function fails with NULL pointer dereference */
new_bio = loop_get_buffer(lo, old_bio, 0, &merge, NULL);
if (!new_bio) {
/* just queue request and let thread handle allocs later */
if (merge)
loop_add_queue_last(lo, merge, &LDE_lo_bio_que1);
else
loop_add_queue_last(lo, old_bio, &LDE_lo_bio_que2);
return 0;
}
if (rw == WRITE) {
extension = new_bio->bi_private;
y = extension->bioext_index;
md = kmap(old_bio->bi_io_vec[y].bv_page) + old_bio->bi_io_vec[y].bv_offset;
if (lo_do_transfer(lo, WRITE, page_address(new_bio->bi_io_vec[0].bv_page), md, extension->bioext_size, extension->bioext_iv)) {
clear_bit(0, &merge->bi_flags);
}
kunmap(old_bio->bi_io_vec[y].bv_page);
}
/* merge & old_bio may vanish during generic_make_request() */
/* if last vec gets processed before function returns */
y = (merge->bi_idx < old_bio->bi_vcnt) ? 1 : 0;
generic_make_request(new_bio);
/* other vecs may need processing too */
if (y)
goto try_next_old_bio_vec;
return 0;
out:
old_bio->bi_next = NULL;
bio_io_error(old_bio, old_bio->bi_size);
return 0;
}
static void loop_unplug_backingdev(request_queue_t *bq)
{
#if defined(QUEUE_FLAG_PLUGGED)
if(bq && bq->unplug_fn)
bq->unplug_fn(bq);
#else
blk_run_queues();
#endif
}
#if defined(QUEUE_FLAG_PLUGGED)
static void loop_unplug_loopdev(request_queue_t *mq)
{
struct loop_device *lo;
struct file *f;
clear_bit(QUEUE_FLAG_PLUGGED, &mq->queue_flags);
lo = mq->queuedata;
if(!lo)
return;
f = lo->lo_backing_file;
if(!f)
return;
blk_run_address_space(f->f_mapping);
}
#endif
struct loop_switch_request {
struct file *file;
struct completion wait;
};
static void do_loop_switch(struct loop_device *lo, struct loop_switch_request *p)
{
struct file *file = p->file;
struct file *old_file=lo->lo_backing_file;
struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
/* This code runs on file backed loop only */
/* no need to worry about -1 old_gfp_mask */
mapping_set_gfp_mask(old_file->f_dentry->d_inode->i_mapping, lo->old_gfp_mask);
lo->lo_backing_file = file;
memset(lo->lo_file_name, 0, LO_NAME_SIZE);
lo->old_gfp_mask = mapping_gfp_mask(mapping);
mapping_set_gfp_mask(mapping, (lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)) | __GFP_HIGH);
complete(&p->wait);
}
/*
* worker thread that handles reads/writes to file backed loop devices,
* to avoid blocking in our make_request_fn. it also does loop decrypting
* on reads for block backed loop, as that is too heavy to do from
* b_end_io context where irqs may be disabled.
*/
static int loop_thread(void *data)
{
struct loop_device *lo = data;
struct bio *bio, *xbio, *merge;
struct loop_bio_extension *extension;
int x, y, flushcnt = 0, isBarrBio;
wait_queue_t waitq;
char *md;
request_queue_t *backingQueue;
static const struct rlimit loop_rlim_defaults[RLIM_NLIMITS] = INIT_RLIMITS;
init_waitqueue_entry(&waitq, current);
#if !defined(OLD_PER_THREAD_RLIMITS)
memcpy(¤t->signal->rlim[0], &loop_rlim_defaults[0], sizeof(current->signal->rlim));
#else
memcpy(¤t->rlim[0], &loop_rlim_defaults[0], sizeof(current->rlim));
#endif
daemonize("loop%d", lo->lo_number);
if(lo->lo_device)
backingQueue = bdev_get_queue(lo->lo_device);
else
backingQueue = NULL;
/*
* loop can be used in an encrypted device,
* hence, it mustn't be stopped at all
* because it could be indirectly used during suspension
*/
#if defined(PF_NOFREEZE)
current->flags |= PF_NOFREEZE;
#elif defined(PF_IOTHREAD)
current->flags |= PF_IOTHREAD;
#endif
current->flags |= PF_LESS_THROTTLE;
if (lo_nice > 0)
lo_nice = 0;
if (lo_nice < -20)
lo_nice = -20;
set_user_nice(current, lo_nice);
atomic_inc(&LDE_lo_pending);
/*
* up sem, we are running
*/
complete(&LDE_lo_done);
for (;;) {
add_wait_queue(&LDE_lo_bio_wait, &waitq);
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (!atomic_read(&LDE_lo_pending))
break;
x = 0;
spin_lock_irq(&lo->lo_lock);
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
if(LDE_lo_keyscrub_fn) x = 1;
#endif
if (LDE_lo_bio_que0) {
/* don't sleep if device backed READ needs processing */
/* don't sleep if file backed READ/WRITE needs processing */
x = 1;
} else if (LDE_lo_bio_que1) {
/* don't sleep if a buffer-bio is available */
/* don't sleep if need-buffer-bio request is not set */
if (LDE_lo_bio_free0 || !(LDE_lo_bio_need & 1))
x = 1;
} else if (LDE_lo_bio_que2) {
/* don't sleep if a merge-bio is available */
/* don't sleep if need-merge-bio request is not set */
if (LDE_lo_bio_free1 || !(LDE_lo_bio_need & 2))
x = 1;
}
spin_unlock_irq(&lo->lo_lock);
if (x)
break;
schedule();
}
set_current_state(TASK_RUNNING);
remove_wait_queue(&LDE_lo_bio_wait, &waitq);
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
if(LDE_lo_keyscrub_fn) {
(*LDE_lo_keyscrub_fn)(LDE_lo_keyscrub_ptr);
LDE_lo_keyscrub_fn = 0;
}
#endif
/*
* could be woken because of tear-down, not because of
* pending work
*/
if (!atomic_read(&LDE_lo_pending))
break;
bio = loop_get_bio(lo, &x);
if (!bio)
continue;
/*
* x list tag usage(has-buffer,has-merge)
* --- --------------- ---------------------------
* 0 LDE_lo_bio_que0 dev-r(y,y) / file-rw
* 1 LDE_lo_bio_que1 dev-rw(n,y)
* 2 LDE_lo_bio_que2 dev-rw(n,n)
*/
if (x >= 1) {
/* loop_make_request_real didn't allocate a buffer, do that now */
if (x == 1) {
merge = bio;
bio = merge->bi_private;
} else {
merge = NULL;
}
try_next_bio_vec:
isBarrBio = 0;
xbio = loop_get_buffer(lo, bio, 1, &merge, &isBarrBio);
if (!xbio) {
loop_unplug_backingdev(backingQueue);
flushcnt = 0;
if (merge)
loop_add_queue_first(lo, merge, &LDE_lo_bio_que1);
else
loop_add_queue_first(lo, bio, &LDE_lo_bio_que2);
/* LDE_lo_bio_need should be non-zero now, go back to sleep */
continue;
}
if (bio_rw(bio) == WRITE) {
extension = xbio->bi_private;
y = extension->bioext_index;
md = kmap(bio->bi_io_vec[y].bv_page) + bio->bi_io_vec[y].bv_offset;
if (lo_do_transfer(lo, WRITE, page_address(xbio->bi_io_vec[0].bv_page), md, extension->bioext_size, extension->bioext_iv)) {
clear_bit(0, &merge->bi_flags);
}
kunmap(bio->bi_io_vec[y].bv_page);
}
/* merge & bio may vanish during generic_make_request() */
/* if last vec gets processed before function returns */
y = (merge->bi_idx < bio->bi_vcnt) ? 1 : 0;
generic_make_request(xbio);
/* maybe just submitted bio was a barrier bio */
if (isBarrBio) {
atomic_dec(&LDE_lo_bio_barr);
}
/* start I/O if there are no more requests lacking buffers */
x = 0;
spin_lock_irq(&lo->lo_lock);
if (!y && !LDE_lo_bio_que1 && !LDE_lo_bio_que2)
x = 1;
spin_unlock_irq(&lo->lo_lock);
if (x || (++flushcnt >= LDE_lo_bio_flsh)) {
loop_unplug_backingdev(backingQueue);
flushcnt = 0;
}
/* other vecs may need processing too */
if (y)
goto try_next_bio_vec;
/* request not completely processed yet */
continue;
}
if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
/* request is for file backed device */
if(unlikely(!bio->bi_bdev)) {
do_loop_switch(lo, bio->bi_private);
bio->bi_next = NULL;
bio_put(bio);
} else {
y = do_bio_filebacked(lo, bio);
bio->bi_next = NULL;
bio_endio(bio, bio->bi_size, y);
}
} else {
/* device backed read has completed, do decrypt now */
extension = bio->bi_private;
merge = extension->bioext_merge;
y = extension->bioext_index;
xbio = merge->bi_private;
md = kmap(xbio->bi_io_vec[y].bv_page) + xbio->bi_io_vec[y].bv_offset;
if (lo_do_transfer(lo, READ, page_address(bio->bi_io_vec[0].bv_page), md, extension->bioext_size, extension->bioext_iv)) {
clear_bit(0, &merge->bi_flags);
}
flush_dcache_page(xbio->bi_io_vec[y].bv_page);
kunmap(xbio->bi_io_vec[y].bv_page);
loop_put_buffer(lo, bio, 0);
if (!atomic_dec_and_test(&merge->bi_cnt))
continue;
xbio->bi_next = NULL;
bio_endio(xbio, xbio->bi_size, test_bit(0, &merge->bi_flags) ? (int)merge->bi_size : -EIO);
loop_put_buffer(lo, merge, 1);
}
/*
* woken both for pending work and tear-down, lo_pending
* will hit zero then
*/
if (atomic_dec_and_test(&LDE_lo_pending))
break;
}
complete(&LDE_lo_done);
return 0;
}
static void loop_set_softblksz(struct loop_device *lo, struct block_device *bdev)
{
int bs, x;
if (lo->lo_device)
bs = block_size(lo->lo_device);
else
bs = PAGE_SIZE;
if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
x = (int) bdev->bd_inode->i_size;
if ((bs == 8192) && (x & 0x1E00))
bs = 4096;
if ((bs == 4096) && (x & 0x0E00))
bs = 2048;
if ((bs == 2048) && (x & 0x0600))
bs = 1024;
if ((bs == 1024) && (x & 0x0200))
bs = 512;
}
set_blocksize(bdev, bs);
}
/*
* loop_change_fd switches the backing store of a loopback device to a
* new file. This is useful for operating system installers to free up the
* original file and in High Availability environments to switch to an
* alternative location for the content in case of server meltdown.
* This can only work if the loop device is used read-only, file backed,
* and if the new backing store is the same size and type as the old
* backing store.
*/
static int loop_change_fd(struct loop_device *lo, unsigned int arg)
{
struct file *file, *old_file;
struct inode *inode;
struct loop_switch_request w;
struct bio *bio;
int error;
error = -EINVAL;
/* loop must be read-only */
if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
goto out;
/* loop must be file backed */
if (!(lo->lo_flags & LO_FLAGS_DO_BMAP))
goto out;
error = -EBADF;
file = fget(arg);
if (!file)
goto out;
inode = file->f_dentry->d_inode;
old_file = lo->lo_backing_file;
error = -EINVAL;
/* new backing store must be file backed */
if (!S_ISREG(inode->i_mode))
goto out_putf;
/* new backing store must support reads */
if (!file->f_op || !file->f_op->read)
goto out_putf;
/* new backing store must be same size as the old one */
if(i_size_read(inode) != i_size_read(old_file->f_dentry->d_inode))
goto out_putf;
/* loop must be in properly initialized state */
if(lo->lo_queue->make_request_fn != loop_make_request_real)
goto out_putf;
error = -ENOMEM;
bio = bio_alloc(GFP_KERNEL, 1);
if (!bio)
goto out_putf;
/* wait for loop thread to do the switch */
init_completion(&w.wait);
w.file = file;
bio->bi_private = &w;
bio->bi_bdev = NULL;
bio->bi_rw = 0;
loop_make_request_real(lo->lo_queue, bio);
wait_for_completion(&w.wait);
fput(old_file);
return 0;
out_putf:
fput(file);
out:
return error;
}
#if defined(blk_fua_rq) || defined(REQ_FUA)
# define loop_blk_queue_ordered(a,b) blk_queue_ordered(a,b,NULL)
#else
# define loop_blk_queue_ordered(a,b) blk_queue_ordered(a,b)
#endif
static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
struct block_device *bdev, unsigned int arg)
{
struct file *file;
struct inode *inode;
struct block_device *lo_device = NULL;
int lo_flags = 0;
int error;
error = -EBADF;
file = fget(arg);
if (!file)
goto out;
error = -EINVAL;
inode = file->f_dentry->d_inode;
if (!(file->f_mode & FMODE_WRITE))
lo_flags |= LO_FLAGS_READ_ONLY;
init_completion(&LDE_lo_done);
spin_lock_init(&lo->lo_lock);
init_waitqueue_head(&LDE_lo_bio_wait);
atomic_set(&LDE_lo_pending, 0);
atomic_set(&LDE_lo_bio_barr, 0);
clear_bit(0, &LDE_lo_bio_flag);
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
LDE_lo_keyscrub_fn = 0;
#endif
lo->lo_offset = lo->lo_sizelimit = 0;
LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
lo->lo_encryption = NULL;
lo->lo_encrypt_key_size = 0;
lo->transfer = NULL;
lo->lo_crypt_name[0] = 0;
lo->lo_file_name[0] = 0;
lo->lo_init[1] = lo->lo_init[0] = 0;
lo->lo_key_owner = 0;
lo->ioctl = NULL;
lo->key_data = NULL;
LDE_lo_bio_que2 = LDE_lo_bio_que1 = LDE_lo_bio_que0 = NULL;
LDE_lo_bio_free1 = LDE_lo_bio_free0 = NULL;
LDE_lo_bio_flsh = LDE_lo_bio_need = 0;
if (S_ISBLK(inode->i_mode)) {
lo_device = inode->i_bdev;
if (lo_device == bdev) {
error = -EBUSY;
goto out_putf;
}
if (loop_prealloc_init(lo, 0)) {
error = -ENOMEM;
goto out_putf;
}
if (bdev_read_only(lo_device))
lo_flags |= LO_FLAGS_READ_ONLY;
else
filemap_fdatawrite(inode->i_mapping);
} else if (S_ISREG(inode->i_mode)) {
/*
* If we can't read - sorry. If we only can't write - well,
* it's going to be read-only.
*/
if (!file->f_op || !file->f_op->read)
goto out_putf;
if (!file->f_op->write)
lo_flags |= LO_FLAGS_READ_ONLY;
lo_flags |= LO_FLAGS_DO_BMAP;
if (loop_prealloc_init(lo, 1)) {
error = -ENOMEM;
goto out_putf;
}
} else
goto out_putf;
get_file(file);
if (!(lo_file->f_mode & FMODE_WRITE))
lo_flags |= LO_FLAGS_READ_ONLY;
set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
lo->lo_device = lo_device;
lo->lo_flags = lo_flags;
if(lo_flags & LO_FLAGS_READ_ONLY)
lo->lo_flags |= 0x200000; /* export to user space */
lo->lo_backing_file = file;
if (figure_loop_size(lo, bdev)) {
error = -EFBIG;
goto out_cleanup;
}
/*
* set queue make_request_fn, and add limits based on lower level
* device
*/
blk_queue_make_request(lo->lo_queue, loop_make_request_err);
blk_queue_bounce_limit(lo->lo_queue, BLK_BOUNCE_ANY);
blk_queue_max_segment_size(lo->lo_queue, PAGE_CACHE_SIZE);
blk_queue_segment_boundary(lo->lo_queue, PAGE_CACHE_SIZE - 1);
blk_queue_max_phys_segments(lo->lo_queue, MAX_PHYS_SEGMENTS);
blk_queue_max_hw_segments(lo->lo_queue, MAX_HW_SEGMENTS);
#if !defined(MAX_SECTORS)
# define MAX_SECTORS SAFE_MAX_SECTORS
#endif
blk_queue_max_sectors(lo->lo_queue, MAX_SECTORS);
lo->lo_queue->queue_flags &= ~(1 << QUEUE_FLAG_CLUSTER);
#if (LINUX_VERSION_CODE >= 0x20609) || defined(QUEUE_FLAG_ORDERED)
loop_blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_NONE);
#endif
#if LINUX_VERSION_CODE >= 0x20609
blk_queue_issue_flush_fn(lo->lo_queue, NULL);
#endif
/*
* we remap to a block device, make sure we correctly stack limits
*/
if (S_ISBLK(inode->i_mode) && lo_device) {
request_queue_t *q = bdev_get_queue(lo_device);
blk_queue_hardsect_size(lo->lo_queue, q->hardsect_size);
#if (LINUX_VERSION_CODE >= 0x20609) && !defined(QUEUE_FLAG_ORDERED)
if(q->ordered == QUEUE_ORDERED_TAG) {
loop_blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_TAG);
if(q->issue_flush_fn) {
blk_queue_issue_flush_fn(lo->lo_queue, loop_issue_flush);
}
}
#elif (LINUX_VERSION_CODE >= 0x20609) || defined(QUEUE_FLAG_ORDERED)
if(q->queue_flags & (1 << QUEUE_FLAG_ORDERED)) {
loop_blk_queue_ordered(lo->lo_queue, 1);
#if LINUX_VERSION_CODE >= 0x20609
if(q->issue_flush_fn) {
blk_queue_issue_flush_fn(lo->lo_queue, loop_issue_flush);
}
#endif
}
#endif
}
if (lo_flags & LO_FLAGS_DO_BMAP) {
lo->old_gfp_mask = mapping_gfp_mask(inode->i_mapping);
mapping_set_gfp_mask(inode->i_mapping, (lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)) | __GFP_HIGH);
} else {
lo->old_gfp_mask = -1;
}
loop_set_softblksz(lo, bdev);
error = kernel_thread(loop_thread, lo, CLONE_KERNEL);
if(error < 0)
goto out_mapping;
wait_for_completion(&LDE_lo_done);
fput(file);
#if defined(QUEUE_FLAG_PLUGGED)
lo->lo_queue->unplug_fn = loop_unplug_loopdev;
#endif
lo->lo_queue->queuedata = lo;
__module_get(THIS_MODULE);
return 0;
out_mapping:
if(lo->old_gfp_mask != -1)
mapping_set_gfp_mask(inode->i_mapping, lo->old_gfp_mask);
out_cleanup:
loop_prealloc_cleanup(lo);
fput(file);
out_putf:
fput(file);
out:
return error;
}
static int loop_release_xfer(struct loop_device *lo)
{
int err = 0;
struct loop_func_table *xfer = lo->lo_encryption;
if (xfer) {
lo->transfer = NULL;
if (xfer->release)
err = xfer->release(lo);
lo->lo_encryption = NULL;
module_put(xfer->owner);
}
return err;
}
static int loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, struct loop_info64 *i)
{
int err = 0;
if (xfer) {
struct module *owner = xfer->owner;
if(!try_module_get(owner))
return -EINVAL;
if (xfer->init) {
/* this ugly cast is needed to work around 'const' damage in function prototype */
/* should be: err = xfer->init(lo, i); */
err = ((int (*)(struct loop_device *, struct loop_info64 *))xfer->init)(lo, i);
}
if (err)
module_put(owner);
else
lo->lo_encryption = xfer;
}
return err;
}
static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
{
struct file *filp = lo->lo_backing_file;
int gfp = lo->old_gfp_mask;
if (bdev->bd_openers != 1) /* one for this fd being open */
return -EBUSY;
if (filp==NULL)
return -EINVAL;
lo->lo_queue->queuedata = NULL;
lo->lo_queue->make_request_fn = loop_make_request_err;
if (atomic_dec_and_test(&LDE_lo_pending))
wake_up_interruptible(&LDE_lo_bio_wait);
wait_for_completion(&LDE_lo_done);
#if (LINUX_VERSION_CODE >= 0x20609) || defined(QUEUE_FLAG_ORDERED)
loop_blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_NONE);
#endif
loop_prealloc_cleanup(lo);
lo->lo_backing_file = NULL;
loop_release_xfer(lo);
lo->transfer = NULL;
lo->ioctl = NULL;
lo->lo_device = NULL;
lo->lo_encryption = NULL;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
LDE_lo_keyscrub_fn = 0;
#endif
lo->lo_offset = lo->lo_sizelimit = 0;
LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
lo->lo_encrypt_key_size = 0;
lo->lo_flags = 0;
lo->lo_init[1] = lo->lo_init[0] = 0;
lo->lo_key_owner = 0;
lo->key_data = NULL;
memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
memset(lo->lo_file_name, 0, LO_NAME_SIZE);
invalidate_bdev(bdev, 0);
set_capacity(disks[lo->lo_number], 0);
if (gfp != -1)
mapping_set_gfp_mask(filp->f_dentry->d_inode->i_mapping, gfp);
fput(filp);
module_put(THIS_MODULE);
return 0;
}
static int loop_set_status(struct loop_device *lo, struct block_device *bdev, struct loop_info64 *info)
{
int err;
struct loop_func_table *xfer = NULL;
if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
!capable(CAP_SYS_ADMIN))
return -EPERM;
if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
return -EINVAL;
err = loop_release_xfer(lo);
if (err)
return err;
if ((loff_t)info->lo_offset < 0) {
/* negative offset == remove offset from IV computations */
lo->lo_offset = -(info->lo_offset);
LDE_lo_iv_remove = lo->lo_offset >> 9;
} else {
/* positive offset == include offset in IV computations */
lo->lo_offset = info->lo_offset;
LDE_lo_iv_remove = 0;
}
LDE_lo_offs_sec = lo->lo_offset >> 9;
lo->lo_sizelimit = info->lo_sizelimit;
err = figure_loop_size(lo, bdev);
if (err)
return err;
loop_set_softblksz(lo, bdev);
if (info->lo_encrypt_type) {
unsigned int type = info->lo_encrypt_type;
if (type >= MAX_LO_CRYPT)
return -EINVAL;
xfer = xfer_funcs[type];
if (xfer == NULL)
return -EINVAL;
}
err = loop_init_xfer(lo, xfer, info);
if (err)
return err;
if (!xfer)
xfer = &none_funcs;
lo->transfer = xfer->transfer;
lo->ioctl = xfer->ioctl;
memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
lo->lo_file_name[LO_NAME_SIZE-1] = 0;
lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
lo->lo_init[0] = info->lo_init[0];
lo->lo_init[1] = info->lo_init[1];
if (info->lo_encrypt_key_size) {
memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
info->lo_encrypt_key_size);
lo->lo_key_owner = current->uid;
}
lo->lo_queue->make_request_fn = loop_make_request_real;
return 0;
}
static int loop_get_status(struct loop_device *lo, struct loop_info64 *info)
{
struct file *file = lo->lo_backing_file;
struct kstat stat;
int error;
error = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
if (error)
return error;
memset(info, 0, sizeof(*info));
info->lo_number = lo->lo_number;
info->lo_device = huge_encode_dev(stat.dev);
info->lo_inode = stat.ino;
info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
info->lo_offset = LDE_lo_iv_remove ? -(lo->lo_offset) : lo->lo_offset;
info->lo_sizelimit = lo->lo_sizelimit;
info->lo_flags = lo->lo_flags;
memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
info->lo_encrypt_type = lo->lo_encryption ? lo->lo_encryption->number : 0;
if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
lo->lo_encrypt_key_size);
info->lo_init[0] = lo->lo_init[0];
info->lo_init[1] = lo->lo_init[1];
}
return 0;
}
static void
loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
{
memset(info64, 0, sizeof(*info64));
info64->lo_number = info->lo_number;
info64->lo_device = info->lo_device;
info64->lo_inode = info->lo_inode;
info64->lo_rdevice = info->lo_rdevice;
info64->lo_offset = info->lo_offset;
info64->lo_encrypt_type = info->lo_encrypt_type;
info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
info64->lo_flags = info->lo_flags;
info64->lo_init[0] = info->lo_init[0];
info64->lo_init[1] = info->lo_init[1];
if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
else
memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
}
static int
loop_info64_to_old(struct loop_info64 *info64, struct loop_info *info)
{
memset(info, 0, sizeof(*info));
info->lo_number = info64->lo_number;
info->lo_device = info64->lo_device;
info->lo_inode = info64->lo_inode;
info->lo_rdevice = info64->lo_rdevice;
info->lo_offset = info64->lo_offset;
info->lo_encrypt_type = info64->lo_encrypt_type;
info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
info->lo_flags = info64->lo_flags;
info->lo_init[0] = info64->lo_init[0];
info->lo_init[1] = info64->lo_init[1];
if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
else
memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
/* error in case values were truncated */
if (info->lo_device != info64->lo_device ||
info->lo_rdevice != info64->lo_rdevice ||
info->lo_inode != info64->lo_inode ||
info->lo_offset != info64->lo_offset ||
info64->lo_sizelimit)
return -EOVERFLOW;
return 0;
}
static int
loop_set_status_old(struct loop_device *lo, struct block_device *bdev, const struct loop_info *arg)
{
struct loop_info info;
struct loop_info64 info64;
if (copy_from_user(&info, arg, sizeof (struct loop_info)))
return -EFAULT;
loop_info64_from_old(&info, &info64);
memset(&info.lo_encrypt_key[0], 0, sizeof(info.lo_encrypt_key));
return loop_set_status(lo, bdev, &info64);
}
static int
loop_set_status64(struct loop_device *lo, struct block_device *bdev, struct loop_info64 *arg)
{
struct loop_info64 info64;
if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
return -EFAULT;
return loop_set_status(lo, bdev, &info64);
}
static int
loop_get_status_old(struct loop_device *lo, struct loop_info *arg) {
struct loop_info info;
struct loop_info64 info64;
int err = 0;
if (!arg)
err = -EINVAL;
if (!err)
err = loop_get_status(lo, &info64);
if (!err)
err = loop_info64_to_old(&info64, &info);
if (!err && copy_to_user(arg, &info, sizeof(info)))
err = -EFAULT;
return err;
}
static int
loop_get_status64(struct loop_device *lo, struct loop_info64 *arg) {
struct loop_info64 info64;
int err = 0;
if (!arg)
err = -EINVAL;
if (!err)
err = loop_get_status(lo, &info64);
if (!err && copy_to_user(arg, &info64, sizeof(info64)))
err = -EFAULT;
return err;
}
static int lo_ioctl(struct inode *inode, struct file * file, unsigned int cmd, unsigned long arg)
{
struct block_device *bdev = inode->i_bdev;
struct loop_device *lo = bdev->bd_disk->private_data;
int err;
wait_queue_t waitq;
/*
* mutual exclusion - lock
*/
init_waitqueue_entry(&waitq, current);
add_wait_queue(&LDE_lo_ioctl_wait, &waitq);
for (;;) {
set_current_state(TASK_UNINTERRUPTIBLE);
spin_lock(&LDE_lo_ioctl_spin);
err = LDE_lo_ioctl_busy;
if(!err) LDE_lo_ioctl_busy = 1;
spin_unlock(&LDE_lo_ioctl_spin);
if(!err) break;
schedule();
}
set_current_state(TASK_RUNNING);
remove_wait_queue(&LDE_lo_ioctl_wait, &waitq);
/*
* LOOP_SET_FD can only be called when no device is attached.
* All other ioctls can only be called when a device is attached.
*/
if (bdev->bd_disk->queue->queuedata != NULL) {
if (cmd == LOOP_SET_FD) {
err = -EBUSY;
goto out_err;
}
} else {
if (cmd != LOOP_SET_FD) {
err = -ENXIO;
goto out_err;
}
}
switch (cmd) {
case LOOP_SET_FD:
err = loop_set_fd(lo, file, bdev, arg);
break;
case LOOP_CHANGE_FD:
err = loop_change_fd(lo, arg);
break;
case LOOP_CLR_FD:
err = loop_clr_fd(lo, bdev);
break;
case LOOP_SET_STATUS:
err = loop_set_status_old(lo, bdev, (struct loop_info *) arg);
break;
case LOOP_GET_STATUS:
err = loop_get_status_old(lo, (struct loop_info *) arg);
break;
case LOOP_SET_STATUS64:
err = loop_set_status64(lo, bdev, (struct loop_info64 *) arg);
break;
case LOOP_GET_STATUS64:
err = loop_get_status64(lo, (struct loop_info64 *) arg);
break;
case LOOP_RECOMPUTE_DEV_SIZE:
err = figure_loop_size(lo, bdev);
break;
default:
err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
}
out_err:
/*
* mutual exclusion - unlock
*/
spin_lock(&LDE_lo_ioctl_spin);
LDE_lo_ioctl_busy = 0;
spin_unlock(&LDE_lo_ioctl_spin);
wake_up_all(&LDE_lo_ioctl_wait);
return err;
}
#if defined(CONFIG_COMPAT) && defined(HAVE_COMPAT_IOCTL)
struct loop_info32 {
compat_int_t lo_number; /* ioctl r/o */
compat_dev_t lo_device; /* ioctl r/o */
compat_ulong_t lo_inode; /* ioctl r/o */
compat_dev_t lo_rdevice; /* ioctl r/o */
compat_int_t lo_offset;
compat_int_t lo_encrypt_type;
compat_int_t lo_encrypt_key_size; /* ioctl w/o */
compat_int_t lo_flags; /* ioctl r/o */
char lo_name[LO_NAME_SIZE];
unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
compat_ulong_t lo_init[2];
char reserved[4];
};
static long lo_compat_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file->f_dentry->d_inode;
mm_segment_t old_fs = get_fs();
struct loop_info l;
struct loop_info32 *ul = (struct loop_info32 *)arg;
int err = -ENOIOCTLCMD;
switch (cmd) {
case LOOP_SET_FD:
case LOOP_CLR_FD:
case LOOP_SET_STATUS64:
case LOOP_GET_STATUS64:
case LOOP_CHANGE_FD:
case LOOP_MULTI_KEY_SETUP:
case LOOP_MULTI_KEY_SETUP_V3:
case LOOP_RECOMPUTE_DEV_SIZE:
err = lo_ioctl(inode, file, cmd, arg);
break;
case LOOP_SET_STATUS:
memset(&l, 0, sizeof(l));
err = get_user(l.lo_number, &ul->lo_number);
err |= get_user(l.lo_device, &ul->lo_device);
err |= get_user(l.lo_inode, &ul->lo_inode);
err |= get_user(l.lo_rdevice, &ul->lo_rdevice);
err |= copy_from_user(&l.lo_offset, &ul->lo_offset,
8 + (unsigned long)l.lo_init - (unsigned long)&l.lo_offset);
if (err) {
err = -EFAULT;
} else {
set_fs (KERNEL_DS);
err = lo_ioctl(inode, file, cmd, (unsigned long)&l);
set_fs (old_fs);
}
memset(&l, 0, sizeof(l));
break;
case LOOP_GET_STATUS:
set_fs (KERNEL_DS);
err = lo_ioctl(inode, file, cmd, (unsigned long)&l);
set_fs (old_fs);
if (!err) {
err = put_user(l.lo_number, &ul->lo_number);
err |= put_user(l.lo_device, &ul->lo_device);
err |= put_user(l.lo_inode, &ul->lo_inode);
err |= put_user(l.lo_rdevice, &ul->lo_rdevice);
err |= copy_to_user(&ul->lo_offset, &l.lo_offset,
(unsigned long)l.lo_init - (unsigned long)&l.lo_offset);
if (err)
err = -EFAULT;
}
memset(&l, 0, sizeof(l));
break;
}
return err;
}
#endif
static int lo_open(struct inode *inode, struct file *file)
{
return 0;
}
static int lo_release(struct inode *inode, struct file *file)
{
sync_blockdev(inode->i_bdev);
return 0;
}
static struct block_device_operations lo_fops = {
.owner = THIS_MODULE,
.open = lo_open,
.release = lo_release,
.ioctl = lo_ioctl,
#if defined(CONFIG_COMPAT) && defined(HAVE_COMPAT_IOCTL)
.compat_ioctl = lo_compat_ioctl,
#endif
};
/*
* And now the modules code and kernel interface.
*/
MODULE_LICENSE("GPL");
#if (LINUX_VERSION_CODE >= 0x20601) || !defined(OLD_REQUEST_MODULE_INTERFACE)
MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
#else
MODULE_ALIAS("block-major-7");
#endif
int loop_register_transfer(struct loop_func_table *funcs)
{
unsigned int n = funcs->number;
if (n >= MAX_LO_CRYPT || xfer_funcs[n])
return -EINVAL;
xfer_funcs[n] = funcs;
return 0;
}
int loop_unregister_transfer(int number)
{
unsigned int n = number;
struct loop_device *lo;
struct loop_func_table *xfer;
int x;
if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
return -EINVAL;
xfer_funcs[n] = NULL;
for (x = 0; x < max_loop; x++) {
lo = loop_dev_ptr_arr[x];
if (!lo)
continue;
if (lo->lo_encryption == xfer)
loop_release_xfer(lo);
}
return 0;
}
EXPORT_SYMBOL(loop_register_transfer);
EXPORT_SYMBOL(loop_unregister_transfer);
int __init loop_init(void)
{
int i;
if ((max_loop < 1) || (max_loop > 256)) {
printk(KERN_WARNING "loop: invalid max_loop (must be between"
" 1 and 256), using default (8)\n");
max_loop = 8;
}
if (register_blkdev(LOOP_MAJOR, "loop"))
return -EIO;
loop_dev_ptr_arr = kmalloc(max_loop * sizeof(struct loop_device *), GFP_KERNEL);
if (!loop_dev_ptr_arr)
goto out_mem1;
disks = kmalloc(max_loop * sizeof(struct gendisk *), GFP_KERNEL);
if (!disks)
goto out_mem2;
for (i = 0; i < max_loop; i++) {
loop_dev_ptr_arr[i] = kmalloc(sizeof(LoDevExt), GFP_KERNEL);
if (!loop_dev_ptr_arr[i])
goto out_mem3;
}
for (i = 0; i < max_loop; i++) {
disks[i] = alloc_disk(1);
if (!disks[i])
goto out_mem4;
}
for (i = 0; i < max_loop; i++) {
disks[i]->queue = blk_alloc_queue(GFP_KERNEL);
if (!disks[i]->queue)
goto out_mem5;
disks[i]->queue->queuedata = NULL;
blk_queue_make_request(disks[i]->queue, loop_make_request_err);
}
{ extern int init_module_aes(void); init_module_aes(); }
for (i = 0; i < (sizeof(lo_prealloc) / sizeof(int)); i += 2) {
if (!lo_prealloc[i])
continue;
if (lo_prealloc[i] < LO_PREALLOC_MIN)
lo_prealloc[i] = LO_PREALLOC_MIN;
if (lo_prealloc[i] > LO_PREALLOC_MAX)
lo_prealloc[i] = LO_PREALLOC_MAX;
}
#if defined(IOCTL32_COMPATIBLE_PTR)
register_ioctl32_conversion(LOOP_MULTI_KEY_SETUP, IOCTL32_COMPATIBLE_PTR);
register_ioctl32_conversion(LOOP_MULTI_KEY_SETUP_V3, IOCTL32_COMPATIBLE_PTR);
register_ioctl32_conversion(LOOP_RECOMPUTE_DEV_SIZE, IOCTL32_COMPATIBLE_PTR);
#endif
#ifdef CONFIG_DEVFS_FS
devfs_mk_dir("loop");
#endif
for (i = 0; i < max_loop; i++) {
struct loop_device *lo = loop_dev_ptr_arr[i];
struct gendisk *disk = disks[i];
memset(lo, 0, sizeof(LoDevExt));
lo->lo_number = i;
lo->lo_queue = disk->queue;
spin_lock_init(&LDE_lo_ioctl_spin);
init_waitqueue_head(&LDE_lo_ioctl_wait);
disk->major = LOOP_MAJOR;
disk->first_minor = i;
disk->fops = &lo_fops;
sprintf(disk->disk_name, "loop%d", i);
#ifdef CONFIG_DEVFS_FS
sprintf(disk->devfs_name, "loop/%d", i);
#endif
disk->private_data = lo;
add_disk(disk);
}
printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop);
return 0;
out_mem5:
while (i--)
blk_put_queue(disks[i]->queue);
i = max_loop;
out_mem4:
while (i--)
put_disk(disks[i]);
i = max_loop;
out_mem3:
while (i--)
kfree(loop_dev_ptr_arr[i]);
kfree(disks);
out_mem2:
kfree(loop_dev_ptr_arr);
out_mem1:
unregister_blkdev(LOOP_MAJOR, "loop");
printk(KERN_ERR "loop: ran out of memory\n");
return -ENOMEM;
}
void loop_exit(void)
{
int i;
{ extern void cleanup_module_aes(void); cleanup_module_aes(); }
for (i = 0; i < max_loop; i++) {
del_gendisk(disks[i]);
put_disk(disks[i]);
blk_put_queue(loop_dev_ptr_arr[i]->lo_queue);
kfree(loop_dev_ptr_arr[i]);
}
#ifdef CONFIG_DEVFS_FS
devfs_remove("loop");
#endif
unregister_blkdev(LOOP_MAJOR, "loop");
kfree(disks);
kfree(loop_dev_ptr_arr);
#if defined(IOCTL32_COMPATIBLE_PTR)
unregister_ioctl32_conversion(LOOP_MULTI_KEY_SETUP);
unregister_ioctl32_conversion(LOOP_MULTI_KEY_SETUP_V3);
unregister_ioctl32_conversion(LOOP_RECOMPUTE_DEV_SIZE);
#endif
}
module_init(loop_init);
module_exit(loop_exit);
extern void loop_compute_sector_iv(sector_t, u_int32_t *);
EXPORT_SYMBOL(loop_compute_sector_iv);
extern void loop_compute_md5_iv_v3(sector_t, u_int32_t *, u_int32_t *);
EXPORT_SYMBOL(loop_compute_md5_iv_v3);
extern void loop_compute_md5_iv(sector_t, u_int32_t *, u_int32_t *);
EXPORT_SYMBOL(loop_compute_md5_iv);
extern void md5_transform_CPUbyteorder(u_int32_t *, u_int32_t const *);
EXPORT_SYMBOL(md5_transform_CPUbyteorder);
extern void md5_transform_CPUbyteorder_C(u_int32_t *, u_int32_t const *);
EXPORT_SYMBOL(md5_transform_CPUbyteorder_C);
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
void loop_add_keyscrub_fn(struct loop_device *lo, void (*fn)(void *), void *ptr)
{
LDE_lo_keyscrub_ptr = ptr;
wmb();
LDE_lo_keyscrub_fn = fn;
wake_up_interruptible(&LDE_lo_bio_wait);
}
EXPORT_SYMBOL(loop_add_keyscrub_fn);
#endif
|