loop.c 56 KB

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