loop.c 52 KB

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