loop.c 43 KB

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