loop.c 45 KB

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