loop.c 55 KB

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