loop.c 43 KB

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