loop.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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/smp_lock.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 <asm/uaccess.h>
  77. static LIST_HEAD(loop_devices);
  78. static DEFINE_MUTEX(loop_devices_mutex);
  79. static int max_part;
  80. static int part_shift;
  81. /*
  82. * Transfer functions
  83. */
  84. static int transfer_none(struct loop_device *lo, int cmd,
  85. struct page *raw_page, unsigned raw_off,
  86. struct page *loop_page, unsigned loop_off,
  87. int size, sector_t real_block)
  88. {
  89. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  90. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  91. if (cmd == READ)
  92. memcpy(loop_buf, raw_buf, size);
  93. else
  94. memcpy(raw_buf, loop_buf, size);
  95. kunmap_atomic(raw_buf, KM_USER0);
  96. kunmap_atomic(loop_buf, KM_USER1);
  97. cond_resched();
  98. return 0;
  99. }
  100. static int transfer_xor(struct loop_device *lo, int cmd,
  101. struct page *raw_page, unsigned raw_off,
  102. struct page *loop_page, unsigned loop_off,
  103. int size, sector_t real_block)
  104. {
  105. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  106. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  107. char *in, *out, *key;
  108. int i, keysize;
  109. if (cmd == READ) {
  110. in = raw_buf;
  111. out = loop_buf;
  112. } else {
  113. in = loop_buf;
  114. out = raw_buf;
  115. }
  116. key = lo->lo_encrypt_key;
  117. keysize = lo->lo_encrypt_key_size;
  118. for (i = 0; i < size; i++)
  119. *out++ = *in++ ^ key[(i & 511) % keysize];
  120. kunmap_atomic(raw_buf, KM_USER0);
  121. kunmap_atomic(loop_buf, KM_USER1);
  122. cond_resched();
  123. return 0;
  124. }
  125. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  126. {
  127. if (unlikely(info->lo_encrypt_key_size <= 0))
  128. return -EINVAL;
  129. return 0;
  130. }
  131. static struct loop_func_table none_funcs = {
  132. .number = LO_CRYPT_NONE,
  133. .transfer = transfer_none,
  134. };
  135. static struct loop_func_table xor_funcs = {
  136. .number = LO_CRYPT_XOR,
  137. .transfer = transfer_xor,
  138. .init = xor_init
  139. };
  140. /* xfer_funcs[0] is special - its release function is never called */
  141. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  142. &none_funcs,
  143. &xor_funcs
  144. };
  145. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  146. {
  147. loff_t size, offset, loopsize;
  148. /* Compute loopsize in bytes */
  149. size = i_size_read(file->f_mapping->host);
  150. offset = lo->lo_offset;
  151. loopsize = size - offset;
  152. if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
  153. loopsize = lo->lo_sizelimit;
  154. /*
  155. * Unfortunately, if we want to do I/O on the device,
  156. * the number of 512-byte sectors has to fit into a sector_t.
  157. */
  158. return loopsize >> 9;
  159. }
  160. static int
  161. figure_loop_size(struct loop_device *lo)
  162. {
  163. loff_t size = get_loop_size(lo, lo->lo_backing_file);
  164. sector_t x = (sector_t)size;
  165. if (unlikely((loff_t)x != size))
  166. return -EFBIG;
  167. set_capacity(lo->lo_disk, x);
  168. return 0;
  169. }
  170. static inline int
  171. lo_do_transfer(struct loop_device *lo, int cmd,
  172. struct page *rpage, unsigned roffs,
  173. struct page *lpage, unsigned loffs,
  174. int size, sector_t rblock)
  175. {
  176. if (unlikely(!lo->transfer))
  177. return 0;
  178. return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  179. }
  180. /**
  181. * do_lo_send_aops - helper for writing data to a loop device
  182. *
  183. * This is the fast version for backing filesystems which implement the address
  184. * space operations write_begin and write_end.
  185. */
  186. static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
  187. loff_t pos, struct page *unused)
  188. {
  189. struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  190. struct address_space *mapping = file->f_mapping;
  191. pgoff_t index;
  192. unsigned offset, bv_offs;
  193. int len, ret;
  194. mutex_lock(&mapping->host->i_mutex);
  195. index = pos >> PAGE_CACHE_SHIFT;
  196. offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
  197. bv_offs = bvec->bv_offset;
  198. len = bvec->bv_len;
  199. while (len > 0) {
  200. sector_t IV;
  201. unsigned size, copied;
  202. int transfer_result;
  203. struct page *page;
  204. void *fsdata;
  205. IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  206. size = PAGE_CACHE_SIZE - offset;
  207. if (size > len)
  208. size = len;
  209. ret = pagecache_write_begin(file, mapping, pos, size, 0,
  210. &page, &fsdata);
  211. if (ret)
  212. goto fail;
  213. file_update_time(file);
  214. transfer_result = lo_do_transfer(lo, WRITE, page, offset,
  215. bvec->bv_page, bv_offs, size, IV);
  216. copied = size;
  217. if (unlikely(transfer_result))
  218. copied = 0;
  219. ret = pagecache_write_end(file, mapping, pos, size, copied,
  220. page, fsdata);
  221. if (ret < 0 || ret != copied)
  222. goto fail;
  223. if (unlikely(transfer_result))
  224. goto fail;
  225. bv_offs += copied;
  226. len -= copied;
  227. offset = 0;
  228. index++;
  229. pos += copied;
  230. }
  231. ret = 0;
  232. out:
  233. mutex_unlock(&mapping->host->i_mutex);
  234. return ret;
  235. fail:
  236. ret = -1;
  237. goto out;
  238. }
  239. /**
  240. * __do_lo_send_write - helper for writing data to a loop device
  241. *
  242. * This helper just factors out common code between do_lo_send_direct_write()
  243. * and do_lo_send_write().
  244. */
  245. static int __do_lo_send_write(struct file *file,
  246. u8 *buf, const int len, loff_t pos)
  247. {
  248. ssize_t bw;
  249. mm_segment_t old_fs = get_fs();
  250. set_fs(get_ds());
  251. bw = file->f_op->write(file, buf, len, &pos);
  252. set_fs(old_fs);
  253. if (likely(bw == len))
  254. return 0;
  255. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  256. (unsigned long long)pos, len);
  257. if (bw >= 0)
  258. bw = -EIO;
  259. return bw;
  260. }
  261. /**
  262. * do_lo_send_direct_write - helper for writing data to a loop device
  263. *
  264. * This is the fast, non-transforming version for backing filesystems which do
  265. * not implement the address space operations write_begin and write_end.
  266. * It uses the write file operation which should be present on all writeable
  267. * filesystems.
  268. */
  269. static int do_lo_send_direct_write(struct loop_device *lo,
  270. struct bio_vec *bvec, loff_t pos, struct page *page)
  271. {
  272. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  273. kmap(bvec->bv_page) + bvec->bv_offset,
  274. bvec->bv_len, pos);
  275. kunmap(bvec->bv_page);
  276. cond_resched();
  277. return bw;
  278. }
  279. /**
  280. * do_lo_send_write - helper for writing data to a loop device
  281. *
  282. * This is the slow, transforming version for filesystems which do not
  283. * implement the address space operations write_begin and write_end. It
  284. * uses the write file operation which should be present on all writeable
  285. * filesystems.
  286. *
  287. * Using fops->write is slower than using aops->{prepare,commit}_write in the
  288. * transforming case because we need to double buffer the data as we cannot do
  289. * the transformations in place as we do not have direct access to the
  290. * destination pages of the backing file.
  291. */
  292. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  293. loff_t pos, struct page *page)
  294. {
  295. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  296. bvec->bv_offset, bvec->bv_len, pos >> 9);
  297. if (likely(!ret))
  298. return __do_lo_send_write(lo->lo_backing_file,
  299. page_address(page), bvec->bv_len,
  300. pos);
  301. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  302. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  303. if (ret > 0)
  304. ret = -EIO;
  305. return ret;
  306. }
  307. static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
  308. {
  309. int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
  310. struct page *page);
  311. struct bio_vec *bvec;
  312. struct page *page = NULL;
  313. int i, ret = 0;
  314. do_lo_send = do_lo_send_aops;
  315. if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
  316. do_lo_send = do_lo_send_direct_write;
  317. if (lo->transfer != transfer_none) {
  318. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  319. if (unlikely(!page))
  320. goto fail;
  321. kmap(page);
  322. do_lo_send = do_lo_send_write;
  323. }
  324. }
  325. bio_for_each_segment(bvec, bio, i) {
  326. ret = do_lo_send(lo, bvec, pos, page);
  327. if (ret < 0)
  328. break;
  329. pos += bvec->bv_len;
  330. }
  331. if (page) {
  332. kunmap(page);
  333. __free_page(page);
  334. }
  335. out:
  336. return ret;
  337. fail:
  338. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  339. ret = -ENOMEM;
  340. goto out;
  341. }
  342. struct lo_read_data {
  343. struct loop_device *lo;
  344. struct page *page;
  345. unsigned offset;
  346. int bsize;
  347. };
  348. static int
  349. lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  350. struct splice_desc *sd)
  351. {
  352. struct lo_read_data *p = sd->u.data;
  353. struct loop_device *lo = p->lo;
  354. struct page *page = buf->page;
  355. sector_t IV;
  356. int size, ret;
  357. ret = buf->ops->confirm(pipe, buf);
  358. if (unlikely(ret))
  359. return ret;
  360. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  361. (buf->offset >> 9);
  362. size = sd->len;
  363. if (size > p->bsize)
  364. size = p->bsize;
  365. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  366. printk(KERN_ERR "loop: transfer error block %ld\n",
  367. page->index);
  368. size = -EINVAL;
  369. }
  370. flush_dcache_page(p->page);
  371. if (size > 0)
  372. p->offset += size;
  373. return size;
  374. }
  375. static int
  376. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  377. {
  378. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  379. }
  380. static int
  381. do_lo_receive(struct loop_device *lo,
  382. struct bio_vec *bvec, int bsize, loff_t pos)
  383. {
  384. struct lo_read_data cookie;
  385. struct splice_desc sd;
  386. struct file *file;
  387. long retval;
  388. cookie.lo = lo;
  389. cookie.page = bvec->bv_page;
  390. cookie.offset = bvec->bv_offset;
  391. cookie.bsize = bsize;
  392. sd.len = 0;
  393. sd.total_len = bvec->bv_len;
  394. sd.flags = 0;
  395. sd.pos = pos;
  396. sd.u.data = &cookie;
  397. file = lo->lo_backing_file;
  398. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  399. if (retval < 0)
  400. return retval;
  401. return 0;
  402. }
  403. static int
  404. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  405. {
  406. struct bio_vec *bvec;
  407. int i, ret = 0;
  408. bio_for_each_segment(bvec, bio, i) {
  409. ret = do_lo_receive(lo, bvec, bsize, pos);
  410. if (ret < 0)
  411. break;
  412. pos += bvec->bv_len;
  413. }
  414. return ret;
  415. }
  416. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  417. {
  418. loff_t pos;
  419. int ret;
  420. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  421. if (bio_rw(bio) == WRITE) {
  422. bool barrier = !!(bio->bi_rw & REQ_HARDBARRIER);
  423. struct file *file = lo->lo_backing_file;
  424. if (barrier) {
  425. if (unlikely(!file->f_op->fsync)) {
  426. ret = -EOPNOTSUPP;
  427. goto out;
  428. }
  429. ret = vfs_fsync(file, 0);
  430. if (unlikely(ret)) {
  431. ret = -EIO;
  432. goto out;
  433. }
  434. }
  435. ret = lo_send(lo, bio, pos);
  436. if (barrier && !ret) {
  437. ret = vfs_fsync(file, 0);
  438. if (unlikely(ret))
  439. ret = -EIO;
  440. }
  441. } else
  442. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  443. out:
  444. return ret;
  445. }
  446. /*
  447. * Add bio to back of pending list
  448. */
  449. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  450. {
  451. bio_list_add(&lo->lo_bio_list, bio);
  452. }
  453. /*
  454. * Grab first pending buffer
  455. */
  456. static struct bio *loop_get_bio(struct loop_device *lo)
  457. {
  458. return bio_list_pop(&lo->lo_bio_list);
  459. }
  460. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  461. {
  462. struct loop_device *lo = q->queuedata;
  463. int rw = bio_rw(old_bio);
  464. if (rw == READA)
  465. rw = READ;
  466. BUG_ON(!lo || (rw != READ && rw != WRITE));
  467. spin_lock_irq(&lo->lo_lock);
  468. if (lo->lo_state != Lo_bound)
  469. goto out;
  470. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  471. goto out;
  472. loop_add_bio(lo, old_bio);
  473. wake_up(&lo->lo_event);
  474. spin_unlock_irq(&lo->lo_lock);
  475. return 0;
  476. out:
  477. spin_unlock_irq(&lo->lo_lock);
  478. bio_io_error(old_bio);
  479. return 0;
  480. }
  481. /*
  482. * kick off io on the underlying address space
  483. */
  484. static void loop_unplug(struct request_queue *q)
  485. {
  486. struct loop_device *lo = q->queuedata;
  487. queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
  488. blk_run_address_space(lo->lo_backing_file->f_mapping);
  489. }
  490. struct switch_request {
  491. struct file *file;
  492. struct completion wait;
  493. };
  494. static void do_loop_switch(struct loop_device *, struct switch_request *);
  495. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  496. {
  497. if (unlikely(!bio->bi_bdev)) {
  498. do_loop_switch(lo, bio->bi_private);
  499. bio_put(bio);
  500. } else {
  501. int ret = do_bio_filebacked(lo, bio);
  502. bio_endio(bio, ret);
  503. }
  504. }
  505. /*
  506. * worker thread that handles reads/writes to file backed loop devices,
  507. * to avoid blocking in our make_request_fn. it also does loop decrypting
  508. * on reads for block backed loop, as that is too heavy to do from
  509. * b_end_io context where irqs may be disabled.
  510. *
  511. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  512. * calling kthread_stop(). Therefore once kthread_should_stop() is
  513. * true, make_request will not place any more requests. Therefore
  514. * once kthread_should_stop() is true and lo_bio is NULL, we are
  515. * done with the loop.
  516. */
  517. static int loop_thread(void *data)
  518. {
  519. struct loop_device *lo = data;
  520. struct bio *bio;
  521. set_user_nice(current, -20);
  522. while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
  523. wait_event_interruptible(lo->lo_event,
  524. !bio_list_empty(&lo->lo_bio_list) ||
  525. kthread_should_stop());
  526. if (bio_list_empty(&lo->lo_bio_list))
  527. continue;
  528. spin_lock_irq(&lo->lo_lock);
  529. bio = loop_get_bio(lo);
  530. spin_unlock_irq(&lo->lo_lock);
  531. BUG_ON(!bio);
  532. loop_handle_bio(lo, bio);
  533. }
  534. return 0;
  535. }
  536. /*
  537. * loop_switch performs the hard work of switching a backing store.
  538. * First it needs to flush existing IO, it does this by sending a magic
  539. * BIO down the pipe. The completion of this BIO does the actual switch.
  540. */
  541. static int loop_switch(struct loop_device *lo, struct file *file)
  542. {
  543. struct switch_request w;
  544. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  545. if (!bio)
  546. return -ENOMEM;
  547. init_completion(&w.wait);
  548. w.file = file;
  549. bio->bi_private = &w;
  550. bio->bi_bdev = NULL;
  551. loop_make_request(lo->lo_queue, bio);
  552. wait_for_completion(&w.wait);
  553. return 0;
  554. }
  555. /*
  556. * Helper to flush the IOs in loop, but keeping loop thread running
  557. */
  558. static int loop_flush(struct loop_device *lo)
  559. {
  560. /* loop not yet configured, no running thread, nothing to flush */
  561. if (!lo->lo_thread)
  562. return 0;
  563. return loop_switch(lo, NULL);
  564. }
  565. /*
  566. * Do the actual switch; called from the BIO completion routine
  567. */
  568. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  569. {
  570. struct file *file = p->file;
  571. struct file *old_file = lo->lo_backing_file;
  572. struct address_space *mapping;
  573. /* if no new file, only flush of queued bios requested */
  574. if (!file)
  575. goto out;
  576. mapping = file->f_mapping;
  577. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  578. lo->lo_backing_file = file;
  579. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  580. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  581. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  582. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  583. out:
  584. complete(&p->wait);
  585. }
  586. /*
  587. * loop_change_fd switched the backing store of a loopback device to
  588. * a new file. This is useful for operating system installers to free up
  589. * the original file and in High Availability environments to switch to
  590. * an alternative location for the content in case of server meltdown.
  591. * This can only work if the loop device is used read-only, and if the
  592. * new backing store is the same size and type as the old backing store.
  593. */
  594. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  595. unsigned int arg)
  596. {
  597. struct file *file, *old_file;
  598. struct inode *inode;
  599. int error;
  600. error = -ENXIO;
  601. if (lo->lo_state != Lo_bound)
  602. goto out;
  603. /* the loop device has to be read-only */
  604. error = -EINVAL;
  605. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  606. goto out;
  607. error = -EBADF;
  608. file = fget(arg);
  609. if (!file)
  610. goto out;
  611. inode = file->f_mapping->host;
  612. old_file = lo->lo_backing_file;
  613. error = -EINVAL;
  614. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  615. goto out_putf;
  616. /* size of the new backing store needs to be the same */
  617. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  618. goto out_putf;
  619. /* and ... switch */
  620. error = loop_switch(lo, file);
  621. if (error)
  622. goto out_putf;
  623. fput(old_file);
  624. if (max_part > 0)
  625. ioctl_by_bdev(bdev, BLKRRPART, 0);
  626. return 0;
  627. out_putf:
  628. fput(file);
  629. out:
  630. return error;
  631. }
  632. static inline int is_loop_device(struct file *file)
  633. {
  634. struct inode *i = file->f_mapping->host;
  635. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  636. }
  637. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  638. struct block_device *bdev, unsigned int arg)
  639. {
  640. struct file *file, *f;
  641. struct inode *inode;
  642. struct address_space *mapping;
  643. unsigned lo_blocksize;
  644. int lo_flags = 0;
  645. int error;
  646. loff_t size;
  647. /* This is safe, since we have a reference from open(). */
  648. __module_get(THIS_MODULE);
  649. error = -EBADF;
  650. file = fget(arg);
  651. if (!file)
  652. goto out;
  653. error = -EBUSY;
  654. if (lo->lo_state != Lo_unbound)
  655. goto out_putf;
  656. /* Avoid recursion */
  657. f = file;
  658. while (is_loop_device(f)) {
  659. struct loop_device *l;
  660. if (f->f_mapping->host->i_bdev == bdev)
  661. goto out_putf;
  662. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  663. if (l->lo_state == Lo_unbound) {
  664. error = -EINVAL;
  665. goto out_putf;
  666. }
  667. f = l->lo_backing_file;
  668. }
  669. mapping = file->f_mapping;
  670. inode = mapping->host;
  671. if (!(file->f_mode & FMODE_WRITE))
  672. lo_flags |= LO_FLAGS_READ_ONLY;
  673. error = -EINVAL;
  674. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  675. const struct address_space_operations *aops = mapping->a_ops;
  676. if (aops->write_begin)
  677. lo_flags |= LO_FLAGS_USE_AOPS;
  678. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  679. lo_flags |= LO_FLAGS_READ_ONLY;
  680. lo_blocksize = S_ISBLK(inode->i_mode) ?
  681. inode->i_bdev->bd_block_size : PAGE_SIZE;
  682. error = 0;
  683. } else {
  684. goto out_putf;
  685. }
  686. size = get_loop_size(lo, file);
  687. if ((loff_t)(sector_t)size != size) {
  688. error = -EFBIG;
  689. goto out_putf;
  690. }
  691. if (!(mode & FMODE_WRITE))
  692. lo_flags |= LO_FLAGS_READ_ONLY;
  693. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  694. lo->lo_blocksize = lo_blocksize;
  695. lo->lo_device = bdev;
  696. lo->lo_flags = lo_flags;
  697. lo->lo_backing_file = file;
  698. lo->transfer = transfer_none;
  699. lo->ioctl = NULL;
  700. lo->lo_sizelimit = 0;
  701. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  702. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  703. bio_list_init(&lo->lo_bio_list);
  704. /*
  705. * set queue make_request_fn, and add limits based on lower level
  706. * device
  707. */
  708. blk_queue_make_request(lo->lo_queue, loop_make_request);
  709. lo->lo_queue->queuedata = lo;
  710. lo->lo_queue->unplug_fn = loop_unplug;
  711. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  712. blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN);
  713. set_capacity(lo->lo_disk, size);
  714. bd_set_size(bdev, size << 9);
  715. /* let user-space know about the new size */
  716. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  717. set_blocksize(bdev, lo_blocksize);
  718. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  719. lo->lo_number);
  720. if (IS_ERR(lo->lo_thread)) {
  721. error = PTR_ERR(lo->lo_thread);
  722. goto out_clr;
  723. }
  724. lo->lo_state = Lo_bound;
  725. wake_up_process(lo->lo_thread);
  726. if (max_part > 0)
  727. ioctl_by_bdev(bdev, BLKRRPART, 0);
  728. return 0;
  729. out_clr:
  730. lo->lo_thread = NULL;
  731. lo->lo_device = NULL;
  732. lo->lo_backing_file = NULL;
  733. lo->lo_flags = 0;
  734. set_capacity(lo->lo_disk, 0);
  735. invalidate_bdev(bdev);
  736. bd_set_size(bdev, 0);
  737. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  738. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  739. lo->lo_state = Lo_unbound;
  740. out_putf:
  741. fput(file);
  742. out:
  743. /* This is safe: open() is still holding a reference. */
  744. module_put(THIS_MODULE);
  745. return error;
  746. }
  747. static int
  748. loop_release_xfer(struct loop_device *lo)
  749. {
  750. int err = 0;
  751. struct loop_func_table *xfer = lo->lo_encryption;
  752. if (xfer) {
  753. if (xfer->release)
  754. err = xfer->release(lo);
  755. lo->transfer = NULL;
  756. lo->lo_encryption = NULL;
  757. module_put(xfer->owner);
  758. }
  759. return err;
  760. }
  761. static int
  762. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  763. const struct loop_info64 *i)
  764. {
  765. int err = 0;
  766. if (xfer) {
  767. struct module *owner = xfer->owner;
  768. if (!try_module_get(owner))
  769. return -EINVAL;
  770. if (xfer->init)
  771. err = xfer->init(lo, i);
  772. if (err)
  773. module_put(owner);
  774. else
  775. lo->lo_encryption = xfer;
  776. }
  777. return err;
  778. }
  779. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  780. {
  781. struct file *filp = lo->lo_backing_file;
  782. gfp_t gfp = lo->old_gfp_mask;
  783. if (lo->lo_state != Lo_bound)
  784. return -ENXIO;
  785. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  786. return -EBUSY;
  787. if (filp == NULL)
  788. return -EINVAL;
  789. spin_lock_irq(&lo->lo_lock);
  790. lo->lo_state = Lo_rundown;
  791. spin_unlock_irq(&lo->lo_lock);
  792. kthread_stop(lo->lo_thread);
  793. lo->lo_queue->unplug_fn = NULL;
  794. lo->lo_backing_file = NULL;
  795. loop_release_xfer(lo);
  796. lo->transfer = NULL;
  797. lo->ioctl = NULL;
  798. lo->lo_device = NULL;
  799. lo->lo_encryption = NULL;
  800. lo->lo_offset = 0;
  801. lo->lo_sizelimit = 0;
  802. lo->lo_encrypt_key_size = 0;
  803. lo->lo_flags = 0;
  804. lo->lo_thread = NULL;
  805. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  806. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  807. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  808. if (bdev)
  809. invalidate_bdev(bdev);
  810. set_capacity(lo->lo_disk, 0);
  811. if (bdev) {
  812. bd_set_size(bdev, 0);
  813. /* let user-space know about this change */
  814. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  815. }
  816. mapping_set_gfp_mask(filp->f_mapping, gfp);
  817. lo->lo_state = Lo_unbound;
  818. /* This is safe: open() is still holding a reference. */
  819. module_put(THIS_MODULE);
  820. if (max_part > 0 && bdev)
  821. ioctl_by_bdev(bdev, BLKRRPART, 0);
  822. mutex_unlock(&lo->lo_ctl_mutex);
  823. /*
  824. * Need not hold lo_ctl_mutex to fput backing file.
  825. * Calling fput holding lo_ctl_mutex triggers a circular
  826. * lock dependency possibility warning as fput can take
  827. * bd_mutex which is usually taken before lo_ctl_mutex.
  828. */
  829. fput(filp);
  830. return 0;
  831. }
  832. static int
  833. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  834. {
  835. int err;
  836. struct loop_func_table *xfer;
  837. uid_t uid = current_uid();
  838. if (lo->lo_encrypt_key_size &&
  839. lo->lo_key_owner != uid &&
  840. !capable(CAP_SYS_ADMIN))
  841. return -EPERM;
  842. if (lo->lo_state != Lo_bound)
  843. return -ENXIO;
  844. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  845. return -EINVAL;
  846. err = loop_release_xfer(lo);
  847. if (err)
  848. return err;
  849. if (info->lo_encrypt_type) {
  850. unsigned int type = info->lo_encrypt_type;
  851. if (type >= MAX_LO_CRYPT)
  852. return -EINVAL;
  853. xfer = xfer_funcs[type];
  854. if (xfer == NULL)
  855. return -EINVAL;
  856. } else
  857. xfer = NULL;
  858. err = loop_init_xfer(lo, xfer, info);
  859. if (err)
  860. return err;
  861. if (lo->lo_offset != info->lo_offset ||
  862. lo->lo_sizelimit != info->lo_sizelimit) {
  863. lo->lo_offset = info->lo_offset;
  864. lo->lo_sizelimit = info->lo_sizelimit;
  865. if (figure_loop_size(lo))
  866. return -EFBIG;
  867. }
  868. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  869. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  870. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  871. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  872. if (!xfer)
  873. xfer = &none_funcs;
  874. lo->transfer = xfer->transfer;
  875. lo->ioctl = xfer->ioctl;
  876. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  877. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  878. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  879. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  880. lo->lo_init[0] = info->lo_init[0];
  881. lo->lo_init[1] = info->lo_init[1];
  882. if (info->lo_encrypt_key_size) {
  883. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  884. info->lo_encrypt_key_size);
  885. lo->lo_key_owner = uid;
  886. }
  887. return 0;
  888. }
  889. static int
  890. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  891. {
  892. struct file *file = lo->lo_backing_file;
  893. struct kstat stat;
  894. int error;
  895. if (lo->lo_state != Lo_bound)
  896. return -ENXIO;
  897. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  898. if (error)
  899. return error;
  900. memset(info, 0, sizeof(*info));
  901. info->lo_number = lo->lo_number;
  902. info->lo_device = huge_encode_dev(stat.dev);
  903. info->lo_inode = stat.ino;
  904. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  905. info->lo_offset = lo->lo_offset;
  906. info->lo_sizelimit = lo->lo_sizelimit;
  907. info->lo_flags = lo->lo_flags;
  908. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  909. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  910. info->lo_encrypt_type =
  911. lo->lo_encryption ? lo->lo_encryption->number : 0;
  912. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  913. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  914. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  915. lo->lo_encrypt_key_size);
  916. }
  917. return 0;
  918. }
  919. static void
  920. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  921. {
  922. memset(info64, 0, sizeof(*info64));
  923. info64->lo_number = info->lo_number;
  924. info64->lo_device = info->lo_device;
  925. info64->lo_inode = info->lo_inode;
  926. info64->lo_rdevice = info->lo_rdevice;
  927. info64->lo_offset = info->lo_offset;
  928. info64->lo_sizelimit = 0;
  929. info64->lo_encrypt_type = info->lo_encrypt_type;
  930. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  931. info64->lo_flags = info->lo_flags;
  932. info64->lo_init[0] = info->lo_init[0];
  933. info64->lo_init[1] = info->lo_init[1];
  934. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  935. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  936. else
  937. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  938. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  939. }
  940. static int
  941. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  942. {
  943. memset(info, 0, sizeof(*info));
  944. info->lo_number = info64->lo_number;
  945. info->lo_device = info64->lo_device;
  946. info->lo_inode = info64->lo_inode;
  947. info->lo_rdevice = info64->lo_rdevice;
  948. info->lo_offset = info64->lo_offset;
  949. info->lo_encrypt_type = info64->lo_encrypt_type;
  950. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  951. info->lo_flags = info64->lo_flags;
  952. info->lo_init[0] = info64->lo_init[0];
  953. info->lo_init[1] = info64->lo_init[1];
  954. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  955. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  956. else
  957. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  958. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  959. /* error in case values were truncated */
  960. if (info->lo_device != info64->lo_device ||
  961. info->lo_rdevice != info64->lo_rdevice ||
  962. info->lo_inode != info64->lo_inode ||
  963. info->lo_offset != info64->lo_offset)
  964. return -EOVERFLOW;
  965. return 0;
  966. }
  967. static int
  968. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  969. {
  970. struct loop_info info;
  971. struct loop_info64 info64;
  972. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  973. return -EFAULT;
  974. loop_info64_from_old(&info, &info64);
  975. return loop_set_status(lo, &info64);
  976. }
  977. static int
  978. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  979. {
  980. struct loop_info64 info64;
  981. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  982. return -EFAULT;
  983. return loop_set_status(lo, &info64);
  984. }
  985. static int
  986. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  987. struct loop_info info;
  988. struct loop_info64 info64;
  989. int err = 0;
  990. if (!arg)
  991. err = -EINVAL;
  992. if (!err)
  993. err = loop_get_status(lo, &info64);
  994. if (!err)
  995. err = loop_info64_to_old(&info64, &info);
  996. if (!err && copy_to_user(arg, &info, sizeof(info)))
  997. err = -EFAULT;
  998. return err;
  999. }
  1000. static int
  1001. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1002. struct loop_info64 info64;
  1003. int err = 0;
  1004. if (!arg)
  1005. err = -EINVAL;
  1006. if (!err)
  1007. err = loop_get_status(lo, &info64);
  1008. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1009. err = -EFAULT;
  1010. return err;
  1011. }
  1012. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1013. {
  1014. int err;
  1015. sector_t sec;
  1016. loff_t sz;
  1017. err = -ENXIO;
  1018. if (unlikely(lo->lo_state != Lo_bound))
  1019. goto out;
  1020. err = figure_loop_size(lo);
  1021. if (unlikely(err))
  1022. goto out;
  1023. sec = get_capacity(lo->lo_disk);
  1024. /* the width of sector_t may be narrow for bit-shift */
  1025. sz = sec;
  1026. sz <<= 9;
  1027. mutex_lock(&bdev->bd_mutex);
  1028. bd_set_size(bdev, sz);
  1029. /* let user-space know about the new size */
  1030. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  1031. mutex_unlock(&bdev->bd_mutex);
  1032. out:
  1033. return err;
  1034. }
  1035. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1036. unsigned int cmd, unsigned long arg)
  1037. {
  1038. struct loop_device *lo = bdev->bd_disk->private_data;
  1039. int err;
  1040. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1041. switch (cmd) {
  1042. case LOOP_SET_FD:
  1043. err = loop_set_fd(lo, mode, bdev, arg);
  1044. break;
  1045. case LOOP_CHANGE_FD:
  1046. err = loop_change_fd(lo, bdev, arg);
  1047. break;
  1048. case LOOP_CLR_FD:
  1049. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1050. err = loop_clr_fd(lo, bdev);
  1051. if (!err)
  1052. goto out_unlocked;
  1053. break;
  1054. case LOOP_SET_STATUS:
  1055. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1056. break;
  1057. case LOOP_GET_STATUS:
  1058. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1059. break;
  1060. case LOOP_SET_STATUS64:
  1061. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1062. break;
  1063. case LOOP_GET_STATUS64:
  1064. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1065. break;
  1066. case LOOP_SET_CAPACITY:
  1067. err = -EPERM;
  1068. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1069. err = loop_set_capacity(lo, bdev);
  1070. break;
  1071. default:
  1072. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1073. }
  1074. mutex_unlock(&lo->lo_ctl_mutex);
  1075. out_unlocked:
  1076. return err;
  1077. }
  1078. #ifdef CONFIG_COMPAT
  1079. struct compat_loop_info {
  1080. compat_int_t lo_number; /* ioctl r/o */
  1081. compat_dev_t lo_device; /* ioctl r/o */
  1082. compat_ulong_t lo_inode; /* ioctl r/o */
  1083. compat_dev_t lo_rdevice; /* ioctl r/o */
  1084. compat_int_t lo_offset;
  1085. compat_int_t lo_encrypt_type;
  1086. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1087. compat_int_t lo_flags; /* ioctl r/o */
  1088. char lo_name[LO_NAME_SIZE];
  1089. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1090. compat_ulong_t lo_init[2];
  1091. char reserved[4];
  1092. };
  1093. /*
  1094. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1095. * - noinlined to reduce stack space usage in main part of driver
  1096. */
  1097. static noinline int
  1098. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1099. struct loop_info64 *info64)
  1100. {
  1101. struct compat_loop_info info;
  1102. if (copy_from_user(&info, arg, sizeof(info)))
  1103. return -EFAULT;
  1104. memset(info64, 0, sizeof(*info64));
  1105. info64->lo_number = info.lo_number;
  1106. info64->lo_device = info.lo_device;
  1107. info64->lo_inode = info.lo_inode;
  1108. info64->lo_rdevice = info.lo_rdevice;
  1109. info64->lo_offset = info.lo_offset;
  1110. info64->lo_sizelimit = 0;
  1111. info64->lo_encrypt_type = info.lo_encrypt_type;
  1112. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1113. info64->lo_flags = info.lo_flags;
  1114. info64->lo_init[0] = info.lo_init[0];
  1115. info64->lo_init[1] = info.lo_init[1];
  1116. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1117. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1118. else
  1119. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1120. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1121. return 0;
  1122. }
  1123. /*
  1124. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1125. * - noinlined to reduce stack space usage in main part of driver
  1126. */
  1127. static noinline int
  1128. loop_info64_to_compat(const struct loop_info64 *info64,
  1129. struct compat_loop_info __user *arg)
  1130. {
  1131. struct compat_loop_info info;
  1132. memset(&info, 0, sizeof(info));
  1133. info.lo_number = info64->lo_number;
  1134. info.lo_device = info64->lo_device;
  1135. info.lo_inode = info64->lo_inode;
  1136. info.lo_rdevice = info64->lo_rdevice;
  1137. info.lo_offset = info64->lo_offset;
  1138. info.lo_encrypt_type = info64->lo_encrypt_type;
  1139. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1140. info.lo_flags = info64->lo_flags;
  1141. info.lo_init[0] = info64->lo_init[0];
  1142. info.lo_init[1] = info64->lo_init[1];
  1143. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1144. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1145. else
  1146. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1147. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1148. /* error in case values were truncated */
  1149. if (info.lo_device != info64->lo_device ||
  1150. info.lo_rdevice != info64->lo_rdevice ||
  1151. info.lo_inode != info64->lo_inode ||
  1152. info.lo_offset != info64->lo_offset ||
  1153. info.lo_init[0] != info64->lo_init[0] ||
  1154. info.lo_init[1] != info64->lo_init[1])
  1155. return -EOVERFLOW;
  1156. if (copy_to_user(arg, &info, sizeof(info)))
  1157. return -EFAULT;
  1158. return 0;
  1159. }
  1160. static int
  1161. loop_set_status_compat(struct loop_device *lo,
  1162. const struct compat_loop_info __user *arg)
  1163. {
  1164. struct loop_info64 info64;
  1165. int ret;
  1166. ret = loop_info64_from_compat(arg, &info64);
  1167. if (ret < 0)
  1168. return ret;
  1169. return loop_set_status(lo, &info64);
  1170. }
  1171. static int
  1172. loop_get_status_compat(struct loop_device *lo,
  1173. struct compat_loop_info __user *arg)
  1174. {
  1175. struct loop_info64 info64;
  1176. int err = 0;
  1177. if (!arg)
  1178. err = -EINVAL;
  1179. if (!err)
  1180. err = loop_get_status(lo, &info64);
  1181. if (!err)
  1182. err = loop_info64_to_compat(&info64, arg);
  1183. return err;
  1184. }
  1185. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1186. unsigned int cmd, unsigned long arg)
  1187. {
  1188. struct loop_device *lo = bdev->bd_disk->private_data;
  1189. int err;
  1190. switch(cmd) {
  1191. case LOOP_SET_STATUS:
  1192. mutex_lock(&lo->lo_ctl_mutex);
  1193. err = loop_set_status_compat(
  1194. lo, (const struct compat_loop_info __user *) arg);
  1195. mutex_unlock(&lo->lo_ctl_mutex);
  1196. break;
  1197. case LOOP_GET_STATUS:
  1198. mutex_lock(&lo->lo_ctl_mutex);
  1199. err = loop_get_status_compat(
  1200. lo, (struct compat_loop_info __user *) arg);
  1201. mutex_unlock(&lo->lo_ctl_mutex);
  1202. break;
  1203. case LOOP_SET_CAPACITY:
  1204. case LOOP_CLR_FD:
  1205. case LOOP_GET_STATUS64:
  1206. case LOOP_SET_STATUS64:
  1207. arg = (unsigned long) compat_ptr(arg);
  1208. case LOOP_SET_FD:
  1209. case LOOP_CHANGE_FD:
  1210. err = lo_ioctl(bdev, mode, cmd, arg);
  1211. break;
  1212. default:
  1213. err = -ENOIOCTLCMD;
  1214. break;
  1215. }
  1216. return err;
  1217. }
  1218. #endif
  1219. static int lo_open(struct block_device *bdev, fmode_t mode)
  1220. {
  1221. struct loop_device *lo = bdev->bd_disk->private_data;
  1222. lock_kernel();
  1223. mutex_lock(&lo->lo_ctl_mutex);
  1224. lo->lo_refcnt++;
  1225. mutex_unlock(&lo->lo_ctl_mutex);
  1226. unlock_kernel();
  1227. return 0;
  1228. }
  1229. static int lo_release(struct gendisk *disk, fmode_t mode)
  1230. {
  1231. struct loop_device *lo = disk->private_data;
  1232. int err;
  1233. lock_kernel();
  1234. mutex_lock(&lo->lo_ctl_mutex);
  1235. if (--lo->lo_refcnt)
  1236. goto out;
  1237. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1238. /*
  1239. * In autoclear mode, stop the loop thread
  1240. * and remove configuration after last close.
  1241. */
  1242. err = loop_clr_fd(lo, NULL);
  1243. if (!err)
  1244. goto out_unlocked;
  1245. } else {
  1246. /*
  1247. * Otherwise keep thread (if running) and config,
  1248. * but flush possible ongoing bios in thread.
  1249. */
  1250. loop_flush(lo);
  1251. }
  1252. out:
  1253. mutex_unlock(&lo->lo_ctl_mutex);
  1254. out_unlocked:
  1255. lock_kernel();
  1256. return 0;
  1257. }
  1258. static const struct block_device_operations lo_fops = {
  1259. .owner = THIS_MODULE,
  1260. .open = lo_open,
  1261. .release = lo_release,
  1262. .ioctl = lo_ioctl,
  1263. #ifdef CONFIG_COMPAT
  1264. .compat_ioctl = lo_compat_ioctl,
  1265. #endif
  1266. };
  1267. /*
  1268. * And now the modules code and kernel interface.
  1269. */
  1270. static int max_loop;
  1271. module_param(max_loop, int, 0);
  1272. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1273. module_param(max_part, int, 0);
  1274. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1275. MODULE_LICENSE("GPL");
  1276. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1277. int loop_register_transfer(struct loop_func_table *funcs)
  1278. {
  1279. unsigned int n = funcs->number;
  1280. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1281. return -EINVAL;
  1282. xfer_funcs[n] = funcs;
  1283. return 0;
  1284. }
  1285. int loop_unregister_transfer(int number)
  1286. {
  1287. unsigned int n = number;
  1288. struct loop_device *lo;
  1289. struct loop_func_table *xfer;
  1290. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1291. return -EINVAL;
  1292. xfer_funcs[n] = NULL;
  1293. list_for_each_entry(lo, &loop_devices, lo_list) {
  1294. mutex_lock(&lo->lo_ctl_mutex);
  1295. if (lo->lo_encryption == xfer)
  1296. loop_release_xfer(lo);
  1297. mutex_unlock(&lo->lo_ctl_mutex);
  1298. }
  1299. return 0;
  1300. }
  1301. EXPORT_SYMBOL(loop_register_transfer);
  1302. EXPORT_SYMBOL(loop_unregister_transfer);
  1303. static struct loop_device *loop_alloc(int i)
  1304. {
  1305. struct loop_device *lo;
  1306. struct gendisk *disk;
  1307. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1308. if (!lo)
  1309. goto out;
  1310. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1311. if (!lo->lo_queue)
  1312. goto out_free_dev;
  1313. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1314. if (!disk)
  1315. goto out_free_queue;
  1316. mutex_init(&lo->lo_ctl_mutex);
  1317. lo->lo_number = i;
  1318. lo->lo_thread = NULL;
  1319. init_waitqueue_head(&lo->lo_event);
  1320. spin_lock_init(&lo->lo_lock);
  1321. disk->major = LOOP_MAJOR;
  1322. disk->first_minor = i << part_shift;
  1323. disk->fops = &lo_fops;
  1324. disk->private_data = lo;
  1325. disk->queue = lo->lo_queue;
  1326. sprintf(disk->disk_name, "loop%d", i);
  1327. return lo;
  1328. out_free_queue:
  1329. blk_cleanup_queue(lo->lo_queue);
  1330. out_free_dev:
  1331. kfree(lo);
  1332. out:
  1333. return NULL;
  1334. }
  1335. static void loop_free(struct loop_device *lo)
  1336. {
  1337. blk_cleanup_queue(lo->lo_queue);
  1338. put_disk(lo->lo_disk);
  1339. list_del(&lo->lo_list);
  1340. kfree(lo);
  1341. }
  1342. static struct loop_device *loop_init_one(int i)
  1343. {
  1344. struct loop_device *lo;
  1345. list_for_each_entry(lo, &loop_devices, lo_list) {
  1346. if (lo->lo_number == i)
  1347. return lo;
  1348. }
  1349. lo = loop_alloc(i);
  1350. if (lo) {
  1351. add_disk(lo->lo_disk);
  1352. list_add_tail(&lo->lo_list, &loop_devices);
  1353. }
  1354. return lo;
  1355. }
  1356. static void loop_del_one(struct loop_device *lo)
  1357. {
  1358. del_gendisk(lo->lo_disk);
  1359. loop_free(lo);
  1360. }
  1361. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1362. {
  1363. struct loop_device *lo;
  1364. struct kobject *kobj;
  1365. mutex_lock(&loop_devices_mutex);
  1366. lo = loop_init_one(dev & MINORMASK);
  1367. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1368. mutex_unlock(&loop_devices_mutex);
  1369. *part = 0;
  1370. return kobj;
  1371. }
  1372. static int __init loop_init(void)
  1373. {
  1374. int i, nr;
  1375. unsigned long range;
  1376. struct loop_device *lo, *next;
  1377. /*
  1378. * loop module now has a feature to instantiate underlying device
  1379. * structure on-demand, provided that there is an access dev node.
  1380. * However, this will not work well with user space tool that doesn't
  1381. * know about such "feature". In order to not break any existing
  1382. * tool, we do the following:
  1383. *
  1384. * (1) if max_loop is specified, create that many upfront, and this
  1385. * also becomes a hard limit.
  1386. * (2) if max_loop is not specified, create 8 loop device on module
  1387. * load, user can further extend loop device by create dev node
  1388. * themselves and have kernel automatically instantiate actual
  1389. * device on-demand.
  1390. */
  1391. part_shift = 0;
  1392. if (max_part > 0)
  1393. part_shift = fls(max_part);
  1394. if (max_loop > 1UL << (MINORBITS - part_shift))
  1395. return -EINVAL;
  1396. if (max_loop) {
  1397. nr = max_loop;
  1398. range = max_loop;
  1399. } else {
  1400. nr = 8;
  1401. range = 1UL << (MINORBITS - part_shift);
  1402. }
  1403. if (register_blkdev(LOOP_MAJOR, "loop"))
  1404. return -EIO;
  1405. for (i = 0; i < nr; i++) {
  1406. lo = loop_alloc(i);
  1407. if (!lo)
  1408. goto Enomem;
  1409. list_add_tail(&lo->lo_list, &loop_devices);
  1410. }
  1411. /* point of no return */
  1412. list_for_each_entry(lo, &loop_devices, lo_list)
  1413. add_disk(lo->lo_disk);
  1414. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1415. THIS_MODULE, loop_probe, NULL, NULL);
  1416. printk(KERN_INFO "loop: module loaded\n");
  1417. return 0;
  1418. Enomem:
  1419. printk(KERN_INFO "loop: out of memory\n");
  1420. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1421. loop_free(lo);
  1422. unregister_blkdev(LOOP_MAJOR, "loop");
  1423. return -ENOMEM;
  1424. }
  1425. static void __exit loop_exit(void)
  1426. {
  1427. unsigned long range;
  1428. struct loop_device *lo, *next;
  1429. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1430. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1431. loop_del_one(lo);
  1432. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1433. unregister_blkdev(LOOP_MAJOR, "loop");
  1434. }
  1435. module_init(loop_init);
  1436. module_exit(loop_exit);
  1437. #ifndef MODULE
  1438. static int __init max_loop_setup(char *str)
  1439. {
  1440. max_loop = simple_strtol(str, NULL, 0);
  1441. return 1;
  1442. }
  1443. __setup("max_loop=", max_loop_setup);
  1444. #endif