loop.c 50 KB

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