loop.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 prepare_write and/or commit_write are not available on the
  44. * backing filesystem.
  45. * Anton Altaparmakov, 16 Feb 2005
  46. *
  47. * Still To Fix:
  48. * - Advisory locking is ignored here.
  49. * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  50. *
  51. */
  52. #include <linux/module.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/sched.h>
  55. #include <linux/fs.h>
  56. #include <linux/file.h>
  57. #include <linux/stat.h>
  58. #include <linux/errno.h>
  59. #include <linux/major.h>
  60. #include <linux/wait.h>
  61. #include <linux/blkdev.h>
  62. #include <linux/blkpg.h>
  63. #include <linux/init.h>
  64. #include <linux/smp_lock.h>
  65. #include <linux/swap.h>
  66. #include <linux/slab.h>
  67. #include <linux/loop.h>
  68. #include <linux/compat.h>
  69. #include <linux/suspend.h>
  70. #include <linux/freezer.h>
  71. #include <linux/writeback.h>
  72. #include <linux/buffer_head.h> /* for invalidate_bdev() */
  73. #include <linux/completion.h>
  74. #include <linux/highmem.h>
  75. #include <linux/gfp.h>
  76. #include <linux/kthread.h>
  77. #include <linux/splice.h>
  78. #include <asm/uaccess.h>
  79. static LIST_HEAD(loop_devices);
  80. static DEFINE_MUTEX(loop_devices_mutex);
  81. static int max_part;
  82. static int part_shift;
  83. /*
  84. * Transfer functions
  85. */
  86. static int transfer_none(struct loop_device *lo, int cmd,
  87. struct page *raw_page, unsigned raw_off,
  88. struct page *loop_page, unsigned loop_off,
  89. int size, sector_t real_block)
  90. {
  91. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  92. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  93. if (cmd == READ)
  94. memcpy(loop_buf, raw_buf, size);
  95. else
  96. memcpy(raw_buf, loop_buf, size);
  97. kunmap_atomic(raw_buf, KM_USER0);
  98. kunmap_atomic(loop_buf, KM_USER1);
  99. cond_resched();
  100. return 0;
  101. }
  102. static int transfer_xor(struct loop_device *lo, int cmd,
  103. struct page *raw_page, unsigned raw_off,
  104. struct page *loop_page, unsigned loop_off,
  105. int size, sector_t real_block)
  106. {
  107. char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
  108. char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
  109. char *in, *out, *key;
  110. int i, keysize;
  111. if (cmd == READ) {
  112. in = raw_buf;
  113. out = loop_buf;
  114. } else {
  115. in = loop_buf;
  116. out = raw_buf;
  117. }
  118. key = lo->lo_encrypt_key;
  119. keysize = lo->lo_encrypt_key_size;
  120. for (i = 0; i < size; i++)
  121. *out++ = *in++ ^ key[(i & 511) % keysize];
  122. kunmap_atomic(raw_buf, KM_USER0);
  123. kunmap_atomic(loop_buf, KM_USER1);
  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_loop_size(struct loop_device *lo, struct file *file)
  148. {
  149. loff_t size, offset, loopsize;
  150. /* Compute loopsize in bytes */
  151. size = i_size_read(file->f_mapping->host);
  152. offset = lo->lo_offset;
  153. loopsize = size - offset;
  154. if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
  155. loopsize = lo->lo_sizelimit;
  156. /*
  157. * Unfortunately, if we want to do I/O on the device,
  158. * the number of 512-byte sectors has to fit into a sector_t.
  159. */
  160. return loopsize >> 9;
  161. }
  162. static int
  163. figure_loop_size(struct loop_device *lo)
  164. {
  165. loff_t size = get_loop_size(lo, lo->lo_backing_file);
  166. sector_t x = (sector_t)size;
  167. if (unlikely((loff_t)x != size))
  168. return -EFBIG;
  169. set_capacity(lo->lo_disk, x);
  170. return 0;
  171. }
  172. static inline int
  173. lo_do_transfer(struct loop_device *lo, int cmd,
  174. struct page *rpage, unsigned roffs,
  175. struct page *lpage, unsigned loffs,
  176. int size, sector_t rblock)
  177. {
  178. if (unlikely(!lo->transfer))
  179. return 0;
  180. return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
  181. }
  182. /**
  183. * do_lo_send_aops - helper for writing data to a loop device
  184. *
  185. * This is the fast version for backing filesystems which implement the address
  186. * space operations write_begin and write_end.
  187. */
  188. static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
  189. loff_t pos, struct page *unused)
  190. {
  191. struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  192. struct address_space *mapping = file->f_mapping;
  193. pgoff_t index;
  194. unsigned offset, bv_offs;
  195. int len, ret;
  196. mutex_lock(&mapping->host->i_mutex);
  197. index = pos >> PAGE_CACHE_SHIFT;
  198. offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
  199. bv_offs = bvec->bv_offset;
  200. len = bvec->bv_len;
  201. while (len > 0) {
  202. sector_t IV;
  203. unsigned size, copied;
  204. int transfer_result;
  205. struct page *page;
  206. void *fsdata;
  207. IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  208. size = PAGE_CACHE_SIZE - offset;
  209. if (size > len)
  210. size = len;
  211. ret = pagecache_write_begin(file, mapping, pos, size, 0,
  212. &page, &fsdata);
  213. if (ret)
  214. goto fail;
  215. transfer_result = lo_do_transfer(lo, WRITE, page, offset,
  216. bvec->bv_page, bv_offs, size, IV);
  217. copied = size;
  218. if (unlikely(transfer_result))
  219. copied = 0;
  220. ret = pagecache_write_end(file, mapping, pos, size, copied,
  221. page, fsdata);
  222. if (ret < 0 || ret != copied)
  223. goto fail;
  224. if (unlikely(transfer_result))
  225. goto fail;
  226. bv_offs += copied;
  227. len -= copied;
  228. offset = 0;
  229. index++;
  230. pos += copied;
  231. }
  232. ret = 0;
  233. out:
  234. mutex_unlock(&mapping->host->i_mutex);
  235. return ret;
  236. fail:
  237. ret = -1;
  238. goto out;
  239. }
  240. /**
  241. * __do_lo_send_write - helper for writing data to a loop device
  242. *
  243. * This helper just factors out common code between do_lo_send_direct_write()
  244. * and do_lo_send_write().
  245. */
  246. static int __do_lo_send_write(struct file *file,
  247. u8 *buf, const int len, loff_t pos)
  248. {
  249. ssize_t bw;
  250. mm_segment_t old_fs = get_fs();
  251. set_fs(get_ds());
  252. bw = file->f_op->write(file, buf, len, &pos);
  253. set_fs(old_fs);
  254. if (likely(bw == len))
  255. return 0;
  256. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  257. (unsigned long long)pos, len);
  258. if (bw >= 0)
  259. bw = -EIO;
  260. return bw;
  261. }
  262. /**
  263. * do_lo_send_direct_write - helper for writing data to a loop device
  264. *
  265. * This is the fast, non-transforming version for backing filesystems which do
  266. * not implement the address space operations write_begin and write_end.
  267. * It uses the write file operation which should be present on all writeable
  268. * filesystems.
  269. */
  270. static int do_lo_send_direct_write(struct loop_device *lo,
  271. struct bio_vec *bvec, loff_t pos, struct page *page)
  272. {
  273. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  274. kmap(bvec->bv_page) + bvec->bv_offset,
  275. bvec->bv_len, pos);
  276. kunmap(bvec->bv_page);
  277. cond_resched();
  278. return bw;
  279. }
  280. /**
  281. * do_lo_send_write - helper for writing data to a loop device
  282. *
  283. * This is the slow, transforming version for filesystems which do not
  284. * implement the address space operations write_begin and write_end. It
  285. * uses the write file operation which should be present on all writeable
  286. * filesystems.
  287. *
  288. * Using fops->write is slower than using aops->{prepare,commit}_write in the
  289. * transforming case because we need to double buffer the data as we cannot do
  290. * the transformations in place as we do not have direct access to the
  291. * destination pages of the backing file.
  292. */
  293. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  294. loff_t pos, struct page *page)
  295. {
  296. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  297. bvec->bv_offset, bvec->bv_len, pos >> 9);
  298. if (likely(!ret))
  299. return __do_lo_send_write(lo->lo_backing_file,
  300. page_address(page), bvec->bv_len,
  301. pos);
  302. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  303. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  304. if (ret > 0)
  305. ret = -EIO;
  306. return ret;
  307. }
  308. static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
  309. {
  310. int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
  311. struct page *page);
  312. struct bio_vec *bvec;
  313. struct page *page = NULL;
  314. int i, ret = 0;
  315. do_lo_send = do_lo_send_aops;
  316. if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
  317. do_lo_send = do_lo_send_direct_write;
  318. if (lo->transfer != transfer_none) {
  319. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  320. if (unlikely(!page))
  321. goto fail;
  322. kmap(page);
  323. do_lo_send = do_lo_send_write;
  324. }
  325. }
  326. bio_for_each_segment(bvec, bio, i) {
  327. ret = do_lo_send(lo, bvec, pos, page);
  328. if (ret < 0)
  329. break;
  330. pos += bvec->bv_len;
  331. }
  332. if (page) {
  333. kunmap(page);
  334. __free_page(page);
  335. }
  336. out:
  337. return ret;
  338. fail:
  339. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  340. ret = -ENOMEM;
  341. goto out;
  342. }
  343. struct lo_read_data {
  344. struct loop_device *lo;
  345. struct page *page;
  346. unsigned offset;
  347. int bsize;
  348. };
  349. static int
  350. lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  351. struct splice_desc *sd)
  352. {
  353. struct lo_read_data *p = sd->u.data;
  354. struct loop_device *lo = p->lo;
  355. struct page *page = buf->page;
  356. sector_t IV;
  357. size_t size;
  358. int ret;
  359. ret = buf->ops->confirm(pipe, buf);
  360. if (unlikely(ret))
  361. return ret;
  362. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  363. (buf->offset >> 9);
  364. size = sd->len;
  365. if (size > p->bsize)
  366. size = p->bsize;
  367. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  368. printk(KERN_ERR "loop: transfer error block %ld\n",
  369. page->index);
  370. size = -EINVAL;
  371. }
  372. flush_dcache_page(p->page);
  373. if (size > 0)
  374. p->offset += size;
  375. return size;
  376. }
  377. static int
  378. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  379. {
  380. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  381. }
  382. static int
  383. do_lo_receive(struct loop_device *lo,
  384. struct bio_vec *bvec, int bsize, loff_t pos)
  385. {
  386. struct lo_read_data cookie;
  387. struct splice_desc sd;
  388. struct file *file;
  389. long retval;
  390. cookie.lo = lo;
  391. cookie.page = bvec->bv_page;
  392. cookie.offset = bvec->bv_offset;
  393. cookie.bsize = bsize;
  394. sd.len = 0;
  395. sd.total_len = bvec->bv_len;
  396. sd.flags = 0;
  397. sd.pos = pos;
  398. sd.u.data = &cookie;
  399. file = lo->lo_backing_file;
  400. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  401. if (retval < 0)
  402. return retval;
  403. return 0;
  404. }
  405. static int
  406. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  407. {
  408. struct bio_vec *bvec;
  409. int i, ret = 0;
  410. bio_for_each_segment(bvec, bio, i) {
  411. ret = do_lo_receive(lo, bvec, bsize, pos);
  412. if (ret < 0)
  413. break;
  414. pos += bvec->bv_len;
  415. }
  416. return ret;
  417. }
  418. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  419. {
  420. loff_t pos;
  421. int ret;
  422. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  423. if (bio_rw(bio) == WRITE)
  424. ret = lo_send(lo, bio, pos);
  425. else
  426. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  427. return ret;
  428. }
  429. /*
  430. * Add bio to back of pending list
  431. */
  432. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  433. {
  434. if (lo->lo_biotail) {
  435. lo->lo_biotail->bi_next = bio;
  436. lo->lo_biotail = bio;
  437. } else
  438. lo->lo_bio = lo->lo_biotail = bio;
  439. }
  440. /*
  441. * Grab first pending buffer
  442. */
  443. static struct bio *loop_get_bio(struct loop_device *lo)
  444. {
  445. struct bio *bio;
  446. if ((bio = lo->lo_bio)) {
  447. if (bio == lo->lo_biotail)
  448. lo->lo_biotail = NULL;
  449. lo->lo_bio = bio->bi_next;
  450. bio->bi_next = NULL;
  451. }
  452. return bio;
  453. }
  454. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  455. {
  456. struct loop_device *lo = q->queuedata;
  457. int rw = bio_rw(old_bio);
  458. if (rw == READA)
  459. rw = READ;
  460. BUG_ON(!lo || (rw != READ && rw != WRITE));
  461. spin_lock_irq(&lo->lo_lock);
  462. if (lo->lo_state != Lo_bound)
  463. goto out;
  464. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  465. goto out;
  466. loop_add_bio(lo, old_bio);
  467. wake_up(&lo->lo_event);
  468. spin_unlock_irq(&lo->lo_lock);
  469. return 0;
  470. out:
  471. spin_unlock_irq(&lo->lo_lock);
  472. bio_io_error(old_bio);
  473. return 0;
  474. }
  475. /*
  476. * kick off io on the underlying address space
  477. */
  478. static void loop_unplug(struct request_queue *q)
  479. {
  480. struct loop_device *lo = q->queuedata;
  481. queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
  482. blk_run_address_space(lo->lo_backing_file->f_mapping);
  483. }
  484. struct switch_request {
  485. struct file *file;
  486. struct completion wait;
  487. };
  488. static void do_loop_switch(struct loop_device *, struct switch_request *);
  489. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  490. {
  491. if (unlikely(!bio->bi_bdev)) {
  492. do_loop_switch(lo, bio->bi_private);
  493. bio_put(bio);
  494. } else {
  495. int ret = do_bio_filebacked(lo, bio);
  496. bio_endio(bio, ret);
  497. }
  498. }
  499. /*
  500. * worker thread that handles reads/writes to file backed loop devices,
  501. * to avoid blocking in our make_request_fn. it also does loop decrypting
  502. * on reads for block backed loop, as that is too heavy to do from
  503. * b_end_io context where irqs may be disabled.
  504. *
  505. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  506. * calling kthread_stop(). Therefore once kthread_should_stop() is
  507. * true, make_request will not place any more requests. Therefore
  508. * once kthread_should_stop() is true and lo_bio is NULL, we are
  509. * done with the loop.
  510. */
  511. static int loop_thread(void *data)
  512. {
  513. struct loop_device *lo = data;
  514. struct bio *bio;
  515. set_user_nice(current, -20);
  516. while (!kthread_should_stop() || lo->lo_bio) {
  517. wait_event_interruptible(lo->lo_event,
  518. lo->lo_bio || kthread_should_stop());
  519. if (!lo->lo_bio)
  520. continue;
  521. spin_lock_irq(&lo->lo_lock);
  522. bio = loop_get_bio(lo);
  523. spin_unlock_irq(&lo->lo_lock);
  524. BUG_ON(!bio);
  525. loop_handle_bio(lo, bio);
  526. }
  527. return 0;
  528. }
  529. /*
  530. * loop_switch performs the hard work of switching a backing store.
  531. * First it needs to flush existing IO, it does this by sending a magic
  532. * BIO down the pipe. The completion of this BIO does the actual switch.
  533. */
  534. static int loop_switch(struct loop_device *lo, struct file *file)
  535. {
  536. struct switch_request w;
  537. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  538. if (!bio)
  539. return -ENOMEM;
  540. init_completion(&w.wait);
  541. w.file = file;
  542. bio->bi_private = &w;
  543. bio->bi_bdev = NULL;
  544. loop_make_request(lo->lo_queue, bio);
  545. wait_for_completion(&w.wait);
  546. return 0;
  547. }
  548. /*
  549. * Do the actual switch; called from the BIO completion routine
  550. */
  551. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  552. {
  553. struct file *file = p->file;
  554. struct file *old_file = lo->lo_backing_file;
  555. struct address_space *mapping = file->f_mapping;
  556. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  557. lo->lo_backing_file = file;
  558. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  559. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  560. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  561. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  562. complete(&p->wait);
  563. }
  564. /*
  565. * loop_change_fd switched the backing store of a loopback device to
  566. * a new file. This is useful for operating system installers to free up
  567. * the original file and in High Availability environments to switch to
  568. * an alternative location for the content in case of server meltdown.
  569. * This can only work if the loop device is used read-only, and if the
  570. * new backing store is the same size and type as the old backing store.
  571. */
  572. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  573. unsigned int arg)
  574. {
  575. struct file *file, *old_file;
  576. struct inode *inode;
  577. int error;
  578. error = -ENXIO;
  579. if (lo->lo_state != Lo_bound)
  580. goto out;
  581. /* the loop device has to be read-only */
  582. error = -EINVAL;
  583. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  584. goto out;
  585. error = -EBADF;
  586. file = fget(arg);
  587. if (!file)
  588. goto out;
  589. inode = file->f_mapping->host;
  590. old_file = lo->lo_backing_file;
  591. error = -EINVAL;
  592. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  593. goto out_putf;
  594. /* new backing store needs to support loop (eg splice_read) */
  595. if (!inode->i_fop->splice_read)
  596. goto out_putf;
  597. /* size of the new backing store needs to be the same */
  598. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  599. goto out_putf;
  600. /* and ... switch */
  601. error = loop_switch(lo, file);
  602. if (error)
  603. goto out_putf;
  604. fput(old_file);
  605. if (max_part > 0)
  606. ioctl_by_bdev(bdev, BLKRRPART, 0);
  607. return 0;
  608. out_putf:
  609. fput(file);
  610. out:
  611. return error;
  612. }
  613. static inline int is_loop_device(struct file *file)
  614. {
  615. struct inode *i = file->f_mapping->host;
  616. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  617. }
  618. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  619. struct block_device *bdev, unsigned int arg)
  620. {
  621. struct file *file, *f;
  622. struct inode *inode;
  623. struct address_space *mapping;
  624. unsigned lo_blocksize;
  625. int lo_flags = 0;
  626. int error;
  627. loff_t size;
  628. /* This is safe, since we have a reference from open(). */
  629. __module_get(THIS_MODULE);
  630. error = -EBADF;
  631. file = fget(arg);
  632. if (!file)
  633. goto out;
  634. error = -EBUSY;
  635. if (lo->lo_state != Lo_unbound)
  636. goto out_putf;
  637. /* Avoid recursion */
  638. f = file;
  639. while (is_loop_device(f)) {
  640. struct loop_device *l;
  641. if (f->f_mapping->host->i_bdev == bdev)
  642. goto out_putf;
  643. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  644. if (l->lo_state == Lo_unbound) {
  645. error = -EINVAL;
  646. goto out_putf;
  647. }
  648. f = l->lo_backing_file;
  649. }
  650. mapping = file->f_mapping;
  651. inode = mapping->host;
  652. if (!(file->f_mode & FMODE_WRITE))
  653. lo_flags |= LO_FLAGS_READ_ONLY;
  654. error = -EINVAL;
  655. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  656. const struct address_space_operations *aops = mapping->a_ops;
  657. /*
  658. * If we can't read - sorry. If we only can't write - well,
  659. * it's going to be read-only.
  660. */
  661. if (!file->f_op->splice_read)
  662. goto out_putf;
  663. if (aops->prepare_write || aops->write_begin)
  664. lo_flags |= LO_FLAGS_USE_AOPS;
  665. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  666. lo_flags |= LO_FLAGS_READ_ONLY;
  667. lo_blocksize = S_ISBLK(inode->i_mode) ?
  668. inode->i_bdev->bd_block_size : PAGE_SIZE;
  669. error = 0;
  670. } else {
  671. goto out_putf;
  672. }
  673. size = get_loop_size(lo, file);
  674. if ((loff_t)(sector_t)size != size) {
  675. error = -EFBIG;
  676. goto out_putf;
  677. }
  678. if (!(mode & FMODE_WRITE))
  679. lo_flags |= LO_FLAGS_READ_ONLY;
  680. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  681. lo->lo_blocksize = lo_blocksize;
  682. lo->lo_device = bdev;
  683. lo->lo_flags = lo_flags;
  684. lo->lo_backing_file = file;
  685. lo->transfer = transfer_none;
  686. lo->ioctl = NULL;
  687. lo->lo_sizelimit = 0;
  688. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  689. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  690. lo->lo_bio = lo->lo_biotail = NULL;
  691. /*
  692. * set queue make_request_fn, and add limits based on lower level
  693. * device
  694. */
  695. blk_queue_make_request(lo->lo_queue, loop_make_request);
  696. lo->lo_queue->queuedata = lo;
  697. lo->lo_queue->unplug_fn = loop_unplug;
  698. set_capacity(lo->lo_disk, size);
  699. bd_set_size(bdev, size << 9);
  700. set_blocksize(bdev, lo_blocksize);
  701. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  702. lo->lo_number);
  703. if (IS_ERR(lo->lo_thread)) {
  704. error = PTR_ERR(lo->lo_thread);
  705. goto out_clr;
  706. }
  707. lo->lo_state = Lo_bound;
  708. wake_up_process(lo->lo_thread);
  709. if (max_part > 0)
  710. ioctl_by_bdev(bdev, BLKRRPART, 0);
  711. return 0;
  712. out_clr:
  713. lo->lo_thread = NULL;
  714. lo->lo_device = NULL;
  715. lo->lo_backing_file = NULL;
  716. lo->lo_flags = 0;
  717. set_capacity(lo->lo_disk, 0);
  718. invalidate_bdev(bdev);
  719. bd_set_size(bdev, 0);
  720. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  721. lo->lo_state = Lo_unbound;
  722. out_putf:
  723. fput(file);
  724. out:
  725. /* This is safe: open() is still holding a reference. */
  726. module_put(THIS_MODULE);
  727. return error;
  728. }
  729. static int
  730. loop_release_xfer(struct loop_device *lo)
  731. {
  732. int err = 0;
  733. struct loop_func_table *xfer = lo->lo_encryption;
  734. if (xfer) {
  735. if (xfer->release)
  736. err = xfer->release(lo);
  737. lo->transfer = NULL;
  738. lo->lo_encryption = NULL;
  739. module_put(xfer->owner);
  740. }
  741. return err;
  742. }
  743. static int
  744. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  745. const struct loop_info64 *i)
  746. {
  747. int err = 0;
  748. if (xfer) {
  749. struct module *owner = xfer->owner;
  750. if (!try_module_get(owner))
  751. return -EINVAL;
  752. if (xfer->init)
  753. err = xfer->init(lo, i);
  754. if (err)
  755. module_put(owner);
  756. else
  757. lo->lo_encryption = xfer;
  758. }
  759. return err;
  760. }
  761. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  762. {
  763. struct file *filp = lo->lo_backing_file;
  764. gfp_t gfp = lo->old_gfp_mask;
  765. if (lo->lo_state != Lo_bound)
  766. return -ENXIO;
  767. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  768. return -EBUSY;
  769. if (filp == NULL)
  770. return -EINVAL;
  771. spin_lock_irq(&lo->lo_lock);
  772. lo->lo_state = Lo_rundown;
  773. spin_unlock_irq(&lo->lo_lock);
  774. kthread_stop(lo->lo_thread);
  775. lo->lo_backing_file = NULL;
  776. loop_release_xfer(lo);
  777. lo->transfer = NULL;
  778. lo->ioctl = NULL;
  779. lo->lo_device = NULL;
  780. lo->lo_encryption = NULL;
  781. lo->lo_offset = 0;
  782. lo->lo_sizelimit = 0;
  783. lo->lo_encrypt_key_size = 0;
  784. lo->lo_flags = 0;
  785. lo->lo_thread = NULL;
  786. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  787. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  788. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  789. if (bdev)
  790. invalidate_bdev(bdev);
  791. set_capacity(lo->lo_disk, 0);
  792. if (bdev)
  793. bd_set_size(bdev, 0);
  794. mapping_set_gfp_mask(filp->f_mapping, gfp);
  795. lo->lo_state = Lo_unbound;
  796. fput(filp);
  797. /* This is safe: open() is still holding a reference. */
  798. module_put(THIS_MODULE);
  799. if (max_part > 0)
  800. ioctl_by_bdev(bdev, BLKRRPART, 0);
  801. return 0;
  802. }
  803. static int
  804. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  805. {
  806. int err;
  807. struct loop_func_table *xfer;
  808. if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
  809. !capable(CAP_SYS_ADMIN))
  810. return -EPERM;
  811. if (lo->lo_state != Lo_bound)
  812. return -ENXIO;
  813. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  814. return -EINVAL;
  815. err = loop_release_xfer(lo);
  816. if (err)
  817. return err;
  818. if (info->lo_encrypt_type) {
  819. unsigned int type = info->lo_encrypt_type;
  820. if (type >= MAX_LO_CRYPT)
  821. return -EINVAL;
  822. xfer = xfer_funcs[type];
  823. if (xfer == NULL)
  824. return -EINVAL;
  825. } else
  826. xfer = NULL;
  827. err = loop_init_xfer(lo, xfer, info);
  828. if (err)
  829. return err;
  830. if (lo->lo_offset != info->lo_offset ||
  831. lo->lo_sizelimit != info->lo_sizelimit) {
  832. lo->lo_offset = info->lo_offset;
  833. lo->lo_sizelimit = info->lo_sizelimit;
  834. if (figure_loop_size(lo))
  835. return -EFBIG;
  836. }
  837. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  838. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  839. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  840. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  841. if (!xfer)
  842. xfer = &none_funcs;
  843. lo->transfer = xfer->transfer;
  844. lo->ioctl = xfer->ioctl;
  845. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  846. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  847. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  848. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  849. lo->lo_init[0] = info->lo_init[0];
  850. lo->lo_init[1] = info->lo_init[1];
  851. if (info->lo_encrypt_key_size) {
  852. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  853. info->lo_encrypt_key_size);
  854. lo->lo_key_owner = current->uid;
  855. }
  856. return 0;
  857. }
  858. static int
  859. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  860. {
  861. struct file *file = lo->lo_backing_file;
  862. struct kstat stat;
  863. int error;
  864. if (lo->lo_state != Lo_bound)
  865. return -ENXIO;
  866. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  867. if (error)
  868. return error;
  869. memset(info, 0, sizeof(*info));
  870. info->lo_number = lo->lo_number;
  871. info->lo_device = huge_encode_dev(stat.dev);
  872. info->lo_inode = stat.ino;
  873. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  874. info->lo_offset = lo->lo_offset;
  875. info->lo_sizelimit = lo->lo_sizelimit;
  876. info->lo_flags = lo->lo_flags;
  877. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  878. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  879. info->lo_encrypt_type =
  880. lo->lo_encryption ? lo->lo_encryption->number : 0;
  881. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  882. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  883. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  884. lo->lo_encrypt_key_size);
  885. }
  886. return 0;
  887. }
  888. static void
  889. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  890. {
  891. memset(info64, 0, sizeof(*info64));
  892. info64->lo_number = info->lo_number;
  893. info64->lo_device = info->lo_device;
  894. info64->lo_inode = info->lo_inode;
  895. info64->lo_rdevice = info->lo_rdevice;
  896. info64->lo_offset = info->lo_offset;
  897. info64->lo_sizelimit = 0;
  898. info64->lo_encrypt_type = info->lo_encrypt_type;
  899. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  900. info64->lo_flags = info->lo_flags;
  901. info64->lo_init[0] = info->lo_init[0];
  902. info64->lo_init[1] = info->lo_init[1];
  903. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  904. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  905. else
  906. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  907. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  908. }
  909. static int
  910. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  911. {
  912. memset(info, 0, sizeof(*info));
  913. info->lo_number = info64->lo_number;
  914. info->lo_device = info64->lo_device;
  915. info->lo_inode = info64->lo_inode;
  916. info->lo_rdevice = info64->lo_rdevice;
  917. info->lo_offset = info64->lo_offset;
  918. info->lo_encrypt_type = info64->lo_encrypt_type;
  919. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  920. info->lo_flags = info64->lo_flags;
  921. info->lo_init[0] = info64->lo_init[0];
  922. info->lo_init[1] = info64->lo_init[1];
  923. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  924. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  925. else
  926. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  927. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  928. /* error in case values were truncated */
  929. if (info->lo_device != info64->lo_device ||
  930. info->lo_rdevice != info64->lo_rdevice ||
  931. info->lo_inode != info64->lo_inode ||
  932. info->lo_offset != info64->lo_offset)
  933. return -EOVERFLOW;
  934. return 0;
  935. }
  936. static int
  937. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  938. {
  939. struct loop_info info;
  940. struct loop_info64 info64;
  941. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  942. return -EFAULT;
  943. loop_info64_from_old(&info, &info64);
  944. return loop_set_status(lo, &info64);
  945. }
  946. static int
  947. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  948. {
  949. struct loop_info64 info64;
  950. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  951. return -EFAULT;
  952. return loop_set_status(lo, &info64);
  953. }
  954. static int
  955. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  956. struct loop_info info;
  957. struct loop_info64 info64;
  958. int err = 0;
  959. if (!arg)
  960. err = -EINVAL;
  961. if (!err)
  962. err = loop_get_status(lo, &info64);
  963. if (!err)
  964. err = loop_info64_to_old(&info64, &info);
  965. if (!err && copy_to_user(arg, &info, sizeof(info)))
  966. err = -EFAULT;
  967. return err;
  968. }
  969. static int
  970. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  971. struct loop_info64 info64;
  972. int err = 0;
  973. if (!arg)
  974. err = -EINVAL;
  975. if (!err)
  976. err = loop_get_status(lo, &info64);
  977. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  978. err = -EFAULT;
  979. return err;
  980. }
  981. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  982. unsigned int cmd, unsigned long arg)
  983. {
  984. struct loop_device *lo = bdev->bd_disk->private_data;
  985. int err;
  986. mutex_lock(&lo->lo_ctl_mutex);
  987. switch (cmd) {
  988. case LOOP_SET_FD:
  989. err = loop_set_fd(lo, mode, bdev, arg);
  990. break;
  991. case LOOP_CHANGE_FD:
  992. err = loop_change_fd(lo, bdev, arg);
  993. break;
  994. case LOOP_CLR_FD:
  995. err = loop_clr_fd(lo, bdev);
  996. break;
  997. case LOOP_SET_STATUS:
  998. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  999. break;
  1000. case LOOP_GET_STATUS:
  1001. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1002. break;
  1003. case LOOP_SET_STATUS64:
  1004. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1005. break;
  1006. case LOOP_GET_STATUS64:
  1007. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1008. break;
  1009. default:
  1010. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1011. }
  1012. mutex_unlock(&lo->lo_ctl_mutex);
  1013. return err;
  1014. }
  1015. #ifdef CONFIG_COMPAT
  1016. struct compat_loop_info {
  1017. compat_int_t lo_number; /* ioctl r/o */
  1018. compat_dev_t lo_device; /* ioctl r/o */
  1019. compat_ulong_t lo_inode; /* ioctl r/o */
  1020. compat_dev_t lo_rdevice; /* ioctl r/o */
  1021. compat_int_t lo_offset;
  1022. compat_int_t lo_encrypt_type;
  1023. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1024. compat_int_t lo_flags; /* ioctl r/o */
  1025. char lo_name[LO_NAME_SIZE];
  1026. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1027. compat_ulong_t lo_init[2];
  1028. char reserved[4];
  1029. };
  1030. /*
  1031. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1032. * - noinlined to reduce stack space usage in main part of driver
  1033. */
  1034. static noinline int
  1035. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1036. struct loop_info64 *info64)
  1037. {
  1038. struct compat_loop_info info;
  1039. if (copy_from_user(&info, arg, sizeof(info)))
  1040. return -EFAULT;
  1041. memset(info64, 0, sizeof(*info64));
  1042. info64->lo_number = info.lo_number;
  1043. info64->lo_device = info.lo_device;
  1044. info64->lo_inode = info.lo_inode;
  1045. info64->lo_rdevice = info.lo_rdevice;
  1046. info64->lo_offset = info.lo_offset;
  1047. info64->lo_sizelimit = 0;
  1048. info64->lo_encrypt_type = info.lo_encrypt_type;
  1049. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1050. info64->lo_flags = info.lo_flags;
  1051. info64->lo_init[0] = info.lo_init[0];
  1052. info64->lo_init[1] = info.lo_init[1];
  1053. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1054. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1055. else
  1056. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1057. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1058. return 0;
  1059. }
  1060. /*
  1061. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1062. * - noinlined to reduce stack space usage in main part of driver
  1063. */
  1064. static noinline int
  1065. loop_info64_to_compat(const struct loop_info64 *info64,
  1066. struct compat_loop_info __user *arg)
  1067. {
  1068. struct compat_loop_info info;
  1069. memset(&info, 0, sizeof(info));
  1070. info.lo_number = info64->lo_number;
  1071. info.lo_device = info64->lo_device;
  1072. info.lo_inode = info64->lo_inode;
  1073. info.lo_rdevice = info64->lo_rdevice;
  1074. info.lo_offset = info64->lo_offset;
  1075. info.lo_encrypt_type = info64->lo_encrypt_type;
  1076. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1077. info.lo_flags = info64->lo_flags;
  1078. info.lo_init[0] = info64->lo_init[0];
  1079. info.lo_init[1] = info64->lo_init[1];
  1080. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1081. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1082. else
  1083. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1084. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1085. /* error in case values were truncated */
  1086. if (info.lo_device != info64->lo_device ||
  1087. info.lo_rdevice != info64->lo_rdevice ||
  1088. info.lo_inode != info64->lo_inode ||
  1089. info.lo_offset != info64->lo_offset ||
  1090. info.lo_init[0] != info64->lo_init[0] ||
  1091. info.lo_init[1] != info64->lo_init[1])
  1092. return -EOVERFLOW;
  1093. if (copy_to_user(arg, &info, sizeof(info)))
  1094. return -EFAULT;
  1095. return 0;
  1096. }
  1097. static int
  1098. loop_set_status_compat(struct loop_device *lo,
  1099. const struct compat_loop_info __user *arg)
  1100. {
  1101. struct loop_info64 info64;
  1102. int ret;
  1103. ret = loop_info64_from_compat(arg, &info64);
  1104. if (ret < 0)
  1105. return ret;
  1106. return loop_set_status(lo, &info64);
  1107. }
  1108. static int
  1109. loop_get_status_compat(struct loop_device *lo,
  1110. struct compat_loop_info __user *arg)
  1111. {
  1112. struct loop_info64 info64;
  1113. int err = 0;
  1114. if (!arg)
  1115. err = -EINVAL;
  1116. if (!err)
  1117. err = loop_get_status(lo, &info64);
  1118. if (!err)
  1119. err = loop_info64_to_compat(&info64, arg);
  1120. return err;
  1121. }
  1122. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1123. unsigned int cmd, unsigned long arg)
  1124. {
  1125. struct loop_device *lo = bdev->bd_disk->private_data;
  1126. int err;
  1127. switch(cmd) {
  1128. case LOOP_SET_STATUS:
  1129. mutex_lock(&lo->lo_ctl_mutex);
  1130. err = loop_set_status_compat(
  1131. lo, (const struct compat_loop_info __user *) arg);
  1132. mutex_unlock(&lo->lo_ctl_mutex);
  1133. break;
  1134. case LOOP_GET_STATUS:
  1135. mutex_lock(&lo->lo_ctl_mutex);
  1136. err = loop_get_status_compat(
  1137. lo, (struct compat_loop_info __user *) arg);
  1138. mutex_unlock(&lo->lo_ctl_mutex);
  1139. break;
  1140. case LOOP_CLR_FD:
  1141. case LOOP_GET_STATUS64:
  1142. case LOOP_SET_STATUS64:
  1143. arg = (unsigned long) compat_ptr(arg);
  1144. case LOOP_SET_FD:
  1145. case LOOP_CHANGE_FD:
  1146. err = lo_ioctl(bdev, mode, cmd, arg);
  1147. break;
  1148. default:
  1149. err = -ENOIOCTLCMD;
  1150. break;
  1151. }
  1152. return err;
  1153. }
  1154. #endif
  1155. static int lo_open(struct block_device *bdev, fmode_t mode)
  1156. {
  1157. struct loop_device *lo = bdev->bd_disk->private_data;
  1158. mutex_lock(&lo->lo_ctl_mutex);
  1159. lo->lo_refcnt++;
  1160. mutex_unlock(&lo->lo_ctl_mutex);
  1161. return 0;
  1162. }
  1163. static int lo_release(struct gendisk *disk, fmode_t mode)
  1164. {
  1165. struct loop_device *lo = disk->private_data;
  1166. mutex_lock(&lo->lo_ctl_mutex);
  1167. --lo->lo_refcnt;
  1168. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) && !lo->lo_refcnt)
  1169. loop_clr_fd(lo, NULL);
  1170. mutex_unlock(&lo->lo_ctl_mutex);
  1171. return 0;
  1172. }
  1173. static struct block_device_operations lo_fops = {
  1174. .owner = THIS_MODULE,
  1175. .open = lo_open,
  1176. .release = lo_release,
  1177. .ioctl = lo_ioctl,
  1178. #ifdef CONFIG_COMPAT
  1179. .compat_ioctl = lo_compat_ioctl,
  1180. #endif
  1181. };
  1182. /*
  1183. * And now the modules code and kernel interface.
  1184. */
  1185. static int max_loop;
  1186. module_param(max_loop, int, 0);
  1187. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1188. module_param(max_part, int, 0);
  1189. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1190. MODULE_LICENSE("GPL");
  1191. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1192. int loop_register_transfer(struct loop_func_table *funcs)
  1193. {
  1194. unsigned int n = funcs->number;
  1195. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1196. return -EINVAL;
  1197. xfer_funcs[n] = funcs;
  1198. return 0;
  1199. }
  1200. int loop_unregister_transfer(int number)
  1201. {
  1202. unsigned int n = number;
  1203. struct loop_device *lo;
  1204. struct loop_func_table *xfer;
  1205. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1206. return -EINVAL;
  1207. xfer_funcs[n] = NULL;
  1208. list_for_each_entry(lo, &loop_devices, lo_list) {
  1209. mutex_lock(&lo->lo_ctl_mutex);
  1210. if (lo->lo_encryption == xfer)
  1211. loop_release_xfer(lo);
  1212. mutex_unlock(&lo->lo_ctl_mutex);
  1213. }
  1214. return 0;
  1215. }
  1216. EXPORT_SYMBOL(loop_register_transfer);
  1217. EXPORT_SYMBOL(loop_unregister_transfer);
  1218. static struct loop_device *loop_alloc(int i)
  1219. {
  1220. struct loop_device *lo;
  1221. struct gendisk *disk;
  1222. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1223. if (!lo)
  1224. goto out;
  1225. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1226. if (!lo->lo_queue)
  1227. goto out_free_dev;
  1228. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1229. if (!disk)
  1230. goto out_free_queue;
  1231. mutex_init(&lo->lo_ctl_mutex);
  1232. lo->lo_number = i;
  1233. lo->lo_thread = NULL;
  1234. init_waitqueue_head(&lo->lo_event);
  1235. spin_lock_init(&lo->lo_lock);
  1236. disk->major = LOOP_MAJOR;
  1237. disk->first_minor = i << part_shift;
  1238. disk->fops = &lo_fops;
  1239. disk->private_data = lo;
  1240. disk->queue = lo->lo_queue;
  1241. sprintf(disk->disk_name, "loop%d", i);
  1242. return lo;
  1243. out_free_queue:
  1244. blk_cleanup_queue(lo->lo_queue);
  1245. out_free_dev:
  1246. kfree(lo);
  1247. out:
  1248. return NULL;
  1249. }
  1250. static void loop_free(struct loop_device *lo)
  1251. {
  1252. blk_cleanup_queue(lo->lo_queue);
  1253. put_disk(lo->lo_disk);
  1254. list_del(&lo->lo_list);
  1255. kfree(lo);
  1256. }
  1257. static struct loop_device *loop_init_one(int i)
  1258. {
  1259. struct loop_device *lo;
  1260. list_for_each_entry(lo, &loop_devices, lo_list) {
  1261. if (lo->lo_number == i)
  1262. return lo;
  1263. }
  1264. lo = loop_alloc(i);
  1265. if (lo) {
  1266. add_disk(lo->lo_disk);
  1267. list_add_tail(&lo->lo_list, &loop_devices);
  1268. }
  1269. return lo;
  1270. }
  1271. static void loop_del_one(struct loop_device *lo)
  1272. {
  1273. del_gendisk(lo->lo_disk);
  1274. loop_free(lo);
  1275. }
  1276. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1277. {
  1278. struct loop_device *lo;
  1279. struct kobject *kobj;
  1280. mutex_lock(&loop_devices_mutex);
  1281. lo = loop_init_one(dev & MINORMASK);
  1282. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1283. mutex_unlock(&loop_devices_mutex);
  1284. *part = 0;
  1285. return kobj;
  1286. }
  1287. static int __init loop_init(void)
  1288. {
  1289. int i, nr;
  1290. unsigned long range;
  1291. struct loop_device *lo, *next;
  1292. /*
  1293. * loop module now has a feature to instantiate underlying device
  1294. * structure on-demand, provided that there is an access dev node.
  1295. * However, this will not work well with user space tool that doesn't
  1296. * know about such "feature". In order to not break any existing
  1297. * tool, we do the following:
  1298. *
  1299. * (1) if max_loop is specified, create that many upfront, and this
  1300. * also becomes a hard limit.
  1301. * (2) if max_loop is not specified, create 8 loop device on module
  1302. * load, user can further extend loop device by create dev node
  1303. * themselves and have kernel automatically instantiate actual
  1304. * device on-demand.
  1305. */
  1306. part_shift = 0;
  1307. if (max_part > 0)
  1308. part_shift = fls(max_part);
  1309. if (max_loop > 1UL << (MINORBITS - part_shift))
  1310. return -EINVAL;
  1311. if (max_loop) {
  1312. nr = max_loop;
  1313. range = max_loop;
  1314. } else {
  1315. nr = 8;
  1316. range = 1UL << (MINORBITS - part_shift);
  1317. }
  1318. if (register_blkdev(LOOP_MAJOR, "loop"))
  1319. return -EIO;
  1320. for (i = 0; i < nr; i++) {
  1321. lo = loop_alloc(i);
  1322. if (!lo)
  1323. goto Enomem;
  1324. list_add_tail(&lo->lo_list, &loop_devices);
  1325. }
  1326. /* point of no return */
  1327. list_for_each_entry(lo, &loop_devices, lo_list)
  1328. add_disk(lo->lo_disk);
  1329. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1330. THIS_MODULE, loop_probe, NULL, NULL);
  1331. printk(KERN_INFO "loop: module loaded\n");
  1332. return 0;
  1333. Enomem:
  1334. printk(KERN_INFO "loop: out of memory\n");
  1335. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1336. loop_free(lo);
  1337. unregister_blkdev(LOOP_MAJOR, "loop");
  1338. return -ENOMEM;
  1339. }
  1340. static void __exit loop_exit(void)
  1341. {
  1342. unsigned long range;
  1343. struct loop_device *lo, *next;
  1344. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1345. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1346. loop_del_one(lo);
  1347. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1348. unregister_blkdev(LOOP_MAJOR, "loop");
  1349. }
  1350. module_init(loop_init);
  1351. module_exit(loop_exit);
  1352. #ifndef MODULE
  1353. static int __init max_loop_setup(char *str)
  1354. {
  1355. max_loop = simple_strtol(str, NULL, 0);
  1356. return 1;
  1357. }
  1358. __setup("max_loop=", max_loop_setup);
  1359. #endif