mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1787 lines
50 KiB

  1. --- linux-2.4.25/drivers/block/loop.c 2003-08-25 13:44:41.000000000 +0200
  2. +++ linux-2.4.25/drivers/block/loop.c 2004-02-08 16:51:25.000000000 +0100
  3. @@ -39,21 +39,29 @@
  4. * Support up to 256 loop devices
  5. * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
  6. *
  7. + * IV is now passed as (512 byte) sector number.
  8. + * Jari Ruusu, May 18 2001
  9. + *
  10. + * External encryption module locking bug fixed.
  11. + * Ingo Rohloff <rohloff@in.tum.de>, June 21 2001
  12. + *
  13. + * Make device backed loop work with swap (pre-allocated buffers + queue rewrite).
  14. + * Jari Ruusu, September 2 2001
  15. + *
  16. + * File backed code now uses file->f_op->read/write. Based on Andrew Morton's idea.
  17. + * Jari Ruusu, May 23 2002
  18. + *
  19. + * Backported struct loop_info64 ioctls from 2.6 kernels (64 bit offsets and
  20. + * 64 bit sizelimits). Added support for removing offset from IV computations.
  21. + * Jari Ruusu, September 21 2003
  22. + *
  23. + *
  24. * Still To Fix:
  25. * - Advisory locking is ignored here.
  26. * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  27. - *
  28. - * WARNING/FIXME:
  29. - * - The block number as IV passing to low level transfer functions is broken:
  30. - * it passes the underlying device's block number instead of the
  31. - * offset. This makes it change for a given block when the file is
  32. - * moved/restored/copied and also doesn't work over NFS.
  33. - * AV, Feb 12, 2000: we pass the logical block number now. It fixes the
  34. - * problem above. Encryption modules that used to rely on the old scheme
  35. - * should just call ->i_mapping->bmap() to calculate the physical block
  36. - * number.
  37. */
  38. +#include <linux/version.h>
  39. #include <linux/config.h>
  40. #include <linux/module.h>
  41. @@ -71,6 +79,9 @@
  42. #include <linux/smp_lock.h>
  43. #include <linux/swap.h>
  44. #include <linux/slab.h>
  45. +#if defined(CONFIG_SOFTWARE_SUSPEND) || defined(CONFIG_SOFTWARE_SUSPEND2)
  46. +#include <linux/suspend.h>
  47. +#endif
  48. #include <asm/uaccess.h>
  49. @@ -79,24 +90,44 @@
  50. #define MAJOR_NR LOOP_MAJOR
  51. static int max_loop = 8;
  52. -static struct loop_device *loop_dev;
  53. static int *loop_sizes;
  54. static int *loop_blksizes;
  55. +static int *loop_hardsizes;
  56. static devfs_handle_t devfs_handle; /* For the directory */
  57. +struct loopinfo64 {
  58. + __u64 lo_device; /* ioctl r/o */
  59. + __u64 lo_inode; /* ioctl r/o */
  60. + __u64 lo_rdevice; /* ioctl r/o */
  61. + __u64 lo_offset;
  62. + __u64 lo_sizelimit;/* bytes, 0 == max available */
  63. + __u32 lo_number; /* ioctl r/o */
  64. + __u32 lo_encrypt_type;
  65. + __u32 lo_encrypt_key_size; /* ioctl w/o */
  66. + __u32 lo_flags; /* ioctl r/o */
  67. + __u8 lo_file_name[LO_NAME_SIZE];
  68. + __u8 lo_crypt_name[LO_NAME_SIZE];
  69. + __u8 lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  70. + __u64 lo_init[2];
  71. +};
  72. +#if !defined(LOOP_SET_STATUS64)
  73. +# define LOOP_SET_STATUS64 0x4C04
  74. +#endif
  75. +#if !defined(LOOP_GET_STATUS64)
  76. +# define LOOP_GET_STATUS64 0x4C05
  77. +#endif
  78. +
  79. /*
  80. * Transfer functions
  81. */
  82. static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
  83. char *loop_buf, int size, int real_block)
  84. {
  85. - if (raw_buf != loop_buf) {
  86. - if (cmd == READ)
  87. - memcpy(loop_buf, raw_buf, size);
  88. - else
  89. - memcpy(raw_buf, loop_buf, size);
  90. - }
  91. + /* this code is only called from file backed loop */
  92. + /* and that code expects this function to be no-op */
  93. + if (current->need_resched)
  94. + {set_current_state(TASK_RUNNING);schedule();}
  95. return 0;
  96. }
  97. @@ -118,12 +149,13 @@ static int transfer_xor(struct loop_devi
  98. keysize = lo->lo_encrypt_key_size;
  99. for (i = 0; i < size; i++)
  100. *out++ = *in++ ^ key[(i & 511) % keysize];
  101. + if (current->need_resched)
  102. + {set_current_state(TASK_RUNNING);schedule();}
  103. return 0;
  104. }
  105. static int none_status(struct loop_device *lo, struct loop_info *info)
  106. {
  107. - lo->lo_flags |= LO_FLAGS_BH_REMAP;
  108. return 0;
  109. }
  110. @@ -136,13 +168,13 @@ static int xor_status(struct loop_device
  111. struct loop_func_table none_funcs = {
  112. number: LO_CRYPT_NONE,
  113. - transfer: transfer_none,
  114. + transfer: (void *)transfer_none,
  115. init: none_status,
  116. };
  117. struct loop_func_table xor_funcs = {
  118. number: LO_CRYPT_XOR,
  119. - transfer: transfer_xor,
  120. + transfer: (void *)transfer_xor,
  121. init: xor_status
  122. };
  123. @@ -152,325 +184,420 @@ struct loop_func_table *xfer_funcs[MAX_L
  124. &xor_funcs
  125. };
  126. -#define MAX_DISK_SIZE 1024*1024*1024
  127. +/*
  128. + * First number of 'lo_prealloc' is the default number of RAM pages
  129. + * to pre-allocate for each device backed loop. Every (configured)
  130. + * device backed loop pre-allocates this amount of RAM pages unless
  131. + * later 'lo_prealloc' numbers provide an override. 'lo_prealloc'
  132. + * overrides are defined in pairs: loop_index,number_of_pages
  133. + */
  134. +static int lo_prealloc[9] = { 125, 999, 0, 999, 0, 999, 0, 999, 0 };
  135. +#define LO_PREALLOC_MIN 4 /* minimum user defined pre-allocated RAM pages */
  136. +#define LO_PREALLOC_MAX 512 /* maximum user defined pre-allocated RAM pages */
  137. -static int compute_loop_size(struct loop_device *lo, struct dentry * lo_dentry, kdev_t lodev)
  138. -{
  139. - if (S_ISREG(lo_dentry->d_inode->i_mode))
  140. - return (lo_dentry->d_inode->i_size - lo->lo_offset) >> BLOCK_SIZE_BITS;
  141. - if (blk_size[MAJOR(lodev)])
  142. - return blk_size[MAJOR(lodev)][MINOR(lodev)] -
  143. - (lo->lo_offset >> BLOCK_SIZE_BITS);
  144. - return MAX_DISK_SIZE;
  145. -}
  146. +MODULE_PARM(lo_prealloc, "1-9i");
  147. +MODULE_PARM_DESC(lo_prealloc, "Number of pre-allocated pages [,index,pages]...");
  148. -static void figure_loop_size(struct loop_device *lo)
  149. -{
  150. - loop_sizes[lo->lo_number] = compute_loop_size(lo,
  151. - lo->lo_backing_file->f_dentry,
  152. - lo->lo_device);
  153. -}
  154. +/*
  155. + * This is loop helper thread nice value in range
  156. + * from 0 (low priority) to -20 (high priority).
  157. + */
  158. +#if defined(DEF_NICE) && defined(DEF_COUNTER)
  159. +static int lo_nice = -20; /* old scheduler default */
  160. +#else
  161. +static int lo_nice = -1; /* O(1) scheduler default */
  162. +#endif
  163. -static int lo_send(struct loop_device *lo, struct buffer_head *bh, int bsize,
  164. - loff_t pos)
  165. -{
  166. - struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  167. - struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
  168. - struct address_space_operations *aops = mapping->a_ops;
  169. - struct page *page;
  170. - char *kaddr, *data;
  171. - unsigned long index;
  172. - unsigned size, offset;
  173. - int len;
  174. +MODULE_PARM(lo_nice, "1i");
  175. +MODULE_PARM_DESC(lo_nice, "Loop thread scheduler nice (0 ... -20)");
  176. - down(&mapping->host->i_sem);
  177. - index = pos >> PAGE_CACHE_SHIFT;
  178. - offset = pos & (PAGE_CACHE_SIZE - 1);
  179. - len = bh->b_size;
  180. - data = bh->b_data;
  181. - while (len > 0) {
  182. - int IV = index * (PAGE_CACHE_SIZE/bsize) + offset/bsize;
  183. - int transfer_result;
  184. +typedef struct {
  185. + struct loop_device lo_orig;
  186. + struct buffer_head *lo_bh_que0;
  187. + struct buffer_head *lo_bh_que1;
  188. + struct buffer_head *lo_bh_que2;
  189. + struct buffer_head *lo_bh_free;
  190. + int lo_bh_flsh;
  191. + int lo_bh_need;
  192. + wait_queue_head_t lo_bh_wait;
  193. + loff_t lo_offset;
  194. + loff_t lo_sizelimit;
  195. + unsigned long lo_offs_sec;
  196. + unsigned long lo_iv_remove;
  197. + unsigned char lo_crypt_name[LO_NAME_SIZE];
  198. +} LoDevExt;
  199. +static LoDevExt *loop_dev;
  200. +
  201. +#define LDE_lo_bh_que0 (((LoDevExt *)lo)->lo_bh_que0)
  202. +#define LDE_lo_bh_que1 (((LoDevExt *)lo)->lo_bh_que1)
  203. +#define LDE_lo_bh_que2 (((LoDevExt *)lo)->lo_bh_que2)
  204. +#define LDE_lo_bh_free (((LoDevExt *)lo)->lo_bh_free)
  205. +#define LDE_lo_bh_flsh (((LoDevExt *)lo)->lo_bh_flsh)
  206. +#define LDE_lo_bh_need (((LoDevExt *)lo)->lo_bh_need)
  207. +#define LDE_lo_bh_wait (((LoDevExt *)lo)->lo_bh_wait)
  208. +#define LDE_lo_offset (((LoDevExt *)lo)->lo_offset)
  209. +#define LDE_lo_sizelimit (((LoDevExt *)lo)->lo_sizelimit)
  210. +#define LDE_lo_offs_sec (((LoDevExt *)lo)->lo_offs_sec)
  211. +#define LDE_lo_iv_remove (((LoDevExt *)lo)->lo_iv_remove)
  212. +#define LDE_lo_crypt_name (((LoDevExt *)lo)->lo_crypt_name)
  213. +
  214. +typedef struct {
  215. + struct buffer_head **q0;
  216. + struct buffer_head **q1;
  217. + struct buffer_head **q2;
  218. + int x0;
  219. + int x1;
  220. + int x2;
  221. +} que_look_up_table;
  222. - size = PAGE_CACHE_SIZE - offset;
  223. - if (size > len)
  224. - size = len;
  225. +static void loop_prealloc_cleanup(struct loop_device *lo)
  226. +{
  227. + struct buffer_head *bh;
  228. - page = grab_cache_page(mapping, index);
  229. - if (!page)
  230. - goto fail;
  231. - kaddr = kmap(page);
  232. - if (aops->prepare_write(file, page, offset, offset+size))
  233. - goto unlock;
  234. - flush_dcache_page(page);
  235. - transfer_result = lo_do_transfer(lo, WRITE, kaddr + offset, data, size, IV);
  236. - if (transfer_result) {
  237. - /*
  238. - * The transfer failed, but we still write the data to
  239. - * keep prepare/commit calls balanced.
  240. - */
  241. - printk(KERN_ERR "loop: transfer error block %ld\n", index);
  242. - memset(kaddr + offset, 0, size);
  243. - }
  244. - if (aops->commit_write(file, page, offset, offset+size))
  245. - goto unlock;
  246. - if (transfer_result)
  247. - goto unlock;
  248. - kunmap(page);
  249. - data += size;
  250. - len -= size;
  251. - offset = 0;
  252. - index++;
  253. - pos += size;
  254. - UnlockPage(page);
  255. - page_cache_release(page);
  256. + while ((bh = LDE_lo_bh_free)) {
  257. + __free_page(bh->b_page);
  258. + LDE_lo_bh_free = bh->b_reqnext;
  259. + bh->b_reqnext = NULL;
  260. + kmem_cache_free(bh_cachep, bh);
  261. }
  262. - up(&mapping->host->i_sem);
  263. - return 0;
  264. -
  265. -unlock:
  266. - kunmap(page);
  267. - UnlockPage(page);
  268. - page_cache_release(page);
  269. -fail:
  270. - up(&mapping->host->i_sem);
  271. - return -1;
  272. }
  273. -struct lo_read_data {
  274. - struct loop_device *lo;
  275. - char *data;
  276. - int bsize;
  277. -};
  278. -
  279. -static int lo_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
  280. +static int loop_prealloc_init(struct loop_device *lo, int y)
  281. {
  282. - char *kaddr;
  283. - unsigned long count = desc->count;
  284. - struct lo_read_data *p = (struct lo_read_data*)desc->buf;
  285. - struct loop_device *lo = p->lo;
  286. - int IV = page->index * (PAGE_CACHE_SIZE/p->bsize) + offset/p->bsize;
  287. + struct buffer_head *bh;
  288. + int x;
  289. - if (size > count)
  290. - size = count;
  291. + if(!y) {
  292. + y = lo_prealloc[0];
  293. + for (x = 1; x < (sizeof(lo_prealloc) / sizeof(int)); x += 2) {
  294. + if (lo_prealloc[x + 1] && (lo->lo_number == lo_prealloc[x])) {
  295. + y = lo_prealloc[x + 1];
  296. + break;
  297. + }
  298. + }
  299. + }
  300. + LDE_lo_bh_flsh = (y * 3) / 4;
  301. - kaddr = kmap(page);
  302. - if (lo_do_transfer(lo, READ, kaddr + offset, p->data, size, IV)) {
  303. - size = 0;
  304. - printk(KERN_ERR "loop: transfer error block %ld\n",page->index);
  305. - desc->error = -EINVAL;
  306. + for (x = 0; x < y; x++) {
  307. + bh = kmem_cache_alloc(bh_cachep, SLAB_KERNEL);
  308. + if (!bh) {
  309. + loop_prealloc_cleanup(lo);
  310. + return 1;
  311. + }
  312. + bh->b_page = alloc_page(GFP_KERNEL);
  313. + if (!bh->b_page) {
  314. + bh->b_reqnext = NULL;
  315. + kmem_cache_free(bh_cachep, bh);
  316. + loop_prealloc_cleanup(lo);
  317. + return 1;
  318. + }
  319. + bh->b_reqnext = LDE_lo_bh_free;
  320. + LDE_lo_bh_free = bh;
  321. }
  322. - kunmap(page);
  323. -
  324. - desc->count = count - size;
  325. - desc->written += size;
  326. - p->data += size;
  327. - return size;
  328. -}
  329. -
  330. -static int lo_receive(struct loop_device *lo, struct buffer_head *bh, int bsize,
  331. - loff_t pos)
  332. -{
  333. - struct lo_read_data cookie;
  334. - read_descriptor_t desc;
  335. - struct file *file;
  336. -
  337. - cookie.lo = lo;
  338. - cookie.data = bh->b_data;
  339. - cookie.bsize = bsize;
  340. - desc.written = 0;
  341. - desc.count = bh->b_size;
  342. - desc.buf = (char*)&cookie;
  343. - desc.error = 0;
  344. - spin_lock_irq(&lo->lo_lock);
  345. - file = lo->lo_backing_file;
  346. - spin_unlock_irq(&lo->lo_lock);
  347. - do_generic_file_read(file, &pos, &desc, lo_read_actor);
  348. - return desc.error;
  349. + return 0;
  350. }
  351. -static inline int loop_get_bs(struct loop_device *lo)
  352. +static void loop_add_queue_last(struct loop_device *lo, struct buffer_head *bh, struct buffer_head **q)
  353. {
  354. - int bs = 0;
  355. + unsigned long flags;
  356. - if (blksize_size[MAJOR(lo->lo_device)])
  357. - bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
  358. - if (!bs)
  359. - bs = BLOCK_SIZE;
  360. + spin_lock_irqsave(&lo->lo_lock, flags);
  361. + if (*q) {
  362. + bh->b_reqnext = (*q)->b_reqnext;
  363. + (*q)->b_reqnext = bh;
  364. + } else {
  365. + bh->b_reqnext = bh;
  366. + }
  367. + *q = bh;
  368. + spin_unlock_irqrestore(&lo->lo_lock, flags);
  369. - return bs;
  370. + if (waitqueue_active(&LDE_lo_bh_wait))
  371. + wake_up_interruptible(&LDE_lo_bh_wait);
  372. }
  373. -static inline unsigned long loop_get_iv(struct loop_device *lo,
  374. - unsigned long sector)
  375. +static void loop_add_queue_first(struct loop_device *lo, struct buffer_head *bh, struct buffer_head **q)
  376. {
  377. - int bs = loop_get_bs(lo);
  378. - unsigned long offset, IV;
  379. -
  380. - IV = sector / (bs >> 9) + lo->lo_offset / bs;
  381. - offset = ((sector % (bs >> 9)) << 9) + lo->lo_offset % bs;
  382. - if (offset >= bs)
  383. - IV++;
  384. -
  385. - return IV;
  386. + spin_lock_irq(&lo->lo_lock);
  387. + if (*q) {
  388. + bh->b_reqnext = (*q)->b_reqnext;
  389. + (*q)->b_reqnext = bh;
  390. + } else {
  391. + bh->b_reqnext = bh;
  392. + *q = bh;
  393. + }
  394. + spin_unlock_irq(&lo->lo_lock);
  395. }
  396. -static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw)
  397. +static struct buffer_head *loop_get_bh(struct loop_device *lo, int *list_nr,
  398. + que_look_up_table *qt)
  399. {
  400. - loff_t pos;
  401. - int ret;
  402. + struct buffer_head *bh = NULL, *last;
  403. - pos = ((loff_t) bh->b_rsector << 9) + lo->lo_offset;
  404. -
  405. - if (rw == WRITE)
  406. - ret = lo_send(lo, bh, loop_get_bs(lo), pos);
  407. - else
  408. - ret = lo_receive(lo, bh, loop_get_bs(lo), pos);
  409. -
  410. - return ret;
  411. -}
  412. -
  413. -static void loop_end_io_transfer(struct buffer_head *bh, int uptodate);
  414. -static void loop_put_buffer(struct buffer_head *bh)
  415. -{
  416. - /*
  417. - * check b_end_io, may just be a remapped bh and not an allocated one
  418. - */
  419. - if (bh && bh->b_end_io == loop_end_io_transfer) {
  420. - __free_page(bh->b_page);
  421. - kmem_cache_free(bh_cachep, bh);
  422. + spin_lock_irq(&lo->lo_lock);
  423. + if ((last = *qt->q0)) {
  424. + bh = last->b_reqnext;
  425. + if (bh == last)
  426. + *qt->q0 = NULL;
  427. + else
  428. + last->b_reqnext = bh->b_reqnext;
  429. + bh->b_reqnext = NULL;
  430. + *list_nr = qt->x0;
  431. + } else if ((last = *qt->q1)) {
  432. + bh = last->b_reqnext;
  433. + if (bh == last)
  434. + *qt->q1 = NULL;
  435. + else
  436. + last->b_reqnext = bh->b_reqnext;
  437. + bh->b_reqnext = NULL;
  438. + *list_nr = qt->x1;
  439. + } else if ((last = *qt->q2)) {
  440. + bh = last->b_reqnext;
  441. + if (bh == last)
  442. + *qt->q2 = NULL;
  443. + else
  444. + last->b_reqnext = bh->b_reqnext;
  445. + bh->b_reqnext = NULL;
  446. + *list_nr = qt->x2;
  447. }
  448. + spin_unlock_irq(&lo->lo_lock);
  449. + return bh;
  450. }
  451. -/*
  452. - * Add buffer_head to back of pending list
  453. - */
  454. -static void loop_add_bh(struct loop_device *lo, struct buffer_head *bh)
  455. +static void loop_put_buffer(struct loop_device *lo, struct buffer_head *b)
  456. {
  457. unsigned long flags;
  458. + int wk;
  459. spin_lock_irqsave(&lo->lo_lock, flags);
  460. - if (lo->lo_bhtail) {
  461. - lo->lo_bhtail->b_reqnext = bh;
  462. - lo->lo_bhtail = bh;
  463. - } else
  464. - lo->lo_bh = lo->lo_bhtail = bh;
  465. + b->b_reqnext = LDE_lo_bh_free;
  466. + LDE_lo_bh_free = b;
  467. + wk = LDE_lo_bh_need;
  468. spin_unlock_irqrestore(&lo->lo_lock, flags);
  469. - up(&lo->lo_bh_mutex);
  470. + if (wk && waitqueue_active(&LDE_lo_bh_wait))
  471. + wake_up_interruptible(&LDE_lo_bh_wait);
  472. }
  473. -/*
  474. - * Grab first pending buffer
  475. - */
  476. -static struct buffer_head *loop_get_bh(struct loop_device *lo)
  477. +static void loop_end_io_transfer_wr(struct buffer_head *bh, int uptodate)
  478. {
  479. - struct buffer_head *bh;
  480. -
  481. - spin_lock_irq(&lo->lo_lock);
  482. - if ((bh = lo->lo_bh)) {
  483. - if (bh == lo->lo_bhtail)
  484. - lo->lo_bhtail = NULL;
  485. - lo->lo_bh = bh->b_reqnext;
  486. - bh->b_reqnext = NULL;
  487. - }
  488. - spin_unlock_irq(&lo->lo_lock);
  489. + struct loop_device *lo = (struct loop_device *)(&loop_dev[MINOR(bh->b_dev)]);
  490. + struct buffer_head *rbh = bh->b_private;
  491. - return bh;
  492. + rbh->b_reqnext = NULL;
  493. + rbh->b_end_io(rbh, uptodate);
  494. + loop_put_buffer(lo, bh);
  495. + if (atomic_dec_and_test(&lo->lo_pending))
  496. + wake_up_interruptible(&LDE_lo_bh_wait);
  497. }
  498. -/*
  499. - * when buffer i/o has completed. if BH_Dirty is set, this was a WRITE
  500. - * and lo->transfer stuff has already been done. if not, it was a READ
  501. - * so queue it for the loop thread and let it do the transfer out of
  502. - * b_end_io context (we don't want to do decrypt of a page with irqs
  503. - * disabled)
  504. - */
  505. -static void loop_end_io_transfer(struct buffer_head *bh, int uptodate)
  506. +static void loop_end_io_transfer_rd(struct buffer_head *bh, int uptodate)
  507. {
  508. - struct loop_device *lo = &loop_dev[MINOR(bh->b_dev)];
  509. -
  510. - if (!uptodate || test_bit(BH_Dirty, &bh->b_state)) {
  511. - struct buffer_head *rbh = bh->b_private;
  512. + struct loop_device *lo = (struct loop_device *)(&loop_dev[MINOR(bh->b_dev)]);
  513. - rbh->b_end_io(rbh, uptodate);
  514. - if (atomic_dec_and_test(&lo->lo_pending))
  515. - up(&lo->lo_bh_mutex);
  516. - loop_put_buffer(bh);
  517. - } else
  518. - loop_add_bh(lo, bh);
  519. + if (!uptodate)
  520. + loop_end_io_transfer_wr(bh, uptodate);
  521. + else
  522. + loop_add_queue_last(lo, bh, &LDE_lo_bh_que0);
  523. }
  524. static struct buffer_head *loop_get_buffer(struct loop_device *lo,
  525. - struct buffer_head *rbh)
  526. + struct buffer_head *rbh, int from_thread, int rw)
  527. {
  528. struct buffer_head *bh;
  529. + struct page *p;
  530. + unsigned long flags;
  531. - /*
  532. - * for xfer_funcs that can operate on the same bh, do that
  533. - */
  534. - if (lo->lo_flags & LO_FLAGS_BH_REMAP) {
  535. - bh = rbh;
  536. - goto out_bh;
  537. + spin_lock_irqsave(&lo->lo_lock, flags);
  538. + bh = LDE_lo_bh_free;
  539. + if (bh) {
  540. + LDE_lo_bh_free = bh->b_reqnext;
  541. + if (from_thread)
  542. + LDE_lo_bh_need = 0;
  543. + } else {
  544. + if (from_thread)
  545. + LDE_lo_bh_need = 1;
  546. }
  547. + spin_unlock_irqrestore(&lo->lo_lock, flags);
  548. + if (!bh)
  549. + return (struct buffer_head *)0;
  550. - do {
  551. - bh = kmem_cache_alloc(bh_cachep, SLAB_NOIO);
  552. - if (bh)
  553. - break;
  554. -
  555. - run_task_queue(&tq_disk);
  556. - set_current_state(TASK_INTERRUPTIBLE);
  557. - schedule_timeout(HZ);
  558. - } while (1);
  559. - memset(bh, 0, sizeof(*bh));
  560. + p = bh->b_page;
  561. + memset(bh, 0, sizeof(struct buffer_head));
  562. + bh->b_page = p;
  563. + bh->b_private = rbh;
  564. bh->b_size = rbh->b_size;
  565. bh->b_dev = rbh->b_rdev;
  566. + bh->b_rdev = lo->lo_device;
  567. bh->b_state = (1 << BH_Req) | (1 << BH_Mapped) | (1 << BH_Lock);
  568. + bh->b_data = page_address(bh->b_page);
  569. + bh->b_end_io = (rw == WRITE) ? loop_end_io_transfer_wr : loop_end_io_transfer_rd;
  570. + bh->b_rsector = rbh->b_rsector + LDE_lo_offs_sec;
  571. + init_waitqueue_head(&bh->b_wait);
  572. +
  573. + return bh;
  574. +}
  575. +
  576. +static int figure_loop_size(struct loop_device *lo)
  577. +{
  578. + loff_t size, offs;
  579. + unsigned int x;
  580. + int err = 0;
  581. + kdev_t lodev = lo->lo_device;
  582. +
  583. + offs = LDE_lo_offset;
  584. + if (S_ISREG(lo->lo_backing_file->f_dentry->d_inode->i_mode)) {
  585. + size = lo->lo_backing_file->f_dentry->d_inode->i_size;
  586. + } else {
  587. + offs &= ~((loff_t)511);
  588. + if (blk_size[MAJOR(lodev)])
  589. + size = (loff_t)(blk_size[MAJOR(lodev)][MINOR(lodev)]) << BLOCK_SIZE_BITS;
  590. + else
  591. + size = 1024*1024*1024; /* unknown size */
  592. + }
  593. + if ((offs > 0) && (offs < size)) {
  594. + size -= offs;
  595. + } else {
  596. + if (offs)
  597. + err = -EINVAL;
  598. + LDE_lo_offset = 0;
  599. + LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
  600. + }
  601. + if ((LDE_lo_sizelimit > 0) && (LDE_lo_sizelimit <= size)) {
  602. + size = LDE_lo_sizelimit;
  603. + } else {
  604. + if (LDE_lo_sizelimit)
  605. + err = -EINVAL;
  606. + LDE_lo_sizelimit = 0;
  607. + }
  608. + size >>= BLOCK_SIZE_BITS;
  609. /*
  610. - * easy way out, although it does waste some memory for < PAGE_SIZE
  611. - * blocks... if highmem bounce buffering can get away with it,
  612. - * so can we :-)
  613. + * Unfortunately, if we want to do I/O on the device,
  614. + * the number of 1024-byte blocks has to fit into unsigned int
  615. */
  616. - do {
  617. - bh->b_page = alloc_page(GFP_NOIO);
  618. - if (bh->b_page)
  619. - break;
  620. + x = (unsigned int)size;
  621. + if ((loff_t)x != size) {
  622. + err = -EFBIG;
  623. + size = 0;
  624. + }
  625. - run_task_queue(&tq_disk);
  626. - set_current_state(TASK_INTERRUPTIBLE);
  627. - schedule_timeout(HZ);
  628. - } while (1);
  629. + loop_sizes[lo->lo_number] = size;
  630. + return err;
  631. +}
  632. - bh->b_data = page_address(bh->b_page);
  633. - bh->b_end_io = loop_end_io_transfer;
  634. - bh->b_private = rbh;
  635. - init_waitqueue_head(&bh->b_wait);
  636. +static inline int lo_do_Transfer(struct loop_device *lo, int cmd, char *rbuf,
  637. + char *lbuf, int size, int rblock)
  638. +{
  639. + if (!lo->transfer)
  640. + return 0;
  641. -out_bh:
  642. - bh->b_rsector = rbh->b_rsector + (lo->lo_offset >> 9);
  643. - spin_lock_irq(&lo->lo_lock);
  644. - bh->b_rdev = lo->lo_device;
  645. - spin_unlock_irq(&lo->lo_lock);
  646. + /* this ugly cast is needed to work around (possible) kmap damage in function prototype */
  647. + /* should be: return lo->transfer(lo, cmd, rbuf, lbuf, size, rblock); */
  648. + return ((int (*)(struct loop_device *, int, char *, char *, int, int))lo->transfer)(lo, cmd, rbuf, lbuf, size, rblock);
  649. +}
  650. - return bh;
  651. +static int loop_file_io(struct file *file, char *buf, int size, loff_t *ppos, int w)
  652. +{
  653. + mm_segment_t fs;
  654. + int x, y, z;
  655. +
  656. + y = 0;
  657. + do {
  658. + z = size - y;
  659. + fs = get_fs();
  660. + set_fs(get_ds());
  661. + if (w) {
  662. + x = file->f_op->write(file, buf + y, z, ppos);
  663. + set_fs(fs);
  664. + } else {
  665. + x = file->f_op->read(file, buf + y, z, ppos);
  666. + set_fs(fs);
  667. + if (!x)
  668. + return 1;
  669. + }
  670. + if (x < 0) {
  671. + if ((x == -EAGAIN) || (x == -ENOMEM) || (x == -ERESTART) || (x == -EINTR)) {
  672. + run_task_queue(&tq_disk);
  673. + set_current_state(TASK_INTERRUPTIBLE);
  674. + schedule_timeout(HZ / 2);
  675. + continue;
  676. + }
  677. + return 1;
  678. + }
  679. + y += x;
  680. + } while (y < size);
  681. + return 0;
  682. +}
  683. +
  684. +static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw)
  685. +{
  686. + loff_t pos;
  687. + struct file *file = lo->lo_backing_file;
  688. + char *data, *buf;
  689. + unsigned int size, len;
  690. + unsigned long IV;
  691. +
  692. + pos = ((loff_t) bh->b_rsector << 9) + LDE_lo_offset;
  693. + buf = page_address(LDE_lo_bh_free->b_page);
  694. + len = bh->b_size;
  695. + data = bh_kmap(bh);
  696. + IV = bh->b_rsector;
  697. + if (!LDE_lo_iv_remove)
  698. + IV += LDE_lo_offs_sec;
  699. + while (len > 0) {
  700. + if (lo->lo_encrypt_type == LO_CRYPT_NONE) {
  701. + /* this code relies that NONE transfer is a no-op */
  702. + buf = data;
  703. + }
  704. + size = PAGE_SIZE;
  705. + if (size > len)
  706. + size = len;
  707. + if (rw == WRITE) {
  708. + if (lo_do_Transfer(lo, WRITE, buf, data, size, IV)) {
  709. + printk(KERN_ERR "loop%d: write transfer error, sector %lu\n", lo->lo_number, IV);
  710. + goto kunmap_and_out;
  711. + }
  712. + if (loop_file_io(file, buf, size, &pos, 1)) {
  713. + printk(KERN_ERR "loop%d: write i/o error, sector %lu\n", lo->lo_number, IV);
  714. + goto kunmap_and_out;
  715. + }
  716. + } else {
  717. + if (loop_file_io(file, buf, size, &pos, 0)) {
  718. + printk(KERN_ERR "loop%d: read i/o error, sector %lu\n", lo->lo_number, IV);
  719. + goto kunmap_and_out;
  720. + }
  721. + if (lo_do_Transfer(lo, READ, buf, data, size, IV)) {
  722. + printk(KERN_ERR "loop%d: read transfer error, sector %lu\n", lo->lo_number, IV);
  723. + goto kunmap_and_out;
  724. + }
  725. + }
  726. + data += size;
  727. + len -= size;
  728. + IV += size >> 9;
  729. + }
  730. + bh_kunmap(bh);
  731. + return 0;
  732. +
  733. +kunmap_and_out:
  734. + bh_kunmap(bh);
  735. + return 1;
  736. }
  737. static int loop_make_request(request_queue_t *q, int rw, struct buffer_head *rbh)
  738. {
  739. - struct buffer_head *bh = NULL;
  740. + struct buffer_head *bh;
  741. struct loop_device *lo;
  742. - unsigned long IV;
  743. + char *md;
  744. + set_current_state(TASK_RUNNING);
  745. if (!buffer_locked(rbh))
  746. BUG();
  747. if (MINOR(rbh->b_rdev) >= max_loop)
  748. goto out;
  749. - lo = &loop_dev[MINOR(rbh->b_rdev)];
  750. + lo = (struct loop_device *)(&loop_dev[MINOR(rbh->b_rdev)]);
  751. spin_lock_irq(&lo->lo_lock);
  752. if (lo->lo_state != Lo_bound)
  753. goto inactive;
  754. @@ -483,45 +610,55 @@ static int loop_make_request(request_que
  755. } else if (rw == READA) {
  756. rw = READ;
  757. } else if (rw != READ) {
  758. - printk(KERN_ERR "loop: unknown command (%d)\n", rw);
  759. + printk(KERN_ERR "loop%d: unknown command (%d)\n", lo->lo_number, rw);
  760. goto err;
  761. }
  762. - rbh = blk_queue_bounce(q, rw, rbh);
  763. -
  764. /*
  765. * file backed, queue for loop_thread to handle
  766. */
  767. if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  768. - /*
  769. - * rbh locked at this point, noone else should clear
  770. - * the dirty flag
  771. - */
  772. - if (rw == WRITE)
  773. - set_bit(BH_Dirty, &rbh->b_state);
  774. - loop_add_bh(lo, rbh);
  775. + loop_add_queue_last(lo, rbh, (rw == WRITE) ? &LDE_lo_bh_que1 : &LDE_lo_bh_que0);
  776. return 0;
  777. }
  778. /*
  779. - * piggy old buffer on original, and submit for I/O
  780. + * device backed, just remap rdev & rsector for NONE transfer
  781. */
  782. - bh = loop_get_buffer(lo, rbh);
  783. - IV = loop_get_iv(lo, rbh->b_rsector);
  784. + if (lo->lo_encrypt_type == LO_CRYPT_NONE) {
  785. + rbh->b_rsector += LDE_lo_offs_sec;
  786. + rbh->b_rdev = lo->lo_device;
  787. + generic_make_request(rw, rbh);
  788. + if (atomic_dec_and_test(&lo->lo_pending))
  789. + wake_up_interruptible(&LDE_lo_bh_wait);
  790. + return 0;
  791. + }
  792. +
  793. + /*
  794. + * device backed, start reads and writes now if buffer available
  795. + */
  796. + bh = loop_get_buffer(lo, rbh, 0, rw);
  797. + if (!bh) {
  798. + /* just queue request and let thread handle alloc later */
  799. + loop_add_queue_last(lo, rbh, (rw == WRITE) ? &LDE_lo_bh_que1 : &LDE_lo_bh_que2);
  800. + return 0;
  801. + }
  802. if (rw == WRITE) {
  803. - set_bit(BH_Dirty, &bh->b_state);
  804. - if (lo_do_transfer(lo, WRITE, bh->b_data, rbh->b_data,
  805. - bh->b_size, IV))
  806. + int trv;
  807. + md = bh_kmap(rbh);
  808. + trv = lo_do_Transfer(lo, WRITE, bh->b_data, md, bh->b_size, bh->b_rsector - LDE_lo_iv_remove);
  809. + bh_kunmap(rbh);
  810. + if (trv) {
  811. + loop_put_buffer(lo, bh);
  812. goto err;
  813. + }
  814. }
  815. -
  816. generic_make_request(rw, bh);
  817. return 0;
  818. err:
  819. if (atomic_dec_and_test(&lo->lo_pending))
  820. - up(&lo->lo_bh_mutex);
  821. - loop_put_buffer(bh);
  822. + wake_up_interruptible(&LDE_lo_bh_wait);
  823. out:
  824. buffer_IO_error(rbh);
  825. return 0;
  826. @@ -530,30 +667,6 @@ inactive:
  827. goto out;
  828. }
  829. -static inline void loop_handle_bh(struct loop_device *lo,struct buffer_head *bh)
  830. -{
  831. - int ret;
  832. -
  833. - /*
  834. - * For block backed loop, we know this is a READ
  835. - */
  836. - if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  837. - int rw = !!test_and_clear_bit(BH_Dirty, &bh->b_state);
  838. -
  839. - ret = do_bh_filebacked(lo, bh, rw);
  840. - bh->b_end_io(bh, !ret);
  841. - } else {
  842. - struct buffer_head *rbh = bh->b_private;
  843. - unsigned long IV = loop_get_iv(lo, rbh->b_rsector);
  844. -
  845. - ret = lo_do_transfer(lo, READ, bh->b_data, rbh->b_data,
  846. - bh->b_size, IV);
  847. -
  848. - rbh->b_end_io(rbh, !ret);
  849. - loop_put_buffer(bh);
  850. - }
  851. -}
  852. -
  853. /*
  854. * worker thread that handles reads/writes to file backed loop devices,
  855. * to avoid blocking in our make_request_fn. it also does loop decrypting
  856. @@ -563,25 +676,71 @@ static inline void loop_handle_bh(struct
  857. static int loop_thread(void *data)
  858. {
  859. struct loop_device *lo = data;
  860. - struct buffer_head *bh;
  861. + struct buffer_head *bh, *xbh;
  862. + int x, rw, qi = 0, flushcnt = 0;
  863. + wait_queue_t waitq;
  864. + que_look_up_table qt[4] = {
  865. + { &LDE_lo_bh_que0, &LDE_lo_bh_que1, &LDE_lo_bh_que2, 0, 1, 2 },
  866. + { &LDE_lo_bh_que2, &LDE_lo_bh_que0, &LDE_lo_bh_que1, 2, 0, 1 },
  867. + { &LDE_lo_bh_que0, &LDE_lo_bh_que2, &LDE_lo_bh_que1, 0, 2, 1 },
  868. + { &LDE_lo_bh_que1, &LDE_lo_bh_que0, &LDE_lo_bh_que2, 1, 0, 2 }
  869. + };
  870. + char *md;
  871. + static const struct rlimit loop_rlim_defaults[RLIM_NLIMITS] = INIT_RLIMITS;
  872. + init_waitqueue_entry(&waitq, current);
  873. + memcpy(&current->rlim[0], &loop_rlim_defaults[0], sizeof(current->rlim));
  874. daemonize();
  875. exit_files(current);
  876. +#if !defined(NO_REPARENT_TO_INIT)
  877. reparent_to_init();
  878. +#endif
  879. sprintf(current->comm, "loop%d", lo->lo_number);
  880. +#if !defined(NO_TASK_STRUCT_SIGMASK_LOCK)
  881. spin_lock_irq(&current->sigmask_lock);
  882. +#elif NO_TASK_STRUCT_SIGMASK_LOCK == 1
  883. + spin_lock_irq(&current->sighand->siglock);
  884. +#else
  885. + spin_lock_irq(&current->sig->siglock);
  886. +#endif
  887. sigfillset(&current->blocked);
  888. flush_signals(current);
  889. +#if !defined(NO_TASK_STRUCT_SIGMASK_LOCK)
  890. spin_unlock_irq(&current->sigmask_lock);
  891. +#elif NO_TASK_STRUCT_SIGMASK_LOCK == 1
  892. + spin_unlock_irq(&current->sighand->siglock);
  893. +#else
  894. + spin_unlock_irq(&current->sig->siglock);
  895. +#endif
  896. +
  897. + if (lo_nice > 0)
  898. + lo_nice = 0;
  899. + if (lo_nice < -20)
  900. + lo_nice = -20;
  901. +#if defined(DEF_NICE) && defined(DEF_COUNTER)
  902. + /* old scheduler syntax */
  903. + current->policy = SCHED_OTHER;
  904. + current->nice = lo_nice;
  905. +#else
  906. + /* O(1) scheduler syntax */
  907. + set_user_nice(current, lo_nice);
  908. +#endif
  909. spin_lock_irq(&lo->lo_lock);
  910. lo->lo_state = Lo_bound;
  911. atomic_inc(&lo->lo_pending);
  912. spin_unlock_irq(&lo->lo_lock);
  913. +#if defined(PF_NOIO)
  914. current->flags |= PF_NOIO;
  915. +#endif
  916. +#if defined(PF_NOFREEZE)
  917. + current->flags |= PF_NOFREEZE;
  918. +#elif defined(PF_IOTHREAD)
  919. + current->flags |= PF_IOTHREAD;
  920. +#endif
  921. /*
  922. * up sem, we are running
  923. @@ -589,23 +748,110 @@ static int loop_thread(void *data)
  924. up(&lo->lo_sem);
  925. for (;;) {
  926. - down_interruptible(&lo->lo_bh_mutex);
  927. + add_wait_queue(&LDE_lo_bh_wait, &waitq);
  928. + for (;;) {
  929. + set_current_state(TASK_INTERRUPTIBLE);
  930. + if (!atomic_read(&lo->lo_pending))
  931. + break;
  932. +
  933. + x = 0;
  934. + spin_lock_irq(&lo->lo_lock);
  935. + if (LDE_lo_bh_que0) {
  936. + x = 1;
  937. + } else if (LDE_lo_bh_que1 || LDE_lo_bh_que2) {
  938. + /* file backed works too because LDE_lo_bh_need == 0 */
  939. + if (LDE_lo_bh_free || !LDE_lo_bh_need)
  940. + x = 1;
  941. + }
  942. + spin_unlock_irq(&lo->lo_lock);
  943. + if (x)
  944. + break;
  945. +
  946. + schedule();
  947. + }
  948. + set_current_state(TASK_RUNNING);
  949. + remove_wait_queue(&LDE_lo_bh_wait, &waitq);
  950. +
  951. /*
  952. - * could be upped because of tear-down, not because of
  953. + * could be woken because of tear-down, not because of
  954. * pending work
  955. */
  956. if (!atomic_read(&lo->lo_pending))
  957. break;
  958. - bh = loop_get_bh(lo);
  959. - if (!bh) {
  960. - printk("loop: missing bh\n");
  961. + /*
  962. + * read queues using alternating order to prevent starvation
  963. + */
  964. + bh = loop_get_bh(lo, &x, &qt[++qi & 3]);
  965. + if (!bh)
  966. + continue;
  967. +
  968. + /*
  969. + * x list tag usage(buffer-allocated)
  970. + * --- -------------- -----------------------
  971. + * 0 LDE_lo_bh_que0 dev-read(y) / file-read
  972. + * 1 LDE_lo_bh_que1 dev-write(n) / file-write
  973. + * 2 LDE_lo_bh_que2 dev-read(n)
  974. + */
  975. + rw = (x == 1) ? WRITE : READ;
  976. + if ((x >= 1) && !(lo->lo_flags & LO_FLAGS_DO_BMAP)) {
  977. + /* loop_make_request didn't allocate a buffer, do that now */
  978. + xbh = loop_get_buffer(lo, bh, 1, rw);
  979. + if (!xbh) {
  980. + run_task_queue(&tq_disk);
  981. + flushcnt = 0;
  982. + loop_add_queue_first(lo, bh, (rw == WRITE) ? &LDE_lo_bh_que1 : &LDE_lo_bh_que2);
  983. + /* LDE_lo_bh_need should be 1 now, go back to sleep */
  984. + continue;
  985. + }
  986. + if (rw == WRITE) {
  987. + int trv;
  988. + md = bh_kmap(bh);
  989. + trv = lo_do_Transfer(lo, WRITE, xbh->b_data, md, xbh->b_size, xbh->b_rsector - LDE_lo_iv_remove);
  990. + bh_kunmap(bh);
  991. + if (trv) {
  992. + loop_put_buffer(lo, xbh);
  993. + buffer_IO_error(bh);
  994. + atomic_dec(&lo->lo_pending);
  995. + continue;
  996. + }
  997. + }
  998. + generic_make_request(rw, xbh);
  999. +
  1000. + /* start I/O if there are no more requests lacking buffers */
  1001. + x = 0;
  1002. + spin_lock_irq(&lo->lo_lock);
  1003. + if (!LDE_lo_bh_que1 && !LDE_lo_bh_que2)
  1004. + x = 1;
  1005. + spin_unlock_irq(&lo->lo_lock);
  1006. + if (x || (++flushcnt >= LDE_lo_bh_flsh)) {
  1007. + run_task_queue(&tq_disk);
  1008. + flushcnt = 0;
  1009. + }
  1010. +
  1011. + /* request not completely processed yet */
  1012. continue;
  1013. }
  1014. - loop_handle_bh(lo, bh);
  1015. + if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1016. + /* request is for file backed device */
  1017. + x = do_bh_filebacked(lo, bh, rw);
  1018. + bh->b_reqnext = NULL;
  1019. + bh->b_end_io(bh, !x);
  1020. + } else {
  1021. + /* device backed read has completed, do decrypt now */
  1022. + xbh = bh->b_private;
  1023. + /* must not use bh->b_rsector as IV, as it may be modified by LVM at this point */
  1024. + /* instead, recompute IV from original request */
  1025. + md = bh_kmap(xbh);
  1026. + x = lo_do_Transfer(lo, READ, bh->b_data, md, bh->b_size, xbh->b_rsector + LDE_lo_offs_sec - LDE_lo_iv_remove);
  1027. + bh_kunmap(xbh);
  1028. + xbh->b_reqnext = NULL;
  1029. + xbh->b_end_io(xbh, !x);
  1030. + loop_put_buffer(lo, bh);
  1031. + }
  1032. /*
  1033. - * upped both for pending work and tear-down, lo_pending
  1034. + * woken both for pending work and tear-down, lo_pending
  1035. * will hit zero then
  1036. */
  1037. if (atomic_dec_and_test(&lo->lo_pending))
  1038. @@ -616,15 +862,34 @@ static int loop_thread(void *data)
  1039. return 0;
  1040. }
  1041. +static void loop_set_softblksz(struct loop_device *lo, kdev_t dev)
  1042. +{
  1043. + int bs = 0, x;
  1044. +
  1045. + if (blksize_size[MAJOR(lo->lo_device)])
  1046. + bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
  1047. + if (!bs)
  1048. + bs = BLOCK_SIZE;
  1049. + if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1050. + x = loop_sizes[lo->lo_number];
  1051. + if ((bs == 8192) && (x & 7))
  1052. + bs = 4096;
  1053. + if ((bs == 4096) && (x & 3))
  1054. + bs = 2048;
  1055. + if ((bs == 2048) && (x & 1))
  1056. + bs = 1024;
  1057. + }
  1058. + set_blocksize(dev, bs);
  1059. +}
  1060. +
  1061. static int loop_set_fd(struct loop_device *lo, struct file *lo_file, kdev_t dev,
  1062. unsigned int arg)
  1063. {
  1064. struct file *file;
  1065. struct inode *inode;
  1066. kdev_t lo_device;
  1067. - int lo_flags = 0;
  1068. + int lo_flags = 0, hardsz = 512;
  1069. int error;
  1070. - int bs;
  1071. MOD_INC_USE_COUNT;
  1072. @@ -643,33 +908,46 @@ static int loop_set_fd(struct loop_devic
  1073. if (!(file->f_mode & FMODE_WRITE))
  1074. lo_flags |= LO_FLAGS_READ_ONLY;
  1075. + LDE_lo_offset = LDE_lo_sizelimit = 0;
  1076. + LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
  1077. + LDE_lo_bh_free = LDE_lo_bh_que2 = LDE_lo_bh_que1 = LDE_lo_bh_que0 = NULL;
  1078. + LDE_lo_bh_need = LDE_lo_bh_flsh = 0;
  1079. + init_waitqueue_head(&LDE_lo_bh_wait);
  1080. if (S_ISBLK(inode->i_mode)) {
  1081. lo_device = inode->i_rdev;
  1082. if (lo_device == dev) {
  1083. error = -EBUSY;
  1084. goto out_putf;
  1085. }
  1086. + if (loop_prealloc_init(lo, 0)) {
  1087. + error = -ENOMEM;
  1088. + goto out_putf;
  1089. + }
  1090. + hardsz = get_hardsect_size(lo_device);
  1091. } else if (S_ISREG(inode->i_mode)) {
  1092. - struct address_space_operations *aops = inode->i_mapping->a_ops;
  1093. /*
  1094. * If we can't read - sorry. If we only can't write - well,
  1095. * it's going to be read-only.
  1096. */
  1097. - if (!aops->readpage)
  1098. + if (!file->f_op || !file->f_op->read)
  1099. goto out_putf;
  1100. - if (!aops->prepare_write || !aops->commit_write)
  1101. + if (!file->f_op->write)
  1102. lo_flags |= LO_FLAGS_READ_ONLY;
  1103. lo_device = inode->i_dev;
  1104. lo_flags |= LO_FLAGS_DO_BMAP;
  1105. + if (loop_prealloc_init(lo, 1)) {
  1106. + error = -ENOMEM;
  1107. + goto out_putf;
  1108. + }
  1109. error = 0;
  1110. } else
  1111. goto out_putf;
  1112. get_file(file);
  1113. - if (IS_RDONLY (inode) || is_read_only(lo_device)
  1114. + if ((S_ISREG(inode->i_mode) && IS_RDONLY(inode)) || is_read_only(lo_device)
  1115. || !(lo_file->f_mode & FMODE_WRITE))
  1116. lo_flags |= LO_FLAGS_READ_ONLY;
  1117. @@ -677,28 +955,40 @@ static int loop_set_fd(struct loop_devic
  1118. lo->lo_device = lo_device;
  1119. lo->lo_flags = lo_flags;
  1120. + if(lo_flags & LO_FLAGS_READ_ONLY)
  1121. + lo->lo_flags |= 0x200000; /* export to user space */
  1122. lo->lo_backing_file = file;
  1123. lo->transfer = NULL;
  1124. lo->ioctl = NULL;
  1125. - figure_loop_size(lo);
  1126. - lo->old_gfp_mask = inode->i_mapping->gfp_mask;
  1127. - inode->i_mapping->gfp_mask &= ~(__GFP_IO|__GFP_FS);
  1128. -
  1129. - bs = 0;
  1130. - if (blksize_size[MAJOR(lo_device)])
  1131. - bs = blksize_size[MAJOR(lo_device)][MINOR(lo_device)];
  1132. - if (!bs)
  1133. - bs = BLOCK_SIZE;
  1134. + if (figure_loop_size(lo)) {
  1135. + error = -EFBIG;
  1136. + goto out_cleanup;
  1137. + }
  1138. - set_blocksize(dev, bs);
  1139. + if (lo_flags & LO_FLAGS_DO_BMAP) {
  1140. + lo->old_gfp_mask = inode->i_mapping->gfp_mask;
  1141. + inode->i_mapping->gfp_mask &= ~(__GFP_IO|__GFP_FS);
  1142. + inode->i_mapping->gfp_mask |= __GFP_HIGH;
  1143. + } else {
  1144. + lo->old_gfp_mask = -1;
  1145. + }
  1146. - lo->lo_bh = lo->lo_bhtail = NULL;
  1147. - kernel_thread(loop_thread, lo, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  1148. - down(&lo->lo_sem);
  1149. + loop_hardsizes[MINOR(dev)] = hardsz;
  1150. + loop_set_softblksz(lo, dev);
  1151. + error = kernel_thread(loop_thread, lo, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  1152. + if(error < 0)
  1153. + goto out_mapping;
  1154. + down(&lo->lo_sem);
  1155. fput(file);
  1156. return 0;
  1157. + out_mapping:
  1158. + if(lo->old_gfp_mask != -1)
  1159. + inode->i_mapping->gfp_mask = lo->old_gfp_mask;
  1160. + out_cleanup:
  1161. + loop_prealloc_cleanup(lo);
  1162. + fput(file);
  1163. out_putf:
  1164. fput(file);
  1165. out:
  1166. @@ -711,6 +1001,7 @@ static int loop_release_xfer(struct loop
  1167. int err = 0;
  1168. if (lo->lo_encrypt_type) {
  1169. struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type];
  1170. + lo->transfer = NULL;
  1171. if (xfer && xfer->release)
  1172. err = xfer->release(lo);
  1173. if (xfer && xfer->unlock)
  1174. @@ -736,7 +1027,11 @@ static int loop_init_xfer(struct loop_de
  1175. return err;
  1176. }
  1177. +#if LINUX_VERSION_CODE >= 0x2040C
  1178. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  1179. +#else
  1180. +static int loop_clr_fd(struct loop_device *lo, kdev_t dev)
  1181. +#endif
  1182. {
  1183. struct file *filp = lo->lo_backing_file;
  1184. int gfp = lo->old_gfp_mask;
  1185. @@ -751,11 +1046,12 @@ static int loop_clr_fd(struct loop_devic
  1186. spin_lock_irq(&lo->lo_lock);
  1187. lo->lo_state = Lo_rundown;
  1188. if (atomic_dec_and_test(&lo->lo_pending))
  1189. - up(&lo->lo_bh_mutex);
  1190. + wake_up_interruptible(&LDE_lo_bh_wait);
  1191. spin_unlock_irq(&lo->lo_lock);
  1192. down(&lo->lo_sem);
  1193. + loop_prealloc_cleanup(lo);
  1194. lo->lo_backing_file = NULL;
  1195. loop_release_xfer(lo);
  1196. @@ -763,23 +1059,81 @@ static int loop_clr_fd(struct loop_devic
  1197. lo->ioctl = NULL;
  1198. lo->lo_device = 0;
  1199. lo->lo_encrypt_type = 0;
  1200. - lo->lo_offset = 0;
  1201. + LDE_lo_offset = LDE_lo_sizelimit = 0;
  1202. + LDE_lo_offs_sec = LDE_lo_iv_remove = 0;
  1203. lo->lo_encrypt_key_size = 0;
  1204. lo->lo_flags = 0;
  1205. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  1206. memset(lo->lo_name, 0, LO_NAME_SIZE);
  1207. + memset(LDE_lo_crypt_name, 0, LO_NAME_SIZE);
  1208. loop_sizes[lo->lo_number] = 0;
  1209. +#if LINUX_VERSION_CODE >= 0x2040C
  1210. invalidate_bdev(bdev, 0);
  1211. - filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
  1212. +#else
  1213. + invalidate_buffers(dev);
  1214. +#endif
  1215. + if (gfp != -1)
  1216. + filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
  1217. lo->lo_state = Lo_unbound;
  1218. fput(filp);
  1219. MOD_DEC_USE_COUNT;
  1220. return 0;
  1221. }
  1222. -static int loop_set_status(struct loop_device *lo, struct loop_info *arg)
  1223. +static void
  1224. +loop_info64_from_old(const struct loop_info *info, struct loopinfo64 *info64)
  1225. +{
  1226. + memset(info64, 0, sizeof(*info64));
  1227. + info64->lo_number = info->lo_number;
  1228. + info64->lo_device = info->lo_device;
  1229. + info64->lo_inode = info->lo_inode;
  1230. + info64->lo_rdevice = info->lo_rdevice;
  1231. + info64->lo_offset = info->lo_offset;
  1232. + info64->lo_encrypt_type = info->lo_encrypt_type;
  1233. + info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1234. + info64->lo_flags = info->lo_flags;
  1235. + info64->lo_init[0] = info->lo_init[0];
  1236. + info64->lo_init[1] = info->lo_init[1];
  1237. + if (info->lo_encrypt_type == 18) /* LO_CRYPT_CRYPTOAPI */
  1238. + memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1239. + else
  1240. + memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1241. + memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1242. +}
  1243. +
  1244. +static int
  1245. +loop_info64_to_old(struct loopinfo64 *info64, struct loop_info *info)
  1246. +{
  1247. + memset(info, 0, sizeof(*info));
  1248. + info->lo_number = info64->lo_number;
  1249. + info->lo_device = info64->lo_device;
  1250. + info->lo_inode = info64->lo_inode;
  1251. + info->lo_rdevice = info64->lo_rdevice;
  1252. + info->lo_offset = info64->lo_offset;
  1253. + info->lo_encrypt_type = info64->lo_encrypt_type;
  1254. + info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1255. + info->lo_flags = info64->lo_flags;
  1256. + info->lo_init[0] = info64->lo_init[0];
  1257. + info->lo_init[1] = info64->lo_init[1];
  1258. + if (info->lo_encrypt_type == 18) /* LO_CRYPT_CRYPTOAPI */
  1259. + memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1260. + else
  1261. + memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1262. + memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1263. +
  1264. + /* error in case values were truncated */
  1265. + if (info->lo_device != info64->lo_device ||
  1266. + info->lo_rdevice != info64->lo_rdevice ||
  1267. + info->lo_inode != info64->lo_inode ||
  1268. + info->lo_offset != info64->lo_offset ||
  1269. + info64->lo_sizelimit)
  1270. + return -EOVERFLOW;
  1271. +
  1272. + return 0;
  1273. +}
  1274. +
  1275. +static int loop_set_status(struct loop_device *lo, kdev_t dev, struct loopinfo64 *info, struct loop_info *oldinfo)
  1276. {
  1277. - struct loop_info info;
  1278. int err;
  1279. unsigned int type;
  1280. @@ -788,62 +1142,137 @@ static int loop_set_status(struct loop_d
  1281. return -EPERM;
  1282. if (lo->lo_state != Lo_bound)
  1283. return -ENXIO;
  1284. - if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1285. - return -EFAULT;
  1286. - if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE)
  1287. + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  1288. return -EINVAL;
  1289. - type = info.lo_encrypt_type;
  1290. + type = info->lo_encrypt_type;
  1291. if (type >= MAX_LO_CRYPT || xfer_funcs[type] == NULL)
  1292. return -EINVAL;
  1293. - if (type == LO_CRYPT_XOR && info.lo_encrypt_key_size == 0)
  1294. + if (type == LO_CRYPT_XOR && info->lo_encrypt_key_size == 0)
  1295. return -EINVAL;
  1296. err = loop_release_xfer(lo);
  1297. - if (!err)
  1298. - err = loop_init_xfer(lo, type, &info);
  1299. if (err)
  1300. return err;
  1301. - lo->lo_offset = info.lo_offset;
  1302. - strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE);
  1303. + if ((loff_t)info->lo_offset < 0) {
  1304. + /* negative offset == remove offset from IV computations */
  1305. + LDE_lo_offset = -(info->lo_offset);
  1306. + LDE_lo_iv_remove = LDE_lo_offset >> 9;
  1307. + } else {
  1308. + /* positive offset == include offset in IV computations */
  1309. + LDE_lo_offset = info->lo_offset;
  1310. + LDE_lo_iv_remove = 0;
  1311. + }
  1312. + LDE_lo_offs_sec = LDE_lo_offset >> 9;
  1313. + LDE_lo_sizelimit = info->lo_sizelimit;
  1314. + err = figure_loop_size(lo);
  1315. + if (err)
  1316. + return err;
  1317. + loop_set_softblksz(lo, dev);
  1318. +
  1319. + /* transfer init function for 2.4 kernels takes old style struct */
  1320. + err = loop_init_xfer(lo, type, oldinfo);
  1321. + /* copy key -- just in case transfer init func modified it */
  1322. + memcpy(info->lo_encrypt_key, oldinfo->lo_encrypt_key, sizeof(info->lo_encrypt_key));
  1323. + if (err)
  1324. + return err;
  1325. + strncpy(lo->lo_name, info->lo_file_name, LO_NAME_SIZE);
  1326. + strncpy(LDE_lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  1327. lo->transfer = xfer_funcs[type]->transfer;
  1328. lo->ioctl = xfer_funcs[type]->ioctl;
  1329. - lo->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1330. - lo->lo_init[0] = info.lo_init[0];
  1331. - lo->lo_init[1] = info.lo_init[1];
  1332. - if (info.lo_encrypt_key_size) {
  1333. - memcpy(lo->lo_encrypt_key, info.lo_encrypt_key,
  1334. - info.lo_encrypt_key_size);
  1335. + lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1336. + lo->lo_init[0] = info->lo_init[0];
  1337. + lo->lo_init[1] = info->lo_init[1];
  1338. + if (info->lo_encrypt_key_size) {
  1339. + memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  1340. + info->lo_encrypt_key_size);
  1341. lo->lo_key_owner = current->uid;
  1342. - }
  1343. - figure_loop_size(lo);
  1344. + }
  1345. +
  1346. return 0;
  1347. }
  1348. -static int loop_get_status(struct loop_device *lo, struct loop_info *arg)
  1349. +static int loop_get_status(struct loop_device *lo, struct loopinfo64 *info)
  1350. {
  1351. - struct loop_info info;
  1352. struct file *file = lo->lo_backing_file;
  1353. if (lo->lo_state != Lo_bound)
  1354. return -ENXIO;
  1355. - if (!arg)
  1356. - return -EINVAL;
  1357. - memset(&info, 0, sizeof(info));
  1358. - info.lo_number = lo->lo_number;
  1359. - info.lo_device = kdev_t_to_nr(file->f_dentry->d_inode->i_dev);
  1360. - info.lo_inode = file->f_dentry->d_inode->i_ino;
  1361. - info.lo_rdevice = kdev_t_to_nr(lo->lo_device);
  1362. - info.lo_offset = lo->lo_offset;
  1363. - info.lo_flags = lo->lo_flags;
  1364. - strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE);
  1365. - info.lo_encrypt_type = lo->lo_encrypt_type;
  1366. + memset(info, 0, sizeof(*info));
  1367. + info->lo_number = lo->lo_number;
  1368. + info->lo_device = kdev_t_to_nr(file->f_dentry->d_inode->i_dev);
  1369. + info->lo_inode = file->f_dentry->d_inode->i_ino;
  1370. + info->lo_rdevice = kdev_t_to_nr(lo->lo_device);
  1371. + info->lo_offset = LDE_lo_iv_remove ? -(LDE_lo_offset) : LDE_lo_offset;
  1372. + info->lo_sizelimit = LDE_lo_sizelimit;
  1373. + info->lo_flags = lo->lo_flags;
  1374. + strncpy(info->lo_file_name, lo->lo_name, LO_NAME_SIZE);
  1375. + strncpy(info->lo_crypt_name, LDE_lo_crypt_name, LO_NAME_SIZE);
  1376. + info->lo_encrypt_type = lo->lo_encrypt_type;
  1377. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  1378. - info.lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1379. - memcpy(info.lo_encrypt_key, lo->lo_encrypt_key,
  1380. + info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1381. + memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  1382. lo->lo_encrypt_key_size);
  1383. + info->lo_init[0] = lo->lo_init[0];
  1384. + info->lo_init[1] = lo->lo_init[1];
  1385. }
  1386. - return copy_to_user(arg, &info, sizeof(info)) ? -EFAULT : 0;
  1387. + return 0;
  1388. +}
  1389. +
  1390. +static int
  1391. +loop_set_status_n(struct loop_device *lo, kdev_t dev, void *arg, int n)
  1392. +{
  1393. + struct loop_info info;
  1394. + struct loopinfo64 info64;
  1395. + int err;
  1396. +
  1397. + if (n) {
  1398. + if (copy_from_user(&info64, arg, sizeof (struct loopinfo64)))
  1399. + return -EFAULT;
  1400. + /* truncation errors can be ignored here as transfer init func only wants key bits */
  1401. + loop_info64_to_old(&info64, &info);
  1402. + } else {
  1403. + if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1404. + return -EFAULT;
  1405. + loop_info64_from_old(&info, &info64);
  1406. + }
  1407. + err = loop_set_status(lo, dev, &info64, &info);
  1408. + memset(&info.lo_encrypt_key[0], 0, sizeof(info.lo_encrypt_key));
  1409. + memset(&info64.lo_encrypt_key[0], 0, sizeof(info64.lo_encrypt_key));
  1410. + return err;
  1411. +}
  1412. +
  1413. +static int
  1414. +loop_get_status_old(struct loop_device *lo, struct loop_info *arg) {
  1415. + struct loop_info info;
  1416. + struct loopinfo64 info64;
  1417. + int err = 0;
  1418. +
  1419. + if (!arg)
  1420. + err = -EINVAL;
  1421. + if (!err)
  1422. + err = loop_get_status(lo, &info64);
  1423. + if (!err)
  1424. + err = loop_info64_to_old(&info64, &info);
  1425. + if (!err && copy_to_user(arg, &info, sizeof(info)))
  1426. + err = -EFAULT;
  1427. +
  1428. + return err;
  1429. +}
  1430. +
  1431. +static int
  1432. +loop_get_status64(struct loop_device *lo, struct loopinfo64 *arg) {
  1433. + struct loopinfo64 info64;
  1434. + int err = 0;
  1435. +
  1436. + if (!arg)
  1437. + err = -EINVAL;
  1438. + if (!err)
  1439. + err = loop_get_status(lo, &info64);
  1440. + if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1441. + err = -EFAULT;
  1442. +
  1443. + return err;
  1444. }
  1445. static int lo_ioctl(struct inode * inode, struct file * file,
  1446. @@ -862,20 +1291,30 @@ static int lo_ioctl(struct inode * inode
  1447. dev = MINOR(inode->i_rdev);
  1448. if (dev >= max_loop)
  1449. return -ENODEV;
  1450. - lo = &loop_dev[dev];
  1451. + lo = (struct loop_device *)(&loop_dev[dev]);
  1452. down(&lo->lo_ctl_mutex);
  1453. switch (cmd) {
  1454. case LOOP_SET_FD:
  1455. err = loop_set_fd(lo, file, inode->i_rdev, arg);
  1456. break;
  1457. case LOOP_CLR_FD:
  1458. +#if LINUX_VERSION_CODE >= 0x2040C
  1459. err = loop_clr_fd(lo, inode->i_bdev);
  1460. +#else
  1461. + err = loop_clr_fd(lo, inode->i_rdev);
  1462. +#endif
  1463. break;
  1464. case LOOP_SET_STATUS:
  1465. - err = loop_set_status(lo, (struct loop_info *) arg);
  1466. + err = loop_set_status_n(lo, inode->i_rdev, (void *) arg, 0);
  1467. break;
  1468. case LOOP_GET_STATUS:
  1469. - err = loop_get_status(lo, (struct loop_info *) arg);
  1470. + err = loop_get_status_old(lo, (struct loop_info *) arg);
  1471. + break;
  1472. + case LOOP_SET_STATUS64:
  1473. + err = loop_set_status_n(lo, inode->i_rdev, (void *) arg, 1);
  1474. + break;
  1475. + case LOOP_GET_STATUS64:
  1476. + err = loop_get_status64(lo, (struct loopinfo64 *) arg);
  1477. break;
  1478. case BLKGETSIZE:
  1479. if (lo->lo_state != Lo_bound) {
  1480. @@ -884,6 +1323,7 @@ static int lo_ioctl(struct inode * inode
  1481. }
  1482. err = put_user((unsigned long)loop_sizes[lo->lo_number] << 1, (unsigned long *) arg);
  1483. break;
  1484. +#if defined(BLKGETSIZE64)
  1485. case BLKGETSIZE64:
  1486. if (lo->lo_state != Lo_bound) {
  1487. err = -ENXIO;
  1488. @@ -891,9 +1331,18 @@ static int lo_ioctl(struct inode * inode
  1489. }
  1490. err = put_user((u64)loop_sizes[lo->lo_number] << 10, (u64*)arg);
  1491. break;
  1492. +#endif
  1493. +#if defined(BLKBSZGET)
  1494. case BLKBSZGET:
  1495. +#endif
  1496. +#if defined(BLKBSZSET)
  1497. case BLKBSZSET:
  1498. +#endif
  1499. +#if defined(BLKSSZGET)
  1500. case BLKSSZGET:
  1501. +#endif
  1502. + case BLKROGET:
  1503. + case BLKROSET:
  1504. err = blk_ioctl(inode->i_rdev, cmd, arg);
  1505. break;
  1506. default:
  1507. @@ -906,7 +1355,7 @@ static int lo_ioctl(struct inode * inode
  1508. static int lo_open(struct inode *inode, struct file *file)
  1509. {
  1510. struct loop_device *lo;
  1511. - int dev, type;
  1512. + int dev;
  1513. if (!inode)
  1514. return -EINVAL;
  1515. @@ -918,13 +1367,9 @@ static int lo_open(struct inode *inode,
  1516. if (dev >= max_loop)
  1517. return -ENODEV;
  1518. - lo = &loop_dev[dev];
  1519. + lo = (struct loop_device *)(&loop_dev[dev]);
  1520. MOD_INC_USE_COUNT;
  1521. down(&lo->lo_ctl_mutex);
  1522. -
  1523. - type = lo->lo_encrypt_type;
  1524. - if (type && xfer_funcs[type] && xfer_funcs[type]->lock)
  1525. - xfer_funcs[type]->lock(lo);
  1526. lo->lo_refcnt++;
  1527. up(&lo->lo_ctl_mutex);
  1528. return 0;
  1529. @@ -933,7 +1378,7 @@ static int lo_open(struct inode *inode,
  1530. static int lo_release(struct inode *inode, struct file *file)
  1531. {
  1532. struct loop_device *lo;
  1533. - int dev, type;
  1534. + int dev;
  1535. if (!inode)
  1536. return 0;
  1537. @@ -946,20 +1391,18 @@ static int lo_release(struct inode *inod
  1538. if (dev >= max_loop)
  1539. return 0;
  1540. - lo = &loop_dev[dev];
  1541. + lo = (struct loop_device *)(&loop_dev[dev]);
  1542. down(&lo->lo_ctl_mutex);
  1543. - type = lo->lo_encrypt_type;
  1544. --lo->lo_refcnt;
  1545. - if (xfer_funcs[type] && xfer_funcs[type]->unlock)
  1546. - xfer_funcs[type]->unlock(lo);
  1547. -
  1548. up(&lo->lo_ctl_mutex);
  1549. MOD_DEC_USE_COUNT;
  1550. return 0;
  1551. }
  1552. static struct block_device_operations lo_fops = {
  1553. +#if !defined(NO_BLOCK_DEVICE_OPERATIONS_OWNER)
  1554. owner: THIS_MODULE,
  1555. +#endif
  1556. open: lo_open,
  1557. release: lo_release,
  1558. ioctl: lo_ioctl,
  1559. @@ -970,11 +1413,13 @@ static struct block_device_operations lo
  1560. */
  1561. MODULE_PARM(max_loop, "i");
  1562. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-256)");
  1563. +#if defined(MODULE_LICENSE)
  1564. MODULE_LICENSE("GPL");
  1565. +#endif
  1566. int loop_register_transfer(struct loop_func_table *funcs)
  1567. {
  1568. - if ((unsigned)funcs->number > MAX_LO_CRYPT || xfer_funcs[funcs->number])
  1569. + if ((unsigned)funcs->number >= MAX_LO_CRYPT || xfer_funcs[funcs->number])
  1570. return -EINVAL;
  1571. xfer_funcs[funcs->number] = funcs;
  1572. return 0;
  1573. @@ -983,15 +1428,15 @@ int loop_register_transfer(struct loop_f
  1574. int loop_unregister_transfer(int number)
  1575. {
  1576. struct loop_device *lo;
  1577. + int x, type;
  1578. if ((unsigned)number >= MAX_LO_CRYPT)
  1579. return -EINVAL;
  1580. - for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) {
  1581. - int type = lo->lo_encrypt_type;
  1582. + for (x = 0; x < max_loop; x++) {
  1583. + lo = (struct loop_device *)(&loop_dev[x]);
  1584. + type = lo->lo_encrypt_type;
  1585. if (type == number) {
  1586. - xfer_funcs[type]->release(lo);
  1587. - lo->transfer = NULL;
  1588. - lo->lo_encrypt_type = 0;
  1589. + loop_release_xfer(lo);
  1590. }
  1591. }
  1592. xfer_funcs[number] = NULL;
  1593. @@ -1017,10 +1462,9 @@ int __init loop_init(void)
  1594. return -EIO;
  1595. }
  1596. -
  1597. - loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL);
  1598. + loop_dev = kmalloc(max_loop * sizeof(LoDevExt), GFP_KERNEL);
  1599. if (!loop_dev)
  1600. - return -ENOMEM;
  1601. + goto out_dev;
  1602. loop_sizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
  1603. if (!loop_sizes)
  1604. @@ -1030,25 +1474,40 @@ int __init loop_init(void)
  1605. if (!loop_blksizes)
  1606. goto out_blksizes;
  1607. + loop_hardsizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
  1608. + if (!loop_hardsizes)
  1609. + goto out_hardsizes;
  1610. +
  1611. blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), loop_make_request);
  1612. for (i = 0; i < max_loop; i++) {
  1613. - struct loop_device *lo = &loop_dev[i];
  1614. - memset(lo, 0, sizeof(struct loop_device));
  1615. + struct loop_device *lo = (struct loop_device *)(&loop_dev[i]);
  1616. + memset(lo, 0, sizeof(LoDevExt));
  1617. init_MUTEX(&lo->lo_ctl_mutex);
  1618. init_MUTEX_LOCKED(&lo->lo_sem);
  1619. - init_MUTEX_LOCKED(&lo->lo_bh_mutex);
  1620. lo->lo_number = i;
  1621. spin_lock_init(&lo->lo_lock);
  1622. }
  1623. memset(loop_sizes, 0, max_loop * sizeof(int));
  1624. memset(loop_blksizes, 0, max_loop * sizeof(int));
  1625. + memset(loop_hardsizes, 0, max_loop * sizeof(int));
  1626. blk_size[MAJOR_NR] = loop_sizes;
  1627. blksize_size[MAJOR_NR] = loop_blksizes;
  1628. + hardsect_size[MAJOR_NR] = loop_hardsizes;
  1629. for (i = 0; i < max_loop; i++)
  1630. register_disk(NULL, MKDEV(MAJOR_NR, i), 1, &lo_fops, 0);
  1631. + { extern int init_module_aes(void); init_module_aes(); }
  1632. + for (i = 0; i < (sizeof(lo_prealloc) / sizeof(int)); i += 2) {
  1633. + if (!lo_prealloc[i])
  1634. + continue;
  1635. + if (lo_prealloc[i] < LO_PREALLOC_MIN)
  1636. + lo_prealloc[i] = LO_PREALLOC_MIN;
  1637. + if (lo_prealloc[i] > LO_PREALLOC_MAX)
  1638. + lo_prealloc[i] = LO_PREALLOC_MAX;
  1639. + }
  1640. +
  1641. devfs_handle = devfs_mk_dir(NULL, "loop", NULL);
  1642. devfs_register_series(devfs_handle, "%u", max_loop, DEVFS_FL_DEFAULT,
  1643. MAJOR_NR, 0,
  1644. @@ -1058,10 +1517,13 @@ int __init loop_init(void)
  1645. printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop);
  1646. return 0;
  1647. +out_hardsizes:
  1648. + kfree(loop_blksizes);
  1649. out_blksizes:
  1650. kfree(loop_sizes);
  1651. out_sizes:
  1652. kfree(loop_dev);
  1653. +out_dev:
  1654. if (devfs_unregister_blkdev(MAJOR_NR, "loop"))
  1655. printk(KERN_WARNING "loop: cannot unregister blkdev\n");
  1656. printk(KERN_ERR "loop: ran out of memory\n");
  1657. @@ -1070,12 +1532,18 @@ out_sizes:
  1658. void loop_exit(void)
  1659. {
  1660. + { extern void cleanup_module_aes(void); cleanup_module_aes(); }
  1661. devfs_unregister(devfs_handle);
  1662. if (devfs_unregister_blkdev(MAJOR_NR, "loop"))
  1663. printk(KERN_WARNING "loop: cannot unregister blkdev\n");
  1664. +
  1665. + blk_size[MAJOR_NR] = 0;
  1666. + blksize_size[MAJOR_NR] = 0;
  1667. + hardsect_size[MAJOR_NR] = 0;
  1668. kfree(loop_dev);
  1669. kfree(loop_sizes);
  1670. kfree(loop_blksizes);
  1671. + kfree(loop_hardsizes);
  1672. }
  1673. module_init(loop_init);
  1674. @@ -1090,3 +1558,10 @@ static int __init max_loop_setup(char *s
  1675. __setup("max_loop=", max_loop_setup);
  1676. #endif
  1677. +
  1678. +extern void loop_compute_sector_iv(int, u_int32_t *);
  1679. +EXPORT_SYMBOL(loop_compute_sector_iv);
  1680. +extern void loop_compute_md5_iv(int, u_int32_t *, u_int32_t *);
  1681. +EXPORT_SYMBOL(loop_compute_md5_iv);
  1682. +extern void md5_transform_CPUbyteorder(u_int32_t *, u_int32_t const *);
  1683. +EXPORT_SYMBOL_NOVERS(md5_transform_CPUbyteorder);