loop.c 52 KB

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