loop.c 51 KB

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