loop.c 43 KB

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