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_MUTEX(loop_mutex);
  79. static LIST_HEAD(loop_devices);
  80. static DEFINE_MUTEX(loop_devices_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, ret;
  359. ret = buf->ops->confirm(pipe, buf);
  360. if (unlikely(ret))
  361. return ret;
  362. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  363. (buf->offset >> 9);
  364. size = sd->len;
  365. if (size > p->bsize)
  366. size = p->bsize;
  367. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  368. printk(KERN_ERR "loop: transfer error block %ld\n",
  369. page->index);
  370. size = -EINVAL;
  371. }
  372. flush_dcache_page(p->page);
  373. if (size > 0)
  374. p->offset += size;
  375. return size;
  376. }
  377. static int
  378. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  379. {
  380. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  381. }
  382. static int
  383. do_lo_receive(struct loop_device *lo,
  384. struct bio_vec *bvec, int bsize, loff_t pos)
  385. {
  386. struct lo_read_data cookie;
  387. struct splice_desc sd;
  388. struct file *file;
  389. long retval;
  390. cookie.lo = lo;
  391. cookie.page = bvec->bv_page;
  392. cookie.offset = bvec->bv_offset;
  393. cookie.bsize = bsize;
  394. sd.len = 0;
  395. sd.total_len = bvec->bv_len;
  396. sd.flags = 0;
  397. sd.pos = pos;
  398. sd.u.data = &cookie;
  399. file = lo->lo_backing_file;
  400. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  401. if (retval < 0)
  402. return retval;
  403. return 0;
  404. }
  405. static int
  406. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  407. {
  408. struct bio_vec *bvec;
  409. int i, ret = 0;
  410. bio_for_each_segment(bvec, bio, i) {
  411. ret = do_lo_receive(lo, bvec, bsize, pos);
  412. if (ret < 0)
  413. break;
  414. pos += bvec->bv_len;
  415. }
  416. return ret;
  417. }
  418. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  419. {
  420. loff_t pos;
  421. int ret;
  422. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  423. if (bio_rw(bio) == WRITE) {
  424. struct file *file = lo->lo_backing_file;
  425. if (bio->bi_rw & REQ_FLUSH) {
  426. ret = vfs_fsync(file, 0);
  427. if (unlikely(ret && ret != -EINVAL)) {
  428. ret = -EIO;
  429. goto out;
  430. }
  431. }
  432. ret = lo_send(lo, bio, pos);
  433. if ((bio->bi_rw & REQ_FUA) && !ret) {
  434. ret = vfs_fsync(file, 0);
  435. if (unlikely(ret && ret != -EINVAL))
  436. ret = -EIO;
  437. }
  438. } else
  439. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  440. out:
  441. return ret;
  442. }
  443. /*
  444. * Add bio to back of pending list
  445. */
  446. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  447. {
  448. bio_list_add(&lo->lo_bio_list, bio);
  449. }
  450. /*
  451. * Grab first pending buffer
  452. */
  453. static struct bio *loop_get_bio(struct loop_device *lo)
  454. {
  455. return bio_list_pop(&lo->lo_bio_list);
  456. }
  457. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  458. {
  459. struct loop_device *lo = q->queuedata;
  460. int rw = bio_rw(old_bio);
  461. if (rw == READA)
  462. rw = READ;
  463. BUG_ON(!lo || (rw != READ && rw != WRITE));
  464. spin_lock_irq(&lo->lo_lock);
  465. if (lo->lo_state != Lo_bound)
  466. goto out;
  467. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  468. goto out;
  469. loop_add_bio(lo, old_bio);
  470. wake_up(&lo->lo_event);
  471. spin_unlock_irq(&lo->lo_lock);
  472. return 0;
  473. out:
  474. spin_unlock_irq(&lo->lo_lock);
  475. bio_io_error(old_bio);
  476. return 0;
  477. }
  478. /*
  479. * kick off io on the underlying address space
  480. */
  481. static void loop_unplug(struct request_queue *q)
  482. {
  483. struct loop_device *lo = q->queuedata;
  484. queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
  485. blk_run_address_space(lo->lo_backing_file->f_mapping);
  486. }
  487. struct switch_request {
  488. struct file *file;
  489. struct completion wait;
  490. };
  491. static void do_loop_switch(struct loop_device *, struct switch_request *);
  492. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  493. {
  494. if (unlikely(!bio->bi_bdev)) {
  495. do_loop_switch(lo, bio->bi_private);
  496. bio_put(bio);
  497. } else {
  498. int ret = do_bio_filebacked(lo, bio);
  499. bio_endio(bio, ret);
  500. }
  501. }
  502. /*
  503. * worker thread that handles reads/writes to file backed loop devices,
  504. * to avoid blocking in our make_request_fn. it also does loop decrypting
  505. * on reads for block backed loop, as that is too heavy to do from
  506. * b_end_io context where irqs may be disabled.
  507. *
  508. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  509. * calling kthread_stop(). Therefore once kthread_should_stop() is
  510. * true, make_request will not place any more requests. Therefore
  511. * once kthread_should_stop() is true and lo_bio is NULL, we are
  512. * done with the loop.
  513. */
  514. static int loop_thread(void *data)
  515. {
  516. struct loop_device *lo = data;
  517. struct bio *bio;
  518. set_user_nice(current, -20);
  519. while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
  520. wait_event_interruptible(lo->lo_event,
  521. !bio_list_empty(&lo->lo_bio_list) ||
  522. kthread_should_stop());
  523. if (bio_list_empty(&lo->lo_bio_list))
  524. continue;
  525. spin_lock_irq(&lo->lo_lock);
  526. bio = loop_get_bio(lo);
  527. spin_unlock_irq(&lo->lo_lock);
  528. BUG_ON(!bio);
  529. loop_handle_bio(lo, bio);
  530. }
  531. return 0;
  532. }
  533. /*
  534. * loop_switch performs the hard work of switching a backing store.
  535. * First it needs to flush existing IO, it does this by sending a magic
  536. * BIO down the pipe. The completion of this BIO does the actual switch.
  537. */
  538. static int loop_switch(struct loop_device *lo, struct file *file)
  539. {
  540. struct switch_request w;
  541. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  542. if (!bio)
  543. return -ENOMEM;
  544. init_completion(&w.wait);
  545. w.file = file;
  546. bio->bi_private = &w;
  547. bio->bi_bdev = NULL;
  548. loop_make_request(lo->lo_queue, bio);
  549. wait_for_completion(&w.wait);
  550. return 0;
  551. }
  552. /*
  553. * Helper to flush the IOs in loop, but keeping loop thread running
  554. */
  555. static int loop_flush(struct loop_device *lo)
  556. {
  557. /* loop not yet configured, no running thread, nothing to flush */
  558. if (!lo->lo_thread)
  559. return 0;
  560. return loop_switch(lo, NULL);
  561. }
  562. /*
  563. * Do the actual switch; called from the BIO completion routine
  564. */
  565. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  566. {
  567. struct file *file = p->file;
  568. struct file *old_file = lo->lo_backing_file;
  569. struct address_space *mapping;
  570. /* if no new file, only flush of queued bios requested */
  571. if (!file)
  572. goto out;
  573. mapping = file->f_mapping;
  574. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  575. lo->lo_backing_file = file;
  576. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  577. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  578. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  579. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  580. out:
  581. complete(&p->wait);
  582. }
  583. /*
  584. * loop_change_fd switched the backing store of a loopback device to
  585. * a new file. This is useful for operating system installers to free up
  586. * the original file and in High Availability environments to switch to
  587. * an alternative location for the content in case of server meltdown.
  588. * This can only work if the loop device is used read-only, and if the
  589. * new backing store is the same size and type as the old backing store.
  590. */
  591. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  592. unsigned int arg)
  593. {
  594. struct file *file, *old_file;
  595. struct inode *inode;
  596. int error;
  597. error = -ENXIO;
  598. if (lo->lo_state != Lo_bound)
  599. goto out;
  600. /* the loop device has to be read-only */
  601. error = -EINVAL;
  602. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  603. goto out;
  604. error = -EBADF;
  605. file = fget(arg);
  606. if (!file)
  607. goto out;
  608. inode = file->f_mapping->host;
  609. old_file = lo->lo_backing_file;
  610. error = -EINVAL;
  611. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  612. goto out_putf;
  613. /* size of the new backing store needs to be the same */
  614. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  615. goto out_putf;
  616. /* and ... switch */
  617. error = loop_switch(lo, file);
  618. if (error)
  619. goto out_putf;
  620. fput(old_file);
  621. if (max_part > 0)
  622. ioctl_by_bdev(bdev, BLKRRPART, 0);
  623. return 0;
  624. out_putf:
  625. fput(file);
  626. out:
  627. return error;
  628. }
  629. static inline int is_loop_device(struct file *file)
  630. {
  631. struct inode *i = file->f_mapping->host;
  632. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  633. }
  634. /* loop sysfs attributes */
  635. static ssize_t loop_attr_show(struct device *dev, char *page,
  636. ssize_t (*callback)(struct loop_device *, char *))
  637. {
  638. struct loop_device *l, *lo = NULL;
  639. mutex_lock(&loop_devices_mutex);
  640. list_for_each_entry(l, &loop_devices, lo_list)
  641. if (disk_to_dev(l->lo_disk) == dev) {
  642. lo = l;
  643. break;
  644. }
  645. mutex_unlock(&loop_devices_mutex);
  646. return lo ? callback(lo, page) : -EIO;
  647. }
  648. #define LOOP_ATTR_RO(_name) \
  649. static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
  650. static ssize_t loop_attr_do_show_##_name(struct device *d, \
  651. struct device_attribute *attr, char *b) \
  652. { \
  653. return loop_attr_show(d, b, loop_attr_##_name##_show); \
  654. } \
  655. static struct device_attribute loop_attr_##_name = \
  656. __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
  657. static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
  658. {
  659. ssize_t ret;
  660. char *p = NULL;
  661. mutex_lock(&lo->lo_ctl_mutex);
  662. if (lo->lo_backing_file)
  663. p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
  664. mutex_unlock(&lo->lo_ctl_mutex);
  665. if (IS_ERR_OR_NULL(p))
  666. ret = PTR_ERR(p);
  667. else {
  668. ret = strlen(p);
  669. memmove(buf, p, ret);
  670. buf[ret++] = '\n';
  671. buf[ret] = 0;
  672. }
  673. return ret;
  674. }
  675. static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
  676. {
  677. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
  678. }
  679. static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
  680. {
  681. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
  682. }
  683. static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
  684. {
  685. int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
  686. return sprintf(buf, "%s\n", autoclear ? "1" : "0");
  687. }
  688. LOOP_ATTR_RO(backing_file);
  689. LOOP_ATTR_RO(offset);
  690. LOOP_ATTR_RO(sizelimit);
  691. LOOP_ATTR_RO(autoclear);
  692. static struct attribute *loop_attrs[] = {
  693. &loop_attr_backing_file.attr,
  694. &loop_attr_offset.attr,
  695. &loop_attr_sizelimit.attr,
  696. &loop_attr_autoclear.attr,
  697. NULL,
  698. };
  699. static struct attribute_group loop_attribute_group = {
  700. .name = "loop",
  701. .attrs= loop_attrs,
  702. };
  703. static int loop_sysfs_init(struct loop_device *lo)
  704. {
  705. return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
  706. &loop_attribute_group);
  707. }
  708. static void loop_sysfs_exit(struct loop_device *lo)
  709. {
  710. sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
  711. &loop_attribute_group);
  712. }
  713. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  714. struct block_device *bdev, unsigned int arg)
  715. {
  716. struct file *file, *f;
  717. struct inode *inode;
  718. struct address_space *mapping;
  719. unsigned lo_blocksize;
  720. int lo_flags = 0;
  721. int error;
  722. loff_t size;
  723. /* This is safe, since we have a reference from open(). */
  724. __module_get(THIS_MODULE);
  725. error = -EBADF;
  726. file = fget(arg);
  727. if (!file)
  728. goto out;
  729. error = -EBUSY;
  730. if (lo->lo_state != Lo_unbound)
  731. goto out_putf;
  732. /* Avoid recursion */
  733. f = file;
  734. while (is_loop_device(f)) {
  735. struct loop_device *l;
  736. if (f->f_mapping->host->i_bdev == bdev)
  737. goto out_putf;
  738. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  739. if (l->lo_state == Lo_unbound) {
  740. error = -EINVAL;
  741. goto out_putf;
  742. }
  743. f = l->lo_backing_file;
  744. }
  745. mapping = file->f_mapping;
  746. inode = mapping->host;
  747. if (!(file->f_mode & FMODE_WRITE))
  748. lo_flags |= LO_FLAGS_READ_ONLY;
  749. error = -EINVAL;
  750. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  751. const struct address_space_operations *aops = mapping->a_ops;
  752. if (aops->write_begin)
  753. lo_flags |= LO_FLAGS_USE_AOPS;
  754. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  755. lo_flags |= LO_FLAGS_READ_ONLY;
  756. lo_blocksize = S_ISBLK(inode->i_mode) ?
  757. inode->i_bdev->bd_block_size : PAGE_SIZE;
  758. error = 0;
  759. } else {
  760. goto out_putf;
  761. }
  762. size = get_loop_size(lo, file);
  763. if ((loff_t)(sector_t)size != size) {
  764. error = -EFBIG;
  765. goto out_putf;
  766. }
  767. if (!(mode & FMODE_WRITE))
  768. lo_flags |= LO_FLAGS_READ_ONLY;
  769. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  770. lo->lo_blocksize = lo_blocksize;
  771. lo->lo_device = bdev;
  772. lo->lo_flags = lo_flags;
  773. lo->lo_backing_file = file;
  774. lo->transfer = transfer_none;
  775. lo->ioctl = NULL;
  776. lo->lo_sizelimit = 0;
  777. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  778. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  779. bio_list_init(&lo->lo_bio_list);
  780. /*
  781. * set queue make_request_fn, and add limits based on lower level
  782. * device
  783. */
  784. blk_queue_make_request(lo->lo_queue, loop_make_request);
  785. lo->lo_queue->queuedata = lo;
  786. lo->lo_queue->unplug_fn = loop_unplug;
  787. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  788. blk_queue_flush(lo->lo_queue, REQ_FLUSH);
  789. set_capacity(lo->lo_disk, size);
  790. bd_set_size(bdev, size << 9);
  791. loop_sysfs_init(lo);
  792. /* let user-space know about the new size */
  793. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  794. set_blocksize(bdev, lo_blocksize);
  795. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  796. lo->lo_number);
  797. if (IS_ERR(lo->lo_thread)) {
  798. error = PTR_ERR(lo->lo_thread);
  799. goto out_clr;
  800. }
  801. lo->lo_state = Lo_bound;
  802. wake_up_process(lo->lo_thread);
  803. if (max_part > 0)
  804. ioctl_by_bdev(bdev, BLKRRPART, 0);
  805. return 0;
  806. out_clr:
  807. loop_sysfs_exit(lo);
  808. lo->lo_thread = NULL;
  809. lo->lo_device = NULL;
  810. lo->lo_backing_file = NULL;
  811. lo->lo_flags = 0;
  812. set_capacity(lo->lo_disk, 0);
  813. invalidate_bdev(bdev);
  814. bd_set_size(bdev, 0);
  815. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  816. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  817. lo->lo_state = Lo_unbound;
  818. out_putf:
  819. fput(file);
  820. out:
  821. /* This is safe: open() is still holding a reference. */
  822. module_put(THIS_MODULE);
  823. return error;
  824. }
  825. static int
  826. loop_release_xfer(struct loop_device *lo)
  827. {
  828. int err = 0;
  829. struct loop_func_table *xfer = lo->lo_encryption;
  830. if (xfer) {
  831. if (xfer->release)
  832. err = xfer->release(lo);
  833. lo->transfer = NULL;
  834. lo->lo_encryption = NULL;
  835. module_put(xfer->owner);
  836. }
  837. return err;
  838. }
  839. static int
  840. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  841. const struct loop_info64 *i)
  842. {
  843. int err = 0;
  844. if (xfer) {
  845. struct module *owner = xfer->owner;
  846. if (!try_module_get(owner))
  847. return -EINVAL;
  848. if (xfer->init)
  849. err = xfer->init(lo, i);
  850. if (err)
  851. module_put(owner);
  852. else
  853. lo->lo_encryption = xfer;
  854. }
  855. return err;
  856. }
  857. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  858. {
  859. struct file *filp = lo->lo_backing_file;
  860. gfp_t gfp = lo->old_gfp_mask;
  861. if (lo->lo_state != Lo_bound)
  862. return -ENXIO;
  863. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  864. return -EBUSY;
  865. if (filp == NULL)
  866. return -EINVAL;
  867. spin_lock_irq(&lo->lo_lock);
  868. lo->lo_state = Lo_rundown;
  869. spin_unlock_irq(&lo->lo_lock);
  870. kthread_stop(lo->lo_thread);
  871. lo->lo_queue->unplug_fn = NULL;
  872. lo->lo_backing_file = NULL;
  873. loop_release_xfer(lo);
  874. lo->transfer = NULL;
  875. lo->ioctl = NULL;
  876. lo->lo_device = NULL;
  877. lo->lo_encryption = NULL;
  878. lo->lo_offset = 0;
  879. lo->lo_sizelimit = 0;
  880. lo->lo_encrypt_key_size = 0;
  881. lo->lo_flags = 0;
  882. lo->lo_thread = NULL;
  883. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  884. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  885. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  886. if (bdev)
  887. invalidate_bdev(bdev);
  888. set_capacity(lo->lo_disk, 0);
  889. loop_sysfs_exit(lo);
  890. if (bdev) {
  891. bd_set_size(bdev, 0);
  892. /* let user-space know about this change */
  893. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  894. }
  895. mapping_set_gfp_mask(filp->f_mapping, gfp);
  896. lo->lo_state = Lo_unbound;
  897. /* This is safe: open() is still holding a reference. */
  898. module_put(THIS_MODULE);
  899. if (max_part > 0 && bdev)
  900. ioctl_by_bdev(bdev, BLKRRPART, 0);
  901. mutex_unlock(&lo->lo_ctl_mutex);
  902. /*
  903. * Need not hold lo_ctl_mutex to fput backing file.
  904. * Calling fput holding lo_ctl_mutex triggers a circular
  905. * lock dependency possibility warning as fput can take
  906. * bd_mutex which is usually taken before lo_ctl_mutex.
  907. */
  908. fput(filp);
  909. return 0;
  910. }
  911. static int
  912. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  913. {
  914. int err;
  915. struct loop_func_table *xfer;
  916. uid_t uid = current_uid();
  917. if (lo->lo_encrypt_key_size &&
  918. lo->lo_key_owner != uid &&
  919. !capable(CAP_SYS_ADMIN))
  920. return -EPERM;
  921. if (lo->lo_state != Lo_bound)
  922. return -ENXIO;
  923. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  924. return -EINVAL;
  925. err = loop_release_xfer(lo);
  926. if (err)
  927. return err;
  928. if (info->lo_encrypt_type) {
  929. unsigned int type = info->lo_encrypt_type;
  930. if (type >= MAX_LO_CRYPT)
  931. return -EINVAL;
  932. xfer = xfer_funcs[type];
  933. if (xfer == NULL)
  934. return -EINVAL;
  935. } else
  936. xfer = NULL;
  937. err = loop_init_xfer(lo, xfer, info);
  938. if (err)
  939. return err;
  940. if (lo->lo_offset != info->lo_offset ||
  941. lo->lo_sizelimit != info->lo_sizelimit) {
  942. lo->lo_offset = info->lo_offset;
  943. lo->lo_sizelimit = info->lo_sizelimit;
  944. if (figure_loop_size(lo))
  945. return -EFBIG;
  946. }
  947. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  948. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  949. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  950. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  951. if (!xfer)
  952. xfer = &none_funcs;
  953. lo->transfer = xfer->transfer;
  954. lo->ioctl = xfer->ioctl;
  955. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  956. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  957. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  958. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  959. lo->lo_init[0] = info->lo_init[0];
  960. lo->lo_init[1] = info->lo_init[1];
  961. if (info->lo_encrypt_key_size) {
  962. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  963. info->lo_encrypt_key_size);
  964. lo->lo_key_owner = uid;
  965. }
  966. return 0;
  967. }
  968. static int
  969. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  970. {
  971. struct file *file = lo->lo_backing_file;
  972. struct kstat stat;
  973. int error;
  974. if (lo->lo_state != Lo_bound)
  975. return -ENXIO;
  976. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  977. if (error)
  978. return error;
  979. memset(info, 0, sizeof(*info));
  980. info->lo_number = lo->lo_number;
  981. info->lo_device = huge_encode_dev(stat.dev);
  982. info->lo_inode = stat.ino;
  983. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  984. info->lo_offset = lo->lo_offset;
  985. info->lo_sizelimit = lo->lo_sizelimit;
  986. info->lo_flags = lo->lo_flags;
  987. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  988. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  989. info->lo_encrypt_type =
  990. lo->lo_encryption ? lo->lo_encryption->number : 0;
  991. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  992. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  993. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  994. lo->lo_encrypt_key_size);
  995. }
  996. return 0;
  997. }
  998. static void
  999. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  1000. {
  1001. memset(info64, 0, sizeof(*info64));
  1002. info64->lo_number = info->lo_number;
  1003. info64->lo_device = info->lo_device;
  1004. info64->lo_inode = info->lo_inode;
  1005. info64->lo_rdevice = info->lo_rdevice;
  1006. info64->lo_offset = info->lo_offset;
  1007. info64->lo_sizelimit = 0;
  1008. info64->lo_encrypt_type = info->lo_encrypt_type;
  1009. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1010. info64->lo_flags = info->lo_flags;
  1011. info64->lo_init[0] = info->lo_init[0];
  1012. info64->lo_init[1] = info->lo_init[1];
  1013. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1014. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1015. else
  1016. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1017. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1018. }
  1019. static int
  1020. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  1021. {
  1022. memset(info, 0, sizeof(*info));
  1023. info->lo_number = info64->lo_number;
  1024. info->lo_device = info64->lo_device;
  1025. info->lo_inode = info64->lo_inode;
  1026. info->lo_rdevice = info64->lo_rdevice;
  1027. info->lo_offset = info64->lo_offset;
  1028. info->lo_encrypt_type = info64->lo_encrypt_type;
  1029. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1030. info->lo_flags = info64->lo_flags;
  1031. info->lo_init[0] = info64->lo_init[0];
  1032. info->lo_init[1] = info64->lo_init[1];
  1033. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1034. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1035. else
  1036. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1037. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1038. /* error in case values were truncated */
  1039. if (info->lo_device != info64->lo_device ||
  1040. info->lo_rdevice != info64->lo_rdevice ||
  1041. info->lo_inode != info64->lo_inode ||
  1042. info->lo_offset != info64->lo_offset)
  1043. return -EOVERFLOW;
  1044. return 0;
  1045. }
  1046. static int
  1047. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  1048. {
  1049. struct loop_info info;
  1050. struct loop_info64 info64;
  1051. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1052. return -EFAULT;
  1053. loop_info64_from_old(&info, &info64);
  1054. return loop_set_status(lo, &info64);
  1055. }
  1056. static int
  1057. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  1058. {
  1059. struct loop_info64 info64;
  1060. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1061. return -EFAULT;
  1062. return loop_set_status(lo, &info64);
  1063. }
  1064. static int
  1065. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  1066. struct loop_info info;
  1067. struct loop_info64 info64;
  1068. int err = 0;
  1069. if (!arg)
  1070. err = -EINVAL;
  1071. if (!err)
  1072. err = loop_get_status(lo, &info64);
  1073. if (!err)
  1074. err = loop_info64_to_old(&info64, &info);
  1075. if (!err && copy_to_user(arg, &info, sizeof(info)))
  1076. err = -EFAULT;
  1077. return err;
  1078. }
  1079. static int
  1080. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1081. struct loop_info64 info64;
  1082. int err = 0;
  1083. if (!arg)
  1084. err = -EINVAL;
  1085. if (!err)
  1086. err = loop_get_status(lo, &info64);
  1087. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1088. err = -EFAULT;
  1089. return err;
  1090. }
  1091. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1092. {
  1093. int err;
  1094. sector_t sec;
  1095. loff_t sz;
  1096. err = -ENXIO;
  1097. if (unlikely(lo->lo_state != Lo_bound))
  1098. goto out;
  1099. err = figure_loop_size(lo);
  1100. if (unlikely(err))
  1101. goto out;
  1102. sec = get_capacity(lo->lo_disk);
  1103. /* the width of sector_t may be narrow for bit-shift */
  1104. sz = sec;
  1105. sz <<= 9;
  1106. mutex_lock(&bdev->bd_mutex);
  1107. bd_set_size(bdev, sz);
  1108. /* let user-space know about the new size */
  1109. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  1110. mutex_unlock(&bdev->bd_mutex);
  1111. out:
  1112. return err;
  1113. }
  1114. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1115. unsigned int cmd, unsigned long arg)
  1116. {
  1117. struct loop_device *lo = bdev->bd_disk->private_data;
  1118. int err;
  1119. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1120. switch (cmd) {
  1121. case LOOP_SET_FD:
  1122. err = loop_set_fd(lo, mode, bdev, arg);
  1123. break;
  1124. case LOOP_CHANGE_FD:
  1125. err = loop_change_fd(lo, bdev, arg);
  1126. break;
  1127. case LOOP_CLR_FD:
  1128. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1129. err = loop_clr_fd(lo, bdev);
  1130. if (!err)
  1131. goto out_unlocked;
  1132. break;
  1133. case LOOP_SET_STATUS:
  1134. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1135. break;
  1136. case LOOP_GET_STATUS:
  1137. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1138. break;
  1139. case LOOP_SET_STATUS64:
  1140. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1141. break;
  1142. case LOOP_GET_STATUS64:
  1143. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1144. break;
  1145. case LOOP_SET_CAPACITY:
  1146. err = -EPERM;
  1147. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1148. err = loop_set_capacity(lo, bdev);
  1149. break;
  1150. default:
  1151. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1152. }
  1153. mutex_unlock(&lo->lo_ctl_mutex);
  1154. out_unlocked:
  1155. return err;
  1156. }
  1157. #ifdef CONFIG_COMPAT
  1158. struct compat_loop_info {
  1159. compat_int_t lo_number; /* ioctl r/o */
  1160. compat_dev_t lo_device; /* ioctl r/o */
  1161. compat_ulong_t lo_inode; /* ioctl r/o */
  1162. compat_dev_t lo_rdevice; /* ioctl r/o */
  1163. compat_int_t lo_offset;
  1164. compat_int_t lo_encrypt_type;
  1165. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1166. compat_int_t lo_flags; /* ioctl r/o */
  1167. char lo_name[LO_NAME_SIZE];
  1168. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1169. compat_ulong_t lo_init[2];
  1170. char reserved[4];
  1171. };
  1172. /*
  1173. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1174. * - noinlined to reduce stack space usage in main part of driver
  1175. */
  1176. static noinline int
  1177. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1178. struct loop_info64 *info64)
  1179. {
  1180. struct compat_loop_info info;
  1181. if (copy_from_user(&info, arg, sizeof(info)))
  1182. return -EFAULT;
  1183. memset(info64, 0, sizeof(*info64));
  1184. info64->lo_number = info.lo_number;
  1185. info64->lo_device = info.lo_device;
  1186. info64->lo_inode = info.lo_inode;
  1187. info64->lo_rdevice = info.lo_rdevice;
  1188. info64->lo_offset = info.lo_offset;
  1189. info64->lo_sizelimit = 0;
  1190. info64->lo_encrypt_type = info.lo_encrypt_type;
  1191. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1192. info64->lo_flags = info.lo_flags;
  1193. info64->lo_init[0] = info.lo_init[0];
  1194. info64->lo_init[1] = info.lo_init[1];
  1195. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1196. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1197. else
  1198. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1199. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1200. return 0;
  1201. }
  1202. /*
  1203. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1204. * - noinlined to reduce stack space usage in main part of driver
  1205. */
  1206. static noinline int
  1207. loop_info64_to_compat(const struct loop_info64 *info64,
  1208. struct compat_loop_info __user *arg)
  1209. {
  1210. struct compat_loop_info info;
  1211. memset(&info, 0, sizeof(info));
  1212. info.lo_number = info64->lo_number;
  1213. info.lo_device = info64->lo_device;
  1214. info.lo_inode = info64->lo_inode;
  1215. info.lo_rdevice = info64->lo_rdevice;
  1216. info.lo_offset = info64->lo_offset;
  1217. info.lo_encrypt_type = info64->lo_encrypt_type;
  1218. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1219. info.lo_flags = info64->lo_flags;
  1220. info.lo_init[0] = info64->lo_init[0];
  1221. info.lo_init[1] = info64->lo_init[1];
  1222. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1223. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1224. else
  1225. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1226. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1227. /* error in case values were truncated */
  1228. if (info.lo_device != info64->lo_device ||
  1229. info.lo_rdevice != info64->lo_rdevice ||
  1230. info.lo_inode != info64->lo_inode ||
  1231. info.lo_offset != info64->lo_offset ||
  1232. info.lo_init[0] != info64->lo_init[0] ||
  1233. info.lo_init[1] != info64->lo_init[1])
  1234. return -EOVERFLOW;
  1235. if (copy_to_user(arg, &info, sizeof(info)))
  1236. return -EFAULT;
  1237. return 0;
  1238. }
  1239. static int
  1240. loop_set_status_compat(struct loop_device *lo,
  1241. const struct compat_loop_info __user *arg)
  1242. {
  1243. struct loop_info64 info64;
  1244. int ret;
  1245. ret = loop_info64_from_compat(arg, &info64);
  1246. if (ret < 0)
  1247. return ret;
  1248. return loop_set_status(lo, &info64);
  1249. }
  1250. static int
  1251. loop_get_status_compat(struct loop_device *lo,
  1252. struct compat_loop_info __user *arg)
  1253. {
  1254. struct loop_info64 info64;
  1255. int err = 0;
  1256. if (!arg)
  1257. err = -EINVAL;
  1258. if (!err)
  1259. err = loop_get_status(lo, &info64);
  1260. if (!err)
  1261. err = loop_info64_to_compat(&info64, arg);
  1262. return err;
  1263. }
  1264. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1265. unsigned int cmd, unsigned long arg)
  1266. {
  1267. struct loop_device *lo = bdev->bd_disk->private_data;
  1268. int err;
  1269. switch(cmd) {
  1270. case LOOP_SET_STATUS:
  1271. mutex_lock(&lo->lo_ctl_mutex);
  1272. err = loop_set_status_compat(
  1273. lo, (const struct compat_loop_info __user *) arg);
  1274. mutex_unlock(&lo->lo_ctl_mutex);
  1275. break;
  1276. case LOOP_GET_STATUS:
  1277. mutex_lock(&lo->lo_ctl_mutex);
  1278. err = loop_get_status_compat(
  1279. lo, (struct compat_loop_info __user *) arg);
  1280. mutex_unlock(&lo->lo_ctl_mutex);
  1281. break;
  1282. case LOOP_SET_CAPACITY:
  1283. case LOOP_CLR_FD:
  1284. case LOOP_GET_STATUS64:
  1285. case LOOP_SET_STATUS64:
  1286. arg = (unsigned long) compat_ptr(arg);
  1287. case LOOP_SET_FD:
  1288. case LOOP_CHANGE_FD:
  1289. err = lo_ioctl(bdev, mode, cmd, arg);
  1290. break;
  1291. default:
  1292. err = -ENOIOCTLCMD;
  1293. break;
  1294. }
  1295. return err;
  1296. }
  1297. #endif
  1298. static int lo_open(struct block_device *bdev, fmode_t mode)
  1299. {
  1300. struct loop_device *lo = bdev->bd_disk->private_data;
  1301. mutex_lock(&loop_mutex);
  1302. mutex_lock(&lo->lo_ctl_mutex);
  1303. lo->lo_refcnt++;
  1304. mutex_unlock(&lo->lo_ctl_mutex);
  1305. mutex_unlock(&loop_mutex);
  1306. return 0;
  1307. }
  1308. static int lo_release(struct gendisk *disk, fmode_t mode)
  1309. {
  1310. struct loop_device *lo = disk->private_data;
  1311. int err;
  1312. mutex_lock(&loop_mutex);
  1313. mutex_lock(&lo->lo_ctl_mutex);
  1314. if (--lo->lo_refcnt)
  1315. goto out;
  1316. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1317. /*
  1318. * In autoclear mode, stop the loop thread
  1319. * and remove configuration after last close.
  1320. */
  1321. err = loop_clr_fd(lo, NULL);
  1322. if (!err)
  1323. goto out_unlocked;
  1324. } else {
  1325. /*
  1326. * Otherwise keep thread (if running) and config,
  1327. * but flush possible ongoing bios in thread.
  1328. */
  1329. loop_flush(lo);
  1330. }
  1331. out:
  1332. mutex_unlock(&lo->lo_ctl_mutex);
  1333. out_unlocked:
  1334. mutex_unlock(&loop_mutex);
  1335. return 0;
  1336. }
  1337. static const struct block_device_operations lo_fops = {
  1338. .owner = THIS_MODULE,
  1339. .open = lo_open,
  1340. .release = lo_release,
  1341. .ioctl = lo_ioctl,
  1342. #ifdef CONFIG_COMPAT
  1343. .compat_ioctl = lo_compat_ioctl,
  1344. #endif
  1345. };
  1346. /*
  1347. * And now the modules code and kernel interface.
  1348. */
  1349. static int max_loop;
  1350. module_param(max_loop, int, 0);
  1351. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1352. module_param(max_part, int, 0);
  1353. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1354. MODULE_LICENSE("GPL");
  1355. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1356. int loop_register_transfer(struct loop_func_table *funcs)
  1357. {
  1358. unsigned int n = funcs->number;
  1359. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1360. return -EINVAL;
  1361. xfer_funcs[n] = funcs;
  1362. return 0;
  1363. }
  1364. int loop_unregister_transfer(int number)
  1365. {
  1366. unsigned int n = number;
  1367. struct loop_device *lo;
  1368. struct loop_func_table *xfer;
  1369. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1370. return -EINVAL;
  1371. xfer_funcs[n] = NULL;
  1372. list_for_each_entry(lo, &loop_devices, lo_list) {
  1373. mutex_lock(&lo->lo_ctl_mutex);
  1374. if (lo->lo_encryption == xfer)
  1375. loop_release_xfer(lo);
  1376. mutex_unlock(&lo->lo_ctl_mutex);
  1377. }
  1378. return 0;
  1379. }
  1380. EXPORT_SYMBOL(loop_register_transfer);
  1381. EXPORT_SYMBOL(loop_unregister_transfer);
  1382. static struct loop_device *loop_alloc(int i)
  1383. {
  1384. struct loop_device *lo;
  1385. struct gendisk *disk;
  1386. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1387. if (!lo)
  1388. goto out;
  1389. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1390. if (!lo->lo_queue)
  1391. goto out_free_dev;
  1392. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1393. if (!disk)
  1394. goto out_free_queue;
  1395. mutex_init(&lo->lo_ctl_mutex);
  1396. lo->lo_number = i;
  1397. lo->lo_thread = NULL;
  1398. init_waitqueue_head(&lo->lo_event);
  1399. spin_lock_init(&lo->lo_lock);
  1400. disk->major = LOOP_MAJOR;
  1401. disk->first_minor = i << part_shift;
  1402. disk->fops = &lo_fops;
  1403. disk->private_data = lo;
  1404. disk->queue = lo->lo_queue;
  1405. sprintf(disk->disk_name, "loop%d", i);
  1406. return lo;
  1407. out_free_queue:
  1408. blk_cleanup_queue(lo->lo_queue);
  1409. out_free_dev:
  1410. kfree(lo);
  1411. out:
  1412. return NULL;
  1413. }
  1414. static void loop_free(struct loop_device *lo)
  1415. {
  1416. blk_cleanup_queue(lo->lo_queue);
  1417. put_disk(lo->lo_disk);
  1418. list_del(&lo->lo_list);
  1419. kfree(lo);
  1420. }
  1421. static struct loop_device *loop_init_one(int i)
  1422. {
  1423. struct loop_device *lo;
  1424. list_for_each_entry(lo, &loop_devices, lo_list) {
  1425. if (lo->lo_number == i)
  1426. return lo;
  1427. }
  1428. lo = loop_alloc(i);
  1429. if (lo) {
  1430. add_disk(lo->lo_disk);
  1431. list_add_tail(&lo->lo_list, &loop_devices);
  1432. }
  1433. return lo;
  1434. }
  1435. static void loop_del_one(struct loop_device *lo)
  1436. {
  1437. del_gendisk(lo->lo_disk);
  1438. loop_free(lo);
  1439. }
  1440. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1441. {
  1442. struct loop_device *lo;
  1443. struct kobject *kobj;
  1444. mutex_lock(&loop_devices_mutex);
  1445. lo = loop_init_one(dev & MINORMASK);
  1446. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1447. mutex_unlock(&loop_devices_mutex);
  1448. *part = 0;
  1449. return kobj;
  1450. }
  1451. static int __init loop_init(void)
  1452. {
  1453. int i, nr;
  1454. unsigned long range;
  1455. struct loop_device *lo, *next;
  1456. /*
  1457. * loop module now has a feature to instantiate underlying device
  1458. * structure on-demand, provided that there is an access dev node.
  1459. * However, this will not work well with user space tool that doesn't
  1460. * know about such "feature". In order to not break any existing
  1461. * tool, we do the following:
  1462. *
  1463. * (1) if max_loop is specified, create that many upfront, and this
  1464. * also becomes a hard limit.
  1465. * (2) if max_loop is not specified, create 8 loop device on module
  1466. * load, user can further extend loop device by create dev node
  1467. * themselves and have kernel automatically instantiate actual
  1468. * device on-demand.
  1469. */
  1470. part_shift = 0;
  1471. if (max_part > 0)
  1472. part_shift = fls(max_part);
  1473. if (max_loop > 1UL << (MINORBITS - part_shift))
  1474. return -EINVAL;
  1475. if (max_loop) {
  1476. nr = max_loop;
  1477. range = max_loop;
  1478. } else {
  1479. nr = 8;
  1480. range = 1UL << (MINORBITS - part_shift);
  1481. }
  1482. if (register_blkdev(LOOP_MAJOR, "loop"))
  1483. return -EIO;
  1484. for (i = 0; i < nr; i++) {
  1485. lo = loop_alloc(i);
  1486. if (!lo)
  1487. goto Enomem;
  1488. list_add_tail(&lo->lo_list, &loop_devices);
  1489. }
  1490. /* point of no return */
  1491. list_for_each_entry(lo, &loop_devices, lo_list)
  1492. add_disk(lo->lo_disk);
  1493. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1494. THIS_MODULE, loop_probe, NULL, NULL);
  1495. printk(KERN_INFO "loop: module loaded\n");
  1496. return 0;
  1497. Enomem:
  1498. printk(KERN_INFO "loop: out of memory\n");
  1499. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1500. loop_free(lo);
  1501. unregister_blkdev(LOOP_MAJOR, "loop");
  1502. return -ENOMEM;
  1503. }
  1504. static void __exit loop_exit(void)
  1505. {
  1506. unsigned long range;
  1507. struct loop_device *lo, *next;
  1508. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1509. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1510. loop_del_one(lo);
  1511. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1512. unregister_blkdev(LOOP_MAJOR, "loop");
  1513. }
  1514. module_init(loop_init);
  1515. module_exit(loop_exit);
  1516. #ifndef MODULE
  1517. static int __init max_loop_setup(char *str)
  1518. {
  1519. max_loop = simple_strtol(str, NULL, 0);
  1520. return 1;
  1521. }
  1522. __setup("max_loop=", max_loop_setup);
  1523. #endif