loop.c 47 KB

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