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