loop.c 47 KB

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