loop.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /*
  2. * linux/drivers/block/loop.c
  3. *
  4. * Written by Theodore Ts'o, 3/29/93
  5. *
  6. * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
  7. * permitted under the GNU General Public License.
  8. *
  9. * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
  10. * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
  11. *
  12. * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
  13. * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
  14. *
  15. * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
  16. *
  17. * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
  18. *
  19. * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
  20. *
  21. * Loadable modules and other fixes by AK, 1998
  22. *
  23. * Make real block number available to downstream transfer functions, enables
  24. * CBC (and relatives) mode encryption requiring unique IVs per data block.
  25. * Reed H. Petty, rhp@draper.net
  26. *
  27. * Maximum number of loop devices now dynamic via max_loop module parameter.
  28. * Russell Kroll <rkroll@exploits.org> 19990701
  29. *
  30. * Maximum number of loop devices when compiled-in now selectable by passing
  31. * max_loop=<1-255> to the kernel on boot.
  32. * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
  33. *
  34. * Completely rewrite request handling to be make_request_fn style and
  35. * non blocking, pushing work to a helper thread. Lots of fixes from
  36. * Al Viro too.
  37. * Jens Axboe <axboe@suse.de>, Nov 2000
  38. *
  39. * Support up to 256 loop devices
  40. * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
  41. *
  42. * Support for falling back on the write file operation when the address space
  43. * operations write_begin is not available on the backing filesystem.
  44. * Anton Altaparmakov, 16 Feb 2005
  45. *
  46. * Still To Fix:
  47. * - Advisory locking is ignored here.
  48. * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  49. *
  50. */
  51. #include <linux/module.h>
  52. #include <linux/moduleparam.h>
  53. #include <linux/sched.h>
  54. #include <linux/fs.h>
  55. #include <linux/file.h>
  56. #include <linux/stat.h>
  57. #include <linux/errno.h>
  58. #include <linux/major.h>
  59. #include <linux/wait.h>
  60. #include <linux/blkdev.h>
  61. #include <linux/blkpg.h>
  62. #include <linux/init.h>
  63. #include <linux/swap.h>
  64. #include <linux/slab.h>
  65. #include <linux/loop.h>
  66. #include <linux/compat.h>
  67. #include <linux/suspend.h>
  68. #include <linux/freezer.h>
  69. #include <linux/writeback.h>
  70. #include <linux/buffer_head.h> /* for invalidate_bdev() */
  71. #include <linux/completion.h>
  72. #include <linux/highmem.h>
  73. #include <linux/kthread.h>
  74. #include <linux/splice.h>
  75. #include <asm/uaccess.h>
  76. static LIST_HEAD(loop_devices);
  77. static DEFINE_MUTEX(loop_devices_mutex);
  78. static int max_part;
  79. static int part_shift;
  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(lo->lo_disk, 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 write_begin and write_end.
  184. */
  185. static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
  186. loff_t pos, struct page *unused)
  187. {
  188. struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  189. struct address_space *mapping = file->f_mapping;
  190. pgoff_t index;
  191. unsigned offset, bv_offs;
  192. int len, ret;
  193. mutex_lock(&mapping->host->i_mutex);
  194. index = pos >> PAGE_CACHE_SHIFT;
  195. offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
  196. bv_offs = bvec->bv_offset;
  197. len = bvec->bv_len;
  198. while (len > 0) {
  199. sector_t IV;
  200. unsigned size, copied;
  201. int transfer_result;
  202. struct page *page;
  203. void *fsdata;
  204. IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
  205. size = PAGE_CACHE_SIZE - offset;
  206. if (size > len)
  207. size = len;
  208. ret = pagecache_write_begin(file, mapping, pos, size, 0,
  209. &page, &fsdata);
  210. if (ret)
  211. goto fail;
  212. file_update_time(file);
  213. transfer_result = lo_do_transfer(lo, WRITE, page, offset,
  214. bvec->bv_page, bv_offs, size, IV);
  215. copied = size;
  216. if (unlikely(transfer_result))
  217. copied = 0;
  218. ret = pagecache_write_end(file, mapping, pos, size, copied,
  219. page, fsdata);
  220. if (ret < 0 || ret != copied)
  221. goto fail;
  222. if (unlikely(transfer_result))
  223. goto fail;
  224. bv_offs += copied;
  225. len -= copied;
  226. offset = 0;
  227. index++;
  228. pos += copied;
  229. }
  230. ret = 0;
  231. out:
  232. mutex_unlock(&mapping->host->i_mutex);
  233. return ret;
  234. fail:
  235. ret = -1;
  236. goto out;
  237. }
  238. /**
  239. * __do_lo_send_write - helper for writing data to a loop device
  240. *
  241. * This helper just factors out common code between do_lo_send_direct_write()
  242. * and do_lo_send_write().
  243. */
  244. static int __do_lo_send_write(struct file *file,
  245. u8 *buf, const int len, loff_t pos)
  246. {
  247. ssize_t bw;
  248. mm_segment_t old_fs = get_fs();
  249. set_fs(get_ds());
  250. bw = file->f_op->write(file, buf, len, &pos);
  251. set_fs(old_fs);
  252. if (likely(bw == len))
  253. return 0;
  254. printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
  255. (unsigned long long)pos, len);
  256. if (bw >= 0)
  257. bw = -EIO;
  258. return bw;
  259. }
  260. /**
  261. * do_lo_send_direct_write - helper for writing data to a loop device
  262. *
  263. * This is the fast, non-transforming version for backing filesystems which do
  264. * not implement the address space operations write_begin and write_end.
  265. * It uses the write file operation which should be present on all writeable
  266. * filesystems.
  267. */
  268. static int do_lo_send_direct_write(struct loop_device *lo,
  269. struct bio_vec *bvec, loff_t pos, struct page *page)
  270. {
  271. ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
  272. kmap(bvec->bv_page) + bvec->bv_offset,
  273. bvec->bv_len, pos);
  274. kunmap(bvec->bv_page);
  275. cond_resched();
  276. return bw;
  277. }
  278. /**
  279. * do_lo_send_write - helper for writing data to a loop device
  280. *
  281. * This is the slow, transforming version for filesystems which do not
  282. * implement the address space operations write_begin and write_end. It
  283. * uses the write file operation which should be present on all writeable
  284. * filesystems.
  285. *
  286. * Using fops->write is slower than using aops->{prepare,commit}_write in the
  287. * transforming case because we need to double buffer the data as we cannot do
  288. * the transformations in place as we do not have direct access to the
  289. * destination pages of the backing file.
  290. */
  291. static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
  292. loff_t pos, struct page *page)
  293. {
  294. int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
  295. bvec->bv_offset, bvec->bv_len, pos >> 9);
  296. if (likely(!ret))
  297. return __do_lo_send_write(lo->lo_backing_file,
  298. page_address(page), bvec->bv_len,
  299. pos);
  300. printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
  301. "length %i.\n", (unsigned long long)pos, bvec->bv_len);
  302. if (ret > 0)
  303. ret = -EIO;
  304. return ret;
  305. }
  306. static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
  307. {
  308. int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
  309. struct page *page);
  310. struct bio_vec *bvec;
  311. struct page *page = NULL;
  312. int i, ret = 0;
  313. do_lo_send = do_lo_send_aops;
  314. if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
  315. do_lo_send = do_lo_send_direct_write;
  316. if (lo->transfer != transfer_none) {
  317. page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
  318. if (unlikely(!page))
  319. goto fail;
  320. kmap(page);
  321. do_lo_send = do_lo_send_write;
  322. }
  323. }
  324. bio_for_each_segment(bvec, bio, i) {
  325. ret = do_lo_send(lo, bvec, pos, page);
  326. if (ret < 0)
  327. break;
  328. pos += bvec->bv_len;
  329. }
  330. if (page) {
  331. kunmap(page);
  332. __free_page(page);
  333. }
  334. out:
  335. return ret;
  336. fail:
  337. printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
  338. ret = -ENOMEM;
  339. goto out;
  340. }
  341. struct lo_read_data {
  342. struct loop_device *lo;
  343. struct page *page;
  344. unsigned offset;
  345. int bsize;
  346. };
  347. static int
  348. lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  349. struct splice_desc *sd)
  350. {
  351. struct lo_read_data *p = sd->u.data;
  352. struct loop_device *lo = p->lo;
  353. struct page *page = buf->page;
  354. sector_t IV;
  355. int size, ret;
  356. ret = buf->ops->confirm(pipe, buf);
  357. if (unlikely(ret))
  358. return ret;
  359. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  360. (buf->offset >> 9);
  361. size = sd->len;
  362. if (size > p->bsize)
  363. size = p->bsize;
  364. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  365. printk(KERN_ERR "loop: transfer error block %ld\n",
  366. page->index);
  367. size = -EINVAL;
  368. }
  369. flush_dcache_page(p->page);
  370. if (size > 0)
  371. p->offset += size;
  372. return size;
  373. }
  374. static int
  375. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  376. {
  377. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  378. }
  379. static int
  380. do_lo_receive(struct loop_device *lo,
  381. struct bio_vec *bvec, int bsize, loff_t pos)
  382. {
  383. struct lo_read_data cookie;
  384. struct splice_desc sd;
  385. struct file *file;
  386. long retval;
  387. cookie.lo = lo;
  388. cookie.page = bvec->bv_page;
  389. cookie.offset = bvec->bv_offset;
  390. cookie.bsize = bsize;
  391. sd.len = 0;
  392. sd.total_len = bvec->bv_len;
  393. sd.flags = 0;
  394. sd.pos = pos;
  395. sd.u.data = &cookie;
  396. file = lo->lo_backing_file;
  397. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  398. if (retval < 0)
  399. return retval;
  400. return 0;
  401. }
  402. static int
  403. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  404. {
  405. struct bio_vec *bvec;
  406. int i, ret = 0;
  407. bio_for_each_segment(bvec, bio, i) {
  408. ret = do_lo_receive(lo, bvec, bsize, pos);
  409. if (ret < 0)
  410. break;
  411. pos += bvec->bv_len;
  412. }
  413. return ret;
  414. }
  415. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  416. {
  417. loff_t pos;
  418. int ret;
  419. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  420. if (bio_rw(bio) == WRITE) {
  421. bool barrier = bio_rw_flagged(bio, BIO_RW_BARRIER);
  422. struct file *file = lo->lo_backing_file;
  423. if (barrier) {
  424. if (unlikely(!file->f_op->fsync)) {
  425. ret = -EOPNOTSUPP;
  426. goto out;
  427. }
  428. ret = vfs_fsync(file, 0);
  429. if (unlikely(ret)) {
  430. ret = -EIO;
  431. goto out;
  432. }
  433. }
  434. ret = lo_send(lo, bio, pos);
  435. if (barrier && !ret) {
  436. ret = vfs_fsync(file, 0);
  437. if (unlikely(ret))
  438. ret = -EIO;
  439. }
  440. } else
  441. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  442. out:
  443. return ret;
  444. }
  445. /*
  446. * Add bio to back of pending list
  447. */
  448. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  449. {
  450. bio_list_add(&lo->lo_bio_list, bio);
  451. }
  452. /*
  453. * Grab first pending buffer
  454. */
  455. static struct bio *loop_get_bio(struct loop_device *lo)
  456. {
  457. return bio_list_pop(&lo->lo_bio_list);
  458. }
  459. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  460. {
  461. struct loop_device *lo = q->queuedata;
  462. int rw = bio_rw(old_bio);
  463. if (rw == READA)
  464. rw = READ;
  465. BUG_ON(!lo || (rw != READ && rw != WRITE));
  466. spin_lock_irq(&lo->lo_lock);
  467. if (lo->lo_state != Lo_bound)
  468. goto out;
  469. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  470. goto out;
  471. loop_add_bio(lo, old_bio);
  472. wake_up(&lo->lo_event);
  473. spin_unlock_irq(&lo->lo_lock);
  474. return 0;
  475. out:
  476. spin_unlock_irq(&lo->lo_lock);
  477. bio_io_error(old_bio);
  478. return 0;
  479. }
  480. /*
  481. * kick off io on the underlying address space
  482. */
  483. static void loop_unplug(struct request_queue *q)
  484. {
  485. struct loop_device *lo = q->queuedata;
  486. queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
  487. blk_run_address_space(lo->lo_backing_file->f_mapping);
  488. }
  489. struct switch_request {
  490. struct file *file;
  491. struct completion wait;
  492. };
  493. static void do_loop_switch(struct loop_device *, struct switch_request *);
  494. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  495. {
  496. if (unlikely(!bio->bi_bdev)) {
  497. do_loop_switch(lo, bio->bi_private);
  498. bio_put(bio);
  499. } else {
  500. int ret = do_bio_filebacked(lo, bio);
  501. bio_endio(bio, ret);
  502. }
  503. }
  504. /*
  505. * worker thread that handles reads/writes to file backed loop devices,
  506. * to avoid blocking in our make_request_fn. it also does loop decrypting
  507. * on reads for block backed loop, as that is too heavy to do from
  508. * b_end_io context where irqs may be disabled.
  509. *
  510. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  511. * calling kthread_stop(). Therefore once kthread_should_stop() is
  512. * true, make_request will not place any more requests. Therefore
  513. * once kthread_should_stop() is true and lo_bio is NULL, we are
  514. * done with the loop.
  515. */
  516. static int loop_thread(void *data)
  517. {
  518. struct loop_device *lo = data;
  519. struct bio *bio;
  520. set_user_nice(current, -20);
  521. while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
  522. wait_event_interruptible(lo->lo_event,
  523. !bio_list_empty(&lo->lo_bio_list) ||
  524. kthread_should_stop());
  525. if (bio_list_empty(&lo->lo_bio_list))
  526. continue;
  527. spin_lock_irq(&lo->lo_lock);
  528. bio = loop_get_bio(lo);
  529. spin_unlock_irq(&lo->lo_lock);
  530. BUG_ON(!bio);
  531. loop_handle_bio(lo, bio);
  532. }
  533. return 0;
  534. }
  535. /*
  536. * loop_switch performs the hard work of switching a backing store.
  537. * First it needs to flush existing IO, it does this by sending a magic
  538. * BIO down the pipe. The completion of this BIO does the actual switch.
  539. */
  540. static int loop_switch(struct loop_device *lo, struct file *file)
  541. {
  542. struct switch_request w;
  543. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  544. if (!bio)
  545. return -ENOMEM;
  546. init_completion(&w.wait);
  547. w.file = file;
  548. bio->bi_private = &w;
  549. bio->bi_bdev = NULL;
  550. loop_make_request(lo->lo_queue, bio);
  551. wait_for_completion(&w.wait);
  552. return 0;
  553. }
  554. /*
  555. * Helper to flush the IOs in loop, but keeping loop thread running
  556. */
  557. static int loop_flush(struct loop_device *lo)
  558. {
  559. /* loop not yet configured, no running thread, nothing to flush */
  560. if (!lo->lo_thread)
  561. return 0;
  562. return loop_switch(lo, NULL);
  563. }
  564. /*
  565. * Do the actual switch; called from the BIO completion routine
  566. */
  567. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  568. {
  569. struct file *file = p->file;
  570. struct file *old_file = lo->lo_backing_file;
  571. struct address_space *mapping;
  572. /* if no new file, only flush of queued bios requested */
  573. if (!file)
  574. goto out;
  575. mapping = file->f_mapping;
  576. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  577. lo->lo_backing_file = file;
  578. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  579. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  580. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  581. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  582. out:
  583. complete(&p->wait);
  584. }
  585. /*
  586. * loop_change_fd switched the backing store of a loopback device to
  587. * a new file. This is useful for operating system installers to free up
  588. * the original file and in High Availability environments to switch to
  589. * an alternative location for the content in case of server meltdown.
  590. * This can only work if the loop device is used read-only, and if the
  591. * new backing store is the same size and type as the old backing store.
  592. */
  593. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  594. unsigned int arg)
  595. {
  596. struct file *file, *old_file;
  597. struct inode *inode;
  598. int error;
  599. error = -ENXIO;
  600. if (lo->lo_state != Lo_bound)
  601. goto out;
  602. /* the loop device has to be read-only */
  603. error = -EINVAL;
  604. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  605. goto out;
  606. error = -EBADF;
  607. file = fget(arg);
  608. if (!file)
  609. goto out;
  610. inode = file->f_mapping->host;
  611. old_file = lo->lo_backing_file;
  612. error = -EINVAL;
  613. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  614. goto out_putf;
  615. /* size of the new backing store needs to be the same */
  616. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  617. goto out_putf;
  618. /* and ... switch */
  619. error = loop_switch(lo, file);
  620. if (error)
  621. goto out_putf;
  622. fput(old_file);
  623. if (max_part > 0)
  624. ioctl_by_bdev(bdev, BLKRRPART, 0);
  625. return 0;
  626. out_putf:
  627. fput(file);
  628. out:
  629. return error;
  630. }
  631. static inline int is_loop_device(struct file *file)
  632. {
  633. struct inode *i = file->f_mapping->host;
  634. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  635. }
  636. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  637. struct block_device *bdev, unsigned int arg)
  638. {
  639. struct file *file, *f;
  640. struct inode *inode;
  641. struct address_space *mapping;
  642. unsigned lo_blocksize;
  643. int lo_flags = 0;
  644. int error;
  645. loff_t size;
  646. /* This is safe, since we have a reference from open(). */
  647. __module_get(THIS_MODULE);
  648. error = -EBADF;
  649. file = fget(arg);
  650. if (!file)
  651. goto out;
  652. error = -EBUSY;
  653. if (lo->lo_state != Lo_unbound)
  654. goto out_putf;
  655. /* Avoid recursion */
  656. f = file;
  657. while (is_loop_device(f)) {
  658. struct loop_device *l;
  659. if (f->f_mapping->host->i_bdev == bdev)
  660. goto out_putf;
  661. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  662. if (l->lo_state == Lo_unbound) {
  663. error = -EINVAL;
  664. goto out_putf;
  665. }
  666. f = l->lo_backing_file;
  667. }
  668. mapping = file->f_mapping;
  669. inode = mapping->host;
  670. if (!(file->f_mode & FMODE_WRITE))
  671. lo_flags |= LO_FLAGS_READ_ONLY;
  672. error = -EINVAL;
  673. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  674. const struct address_space_operations *aops = mapping->a_ops;
  675. if (aops->write_begin)
  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 (!(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. bio_list_init(&lo->lo_bio_list);
  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. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  711. blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN, NULL);
  712. set_capacity(lo->lo_disk, size);
  713. bd_set_size(bdev, size << 9);
  714. /* let user-space know about the new size */
  715. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  716. set_blocksize(bdev, lo_blocksize);
  717. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  718. lo->lo_number);
  719. if (IS_ERR(lo->lo_thread)) {
  720. error = PTR_ERR(lo->lo_thread);
  721. goto out_clr;
  722. }
  723. lo->lo_state = Lo_bound;
  724. wake_up_process(lo->lo_thread);
  725. if (max_part > 0)
  726. ioctl_by_bdev(bdev, BLKRRPART, 0);
  727. return 0;
  728. out_clr:
  729. lo->lo_thread = NULL;
  730. lo->lo_device = NULL;
  731. lo->lo_backing_file = NULL;
  732. lo->lo_flags = 0;
  733. set_capacity(lo->lo_disk, 0);
  734. invalidate_bdev(bdev);
  735. bd_set_size(bdev, 0);
  736. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  737. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  738. lo->lo_state = Lo_unbound;
  739. out_putf:
  740. fput(file);
  741. out:
  742. /* This is safe: open() is still holding a reference. */
  743. module_put(THIS_MODULE);
  744. return error;
  745. }
  746. static int
  747. loop_release_xfer(struct loop_device *lo)
  748. {
  749. int err = 0;
  750. struct loop_func_table *xfer = lo->lo_encryption;
  751. if (xfer) {
  752. if (xfer->release)
  753. err = xfer->release(lo);
  754. lo->transfer = NULL;
  755. lo->lo_encryption = NULL;
  756. module_put(xfer->owner);
  757. }
  758. return err;
  759. }
  760. static int
  761. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  762. const struct loop_info64 *i)
  763. {
  764. int err = 0;
  765. if (xfer) {
  766. struct module *owner = xfer->owner;
  767. if (!try_module_get(owner))
  768. return -EINVAL;
  769. if (xfer->init)
  770. err = xfer->init(lo, i);
  771. if (err)
  772. module_put(owner);
  773. else
  774. lo->lo_encryption = xfer;
  775. }
  776. return err;
  777. }
  778. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  779. {
  780. struct file *filp = lo->lo_backing_file;
  781. gfp_t gfp = lo->old_gfp_mask;
  782. if (lo->lo_state != Lo_bound)
  783. return -ENXIO;
  784. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  785. return -EBUSY;
  786. if (filp == NULL)
  787. return -EINVAL;
  788. spin_lock_irq(&lo->lo_lock);
  789. lo->lo_state = Lo_rundown;
  790. spin_unlock_irq(&lo->lo_lock);
  791. kthread_stop(lo->lo_thread);
  792. lo->lo_queue->unplug_fn = NULL;
  793. lo->lo_backing_file = NULL;
  794. loop_release_xfer(lo);
  795. lo->transfer = NULL;
  796. lo->ioctl = NULL;
  797. lo->lo_device = NULL;
  798. lo->lo_encryption = NULL;
  799. lo->lo_offset = 0;
  800. lo->lo_sizelimit = 0;
  801. lo->lo_encrypt_key_size = 0;
  802. lo->lo_flags = 0;
  803. lo->lo_thread = NULL;
  804. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  805. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  806. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  807. if (bdev)
  808. invalidate_bdev(bdev);
  809. set_capacity(lo->lo_disk, 0);
  810. if (bdev) {
  811. bd_set_size(bdev, 0);
  812. /* let user-space know about this change */
  813. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  814. }
  815. mapping_set_gfp_mask(filp->f_mapping, gfp);
  816. lo->lo_state = Lo_unbound;
  817. /* This is safe: open() is still holding a reference. */
  818. module_put(THIS_MODULE);
  819. if (max_part > 0 && bdev)
  820. ioctl_by_bdev(bdev, BLKRRPART, 0);
  821. mutex_unlock(&lo->lo_ctl_mutex);
  822. /*
  823. * Need not hold lo_ctl_mutex to fput backing file.
  824. * Calling fput holding lo_ctl_mutex triggers a circular
  825. * lock dependency possibility warning as fput can take
  826. * bd_mutex which is usually taken before lo_ctl_mutex.
  827. */
  828. fput(filp);
  829. return 0;
  830. }
  831. static int
  832. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  833. {
  834. int err;
  835. struct loop_func_table *xfer;
  836. uid_t uid = current_uid();
  837. if (lo->lo_encrypt_key_size &&
  838. lo->lo_key_owner != uid &&
  839. !capable(CAP_SYS_ADMIN))
  840. return -EPERM;
  841. if (lo->lo_state != Lo_bound)
  842. return -ENXIO;
  843. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  844. return -EINVAL;
  845. err = loop_release_xfer(lo);
  846. if (err)
  847. return err;
  848. if (info->lo_encrypt_type) {
  849. unsigned int type = info->lo_encrypt_type;
  850. if (type >= MAX_LO_CRYPT)
  851. return -EINVAL;
  852. xfer = xfer_funcs[type];
  853. if (xfer == NULL)
  854. return -EINVAL;
  855. } else
  856. xfer = NULL;
  857. err = loop_init_xfer(lo, xfer, info);
  858. if (err)
  859. return err;
  860. if (lo->lo_offset != info->lo_offset ||
  861. lo->lo_sizelimit != info->lo_sizelimit) {
  862. lo->lo_offset = info->lo_offset;
  863. lo->lo_sizelimit = info->lo_sizelimit;
  864. if (figure_loop_size(lo))
  865. return -EFBIG;
  866. }
  867. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  868. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  869. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  870. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  871. if (!xfer)
  872. xfer = &none_funcs;
  873. lo->transfer = xfer->transfer;
  874. lo->ioctl = xfer->ioctl;
  875. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  876. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  877. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  878. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  879. lo->lo_init[0] = info->lo_init[0];
  880. lo->lo_init[1] = info->lo_init[1];
  881. if (info->lo_encrypt_key_size) {
  882. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  883. info->lo_encrypt_key_size);
  884. lo->lo_key_owner = uid;
  885. }
  886. return 0;
  887. }
  888. static int
  889. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  890. {
  891. struct file *file = lo->lo_backing_file;
  892. struct kstat stat;
  893. int error;
  894. if (lo->lo_state != Lo_bound)
  895. return -ENXIO;
  896. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  897. if (error)
  898. return error;
  899. memset(info, 0, sizeof(*info));
  900. info->lo_number = lo->lo_number;
  901. info->lo_device = huge_encode_dev(stat.dev);
  902. info->lo_inode = stat.ino;
  903. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  904. info->lo_offset = lo->lo_offset;
  905. info->lo_sizelimit = lo->lo_sizelimit;
  906. info->lo_flags = lo->lo_flags;
  907. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  908. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  909. info->lo_encrypt_type =
  910. lo->lo_encryption ? lo->lo_encryption->number : 0;
  911. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  912. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  913. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  914. lo->lo_encrypt_key_size);
  915. }
  916. return 0;
  917. }
  918. static void
  919. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  920. {
  921. memset(info64, 0, sizeof(*info64));
  922. info64->lo_number = info->lo_number;
  923. info64->lo_device = info->lo_device;
  924. info64->lo_inode = info->lo_inode;
  925. info64->lo_rdevice = info->lo_rdevice;
  926. info64->lo_offset = info->lo_offset;
  927. info64->lo_sizelimit = 0;
  928. info64->lo_encrypt_type = info->lo_encrypt_type;
  929. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  930. info64->lo_flags = info->lo_flags;
  931. info64->lo_init[0] = info->lo_init[0];
  932. info64->lo_init[1] = info->lo_init[1];
  933. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  934. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  935. else
  936. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  937. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  938. }
  939. static int
  940. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  941. {
  942. memset(info, 0, sizeof(*info));
  943. info->lo_number = info64->lo_number;
  944. info->lo_device = info64->lo_device;
  945. info->lo_inode = info64->lo_inode;
  946. info->lo_rdevice = info64->lo_rdevice;
  947. info->lo_offset = info64->lo_offset;
  948. info->lo_encrypt_type = info64->lo_encrypt_type;
  949. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  950. info->lo_flags = info64->lo_flags;
  951. info->lo_init[0] = info64->lo_init[0];
  952. info->lo_init[1] = info64->lo_init[1];
  953. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  954. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  955. else
  956. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  957. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  958. /* error in case values were truncated */
  959. if (info->lo_device != info64->lo_device ||
  960. info->lo_rdevice != info64->lo_rdevice ||
  961. info->lo_inode != info64->lo_inode ||
  962. info->lo_offset != info64->lo_offset)
  963. return -EOVERFLOW;
  964. return 0;
  965. }
  966. static int
  967. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  968. {
  969. struct loop_info info;
  970. struct loop_info64 info64;
  971. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  972. return -EFAULT;
  973. loop_info64_from_old(&info, &info64);
  974. return loop_set_status(lo, &info64);
  975. }
  976. static int
  977. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  978. {
  979. struct loop_info64 info64;
  980. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  981. return -EFAULT;
  982. return loop_set_status(lo, &info64);
  983. }
  984. static int
  985. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  986. struct loop_info info;
  987. struct loop_info64 info64;
  988. int err = 0;
  989. if (!arg)
  990. err = -EINVAL;
  991. if (!err)
  992. err = loop_get_status(lo, &info64);
  993. if (!err)
  994. err = loop_info64_to_old(&info64, &info);
  995. if (!err && copy_to_user(arg, &info, sizeof(info)))
  996. err = -EFAULT;
  997. return err;
  998. }
  999. static int
  1000. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1001. struct loop_info64 info64;
  1002. int err = 0;
  1003. if (!arg)
  1004. err = -EINVAL;
  1005. if (!err)
  1006. err = loop_get_status(lo, &info64);
  1007. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1008. err = -EFAULT;
  1009. return err;
  1010. }
  1011. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1012. {
  1013. int err;
  1014. sector_t sec;
  1015. loff_t sz;
  1016. err = -ENXIO;
  1017. if (unlikely(lo->lo_state != Lo_bound))
  1018. goto out;
  1019. err = figure_loop_size(lo);
  1020. if (unlikely(err))
  1021. goto out;
  1022. sec = get_capacity(lo->lo_disk);
  1023. /* the width of sector_t may be narrow for bit-shift */
  1024. sz = sec;
  1025. sz <<= 9;
  1026. mutex_lock(&bdev->bd_mutex);
  1027. bd_set_size(bdev, sz);
  1028. /* let user-space know about the new size */
  1029. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  1030. mutex_unlock(&bdev->bd_mutex);
  1031. out:
  1032. return err;
  1033. }
  1034. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1035. unsigned int cmd, unsigned long arg)
  1036. {
  1037. struct loop_device *lo = bdev->bd_disk->private_data;
  1038. int err;
  1039. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1040. switch (cmd) {
  1041. case LOOP_SET_FD:
  1042. err = loop_set_fd(lo, mode, bdev, arg);
  1043. break;
  1044. case LOOP_CHANGE_FD:
  1045. err = loop_change_fd(lo, bdev, arg);
  1046. break;
  1047. case LOOP_CLR_FD:
  1048. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1049. err = loop_clr_fd(lo, bdev);
  1050. if (!err)
  1051. goto out_unlocked;
  1052. break;
  1053. case LOOP_SET_STATUS:
  1054. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1055. break;
  1056. case LOOP_GET_STATUS:
  1057. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1058. break;
  1059. case LOOP_SET_STATUS64:
  1060. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1061. break;
  1062. case LOOP_GET_STATUS64:
  1063. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1064. break;
  1065. case LOOP_SET_CAPACITY:
  1066. err = -EPERM;
  1067. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1068. err = loop_set_capacity(lo, bdev);
  1069. break;
  1070. default:
  1071. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1072. }
  1073. mutex_unlock(&lo->lo_ctl_mutex);
  1074. out_unlocked:
  1075. return err;
  1076. }
  1077. #ifdef CONFIG_COMPAT
  1078. struct compat_loop_info {
  1079. compat_int_t lo_number; /* ioctl r/o */
  1080. compat_dev_t lo_device; /* ioctl r/o */
  1081. compat_ulong_t lo_inode; /* ioctl r/o */
  1082. compat_dev_t lo_rdevice; /* ioctl r/o */
  1083. compat_int_t lo_offset;
  1084. compat_int_t lo_encrypt_type;
  1085. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1086. compat_int_t lo_flags; /* ioctl r/o */
  1087. char lo_name[LO_NAME_SIZE];
  1088. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1089. compat_ulong_t lo_init[2];
  1090. char reserved[4];
  1091. };
  1092. /*
  1093. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1094. * - noinlined to reduce stack space usage in main part of driver
  1095. */
  1096. static noinline int
  1097. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1098. struct loop_info64 *info64)
  1099. {
  1100. struct compat_loop_info info;
  1101. if (copy_from_user(&info, arg, sizeof(info)))
  1102. return -EFAULT;
  1103. memset(info64, 0, sizeof(*info64));
  1104. info64->lo_number = info.lo_number;
  1105. info64->lo_device = info.lo_device;
  1106. info64->lo_inode = info.lo_inode;
  1107. info64->lo_rdevice = info.lo_rdevice;
  1108. info64->lo_offset = info.lo_offset;
  1109. info64->lo_sizelimit = 0;
  1110. info64->lo_encrypt_type = info.lo_encrypt_type;
  1111. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1112. info64->lo_flags = info.lo_flags;
  1113. info64->lo_init[0] = info.lo_init[0];
  1114. info64->lo_init[1] = info.lo_init[1];
  1115. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1116. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1117. else
  1118. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1119. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1120. return 0;
  1121. }
  1122. /*
  1123. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1124. * - noinlined to reduce stack space usage in main part of driver
  1125. */
  1126. static noinline int
  1127. loop_info64_to_compat(const struct loop_info64 *info64,
  1128. struct compat_loop_info __user *arg)
  1129. {
  1130. struct compat_loop_info info;
  1131. memset(&info, 0, sizeof(info));
  1132. info.lo_number = info64->lo_number;
  1133. info.lo_device = info64->lo_device;
  1134. info.lo_inode = info64->lo_inode;
  1135. info.lo_rdevice = info64->lo_rdevice;
  1136. info.lo_offset = info64->lo_offset;
  1137. info.lo_encrypt_type = info64->lo_encrypt_type;
  1138. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1139. info.lo_flags = info64->lo_flags;
  1140. info.lo_init[0] = info64->lo_init[0];
  1141. info.lo_init[1] = info64->lo_init[1];
  1142. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1143. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1144. else
  1145. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1146. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1147. /* error in case values were truncated */
  1148. if (info.lo_device != info64->lo_device ||
  1149. info.lo_rdevice != info64->lo_rdevice ||
  1150. info.lo_inode != info64->lo_inode ||
  1151. info.lo_offset != info64->lo_offset ||
  1152. info.lo_init[0] != info64->lo_init[0] ||
  1153. info.lo_init[1] != info64->lo_init[1])
  1154. return -EOVERFLOW;
  1155. if (copy_to_user(arg, &info, sizeof(info)))
  1156. return -EFAULT;
  1157. return 0;
  1158. }
  1159. static int
  1160. loop_set_status_compat(struct loop_device *lo,
  1161. const struct compat_loop_info __user *arg)
  1162. {
  1163. struct loop_info64 info64;
  1164. int ret;
  1165. ret = loop_info64_from_compat(arg, &info64);
  1166. if (ret < 0)
  1167. return ret;
  1168. return loop_set_status(lo, &info64);
  1169. }
  1170. static int
  1171. loop_get_status_compat(struct loop_device *lo,
  1172. struct compat_loop_info __user *arg)
  1173. {
  1174. struct loop_info64 info64;
  1175. int err = 0;
  1176. if (!arg)
  1177. err = -EINVAL;
  1178. if (!err)
  1179. err = loop_get_status(lo, &info64);
  1180. if (!err)
  1181. err = loop_info64_to_compat(&info64, arg);
  1182. return err;
  1183. }
  1184. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1185. unsigned int cmd, unsigned long arg)
  1186. {
  1187. struct loop_device *lo = bdev->bd_disk->private_data;
  1188. int err;
  1189. switch(cmd) {
  1190. case LOOP_SET_STATUS:
  1191. mutex_lock(&lo->lo_ctl_mutex);
  1192. err = loop_set_status_compat(
  1193. lo, (const struct compat_loop_info __user *) arg);
  1194. mutex_unlock(&lo->lo_ctl_mutex);
  1195. break;
  1196. case LOOP_GET_STATUS:
  1197. mutex_lock(&lo->lo_ctl_mutex);
  1198. err = loop_get_status_compat(
  1199. lo, (struct compat_loop_info __user *) arg);
  1200. mutex_unlock(&lo->lo_ctl_mutex);
  1201. break;
  1202. case LOOP_SET_CAPACITY:
  1203. case LOOP_CLR_FD:
  1204. case LOOP_GET_STATUS64:
  1205. case LOOP_SET_STATUS64:
  1206. arg = (unsigned long) compat_ptr(arg);
  1207. case LOOP_SET_FD:
  1208. case LOOP_CHANGE_FD:
  1209. err = lo_ioctl(bdev, mode, cmd, arg);
  1210. break;
  1211. default:
  1212. err = -ENOIOCTLCMD;
  1213. break;
  1214. }
  1215. return err;
  1216. }
  1217. #endif
  1218. static int lo_open(struct block_device *bdev, fmode_t mode)
  1219. {
  1220. struct loop_device *lo = bdev->bd_disk->private_data;
  1221. mutex_lock(&lo->lo_ctl_mutex);
  1222. lo->lo_refcnt++;
  1223. mutex_unlock(&lo->lo_ctl_mutex);
  1224. return 0;
  1225. }
  1226. static int lo_release(struct gendisk *disk, fmode_t mode)
  1227. {
  1228. struct loop_device *lo = disk->private_data;
  1229. int err;
  1230. mutex_lock(&lo->lo_ctl_mutex);
  1231. if (--lo->lo_refcnt)
  1232. goto out;
  1233. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1234. /*
  1235. * In autoclear mode, stop the loop thread
  1236. * and remove configuration after last close.
  1237. */
  1238. err = loop_clr_fd(lo, NULL);
  1239. if (!err)
  1240. goto out_unlocked;
  1241. } else {
  1242. /*
  1243. * Otherwise keep thread (if running) and config,
  1244. * but flush possible ongoing bios in thread.
  1245. */
  1246. loop_flush(lo);
  1247. }
  1248. out:
  1249. mutex_unlock(&lo->lo_ctl_mutex);
  1250. out_unlocked:
  1251. return 0;
  1252. }
  1253. static const struct block_device_operations lo_fops = {
  1254. .owner = THIS_MODULE,
  1255. .open = lo_open,
  1256. .release = lo_release,
  1257. .ioctl = lo_ioctl,
  1258. #ifdef CONFIG_COMPAT
  1259. .compat_ioctl = lo_compat_ioctl,
  1260. #endif
  1261. };
  1262. /*
  1263. * And now the modules code and kernel interface.
  1264. */
  1265. static int max_loop;
  1266. module_param(max_loop, int, 0);
  1267. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1268. module_param(max_part, int, 0);
  1269. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1272. int loop_register_transfer(struct loop_func_table *funcs)
  1273. {
  1274. unsigned int n = funcs->number;
  1275. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1276. return -EINVAL;
  1277. xfer_funcs[n] = funcs;
  1278. return 0;
  1279. }
  1280. int loop_unregister_transfer(int number)
  1281. {
  1282. unsigned int n = number;
  1283. struct loop_device *lo;
  1284. struct loop_func_table *xfer;
  1285. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1286. return -EINVAL;
  1287. xfer_funcs[n] = NULL;
  1288. list_for_each_entry(lo, &loop_devices, lo_list) {
  1289. mutex_lock(&lo->lo_ctl_mutex);
  1290. if (lo->lo_encryption == xfer)
  1291. loop_release_xfer(lo);
  1292. mutex_unlock(&lo->lo_ctl_mutex);
  1293. }
  1294. return 0;
  1295. }
  1296. EXPORT_SYMBOL(loop_register_transfer);
  1297. EXPORT_SYMBOL(loop_unregister_transfer);
  1298. static struct loop_device *loop_alloc(int i)
  1299. {
  1300. struct loop_device *lo;
  1301. struct gendisk *disk;
  1302. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1303. if (!lo)
  1304. goto out;
  1305. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1306. if (!lo->lo_queue)
  1307. goto out_free_dev;
  1308. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1309. if (!disk)
  1310. goto out_free_queue;
  1311. mutex_init(&lo->lo_ctl_mutex);
  1312. lo->lo_number = i;
  1313. lo->lo_thread = NULL;
  1314. init_waitqueue_head(&lo->lo_event);
  1315. spin_lock_init(&lo->lo_lock);
  1316. disk->major = LOOP_MAJOR;
  1317. disk->first_minor = i << part_shift;
  1318. disk->fops = &lo_fops;
  1319. disk->private_data = lo;
  1320. disk->queue = lo->lo_queue;
  1321. sprintf(disk->disk_name, "loop%d", i);
  1322. return lo;
  1323. out_free_queue:
  1324. blk_cleanup_queue(lo->lo_queue);
  1325. out_free_dev:
  1326. kfree(lo);
  1327. out:
  1328. return NULL;
  1329. }
  1330. static void loop_free(struct loop_device *lo)
  1331. {
  1332. blk_cleanup_queue(lo->lo_queue);
  1333. put_disk(lo->lo_disk);
  1334. list_del(&lo->lo_list);
  1335. kfree(lo);
  1336. }
  1337. static struct loop_device *loop_init_one(int i)
  1338. {
  1339. struct loop_device *lo;
  1340. list_for_each_entry(lo, &loop_devices, lo_list) {
  1341. if (lo->lo_number == i)
  1342. return lo;
  1343. }
  1344. lo = loop_alloc(i);
  1345. if (lo) {
  1346. add_disk(lo->lo_disk);
  1347. list_add_tail(&lo->lo_list, &loop_devices);
  1348. }
  1349. return lo;
  1350. }
  1351. static void loop_del_one(struct loop_device *lo)
  1352. {
  1353. del_gendisk(lo->lo_disk);
  1354. loop_free(lo);
  1355. }
  1356. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1357. {
  1358. struct loop_device *lo;
  1359. struct kobject *kobj;
  1360. mutex_lock(&loop_devices_mutex);
  1361. lo = loop_init_one(dev & MINORMASK);
  1362. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1363. mutex_unlock(&loop_devices_mutex);
  1364. *part = 0;
  1365. return kobj;
  1366. }
  1367. static int __init loop_init(void)
  1368. {
  1369. int i, nr;
  1370. unsigned long range;
  1371. struct loop_device *lo, *next;
  1372. /*
  1373. * loop module now has a feature to instantiate underlying device
  1374. * structure on-demand, provided that there is an access dev node.
  1375. * However, this will not work well with user space tool that doesn't
  1376. * know about such "feature". In order to not break any existing
  1377. * tool, we do the following:
  1378. *
  1379. * (1) if max_loop is specified, create that many upfront, and this
  1380. * also becomes a hard limit.
  1381. * (2) if max_loop is not specified, create 8 loop device on module
  1382. * load, user can further extend loop device by create dev node
  1383. * themselves and have kernel automatically instantiate actual
  1384. * device on-demand.
  1385. */
  1386. part_shift = 0;
  1387. if (max_part > 0)
  1388. part_shift = fls(max_part);
  1389. if (max_loop > 1UL << (MINORBITS - part_shift))
  1390. return -EINVAL;
  1391. if (max_loop) {
  1392. nr = max_loop;
  1393. range = max_loop;
  1394. } else {
  1395. nr = 8;
  1396. range = 1UL << (MINORBITS - part_shift);
  1397. }
  1398. if (register_blkdev(LOOP_MAJOR, "loop"))
  1399. return -EIO;
  1400. for (i = 0; i < nr; i++) {
  1401. lo = loop_alloc(i);
  1402. if (!lo)
  1403. goto Enomem;
  1404. list_add_tail(&lo->lo_list, &loop_devices);
  1405. }
  1406. /* point of no return */
  1407. list_for_each_entry(lo, &loop_devices, lo_list)
  1408. add_disk(lo->lo_disk);
  1409. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1410. THIS_MODULE, loop_probe, NULL, NULL);
  1411. printk(KERN_INFO "loop: module loaded\n");
  1412. return 0;
  1413. Enomem:
  1414. printk(KERN_INFO "loop: out of memory\n");
  1415. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1416. loop_free(lo);
  1417. unregister_blkdev(LOOP_MAJOR, "loop");
  1418. return -ENOMEM;
  1419. }
  1420. static void __exit loop_exit(void)
  1421. {
  1422. unsigned long range;
  1423. struct loop_device *lo, *next;
  1424. range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
  1425. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1426. loop_del_one(lo);
  1427. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1428. unregister_blkdev(LOOP_MAJOR, "loop");
  1429. }
  1430. module_init(loop_init);
  1431. module_exit(loop_exit);
  1432. #ifndef MODULE
  1433. static int __init max_loop_setup(char *str)
  1434. {
  1435. max_loop = simple_strtol(str, NULL, 0);
  1436. return 1;
  1437. }
  1438. __setup("max_loop=", max_loop_setup);
  1439. #endif