loop.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. * Do the actual switch; called from the BIO completion routine
  549. */
  550. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  551. {
  552. struct file *file = p->file;
  553. struct file *old_file = lo->lo_backing_file;
  554. struct address_space *mapping = file->f_mapping;
  555. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  556. lo->lo_backing_file = file;
  557. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  558. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  559. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  560. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  561. complete(&p->wait);
  562. }
  563. /*
  564. * loop_change_fd switched the backing store of a loopback device to
  565. * a new file. This is useful for operating system installers to free up
  566. * the original file and in High Availability environments to switch to
  567. * an alternative location for the content in case of server meltdown.
  568. * This can only work if the loop device is used read-only, and if the
  569. * new backing store is the same size and type as the old backing store.
  570. */
  571. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  572. unsigned int arg)
  573. {
  574. struct file *file, *old_file;
  575. struct inode *inode;
  576. int error;
  577. error = -ENXIO;
  578. if (lo->lo_state != Lo_bound)
  579. goto out;
  580. /* the loop device has to be read-only */
  581. error = -EINVAL;
  582. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  583. goto out;
  584. error = -EBADF;
  585. file = fget(arg);
  586. if (!file)
  587. goto out;
  588. inode = file->f_mapping->host;
  589. old_file = lo->lo_backing_file;
  590. error = -EINVAL;
  591. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  592. goto out_putf;
  593. /* new backing store needs to support loop (eg splice_read) */
  594. if (!inode->i_fop->splice_read)
  595. goto out_putf;
  596. /* size of the new backing store needs to be the same */
  597. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  598. goto out_putf;
  599. /* and ... switch */
  600. error = loop_switch(lo, file);
  601. if (error)
  602. goto out_putf;
  603. fput(old_file);
  604. if (max_part > 0)
  605. ioctl_by_bdev(bdev, BLKRRPART, 0);
  606. return 0;
  607. out_putf:
  608. fput(file);
  609. out:
  610. return error;
  611. }
  612. static inline int is_loop_device(struct file *file)
  613. {
  614. struct inode *i = file->f_mapping->host;
  615. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  616. }
  617. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  618. struct block_device *bdev, unsigned int arg)
  619. {
  620. struct file *file, *f;
  621. struct inode *inode;
  622. struct address_space *mapping;
  623. unsigned lo_blocksize;
  624. int lo_flags = 0;
  625. int error;
  626. loff_t size;
  627. /* This is safe, since we have a reference from open(). */
  628. __module_get(THIS_MODULE);
  629. error = -EBADF;
  630. file = fget(arg);
  631. if (!file)
  632. goto out;
  633. error = -EBUSY;
  634. if (lo->lo_state != Lo_unbound)
  635. goto out_putf;
  636. /* Avoid recursion */
  637. f = file;
  638. while (is_loop_device(f)) {
  639. struct loop_device *l;
  640. if (f->f_mapping->host->i_bdev == bdev)
  641. goto out_putf;
  642. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  643. if (l->lo_state == Lo_unbound) {
  644. error = -EINVAL;
  645. goto out_putf;
  646. }
  647. f = l->lo_backing_file;
  648. }
  649. mapping = file->f_mapping;
  650. inode = mapping->host;
  651. if (!(file->f_mode & FMODE_WRITE))
  652. lo_flags |= LO_FLAGS_READ_ONLY;
  653. error = -EINVAL;
  654. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  655. const struct address_space_operations *aops = mapping->a_ops;
  656. /*
  657. * If we can't read - sorry. If we only can't write - well,
  658. * it's going to be read-only.
  659. */
  660. if (!file->f_op->splice_read)
  661. goto out_putf;
  662. if (aops->write_begin)
  663. lo_flags |= LO_FLAGS_USE_AOPS;
  664. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  665. lo_flags |= LO_FLAGS_READ_ONLY;
  666. lo_blocksize = S_ISBLK(inode->i_mode) ?
  667. inode->i_bdev->bd_block_size : PAGE_SIZE;
  668. error = 0;
  669. } else {
  670. goto out_putf;
  671. }
  672. size = get_loop_size(lo, file);
  673. if ((loff_t)(sector_t)size != size) {
  674. error = -EFBIG;
  675. goto out_putf;
  676. }
  677. if (!(mode & FMODE_WRITE))
  678. lo_flags |= LO_FLAGS_READ_ONLY;
  679. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  680. lo->lo_blocksize = lo_blocksize;
  681. lo->lo_device = bdev;
  682. lo->lo_flags = lo_flags;
  683. lo->lo_backing_file = file;
  684. lo->transfer = transfer_none;
  685. lo->ioctl = NULL;
  686. lo->lo_sizelimit = 0;
  687. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  688. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  689. lo->lo_bio = lo->lo_biotail = NULL;
  690. /*
  691. * set queue make_request_fn, and add limits based on lower level
  692. * device
  693. */
  694. blk_queue_make_request(lo->lo_queue, loop_make_request);
  695. lo->lo_queue->queuedata = lo;
  696. lo->lo_queue->unplug_fn = loop_unplug;
  697. set_capacity(lo->lo_disk, size);
  698. bd_set_size(bdev, size << 9);
  699. set_blocksize(bdev, lo_blocksize);
  700. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  701. lo->lo_number);
  702. if (IS_ERR(lo->lo_thread)) {
  703. error = PTR_ERR(lo->lo_thread);
  704. goto out_clr;
  705. }
  706. lo->lo_state = Lo_bound;
  707. wake_up_process(lo->lo_thread);
  708. if (max_part > 0)
  709. ioctl_by_bdev(bdev, BLKRRPART, 0);
  710. return 0;
  711. out_clr:
  712. lo->lo_thread = NULL;
  713. lo->lo_device = NULL;
  714. lo->lo_backing_file = NULL;
  715. lo->lo_flags = 0;
  716. set_capacity(lo->lo_disk, 0);
  717. invalidate_bdev(bdev);
  718. bd_set_size(bdev, 0);
  719. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  720. lo->lo_state = Lo_unbound;
  721. out_putf:
  722. fput(file);
  723. out:
  724. /* This is safe: open() is still holding a reference. */
  725. module_put(THIS_MODULE);
  726. return error;
  727. }
  728. static int
  729. loop_release_xfer(struct loop_device *lo)
  730. {
  731. int err = 0;
  732. struct loop_func_table *xfer = lo->lo_encryption;
  733. if (xfer) {
  734. if (xfer->release)
  735. err = xfer->release(lo);
  736. lo->transfer = NULL;
  737. lo->lo_encryption = NULL;
  738. module_put(xfer->owner);
  739. }
  740. return err;
  741. }
  742. static int
  743. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  744. const struct loop_info64 *i)
  745. {
  746. int err = 0;
  747. if (xfer) {
  748. struct module *owner = xfer->owner;
  749. if (!try_module_get(owner))
  750. return -EINVAL;
  751. if (xfer->init)
  752. err = xfer->init(lo, i);
  753. if (err)
  754. module_put(owner);
  755. else
  756. lo->lo_encryption = xfer;
  757. }
  758. return err;
  759. }
  760. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  761. {
  762. struct file *filp = lo->lo_backing_file;
  763. gfp_t gfp = lo->old_gfp_mask;
  764. if (lo->lo_state != Lo_bound)
  765. return -ENXIO;
  766. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  767. return -EBUSY;
  768. if (filp == NULL)
  769. return -EINVAL;
  770. spin_lock_irq(&lo->lo_lock);
  771. lo->lo_state = Lo_rundown;
  772. spin_unlock_irq(&lo->lo_lock);
  773. kthread_stop(lo->lo_thread);
  774. lo->lo_backing_file = NULL;
  775. loop_release_xfer(lo);
  776. lo->transfer = NULL;
  777. lo->ioctl = NULL;
  778. lo->lo_device = NULL;
  779. lo->lo_encryption = NULL;
  780. lo->lo_offset = 0;
  781. lo->lo_sizelimit = 0;
  782. lo->lo_encrypt_key_size = 0;
  783. lo->lo_flags = 0;
  784. lo->lo_thread = NULL;
  785. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  786. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  787. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  788. if (bdev)
  789. invalidate_bdev(bdev);
  790. set_capacity(lo->lo_disk, 0);
  791. if (bdev)
  792. bd_set_size(bdev, 0);
  793. mapping_set_gfp_mask(filp->f_mapping, gfp);
  794. lo->lo_state = Lo_unbound;
  795. fput(filp);
  796. /* This is safe: open() is still holding a reference. */
  797. module_put(THIS_MODULE);
  798. if (max_part > 0)
  799. ioctl_by_bdev(bdev, BLKRRPART, 0);
  800. return 0;
  801. }
  802. static int
  803. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  804. {
  805. int err;
  806. struct loop_func_table *xfer;
  807. uid_t uid = current_uid();
  808. if (lo->lo_encrypt_key_size &&
  809. lo->lo_key_owner != uid &&
  810. !capable(CAP_SYS_ADMIN))
  811. return -EPERM;
  812. if (lo->lo_state != Lo_bound)
  813. return -ENXIO;
  814. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  815. return -EINVAL;
  816. err = loop_release_xfer(lo);
  817. if (err)
  818. return err;
  819. if (info->lo_encrypt_type) {
  820. unsigned int type = info->lo_encrypt_type;
  821. if (type >= MAX_LO_CRYPT)
  822. return -EINVAL;
  823. xfer = xfer_funcs[type];
  824. if (xfer == NULL)
  825. return -EINVAL;
  826. } else
  827. xfer = NULL;
  828. err = loop_init_xfer(lo, xfer, info);
  829. if (err)
  830. return err;
  831. if (lo->lo_offset != info->lo_offset ||
  832. lo->lo_sizelimit != info->lo_sizelimit) {
  833. lo->lo_offset = info->lo_offset;
  834. lo->lo_sizelimit = info->lo_sizelimit;
  835. if (figure_loop_size(lo))
  836. return -EFBIG;
  837. }
  838. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  839. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  840. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  841. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  842. if (!xfer)
  843. xfer = &none_funcs;
  844. lo->transfer = xfer->transfer;
  845. lo->ioctl = xfer->ioctl;
  846. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  847. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  848. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  849. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  850. lo->lo_init[0] = info->lo_init[0];
  851. lo->lo_init[1] = info->lo_init[1];
  852. if (info->lo_encrypt_key_size) {
  853. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  854. info->lo_encrypt_key_size);
  855. lo->lo_key_owner = uid;
  856. }
  857. return 0;
  858. }
  859. static int
  860. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  861. {
  862. struct file *file = lo->lo_backing_file;
  863. struct kstat stat;
  864. int error;
  865. if (lo->lo_state != Lo_bound)
  866. return -ENXIO;
  867. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  868. if (error)
  869. return error;
  870. memset(info, 0, sizeof(*info));
  871. info->lo_number = lo->lo_number;
  872. info->lo_device = huge_encode_dev(stat.dev);
  873. info->lo_inode = stat.ino;
  874. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  875. info->lo_offset = lo->lo_offset;
  876. info->lo_sizelimit = lo->lo_sizelimit;
  877. info->lo_flags = lo->lo_flags;
  878. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  879. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  880. info->lo_encrypt_type =
  881. lo->lo_encryption ? lo->lo_encryption->number : 0;
  882. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  883. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  884. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  885. lo->lo_encrypt_key_size);
  886. }
  887. return 0;
  888. }
  889. static void
  890. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  891. {
  892. memset(info64, 0, sizeof(*info64));
  893. info64->lo_number = info->lo_number;
  894. info64->lo_device = info->lo_device;
  895. info64->lo_inode = info->lo_inode;
  896. info64->lo_rdevice = info->lo_rdevice;
  897. info64->lo_offset = info->lo_offset;
  898. info64->lo_sizelimit = 0;
  899. info64->lo_encrypt_type = info->lo_encrypt_type;
  900. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  901. info64->lo_flags = info->lo_flags;
  902. info64->lo_init[0] = info->lo_init[0];
  903. info64->lo_init[1] = info->lo_init[1];
  904. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  905. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  906. else
  907. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  908. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  909. }
  910. static int
  911. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  912. {
  913. memset(info, 0, sizeof(*info));
  914. info->lo_number = info64->lo_number;
  915. info->lo_device = info64->lo_device;
  916. info->lo_inode = info64->lo_inode;
  917. info->lo_rdevice = info64->lo_rdevice;
  918. info->lo_offset = info64->lo_offset;
  919. info->lo_encrypt_type = info64->lo_encrypt_type;
  920. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  921. info->lo_flags = info64->lo_flags;
  922. info->lo_init[0] = info64->lo_init[0];
  923. info->lo_init[1] = info64->lo_init[1];
  924. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  925. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  926. else
  927. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  928. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  929. /* error in case values were truncated */
  930. if (info->lo_device != info64->lo_device ||
  931. info->lo_rdevice != info64->lo_rdevice ||
  932. info->lo_inode != info64->lo_inode ||
  933. info->lo_offset != info64->lo_offset)
  934. return -EOVERFLOW;
  935. return 0;
  936. }
  937. static int
  938. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  939. {
  940. struct loop_info info;
  941. struct loop_info64 info64;
  942. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  943. return -EFAULT;
  944. loop_info64_from_old(&info, &info64);
  945. return loop_set_status(lo, &info64);
  946. }
  947. static int
  948. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  949. {
  950. struct loop_info64 info64;
  951. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  952. return -EFAULT;
  953. return loop_set_status(lo, &info64);
  954. }
  955. static int
  956. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  957. struct loop_info info;
  958. struct loop_info64 info64;
  959. int err = 0;
  960. if (!arg)
  961. err = -EINVAL;
  962. if (!err)
  963. err = loop_get_status(lo, &info64);
  964. if (!err)
  965. err = loop_info64_to_old(&info64, &info);
  966. if (!err && copy_to_user(arg, &info, sizeof(info)))
  967. err = -EFAULT;
  968. return err;
  969. }
  970. static int
  971. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  972. struct loop_info64 info64;
  973. int err = 0;
  974. if (!arg)
  975. err = -EINVAL;
  976. if (!err)
  977. err = loop_get_status(lo, &info64);
  978. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  979. err = -EFAULT;
  980. return err;
  981. }
  982. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  983. unsigned int cmd, unsigned long arg)
  984. {
  985. struct loop_device *lo = bdev->bd_disk->private_data;
  986. int err;
  987. mutex_lock(&lo->lo_ctl_mutex);
  988. switch (cmd) {
  989. case LOOP_SET_FD:
  990. err = loop_set_fd(lo, mode, bdev, arg);
  991. break;
  992. case LOOP_CHANGE_FD:
  993. err = loop_change_fd(lo, bdev, arg);
  994. break;
  995. case LOOP_CLR_FD:
  996. err = loop_clr_fd(lo, bdev);
  997. break;
  998. case LOOP_SET_STATUS:
  999. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1000. break;
  1001. case LOOP_GET_STATUS:
  1002. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1003. break;
  1004. case LOOP_SET_STATUS64:
  1005. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1006. break;
  1007. case LOOP_GET_STATUS64:
  1008. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1009. break;
  1010. default:
  1011. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1012. }
  1013. mutex_unlock(&lo->lo_ctl_mutex);
  1014. return err;
  1015. }
  1016. #ifdef CONFIG_COMPAT
  1017. struct compat_loop_info {
  1018. compat_int_t lo_number; /* ioctl r/o */
  1019. compat_dev_t lo_device; /* ioctl r/o */
  1020. compat_ulong_t lo_inode; /* ioctl r/o */
  1021. compat_dev_t lo_rdevice; /* ioctl r/o */
  1022. compat_int_t lo_offset;
  1023. compat_int_t lo_encrypt_type;
  1024. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1025. compat_int_t lo_flags; /* ioctl r/o */
  1026. char lo_name[LO_NAME_SIZE];
  1027. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1028. compat_ulong_t lo_init[2];
  1029. char reserved[4];
  1030. };
  1031. /*
  1032. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1033. * - noinlined to reduce stack space usage in main part of driver
  1034. */
  1035. static noinline int
  1036. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1037. struct loop_info64 *info64)
  1038. {
  1039. struct compat_loop_info info;
  1040. if (copy_from_user(&info, arg, sizeof(info)))
  1041. return -EFAULT;
  1042. memset(info64, 0, sizeof(*info64));
  1043. info64->lo_number = info.lo_number;
  1044. info64->lo_device = info.lo_device;
  1045. info64->lo_inode = info.lo_inode;
  1046. info64->lo_rdevice = info.lo_rdevice;
  1047. info64->lo_offset = info.lo_offset;
  1048. info64->lo_sizelimit = 0;
  1049. info64->lo_encrypt_type = info.lo_encrypt_type;
  1050. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1051. info64->lo_flags = info.lo_flags;
  1052. info64->lo_init[0] = info.lo_init[0];
  1053. info64->lo_init[1] = info.lo_init[1];
  1054. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1055. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1056. else
  1057. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1058. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1059. return 0;
  1060. }
  1061. /*
  1062. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1063. * - noinlined to reduce stack space usage in main part of driver
  1064. */
  1065. static noinline int
  1066. loop_info64_to_compat(const struct loop_info64 *info64,
  1067. struct compat_loop_info __user *arg)
  1068. {
  1069. struct compat_loop_info info;
  1070. memset(&info, 0, sizeof(info));
  1071. info.lo_number = info64->lo_number;
  1072. info.lo_device = info64->lo_device;
  1073. info.lo_inode = info64->lo_inode;
  1074. info.lo_rdevice = info64->lo_rdevice;
  1075. info.lo_offset = info64->lo_offset;
  1076. info.lo_encrypt_type = info64->lo_encrypt_type;
  1077. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1078. info.lo_flags = info64->lo_flags;
  1079. info.lo_init[0] = info64->lo_init[0];
  1080. info.lo_init[1] = info64->lo_init[1];
  1081. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1082. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1083. else
  1084. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1085. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1086. /* error in case values were truncated */
  1087. if (info.lo_device != info64->lo_device ||
  1088. info.lo_rdevice != info64->lo_rdevice ||
  1089. info.lo_inode != info64->lo_inode ||
  1090. info.lo_offset != info64->lo_offset ||
  1091. info.lo_init[0] != info64->lo_init[0] ||
  1092. info.lo_init[1] != info64->lo_init[1])
  1093. return -EOVERFLOW;
  1094. if (copy_to_user(arg, &info, sizeof(info)))
  1095. return -EFAULT;
  1096. return 0;
  1097. }
  1098. static int
  1099. loop_set_status_compat(struct loop_device *lo,
  1100. const struct compat_loop_info __user *arg)
  1101. {
  1102. struct loop_info64 info64;
  1103. int ret;
  1104. ret = loop_info64_from_compat(arg, &info64);
  1105. if (ret < 0)
  1106. return ret;
  1107. return loop_set_status(lo, &info64);
  1108. }
  1109. static int
  1110. loop_get_status_compat(struct loop_device *lo,
  1111. struct compat_loop_info __user *arg)
  1112. {
  1113. struct loop_info64 info64;
  1114. int err = 0;
  1115. if (!arg)
  1116. err = -EINVAL;
  1117. if (!err)
  1118. err = loop_get_status(lo, &info64);
  1119. if (!err)
  1120. err = loop_info64_to_compat(&info64, arg);
  1121. return err;
  1122. }
  1123. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1124. unsigned int cmd, unsigned long arg)
  1125. {
  1126. struct loop_device *lo = bdev->bd_disk->private_data;
  1127. int err;
  1128. switch(cmd) {
  1129. case LOOP_SET_STATUS:
  1130. mutex_lock(&lo->lo_ctl_mutex);
  1131. err = loop_set_status_compat(
  1132. lo, (const struct compat_loop_info __user *) arg);
  1133. mutex_unlock(&lo->lo_ctl_mutex);
  1134. break;
  1135. case LOOP_GET_STATUS:
  1136. mutex_lock(&lo->lo_ctl_mutex);
  1137. err = loop_get_status_compat(
  1138. lo, (struct compat_loop_info __user *) arg);
  1139. mutex_unlock(&lo->lo_ctl_mutex);
  1140. break;
  1141. case LOOP_CLR_FD:
  1142. case LOOP_GET_STATUS64:
  1143. case LOOP_SET_STATUS64:
  1144. arg = (unsigned long) compat_ptr(arg);
  1145. case LOOP_SET_FD:
  1146. case LOOP_CHANGE_FD:
  1147. err = lo_ioctl(bdev, mode, cmd, arg);
  1148. break;
  1149. default:
  1150. err = -ENOIOCTLCMD;
  1151. break;
  1152. }
  1153. return err;
  1154. }
  1155. #endif
  1156. static int lo_open(struct block_device *bdev, fmode_t mode)
  1157. {
  1158. struct loop_device *lo = bdev->bd_disk->private_data;
  1159. mutex_lock(&lo->lo_ctl_mutex);
  1160. lo->lo_refcnt++;
  1161. mutex_unlock(&lo->lo_ctl_mutex);
  1162. return 0;
  1163. }
  1164. static int lo_release(struct gendisk *disk, fmode_t mode)
  1165. {
  1166. struct loop_device *lo = disk->private_data;
  1167. mutex_lock(&lo->lo_ctl_mutex);
  1168. --lo->lo_refcnt;
  1169. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) && !lo->lo_refcnt)
  1170. loop_clr_fd(lo, NULL);
  1171. mutex_unlock(&lo->lo_ctl_mutex);
  1172. return 0;
  1173. }
  1174. static struct block_device_operations lo_fops = {
  1175. .owner = THIS_MODULE,
  1176. .open = lo_open,
  1177. .release = lo_release,
  1178. .ioctl = lo_ioctl,
  1179. #ifdef CONFIG_COMPAT
  1180. .compat_ioctl = lo_compat_ioctl,
  1181. #endif
  1182. };
  1183. /*
  1184. * And now the modules code and kernel interface.
  1185. */
  1186. static int max_loop;
  1187. module_param(max_loop, int, 0);
  1188. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1189. module_param(max_part, int, 0);
  1190. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1191. MODULE_LICENSE("GPL");
  1192. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1193. int loop_register_transfer(struct loop_func_table *funcs)
  1194. {
  1195. unsigned int n = funcs->number;
  1196. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1197. return -EINVAL;
  1198. xfer_funcs[n] = funcs;
  1199. return 0;
  1200. }
  1201. int loop_unregister_transfer(int number)
  1202. {
  1203. unsigned int n = number;
  1204. struct loop_device *lo;
  1205. struct loop_func_table *xfer;
  1206. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1207. return -EINVAL;
  1208. xfer_funcs[n] = NULL;
  1209. list_for_each_entry(lo, &loop_devices, lo_list) {
  1210. mutex_lock(&lo->lo_ctl_mutex);
  1211. if (lo->lo_encryption == xfer)
  1212. loop_release_xfer(lo);
  1213. mutex_unlock(&lo->lo_ctl_mutex);
  1214. }
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL(loop_register_transfer);
  1218. EXPORT_SYMBOL(loop_unregister_transfer);
  1219. static struct loop_device *loop_alloc(int i)
  1220. {
  1221. struct loop_device *lo;
  1222. struct gendisk *disk;
  1223. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1224. if (!lo)
  1225. goto out;
  1226. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1227. if (!lo->lo_queue)
  1228. goto out_free_dev;
  1229. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1230. if (!disk)
  1231. goto out_free_queue;
  1232. mutex_init(&lo->lo_ctl_mutex);
  1233. lo->lo_number = i;
  1234. lo->lo_thread = NULL;
  1235. init_waitqueue_head(&lo->lo_event);
  1236. spin_lock_init(&lo->lo_lock);
  1237. disk->major = LOOP_MAJOR;
  1238. disk->first_minor = i << part_shift;
  1239. disk->fops = &lo_fops;
  1240. disk->private_data = lo;
  1241. disk->queue = lo->lo_queue;
  1242. sprintf(disk->disk_name, "loop%d", i);
  1243. return lo;
  1244. out_free_queue:
  1245. blk_cleanup_queue(lo->lo_queue);
  1246. out_free_dev:
  1247. kfree(lo);
  1248. out:
  1249. return NULL;
  1250. }
  1251. static void loop_free(struct loop_device *lo)
  1252. {
  1253. blk_cleanup_queue(lo->lo_queue);
  1254. put_disk(lo->lo_disk);
  1255. list_del(&lo->lo_list);
  1256. kfree(lo);
  1257. }
  1258. static struct loop_device *loop_init_one(int i)
  1259. {
  1260. struct loop_device *lo;
  1261. list_for_each_entry(lo, &loop_devices, lo_list) {
  1262. if (lo->lo_number == i)
  1263. return lo;
  1264. }
  1265. lo = loop_alloc(i);
  1266. if (lo) {
  1267. add_disk(lo->lo_disk);
  1268. list_add_tail(&lo->lo_list, &loop_devices);
  1269. }
  1270. return lo;
  1271. }
  1272. static void loop_del_one(struct loop_device *lo)
  1273. {
  1274. del_gendisk(lo->lo_disk);
  1275. loop_free(lo);
  1276. }
  1277. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1278. {
  1279. struct loop_device *lo;
  1280. struct kobject *kobj;
  1281. mutex_lock(&loop_devices_mutex);
  1282. lo = loop_init_one(dev & MINORMASK);
  1283. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1284. mutex_unlock(&loop_devices_mutex);
  1285. *part = 0;
  1286. return kobj;
  1287. }
  1288. static int __init loop_init(void)
  1289. {
  1290. int i, nr;
  1291. unsigned long range;
  1292. struct loop_device *lo, *next;
  1293. /*
  1294. * loop module now has a feature to instantiate underlying device
  1295. * structure on-demand, provided that there is an access dev node.
  1296. * However, this will not work well with user space tool that doesn't
  1297. * know about such "feature". In order to not break any existing
  1298. * tool, we do the following:
  1299. *
  1300. * (1) if max_loop is specified, create that many upfront, and this
  1301. * also becomes a hard limit.
  1302. * (2) if max_loop is not specified, create 8 loop device on module
  1303. * load, user can further extend loop device by create dev node
  1304. * themselves and have kernel automatically instantiate actual
  1305. * device on-demand.
  1306. */
  1307. part_shift = 0;
  1308. if (max_part > 0)
  1309. part_shift = fls(max_part);
  1310. if (max_loop > 1UL << (MINORBITS - part_shift))
  1311. return -EINVAL;
  1312. if (max_loop) {
  1313. nr = max_loop;
  1314. range = max_loop;
  1315. } else {
  1316. nr = 8;
  1317. range = 1UL << (MINORBITS - part_shift);
  1318. }
  1319. if (register_blkdev(LOOP_MAJOR, "loop"))
  1320. return -EIO;
  1321. for (i = 0; i < nr; i++) {
  1322. lo = loop_alloc(i);
  1323. if (!lo)
  1324. goto Enomem;
  1325. list_add_tail(&lo->lo_list, &loop_devices);
  1326. }
  1327. /* point of no return */
  1328. list_for_each_entry(lo, &loop_devices, lo_list)
  1329. add_disk(lo->lo_disk);
  1330. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1331. THIS_MODULE, loop_probe, NULL, NULL);
  1332. printk(KERN_INFO "loop: module loaded\n");
  1333. return 0;
  1334. Enomem:
  1335. printk(KERN_INFO "loop: out of memory\n");
  1336. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1337. loop_free(lo);
  1338. unregister_blkdev(LOOP_MAJOR, "loop");
  1339. return -ENOMEM;
  1340. }
  1341. static void __exit loop_exit(void)
  1342. {
  1343. unsigned long range;
  1344. struct loop_device *lo, *next;
  1345. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1346. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1347. loop_del_one(lo);
  1348. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1349. unregister_blkdev(LOOP_MAJOR, "loop");
  1350. }
  1351. module_init(loop_init);
  1352. module_exit(loop_exit);
  1353. #ifndef MODULE
  1354. static int __init max_loop_setup(char *str)
  1355. {
  1356. max_loop = simple_strtol(str, NULL, 0);
  1357. return 1;
  1358. }
  1359. __setup("max_loop=", max_loop_setup);
  1360. #endif