loop.c 43 KB

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