loop.c 47 KB

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