loop.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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/completion.h>
  72. #include <linux/highmem.h>
  73. #include <linux/kthread.h>
  74. #include <linux/splice.h>
  75. #include <linux/sysfs.h>
  76. #include <linux/miscdevice.h>
  77. #include <linux/falloc.h>
  78. #include <asm/uaccess.h>
  79. static DEFINE_IDR(loop_index_idr);
  80. static DEFINE_MUTEX(loop_index_mutex);
  81. static int max_part;
  82. static int part_shift;
  83. /*
  84. * Transfer functions
  85. */
  86. static int transfer_none(struct loop_device *lo, int cmd,
  87. struct page *raw_page, unsigned raw_off,
  88. struct page *loop_page, unsigned loop_off,
  89. int size, sector_t real_block)
  90. {
  91. char *raw_buf = kmap_atomic(raw_page) + raw_off;
  92. char *loop_buf = kmap_atomic(loop_page) + loop_off;
  93. if (cmd == READ)
  94. memcpy(loop_buf, raw_buf, size);
  95. else
  96. memcpy(raw_buf, loop_buf, size);
  97. kunmap_atomic(loop_buf);
  98. kunmap_atomic(raw_buf);
  99. cond_resched();
  100. return 0;
  101. }
  102. static int transfer_xor(struct loop_device *lo, int cmd,
  103. struct page *raw_page, unsigned raw_off,
  104. struct page *loop_page, unsigned loop_off,
  105. int size, sector_t real_block)
  106. {
  107. char *raw_buf = kmap_atomic(raw_page) + raw_off;
  108. char *loop_buf = kmap_atomic(loop_page) + loop_off;
  109. char *in, *out, *key;
  110. int i, keysize;
  111. if (cmd == READ) {
  112. in = raw_buf;
  113. out = loop_buf;
  114. } else {
  115. in = loop_buf;
  116. out = raw_buf;
  117. }
  118. key = lo->lo_encrypt_key;
  119. keysize = lo->lo_encrypt_key_size;
  120. for (i = 0; i < size; i++)
  121. *out++ = *in++ ^ key[(i & 511) % keysize];
  122. kunmap_atomic(loop_buf);
  123. kunmap_atomic(raw_buf);
  124. cond_resched();
  125. return 0;
  126. }
  127. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  128. {
  129. if (unlikely(info->lo_encrypt_key_size <= 0))
  130. return -EINVAL;
  131. return 0;
  132. }
  133. static struct loop_func_table none_funcs = {
  134. .number = LO_CRYPT_NONE,
  135. .transfer = transfer_none,
  136. };
  137. static struct loop_func_table xor_funcs = {
  138. .number = LO_CRYPT_XOR,
  139. .transfer = transfer_xor,
  140. .init = xor_init
  141. };
  142. /* xfer_funcs[0] is special - its release function is never called */
  143. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  144. &none_funcs,
  145. &xor_funcs
  146. };
  147. static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
  148. {
  149. loff_t size, loopsize;
  150. /* Compute loopsize in bytes */
  151. size = i_size_read(file->f_mapping->host);
  152. loopsize = size - offset;
  153. /* offset is beyond i_size, wierd but possible */
  154. if (loopsize < 0)
  155. return 0;
  156. if (sizelimit > 0 && sizelimit < loopsize)
  157. loopsize = sizelimit;
  158. /*
  159. * Unfortunately, if we want to do I/O on the device,
  160. * the number of 512-byte sectors has to fit into a sector_t.
  161. */
  162. return loopsize >> 9;
  163. }
  164. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  165. {
  166. return get_size(lo->lo_offset, lo->lo_sizelimit, file);
  167. }
  168. static int
  169. figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
  170. {
  171. loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
  172. sector_t x = (sector_t)size;
  173. if (unlikely((loff_t)x != size))
  174. return -EFBIG;
  175. if (lo->lo_offset != offset)
  176. lo->lo_offset = offset;
  177. if (lo->lo_sizelimit != sizelimit)
  178. lo->lo_sizelimit = sizelimit;
  179. set_capacity(lo->lo_disk, x);
  180. return 0;
  181. }
  182. static inline int
  183. lo_do_transfer(struct loop_device *lo, int cmd,
  184. struct page *rpage, unsigned roffs,
  185. struct page *lpage, unsigned loffs,
  186. int size, sector_t rblock)
  187. {
  188. if (unlikely(!lo->transfer))
  189. return 0;
  190. return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  191. }
  192. /**
  193. * __do_lo_send_write - helper for writing data to a loop device
  194. *
  195. * This helper just factors out common code between do_lo_send_direct_write()
  196. * and do_lo_send_write().
  197. */
  198. static int __do_lo_send_write(struct file *file,
  199. u8 *buf, const int len, loff_t pos)
  200. {
  201. ssize_t bw;
  202. mm_segment_t old_fs = get_fs();
  203. set_fs(get_ds());
  204. bw = file->f_op->write(file, buf, len, &pos);
  205. set_fs(old_fs);
  206. if (likely(bw == len))
  207. return 0;
  208. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  209. (unsigned long long)pos, len);
  210. if (bw >= 0)
  211. bw = -EIO;
  212. return bw;
  213. }
  214. /**
  215. * do_lo_send_direct_write - helper for writing data to a loop device
  216. *
  217. * This is the fast, non-transforming version that does not need double
  218. * buffering.
  219. */
  220. static int do_lo_send_direct_write(struct loop_device *lo,
  221. struct bio_vec *bvec, loff_t pos, struct page *page)
  222. {
  223. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  224. kmap(bvec->bv_page) + bvec->bv_offset,
  225. bvec->bv_len, pos);
  226. kunmap(bvec->bv_page);
  227. cond_resched();
  228. return bw;
  229. }
  230. /**
  231. * do_lo_send_write - helper for writing data to a loop device
  232. *
  233. * This is the slow, transforming version that needs to double buffer the
  234. * data as it cannot do the transformations in place without having direct
  235. * access to the destination pages of the backing file.
  236. */
  237. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  238. loff_t pos, struct page *page)
  239. {
  240. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  241. bvec->bv_offset, bvec->bv_len, pos >> 9);
  242. if (likely(!ret))
  243. return __do_lo_send_write(lo->lo_backing_file,
  244. page_address(page), bvec->bv_len,
  245. pos);
  246. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  247. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  248. if (ret > 0)
  249. ret = -EIO;
  250. return ret;
  251. }
  252. static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
  253. {
  254. int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
  255. struct page *page);
  256. struct bio_vec *bvec;
  257. struct page *page = NULL;
  258. int i, ret = 0;
  259. if (lo->transfer != transfer_none) {
  260. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  261. if (unlikely(!page))
  262. goto fail;
  263. kmap(page);
  264. do_lo_send = do_lo_send_write;
  265. } else {
  266. do_lo_send = do_lo_send_direct_write;
  267. }
  268. bio_for_each_segment(bvec, bio, i) {
  269. ret = do_lo_send(lo, bvec, pos, page);
  270. if (ret < 0)
  271. break;
  272. pos += bvec->bv_len;
  273. }
  274. if (page) {
  275. kunmap(page);
  276. __free_page(page);
  277. }
  278. out:
  279. return ret;
  280. fail:
  281. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  282. ret = -ENOMEM;
  283. goto out;
  284. }
  285. struct lo_read_data {
  286. struct loop_device *lo;
  287. struct page *page;
  288. unsigned offset;
  289. int bsize;
  290. };
  291. static int
  292. lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  293. struct splice_desc *sd)
  294. {
  295. struct lo_read_data *p = sd->u.data;
  296. struct loop_device *lo = p->lo;
  297. struct page *page = buf->page;
  298. sector_t IV;
  299. int size;
  300. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  301. (buf->offset >> 9);
  302. size = sd->len;
  303. if (size > p->bsize)
  304. size = p->bsize;
  305. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  306. printk(KERN_ERR "loop: transfer error block %ld\n",
  307. page->index);
  308. size = -EINVAL;
  309. }
  310. flush_dcache_page(p->page);
  311. if (size > 0)
  312. p->offset += size;
  313. return size;
  314. }
  315. static int
  316. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  317. {
  318. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  319. }
  320. static ssize_t
  321. do_lo_receive(struct loop_device *lo,
  322. struct bio_vec *bvec, int bsize, loff_t pos)
  323. {
  324. struct lo_read_data cookie;
  325. struct splice_desc sd;
  326. struct file *file;
  327. ssize_t retval;
  328. cookie.lo = lo;
  329. cookie.page = bvec->bv_page;
  330. cookie.offset = bvec->bv_offset;
  331. cookie.bsize = bsize;
  332. sd.len = 0;
  333. sd.total_len = bvec->bv_len;
  334. sd.flags = 0;
  335. sd.pos = pos;
  336. sd.u.data = &cookie;
  337. file = lo->lo_backing_file;
  338. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  339. return retval;
  340. }
  341. static int
  342. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  343. {
  344. struct bio_vec *bvec;
  345. ssize_t s;
  346. int i;
  347. bio_for_each_segment(bvec, bio, i) {
  348. s = do_lo_receive(lo, bvec, bsize, pos);
  349. if (s < 0)
  350. return s;
  351. if (s != bvec->bv_len) {
  352. zero_fill_bio(bio);
  353. break;
  354. }
  355. pos += bvec->bv_len;
  356. }
  357. return 0;
  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. /*
  836. * If we've explicitly asked to tear down the loop device,
  837. * and it has an elevated reference count, set it for auto-teardown when
  838. * the last reference goes away. This stops $!~#$@ udev from
  839. * preventing teardown because it decided that it needs to run blkid on
  840. * the loopback device whenever they appear. xfstests is notorious for
  841. * failing tests because blkid via udev races with a losetup
  842. * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
  843. * command to fail with EBUSY.
  844. */
  845. if (lo->lo_refcnt > 1) {
  846. lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
  847. mutex_unlock(&lo->lo_ctl_mutex);
  848. return 0;
  849. }
  850. if (filp == NULL)
  851. return -EINVAL;
  852. spin_lock_irq(&lo->lo_lock);
  853. lo->lo_state = Lo_rundown;
  854. spin_unlock_irq(&lo->lo_lock);
  855. kthread_stop(lo->lo_thread);
  856. spin_lock_irq(&lo->lo_lock);
  857. lo->lo_backing_file = NULL;
  858. spin_unlock_irq(&lo->lo_lock);
  859. loop_release_xfer(lo);
  860. lo->transfer = NULL;
  861. lo->ioctl = NULL;
  862. lo->lo_device = NULL;
  863. lo->lo_encryption = NULL;
  864. lo->lo_offset = 0;
  865. lo->lo_sizelimit = 0;
  866. lo->lo_encrypt_key_size = 0;
  867. lo->lo_thread = NULL;
  868. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  869. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  870. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  871. if (bdev)
  872. invalidate_bdev(bdev);
  873. set_capacity(lo->lo_disk, 0);
  874. loop_sysfs_exit(lo);
  875. if (bdev) {
  876. bd_set_size(bdev, 0);
  877. /* let user-space know about this change */
  878. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  879. }
  880. mapping_set_gfp_mask(filp->f_mapping, gfp);
  881. lo->lo_state = Lo_unbound;
  882. /* This is safe: open() is still holding a reference. */
  883. module_put(THIS_MODULE);
  884. if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
  885. ioctl_by_bdev(bdev, BLKRRPART, 0);
  886. lo->lo_flags = 0;
  887. if (!part_shift)
  888. lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
  889. mutex_unlock(&lo->lo_ctl_mutex);
  890. /*
  891. * Need not hold lo_ctl_mutex to fput backing file.
  892. * Calling fput holding lo_ctl_mutex triggers a circular
  893. * lock dependency possibility warning as fput can take
  894. * bd_mutex which is usually taken before lo_ctl_mutex.
  895. */
  896. fput(filp);
  897. return 0;
  898. }
  899. static int
  900. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  901. {
  902. int err;
  903. struct loop_func_table *xfer;
  904. kuid_t uid = current_uid();
  905. if (lo->lo_encrypt_key_size &&
  906. !uid_eq(lo->lo_key_owner, uid) &&
  907. !capable(CAP_SYS_ADMIN))
  908. return -EPERM;
  909. if (lo->lo_state != Lo_bound)
  910. return -ENXIO;
  911. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  912. return -EINVAL;
  913. err = loop_release_xfer(lo);
  914. if (err)
  915. return err;
  916. if (info->lo_encrypt_type) {
  917. unsigned int type = info->lo_encrypt_type;
  918. if (type >= MAX_LO_CRYPT)
  919. return -EINVAL;
  920. xfer = xfer_funcs[type];
  921. if (xfer == NULL)
  922. return -EINVAL;
  923. } else
  924. xfer = NULL;
  925. err = loop_init_xfer(lo, xfer, info);
  926. if (err)
  927. return err;
  928. if (lo->lo_offset != info->lo_offset ||
  929. lo->lo_sizelimit != info->lo_sizelimit) {
  930. if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
  931. return -EFBIG;
  932. }
  933. loop_config_discard(lo);
  934. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  935. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  936. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  937. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  938. if (!xfer)
  939. xfer = &none_funcs;
  940. lo->transfer = xfer->transfer;
  941. lo->ioctl = xfer->ioctl;
  942. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  943. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  944. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  945. if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
  946. !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
  947. lo->lo_flags |= LO_FLAGS_PARTSCAN;
  948. lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
  949. ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
  950. }
  951. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  952. lo->lo_init[0] = info->lo_init[0];
  953. lo->lo_init[1] = info->lo_init[1];
  954. if (info->lo_encrypt_key_size) {
  955. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  956. info->lo_encrypt_key_size);
  957. lo->lo_key_owner = uid;
  958. }
  959. return 0;
  960. }
  961. static int
  962. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  963. {
  964. struct file *file = lo->lo_backing_file;
  965. struct kstat stat;
  966. int error;
  967. if (lo->lo_state != Lo_bound)
  968. return -ENXIO;
  969. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  970. if (error)
  971. return error;
  972. memset(info, 0, sizeof(*info));
  973. info->lo_number = lo->lo_number;
  974. info->lo_device = huge_encode_dev(stat.dev);
  975. info->lo_inode = stat.ino;
  976. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  977. info->lo_offset = lo->lo_offset;
  978. info->lo_sizelimit = lo->lo_sizelimit;
  979. info->lo_flags = lo->lo_flags;
  980. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  981. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  982. info->lo_encrypt_type =
  983. lo->lo_encryption ? lo->lo_encryption->number : 0;
  984. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  985. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  986. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  987. lo->lo_encrypt_key_size);
  988. }
  989. return 0;
  990. }
  991. static void
  992. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  993. {
  994. memset(info64, 0, sizeof(*info64));
  995. info64->lo_number = info->lo_number;
  996. info64->lo_device = info->lo_device;
  997. info64->lo_inode = info->lo_inode;
  998. info64->lo_rdevice = info->lo_rdevice;
  999. info64->lo_offset = info->lo_offset;
  1000. info64->lo_sizelimit = 0;
  1001. info64->lo_encrypt_type = info->lo_encrypt_type;
  1002. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1003. info64->lo_flags = info->lo_flags;
  1004. info64->lo_init[0] = info->lo_init[0];
  1005. info64->lo_init[1] = info->lo_init[1];
  1006. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1007. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1008. else
  1009. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1010. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1011. }
  1012. static int
  1013. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  1014. {
  1015. memset(info, 0, sizeof(*info));
  1016. info->lo_number = info64->lo_number;
  1017. info->lo_device = info64->lo_device;
  1018. info->lo_inode = info64->lo_inode;
  1019. info->lo_rdevice = info64->lo_rdevice;
  1020. info->lo_offset = info64->lo_offset;
  1021. info->lo_encrypt_type = info64->lo_encrypt_type;
  1022. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1023. info->lo_flags = info64->lo_flags;
  1024. info->lo_init[0] = info64->lo_init[0];
  1025. info->lo_init[1] = info64->lo_init[1];
  1026. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1027. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1028. else
  1029. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1030. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1031. /* error in case values were truncated */
  1032. if (info->lo_device != info64->lo_device ||
  1033. info->lo_rdevice != info64->lo_rdevice ||
  1034. info->lo_inode != info64->lo_inode ||
  1035. info->lo_offset != info64->lo_offset)
  1036. return -EOVERFLOW;
  1037. return 0;
  1038. }
  1039. static int
  1040. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  1041. {
  1042. struct loop_info info;
  1043. struct loop_info64 info64;
  1044. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1045. return -EFAULT;
  1046. loop_info64_from_old(&info, &info64);
  1047. return loop_set_status(lo, &info64);
  1048. }
  1049. static int
  1050. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  1051. {
  1052. struct loop_info64 info64;
  1053. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1054. return -EFAULT;
  1055. return loop_set_status(lo, &info64);
  1056. }
  1057. static int
  1058. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  1059. struct loop_info info;
  1060. struct loop_info64 info64;
  1061. int err = 0;
  1062. if (!arg)
  1063. err = -EINVAL;
  1064. if (!err)
  1065. err = loop_get_status(lo, &info64);
  1066. if (!err)
  1067. err = loop_info64_to_old(&info64, &info);
  1068. if (!err && copy_to_user(arg, &info, sizeof(info)))
  1069. err = -EFAULT;
  1070. return err;
  1071. }
  1072. static int
  1073. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1074. struct loop_info64 info64;
  1075. int err = 0;
  1076. if (!arg)
  1077. err = -EINVAL;
  1078. if (!err)
  1079. err = loop_get_status(lo, &info64);
  1080. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1081. err = -EFAULT;
  1082. return err;
  1083. }
  1084. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1085. {
  1086. int err;
  1087. sector_t sec;
  1088. loff_t sz;
  1089. err = -ENXIO;
  1090. if (unlikely(lo->lo_state != Lo_bound))
  1091. goto out;
  1092. err = figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
  1093. if (unlikely(err))
  1094. goto out;
  1095. sec = get_capacity(lo->lo_disk);
  1096. /* the width of sector_t may be narrow for bit-shift */
  1097. sz = sec;
  1098. sz <<= 9;
  1099. mutex_lock(&bdev->bd_mutex);
  1100. bd_set_size(bdev, sz);
  1101. /* let user-space know about the new size */
  1102. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  1103. mutex_unlock(&bdev->bd_mutex);
  1104. out:
  1105. return err;
  1106. }
  1107. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1108. unsigned int cmd, unsigned long arg)
  1109. {
  1110. struct loop_device *lo = bdev->bd_disk->private_data;
  1111. int err;
  1112. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1113. switch (cmd) {
  1114. case LOOP_SET_FD:
  1115. err = loop_set_fd(lo, mode, bdev, arg);
  1116. break;
  1117. case LOOP_CHANGE_FD:
  1118. err = loop_change_fd(lo, bdev, arg);
  1119. break;
  1120. case LOOP_CLR_FD:
  1121. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1122. err = loop_clr_fd(lo);
  1123. if (!err)
  1124. goto out_unlocked;
  1125. break;
  1126. case LOOP_SET_STATUS:
  1127. err = -EPERM;
  1128. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1129. err = loop_set_status_old(lo,
  1130. (struct loop_info __user *)arg);
  1131. break;
  1132. case LOOP_GET_STATUS:
  1133. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1134. break;
  1135. case LOOP_SET_STATUS64:
  1136. err = -EPERM;
  1137. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1138. err = loop_set_status64(lo,
  1139. (struct loop_info64 __user *) arg);
  1140. break;
  1141. case LOOP_GET_STATUS64:
  1142. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1143. break;
  1144. case LOOP_SET_CAPACITY:
  1145. err = -EPERM;
  1146. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1147. err = loop_set_capacity(lo, bdev);
  1148. break;
  1149. default:
  1150. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1151. }
  1152. mutex_unlock(&lo->lo_ctl_mutex);
  1153. out_unlocked:
  1154. return err;
  1155. }
  1156. #ifdef CONFIG_COMPAT
  1157. struct compat_loop_info {
  1158. compat_int_t lo_number; /* ioctl r/o */
  1159. compat_dev_t lo_device; /* ioctl r/o */
  1160. compat_ulong_t lo_inode; /* ioctl r/o */
  1161. compat_dev_t lo_rdevice; /* ioctl r/o */
  1162. compat_int_t lo_offset;
  1163. compat_int_t lo_encrypt_type;
  1164. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1165. compat_int_t lo_flags; /* ioctl r/o */
  1166. char lo_name[LO_NAME_SIZE];
  1167. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1168. compat_ulong_t lo_init[2];
  1169. char reserved[4];
  1170. };
  1171. /*
  1172. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1173. * - noinlined to reduce stack space usage in main part of driver
  1174. */
  1175. static noinline int
  1176. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1177. struct loop_info64 *info64)
  1178. {
  1179. struct compat_loop_info info;
  1180. if (copy_from_user(&info, arg, sizeof(info)))
  1181. return -EFAULT;
  1182. memset(info64, 0, sizeof(*info64));
  1183. info64->lo_number = info.lo_number;
  1184. info64->lo_device = info.lo_device;
  1185. info64->lo_inode = info.lo_inode;
  1186. info64->lo_rdevice = info.lo_rdevice;
  1187. info64->lo_offset = info.lo_offset;
  1188. info64->lo_sizelimit = 0;
  1189. info64->lo_encrypt_type = info.lo_encrypt_type;
  1190. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1191. info64->lo_flags = info.lo_flags;
  1192. info64->lo_init[0] = info.lo_init[0];
  1193. info64->lo_init[1] = info.lo_init[1];
  1194. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1195. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1196. else
  1197. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1198. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1199. return 0;
  1200. }
  1201. /*
  1202. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1203. * - noinlined to reduce stack space usage in main part of driver
  1204. */
  1205. static noinline int
  1206. loop_info64_to_compat(const struct loop_info64 *info64,
  1207. struct compat_loop_info __user *arg)
  1208. {
  1209. struct compat_loop_info info;
  1210. memset(&info, 0, sizeof(info));
  1211. info.lo_number = info64->lo_number;
  1212. info.lo_device = info64->lo_device;
  1213. info.lo_inode = info64->lo_inode;
  1214. info.lo_rdevice = info64->lo_rdevice;
  1215. info.lo_offset = info64->lo_offset;
  1216. info.lo_encrypt_type = info64->lo_encrypt_type;
  1217. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1218. info.lo_flags = info64->lo_flags;
  1219. info.lo_init[0] = info64->lo_init[0];
  1220. info.lo_init[1] = info64->lo_init[1];
  1221. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1222. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1223. else
  1224. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1225. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1226. /* error in case values were truncated */
  1227. if (info.lo_device != info64->lo_device ||
  1228. info.lo_rdevice != info64->lo_rdevice ||
  1229. info.lo_inode != info64->lo_inode ||
  1230. info.lo_offset != info64->lo_offset ||
  1231. info.lo_init[0] != info64->lo_init[0] ||
  1232. info.lo_init[1] != info64->lo_init[1])
  1233. return -EOVERFLOW;
  1234. if (copy_to_user(arg, &info, sizeof(info)))
  1235. return -EFAULT;
  1236. return 0;
  1237. }
  1238. static int
  1239. loop_set_status_compat(struct loop_device *lo,
  1240. const struct compat_loop_info __user *arg)
  1241. {
  1242. struct loop_info64 info64;
  1243. int ret;
  1244. ret = loop_info64_from_compat(arg, &info64);
  1245. if (ret < 0)
  1246. return ret;
  1247. return loop_set_status(lo, &info64);
  1248. }
  1249. static int
  1250. loop_get_status_compat(struct loop_device *lo,
  1251. struct compat_loop_info __user *arg)
  1252. {
  1253. struct loop_info64 info64;
  1254. int err = 0;
  1255. if (!arg)
  1256. err = -EINVAL;
  1257. if (!err)
  1258. err = loop_get_status(lo, &info64);
  1259. if (!err)
  1260. err = loop_info64_to_compat(&info64, arg);
  1261. return err;
  1262. }
  1263. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1264. unsigned int cmd, unsigned long arg)
  1265. {
  1266. struct loop_device *lo = bdev->bd_disk->private_data;
  1267. int err;
  1268. switch(cmd) {
  1269. case LOOP_SET_STATUS:
  1270. mutex_lock(&lo->lo_ctl_mutex);
  1271. err = loop_set_status_compat(
  1272. lo, (const struct compat_loop_info __user *) arg);
  1273. mutex_unlock(&lo->lo_ctl_mutex);
  1274. break;
  1275. case LOOP_GET_STATUS:
  1276. mutex_lock(&lo->lo_ctl_mutex);
  1277. err = loop_get_status_compat(
  1278. lo, (struct compat_loop_info __user *) arg);
  1279. mutex_unlock(&lo->lo_ctl_mutex);
  1280. break;
  1281. case LOOP_SET_CAPACITY:
  1282. case LOOP_CLR_FD:
  1283. case LOOP_GET_STATUS64:
  1284. case LOOP_SET_STATUS64:
  1285. arg = (unsigned long) compat_ptr(arg);
  1286. case LOOP_SET_FD:
  1287. case LOOP_CHANGE_FD:
  1288. err = lo_ioctl(bdev, mode, cmd, arg);
  1289. break;
  1290. default:
  1291. err = -ENOIOCTLCMD;
  1292. break;
  1293. }
  1294. return err;
  1295. }
  1296. #endif
  1297. static int lo_open(struct block_device *bdev, fmode_t mode)
  1298. {
  1299. struct loop_device *lo;
  1300. int err = 0;
  1301. mutex_lock(&loop_index_mutex);
  1302. lo = bdev->bd_disk->private_data;
  1303. if (!lo) {
  1304. err = -ENXIO;
  1305. goto out;
  1306. }
  1307. mutex_lock(&lo->lo_ctl_mutex);
  1308. lo->lo_refcnt++;
  1309. mutex_unlock(&lo->lo_ctl_mutex);
  1310. out:
  1311. mutex_unlock(&loop_index_mutex);
  1312. return err;
  1313. }
  1314. static int lo_release(struct gendisk *disk, fmode_t mode)
  1315. {
  1316. struct loop_device *lo = disk->private_data;
  1317. int err;
  1318. mutex_lock(&lo->lo_ctl_mutex);
  1319. if (--lo->lo_refcnt)
  1320. goto out;
  1321. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1322. /*
  1323. * In autoclear mode, stop the loop thread
  1324. * and remove configuration after last close.
  1325. */
  1326. err = loop_clr_fd(lo);
  1327. if (!err)
  1328. goto out_unlocked;
  1329. } else {
  1330. /*
  1331. * Otherwise keep thread (if running) and config,
  1332. * but flush possible ongoing bios in thread.
  1333. */
  1334. loop_flush(lo);
  1335. }
  1336. out:
  1337. mutex_unlock(&lo->lo_ctl_mutex);
  1338. out_unlocked:
  1339. return 0;
  1340. }
  1341. static const struct block_device_operations lo_fops = {
  1342. .owner = THIS_MODULE,
  1343. .open = lo_open,
  1344. .release = lo_release,
  1345. .ioctl = lo_ioctl,
  1346. #ifdef CONFIG_COMPAT
  1347. .compat_ioctl = lo_compat_ioctl,
  1348. #endif
  1349. };
  1350. /*
  1351. * And now the modules code and kernel interface.
  1352. */
  1353. static int max_loop;
  1354. module_param(max_loop, int, S_IRUGO);
  1355. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1356. module_param(max_part, int, S_IRUGO);
  1357. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1358. MODULE_LICENSE("GPL");
  1359. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1360. int loop_register_transfer(struct loop_func_table *funcs)
  1361. {
  1362. unsigned int n = funcs->number;
  1363. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1364. return -EINVAL;
  1365. xfer_funcs[n] = funcs;
  1366. return 0;
  1367. }
  1368. static int unregister_transfer_cb(int id, void *ptr, void *data)
  1369. {
  1370. struct loop_device *lo = ptr;
  1371. struct loop_func_table *xfer = data;
  1372. mutex_lock(&lo->lo_ctl_mutex);
  1373. if (lo->lo_encryption == xfer)
  1374. loop_release_xfer(lo);
  1375. mutex_unlock(&lo->lo_ctl_mutex);
  1376. return 0;
  1377. }
  1378. int loop_unregister_transfer(int number)
  1379. {
  1380. unsigned int n = number;
  1381. struct loop_func_table *xfer;
  1382. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1383. return -EINVAL;
  1384. xfer_funcs[n] = NULL;
  1385. idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
  1386. return 0;
  1387. }
  1388. EXPORT_SYMBOL(loop_register_transfer);
  1389. EXPORT_SYMBOL(loop_unregister_transfer);
  1390. static int loop_add(struct loop_device **l, int i)
  1391. {
  1392. struct loop_device *lo;
  1393. struct gendisk *disk;
  1394. int err;
  1395. err = -ENOMEM;
  1396. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1397. if (!lo)
  1398. goto out;
  1399. if (!idr_pre_get(&loop_index_idr, GFP_KERNEL))
  1400. goto out_free_dev;
  1401. if (i >= 0) {
  1402. int m;
  1403. /* create specific i in the index */
  1404. err = idr_get_new_above(&loop_index_idr, lo, i, &m);
  1405. if (err >= 0 && i != m) {
  1406. idr_remove(&loop_index_idr, m);
  1407. err = -EEXIST;
  1408. }
  1409. } else if (i == -1) {
  1410. int m;
  1411. /* get next free nr */
  1412. err = idr_get_new(&loop_index_idr, lo, &m);
  1413. if (err >= 0)
  1414. i = m;
  1415. } else {
  1416. err = -EINVAL;
  1417. }
  1418. if (err < 0)
  1419. goto out_free_dev;
  1420. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1421. if (!lo->lo_queue)
  1422. goto out_free_dev;
  1423. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1424. if (!disk)
  1425. goto out_free_queue;
  1426. /*
  1427. * Disable partition scanning by default. The in-kernel partition
  1428. * scanning can be requested individually per-device during its
  1429. * setup. Userspace can always add and remove partitions from all
  1430. * devices. The needed partition minors are allocated from the
  1431. * extended minor space, the main loop device numbers will continue
  1432. * to match the loop minors, regardless of the number of partitions
  1433. * used.
  1434. *
  1435. * If max_part is given, partition scanning is globally enabled for
  1436. * all loop devices. The minors for the main loop devices will be
  1437. * multiples of max_part.
  1438. *
  1439. * Note: Global-for-all-devices, set-only-at-init, read-only module
  1440. * parameteters like 'max_loop' and 'max_part' make things needlessly
  1441. * complicated, are too static, inflexible and may surprise
  1442. * userspace tools. Parameters like this in general should be avoided.
  1443. */
  1444. if (!part_shift)
  1445. disk->flags |= GENHD_FL_NO_PART_SCAN;
  1446. disk->flags |= GENHD_FL_EXT_DEVT;
  1447. mutex_init(&lo->lo_ctl_mutex);
  1448. lo->lo_number = i;
  1449. lo->lo_thread = NULL;
  1450. init_waitqueue_head(&lo->lo_event);
  1451. spin_lock_init(&lo->lo_lock);
  1452. disk->major = LOOP_MAJOR;
  1453. disk->first_minor = i << part_shift;
  1454. disk->fops = &lo_fops;
  1455. disk->private_data = lo;
  1456. disk->queue = lo->lo_queue;
  1457. sprintf(disk->disk_name, "loop%d", i);
  1458. add_disk(disk);
  1459. *l = lo;
  1460. return lo->lo_number;
  1461. out_free_queue:
  1462. blk_cleanup_queue(lo->lo_queue);
  1463. out_free_dev:
  1464. kfree(lo);
  1465. out:
  1466. return err;
  1467. }
  1468. static void loop_remove(struct loop_device *lo)
  1469. {
  1470. del_gendisk(lo->lo_disk);
  1471. blk_cleanup_queue(lo->lo_queue);
  1472. put_disk(lo->lo_disk);
  1473. kfree(lo);
  1474. }
  1475. static int find_free_cb(int id, void *ptr, void *data)
  1476. {
  1477. struct loop_device *lo = ptr;
  1478. struct loop_device **l = data;
  1479. if (lo->lo_state == Lo_unbound) {
  1480. *l = lo;
  1481. return 1;
  1482. }
  1483. return 0;
  1484. }
  1485. static int loop_lookup(struct loop_device **l, int i)
  1486. {
  1487. struct loop_device *lo;
  1488. int ret = -ENODEV;
  1489. if (i < 0) {
  1490. int err;
  1491. err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
  1492. if (err == 1) {
  1493. *l = lo;
  1494. ret = lo->lo_number;
  1495. }
  1496. goto out;
  1497. }
  1498. /* lookup and return a specific i */
  1499. lo = idr_find(&loop_index_idr, i);
  1500. if (lo) {
  1501. *l = lo;
  1502. ret = lo->lo_number;
  1503. }
  1504. out:
  1505. return ret;
  1506. }
  1507. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1508. {
  1509. struct loop_device *lo;
  1510. struct kobject *kobj;
  1511. int err;
  1512. mutex_lock(&loop_index_mutex);
  1513. err = loop_lookup(&lo, MINOR(dev) >> part_shift);
  1514. if (err < 0)
  1515. err = loop_add(&lo, MINOR(dev) >> part_shift);
  1516. if (err < 0)
  1517. kobj = ERR_PTR(err);
  1518. else
  1519. kobj = get_disk(lo->lo_disk);
  1520. mutex_unlock(&loop_index_mutex);
  1521. *part = 0;
  1522. return kobj;
  1523. }
  1524. static long loop_control_ioctl(struct file *file, unsigned int cmd,
  1525. unsigned long parm)
  1526. {
  1527. struct loop_device *lo;
  1528. int ret = -ENOSYS;
  1529. mutex_lock(&loop_index_mutex);
  1530. switch (cmd) {
  1531. case LOOP_CTL_ADD:
  1532. ret = loop_lookup(&lo, parm);
  1533. if (ret >= 0) {
  1534. ret = -EEXIST;
  1535. break;
  1536. }
  1537. ret = loop_add(&lo, parm);
  1538. break;
  1539. case LOOP_CTL_REMOVE:
  1540. ret = loop_lookup(&lo, parm);
  1541. if (ret < 0)
  1542. break;
  1543. mutex_lock(&lo->lo_ctl_mutex);
  1544. if (lo->lo_state != Lo_unbound) {
  1545. ret = -EBUSY;
  1546. mutex_unlock(&lo->lo_ctl_mutex);
  1547. break;
  1548. }
  1549. if (lo->lo_refcnt > 0) {
  1550. ret = -EBUSY;
  1551. mutex_unlock(&lo->lo_ctl_mutex);
  1552. break;
  1553. }
  1554. lo->lo_disk->private_data = NULL;
  1555. mutex_unlock(&lo->lo_ctl_mutex);
  1556. idr_remove(&loop_index_idr, lo->lo_number);
  1557. loop_remove(lo);
  1558. break;
  1559. case LOOP_CTL_GET_FREE:
  1560. ret = loop_lookup(&lo, -1);
  1561. if (ret >= 0)
  1562. break;
  1563. ret = loop_add(&lo, -1);
  1564. }
  1565. mutex_unlock(&loop_index_mutex);
  1566. return ret;
  1567. }
  1568. static const struct file_operations loop_ctl_fops = {
  1569. .open = nonseekable_open,
  1570. .unlocked_ioctl = loop_control_ioctl,
  1571. .compat_ioctl = loop_control_ioctl,
  1572. .owner = THIS_MODULE,
  1573. .llseek = noop_llseek,
  1574. };
  1575. static struct miscdevice loop_misc = {
  1576. .minor = LOOP_CTRL_MINOR,
  1577. .name = "loop-control",
  1578. .fops = &loop_ctl_fops,
  1579. };
  1580. MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
  1581. MODULE_ALIAS("devname:loop-control");
  1582. static int __init loop_init(void)
  1583. {
  1584. int i, nr;
  1585. unsigned long range;
  1586. struct loop_device *lo;
  1587. int err;
  1588. err = misc_register(&loop_misc);
  1589. if (err < 0)
  1590. return err;
  1591. part_shift = 0;
  1592. if (max_part > 0) {
  1593. part_shift = fls(max_part);
  1594. /*
  1595. * Adjust max_part according to part_shift as it is exported
  1596. * to user space so that user can decide correct minor number
  1597. * if [s]he want to create more devices.
  1598. *
  1599. * Note that -1 is required because partition 0 is reserved
  1600. * for the whole disk.
  1601. */
  1602. max_part = (1UL << part_shift) - 1;
  1603. }
  1604. if ((1UL << part_shift) > DISK_MAX_PARTS)
  1605. return -EINVAL;
  1606. if (max_loop > 1UL << (MINORBITS - part_shift))
  1607. return -EINVAL;
  1608. /*
  1609. * If max_loop is specified, create that many devices upfront.
  1610. * This also becomes a hard limit. If max_loop is not specified,
  1611. * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
  1612. * init time. Loop devices can be requested on-demand with the
  1613. * /dev/loop-control interface, or be instantiated by accessing
  1614. * a 'dead' device node.
  1615. */
  1616. if (max_loop) {
  1617. nr = max_loop;
  1618. range = max_loop << part_shift;
  1619. } else {
  1620. nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
  1621. range = 1UL << MINORBITS;
  1622. }
  1623. if (register_blkdev(LOOP_MAJOR, "loop"))
  1624. return -EIO;
  1625. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1626. THIS_MODULE, loop_probe, NULL, NULL);
  1627. /* pre-create number of devices given by config or max_loop */
  1628. mutex_lock(&loop_index_mutex);
  1629. for (i = 0; i < nr; i++)
  1630. loop_add(&lo, i);
  1631. mutex_unlock(&loop_index_mutex);
  1632. printk(KERN_INFO "loop: module loaded\n");
  1633. return 0;
  1634. }
  1635. static int loop_exit_cb(int id, void *ptr, void *data)
  1636. {
  1637. struct loop_device *lo = ptr;
  1638. loop_remove(lo);
  1639. return 0;
  1640. }
  1641. static void __exit loop_exit(void)
  1642. {
  1643. unsigned long range;
  1644. range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
  1645. idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
  1646. idr_remove_all(&loop_index_idr);
  1647. idr_destroy(&loop_index_idr);
  1648. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1649. unregister_blkdev(LOOP_MAJOR, "loop");
  1650. misc_deregister(&loop_misc);
  1651. }
  1652. module_init(loop_init);
  1653. module_exit(loop_exit);
  1654. #ifndef MODULE
  1655. static int __init max_loop_setup(char *str)
  1656. {
  1657. max_loop = simple_strtol(str, NULL, 0);
  1658. return 1;
  1659. }
  1660. __setup("max_loop=", max_loop_setup);
  1661. #endif