loop.c 46 KB

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