loop.c 53 KB

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