loop.c 39 KB

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