loop.c 43 KB

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