loop.c 46 KB

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