loop.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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/compat.h>
  66. #include <linux/suspend.h>
  67. #include <linux/freezer.h>
  68. #include <linux/mutex.h>
  69. #include <linux/writeback.h>
  70. #include <linux/completion.h>
  71. #include <linux/highmem.h>
  72. #include <linux/kthread.h>
  73. #include <linux/splice.h>
  74. #include <linux/sysfs.h>
  75. #include <linux/miscdevice.h>
  76. #include <linux/falloc.h>
  77. #include <linux/uio.h>
  78. #include "loop.h"
  79. #include <asm/uaccess.h>
  80. static DEFINE_IDR(loop_index_idr);
  81. static DEFINE_MUTEX(loop_index_mutex);
  82. static int max_part;
  83. static int part_shift;
  84. static int transfer_xor(struct loop_device *lo, int cmd,
  85. struct page *raw_page, unsigned raw_off,
  86. struct page *loop_page, unsigned loop_off,
  87. int size, sector_t real_block)
  88. {
  89. char *raw_buf = kmap_atomic(raw_page) + raw_off;
  90. char *loop_buf = kmap_atomic(loop_page) + loop_off;
  91. char *in, *out, *key;
  92. int i, keysize;
  93. if (cmd == READ) {
  94. in = raw_buf;
  95. out = loop_buf;
  96. } else {
  97. in = loop_buf;
  98. out = raw_buf;
  99. }
  100. key = lo->lo_encrypt_key;
  101. keysize = lo->lo_encrypt_key_size;
  102. for (i = 0; i < size; i++)
  103. *out++ = *in++ ^ key[(i & 511) % keysize];
  104. kunmap_atomic(loop_buf);
  105. kunmap_atomic(raw_buf);
  106. cond_resched();
  107. return 0;
  108. }
  109. static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
  110. {
  111. if (unlikely(info->lo_encrypt_key_size <= 0))
  112. return -EINVAL;
  113. return 0;
  114. }
  115. static struct loop_func_table none_funcs = {
  116. .number = LO_CRYPT_NONE,
  117. };
  118. static struct loop_func_table xor_funcs = {
  119. .number = LO_CRYPT_XOR,
  120. .transfer = transfer_xor,
  121. .init = xor_init
  122. };
  123. /* xfer_funcs[0] is special - its release function is never called */
  124. static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  125. &none_funcs,
  126. &xor_funcs
  127. };
  128. static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
  129. {
  130. loff_t loopsize;
  131. /* Compute loopsize in bytes */
  132. loopsize = i_size_read(file->f_mapping->host);
  133. if (offset > 0)
  134. loopsize -= offset;
  135. /* offset is beyond i_size, weird but possible */
  136. if (loopsize < 0)
  137. return 0;
  138. if (sizelimit > 0 && sizelimit < loopsize)
  139. loopsize = sizelimit;
  140. /*
  141. * Unfortunately, if we want to do I/O on the device,
  142. * the number of 512-byte sectors has to fit into a sector_t.
  143. */
  144. return loopsize >> 9;
  145. }
  146. static loff_t get_loop_size(struct loop_device *lo, struct file *file)
  147. {
  148. return get_size(lo->lo_offset, lo->lo_sizelimit, file);
  149. }
  150. static void __loop_update_dio(struct loop_device *lo, bool dio)
  151. {
  152. struct file *file = lo->lo_backing_file;
  153. struct address_space *mapping = file->f_mapping;
  154. struct inode *inode = mapping->host;
  155. unsigned short sb_bsize = 0;
  156. unsigned dio_align = 0;
  157. bool use_dio;
  158. if (inode->i_sb->s_bdev) {
  159. sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
  160. dio_align = sb_bsize - 1;
  161. }
  162. /*
  163. * We support direct I/O only if lo_offset is aligned with the
  164. * logical I/O size of backing device, and the logical block
  165. * size of loop is bigger than the backing device's and the loop
  166. * needn't transform transfer.
  167. *
  168. * TODO: the above condition may be loosed in the future, and
  169. * direct I/O may be switched runtime at that time because most
  170. * of requests in sane appplications should be PAGE_SIZE algined
  171. */
  172. if (dio) {
  173. if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
  174. !(lo->lo_offset & dio_align) &&
  175. mapping->a_ops->direct_IO &&
  176. !lo->transfer)
  177. use_dio = true;
  178. else
  179. use_dio = false;
  180. } else {
  181. use_dio = false;
  182. }
  183. if (lo->use_dio == use_dio)
  184. return;
  185. /* flush dirty pages before changing direct IO */
  186. vfs_fsync(file, 0);
  187. /*
  188. * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
  189. * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
  190. * will get updated by ioctl(LOOP_GET_STATUS)
  191. */
  192. blk_mq_freeze_queue(lo->lo_queue);
  193. lo->use_dio = use_dio;
  194. if (use_dio)
  195. lo->lo_flags |= LO_FLAGS_DIRECT_IO;
  196. else
  197. lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
  198. blk_mq_unfreeze_queue(lo->lo_queue);
  199. }
  200. static int
  201. figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
  202. {
  203. loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
  204. sector_t x = (sector_t)size;
  205. struct block_device *bdev = lo->lo_device;
  206. if (unlikely((loff_t)x != size))
  207. return -EFBIG;
  208. if (lo->lo_offset != offset)
  209. lo->lo_offset = offset;
  210. if (lo->lo_sizelimit != sizelimit)
  211. lo->lo_sizelimit = sizelimit;
  212. set_capacity(lo->lo_disk, x);
  213. bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
  214. /* let user-space know about the new size */
  215. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  216. return 0;
  217. }
  218. static inline int
  219. lo_do_transfer(struct loop_device *lo, int cmd,
  220. struct page *rpage, unsigned roffs,
  221. struct page *lpage, unsigned loffs,
  222. int size, sector_t rblock)
  223. {
  224. int ret;
  225. ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  226. if (likely(!ret))
  227. return 0;
  228. printk_ratelimited(KERN_ERR
  229. "loop: Transfer error at byte offset %llu, length %i.\n",
  230. (unsigned long long)rblock << 9, size);
  231. return ret;
  232. }
  233. static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
  234. {
  235. struct iov_iter i;
  236. ssize_t bw;
  237. iov_iter_bvec(&i, ITER_BVEC, bvec, 1, bvec->bv_len);
  238. file_start_write(file);
  239. bw = vfs_iter_write(file, &i, ppos);
  240. file_end_write(file);
  241. if (likely(bw == bvec->bv_len))
  242. return 0;
  243. printk_ratelimited(KERN_ERR
  244. "loop: Write error at byte offset %llu, length %i.\n",
  245. (unsigned long long)*ppos, bvec->bv_len);
  246. if (bw >= 0)
  247. bw = -EIO;
  248. return bw;
  249. }
  250. static int lo_write_simple(struct loop_device *lo, struct request *rq,
  251. loff_t pos)
  252. {
  253. struct bio_vec bvec;
  254. struct req_iterator iter;
  255. int ret = 0;
  256. rq_for_each_segment(bvec, rq, iter) {
  257. ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
  258. if (ret < 0)
  259. break;
  260. cond_resched();
  261. }
  262. return ret;
  263. }
  264. /*
  265. * This is the slow, transforming version that needs to double buffer the
  266. * data as it cannot do the transformations in place without having direct
  267. * access to the destination pages of the backing file.
  268. */
  269. static int lo_write_transfer(struct loop_device *lo, struct request *rq,
  270. loff_t pos)
  271. {
  272. struct bio_vec bvec, b;
  273. struct req_iterator iter;
  274. struct page *page;
  275. int ret = 0;
  276. page = alloc_page(GFP_NOIO);
  277. if (unlikely(!page))
  278. return -ENOMEM;
  279. rq_for_each_segment(bvec, rq, iter) {
  280. ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
  281. bvec.bv_offset, bvec.bv_len, pos >> 9);
  282. if (unlikely(ret))
  283. break;
  284. b.bv_page = page;
  285. b.bv_offset = 0;
  286. b.bv_len = bvec.bv_len;
  287. ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
  288. if (ret < 0)
  289. break;
  290. }
  291. __free_page(page);
  292. return ret;
  293. }
  294. static int lo_read_simple(struct loop_device *lo, struct request *rq,
  295. loff_t pos)
  296. {
  297. struct bio_vec bvec;
  298. struct req_iterator iter;
  299. struct iov_iter i;
  300. ssize_t len;
  301. rq_for_each_segment(bvec, rq, iter) {
  302. iov_iter_bvec(&i, ITER_BVEC, &bvec, 1, bvec.bv_len);
  303. len = vfs_iter_read(lo->lo_backing_file, &i, &pos);
  304. if (len < 0)
  305. return len;
  306. flush_dcache_page(bvec.bv_page);
  307. if (len != bvec.bv_len) {
  308. struct bio *bio;
  309. __rq_for_each_bio(bio, rq)
  310. zero_fill_bio(bio);
  311. break;
  312. }
  313. cond_resched();
  314. }
  315. return 0;
  316. }
  317. static int lo_read_transfer(struct loop_device *lo, struct request *rq,
  318. loff_t pos)
  319. {
  320. struct bio_vec bvec, b;
  321. struct req_iterator iter;
  322. struct iov_iter i;
  323. struct page *page;
  324. ssize_t len;
  325. int ret = 0;
  326. page = alloc_page(GFP_NOIO);
  327. if (unlikely(!page))
  328. return -ENOMEM;
  329. rq_for_each_segment(bvec, rq, iter) {
  330. loff_t offset = pos;
  331. b.bv_page = page;
  332. b.bv_offset = 0;
  333. b.bv_len = bvec.bv_len;
  334. iov_iter_bvec(&i, ITER_BVEC, &b, 1, b.bv_len);
  335. len = vfs_iter_read(lo->lo_backing_file, &i, &pos);
  336. if (len < 0) {
  337. ret = len;
  338. goto out_free_page;
  339. }
  340. ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
  341. bvec.bv_offset, len, offset >> 9);
  342. if (ret)
  343. goto out_free_page;
  344. flush_dcache_page(bvec.bv_page);
  345. if (len != bvec.bv_len) {
  346. struct bio *bio;
  347. __rq_for_each_bio(bio, rq)
  348. zero_fill_bio(bio);
  349. break;
  350. }
  351. }
  352. ret = 0;
  353. out_free_page:
  354. __free_page(page);
  355. return ret;
  356. }
  357. static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
  358. {
  359. /*
  360. * We use punch hole to reclaim the free space used by the
  361. * image a.k.a. discard. However we do not support discard if
  362. * encryption is enabled, because it may give an attacker
  363. * useful information.
  364. */
  365. struct file *file = lo->lo_backing_file;
  366. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  367. int ret;
  368. if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
  369. ret = -EOPNOTSUPP;
  370. goto out;
  371. }
  372. ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
  373. if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
  374. ret = -EIO;
  375. out:
  376. return ret;
  377. }
  378. static int lo_req_flush(struct loop_device *lo, struct request *rq)
  379. {
  380. struct file *file = lo->lo_backing_file;
  381. int ret = vfs_fsync(file, 0);
  382. if (unlikely(ret && ret != -EINVAL))
  383. ret = -EIO;
  384. return ret;
  385. }
  386. static inline void handle_partial_read(struct loop_cmd *cmd, long bytes)
  387. {
  388. if (bytes < 0 || (cmd->rq->cmd_flags & REQ_WRITE))
  389. return;
  390. if (unlikely(bytes < blk_rq_bytes(cmd->rq))) {
  391. struct bio *bio = cmd->rq->bio;
  392. bio_advance(bio, bytes);
  393. zero_fill_bio(bio);
  394. }
  395. }
  396. static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
  397. {
  398. struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
  399. struct request *rq = cmd->rq;
  400. handle_partial_read(cmd, ret);
  401. if (ret > 0)
  402. ret = 0;
  403. else if (ret < 0)
  404. ret = -EIO;
  405. blk_mq_complete_request(rq, ret);
  406. }
  407. static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
  408. loff_t pos, bool rw)
  409. {
  410. struct iov_iter iter;
  411. struct bio_vec *bvec;
  412. struct bio *bio = cmd->rq->bio;
  413. struct file *file = lo->lo_backing_file;
  414. int ret;
  415. /* nomerge for loop request queue */
  416. WARN_ON(cmd->rq->bio != cmd->rq->biotail);
  417. bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
  418. iov_iter_bvec(&iter, ITER_BVEC | rw, bvec,
  419. bio_segments(bio), blk_rq_bytes(cmd->rq));
  420. cmd->iocb.ki_pos = pos;
  421. cmd->iocb.ki_filp = file;
  422. cmd->iocb.ki_complete = lo_rw_aio_complete;
  423. cmd->iocb.ki_flags = IOCB_DIRECT;
  424. if (rw == WRITE)
  425. ret = file->f_op->write_iter(&cmd->iocb, &iter);
  426. else
  427. ret = file->f_op->read_iter(&cmd->iocb, &iter);
  428. if (ret != -EIOCBQUEUED)
  429. cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
  430. return 0;
  431. }
  432. static inline int lo_rw_simple(struct loop_device *lo,
  433. struct request *rq, loff_t pos, bool rw)
  434. {
  435. struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  436. if (cmd->use_aio)
  437. return lo_rw_aio(lo, cmd, pos, rw);
  438. /*
  439. * lo_write_simple and lo_read_simple should have been covered
  440. * by io submit style function like lo_rw_aio(), one blocker
  441. * is that lo_read_simple() need to call flush_dcache_page after
  442. * the page is written from kernel, and it isn't easy to handle
  443. * this in io submit style function which submits all segments
  444. * of the req at one time. And direct read IO doesn't need to
  445. * run flush_dcache_page().
  446. */
  447. if (rw == WRITE)
  448. return lo_write_simple(lo, rq, pos);
  449. else
  450. return lo_read_simple(lo, rq, pos);
  451. }
  452. static int do_req_filebacked(struct loop_device *lo, struct request *rq)
  453. {
  454. loff_t pos;
  455. int ret;
  456. pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
  457. if (rq->cmd_flags & REQ_WRITE) {
  458. if (rq->cmd_flags & REQ_FLUSH)
  459. ret = lo_req_flush(lo, rq);
  460. else if (rq->cmd_flags & REQ_DISCARD)
  461. ret = lo_discard(lo, rq, pos);
  462. else if (lo->transfer)
  463. ret = lo_write_transfer(lo, rq, pos);
  464. else
  465. ret = lo_rw_simple(lo, rq, pos, WRITE);
  466. } else {
  467. if (lo->transfer)
  468. ret = lo_read_transfer(lo, rq, pos);
  469. else
  470. ret = lo_rw_simple(lo, rq, pos, READ);
  471. }
  472. return ret;
  473. }
  474. struct switch_request {
  475. struct file *file;
  476. struct completion wait;
  477. };
  478. static inline void loop_update_dio(struct loop_device *lo)
  479. {
  480. __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) |
  481. lo->use_dio);
  482. }
  483. /*
  484. * Do the actual switch; called from the BIO completion routine
  485. */
  486. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  487. {
  488. struct file *file = p->file;
  489. struct file *old_file = lo->lo_backing_file;
  490. struct address_space *mapping;
  491. /* if no new file, only flush of queued bios requested */
  492. if (!file)
  493. return;
  494. mapping = file->f_mapping;
  495. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  496. lo->lo_backing_file = file;
  497. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  498. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  499. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  500. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  501. loop_update_dio(lo);
  502. }
  503. /*
  504. * loop_switch performs the hard work of switching a backing store.
  505. * First it needs to flush existing IO, it does this by sending a magic
  506. * BIO down the pipe. The completion of this BIO does the actual switch.
  507. */
  508. static int loop_switch(struct loop_device *lo, struct file *file)
  509. {
  510. struct switch_request w;
  511. w.file = file;
  512. /* freeze queue and wait for completion of scheduled requests */
  513. blk_mq_freeze_queue(lo->lo_queue);
  514. /* do the switch action */
  515. do_loop_switch(lo, &w);
  516. /* unfreeze */
  517. blk_mq_unfreeze_queue(lo->lo_queue);
  518. return 0;
  519. }
  520. /*
  521. * Helper to flush the IOs in loop, but keeping loop thread running
  522. */
  523. static int loop_flush(struct loop_device *lo)
  524. {
  525. return loop_switch(lo, NULL);
  526. }
  527. static void loop_reread_partitions(struct loop_device *lo,
  528. struct block_device *bdev)
  529. {
  530. int rc;
  531. /*
  532. * bd_mutex has been held already in release path, so don't
  533. * acquire it if this function is called in such case.
  534. *
  535. * If the reread partition isn't from release path, lo_refcnt
  536. * must be at least one and it can only become zero when the
  537. * current holder is released.
  538. */
  539. if (!atomic_read(&lo->lo_refcnt))
  540. rc = __blkdev_reread_part(bdev);
  541. else
  542. rc = blkdev_reread_part(bdev);
  543. if (rc)
  544. pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
  545. __func__, lo->lo_number, lo->lo_file_name, rc);
  546. }
  547. /*
  548. * loop_change_fd switched the backing store of a loopback device to
  549. * a new file. This is useful for operating system installers to free up
  550. * the original file and in High Availability environments to switch to
  551. * an alternative location for the content in case of server meltdown.
  552. * This can only work if the loop device is used read-only, and if the
  553. * new backing store is the same size and type as the old backing store.
  554. */
  555. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  556. unsigned int arg)
  557. {
  558. struct file *file, *old_file;
  559. struct inode *inode;
  560. int error;
  561. error = -ENXIO;
  562. if (lo->lo_state != Lo_bound)
  563. goto out;
  564. /* the loop device has to be read-only */
  565. error = -EINVAL;
  566. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  567. goto out;
  568. error = -EBADF;
  569. file = fget(arg);
  570. if (!file)
  571. goto out;
  572. inode = file->f_mapping->host;
  573. old_file = lo->lo_backing_file;
  574. error = -EINVAL;
  575. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  576. goto out_putf;
  577. /* size of the new backing store needs to be the same */
  578. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  579. goto out_putf;
  580. /* and ... switch */
  581. error = loop_switch(lo, file);
  582. if (error)
  583. goto out_putf;
  584. fput(old_file);
  585. if (lo->lo_flags & LO_FLAGS_PARTSCAN)
  586. loop_reread_partitions(lo, bdev);
  587. return 0;
  588. out_putf:
  589. fput(file);
  590. out:
  591. return error;
  592. }
  593. static inline int is_loop_device(struct file *file)
  594. {
  595. struct inode *i = file->f_mapping->host;
  596. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  597. }
  598. /* loop sysfs attributes */
  599. static ssize_t loop_attr_show(struct device *dev, char *page,
  600. ssize_t (*callback)(struct loop_device *, char *))
  601. {
  602. struct gendisk *disk = dev_to_disk(dev);
  603. struct loop_device *lo = disk->private_data;
  604. return callback(lo, page);
  605. }
  606. #define LOOP_ATTR_RO(_name) \
  607. static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
  608. static ssize_t loop_attr_do_show_##_name(struct device *d, \
  609. struct device_attribute *attr, char *b) \
  610. { \
  611. return loop_attr_show(d, b, loop_attr_##_name##_show); \
  612. } \
  613. static struct device_attribute loop_attr_##_name = \
  614. __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
  615. static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
  616. {
  617. ssize_t ret;
  618. char *p = NULL;
  619. spin_lock_irq(&lo->lo_lock);
  620. if (lo->lo_backing_file)
  621. p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
  622. spin_unlock_irq(&lo->lo_lock);
  623. if (IS_ERR_OR_NULL(p))
  624. ret = PTR_ERR(p);
  625. else {
  626. ret = strlen(p);
  627. memmove(buf, p, ret);
  628. buf[ret++] = '\n';
  629. buf[ret] = 0;
  630. }
  631. return ret;
  632. }
  633. static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
  634. {
  635. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
  636. }
  637. static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
  638. {
  639. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
  640. }
  641. static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
  642. {
  643. int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
  644. return sprintf(buf, "%s\n", autoclear ? "1" : "0");
  645. }
  646. static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
  647. {
  648. int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
  649. return sprintf(buf, "%s\n", partscan ? "1" : "0");
  650. }
  651. static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
  652. {
  653. int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
  654. return sprintf(buf, "%s\n", dio ? "1" : "0");
  655. }
  656. LOOP_ATTR_RO(backing_file);
  657. LOOP_ATTR_RO(offset);
  658. LOOP_ATTR_RO(sizelimit);
  659. LOOP_ATTR_RO(autoclear);
  660. LOOP_ATTR_RO(partscan);
  661. LOOP_ATTR_RO(dio);
  662. static struct attribute *loop_attrs[] = {
  663. &loop_attr_backing_file.attr,
  664. &loop_attr_offset.attr,
  665. &loop_attr_sizelimit.attr,
  666. &loop_attr_autoclear.attr,
  667. &loop_attr_partscan.attr,
  668. &loop_attr_dio.attr,
  669. NULL,
  670. };
  671. static struct attribute_group loop_attribute_group = {
  672. .name = "loop",
  673. .attrs= loop_attrs,
  674. };
  675. static int loop_sysfs_init(struct loop_device *lo)
  676. {
  677. return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
  678. &loop_attribute_group);
  679. }
  680. static void loop_sysfs_exit(struct loop_device *lo)
  681. {
  682. sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
  683. &loop_attribute_group);
  684. }
  685. static void loop_config_discard(struct loop_device *lo)
  686. {
  687. struct file *file = lo->lo_backing_file;
  688. struct inode *inode = file->f_mapping->host;
  689. struct request_queue *q = lo->lo_queue;
  690. /*
  691. * We use punch hole to reclaim the free space used by the
  692. * image a.k.a. discard. However we do not support discard if
  693. * encryption is enabled, because it may give an attacker
  694. * useful information.
  695. */
  696. if ((!file->f_op->fallocate) ||
  697. lo->lo_encrypt_key_size) {
  698. q->limits.discard_granularity = 0;
  699. q->limits.discard_alignment = 0;
  700. blk_queue_max_discard_sectors(q, 0);
  701. q->limits.discard_zeroes_data = 0;
  702. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  703. return;
  704. }
  705. q->limits.discard_granularity = inode->i_sb->s_blocksize;
  706. q->limits.discard_alignment = 0;
  707. blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
  708. q->limits.discard_zeroes_data = 1;
  709. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  710. }
  711. static void loop_unprepare_queue(struct loop_device *lo)
  712. {
  713. flush_kthread_worker(&lo->worker);
  714. kthread_stop(lo->worker_task);
  715. }
  716. static int loop_prepare_queue(struct loop_device *lo)
  717. {
  718. init_kthread_worker(&lo->worker);
  719. lo->worker_task = kthread_run(kthread_worker_fn,
  720. &lo->worker, "loop%d", lo->lo_number);
  721. if (IS_ERR(lo->worker_task))
  722. return -ENOMEM;
  723. set_user_nice(lo->worker_task, MIN_NICE);
  724. return 0;
  725. }
  726. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  727. struct block_device *bdev, unsigned int arg)
  728. {
  729. struct file *file, *f;
  730. struct inode *inode;
  731. struct address_space *mapping;
  732. unsigned lo_blocksize;
  733. int lo_flags = 0;
  734. int error;
  735. loff_t size;
  736. /* This is safe, since we have a reference from open(). */
  737. __module_get(THIS_MODULE);
  738. error = -EBADF;
  739. file = fget(arg);
  740. if (!file)
  741. goto out;
  742. error = -EBUSY;
  743. if (lo->lo_state != Lo_unbound)
  744. goto out_putf;
  745. /* Avoid recursion */
  746. f = file;
  747. while (is_loop_device(f)) {
  748. struct loop_device *l;
  749. if (f->f_mapping->host->i_bdev == bdev)
  750. goto out_putf;
  751. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  752. if (l->lo_state == Lo_unbound) {
  753. error = -EINVAL;
  754. goto out_putf;
  755. }
  756. f = l->lo_backing_file;
  757. }
  758. mapping = file->f_mapping;
  759. inode = mapping->host;
  760. error = -EINVAL;
  761. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  762. goto out_putf;
  763. if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
  764. !file->f_op->write_iter)
  765. lo_flags |= LO_FLAGS_READ_ONLY;
  766. lo_blocksize = S_ISBLK(inode->i_mode) ?
  767. inode->i_bdev->bd_block_size : PAGE_SIZE;
  768. error = -EFBIG;
  769. size = get_loop_size(lo, file);
  770. if ((loff_t)(sector_t)size != size)
  771. goto out_putf;
  772. error = loop_prepare_queue(lo);
  773. if (error)
  774. goto out_putf;
  775. error = 0;
  776. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  777. lo->use_dio = false;
  778. lo->lo_blocksize = lo_blocksize;
  779. lo->lo_device = bdev;
  780. lo->lo_flags = lo_flags;
  781. lo->lo_backing_file = file;
  782. lo->transfer = NULL;
  783. lo->ioctl = NULL;
  784. lo->lo_sizelimit = 0;
  785. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  786. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  787. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  788. blk_queue_flush(lo->lo_queue, REQ_FLUSH);
  789. loop_update_dio(lo);
  790. set_capacity(lo->lo_disk, size);
  791. bd_set_size(bdev, size << 9);
  792. loop_sysfs_init(lo);
  793. /* let user-space know about the new size */
  794. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  795. set_blocksize(bdev, lo_blocksize);
  796. lo->lo_state = Lo_bound;
  797. if (part_shift)
  798. lo->lo_flags |= LO_FLAGS_PARTSCAN;
  799. if (lo->lo_flags & LO_FLAGS_PARTSCAN)
  800. loop_reread_partitions(lo, bdev);
  801. /* Grab the block_device to prevent its destruction after we
  802. * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
  803. */
  804. bdgrab(bdev);
  805. return 0;
  806. out_putf:
  807. fput(file);
  808. out:
  809. /* This is safe: open() is still holding a reference. */
  810. module_put(THIS_MODULE);
  811. return error;
  812. }
  813. static int
  814. loop_release_xfer(struct loop_device *lo)
  815. {
  816. int err = 0;
  817. struct loop_func_table *xfer = lo->lo_encryption;
  818. if (xfer) {
  819. if (xfer->release)
  820. err = xfer->release(lo);
  821. lo->transfer = NULL;
  822. lo->lo_encryption = NULL;
  823. module_put(xfer->owner);
  824. }
  825. return err;
  826. }
  827. static int
  828. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  829. const struct loop_info64 *i)
  830. {
  831. int err = 0;
  832. if (xfer) {
  833. struct module *owner = xfer->owner;
  834. if (!try_module_get(owner))
  835. return -EINVAL;
  836. if (xfer->init)
  837. err = xfer->init(lo, i);
  838. if (err)
  839. module_put(owner);
  840. else
  841. lo->lo_encryption = xfer;
  842. }
  843. return err;
  844. }
  845. static int loop_clr_fd(struct loop_device *lo)
  846. {
  847. struct file *filp = lo->lo_backing_file;
  848. gfp_t gfp = lo->old_gfp_mask;
  849. struct block_device *bdev = lo->lo_device;
  850. if (lo->lo_state != Lo_bound)
  851. return -ENXIO;
  852. /*
  853. * If we've explicitly asked to tear down the loop device,
  854. * and it has an elevated reference count, set it for auto-teardown when
  855. * the last reference goes away. This stops $!~#$@ udev from
  856. * preventing teardown because it decided that it needs to run blkid on
  857. * the loopback device whenever they appear. xfstests is notorious for
  858. * failing tests because blkid via udev races with a losetup
  859. * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
  860. * command to fail with EBUSY.
  861. */
  862. if (atomic_read(&lo->lo_refcnt) > 1) {
  863. lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
  864. mutex_unlock(&lo->lo_ctl_mutex);
  865. return 0;
  866. }
  867. if (filp == NULL)
  868. return -EINVAL;
  869. /* freeze request queue during the transition */
  870. blk_mq_freeze_queue(lo->lo_queue);
  871. spin_lock_irq(&lo->lo_lock);
  872. lo->lo_state = Lo_rundown;
  873. lo->lo_backing_file = NULL;
  874. spin_unlock_irq(&lo->lo_lock);
  875. loop_release_xfer(lo);
  876. lo->transfer = NULL;
  877. lo->ioctl = NULL;
  878. lo->lo_device = NULL;
  879. lo->lo_encryption = NULL;
  880. lo->lo_offset = 0;
  881. lo->lo_sizelimit = 0;
  882. lo->lo_encrypt_key_size = 0;
  883. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  884. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  885. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  886. if (bdev) {
  887. bdput(bdev);
  888. invalidate_bdev(bdev);
  889. }
  890. set_capacity(lo->lo_disk, 0);
  891. loop_sysfs_exit(lo);
  892. if (bdev) {
  893. bd_set_size(bdev, 0);
  894. /* let user-space know about this change */
  895. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  896. }
  897. mapping_set_gfp_mask(filp->f_mapping, gfp);
  898. lo->lo_state = Lo_unbound;
  899. /* This is safe: open() is still holding a reference. */
  900. module_put(THIS_MODULE);
  901. blk_mq_unfreeze_queue(lo->lo_queue);
  902. if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
  903. loop_reread_partitions(lo, bdev);
  904. lo->lo_flags = 0;
  905. if (!part_shift)
  906. lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
  907. loop_unprepare_queue(lo);
  908. mutex_unlock(&lo->lo_ctl_mutex);
  909. /*
  910. * Need not hold lo_ctl_mutex to fput backing file.
  911. * Calling fput holding lo_ctl_mutex triggers a circular
  912. * lock dependency possibility warning as fput can take
  913. * bd_mutex which is usually taken before lo_ctl_mutex.
  914. */
  915. fput(filp);
  916. return 0;
  917. }
  918. static int
  919. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  920. {
  921. int err;
  922. struct loop_func_table *xfer;
  923. kuid_t uid = current_uid();
  924. if (lo->lo_encrypt_key_size &&
  925. !uid_eq(lo->lo_key_owner, uid) &&
  926. !capable(CAP_SYS_ADMIN))
  927. return -EPERM;
  928. if (lo->lo_state != Lo_bound)
  929. return -ENXIO;
  930. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  931. return -EINVAL;
  932. err = loop_release_xfer(lo);
  933. if (err)
  934. return err;
  935. if (info->lo_encrypt_type) {
  936. unsigned int type = info->lo_encrypt_type;
  937. if (type >= MAX_LO_CRYPT)
  938. return -EINVAL;
  939. xfer = xfer_funcs[type];
  940. if (xfer == NULL)
  941. return -EINVAL;
  942. } else
  943. xfer = NULL;
  944. err = loop_init_xfer(lo, xfer, info);
  945. if (err)
  946. return err;
  947. if (lo->lo_offset != info->lo_offset ||
  948. lo->lo_sizelimit != info->lo_sizelimit)
  949. if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
  950. return -EFBIG;
  951. loop_config_discard(lo);
  952. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  953. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  954. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  955. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  956. if (!xfer)
  957. xfer = &none_funcs;
  958. lo->transfer = xfer->transfer;
  959. lo->ioctl = xfer->ioctl;
  960. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  961. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  962. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  963. if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
  964. !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
  965. lo->lo_flags |= LO_FLAGS_PARTSCAN;
  966. lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
  967. loop_reread_partitions(lo, lo->lo_device);
  968. }
  969. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  970. lo->lo_init[0] = info->lo_init[0];
  971. lo->lo_init[1] = info->lo_init[1];
  972. if (info->lo_encrypt_key_size) {
  973. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  974. info->lo_encrypt_key_size);
  975. lo->lo_key_owner = uid;
  976. }
  977. /* update dio if lo_offset or transfer is changed */
  978. __loop_update_dio(lo, lo->use_dio);
  979. return 0;
  980. }
  981. static int
  982. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  983. {
  984. struct file *file = lo->lo_backing_file;
  985. struct kstat stat;
  986. int error;
  987. if (lo->lo_state != Lo_bound)
  988. return -ENXIO;
  989. error = vfs_getattr(&file->f_path, &stat);
  990. if (error)
  991. return error;
  992. memset(info, 0, sizeof(*info));
  993. info->lo_number = lo->lo_number;
  994. info->lo_device = huge_encode_dev(stat.dev);
  995. info->lo_inode = stat.ino;
  996. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  997. info->lo_offset = lo->lo_offset;
  998. info->lo_sizelimit = lo->lo_sizelimit;
  999. info->lo_flags = lo->lo_flags;
  1000. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  1001. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  1002. info->lo_encrypt_type =
  1003. lo->lo_encryption ? lo->lo_encryption->number : 0;
  1004. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  1005. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1006. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  1007. lo->lo_encrypt_key_size);
  1008. }
  1009. return 0;
  1010. }
  1011. static void
  1012. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  1013. {
  1014. memset(info64, 0, sizeof(*info64));
  1015. info64->lo_number = info->lo_number;
  1016. info64->lo_device = info->lo_device;
  1017. info64->lo_inode = info->lo_inode;
  1018. info64->lo_rdevice = info->lo_rdevice;
  1019. info64->lo_offset = info->lo_offset;
  1020. info64->lo_sizelimit = 0;
  1021. info64->lo_encrypt_type = info->lo_encrypt_type;
  1022. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1023. info64->lo_flags = info->lo_flags;
  1024. info64->lo_init[0] = info->lo_init[0];
  1025. info64->lo_init[1] = info->lo_init[1];
  1026. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1027. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1028. else
  1029. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1030. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1031. }
  1032. static int
  1033. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  1034. {
  1035. memset(info, 0, sizeof(*info));
  1036. info->lo_number = info64->lo_number;
  1037. info->lo_device = info64->lo_device;
  1038. info->lo_inode = info64->lo_inode;
  1039. info->lo_rdevice = info64->lo_rdevice;
  1040. info->lo_offset = info64->lo_offset;
  1041. info->lo_encrypt_type = info64->lo_encrypt_type;
  1042. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1043. info->lo_flags = info64->lo_flags;
  1044. info->lo_init[0] = info64->lo_init[0];
  1045. info->lo_init[1] = info64->lo_init[1];
  1046. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1047. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1048. else
  1049. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1050. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1051. /* error in case values were truncated */
  1052. if (info->lo_device != info64->lo_device ||
  1053. info->lo_rdevice != info64->lo_rdevice ||
  1054. info->lo_inode != info64->lo_inode ||
  1055. info->lo_offset != info64->lo_offset)
  1056. return -EOVERFLOW;
  1057. return 0;
  1058. }
  1059. static int
  1060. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  1061. {
  1062. struct loop_info info;
  1063. struct loop_info64 info64;
  1064. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1065. return -EFAULT;
  1066. loop_info64_from_old(&info, &info64);
  1067. return loop_set_status(lo, &info64);
  1068. }
  1069. static int
  1070. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  1071. {
  1072. struct loop_info64 info64;
  1073. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1074. return -EFAULT;
  1075. return loop_set_status(lo, &info64);
  1076. }
  1077. static int
  1078. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  1079. struct loop_info info;
  1080. struct loop_info64 info64;
  1081. int err = 0;
  1082. if (!arg)
  1083. err = -EINVAL;
  1084. if (!err)
  1085. err = loop_get_status(lo, &info64);
  1086. if (!err)
  1087. err = loop_info64_to_old(&info64, &info);
  1088. if (!err && copy_to_user(arg, &info, sizeof(info)))
  1089. err = -EFAULT;
  1090. return err;
  1091. }
  1092. static int
  1093. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1094. struct loop_info64 info64;
  1095. int err = 0;
  1096. if (!arg)
  1097. err = -EINVAL;
  1098. if (!err)
  1099. err = loop_get_status(lo, &info64);
  1100. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1101. err = -EFAULT;
  1102. return err;
  1103. }
  1104. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1105. {
  1106. if (unlikely(lo->lo_state != Lo_bound))
  1107. return -ENXIO;
  1108. return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
  1109. }
  1110. static int loop_set_dio(struct loop_device *lo, unsigned long arg)
  1111. {
  1112. int error = -ENXIO;
  1113. if (lo->lo_state != Lo_bound)
  1114. goto out;
  1115. __loop_update_dio(lo, !!arg);
  1116. if (lo->use_dio == !!arg)
  1117. return 0;
  1118. error = -EINVAL;
  1119. out:
  1120. return error;
  1121. }
  1122. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1123. unsigned int cmd, unsigned long arg)
  1124. {
  1125. struct loop_device *lo = bdev->bd_disk->private_data;
  1126. int err;
  1127. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1128. switch (cmd) {
  1129. case LOOP_SET_FD:
  1130. err = loop_set_fd(lo, mode, bdev, arg);
  1131. break;
  1132. case LOOP_CHANGE_FD:
  1133. err = loop_change_fd(lo, bdev, arg);
  1134. break;
  1135. case LOOP_CLR_FD:
  1136. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1137. err = loop_clr_fd(lo);
  1138. if (!err)
  1139. goto out_unlocked;
  1140. break;
  1141. case LOOP_SET_STATUS:
  1142. err = -EPERM;
  1143. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1144. err = loop_set_status_old(lo,
  1145. (struct loop_info __user *)arg);
  1146. break;
  1147. case LOOP_GET_STATUS:
  1148. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1149. break;
  1150. case LOOP_SET_STATUS64:
  1151. err = -EPERM;
  1152. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1153. err = loop_set_status64(lo,
  1154. (struct loop_info64 __user *) arg);
  1155. break;
  1156. case LOOP_GET_STATUS64:
  1157. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1158. break;
  1159. case LOOP_SET_CAPACITY:
  1160. err = -EPERM;
  1161. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1162. err = loop_set_capacity(lo, bdev);
  1163. break;
  1164. case LOOP_SET_DIRECT_IO:
  1165. err = -EPERM;
  1166. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1167. err = loop_set_dio(lo, arg);
  1168. break;
  1169. default:
  1170. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1171. }
  1172. mutex_unlock(&lo->lo_ctl_mutex);
  1173. out_unlocked:
  1174. return err;
  1175. }
  1176. #ifdef CONFIG_COMPAT
  1177. struct compat_loop_info {
  1178. compat_int_t lo_number; /* ioctl r/o */
  1179. compat_dev_t lo_device; /* ioctl r/o */
  1180. compat_ulong_t lo_inode; /* ioctl r/o */
  1181. compat_dev_t lo_rdevice; /* ioctl r/o */
  1182. compat_int_t lo_offset;
  1183. compat_int_t lo_encrypt_type;
  1184. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1185. compat_int_t lo_flags; /* ioctl r/o */
  1186. char lo_name[LO_NAME_SIZE];
  1187. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1188. compat_ulong_t lo_init[2];
  1189. char reserved[4];
  1190. };
  1191. /*
  1192. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1193. * - noinlined to reduce stack space usage in main part of driver
  1194. */
  1195. static noinline int
  1196. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1197. struct loop_info64 *info64)
  1198. {
  1199. struct compat_loop_info info;
  1200. if (copy_from_user(&info, arg, sizeof(info)))
  1201. return -EFAULT;
  1202. memset(info64, 0, sizeof(*info64));
  1203. info64->lo_number = info.lo_number;
  1204. info64->lo_device = info.lo_device;
  1205. info64->lo_inode = info.lo_inode;
  1206. info64->lo_rdevice = info.lo_rdevice;
  1207. info64->lo_offset = info.lo_offset;
  1208. info64->lo_sizelimit = 0;
  1209. info64->lo_encrypt_type = info.lo_encrypt_type;
  1210. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1211. info64->lo_flags = info.lo_flags;
  1212. info64->lo_init[0] = info.lo_init[0];
  1213. info64->lo_init[1] = info.lo_init[1];
  1214. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1215. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1216. else
  1217. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1218. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1219. return 0;
  1220. }
  1221. /*
  1222. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1223. * - noinlined to reduce stack space usage in main part of driver
  1224. */
  1225. static noinline int
  1226. loop_info64_to_compat(const struct loop_info64 *info64,
  1227. struct compat_loop_info __user *arg)
  1228. {
  1229. struct compat_loop_info info;
  1230. memset(&info, 0, sizeof(info));
  1231. info.lo_number = info64->lo_number;
  1232. info.lo_device = info64->lo_device;
  1233. info.lo_inode = info64->lo_inode;
  1234. info.lo_rdevice = info64->lo_rdevice;
  1235. info.lo_offset = info64->lo_offset;
  1236. info.lo_encrypt_type = info64->lo_encrypt_type;
  1237. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1238. info.lo_flags = info64->lo_flags;
  1239. info.lo_init[0] = info64->lo_init[0];
  1240. info.lo_init[1] = info64->lo_init[1];
  1241. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1242. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1243. else
  1244. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1245. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1246. /* error in case values were truncated */
  1247. if (info.lo_device != info64->lo_device ||
  1248. info.lo_rdevice != info64->lo_rdevice ||
  1249. info.lo_inode != info64->lo_inode ||
  1250. info.lo_offset != info64->lo_offset ||
  1251. info.lo_init[0] != info64->lo_init[0] ||
  1252. info.lo_init[1] != info64->lo_init[1])
  1253. return -EOVERFLOW;
  1254. if (copy_to_user(arg, &info, sizeof(info)))
  1255. return -EFAULT;
  1256. return 0;
  1257. }
  1258. static int
  1259. loop_set_status_compat(struct loop_device *lo,
  1260. const struct compat_loop_info __user *arg)
  1261. {
  1262. struct loop_info64 info64;
  1263. int ret;
  1264. ret = loop_info64_from_compat(arg, &info64);
  1265. if (ret < 0)
  1266. return ret;
  1267. return loop_set_status(lo, &info64);
  1268. }
  1269. static int
  1270. loop_get_status_compat(struct loop_device *lo,
  1271. struct compat_loop_info __user *arg)
  1272. {
  1273. struct loop_info64 info64;
  1274. int err = 0;
  1275. if (!arg)
  1276. err = -EINVAL;
  1277. if (!err)
  1278. err = loop_get_status(lo, &info64);
  1279. if (!err)
  1280. err = loop_info64_to_compat(&info64, arg);
  1281. return err;
  1282. }
  1283. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1284. unsigned int cmd, unsigned long arg)
  1285. {
  1286. struct loop_device *lo = bdev->bd_disk->private_data;
  1287. int err;
  1288. switch(cmd) {
  1289. case LOOP_SET_STATUS:
  1290. mutex_lock(&lo->lo_ctl_mutex);
  1291. err = loop_set_status_compat(
  1292. lo, (const struct compat_loop_info __user *) arg);
  1293. mutex_unlock(&lo->lo_ctl_mutex);
  1294. break;
  1295. case LOOP_GET_STATUS:
  1296. mutex_lock(&lo->lo_ctl_mutex);
  1297. err = loop_get_status_compat(
  1298. lo, (struct compat_loop_info __user *) arg);
  1299. mutex_unlock(&lo->lo_ctl_mutex);
  1300. break;
  1301. case LOOP_SET_CAPACITY:
  1302. case LOOP_CLR_FD:
  1303. case LOOP_GET_STATUS64:
  1304. case LOOP_SET_STATUS64:
  1305. arg = (unsigned long) compat_ptr(arg);
  1306. case LOOP_SET_FD:
  1307. case LOOP_CHANGE_FD:
  1308. err = lo_ioctl(bdev, mode, cmd, arg);
  1309. break;
  1310. default:
  1311. err = -ENOIOCTLCMD;
  1312. break;
  1313. }
  1314. return err;
  1315. }
  1316. #endif
  1317. static int lo_open(struct block_device *bdev, fmode_t mode)
  1318. {
  1319. struct loop_device *lo;
  1320. int err = 0;
  1321. mutex_lock(&loop_index_mutex);
  1322. lo = bdev->bd_disk->private_data;
  1323. if (!lo) {
  1324. err = -ENXIO;
  1325. goto out;
  1326. }
  1327. atomic_inc(&lo->lo_refcnt);
  1328. out:
  1329. mutex_unlock(&loop_index_mutex);
  1330. return err;
  1331. }
  1332. static void lo_release(struct gendisk *disk, fmode_t mode)
  1333. {
  1334. struct loop_device *lo = disk->private_data;
  1335. int err;
  1336. if (atomic_dec_return(&lo->lo_refcnt))
  1337. return;
  1338. mutex_lock(&lo->lo_ctl_mutex);
  1339. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1340. /*
  1341. * In autoclear mode, stop the loop thread
  1342. * and remove configuration after last close.
  1343. */
  1344. err = loop_clr_fd(lo);
  1345. if (!err)
  1346. return;
  1347. } else {
  1348. /*
  1349. * Otherwise keep thread (if running) and config,
  1350. * but flush possible ongoing bios in thread.
  1351. */
  1352. loop_flush(lo);
  1353. }
  1354. mutex_unlock(&lo->lo_ctl_mutex);
  1355. }
  1356. static const struct block_device_operations lo_fops = {
  1357. .owner = THIS_MODULE,
  1358. .open = lo_open,
  1359. .release = lo_release,
  1360. .ioctl = lo_ioctl,
  1361. #ifdef CONFIG_COMPAT
  1362. .compat_ioctl = lo_compat_ioctl,
  1363. #endif
  1364. };
  1365. /*
  1366. * And now the modules code and kernel interface.
  1367. */
  1368. static int max_loop;
  1369. module_param(max_loop, int, S_IRUGO);
  1370. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1371. module_param(max_part, int, S_IRUGO);
  1372. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1373. MODULE_LICENSE("GPL");
  1374. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1375. int loop_register_transfer(struct loop_func_table *funcs)
  1376. {
  1377. unsigned int n = funcs->number;
  1378. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1379. return -EINVAL;
  1380. xfer_funcs[n] = funcs;
  1381. return 0;
  1382. }
  1383. static int unregister_transfer_cb(int id, void *ptr, void *data)
  1384. {
  1385. struct loop_device *lo = ptr;
  1386. struct loop_func_table *xfer = data;
  1387. mutex_lock(&lo->lo_ctl_mutex);
  1388. if (lo->lo_encryption == xfer)
  1389. loop_release_xfer(lo);
  1390. mutex_unlock(&lo->lo_ctl_mutex);
  1391. return 0;
  1392. }
  1393. int loop_unregister_transfer(int number)
  1394. {
  1395. unsigned int n = number;
  1396. struct loop_func_table *xfer;
  1397. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1398. return -EINVAL;
  1399. xfer_funcs[n] = NULL;
  1400. idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
  1401. return 0;
  1402. }
  1403. EXPORT_SYMBOL(loop_register_transfer);
  1404. EXPORT_SYMBOL(loop_unregister_transfer);
  1405. static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
  1406. const struct blk_mq_queue_data *bd)
  1407. {
  1408. struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  1409. struct loop_device *lo = cmd->rq->q->queuedata;
  1410. blk_mq_start_request(bd->rq);
  1411. if (lo->lo_state != Lo_bound)
  1412. return -EIO;
  1413. if (lo->use_dio && !(cmd->rq->cmd_flags & (REQ_FLUSH |
  1414. REQ_DISCARD)))
  1415. cmd->use_aio = true;
  1416. else
  1417. cmd->use_aio = false;
  1418. queue_kthread_work(&lo->worker, &cmd->work);
  1419. return BLK_MQ_RQ_QUEUE_OK;
  1420. }
  1421. static void loop_handle_cmd(struct loop_cmd *cmd)
  1422. {
  1423. const bool write = cmd->rq->cmd_flags & REQ_WRITE;
  1424. struct loop_device *lo = cmd->rq->q->queuedata;
  1425. int ret = 0;
  1426. if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
  1427. ret = -EIO;
  1428. goto failed;
  1429. }
  1430. ret = do_req_filebacked(lo, cmd->rq);
  1431. failed:
  1432. /* complete non-aio request */
  1433. if (!cmd->use_aio || ret)
  1434. blk_mq_complete_request(cmd->rq, ret ? -EIO : 0);
  1435. }
  1436. static void loop_queue_work(struct kthread_work *work)
  1437. {
  1438. struct loop_cmd *cmd =
  1439. container_of(work, struct loop_cmd, work);
  1440. loop_handle_cmd(cmd);
  1441. }
  1442. static int loop_init_request(void *data, struct request *rq,
  1443. unsigned int hctx_idx, unsigned int request_idx,
  1444. unsigned int numa_node)
  1445. {
  1446. struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  1447. cmd->rq = rq;
  1448. init_kthread_work(&cmd->work, loop_queue_work);
  1449. return 0;
  1450. }
  1451. static struct blk_mq_ops loop_mq_ops = {
  1452. .queue_rq = loop_queue_rq,
  1453. .map_queue = blk_mq_map_queue,
  1454. .init_request = loop_init_request,
  1455. };
  1456. static int loop_add(struct loop_device **l, int i)
  1457. {
  1458. struct loop_device *lo;
  1459. struct gendisk *disk;
  1460. int err;
  1461. err = -ENOMEM;
  1462. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1463. if (!lo)
  1464. goto out;
  1465. lo->lo_state = Lo_unbound;
  1466. /* allocate id, if @id >= 0, we're requesting that specific id */
  1467. if (i >= 0) {
  1468. err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
  1469. if (err == -ENOSPC)
  1470. err = -EEXIST;
  1471. } else {
  1472. err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
  1473. }
  1474. if (err < 0)
  1475. goto out_free_dev;
  1476. i = err;
  1477. err = -ENOMEM;
  1478. lo->tag_set.ops = &loop_mq_ops;
  1479. lo->tag_set.nr_hw_queues = 1;
  1480. lo->tag_set.queue_depth = 128;
  1481. lo->tag_set.numa_node = NUMA_NO_NODE;
  1482. lo->tag_set.cmd_size = sizeof(struct loop_cmd);
  1483. lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1484. lo->tag_set.driver_data = lo;
  1485. err = blk_mq_alloc_tag_set(&lo->tag_set);
  1486. if (err)
  1487. goto out_free_idr;
  1488. lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
  1489. if (IS_ERR_OR_NULL(lo->lo_queue)) {
  1490. err = PTR_ERR(lo->lo_queue);
  1491. goto out_cleanup_tags;
  1492. }
  1493. lo->lo_queue->queuedata = lo;
  1494. /*
  1495. * It doesn't make sense to enable merge because the I/O
  1496. * submitted to backing file is handled page by page.
  1497. */
  1498. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
  1499. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1500. if (!disk)
  1501. goto out_free_queue;
  1502. /*
  1503. * Disable partition scanning by default. The in-kernel partition
  1504. * scanning can be requested individually per-device during its
  1505. * setup. Userspace can always add and remove partitions from all
  1506. * devices. The needed partition minors are allocated from the
  1507. * extended minor space, the main loop device numbers will continue
  1508. * to match the loop minors, regardless of the number of partitions
  1509. * used.
  1510. *
  1511. * If max_part is given, partition scanning is globally enabled for
  1512. * all loop devices. The minors for the main loop devices will be
  1513. * multiples of max_part.
  1514. *
  1515. * Note: Global-for-all-devices, set-only-at-init, read-only module
  1516. * parameteters like 'max_loop' and 'max_part' make things needlessly
  1517. * complicated, are too static, inflexible and may surprise
  1518. * userspace tools. Parameters like this in general should be avoided.
  1519. */
  1520. if (!part_shift)
  1521. disk->flags |= GENHD_FL_NO_PART_SCAN;
  1522. disk->flags |= GENHD_FL_EXT_DEVT;
  1523. mutex_init(&lo->lo_ctl_mutex);
  1524. atomic_set(&lo->lo_refcnt, 0);
  1525. lo->lo_number = i;
  1526. spin_lock_init(&lo->lo_lock);
  1527. disk->major = LOOP_MAJOR;
  1528. disk->first_minor = i << part_shift;
  1529. disk->fops = &lo_fops;
  1530. disk->private_data = lo;
  1531. disk->queue = lo->lo_queue;
  1532. sprintf(disk->disk_name, "loop%d", i);
  1533. add_disk(disk);
  1534. *l = lo;
  1535. return lo->lo_number;
  1536. out_free_queue:
  1537. blk_cleanup_queue(lo->lo_queue);
  1538. out_cleanup_tags:
  1539. blk_mq_free_tag_set(&lo->tag_set);
  1540. out_free_idr:
  1541. idr_remove(&loop_index_idr, i);
  1542. out_free_dev:
  1543. kfree(lo);
  1544. out:
  1545. return err;
  1546. }
  1547. static void loop_remove(struct loop_device *lo)
  1548. {
  1549. blk_cleanup_queue(lo->lo_queue);
  1550. del_gendisk(lo->lo_disk);
  1551. blk_mq_free_tag_set(&lo->tag_set);
  1552. put_disk(lo->lo_disk);
  1553. kfree(lo);
  1554. }
  1555. static int find_free_cb(int id, void *ptr, void *data)
  1556. {
  1557. struct loop_device *lo = ptr;
  1558. struct loop_device **l = data;
  1559. if (lo->lo_state == Lo_unbound) {
  1560. *l = lo;
  1561. return 1;
  1562. }
  1563. return 0;
  1564. }
  1565. static int loop_lookup(struct loop_device **l, int i)
  1566. {
  1567. struct loop_device *lo;
  1568. int ret = -ENODEV;
  1569. if (i < 0) {
  1570. int err;
  1571. err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
  1572. if (err == 1) {
  1573. *l = lo;
  1574. ret = lo->lo_number;
  1575. }
  1576. goto out;
  1577. }
  1578. /* lookup and return a specific i */
  1579. lo = idr_find(&loop_index_idr, i);
  1580. if (lo) {
  1581. *l = lo;
  1582. ret = lo->lo_number;
  1583. }
  1584. out:
  1585. return ret;
  1586. }
  1587. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1588. {
  1589. struct loop_device *lo;
  1590. struct kobject *kobj;
  1591. int err;
  1592. mutex_lock(&loop_index_mutex);
  1593. err = loop_lookup(&lo, MINOR(dev) >> part_shift);
  1594. if (err < 0)
  1595. err = loop_add(&lo, MINOR(dev) >> part_shift);
  1596. if (err < 0)
  1597. kobj = NULL;
  1598. else
  1599. kobj = get_disk(lo->lo_disk);
  1600. mutex_unlock(&loop_index_mutex);
  1601. *part = 0;
  1602. return kobj;
  1603. }
  1604. static long loop_control_ioctl(struct file *file, unsigned int cmd,
  1605. unsigned long parm)
  1606. {
  1607. struct loop_device *lo;
  1608. int ret = -ENOSYS;
  1609. mutex_lock(&loop_index_mutex);
  1610. switch (cmd) {
  1611. case LOOP_CTL_ADD:
  1612. ret = loop_lookup(&lo, parm);
  1613. if (ret >= 0) {
  1614. ret = -EEXIST;
  1615. break;
  1616. }
  1617. ret = loop_add(&lo, parm);
  1618. break;
  1619. case LOOP_CTL_REMOVE:
  1620. ret = loop_lookup(&lo, parm);
  1621. if (ret < 0)
  1622. break;
  1623. mutex_lock(&lo->lo_ctl_mutex);
  1624. if (lo->lo_state != Lo_unbound) {
  1625. ret = -EBUSY;
  1626. mutex_unlock(&lo->lo_ctl_mutex);
  1627. break;
  1628. }
  1629. if (atomic_read(&lo->lo_refcnt) > 0) {
  1630. ret = -EBUSY;
  1631. mutex_unlock(&lo->lo_ctl_mutex);
  1632. break;
  1633. }
  1634. lo->lo_disk->private_data = NULL;
  1635. mutex_unlock(&lo->lo_ctl_mutex);
  1636. idr_remove(&loop_index_idr, lo->lo_number);
  1637. loop_remove(lo);
  1638. break;
  1639. case LOOP_CTL_GET_FREE:
  1640. ret = loop_lookup(&lo, -1);
  1641. if (ret >= 0)
  1642. break;
  1643. ret = loop_add(&lo, -1);
  1644. }
  1645. mutex_unlock(&loop_index_mutex);
  1646. return ret;
  1647. }
  1648. static const struct file_operations loop_ctl_fops = {
  1649. .open = nonseekable_open,
  1650. .unlocked_ioctl = loop_control_ioctl,
  1651. .compat_ioctl = loop_control_ioctl,
  1652. .owner = THIS_MODULE,
  1653. .llseek = noop_llseek,
  1654. };
  1655. static struct miscdevice loop_misc = {
  1656. .minor = LOOP_CTRL_MINOR,
  1657. .name = "loop-control",
  1658. .fops = &loop_ctl_fops,
  1659. };
  1660. MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
  1661. MODULE_ALIAS("devname:loop-control");
  1662. static int __init loop_init(void)
  1663. {
  1664. int i, nr;
  1665. unsigned long range;
  1666. struct loop_device *lo;
  1667. int err;
  1668. err = misc_register(&loop_misc);
  1669. if (err < 0)
  1670. return err;
  1671. part_shift = 0;
  1672. if (max_part > 0) {
  1673. part_shift = fls(max_part);
  1674. /*
  1675. * Adjust max_part according to part_shift as it is exported
  1676. * to user space so that user can decide correct minor number
  1677. * if [s]he want to create more devices.
  1678. *
  1679. * Note that -1 is required because partition 0 is reserved
  1680. * for the whole disk.
  1681. */
  1682. max_part = (1UL << part_shift) - 1;
  1683. }
  1684. if ((1UL << part_shift) > DISK_MAX_PARTS) {
  1685. err = -EINVAL;
  1686. goto misc_out;
  1687. }
  1688. if (max_loop > 1UL << (MINORBITS - part_shift)) {
  1689. err = -EINVAL;
  1690. goto misc_out;
  1691. }
  1692. /*
  1693. * If max_loop is specified, create that many devices upfront.
  1694. * This also becomes a hard limit. If max_loop is not specified,
  1695. * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
  1696. * init time. Loop devices can be requested on-demand with the
  1697. * /dev/loop-control interface, or be instantiated by accessing
  1698. * a 'dead' device node.
  1699. */
  1700. if (max_loop) {
  1701. nr = max_loop;
  1702. range = max_loop << part_shift;
  1703. } else {
  1704. nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
  1705. range = 1UL << MINORBITS;
  1706. }
  1707. if (register_blkdev(LOOP_MAJOR, "loop")) {
  1708. err = -EIO;
  1709. goto misc_out;
  1710. }
  1711. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1712. THIS_MODULE, loop_probe, NULL, NULL);
  1713. /* pre-create number of devices given by config or max_loop */
  1714. mutex_lock(&loop_index_mutex);
  1715. for (i = 0; i < nr; i++)
  1716. loop_add(&lo, i);
  1717. mutex_unlock(&loop_index_mutex);
  1718. printk(KERN_INFO "loop: module loaded\n");
  1719. return 0;
  1720. misc_out:
  1721. misc_deregister(&loop_misc);
  1722. return err;
  1723. }
  1724. static int loop_exit_cb(int id, void *ptr, void *data)
  1725. {
  1726. struct loop_device *lo = ptr;
  1727. loop_remove(lo);
  1728. return 0;
  1729. }
  1730. static void __exit loop_exit(void)
  1731. {
  1732. unsigned long range;
  1733. range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
  1734. idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
  1735. idr_destroy(&loop_index_idr);
  1736. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1737. unregister_blkdev(LOOP_MAJOR, "loop");
  1738. misc_deregister(&loop_misc);
  1739. }
  1740. module_init(loop_init);
  1741. module_exit(loop_exit);
  1742. #ifndef MODULE
  1743. static int __init max_loop_setup(char *str)
  1744. {
  1745. max_loop = simple_strtol(str, NULL, 0);
  1746. return 1;
  1747. }
  1748. __setup("max_loop=", max_loop_setup);
  1749. #endif