loop.c 37 KB

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