loop.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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/compat.h>
  66. #include <linux/suspend.h>
  67. #include <linux/freezer.h>
  68. #include <linux/mutex.h>
  69. #include <linux/writeback.h>
  70. #include <linux/completion.h>
  71. #include <linux/highmem.h>
  72. #include <linux/kthread.h>
  73. #include <linux/splice.h>
  74. #include <linux/sysfs.h>
  75. #include <linux/miscdevice.h>
  76. #include <linux/falloc.h>
  77. #include <linux/uio.h>
  78. #include "loop.h"
  79. #include <asm/uaccess.h>
  80. static DEFINE_IDR(loop_index_idr);
  81. static DEFINE_MUTEX(loop_index_mutex);
  82. static int max_part;
  83. static int part_shift;
  84. static int transfer_xor(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) + raw_off;
  90. char *loop_buf = kmap_atomic(loop_page) + loop_off;
  91. char *in, *out, *key;
  92. int i, keysize;
  93. if (cmd == READ) {
  94. in = raw_buf;
  95. out = loop_buf;
  96. } else {
  97. in = loop_buf;
  98. out = raw_buf;
  99. }
  100. key = lo->lo_encrypt_key;
  101. keysize = lo->lo_encrypt_key_size;
  102. for (i = 0; i < size; i++)
  103. *out++ = *in++ ^ key[(i & 511) % keysize];
  104. kunmap_atomic(loop_buf);
  105. kunmap_atomic(raw_buf);
  106. cond_resched();
  107. return 0;
  108. }
  109. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  110. {
  111. if (unlikely(info->lo_encrypt_key_size <= 0))
  112. return -EINVAL;
  113. return 0;
  114. }
  115. static struct loop_func_table none_funcs = {
  116. .number = LO_CRYPT_NONE,
  117. };
  118. static struct loop_func_table xor_funcs = {
  119. .number = LO_CRYPT_XOR,
  120. .transfer = transfer_xor,
  121. .init = xor_init
  122. };
  123. /* xfer_funcs[0] is special - its release function is never called */
  124. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  125. &none_funcs,
  126. &xor_funcs
  127. };
  128. static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
  129. {
  130. loff_t loopsize;
  131. /* Compute loopsize in bytes */
  132. loopsize = i_size_read(file->f_mapping->host);
  133. if (offset > 0)
  134. loopsize -= offset;
  135. /* offset is beyond i_size, weird but possible */
  136. if (loopsize < 0)
  137. return 0;
  138. if (sizelimit > 0 && sizelimit < loopsize)
  139. loopsize = sizelimit;
  140. /*
  141. * Unfortunately, if we want to do I/O on the device,
  142. * the number of 512-byte sectors has to fit into a sector_t.
  143. */
  144. return loopsize >> 9;
  145. }
  146. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  147. {
  148. return get_size(lo->lo_offset, lo->lo_sizelimit, file);
  149. }
  150. static int
  151. figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
  152. {
  153. loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
  154. sector_t x = (sector_t)size;
  155. struct block_device *bdev = lo->lo_device;
  156. if (unlikely((loff_t)x != size))
  157. return -EFBIG;
  158. if (lo->lo_offset != offset)
  159. lo->lo_offset = offset;
  160. if (lo->lo_sizelimit != sizelimit)
  161. lo->lo_sizelimit = sizelimit;
  162. set_capacity(lo->lo_disk, x);
  163. bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
  164. /* let user-space know about the new size */
  165. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  166. return 0;
  167. }
  168. static inline int
  169. lo_do_transfer(struct loop_device *lo, int cmd,
  170. struct page *rpage, unsigned roffs,
  171. struct page *lpage, unsigned loffs,
  172. int size, sector_t rblock)
  173. {
  174. int ret;
  175. ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  176. if (likely(!ret))
  177. return 0;
  178. printk_ratelimited(KERN_ERR
  179. "loop: Transfer error at byte offset %llu, length %i.\n",
  180. (unsigned long long)rblock << 9, size);
  181. return ret;
  182. }
  183. static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
  184. {
  185. struct iov_iter i;
  186. ssize_t bw;
  187. iov_iter_bvec(&i, ITER_BVEC, bvec, 1, bvec->bv_len);
  188. file_start_write(file);
  189. bw = vfs_iter_write(file, &i, ppos);
  190. file_end_write(file);
  191. if (likely(bw == bvec->bv_len))
  192. return 0;
  193. printk_ratelimited(KERN_ERR
  194. "loop: Write error at byte offset %llu, length %i.\n",
  195. (unsigned long long)*ppos, bvec->bv_len);
  196. if (bw >= 0)
  197. bw = -EIO;
  198. return bw;
  199. }
  200. static int lo_write_simple(struct loop_device *lo, struct request *rq,
  201. loff_t pos)
  202. {
  203. struct bio_vec bvec;
  204. struct req_iterator iter;
  205. int ret = 0;
  206. rq_for_each_segment(bvec, rq, iter) {
  207. ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
  208. if (ret < 0)
  209. break;
  210. cond_resched();
  211. }
  212. return ret;
  213. }
  214. /*
  215. * This is the slow, transforming version that needs to double buffer the
  216. * data as it cannot do the transformations in place without having direct
  217. * access to the destination pages of the backing file.
  218. */
  219. static int lo_write_transfer(struct loop_device *lo, struct request *rq,
  220. loff_t pos)
  221. {
  222. struct bio_vec bvec, b;
  223. struct req_iterator iter;
  224. struct page *page;
  225. int ret = 0;
  226. page = alloc_page(GFP_NOIO);
  227. if (unlikely(!page))
  228. return -ENOMEM;
  229. rq_for_each_segment(bvec, rq, iter) {
  230. ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
  231. bvec.bv_offset, bvec.bv_len, pos >> 9);
  232. if (unlikely(ret))
  233. break;
  234. b.bv_page = page;
  235. b.bv_offset = 0;
  236. b.bv_len = bvec.bv_len;
  237. ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
  238. if (ret < 0)
  239. break;
  240. }
  241. __free_page(page);
  242. return ret;
  243. }
  244. static int lo_read_simple(struct loop_device *lo, struct request *rq,
  245. loff_t pos)
  246. {
  247. struct bio_vec bvec;
  248. struct req_iterator iter;
  249. struct iov_iter i;
  250. ssize_t len;
  251. rq_for_each_segment(bvec, rq, iter) {
  252. iov_iter_bvec(&i, ITER_BVEC, &bvec, 1, bvec.bv_len);
  253. len = vfs_iter_read(lo->lo_backing_file, &i, &pos);
  254. if (len < 0)
  255. return len;
  256. flush_dcache_page(bvec.bv_page);
  257. if (len != bvec.bv_len) {
  258. struct bio *bio;
  259. __rq_for_each_bio(bio, rq)
  260. zero_fill_bio(bio);
  261. break;
  262. }
  263. cond_resched();
  264. }
  265. return 0;
  266. }
  267. static int lo_read_transfer(struct loop_device *lo, struct request *rq,
  268. loff_t pos)
  269. {
  270. struct bio_vec bvec, b;
  271. struct req_iterator iter;
  272. struct iov_iter i;
  273. struct page *page;
  274. ssize_t len;
  275. int ret = 0;
  276. page = alloc_page(GFP_NOIO);
  277. if (unlikely(!page))
  278. return -ENOMEM;
  279. rq_for_each_segment(bvec, rq, iter) {
  280. loff_t offset = pos;
  281. b.bv_page = page;
  282. b.bv_offset = 0;
  283. b.bv_len = bvec.bv_len;
  284. iov_iter_bvec(&i, ITER_BVEC, &b, 1, b.bv_len);
  285. len = vfs_iter_read(lo->lo_backing_file, &i, &pos);
  286. if (len < 0) {
  287. ret = len;
  288. goto out_free_page;
  289. }
  290. ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
  291. bvec.bv_offset, len, offset >> 9);
  292. if (ret)
  293. goto out_free_page;
  294. flush_dcache_page(bvec.bv_page);
  295. if (len != bvec.bv_len) {
  296. struct bio *bio;
  297. __rq_for_each_bio(bio, rq)
  298. zero_fill_bio(bio);
  299. break;
  300. }
  301. }
  302. ret = 0;
  303. out_free_page:
  304. __free_page(page);
  305. return ret;
  306. }
  307. static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
  308. {
  309. /*
  310. * We use punch hole to reclaim the free space used by the
  311. * image a.k.a. discard. However we do not support discard if
  312. * encryption is enabled, because it may give an attacker
  313. * useful information.
  314. */
  315. struct file *file = lo->lo_backing_file;
  316. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  317. int ret;
  318. if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
  319. ret = -EOPNOTSUPP;
  320. goto out;
  321. }
  322. ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
  323. if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
  324. ret = -EIO;
  325. out:
  326. return ret;
  327. }
  328. static int lo_req_flush(struct loop_device *lo, struct request *rq)
  329. {
  330. struct file *file = lo->lo_backing_file;
  331. int ret = vfs_fsync(file, 0);
  332. if (unlikely(ret && ret != -EINVAL))
  333. ret = -EIO;
  334. return ret;
  335. }
  336. static int do_req_filebacked(struct loop_device *lo, struct request *rq)
  337. {
  338. loff_t pos;
  339. int ret;
  340. pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
  341. if (rq->cmd_flags & REQ_WRITE) {
  342. if (rq->cmd_flags & REQ_FLUSH)
  343. ret = lo_req_flush(lo, rq);
  344. else if (rq->cmd_flags & REQ_DISCARD)
  345. ret = lo_discard(lo, rq, pos);
  346. else if (lo->transfer)
  347. ret = lo_write_transfer(lo, rq, pos);
  348. else
  349. ret = lo_write_simple(lo, rq, pos);
  350. } else {
  351. if (lo->transfer)
  352. ret = lo_read_transfer(lo, rq, pos);
  353. else
  354. ret = lo_read_simple(lo, rq, pos);
  355. }
  356. return ret;
  357. }
  358. struct switch_request {
  359. struct file *file;
  360. struct completion wait;
  361. };
  362. /*
  363. * Do the actual switch; called from the BIO completion routine
  364. */
  365. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  366. {
  367. struct file *file = p->file;
  368. struct file *old_file = lo->lo_backing_file;
  369. struct address_space *mapping;
  370. /* if no new file, only flush of queued bios requested */
  371. if (!file)
  372. return;
  373. mapping = file->f_mapping;
  374. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  375. lo->lo_backing_file = file;
  376. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  377. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  378. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  379. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  380. }
  381. /*
  382. * loop_switch performs the hard work of switching a backing store.
  383. * First it needs to flush existing IO, it does this by sending a magic
  384. * BIO down the pipe. The completion of this BIO does the actual switch.
  385. */
  386. static int loop_switch(struct loop_device *lo, struct file *file)
  387. {
  388. struct switch_request w;
  389. w.file = file;
  390. /* freeze queue and wait for completion of scheduled requests */
  391. blk_mq_freeze_queue(lo->lo_queue);
  392. /* do the switch action */
  393. do_loop_switch(lo, &w);
  394. /* unfreeze */
  395. blk_mq_unfreeze_queue(lo->lo_queue);
  396. return 0;
  397. }
  398. /*
  399. * Helper to flush the IOs in loop, but keeping loop thread running
  400. */
  401. static int loop_flush(struct loop_device *lo)
  402. {
  403. return loop_switch(lo, NULL);
  404. }
  405. /*
  406. * loop_change_fd switched the backing store of a loopback device to
  407. * a new file. This is useful for operating system installers to free up
  408. * the original file and in High Availability environments to switch to
  409. * an alternative location for the content in case of server meltdown.
  410. * This can only work if the loop device is used read-only, and if the
  411. * new backing store is the same size and type as the old backing store.
  412. */
  413. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  414. unsigned int arg)
  415. {
  416. struct file *file, *old_file;
  417. struct inode *inode;
  418. int error;
  419. error = -ENXIO;
  420. if (lo->lo_state != Lo_bound)
  421. goto out;
  422. /* the loop device has to be read-only */
  423. error = -EINVAL;
  424. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  425. goto out;
  426. error = -EBADF;
  427. file = fget(arg);
  428. if (!file)
  429. goto out;
  430. inode = file->f_mapping->host;
  431. old_file = lo->lo_backing_file;
  432. error = -EINVAL;
  433. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  434. goto out_putf;
  435. /* size of the new backing store needs to be the same */
  436. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  437. goto out_putf;
  438. /* and ... switch */
  439. error = loop_switch(lo, file);
  440. if (error)
  441. goto out_putf;
  442. fput(old_file);
  443. if (lo->lo_flags & LO_FLAGS_PARTSCAN)
  444. ioctl_by_bdev(bdev, BLKRRPART, 0);
  445. return 0;
  446. out_putf:
  447. fput(file);
  448. out:
  449. return error;
  450. }
  451. static inline int is_loop_device(struct file *file)
  452. {
  453. struct inode *i = file->f_mapping->host;
  454. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  455. }
  456. /* loop sysfs attributes */
  457. static ssize_t loop_attr_show(struct device *dev, char *page,
  458. ssize_t (*callback)(struct loop_device *, char *))
  459. {
  460. struct gendisk *disk = dev_to_disk(dev);
  461. struct loop_device *lo = disk->private_data;
  462. return callback(lo, page);
  463. }
  464. #define LOOP_ATTR_RO(_name) \
  465. static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
  466. static ssize_t loop_attr_do_show_##_name(struct device *d, \
  467. struct device_attribute *attr, char *b) \
  468. { \
  469. return loop_attr_show(d, b, loop_attr_##_name##_show); \
  470. } \
  471. static struct device_attribute loop_attr_##_name = \
  472. __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
  473. static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
  474. {
  475. ssize_t ret;
  476. char *p = NULL;
  477. spin_lock_irq(&lo->lo_lock);
  478. if (lo->lo_backing_file)
  479. p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
  480. spin_unlock_irq(&lo->lo_lock);
  481. if (IS_ERR_OR_NULL(p))
  482. ret = PTR_ERR(p);
  483. else {
  484. ret = strlen(p);
  485. memmove(buf, p, ret);
  486. buf[ret++] = '\n';
  487. buf[ret] = 0;
  488. }
  489. return ret;
  490. }
  491. static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
  492. {
  493. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
  494. }
  495. static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
  496. {
  497. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
  498. }
  499. static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
  500. {
  501. int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
  502. return sprintf(buf, "%s\n", autoclear ? "1" : "0");
  503. }
  504. static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
  505. {
  506. int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
  507. return sprintf(buf, "%s\n", partscan ? "1" : "0");
  508. }
  509. LOOP_ATTR_RO(backing_file);
  510. LOOP_ATTR_RO(offset);
  511. LOOP_ATTR_RO(sizelimit);
  512. LOOP_ATTR_RO(autoclear);
  513. LOOP_ATTR_RO(partscan);
  514. static struct attribute *loop_attrs[] = {
  515. &loop_attr_backing_file.attr,
  516. &loop_attr_offset.attr,
  517. &loop_attr_sizelimit.attr,
  518. &loop_attr_autoclear.attr,
  519. &loop_attr_partscan.attr,
  520. NULL,
  521. };
  522. static struct attribute_group loop_attribute_group = {
  523. .name = "loop",
  524. .attrs= loop_attrs,
  525. };
  526. static int loop_sysfs_init(struct loop_device *lo)
  527. {
  528. return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
  529. &loop_attribute_group);
  530. }
  531. static void loop_sysfs_exit(struct loop_device *lo)
  532. {
  533. sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
  534. &loop_attribute_group);
  535. }
  536. static void loop_config_discard(struct loop_device *lo)
  537. {
  538. struct file *file = lo->lo_backing_file;
  539. struct inode *inode = file->f_mapping->host;
  540. struct request_queue *q = lo->lo_queue;
  541. /*
  542. * We use punch hole to reclaim the free space used by the
  543. * image a.k.a. discard. However we do not support discard if
  544. * encryption is enabled, because it may give an attacker
  545. * useful information.
  546. */
  547. if ((!file->f_op->fallocate) ||
  548. lo->lo_encrypt_key_size) {
  549. q->limits.discard_granularity = 0;
  550. q->limits.discard_alignment = 0;
  551. q->limits.max_discard_sectors = 0;
  552. q->limits.discard_zeroes_data = 0;
  553. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  554. return;
  555. }
  556. q->limits.discard_granularity = inode->i_sb->s_blocksize;
  557. q->limits.discard_alignment = 0;
  558. q->limits.max_discard_sectors = UINT_MAX >> 9;
  559. q->limits.discard_zeroes_data = 1;
  560. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  561. }
  562. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  563. struct block_device *bdev, unsigned int arg)
  564. {
  565. struct file *file, *f;
  566. struct inode *inode;
  567. struct address_space *mapping;
  568. unsigned lo_blocksize;
  569. int lo_flags = 0;
  570. int error;
  571. loff_t size;
  572. /* This is safe, since we have a reference from open(). */
  573. __module_get(THIS_MODULE);
  574. error = -EBADF;
  575. file = fget(arg);
  576. if (!file)
  577. goto out;
  578. error = -EBUSY;
  579. if (lo->lo_state != Lo_unbound)
  580. goto out_putf;
  581. /* Avoid recursion */
  582. f = file;
  583. while (is_loop_device(f)) {
  584. struct loop_device *l;
  585. if (f->f_mapping->host->i_bdev == bdev)
  586. goto out_putf;
  587. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  588. if (l->lo_state == Lo_unbound) {
  589. error = -EINVAL;
  590. goto out_putf;
  591. }
  592. f = l->lo_backing_file;
  593. }
  594. mapping = file->f_mapping;
  595. inode = mapping->host;
  596. error = -EINVAL;
  597. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  598. goto out_putf;
  599. if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
  600. !file->f_op->write_iter)
  601. lo_flags |= LO_FLAGS_READ_ONLY;
  602. lo_blocksize = S_ISBLK(inode->i_mode) ?
  603. inode->i_bdev->bd_block_size : PAGE_SIZE;
  604. error = -EFBIG;
  605. size = get_loop_size(lo, file);
  606. if ((loff_t)(sector_t)size != size)
  607. goto out_putf;
  608. error = -ENOMEM;
  609. lo->wq = alloc_workqueue("kloopd%d",
  610. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16,
  611. lo->lo_number);
  612. if (!lo->wq)
  613. goto out_putf;
  614. error = 0;
  615. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  616. lo->lo_blocksize = lo_blocksize;
  617. lo->lo_device = bdev;
  618. lo->lo_flags = lo_flags;
  619. lo->lo_backing_file = file;
  620. lo->transfer = NULL;
  621. lo->ioctl = NULL;
  622. lo->lo_sizelimit = 0;
  623. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  624. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  625. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  626. blk_queue_flush(lo->lo_queue, REQ_FLUSH);
  627. set_capacity(lo->lo_disk, size);
  628. bd_set_size(bdev, size << 9);
  629. loop_sysfs_init(lo);
  630. /* let user-space know about the new size */
  631. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  632. set_blocksize(bdev, lo_blocksize);
  633. lo->lo_state = Lo_bound;
  634. if (part_shift)
  635. lo->lo_flags |= LO_FLAGS_PARTSCAN;
  636. if (lo->lo_flags & LO_FLAGS_PARTSCAN)
  637. ioctl_by_bdev(bdev, BLKRRPART, 0);
  638. /* Grab the block_device to prevent its destruction after we
  639. * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
  640. */
  641. bdgrab(bdev);
  642. return 0;
  643. out_putf:
  644. fput(file);
  645. out:
  646. /* This is safe: open() is still holding a reference. */
  647. module_put(THIS_MODULE);
  648. return error;
  649. }
  650. static int
  651. loop_release_xfer(struct loop_device *lo)
  652. {
  653. int err = 0;
  654. struct loop_func_table *xfer = lo->lo_encryption;
  655. if (xfer) {
  656. if (xfer->release)
  657. err = xfer->release(lo);
  658. lo->transfer = NULL;
  659. lo->lo_encryption = NULL;
  660. module_put(xfer->owner);
  661. }
  662. return err;
  663. }
  664. static int
  665. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  666. const struct loop_info64 *i)
  667. {
  668. int err = 0;
  669. if (xfer) {
  670. struct module *owner = xfer->owner;
  671. if (!try_module_get(owner))
  672. return -EINVAL;
  673. if (xfer->init)
  674. err = xfer->init(lo, i);
  675. if (err)
  676. module_put(owner);
  677. else
  678. lo->lo_encryption = xfer;
  679. }
  680. return err;
  681. }
  682. static int loop_clr_fd(struct loop_device *lo)
  683. {
  684. struct file *filp = lo->lo_backing_file;
  685. gfp_t gfp = lo->old_gfp_mask;
  686. struct block_device *bdev = lo->lo_device;
  687. if (lo->lo_state != Lo_bound)
  688. return -ENXIO;
  689. /*
  690. * If we've explicitly asked to tear down the loop device,
  691. * and it has an elevated reference count, set it for auto-teardown when
  692. * the last reference goes away. This stops $!~#$@ udev from
  693. * preventing teardown because it decided that it needs to run blkid on
  694. * the loopback device whenever they appear. xfstests is notorious for
  695. * failing tests because blkid via udev races with a losetup
  696. * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
  697. * command to fail with EBUSY.
  698. */
  699. if (lo->lo_refcnt > 1) {
  700. lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
  701. mutex_unlock(&lo->lo_ctl_mutex);
  702. return 0;
  703. }
  704. if (filp == NULL)
  705. return -EINVAL;
  706. spin_lock_irq(&lo->lo_lock);
  707. lo->lo_state = Lo_rundown;
  708. lo->lo_backing_file = NULL;
  709. spin_unlock_irq(&lo->lo_lock);
  710. loop_release_xfer(lo);
  711. lo->transfer = NULL;
  712. lo->ioctl = NULL;
  713. lo->lo_device = NULL;
  714. lo->lo_encryption = NULL;
  715. lo->lo_offset = 0;
  716. lo->lo_sizelimit = 0;
  717. lo->lo_encrypt_key_size = 0;
  718. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  719. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  720. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  721. if (bdev) {
  722. bdput(bdev);
  723. invalidate_bdev(bdev);
  724. }
  725. set_capacity(lo->lo_disk, 0);
  726. loop_sysfs_exit(lo);
  727. if (bdev) {
  728. bd_set_size(bdev, 0);
  729. /* let user-space know about this change */
  730. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  731. }
  732. mapping_set_gfp_mask(filp->f_mapping, gfp);
  733. lo->lo_state = Lo_unbound;
  734. /* This is safe: open() is still holding a reference. */
  735. module_put(THIS_MODULE);
  736. if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
  737. ioctl_by_bdev(bdev, BLKRRPART, 0);
  738. lo->lo_flags = 0;
  739. if (!part_shift)
  740. lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
  741. destroy_workqueue(lo->wq);
  742. lo->wq = NULL;
  743. mutex_unlock(&lo->lo_ctl_mutex);
  744. /*
  745. * Need not hold lo_ctl_mutex to fput backing file.
  746. * Calling fput holding lo_ctl_mutex triggers a circular
  747. * lock dependency possibility warning as fput can take
  748. * bd_mutex which is usually taken before lo_ctl_mutex.
  749. */
  750. fput(filp);
  751. return 0;
  752. }
  753. static int
  754. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  755. {
  756. int err;
  757. struct loop_func_table *xfer;
  758. kuid_t uid = current_uid();
  759. if (lo->lo_encrypt_key_size &&
  760. !uid_eq(lo->lo_key_owner, uid) &&
  761. !capable(CAP_SYS_ADMIN))
  762. return -EPERM;
  763. if (lo->lo_state != Lo_bound)
  764. return -ENXIO;
  765. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  766. return -EINVAL;
  767. err = loop_release_xfer(lo);
  768. if (err)
  769. return err;
  770. if (info->lo_encrypt_type) {
  771. unsigned int type = info->lo_encrypt_type;
  772. if (type >= MAX_LO_CRYPT)
  773. return -EINVAL;
  774. xfer = xfer_funcs[type];
  775. if (xfer == NULL)
  776. return -EINVAL;
  777. } else
  778. xfer = NULL;
  779. err = loop_init_xfer(lo, xfer, info);
  780. if (err)
  781. return err;
  782. if (lo->lo_offset != info->lo_offset ||
  783. lo->lo_sizelimit != info->lo_sizelimit)
  784. if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
  785. return -EFBIG;
  786. loop_config_discard(lo);
  787. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  788. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  789. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  790. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  791. if (!xfer)
  792. xfer = &none_funcs;
  793. lo->transfer = xfer->transfer;
  794. lo->ioctl = xfer->ioctl;
  795. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  796. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  797. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  798. if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
  799. !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
  800. lo->lo_flags |= LO_FLAGS_PARTSCAN;
  801. lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
  802. ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
  803. }
  804. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  805. lo->lo_init[0] = info->lo_init[0];
  806. lo->lo_init[1] = info->lo_init[1];
  807. if (info->lo_encrypt_key_size) {
  808. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  809. info->lo_encrypt_key_size);
  810. lo->lo_key_owner = uid;
  811. }
  812. return 0;
  813. }
  814. static int
  815. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  816. {
  817. struct file *file = lo->lo_backing_file;
  818. struct kstat stat;
  819. int error;
  820. if (lo->lo_state != Lo_bound)
  821. return -ENXIO;
  822. error = vfs_getattr(&file->f_path, &stat);
  823. if (error)
  824. return error;
  825. memset(info, 0, sizeof(*info));
  826. info->lo_number = lo->lo_number;
  827. info->lo_device = huge_encode_dev(stat.dev);
  828. info->lo_inode = stat.ino;
  829. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  830. info->lo_offset = lo->lo_offset;
  831. info->lo_sizelimit = lo->lo_sizelimit;
  832. info->lo_flags = lo->lo_flags;
  833. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  834. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  835. info->lo_encrypt_type =
  836. lo->lo_encryption ? lo->lo_encryption->number : 0;
  837. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  838. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  839. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  840. lo->lo_encrypt_key_size);
  841. }
  842. return 0;
  843. }
  844. static void
  845. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  846. {
  847. memset(info64, 0, sizeof(*info64));
  848. info64->lo_number = info->lo_number;
  849. info64->lo_device = info->lo_device;
  850. info64->lo_inode = info->lo_inode;
  851. info64->lo_rdevice = info->lo_rdevice;
  852. info64->lo_offset = info->lo_offset;
  853. info64->lo_sizelimit = 0;
  854. info64->lo_encrypt_type = info->lo_encrypt_type;
  855. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  856. info64->lo_flags = info->lo_flags;
  857. info64->lo_init[0] = info->lo_init[0];
  858. info64->lo_init[1] = info->lo_init[1];
  859. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  860. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  861. else
  862. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  863. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  864. }
  865. static int
  866. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  867. {
  868. memset(info, 0, sizeof(*info));
  869. info->lo_number = info64->lo_number;
  870. info->lo_device = info64->lo_device;
  871. info->lo_inode = info64->lo_inode;
  872. info->lo_rdevice = info64->lo_rdevice;
  873. info->lo_offset = info64->lo_offset;
  874. info->lo_encrypt_type = info64->lo_encrypt_type;
  875. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  876. info->lo_flags = info64->lo_flags;
  877. info->lo_init[0] = info64->lo_init[0];
  878. info->lo_init[1] = info64->lo_init[1];
  879. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  880. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  881. else
  882. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  883. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  884. /* error in case values were truncated */
  885. if (info->lo_device != info64->lo_device ||
  886. info->lo_rdevice != info64->lo_rdevice ||
  887. info->lo_inode != info64->lo_inode ||
  888. info->lo_offset != info64->lo_offset)
  889. return -EOVERFLOW;
  890. return 0;
  891. }
  892. static int
  893. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  894. {
  895. struct loop_info info;
  896. struct loop_info64 info64;
  897. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  898. return -EFAULT;
  899. loop_info64_from_old(&info, &info64);
  900. return loop_set_status(lo, &info64);
  901. }
  902. static int
  903. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  904. {
  905. struct loop_info64 info64;
  906. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  907. return -EFAULT;
  908. return loop_set_status(lo, &info64);
  909. }
  910. static int
  911. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  912. struct loop_info info;
  913. struct loop_info64 info64;
  914. int err = 0;
  915. if (!arg)
  916. err = -EINVAL;
  917. if (!err)
  918. err = loop_get_status(lo, &info64);
  919. if (!err)
  920. err = loop_info64_to_old(&info64, &info);
  921. if (!err && copy_to_user(arg, &info, sizeof(info)))
  922. err = -EFAULT;
  923. return err;
  924. }
  925. static int
  926. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  927. struct loop_info64 info64;
  928. int err = 0;
  929. if (!arg)
  930. err = -EINVAL;
  931. if (!err)
  932. err = loop_get_status(lo, &info64);
  933. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  934. err = -EFAULT;
  935. return err;
  936. }
  937. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  938. {
  939. if (unlikely(lo->lo_state != Lo_bound))
  940. return -ENXIO;
  941. return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
  942. }
  943. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  944. unsigned int cmd, unsigned long arg)
  945. {
  946. struct loop_device *lo = bdev->bd_disk->private_data;
  947. int err;
  948. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  949. switch (cmd) {
  950. case LOOP_SET_FD:
  951. err = loop_set_fd(lo, mode, bdev, arg);
  952. break;
  953. case LOOP_CHANGE_FD:
  954. err = loop_change_fd(lo, bdev, arg);
  955. break;
  956. case LOOP_CLR_FD:
  957. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  958. err = loop_clr_fd(lo);
  959. if (!err)
  960. goto out_unlocked;
  961. break;
  962. case LOOP_SET_STATUS:
  963. err = -EPERM;
  964. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  965. err = loop_set_status_old(lo,
  966. (struct loop_info __user *)arg);
  967. break;
  968. case LOOP_GET_STATUS:
  969. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  970. break;
  971. case LOOP_SET_STATUS64:
  972. err = -EPERM;
  973. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  974. err = loop_set_status64(lo,
  975. (struct loop_info64 __user *) arg);
  976. break;
  977. case LOOP_GET_STATUS64:
  978. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  979. break;
  980. case LOOP_SET_CAPACITY:
  981. err = -EPERM;
  982. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  983. err = loop_set_capacity(lo, bdev);
  984. break;
  985. default:
  986. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  987. }
  988. mutex_unlock(&lo->lo_ctl_mutex);
  989. out_unlocked:
  990. return err;
  991. }
  992. #ifdef CONFIG_COMPAT
  993. struct compat_loop_info {
  994. compat_int_t lo_number; /* ioctl r/o */
  995. compat_dev_t lo_device; /* ioctl r/o */
  996. compat_ulong_t lo_inode; /* ioctl r/o */
  997. compat_dev_t lo_rdevice; /* ioctl r/o */
  998. compat_int_t lo_offset;
  999. compat_int_t lo_encrypt_type;
  1000. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1001. compat_int_t lo_flags; /* ioctl r/o */
  1002. char lo_name[LO_NAME_SIZE];
  1003. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1004. compat_ulong_t lo_init[2];
  1005. char reserved[4];
  1006. };
  1007. /*
  1008. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1009. * - noinlined to reduce stack space usage in main part of driver
  1010. */
  1011. static noinline int
  1012. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1013. struct loop_info64 *info64)
  1014. {
  1015. struct compat_loop_info info;
  1016. if (copy_from_user(&info, arg, sizeof(info)))
  1017. return -EFAULT;
  1018. memset(info64, 0, sizeof(*info64));
  1019. info64->lo_number = info.lo_number;
  1020. info64->lo_device = info.lo_device;
  1021. info64->lo_inode = info.lo_inode;
  1022. info64->lo_rdevice = info.lo_rdevice;
  1023. info64->lo_offset = info.lo_offset;
  1024. info64->lo_sizelimit = 0;
  1025. info64->lo_encrypt_type = info.lo_encrypt_type;
  1026. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1027. info64->lo_flags = info.lo_flags;
  1028. info64->lo_init[0] = info.lo_init[0];
  1029. info64->lo_init[1] = info.lo_init[1];
  1030. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1031. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1032. else
  1033. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1034. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1035. return 0;
  1036. }
  1037. /*
  1038. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1039. * - noinlined to reduce stack space usage in main part of driver
  1040. */
  1041. static noinline int
  1042. loop_info64_to_compat(const struct loop_info64 *info64,
  1043. struct compat_loop_info __user *arg)
  1044. {
  1045. struct compat_loop_info info;
  1046. memset(&info, 0, sizeof(info));
  1047. info.lo_number = info64->lo_number;
  1048. info.lo_device = info64->lo_device;
  1049. info.lo_inode = info64->lo_inode;
  1050. info.lo_rdevice = info64->lo_rdevice;
  1051. info.lo_offset = info64->lo_offset;
  1052. info.lo_encrypt_type = info64->lo_encrypt_type;
  1053. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1054. info.lo_flags = info64->lo_flags;
  1055. info.lo_init[0] = info64->lo_init[0];
  1056. info.lo_init[1] = info64->lo_init[1];
  1057. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1058. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1059. else
  1060. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1061. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1062. /* error in case values were truncated */
  1063. if (info.lo_device != info64->lo_device ||
  1064. info.lo_rdevice != info64->lo_rdevice ||
  1065. info.lo_inode != info64->lo_inode ||
  1066. info.lo_offset != info64->lo_offset ||
  1067. info.lo_init[0] != info64->lo_init[0] ||
  1068. info.lo_init[1] != info64->lo_init[1])
  1069. return -EOVERFLOW;
  1070. if (copy_to_user(arg, &info, sizeof(info)))
  1071. return -EFAULT;
  1072. return 0;
  1073. }
  1074. static int
  1075. loop_set_status_compat(struct loop_device *lo,
  1076. const struct compat_loop_info __user *arg)
  1077. {
  1078. struct loop_info64 info64;
  1079. int ret;
  1080. ret = loop_info64_from_compat(arg, &info64);
  1081. if (ret < 0)
  1082. return ret;
  1083. return loop_set_status(lo, &info64);
  1084. }
  1085. static int
  1086. loop_get_status_compat(struct loop_device *lo,
  1087. struct compat_loop_info __user *arg)
  1088. {
  1089. struct loop_info64 info64;
  1090. int err = 0;
  1091. if (!arg)
  1092. err = -EINVAL;
  1093. if (!err)
  1094. err = loop_get_status(lo, &info64);
  1095. if (!err)
  1096. err = loop_info64_to_compat(&info64, arg);
  1097. return err;
  1098. }
  1099. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1100. unsigned int cmd, unsigned long arg)
  1101. {
  1102. struct loop_device *lo = bdev->bd_disk->private_data;
  1103. int err;
  1104. switch(cmd) {
  1105. case LOOP_SET_STATUS:
  1106. mutex_lock(&lo->lo_ctl_mutex);
  1107. err = loop_set_status_compat(
  1108. lo, (const struct compat_loop_info __user *) arg);
  1109. mutex_unlock(&lo->lo_ctl_mutex);
  1110. break;
  1111. case LOOP_GET_STATUS:
  1112. mutex_lock(&lo->lo_ctl_mutex);
  1113. err = loop_get_status_compat(
  1114. lo, (struct compat_loop_info __user *) arg);
  1115. mutex_unlock(&lo->lo_ctl_mutex);
  1116. break;
  1117. case LOOP_SET_CAPACITY:
  1118. case LOOP_CLR_FD:
  1119. case LOOP_GET_STATUS64:
  1120. case LOOP_SET_STATUS64:
  1121. arg = (unsigned long) compat_ptr(arg);
  1122. case LOOP_SET_FD:
  1123. case LOOP_CHANGE_FD:
  1124. err = lo_ioctl(bdev, mode, cmd, arg);
  1125. break;
  1126. default:
  1127. err = -ENOIOCTLCMD;
  1128. break;
  1129. }
  1130. return err;
  1131. }
  1132. #endif
  1133. static int lo_open(struct block_device *bdev, fmode_t mode)
  1134. {
  1135. struct loop_device *lo;
  1136. int err = 0;
  1137. mutex_lock(&loop_index_mutex);
  1138. lo = bdev->bd_disk->private_data;
  1139. if (!lo) {
  1140. err = -ENXIO;
  1141. goto out;
  1142. }
  1143. mutex_lock(&lo->lo_ctl_mutex);
  1144. lo->lo_refcnt++;
  1145. mutex_unlock(&lo->lo_ctl_mutex);
  1146. out:
  1147. mutex_unlock(&loop_index_mutex);
  1148. return err;
  1149. }
  1150. static void lo_release(struct gendisk *disk, fmode_t mode)
  1151. {
  1152. struct loop_device *lo = disk->private_data;
  1153. int err;
  1154. mutex_lock(&lo->lo_ctl_mutex);
  1155. if (--lo->lo_refcnt)
  1156. goto out;
  1157. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1158. /*
  1159. * In autoclear mode, stop the loop thread
  1160. * and remove configuration after last close.
  1161. */
  1162. err = loop_clr_fd(lo);
  1163. if (!err)
  1164. return;
  1165. } else {
  1166. /*
  1167. * Otherwise keep thread (if running) and config,
  1168. * but flush possible ongoing bios in thread.
  1169. */
  1170. loop_flush(lo);
  1171. }
  1172. out:
  1173. mutex_unlock(&lo->lo_ctl_mutex);
  1174. }
  1175. static const struct block_device_operations lo_fops = {
  1176. .owner = THIS_MODULE,
  1177. .open = lo_open,
  1178. .release = lo_release,
  1179. .ioctl = lo_ioctl,
  1180. #ifdef CONFIG_COMPAT
  1181. .compat_ioctl = lo_compat_ioctl,
  1182. #endif
  1183. };
  1184. /*
  1185. * And now the modules code and kernel interface.
  1186. */
  1187. static int max_loop;
  1188. module_param(max_loop, int, S_IRUGO);
  1189. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1190. module_param(max_part, int, S_IRUGO);
  1191. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1192. MODULE_LICENSE("GPL");
  1193. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1194. int loop_register_transfer(struct loop_func_table *funcs)
  1195. {
  1196. unsigned int n = funcs->number;
  1197. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1198. return -EINVAL;
  1199. xfer_funcs[n] = funcs;
  1200. return 0;
  1201. }
  1202. static int unregister_transfer_cb(int id, void *ptr, void *data)
  1203. {
  1204. struct loop_device *lo = ptr;
  1205. struct loop_func_table *xfer = data;
  1206. mutex_lock(&lo->lo_ctl_mutex);
  1207. if (lo->lo_encryption == xfer)
  1208. loop_release_xfer(lo);
  1209. mutex_unlock(&lo->lo_ctl_mutex);
  1210. return 0;
  1211. }
  1212. int loop_unregister_transfer(int number)
  1213. {
  1214. unsigned int n = number;
  1215. struct loop_func_table *xfer;
  1216. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1217. return -EINVAL;
  1218. xfer_funcs[n] = NULL;
  1219. idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
  1220. return 0;
  1221. }
  1222. EXPORT_SYMBOL(loop_register_transfer);
  1223. EXPORT_SYMBOL(loop_unregister_transfer);
  1224. static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
  1225. const struct blk_mq_queue_data *bd)
  1226. {
  1227. struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  1228. struct loop_device *lo = cmd->rq->q->queuedata;
  1229. blk_mq_start_request(bd->rq);
  1230. if (lo->lo_state != Lo_bound)
  1231. return -EIO;
  1232. if (cmd->rq->cmd_flags & REQ_WRITE) {
  1233. struct loop_device *lo = cmd->rq->q->queuedata;
  1234. bool need_sched = true;
  1235. spin_lock_irq(&lo->lo_lock);
  1236. if (lo->write_started)
  1237. need_sched = false;
  1238. else
  1239. lo->write_started = true;
  1240. list_add_tail(&cmd->list, &lo->write_cmd_head);
  1241. spin_unlock_irq(&lo->lo_lock);
  1242. if (need_sched)
  1243. queue_work(lo->wq, &lo->write_work);
  1244. } else {
  1245. queue_work(lo->wq, &cmd->read_work);
  1246. }
  1247. return BLK_MQ_RQ_QUEUE_OK;
  1248. }
  1249. static void loop_handle_cmd(struct loop_cmd *cmd)
  1250. {
  1251. const bool write = cmd->rq->cmd_flags & REQ_WRITE;
  1252. struct loop_device *lo = cmd->rq->q->queuedata;
  1253. int ret = -EIO;
  1254. if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
  1255. goto failed;
  1256. ret = do_req_filebacked(lo, cmd->rq);
  1257. failed:
  1258. if (ret)
  1259. cmd->rq->errors = -EIO;
  1260. blk_mq_complete_request(cmd->rq);
  1261. }
  1262. static void loop_queue_write_work(struct work_struct *work)
  1263. {
  1264. struct loop_device *lo =
  1265. container_of(work, struct loop_device, write_work);
  1266. LIST_HEAD(cmd_list);
  1267. spin_lock_irq(&lo->lo_lock);
  1268. repeat:
  1269. list_splice_init(&lo->write_cmd_head, &cmd_list);
  1270. spin_unlock_irq(&lo->lo_lock);
  1271. while (!list_empty(&cmd_list)) {
  1272. struct loop_cmd *cmd = list_first_entry(&cmd_list,
  1273. struct loop_cmd, list);
  1274. list_del_init(&cmd->list);
  1275. loop_handle_cmd(cmd);
  1276. }
  1277. spin_lock_irq(&lo->lo_lock);
  1278. if (!list_empty(&lo->write_cmd_head))
  1279. goto repeat;
  1280. lo->write_started = false;
  1281. spin_unlock_irq(&lo->lo_lock);
  1282. }
  1283. static void loop_queue_read_work(struct work_struct *work)
  1284. {
  1285. struct loop_cmd *cmd =
  1286. container_of(work, struct loop_cmd, read_work);
  1287. loop_handle_cmd(cmd);
  1288. }
  1289. static int loop_init_request(void *data, struct request *rq,
  1290. unsigned int hctx_idx, unsigned int request_idx,
  1291. unsigned int numa_node)
  1292. {
  1293. struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  1294. cmd->rq = rq;
  1295. INIT_WORK(&cmd->read_work, loop_queue_read_work);
  1296. return 0;
  1297. }
  1298. static struct blk_mq_ops loop_mq_ops = {
  1299. .queue_rq = loop_queue_rq,
  1300. .map_queue = blk_mq_map_queue,
  1301. .init_request = loop_init_request,
  1302. };
  1303. static int loop_add(struct loop_device **l, int i)
  1304. {
  1305. struct loop_device *lo;
  1306. struct gendisk *disk;
  1307. int err;
  1308. err = -ENOMEM;
  1309. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1310. if (!lo)
  1311. goto out;
  1312. lo->lo_state = Lo_unbound;
  1313. /* allocate id, if @id >= 0, we're requesting that specific id */
  1314. if (i >= 0) {
  1315. err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
  1316. if (err == -ENOSPC)
  1317. err = -EEXIST;
  1318. } else {
  1319. err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
  1320. }
  1321. if (err < 0)
  1322. goto out_free_dev;
  1323. i = err;
  1324. err = -ENOMEM;
  1325. lo->tag_set.ops = &loop_mq_ops;
  1326. lo->tag_set.nr_hw_queues = 1;
  1327. lo->tag_set.queue_depth = 128;
  1328. lo->tag_set.numa_node = NUMA_NO_NODE;
  1329. lo->tag_set.cmd_size = sizeof(struct loop_cmd);
  1330. lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1331. lo->tag_set.driver_data = lo;
  1332. err = blk_mq_alloc_tag_set(&lo->tag_set);
  1333. if (err)
  1334. goto out_free_idr;
  1335. lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
  1336. if (IS_ERR_OR_NULL(lo->lo_queue)) {
  1337. err = PTR_ERR(lo->lo_queue);
  1338. goto out_cleanup_tags;
  1339. }
  1340. lo->lo_queue->queuedata = lo;
  1341. INIT_LIST_HEAD(&lo->write_cmd_head);
  1342. INIT_WORK(&lo->write_work, loop_queue_write_work);
  1343. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1344. if (!disk)
  1345. goto out_free_queue;
  1346. /*
  1347. * Disable partition scanning by default. The in-kernel partition
  1348. * scanning can be requested individually per-device during its
  1349. * setup. Userspace can always add and remove partitions from all
  1350. * devices. The needed partition minors are allocated from the
  1351. * extended minor space, the main loop device numbers will continue
  1352. * to match the loop minors, regardless of the number of partitions
  1353. * used.
  1354. *
  1355. * If max_part is given, partition scanning is globally enabled for
  1356. * all loop devices. The minors for the main loop devices will be
  1357. * multiples of max_part.
  1358. *
  1359. * Note: Global-for-all-devices, set-only-at-init, read-only module
  1360. * parameteters like 'max_loop' and 'max_part' make things needlessly
  1361. * complicated, are too static, inflexible and may surprise
  1362. * userspace tools. Parameters like this in general should be avoided.
  1363. */
  1364. if (!part_shift)
  1365. disk->flags |= GENHD_FL_NO_PART_SCAN;
  1366. disk->flags |= GENHD_FL_EXT_DEVT;
  1367. mutex_init(&lo->lo_ctl_mutex);
  1368. lo->lo_number = i;
  1369. spin_lock_init(&lo->lo_lock);
  1370. disk->major = LOOP_MAJOR;
  1371. disk->first_minor = i << part_shift;
  1372. disk->fops = &lo_fops;
  1373. disk->private_data = lo;
  1374. disk->queue = lo->lo_queue;
  1375. sprintf(disk->disk_name, "loop%d", i);
  1376. add_disk(disk);
  1377. *l = lo;
  1378. return lo->lo_number;
  1379. out_free_queue:
  1380. blk_cleanup_queue(lo->lo_queue);
  1381. out_cleanup_tags:
  1382. blk_mq_free_tag_set(&lo->tag_set);
  1383. out_free_idr:
  1384. idr_remove(&loop_index_idr, i);
  1385. out_free_dev:
  1386. kfree(lo);
  1387. out:
  1388. return err;
  1389. }
  1390. static void loop_remove(struct loop_device *lo)
  1391. {
  1392. del_gendisk(lo->lo_disk);
  1393. blk_cleanup_queue(lo->lo_queue);
  1394. blk_mq_free_tag_set(&lo->tag_set);
  1395. put_disk(lo->lo_disk);
  1396. kfree(lo);
  1397. }
  1398. static int find_free_cb(int id, void *ptr, void *data)
  1399. {
  1400. struct loop_device *lo = ptr;
  1401. struct loop_device **l = data;
  1402. if (lo->lo_state == Lo_unbound) {
  1403. *l = lo;
  1404. return 1;
  1405. }
  1406. return 0;
  1407. }
  1408. static int loop_lookup(struct loop_device **l, int i)
  1409. {
  1410. struct loop_device *lo;
  1411. int ret = -ENODEV;
  1412. if (i < 0) {
  1413. int err;
  1414. err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
  1415. if (err == 1) {
  1416. *l = lo;
  1417. ret = lo->lo_number;
  1418. }
  1419. goto out;
  1420. }
  1421. /* lookup and return a specific i */
  1422. lo = idr_find(&loop_index_idr, i);
  1423. if (lo) {
  1424. *l = lo;
  1425. ret = lo->lo_number;
  1426. }
  1427. out:
  1428. return ret;
  1429. }
  1430. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1431. {
  1432. struct loop_device *lo;
  1433. struct kobject *kobj;
  1434. int err;
  1435. mutex_lock(&loop_index_mutex);
  1436. err = loop_lookup(&lo, MINOR(dev) >> part_shift);
  1437. if (err < 0)
  1438. err = loop_add(&lo, MINOR(dev) >> part_shift);
  1439. if (err < 0)
  1440. kobj = NULL;
  1441. else
  1442. kobj = get_disk(lo->lo_disk);
  1443. mutex_unlock(&loop_index_mutex);
  1444. *part = 0;
  1445. return kobj;
  1446. }
  1447. static long loop_control_ioctl(struct file *file, unsigned int cmd,
  1448. unsigned long parm)
  1449. {
  1450. struct loop_device *lo;
  1451. int ret = -ENOSYS;
  1452. mutex_lock(&loop_index_mutex);
  1453. switch (cmd) {
  1454. case LOOP_CTL_ADD:
  1455. ret = loop_lookup(&lo, parm);
  1456. if (ret >= 0) {
  1457. ret = -EEXIST;
  1458. break;
  1459. }
  1460. ret = loop_add(&lo, parm);
  1461. break;
  1462. case LOOP_CTL_REMOVE:
  1463. ret = loop_lookup(&lo, parm);
  1464. if (ret < 0)
  1465. break;
  1466. mutex_lock(&lo->lo_ctl_mutex);
  1467. if (lo->lo_state != Lo_unbound) {
  1468. ret = -EBUSY;
  1469. mutex_unlock(&lo->lo_ctl_mutex);
  1470. break;
  1471. }
  1472. if (lo->lo_refcnt > 0) {
  1473. ret = -EBUSY;
  1474. mutex_unlock(&lo->lo_ctl_mutex);
  1475. break;
  1476. }
  1477. lo->lo_disk->private_data = NULL;
  1478. mutex_unlock(&lo->lo_ctl_mutex);
  1479. idr_remove(&loop_index_idr, lo->lo_number);
  1480. loop_remove(lo);
  1481. break;
  1482. case LOOP_CTL_GET_FREE:
  1483. ret = loop_lookup(&lo, -1);
  1484. if (ret >= 0)
  1485. break;
  1486. ret = loop_add(&lo, -1);
  1487. }
  1488. mutex_unlock(&loop_index_mutex);
  1489. return ret;
  1490. }
  1491. static const struct file_operations loop_ctl_fops = {
  1492. .open = nonseekable_open,
  1493. .unlocked_ioctl = loop_control_ioctl,
  1494. .compat_ioctl = loop_control_ioctl,
  1495. .owner = THIS_MODULE,
  1496. .llseek = noop_llseek,
  1497. };
  1498. static struct miscdevice loop_misc = {
  1499. .minor = LOOP_CTRL_MINOR,
  1500. .name = "loop-control",
  1501. .fops = &loop_ctl_fops,
  1502. };
  1503. MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
  1504. MODULE_ALIAS("devname:loop-control");
  1505. static int __init loop_init(void)
  1506. {
  1507. int i, nr;
  1508. unsigned long range;
  1509. struct loop_device *lo;
  1510. int err;
  1511. err = misc_register(&loop_misc);
  1512. if (err < 0)
  1513. return err;
  1514. part_shift = 0;
  1515. if (max_part > 0) {
  1516. part_shift = fls(max_part);
  1517. /*
  1518. * Adjust max_part according to part_shift as it is exported
  1519. * to user space so that user can decide correct minor number
  1520. * if [s]he want to create more devices.
  1521. *
  1522. * Note that -1 is required because partition 0 is reserved
  1523. * for the whole disk.
  1524. */
  1525. max_part = (1UL << part_shift) - 1;
  1526. }
  1527. if ((1UL << part_shift) > DISK_MAX_PARTS) {
  1528. err = -EINVAL;
  1529. goto misc_out;
  1530. }
  1531. if (max_loop > 1UL << (MINORBITS - part_shift)) {
  1532. err = -EINVAL;
  1533. goto misc_out;
  1534. }
  1535. /*
  1536. * If max_loop is specified, create that many devices upfront.
  1537. * This also becomes a hard limit. If max_loop is not specified,
  1538. * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
  1539. * init time. Loop devices can be requested on-demand with the
  1540. * /dev/loop-control interface, or be instantiated by accessing
  1541. * a 'dead' device node.
  1542. */
  1543. if (max_loop) {
  1544. nr = max_loop;
  1545. range = max_loop << part_shift;
  1546. } else {
  1547. nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
  1548. range = 1UL << MINORBITS;
  1549. }
  1550. if (register_blkdev(LOOP_MAJOR, "loop")) {
  1551. err = -EIO;
  1552. goto misc_out;
  1553. }
  1554. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1555. THIS_MODULE, loop_probe, NULL, NULL);
  1556. /* pre-create number of devices given by config or max_loop */
  1557. mutex_lock(&loop_index_mutex);
  1558. for (i = 0; i < nr; i++)
  1559. loop_add(&lo, i);
  1560. mutex_unlock(&loop_index_mutex);
  1561. printk(KERN_INFO "loop: module loaded\n");
  1562. return 0;
  1563. misc_out:
  1564. misc_deregister(&loop_misc);
  1565. return err;
  1566. }
  1567. static int loop_exit_cb(int id, void *ptr, void *data)
  1568. {
  1569. struct loop_device *lo = ptr;
  1570. loop_remove(lo);
  1571. return 0;
  1572. }
  1573. static void __exit loop_exit(void)
  1574. {
  1575. unsigned long range;
  1576. range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
  1577. idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
  1578. idr_destroy(&loop_index_idr);
  1579. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1580. unregister_blkdev(LOOP_MAJOR, "loop");
  1581. misc_deregister(&loop_misc);
  1582. }
  1583. module_init(loop_init);
  1584. module_exit(loop_exit);
  1585. #ifndef MODULE
  1586. static int __init max_loop_setup(char *str)
  1587. {
  1588. max_loop = simple_strtol(str, NULL, 0);
  1589. return 1;
  1590. }
  1591. __setup("max_loop=", max_loop_setup);
  1592. #endif