loop.c 47 KB

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