loop.c 53 KB

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