loop.c 50 KB

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