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; 1937b0576a3SGuo Chao struct block_device *bdev = lo->lo_device; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds if (unlikely((loff_t)x != size)) 1961da177e4SLinus Torvalds return -EFBIG; 1977035b5dfSDmitry Monakhov if (lo->lo_offset != offset) 1987035b5dfSDmitry Monakhov lo->lo_offset = offset; 1997035b5dfSDmitry Monakhov if (lo->lo_sizelimit != sizelimit) 2007035b5dfSDmitry Monakhov lo->lo_sizelimit = sizelimit; 20173285082SKen Chen set_capacity(lo->lo_disk, x); 2027b0576a3SGuo Chao bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 2037b0576a3SGuo Chao /* let user-space know about the new size */ 2047b0576a3SGuo Chao kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 2051da177e4SLinus Torvalds return 0; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds static inline int 2091da177e4SLinus Torvalds lo_do_transfer(struct loop_device *lo, int cmd, 2101da177e4SLinus Torvalds struct page *rpage, unsigned roffs, 2111da177e4SLinus Torvalds struct page *lpage, unsigned loffs, 2121da177e4SLinus Torvalds int size, sector_t rblock) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds if (unlikely(!lo->transfer)) 2151da177e4SLinus Torvalds return 0; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /** 2211da177e4SLinus Torvalds * __do_lo_send_write - helper for writing data to a loop device 2221da177e4SLinus Torvalds * 2231da177e4SLinus Torvalds * This helper just factors out common code between do_lo_send_direct_write() 2241da177e4SLinus Torvalds * and do_lo_send_write(). 2251da177e4SLinus Torvalds */ 226858119e1SArjan van de Ven static int __do_lo_send_write(struct file *file, 22798ae6ccdSAl Viro u8 *buf, const int len, loff_t pos) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds ssize_t bw; 2301da177e4SLinus Torvalds mm_segment_t old_fs = get_fs(); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds set_fs(get_ds()); 2331da177e4SLinus Torvalds bw = file->f_op->write(file, buf, len, &pos); 2341da177e4SLinus Torvalds set_fs(old_fs); 2351da177e4SLinus Torvalds if (likely(bw == len)) 2361da177e4SLinus Torvalds return 0; 2371da177e4SLinus Torvalds printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 2381da177e4SLinus Torvalds (unsigned long long)pos, len); 2391da177e4SLinus Torvalds if (bw >= 0) 2401da177e4SLinus Torvalds bw = -EIO; 2411da177e4SLinus Torvalds return bw; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds /** 2451da177e4SLinus Torvalds * do_lo_send_direct_write - helper for writing data to a loop device 2461da177e4SLinus Torvalds * 247456be148SChristoph Hellwig * This is the fast, non-transforming version that does not need double 248456be148SChristoph Hellwig * buffering. 2491da177e4SLinus Torvalds */ 2501da177e4SLinus Torvalds static int do_lo_send_direct_write(struct loop_device *lo, 251511de73fSAl Viro struct bio_vec *bvec, loff_t pos, struct page *page) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 25498ae6ccdSAl Viro kmap(bvec->bv_page) + bvec->bv_offset, 2551da177e4SLinus Torvalds bvec->bv_len, pos); 2561da177e4SLinus Torvalds kunmap(bvec->bv_page); 2571da177e4SLinus Torvalds cond_resched(); 2581da177e4SLinus Torvalds return bw; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /** 2621da177e4SLinus Torvalds * do_lo_send_write - helper for writing data to a loop device 2631da177e4SLinus Torvalds * 264456be148SChristoph Hellwig * This is the slow, transforming version that needs to double buffer the 265456be148SChristoph Hellwig * data as it cannot do the transformations in place without having direct 266456be148SChristoph Hellwig * access to the destination pages of the backing file. 2671da177e4SLinus Torvalds */ 2681da177e4SLinus Torvalds static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 269511de73fSAl Viro loff_t pos, struct page *page) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 2721da177e4SLinus Torvalds bvec->bv_offset, bvec->bv_len, pos >> 9); 2731da177e4SLinus Torvalds if (likely(!ret)) 2741da177e4SLinus Torvalds return __do_lo_send_write(lo->lo_backing_file, 27598ae6ccdSAl Viro page_address(page), bvec->bv_len, 2761da177e4SLinus Torvalds pos); 2771da177e4SLinus Torvalds printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 2781da177e4SLinus Torvalds "length %i.\n", (unsigned long long)pos, bvec->bv_len); 2791da177e4SLinus Torvalds if (ret > 0) 2801da177e4SLinus Torvalds ret = -EIO; 2811da177e4SLinus Torvalds return ret; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 284511de73fSAl Viro static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) 2851da177e4SLinus Torvalds { 286511de73fSAl Viro int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, 2871da177e4SLinus Torvalds struct page *page); 2881da177e4SLinus Torvalds struct bio_vec *bvec; 2891da177e4SLinus Torvalds struct page *page = NULL; 2901da177e4SLinus Torvalds int i, ret = 0; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds if (lo->transfer != transfer_none) { 2931da177e4SLinus Torvalds page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 2941da177e4SLinus Torvalds if (unlikely(!page)) 2951da177e4SLinus Torvalds goto fail; 2961da177e4SLinus Torvalds kmap(page); 2971da177e4SLinus Torvalds do_lo_send = do_lo_send_write; 298456be148SChristoph Hellwig } else { 299456be148SChristoph Hellwig do_lo_send = do_lo_send_direct_write; 3001da177e4SLinus Torvalds } 301456be148SChristoph Hellwig 3021da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 303511de73fSAl Viro ret = do_lo_send(lo, bvec, pos, page); 3041da177e4SLinus Torvalds if (ret < 0) 3051da177e4SLinus Torvalds break; 3061da177e4SLinus Torvalds pos += bvec->bv_len; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds if (page) { 3091da177e4SLinus Torvalds kunmap(page); 3101da177e4SLinus Torvalds __free_page(page); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds out: 3131da177e4SLinus Torvalds return ret; 3141da177e4SLinus Torvalds fail: 3151da177e4SLinus Torvalds printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 3161da177e4SLinus Torvalds ret = -ENOMEM; 3171da177e4SLinus Torvalds goto out; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds struct lo_read_data { 3211da177e4SLinus Torvalds struct loop_device *lo; 3221da177e4SLinus Torvalds struct page *page; 3231da177e4SLinus Torvalds unsigned offset; 3241da177e4SLinus Torvalds int bsize; 3251da177e4SLinus Torvalds }; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds static int 328fd582140SJens Axboe lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 329fd582140SJens Axboe struct splice_desc *sd) 3301da177e4SLinus Torvalds { 331fd582140SJens Axboe struct lo_read_data *p = sd->u.data; 3321da177e4SLinus Torvalds struct loop_device *lo = p->lo; 333fd582140SJens Axboe struct page *page = buf->page; 3341da177e4SLinus Torvalds sector_t IV; 3353603b8eaSJens Axboe int size; 3361da177e4SLinus Torvalds 337fd582140SJens Axboe IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + 338fd582140SJens Axboe (buf->offset >> 9); 339fd582140SJens Axboe size = sd->len; 340fd582140SJens Axboe if (size > p->bsize) 341fd582140SJens Axboe size = p->bsize; 3421da177e4SLinus Torvalds 343fd582140SJens Axboe if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { 3441da177e4SLinus Torvalds printk(KERN_ERR "loop: transfer error block %ld\n", 3451da177e4SLinus Torvalds page->index); 346fd582140SJens Axboe size = -EINVAL; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds flush_dcache_page(p->page); 3501da177e4SLinus Torvalds 351fd582140SJens Axboe if (size > 0) 3521da177e4SLinus Torvalds p->offset += size; 353fd582140SJens Axboe 3541da177e4SLinus Torvalds return size; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds static int 358fd582140SJens Axboe lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) 359fd582140SJens Axboe { 360fd582140SJens Axboe return __splice_from_pipe(pipe, sd, lo_splice_actor); 361fd582140SJens Axboe } 362fd582140SJens Axboe 363306df071SDave Young static ssize_t 3641da177e4SLinus Torvalds do_lo_receive(struct loop_device *lo, 3651da177e4SLinus Torvalds struct bio_vec *bvec, int bsize, loff_t pos) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct lo_read_data cookie; 368fd582140SJens Axboe struct splice_desc sd; 3691da177e4SLinus Torvalds struct file *file; 370306df071SDave Young ssize_t retval; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds cookie.lo = lo; 3731da177e4SLinus Torvalds cookie.page = bvec->bv_page; 3741da177e4SLinus Torvalds cookie.offset = bvec->bv_offset; 3751da177e4SLinus Torvalds cookie.bsize = bsize; 376fd582140SJens Axboe 377fd582140SJens Axboe sd.len = 0; 378fd582140SJens Axboe sd.total_len = bvec->bv_len; 379fd582140SJens Axboe sd.flags = 0; 380fd582140SJens Axboe sd.pos = pos; 381fd582140SJens Axboe sd.u.data = &cookie; 382fd582140SJens Axboe 3831da177e4SLinus Torvalds file = lo->lo_backing_file; 384fd582140SJens Axboe retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); 385fd582140SJens Axboe 386fd582140SJens Axboe return retval; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static int 3901da177e4SLinus Torvalds lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct bio_vec *bvec; 393306df071SDave Young ssize_t s; 394306df071SDave Young int i; 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds bio_for_each_segment(bvec, bio, i) { 397306df071SDave Young s = do_lo_receive(lo, bvec, bsize, pos); 398306df071SDave Young if (s < 0) 399306df071SDave Young return s; 400306df071SDave Young 401306df071SDave Young if (s != bvec->bv_len) { 402306df071SDave Young zero_fill_bio(bio); 4031da177e4SLinus Torvalds break; 404306df071SDave Young } 4051da177e4SLinus Torvalds pos += bvec->bv_len; 4061da177e4SLinus Torvalds } 407306df071SDave Young return 0; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds loff_t pos; 4131da177e4SLinus Torvalds int ret; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 41668db1961SNikanth Karthikesan 41768db1961SNikanth Karthikesan if (bio_rw(bio) == WRITE) { 41868db1961SNikanth Karthikesan struct file *file = lo->lo_backing_file; 41968db1961SNikanth Karthikesan 4206259f284STejun Heo if (bio->bi_rw & REQ_FLUSH) { 4218018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 4226259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) { 42368db1961SNikanth Karthikesan ret = -EIO; 42468db1961SNikanth Karthikesan goto out; 42568db1961SNikanth Karthikesan } 42668db1961SNikanth Karthikesan } 42768db1961SNikanth Karthikesan 428dfaa2ef6SLukas Czerner /* 429dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 430ae95757aSDave Young * image a.k.a. discard. However we do not support discard if 431dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 432dfaa2ef6SLukas Czerner * useful information. 433dfaa2ef6SLukas Czerner */ 434dfaa2ef6SLukas Czerner if (bio->bi_rw & REQ_DISCARD) { 435dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 436dfaa2ef6SLukas Czerner int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 437dfaa2ef6SLukas Czerner 438dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 439dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 440dfaa2ef6SLukas Czerner ret = -EOPNOTSUPP; 441dfaa2ef6SLukas Czerner goto out; 442dfaa2ef6SLukas Czerner } 443dfaa2ef6SLukas Czerner ret = file->f_op->fallocate(file, mode, pos, 444dfaa2ef6SLukas Czerner bio->bi_size); 445dfaa2ef6SLukas Czerner if (unlikely(ret && ret != -EINVAL && 446dfaa2ef6SLukas Czerner ret != -EOPNOTSUPP)) 447dfaa2ef6SLukas Czerner ret = -EIO; 448dfaa2ef6SLukas Czerner goto out; 449dfaa2ef6SLukas Czerner } 450dfaa2ef6SLukas Czerner 451511de73fSAl Viro ret = lo_send(lo, bio, pos); 45268db1961SNikanth Karthikesan 4536259f284STejun Heo if ((bio->bi_rw & REQ_FUA) && !ret) { 4548018ab05SChristoph Hellwig ret = vfs_fsync(file, 0); 4556259f284STejun Heo if (unlikely(ret && ret != -EINVAL)) 45668db1961SNikanth Karthikesan ret = -EIO; 45768db1961SNikanth Karthikesan } 45868db1961SNikanth Karthikesan } else 4591da177e4SLinus Torvalds ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 46068db1961SNikanth Karthikesan 46168db1961SNikanth Karthikesan out: 4621da177e4SLinus Torvalds return ret; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* 4661da177e4SLinus Torvalds * Add bio to back of pending list 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds static void loop_add_bio(struct loop_device *lo, struct bio *bio) 4691da177e4SLinus Torvalds { 4707b5a3522SLukas Czerner lo->lo_bio_count++; 471e686307fSAkinobu Mita bio_list_add(&lo->lo_bio_list, bio); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 4751da177e4SLinus Torvalds * Grab first pending buffer 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds static struct bio *loop_get_bio(struct loop_device *lo) 4781da177e4SLinus Torvalds { 4797b5a3522SLukas Czerner lo->lo_bio_count--; 480e686307fSAkinobu Mita return bio_list_pop(&lo->lo_bio_list); 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4835a7bbad2SChristoph Hellwig static void loop_make_request(struct request_queue *q, struct bio *old_bio) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds struct loop_device *lo = q->queuedata; 4861da177e4SLinus Torvalds int rw = bio_rw(old_bio); 4871da177e4SLinus Torvalds 48835a82d1aSNick Piggin if (rw == READA) 48935a82d1aSNick Piggin rw = READ; 49035a82d1aSNick Piggin 49135a82d1aSNick Piggin BUG_ON(!lo || (rw != READ && rw != WRITE)); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 4941da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 49535a82d1aSNick Piggin goto out; 49635a82d1aSNick Piggin if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 49735a82d1aSNick Piggin goto out; 4987b5a3522SLukas Czerner if (lo->lo_bio_count >= q->nr_congestion_on) 4997b5a3522SLukas Czerner wait_event_lock_irq(lo->lo_req_wait, 5007b5a3522SLukas Czerner lo->lo_bio_count < q->nr_congestion_off, 5017b5a3522SLukas Czerner lo->lo_lock); 5021da177e4SLinus Torvalds loop_add_bio(lo, old_bio); 5036c997918SSerge E. Hallyn wake_up(&lo->lo_event); 50435a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 5055a7bbad2SChristoph Hellwig return; 50635a82d1aSNick Piggin 5071da177e4SLinus Torvalds out: 50835a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 5096712ecf8SNeilBrown bio_io_error(old_bio); 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds struct switch_request { 5131da177e4SLinus Torvalds struct file *file; 5141da177e4SLinus Torvalds struct completion wait; 5151da177e4SLinus Torvalds }; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *, struct switch_request *); 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 5201da177e4SLinus Torvalds { 5211da177e4SLinus Torvalds if (unlikely(!bio->bi_bdev)) { 5221da177e4SLinus Torvalds do_loop_switch(lo, bio->bi_private); 5231da177e4SLinus Torvalds bio_put(bio); 5241da177e4SLinus Torvalds } else { 52535a82d1aSNick Piggin int ret = do_bio_filebacked(lo, bio); 5266712ecf8SNeilBrown bio_endio(bio, ret); 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * worker thread that handles reads/writes to file backed loop devices, 5321da177e4SLinus Torvalds * to avoid blocking in our make_request_fn. it also does loop decrypting 5331da177e4SLinus Torvalds * on reads for block backed loop, as that is too heavy to do from 5341da177e4SLinus Torvalds * b_end_io context where irqs may be disabled. 5356c997918SSerge E. Hallyn * 5366c997918SSerge E. Hallyn * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before 5376c997918SSerge E. Hallyn * calling kthread_stop(). Therefore once kthread_should_stop() is 5386c997918SSerge E. Hallyn * true, make_request will not place any more requests. Therefore 5396c997918SSerge E. Hallyn * once kthread_should_stop() is true and lo_bio is NULL, we are 5406c997918SSerge E. Hallyn * done with the loop. 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds static int loop_thread(void *data) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct loop_device *lo = data; 5451da177e4SLinus Torvalds struct bio *bio; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds set_user_nice(current, -20); 5481da177e4SLinus Torvalds 549e686307fSAkinobu Mita while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) { 5501da177e4SLinus Torvalds 5516c997918SSerge E. Hallyn wait_event_interruptible(lo->lo_event, 552e686307fSAkinobu Mita !bio_list_empty(&lo->lo_bio_list) || 553e686307fSAkinobu Mita kthread_should_stop()); 55409c0dc68SLinus Torvalds 555e686307fSAkinobu Mita if (bio_list_empty(&lo->lo_bio_list)) 55635a82d1aSNick Piggin continue; 55735a82d1aSNick Piggin spin_lock_irq(&lo->lo_lock); 5581da177e4SLinus Torvalds bio = loop_get_bio(lo); 5597b5a3522SLukas Czerner if (lo->lo_bio_count < lo->lo_queue->nr_congestion_off) 5607b5a3522SLukas Czerner wake_up(&lo->lo_req_wait); 56135a82d1aSNick Piggin spin_unlock_irq(&lo->lo_lock); 56235a82d1aSNick Piggin 56335a82d1aSNick Piggin BUG_ON(!bio); 5641da177e4SLinus Torvalds loop_handle_bio(lo, bio); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds return 0; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* 5711da177e4SLinus Torvalds * loop_switch performs the hard work of switching a backing store. 5721da177e4SLinus Torvalds * First it needs to flush existing IO, it does this by sending a magic 5731da177e4SLinus Torvalds * BIO down the pipe. The completion of this BIO does the actual switch. 5741da177e4SLinus Torvalds */ 5751da177e4SLinus Torvalds static int loop_switch(struct loop_device *lo, struct file *file) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds struct switch_request w; 578a24eab1eSJens Axboe struct bio *bio = bio_alloc(GFP_KERNEL, 0); 5791da177e4SLinus Torvalds if (!bio) 5801da177e4SLinus Torvalds return -ENOMEM; 5811da177e4SLinus Torvalds init_completion(&w.wait); 5821da177e4SLinus Torvalds w.file = file; 5831da177e4SLinus Torvalds bio->bi_private = &w; 5841da177e4SLinus Torvalds bio->bi_bdev = NULL; 5851da177e4SLinus Torvalds loop_make_request(lo->lo_queue, bio); 5861da177e4SLinus Torvalds wait_for_completion(&w.wait); 5871da177e4SLinus Torvalds return 0; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* 59114f27939SMilan Broz * Helper to flush the IOs in loop, but keeping loop thread running 59214f27939SMilan Broz */ 59314f27939SMilan Broz static int loop_flush(struct loop_device *lo) 59414f27939SMilan Broz { 59514f27939SMilan Broz /* loop not yet configured, no running thread, nothing to flush */ 59614f27939SMilan Broz if (!lo->lo_thread) 59714f27939SMilan Broz return 0; 59814f27939SMilan Broz 59914f27939SMilan Broz return loop_switch(lo, NULL); 60014f27939SMilan Broz } 60114f27939SMilan Broz 60214f27939SMilan Broz /* 6031da177e4SLinus Torvalds * Do the actual switch; called from the BIO completion routine 6041da177e4SLinus Torvalds */ 6051da177e4SLinus Torvalds static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 6061da177e4SLinus Torvalds { 6071da177e4SLinus Torvalds struct file *file = p->file; 6081da177e4SLinus Torvalds struct file *old_file = lo->lo_backing_file; 60914f27939SMilan Broz struct address_space *mapping; 6101da177e4SLinus Torvalds 61114f27939SMilan Broz /* if no new file, only flush of queued bios requested */ 61214f27939SMilan Broz if (!file) 61314f27939SMilan Broz goto out; 61414f27939SMilan Broz 61514f27939SMilan Broz mapping = file->f_mapping; 6161da177e4SLinus Torvalds mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 6171da177e4SLinus Torvalds lo->lo_backing_file = file; 618ba52de12STheodore Ts'o lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 619ba52de12STheodore Ts'o mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 6201da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 6211da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 62214f27939SMilan Broz out: 6231da177e4SLinus Torvalds complete(&p->wait); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /* 6281da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to 6291da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up 6301da177e4SLinus Torvalds * the original file and in High Availability environments to switch to 6311da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown. 6321da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the 6331da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store. 6341da177e4SLinus Torvalds */ 635bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 636bb214884SAl Viro unsigned int arg) 6371da177e4SLinus Torvalds { 6381da177e4SLinus Torvalds struct file *file, *old_file; 6391da177e4SLinus Torvalds struct inode *inode; 6401da177e4SLinus Torvalds int error; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds error = -ENXIO; 6431da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 6441da177e4SLinus Torvalds goto out; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* the loop device has to be read-only */ 6471da177e4SLinus Torvalds error = -EINVAL; 6481da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 6491da177e4SLinus Torvalds goto out; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds error = -EBADF; 6521da177e4SLinus Torvalds file = fget(arg); 6531da177e4SLinus Torvalds if (!file) 6541da177e4SLinus Torvalds goto out; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds inode = file->f_mapping->host; 6571da177e4SLinus Torvalds old_file = lo->lo_backing_file; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds error = -EINVAL; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 6621da177e4SLinus Torvalds goto out_putf; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* size of the new backing store needs to be the same */ 6651da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 6661da177e4SLinus Torvalds goto out_putf; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* and ... switch */ 6691da177e4SLinus Torvalds error = loop_switch(lo, file); 6701da177e4SLinus Torvalds if (error) 6711da177e4SLinus Torvalds goto out_putf; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds fput(old_file); 674e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 675476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 6761da177e4SLinus Torvalds return 0; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds out_putf: 6791da177e4SLinus Torvalds fput(file); 6801da177e4SLinus Torvalds out: 6811da177e4SLinus Torvalds return error; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds static inline int is_loop_device(struct file *file) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds struct inode *i = file->f_mapping->host; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 691ee862730SMilan Broz /* loop sysfs attributes */ 692ee862730SMilan Broz 693ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page, 694ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *)) 695ee862730SMilan Broz { 69634dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev); 69734dd82afSKay Sievers struct loop_device *lo = disk->private_data; 698ee862730SMilan Broz 69934dd82afSKay Sievers return callback(lo, page); 700ee862730SMilan Broz } 701ee862730SMilan Broz 702ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \ 703ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 704ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \ 705ee862730SMilan Broz struct device_attribute *attr, char *b) \ 706ee862730SMilan Broz { \ 707ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \ 708ee862730SMilan Broz } \ 709ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \ 710ee862730SMilan Broz __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 711ee862730SMilan Broz 712ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 713ee862730SMilan Broz { 714ee862730SMilan Broz ssize_t ret; 715ee862730SMilan Broz char *p = NULL; 716ee862730SMilan Broz 71705eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 718ee862730SMilan Broz if (lo->lo_backing_file) 719ee862730SMilan Broz p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1); 72005eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 721ee862730SMilan Broz 722ee862730SMilan Broz if (IS_ERR_OR_NULL(p)) 723ee862730SMilan Broz ret = PTR_ERR(p); 724ee862730SMilan Broz else { 725ee862730SMilan Broz ret = strlen(p); 726ee862730SMilan Broz memmove(buf, p, ret); 727ee862730SMilan Broz buf[ret++] = '\n'; 728ee862730SMilan Broz buf[ret] = 0; 729ee862730SMilan Broz } 730ee862730SMilan Broz 731ee862730SMilan Broz return ret; 732ee862730SMilan Broz } 733ee862730SMilan Broz 734ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 735ee862730SMilan Broz { 736ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 737ee862730SMilan Broz } 738ee862730SMilan Broz 739ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 740ee862730SMilan Broz { 741ee862730SMilan Broz return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 742ee862730SMilan Broz } 743ee862730SMilan Broz 744ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 745ee862730SMilan Broz { 746ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 747ee862730SMilan Broz 748ee862730SMilan Broz return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 749ee862730SMilan Broz } 750ee862730SMilan Broz 751e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 752e03c8dd1SKay Sievers { 753e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 754e03c8dd1SKay Sievers 755e03c8dd1SKay Sievers return sprintf(buf, "%s\n", partscan ? "1" : "0"); 756e03c8dd1SKay Sievers } 757e03c8dd1SKay Sievers 758ee862730SMilan Broz LOOP_ATTR_RO(backing_file); 759ee862730SMilan Broz LOOP_ATTR_RO(offset); 760ee862730SMilan Broz LOOP_ATTR_RO(sizelimit); 761ee862730SMilan Broz LOOP_ATTR_RO(autoclear); 762e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan); 763ee862730SMilan Broz 764ee862730SMilan Broz static struct attribute *loop_attrs[] = { 765ee862730SMilan Broz &loop_attr_backing_file.attr, 766ee862730SMilan Broz &loop_attr_offset.attr, 767ee862730SMilan Broz &loop_attr_sizelimit.attr, 768ee862730SMilan Broz &loop_attr_autoclear.attr, 769e03c8dd1SKay Sievers &loop_attr_partscan.attr, 770ee862730SMilan Broz NULL, 771ee862730SMilan Broz }; 772ee862730SMilan Broz 773ee862730SMilan Broz static struct attribute_group loop_attribute_group = { 774ee862730SMilan Broz .name = "loop", 775ee862730SMilan Broz .attrs= loop_attrs, 776ee862730SMilan Broz }; 777ee862730SMilan Broz 778ee862730SMilan Broz static int loop_sysfs_init(struct loop_device *lo) 779ee862730SMilan Broz { 780ee862730SMilan Broz return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 781ee862730SMilan Broz &loop_attribute_group); 782ee862730SMilan Broz } 783ee862730SMilan Broz 784ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo) 785ee862730SMilan Broz { 786ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 787ee862730SMilan Broz &loop_attribute_group); 788ee862730SMilan Broz } 789ee862730SMilan Broz 790dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo) 791dfaa2ef6SLukas Czerner { 792dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file; 793dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host; 794dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue; 795dfaa2ef6SLukas Czerner 796dfaa2ef6SLukas Czerner /* 797dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the 798dfaa2ef6SLukas Czerner * image a.k.a. discard. However we do support discard if 799dfaa2ef6SLukas Czerner * encryption is enabled, because it may give an attacker 800dfaa2ef6SLukas Czerner * useful information. 801dfaa2ef6SLukas Czerner */ 802dfaa2ef6SLukas Czerner if ((!file->f_op->fallocate) || 803dfaa2ef6SLukas Czerner lo->lo_encrypt_key_size) { 804dfaa2ef6SLukas Czerner q->limits.discard_granularity = 0; 805dfaa2ef6SLukas Czerner q->limits.discard_alignment = 0; 806dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = 0; 807dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 0; 808dfaa2ef6SLukas Czerner queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); 809dfaa2ef6SLukas Czerner return; 810dfaa2ef6SLukas Czerner } 811dfaa2ef6SLukas Czerner 812dfaa2ef6SLukas Czerner q->limits.discard_granularity = inode->i_sb->s_blocksize; 813dfaf3c03SLukas Czerner q->limits.discard_alignment = 0; 814dfaa2ef6SLukas Czerner q->limits.max_discard_sectors = UINT_MAX >> 9; 815dfaa2ef6SLukas Czerner q->limits.discard_zeroes_data = 1; 816dfaa2ef6SLukas Czerner queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); 817dfaa2ef6SLukas Czerner } 818dfaa2ef6SLukas Czerner 819bb214884SAl Viro static int loop_set_fd(struct loop_device *lo, fmode_t mode, 8201da177e4SLinus Torvalds struct block_device *bdev, unsigned int arg) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds struct file *file, *f; 8231da177e4SLinus Torvalds struct inode *inode; 8241da177e4SLinus Torvalds struct address_space *mapping; 8251da177e4SLinus Torvalds unsigned lo_blocksize; 8261da177e4SLinus Torvalds int lo_flags = 0; 8271da177e4SLinus Torvalds int error; 8281da177e4SLinus Torvalds loff_t size; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */ 8311da177e4SLinus Torvalds __module_get(THIS_MODULE); 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds error = -EBADF; 8341da177e4SLinus Torvalds file = fget(arg); 8351da177e4SLinus Torvalds if (!file) 8361da177e4SLinus Torvalds goto out; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds error = -EBUSY; 8391da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound) 8401da177e4SLinus Torvalds goto out_putf; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /* Avoid recursion */ 8431da177e4SLinus Torvalds f = file; 8441da177e4SLinus Torvalds while (is_loop_device(f)) { 8451da177e4SLinus Torvalds struct loop_device *l; 8461da177e4SLinus Torvalds 847bb214884SAl Viro if (f->f_mapping->host->i_bdev == bdev) 8481da177e4SLinus Torvalds goto out_putf; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds l = f->f_mapping->host->i_bdev->bd_disk->private_data; 8511da177e4SLinus Torvalds if (l->lo_state == Lo_unbound) { 8521da177e4SLinus Torvalds error = -EINVAL; 8531da177e4SLinus Torvalds goto out_putf; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds f = l->lo_backing_file; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds mapping = file->f_mapping; 8591da177e4SLinus Torvalds inode = mapping->host; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds error = -EINVAL; 862456be148SChristoph Hellwig if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 863456be148SChristoph Hellwig goto out_putf; 8646818173bSMiklos Szeredi 865456be148SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 866456be148SChristoph Hellwig !file->f_op->write) 8671da177e4SLinus Torvalds lo_flags |= LO_FLAGS_READ_ONLY; 8681da177e4SLinus Torvalds 869ba52de12STheodore Ts'o lo_blocksize = S_ISBLK(inode->i_mode) ? 870ba52de12STheodore Ts'o inode->i_bdev->bd_block_size : PAGE_SIZE; 871ba52de12STheodore Ts'o 8721da177e4SLinus Torvalds error = -EFBIG; 873456be148SChristoph Hellwig size = get_loop_size(lo, file); 874456be148SChristoph Hellwig if ((loff_t)(sector_t)size != size) 8751da177e4SLinus Torvalds goto out_putf; 8761da177e4SLinus Torvalds 877456be148SChristoph Hellwig error = 0; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds lo->lo_blocksize = lo_blocksize; 8821da177e4SLinus Torvalds lo->lo_device = bdev; 8831da177e4SLinus Torvalds lo->lo_flags = lo_flags; 8841da177e4SLinus Torvalds lo->lo_backing_file = file; 885eefe85eeSConstantine Sapuntzakis lo->transfer = transfer_none; 8861da177e4SLinus Torvalds lo->ioctl = NULL; 8871da177e4SLinus Torvalds lo->lo_sizelimit = 0; 8887b5a3522SLukas Czerner lo->lo_bio_count = 0; 8891da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping); 8901da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 8911da177e4SLinus Torvalds 892e686307fSAkinobu Mita bio_list_init(&lo->lo_bio_list); 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* 8951da177e4SLinus Torvalds * set queue make_request_fn, and add limits based on lower level 8961da177e4SLinus Torvalds * device 8971da177e4SLinus Torvalds */ 8981da177e4SLinus Torvalds blk_queue_make_request(lo->lo_queue, loop_make_request); 8991da177e4SLinus Torvalds lo->lo_queue->queuedata = lo; 9001da177e4SLinus Torvalds 90168db1961SNikanth Karthikesan if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 9024913efe4STejun Heo blk_queue_flush(lo->lo_queue, REQ_FLUSH); 90368db1961SNikanth Karthikesan 90473285082SKen Chen set_capacity(lo->lo_disk, size); 9051da177e4SLinus Torvalds bd_set_size(bdev, size << 9); 906ee862730SMilan Broz loop_sysfs_init(lo); 907c3473c63SDavid Zeuthen /* let user-space know about the new size */ 908c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds set_blocksize(bdev, lo_blocksize); 9111da177e4SLinus Torvalds 9126c997918SSerge E. Hallyn lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 9136c997918SSerge E. Hallyn lo->lo_number); 9146c997918SSerge E. Hallyn if (IS_ERR(lo->lo_thread)) { 9156c997918SSerge E. Hallyn error = PTR_ERR(lo->lo_thread); 916a7422bf8SSerge E. Hallyn goto out_clr; 9176c997918SSerge E. Hallyn } 9186c997918SSerge E. Hallyn lo->lo_state = Lo_bound; 9196c997918SSerge E. Hallyn wake_up_process(lo->lo_thread); 920e03c8dd1SKay Sievers if (part_shift) 921e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 922e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN) 923476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 9241da177e4SLinus Torvalds return 0; 9251da177e4SLinus Torvalds 926a7422bf8SSerge E. Hallyn out_clr: 927ee862730SMilan Broz loop_sysfs_exit(lo); 928a7422bf8SSerge E. Hallyn lo->lo_thread = NULL; 929a7422bf8SSerge E. Hallyn lo->lo_device = NULL; 930a7422bf8SSerge E. Hallyn lo->lo_backing_file = NULL; 931a7422bf8SSerge E. Hallyn lo->lo_flags = 0; 93273285082SKen Chen set_capacity(lo->lo_disk, 0); 933f98393a6SPeter Zijlstra invalidate_bdev(bdev); 934a7422bf8SSerge E. Hallyn bd_set_size(bdev, 0); 935c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 936a7422bf8SSerge E. Hallyn mapping_set_gfp_mask(mapping, lo->old_gfp_mask); 937a7422bf8SSerge E. Hallyn lo->lo_state = Lo_unbound; 9381da177e4SLinus Torvalds out_putf: 9391da177e4SLinus Torvalds fput(file); 9401da177e4SLinus Torvalds out: 9411da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 9421da177e4SLinus Torvalds module_put(THIS_MODULE); 9431da177e4SLinus Torvalds return error; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds static int 9471da177e4SLinus Torvalds loop_release_xfer(struct loop_device *lo) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds int err = 0; 9501da177e4SLinus Torvalds struct loop_func_table *xfer = lo->lo_encryption; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds if (xfer) { 9531da177e4SLinus Torvalds if (xfer->release) 9541da177e4SLinus Torvalds err = xfer->release(lo); 9551da177e4SLinus Torvalds lo->transfer = NULL; 9561da177e4SLinus Torvalds lo->lo_encryption = NULL; 9571da177e4SLinus Torvalds module_put(xfer->owner); 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds return err; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static int 9631da177e4SLinus Torvalds loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 9641da177e4SLinus Torvalds const struct loop_info64 *i) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds int err = 0; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds if (xfer) { 9691da177e4SLinus Torvalds struct module *owner = xfer->owner; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (!try_module_get(owner)) 9721da177e4SLinus Torvalds return -EINVAL; 9731da177e4SLinus Torvalds if (xfer->init) 9741da177e4SLinus Torvalds err = xfer->init(lo, i); 9751da177e4SLinus Torvalds if (err) 9761da177e4SLinus Torvalds module_put(owner); 9771da177e4SLinus Torvalds else 9781da177e4SLinus Torvalds lo->lo_encryption = xfer; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds return err; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9834c823cc3SAyan George static int loop_clr_fd(struct loop_device *lo) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds struct file *filp = lo->lo_backing_file; 986b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask; 9874c823cc3SAyan George struct block_device *bdev = lo->lo_device; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 9901da177e4SLinus Torvalds return -ENXIO; 9911da177e4SLinus Torvalds 992a1ecac3bSDave Chinner /* 993a1ecac3bSDave Chinner * If we've explicitly asked to tear down the loop device, 994a1ecac3bSDave Chinner * and it has an elevated reference count, set it for auto-teardown when 995a1ecac3bSDave Chinner * the last reference goes away. This stops $!~#$@ udev from 996a1ecac3bSDave Chinner * preventing teardown because it decided that it needs to run blkid on 997a1ecac3bSDave Chinner * the loopback device whenever they appear. xfstests is notorious for 998a1ecac3bSDave Chinner * failing tests because blkid via udev races with a losetup 999a1ecac3bSDave Chinner * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1000a1ecac3bSDave Chinner * command to fail with EBUSY. 1001a1ecac3bSDave Chinner */ 1002a1ecac3bSDave Chinner if (lo->lo_refcnt > 1) { 1003a1ecac3bSDave Chinner lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1004a1ecac3bSDave Chinner mutex_unlock(&lo->lo_ctl_mutex); 1005a1ecac3bSDave Chinner return 0; 1006a1ecac3bSDave Chinner } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds if (filp == NULL) 10091da177e4SLinus Torvalds return -EINVAL; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock); 10121da177e4SLinus Torvalds lo->lo_state = Lo_rundown; 10131da177e4SLinus Torvalds spin_unlock_irq(&lo->lo_lock); 10141da177e4SLinus Torvalds 10156c997918SSerge E. Hallyn kthread_stop(lo->lo_thread); 10161da177e4SLinus Torvalds 101705eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock); 10181da177e4SLinus Torvalds lo->lo_backing_file = NULL; 101905eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock); 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds loop_release_xfer(lo); 10221da177e4SLinus Torvalds lo->transfer = NULL; 10231da177e4SLinus Torvalds lo->ioctl = NULL; 10241da177e4SLinus Torvalds lo->lo_device = NULL; 10251da177e4SLinus Torvalds lo->lo_encryption = NULL; 10261da177e4SLinus Torvalds lo->lo_offset = 0; 10271da177e4SLinus Torvalds lo->lo_sizelimit = 0; 10281da177e4SLinus Torvalds lo->lo_encrypt_key_size = 0; 10296c997918SSerge E. Hallyn lo->lo_thread = NULL; 10301da177e4SLinus Torvalds memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 10311da177e4SLinus Torvalds memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 10321da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1033bb214884SAl Viro if (bdev) 1034f98393a6SPeter Zijlstra invalidate_bdev(bdev); 103573285082SKen Chen set_capacity(lo->lo_disk, 0); 103651a0bb0cSMilan Broz loop_sysfs_exit(lo); 1037c3473c63SDavid Zeuthen if (bdev) { 10381da177e4SLinus Torvalds bd_set_size(bdev, 0); 1039c3473c63SDavid Zeuthen /* let user-space know about this change */ 1040c3473c63SDavid Zeuthen kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1041c3473c63SDavid Zeuthen } 10421da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp); 10431da177e4SLinus Torvalds lo->lo_state = Lo_unbound; 10441da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */ 10451da177e4SLinus Torvalds module_put(THIS_MODULE); 1046e03c8dd1SKay Sievers if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) 1047476a4813SLaurent Vivier ioctl_by_bdev(bdev, BLKRRPART, 0); 1048e03c8dd1SKay Sievers lo->lo_flags = 0; 1049e03c8dd1SKay Sievers if (!part_shift) 1050e03c8dd1SKay Sievers lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1051f028f3b2SNikanth Karthikesan mutex_unlock(&lo->lo_ctl_mutex); 1052f028f3b2SNikanth Karthikesan /* 1053f028f3b2SNikanth Karthikesan * Need not hold lo_ctl_mutex to fput backing file. 1054f028f3b2SNikanth Karthikesan * Calling fput holding lo_ctl_mutex triggers a circular 1055f028f3b2SNikanth Karthikesan * lock dependency possibility warning as fput can take 1056f028f3b2SNikanth Karthikesan * bd_mutex which is usually taken before lo_ctl_mutex. 1057f028f3b2SNikanth Karthikesan */ 1058f028f3b2SNikanth Karthikesan fput(filp); 10591da177e4SLinus Torvalds return 0; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds static int 10631da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds int err; 10661da177e4SLinus Torvalds struct loop_func_table *xfer; 1067e4849737SEric W. Biederman kuid_t uid = current_uid(); 10681da177e4SLinus Torvalds 1069b0fafa81SDavid Howells if (lo->lo_encrypt_key_size && 1070e4849737SEric W. Biederman !uid_eq(lo->lo_key_owner, uid) && 10711da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) 10721da177e4SLinus Torvalds return -EPERM; 10731da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 10741da177e4SLinus Torvalds return -ENXIO; 10751da177e4SLinus Torvalds if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 10761da177e4SLinus Torvalds return -EINVAL; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds err = loop_release_xfer(lo); 10791da177e4SLinus Torvalds if (err) 10801da177e4SLinus Torvalds return err; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (info->lo_encrypt_type) { 10831da177e4SLinus Torvalds unsigned int type = info->lo_encrypt_type; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds if (type >= MAX_LO_CRYPT) 10861da177e4SLinus Torvalds return -EINVAL; 10871da177e4SLinus Torvalds xfer = xfer_funcs[type]; 10881da177e4SLinus Torvalds if (xfer == NULL) 10891da177e4SLinus Torvalds return -EINVAL; 10901da177e4SLinus Torvalds } else 10911da177e4SLinus Torvalds xfer = NULL; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds err = loop_init_xfer(lo, xfer, info); 10941da177e4SLinus Torvalds if (err) 10951da177e4SLinus Torvalds return err; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds if (lo->lo_offset != info->lo_offset || 10987b0576a3SGuo Chao lo->lo_sizelimit != info->lo_sizelimit) 10997035b5dfSDmitry Monakhov if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) 11001da177e4SLinus Torvalds return -EFBIG; 1101541c742aSGuo Chao 1102dfaa2ef6SLukas Czerner loop_config_discard(lo); 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 11051da177e4SLinus Torvalds memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 11061da177e4SLinus Torvalds lo->lo_file_name[LO_NAME_SIZE-1] = 0; 11071da177e4SLinus Torvalds lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds if (!xfer) 11101da177e4SLinus Torvalds xfer = &none_funcs; 11111da177e4SLinus Torvalds lo->transfer = xfer->transfer; 11121da177e4SLinus Torvalds lo->ioctl = xfer->ioctl; 11131da177e4SLinus Torvalds 111496c58655SDavid Woodhouse if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 111596c58655SDavid Woodhouse (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 111696c58655SDavid Woodhouse lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 111796c58655SDavid Woodhouse 1118e03c8dd1SKay Sievers if ((info->lo_flags & LO_FLAGS_PARTSCAN) && 1119e03c8dd1SKay Sievers !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1120e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN; 1121e03c8dd1SKay Sievers lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1122e03c8dd1SKay Sievers ioctl_by_bdev(lo->lo_device, BLKRRPART, 0); 1123e03c8dd1SKay Sievers } 1124e03c8dd1SKay Sievers 11251da177e4SLinus Torvalds lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 11261da177e4SLinus Torvalds lo->lo_init[0] = info->lo_init[0]; 11271da177e4SLinus Torvalds lo->lo_init[1] = info->lo_init[1]; 11281da177e4SLinus Torvalds if (info->lo_encrypt_key_size) { 11291da177e4SLinus Torvalds memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 11301da177e4SLinus Torvalds info->lo_encrypt_key_size); 1131b0fafa81SDavid Howells lo->lo_key_owner = uid; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds return 0; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds static int 11381da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info) 11391da177e4SLinus Torvalds { 11401da177e4SLinus Torvalds struct file *file = lo->lo_backing_file; 11411da177e4SLinus Torvalds struct kstat stat; 11421da177e4SLinus Torvalds int error; 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds if (lo->lo_state != Lo_bound) 11451da177e4SLinus Torvalds return -ENXIO; 11466c648be6SJosef Sipek error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat); 11471da177e4SLinus Torvalds if (error) 11481da177e4SLinus Torvalds return error; 11491da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11501da177e4SLinus Torvalds info->lo_number = lo->lo_number; 11511da177e4SLinus Torvalds info->lo_device = huge_encode_dev(stat.dev); 11521da177e4SLinus Torvalds info->lo_inode = stat.ino; 11531da177e4SLinus Torvalds info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 11541da177e4SLinus Torvalds info->lo_offset = lo->lo_offset; 11551da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit; 11561da177e4SLinus Torvalds info->lo_flags = lo->lo_flags; 11571da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 11581da177e4SLinus Torvalds memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 11591da177e4SLinus Torvalds info->lo_encrypt_type = 11601da177e4SLinus Torvalds lo->lo_encryption ? lo->lo_encryption->number : 0; 11611da177e4SLinus Torvalds if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 11621da177e4SLinus Torvalds info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 11631da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 11641da177e4SLinus Torvalds lo->lo_encrypt_key_size); 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds return 0; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds static void 11701da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64)); 11731da177e4SLinus Torvalds info64->lo_number = info->lo_number; 11741da177e4SLinus Torvalds info64->lo_device = info->lo_device; 11751da177e4SLinus Torvalds info64->lo_inode = info->lo_inode; 11761da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice; 11771da177e4SLinus Torvalds info64->lo_offset = info->lo_offset; 11781da177e4SLinus Torvalds info64->lo_sizelimit = 0; 11791da177e4SLinus Torvalds info64->lo_encrypt_type = info->lo_encrypt_type; 11801da177e4SLinus Torvalds info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 11811da177e4SLinus Torvalds info64->lo_flags = info->lo_flags; 11821da177e4SLinus Torvalds info64->lo_init[0] = info->lo_init[0]; 11831da177e4SLinus Torvalds info64->lo_init[1] = info->lo_init[1]; 11841da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 11851da177e4SLinus Torvalds memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 11861da177e4SLinus Torvalds else 11871da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 11881da177e4SLinus Torvalds memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds static int 11921da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 11931da177e4SLinus Torvalds { 11941da177e4SLinus Torvalds memset(info, 0, sizeof(*info)); 11951da177e4SLinus Torvalds info->lo_number = info64->lo_number; 11961da177e4SLinus Torvalds info->lo_device = info64->lo_device; 11971da177e4SLinus Torvalds info->lo_inode = info64->lo_inode; 11981da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice; 11991da177e4SLinus Torvalds info->lo_offset = info64->lo_offset; 12001da177e4SLinus Torvalds info->lo_encrypt_type = info64->lo_encrypt_type; 12011da177e4SLinus Torvalds info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 12021da177e4SLinus Torvalds info->lo_flags = info64->lo_flags; 12031da177e4SLinus Torvalds info->lo_init[0] = info64->lo_init[0]; 12041da177e4SLinus Torvalds info->lo_init[1] = info64->lo_init[1]; 12051da177e4SLinus Torvalds if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 12061da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 12071da177e4SLinus Torvalds else 12081da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 12091da177e4SLinus Torvalds memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds /* error in case values were truncated */ 12121da177e4SLinus Torvalds if (info->lo_device != info64->lo_device || 12131da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice || 12141da177e4SLinus Torvalds info->lo_inode != info64->lo_inode || 12151da177e4SLinus Torvalds info->lo_offset != info64->lo_offset) 12161da177e4SLinus Torvalds return -EOVERFLOW; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds return 0; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds static int 12221da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 12231da177e4SLinus Torvalds { 12241da177e4SLinus Torvalds struct loop_info info; 12251da177e4SLinus Torvalds struct loop_info64 info64; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info))) 12281da177e4SLinus Torvalds return -EFAULT; 12291da177e4SLinus Torvalds loop_info64_from_old(&info, &info64); 12301da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds static int 12341da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 12351da177e4SLinus Torvalds { 12361da177e4SLinus Torvalds struct loop_info64 info64; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 12391da177e4SLinus Torvalds return -EFAULT; 12401da177e4SLinus Torvalds return loop_set_status(lo, &info64); 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds static int 12441da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 12451da177e4SLinus Torvalds struct loop_info info; 12461da177e4SLinus Torvalds struct loop_info64 info64; 12471da177e4SLinus Torvalds int err = 0; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (!arg) 12501da177e4SLinus Torvalds err = -EINVAL; 12511da177e4SLinus Torvalds if (!err) 12521da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12531da177e4SLinus Torvalds if (!err) 12541da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info); 12551da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info))) 12561da177e4SLinus Torvalds err = -EFAULT; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds return err; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds static int 12621da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 12631da177e4SLinus Torvalds struct loop_info64 info64; 12641da177e4SLinus Torvalds int err = 0; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds if (!arg) 12671da177e4SLinus Torvalds err = -EINVAL; 12681da177e4SLinus Torvalds if (!err) 12691da177e4SLinus Torvalds err = loop_get_status(lo, &info64); 12701da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64))) 12711da177e4SLinus Torvalds err = -EFAULT; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds return err; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 127653d66608SJ. R. Okajima static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 127753d66608SJ. R. Okajima { 127853d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound)) 12797b0576a3SGuo Chao return -ENXIO; 128053d66608SJ. R. Okajima 12817b0576a3SGuo Chao return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 128253d66608SJ. R. Okajima } 128353d66608SJ. R. Okajima 1284bb214884SAl Viro static int lo_ioctl(struct block_device *bdev, fmode_t mode, 12851da177e4SLinus Torvalds unsigned int cmd, unsigned long arg) 12861da177e4SLinus Torvalds { 1287bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 12881da177e4SLinus Torvalds int err; 12891da177e4SLinus Torvalds 1290f028f3b2SNikanth Karthikesan mutex_lock_nested(&lo->lo_ctl_mutex, 1); 12911da177e4SLinus Torvalds switch (cmd) { 12921da177e4SLinus Torvalds case LOOP_SET_FD: 1293bb214884SAl Viro err = loop_set_fd(lo, mode, bdev, arg); 12941da177e4SLinus Torvalds break; 12951da177e4SLinus Torvalds case LOOP_CHANGE_FD: 1296bb214884SAl Viro err = loop_change_fd(lo, bdev, arg); 12971da177e4SLinus Torvalds break; 12981da177e4SLinus Torvalds case LOOP_CLR_FD: 1299f028f3b2SNikanth Karthikesan /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 13004c823cc3SAyan George err = loop_clr_fd(lo); 1301f028f3b2SNikanth Karthikesan if (!err) 1302f028f3b2SNikanth Karthikesan goto out_unlocked; 13031da177e4SLinus Torvalds break; 13041da177e4SLinus Torvalds case LOOP_SET_STATUS: 13057035b5dfSDmitry Monakhov err = -EPERM; 13067035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13077035b5dfSDmitry Monakhov err = loop_set_status_old(lo, 13087035b5dfSDmitry Monakhov (struct loop_info __user *)arg); 13091da177e4SLinus Torvalds break; 13101da177e4SLinus Torvalds case LOOP_GET_STATUS: 13111da177e4SLinus Torvalds err = loop_get_status_old(lo, (struct loop_info __user *) arg); 13121da177e4SLinus Torvalds break; 13131da177e4SLinus Torvalds case LOOP_SET_STATUS64: 13147035b5dfSDmitry Monakhov err = -EPERM; 13157035b5dfSDmitry Monakhov if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 13167035b5dfSDmitry Monakhov err = loop_set_status64(lo, 13177035b5dfSDmitry Monakhov (struct loop_info64 __user *) arg); 13181da177e4SLinus Torvalds break; 13191da177e4SLinus Torvalds case LOOP_GET_STATUS64: 13201da177e4SLinus Torvalds err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 13211da177e4SLinus Torvalds break; 132253d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 132353d66608SJ. R. Okajima err = -EPERM; 132453d66608SJ. R. Okajima if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 132553d66608SJ. R. Okajima err = loop_set_capacity(lo, bdev); 132653d66608SJ. R. Okajima break; 13271da177e4SLinus Torvalds default: 13281da177e4SLinus Torvalds err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 13291da177e4SLinus Torvalds } 1330f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1331f028f3b2SNikanth Karthikesan 1332f028f3b2SNikanth Karthikesan out_unlocked: 13331da177e4SLinus Torvalds return err; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 1336863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1337863d5b82SDavid Howells struct compat_loop_info { 1338863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */ 1339863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */ 1340863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */ 1341863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */ 1342863d5b82SDavid Howells compat_int_t lo_offset; 1343863d5b82SDavid Howells compat_int_t lo_encrypt_type; 1344863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1345863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */ 1346863d5b82SDavid Howells char lo_name[LO_NAME_SIZE]; 1347863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1348863d5b82SDavid Howells compat_ulong_t lo_init[2]; 1349863d5b82SDavid Howells char reserved[4]; 1350863d5b82SDavid Howells }; 1351863d5b82SDavid Howells 1352863d5b82SDavid Howells /* 1353863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1354863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1355863d5b82SDavid Howells */ 1356863d5b82SDavid Howells static noinline int 1357ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg, 1358863d5b82SDavid Howells struct loop_info64 *info64) 1359863d5b82SDavid Howells { 1360863d5b82SDavid Howells struct compat_loop_info info; 1361863d5b82SDavid Howells 1362863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info))) 1363863d5b82SDavid Howells return -EFAULT; 1364863d5b82SDavid Howells 1365863d5b82SDavid Howells memset(info64, 0, sizeof(*info64)); 1366863d5b82SDavid Howells info64->lo_number = info.lo_number; 1367863d5b82SDavid Howells info64->lo_device = info.lo_device; 1368863d5b82SDavid Howells info64->lo_inode = info.lo_inode; 1369863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice; 1370863d5b82SDavid Howells info64->lo_offset = info.lo_offset; 1371863d5b82SDavid Howells info64->lo_sizelimit = 0; 1372863d5b82SDavid Howells info64->lo_encrypt_type = info.lo_encrypt_type; 1373863d5b82SDavid Howells info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1374863d5b82SDavid Howells info64->lo_flags = info.lo_flags; 1375863d5b82SDavid Howells info64->lo_init[0] = info.lo_init[0]; 1376863d5b82SDavid Howells info64->lo_init[1] = info.lo_init[1]; 1377863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1378863d5b82SDavid Howells memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1379863d5b82SDavid Howells else 1380863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1381863d5b82SDavid Howells memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1382863d5b82SDavid Howells return 0; 1383863d5b82SDavid Howells } 1384863d5b82SDavid Howells 1385863d5b82SDavid Howells /* 1386863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1387863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver 1388863d5b82SDavid Howells */ 1389863d5b82SDavid Howells static noinline int 1390863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64, 1391863d5b82SDavid Howells struct compat_loop_info __user *arg) 1392863d5b82SDavid Howells { 1393863d5b82SDavid Howells struct compat_loop_info info; 1394863d5b82SDavid Howells 1395863d5b82SDavid Howells memset(&info, 0, sizeof(info)); 1396863d5b82SDavid Howells info.lo_number = info64->lo_number; 1397863d5b82SDavid Howells info.lo_device = info64->lo_device; 1398863d5b82SDavid Howells info.lo_inode = info64->lo_inode; 1399863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice; 1400863d5b82SDavid Howells info.lo_offset = info64->lo_offset; 1401863d5b82SDavid Howells info.lo_encrypt_type = info64->lo_encrypt_type; 1402863d5b82SDavid Howells info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1403863d5b82SDavid Howells info.lo_flags = info64->lo_flags; 1404863d5b82SDavid Howells info.lo_init[0] = info64->lo_init[0]; 1405863d5b82SDavid Howells info.lo_init[1] = info64->lo_init[1]; 1406863d5b82SDavid Howells if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1407863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1408863d5b82SDavid Howells else 1409863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1410863d5b82SDavid Howells memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1411863d5b82SDavid Howells 1412863d5b82SDavid Howells /* error in case values were truncated */ 1413863d5b82SDavid Howells if (info.lo_device != info64->lo_device || 1414863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice || 1415863d5b82SDavid Howells info.lo_inode != info64->lo_inode || 1416863d5b82SDavid Howells info.lo_offset != info64->lo_offset || 1417863d5b82SDavid Howells info.lo_init[0] != info64->lo_init[0] || 1418863d5b82SDavid Howells info.lo_init[1] != info64->lo_init[1]) 1419863d5b82SDavid Howells return -EOVERFLOW; 1420863d5b82SDavid Howells 1421863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info))) 1422863d5b82SDavid Howells return -EFAULT; 1423863d5b82SDavid Howells return 0; 1424863d5b82SDavid Howells } 1425863d5b82SDavid Howells 1426863d5b82SDavid Howells static int 1427863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo, 1428863d5b82SDavid Howells const struct compat_loop_info __user *arg) 1429863d5b82SDavid Howells { 1430863d5b82SDavid Howells struct loop_info64 info64; 1431863d5b82SDavid Howells int ret; 1432863d5b82SDavid Howells 1433863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64); 1434863d5b82SDavid Howells if (ret < 0) 1435863d5b82SDavid Howells return ret; 1436863d5b82SDavid Howells return loop_set_status(lo, &info64); 1437863d5b82SDavid Howells } 1438863d5b82SDavid Howells 1439863d5b82SDavid Howells static int 1440863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo, 1441863d5b82SDavid Howells struct compat_loop_info __user *arg) 1442863d5b82SDavid Howells { 1443863d5b82SDavid Howells struct loop_info64 info64; 1444863d5b82SDavid Howells int err = 0; 1445863d5b82SDavid Howells 1446863d5b82SDavid Howells if (!arg) 1447863d5b82SDavid Howells err = -EINVAL; 1448863d5b82SDavid Howells if (!err) 1449863d5b82SDavid Howells err = loop_get_status(lo, &info64); 1450863d5b82SDavid Howells if (!err) 1451863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg); 1452863d5b82SDavid Howells return err; 1453863d5b82SDavid Howells } 1454863d5b82SDavid Howells 1455bb214884SAl Viro static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1456bb214884SAl Viro unsigned int cmd, unsigned long arg) 1457863d5b82SDavid Howells { 1458bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data; 1459863d5b82SDavid Howells int err; 1460863d5b82SDavid Howells 1461863d5b82SDavid Howells switch(cmd) { 1462863d5b82SDavid Howells case LOOP_SET_STATUS: 1463863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1464863d5b82SDavid Howells err = loop_set_status_compat( 1465863d5b82SDavid Howells lo, (const struct compat_loop_info __user *) arg); 1466863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1467863d5b82SDavid Howells break; 1468863d5b82SDavid Howells case LOOP_GET_STATUS: 1469863d5b82SDavid Howells mutex_lock(&lo->lo_ctl_mutex); 1470863d5b82SDavid Howells err = loop_get_status_compat( 1471863d5b82SDavid Howells lo, (struct compat_loop_info __user *) arg); 1472863d5b82SDavid Howells mutex_unlock(&lo->lo_ctl_mutex); 1473863d5b82SDavid Howells break; 147453d66608SJ. R. Okajima case LOOP_SET_CAPACITY: 1475863d5b82SDavid Howells case LOOP_CLR_FD: 1476863d5b82SDavid Howells case LOOP_GET_STATUS64: 1477863d5b82SDavid Howells case LOOP_SET_STATUS64: 1478863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg); 1479863d5b82SDavid Howells case LOOP_SET_FD: 1480863d5b82SDavid Howells case LOOP_CHANGE_FD: 1481bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg); 1482863d5b82SDavid Howells break; 1483863d5b82SDavid Howells default: 1484863d5b82SDavid Howells err = -ENOIOCTLCMD; 1485863d5b82SDavid Howells break; 1486863d5b82SDavid Howells } 1487863d5b82SDavid Howells return err; 1488863d5b82SDavid Howells } 1489863d5b82SDavid Howells #endif 1490863d5b82SDavid Howells 1491bb214884SAl Viro static int lo_open(struct block_device *bdev, fmode_t mode) 14921da177e4SLinus Torvalds { 1493770fe30aSKay Sievers struct loop_device *lo; 1494770fe30aSKay Sievers int err = 0; 1495770fe30aSKay Sievers 1496770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1497770fe30aSKay Sievers lo = bdev->bd_disk->private_data; 1498770fe30aSKay Sievers if (!lo) { 1499770fe30aSKay Sievers err = -ENXIO; 1500770fe30aSKay Sievers goto out; 1501770fe30aSKay Sievers } 15021da177e4SLinus Torvalds 1503f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 15041da177e4SLinus Torvalds lo->lo_refcnt++; 1505f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1506770fe30aSKay Sievers out: 1507770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1508770fe30aSKay Sievers return err; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds 1511bb214884SAl Viro static int lo_release(struct gendisk *disk, fmode_t mode) 15121da177e4SLinus Torvalds { 1513bb214884SAl Viro struct loop_device *lo = disk->private_data; 1514ffcd7dcaSAlexander Beregalov int err; 15151da177e4SLinus Torvalds 1516f85221ddSIngo Molnar mutex_lock(&lo->lo_ctl_mutex); 151796c58655SDavid Woodhouse 151814f27939SMilan Broz if (--lo->lo_refcnt) 151914f27939SMilan Broz goto out; 152014f27939SMilan Broz 152114f27939SMilan Broz if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 152214f27939SMilan Broz /* 152314f27939SMilan Broz * In autoclear mode, stop the loop thread 152414f27939SMilan Broz * and remove configuration after last close. 152514f27939SMilan Broz */ 15264c823cc3SAyan George err = loop_clr_fd(lo); 1527ffcd7dcaSAlexander Beregalov if (!err) 1528ffcd7dcaSAlexander Beregalov goto out_unlocked; 152914f27939SMilan Broz } else { 153014f27939SMilan Broz /* 153114f27939SMilan Broz * Otherwise keep thread (if running) and config, 153214f27939SMilan Broz * but flush possible ongoing bios in thread. 153314f27939SMilan Broz */ 153414f27939SMilan Broz loop_flush(lo); 153514f27939SMilan Broz } 153696c58655SDavid Woodhouse 153714f27939SMilan Broz out: 1538f85221ddSIngo Molnar mutex_unlock(&lo->lo_ctl_mutex); 1539ffcd7dcaSAlexander Beregalov out_unlocked: 15401da177e4SLinus Torvalds return 0; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 154383d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = { 15441da177e4SLinus Torvalds .owner = THIS_MODULE, 1545bb214884SAl Viro .open = lo_open, 1546bb214884SAl Viro .release = lo_release, 1547bb214884SAl Viro .ioctl = lo_ioctl, 1548863d5b82SDavid Howells #ifdef CONFIG_COMPAT 1549bb214884SAl Viro .compat_ioctl = lo_compat_ioctl, 1550863d5b82SDavid Howells #endif 15511da177e4SLinus Torvalds }; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* 15541da177e4SLinus Torvalds * And now the modules code and kernel interface. 15551da177e4SLinus Torvalds */ 155673285082SKen Chen static int max_loop; 1557ac04fee0SNamhyung Kim module_param(max_loop, int, S_IRUGO); 1558a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1559ac04fee0SNamhyung Kim module_param(max_part, int, S_IRUGO); 1560476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 15611da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 15621da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds int loop_register_transfer(struct loop_func_table *funcs) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds unsigned int n = funcs->number; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 15691da177e4SLinus Torvalds return -EINVAL; 15701da177e4SLinus Torvalds xfer_funcs[n] = funcs; 15711da177e4SLinus Torvalds return 0; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 157434dd82afSKay Sievers static int unregister_transfer_cb(int id, void *ptr, void *data) 157534dd82afSKay Sievers { 157634dd82afSKay Sievers struct loop_device *lo = ptr; 157734dd82afSKay Sievers struct loop_func_table *xfer = data; 157834dd82afSKay Sievers 157934dd82afSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 158034dd82afSKay Sievers if (lo->lo_encryption == xfer) 158134dd82afSKay Sievers loop_release_xfer(lo); 158234dd82afSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 158334dd82afSKay Sievers return 0; 158434dd82afSKay Sievers } 158534dd82afSKay Sievers 15861da177e4SLinus Torvalds int loop_unregister_transfer(int number) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds unsigned int n = number; 15891da177e4SLinus Torvalds struct loop_func_table *xfer; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 15921da177e4SLinus Torvalds return -EINVAL; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds xfer_funcs[n] = NULL; 159534dd82afSKay Sievers idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 15961da177e4SLinus Torvalds return 0; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds EXPORT_SYMBOL(loop_register_transfer); 16001da177e4SLinus Torvalds EXPORT_SYMBOL(loop_unregister_transfer); 16011da177e4SLinus Torvalds 160234dd82afSKay Sievers static int loop_add(struct loop_device **l, int i) 16031da177e4SLinus Torvalds { 160473285082SKen Chen struct loop_device *lo; 160573285082SKen Chen struct gendisk *disk; 160634dd82afSKay Sievers int err; 16071da177e4SLinus Torvalds 160834dd82afSKay Sievers err = -ENOMEM; 160968d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL); 161068d740d7SSilva Paulo if (!lo) 161173285082SKen Chen goto out; 161234dd82afSKay Sievers 161368d740d7SSilva Paulo if (!idr_pre_get(&loop_index_idr, GFP_KERNEL)) 161434dd82afSKay Sievers goto out_free_dev; 161534dd82afSKay Sievers 161634dd82afSKay Sievers if (i >= 0) { 161734dd82afSKay Sievers int m; 161834dd82afSKay Sievers 161934dd82afSKay Sievers /* create specific i in the index */ 162034dd82afSKay Sievers err = idr_get_new_above(&loop_index_idr, lo, i, &m); 162134dd82afSKay Sievers if (err >= 0 && i != m) { 162234dd82afSKay Sievers idr_remove(&loop_index_idr, m); 162334dd82afSKay Sievers err = -EEXIST; 162434dd82afSKay Sievers } 1625770fe30aSKay Sievers } else if (i == -1) { 1626770fe30aSKay Sievers int m; 1627770fe30aSKay Sievers 1628770fe30aSKay Sievers /* get next free nr */ 1629770fe30aSKay Sievers err = idr_get_new(&loop_index_idr, lo, &m); 1630770fe30aSKay Sievers if (err >= 0) 1631770fe30aSKay Sievers i = m; 163234dd82afSKay Sievers } else { 163334dd82afSKay Sievers err = -EINVAL; 163434dd82afSKay Sievers } 163534dd82afSKay Sievers if (err < 0) 163634dd82afSKay Sievers goto out_free_dev; 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 16391da177e4SLinus Torvalds if (!lo->lo_queue) 164073285082SKen Chen goto out_free_dev; 164173285082SKen Chen 1642476a4813SLaurent Vivier disk = lo->lo_disk = alloc_disk(1 << part_shift); 164373285082SKen Chen if (!disk) 164473285082SKen Chen goto out_free_queue; 164573285082SKen Chen 1646e03c8dd1SKay Sievers /* 1647e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition 1648e03c8dd1SKay Sievers * scanning can be requested individually per-device during its 1649e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all 1650e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the 1651e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue 1652e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions 1653e03c8dd1SKay Sievers * used. 1654e03c8dd1SKay Sievers * 1655e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for 1656e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be 1657e03c8dd1SKay Sievers * multiples of max_part. 1658e03c8dd1SKay Sievers * 1659e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module 1660e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly 1661e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise 1662e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided. 1663e03c8dd1SKay Sievers */ 1664e03c8dd1SKay Sievers if (!part_shift) 1665e03c8dd1SKay Sievers disk->flags |= GENHD_FL_NO_PART_SCAN; 1666e03c8dd1SKay Sievers disk->flags |= GENHD_FL_EXT_DEVT; 1667f85221ddSIngo Molnar mutex_init(&lo->lo_ctl_mutex); 16681da177e4SLinus Torvalds lo->lo_number = i; 16696c997918SSerge E. Hallyn lo->lo_thread = NULL; 16706c997918SSerge E. Hallyn init_waitqueue_head(&lo->lo_event); 16717b5a3522SLukas Czerner init_waitqueue_head(&lo->lo_req_wait); 16721da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock); 16731da177e4SLinus Torvalds disk->major = LOOP_MAJOR; 1674476a4813SLaurent Vivier disk->first_minor = i << part_shift; 16751da177e4SLinus Torvalds disk->fops = &lo_fops; 16761da177e4SLinus Torvalds disk->private_data = lo; 16771da177e4SLinus Torvalds disk->queue = lo->lo_queue; 167873285082SKen Chen sprintf(disk->disk_name, "loop%d", i); 167934dd82afSKay Sievers add_disk(disk); 168034dd82afSKay Sievers *l = lo; 168134dd82afSKay Sievers return lo->lo_number; 168273285082SKen Chen 168373285082SKen Chen out_free_queue: 168473285082SKen Chen blk_cleanup_queue(lo->lo_queue); 168573285082SKen Chen out_free_dev: 168673285082SKen Chen kfree(lo); 168773285082SKen Chen out: 168834dd82afSKay Sievers return err; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 169134dd82afSKay Sievers static void loop_remove(struct loop_device *lo) 169273285082SKen Chen { 169334dd82afSKay Sievers del_gendisk(lo->lo_disk); 169473285082SKen Chen blk_cleanup_queue(lo->lo_queue); 169573285082SKen Chen put_disk(lo->lo_disk); 169673285082SKen Chen kfree(lo); 169773285082SKen Chen } 169873285082SKen Chen 1699770fe30aSKay Sievers static int find_free_cb(int id, void *ptr, void *data) 1700770fe30aSKay Sievers { 1701770fe30aSKay Sievers struct loop_device *lo = ptr; 1702770fe30aSKay Sievers struct loop_device **l = data; 1703770fe30aSKay Sievers 1704770fe30aSKay Sievers if (lo->lo_state == Lo_unbound) { 1705770fe30aSKay Sievers *l = lo; 1706770fe30aSKay Sievers return 1; 1707770fe30aSKay Sievers } 1708770fe30aSKay Sievers return 0; 1709770fe30aSKay Sievers } 1710770fe30aSKay Sievers 171134dd82afSKay Sievers static int loop_lookup(struct loop_device **l, int i) 1712a47653fcSKen Chen { 1713a47653fcSKen Chen struct loop_device *lo; 171434dd82afSKay Sievers int ret = -ENODEV; 1715a47653fcSKen Chen 1716770fe30aSKay Sievers if (i < 0) { 1717770fe30aSKay Sievers int err; 1718770fe30aSKay Sievers 1719770fe30aSKay Sievers err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 1720770fe30aSKay Sievers if (err == 1) { 1721770fe30aSKay Sievers *l = lo; 1722770fe30aSKay Sievers ret = lo->lo_number; 1723770fe30aSKay Sievers } 1724770fe30aSKay Sievers goto out; 1725a47653fcSKen Chen } 1726a47653fcSKen Chen 1727770fe30aSKay Sievers /* lookup and return a specific i */ 172834dd82afSKay Sievers lo = idr_find(&loop_index_idr, i); 1729a47653fcSKen Chen if (lo) { 173034dd82afSKay Sievers *l = lo; 173134dd82afSKay Sievers ret = lo->lo_number; 1732a47653fcSKen Chen } 1733770fe30aSKay Sievers out: 173434dd82afSKay Sievers return ret; 1735a47653fcSKen Chen } 1736a47653fcSKen Chen 173773285082SKen Chen static struct kobject *loop_probe(dev_t dev, int *part, void *data) 173873285082SKen Chen { 1739705962ccSAl Viro struct loop_device *lo; 174007002e99SAl Viro struct kobject *kobj; 174134dd82afSKay Sievers int err; 174273285082SKen Chen 174334dd82afSKay Sievers mutex_lock(&loop_index_mutex); 174434dd82afSKay Sievers err = loop_lookup(&lo, MINOR(dev) >> part_shift); 174534dd82afSKay Sievers if (err < 0) 174634dd82afSKay Sievers err = loop_add(&lo, MINOR(dev) >> part_shift); 174734dd82afSKay Sievers if (err < 0) 174834dd82afSKay Sievers kobj = ERR_PTR(err); 174934dd82afSKay Sievers else 175034dd82afSKay Sievers kobj = get_disk(lo->lo_disk); 175134dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 175273285082SKen Chen 175373285082SKen Chen *part = 0; 175407002e99SAl Viro return kobj; 175573285082SKen Chen } 175673285082SKen Chen 1757770fe30aSKay Sievers static long loop_control_ioctl(struct file *file, unsigned int cmd, 1758770fe30aSKay Sievers unsigned long parm) 1759770fe30aSKay Sievers { 1760770fe30aSKay Sievers struct loop_device *lo; 1761770fe30aSKay Sievers int ret = -ENOSYS; 1762770fe30aSKay Sievers 1763770fe30aSKay Sievers mutex_lock(&loop_index_mutex); 1764770fe30aSKay Sievers switch (cmd) { 1765770fe30aSKay Sievers case LOOP_CTL_ADD: 1766770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1767770fe30aSKay Sievers if (ret >= 0) { 1768770fe30aSKay Sievers ret = -EEXIST; 1769770fe30aSKay Sievers break; 1770770fe30aSKay Sievers } 1771770fe30aSKay Sievers ret = loop_add(&lo, parm); 1772770fe30aSKay Sievers break; 1773770fe30aSKay Sievers case LOOP_CTL_REMOVE: 1774770fe30aSKay Sievers ret = loop_lookup(&lo, parm); 1775770fe30aSKay Sievers if (ret < 0) 1776770fe30aSKay Sievers break; 1777770fe30aSKay Sievers mutex_lock(&lo->lo_ctl_mutex); 1778770fe30aSKay Sievers if (lo->lo_state != Lo_unbound) { 1779770fe30aSKay Sievers ret = -EBUSY; 1780770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1781770fe30aSKay Sievers break; 1782770fe30aSKay Sievers } 1783770fe30aSKay Sievers if (lo->lo_refcnt > 0) { 1784770fe30aSKay Sievers ret = -EBUSY; 1785770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1786770fe30aSKay Sievers break; 1787770fe30aSKay Sievers } 1788770fe30aSKay Sievers lo->lo_disk->private_data = NULL; 1789770fe30aSKay Sievers mutex_unlock(&lo->lo_ctl_mutex); 1790770fe30aSKay Sievers idr_remove(&loop_index_idr, lo->lo_number); 1791770fe30aSKay Sievers loop_remove(lo); 1792770fe30aSKay Sievers break; 1793770fe30aSKay Sievers case LOOP_CTL_GET_FREE: 1794770fe30aSKay Sievers ret = loop_lookup(&lo, -1); 1795770fe30aSKay Sievers if (ret >= 0) 1796770fe30aSKay Sievers break; 1797770fe30aSKay Sievers ret = loop_add(&lo, -1); 1798770fe30aSKay Sievers } 1799770fe30aSKay Sievers mutex_unlock(&loop_index_mutex); 1800770fe30aSKay Sievers 1801770fe30aSKay Sievers return ret; 1802770fe30aSKay Sievers } 1803770fe30aSKay Sievers 1804770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = { 1805770fe30aSKay Sievers .open = nonseekable_open, 1806770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl, 1807770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl, 1808770fe30aSKay Sievers .owner = THIS_MODULE, 1809770fe30aSKay Sievers .llseek = noop_llseek, 1810770fe30aSKay Sievers }; 1811770fe30aSKay Sievers 1812770fe30aSKay Sievers static struct miscdevice loop_misc = { 1813770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR, 1814770fe30aSKay Sievers .name = "loop-control", 1815770fe30aSKay Sievers .fops = &loop_ctl_fops, 1816770fe30aSKay Sievers }; 1817770fe30aSKay Sievers 1818770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 1819770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control"); 1820770fe30aSKay Sievers 182173285082SKen Chen static int __init loop_init(void) 182273285082SKen Chen { 1823a47653fcSKen Chen int i, nr; 1824a47653fcSKen Chen unsigned long range; 182534dd82afSKay Sievers struct loop_device *lo; 1826770fe30aSKay Sievers int err; 1827a47653fcSKen Chen 1828770fe30aSKay Sievers err = misc_register(&loop_misc); 1829770fe30aSKay Sievers if (err < 0) 1830770fe30aSKay Sievers return err; 1831476a4813SLaurent Vivier 1832476a4813SLaurent Vivier part_shift = 0; 1833ac04fee0SNamhyung Kim if (max_part > 0) { 1834476a4813SLaurent Vivier part_shift = fls(max_part); 1835476a4813SLaurent Vivier 1836ac04fee0SNamhyung Kim /* 1837ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported 1838ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number 1839ac04fee0SNamhyung Kim * if [s]he want to create more devices. 1840ac04fee0SNamhyung Kim * 1841ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved 1842ac04fee0SNamhyung Kim * for the whole disk. 1843ac04fee0SNamhyung Kim */ 1844ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1; 1845ac04fee0SNamhyung Kim } 1846ac04fee0SNamhyung Kim 1847*b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) { 1848*b1a66504SGuo Chao err = -EINVAL; 1849*b1a66504SGuo Chao goto misc_out; 1850*b1a66504SGuo Chao } 185178f4bb36SNamhyung Kim 1852*b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) { 1853*b1a66504SGuo Chao err = -EINVAL; 1854*b1a66504SGuo Chao goto misc_out; 1855*b1a66504SGuo Chao } 185673285082SKen Chen 1857d134b00bSKay Sievers /* 1858d134b00bSKay Sievers * If max_loop is specified, create that many devices upfront. 1859d134b00bSKay Sievers * This also becomes a hard limit. If max_loop is not specified, 1860d134b00bSKay Sievers * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 1861d134b00bSKay Sievers * init time. Loop devices can be requested on-demand with the 1862d134b00bSKay Sievers * /dev/loop-control interface, or be instantiated by accessing 1863d134b00bSKay Sievers * a 'dead' device node. 1864d134b00bSKay Sievers */ 186573285082SKen Chen if (max_loop) { 1866a47653fcSKen Chen nr = max_loop; 1867a1c15c59SNamhyung Kim range = max_loop << part_shift; 1868a47653fcSKen Chen } else { 1869d134b00bSKay Sievers nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 1870a1c15c59SNamhyung Kim range = 1UL << MINORBITS; 187173285082SKen Chen } 1872a47653fcSKen Chen 1873*b1a66504SGuo Chao if (register_blkdev(LOOP_MAJOR, "loop")) { 1874*b1a66504SGuo Chao err = -EIO; 1875*b1a66504SGuo Chao goto misc_out; 1876*b1a66504SGuo Chao } 1877a47653fcSKen Chen 1878a47653fcSKen Chen blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1879a47653fcSKen Chen THIS_MODULE, loop_probe, NULL, NULL); 1880a47653fcSKen Chen 1881d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */ 188234dd82afSKay Sievers mutex_lock(&loop_index_mutex); 188334dd82afSKay Sievers for (i = 0; i < nr; i++) 188434dd82afSKay Sievers loop_add(&lo, i); 188534dd82afSKay Sievers mutex_unlock(&loop_index_mutex); 188634dd82afSKay Sievers 188773285082SKen Chen printk(KERN_INFO "loop: module loaded\n"); 18881da177e4SLinus Torvalds return 0; 1889*b1a66504SGuo Chao 1890*b1a66504SGuo Chao misc_out: 1891*b1a66504SGuo Chao misc_deregister(&loop_misc); 1892*b1a66504SGuo Chao return err; 189334dd82afSKay Sievers } 1894a47653fcSKen Chen 189534dd82afSKay Sievers static int loop_exit_cb(int id, void *ptr, void *data) 189634dd82afSKay Sievers { 189734dd82afSKay Sievers struct loop_device *lo = ptr; 1898a47653fcSKen Chen 189934dd82afSKay Sievers loop_remove(lo); 190034dd82afSKay Sievers return 0; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds 190373285082SKen Chen static void __exit loop_exit(void) 19041da177e4SLinus Torvalds { 1905a47653fcSKen Chen unsigned long range; 19061da177e4SLinus Torvalds 1907a1c15c59SNamhyung Kim range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 1908a47653fcSKen Chen 190934dd82afSKay Sievers idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 191034dd82afSKay Sievers idr_remove_all(&loop_index_idr); 191134dd82afSKay Sievers idr_destroy(&loop_index_idr); 191273285082SKen Chen 1913a47653fcSKen Chen blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 191400d59405SAkinobu Mita unregister_blkdev(LOOP_MAJOR, "loop"); 1915770fe30aSKay Sievers 1916770fe30aSKay Sievers misc_deregister(&loop_misc); 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds module_init(loop_init); 19201da177e4SLinus Torvalds module_exit(loop_exit); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds #ifndef MODULE 19231da177e4SLinus Torvalds static int __init max_loop_setup(char *str) 19241da177e4SLinus Torvalds { 19251da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0); 19261da177e4SLinus Torvalds return 1; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup); 19301da177e4SLinus Torvalds #endif 1931