Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/block/loop.c |
| 3 | * |
| 4 | * Written by Theodore Ts'o, 3/29/93 |
| 5 | * |
| 6 | * Copyright 1993 by Theodore Ts'o. Redistribution of this file is |
| 7 | * permitted under the GNU General Public License. |
| 8 | * |
| 9 | * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993 |
| 10 | * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996 |
| 11 | * |
| 12 | * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994 |
| 13 | * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996 |
| 14 | * |
| 15 | * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997 |
| 16 | * |
| 17 | * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998 |
| 18 | * |
| 19 | * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998 |
| 20 | * |
| 21 | * Loadable modules and other fixes by AK, 1998 |
| 22 | * |
| 23 | * Make real block number available to downstream transfer functions, enables |
| 24 | * CBC (and relatives) mode encryption requiring unique IVs per data block. |
| 25 | * Reed H. Petty, rhp@draper.net |
| 26 | * |
| 27 | * Maximum number of loop devices now dynamic via max_loop module parameter. |
| 28 | * Russell Kroll <rkroll@exploits.org> 19990701 |
| 29 | * |
| 30 | * Maximum number of loop devices when compiled-in now selectable by passing |
| 31 | * max_loop=<1-255> to the kernel on boot. |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 32 | * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | * Completely rewrite request handling to be make_request_fn style and |
| 35 | * non blocking, pushing work to a helper thread. Lots of fixes from |
| 36 | * Al Viro too. |
| 37 | * Jens Axboe <axboe@suse.de>, Nov 2000 |
| 38 | * |
| 39 | * Support up to 256 loop devices |
| 40 | * Heinz Mauelshagen <mge@sistina.com>, Feb 2002 |
| 41 | * |
| 42 | * Support for falling back on the write file operation when the address space |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 43 | * operations write_begin is not available on the backing filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * Anton Altaparmakov, 16 Feb 2005 |
| 45 | * |
| 46 | * Still To Fix: |
| 47 | * - Advisory locking is ignored here. |
| 48 | * - Should use an own CAP_* category instead of CAP_SYS_ADMIN |
| 49 | * |
| 50 | */ |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/module.h> |
| 53 | #include <linux/moduleparam.h> |
| 54 | #include <linux/sched.h> |
| 55 | #include <linux/fs.h> |
| 56 | #include <linux/file.h> |
| 57 | #include <linux/stat.h> |
| 58 | #include <linux/errno.h> |
| 59 | #include <linux/major.h> |
| 60 | #include <linux/wait.h> |
| 61 | #include <linux/blkdev.h> |
| 62 | #include <linux/blkpg.h> |
| 63 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/swap.h> |
| 65 | #include <linux/slab.h> |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 66 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/suspend.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 68 | #include <linux/freezer.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 69 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #include <linux/completion.h> |
| 72 | #include <linux/highmem.h> |
Serge E. Hallyn | 6c99791 | 2006-09-29 01:59:11 -0700 | [diff] [blame] | 73 | #include <linux/kthread.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 74 | #include <linux/splice.h> |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 75 | #include <linux/sysfs.h> |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 76 | #include <linux/miscdevice.h> |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 77 | #include <linux/falloc.h> |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 78 | #include <linux/blk-mq.h> |
Al Viro | 83a8761 | 2013-05-12 10:14:07 -0400 | [diff] [blame] | 79 | #include "loop.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | #include <asm/uaccess.h> |
| 82 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 83 | static DEFINE_IDR(loop_index_idr); |
| 84 | static DEFINE_MUTEX(loop_index_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 86 | static int max_part; |
| 87 | static int part_shift; |
| 88 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 89 | static struct workqueue_struct *loop_wq; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Transfer functions |
| 93 | */ |
| 94 | static int transfer_none(struct loop_device *lo, int cmd, |
| 95 | struct page *raw_page, unsigned raw_off, |
| 96 | struct page *loop_page, unsigned loop_off, |
| 97 | int size, sector_t real_block) |
| 98 | { |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 99 | char *raw_buf = kmap_atomic(raw_page) + raw_off; |
| 100 | char *loop_buf = kmap_atomic(loop_page) + loop_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | if (cmd == READ) |
| 103 | memcpy(loop_buf, raw_buf, size); |
| 104 | else |
| 105 | memcpy(raw_buf, loop_buf, size); |
| 106 | |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 107 | kunmap_atomic(loop_buf); |
| 108 | kunmap_atomic(raw_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | cond_resched(); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int transfer_xor(struct loop_device *lo, int cmd, |
| 114 | struct page *raw_page, unsigned raw_off, |
| 115 | struct page *loop_page, unsigned loop_off, |
| 116 | int size, sector_t real_block) |
| 117 | { |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 118 | char *raw_buf = kmap_atomic(raw_page) + raw_off; |
| 119 | char *loop_buf = kmap_atomic(loop_page) + loop_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | char *in, *out, *key; |
| 121 | int i, keysize; |
| 122 | |
| 123 | if (cmd == READ) { |
| 124 | in = raw_buf; |
| 125 | out = loop_buf; |
| 126 | } else { |
| 127 | in = loop_buf; |
| 128 | out = raw_buf; |
| 129 | } |
| 130 | |
| 131 | key = lo->lo_encrypt_key; |
| 132 | keysize = lo->lo_encrypt_key_size; |
| 133 | for (i = 0; i < size; i++) |
| 134 | *out++ = *in++ ^ key[(i & 511) % keysize]; |
| 135 | |
Cong Wang | cfd8005 | 2011-11-25 23:14:18 +0800 | [diff] [blame] | 136 | kunmap_atomic(loop_buf); |
| 137 | kunmap_atomic(raw_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | cond_resched(); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int xor_init(struct loop_device *lo, const struct loop_info64 *info) |
| 143 | { |
| 144 | if (unlikely(info->lo_encrypt_key_size <= 0)) |
| 145 | return -EINVAL; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static struct loop_func_table none_funcs = { |
| 150 | .number = LO_CRYPT_NONE, |
| 151 | .transfer = transfer_none, |
| 152 | }; |
| 153 | |
| 154 | static struct loop_func_table xor_funcs = { |
| 155 | .number = LO_CRYPT_XOR, |
| 156 | .transfer = transfer_xor, |
| 157 | .init = xor_init |
| 158 | }; |
| 159 | |
| 160 | /* xfer_funcs[0] is special - its release function is never called */ |
| 161 | static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { |
| 162 | &none_funcs, |
| 163 | &xor_funcs |
| 164 | }; |
| 165 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 166 | static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Guo Chao | b7a1da6 | 2013-02-21 15:16:50 -0800 | [diff] [blame] | 168 | loff_t loopsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* Compute loopsize in bytes */ |
Guo Chao | b7a1da6 | 2013-02-21 15:16:50 -0800 | [diff] [blame] | 171 | loopsize = i_size_read(file->f_mapping->host); |
| 172 | if (offset > 0) |
| 173 | loopsize -= offset; |
| 174 | /* offset is beyond i_size, weird but possible */ |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 175 | if (loopsize < 0) |
| 176 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 178 | if (sizelimit > 0 && sizelimit < loopsize) |
| 179 | loopsize = sizelimit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
| 181 | * Unfortunately, if we want to do I/O on the device, |
| 182 | * the number of 512-byte sectors has to fit into a sector_t. |
| 183 | */ |
| 184 | return loopsize >> 9; |
| 185 | } |
| 186 | |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 187 | static loff_t get_loop_size(struct loop_device *lo, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 189 | return get_size(lo->lo_offset, lo->lo_sizelimit, file); |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) |
| 194 | { |
| 195 | loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | sector_t x = (sector_t)size; |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 197 | struct block_device *bdev = lo->lo_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | if (unlikely((loff_t)x != size)) |
| 200 | return -EFBIG; |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 201 | if (lo->lo_offset != offset) |
| 202 | lo->lo_offset = offset; |
| 203 | if (lo->lo_sizelimit != sizelimit) |
| 204 | lo->lo_sizelimit = sizelimit; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 205 | set_capacity(lo->lo_disk, x); |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 206 | bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); |
| 207 | /* let user-space know about the new size */ |
| 208 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 209 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static inline int |
| 213 | lo_do_transfer(struct loop_device *lo, int cmd, |
| 214 | struct page *rpage, unsigned roffs, |
| 215 | struct page *lpage, unsigned loffs, |
| 216 | int size, sector_t rblock) |
| 217 | { |
| 218 | if (unlikely(!lo->transfer)) |
| 219 | return 0; |
| 220 | |
| 221 | return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); |
| 222 | } |
| 223 | |
| 224 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * __do_lo_send_write - helper for writing data to a loop device |
| 226 | * |
| 227 | * This helper just factors out common code between do_lo_send_direct_write() |
| 228 | * and do_lo_send_write(). |
| 229 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 230 | static int __do_lo_send_write(struct file *file, |
Al Viro | 98ae6ccd | 2006-10-10 22:45:07 +0100 | [diff] [blame] | 231 | u8 *buf, const int len, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | ssize_t bw; |
| 234 | mm_segment_t old_fs = get_fs(); |
| 235 | |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 236 | file_start_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | set_fs(get_ds()); |
| 238 | bw = file->f_op->write(file, buf, len, &pos); |
| 239 | set_fs(old_fs); |
Al Viro | 03d95eb | 2013-03-20 13:04:20 -0400 | [diff] [blame] | 240 | file_end_write(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (likely(bw == len)) |
| 242 | return 0; |
Mike Galbraith | 44bd70c | 2014-04-08 13:43:52 -0700 | [diff] [blame] | 243 | printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | (unsigned long long)pos, len); |
| 245 | if (bw >= 0) |
| 246 | bw = -EIO; |
| 247 | return bw; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * do_lo_send_direct_write - helper for writing data to a loop device |
| 252 | * |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 253 | * This is the fast, non-transforming version that does not need double |
| 254 | * buffering. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | */ |
| 256 | static int do_lo_send_direct_write(struct loop_device *lo, |
Al Viro | 511de73 | 2007-10-08 12:10:13 -0400 | [diff] [blame] | 257 | struct bio_vec *bvec, loff_t pos, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | ssize_t bw = __do_lo_send_write(lo->lo_backing_file, |
Al Viro | 98ae6ccd | 2006-10-10 22:45:07 +0100 | [diff] [blame] | 260 | kmap(bvec->bv_page) + bvec->bv_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | bvec->bv_len, pos); |
| 262 | kunmap(bvec->bv_page); |
| 263 | cond_resched(); |
| 264 | return bw; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * do_lo_send_write - helper for writing data to a loop device |
| 269 | * |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 270 | * This is the slow, transforming version that needs to double buffer the |
| 271 | * data as it cannot do the transformations in place without having direct |
| 272 | * access to the destination pages of the backing file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
| 274 | static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, |
Al Viro | 511de73 | 2007-10-08 12:10:13 -0400 | [diff] [blame] | 275 | loff_t pos, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, |
| 278 | bvec->bv_offset, bvec->bv_len, pos >> 9); |
| 279 | if (likely(!ret)) |
| 280 | return __do_lo_send_write(lo->lo_backing_file, |
Al Viro | 98ae6ccd | 2006-10-10 22:45:07 +0100 | [diff] [blame] | 281 | page_address(page), bvec->bv_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | pos); |
Mike Galbraith | 44bd70c | 2014-04-08 13:43:52 -0700 | [diff] [blame] | 283 | printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | "length %i.\n", (unsigned long long)pos, bvec->bv_len); |
| 285 | if (ret > 0) |
| 286 | ret = -EIO; |
| 287 | return ret; |
| 288 | } |
| 289 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 290 | static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Al Viro | 511de73 | 2007-10-08 12:10:13 -0400 | [diff] [blame] | 292 | int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | struct page *page); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 294 | struct bio_vec bvec; |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 295 | struct req_iterator iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | struct page *page = NULL; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 297 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 299 | if (lo->transfer != transfer_none) { |
| 300 | page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); |
| 301 | if (unlikely(!page)) |
| 302 | goto fail; |
| 303 | kmap(page); |
| 304 | do_lo_send = do_lo_send_write; |
| 305 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | do_lo_send = do_lo_send_direct_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 308 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 309 | rq_for_each_segment(bvec, rq, iter) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 310 | ret = do_lo_send(lo, &bvec, pos, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (ret < 0) |
| 312 | break; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 313 | pos += bvec.bv_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | if (page) { |
| 316 | kunmap(page); |
| 317 | __free_page(page); |
| 318 | } |
| 319 | out: |
| 320 | return ret; |
| 321 | fail: |
Mike Galbraith | 44bd70c | 2014-04-08 13:43:52 -0700 | [diff] [blame] | 322 | printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | ret = -ENOMEM; |
| 324 | goto out; |
| 325 | } |
| 326 | |
| 327 | struct lo_read_data { |
| 328 | struct loop_device *lo; |
| 329 | struct page *page; |
| 330 | unsigned offset; |
| 331 | int bsize; |
| 332 | }; |
| 333 | |
| 334 | static int |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 335 | lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 336 | struct splice_desc *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 338 | struct lo_read_data *p = sd->u.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | struct loop_device *lo = p->lo; |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 340 | struct page *page = buf->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | sector_t IV; |
Jens Axboe | 3603b8e | 2010-12-20 09:15:19 +0100 | [diff] [blame] | 342 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 344 | IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + |
| 345 | (buf->offset >> 9); |
| 346 | size = sd->len; |
| 347 | if (size > p->bsize) |
| 348 | size = p->bsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 350 | if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { |
Mike Galbraith | 44bd70c | 2014-04-08 13:43:52 -0700 | [diff] [blame] | 351 | printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | page->index); |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 353 | size = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | flush_dcache_page(p->page); |
| 357 | |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 358 | if (size > 0) |
| 359 | p->offset += size; |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return size; |
| 362 | } |
| 363 | |
| 364 | static int |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 365 | lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) |
| 366 | { |
| 367 | return __splice_from_pipe(pipe, sd, lo_splice_actor); |
| 368 | } |
| 369 | |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 370 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | do_lo_receive(struct loop_device *lo, |
| 372 | struct bio_vec *bvec, int bsize, loff_t pos) |
| 373 | { |
| 374 | struct lo_read_data cookie; |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 375 | struct splice_desc sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | struct file *file; |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 377 | ssize_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | cookie.lo = lo; |
| 380 | cookie.page = bvec->bv_page; |
| 381 | cookie.offset = bvec->bv_offset; |
| 382 | cookie.bsize = bsize; |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 383 | |
| 384 | sd.len = 0; |
| 385 | sd.total_len = bvec->bv_len; |
| 386 | sd.flags = 0; |
| 387 | sd.pos = pos; |
| 388 | sd.u.data = &cookie; |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | file = lo->lo_backing_file; |
Jens Axboe | fd58214 | 2007-06-12 21:20:37 +0200 | [diff] [blame] | 391 | retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); |
| 392 | |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 393 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 397 | lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 399 | struct bio_vec bvec; |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 400 | struct req_iterator iter; |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 401 | ssize_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 403 | rq_for_each_segment(bvec, rq, iter) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 404 | s = do_lo_receive(lo, &bvec, bsize, pos); |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 405 | if (s < 0) |
| 406 | return s; |
| 407 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 408 | if (s != bvec.bv_len) { |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 409 | struct bio *bio; |
| 410 | |
| 411 | __rq_for_each_bio(bio, rq) |
| 412 | zero_fill_bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | break; |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 414 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 415 | pos += bvec.bv_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
Dave Young | 306df07 | 2012-02-08 22:07:19 +0100 | [diff] [blame] | 417 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 420 | static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) |
| 421 | { |
| 422 | /* |
| 423 | * We use punch hole to reclaim the free space used by the |
| 424 | * image a.k.a. discard. However we do not support discard if |
| 425 | * encryption is enabled, because it may give an attacker |
| 426 | * useful information. |
| 427 | */ |
| 428 | struct file *file = lo->lo_backing_file; |
| 429 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; |
| 430 | int ret; |
| 431 | |
| 432 | if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { |
| 433 | ret = -EOPNOTSUPP; |
| 434 | goto out; |
| 435 | } |
| 436 | |
| 437 | ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); |
| 438 | if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) |
| 439 | ret = -EIO; |
| 440 | out: |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static int lo_req_flush(struct loop_device *lo, struct request *rq) |
| 445 | { |
| 446 | struct file *file = lo->lo_backing_file; |
| 447 | int ret = vfs_fsync(file, 0); |
| 448 | if (unlikely(ret && ret != -EINVAL)) |
| 449 | ret = -EIO; |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 454 | static int do_req_filebacked(struct loop_device *lo, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | loff_t pos; |
| 457 | int ret; |
| 458 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 459 | pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 460 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 461 | if (rq->cmd_flags & REQ_WRITE) { |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 462 | if (rq->cmd_flags & REQ_FLUSH) |
| 463 | ret = lo_req_flush(lo, rq); |
Ming Lei | af65aa8 | 2014-12-31 13:23:00 +0000 | [diff] [blame^] | 464 | else if (rq->cmd_flags & REQ_DISCARD) |
Ming Lei | cf655d9 | 2014-12-31 13:22:59 +0000 | [diff] [blame] | 465 | ret = lo_discard(lo, rq, pos); |
Ming Lei | af65aa8 | 2014-12-31 13:23:00 +0000 | [diff] [blame^] | 466 | else |
| 467 | ret = lo_send(lo, rq, pos); |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 468 | } else |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 469 | ret = lo_receive(lo, rq, lo->lo_blocksize, pos); |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | return ret; |
| 472 | } |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | struct switch_request { |
| 475 | struct file *file; |
| 476 | struct completion wait; |
| 477 | }; |
| 478 | |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 479 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | * Do the actual switch; called from the BIO completion routine |
| 481 | */ |
| 482 | static void do_loop_switch(struct loop_device *lo, struct switch_request *p) |
| 483 | { |
| 484 | struct file *file = p->file; |
| 485 | struct file *old_file = lo->lo_backing_file; |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 486 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 488 | /* if no new file, only flush of queued bios requested */ |
| 489 | if (!file) |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 490 | return; |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 491 | |
| 492 | mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); |
| 494 | lo->lo_backing_file = file; |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 495 | lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? |
| 496 | mapping->host->i_bdev->bd_block_size : PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | lo->old_gfp_mask = mapping_gfp_mask(mapping); |
| 498 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 501 | /* |
| 502 | * loop_switch performs the hard work of switching a backing store. |
| 503 | * First it needs to flush existing IO, it does this by sending a magic |
| 504 | * BIO down the pipe. The completion of this BIO does the actual switch. |
| 505 | */ |
| 506 | static int loop_switch(struct loop_device *lo, struct file *file) |
| 507 | { |
| 508 | struct switch_request w; |
| 509 | |
| 510 | w.file = file; |
| 511 | |
| 512 | /* freeze queue and wait for completion of scheduled requests */ |
| 513 | blk_mq_freeze_queue(lo->lo_queue); |
| 514 | |
| 515 | /* do the switch action */ |
| 516 | do_loop_switch(lo, &w); |
| 517 | |
| 518 | /* unfreeze */ |
| 519 | blk_mq_unfreeze_queue(lo->lo_queue); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Helper to flush the IOs in loop, but keeping loop thread running |
| 526 | */ |
| 527 | static int loop_flush(struct loop_device *lo) |
| 528 | { |
| 529 | return loop_switch(lo, NULL); |
| 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * loop_change_fd switched the backing store of a loopback device to |
| 534 | * a new file. This is useful for operating system installers to free up |
| 535 | * the original file and in High Availability environments to switch to |
| 536 | * an alternative location for the content in case of server meltdown. |
| 537 | * This can only work if the loop device is used read-only, and if the |
| 538 | * new backing store is the same size and type as the old backing store. |
| 539 | */ |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 540 | static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, |
| 541 | unsigned int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | struct file *file, *old_file; |
| 544 | struct inode *inode; |
| 545 | int error; |
| 546 | |
| 547 | error = -ENXIO; |
| 548 | if (lo->lo_state != Lo_bound) |
| 549 | goto out; |
| 550 | |
| 551 | /* the loop device has to be read-only */ |
| 552 | error = -EINVAL; |
| 553 | if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) |
| 554 | goto out; |
| 555 | |
| 556 | error = -EBADF; |
| 557 | file = fget(arg); |
| 558 | if (!file) |
| 559 | goto out; |
| 560 | |
| 561 | inode = file->f_mapping->host; |
| 562 | old_file = lo->lo_backing_file; |
| 563 | |
| 564 | error = -EINVAL; |
| 565 | |
| 566 | if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) |
| 567 | goto out_putf; |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /* size of the new backing store needs to be the same */ |
| 570 | if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) |
| 571 | goto out_putf; |
| 572 | |
| 573 | /* and ... switch */ |
| 574 | error = loop_switch(lo, file); |
| 575 | if (error) |
| 576 | goto out_putf; |
| 577 | |
| 578 | fput(old_file); |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 579 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 580 | ioctl_by_bdev(bdev, BLKRRPART, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | |
| 583 | out_putf: |
| 584 | fput(file); |
| 585 | out: |
| 586 | return error; |
| 587 | } |
| 588 | |
| 589 | static inline int is_loop_device(struct file *file) |
| 590 | { |
| 591 | struct inode *i = file->f_mapping->host; |
| 592 | |
| 593 | return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; |
| 594 | } |
| 595 | |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 596 | /* loop sysfs attributes */ |
| 597 | |
| 598 | static ssize_t loop_attr_show(struct device *dev, char *page, |
| 599 | ssize_t (*callback)(struct loop_device *, char *)) |
| 600 | { |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 601 | struct gendisk *disk = dev_to_disk(dev); |
| 602 | struct loop_device *lo = disk->private_data; |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 603 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 604 | return callback(lo, page); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | #define LOOP_ATTR_RO(_name) \ |
| 608 | static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ |
| 609 | static ssize_t loop_attr_do_show_##_name(struct device *d, \ |
| 610 | struct device_attribute *attr, char *b) \ |
| 611 | { \ |
| 612 | return loop_attr_show(d, b, loop_attr_##_name##_show); \ |
| 613 | } \ |
| 614 | static struct device_attribute loop_attr_##_name = \ |
| 615 | __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); |
| 616 | |
| 617 | static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) |
| 618 | { |
| 619 | ssize_t ret; |
| 620 | char *p = NULL; |
| 621 | |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 622 | spin_lock_irq(&lo->lo_lock); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 623 | if (lo->lo_backing_file) |
| 624 | p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1); |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 625 | spin_unlock_irq(&lo->lo_lock); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 626 | |
| 627 | if (IS_ERR_OR_NULL(p)) |
| 628 | ret = PTR_ERR(p); |
| 629 | else { |
| 630 | ret = strlen(p); |
| 631 | memmove(buf, p, ret); |
| 632 | buf[ret++] = '\n'; |
| 633 | buf[ret] = 0; |
| 634 | } |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) |
| 640 | { |
| 641 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); |
| 642 | } |
| 643 | |
| 644 | static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) |
| 645 | { |
| 646 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); |
| 647 | } |
| 648 | |
| 649 | static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) |
| 650 | { |
| 651 | int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); |
| 652 | |
| 653 | return sprintf(buf, "%s\n", autoclear ? "1" : "0"); |
| 654 | } |
| 655 | |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 656 | static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) |
| 657 | { |
| 658 | int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); |
| 659 | |
| 660 | return sprintf(buf, "%s\n", partscan ? "1" : "0"); |
| 661 | } |
| 662 | |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 663 | LOOP_ATTR_RO(backing_file); |
| 664 | LOOP_ATTR_RO(offset); |
| 665 | LOOP_ATTR_RO(sizelimit); |
| 666 | LOOP_ATTR_RO(autoclear); |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 667 | LOOP_ATTR_RO(partscan); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 668 | |
| 669 | static struct attribute *loop_attrs[] = { |
| 670 | &loop_attr_backing_file.attr, |
| 671 | &loop_attr_offset.attr, |
| 672 | &loop_attr_sizelimit.attr, |
| 673 | &loop_attr_autoclear.attr, |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 674 | &loop_attr_partscan.attr, |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 675 | NULL, |
| 676 | }; |
| 677 | |
| 678 | static struct attribute_group loop_attribute_group = { |
| 679 | .name = "loop", |
| 680 | .attrs= loop_attrs, |
| 681 | }; |
| 682 | |
| 683 | static int loop_sysfs_init(struct loop_device *lo) |
| 684 | { |
| 685 | return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, |
| 686 | &loop_attribute_group); |
| 687 | } |
| 688 | |
| 689 | static void loop_sysfs_exit(struct loop_device *lo) |
| 690 | { |
| 691 | sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, |
| 692 | &loop_attribute_group); |
| 693 | } |
| 694 | |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 695 | static void loop_config_discard(struct loop_device *lo) |
| 696 | { |
| 697 | struct file *file = lo->lo_backing_file; |
| 698 | struct inode *inode = file->f_mapping->host; |
| 699 | struct request_queue *q = lo->lo_queue; |
| 700 | |
| 701 | /* |
| 702 | * We use punch hole to reclaim the free space used by the |
Olaf Hering | 12a64d2 | 2014-01-21 14:39:24 -0800 | [diff] [blame] | 703 | * image a.k.a. discard. However we do not support discard if |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 704 | * encryption is enabled, because it may give an attacker |
| 705 | * useful information. |
| 706 | */ |
| 707 | if ((!file->f_op->fallocate) || |
| 708 | lo->lo_encrypt_key_size) { |
| 709 | q->limits.discard_granularity = 0; |
| 710 | q->limits.discard_alignment = 0; |
| 711 | q->limits.max_discard_sectors = 0; |
| 712 | q->limits.discard_zeroes_data = 0; |
| 713 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | q->limits.discard_granularity = inode->i_sb->s_blocksize; |
Lukas Czerner | dfaf3c0 | 2011-12-02 14:47:03 +0100 | [diff] [blame] | 718 | q->limits.discard_alignment = 0; |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 719 | q->limits.max_discard_sectors = UINT_MAX >> 9; |
| 720 | q->limits.discard_zeroes_data = 1; |
| 721 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 722 | } |
| 723 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 724 | static int loop_set_fd(struct loop_device *lo, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | struct block_device *bdev, unsigned int arg) |
| 726 | { |
| 727 | struct file *file, *f; |
| 728 | struct inode *inode; |
| 729 | struct address_space *mapping; |
| 730 | unsigned lo_blocksize; |
| 731 | int lo_flags = 0; |
| 732 | int error; |
| 733 | loff_t size; |
| 734 | |
| 735 | /* This is safe, since we have a reference from open(). */ |
| 736 | __module_get(THIS_MODULE); |
| 737 | |
| 738 | error = -EBADF; |
| 739 | file = fget(arg); |
| 740 | if (!file) |
| 741 | goto out; |
| 742 | |
| 743 | error = -EBUSY; |
| 744 | if (lo->lo_state != Lo_unbound) |
| 745 | goto out_putf; |
| 746 | |
| 747 | /* Avoid recursion */ |
| 748 | f = file; |
| 749 | while (is_loop_device(f)) { |
| 750 | struct loop_device *l; |
| 751 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 752 | if (f->f_mapping->host->i_bdev == bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | goto out_putf; |
| 754 | |
| 755 | l = f->f_mapping->host->i_bdev->bd_disk->private_data; |
| 756 | if (l->lo_state == Lo_unbound) { |
| 757 | error = -EINVAL; |
| 758 | goto out_putf; |
| 759 | } |
| 760 | f = l->lo_backing_file; |
| 761 | } |
| 762 | |
| 763 | mapping = file->f_mapping; |
| 764 | inode = mapping->host; |
| 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | error = -EINVAL; |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 767 | if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | goto out_putf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 770 | if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || |
| 771 | !file->f_op->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | lo_flags |= LO_FLAGS_READ_ONLY; |
| 773 | |
Christoph Hellwig | 456be14 | 2011-10-17 12:57:20 +0200 | [diff] [blame] | 774 | lo_blocksize = S_ISBLK(inode->i_mode) ? |
| 775 | inode->i_bdev->bd_block_size : PAGE_SIZE; |
| 776 | |
| 777 | error = -EFBIG; |
| 778 | size = get_loop_size(lo, file); |
| 779 | if ((loff_t)(sector_t)size != size) |
| 780 | goto out_putf; |
| 781 | |
| 782 | error = 0; |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); |
| 785 | |
| 786 | lo->lo_blocksize = lo_blocksize; |
| 787 | lo->lo_device = bdev; |
| 788 | lo->lo_flags = lo_flags; |
| 789 | lo->lo_backing_file = file; |
Constantine Sapuntzakis | eefe85e | 2006-06-23 02:06:08 -0700 | [diff] [blame] | 790 | lo->transfer = transfer_none; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | lo->ioctl = NULL; |
| 792 | lo->lo_sizelimit = 0; |
| 793 | lo->old_gfp_mask = mapping_gfp_mask(mapping); |
| 794 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); |
| 795 | |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 796 | if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 797 | blk_queue_flush(lo->lo_queue, REQ_FLUSH); |
Nikanth Karthikesan | 68db196 | 2009-03-24 12:29:54 +0100 | [diff] [blame] | 798 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 799 | set_capacity(lo->lo_disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | bd_set_size(bdev, size << 9); |
Milan Broz | ee86273 | 2010-08-23 15:16:00 +0200 | [diff] [blame] | 801 | loop_sysfs_init(lo); |
David Zeuthen | c3473c6 | 2010-05-03 14:08:59 +0200 | [diff] [blame] | 802 | /* let user-space know about the new size */ |
| 803 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | set_blocksize(bdev, lo_blocksize); |
| 806 | |
Serge E. Hallyn | 6c99791 | 2006-09-29 01:59:11 -0700 | [diff] [blame] | 807 | lo->lo_state = Lo_bound; |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 808 | if (part_shift) |
| 809 | lo->lo_flags |= LO_FLAGS_PARTSCAN; |
| 810 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 811 | ioctl_by_bdev(bdev, BLKRRPART, 0); |
Anatol Pomozov | c1681bf | 2013-04-01 09:47:56 -0700 | [diff] [blame] | 812 | |
| 813 | /* Grab the block_device to prevent its destruction after we |
| 814 | * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev). |
| 815 | */ |
| 816 | bdgrab(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | return 0; |
| 818 | |
| 819 | out_putf: |
| 820 | fput(file); |
| 821 | out: |
| 822 | /* This is safe: open() is still holding a reference. */ |
| 823 | module_put(THIS_MODULE); |
| 824 | return error; |
| 825 | } |
| 826 | |
| 827 | static int |
| 828 | loop_release_xfer(struct loop_device *lo) |
| 829 | { |
| 830 | int err = 0; |
| 831 | struct loop_func_table *xfer = lo->lo_encryption; |
| 832 | |
| 833 | if (xfer) { |
| 834 | if (xfer->release) |
| 835 | err = xfer->release(lo); |
| 836 | lo->transfer = NULL; |
| 837 | lo->lo_encryption = NULL; |
| 838 | module_put(xfer->owner); |
| 839 | } |
| 840 | return err; |
| 841 | } |
| 842 | |
| 843 | static int |
| 844 | loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, |
| 845 | const struct loop_info64 *i) |
| 846 | { |
| 847 | int err = 0; |
| 848 | |
| 849 | if (xfer) { |
| 850 | struct module *owner = xfer->owner; |
| 851 | |
| 852 | if (!try_module_get(owner)) |
| 853 | return -EINVAL; |
| 854 | if (xfer->init) |
| 855 | err = xfer->init(lo, i); |
| 856 | if (err) |
| 857 | module_put(owner); |
| 858 | else |
| 859 | lo->lo_encryption = xfer; |
| 860 | } |
| 861 | return err; |
| 862 | } |
| 863 | |
Ayan George | 4c823cc | 2011-09-21 10:02:13 +0200 | [diff] [blame] | 864 | static int loop_clr_fd(struct loop_device *lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { |
| 866 | struct file *filp = lo->lo_backing_file; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 867 | gfp_t gfp = lo->old_gfp_mask; |
Ayan George | 4c823cc | 2011-09-21 10:02:13 +0200 | [diff] [blame] | 868 | struct block_device *bdev = lo->lo_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
| 870 | if (lo->lo_state != Lo_bound) |
| 871 | return -ENXIO; |
| 872 | |
Dave Chinner | a1ecac3 | 2012-09-28 10:42:23 +0200 | [diff] [blame] | 873 | /* |
| 874 | * If we've explicitly asked to tear down the loop device, |
| 875 | * and it has an elevated reference count, set it for auto-teardown when |
| 876 | * the last reference goes away. This stops $!~#$@ udev from |
| 877 | * preventing teardown because it decided that it needs to run blkid on |
| 878 | * the loopback device whenever they appear. xfstests is notorious for |
| 879 | * failing tests because blkid via udev races with a losetup |
| 880 | * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d |
| 881 | * command to fail with EBUSY. |
| 882 | */ |
| 883 | if (lo->lo_refcnt > 1) { |
| 884 | lo->lo_flags |= LO_FLAGS_AUTOCLEAR; |
| 885 | mutex_unlock(&lo->lo_ctl_mutex); |
| 886 | return 0; |
| 887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | if (filp == NULL) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | spin_lock_irq(&lo->lo_lock); |
| 893 | lo->lo_state = Lo_rundown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | lo->lo_backing_file = NULL; |
Kay Sievers | 05eb0f2 | 2011-07-31 22:21:35 +0200 | [diff] [blame] | 895 | spin_unlock_irq(&lo->lo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
| 897 | loop_release_xfer(lo); |
| 898 | lo->transfer = NULL; |
| 899 | lo->ioctl = NULL; |
| 900 | lo->lo_device = NULL; |
| 901 | lo->lo_encryption = NULL; |
| 902 | lo->lo_offset = 0; |
| 903 | lo->lo_sizelimit = 0; |
| 904 | lo->lo_encrypt_key_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); |
| 906 | memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); |
| 907 | memset(lo->lo_file_name, 0, LO_NAME_SIZE); |
Anatol Pomozov | c1681bf | 2013-04-01 09:47:56 -0700 | [diff] [blame] | 908 | if (bdev) { |
| 909 | bdput(bdev); |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 910 | invalidate_bdev(bdev); |
Anatol Pomozov | c1681bf | 2013-04-01 09:47:56 -0700 | [diff] [blame] | 911 | } |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 912 | set_capacity(lo->lo_disk, 0); |
Milan Broz | 51a0bb0 | 2010-10-27 19:51:30 -0600 | [diff] [blame] | 913 | loop_sysfs_exit(lo); |
David Zeuthen | c3473c6 | 2010-05-03 14:08:59 +0200 | [diff] [blame] | 914 | if (bdev) { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 915 | bd_set_size(bdev, 0); |
David Zeuthen | c3473c6 | 2010-05-03 14:08:59 +0200 | [diff] [blame] | 916 | /* let user-space know about this change */ |
| 917 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); |
| 918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | mapping_set_gfp_mask(filp->f_mapping, gfp); |
| 920 | lo->lo_state = Lo_unbound; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | /* This is safe: open() is still holding a reference. */ |
| 922 | module_put(THIS_MODULE); |
Jens Axboe | c2fccc1 | 2013-04-08 10:12:11 +0200 | [diff] [blame] | 923 | if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) |
| 924 | ioctl_by_bdev(bdev, BLKRRPART, 0); |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 925 | lo->lo_flags = 0; |
| 926 | if (!part_shift) |
| 927 | lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 928 | mutex_unlock(&lo->lo_ctl_mutex); |
| 929 | /* |
| 930 | * Need not hold lo_ctl_mutex to fput backing file. |
| 931 | * Calling fput holding lo_ctl_mutex triggers a circular |
| 932 | * lock dependency possibility warning as fput can take |
| 933 | * bd_mutex which is usually taken before lo_ctl_mutex. |
| 934 | */ |
| 935 | fput(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static int |
| 940 | loop_set_status(struct loop_device *lo, const struct loop_info64 *info) |
| 941 | { |
| 942 | int err; |
| 943 | struct loop_func_table *xfer; |
Eric W. Biederman | e484973 | 2012-02-11 11:23:51 -0800 | [diff] [blame] | 944 | kuid_t uid = current_uid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
David Howells | b0fafa8 | 2008-11-14 10:38:41 +1100 | [diff] [blame] | 946 | if (lo->lo_encrypt_key_size && |
Eric W. Biederman | e484973 | 2012-02-11 11:23:51 -0800 | [diff] [blame] | 947 | !uid_eq(lo->lo_key_owner, uid) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | !capable(CAP_SYS_ADMIN)) |
| 949 | return -EPERM; |
| 950 | if (lo->lo_state != Lo_bound) |
| 951 | return -ENXIO; |
| 952 | if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) |
| 953 | return -EINVAL; |
| 954 | |
| 955 | err = loop_release_xfer(lo); |
| 956 | if (err) |
| 957 | return err; |
| 958 | |
| 959 | if (info->lo_encrypt_type) { |
| 960 | unsigned int type = info->lo_encrypt_type; |
| 961 | |
| 962 | if (type >= MAX_LO_CRYPT) |
| 963 | return -EINVAL; |
| 964 | xfer = xfer_funcs[type]; |
| 965 | if (xfer == NULL) |
| 966 | return -EINVAL; |
| 967 | } else |
| 968 | xfer = NULL; |
| 969 | |
| 970 | err = loop_init_xfer(lo, xfer, info); |
| 971 | if (err) |
| 972 | return err; |
| 973 | |
| 974 | if (lo->lo_offset != info->lo_offset || |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 975 | lo->lo_sizelimit != info->lo_sizelimit) |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 976 | if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return -EFBIG; |
Guo Chao | 541c742 | 2013-02-21 15:16:46 -0800 | [diff] [blame] | 978 | |
Lukas Czerner | dfaa2ef | 2011-08-19 14:50:46 +0200 | [diff] [blame] | 979 | loop_config_discard(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
| 981 | memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); |
| 982 | memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); |
| 983 | lo->lo_file_name[LO_NAME_SIZE-1] = 0; |
| 984 | lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; |
| 985 | |
| 986 | if (!xfer) |
| 987 | xfer = &none_funcs; |
| 988 | lo->transfer = xfer->transfer; |
| 989 | lo->ioctl = xfer->ioctl; |
| 990 | |
David Woodhouse | 96c5865 | 2008-02-06 01:36:27 -0800 | [diff] [blame] | 991 | if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != |
| 992 | (info->lo_flags & LO_FLAGS_AUTOCLEAR)) |
| 993 | lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; |
| 994 | |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 995 | if ((info->lo_flags & LO_FLAGS_PARTSCAN) && |
| 996 | !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { |
| 997 | lo->lo_flags |= LO_FLAGS_PARTSCAN; |
| 998 | lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; |
| 999 | ioctl_by_bdev(lo->lo_device, BLKRRPART, 0); |
| 1000 | } |
| 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | lo->lo_encrypt_key_size = info->lo_encrypt_key_size; |
| 1003 | lo->lo_init[0] = info->lo_init[0]; |
| 1004 | lo->lo_init[1] = info->lo_init[1]; |
| 1005 | if (info->lo_encrypt_key_size) { |
| 1006 | memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, |
| 1007 | info->lo_encrypt_key_size); |
David Howells | b0fafa8 | 2008-11-14 10:38:41 +1100 | [diff] [blame] | 1008 | lo->lo_key_owner = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static int |
| 1015 | loop_get_status(struct loop_device *lo, struct loop_info64 *info) |
| 1016 | { |
| 1017 | struct file *file = lo->lo_backing_file; |
| 1018 | struct kstat stat; |
| 1019 | int error; |
| 1020 | |
| 1021 | if (lo->lo_state != Lo_bound) |
| 1022 | return -ENXIO; |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 1023 | error = vfs_getattr(&file->f_path, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | if (error) |
| 1025 | return error; |
| 1026 | memset(info, 0, sizeof(*info)); |
| 1027 | info->lo_number = lo->lo_number; |
| 1028 | info->lo_device = huge_encode_dev(stat.dev); |
| 1029 | info->lo_inode = stat.ino; |
| 1030 | info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); |
| 1031 | info->lo_offset = lo->lo_offset; |
| 1032 | info->lo_sizelimit = lo->lo_sizelimit; |
| 1033 | info->lo_flags = lo->lo_flags; |
| 1034 | memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); |
| 1035 | memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); |
| 1036 | info->lo_encrypt_type = |
| 1037 | lo->lo_encryption ? lo->lo_encryption->number : 0; |
| 1038 | if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { |
| 1039 | info->lo_encrypt_key_size = lo->lo_encrypt_key_size; |
| 1040 | memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, |
| 1041 | lo->lo_encrypt_key_size); |
| 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static void |
| 1047 | loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) |
| 1048 | { |
| 1049 | memset(info64, 0, sizeof(*info64)); |
| 1050 | info64->lo_number = info->lo_number; |
| 1051 | info64->lo_device = info->lo_device; |
| 1052 | info64->lo_inode = info->lo_inode; |
| 1053 | info64->lo_rdevice = info->lo_rdevice; |
| 1054 | info64->lo_offset = info->lo_offset; |
| 1055 | info64->lo_sizelimit = 0; |
| 1056 | info64->lo_encrypt_type = info->lo_encrypt_type; |
| 1057 | info64->lo_encrypt_key_size = info->lo_encrypt_key_size; |
| 1058 | info64->lo_flags = info->lo_flags; |
| 1059 | info64->lo_init[0] = info->lo_init[0]; |
| 1060 | info64->lo_init[1] = info->lo_init[1]; |
| 1061 | if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) |
| 1062 | memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); |
| 1063 | else |
| 1064 | memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); |
| 1065 | memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); |
| 1066 | } |
| 1067 | |
| 1068 | static int |
| 1069 | loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) |
| 1070 | { |
| 1071 | memset(info, 0, sizeof(*info)); |
| 1072 | info->lo_number = info64->lo_number; |
| 1073 | info->lo_device = info64->lo_device; |
| 1074 | info->lo_inode = info64->lo_inode; |
| 1075 | info->lo_rdevice = info64->lo_rdevice; |
| 1076 | info->lo_offset = info64->lo_offset; |
| 1077 | info->lo_encrypt_type = info64->lo_encrypt_type; |
| 1078 | info->lo_encrypt_key_size = info64->lo_encrypt_key_size; |
| 1079 | info->lo_flags = info64->lo_flags; |
| 1080 | info->lo_init[0] = info64->lo_init[0]; |
| 1081 | info->lo_init[1] = info64->lo_init[1]; |
| 1082 | if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) |
| 1083 | memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); |
| 1084 | else |
| 1085 | memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); |
| 1086 | memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); |
| 1087 | |
| 1088 | /* error in case values were truncated */ |
| 1089 | if (info->lo_device != info64->lo_device || |
| 1090 | info->lo_rdevice != info64->lo_rdevice || |
| 1091 | info->lo_inode != info64->lo_inode || |
| 1092 | info->lo_offset != info64->lo_offset) |
| 1093 | return -EOVERFLOW; |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static int |
| 1099 | loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) |
| 1100 | { |
| 1101 | struct loop_info info; |
| 1102 | struct loop_info64 info64; |
| 1103 | |
| 1104 | if (copy_from_user(&info, arg, sizeof (struct loop_info))) |
| 1105 | return -EFAULT; |
| 1106 | loop_info64_from_old(&info, &info64); |
| 1107 | return loop_set_status(lo, &info64); |
| 1108 | } |
| 1109 | |
| 1110 | static int |
| 1111 | loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) |
| 1112 | { |
| 1113 | struct loop_info64 info64; |
| 1114 | |
| 1115 | if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) |
| 1116 | return -EFAULT; |
| 1117 | return loop_set_status(lo, &info64); |
| 1118 | } |
| 1119 | |
| 1120 | static int |
| 1121 | loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { |
| 1122 | struct loop_info info; |
| 1123 | struct loop_info64 info64; |
| 1124 | int err = 0; |
| 1125 | |
| 1126 | if (!arg) |
| 1127 | err = -EINVAL; |
| 1128 | if (!err) |
| 1129 | err = loop_get_status(lo, &info64); |
| 1130 | if (!err) |
| 1131 | err = loop_info64_to_old(&info64, &info); |
| 1132 | if (!err && copy_to_user(arg, &info, sizeof(info))) |
| 1133 | err = -EFAULT; |
| 1134 | |
| 1135 | return err; |
| 1136 | } |
| 1137 | |
| 1138 | static int |
| 1139 | loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { |
| 1140 | struct loop_info64 info64; |
| 1141 | int err = 0; |
| 1142 | |
| 1143 | if (!arg) |
| 1144 | err = -EINVAL; |
| 1145 | if (!err) |
| 1146 | err = loop_get_status(lo, &info64); |
| 1147 | if (!err && copy_to_user(arg, &info64, sizeof(info64))) |
| 1148 | err = -EFAULT; |
| 1149 | |
| 1150 | return err; |
| 1151 | } |
| 1152 | |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1153 | static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) |
| 1154 | { |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1155 | if (unlikely(lo->lo_state != Lo_bound)) |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 1156 | return -ENXIO; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1157 | |
Guo Chao | 7b0576a | 2013-02-21 15:16:47 -0800 | [diff] [blame] | 1158 | return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1161 | static int lo_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | unsigned int cmd, unsigned long arg) |
| 1163 | { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1164 | struct loop_device *lo = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | int err; |
| 1166 | |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 1167 | mutex_lock_nested(&lo->lo_ctl_mutex, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | switch (cmd) { |
| 1169 | case LOOP_SET_FD: |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1170 | err = loop_set_fd(lo, mode, bdev, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | case LOOP_CHANGE_FD: |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1173 | err = loop_change_fd(lo, bdev, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | case LOOP_CLR_FD: |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 1176 | /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ |
Ayan George | 4c823cc | 2011-09-21 10:02:13 +0200 | [diff] [blame] | 1177 | err = loop_clr_fd(lo); |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 1178 | if (!err) |
| 1179 | goto out_unlocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | break; |
| 1181 | case LOOP_SET_STATUS: |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 1182 | err = -EPERM; |
| 1183 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) |
| 1184 | err = loop_set_status_old(lo, |
| 1185 | (struct loop_info __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | break; |
| 1187 | case LOOP_GET_STATUS: |
| 1188 | err = loop_get_status_old(lo, (struct loop_info __user *) arg); |
| 1189 | break; |
| 1190 | case LOOP_SET_STATUS64: |
Dmitry Monakhov | 7035b5d | 2011-11-16 09:21:49 +0100 | [diff] [blame] | 1191 | err = -EPERM; |
| 1192 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) |
| 1193 | err = loop_set_status64(lo, |
| 1194 | (struct loop_info64 __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | break; |
| 1196 | case LOOP_GET_STATUS64: |
| 1197 | err = loop_get_status64(lo, (struct loop_info64 __user *) arg); |
| 1198 | break; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1199 | case LOOP_SET_CAPACITY: |
| 1200 | err = -EPERM; |
| 1201 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) |
| 1202 | err = loop_set_capacity(lo, bdev); |
| 1203 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | default: |
| 1205 | err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; |
| 1206 | } |
Ingo Molnar | f85221d | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1207 | mutex_unlock(&lo->lo_ctl_mutex); |
Nikanth Karthikesan | f028f3b | 2009-03-24 12:33:41 +0100 | [diff] [blame] | 1208 | |
| 1209 | out_unlocked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | return err; |
| 1211 | } |
| 1212 | |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1213 | #ifdef CONFIG_COMPAT |
| 1214 | struct compat_loop_info { |
| 1215 | compat_int_t lo_number; /* ioctl r/o */ |
| 1216 | compat_dev_t lo_device; /* ioctl r/o */ |
| 1217 | compat_ulong_t lo_inode; /* ioctl r/o */ |
| 1218 | compat_dev_t lo_rdevice; /* ioctl r/o */ |
| 1219 | compat_int_t lo_offset; |
| 1220 | compat_int_t lo_encrypt_type; |
| 1221 | compat_int_t lo_encrypt_key_size; /* ioctl w/o */ |
| 1222 | compat_int_t lo_flags; /* ioctl r/o */ |
| 1223 | char lo_name[LO_NAME_SIZE]; |
| 1224 | unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ |
| 1225 | compat_ulong_t lo_init[2]; |
| 1226 | char reserved[4]; |
| 1227 | }; |
| 1228 | |
| 1229 | /* |
| 1230 | * Transfer 32-bit compatibility structure in userspace to 64-bit loop info |
| 1231 | * - noinlined to reduce stack space usage in main part of driver |
| 1232 | */ |
| 1233 | static noinline int |
Al Viro | ba674cf | 2006-10-10 22:48:27 +0100 | [diff] [blame] | 1234 | loop_info64_from_compat(const struct compat_loop_info __user *arg, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1235 | struct loop_info64 *info64) |
| 1236 | { |
| 1237 | struct compat_loop_info info; |
| 1238 | |
| 1239 | if (copy_from_user(&info, arg, sizeof(info))) |
| 1240 | return -EFAULT; |
| 1241 | |
| 1242 | memset(info64, 0, sizeof(*info64)); |
| 1243 | info64->lo_number = info.lo_number; |
| 1244 | info64->lo_device = info.lo_device; |
| 1245 | info64->lo_inode = info.lo_inode; |
| 1246 | info64->lo_rdevice = info.lo_rdevice; |
| 1247 | info64->lo_offset = info.lo_offset; |
| 1248 | info64->lo_sizelimit = 0; |
| 1249 | info64->lo_encrypt_type = info.lo_encrypt_type; |
| 1250 | info64->lo_encrypt_key_size = info.lo_encrypt_key_size; |
| 1251 | info64->lo_flags = info.lo_flags; |
| 1252 | info64->lo_init[0] = info.lo_init[0]; |
| 1253 | info64->lo_init[1] = info.lo_init[1]; |
| 1254 | if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) |
| 1255 | memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); |
| 1256 | else |
| 1257 | memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); |
| 1258 | memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Transfer 64-bit loop info to 32-bit compatibility structure in userspace |
| 1264 | * - noinlined to reduce stack space usage in main part of driver |
| 1265 | */ |
| 1266 | static noinline int |
| 1267 | loop_info64_to_compat(const struct loop_info64 *info64, |
| 1268 | struct compat_loop_info __user *arg) |
| 1269 | { |
| 1270 | struct compat_loop_info info; |
| 1271 | |
| 1272 | memset(&info, 0, sizeof(info)); |
| 1273 | info.lo_number = info64->lo_number; |
| 1274 | info.lo_device = info64->lo_device; |
| 1275 | info.lo_inode = info64->lo_inode; |
| 1276 | info.lo_rdevice = info64->lo_rdevice; |
| 1277 | info.lo_offset = info64->lo_offset; |
| 1278 | info.lo_encrypt_type = info64->lo_encrypt_type; |
| 1279 | info.lo_encrypt_key_size = info64->lo_encrypt_key_size; |
| 1280 | info.lo_flags = info64->lo_flags; |
| 1281 | info.lo_init[0] = info64->lo_init[0]; |
| 1282 | info.lo_init[1] = info64->lo_init[1]; |
| 1283 | if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) |
| 1284 | memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); |
| 1285 | else |
| 1286 | memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); |
| 1287 | memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); |
| 1288 | |
| 1289 | /* error in case values were truncated */ |
| 1290 | if (info.lo_device != info64->lo_device || |
| 1291 | info.lo_rdevice != info64->lo_rdevice || |
| 1292 | info.lo_inode != info64->lo_inode || |
| 1293 | info.lo_offset != info64->lo_offset || |
| 1294 | info.lo_init[0] != info64->lo_init[0] || |
| 1295 | info.lo_init[1] != info64->lo_init[1]) |
| 1296 | return -EOVERFLOW; |
| 1297 | |
| 1298 | if (copy_to_user(arg, &info, sizeof(info))) |
| 1299 | return -EFAULT; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static int |
| 1304 | loop_set_status_compat(struct loop_device *lo, |
| 1305 | const struct compat_loop_info __user *arg) |
| 1306 | { |
| 1307 | struct loop_info64 info64; |
| 1308 | int ret; |
| 1309 | |
| 1310 | ret = loop_info64_from_compat(arg, &info64); |
| 1311 | if (ret < 0) |
| 1312 | return ret; |
| 1313 | return loop_set_status(lo, &info64); |
| 1314 | } |
| 1315 | |
| 1316 | static int |
| 1317 | loop_get_status_compat(struct loop_device *lo, |
| 1318 | struct compat_loop_info __user *arg) |
| 1319 | { |
| 1320 | struct loop_info64 info64; |
| 1321 | int err = 0; |
| 1322 | |
| 1323 | if (!arg) |
| 1324 | err = -EINVAL; |
| 1325 | if (!err) |
| 1326 | err = loop_get_status(lo, &info64); |
| 1327 | if (!err) |
| 1328 | err = loop_info64_to_compat(&info64, arg); |
| 1329 | return err; |
| 1330 | } |
| 1331 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1332 | static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1333 | unsigned int cmd, unsigned long arg) |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1334 | { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1335 | struct loop_device *lo = bdev->bd_disk->private_data; |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1336 | int err; |
| 1337 | |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1338 | switch(cmd) { |
| 1339 | case LOOP_SET_STATUS: |
| 1340 | mutex_lock(&lo->lo_ctl_mutex); |
| 1341 | err = loop_set_status_compat( |
| 1342 | lo, (const struct compat_loop_info __user *) arg); |
| 1343 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1344 | break; |
| 1345 | case LOOP_GET_STATUS: |
| 1346 | mutex_lock(&lo->lo_ctl_mutex); |
| 1347 | err = loop_get_status_compat( |
| 1348 | lo, (struct compat_loop_info __user *) arg); |
| 1349 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1350 | break; |
J. R. Okajima | 53d6660 | 2009-03-31 15:23:43 -0700 | [diff] [blame] | 1351 | case LOOP_SET_CAPACITY: |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1352 | case LOOP_CLR_FD: |
| 1353 | case LOOP_GET_STATUS64: |
| 1354 | case LOOP_SET_STATUS64: |
| 1355 | arg = (unsigned long) compat_ptr(arg); |
| 1356 | case LOOP_SET_FD: |
| 1357 | case LOOP_CHANGE_FD: |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1358 | err = lo_ioctl(bdev, mode, cmd, arg); |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1359 | break; |
| 1360 | default: |
| 1361 | err = -ENOIOCTLCMD; |
| 1362 | break; |
| 1363 | } |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1364 | return err; |
| 1365 | } |
| 1366 | #endif |
| 1367 | |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1368 | static int lo_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | { |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1370 | struct loop_device *lo; |
| 1371 | int err = 0; |
| 1372 | |
| 1373 | mutex_lock(&loop_index_mutex); |
| 1374 | lo = bdev->bd_disk->private_data; |
| 1375 | if (!lo) { |
| 1376 | err = -ENXIO; |
| 1377 | goto out; |
| 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
Ingo Molnar | f85221d | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1380 | mutex_lock(&lo->lo_ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | lo->lo_refcnt++; |
Ingo Molnar | f85221d | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1382 | mutex_unlock(&lo->lo_ctl_mutex); |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1383 | out: |
| 1384 | mutex_unlock(&loop_index_mutex); |
| 1385 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 1388 | static void lo_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | { |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1390 | struct loop_device *lo = disk->private_data; |
Alexander Beregalov | ffcd7dc | 2009-04-07 13:48:21 +0200 | [diff] [blame] | 1391 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Ingo Molnar | f85221d | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1393 | mutex_lock(&lo->lo_ctl_mutex); |
David Woodhouse | 96c5865 | 2008-02-06 01:36:27 -0800 | [diff] [blame] | 1394 | |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1395 | if (--lo->lo_refcnt) |
| 1396 | goto out; |
| 1397 | |
| 1398 | if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { |
| 1399 | /* |
| 1400 | * In autoclear mode, stop the loop thread |
| 1401 | * and remove configuration after last close. |
| 1402 | */ |
Ayan George | 4c823cc | 2011-09-21 10:02:13 +0200 | [diff] [blame] | 1403 | err = loop_clr_fd(lo); |
Alexander Beregalov | ffcd7dc | 2009-04-07 13:48:21 +0200 | [diff] [blame] | 1404 | if (!err) |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 1405 | return; |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1406 | } else { |
| 1407 | /* |
| 1408 | * Otherwise keep thread (if running) and config, |
| 1409 | * but flush possible ongoing bios in thread. |
| 1410 | */ |
| 1411 | loop_flush(lo); |
| 1412 | } |
David Woodhouse | 96c5865 | 2008-02-06 01:36:27 -0800 | [diff] [blame] | 1413 | |
Milan Broz | 14f2793 | 2008-12-12 14:48:27 +0100 | [diff] [blame] | 1414 | out: |
Ingo Molnar | f85221d | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1415 | mutex_unlock(&lo->lo_ctl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1418 | static const struct block_device_operations lo_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | .owner = THIS_MODULE, |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1420 | .open = lo_open, |
| 1421 | .release = lo_release, |
| 1422 | .ioctl = lo_ioctl, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1423 | #ifdef CONFIG_COMPAT |
Al Viro | bb21488 | 2008-03-02 09:29:48 -0500 | [diff] [blame] | 1424 | .compat_ioctl = lo_compat_ioctl, |
David Howells | 863d5b82 | 2006-08-29 19:06:14 +0100 | [diff] [blame] | 1425 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | }; |
| 1427 | |
| 1428 | /* |
| 1429 | * And now the modules code and kernel interface. |
| 1430 | */ |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1431 | static int max_loop; |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 1432 | module_param(max_loop, int, S_IRUGO); |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1433 | MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 1434 | module_param(max_part, int, S_IRUGO); |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1435 | MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | MODULE_LICENSE("GPL"); |
| 1437 | MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); |
| 1438 | |
| 1439 | int loop_register_transfer(struct loop_func_table *funcs) |
| 1440 | { |
| 1441 | unsigned int n = funcs->number; |
| 1442 | |
| 1443 | if (n >= MAX_LO_CRYPT || xfer_funcs[n]) |
| 1444 | return -EINVAL; |
| 1445 | xfer_funcs[n] = funcs; |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1449 | static int unregister_transfer_cb(int id, void *ptr, void *data) |
| 1450 | { |
| 1451 | struct loop_device *lo = ptr; |
| 1452 | struct loop_func_table *xfer = data; |
| 1453 | |
| 1454 | mutex_lock(&lo->lo_ctl_mutex); |
| 1455 | if (lo->lo_encryption == xfer) |
| 1456 | loop_release_xfer(lo); |
| 1457 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | int loop_unregister_transfer(int number) |
| 1462 | { |
| 1463 | unsigned int n = number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | struct loop_func_table *xfer; |
| 1465 | |
| 1466 | if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) |
| 1467 | return -EINVAL; |
| 1468 | |
| 1469 | xfer_funcs[n] = NULL; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1470 | idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | EXPORT_SYMBOL(loop_register_transfer); |
| 1475 | EXPORT_SYMBOL(loop_unregister_transfer); |
| 1476 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1477 | static int loop_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1478 | const struct blk_mq_queue_data *bd) |
| 1479 | { |
| 1480 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
| 1481 | |
| 1482 | blk_mq_start_request(bd->rq); |
| 1483 | |
| 1484 | if (cmd->rq->cmd_flags & REQ_WRITE) { |
| 1485 | struct loop_device *lo = cmd->rq->q->queuedata; |
| 1486 | bool need_sched = true; |
| 1487 | |
| 1488 | spin_lock_irq(&lo->lo_lock); |
| 1489 | if (lo->write_started) |
| 1490 | need_sched = false; |
| 1491 | else |
| 1492 | lo->write_started = true; |
| 1493 | list_add_tail(&cmd->list, &lo->write_cmd_head); |
| 1494 | spin_unlock_irq(&lo->lo_lock); |
| 1495 | |
| 1496 | if (need_sched) |
| 1497 | queue_work(loop_wq, &lo->write_work); |
| 1498 | } else { |
| 1499 | queue_work(loop_wq, &cmd->read_work); |
| 1500 | } |
| 1501 | |
| 1502 | return BLK_MQ_RQ_QUEUE_OK; |
| 1503 | } |
| 1504 | |
| 1505 | static void loop_handle_cmd(struct loop_cmd *cmd) |
| 1506 | { |
| 1507 | const bool write = cmd->rq->cmd_flags & REQ_WRITE; |
| 1508 | struct loop_device *lo = cmd->rq->q->queuedata; |
| 1509 | int ret = -EIO; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (lo->lo_state != Lo_bound) |
| 1512 | goto failed; |
| 1513 | |
| 1514 | if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) |
| 1515 | goto failed; |
| 1516 | |
Ming Lei | 3011201 | 2014-12-31 13:22:58 +0000 | [diff] [blame] | 1517 | ret = do_req_filebacked(lo, cmd->rq); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1518 | |
| 1519 | failed: |
| 1520 | if (ret) |
| 1521 | cmd->rq->errors = -EIO; |
| 1522 | blk_mq_complete_request(cmd->rq); |
| 1523 | } |
| 1524 | |
| 1525 | static void loop_queue_write_work(struct work_struct *work) |
| 1526 | { |
| 1527 | struct loop_device *lo = |
| 1528 | container_of(work, struct loop_device, write_work); |
| 1529 | LIST_HEAD(cmd_list); |
| 1530 | |
| 1531 | spin_lock_irq(&lo->lo_lock); |
| 1532 | repeat: |
| 1533 | list_splice_init(&lo->write_cmd_head, &cmd_list); |
| 1534 | spin_unlock_irq(&lo->lo_lock); |
| 1535 | |
| 1536 | while (!list_empty(&cmd_list)) { |
| 1537 | struct loop_cmd *cmd = list_first_entry(&cmd_list, |
| 1538 | struct loop_cmd, list); |
| 1539 | list_del_init(&cmd->list); |
| 1540 | loop_handle_cmd(cmd); |
| 1541 | } |
| 1542 | |
| 1543 | spin_lock_irq(&lo->lo_lock); |
| 1544 | if (!list_empty(&lo->write_cmd_head)) |
| 1545 | goto repeat; |
| 1546 | lo->write_started = false; |
| 1547 | spin_unlock_irq(&lo->lo_lock); |
| 1548 | } |
| 1549 | |
| 1550 | static void loop_queue_read_work(struct work_struct *work) |
| 1551 | { |
| 1552 | struct loop_cmd *cmd = |
| 1553 | container_of(work, struct loop_cmd, read_work); |
| 1554 | |
| 1555 | loop_handle_cmd(cmd); |
| 1556 | } |
| 1557 | |
| 1558 | static int loop_init_request(void *data, struct request *rq, |
| 1559 | unsigned int hctx_idx, unsigned int request_idx, |
| 1560 | unsigned int numa_node) |
| 1561 | { |
| 1562 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 1563 | |
| 1564 | cmd->rq = rq; |
| 1565 | INIT_WORK(&cmd->read_work, loop_queue_read_work); |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static struct blk_mq_ops loop_mq_ops = { |
| 1571 | .queue_rq = loop_queue_rq, |
| 1572 | .map_queue = blk_mq_map_queue, |
| 1573 | .init_request = loop_init_request, |
| 1574 | }; |
| 1575 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1576 | static int loop_add(struct loop_device **l, int i) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1577 | { |
| 1578 | struct loop_device *lo; |
| 1579 | struct gendisk *disk; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1580 | int err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1581 | |
Silva Paulo | 68d740d | 2012-07-14 15:39:58 -0700 | [diff] [blame] | 1582 | err = -ENOMEM; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1583 | lo = kzalloc(sizeof(*lo), GFP_KERNEL); |
Silva Paulo | 68d740d | 2012-07-14 15:39:58 -0700 | [diff] [blame] | 1584 | if (!lo) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1585 | goto out; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1586 | |
Mikulas Patocka | ef7e7c8 | 2013-10-15 14:14:38 -0600 | [diff] [blame] | 1587 | lo->lo_state = Lo_unbound; |
| 1588 | |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1589 | /* allocate id, if @id >= 0, we're requesting that specific id */ |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1590 | if (i >= 0) { |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1591 | err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); |
| 1592 | if (err == -ENOSPC) |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1593 | err = -EEXIST; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1594 | } else { |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1595 | err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1596 | } |
| 1597 | if (err < 0) |
| 1598 | goto out_free_dev; |
Tejun Heo | c718aa6 | 2013-02-27 17:03:58 -0800 | [diff] [blame] | 1599 | i = err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1600 | |
Wei Yongjun | 183cfb5 | 2013-03-22 08:59:19 -0600 | [diff] [blame] | 1601 | err = -ENOMEM; |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1602 | lo->tag_set.ops = &loop_mq_ops; |
| 1603 | lo->tag_set.nr_hw_queues = 1; |
| 1604 | lo->tag_set.queue_depth = 128; |
| 1605 | lo->tag_set.numa_node = NUMA_NO_NODE; |
| 1606 | lo->tag_set.cmd_size = sizeof(struct loop_cmd); |
| 1607 | lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
| 1608 | lo->tag_set.driver_data = lo; |
| 1609 | |
| 1610 | err = blk_mq_alloc_tag_set(&lo->tag_set); |
| 1611 | if (err) |
Mikulas Patocka | 3ec981e | 2013-10-14 12:12:24 -0400 | [diff] [blame] | 1612 | goto out_free_idr; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1613 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1614 | lo->lo_queue = blk_mq_init_queue(&lo->tag_set); |
| 1615 | if (IS_ERR_OR_NULL(lo->lo_queue)) { |
| 1616 | err = PTR_ERR(lo->lo_queue); |
| 1617 | goto out_cleanup_tags; |
| 1618 | } |
Mikulas Patocka | ef7e7c8 | 2013-10-15 14:14:38 -0600 | [diff] [blame] | 1619 | lo->lo_queue->queuedata = lo; |
| 1620 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1621 | INIT_LIST_HEAD(&lo->write_cmd_head); |
| 1622 | INIT_WORK(&lo->write_work, loop_queue_write_work); |
| 1623 | |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1624 | disk = lo->lo_disk = alloc_disk(1 << part_shift); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1625 | if (!disk) |
| 1626 | goto out_free_queue; |
| 1627 | |
Kay Sievers | e03c8dd | 2011-08-23 20:12:04 +0200 | [diff] [blame] | 1628 | /* |
| 1629 | * Disable partition scanning by default. The in-kernel partition |
| 1630 | * scanning can be requested individually per-device during its |
| 1631 | * setup. Userspace can always add and remove partitions from all |
| 1632 | * devices. The needed partition minors are allocated from the |
| 1633 | * extended minor space, the main loop device numbers will continue |
| 1634 | * to match the loop minors, regardless of the number of partitions |
| 1635 | * used. |
| 1636 | * |
| 1637 | * If max_part is given, partition scanning is globally enabled for |
| 1638 | * all loop devices. The minors for the main loop devices will be |
| 1639 | * multiples of max_part. |
| 1640 | * |
| 1641 | * Note: Global-for-all-devices, set-only-at-init, read-only module |
| 1642 | * parameteters like 'max_loop' and 'max_part' make things needlessly |
| 1643 | * complicated, are too static, inflexible and may surprise |
| 1644 | * userspace tools. Parameters like this in general should be avoided. |
| 1645 | */ |
| 1646 | if (!part_shift) |
| 1647 | disk->flags |= GENHD_FL_NO_PART_SCAN; |
| 1648 | disk->flags |= GENHD_FL_EXT_DEVT; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1649 | mutex_init(&lo->lo_ctl_mutex); |
| 1650 | lo->lo_number = i; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1651 | spin_lock_init(&lo->lo_lock); |
| 1652 | disk->major = LOOP_MAJOR; |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1653 | disk->first_minor = i << part_shift; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1654 | disk->fops = &lo_fops; |
| 1655 | disk->private_data = lo; |
| 1656 | disk->queue = lo->lo_queue; |
| 1657 | sprintf(disk->disk_name, "loop%d", i); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1658 | add_disk(disk); |
| 1659 | *l = lo; |
| 1660 | return lo->lo_number; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1661 | |
| 1662 | out_free_queue: |
| 1663 | blk_cleanup_queue(lo->lo_queue); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1664 | out_cleanup_tags: |
| 1665 | blk_mq_free_tag_set(&lo->tag_set); |
Mikulas Patocka | 3ec981e | 2013-10-14 12:12:24 -0400 | [diff] [blame] | 1666 | out_free_idr: |
| 1667 | idr_remove(&loop_index_idr, i); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1668 | out_free_dev: |
| 1669 | kfree(lo); |
| 1670 | out: |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1671 | return err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1674 | static void loop_remove(struct loop_device *lo) |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1675 | { |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1676 | del_gendisk(lo->lo_disk); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1677 | blk_cleanup_queue(lo->lo_queue); |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1678 | blk_mq_free_tag_set(&lo->tag_set); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1679 | put_disk(lo->lo_disk); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1680 | kfree(lo); |
| 1681 | } |
| 1682 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1683 | static int find_free_cb(int id, void *ptr, void *data) |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1684 | { |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1685 | struct loop_device *lo = ptr; |
| 1686 | struct loop_device **l = data; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1687 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1688 | if (lo->lo_state == Lo_unbound) { |
| 1689 | *l = lo; |
| 1690 | return 1; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1691 | } |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1692 | return 0; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1695 | static int loop_lookup(struct loop_device **l, int i) |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1696 | { |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1697 | struct loop_device *lo; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1698 | int ret = -ENODEV; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1699 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1700 | if (i < 0) { |
| 1701 | int err; |
| 1702 | |
| 1703 | err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); |
| 1704 | if (err == 1) { |
| 1705 | *l = lo; |
| 1706 | ret = lo->lo_number; |
| 1707 | } |
| 1708 | goto out; |
| 1709 | } |
| 1710 | |
| 1711 | /* lookup and return a specific i */ |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1712 | lo = idr_find(&loop_index_idr, i); |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1713 | if (lo) { |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1714 | *l = lo; |
| 1715 | ret = lo->lo_number; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1716 | } |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1717 | out: |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1718 | return ret; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1721 | static struct kobject *loop_probe(dev_t dev, int *part, void *data) |
| 1722 | { |
Al Viro | 705962c | 2007-05-13 05:52:32 -0400 | [diff] [blame] | 1723 | struct loop_device *lo; |
Al Viro | 07002e9 | 2007-05-12 16:23:15 -0400 | [diff] [blame] | 1724 | struct kobject *kobj; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1725 | int err; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1726 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1727 | mutex_lock(&loop_index_mutex); |
| 1728 | err = loop_lookup(&lo, MINOR(dev) >> part_shift); |
| 1729 | if (err < 0) |
| 1730 | err = loop_add(&lo, MINOR(dev) >> part_shift); |
| 1731 | if (err < 0) |
Mikulas Patocka | a207f59 | 2013-10-14 12:13:24 -0400 | [diff] [blame] | 1732 | kobj = NULL; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1733 | else |
| 1734 | kobj = get_disk(lo->lo_disk); |
| 1735 | mutex_unlock(&loop_index_mutex); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1736 | |
| 1737 | *part = 0; |
Al Viro | 07002e9 | 2007-05-12 16:23:15 -0400 | [diff] [blame] | 1738 | return kobj; |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1741 | static long loop_control_ioctl(struct file *file, unsigned int cmd, |
| 1742 | unsigned long parm) |
| 1743 | { |
| 1744 | struct loop_device *lo; |
| 1745 | int ret = -ENOSYS; |
| 1746 | |
| 1747 | mutex_lock(&loop_index_mutex); |
| 1748 | switch (cmd) { |
| 1749 | case LOOP_CTL_ADD: |
| 1750 | ret = loop_lookup(&lo, parm); |
| 1751 | if (ret >= 0) { |
| 1752 | ret = -EEXIST; |
| 1753 | break; |
| 1754 | } |
| 1755 | ret = loop_add(&lo, parm); |
| 1756 | break; |
| 1757 | case LOOP_CTL_REMOVE: |
| 1758 | ret = loop_lookup(&lo, parm); |
| 1759 | if (ret < 0) |
| 1760 | break; |
| 1761 | mutex_lock(&lo->lo_ctl_mutex); |
| 1762 | if (lo->lo_state != Lo_unbound) { |
| 1763 | ret = -EBUSY; |
| 1764 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1765 | break; |
| 1766 | } |
| 1767 | if (lo->lo_refcnt > 0) { |
| 1768 | ret = -EBUSY; |
| 1769 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1770 | break; |
| 1771 | } |
| 1772 | lo->lo_disk->private_data = NULL; |
| 1773 | mutex_unlock(&lo->lo_ctl_mutex); |
| 1774 | idr_remove(&loop_index_idr, lo->lo_number); |
| 1775 | loop_remove(lo); |
| 1776 | break; |
| 1777 | case LOOP_CTL_GET_FREE: |
| 1778 | ret = loop_lookup(&lo, -1); |
| 1779 | if (ret >= 0) |
| 1780 | break; |
| 1781 | ret = loop_add(&lo, -1); |
| 1782 | } |
| 1783 | mutex_unlock(&loop_index_mutex); |
| 1784 | |
| 1785 | return ret; |
| 1786 | } |
| 1787 | |
| 1788 | static const struct file_operations loop_ctl_fops = { |
| 1789 | .open = nonseekable_open, |
| 1790 | .unlocked_ioctl = loop_control_ioctl, |
| 1791 | .compat_ioctl = loop_control_ioctl, |
| 1792 | .owner = THIS_MODULE, |
| 1793 | .llseek = noop_llseek, |
| 1794 | }; |
| 1795 | |
| 1796 | static struct miscdevice loop_misc = { |
| 1797 | .minor = LOOP_CTRL_MINOR, |
| 1798 | .name = "loop-control", |
| 1799 | .fops = &loop_ctl_fops, |
| 1800 | }; |
| 1801 | |
| 1802 | MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); |
| 1803 | MODULE_ALIAS("devname:loop-control"); |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | static int __init loop_init(void) |
| 1806 | { |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1807 | int i, nr; |
| 1808 | unsigned long range; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1809 | struct loop_device *lo; |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1810 | int err; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1811 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1812 | err = misc_register(&loop_misc); |
| 1813 | if (err < 0) |
| 1814 | return err; |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1815 | |
| 1816 | part_shift = 0; |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 1817 | if (max_part > 0) { |
Laurent Vivier | 476a481 | 2008-03-26 12:11:53 +0100 | [diff] [blame] | 1818 | part_shift = fls(max_part); |
| 1819 | |
Namhyung Kim | ac04fee | 2011-05-27 07:59:25 +0200 | [diff] [blame] | 1820 | /* |
| 1821 | * Adjust max_part according to part_shift as it is exported |
| 1822 | * to user space so that user can decide correct minor number |
| 1823 | * if [s]he want to create more devices. |
| 1824 | * |
| 1825 | * Note that -1 is required because partition 0 is reserved |
| 1826 | * for the whole disk. |
| 1827 | */ |
| 1828 | max_part = (1UL << part_shift) - 1; |
| 1829 | } |
| 1830 | |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 1831 | if ((1UL << part_shift) > DISK_MAX_PARTS) { |
| 1832 | err = -EINVAL; |
| 1833 | goto misc_out; |
| 1834 | } |
Namhyung Kim | 78f4bb3 | 2011-05-24 16:48:54 +0200 | [diff] [blame] | 1835 | |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 1836 | if (max_loop > 1UL << (MINORBITS - part_shift)) { |
| 1837 | err = -EINVAL; |
| 1838 | goto misc_out; |
| 1839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1841 | /* |
| 1842 | * If max_loop is specified, create that many devices upfront. |
| 1843 | * This also becomes a hard limit. If max_loop is not specified, |
| 1844 | * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module |
| 1845 | * init time. Loop devices can be requested on-demand with the |
| 1846 | * /dev/loop-control interface, or be instantiated by accessing |
| 1847 | * a 'dead' device node. |
| 1848 | */ |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1849 | if (max_loop) { |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1850 | nr = max_loop; |
Namhyung Kim | a1c15c5 | 2011-05-24 16:48:55 +0200 | [diff] [blame] | 1851 | range = max_loop << part_shift; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1852 | } else { |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1853 | nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; |
Namhyung Kim | a1c15c5 | 2011-05-24 16:48:55 +0200 | [diff] [blame] | 1854 | range = 1UL << MINORBITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1856 | |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 1857 | if (register_blkdev(LOOP_MAJOR, "loop")) { |
| 1858 | err = -EIO; |
| 1859 | goto misc_out; |
| 1860 | } |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1861 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1862 | loop_wq = alloc_workqueue("kloopd", |
| 1863 | WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0); |
| 1864 | if (!loop_wq) { |
| 1865 | err = -ENOMEM; |
| 1866 | goto misc_out; |
| 1867 | } |
| 1868 | |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1869 | blk_register_region(MKDEV(LOOP_MAJOR, 0), range, |
| 1870 | THIS_MODULE, loop_probe, NULL, NULL); |
| 1871 | |
Kay Sievers | d134b00 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1872 | /* pre-create number of devices given by config or max_loop */ |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1873 | mutex_lock(&loop_index_mutex); |
| 1874 | for (i = 0; i < nr; i++) |
| 1875 | loop_add(&lo, i); |
| 1876 | mutex_unlock(&loop_index_mutex); |
| 1877 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1878 | printk(KERN_INFO "loop: module loaded\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | return 0; |
Guo Chao | b1a6650 | 2013-02-21 15:16:49 -0800 | [diff] [blame] | 1880 | |
| 1881 | misc_out: |
| 1882 | misc_deregister(&loop_misc); |
| 1883 | return err; |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1884 | } |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1885 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1886 | static int loop_exit_cb(int id, void *ptr, void *data) |
| 1887 | { |
| 1888 | struct loop_device *lo = ptr; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1889 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1890 | loop_remove(lo); |
| 1891 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1894 | static void __exit loop_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | { |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1896 | unsigned long range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
Namhyung Kim | a1c15c5 | 2011-05-24 16:48:55 +0200 | [diff] [blame] | 1898 | range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1899 | |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1900 | idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); |
Kay Sievers | 34dd82a | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1901 | idr_destroy(&loop_index_idr); |
Ken Chen | 7328508 | 2007-05-08 00:28:20 -0700 | [diff] [blame] | 1902 | |
Ken Chen | a47653f | 2007-06-08 13:46:44 -0700 | [diff] [blame] | 1903 | blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 1904 | unregister_blkdev(LOOP_MAJOR, "loop"); |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1905 | |
Ming Lei | b5dd2f6 | 2014-12-31 13:22:57 +0000 | [diff] [blame] | 1906 | destroy_workqueue(loop_wq); |
| 1907 | |
Kay Sievers | 770fe30 | 2011-07-31 22:08:04 +0200 | [diff] [blame] | 1908 | misc_deregister(&loop_misc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | module_init(loop_init); |
| 1912 | module_exit(loop_exit); |
| 1913 | |
| 1914 | #ifndef MODULE |
| 1915 | static int __init max_loop_setup(char *str) |
| 1916 | { |
| 1917 | max_loop = simple_strtol(str, NULL, 0); |
| 1918 | return 1; |
| 1919 | } |
| 1920 | |
| 1921 | __setup("max_loop=", max_loop_setup); |
| 1922 | #endif |