loop.c 43 KB

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