loop.c 43 KB

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