/kern_oII/drivers/block/loop.c

http://omnia2droid.googlecode.com/ · C · 1673 lines · 1242 code · 242 blank · 189 comment · 208 complexity · 81cf76aa0a38585f0cbccaaf03c7380e MD5 · raw file

  1. /*
  2. * linux/drivers/block/loop.c
  3. *
  4. * Written by Theodore Ts'o, 3/29/93
  5. *
  6. * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
  7. * permitted under the GNU General Public License.
  8. *
  9. * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
  10. * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
  11. *
  12. * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
  13. * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
  14. *
  15. * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
  16. *
  17. * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
  18. *
  19. * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
  20. *
  21. * Loadable modules and other fixes by AK, 1998
  22. *
  23. * Make real block number available to downstream transfer functions, enables
  24. * CBC (and relatives) mode encryption requiring unique IVs per data block.
  25. * Reed H. Petty, rhp@draper.net
  26. *
  27. * Maximum number of loop devices now dynamic via max_loop module parameter.
  28. * Russell Kroll <rkroll@exploits.org> 19990701
  29. *
  30. * Maximum number of loop devices when compiled-in now selectable by passing
  31. * max_loop=<1-255> to the kernel on boot.
  32. * Erik I. Bols?¸, <eriki@himolde.no>, Oct 31, 1999
  33. *
  34. * Completely rewrite request handling to be make_request_fn style and
  35. * non blocking, pushing work to a helper thread. Lots of fixes from
  36. * Al Viro too.
  37. * Jens Axboe <axboe@suse.de>, Nov 2000
  38. *
  39. * Support up to 256 loop devices
  40. * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
  41. *
  42. * Support for falling back on the write file operation when the address space
  43. * operations write_begin is not available on the backing filesystem.
  44. * Anton Altaparmakov, 16 Feb 2005
  45. *
  46. * Still To Fix:
  47. * - Advisory locking is ignored here.
  48. * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  49. *
  50. */
  51. #include <linux/module.h>
  52. #include <linux/moduleparam.h>
  53. #include <linux/sched.h>
  54. #include <linux/fs.h>
  55. #include <linux/file.h>
  56. #include <linux/stat.h>
  57. #include <linux/errno.h>
  58. #include <linux/major.h>
  59. #include <linux/wait.h>
  60. #include <linux/blkdev.h>
  61. #include <linux/blkpg.h>
  62. #include <linux/init.h>
  63. #include <linux/swap.h>
  64. #include <linux/slab.h>
  65. #include <linux/loop.h>
  66. #include <linux/compat.h>
  67. #include <linux/suspend.h>
  68. #include <linux/freezer.h>
  69. #include <linux/writeback.h>
  70. #include <linux/buffer_head.h> /* for invalidate_bdev() */
  71. #include <linux/completion.h>
  72. #include <linux/highmem.h>
  73. #include <linux/gfp.h>
  74. #include <linux/kthread.h>
  75. #include <linux/splice.h>
  76. #include <asm/uaccess.h>
  77. static LIST_HEAD(loop_devices);
  78. static DEFINE_MUTEX(loop_devices_mutex);
  79. static int max_part;
  80. static int part_shift;
  81. /*
  82. * Transfer functions
  83. */
  84. static int transfer_none(struct loop_device *lo, int cmd,
  85. struct page *raw_page, unsigned raw_off,
  86. struct page *loop_page, unsigned loop_off,
  87. int size, sector_t real_block)
  88. {
  89. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  90. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  91. if (cmd == READ)
  92. memcpy(loop_buf, raw_buf, size);
  93. else
  94. memcpy(raw_buf, loop_buf, size);
  95. kunmap_atomic(raw_buf, KM_USER0);
  96. kunmap_atomic(loop_buf, KM_USER1);
  97. cond_resched();
  98. return 0;
  99. }
  100. static int transfer_xor(struct loop_device *lo, int cmd,
  101. struct page *raw_page, unsigned raw_off,
  102. struct page *loop_page, unsigned loop_off,
  103. int size, sector_t real_block)
  104. {
  105. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  106. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  107. char *in, *out, *key;
  108. int i, keysize;
  109. if (cmd == READ) {
  110. in = raw_buf;
  111. out = loop_buf;
  112. } else {
  113. in = loop_buf;
  114. out = raw_buf;
  115. }
  116. key = lo->lo_encrypt_key;
  117. keysize = lo->lo_encrypt_key_size;
  118. for (i = 0; i < size; i++)
  119. *out++ = *in++ ^ key[(i & 511) % keysize];
  120. kunmap_atomic(raw_buf, KM_USER0);
  121. kunmap_atomic(loop_buf, KM_USER1);
  122. cond_resched();
  123. return 0;
  124. }
  125. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  126. {
  127. if (unlikely(info->lo_encrypt_key_size <= 0))
  128. return -EINVAL;
  129. return 0;
  130. }
  131. static struct loop_func_table none_funcs = {
  132. .number = LO_CRYPT_NONE,
  133. .transfer = transfer_none,
  134. };
  135. static struct loop_func_table xor_funcs = {
  136. .number = LO_CRYPT_XOR,
  137. .transfer = transfer_xor,
  138. .init = xor_init
  139. };
  140. /* xfer_funcs[0] is special - its release function is never called */
  141. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  142. &none_funcs,
  143. &xor_funcs
  144. };
  145. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  146. {
  147. loff_t size, offset, loopsize;
  148. /* Compute loopsize in bytes */
  149. size = i_size_read(file->f_mapping->host);
  150. offset = lo->lo_offset;
  151. loopsize = size - offset;
  152. if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
  153. loopsize = lo->lo_sizelimit;
  154. /*
  155. * Unfortunately, if we want to do I/O on the device,
  156. * the number of 512-byte sectors has to fit into a sector_t.
  157. */
  158. return loopsize >> 9;
  159. }
  160. static int
  161. figure_loop_size(struct loop_device *lo)
  162. {
  163. loff_t size = get_loop_size(lo, lo->lo_backing_file);
  164. sector_t x = (sector_t)size;
  165. if (unlikely((loff_t)x != size))
  166. return -EFBIG;
  167. set_capacity(lo->lo_disk, x);
  168. return 0;
  169. }
  170. static inline int
  171. lo_do_transfer(struct loop_device *lo, int cmd,
  172. struct page *rpage, unsigned roffs,
  173. struct page *lpage, unsigned loffs,
  174. int size, sector_t rblock)
  175. {
  176. if (unlikely(!lo->transfer))
  177. return 0;
  178. return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  179. }
  180. /**
  181. * do_lo_send_aops - helper for writing data to a loop device
  182. *
  183. * This is the fast version for backing filesystems which implement the address
  184. * space operations write_begin and write_end.
  185. */
  186. static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
  187. loff_t pos, struct page *unused)
  188. {
  189. struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  190. struct address_space *mapping = file->f_mapping;
  191. pgoff_t index;
  192. unsigned offset, bv_offs;
  193. int len, ret;
  194. mutex_lock(&mapping->host->i_mutex);
  195. index = pos >> PAGE_CACHE_SHIFT;
  196. offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
  197. bv_offs = bvec->bv_offset;
  198. len = bvec->bv_len;
  199. while (len > 0) {
  200. sector_t IV;
  201. unsigned size, copied;
  202. int transfer_result;
  203. struct page *page;
  204. void *fsdata;
  205. IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  206. size = PAGE_CACHE_SIZE - offset;
  207. if (size > len)
  208. size = len;
  209. ret = pagecache_write_begin(file, mapping, pos, size, 0,
  210. &page, &fsdata);
  211. if (ret)
  212. goto fail;
  213. transfer_result = lo_do_transfer(lo, WRITE, page, offset,
  214. bvec->bv_page, bv_offs, size, IV);
  215. copied = size;
  216. if (unlikely(transfer_result))
  217. copied = 0;
  218. ret = pagecache_write_end(file, mapping, pos, size, copied,
  219. page, fsdata);
  220. if (ret < 0 || ret != copied)
  221. goto fail;
  222. if (unlikely(transfer_result))
  223. goto fail;
  224. bv_offs += copied;
  225. len -= copied;
  226. offset = 0;
  227. index++;
  228. pos += copied;
  229. }
  230. ret = 0;
  231. out:
  232. mutex_unlock(&mapping->host->i_mutex);
  233. return ret;
  234. fail:
  235. ret = -1;
  236. goto out;
  237. }
  238. /**
  239. * __do_lo_send_write - helper for writing data to a loop device
  240. *
  241. * This helper just factors out common code between do_lo_send_direct_write()
  242. * and do_lo_send_write().
  243. */
  244. static int __do_lo_send_write(struct file *file,
  245. u8 *buf, const int len, loff_t pos)
  246. {
  247. ssize_t bw;
  248. mm_segment_t old_fs = get_fs();
  249. set_fs(get_ds());
  250. bw = file->f_op->write(file, buf, len, &pos);
  251. set_fs(old_fs);
  252. if (likely(bw == len))
  253. return 0;
  254. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  255. (unsigned long long)pos, len);
  256. if (bw >= 0)
  257. bw = -EIO;
  258. return bw;
  259. }
  260. /**
  261. * do_lo_send_direct_write - helper for writing data to a loop device
  262. *
  263. * This is the fast, non-transforming version for backing filesystems which do
  264. * not implement the address space operations write_begin and write_end.
  265. * It uses the write file operation which should be present on all writeable
  266. * filesystems.
  267. */
  268. static int do_lo_send_direct_write(struct loop_device *lo,
  269. struct bio_vec *bvec, loff_t pos, struct page *page)
  270. {
  271. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  272. kmap(bvec->bv_page) + bvec->bv_offset,
  273. bvec->bv_len, pos);
  274. kunmap(bvec->bv_page);
  275. cond_resched();
  276. return bw;
  277. }
  278. /**
  279. * do_lo_send_write - helper for writing data to a loop device
  280. *
  281. * This is the slow, transforming version for filesystems which do not
  282. * implement the address space operations write_begin and write_end. It
  283. * uses the write file operation which should be present on all writeable
  284. * filesystems.
  285. *
  286. * Using fops->write is slower than using aops->{prepare,commit}_write in the
  287. * transforming case because we need to double buffer the data as we cannot do
  288. * the transformations in place as we do not have direct access to the
  289. * destination pages of the backing file.
  290. */
  291. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  292. loff_t pos, struct page *page)
  293. {
  294. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  295. bvec->bv_offset, bvec->bv_len, pos >> 9);
  296. if (likely(!ret))
  297. return __do_lo_send_write(lo->lo_backing_file,
  298. page_address(page), bvec->bv_len,
  299. pos);
  300. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  301. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  302. if (ret > 0)
  303. ret = -EIO;
  304. return ret;
  305. }
  306. static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
  307. {
  308. int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
  309. struct page *page);
  310. struct bio_vec *bvec;
  311. struct page *page = NULL;
  312. int i, ret = 0;
  313. do_lo_send = do_lo_send_aops;
  314. if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
  315. do_lo_send = do_lo_send_direct_write;
  316. if (lo->transfer != transfer_none) {
  317. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  318. if (unlikely(!page))
  319. goto fail;
  320. kmap(page);
  321. do_lo_send = do_lo_send_write;
  322. }
  323. }
  324. bio_for_each_segment(bvec, bio, i) {
  325. ret = do_lo_send(lo, bvec, pos, page);
  326. if (ret < 0)
  327. break;
  328. pos += bvec->bv_len;
  329. }
  330. if (page) {
  331. kunmap(page);
  332. __free_page(page);
  333. }
  334. out:
  335. return ret;
  336. fail:
  337. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  338. ret = -ENOMEM;
  339. goto out;
  340. }
  341. struct lo_read_data {
  342. struct loop_device *lo;
  343. struct page *page;
  344. unsigned offset;
  345. int bsize;
  346. };
  347. static int
  348. lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  349. struct splice_desc *sd)
  350. {
  351. struct lo_read_data *p = sd->u.data;
  352. struct loop_device *lo = p->lo;
  353. struct page *page = buf->page;
  354. sector_t IV;
  355. int size, ret;
  356. ret = buf->ops->confirm(pipe, buf);
  357. if (unlikely(ret))
  358. return ret;
  359. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  360. (buf->offset >> 9);
  361. size = sd->len;
  362. if (size > p->bsize)
  363. size = p->bsize;
  364. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  365. printk(KERN_ERR "loop: transfer error block %ld\n",
  366. page->index);
  367. size = -EINVAL;
  368. }
  369. flush_dcache_page(p->page);
  370. if (size > 0)
  371. p->offset += size;
  372. return size;
  373. }
  374. static int
  375. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  376. {
  377. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  378. }
  379. static int
  380. do_lo_receive(struct loop_device *lo,
  381. struct bio_vec *bvec, int bsize, loff_t pos)
  382. {
  383. struct lo_read_data cookie;
  384. struct splice_desc sd;
  385. struct file *file;
  386. long retval;
  387. cookie.lo = lo;
  388. cookie.page = bvec->bv_page;
  389. cookie.offset = bvec->bv_offset;
  390. cookie.bsize = bsize;
  391. sd.len = 0;
  392. sd.total_len = bvec->bv_len;
  393. sd.flags = 0;
  394. sd.pos = pos;
  395. sd.u.data = &cookie;
  396. file = lo->lo_backing_file;
  397. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  398. if (retval < 0)
  399. return retval;
  400. return 0;
  401. }
  402. static int
  403. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  404. {
  405. struct bio_vec *bvec;
  406. int i, ret = 0;
  407. bio_for_each_segment(bvec, bio, i) {
  408. ret = do_lo_receive(lo, bvec, bsize, pos);
  409. if (ret < 0)
  410. break;
  411. pos += bvec->bv_len;
  412. }
  413. return ret;
  414. }
  415. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  416. {
  417. loff_t pos;
  418. int ret;
  419. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  420. if (bio_rw(bio) == WRITE) {
  421. int barrier = bio_barrier(bio);
  422. struct file *file = lo->lo_backing_file;
  423. if (barrier) {
  424. if (unlikely(!file->f_op->fsync)) {
  425. ret = -EOPNOTSUPP;
  426. goto out;
  427. }
  428. ret = vfs_fsync(file, file->f_path.dentry, 0);
  429. if (unlikely(ret)) {
  430. ret = -EIO;
  431. goto out;
  432. }
  433. }
  434. ret = lo_send(lo, bio, pos);
  435. if (barrier && !ret) {
  436. ret = vfs_fsync(file, file->f_path.dentry, 0);
  437. if (unlikely(ret))
  438. ret = -EIO;
  439. }
  440. } else
  441. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  442. out:
  443. return ret;
  444. }
  445. /*
  446. * Add bio to back of pending list
  447. */
  448. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  449. {
  450. bio_list_add(&lo->lo_bio_list, bio);
  451. }
  452. /*
  453. * Grab first pending buffer
  454. */
  455. static struct bio *loop_get_bio(struct loop_device *lo)
  456. {
  457. return bio_list_pop(&lo->lo_bio_list);
  458. }
  459. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  460. {
  461. struct loop_device *lo = q->queuedata;
  462. int rw = bio_rw(old_bio);
  463. if (rw == READA)
  464. rw = READ;
  465. BUG_ON(!lo || (rw != READ && rw != WRITE));
  466. spin_lock_irq(&lo->lo_lock);
  467. if (lo->lo_state != Lo_bound)
  468. goto out;
  469. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  470. goto out;
  471. loop_add_bio(lo, old_bio);
  472. wake_up(&lo->lo_event);
  473. spin_unlock_irq(&lo->lo_lock);
  474. return 0;
  475. out:
  476. spin_unlock_irq(&lo->lo_lock);
  477. bio_io_error(old_bio);
  478. return 0;
  479. }
  480. /*
  481. * kick off io on the underlying address space
  482. */
  483. static void loop_unplug(struct request_queue *q)
  484. {
  485. struct loop_device *lo = q->queuedata;
  486. queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
  487. blk_run_address_space(lo->lo_backing_file->f_mapping);
  488. }
  489. struct switch_request {
  490. struct file *file;
  491. struct completion wait;
  492. };
  493. static void do_loop_switch(struct loop_device *, struct switch_request *);
  494. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  495. {
  496. if (unlikely(!bio->bi_bdev)) {
  497. do_loop_switch(lo, bio->bi_private);
  498. bio_put(bio);
  499. } else {
  500. int ret = do_bio_filebacked(lo, bio);
  501. bio_endio(bio, ret);
  502. }
  503. }
  504. /*
  505. * worker thread that handles reads/writes to file backed loop devices,
  506. * to avoid blocking in our make_request_fn. it also does loop decrypting
  507. * on reads for block backed loop, as that is too heavy to do from
  508. * b_end_io context where irqs may be disabled.
  509. *
  510. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  511. * calling kthread_stop(). Therefore once kthread_should_stop() is
  512. * true, make_request will not place any more requests. Therefore
  513. * once kthread_should_stop() is true and lo_bio is NULL, we are
  514. * done with the loop.
  515. */
  516. static int loop_thread(void *data)
  517. {
  518. struct loop_device *lo = data;
  519. struct bio *bio;
  520. set_user_nice(current, -20);
  521. while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
  522. wait_event_interruptible(lo->lo_event,
  523. !bio_list_empty(&lo->lo_bio_list) ||
  524. kthread_should_stop());
  525. if (bio_list_empty(&lo->lo_bio_list))
  526. continue;
  527. spin_lock_irq(&lo->lo_lock);
  528. bio = loop_get_bio(lo);
  529. spin_unlock_irq(&lo->lo_lock);
  530. BUG_ON(!bio);
  531. loop_handle_bio(lo, bio);
  532. }
  533. return 0;
  534. }
  535. /*
  536. * loop_switch performs the hard work of switching a backing store.
  537. * First it needs to flush existing IO, it does this by sending a magic
  538. * BIO down the pipe. The completion of this BIO does the actual switch.
  539. */
  540. static int loop_switch(struct loop_device *lo, struct file *file)
  541. {
  542. struct switch_request w;
  543. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  544. if (!bio)
  545. return -ENOMEM;
  546. init_completion(&w.wait);
  547. w.file = file;
  548. bio->bi_private = &w;
  549. bio->bi_bdev = NULL;
  550. loop_make_request(lo->lo_queue, bio);
  551. wait_for_completion(&w.wait);
  552. return 0;
  553. }
  554. /*
  555. * Helper to flush the IOs in loop, but keeping loop thread running
  556. */
  557. static int loop_flush(struct loop_device *lo)
  558. {
  559. /* loop not yet configured, no running thread, nothing to flush */
  560. if (!lo->lo_thread)
  561. return 0;
  562. return loop_switch(lo, NULL);
  563. }
  564. /*
  565. * Do the actual switch; called from the BIO completion routine
  566. */
  567. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  568. {
  569. struct file *file = p->file;
  570. struct file *old_file = lo->lo_backing_file;
  571. struct address_space *mapping;
  572. /* if no new file, only flush of queued bios requested */
  573. if (!file)
  574. goto out;
  575. mapping = file->f_mapping;
  576. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  577. lo->lo_backing_file = file;
  578. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  579. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  580. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  581. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  582. out:
  583. complete(&p->wait);
  584. }
  585. /*
  586. * loop_change_fd switched the backing store of a loopback device to
  587. * a new file. This is useful for operating system installers to free up
  588. * the original file and in High Availability environments to switch to
  589. * an alternative location for the content in case of server meltdown.
  590. * This can only work if the loop device is used read-only, and if the
  591. * new backing store is the same size and type as the old backing store.
  592. */
  593. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  594. unsigned int arg)
  595. {
  596. struct file *file, *old_file;
  597. struct inode *inode;
  598. int error;
  599. error = -ENXIO;
  600. if (lo->lo_state != Lo_bound)
  601. goto out;
  602. /* the loop device has to be read-only */
  603. error = -EINVAL;
  604. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  605. goto out;
  606. error = -EBADF;
  607. file = fget(arg);
  608. if (!file)
  609. goto out;
  610. inode = file->f_mapping->host;
  611. old_file = lo->lo_backing_file;
  612. error = -EINVAL;
  613. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  614. goto out_putf;
  615. /* size of the new backing store needs to be the same */
  616. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  617. goto out_putf;
  618. /* and ... switch */
  619. error = loop_switch(lo, file);
  620. if (error)
  621. goto out_putf;
  622. fput(old_file);
  623. if (max_part > 0)
  624. ioctl_by_bdev(bdev, BLKRRPART, 0);
  625. return 0;
  626. out_putf:
  627. fput(file);
  628. out:
  629. return error;
  630. }
  631. static inline int is_loop_device(struct file *file)
  632. {
  633. struct inode *i = file->f_mapping->host;
  634. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  635. }
  636. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  637. struct block_device *bdev, unsigned int arg)
  638. {
  639. struct file *file, *f;
  640. struct inode *inode;
  641. struct address_space *mapping;
  642. unsigned lo_blocksize;
  643. int lo_flags = 0;
  644. int error;
  645. loff_t size;
  646. /* This is safe, since we have a reference from open(). */
  647. __module_get(THIS_MODULE);
  648. error = -EBADF;
  649. file = fget(arg);
  650. if (!file)
  651. goto out;
  652. error = -EBUSY;
  653. if (lo->lo_state != Lo_unbound)
  654. goto out_putf;
  655. /* Avoid recursion */
  656. f = file;
  657. while (is_loop_device(f)) {
  658. struct loop_device *l;
  659. if (f->f_mapping->host->i_bdev == bdev)
  660. goto out_putf;
  661. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  662. if (l->lo_state == Lo_unbound) {
  663. error = -EINVAL;
  664. goto out_putf;
  665. }
  666. f = l->lo_backing_file;
  667. }
  668. mapping = file->f_mapping;
  669. inode = mapping->host;
  670. if (!(file->f_mode & FMODE_WRITE))
  671. lo_flags |= LO_FLAGS_READ_ONLY;
  672. error = -EINVAL;
  673. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  674. const struct address_space_operations *aops = mapping->a_ops;
  675. if (aops->write_begin)
  676. lo_flags |= LO_FLAGS_USE_AOPS;
  677. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  678. lo_flags |= LO_FLAGS_READ_ONLY;
  679. lo_blocksize = S_ISBLK(inode->i_mode) ?
  680. inode->i_bdev->bd_block_size : PAGE_SIZE;
  681. error = 0;
  682. } else {
  683. goto out_putf;
  684. }
  685. size = get_loop_size(lo, file);
  686. if ((loff_t)(sector_t)size != size) {
  687. error = -EFBIG;
  688. goto out_putf;
  689. }
  690. if (!(mode & FMODE_WRITE))
  691. lo_flags |= LO_FLAGS_READ_ONLY;
  692. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  693. lo->lo_blocksize = lo_blocksize;
  694. lo->lo_device = bdev;
  695. lo->lo_flags = lo_flags;
  696. lo->lo_backing_file = file;
  697. lo->transfer = transfer_none;
  698. lo->ioctl = NULL;
  699. lo->lo_sizelimit = 0;
  700. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  701. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  702. bio_list_init(&lo->lo_bio_list);
  703. /*
  704. * set queue make_request_fn, and add limits based on lower level
  705. * device
  706. */
  707. blk_queue_make_request(lo->lo_queue, loop_make_request);
  708. lo->lo_queue->queuedata = lo;
  709. lo->lo_queue->unplug_fn = loop_unplug;
  710. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  711. blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN, NULL);
  712. set_capacity(lo->lo_disk, size);
  713. bd_set_size(bdev, size << 9);
  714. set_blocksize(bdev, lo_blocksize);
  715. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  716. lo->lo_number);
  717. if (IS_ERR(lo->lo_thread)) {
  718. error = PTR_ERR(lo->lo_thread);
  719. goto out_clr;
  720. }
  721. lo->lo_state = Lo_bound;
  722. wake_up_process(lo->lo_thread);
  723. if (max_part > 0)
  724. ioctl_by_bdev(bdev, BLKRRPART, 0);
  725. return 0;
  726. out_clr:
  727. lo->lo_thread = NULL;
  728. lo->lo_device = NULL;
  729. lo->lo_backing_file = NULL;
  730. lo->lo_flags = 0;
  731. set_capacity(lo->lo_disk, 0);
  732. invalidate_bdev(bdev);
  733. bd_set_size(bdev, 0);
  734. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  735. lo->lo_state = Lo_unbound;
  736. out_putf:
  737. fput(file);
  738. out:
  739. /* This is safe: open() is still holding a reference. */
  740. module_put(THIS_MODULE);
  741. return error;
  742. }
  743. static int
  744. loop_release_xfer(struct loop_device *lo)
  745. {
  746. int err = 0;
  747. struct loop_func_table *xfer = lo->lo_encryption;
  748. if (xfer) {
  749. if (xfer->release)
  750. err = xfer->release(lo);
  751. lo->transfer = NULL;
  752. lo->lo_encryption = NULL;
  753. module_put(xfer->owner);
  754. }
  755. return err;
  756. }
  757. static int
  758. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  759. const struct loop_info64 *i)
  760. {
  761. int err = 0;
  762. if (xfer) {
  763. struct module *owner = xfer->owner;
  764. if (!try_module_get(owner))
  765. return -EINVAL;
  766. if (xfer->init)
  767. err = xfer->init(lo, i);
  768. if (err)
  769. module_put(owner);
  770. else
  771. lo->lo_encryption = xfer;
  772. }
  773. return err;
  774. }
  775. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  776. {
  777. struct file *filp = lo->lo_backing_file;
  778. gfp_t gfp = lo->old_gfp_mask;
  779. if (lo->lo_state != Lo_bound)
  780. return -ENXIO;
  781. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  782. return -EBUSY;
  783. if (filp == NULL)
  784. return -EINVAL;
  785. spin_lock_irq(&lo->lo_lock);
  786. lo->lo_state = Lo_rundown;
  787. spin_unlock_irq(&lo->lo_lock);
  788. kthread_stop(lo->lo_thread);
  789. lo->lo_queue->unplug_fn = NULL;
  790. lo->lo_backing_file = NULL;
  791. loop_release_xfer(lo);
  792. lo->transfer = NULL;
  793. lo->ioctl = NULL;
  794. lo->lo_device = NULL;
  795. lo->lo_encryption = NULL;
  796. lo->lo_offset = 0;
  797. lo->lo_sizelimit = 0;
  798. lo->lo_encrypt_key_size = 0;
  799. lo->lo_flags = 0;
  800. lo->lo_thread = NULL;
  801. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  802. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  803. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  804. if (bdev)
  805. invalidate_bdev(bdev);
  806. set_capacity(lo->lo_disk, 0);
  807. if (bdev)
  808. bd_set_size(bdev, 0);
  809. mapping_set_gfp_mask(filp->f_mapping, gfp);
  810. lo->lo_state = Lo_unbound;
  811. /* This is safe: open() is still holding a reference. */
  812. module_put(THIS_MODULE);
  813. if (max_part > 0)
  814. ioctl_by_bdev(bdev, BLKRRPART, 0);
  815. mutex_unlock(&lo->lo_ctl_mutex);
  816. /*
  817. * Need not hold lo_ctl_mutex to fput backing file.
  818. * Calling fput holding lo_ctl_mutex triggers a circular
  819. * lock dependency possibility warning as fput can take
  820. * bd_mutex which is usually taken before lo_ctl_mutex.
  821. */
  822. fput(filp);
  823. return 0;
  824. }
  825. static int
  826. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  827. {
  828. int err;
  829. struct loop_func_table *xfer;
  830. uid_t uid = current_uid();
  831. if (lo->lo_encrypt_key_size &&
  832. lo->lo_key_owner != uid &&
  833. !capable(CAP_SYS_ADMIN))
  834. return -EPERM;
  835. if (lo->lo_state != Lo_bound)
  836. return -ENXIO;
  837. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  838. return -EINVAL;
  839. err = loop_release_xfer(lo);
  840. if (err)
  841. return err;
  842. if (info->lo_encrypt_type) {
  843. unsigned int type = info->lo_encrypt_type;
  844. if (type >= MAX_LO_CRYPT)
  845. return -EINVAL;
  846. xfer = xfer_funcs[type];
  847. if (xfer == NULL)
  848. return -EINVAL;
  849. } else
  850. xfer = NULL;
  851. err = loop_init_xfer(lo, xfer, info);
  852. if (err)
  853. return err;
  854. if (lo->lo_offset != info->lo_offset ||
  855. lo->lo_sizelimit != info->lo_sizelimit) {
  856. lo->lo_offset = info->lo_offset;
  857. lo->lo_sizelimit = info->lo_sizelimit;
  858. if (figure_loop_size(lo))
  859. return -EFBIG;
  860. }
  861. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  862. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  863. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  864. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  865. if (!xfer)
  866. xfer = &none_funcs;
  867. lo->transfer = xfer->transfer;
  868. lo->ioctl = xfer->ioctl;
  869. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  870. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  871. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  872. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  873. lo->lo_init[0] = info->lo_init[0];
  874. lo->lo_init[1] = info->lo_init[1];
  875. if (info->lo_encrypt_key_size) {
  876. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  877. info->lo_encrypt_key_size);
  878. lo->lo_key_owner = uid;
  879. }
  880. return 0;
  881. }
  882. static int
  883. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  884. {
  885. struct file *file = lo->lo_backing_file;
  886. struct kstat stat;
  887. int error;
  888. if (lo->lo_state != Lo_bound)
  889. return -ENXIO;
  890. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  891. if (error)
  892. return error;
  893. memset(info, 0, sizeof(*info));
  894. info->lo_number = lo->lo_number;
  895. info->lo_device = huge_encode_dev(stat.dev);
  896. info->lo_inode = stat.ino;
  897. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  898. info->lo_offset = lo->lo_offset;
  899. info->lo_sizelimit = lo->lo_sizelimit;
  900. info->lo_flags = lo->lo_flags;
  901. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  902. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  903. info->lo_encrypt_type =
  904. lo->lo_encryption ? lo->lo_encryption->number : 0;
  905. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  906. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  907. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  908. lo->lo_encrypt_key_size);
  909. }
  910. return 0;
  911. }
  912. static void
  913. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  914. {
  915. memset(info64, 0, sizeof(*info64));
  916. info64->lo_number = info->lo_number;
  917. info64->lo_device = info->lo_device;
  918. info64->lo_inode = info->lo_inode;
  919. info64->lo_rdevice = info->lo_rdevice;
  920. info64->lo_offset = info->lo_offset;
  921. info64->lo_sizelimit = 0;
  922. info64->lo_encrypt_type = info->lo_encrypt_type;
  923. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  924. info64->lo_flags = info->lo_flags;
  925. info64->lo_init[0] = info->lo_init[0];
  926. info64->lo_init[1] = info->lo_init[1];
  927. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  928. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  929. else
  930. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  931. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  932. }
  933. static int
  934. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  935. {
  936. memset(info, 0, sizeof(*info));
  937. info->lo_number = info64->lo_number;
  938. info->lo_device = info64->lo_device;
  939. info->lo_inode = info64->lo_inode;
  940. info->lo_rdevice = info64->lo_rdevice;
  941. info->lo_offset = info64->lo_offset;
  942. info->lo_encrypt_type = info64->lo_encrypt_type;
  943. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  944. info->lo_flags = info64->lo_flags;
  945. info->lo_init[0] = info64->lo_init[0];
  946. info->lo_init[1] = info64->lo_init[1];
  947. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  948. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  949. else
  950. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  951. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  952. /* error in case values were truncated */
  953. if (info->lo_device != info64->lo_device ||
  954. info->lo_rdevice != info64->lo_rdevice ||
  955. info->lo_inode != info64->lo_inode ||
  956. info->lo_offset != info64->lo_offset)
  957. return -EOVERFLOW;
  958. return 0;
  959. }
  960. static int
  961. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  962. {
  963. struct loop_info info;
  964. struct loop_info64 info64;
  965. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  966. return -EFAULT;
  967. loop_info64_from_old(&info, &info64);
  968. return loop_set_status(lo, &info64);
  969. }
  970. static int
  971. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  972. {
  973. struct loop_info64 info64;
  974. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  975. return -EFAULT;
  976. return loop_set_status(lo, &info64);
  977. }
  978. static int
  979. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  980. struct loop_info info;
  981. struct loop_info64 info64;
  982. int err = 0;
  983. if (!arg)
  984. err = -EINVAL;
  985. if (!err)
  986. err = loop_get_status(lo, &info64);
  987. if (!err)
  988. err = loop_info64_to_old(&info64, &info);
  989. if (!err && copy_to_user(arg, &info, sizeof(info)))
  990. err = -EFAULT;
  991. return err;
  992. }
  993. static int
  994. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  995. struct loop_info64 info64;
  996. int err = 0;
  997. if (!arg)
  998. err = -EINVAL;
  999. if (!err)
  1000. err = loop_get_status(lo, &info64);
  1001. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1002. err = -EFAULT;
  1003. return err;
  1004. }
  1005. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1006. {
  1007. int err;
  1008. sector_t sec;
  1009. loff_t sz;
  1010. err = -ENXIO;
  1011. if (unlikely(lo->lo_state != Lo_bound))
  1012. goto out;
  1013. err = figure_loop_size(lo);
  1014. if (unlikely(err))
  1015. goto out;
  1016. sec = get_capacity(lo->lo_disk);
  1017. /* the width of sector_t may be narrow for bit-shift */
  1018. sz = sec;
  1019. sz <<= 9;
  1020. mutex_lock(&bdev->bd_mutex);
  1021. bd_set_size(bdev, sz);
  1022. mutex_unlock(&bdev->bd_mutex);
  1023. out:
  1024. return err;
  1025. }
  1026. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1027. unsigned int cmd, unsigned long arg)
  1028. {
  1029. struct loop_device *lo = bdev->bd_disk->private_data;
  1030. int err;
  1031. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1032. switch (cmd) {
  1033. case LOOP_SET_FD:
  1034. err = loop_set_fd(lo, mode, bdev, arg);
  1035. break;
  1036. case LOOP_CHANGE_FD:
  1037. err = loop_change_fd(lo, bdev, arg);
  1038. break;
  1039. case LOOP_CLR_FD:
  1040. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1041. err = loop_clr_fd(lo, bdev);
  1042. if (!err)
  1043. goto out_unlocked;
  1044. break;
  1045. case LOOP_SET_STATUS:
  1046. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1047. break;
  1048. case LOOP_GET_STATUS:
  1049. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1050. break;
  1051. case LOOP_SET_STATUS64:
  1052. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1053. break;
  1054. case LOOP_GET_STATUS64:
  1055. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1056. break;
  1057. case LOOP_SET_CAPACITY:
  1058. err = -EPERM;
  1059. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1060. err = loop_set_capacity(lo, bdev);
  1061. break;
  1062. default:
  1063. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1064. }
  1065. mutex_unlock(&lo->lo_ctl_mutex);
  1066. out_unlocked:
  1067. return err;
  1068. }
  1069. #ifdef CONFIG_COMPAT
  1070. struct compat_loop_info {
  1071. compat_int_t lo_number; /* ioctl r/o */
  1072. compat_dev_t lo_device; /* ioctl r/o */
  1073. compat_ulong_t lo_inode; /* ioctl r/o */
  1074. compat_dev_t lo_rdevice; /* ioctl r/o */
  1075. compat_int_t lo_offset;
  1076. compat_int_t lo_encrypt_type;
  1077. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1078. compat_int_t lo_flags; /* ioctl r/o */
  1079. char lo_name[LO_NAME_SIZE];
  1080. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1081. compat_ulong_t lo_init[2];
  1082. char reserved[4];
  1083. };
  1084. /*
  1085. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1086. * - noinlined to reduce stack space usage in main part of driver
  1087. */
  1088. static noinline int
  1089. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1090. struct loop_info64 *info64)
  1091. {
  1092. struct compat_loop_info info;
  1093. if (copy_from_user(&info, arg, sizeof(info)))
  1094. return -EFAULT;
  1095. memset(info64, 0, sizeof(*info64));
  1096. info64->lo_number = info.lo_number;
  1097. info64->lo_device = info.lo_device;
  1098. info64->lo_inode = info.lo_inode;
  1099. info64->lo_rdevice = info.lo_rdevice;
  1100. info64->lo_offset = info.lo_offset;
  1101. info64->lo_sizelimit = 0;
  1102. info64->lo_encrypt_type = info.lo_encrypt_type;
  1103. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1104. info64->lo_flags = info.lo_flags;
  1105. info64->lo_init[0] = info.lo_init[0];
  1106. info64->lo_init[1] = info.lo_init[1];
  1107. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1108. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1109. else
  1110. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1111. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1112. return 0;
  1113. }
  1114. /*
  1115. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1116. * - noinlined to reduce stack space usage in main part of driver
  1117. */
  1118. static noinline int
  1119. loop_info64_to_compat(const struct loop_info64 *info64,
  1120. struct compat_loop_info __user *arg)
  1121. {
  1122. struct compat_loop_info info;
  1123. memset(&info, 0, sizeof(info));
  1124. info.lo_number = info64->lo_number;
  1125. info.lo_device = info64->lo_device;
  1126. info.lo_inode = info64->lo_inode;
  1127. info.lo_rdevice = info64->lo_rdevice;
  1128. info.lo_offset = info64->lo_offset;
  1129. info.lo_encrypt_type = info64->lo_encrypt_type;
  1130. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1131. info.lo_flags = info64->lo_flags;
  1132. info.lo_init[0] = info64->lo_init[0];
  1133. info.lo_init[1] = info64->lo_init[1];
  1134. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1135. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1136. else
  1137. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1138. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1139. /* error in case values were truncated */
  1140. if (info.lo_device != info64->lo_device ||
  1141. info.lo_rdevice != info64->lo_rdevice ||
  1142. info.lo_inode != info64->lo_inode ||
  1143. info.lo_offset != info64->lo_offset ||
  1144. info.lo_init[0] != info64->lo_init[0] ||
  1145. info.lo_init[1] != info64->lo_init[1])
  1146. return -EOVERFLOW;
  1147. if (copy_to_user(arg, &info, sizeof(info)))
  1148. return -EFAULT;
  1149. return 0;
  1150. }
  1151. static int
  1152. loop_set_status_compat(struct loop_device *lo,
  1153. const struct compat_loop_info __user *arg)
  1154. {
  1155. struct loop_info64 info64;
  1156. int ret;
  1157. ret = loop_info64_from_compat(arg, &info64);
  1158. if (ret < 0)
  1159. return ret;
  1160. return loop_set_status(lo, &info64);
  1161. }
  1162. static int
  1163. loop_get_status_compat(struct loop_device *lo,
  1164. struct compat_loop_info __user *arg)
  1165. {
  1166. struct loop_info64 info64;
  1167. int err = 0;
  1168. if (!arg)
  1169. err = -EINVAL;
  1170. if (!err)
  1171. err = loop_get_status(lo, &info64);
  1172. if (!err)
  1173. err = loop_info64_to_compat(&info64, arg);
  1174. return err;
  1175. }
  1176. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1177. unsigned int cmd, unsigned long arg)
  1178. {
  1179. struct loop_device *lo = bdev->bd_disk->private_data;
  1180. int err;
  1181. switch(cmd) {
  1182. case LOOP_SET_STATUS:
  1183. mutex_lock(&lo->lo_ctl_mutex);
  1184. err = loop_set_status_compat(
  1185. lo, (const struct compat_loop_info __user *) arg);
  1186. mutex_unlock(&lo->lo_ctl_mutex);
  1187. break;
  1188. case LOOP_GET_STATUS:
  1189. mutex_lock(&lo->lo_ctl_mutex);
  1190. err = loop_get_status_compat(
  1191. lo, (struct compat_loop_info __user *) arg);
  1192. mutex_unlock(&lo->lo_ctl_mutex);
  1193. break;
  1194. case LOOP_SET_CAPACITY:
  1195. case LOOP_CLR_FD:
  1196. case LOOP_GET_STATUS64:
  1197. case LOOP_SET_STATUS64:
  1198. arg = (unsigned long) compat_ptr(arg);
  1199. case LOOP_SET_FD:
  1200. case LOOP_CHANGE_FD:
  1201. err = lo_ioctl(bdev, mode, cmd, arg);
  1202. break;
  1203. default:
  1204. err = -ENOIOCTLCMD;
  1205. break;
  1206. }
  1207. return err;
  1208. }
  1209. #endif
  1210. static int lo_open(struct block_device *bdev, fmode_t mode)
  1211. {
  1212. struct loop_device *lo = bdev->bd_disk->private_data;
  1213. mutex_lock(&lo->lo_ctl_mutex);
  1214. lo->lo_refcnt++;
  1215. mutex_unlock(&lo->lo_ctl_mutex);
  1216. return 0;
  1217. }
  1218. static int lo_release(struct gendisk *disk, fmode_t mode)
  1219. {
  1220. struct loop_device *lo = disk->private_data;
  1221. int err;
  1222. mutex_lock(&lo->lo_ctl_mutex);
  1223. if (--lo->lo_refcnt)
  1224. goto out;
  1225. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1226. /*
  1227. * In autoclear mode, stop the loop thread
  1228. * and remove configuration after last close.
  1229. */
  1230. err = loop_clr_fd(lo, NULL);
  1231. if (!err)
  1232. goto out_unlocked;
  1233. } else {
  1234. /*
  1235. * Otherwise keep thread (if running) and config,
  1236. * but flush possible ongoing bios in thread.
  1237. */
  1238. loop_flush(lo);
  1239. }
  1240. out:
  1241. mutex_unlock(&lo->lo_ctl_mutex);
  1242. out_unlocked:
  1243. return 0;
  1244. }
  1245. static struct block_device_operations lo_fops = {
  1246. .owner = THIS_MODULE,
  1247. .open = lo_open,
  1248. .release = lo_release,
  1249. .ioctl = lo_ioctl,
  1250. #ifdef CONFIG_COMPAT
  1251. .compat_ioctl = lo_compat_ioctl,
  1252. #endif
  1253. };
  1254. /*
  1255. * And now the modules code and kernel interface.
  1256. */
  1257. static int max_loop;
  1258. module_param(max_loop, int, 0);
  1259. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1260. module_param(max_part, int, 0);
  1261. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1262. MODULE_LICENSE("GPL");
  1263. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1264. int loop_register_transfer(struct loop_func_table *funcs)
  1265. {
  1266. unsigned int n = funcs->number;
  1267. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1268. return -EINVAL;
  1269. xfer_funcs[n] = funcs;
  1270. return 0;
  1271. }
  1272. int loop_unregister_transfer(int number)
  1273. {
  1274. unsigned int n = number;
  1275. struct loop_device *lo;
  1276. struct loop_func_table *xfer;
  1277. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1278. return -EINVAL;
  1279. xfer_funcs[n] = NULL;
  1280. list_for_each_entry(lo, &loop_devices, lo_list) {
  1281. mutex_lock(&lo->lo_ctl_mutex);
  1282. if (lo->lo_encryption == xfer)
  1283. loop_release_xfer(lo);
  1284. mutex_unlock(&lo->lo_ctl_mutex);
  1285. }
  1286. return 0;
  1287. }
  1288. EXPORT_SYMBOL(loop_register_transfer);
  1289. EXPORT_SYMBOL(loop_unregister_transfer);
  1290. static struct loop_device *loop_alloc(int i)
  1291. {
  1292. struct loop_device *lo;
  1293. struct gendisk *disk;
  1294. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1295. if (!lo)
  1296. goto out;
  1297. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1298. if (!lo->lo_queue)
  1299. goto out_free_dev;
  1300. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1301. if (!disk)
  1302. goto out_free_queue;
  1303. mutex_init(&lo->lo_ctl_mutex);
  1304. lo->lo_number = i;
  1305. lo->lo_thread = NULL;
  1306. init_waitqueue_head(&lo->lo_event);
  1307. spin_lock_init(&lo->lo_lock);
  1308. disk->major = LOOP_MAJOR;
  1309. disk->first_minor = i << part_shift;
  1310. disk->fops = &lo_fops;
  1311. disk->private_data = lo;
  1312. disk->queue = lo->lo_queue;
  1313. sprintf(disk->disk_name, "loop%d", i);
  1314. return lo;
  1315. out_free_queue:
  1316. blk_cleanup_queue(lo->lo_queue);
  1317. out_free_dev:
  1318. kfree(lo);
  1319. out:
  1320. return NULL;
  1321. }
  1322. static void loop_free(struct loop_device *lo)
  1323. {
  1324. blk_cleanup_queue(lo->lo_queue);
  1325. put_disk(lo->lo_disk);
  1326. list_del(&lo->lo_list);
  1327. kfree(lo);
  1328. }
  1329. static struct loop_device *loop_init_one(int i)
  1330. {
  1331. struct loop_device *lo;
  1332. list_for_each_entry(lo, &loop_devices, lo_list) {
  1333. if (lo->lo_number == i)
  1334. return lo;
  1335. }
  1336. lo = loop_alloc(i);
  1337. if (lo) {
  1338. add_disk(lo->lo_disk);
  1339. list_add_tail(&lo->lo_list, &loop_devices);
  1340. }
  1341. return lo;
  1342. }
  1343. static void loop_del_one(struct loop_device *lo)
  1344. {
  1345. del_gendisk(lo->lo_disk);
  1346. loop_free(lo);
  1347. }
  1348. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1349. {
  1350. struct loop_device *lo;
  1351. struct kobject *kobj;
  1352. mutex_lock(&loop_devices_mutex);
  1353. lo = loop_init_one(dev & MINORMASK);
  1354. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1355. mutex_unlock(&loop_devices_mutex);
  1356. *part = 0;
  1357. return kobj;
  1358. }
  1359. static int __init loop_init(void)
  1360. {
  1361. int i, nr;
  1362. unsigned long range;
  1363. struct loop_device *lo, *next;
  1364. /*
  1365. * loop module now has a feature to instantiate underlying device
  1366. * structure on-demand, provided that there is an access dev node.
  1367. * However, this will not work well with user space tool that doesn't
  1368. * know about such "feature". In order to not break any existing
  1369. * tool, we do the following:
  1370. *
  1371. * (1) if max_loop is specified, create that many upfront, and this
  1372. * also becomes a hard limit.
  1373. * (2) if max_loop is not specified, create 8 loop device on module
  1374. * load, user can further extend loop device by create dev node
  1375. * themselves and have kernel automatically instantiate actual
  1376. * device on-demand.
  1377. */
  1378. part_shift = 0;
  1379. if (max_part > 0)
  1380. part_shift = fls(max_part);
  1381. if (max_loop > 1UL << (MINORBITS - part_shift))
  1382. return -EINVAL;
  1383. if (max_loop) {
  1384. nr = max_loop;
  1385. range = max_loop;
  1386. } else {
  1387. nr = 8;
  1388. range = 1UL << (MINORBITS - part_shift);
  1389. }
  1390. if (register_blkdev(LOOP_MAJOR, "loop"))
  1391. return -EIO;
  1392. for (i = 0; i < nr; i++) {
  1393. lo = loop_alloc(i);
  1394. if (!lo)
  1395. goto Enomem;
  1396. list_add_tail(&lo->lo_list, &loop_devices);
  1397. }
  1398. /* point of no return */
  1399. list_for_each_entry(lo, &loop_devices, lo_list)
  1400. add_disk(lo->lo_disk);
  1401. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1402. THIS_MODULE, loop_probe, NULL, NULL);
  1403. printk(KERN_INFO "loop: module loaded\n");
  1404. return 0;
  1405. Enomem:
  1406. printk(KERN_INFO "loop: out of memory\n");
  1407. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1408. loop_free(lo);
  1409. unregister_blkdev(LOOP_MAJOR, "loop");
  1410. return -ENOMEM;
  1411. }
  1412. static void __exit loop_exit(void)
  1413. {
  1414. unsigned long range;
  1415. struct loop_device *lo, *next;
  1416. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1417. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1418. loop_del_one(lo);
  1419. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1420. unregister_blkdev(LOOP_MAJOR, "loop");
  1421. }
  1422. module_init(loop_init);
  1423. module_exit(loop_exit);
  1424. #ifndef MODULE
  1425. static int __init max_loop_setup(char *str)
  1426. {
  1427. max_loop = simple_strtol(str, NULL, 0);
  1428. return 1;
  1429. }
  1430. __setup("max_loop=", max_loop_setup);
  1431. #endif