loop.c 46 KB

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