11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/block/loop.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by Theodore Ts'o, 3/29/93 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright 1993 by Theodore Ts'o. Redistribution of this file is 71da177e4SLinus Torvalds * permitted under the GNU General Public License. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 101da177e4SLinus Torvalds * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 131da177e4SLinus Torvalds * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Loadable modules and other fixes by AK, 1998 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Make real block number available to downstream transfer functions, enables 241da177e4SLinus Torvalds * CBC (and relatives) mode encryption requiring unique IVs per data block. 251da177e4SLinus Torvalds * Reed H. Petty, rhp@draper.net 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Maximum number of loop devices now dynamic via max_loop module parameter. 281da177e4SLinus Torvalds * Russell Kroll <rkroll@exploits.org> 19990701 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Maximum number of loop devices when compiled-in now selectable by passing 311da177e4SLinus Torvalds * max_loop=<1-255> to the kernel on boot. 3296de0e25SJan Engelhardt * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Completely rewrite request handling to be make_request_fn style and 351da177e4SLinus Torvalds * non blocking, pushing work to a helper thread. Lots of fixes from 361da177e4SLinus Torvalds * Al Viro too. 371da177e4SLinus Torvalds * Jens Axboe <axboe@suse.de>, Nov 2000 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * Support up to 256 loop devices 401da177e4SLinus Torvalds * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Support for falling back on the write file operation when the address space 434e02ed4bSNick Piggin * operations write_begin is not available on the backing filesystem. 441da177e4SLinus Torvalds * Anton Altaparmakov, 16 Feb 2005 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Still To Fix: 471da177e4SLinus Torvalds * - Advisory locking is ignored here. 481da177e4SLinus Torvalds * - Should use an own CAP_* category instead of CAP_SYS_ADMIN 491da177e4SLinus Torvalds * 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/module.h> 531da177e4SLinus Torvalds #include <linux/moduleparam.h> 541da177e4SLinus Torvalds #include <linux/sched.h> 551da177e4SLinus Torvalds #include <linux/fs.h> 561da177e4SLinus Torvalds #include <linux/file.h> 571da177e4SLinus Torvalds #include <linux/stat.h> 581da177e4SLinus Torvalds #include <linux/errno.h> 591da177e4SLinus Torvalds #include <linux/major.h> 601da177e4SLinus Torvalds #include <linux/wait.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 621da177e4SLinus Torvalds #include <linux/blkpg.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds #include <linux/swap.h> 651da177e4SLinus Torvalds #include <linux/slab.h> 661da177e4SLinus Torvalds #include <linux/loop.h> 67863d5b82SDavid Howells #include <linux/compat.h> 681da177e4SLinus Torvalds #include <linux/suspend.h> 6983144186SRafael J. Wysocki #include <linux/freezer.h> 702a48fc0aSArnd Bergmann #include <linux/mutex.h> 711da177e4SLinus Torvalds #include <linux/writeback.h> 721da177e4SLinus Torvalds #include <linux/completion.h> 731da177e4SLinus Torvalds #include <linux/highmem.h> 746c997918SSerge E. Hallyn #include <linux/kthread.h> 75d6b29d7cSJens Axboe #include <linux/splice.h> 76ee862730SMilan Broz #include <linux/sysfs.h> 77770fe30aSKay Sievers #include <linux/miscdevice.h> 78dfaa2ef6SLukas Czerner #include <linux/falloc.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include <asm/uaccess.h> 811da177e4SLinus Torvalds 8234dd82afSKay Sievers static DEFINE_IDR(loop_index_idr); 8334dd82afSKay Sievers static DEFINE_MUTEX(loop_index_mutex); 841da177e4SLinus Torvalds 85476a4813SLaurent Vivier static int max_part; 86476a4813SLaurent Vivier static int part_shift; 87476a4813SLaurent Vivier 881da177e4SLinus Torvalds /* 891da177e4SLinus Torvalds * Transfer functions 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds static int transfer_none(struct loop_device *lo, int cmd, 921da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 931da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 941da177e4SLinus Torvalds int size, sector_t real_block) 951da177e4SLinus Torvalds { 96cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 97cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (cmd == READ) 1001da177e4SLinus Torvalds memcpy(loop_buf, raw_buf, size); 1011da177e4SLinus Torvalds else 1021da177e4SLinus Torvalds memcpy(raw_buf, loop_buf, size); 1031da177e4SLinus Torvalds 104cfd8005cSCong Wang kunmap_atomic(loop_buf); 105cfd8005cSCong Wang kunmap_atomic(raw_buf); 1061da177e4SLinus Torvalds cond_resched(); 1071da177e4SLinus Torvalds return 0; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds static int transfer_xor(struct loop_device *lo, int cmd, 1111da177e4SLinus Torvalds struct page *raw_page, unsigned raw_off, 1121da177e4SLinus Torvalds struct page *loop_page, unsigned loop_off, 1131da177e4SLinus Torvalds int size, sector_t real_block) 1141da177e4SLinus Torvalds { 115cfd8005cSCong Wang char *raw_buf = kmap_atomic(raw_page) + raw_off; 116cfd8005cSCong Wang char *loop_buf = kmap_atomic(loop_page) + loop_off; 1171da177e4SLinus Torvalds char *in, *out, *key; 1181da177e4SLinus Torvalds int i, keysize; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (cmd == READ) { 1211da177e4SLinus Torvalds in = raw_buf; 1221da177e4SLinus Torvalds out = loop_buf; 1231da177e4SLinus Torvalds } else { 1241da177e4SLinus Torvalds in = loop_buf; 1251da177e4SLinus Torvalds out = raw_buf; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds key = lo->lo_encrypt_key; 1291da177e4SLinus Torvalds keysize = lo->lo_encrypt_key_size; 1301da177e4SLinus Torvalds for (i = 0; i < size; i++) 1311da177e4SLinus Torvalds *out++ = *in++ ^ key[(i & 511) % keysize]; 1321da177e4SLinus Torvalds 133cfd8005cSCong Wang kunmap_atomic(loop_buf); 134cfd8005cSCong Wang kunmap_atomic(raw_buf); 1351da177e4SLinus Torvalds cond_resched(); 1361da177e4SLinus Torvalds return 0; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds if (unlikely(info->lo_encrypt_key_size <= 0)) 1421da177e4SLinus Torvalds return -EINVAL; 1431da177e4SLinus Torvalds return 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static struct loop_func_table none_funcs = { 1471da177e4SLinus Torvalds .number = LO_CRYPT_NONE, 1481da177e4SLinus Torvalds .transfer = transfer_none, 1491da177e4SLinus Torvalds }; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct loop_func_table xor_funcs = { 1521da177e4SLinus Torvalds .number = LO_CRYPT_XOR, 1531da177e4SLinus Torvalds .transfer = transfer_xor, 1541da177e4SLinus Torvalds .init = xor_init 1551da177e4SLinus Torvalds }; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* xfer_funcs[0] is special - its release function is never called */ 1581da177e4SLinus Torvalds static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 1591da177e4SLinus Torvalds &none_funcs, 1601da177e4SLinus Torvalds &xor_funcs 1611da177e4SLinus Torvalds }; 1621da177e4SLinus Torvalds 1637035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 1641da177e4SLinus Torvalds { 1657035b5dfSDmitry Monakhov loff_t size, loopsize; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* Compute loopsize in bytes */ 1681da177e4SLinus Torvalds size = i_size_read(file->f_mapping->host); 1691da177e4SLinus Torvalds loopsize = size - offset; 1707035b5dfSDmitry Monakhov /* offset is beyond i_size, wierd but possible */ 1717035b5dfSDmitry Monakhov if (loopsize < 0) 1727035b5dfSDmitry Monakhov return 0; 1731da177e4SLinus Torvalds 1747035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize) 1757035b5dfSDmitry Monakhov loopsize = sizelimit; 1761da177e4SLinus Torvalds /* 1771da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device, 1781da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t. 1791da177e4SLinus Torvalds */ 1801da177e4SLinus Torvalds return loopsize >> 9; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1837035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file) 1841da177e4SLinus Torvalds { 1857035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file); 1867035b5dfSDmitry Monakhov } 1877035b5dfSDmitry Monakhov 1887035b5dfSDmitry Monakhov static int 1897035b5dfSDmitry Monakhov figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 1907035b5dfSDmitry Monakhov { 1917035b5dfSDmitry Monakhov loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 1921da177e4SLinus Torvalds sector_t x = (sector_t)size; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 1951da177e4SLinus Torvalds return -EFBIG; 1967035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 1977035b5dfSDmitry Monakhov lo->lo_offset = offset; 1987035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 1997035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 20073285082SKen Chen set_capacity(lo->lo_disk, x); 2011da177e4SLinus Torvalds return 0; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds static inline int 2051da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2061da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2071da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2081da177e4SLinus Torvalds int size, sector_t rblock) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds if (unlikely(!lo->transfer)) 2111da177e4SLinus Torvalds return 0; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /** 2171da177e4SLinus Torvalds * __do_lo_send_write - helper for writing data to a loop device 2181da177e4SLinus Torvalds * 2191da177e4SLinus Torvalds * This helper just factors out common code between do_lo_send_direct_write() 2201da177e4SLinus Torvalds * and do_lo_send_write(). 2211da177e4SLinus Torvalds */ 222858119e1SArjan van de Ven static int __do_lo_send_write(struct file *file, 22398ae6ccdSAl Viro u8 *buf, const int len, loff_t pos) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds ssize_t bw; 2261da177e4SLinus Torvalds mm_segment_t old_fs = get_fs(); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds set_fs(get_ds()); 2291da177e4SLinus Torvalds bw = file->f_op->write(file, buf, len, &pos); 2301da177e4SLinus Torvalds set_fs(old_fs); 2311da177e4SLinus Torvalds if (likely(bw == len)) 2321da177e4SLinus Torvalds return 0; 2331da177e4SLinus Torvalds printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 2341da177e4SLinus Torvalds (unsigned long long)pos, len); 2351da177e4SLinus Torvalds if (bw >= 0) 2361da177e4SLinus Torvalds bw = -EIO; 2371da177e4SLinus Torvalds return bw; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /** 2411da177e4SLinus Torvalds * do_lo_send_direct_write - helper for writing data to a loop device 2421da177e4SLinus Torvalds * 243456be148SChristoph Hellwig * This is the fast, non-transforming version that does not need double 244456be148SChristoph Hellwig * buffering. 2451da177e4SLinus Torvalds */ 2461da177e4SLinus Torvalds static int do_lo_send_direct_write(struct loop_device *lo, 247511de73fSAl Viro struct bio_vec *bvec, loff_t pos, struct page *page) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 25098ae6ccdSAl Viro kmap(bvec->bv_page) + bvec->bv_offset, 2511da177e4SLinus Torvalds bvec->bv_len, pos); 2521da177e4SLinus Torvalds kunmap(bvec->bv_page); 2531da177e4SLinus Torvalds cond_resched(); 2541da177e4SLinus Torvalds return bw; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds /** 2581da177e4SLinus Torvalds * do_lo_send_write - helper for writing data to a loop device 2591da177e4SLinus Torvalds * 260456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 261456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 262456be148SChristoph Hellwig * access to the destination pages of the backing file. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 265511de73fSAl Viro loff_t pos, struct page *page) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 2681da177e4SLinus Torvalds bvec->bv_offset, bvec->bv_len, pos >> 9); 2691da177e4SLinus Torvalds if (likely(!ret)) 2701da177e4SLinus Torvalds return __do_lo_send_write(lo->lo_backing_file, 27198ae6ccdSAl Viro page_address(page), bvec->bv_len, 2721da177e4SLinus Torvalds pos); 2731da177e4SLinus Torvalds printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 2741da177e4SLinus Torvalds "length %i.\n", (unsigned long long)pos, bvec->bv_len); 2751da177e4SLinus Torvalds if (ret > 0) 2761da177e4SLinus Torvalds ret = -EIO; 2771da177e4SLinus Torvalds return ret; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 280511de73fSAl Viro static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) 2811da177e4SLinus Torvalds { 282511de73fSAl Viro int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, 2831da177e4SLinus Torvalds struct page *page); 2841da177e4SLinus Torvalds struct bio_vec *bvec; 2851da177e4SLinus Torvalds struct page *page = NULL; 2861da177e4SLinus Torvalds int i, ret = 0; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds if (lo->transfer != transfer_none) { 2891da177e4SLinus Torvalds page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 2901da177e4SLinus Torvalds if (unlikely(!page)) 2911da177e4SLinus Torvalds goto fail; 2921da177e4SLinus Torvalds kmap(page); 2931da177e4SLinus Torvalds do_lo_send = do_lo_send_write; 294456be148SChristoph Hellwig } else { 295456be148SChristoph Hellwig do_lo_send = do_lo_send_direct_write; 2961da177e4SLinus Torvalds } 297456be148SChristoph Hellwig 2981da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 299511de73fSAl Viro ret = do_lo_send(lo, bvec, pos, page); 3001da177e4SLinus Torvalds if (ret < 0) 3011da177e4SLinus Torvalds break; 3021da177e4SLinus Torvalds pos += bvec->bv_len; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds if (page) { 3051da177e4SLinus Torvalds kunmap(page); 3061da177e4SLinus Torvalds __free_page(page); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds out: 3091da177e4SLinus Torvalds return ret; 3101da177e4SLinus Torvalds fail: 3111da177e4SLinus Torvalds printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 3121da177e4SLinus Torvalds ret = -ENOMEM; 3131da177e4SLinus Torvalds goto out; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds struct lo_read_data { 3171da177e4SLinus Torvalds struct loop_device *lo; 3181da177e4SLinus Torvalds struct page *page; 3191da177e4SLinus Torvalds unsigned offset; 3201da177e4SLinus Torvalds int bsize; 3211da177e4SLinus Torvalds }; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static int 324fd582140SJens Axboe lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 325fd582140SJens Axboe struct splice_desc *sd) 3261da177e4SLinus Torvalds { 327fd582140SJens Axboe struct lo_read_data *p = sd->u.data; 3281da177e4SLinus Torvalds struct loop_device *lo = p->lo; 329fd582140SJens Axboe struct page *page = buf->page; 3301da177e4SLinus Torvalds sector_t IV; 3313603b8eaSJens Axboe int size; 3321da177e4SLinus Torvalds 333fd582140SJens Axboe IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + 334fd582140SJens Axboe (buf->offset >> 9); 335fd582140SJens Axboe size = sd->len; 336fd582140SJens Axboe if (size > p->bsize) 337fd582140SJens Axboe size = p->bsize; 3381da177e4SLinus Torvalds 339fd582140SJens Axboe if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { 3401da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %ld\n", 3411da177e4SLinus Torvalds page->index); 342fd582140SJens Axboe size = -EINVAL; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds flush_dcache_page(p->page); 3461da177e4SLinus Torvalds 347fd582140SJens Axboe if (size > 0) 3481da177e4SLinus Torvalds p->offset += size; 349fd582140SJens Axboe 3501da177e4SLinus Torvalds return size; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds static int 354fd582140SJens Axboe lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) 355fd582140SJens Axboe { 356fd582140SJens Axboe return __splice_from_pipe(pipe, sd, lo_splice_actor); 357fd582140SJens Axboe } 358fd582140SJens Axboe 359306df071SDave Young static ssize_t 3601da177e4SLinus Torvalds do_lo_receive(struct loop_device *lo, 3611da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds struct lo_read_data cookie; 364fd582140SJens Axboe struct splice_desc sd; 3651da177e4SLinus Torvalds struct file *file; 366306df071SDave Young ssize_t retval; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds cookie.lo = lo; 3691da177e4SLinus Torvalds cookie.page = bvec->bv_page; 3701da177e4SLinus Torvalds cookie.offset = bvec->bv_offset; 3711da177e4SLinus Torvalds cookie.bsize = bsize; 372fd582140SJens Axboe 373fd582140SJens Axboe sd.len = 0; 374fd582140SJens Axboe sd.total_len = bvec->bv_len; 375fd582140SJens Axboe sd.flags = 0; 376fd582140SJens Axboe sd.pos = pos; 377fd582140SJens Axboe sd.u.data = &cookie; 378fd582140SJens Axboe 3791da177e4SLinus Torvalds file = lo->lo_backing_file; 380fd582140SJens Axboe retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); 381fd582140SJens Axboe 382fd582140SJens Axboe return retval; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds static int 3861da177e4SLinus Torvalds lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct bio_vec *bvec; 389306df071SDave Young ssize_t s; 390306df071SDave Young int i; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 393306df071SDave Young s = do_lo_receive(lo, bvec, bsize, pos); 394306df071SDave Young if (s < 0) 395306df071SDave Young return s; 396306df071SDave Young 397306df071SDave Young if (s != bvec->bv_len) { 398306df071SDave Young zero_fill_bio(bio); 3991da177e4SLinus Torvalds break; 400306df071SDave Young } 4011da177e4SLinus Torvalds pos += bvec->bv_len; 4021da177e4SLinus Torvalds } 403306df071SDave Young return 0; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds loff_t pos; 4091da177e4SLinus Torvalds int ret; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 41268db1961SNikanth Karthikesan 41368db1961SNikanth Karthikesan if (bio_rw(bio) == WRITE) { 41468db1961SNikanth Karthikesan struct file *file = lo->lo_backing_file; 41568db1961SNikanth Karthikesan 4166259f284STejun Heo if (bio->bi_rw & REQ_FLUSH) { 4178018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 4186259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) { 41968db1961SNikanth Karthikesan ret = -EIO; 42068db1961SNikanth Karthikesan goto out; 42168db1961SNikanth Karthikesan } 42268db1961SNikanth Karthikesan } 42368db1961SNikanth Karthikesan 424dfaa2ef6SLukas Czerner /* 425dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 426ae95757aSDave Young * image a.k.a. discard. However we do not support discard if 427dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 428dfaa2ef6SLukas Czerner * useful information. 429dfaa2ef6SLukas Czerner */ 430dfaa2ef6SLukas Czerner if (bio->bi_rw & REQ_DISCARD) { 431dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 432dfaa2ef6SLukas Czerner int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 433dfaa2ef6SLukas Czerner 434dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 435dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 436dfaa2ef6SLukas Czerner ret = -EOPNOTSUPP; 437dfaa2ef6SLukas Czerner goto out; 438dfaa2ef6SLukas Czerner } 439dfaa2ef6SLukas Czerner ret = file->f_op->fallocate(file, mode, pos, 440dfaa2ef6SLukas Czerner bio->bi_size); 441dfaa2ef6SLukas Czerner if (unlikely(ret && ret != -EINVAL && 442dfaa2ef6SLukas Czerner ret != -EOPNOTSUPP)) 443dfaa2ef6SLukas Czerner ret = -EIO; 444dfaa2ef6SLukas Czerner goto out; 445dfaa2ef6SLukas Czerner } 446dfaa2ef6SLukas Czerner 447511de73fSAl Viro ret = lo_send(lo, bio, pos); 44868db1961SNikanth Karthikesan 4496259f284STejun Heo if ((bio->bi_rw & REQ_FUA) && !ret) { 4508018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 4516259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) 45268db1961SNikanth Karthikesan ret = -EIO; 45368db1961SNikanth Karthikesan } 45468db1961SNikanth Karthikesan } else 4551da177e4SLinus Torvalds ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 45668db1961SNikanth Karthikesan 45768db1961SNikanth Karthikesan out: 4581da177e4SLinus Torvalds return ret; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* 4621da177e4SLinus Torvalds * Add bio to back of pending list 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds static void loop_add_bio(struct loop_device *lo, struct bio *bio) 4651da177e4SLinus Torvalds { 466e686307fSAkinobu Mita bio_list_add(&lo->lo_bio_list, bio); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * Grab first pending buffer 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds static struct bio *loop_get_bio(struct loop_device *lo) 4731da177e4SLinus Torvalds { 474e686307fSAkinobu Mita return bio_list_pop(&lo->lo_bio_list); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4775a7bbad2SChristoph Hellwig static void loop_make_request(struct request_queue *q, struct bio *old_bio) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 4801da177e4SLinus Torvalds int rw = bio_rw(old_bio); 4811da177e4SLinus Torvalds 48235a82d1aSNick Piggin if (rw == READA) 48335a82d1aSNick Piggin rw = READ; 48435a82d1aSNick Piggin 48535a82d1aSNick Piggin BUG_ON(!lo || (rw != READ && rw != WRITE)); 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 4881da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 48935a82d1aSNick Piggin goto out; 49035a82d1aSNick Piggin if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 49135a82d1aSNick Piggin goto out; 4921da177e4SLinus Torvalds loop_add_bio(lo, old_bio); 4936c997918SSerge E. Hallyn wake_up(&lo->lo_event); 49435a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 4955a7bbad2SChristoph Hellwig return; 49635a82d1aSNick Piggin 4971da177e4SLinus Torvalds out: 49835a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 4996712ecf8SNeilBrown bio_io_error(old_bio); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds struct switch_request { 5031da177e4SLinus Torvalds struct file *file; 5041da177e4SLinus Torvalds struct completion wait; 5051da177e4SLinus Torvalds }; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *, struct switch_request *); 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 5101da177e4SLinus Torvalds { 5111da177e4SLinus Torvalds if (unlikely(!bio->bi_bdev)) { 5121da177e4SLinus Torvalds do_loop_switch(lo, bio->bi_private); 5131da177e4SLinus Torvalds bio_put(bio); 5141da177e4SLinus Torvalds } else { 51535a82d1aSNick Piggin int ret = do_bio_filebacked(lo, bio); 5166712ecf8SNeilBrown bio_endio(bio, ret); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* 5211da177e4SLinus Torvalds * worker thread that handles reads/writes to file backed loop devices, 5221da177e4SLinus Torvalds * to avoid blocking in our make_request_fn. it also does loop decrypting 5231da177e4SLinus Torvalds * on reads for block backed loop, as that is too heavy to do from 5241da177e4SLinus Torvalds * b_end_io context where irqs may be disabled. 5256c997918SSerge E. Hallyn * 5266c997918SSerge E. Hallyn * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before 5276c997918SSerge E. Hallyn * calling kthread_stop(). Therefore once kthread_should_stop() is 5286c997918SSerge E. Hallyn * true, make_request will not place any more requests. Therefore 5296c997918SSerge E. Hallyn * once kthread_should_stop() is true and lo_bio is NULL, we are 5306c997918SSerge E. Hallyn * done with the loop. 5311da177e4SLinus Torvalds */ 5321da177e4SLinus Torvalds static int loop_thread(void *data) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds struct loop_device *lo = data; 5351da177e4SLinus Torvalds struct bio *bio; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds set_user_nice(current, -20); 5381da177e4SLinus Torvalds 539e686307fSAkinobu Mita while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) { 5401da177e4SLinus Torvalds 5416c997918SSerge E. Hallyn wait_event_interruptible(lo->lo_event, 542e686307fSAkinobu Mita !bio_list_empty(&lo->lo_bio_list) || 543e686307fSAkinobu Mita kthread_should_stop()); 54409c0dc68SLinus Torvalds 545e686307fSAkinobu Mita if (bio_list_empty(&lo->lo_bio_list)) 54635a82d1aSNick Piggin continue; 54735a82d1aSNick Piggin spin_lock_irq(&lo->lo_lock); 5481da177e4SLinus Torvalds bio = loop_get_bio(lo); 54935a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 55035a82d1aSNick Piggin 55135a82d1aSNick Piggin BUG_ON(!bio); 5521da177e4SLinus Torvalds loop_handle_bio(lo, bio); 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds return 0; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* 5591da177e4SLinus Torvalds * loop_switch performs the hard work of switching a backing store. 5601da177e4SLinus Torvalds * First it needs to flush existing IO, it does this by sending a magic 5611da177e4SLinus Torvalds * BIO down the pipe. The completion of this BIO does the actual switch. 5621da177e4SLinus Torvalds */ 5631da177e4SLinus Torvalds static int loop_switch(struct loop_device *lo, struct file *file) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds struct switch_request w; 566a24eab1eSJens Axboe struct bio *bio = bio_alloc(GFP_KERNEL, 0); 5671da177e4SLinus Torvalds if (!bio) 5681da177e4SLinus Torvalds return -ENOMEM; 5691da177e4SLinus Torvalds init_completion(&w.wait); 5701da177e4SLinus Torvalds w.file = file; 5711da177e4SLinus Torvalds bio->bi_private = &w; 5721da177e4SLinus Torvalds bio->bi_bdev = NULL; 5731da177e4SLinus Torvalds loop_make_request(lo->lo_queue, bio); 5741da177e4SLinus Torvalds wait_for_completion(&w.wait); 5751da177e4SLinus Torvalds return 0; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* 57914f27939SMilan Broz * Helper to flush the IOs in loop, but keeping loop thread running 58014f27939SMilan Broz */ 58114f27939SMilan Broz static int loop_flush(struct loop_device *lo) 58214f27939SMilan Broz { 58314f27939SMilan Broz /* loop not yet configured, no running thread, nothing to flush */ 58414f27939SMilan Broz if (!lo->lo_thread) 58514f27939SMilan Broz return 0; 58614f27939SMilan Broz 58714f27939SMilan Broz return loop_switch(lo, NULL); 58814f27939SMilan Broz } 58914f27939SMilan Broz 59014f27939SMilan Broz /* 5911da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 5921da177e4SLinus Torvalds */ 5931da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds struct file *file = p->file; 5961da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 59714f27939SMilan Broz struct address_space *mapping; 5981da177e4SLinus Torvalds 59914f27939SMilan Broz /* if no new file, only flush of queued bios requested */ 60014f27939SMilan Broz if (!file) 60114f27939SMilan Broz goto out; 60214f27939SMilan Broz 60314f27939SMilan Broz mapping = file->f_mapping; 6041da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 6051da177e4SLinus Torvalds lo->lo_backing_file = file; 606ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 607ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 6081da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 6091da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 61014f27939SMilan Broz out: 6111da177e4SLinus Torvalds complete(&p->wait); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* 6161da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6171da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6181da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6191da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6201da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6211da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6221da177e4SLinus Torvalds */ 623bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 624bb214884SAl Viro unsigned int arg) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds struct file *file, *old_file; 6271da177e4SLinus Torvalds struct inode *inode; 6281da177e4SLinus Torvalds int error; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds error = -ENXIO; 6311da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6321da177e4SLinus Torvalds goto out; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* the loop device has to be read-only */ 6351da177e4SLinus Torvalds error = -EINVAL; 6361da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6371da177e4SLinus Torvalds goto out; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds error = -EBADF; 6401da177e4SLinus Torvalds file = fget(arg); 6411da177e4SLinus Torvalds if (!file) 6421da177e4SLinus Torvalds goto out; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds inode = file->f_mapping->host; 6451da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds error = -EINVAL; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 6501da177e4SLinus Torvalds goto out_putf; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6531da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6541da177e4SLinus Torvalds goto out_putf; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* and ... switch */ 6571da177e4SLinus Torvalds error = loop_switch(lo, file); 6581da177e4SLinus Torvalds if (error) 6591da177e4SLinus Torvalds goto out_putf; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds fput(old_file); 662e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 663476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 6641da177e4SLinus Torvalds return 0; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds out_putf: 6671da177e4SLinus Torvalds fput(file); 6681da177e4SLinus Torvalds out: 6691da177e4SLinus Torvalds return error; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 6731da177e4SLinus Torvalds { 6741da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 679ee862730SMilan Broz /* loop sysfs attributes */ 680ee862730SMilan Broz 681ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 682ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 683ee862730SMilan Broz { 68434dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 68534dd82afSKay Sievers struct loop_device *lo = disk->private_data; 686ee862730SMilan Broz 68734dd82afSKay Sievers return callback(lo, page); 688ee862730SMilan Broz } 689ee862730SMilan Broz 690ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 691ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 692ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 693ee862730SMilan Broz struct device_attribute *attr, char *b) \ 694ee862730SMilan Broz { \ 695ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 696ee862730SMilan Broz } \ 697ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 698ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 699ee862730SMilan Broz 700ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 701ee862730SMilan Broz { 702ee862730SMilan Broz ssize_t ret; 703ee862730SMilan Broz char *p = NULL; 704ee862730SMilan Broz 70505eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 706ee862730SMilan Broz if (lo->lo_backing_file) 707ee862730SMilan Broz p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1); 70805eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 709ee862730SMilan Broz 710ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 711ee862730SMilan Broz ret = PTR_ERR(p); 712ee862730SMilan Broz else { 713ee862730SMilan Broz ret = strlen(p); 714ee862730SMilan Broz memmove(buf, p, ret); 715ee862730SMilan Broz buf[ret++] = '\n'; 716ee862730SMilan Broz buf[ret] = 0; 717ee862730SMilan Broz } 718ee862730SMilan Broz 719ee862730SMilan Broz return ret; 720ee862730SMilan Broz } 721ee862730SMilan Broz 722ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 723ee862730SMilan Broz { 724ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 725ee862730SMilan Broz } 726ee862730SMilan Broz 727ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 728ee862730SMilan Broz { 729ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 730ee862730SMilan Broz } 731ee862730SMilan Broz 732ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 733ee862730SMilan Broz { 734ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 735ee862730SMilan Broz 736ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 737ee862730SMilan Broz } 738ee862730SMilan Broz 739e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 740e03c8dd1SKay Sievers { 741e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 742e03c8dd1SKay Sievers 743e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 744e03c8dd1SKay Sievers } 745e03c8dd1SKay Sievers 746ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 747ee862730SMilan Broz LOOP_ATTR_RO(offset); 748ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 749ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 750e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 751ee862730SMilan Broz 752ee862730SMilan Broz static struct attribute *loop_attrs[] = { 753ee862730SMilan Broz &loop_attr_backing_file.attr, 754ee862730SMilan Broz &loop_attr_offset.attr, 755ee862730SMilan Broz &loop_attr_sizelimit.attr, 756ee862730SMilan Broz &loop_attr_autoclear.attr, 757e03c8dd1SKay Sievers &loop_attr_partscan.attr, 758ee862730SMilan Broz NULL, 759ee862730SMilan Broz }; 760ee862730SMilan Broz 761ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 762ee862730SMilan Broz .name = "loop", 763ee862730SMilan Broz .attrs= loop_attrs, 764ee862730SMilan Broz }; 765ee862730SMilan Broz 766ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 767ee862730SMilan Broz { 768ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 769ee862730SMilan Broz &loop_attribute_group); 770ee862730SMilan Broz } 771ee862730SMilan Broz 772ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 773ee862730SMilan Broz { 774ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 775ee862730SMilan Broz &loop_attribute_group); 776ee862730SMilan Broz } 777ee862730SMilan Broz 778dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 779dfaa2ef6SLukas Czerner { 780dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 781dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 782dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 783dfaa2ef6SLukas Czerner 784dfaa2ef6SLukas Czerner /* 785dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 786dfaa2ef6SLukas Czerner * image a.k.a. discard. However we do support discard if 787dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 788dfaa2ef6SLukas Czerner * useful information. 789dfaa2ef6SLukas Czerner */ 790dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 791dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 792dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 793dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 794dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = 0; 795dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 0; 796dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 797dfaa2ef6SLukas Czerner return; 798dfaa2ef6SLukas Czerner } 799dfaa2ef6SLukas Czerner 800dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 801dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 802dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = UINT_MAX >> 9; 803dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 1; 804dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 805dfaa2ef6SLukas Czerner } 806dfaa2ef6SLukas Czerner 807bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8081da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds struct file *file, *f; 8111da177e4SLinus Torvalds struct inode *inode; 8121da177e4SLinus Torvalds struct address_space *mapping; 8131da177e4SLinus Torvalds unsigned lo_blocksize; 8141da177e4SLinus Torvalds int lo_flags = 0; 8151da177e4SLinus Torvalds int error; 8161da177e4SLinus Torvalds loff_t size; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8191da177e4SLinus Torvalds __module_get(THIS_MODULE); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds error = -EBADF; 8221da177e4SLinus Torvalds file = fget(arg); 8231da177e4SLinus Torvalds if (!file) 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds error = -EBUSY; 8271da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8281da177e4SLinus Torvalds goto out_putf; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* Avoid recursion */ 8311da177e4SLinus Torvalds f = file; 8321da177e4SLinus Torvalds while (is_loop_device(f)) { 8331da177e4SLinus Torvalds struct loop_device *l; 8341da177e4SLinus Torvalds 835bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 8361da177e4SLinus Torvalds goto out_putf; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 8391da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 8401da177e4SLinus Torvalds error = -EINVAL; 8411da177e4SLinus Torvalds goto out_putf; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds f = l->lo_backing_file; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds mapping = file->f_mapping; 8471da177e4SLinus Torvalds inode = mapping->host; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds error = -EINVAL; 850456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 851456be148SChristoph Hellwig goto out_putf; 8526818173bSMiklos Szeredi 853456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 854456be148SChristoph Hellwig !file->f_op->write) 8551da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 8561da177e4SLinus Torvalds 857ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 858ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 859ba52de12STheodore Ts'o 8601da177e4SLinus Torvalds error = -EFBIG; 861456be148SChristoph Hellwig size = get_loop_size(lo, file); 862456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 8631da177e4SLinus Torvalds goto out_putf; 8641da177e4SLinus Torvalds 865456be148SChristoph Hellwig error = 0; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 8701da177e4SLinus Torvalds lo->lo_device = bdev; 8711da177e4SLinus Torvalds lo->lo_flags = lo_flags; 8721da177e4SLinus Torvalds lo->lo_backing_file = file; 873eefe85eeSConstantine Sapuntzakis lo->transfer = transfer_none; 8741da177e4SLinus Torvalds lo->ioctl = NULL; 8751da177e4SLinus Torvalds lo->lo_sizelimit = 0; 8761da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 8771da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 8781da177e4SLinus Torvalds 879e686307fSAkinobu Mita bio_list_init(&lo->lo_bio_list); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* 8821da177e4SLinus Torvalds * set queue make_request_fn, and add limits based on lower level 8831da177e4SLinus Torvalds * device 8841da177e4SLinus Torvalds */ 8851da177e4SLinus Torvalds blk_queue_make_request(lo->lo_queue, loop_make_request); 8861da177e4SLinus Torvalds lo->lo_queue->queuedata = lo; 8871da177e4SLinus Torvalds 88868db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 8894913efe4STejun Heo blk_queue_flush(lo->lo_queue, REQ_FLUSH); 89068db1961SNikanth Karthikesan 89173285082SKen Chen set_capacity(lo->lo_disk, size); 8921da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 893ee862730SMilan Broz loop_sysfs_init(lo); 894c3473c63SDavid Zeuthen /* let user-space know about the new size */ 895c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 8981da177e4SLinus Torvalds 8996c997918SSerge E. Hallyn lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 9006c997918SSerge E. Hallyn lo->lo_number); 9016c997918SSerge E. Hallyn if (IS_ERR(lo->lo_thread)) { 9026c997918SSerge E. Hallyn error = PTR_ERR(lo->lo_thread); 903a7422bf8SSerge E. Hallyn goto out_clr; 9046c997918SSerge E. Hallyn } 9056c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 9066c997918SSerge E. Hallyn wake_up_process(lo->lo_thread); 907e03c8dd1SKay Sievers if (part_shift) 908e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 909e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 910476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 9111da177e4SLinus Torvalds return 0; 9121da177e4SLinus Torvalds 913a7422bf8SSerge E. Hallyn out_clr: 914ee862730SMilan Broz loop_sysfs_exit(lo); 915a7422bf8SSerge E. Hallyn lo->lo_thread = NULL; 916a7422bf8SSerge E. Hallyn lo->lo_device = NULL; 917a7422bf8SSerge E. Hallyn lo->lo_backing_file = NULL; 918a7422bf8SSerge E. Hallyn lo->lo_flags = 0; 91973285082SKen Chen set_capacity(lo->lo_disk, 0); 920f98393a6SPeter Zijlstra invalidate_bdev(bdev); 921a7422bf8SSerge E. Hallyn bd_set_size(bdev, 0); 922c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 923a7422bf8SSerge E. Hallyn mapping_set_gfp_mask(mapping, lo->old_gfp_mask); 924a7422bf8SSerge E. Hallyn lo->lo_state = Lo_unbound; 9251da177e4SLinus Torvalds out_putf: 9261da177e4SLinus Torvalds fput(file); 9271da177e4SLinus Torvalds out: 9281da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9291da177e4SLinus Torvalds module_put(THIS_MODULE); 9301da177e4SLinus Torvalds return error; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds static int 9341da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9351da177e4SLinus Torvalds { 9361da177e4SLinus Torvalds int err = 0; 9371da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds if (xfer) { 9401da177e4SLinus Torvalds if (xfer->release) 9411da177e4SLinus Torvalds err = xfer->release(lo); 9421da177e4SLinus Torvalds lo->transfer = NULL; 9431da177e4SLinus Torvalds lo->lo_encryption = NULL; 9441da177e4SLinus Torvalds module_put(xfer->owner); 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds return err; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds static int 9501da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9511da177e4SLinus Torvalds const struct loop_info64 *i) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds int err = 0; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds if (xfer) { 9561da177e4SLinus Torvalds struct module *owner = xfer->owner; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds if (!try_module_get(owner)) 9591da177e4SLinus Torvalds return -EINVAL; 9601da177e4SLinus Torvalds if (xfer->init) 9611da177e4SLinus Torvalds err = xfer->init(lo, i); 9621da177e4SLinus Torvalds if (err) 9631da177e4SLinus Torvalds module_put(owner); 9641da177e4SLinus Torvalds else 9651da177e4SLinus Torvalds lo->lo_encryption = xfer; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds return err; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9704c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 973b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 9744c823cc3SAyan George struct block_device *bdev = lo->lo_device; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9771da177e4SLinus Torvalds return -ENXIO; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 9801da177e4SLinus Torvalds return -EBUSY; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds if (filp == NULL) 9831da177e4SLinus Torvalds return -EINVAL; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 9861da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 9871da177e4SLinus Torvalds spin_unlock_irq(&lo->lo_lock); 9881da177e4SLinus Torvalds 9896c997918SSerge E. Hallyn kthread_stop(lo->lo_thread); 9901da177e4SLinus Torvalds 99105eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 9921da177e4SLinus Torvalds lo->lo_backing_file = NULL; 99305eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds loop_release_xfer(lo); 9961da177e4SLinus Torvalds lo->transfer = NULL; 9971da177e4SLinus Torvalds lo->ioctl = NULL; 9981da177e4SLinus Torvalds lo->lo_device = NULL; 9991da177e4SLinus Torvalds lo->lo_encryption = NULL; 10001da177e4SLinus Torvalds lo->lo_offset = 0; 10011da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10021da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10036c997918SSerge E. Hallyn lo->lo_thread = NULL; 10041da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10051da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10061da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1007bb214884SAl Viro if (bdev) 1008f98393a6SPeter Zijlstra invalidate_bdev(bdev); 100973285082SKen Chen set_capacity(lo->lo_disk, 0); 101051a0bb0cSMilan Broz loop_sysfs_exit(lo); 1011c3473c63SDavid Zeuthen if (bdev) { 10121da177e4SLinus Torvalds bd_set_size(bdev, 0); 1013c3473c63SDavid Zeuthen /* let user-space know about this change */ 1014c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1015c3473c63SDavid Zeuthen } 10161da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10171da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10181da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10191da177e4SLinus Torvalds module_put(THIS_MODULE); 1020e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 1021476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 1022e03c8dd1SKay Sievers lo->lo_flags = 0; 1023e03c8dd1SKay Sievers if (!part_shift) 1024e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1025f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1026f028f3b2SNikanth Karthikesan /* 1027f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1028f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1029f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1030f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1031f028f3b2SNikanth Karthikesan */ 1032f028f3b2SNikanth Karthikesan fput(filp); 10331da177e4SLinus Torvalds return 0; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds static int 10371da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds int err; 10401da177e4SLinus Torvalds struct loop_func_table *xfer; 1041b0fafa81SDavid Howells uid_t uid = current_uid(); 10421da177e4SLinus Torvalds 1043b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1044b0fafa81SDavid Howells lo->lo_key_owner != uid && 10451da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 10461da177e4SLinus Torvalds return -EPERM; 10471da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10481da177e4SLinus Torvalds return -ENXIO; 10491da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 10501da177e4SLinus Torvalds return -EINVAL; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds err = loop_release_xfer(lo); 10531da177e4SLinus Torvalds if (err) 10541da177e4SLinus Torvalds return err; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (info->lo_encrypt_type) { 10571da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 10601da177e4SLinus Torvalds return -EINVAL; 10611da177e4SLinus Torvalds xfer = xfer_funcs[type]; 10621da177e4SLinus Torvalds if (xfer == NULL) 10631da177e4SLinus Torvalds return -EINVAL; 10641da177e4SLinus Torvalds } else 10651da177e4SLinus Torvalds xfer = NULL; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 10681da177e4SLinus Torvalds if (err) 10691da177e4SLinus Torvalds return err; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 10721da177e4SLinus Torvalds lo->lo_sizelimit != info->lo_sizelimit) { 10737035b5dfSDmitry Monakhov if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) 10741da177e4SLinus Torvalds return -EFBIG; 10751da177e4SLinus Torvalds } 1076dfaa2ef6SLukas Czerner loop_config_discard(lo); 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 10791da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 10801da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 10811da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds if (!xfer) 10841da177e4SLinus Torvalds xfer = &none_funcs; 10851da177e4SLinus Torvalds lo->transfer = xfer->transfer; 10861da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 10871da177e4SLinus Torvalds 108896c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 108996c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 109096c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 109196c58655SDavid Woodhouse 1092e03c8dd1SKay Sievers if ((info->lo_flags & LO_FLAGS_PARTSCAN) && 1093e03c8dd1SKay Sievers !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1094e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 1095e03c8dd1SKay Sievers lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1096e03c8dd1SKay Sievers ioctl_by_bdev(lo->lo_device, BLKRRPART, 0); 1097e03c8dd1SKay Sievers } 1098e03c8dd1SKay Sievers 10991da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11001da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11011da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11021da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11031da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11041da177e4SLinus Torvalds info->lo_encrypt_key_size); 1105b0fafa81SDavid Howells lo->lo_key_owner = uid; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds return 0; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds static int 11121da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 11151da177e4SLinus Torvalds struct kstat stat; 11161da177e4SLinus Torvalds int error; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11191da177e4SLinus Torvalds return -ENXIO; 11206c648be6SJosef Sipek error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat); 11211da177e4SLinus Torvalds if (error) 11221da177e4SLinus Torvalds return error; 11231da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11241da177e4SLinus Torvalds info->lo_number = lo->lo_number; 11251da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 11261da177e4SLinus Torvalds info->lo_inode = stat.ino; 11271da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 11281da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 11291da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 11301da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 11311da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 11321da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 11331da177e4SLinus Torvalds info->lo_encrypt_type = 11341da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 11351da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 11361da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 11371da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 11381da177e4SLinus Torvalds lo->lo_encrypt_key_size); 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds return 0; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds static void 11441da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 11451da177e4SLinus Torvalds { 11461da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 11471da177e4SLinus Torvalds info64->lo_number = info->lo_number; 11481da177e4SLinus Torvalds info64->lo_device = info->lo_device; 11491da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 11501da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 11511da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 11521da177e4SLinus Torvalds info64->lo_sizelimit = 0; 11531da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 11541da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 11551da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 11561da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 11571da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 11581da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 11591da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 11601da177e4SLinus Torvalds else 11611da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 11621da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds static int 11661da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 11671da177e4SLinus Torvalds { 11681da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11691da177e4SLinus Torvalds info->lo_number = info64->lo_number; 11701da177e4SLinus Torvalds info->lo_device = info64->lo_device; 11711da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 11721da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 11731da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 11741da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 11751da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 11761da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 11771da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 11781da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 11791da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 11801da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 11811da177e4SLinus Torvalds else 11821da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 11831da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds /* error in case values were truncated */ 11861da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 11871da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 11881da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 11891da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 11901da177e4SLinus Torvalds return -EOVERFLOW; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds return 0; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds static int 11961da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 11971da177e4SLinus Torvalds { 11981da177e4SLinus Torvalds struct loop_info info; 11991da177e4SLinus Torvalds struct loop_info64 info64; 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12021da177e4SLinus Torvalds return -EFAULT; 12031da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12041da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds static int 12081da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds struct loop_info64 info64; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 12131da177e4SLinus Torvalds return -EFAULT; 12141da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds static int 12181da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 12191da177e4SLinus Torvalds struct loop_info info; 12201da177e4SLinus Torvalds struct loop_info64 info64; 12211da177e4SLinus Torvalds int err = 0; 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds if (!arg) 12241da177e4SLinus Torvalds err = -EINVAL; 12251da177e4SLinus Torvalds if (!err) 12261da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12271da177e4SLinus Torvalds if (!err) 12281da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 12291da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 12301da177e4SLinus Torvalds err = -EFAULT; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds return err; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds static int 12361da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 12371da177e4SLinus Torvalds struct loop_info64 info64; 12381da177e4SLinus Torvalds int err = 0; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds if (!arg) 12411da177e4SLinus Torvalds err = -EINVAL; 12421da177e4SLinus Torvalds if (!err) 12431da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12441da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 12451da177e4SLinus Torvalds err = -EFAULT; 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds return err; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 125053d66608SJ. R. Okajima static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 125153d66608SJ. R. Okajima { 125253d66608SJ. R. Okajima int err; 125353d66608SJ. R. Okajima sector_t sec; 125453d66608SJ. R. Okajima loff_t sz; 125553d66608SJ. R. Okajima 125653d66608SJ. R. Okajima err = -ENXIO; 125753d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 125853d66608SJ. R. Okajima goto out; 12597035b5dfSDmitry Monakhov err = figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 126053d66608SJ. R. Okajima if (unlikely(err)) 126153d66608SJ. R. Okajima goto out; 126253d66608SJ. R. Okajima sec = get_capacity(lo->lo_disk); 126353d66608SJ. R. Okajima /* the width of sector_t may be narrow for bit-shift */ 126453d66608SJ. R. Okajima sz = sec; 126553d66608SJ. R. Okajima sz <<= 9; 126653d66608SJ. R. Okajima mutex_lock(&bdev->bd_mutex); 126753d66608SJ. R. Okajima bd_set_size(bdev, sz); 1268c3473c63SDavid Zeuthen /* let user-space know about the new size */ 1269c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 127053d66608SJ. R. Okajima mutex_unlock(&bdev->bd_mutex); 127153d66608SJ. R. Okajima 127253d66608SJ. R. Okajima out: 127353d66608SJ. R. Okajima return err; 127453d66608SJ. R. Okajima } 127553d66608SJ. R. Okajima 1276bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 12771da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 12781da177e4SLinus Torvalds { 1279bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 12801da177e4SLinus Torvalds int err; 12811da177e4SLinus Torvalds 1282f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 12831da177e4SLinus Torvalds switch (cmd) { 12841da177e4SLinus Torvalds case LOOP_SET_FD: 1285bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 12861da177e4SLinus Torvalds break; 12871da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1288bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 12891da177e4SLinus Torvalds break; 12901da177e4SLinus Torvalds case LOOP_CLR_FD: 1291f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 12924c823cc3SAyan George err = loop_clr_fd(lo); 1293f028f3b2SNikanth Karthikesan if (!err) 1294f028f3b2SNikanth Karthikesan goto out_unlocked; 12951da177e4SLinus Torvalds break; 12961da177e4SLinus Torvalds case LOOP_SET_STATUS: 12977035b5dfSDmitry Monakhov err = -EPERM; 12987035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 12997035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 13007035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 13011da177e4SLinus Torvalds break; 13021da177e4SLinus Torvalds case LOOP_GET_STATUS: 13031da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 13041da177e4SLinus Torvalds break; 13051da177e4SLinus Torvalds case LOOP_SET_STATUS64: 13067035b5dfSDmitry Monakhov err = -EPERM; 13077035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13087035b5dfSDmitry Monakhov err = loop_set_status64(lo, 13097035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 13101da177e4SLinus Torvalds break; 13111da177e4SLinus Torvalds case LOOP_GET_STATUS64: 13121da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 13131da177e4SLinus Torvalds break; 131453d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 131553d66608SJ. R. Okajima err = -EPERM; 131653d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 131753d66608SJ. R. Okajima err = loop_set_capacity(lo, bdev); 131853d66608SJ. R. Okajima break; 13191da177e4SLinus Torvalds default: 13201da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 13211da177e4SLinus Torvalds } 1322f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1323f028f3b2SNikanth Karthikesan 1324f028f3b2SNikanth Karthikesan out_unlocked: 13251da177e4SLinus Torvalds return err; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 1328863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1329863d5b82SDavid Howells struct compat_loop_info { 1330863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1331863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1332863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1333863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1334863d5b82SDavid Howells compat_int_t lo_offset; 1335863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1336863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1337863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1338863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1339863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1340863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1341863d5b82SDavid Howells char reserved[4]; 1342863d5b82SDavid Howells }; 1343863d5b82SDavid Howells 1344863d5b82SDavid Howells /* 1345863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1346863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1347863d5b82SDavid Howells */ 1348863d5b82SDavid Howells static noinline int 1349ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1350863d5b82SDavid Howells struct loop_info64 *info64) 1351863d5b82SDavid Howells { 1352863d5b82SDavid Howells struct compat_loop_info info; 1353863d5b82SDavid Howells 1354863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1355863d5b82SDavid Howells return -EFAULT; 1356863d5b82SDavid Howells 1357863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1358863d5b82SDavid Howells info64->lo_number = info.lo_number; 1359863d5b82SDavid Howells info64->lo_device = info.lo_device; 1360863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1361863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1362863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1363863d5b82SDavid Howells info64->lo_sizelimit = 0; 1364863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1365863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1366863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1367863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1368863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1369863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1370863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1371863d5b82SDavid Howells else 1372863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1373863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1374863d5b82SDavid Howells return 0; 1375863d5b82SDavid Howells } 1376863d5b82SDavid Howells 1377863d5b82SDavid Howells /* 1378863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1379863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1380863d5b82SDavid Howells */ 1381863d5b82SDavid Howells static noinline int 1382863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1383863d5b82SDavid Howells struct compat_loop_info __user *arg) 1384863d5b82SDavid Howells { 1385863d5b82SDavid Howells struct compat_loop_info info; 1386863d5b82SDavid Howells 1387863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1388863d5b82SDavid Howells info.lo_number = info64->lo_number; 1389863d5b82SDavid Howells info.lo_device = info64->lo_device; 1390863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1391863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1392863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1393863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1394863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1395863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1396863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1397863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1398863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1399863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1400863d5b82SDavid Howells else 1401863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1402863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1403863d5b82SDavid Howells 1404863d5b82SDavid Howells /* error in case values were truncated */ 1405863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1406863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1407863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1408863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1409863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1410863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1411863d5b82SDavid Howells return -EOVERFLOW; 1412863d5b82SDavid Howells 1413863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1414863d5b82SDavid Howells return -EFAULT; 1415863d5b82SDavid Howells return 0; 1416863d5b82SDavid Howells } 1417863d5b82SDavid Howells 1418863d5b82SDavid Howells static int 1419863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1420863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1421863d5b82SDavid Howells { 1422863d5b82SDavid Howells struct loop_info64 info64; 1423863d5b82SDavid Howells int ret; 1424863d5b82SDavid Howells 1425863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1426863d5b82SDavid Howells if (ret < 0) 1427863d5b82SDavid Howells return ret; 1428863d5b82SDavid Howells return loop_set_status(lo, &info64); 1429863d5b82SDavid Howells } 1430863d5b82SDavid Howells 1431863d5b82SDavid Howells static int 1432863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1433863d5b82SDavid Howells struct compat_loop_info __user *arg) 1434863d5b82SDavid Howells { 1435863d5b82SDavid Howells struct loop_info64 info64; 1436863d5b82SDavid Howells int err = 0; 1437863d5b82SDavid Howells 1438863d5b82SDavid Howells if (!arg) 1439863d5b82SDavid Howells err = -EINVAL; 1440863d5b82SDavid Howells if (!err) 1441863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1442863d5b82SDavid Howells if (!err) 1443863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1444863d5b82SDavid Howells return err; 1445863d5b82SDavid Howells } 1446863d5b82SDavid Howells 1447bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1448bb214884SAl Viro unsigned int cmd, unsigned long arg) 1449863d5b82SDavid Howells { 1450bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1451863d5b82SDavid Howells int err; 1452863d5b82SDavid Howells 1453863d5b82SDavid Howells switch(cmd) { 1454863d5b82SDavid Howells case LOOP_SET_STATUS: 1455863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1456863d5b82SDavid Howells err = loop_set_status_compat( 1457863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1458863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1459863d5b82SDavid Howells break; 1460863d5b82SDavid Howells case LOOP_GET_STATUS: 1461863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1462863d5b82SDavid Howells err = loop_get_status_compat( 1463863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1464863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1465863d5b82SDavid Howells break; 146653d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1467863d5b82SDavid Howells case LOOP_CLR_FD: 1468863d5b82SDavid Howells case LOOP_GET_STATUS64: 1469863d5b82SDavid Howells case LOOP_SET_STATUS64: 1470863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1471863d5b82SDavid Howells case LOOP_SET_FD: 1472863d5b82SDavid Howells case LOOP_CHANGE_FD: 1473bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1474863d5b82SDavid Howells break; 1475863d5b82SDavid Howells default: 1476863d5b82SDavid Howells err = -ENOIOCTLCMD; 1477863d5b82SDavid Howells break; 1478863d5b82SDavid Howells } 1479863d5b82SDavid Howells return err; 1480863d5b82SDavid Howells } 1481863d5b82SDavid Howells #endif 1482863d5b82SDavid Howells 1483bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 14841da177e4SLinus Torvalds { 1485770fe30aSKay Sievers struct loop_device *lo; 1486770fe30aSKay Sievers int err = 0; 1487770fe30aSKay Sievers 1488770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1489770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1490770fe30aSKay Sievers if (!lo) { 1491770fe30aSKay Sievers err = -ENXIO; 1492770fe30aSKay Sievers goto out; 1493770fe30aSKay Sievers } 14941da177e4SLinus Torvalds 1495f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 14961da177e4SLinus Torvalds lo->lo_refcnt++; 1497f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1498770fe30aSKay Sievers out: 1499770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1500770fe30aSKay Sievers return err; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 1503bb214884SAl Viro static int lo_release(struct gendisk *disk, fmode_t mode) 15041da177e4SLinus Torvalds { 1505bb214884SAl Viro struct loop_device *lo = disk->private_data; 1506ffcd7dcaSAlexander Beregalov int err; 15071da177e4SLinus Torvalds 1508f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 150996c58655SDavid Woodhouse 151014f27939SMilan Broz if (--lo->lo_refcnt) 151114f27939SMilan Broz goto out; 151214f27939SMilan Broz 151314f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 151414f27939SMilan Broz /* 151514f27939SMilan Broz * In autoclear mode, stop the loop thread 151614f27939SMilan Broz * and remove configuration after last close. 151714f27939SMilan Broz */ 15184c823cc3SAyan George err = loop_clr_fd(lo); 1519ffcd7dcaSAlexander Beregalov if (!err) 1520ffcd7dcaSAlexander Beregalov goto out_unlocked; 152114f27939SMilan Broz } else { 152214f27939SMilan Broz /* 152314f27939SMilan Broz * Otherwise keep thread (if running) and config, 152414f27939SMilan Broz * but flush possible ongoing bios in thread. 152514f27939SMilan Broz */ 152614f27939SMilan Broz loop_flush(lo); 152714f27939SMilan Broz } 152896c58655SDavid Woodhouse 152914f27939SMilan Broz out: 1530f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1531ffcd7dcaSAlexander Beregalov out_unlocked: 15321da177e4SLinus Torvalds return 0; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 153583d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 15361da177e4SLinus Torvalds .owner = THIS_MODULE, 1537bb214884SAl Viro .open = lo_open, 1538bb214884SAl Viro .release = lo_release, 1539bb214884SAl Viro .ioctl = lo_ioctl, 1540863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1541bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1542863d5b82SDavid Howells #endif 15431da177e4SLinus Torvalds }; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * And now the modules code and kernel interface. 15471da177e4SLinus Torvalds */ 154873285082SKen Chen static int max_loop; 1549ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1550a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1551ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1552476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 15531da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 15541da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 15571da177e4SLinus Torvalds { 15581da177e4SLinus Torvalds unsigned int n = funcs->number; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 15611da177e4SLinus Torvalds return -EINVAL; 15621da177e4SLinus Torvalds xfer_funcs[n] = funcs; 15631da177e4SLinus Torvalds return 0; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 156634dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 156734dd82afSKay Sievers { 156834dd82afSKay Sievers struct loop_device *lo = ptr; 156934dd82afSKay Sievers struct loop_func_table *xfer = data; 157034dd82afSKay Sievers 157134dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 157234dd82afSKay Sievers if (lo->lo_encryption == xfer) 157334dd82afSKay Sievers loop_release_xfer(lo); 157434dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 157534dd82afSKay Sievers return 0; 157634dd82afSKay Sievers } 157734dd82afSKay Sievers 15781da177e4SLinus Torvalds int loop_unregister_transfer(int number) 15791da177e4SLinus Torvalds { 15801da177e4SLinus Torvalds unsigned int n = number; 15811da177e4SLinus Torvalds struct loop_func_table *xfer; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 15841da177e4SLinus Torvalds return -EINVAL; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds xfer_funcs[n] = NULL; 158734dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 15881da177e4SLinus Torvalds return 0; 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 15921da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 15931da177e4SLinus Torvalds 159434dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 15951da177e4SLinus Torvalds { 159673285082SKen Chen struct loop_device *lo; 159773285082SKen Chen struct gendisk *disk; 159834dd82afSKay Sievers int err; 15991da177e4SLinus Torvalds 160034dd82afSKay Sievers err = -ENOMEM; 1601*68d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 1602*68d740d7SSilva Paulo if (!lo) 160373285082SKen Chen goto out; 160434dd82afSKay Sievers 1605*68d740d7SSilva Paulo if (!idr_pre_get(&loop_index_idr, GFP_KERNEL)) 160634dd82afSKay Sievers goto out_free_dev; 160734dd82afSKay Sievers 160834dd82afSKay Sievers if (i >= 0) { 160934dd82afSKay Sievers int m; 161034dd82afSKay Sievers 161134dd82afSKay Sievers /* create specific i in the index */ 161234dd82afSKay Sievers err = idr_get_new_above(&loop_index_idr, lo, i, &m); 161334dd82afSKay Sievers if (err >= 0 && i != m) { 161434dd82afSKay Sievers idr_remove(&loop_index_idr, m); 161534dd82afSKay Sievers err = -EEXIST; 161634dd82afSKay Sievers } 1617770fe30aSKay Sievers } else if (i == -1) { 1618770fe30aSKay Sievers int m; 1619770fe30aSKay Sievers 1620770fe30aSKay Sievers /* get next free nr */ 1621770fe30aSKay Sievers err = idr_get_new(&loop_index_idr, lo, &m); 1622770fe30aSKay Sievers if (err >= 0) 1623770fe30aSKay Sievers i = m; 162434dd82afSKay Sievers } else { 162534dd82afSKay Sievers err = -EINVAL; 162634dd82afSKay Sievers } 162734dd82afSKay Sievers if (err < 0) 162834dd82afSKay Sievers goto out_free_dev; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 16311da177e4SLinus Torvalds if (!lo->lo_queue) 163273285082SKen Chen goto out_free_dev; 163373285082SKen Chen 1634476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 163573285082SKen Chen if (!disk) 163673285082SKen Chen goto out_free_queue; 163773285082SKen Chen 1638e03c8dd1SKay Sievers /* 1639e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1640e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1641e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1642e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1643e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1644e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1645e03c8dd1SKay Sievers * used. 1646e03c8dd1SKay Sievers * 1647e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1648e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1649e03c8dd1SKay Sievers * multiples of max_part. 1650e03c8dd1SKay Sievers * 1651e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1652e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1653e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1654e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1655e03c8dd1SKay Sievers */ 1656e03c8dd1SKay Sievers if (!part_shift) 1657e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1658e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1659f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 16601da177e4SLinus Torvalds lo->lo_number = i; 16616c997918SSerge E. Hallyn lo->lo_thread = NULL; 16626c997918SSerge E. Hallyn init_waitqueue_head(&lo->lo_event); 16631da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 16641da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1665476a4813SLaurent Vivier disk->first_minor = i << part_shift; 16661da177e4SLinus Torvalds disk->fops = &lo_fops; 16671da177e4SLinus Torvalds disk->private_data = lo; 16681da177e4SLinus Torvalds disk->queue = lo->lo_queue; 166973285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 167034dd82afSKay Sievers add_disk(disk); 167134dd82afSKay Sievers *l = lo; 167234dd82afSKay Sievers return lo->lo_number; 167373285082SKen Chen 167473285082SKen Chen out_free_queue: 167573285082SKen Chen blk_cleanup_queue(lo->lo_queue); 167673285082SKen Chen out_free_dev: 167773285082SKen Chen kfree(lo); 167873285082SKen Chen out: 167934dd82afSKay Sievers return err; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 168234dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 168373285082SKen Chen { 168434dd82afSKay Sievers del_gendisk(lo->lo_disk); 168573285082SKen Chen blk_cleanup_queue(lo->lo_queue); 168673285082SKen Chen put_disk(lo->lo_disk); 168773285082SKen Chen kfree(lo); 168873285082SKen Chen } 168973285082SKen Chen 1690770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1691770fe30aSKay Sievers { 1692770fe30aSKay Sievers struct loop_device *lo = ptr; 1693770fe30aSKay Sievers struct loop_device **l = data; 1694770fe30aSKay Sievers 1695770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1696770fe30aSKay Sievers *l = lo; 1697770fe30aSKay Sievers return 1; 1698770fe30aSKay Sievers } 1699770fe30aSKay Sievers return 0; 1700770fe30aSKay Sievers } 1701770fe30aSKay Sievers 170234dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1703a47653fcSKen Chen { 1704a47653fcSKen Chen struct loop_device *lo; 170534dd82afSKay Sievers int ret = -ENODEV; 1706a47653fcSKen Chen 1707770fe30aSKay Sievers if (i < 0) { 1708770fe30aSKay Sievers int err; 1709770fe30aSKay Sievers 1710770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1711770fe30aSKay Sievers if (err == 1) { 1712770fe30aSKay Sievers *l = lo; 1713770fe30aSKay Sievers ret = lo->lo_number; 1714770fe30aSKay Sievers } 1715770fe30aSKay Sievers goto out; 1716a47653fcSKen Chen } 1717a47653fcSKen Chen 1718770fe30aSKay Sievers /* lookup and return a specific i */ 171934dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1720a47653fcSKen Chen if (lo) { 172134dd82afSKay Sievers *l = lo; 172234dd82afSKay Sievers ret = lo->lo_number; 1723a47653fcSKen Chen } 1724770fe30aSKay Sievers out: 172534dd82afSKay Sievers return ret; 1726a47653fcSKen Chen } 1727a47653fcSKen Chen 172873285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 172973285082SKen Chen { 1730705962ccSAl Viro struct loop_device *lo; 173107002e99SAl Viro struct kobject *kobj; 173234dd82afSKay Sievers int err; 173373285082SKen Chen 173434dd82afSKay Sievers mutex_lock(&loop_index_mutex); 173534dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 173634dd82afSKay Sievers if (err < 0) 173734dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 173834dd82afSKay Sievers if (err < 0) 173934dd82afSKay Sievers kobj = ERR_PTR(err); 174034dd82afSKay Sievers else 174134dd82afSKay Sievers kobj = get_disk(lo->lo_disk); 174234dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 174373285082SKen Chen 174473285082SKen Chen *part = 0; 174507002e99SAl Viro return kobj; 174673285082SKen Chen } 174773285082SKen Chen 1748770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1749770fe30aSKay Sievers unsigned long parm) 1750770fe30aSKay Sievers { 1751770fe30aSKay Sievers struct loop_device *lo; 1752770fe30aSKay Sievers int ret = -ENOSYS; 1753770fe30aSKay Sievers 1754770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1755770fe30aSKay Sievers switch (cmd) { 1756770fe30aSKay Sievers case LOOP_CTL_ADD: 1757770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1758770fe30aSKay Sievers if (ret >= 0) { 1759770fe30aSKay Sievers ret = -EEXIST; 1760770fe30aSKay Sievers break; 1761770fe30aSKay Sievers } 1762770fe30aSKay Sievers ret = loop_add(&lo, parm); 1763770fe30aSKay Sievers break; 1764770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1765770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1766770fe30aSKay Sievers if (ret < 0) 1767770fe30aSKay Sievers break; 1768770fe30aSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 1769770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 1770770fe30aSKay Sievers ret = -EBUSY; 1771770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1772770fe30aSKay Sievers break; 1773770fe30aSKay Sievers } 1774770fe30aSKay Sievers if (lo->lo_refcnt > 0) { 1775770fe30aSKay Sievers ret = -EBUSY; 1776770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1777770fe30aSKay Sievers break; 1778770fe30aSKay Sievers } 1779770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 1780770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1781770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 1782770fe30aSKay Sievers loop_remove(lo); 1783770fe30aSKay Sievers break; 1784770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 1785770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 1786770fe30aSKay Sievers if (ret >= 0) 1787770fe30aSKay Sievers break; 1788770fe30aSKay Sievers ret = loop_add(&lo, -1); 1789770fe30aSKay Sievers } 1790770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1791770fe30aSKay Sievers 1792770fe30aSKay Sievers return ret; 1793770fe30aSKay Sievers } 1794770fe30aSKay Sievers 1795770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 1796770fe30aSKay Sievers .open = nonseekable_open, 1797770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 1798770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 1799770fe30aSKay Sievers .owner = THIS_MODULE, 1800770fe30aSKay Sievers .llseek = noop_llseek, 1801770fe30aSKay Sievers }; 1802770fe30aSKay Sievers 1803770fe30aSKay Sievers static struct miscdevice loop_misc = { 1804770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 1805770fe30aSKay Sievers .name = "loop-control", 1806770fe30aSKay Sievers .fops = &loop_ctl_fops, 1807770fe30aSKay Sievers }; 1808770fe30aSKay Sievers 1809770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 1810770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 1811770fe30aSKay Sievers 181273285082SKen Chen static int __init loop_init(void) 181373285082SKen Chen { 1814a47653fcSKen Chen int i, nr; 1815a47653fcSKen Chen unsigned long range; 181634dd82afSKay Sievers struct loop_device *lo; 1817770fe30aSKay Sievers int err; 1818a47653fcSKen Chen 1819770fe30aSKay Sievers err = misc_register(&loop_misc); 1820770fe30aSKay Sievers if (err < 0) 1821770fe30aSKay Sievers return err; 1822476a4813SLaurent Vivier 1823476a4813SLaurent Vivier part_shift = 0; 1824ac04fee0SNamhyung Kim if (max_part > 0) { 1825476a4813SLaurent Vivier part_shift = fls(max_part); 1826476a4813SLaurent Vivier 1827ac04fee0SNamhyung Kim /* 1828ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 1829ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 1830ac04fee0SNamhyung Kim * if [s]he want to create more devices. 1831ac04fee0SNamhyung Kim * 1832ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 1833ac04fee0SNamhyung Kim * for the whole disk. 1834ac04fee0SNamhyung Kim */ 1835ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 1836ac04fee0SNamhyung Kim } 1837ac04fee0SNamhyung Kim 183878f4bb36SNamhyung Kim if ((1UL << part_shift) > DISK_MAX_PARTS) 183978f4bb36SNamhyung Kim return -EINVAL; 184078f4bb36SNamhyung Kim 1841476a4813SLaurent Vivier if (max_loop > 1UL << (MINORBITS - part_shift)) 1842a47653fcSKen Chen return -EINVAL; 184373285082SKen Chen 1844d134b00bSKay Sievers /* 1845d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 1846d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 1847d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 1848d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 1849d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 1850d134b00bSKay Sievers * a 'dead' device node. 1851d134b00bSKay Sievers */ 185273285082SKen Chen if (max_loop) { 1853a47653fcSKen Chen nr = max_loop; 1854a1c15c59SNamhyung Kim range = max_loop << part_shift; 1855a47653fcSKen Chen } else { 1856d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 1857a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 185873285082SKen Chen } 1859a47653fcSKen Chen 1860a47653fcSKen Chen if (register_blkdev(LOOP_MAJOR, "loop")) 1861a47653fcSKen Chen return -EIO; 1862a47653fcSKen Chen 1863a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1864a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 1865a47653fcSKen Chen 1866d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 186734dd82afSKay Sievers mutex_lock(&loop_index_mutex); 186834dd82afSKay Sievers for (i = 0; i < nr; i++) 186934dd82afSKay Sievers loop_add(&lo, i); 187034dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 187134dd82afSKay Sievers 187273285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 18731da177e4SLinus Torvalds return 0; 187434dd82afSKay Sievers } 1875a47653fcSKen Chen 187634dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 187734dd82afSKay Sievers { 187834dd82afSKay Sievers struct loop_device *lo = ptr; 1879a47653fcSKen Chen 188034dd82afSKay Sievers loop_remove(lo); 188134dd82afSKay Sievers return 0; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 188473285082SKen Chen static void __exit loop_exit(void) 18851da177e4SLinus Torvalds { 1886a47653fcSKen Chen unsigned long range; 18871da177e4SLinus Torvalds 1888a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 1889a47653fcSKen Chen 189034dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 189134dd82afSKay Sievers idr_remove_all(&loop_index_idr); 189234dd82afSKay Sievers idr_destroy(&loop_index_idr); 189373285082SKen Chen 1894a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 189500d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 1896770fe30aSKay Sievers 1897770fe30aSKay Sievers misc_deregister(&loop_misc); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds module_init(loop_init); 19011da177e4SLinus Torvalds module_exit(loop_exit); 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds #ifndef MODULE 19041da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 19051da177e4SLinus Torvalds { 19061da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 19071da177e4SLinus Torvalds return 1; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 19111da177e4SLinus Torvalds #endif 1912