loop.c 41 KB

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