loop.c 41 KB

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