loop.c 43 KB

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