loop.c 45 KB

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