loop.c 47 KB

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