loop.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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 <linux/sysfs.h>
  77. #include <asm/uaccess.h>
  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(loop_buf, KM_USER1);
  97. kunmap_atomic(raw_buf, KM_USER0);
  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(loop_buf, KM_USER1);
  122. kunmap_atomic(raw_buf, KM_USER0);
  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;
  358. IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
  359. (buf->offset >> 9);
  360. size = sd->len;
  361. if (size > p->bsize)
  362. size = p->bsize;
  363. if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
  364. printk(KERN_ERR "loop: transfer error block %ld\n",
  365. page->index);
  366. size = -EINVAL;
  367. }
  368. flush_dcache_page(p->page);
  369. if (size > 0)
  370. p->offset += size;
  371. return size;
  372. }
  373. static int
  374. lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
  375. {
  376. return __splice_from_pipe(pipe, sd, lo_splice_actor);
  377. }
  378. static int
  379. do_lo_receive(struct loop_device *lo,
  380. struct bio_vec *bvec, int bsize, loff_t pos)
  381. {
  382. struct lo_read_data cookie;
  383. struct splice_desc sd;
  384. struct file *file;
  385. long retval;
  386. cookie.lo = lo;
  387. cookie.page = bvec->bv_page;
  388. cookie.offset = bvec->bv_offset;
  389. cookie.bsize = bsize;
  390. sd.len = 0;
  391. sd.total_len = bvec->bv_len;
  392. sd.flags = 0;
  393. sd.pos = pos;
  394. sd.u.data = &cookie;
  395. file = lo->lo_backing_file;
  396. retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
  397. if (retval < 0)
  398. return retval;
  399. return 0;
  400. }
  401. static int
  402. lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
  403. {
  404. struct bio_vec *bvec;
  405. int i, ret = 0;
  406. bio_for_each_segment(bvec, bio, i) {
  407. ret = do_lo_receive(lo, bvec, bsize, pos);
  408. if (ret < 0)
  409. break;
  410. pos += bvec->bv_len;
  411. }
  412. return ret;
  413. }
  414. static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
  415. {
  416. loff_t pos;
  417. int ret;
  418. pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
  419. if (bio_rw(bio) == WRITE) {
  420. struct file *file = lo->lo_backing_file;
  421. if (bio->bi_rw & REQ_FLUSH) {
  422. ret = vfs_fsync(file, 0);
  423. if (unlikely(ret && ret != -EINVAL)) {
  424. ret = -EIO;
  425. goto out;
  426. }
  427. }
  428. ret = lo_send(lo, bio, pos);
  429. if ((bio->bi_rw & REQ_FUA) && !ret) {
  430. ret = vfs_fsync(file, 0);
  431. if (unlikely(ret && ret != -EINVAL))
  432. ret = -EIO;
  433. }
  434. } else
  435. ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
  436. out:
  437. return ret;
  438. }
  439. /*
  440. * Add bio to back of pending list
  441. */
  442. static void loop_add_bio(struct loop_device *lo, struct bio *bio)
  443. {
  444. bio_list_add(&lo->lo_bio_list, bio);
  445. }
  446. /*
  447. * Grab first pending buffer
  448. */
  449. static struct bio *loop_get_bio(struct loop_device *lo)
  450. {
  451. return bio_list_pop(&lo->lo_bio_list);
  452. }
  453. static int loop_make_request(struct request_queue *q, struct bio *old_bio)
  454. {
  455. struct loop_device *lo = q->queuedata;
  456. int rw = bio_rw(old_bio);
  457. if (rw == READA)
  458. rw = READ;
  459. BUG_ON(!lo || (rw != READ && rw != WRITE));
  460. spin_lock_irq(&lo->lo_lock);
  461. if (lo->lo_state != Lo_bound)
  462. goto out;
  463. if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
  464. goto out;
  465. loop_add_bio(lo, old_bio);
  466. wake_up(&lo->lo_event);
  467. spin_unlock_irq(&lo->lo_lock);
  468. return 0;
  469. out:
  470. spin_unlock_irq(&lo->lo_lock);
  471. bio_io_error(old_bio);
  472. return 0;
  473. }
  474. struct switch_request {
  475. struct file *file;
  476. struct completion wait;
  477. };
  478. static void do_loop_switch(struct loop_device *, struct switch_request *);
  479. static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
  480. {
  481. if (unlikely(!bio->bi_bdev)) {
  482. do_loop_switch(lo, bio->bi_private);
  483. bio_put(bio);
  484. } else {
  485. int ret = do_bio_filebacked(lo, bio);
  486. bio_endio(bio, ret);
  487. }
  488. }
  489. /*
  490. * worker thread that handles reads/writes to file backed loop devices,
  491. * to avoid blocking in our make_request_fn. it also does loop decrypting
  492. * on reads for block backed loop, as that is too heavy to do from
  493. * b_end_io context where irqs may be disabled.
  494. *
  495. * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
  496. * calling kthread_stop(). Therefore once kthread_should_stop() is
  497. * true, make_request will not place any more requests. Therefore
  498. * once kthread_should_stop() is true and lo_bio is NULL, we are
  499. * done with the loop.
  500. */
  501. static int loop_thread(void *data)
  502. {
  503. struct loop_device *lo = data;
  504. struct bio *bio;
  505. set_user_nice(current, -20);
  506. while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
  507. wait_event_interruptible(lo->lo_event,
  508. !bio_list_empty(&lo->lo_bio_list) ||
  509. kthread_should_stop());
  510. if (bio_list_empty(&lo->lo_bio_list))
  511. continue;
  512. spin_lock_irq(&lo->lo_lock);
  513. bio = loop_get_bio(lo);
  514. spin_unlock_irq(&lo->lo_lock);
  515. BUG_ON(!bio);
  516. loop_handle_bio(lo, bio);
  517. }
  518. return 0;
  519. }
  520. /*
  521. * loop_switch performs the hard work of switching a backing store.
  522. * First it needs to flush existing IO, it does this by sending a magic
  523. * BIO down the pipe. The completion of this BIO does the actual switch.
  524. */
  525. static int loop_switch(struct loop_device *lo, struct file *file)
  526. {
  527. struct switch_request w;
  528. struct bio *bio = bio_alloc(GFP_KERNEL, 0);
  529. if (!bio)
  530. return -ENOMEM;
  531. init_completion(&w.wait);
  532. w.file = file;
  533. bio->bi_private = &w;
  534. bio->bi_bdev = NULL;
  535. loop_make_request(lo->lo_queue, bio);
  536. wait_for_completion(&w.wait);
  537. return 0;
  538. }
  539. /*
  540. * Helper to flush the IOs in loop, but keeping loop thread running
  541. */
  542. static int loop_flush(struct loop_device *lo)
  543. {
  544. /* loop not yet configured, no running thread, nothing to flush */
  545. if (!lo->lo_thread)
  546. return 0;
  547. return loop_switch(lo, NULL);
  548. }
  549. /*
  550. * Do the actual switch; called from the BIO completion routine
  551. */
  552. static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
  553. {
  554. struct file *file = p->file;
  555. struct file *old_file = lo->lo_backing_file;
  556. struct address_space *mapping;
  557. /* if no new file, only flush of queued bios requested */
  558. if (!file)
  559. goto out;
  560. mapping = file->f_mapping;
  561. mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
  562. lo->lo_backing_file = file;
  563. lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
  564. mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
  565. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  566. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  567. out:
  568. complete(&p->wait);
  569. }
  570. /*
  571. * loop_change_fd switched the backing store of a loopback device to
  572. * a new file. This is useful for operating system installers to free up
  573. * the original file and in High Availability environments to switch to
  574. * an alternative location for the content in case of server meltdown.
  575. * This can only work if the loop device is used read-only, and if the
  576. * new backing store is the same size and type as the old backing store.
  577. */
  578. static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
  579. unsigned int arg)
  580. {
  581. struct file *file, *old_file;
  582. struct inode *inode;
  583. int error;
  584. error = -ENXIO;
  585. if (lo->lo_state != Lo_bound)
  586. goto out;
  587. /* the loop device has to be read-only */
  588. error = -EINVAL;
  589. if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
  590. goto out;
  591. error = -EBADF;
  592. file = fget(arg);
  593. if (!file)
  594. goto out;
  595. inode = file->f_mapping->host;
  596. old_file = lo->lo_backing_file;
  597. error = -EINVAL;
  598. if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
  599. goto out_putf;
  600. /* size of the new backing store needs to be the same */
  601. if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
  602. goto out_putf;
  603. /* and ... switch */
  604. error = loop_switch(lo, file);
  605. if (error)
  606. goto out_putf;
  607. fput(old_file);
  608. if (max_part > 0)
  609. ioctl_by_bdev(bdev, BLKRRPART, 0);
  610. return 0;
  611. out_putf:
  612. fput(file);
  613. out:
  614. return error;
  615. }
  616. static inline int is_loop_device(struct file *file)
  617. {
  618. struct inode *i = file->f_mapping->host;
  619. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  620. }
  621. /* loop sysfs attributes */
  622. static ssize_t loop_attr_show(struct device *dev, char *page,
  623. ssize_t (*callback)(struct loop_device *, char *))
  624. {
  625. struct loop_device *l, *lo = NULL;
  626. mutex_lock(&loop_devices_mutex);
  627. list_for_each_entry(l, &loop_devices, lo_list)
  628. if (disk_to_dev(l->lo_disk) == dev) {
  629. lo = l;
  630. break;
  631. }
  632. mutex_unlock(&loop_devices_mutex);
  633. return lo ? callback(lo, page) : -EIO;
  634. }
  635. #define LOOP_ATTR_RO(_name) \
  636. static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
  637. static ssize_t loop_attr_do_show_##_name(struct device *d, \
  638. struct device_attribute *attr, char *b) \
  639. { \
  640. return loop_attr_show(d, b, loop_attr_##_name##_show); \
  641. } \
  642. static struct device_attribute loop_attr_##_name = \
  643. __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
  644. static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
  645. {
  646. ssize_t ret;
  647. char *p = NULL;
  648. mutex_lock(&lo->lo_ctl_mutex);
  649. if (lo->lo_backing_file)
  650. p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
  651. mutex_unlock(&lo->lo_ctl_mutex);
  652. if (IS_ERR_OR_NULL(p))
  653. ret = PTR_ERR(p);
  654. else {
  655. ret = strlen(p);
  656. memmove(buf, p, ret);
  657. buf[ret++] = '\n';
  658. buf[ret] = 0;
  659. }
  660. return ret;
  661. }
  662. static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
  663. {
  664. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
  665. }
  666. static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
  667. {
  668. return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
  669. }
  670. static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
  671. {
  672. int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
  673. return sprintf(buf, "%s\n", autoclear ? "1" : "0");
  674. }
  675. LOOP_ATTR_RO(backing_file);
  676. LOOP_ATTR_RO(offset);
  677. LOOP_ATTR_RO(sizelimit);
  678. LOOP_ATTR_RO(autoclear);
  679. static struct attribute *loop_attrs[] = {
  680. &loop_attr_backing_file.attr,
  681. &loop_attr_offset.attr,
  682. &loop_attr_sizelimit.attr,
  683. &loop_attr_autoclear.attr,
  684. NULL,
  685. };
  686. static struct attribute_group loop_attribute_group = {
  687. .name = "loop",
  688. .attrs= loop_attrs,
  689. };
  690. static int loop_sysfs_init(struct loop_device *lo)
  691. {
  692. return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
  693. &loop_attribute_group);
  694. }
  695. static void loop_sysfs_exit(struct loop_device *lo)
  696. {
  697. sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
  698. &loop_attribute_group);
  699. }
  700. static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  701. struct block_device *bdev, unsigned int arg)
  702. {
  703. struct file *file, *f;
  704. struct inode *inode;
  705. struct address_space *mapping;
  706. unsigned lo_blocksize;
  707. int lo_flags = 0;
  708. int error;
  709. loff_t size;
  710. /* This is safe, since we have a reference from open(). */
  711. __module_get(THIS_MODULE);
  712. error = -EBADF;
  713. file = fget(arg);
  714. if (!file)
  715. goto out;
  716. error = -EBUSY;
  717. if (lo->lo_state != Lo_unbound)
  718. goto out_putf;
  719. /* Avoid recursion */
  720. f = file;
  721. while (is_loop_device(f)) {
  722. struct loop_device *l;
  723. if (f->f_mapping->host->i_bdev == bdev)
  724. goto out_putf;
  725. l = f->f_mapping->host->i_bdev->bd_disk->private_data;
  726. if (l->lo_state == Lo_unbound) {
  727. error = -EINVAL;
  728. goto out_putf;
  729. }
  730. f = l->lo_backing_file;
  731. }
  732. mapping = file->f_mapping;
  733. inode = mapping->host;
  734. if (!(file->f_mode & FMODE_WRITE))
  735. lo_flags |= LO_FLAGS_READ_ONLY;
  736. error = -EINVAL;
  737. if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  738. const struct address_space_operations *aops = mapping->a_ops;
  739. if (aops->write_begin)
  740. lo_flags |= LO_FLAGS_USE_AOPS;
  741. if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
  742. lo_flags |= LO_FLAGS_READ_ONLY;
  743. lo_blocksize = S_ISBLK(inode->i_mode) ?
  744. inode->i_bdev->bd_block_size : PAGE_SIZE;
  745. error = 0;
  746. } else {
  747. goto out_putf;
  748. }
  749. size = get_loop_size(lo, file);
  750. if ((loff_t)(sector_t)size != size) {
  751. error = -EFBIG;
  752. goto out_putf;
  753. }
  754. if (!(mode & FMODE_WRITE))
  755. lo_flags |= LO_FLAGS_READ_ONLY;
  756. set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
  757. lo->lo_blocksize = lo_blocksize;
  758. lo->lo_device = bdev;
  759. lo->lo_flags = lo_flags;
  760. lo->lo_backing_file = file;
  761. lo->transfer = transfer_none;
  762. lo->ioctl = NULL;
  763. lo->lo_sizelimit = 0;
  764. lo->old_gfp_mask = mapping_gfp_mask(mapping);
  765. mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
  766. bio_list_init(&lo->lo_bio_list);
  767. /*
  768. * set queue make_request_fn, and add limits based on lower level
  769. * device
  770. */
  771. blk_queue_make_request(lo->lo_queue, loop_make_request);
  772. lo->lo_queue->queuedata = lo;
  773. if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  774. blk_queue_flush(lo->lo_queue, REQ_FLUSH);
  775. set_capacity(lo->lo_disk, size);
  776. bd_set_size(bdev, size << 9);
  777. loop_sysfs_init(lo);
  778. /* let user-space know about the new size */
  779. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  780. set_blocksize(bdev, lo_blocksize);
  781. lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
  782. lo->lo_number);
  783. if (IS_ERR(lo->lo_thread)) {
  784. error = PTR_ERR(lo->lo_thread);
  785. goto out_clr;
  786. }
  787. lo->lo_state = Lo_bound;
  788. wake_up_process(lo->lo_thread);
  789. if (max_part > 0)
  790. ioctl_by_bdev(bdev, BLKRRPART, 0);
  791. return 0;
  792. out_clr:
  793. loop_sysfs_exit(lo);
  794. lo->lo_thread = NULL;
  795. lo->lo_device = NULL;
  796. lo->lo_backing_file = NULL;
  797. lo->lo_flags = 0;
  798. set_capacity(lo->lo_disk, 0);
  799. invalidate_bdev(bdev);
  800. bd_set_size(bdev, 0);
  801. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  802. mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
  803. lo->lo_state = Lo_unbound;
  804. out_putf:
  805. fput(file);
  806. out:
  807. /* This is safe: open() is still holding a reference. */
  808. module_put(THIS_MODULE);
  809. return error;
  810. }
  811. static int
  812. loop_release_xfer(struct loop_device *lo)
  813. {
  814. int err = 0;
  815. struct loop_func_table *xfer = lo->lo_encryption;
  816. if (xfer) {
  817. if (xfer->release)
  818. err = xfer->release(lo);
  819. lo->transfer = NULL;
  820. lo->lo_encryption = NULL;
  821. module_put(xfer->owner);
  822. }
  823. return err;
  824. }
  825. static int
  826. loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
  827. const struct loop_info64 *i)
  828. {
  829. int err = 0;
  830. if (xfer) {
  831. struct module *owner = xfer->owner;
  832. if (!try_module_get(owner))
  833. return -EINVAL;
  834. if (xfer->init)
  835. err = xfer->init(lo, i);
  836. if (err)
  837. module_put(owner);
  838. else
  839. lo->lo_encryption = xfer;
  840. }
  841. return err;
  842. }
  843. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  844. {
  845. struct file *filp = lo->lo_backing_file;
  846. gfp_t gfp = lo->old_gfp_mask;
  847. if (lo->lo_state != Lo_bound)
  848. return -ENXIO;
  849. if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
  850. return -EBUSY;
  851. if (filp == NULL)
  852. return -EINVAL;
  853. spin_lock_irq(&lo->lo_lock);
  854. lo->lo_state = Lo_rundown;
  855. spin_unlock_irq(&lo->lo_lock);
  856. kthread_stop(lo->lo_thread);
  857. lo->lo_backing_file = NULL;
  858. loop_release_xfer(lo);
  859. lo->transfer = NULL;
  860. lo->ioctl = NULL;
  861. lo->lo_device = NULL;
  862. lo->lo_encryption = NULL;
  863. lo->lo_offset = 0;
  864. lo->lo_sizelimit = 0;
  865. lo->lo_encrypt_key_size = 0;
  866. lo->lo_flags = 0;
  867. lo->lo_thread = NULL;
  868. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  869. memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  870. memset(lo->lo_file_name, 0, LO_NAME_SIZE);
  871. if (bdev)
  872. invalidate_bdev(bdev);
  873. set_capacity(lo->lo_disk, 0);
  874. loop_sysfs_exit(lo);
  875. if (bdev) {
  876. bd_set_size(bdev, 0);
  877. /* let user-space know about this change */
  878. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  879. }
  880. mapping_set_gfp_mask(filp->f_mapping, gfp);
  881. lo->lo_state = Lo_unbound;
  882. /* This is safe: open() is still holding a reference. */
  883. module_put(THIS_MODULE);
  884. if (max_part > 0 && bdev)
  885. ioctl_by_bdev(bdev, BLKRRPART, 0);
  886. mutex_unlock(&lo->lo_ctl_mutex);
  887. /*
  888. * Need not hold lo_ctl_mutex to fput backing file.
  889. * Calling fput holding lo_ctl_mutex triggers a circular
  890. * lock dependency possibility warning as fput can take
  891. * bd_mutex which is usually taken before lo_ctl_mutex.
  892. */
  893. fput(filp);
  894. return 0;
  895. }
  896. static int
  897. loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
  898. {
  899. int err;
  900. struct loop_func_table *xfer;
  901. uid_t uid = current_uid();
  902. if (lo->lo_encrypt_key_size &&
  903. lo->lo_key_owner != uid &&
  904. !capable(CAP_SYS_ADMIN))
  905. return -EPERM;
  906. if (lo->lo_state != Lo_bound)
  907. return -ENXIO;
  908. if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  909. return -EINVAL;
  910. err = loop_release_xfer(lo);
  911. if (err)
  912. return err;
  913. if (info->lo_encrypt_type) {
  914. unsigned int type = info->lo_encrypt_type;
  915. if (type >= MAX_LO_CRYPT)
  916. return -EINVAL;
  917. xfer = xfer_funcs[type];
  918. if (xfer == NULL)
  919. return -EINVAL;
  920. } else
  921. xfer = NULL;
  922. err = loop_init_xfer(lo, xfer, info);
  923. if (err)
  924. return err;
  925. if (lo->lo_offset != info->lo_offset ||
  926. lo->lo_sizelimit != info->lo_sizelimit) {
  927. lo->lo_offset = info->lo_offset;
  928. lo->lo_sizelimit = info->lo_sizelimit;
  929. if (figure_loop_size(lo))
  930. return -EFBIG;
  931. }
  932. memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
  933. memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  934. lo->lo_file_name[LO_NAME_SIZE-1] = 0;
  935. lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
  936. if (!xfer)
  937. xfer = &none_funcs;
  938. lo->transfer = xfer->transfer;
  939. lo->ioctl = xfer->ioctl;
  940. if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
  941. (info->lo_flags & LO_FLAGS_AUTOCLEAR))
  942. lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
  943. lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  944. lo->lo_init[0] = info->lo_init[0];
  945. lo->lo_init[1] = info->lo_init[1];
  946. if (info->lo_encrypt_key_size) {
  947. memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  948. info->lo_encrypt_key_size);
  949. lo->lo_key_owner = uid;
  950. }
  951. return 0;
  952. }
  953. static int
  954. loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  955. {
  956. struct file *file = lo->lo_backing_file;
  957. struct kstat stat;
  958. int error;
  959. if (lo->lo_state != Lo_bound)
  960. return -ENXIO;
  961. error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
  962. if (error)
  963. return error;
  964. memset(info, 0, sizeof(*info));
  965. info->lo_number = lo->lo_number;
  966. info->lo_device = huge_encode_dev(stat.dev);
  967. info->lo_inode = stat.ino;
  968. info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
  969. info->lo_offset = lo->lo_offset;
  970. info->lo_sizelimit = lo->lo_sizelimit;
  971. info->lo_flags = lo->lo_flags;
  972. memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
  973. memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  974. info->lo_encrypt_type =
  975. lo->lo_encryption ? lo->lo_encryption->number : 0;
  976. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  977. info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  978. memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  979. lo->lo_encrypt_key_size);
  980. }
  981. return 0;
  982. }
  983. static void
  984. loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  985. {
  986. memset(info64, 0, sizeof(*info64));
  987. info64->lo_number = info->lo_number;
  988. info64->lo_device = info->lo_device;
  989. info64->lo_inode = info->lo_inode;
  990. info64->lo_rdevice = info->lo_rdevice;
  991. info64->lo_offset = info->lo_offset;
  992. info64->lo_sizelimit = 0;
  993. info64->lo_encrypt_type = info->lo_encrypt_type;
  994. info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  995. info64->lo_flags = info->lo_flags;
  996. info64->lo_init[0] = info->lo_init[0];
  997. info64->lo_init[1] = info->lo_init[1];
  998. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  999. memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1000. else
  1001. memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1002. memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1003. }
  1004. static int
  1005. loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
  1006. {
  1007. memset(info, 0, sizeof(*info));
  1008. info->lo_number = info64->lo_number;
  1009. info->lo_device = info64->lo_device;
  1010. info->lo_inode = info64->lo_inode;
  1011. info->lo_rdevice = info64->lo_rdevice;
  1012. info->lo_offset = info64->lo_offset;
  1013. info->lo_encrypt_type = info64->lo_encrypt_type;
  1014. info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1015. info->lo_flags = info64->lo_flags;
  1016. info->lo_init[0] = info64->lo_init[0];
  1017. info->lo_init[1] = info64->lo_init[1];
  1018. if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1019. memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1020. else
  1021. memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1022. memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1023. /* error in case values were truncated */
  1024. if (info->lo_device != info64->lo_device ||
  1025. info->lo_rdevice != info64->lo_rdevice ||
  1026. info->lo_inode != info64->lo_inode ||
  1027. info->lo_offset != info64->lo_offset)
  1028. return -EOVERFLOW;
  1029. return 0;
  1030. }
  1031. static int
  1032. loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
  1033. {
  1034. struct loop_info info;
  1035. struct loop_info64 info64;
  1036. if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1037. return -EFAULT;
  1038. loop_info64_from_old(&info, &info64);
  1039. return loop_set_status(lo, &info64);
  1040. }
  1041. static int
  1042. loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
  1043. {
  1044. struct loop_info64 info64;
  1045. if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1046. return -EFAULT;
  1047. return loop_set_status(lo, &info64);
  1048. }
  1049. static int
  1050. loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
  1051. struct loop_info info;
  1052. struct loop_info64 info64;
  1053. int err = 0;
  1054. if (!arg)
  1055. err = -EINVAL;
  1056. if (!err)
  1057. err = loop_get_status(lo, &info64);
  1058. if (!err)
  1059. err = loop_info64_to_old(&info64, &info);
  1060. if (!err && copy_to_user(arg, &info, sizeof(info)))
  1061. err = -EFAULT;
  1062. return err;
  1063. }
  1064. static int
  1065. loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
  1066. struct loop_info64 info64;
  1067. int err = 0;
  1068. if (!arg)
  1069. err = -EINVAL;
  1070. if (!err)
  1071. err = loop_get_status(lo, &info64);
  1072. if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1073. err = -EFAULT;
  1074. return err;
  1075. }
  1076. static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
  1077. {
  1078. int err;
  1079. sector_t sec;
  1080. loff_t sz;
  1081. err = -ENXIO;
  1082. if (unlikely(lo->lo_state != Lo_bound))
  1083. goto out;
  1084. err = figure_loop_size(lo);
  1085. if (unlikely(err))
  1086. goto out;
  1087. sec = get_capacity(lo->lo_disk);
  1088. /* the width of sector_t may be narrow for bit-shift */
  1089. sz = sec;
  1090. sz <<= 9;
  1091. mutex_lock(&bdev->bd_mutex);
  1092. bd_set_size(bdev, sz);
  1093. /* let user-space know about the new size */
  1094. kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
  1095. mutex_unlock(&bdev->bd_mutex);
  1096. out:
  1097. return err;
  1098. }
  1099. static int lo_ioctl(struct block_device *bdev, fmode_t mode,
  1100. unsigned int cmd, unsigned long arg)
  1101. {
  1102. struct loop_device *lo = bdev->bd_disk->private_data;
  1103. int err;
  1104. mutex_lock_nested(&lo->lo_ctl_mutex, 1);
  1105. switch (cmd) {
  1106. case LOOP_SET_FD:
  1107. err = loop_set_fd(lo, mode, bdev, arg);
  1108. break;
  1109. case LOOP_CHANGE_FD:
  1110. err = loop_change_fd(lo, bdev, arg);
  1111. break;
  1112. case LOOP_CLR_FD:
  1113. /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
  1114. err = loop_clr_fd(lo, bdev);
  1115. if (!err)
  1116. goto out_unlocked;
  1117. break;
  1118. case LOOP_SET_STATUS:
  1119. err = loop_set_status_old(lo, (struct loop_info __user *) arg);
  1120. break;
  1121. case LOOP_GET_STATUS:
  1122. err = loop_get_status_old(lo, (struct loop_info __user *) arg);
  1123. break;
  1124. case LOOP_SET_STATUS64:
  1125. err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
  1126. break;
  1127. case LOOP_GET_STATUS64:
  1128. err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
  1129. break;
  1130. case LOOP_SET_CAPACITY:
  1131. err = -EPERM;
  1132. if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
  1133. err = loop_set_capacity(lo, bdev);
  1134. break;
  1135. default:
  1136. err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
  1137. }
  1138. mutex_unlock(&lo->lo_ctl_mutex);
  1139. out_unlocked:
  1140. return err;
  1141. }
  1142. #ifdef CONFIG_COMPAT
  1143. struct compat_loop_info {
  1144. compat_int_t lo_number; /* ioctl r/o */
  1145. compat_dev_t lo_device; /* ioctl r/o */
  1146. compat_ulong_t lo_inode; /* ioctl r/o */
  1147. compat_dev_t lo_rdevice; /* ioctl r/o */
  1148. compat_int_t lo_offset;
  1149. compat_int_t lo_encrypt_type;
  1150. compat_int_t lo_encrypt_key_size; /* ioctl w/o */
  1151. compat_int_t lo_flags; /* ioctl r/o */
  1152. char lo_name[LO_NAME_SIZE];
  1153. unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  1154. compat_ulong_t lo_init[2];
  1155. char reserved[4];
  1156. };
  1157. /*
  1158. * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
  1159. * - noinlined to reduce stack space usage in main part of driver
  1160. */
  1161. static noinline int
  1162. loop_info64_from_compat(const struct compat_loop_info __user *arg,
  1163. struct loop_info64 *info64)
  1164. {
  1165. struct compat_loop_info info;
  1166. if (copy_from_user(&info, arg, sizeof(info)))
  1167. return -EFAULT;
  1168. memset(info64, 0, sizeof(*info64));
  1169. info64->lo_number = info.lo_number;
  1170. info64->lo_device = info.lo_device;
  1171. info64->lo_inode = info.lo_inode;
  1172. info64->lo_rdevice = info.lo_rdevice;
  1173. info64->lo_offset = info.lo_offset;
  1174. info64->lo_sizelimit = 0;
  1175. info64->lo_encrypt_type = info.lo_encrypt_type;
  1176. info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1177. info64->lo_flags = info.lo_flags;
  1178. info64->lo_init[0] = info.lo_init[0];
  1179. info64->lo_init[1] = info.lo_init[1];
  1180. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1181. memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
  1182. else
  1183. memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
  1184. memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
  1185. return 0;
  1186. }
  1187. /*
  1188. * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
  1189. * - noinlined to reduce stack space usage in main part of driver
  1190. */
  1191. static noinline int
  1192. loop_info64_to_compat(const struct loop_info64 *info64,
  1193. struct compat_loop_info __user *arg)
  1194. {
  1195. struct compat_loop_info info;
  1196. memset(&info, 0, sizeof(info));
  1197. info.lo_number = info64->lo_number;
  1198. info.lo_device = info64->lo_device;
  1199. info.lo_inode = info64->lo_inode;
  1200. info.lo_rdevice = info64->lo_rdevice;
  1201. info.lo_offset = info64->lo_offset;
  1202. info.lo_encrypt_type = info64->lo_encrypt_type;
  1203. info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1204. info.lo_flags = info64->lo_flags;
  1205. info.lo_init[0] = info64->lo_init[0];
  1206. info.lo_init[1] = info64->lo_init[1];
  1207. if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
  1208. memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1209. else
  1210. memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1211. memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1212. /* error in case values were truncated */
  1213. if (info.lo_device != info64->lo_device ||
  1214. info.lo_rdevice != info64->lo_rdevice ||
  1215. info.lo_inode != info64->lo_inode ||
  1216. info.lo_offset != info64->lo_offset ||
  1217. info.lo_init[0] != info64->lo_init[0] ||
  1218. info.lo_init[1] != info64->lo_init[1])
  1219. return -EOVERFLOW;
  1220. if (copy_to_user(arg, &info, sizeof(info)))
  1221. return -EFAULT;
  1222. return 0;
  1223. }
  1224. static int
  1225. loop_set_status_compat(struct loop_device *lo,
  1226. const struct compat_loop_info __user *arg)
  1227. {
  1228. struct loop_info64 info64;
  1229. int ret;
  1230. ret = loop_info64_from_compat(arg, &info64);
  1231. if (ret < 0)
  1232. return ret;
  1233. return loop_set_status(lo, &info64);
  1234. }
  1235. static int
  1236. loop_get_status_compat(struct loop_device *lo,
  1237. struct compat_loop_info __user *arg)
  1238. {
  1239. struct loop_info64 info64;
  1240. int err = 0;
  1241. if (!arg)
  1242. err = -EINVAL;
  1243. if (!err)
  1244. err = loop_get_status(lo, &info64);
  1245. if (!err)
  1246. err = loop_info64_to_compat(&info64, arg);
  1247. return err;
  1248. }
  1249. static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1250. unsigned int cmd, unsigned long arg)
  1251. {
  1252. struct loop_device *lo = bdev->bd_disk->private_data;
  1253. int err;
  1254. switch(cmd) {
  1255. case LOOP_SET_STATUS:
  1256. mutex_lock(&lo->lo_ctl_mutex);
  1257. err = loop_set_status_compat(
  1258. lo, (const struct compat_loop_info __user *) arg);
  1259. mutex_unlock(&lo->lo_ctl_mutex);
  1260. break;
  1261. case LOOP_GET_STATUS:
  1262. mutex_lock(&lo->lo_ctl_mutex);
  1263. err = loop_get_status_compat(
  1264. lo, (struct compat_loop_info __user *) arg);
  1265. mutex_unlock(&lo->lo_ctl_mutex);
  1266. break;
  1267. case LOOP_SET_CAPACITY:
  1268. case LOOP_CLR_FD:
  1269. case LOOP_GET_STATUS64:
  1270. case LOOP_SET_STATUS64:
  1271. arg = (unsigned long) compat_ptr(arg);
  1272. case LOOP_SET_FD:
  1273. case LOOP_CHANGE_FD:
  1274. err = lo_ioctl(bdev, mode, cmd, arg);
  1275. break;
  1276. default:
  1277. err = -ENOIOCTLCMD;
  1278. break;
  1279. }
  1280. return err;
  1281. }
  1282. #endif
  1283. static int lo_open(struct block_device *bdev, fmode_t mode)
  1284. {
  1285. struct loop_device *lo = bdev->bd_disk->private_data;
  1286. mutex_lock(&lo->lo_ctl_mutex);
  1287. lo->lo_refcnt++;
  1288. mutex_unlock(&lo->lo_ctl_mutex);
  1289. return 0;
  1290. }
  1291. static int lo_release(struct gendisk *disk, fmode_t mode)
  1292. {
  1293. struct loop_device *lo = disk->private_data;
  1294. int err;
  1295. mutex_lock(&lo->lo_ctl_mutex);
  1296. if (--lo->lo_refcnt)
  1297. goto out;
  1298. if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
  1299. /*
  1300. * In autoclear mode, stop the loop thread
  1301. * and remove configuration after last close.
  1302. */
  1303. err = loop_clr_fd(lo, NULL);
  1304. if (!err)
  1305. goto out_unlocked;
  1306. } else {
  1307. /*
  1308. * Otherwise keep thread (if running) and config,
  1309. * but flush possible ongoing bios in thread.
  1310. */
  1311. loop_flush(lo);
  1312. }
  1313. out:
  1314. mutex_unlock(&lo->lo_ctl_mutex);
  1315. out_unlocked:
  1316. return 0;
  1317. }
  1318. static const struct block_device_operations lo_fops = {
  1319. .owner = THIS_MODULE,
  1320. .open = lo_open,
  1321. .release = lo_release,
  1322. .ioctl = lo_ioctl,
  1323. #ifdef CONFIG_COMPAT
  1324. .compat_ioctl = lo_compat_ioctl,
  1325. #endif
  1326. };
  1327. /*
  1328. * And now the modules code and kernel interface.
  1329. */
  1330. static int max_loop;
  1331. module_param(max_loop, int, S_IRUGO);
  1332. MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
  1333. module_param(max_part, int, S_IRUGO);
  1334. MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
  1335. MODULE_LICENSE("GPL");
  1336. MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
  1337. int loop_register_transfer(struct loop_func_table *funcs)
  1338. {
  1339. unsigned int n = funcs->number;
  1340. if (n >= MAX_LO_CRYPT || xfer_funcs[n])
  1341. return -EINVAL;
  1342. xfer_funcs[n] = funcs;
  1343. return 0;
  1344. }
  1345. int loop_unregister_transfer(int number)
  1346. {
  1347. unsigned int n = number;
  1348. struct loop_device *lo;
  1349. struct loop_func_table *xfer;
  1350. if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
  1351. return -EINVAL;
  1352. xfer_funcs[n] = NULL;
  1353. list_for_each_entry(lo, &loop_devices, lo_list) {
  1354. mutex_lock(&lo->lo_ctl_mutex);
  1355. if (lo->lo_encryption == xfer)
  1356. loop_release_xfer(lo);
  1357. mutex_unlock(&lo->lo_ctl_mutex);
  1358. }
  1359. return 0;
  1360. }
  1361. EXPORT_SYMBOL(loop_register_transfer);
  1362. EXPORT_SYMBOL(loop_unregister_transfer);
  1363. static struct loop_device *loop_alloc(int i)
  1364. {
  1365. struct loop_device *lo;
  1366. struct gendisk *disk;
  1367. lo = kzalloc(sizeof(*lo), GFP_KERNEL);
  1368. if (!lo)
  1369. goto out;
  1370. lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
  1371. if (!lo->lo_queue)
  1372. goto out_free_dev;
  1373. disk = lo->lo_disk = alloc_disk(1 << part_shift);
  1374. if (!disk)
  1375. goto out_free_queue;
  1376. mutex_init(&lo->lo_ctl_mutex);
  1377. lo->lo_number = i;
  1378. lo->lo_thread = NULL;
  1379. init_waitqueue_head(&lo->lo_event);
  1380. spin_lock_init(&lo->lo_lock);
  1381. disk->major = LOOP_MAJOR;
  1382. disk->first_minor = i << part_shift;
  1383. disk->fops = &lo_fops;
  1384. disk->private_data = lo;
  1385. disk->queue = lo->lo_queue;
  1386. sprintf(disk->disk_name, "loop%d", i);
  1387. return lo;
  1388. out_free_queue:
  1389. blk_cleanup_queue(lo->lo_queue);
  1390. out_free_dev:
  1391. kfree(lo);
  1392. out:
  1393. return NULL;
  1394. }
  1395. static void loop_free(struct loop_device *lo)
  1396. {
  1397. blk_cleanup_queue(lo->lo_queue);
  1398. put_disk(lo->lo_disk);
  1399. list_del(&lo->lo_list);
  1400. kfree(lo);
  1401. }
  1402. static struct loop_device *loop_init_one(int i)
  1403. {
  1404. struct loop_device *lo;
  1405. list_for_each_entry(lo, &loop_devices, lo_list) {
  1406. if (lo->lo_number == i)
  1407. return lo;
  1408. }
  1409. lo = loop_alloc(i);
  1410. if (lo) {
  1411. add_disk(lo->lo_disk);
  1412. list_add_tail(&lo->lo_list, &loop_devices);
  1413. }
  1414. return lo;
  1415. }
  1416. static void loop_del_one(struct loop_device *lo)
  1417. {
  1418. del_gendisk(lo->lo_disk);
  1419. loop_free(lo);
  1420. }
  1421. static struct kobject *loop_probe(dev_t dev, int *part, void *data)
  1422. {
  1423. struct loop_device *lo;
  1424. struct kobject *kobj;
  1425. mutex_lock(&loop_devices_mutex);
  1426. lo = loop_init_one(MINOR(dev) >> part_shift);
  1427. kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
  1428. mutex_unlock(&loop_devices_mutex);
  1429. *part = 0;
  1430. return kobj;
  1431. }
  1432. static int __init loop_init(void)
  1433. {
  1434. int i, nr;
  1435. unsigned long range;
  1436. struct loop_device *lo, *next;
  1437. /*
  1438. * loop module now has a feature to instantiate underlying device
  1439. * structure on-demand, provided that there is an access dev node.
  1440. * However, this will not work well with user space tool that doesn't
  1441. * know about such "feature". In order to not break any existing
  1442. * tool, we do the following:
  1443. *
  1444. * (1) if max_loop is specified, create that many upfront, and this
  1445. * also becomes a hard limit.
  1446. * (2) if max_loop is not specified, create 8 loop device on module
  1447. * load, user can further extend loop device by create dev node
  1448. * themselves and have kernel automatically instantiate actual
  1449. * device on-demand.
  1450. */
  1451. part_shift = 0;
  1452. if (max_part > 0) {
  1453. part_shift = fls(max_part);
  1454. /*
  1455. * Adjust max_part according to part_shift as it is exported
  1456. * to user space so that user can decide correct minor number
  1457. * if [s]he want to create more devices.
  1458. *
  1459. * Note that -1 is required because partition 0 is reserved
  1460. * for the whole disk.
  1461. */
  1462. max_part = (1UL << part_shift) - 1;
  1463. }
  1464. if ((1UL << part_shift) > DISK_MAX_PARTS)
  1465. return -EINVAL;
  1466. if (max_loop > 1UL << (MINORBITS - part_shift))
  1467. return -EINVAL;
  1468. if (max_loop) {
  1469. nr = max_loop;
  1470. range = max_loop << part_shift;
  1471. } else {
  1472. nr = 8;
  1473. range = 1UL << MINORBITS;
  1474. }
  1475. if (register_blkdev(LOOP_MAJOR, "loop"))
  1476. return -EIO;
  1477. for (i = 0; i < nr; i++) {
  1478. lo = loop_alloc(i);
  1479. if (!lo)
  1480. goto Enomem;
  1481. list_add_tail(&lo->lo_list, &loop_devices);
  1482. }
  1483. /* point of no return */
  1484. list_for_each_entry(lo, &loop_devices, lo_list)
  1485. add_disk(lo->lo_disk);
  1486. blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
  1487. THIS_MODULE, loop_probe, NULL, NULL);
  1488. printk(KERN_INFO "loop: module loaded\n");
  1489. return 0;
  1490. Enomem:
  1491. printk(KERN_INFO "loop: out of memory\n");
  1492. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1493. loop_free(lo);
  1494. unregister_blkdev(LOOP_MAJOR, "loop");
  1495. return -ENOMEM;
  1496. }
  1497. static void __exit loop_exit(void)
  1498. {
  1499. unsigned long range;
  1500. struct loop_device *lo, *next;
  1501. range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
  1502. list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
  1503. loop_del_one(lo);
  1504. blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
  1505. unregister_blkdev(LOOP_MAJOR, "loop");
  1506. }
  1507. module_init(loop_init);
  1508. module_exit(loop_exit);
  1509. #ifndef MODULE
  1510. static int __init max_loop_setup(char *str)
  1511. {
  1512. max_loop = simple_strtol(str, NULL, 0);
  1513. return 1;
  1514. }
  1515. __setup("max_loop=", max_loop_setup);
  1516. #endif