loop.c 38 KB

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