loop.c 46 KB

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