loop.c 47 KB

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