loop.c 47 KB

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