loop.c 47 KB

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