loop.c 51 KB

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