loop.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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. 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 = call_write_iter(file, &cmd->iocb, &iter);
  432. else
  433. ret = call_read_iter(file, &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. STATX_INO, AT_STATX_SYNC_AS_STAT);
  999. if (error)
  1000. return error;
  1001. memset(info, 0, sizeof(*info));
  1002. info->lo_number = lo->lo_number;
  1003. info->lo_device = huge_encode_dev(stat.dev);
  1004. info->lo_inode = stat.ino;
  1005. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  1006. info->lo_offset = lo->lo_offset;
  1007. info->lo_sizelimit = lo->lo_sizelimit;
  1008. info->lo_flags = lo->lo_flags;
  1009. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  1010. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  1011. info->lo_encrypt_type =
  1012. lo->lo_encryption ? lo->lo_encryption->number : 0;
  1013. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  1014. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1015. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  1016. lo->lo_encrypt_key_size);
  1017. }
  1018. return 0;
  1019. }
  1020. static void
  1021. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  1022. {
  1023. memset(info64, 0, sizeof(*info64));
  1024. info64->lo_number = info->lo_number;
  1025. info64->lo_device = info->lo_device;
  1026. info64->lo_inode = info->lo_inode;
  1027. info64->lo_rdevice = info->lo_rdevice;
  1028. info64->lo_offset = info->lo_offset;
  1029. info64->lo_sizelimit = 0;
  1030. info64->lo_encrypt_type = info->lo_encrypt_type;
  1031. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1032. info64->lo_flags = info->lo_flags;
  1033. info64->lo_init[0] = info->lo_init[0];
  1034. info64->lo_init[1] = info->lo_init[1];
  1035. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1036. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1037. else
  1038. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1039. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1040. }
  1041. static int
  1042. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  1043. {
  1044. memset(info, 0, sizeof(*info));
  1045. info->lo_number = info64->lo_number;
  1046. info->lo_device = info64->lo_device;
  1047. info->lo_inode = info64->lo_inode;
  1048. info->lo_rdevice = info64->lo_rdevice;
  1049. info->lo_offset = info64->lo_offset;
  1050. info->lo_encrypt_type = info64->lo_encrypt_type;
  1051. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1052. info->lo_flags = info64->lo_flags;
  1053. info->lo_init[0] = info64->lo_init[0];
  1054. info->lo_init[1] = info64->lo_init[1];
  1055. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1056. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1057. else
  1058. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1059. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1060. /* error in case values were truncated */
  1061. if (info->lo_device != info64->lo_device ||
  1062. info->lo_rdevice != info64->lo_rdevice ||
  1063. info->lo_inode != info64->lo_inode ||
  1064. info->lo_offset != info64->lo_offset)
  1065. return -EOVERFLOW;
  1066. return 0;
  1067. }
  1068. static int
  1069. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  1070. {
  1071. struct loop_info info;
  1072. struct loop_info64 info64;
  1073. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1074. return -EFAULT;
  1075. loop_info64_from_old(&info, &info64);
  1076. return loop_set_status(lo, &info64);
  1077. }
  1078. static int
  1079. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  1080. {
  1081. struct loop_info64 info64;
  1082. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1083. return -EFAULT;
  1084. return loop_set_status(lo, &info64);
  1085. }
  1086. static int
  1087. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  1088. struct loop_info info;
  1089. struct loop_info64 info64;
  1090. int err = 0;
  1091. if (!arg)
  1092. err = -EINVAL;
  1093. if (!err)
  1094. err = loop_get_status(lo, &info64);
  1095. if (!err)
  1096. err = loop_info64_to_old(&info64, &info);
  1097. if (!err && copy_to_user(arg, &info, sizeof(info)))
  1098. err = -EFAULT;
  1099. return err;
  1100. }
  1101. static int
  1102. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1103. struct loop_info64 info64;
  1104. int err = 0;
  1105. if (!arg)
  1106. err = -EINVAL;
  1107. if (!err)
  1108. err = loop_get_status(lo, &info64);
  1109. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1110. err = -EFAULT;
  1111. return err;
  1112. }
  1113. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1114. {
  1115. if (unlikely(lo->lo_state != Lo_bound))
  1116. return -ENXIO;
  1117. return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
  1118. }
  1119. static int loop_set_dio(struct loop_device *lo, unsigned long arg)
  1120. {
  1121. int error = -ENXIO;
  1122. if (lo->lo_state != Lo_bound)
  1123. goto out;
  1124. __loop_update_dio(lo, !!arg);
  1125. if (lo->use_dio == !!arg)
  1126. return 0;
  1127. error = -EINVAL;
  1128. out:
  1129. return error;
  1130. }
  1131. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1132. unsigned int cmd, unsigned long arg)
  1133. {
  1134. struct loop_device *lo = bdev->bd_disk->private_data;
  1135. int err;
  1136. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1137. switch (cmd) {
  1138. case LOOP_SET_FD:
  1139. err = loop_set_fd(lo, mode, bdev, arg);
  1140. break;
  1141. case LOOP_CHANGE_FD:
  1142. err = loop_change_fd(lo, bdev, arg);
  1143. break;
  1144. case LOOP_CLR_FD:
  1145. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1146. err = loop_clr_fd(lo);
  1147. if (!err)
  1148. goto out_unlocked;
  1149. break;
  1150. case LOOP_SET_STATUS:
  1151. err = -EPERM;
  1152. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1153. err = loop_set_status_old(lo,
  1154. (struct loop_info __user *)arg);
  1155. break;
  1156. case LOOP_GET_STATUS:
  1157. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1158. break;
  1159. case LOOP_SET_STATUS64:
  1160. err = -EPERM;
  1161. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1162. err = loop_set_status64(lo,
  1163. (struct loop_info64 __user *) arg);
  1164. break;
  1165. case LOOP_GET_STATUS64:
  1166. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1167. break;
  1168. case LOOP_SET_CAPACITY:
  1169. err = -EPERM;
  1170. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1171. err = loop_set_capacity(lo, bdev);
  1172. break;
  1173. case LOOP_SET_DIRECT_IO:
  1174. err = -EPERM;
  1175. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1176. err = loop_set_dio(lo, arg);
  1177. break;
  1178. default:
  1179. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1180. }
  1181. mutex_unlock(&lo->lo_ctl_mutex);
  1182. out_unlocked:
  1183. return err;
  1184. }
  1185. #ifdef CONFIG_COMPAT
  1186. struct compat_loop_info {
  1187. compat_int_t lo_number; /* ioctl r/o */
  1188. compat_dev_t lo_device; /* ioctl r/o */
  1189. compat_ulong_t lo_inode; /* ioctl r/o */
  1190. compat_dev_t lo_rdevice; /* ioctl r/o */
  1191. compat_int_t lo_offset;
  1192. compat_int_t lo_encrypt_type;
  1193. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1194. compat_int_t lo_flags; /* ioctl r/o */
  1195. char lo_name[LO_NAME_SIZE];
  1196. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1197. compat_ulong_t lo_init[2];
  1198. char reserved[4];
  1199. };
  1200. /*
  1201. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1202. * - noinlined to reduce stack space usage in main part of driver
  1203. */
  1204. static noinline int
  1205. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1206. struct loop_info64 *info64)
  1207. {
  1208. struct compat_loop_info info;
  1209. if (copy_from_user(&info, arg, sizeof(info)))
  1210. return -EFAULT;
  1211. memset(info64, 0, sizeof(*info64));
  1212. info64->lo_number = info.lo_number;
  1213. info64->lo_device = info.lo_device;
  1214. info64->lo_inode = info.lo_inode;
  1215. info64->lo_rdevice = info.lo_rdevice;
  1216. info64->lo_offset = info.lo_offset;
  1217. info64->lo_sizelimit = 0;
  1218. info64->lo_encrypt_type = info.lo_encrypt_type;
  1219. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1220. info64->lo_flags = info.lo_flags;
  1221. info64->lo_init[0] = info.lo_init[0];
  1222. info64->lo_init[1] = info.lo_init[1];
  1223. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1224. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1225. else
  1226. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1227. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1228. return 0;
  1229. }
  1230. /*
  1231. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1232. * - noinlined to reduce stack space usage in main part of driver
  1233. */
  1234. static noinline int
  1235. loop_info64_to_compat(const struct loop_info64 *info64,
  1236. struct compat_loop_info __user *arg)
  1237. {
  1238. struct compat_loop_info info;
  1239. memset(&info, 0, sizeof(info));
  1240. info.lo_number = info64->lo_number;
  1241. info.lo_device = info64->lo_device;
  1242. info.lo_inode = info64->lo_inode;
  1243. info.lo_rdevice = info64->lo_rdevice;
  1244. info.lo_offset = info64->lo_offset;
  1245. info.lo_encrypt_type = info64->lo_encrypt_type;
  1246. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1247. info.lo_flags = info64->lo_flags;
  1248. info.lo_init[0] = info64->lo_init[0];
  1249. info.lo_init[1] = info64->lo_init[1];
  1250. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1251. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1252. else
  1253. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1254. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1255. /* error in case values were truncated */
  1256. if (info.lo_device != info64->lo_device ||
  1257. info.lo_rdevice != info64->lo_rdevice ||
  1258. info.lo_inode != info64->lo_inode ||
  1259. info.lo_offset != info64->lo_offset ||
  1260. info.lo_init[0] != info64->lo_init[0] ||
  1261. info.lo_init[1] != info64->lo_init[1])
  1262. return -EOVERFLOW;
  1263. if (copy_to_user(arg, &info, sizeof(info)))
  1264. return -EFAULT;
  1265. return 0;
  1266. }
  1267. static int
  1268. loop_set_status_compat(struct loop_device *lo,
  1269. const struct compat_loop_info __user *arg)
  1270. {
  1271. struct loop_info64 info64;
  1272. int ret;
  1273. ret = loop_info64_from_compat(arg, &info64);
  1274. if (ret < 0)
  1275. return ret;
  1276. return loop_set_status(lo, &info64);
  1277. }
  1278. static int
  1279. loop_get_status_compat(struct loop_device *lo,
  1280. struct compat_loop_info __user *arg)
  1281. {
  1282. struct loop_info64 info64;
  1283. int err = 0;
  1284. if (!arg)
  1285. err = -EINVAL;
  1286. if (!err)
  1287. err = loop_get_status(lo, &info64);
  1288. if (!err)
  1289. err = loop_info64_to_compat(&info64, arg);
  1290. return err;
  1291. }
  1292. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1293. unsigned int cmd, unsigned long arg)
  1294. {
  1295. struct loop_device *lo = bdev->bd_disk->private_data;
  1296. int err;
  1297. switch(cmd) {
  1298. case LOOP_SET_STATUS:
  1299. mutex_lock(&lo->lo_ctl_mutex);
  1300. err = loop_set_status_compat(
  1301. lo, (const struct compat_loop_info __user *) arg);
  1302. mutex_unlock(&lo->lo_ctl_mutex);
  1303. break;
  1304. case LOOP_GET_STATUS:
  1305. mutex_lock(&lo->lo_ctl_mutex);
  1306. err = loop_get_status_compat(
  1307. lo, (struct compat_loop_info __user *) arg);
  1308. mutex_unlock(&lo->lo_ctl_mutex);
  1309. break;
  1310. case LOOP_SET_CAPACITY:
  1311. case LOOP_CLR_FD:
  1312. case LOOP_GET_STATUS64:
  1313. case LOOP_SET_STATUS64:
  1314. arg = (unsigned long) compat_ptr(arg);
  1315. case LOOP_SET_FD:
  1316. case LOOP_CHANGE_FD:
  1317. err = lo_ioctl(bdev, mode, cmd, arg);
  1318. break;
  1319. default:
  1320. err = -ENOIOCTLCMD;
  1321. break;
  1322. }
  1323. return err;
  1324. }
  1325. #endif
  1326. static int lo_open(struct block_device *bdev, fmode_t mode)
  1327. {
  1328. struct loop_device *lo;
  1329. int err = 0;
  1330. mutex_lock(&loop_index_mutex);
  1331. lo = bdev->bd_disk->private_data;
  1332. if (!lo) {
  1333. err = -ENXIO;
  1334. goto out;
  1335. }
  1336. atomic_inc(&lo->lo_refcnt);
  1337. out:
  1338. mutex_unlock(&loop_index_mutex);
  1339. return err;
  1340. }
  1341. static void lo_release(struct gendisk *disk, fmode_t mode)
  1342. {
  1343. struct loop_device *lo = disk->private_data;
  1344. int err;
  1345. if (atomic_dec_return(&lo->lo_refcnt))
  1346. return;
  1347. mutex_lock(&lo->lo_ctl_mutex);
  1348. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1349. /*
  1350. * In autoclear mode, stop the loop thread
  1351. * and remove configuration after last close.
  1352. */
  1353. err = loop_clr_fd(lo);
  1354. if (!err)
  1355. return;
  1356. } else {
  1357. /*
  1358. * Otherwise keep thread (if running) and config,
  1359. * but flush possible ongoing bios in thread.
  1360. */
  1361. loop_flush(lo);
  1362. }
  1363. mutex_unlock(&lo->lo_ctl_mutex);
  1364. }
  1365. static const struct block_device_operations lo_fops = {
  1366. .owner = THIS_MODULE,
  1367. .open = lo_open,
  1368. .release = lo_release,
  1369. .ioctl = lo_ioctl,
  1370. #ifdef CONFIG_COMPAT
  1371. .compat_ioctl = lo_compat_ioctl,
  1372. #endif
  1373. };
  1374. /*
  1375. * And now the modules code and kernel interface.
  1376. */
  1377. static int max_loop;
  1378. module_param(max_loop, int, S_IRUGO);
  1379. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1380. module_param(max_part, int, S_IRUGO);
  1381. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1382. MODULE_LICENSE("GPL");
  1383. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1384. int loop_register_transfer(struct loop_func_table *funcs)
  1385. {
  1386. unsigned int n = funcs->number;
  1387. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1388. return -EINVAL;
  1389. xfer_funcs[n] = funcs;
  1390. return 0;
  1391. }
  1392. static int unregister_transfer_cb(int id, void *ptr, void *data)
  1393. {
  1394. struct loop_device *lo = ptr;
  1395. struct loop_func_table *xfer = data;
  1396. mutex_lock(&lo->lo_ctl_mutex);
  1397. if (lo->lo_encryption == xfer)
  1398. loop_release_xfer(lo);
  1399. mutex_unlock(&lo->lo_ctl_mutex);
  1400. return 0;
  1401. }
  1402. int loop_unregister_transfer(int number)
  1403. {
  1404. unsigned int n = number;
  1405. struct loop_func_table *xfer;
  1406. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1407. return -EINVAL;
  1408. xfer_funcs[n] = NULL;
  1409. idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
  1410. return 0;
  1411. }
  1412. EXPORT_SYMBOL(loop_register_transfer);
  1413. EXPORT_SYMBOL(loop_unregister_transfer);
  1414. static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
  1415. const struct blk_mq_queue_data *bd)
  1416. {
  1417. struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  1418. struct loop_device *lo = cmd->rq->q->queuedata;
  1419. blk_mq_start_request(bd->rq);
  1420. if (lo->lo_state != Lo_bound)
  1421. return BLK_MQ_RQ_QUEUE_ERROR;
  1422. switch (req_op(cmd->rq)) {
  1423. case REQ_OP_FLUSH:
  1424. case REQ_OP_DISCARD:
  1425. cmd->use_aio = false;
  1426. break;
  1427. default:
  1428. cmd->use_aio = lo->use_dio;
  1429. break;
  1430. }
  1431. kthread_queue_work(&lo->worker, &cmd->work);
  1432. return BLK_MQ_RQ_QUEUE_OK;
  1433. }
  1434. static void loop_handle_cmd(struct loop_cmd *cmd)
  1435. {
  1436. const bool write = op_is_write(req_op(cmd->rq));
  1437. struct loop_device *lo = cmd->rq->q->queuedata;
  1438. int ret = 0;
  1439. if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
  1440. ret = -EIO;
  1441. goto failed;
  1442. }
  1443. ret = do_req_filebacked(lo, cmd->rq);
  1444. failed:
  1445. /* complete non-aio request */
  1446. if (!cmd->use_aio || ret)
  1447. blk_mq_complete_request(cmd->rq, ret ? -EIO : 0);
  1448. }
  1449. static void loop_queue_work(struct kthread_work *work)
  1450. {
  1451. struct loop_cmd *cmd =
  1452. container_of(work, struct loop_cmd, work);
  1453. loop_handle_cmd(cmd);
  1454. }
  1455. static int loop_init_request(void *data, struct request *rq,
  1456. unsigned int hctx_idx, unsigned int request_idx,
  1457. unsigned int numa_node)
  1458. {
  1459. struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  1460. cmd->rq = rq;
  1461. kthread_init_work(&cmd->work, loop_queue_work);
  1462. return 0;
  1463. }
  1464. static struct blk_mq_ops loop_mq_ops = {
  1465. .queue_rq = loop_queue_rq,
  1466. .init_request = loop_init_request,
  1467. };
  1468. static int loop_add(struct loop_device **l, int i)
  1469. {
  1470. struct loop_device *lo;
  1471. struct gendisk *disk;
  1472. int err;
  1473. err = -ENOMEM;
  1474. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1475. if (!lo)
  1476. goto out;
  1477. lo->lo_state = Lo_unbound;
  1478. /* allocate id, if @id >= 0, we're requesting that specific id */
  1479. if (i >= 0) {
  1480. err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
  1481. if (err == -ENOSPC)
  1482. err = -EEXIST;
  1483. } else {
  1484. err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
  1485. }
  1486. if (err < 0)
  1487. goto out_free_dev;
  1488. i = err;
  1489. err = -ENOMEM;
  1490. lo->tag_set.ops = &loop_mq_ops;
  1491. lo->tag_set.nr_hw_queues = 1;
  1492. lo->tag_set.queue_depth = 128;
  1493. lo->tag_set.numa_node = NUMA_NO_NODE;
  1494. lo->tag_set.cmd_size = sizeof(struct loop_cmd);
  1495. lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1496. lo->tag_set.driver_data = lo;
  1497. err = blk_mq_alloc_tag_set(&lo->tag_set);
  1498. if (err)
  1499. goto out_free_idr;
  1500. lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
  1501. if (IS_ERR_OR_NULL(lo->lo_queue)) {
  1502. err = PTR_ERR(lo->lo_queue);
  1503. goto out_cleanup_tags;
  1504. }
  1505. lo->lo_queue->queuedata = lo;
  1506. /*
  1507. * It doesn't make sense to enable merge because the I/O
  1508. * submitted to backing file is handled page by page.
  1509. */
  1510. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
  1511. err = -ENOMEM;
  1512. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1513. if (!disk)
  1514. goto out_free_queue;
  1515. /*
  1516. * Disable partition scanning by default. The in-kernel partition
  1517. * scanning can be requested individually per-device during its
  1518. * setup. Userspace can always add and remove partitions from all
  1519. * devices. The needed partition minors are allocated from the
  1520. * extended minor space, the main loop device numbers will continue
  1521. * to match the loop minors, regardless of the number of partitions
  1522. * used.
  1523. *
  1524. * If max_part is given, partition scanning is globally enabled for
  1525. * all loop devices. The minors for the main loop devices will be
  1526. * multiples of max_part.
  1527. *
  1528. * Note: Global-for-all-devices, set-only-at-init, read-only module
  1529. * parameteters like 'max_loop' and 'max_part' make things needlessly
  1530. * complicated, are too static, inflexible and may surprise
  1531. * userspace tools. Parameters like this in general should be avoided.
  1532. */
  1533. if (!part_shift)
  1534. disk->flags |= GENHD_FL_NO_PART_SCAN;
  1535. disk->flags |= GENHD_FL_EXT_DEVT;
  1536. mutex_init(&lo->lo_ctl_mutex);
  1537. atomic_set(&lo->lo_refcnt, 0);
  1538. lo->lo_number = i;
  1539. spin_lock_init(&lo->lo_lock);
  1540. disk->major = LOOP_MAJOR;
  1541. disk->first_minor = i << part_shift;
  1542. disk->fops = &lo_fops;
  1543. disk->private_data = lo;
  1544. disk->queue = lo->lo_queue;
  1545. sprintf(disk->disk_name, "loop%d", i);
  1546. add_disk(disk);
  1547. *l = lo;
  1548. return lo->lo_number;
  1549. out_free_queue:
  1550. blk_cleanup_queue(lo->lo_queue);
  1551. out_cleanup_tags:
  1552. blk_mq_free_tag_set(&lo->tag_set);
  1553. out_free_idr:
  1554. idr_remove(&loop_index_idr, i);
  1555. out_free_dev:
  1556. kfree(lo);
  1557. out:
  1558. return err;
  1559. }
  1560. static void loop_remove(struct loop_device *lo)
  1561. {
  1562. blk_cleanup_queue(lo->lo_queue);
  1563. del_gendisk(lo->lo_disk);
  1564. blk_mq_free_tag_set(&lo->tag_set);
  1565. put_disk(lo->lo_disk);
  1566. kfree(lo);
  1567. }
  1568. static int find_free_cb(int id, void *ptr, void *data)
  1569. {
  1570. struct loop_device *lo = ptr;
  1571. struct loop_device **l = data;
  1572. if (lo->lo_state == Lo_unbound) {
  1573. *l = lo;
  1574. return 1;
  1575. }
  1576. return 0;
  1577. }
  1578. static int loop_lookup(struct loop_device **l, int i)
  1579. {
  1580. struct loop_device *lo;
  1581. int ret = -ENODEV;
  1582. if (i < 0) {
  1583. int err;
  1584. err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
  1585. if (err == 1) {
  1586. *l = lo;
  1587. ret = lo->lo_number;
  1588. }
  1589. goto out;
  1590. }
  1591. /* lookup and return a specific i */
  1592. lo = idr_find(&loop_index_idr, i);
  1593. if (lo) {
  1594. *l = lo;
  1595. ret = lo->lo_number;
  1596. }
  1597. out:
  1598. return ret;
  1599. }
  1600. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1601. {
  1602. struct loop_device *lo;
  1603. struct kobject *kobj;
  1604. int err;
  1605. mutex_lock(&loop_index_mutex);
  1606. err = loop_lookup(&lo, MINOR(dev) >> part_shift);
  1607. if (err < 0)
  1608. err = loop_add(&lo, MINOR(dev) >> part_shift);
  1609. if (err < 0)
  1610. kobj = NULL;
  1611. else
  1612. kobj = get_disk(lo->lo_disk);
  1613. mutex_unlock(&loop_index_mutex);
  1614. *part = 0;
  1615. return kobj;
  1616. }
  1617. static long loop_control_ioctl(struct file *file, unsigned int cmd,
  1618. unsigned long parm)
  1619. {
  1620. struct loop_device *lo;
  1621. int ret = -ENOSYS;
  1622. mutex_lock(&loop_index_mutex);
  1623. switch (cmd) {
  1624. case LOOP_CTL_ADD:
  1625. ret = loop_lookup(&lo, parm);
  1626. if (ret >= 0) {
  1627. ret = -EEXIST;
  1628. break;
  1629. }
  1630. ret = loop_add(&lo, parm);
  1631. break;
  1632. case LOOP_CTL_REMOVE:
  1633. ret = loop_lookup(&lo, parm);
  1634. if (ret < 0)
  1635. break;
  1636. mutex_lock(&lo->lo_ctl_mutex);
  1637. if (lo->lo_state != Lo_unbound) {
  1638. ret = -EBUSY;
  1639. mutex_unlock(&lo->lo_ctl_mutex);
  1640. break;
  1641. }
  1642. if (atomic_read(&lo->lo_refcnt) > 0) {
  1643. ret = -EBUSY;
  1644. mutex_unlock(&lo->lo_ctl_mutex);
  1645. break;
  1646. }
  1647. lo->lo_disk->private_data = NULL;
  1648. mutex_unlock(&lo->lo_ctl_mutex);
  1649. idr_remove(&loop_index_idr, lo->lo_number);
  1650. loop_remove(lo);
  1651. break;
  1652. case LOOP_CTL_GET_FREE:
  1653. ret = loop_lookup(&lo, -1);
  1654. if (ret >= 0)
  1655. break;
  1656. ret = loop_add(&lo, -1);
  1657. }
  1658. mutex_unlock(&loop_index_mutex);
  1659. return ret;
  1660. }
  1661. static const struct file_operations loop_ctl_fops = {
  1662. .open = nonseekable_open,
  1663. .unlocked_ioctl = loop_control_ioctl,
  1664. .compat_ioctl = loop_control_ioctl,
  1665. .owner = THIS_MODULE,
  1666. .llseek = noop_llseek,
  1667. };
  1668. static struct miscdevice loop_misc = {
  1669. .minor = LOOP_CTRL_MINOR,
  1670. .name = "loop-control",
  1671. .fops = &loop_ctl_fops,
  1672. };
  1673. MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
  1674. MODULE_ALIAS("devname:loop-control");
  1675. static int __init loop_init(void)
  1676. {
  1677. int i, nr;
  1678. unsigned long range;
  1679. struct loop_device *lo;
  1680. int err;
  1681. err = misc_register(&loop_misc);
  1682. if (err < 0)
  1683. return err;
  1684. part_shift = 0;
  1685. if (max_part > 0) {
  1686. part_shift = fls(max_part);
  1687. /*
  1688. * Adjust max_part according to part_shift as it is exported
  1689. * to user space so that user can decide correct minor number
  1690. * if [s]he want to create more devices.
  1691. *
  1692. * Note that -1 is required because partition 0 is reserved
  1693. * for the whole disk.
  1694. */
  1695. max_part = (1UL << part_shift) - 1;
  1696. }
  1697. if ((1UL << part_shift) > DISK_MAX_PARTS) {
  1698. err = -EINVAL;
  1699. goto misc_out;
  1700. }
  1701. if (max_loop > 1UL << (MINORBITS - part_shift)) {
  1702. err = -EINVAL;
  1703. goto misc_out;
  1704. }
  1705. /*
  1706. * If max_loop is specified, create that many devices upfront.
  1707. * This also becomes a hard limit. If max_loop is not specified,
  1708. * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
  1709. * init time. Loop devices can be requested on-demand with the
  1710. * /dev/loop-control interface, or be instantiated by accessing
  1711. * a 'dead' device node.
  1712. */
  1713. if (max_loop) {
  1714. nr = max_loop;
  1715. range = max_loop << part_shift;
  1716. } else {
  1717. nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
  1718. range = 1UL << MINORBITS;
  1719. }
  1720. if (register_blkdev(LOOP_MAJOR, "loop")) {
  1721. err = -EIO;
  1722. goto misc_out;
  1723. }
  1724. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1725. THIS_MODULE, loop_probe, NULL, NULL);
  1726. /* pre-create number of devices given by config or max_loop */
  1727. mutex_lock(&loop_index_mutex);
  1728. for (i = 0; i < nr; i++)
  1729. loop_add(&lo, i);
  1730. mutex_unlock(&loop_index_mutex);
  1731. printk(KERN_INFO "loop: module loaded\n");
  1732. return 0;
  1733. misc_out:
  1734. misc_deregister(&loop_misc);
  1735. return err;
  1736. }
  1737. static int loop_exit_cb(int id, void *ptr, void *data)
  1738. {
  1739. struct loop_device *lo = ptr;
  1740. loop_remove(lo);
  1741. return 0;
  1742. }
  1743. static void __exit loop_exit(void)
  1744. {
  1745. unsigned long range;
  1746. range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
  1747. idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
  1748. idr_destroy(&loop_index_idr);
  1749. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1750. unregister_blkdev(LOOP_MAJOR, "loop");
  1751. misc_deregister(&loop_misc);
  1752. }
  1753. module_init(loop_init);
  1754. module_exit(loop_exit);
  1755. #ifndef MODULE
  1756. static int __init max_loop_setup(char *str)
  1757. {
  1758. max_loop = simple_strtol(str, NULL, 0);
  1759. return 1;
  1760. }
  1761. __setup("max_loop=", max_loop_setup);
  1762. #endif