blob: c4fd1e45ce1e82a8f303aacda953539dba99b3be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Engelhardt96de0e22007-10-19 23:21:04 +020032 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
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 Piggin4e02ed42008-10-29 14:00:55 -070043 * operations write_begin is not available on the backing filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 Torvalds1da177e2005-04-16 15:20:36 -070052#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 Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/swap.h>
65#include <linux/slab.h>
David Howells863d5b822006-08-29 19:06:14 +010066#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/suspend.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070068#include <linux/freezer.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020069#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/completion.h>
72#include <linux/highmem.h>
Serge E. Hallyn6c997912006-09-29 01:59:11 -070073#include <linux/kthread.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020074#include <linux/splice.h>
Milan Brozee862732010-08-23 15:16:00 +020075#include <linux/sysfs.h>
Kay Sievers770fe302011-07-31 22:08:04 +020076#include <linux/miscdevice.h>
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +020077#include <linux/falloc.h>
Al Viro283e7e52015-04-03 15:21:59 -040078#include <linux/uio.h>
Al Viro83a87612013-05-12 10:14:07 -040079#include "loop.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#include <asm/uaccess.h>
82
Kay Sievers34dd82a2011-07-31 22:08:04 +020083static DEFINE_IDR(loop_index_idr);
84static DEFINE_MUTEX(loop_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Laurent Vivier476a4812008-03-26 12:11:53 +010086static int max_part;
87static int part_shift;
88
Ming Leib5dd2f62014-12-31 13:22:57 +000089static struct workqueue_struct *loop_wq;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Transfer functions
93 */
94static int transfer_none(struct loop_device *lo, int cmd,
95 struct page *raw_page, unsigned raw_off,
96 struct page *loop_page, unsigned loop_off,
97 int size, sector_t real_block)
98{
Cong Wangcfd80052011-11-25 23:14:18 +080099 char *raw_buf = kmap_atomic(raw_page) + raw_off;
100 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (cmd == READ)
103 memcpy(loop_buf, raw_buf, size);
104 else
105 memcpy(raw_buf, loop_buf, size);
106
Cong Wangcfd80052011-11-25 23:14:18 +0800107 kunmap_atomic(loop_buf);
108 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 cond_resched();
110 return 0;
111}
112
113static int transfer_xor(struct loop_device *lo, int cmd,
114 struct page *raw_page, unsigned raw_off,
115 struct page *loop_page, unsigned loop_off,
116 int size, sector_t real_block)
117{
Cong Wangcfd80052011-11-25 23:14:18 +0800118 char *raw_buf = kmap_atomic(raw_page) + raw_off;
119 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char *in, *out, *key;
121 int i, keysize;
122
123 if (cmd == READ) {
124 in = raw_buf;
125 out = loop_buf;
126 } else {
127 in = loop_buf;
128 out = raw_buf;
129 }
130
131 key = lo->lo_encrypt_key;
132 keysize = lo->lo_encrypt_key_size;
133 for (i = 0; i < size; i++)
134 *out++ = *in++ ^ key[(i & 511) % keysize];
135
Cong Wangcfd80052011-11-25 23:14:18 +0800136 kunmap_atomic(loop_buf);
137 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 cond_resched();
139 return 0;
140}
141
142static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
143{
144 if (unlikely(info->lo_encrypt_key_size <= 0))
145 return -EINVAL;
146 return 0;
147}
148
149static struct loop_func_table none_funcs = {
150 .number = LO_CRYPT_NONE,
151 .transfer = transfer_none,
152};
153
154static struct loop_func_table xor_funcs = {
155 .number = LO_CRYPT_XOR,
156 .transfer = transfer_xor,
157 .init = xor_init
158};
159
160/* xfer_funcs[0] is special - its release function is never called */
161static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
162 &none_funcs,
163 &xor_funcs
164};
165
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100166static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Guo Chaob7a1da62013-02-21 15:16:50 -0800168 loff_t loopsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Compute loopsize in bytes */
Guo Chaob7a1da62013-02-21 15:16:50 -0800171 loopsize = i_size_read(file->f_mapping->host);
172 if (offset > 0)
173 loopsize -= offset;
174 /* offset is beyond i_size, weird but possible */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100175 if (loopsize < 0)
176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100178 if (sizelimit > 0 && sizelimit < loopsize)
179 loopsize = sizelimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
181 * Unfortunately, if we want to do I/O on the device,
182 * the number of 512-byte sectors has to fit into a sector_t.
183 */
184 return loopsize >> 9;
185}
186
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100187static loff_t get_loop_size(struct loop_device *lo, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100189 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
190}
191
192static int
193figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
194{
195 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 sector_t x = (sector_t)size;
Guo Chao7b0576a2013-02-21 15:16:47 -0800197 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (unlikely((loff_t)x != size))
200 return -EFBIG;
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100201 if (lo->lo_offset != offset)
202 lo->lo_offset = offset;
203 if (lo->lo_sizelimit != sizelimit)
204 lo->lo_sizelimit = sizelimit;
Ken Chen73285082007-05-08 00:28:20 -0700205 set_capacity(lo->lo_disk, x);
Guo Chao7b0576a2013-02-21 15:16:47 -0800206 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
207 /* let user-space know about the new size */
208 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static inline int
213lo_do_transfer(struct loop_device *lo, int cmd,
214 struct page *rpage, unsigned roffs,
215 struct page *lpage, unsigned loffs,
216 int size, sector_t rblock)
217{
218 if (unlikely(!lo->transfer))
219 return 0;
220
221 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
222}
223
224/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * __do_lo_send_write - helper for writing data to a loop device
226 *
227 * This helper just factors out common code between do_lo_send_direct_write()
228 * and do_lo_send_write().
229 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800230static int __do_lo_send_write(struct file *file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100231 u8 *buf, const int len, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Al Viro283e7e52015-04-03 15:21:59 -0400233 struct kvec kvec = {.iov_base = buf, .iov_len = len};
234 struct iov_iter from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ssize_t bw;
Al Viro283e7e52015-04-03 15:21:59 -0400236
237 iov_iter_kvec(&from, ITER_KVEC | WRITE, &kvec, 1, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Al Viro03d95eb2013-03-20 13:04:20 -0400239 file_start_write(file);
Al Viro283e7e52015-04-03 15:21:59 -0400240 bw = vfs_iter_write(file, &from, &pos);
Al Viro03d95eb2013-03-20 13:04:20 -0400241 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (likely(bw == len))
243 return 0;
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700244 printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 (unsigned long long)pos, len);
246 if (bw >= 0)
247 bw = -EIO;
248 return bw;
249}
250
251/**
252 * do_lo_send_direct_write - helper for writing data to a loop device
253 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200254 * This is the fast, non-transforming version that does not need double
255 * buffering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
257static int do_lo_send_direct_write(struct loop_device *lo,
Al Viro511de732007-10-08 12:10:13 -0400258 struct bio_vec *bvec, loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100261 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 bvec->bv_len, pos);
263 kunmap(bvec->bv_page);
264 cond_resched();
265 return bw;
266}
267
268/**
269 * do_lo_send_write - helper for writing data to a loop device
270 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200271 * This is the slow, transforming version that needs to double buffer the
272 * data as it cannot do the transformations in place without having direct
273 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400276 loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
279 bvec->bv_offset, bvec->bv_len, pos >> 9);
280 if (likely(!ret))
281 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100282 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pos);
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700284 printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
286 if (ret > 0)
287 ret = -EIO;
288 return ret;
289}
290
Ming Lei30112012014-12-31 13:22:58 +0000291static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Al Viro511de732007-10-08 12:10:13 -0400293 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page);
Kent Overstreet79886132013-11-23 17:19:00 -0800295 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000296 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct page *page = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800298 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Christoph Hellwig456be142011-10-17 12:57:20 +0200300 if (lo->transfer != transfer_none) {
301 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
302 if (unlikely(!page))
303 goto fail;
304 kmap(page);
305 do_lo_send = do_lo_send_write;
306 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 do_lo_send = do_lo_send_direct_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Christoph Hellwig456be142011-10-17 12:57:20 +0200309
Ming Lei30112012014-12-31 13:22:58 +0000310 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800311 ret = do_lo_send(lo, &bvec, pos, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ret < 0)
313 break;
Kent Overstreet79886132013-11-23 17:19:00 -0800314 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 if (page) {
317 kunmap(page);
318 __free_page(page);
319 }
320out:
321 return ret;
322fail:
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700323 printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ret = -ENOMEM;
325 goto out;
326}
327
328struct lo_read_data {
329 struct loop_device *lo;
330 struct page *page;
331 unsigned offset;
332 int bsize;
333};
334
335static int
Jens Axboefd582142007-06-12 21:20:37 +0200336lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
337 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Jens Axboefd582142007-06-12 21:20:37 +0200339 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200341 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sector_t IV;
Jens Axboe3603b8e2010-12-20 09:15:19 +0100343 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Jens Axboefd582142007-06-12 21:20:37 +0200345 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
346 (buf->offset >> 9);
347 size = sd->len;
348 if (size > p->bsize)
349 size = p->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jens Axboefd582142007-06-12 21:20:37 +0200351 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700352 printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 page->index);
Jens Axboefd582142007-06-12 21:20:37 +0200354 size = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
357 flush_dcache_page(p->page);
358
Jens Axboefd582142007-06-12 21:20:37 +0200359 if (size > 0)
360 p->offset += size;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return size;
363}
364
365static int
Jens Axboefd582142007-06-12 21:20:37 +0200366lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
367{
368 return __splice_from_pipe(pipe, sd, lo_splice_actor);
369}
370
Dave Young306df072012-02-08 22:07:19 +0100371static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372do_lo_receive(struct loop_device *lo,
373 struct bio_vec *bvec, int bsize, loff_t pos)
374{
375 struct lo_read_data cookie;
Jens Axboefd582142007-06-12 21:20:37 +0200376 struct splice_desc sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct file *file;
Dave Young306df072012-02-08 22:07:19 +0100378 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 cookie.lo = lo;
381 cookie.page = bvec->bv_page;
382 cookie.offset = bvec->bv_offset;
383 cookie.bsize = bsize;
Jens Axboefd582142007-06-12 21:20:37 +0200384
385 sd.len = 0;
386 sd.total_len = bvec->bv_len;
387 sd.flags = 0;
388 sd.pos = pos;
389 sd.u.data = &cookie;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 file = lo->lo_backing_file;
Jens Axboefd582142007-06-12 21:20:37 +0200392 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
393
Dave Young306df072012-02-08 22:07:19 +0100394 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static int
Ming Lei30112012014-12-31 13:22:58 +0000398lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Kent Overstreet79886132013-11-23 17:19:00 -0800400 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000401 struct req_iterator iter;
Dave Young306df072012-02-08 22:07:19 +0100402 ssize_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Ming Lei30112012014-12-31 13:22:58 +0000404 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800405 s = do_lo_receive(lo, &bvec, bsize, pos);
Dave Young306df072012-02-08 22:07:19 +0100406 if (s < 0)
407 return s;
408
Kent Overstreet79886132013-11-23 17:19:00 -0800409 if (s != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000410 struct bio *bio;
411
412 __rq_for_each_bio(bio, rq)
413 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
Dave Young306df072012-02-08 22:07:19 +0100415 }
Kent Overstreet79886132013-11-23 17:19:00 -0800416 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Dave Young306df072012-02-08 22:07:19 +0100418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Ming Leicf655d92014-12-31 13:22:59 +0000421static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
422{
423 /*
424 * We use punch hole to reclaim the free space used by the
425 * image a.k.a. discard. However we do not support discard if
426 * encryption is enabled, because it may give an attacker
427 * useful information.
428 */
429 struct file *file = lo->lo_backing_file;
430 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
431 int ret;
432
433 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
434 ret = -EOPNOTSUPP;
435 goto out;
436 }
437
438 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
439 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
440 ret = -EIO;
441 out:
442 return ret;
443}
444
445static int lo_req_flush(struct loop_device *lo, struct request *rq)
446{
447 struct file *file = lo->lo_backing_file;
448 int ret = vfs_fsync(file, 0);
449 if (unlikely(ret && ret != -EINVAL))
450 ret = -EIO;
451
452 return ret;
453}
454
Ming Lei30112012014-12-31 13:22:58 +0000455static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 loff_t pos;
458 int ret;
459
Ming Lei30112012014-12-31 13:22:58 +0000460 pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100461
Ming Lei30112012014-12-31 13:22:58 +0000462 if (rq->cmd_flags & REQ_WRITE) {
Ming Leicf655d92014-12-31 13:22:59 +0000463 if (rq->cmd_flags & REQ_FLUSH)
464 ret = lo_req_flush(lo, rq);
Ming Leiaf65aa82014-12-31 13:23:00 +0000465 else if (rq->cmd_flags & REQ_DISCARD)
Ming Leicf655d92014-12-31 13:22:59 +0000466 ret = lo_discard(lo, rq, pos);
Ming Leiaf65aa82014-12-31 13:23:00 +0000467 else
468 ret = lo_send(lo, rq, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100469 } else
Ming Lei30112012014-12-31 13:22:58 +0000470 ret = lo_receive(lo, rq, lo->lo_blocksize, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return ret;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475struct switch_request {
476 struct file *file;
477 struct completion wait;
478};
479
Milan Broz14f27932008-12-12 14:48:27 +0100480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * Do the actual switch; called from the BIO completion routine
482 */
483static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
484{
485 struct file *file = p->file;
486 struct file *old_file = lo->lo_backing_file;
Milan Broz14f27932008-12-12 14:48:27 +0100487 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Milan Broz14f27932008-12-12 14:48:27 +0100489 /* if no new file, only flush of queued bios requested */
490 if (!file)
Ming Leib5dd2f62014-12-31 13:22:57 +0000491 return;
Milan Broz14f27932008-12-12 14:48:27 +0100492
493 mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
495 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700496 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
497 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 lo->old_gfp_mask = mapping_gfp_mask(mapping);
499 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Ming Leib5dd2f62014-12-31 13:22:57 +0000502/*
503 * loop_switch performs the hard work of switching a backing store.
504 * First it needs to flush existing IO, it does this by sending a magic
505 * BIO down the pipe. The completion of this BIO does the actual switch.
506 */
507static int loop_switch(struct loop_device *lo, struct file *file)
508{
509 struct switch_request w;
510
511 w.file = file;
512
513 /* freeze queue and wait for completion of scheduled requests */
514 blk_mq_freeze_queue(lo->lo_queue);
515
516 /* do the switch action */
517 do_loop_switch(lo, &w);
518
519 /* unfreeze */
520 blk_mq_unfreeze_queue(lo->lo_queue);
521
522 return 0;
523}
524
525/*
526 * Helper to flush the IOs in loop, but keeping loop thread running
527 */
528static int loop_flush(struct loop_device *lo)
529{
530 return loop_switch(lo, NULL);
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533/*
534 * loop_change_fd switched the backing store of a loopback device to
535 * a new file. This is useful for operating system installers to free up
536 * the original file and in High Availability environments to switch to
537 * an alternative location for the content in case of server meltdown.
538 * This can only work if the loop device is used read-only, and if the
539 * new backing store is the same size and type as the old backing store.
540 */
Al Virobb214882008-03-02 09:29:48 -0500541static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
542 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct file *file, *old_file;
545 struct inode *inode;
546 int error;
547
548 error = -ENXIO;
549 if (lo->lo_state != Lo_bound)
550 goto out;
551
552 /* the loop device has to be read-only */
553 error = -EINVAL;
554 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
555 goto out;
556
557 error = -EBADF;
558 file = fget(arg);
559 if (!file)
560 goto out;
561
562 inode = file->f_mapping->host;
563 old_file = lo->lo_backing_file;
564
565 error = -EINVAL;
566
567 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
568 goto out_putf;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* size of the new backing store needs to be the same */
571 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
572 goto out_putf;
573
574 /* and ... switch */
575 error = loop_switch(lo, file);
576 if (error)
577 goto out_putf;
578
579 fput(old_file);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200580 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100581 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583
584 out_putf:
585 fput(file);
586 out:
587 return error;
588}
589
590static inline int is_loop_device(struct file *file)
591{
592 struct inode *i = file->f_mapping->host;
593
594 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
595}
596
Milan Brozee862732010-08-23 15:16:00 +0200597/* loop sysfs attributes */
598
599static ssize_t loop_attr_show(struct device *dev, char *page,
600 ssize_t (*callback)(struct loop_device *, char *))
601{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200602 struct gendisk *disk = dev_to_disk(dev);
603 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200604
Kay Sievers34dd82a2011-07-31 22:08:04 +0200605 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200606}
607
608#define LOOP_ATTR_RO(_name) \
609static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
610static ssize_t loop_attr_do_show_##_name(struct device *d, \
611 struct device_attribute *attr, char *b) \
612{ \
613 return loop_attr_show(d, b, loop_attr_##_name##_show); \
614} \
615static struct device_attribute loop_attr_##_name = \
616 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
617
618static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
619{
620 ssize_t ret;
621 char *p = NULL;
622
Kay Sievers05eb0f22011-07-31 22:21:35 +0200623 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200624 if (lo->lo_backing_file)
625 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200626 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200627
628 if (IS_ERR_OR_NULL(p))
629 ret = PTR_ERR(p);
630 else {
631 ret = strlen(p);
632 memmove(buf, p, ret);
633 buf[ret++] = '\n';
634 buf[ret] = 0;
635 }
636
637 return ret;
638}
639
640static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
641{
642 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
643}
644
645static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
646{
647 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
648}
649
650static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
651{
652 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
653
654 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
655}
656
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200657static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
658{
659 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
660
661 return sprintf(buf, "%s\n", partscan ? "1" : "0");
662}
663
Milan Brozee862732010-08-23 15:16:00 +0200664LOOP_ATTR_RO(backing_file);
665LOOP_ATTR_RO(offset);
666LOOP_ATTR_RO(sizelimit);
667LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200668LOOP_ATTR_RO(partscan);
Milan Brozee862732010-08-23 15:16:00 +0200669
670static struct attribute *loop_attrs[] = {
671 &loop_attr_backing_file.attr,
672 &loop_attr_offset.attr,
673 &loop_attr_sizelimit.attr,
674 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200675 &loop_attr_partscan.attr,
Milan Brozee862732010-08-23 15:16:00 +0200676 NULL,
677};
678
679static struct attribute_group loop_attribute_group = {
680 .name = "loop",
681 .attrs= loop_attrs,
682};
683
684static int loop_sysfs_init(struct loop_device *lo)
685{
686 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
687 &loop_attribute_group);
688}
689
690static void loop_sysfs_exit(struct loop_device *lo)
691{
692 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
693 &loop_attribute_group);
694}
695
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200696static void loop_config_discard(struct loop_device *lo)
697{
698 struct file *file = lo->lo_backing_file;
699 struct inode *inode = file->f_mapping->host;
700 struct request_queue *q = lo->lo_queue;
701
702 /*
703 * We use punch hole to reclaim the free space used by the
Olaf Hering12a64d22014-01-21 14:39:24 -0800704 * image a.k.a. discard. However we do not support discard if
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200705 * encryption is enabled, because it may give an attacker
706 * useful information.
707 */
708 if ((!file->f_op->fallocate) ||
709 lo->lo_encrypt_key_size) {
710 q->limits.discard_granularity = 0;
711 q->limits.discard_alignment = 0;
712 q->limits.max_discard_sectors = 0;
713 q->limits.discard_zeroes_data = 0;
714 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
715 return;
716 }
717
718 q->limits.discard_granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaf3c02011-12-02 14:47:03 +0100719 q->limits.discard_alignment = 0;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200720 q->limits.max_discard_sectors = UINT_MAX >> 9;
721 q->limits.discard_zeroes_data = 1;
722 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
723}
724
Al Virobb214882008-03-02 09:29:48 -0500725static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct block_device *bdev, unsigned int arg)
727{
728 struct file *file, *f;
729 struct inode *inode;
730 struct address_space *mapping;
731 unsigned lo_blocksize;
732 int lo_flags = 0;
733 int error;
734 loff_t size;
735
736 /* This is safe, since we have a reference from open(). */
737 __module_get(THIS_MODULE);
738
739 error = -EBADF;
740 file = fget(arg);
741 if (!file)
742 goto out;
743
744 error = -EBUSY;
745 if (lo->lo_state != Lo_unbound)
746 goto out_putf;
747
748 /* Avoid recursion */
749 f = file;
750 while (is_loop_device(f)) {
751 struct loop_device *l;
752
Al Virobb214882008-03-02 09:29:48 -0500753 if (f->f_mapping->host->i_bdev == bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto out_putf;
755
756 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
757 if (l->lo_state == Lo_unbound) {
758 error = -EINVAL;
759 goto out_putf;
760 }
761 f = l->lo_backing_file;
762 }
763
764 mapping = file->f_mapping;
765 inode = mapping->host;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 error = -EINVAL;
Christoph Hellwig456be142011-10-17 12:57:20 +0200768 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Christoph Hellwig456be142011-10-17 12:57:20 +0200771 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
Al Viro283e7e52015-04-03 15:21:59 -0400772 !file->f_op->write_iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 lo_flags |= LO_FLAGS_READ_ONLY;
774
Christoph Hellwig456be142011-10-17 12:57:20 +0200775 lo_blocksize = S_ISBLK(inode->i_mode) ?
776 inode->i_bdev->bd_block_size : PAGE_SIZE;
777
778 error = -EFBIG;
779 size = get_loop_size(lo, file);
780 if ((loff_t)(sector_t)size != size)
781 goto out_putf;
782
783 error = 0;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
786
787 lo->lo_blocksize = lo_blocksize;
788 lo->lo_device = bdev;
789 lo->lo_flags = lo_flags;
790 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700791 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 lo->ioctl = NULL;
793 lo->lo_sizelimit = 0;
794 lo->old_gfp_mask = mapping_gfp_mask(mapping);
795 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
796
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100797 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Tejun Heo4913efe2010-09-03 11:56:16 +0200798 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100799
Ken Chen73285082007-05-08 00:28:20 -0700800 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +0200802 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200803 /* let user-space know about the new size */
804 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 set_blocksize(bdev, lo_blocksize);
807
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700808 lo->lo_state = Lo_bound;
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200809 if (part_shift)
810 lo->lo_flags |= LO_FLAGS_PARTSCAN;
811 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100812 ioctl_by_bdev(bdev, BLKRRPART, 0);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700813
814 /* Grab the block_device to prevent its destruction after we
815 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
816 */
817 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819
820 out_putf:
821 fput(file);
822 out:
823 /* This is safe: open() is still holding a reference. */
824 module_put(THIS_MODULE);
825 return error;
826}
827
828static int
829loop_release_xfer(struct loop_device *lo)
830{
831 int err = 0;
832 struct loop_func_table *xfer = lo->lo_encryption;
833
834 if (xfer) {
835 if (xfer->release)
836 err = xfer->release(lo);
837 lo->transfer = NULL;
838 lo->lo_encryption = NULL;
839 module_put(xfer->owner);
840 }
841 return err;
842}
843
844static int
845loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
846 const struct loop_info64 *i)
847{
848 int err = 0;
849
850 if (xfer) {
851 struct module *owner = xfer->owner;
852
853 if (!try_module_get(owner))
854 return -EINVAL;
855 if (xfer->init)
856 err = xfer->init(lo, i);
857 if (err)
858 module_put(owner);
859 else
860 lo->lo_encryption = xfer;
861 }
862 return err;
863}
864
Ayan George4c823cc2011-09-21 10:02:13 +0200865static int loop_clr_fd(struct loop_device *lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -0400868 gfp_t gfp = lo->old_gfp_mask;
Ayan George4c823cc2011-09-21 10:02:13 +0200869 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 if (lo->lo_state != Lo_bound)
872 return -ENXIO;
873
Dave Chinnera1ecac32012-09-28 10:42:23 +0200874 /*
875 * If we've explicitly asked to tear down the loop device,
876 * and it has an elevated reference count, set it for auto-teardown when
877 * the last reference goes away. This stops $!~#$@ udev from
878 * preventing teardown because it decided that it needs to run blkid on
879 * the loopback device whenever they appear. xfstests is notorious for
880 * failing tests because blkid via udev races with a losetup
881 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
882 * command to fail with EBUSY.
883 */
884 if (lo->lo_refcnt > 1) {
885 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
886 mutex_unlock(&lo->lo_ctl_mutex);
887 return 0;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 if (filp == NULL)
891 return -EINVAL;
892
893 spin_lock_irq(&lo->lo_lock);
894 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +0200896 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 loop_release_xfer(lo);
899 lo->transfer = NULL;
900 lo->ioctl = NULL;
901 lo->lo_device = NULL;
902 lo->lo_encryption = NULL;
903 lo->lo_offset = 0;
904 lo->lo_sizelimit = 0;
905 lo->lo_encrypt_key_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
907 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
908 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700909 if (bdev) {
910 bdput(bdev);
Al Virobb214882008-03-02 09:29:48 -0500911 invalidate_bdev(bdev);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700912 }
Ken Chen73285082007-05-08 00:28:20 -0700913 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -0600914 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200915 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -0500916 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200917 /* let user-space know about this change */
918 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 mapping_set_gfp_mask(filp->f_mapping, gfp);
921 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* This is safe: open() is still holding a reference. */
923 module_put(THIS_MODULE);
Jens Axboec2fccc12013-04-08 10:12:11 +0200924 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
925 ioctl_by_bdev(bdev, BLKRRPART, 0);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200926 lo->lo_flags = 0;
927 if (!part_shift)
928 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +0100929 mutex_unlock(&lo->lo_ctl_mutex);
930 /*
931 * Need not hold lo_ctl_mutex to fput backing file.
932 * Calling fput holding lo_ctl_mutex triggers a circular
933 * lock dependency possibility warning as fput can take
934 * bd_mutex which is usually taken before lo_ctl_mutex.
935 */
936 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
940static int
941loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
942{
943 int err;
944 struct loop_func_table *xfer;
Eric W. Biedermane4849732012-02-11 11:23:51 -0800945 kuid_t uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
David Howellsb0fafa82008-11-14 10:38:41 +1100947 if (lo->lo_encrypt_key_size &&
Eric W. Biedermane4849732012-02-11 11:23:51 -0800948 !uid_eq(lo->lo_key_owner, uid) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 !capable(CAP_SYS_ADMIN))
950 return -EPERM;
951 if (lo->lo_state != Lo_bound)
952 return -ENXIO;
953 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
954 return -EINVAL;
955
956 err = loop_release_xfer(lo);
957 if (err)
958 return err;
959
960 if (info->lo_encrypt_type) {
961 unsigned int type = info->lo_encrypt_type;
962
963 if (type >= MAX_LO_CRYPT)
964 return -EINVAL;
965 xfer = xfer_funcs[type];
966 if (xfer == NULL)
967 return -EINVAL;
968 } else
969 xfer = NULL;
970
971 err = loop_init_xfer(lo, xfer, info);
972 if (err)
973 return err;
974
975 if (lo->lo_offset != info->lo_offset ||
Guo Chao7b0576a2013-02-21 15:16:47 -0800976 lo->lo_sizelimit != info->lo_sizelimit)
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100977 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EFBIG;
Guo Chao541c7422013-02-21 15:16:46 -0800979
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200980 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
983 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
984 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
985 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
986
987 if (!xfer)
988 xfer = &none_funcs;
989 lo->transfer = xfer->transfer;
990 lo->ioctl = xfer->ioctl;
991
David Woodhouse96c58652008-02-06 01:36:27 -0800992 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
993 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
994 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
995
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200996 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
997 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
998 lo->lo_flags |= LO_FLAGS_PARTSCAN;
999 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1000 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
1001 }
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1004 lo->lo_init[0] = info->lo_init[0];
1005 lo->lo_init[1] = info->lo_init[1];
1006 if (info->lo_encrypt_key_size) {
1007 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1008 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001009 lo->lo_key_owner = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 return 0;
1013}
1014
1015static int
1016loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1017{
1018 struct file *file = lo->lo_backing_file;
1019 struct kstat stat;
1020 int error;
1021
1022 if (lo->lo_state != Lo_bound)
1023 return -ENXIO;
Al Viro3dadecc2013-01-24 02:18:08 -05001024 error = vfs_getattr(&file->f_path, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (error)
1026 return error;
1027 memset(info, 0, sizeof(*info));
1028 info->lo_number = lo->lo_number;
1029 info->lo_device = huge_encode_dev(stat.dev);
1030 info->lo_inode = stat.ino;
1031 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1032 info->lo_offset = lo->lo_offset;
1033 info->lo_sizelimit = lo->lo_sizelimit;
1034 info->lo_flags = lo->lo_flags;
1035 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1036 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1037 info->lo_encrypt_type =
1038 lo->lo_encryption ? lo->lo_encryption->number : 0;
1039 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1040 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1041 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1042 lo->lo_encrypt_key_size);
1043 }
1044 return 0;
1045}
1046
1047static void
1048loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1049{
1050 memset(info64, 0, sizeof(*info64));
1051 info64->lo_number = info->lo_number;
1052 info64->lo_device = info->lo_device;
1053 info64->lo_inode = info->lo_inode;
1054 info64->lo_rdevice = info->lo_rdevice;
1055 info64->lo_offset = info->lo_offset;
1056 info64->lo_sizelimit = 0;
1057 info64->lo_encrypt_type = info->lo_encrypt_type;
1058 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1059 info64->lo_flags = info->lo_flags;
1060 info64->lo_init[0] = info->lo_init[0];
1061 info64->lo_init[1] = info->lo_init[1];
1062 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1063 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1064 else
1065 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1066 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1067}
1068
1069static int
1070loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1071{
1072 memset(info, 0, sizeof(*info));
1073 info->lo_number = info64->lo_number;
1074 info->lo_device = info64->lo_device;
1075 info->lo_inode = info64->lo_inode;
1076 info->lo_rdevice = info64->lo_rdevice;
1077 info->lo_offset = info64->lo_offset;
1078 info->lo_encrypt_type = info64->lo_encrypt_type;
1079 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1080 info->lo_flags = info64->lo_flags;
1081 info->lo_init[0] = info64->lo_init[0];
1082 info->lo_init[1] = info64->lo_init[1];
1083 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1084 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1085 else
1086 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1087 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1088
1089 /* error in case values were truncated */
1090 if (info->lo_device != info64->lo_device ||
1091 info->lo_rdevice != info64->lo_rdevice ||
1092 info->lo_inode != info64->lo_inode ||
1093 info->lo_offset != info64->lo_offset)
1094 return -EOVERFLOW;
1095
1096 return 0;
1097}
1098
1099static int
1100loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1101{
1102 struct loop_info info;
1103 struct loop_info64 info64;
1104
1105 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1106 return -EFAULT;
1107 loop_info64_from_old(&info, &info64);
1108 return loop_set_status(lo, &info64);
1109}
1110
1111static int
1112loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1113{
1114 struct loop_info64 info64;
1115
1116 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1117 return -EFAULT;
1118 return loop_set_status(lo, &info64);
1119}
1120
1121static int
1122loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1123 struct loop_info info;
1124 struct loop_info64 info64;
1125 int err = 0;
1126
1127 if (!arg)
1128 err = -EINVAL;
1129 if (!err)
1130 err = loop_get_status(lo, &info64);
1131 if (!err)
1132 err = loop_info64_to_old(&info64, &info);
1133 if (!err && copy_to_user(arg, &info, sizeof(info)))
1134 err = -EFAULT;
1135
1136 return err;
1137}
1138
1139static int
1140loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1141 struct loop_info64 info64;
1142 int err = 0;
1143
1144 if (!arg)
1145 err = -EINVAL;
1146 if (!err)
1147 err = loop_get_status(lo, &info64);
1148 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1149 err = -EFAULT;
1150
1151 return err;
1152}
1153
J. R. Okajima53d66602009-03-31 15:23:43 -07001154static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1155{
J. R. Okajima53d66602009-03-31 15:23:43 -07001156 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001157 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001158
Guo Chao7b0576a2013-02-21 15:16:47 -08001159 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
J. R. Okajima53d66602009-03-31 15:23:43 -07001160}
1161
Al Virobb214882008-03-02 09:29:48 -05001162static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unsigned int cmd, unsigned long arg)
1164{
Al Virobb214882008-03-02 09:29:48 -05001165 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int err;
1167
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001168 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 switch (cmd) {
1170 case LOOP_SET_FD:
Al Virobb214882008-03-02 09:29:48 -05001171 err = loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001174 err = loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
1176 case LOOP_CLR_FD:
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001177 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
Ayan George4c823cc2011-09-21 10:02:13 +02001178 err = loop_clr_fd(lo);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001179 if (!err)
1180 goto out_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001183 err = -EPERM;
1184 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1185 err = loop_set_status_old(lo,
1186 (struct loop_info __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 case LOOP_GET_STATUS:
1189 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1190 break;
1191 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001192 err = -EPERM;
1193 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1194 err = loop_set_status64(lo,
1195 (struct loop_info64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197 case LOOP_GET_STATUS64:
1198 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1199 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001200 case LOOP_SET_CAPACITY:
1201 err = -EPERM;
1202 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1203 err = loop_set_capacity(lo, bdev);
1204 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 default:
1206 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1207 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001208 mutex_unlock(&lo->lo_ctl_mutex);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001209
1210out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return err;
1212}
1213
David Howells863d5b822006-08-29 19:06:14 +01001214#ifdef CONFIG_COMPAT
1215struct compat_loop_info {
1216 compat_int_t lo_number; /* ioctl r/o */
1217 compat_dev_t lo_device; /* ioctl r/o */
1218 compat_ulong_t lo_inode; /* ioctl r/o */
1219 compat_dev_t lo_rdevice; /* ioctl r/o */
1220 compat_int_t lo_offset;
1221 compat_int_t lo_encrypt_type;
1222 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1223 compat_int_t lo_flags; /* ioctl r/o */
1224 char lo_name[LO_NAME_SIZE];
1225 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1226 compat_ulong_t lo_init[2];
1227 char reserved[4];
1228};
1229
1230/*
1231 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1232 * - noinlined to reduce stack space usage in main part of driver
1233 */
1234static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001235loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001236 struct loop_info64 *info64)
1237{
1238 struct compat_loop_info info;
1239
1240 if (copy_from_user(&info, arg, sizeof(info)))
1241 return -EFAULT;
1242
1243 memset(info64, 0, sizeof(*info64));
1244 info64->lo_number = info.lo_number;
1245 info64->lo_device = info.lo_device;
1246 info64->lo_inode = info.lo_inode;
1247 info64->lo_rdevice = info.lo_rdevice;
1248 info64->lo_offset = info.lo_offset;
1249 info64->lo_sizelimit = 0;
1250 info64->lo_encrypt_type = info.lo_encrypt_type;
1251 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1252 info64->lo_flags = info.lo_flags;
1253 info64->lo_init[0] = info.lo_init[0];
1254 info64->lo_init[1] = info.lo_init[1];
1255 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1256 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1257 else
1258 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1259 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1260 return 0;
1261}
1262
1263/*
1264 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1265 * - noinlined to reduce stack space usage in main part of driver
1266 */
1267static noinline int
1268loop_info64_to_compat(const struct loop_info64 *info64,
1269 struct compat_loop_info __user *arg)
1270{
1271 struct compat_loop_info info;
1272
1273 memset(&info, 0, sizeof(info));
1274 info.lo_number = info64->lo_number;
1275 info.lo_device = info64->lo_device;
1276 info.lo_inode = info64->lo_inode;
1277 info.lo_rdevice = info64->lo_rdevice;
1278 info.lo_offset = info64->lo_offset;
1279 info.lo_encrypt_type = info64->lo_encrypt_type;
1280 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1281 info.lo_flags = info64->lo_flags;
1282 info.lo_init[0] = info64->lo_init[0];
1283 info.lo_init[1] = info64->lo_init[1];
1284 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1285 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1286 else
1287 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1288 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1289
1290 /* error in case values were truncated */
1291 if (info.lo_device != info64->lo_device ||
1292 info.lo_rdevice != info64->lo_rdevice ||
1293 info.lo_inode != info64->lo_inode ||
1294 info.lo_offset != info64->lo_offset ||
1295 info.lo_init[0] != info64->lo_init[0] ||
1296 info.lo_init[1] != info64->lo_init[1])
1297 return -EOVERFLOW;
1298
1299 if (copy_to_user(arg, &info, sizeof(info)))
1300 return -EFAULT;
1301 return 0;
1302}
1303
1304static int
1305loop_set_status_compat(struct loop_device *lo,
1306 const struct compat_loop_info __user *arg)
1307{
1308 struct loop_info64 info64;
1309 int ret;
1310
1311 ret = loop_info64_from_compat(arg, &info64);
1312 if (ret < 0)
1313 return ret;
1314 return loop_set_status(lo, &info64);
1315}
1316
1317static int
1318loop_get_status_compat(struct loop_device *lo,
1319 struct compat_loop_info __user *arg)
1320{
1321 struct loop_info64 info64;
1322 int err = 0;
1323
1324 if (!arg)
1325 err = -EINVAL;
1326 if (!err)
1327 err = loop_get_status(lo, &info64);
1328 if (!err)
1329 err = loop_info64_to_compat(&info64, arg);
1330 return err;
1331}
1332
Al Virobb214882008-03-02 09:29:48 -05001333static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1334 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001335{
Al Virobb214882008-03-02 09:29:48 -05001336 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001337 int err;
1338
David Howells863d5b822006-08-29 19:06:14 +01001339 switch(cmd) {
1340 case LOOP_SET_STATUS:
1341 mutex_lock(&lo->lo_ctl_mutex);
1342 err = loop_set_status_compat(
1343 lo, (const struct compat_loop_info __user *) arg);
1344 mutex_unlock(&lo->lo_ctl_mutex);
1345 break;
1346 case LOOP_GET_STATUS:
1347 mutex_lock(&lo->lo_ctl_mutex);
1348 err = loop_get_status_compat(
1349 lo, (struct compat_loop_info __user *) arg);
1350 mutex_unlock(&lo->lo_ctl_mutex);
1351 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001352 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001353 case LOOP_CLR_FD:
1354 case LOOP_GET_STATUS64:
1355 case LOOP_SET_STATUS64:
1356 arg = (unsigned long) compat_ptr(arg);
1357 case LOOP_SET_FD:
1358 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001359 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001360 break;
1361 default:
1362 err = -ENOIOCTLCMD;
1363 break;
1364 }
David Howells863d5b822006-08-29 19:06:14 +01001365 return err;
1366}
1367#endif
1368
Al Virobb214882008-03-02 09:29:48 -05001369static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Kay Sievers770fe302011-07-31 22:08:04 +02001371 struct loop_device *lo;
1372 int err = 0;
1373
1374 mutex_lock(&loop_index_mutex);
1375 lo = bdev->bd_disk->private_data;
1376 if (!lo) {
1377 err = -ENXIO;
1378 goto out;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Ingo Molnarf85221d2006-03-23 03:00:38 -08001381 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001383 mutex_unlock(&lo->lo_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02001384out:
1385 mutex_unlock(&loop_index_mutex);
1386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Al Virodb2a1442013-05-05 21:52:57 -04001389static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Al Virobb214882008-03-02 09:29:48 -05001391 struct loop_device *lo = disk->private_data;
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001392 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Ingo Molnarf85221d2006-03-23 03:00:38 -08001394 mutex_lock(&lo->lo_ctl_mutex);
David Woodhouse96c58652008-02-06 01:36:27 -08001395
Milan Broz14f27932008-12-12 14:48:27 +01001396 if (--lo->lo_refcnt)
1397 goto out;
1398
1399 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1400 /*
1401 * In autoclear mode, stop the loop thread
1402 * and remove configuration after last close.
1403 */
Ayan George4c823cc2011-09-21 10:02:13 +02001404 err = loop_clr_fd(lo);
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001405 if (!err)
Al Virodb2a1442013-05-05 21:52:57 -04001406 return;
Milan Broz14f27932008-12-12 14:48:27 +01001407 } else {
1408 /*
1409 * Otherwise keep thread (if running) and config,
1410 * but flush possible ongoing bios in thread.
1411 */
1412 loop_flush(lo);
1413 }
David Woodhouse96c58652008-02-06 01:36:27 -08001414
Milan Broz14f27932008-12-12 14:48:27 +01001415out:
Ingo Molnarf85221d2006-03-23 03:00:38 -08001416 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001419static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001421 .open = lo_open,
1422 .release = lo_release,
1423 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001424#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001425 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427};
1428
1429/*
1430 * And now the modules code and kernel interface.
1431 */
Ken Chen73285082007-05-08 00:28:20 -07001432static int max_loop;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001433module_param(max_loop, int, S_IRUGO);
Ken Chena47653f2007-06-08 13:46:44 -07001434MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Namhyung Kimac04fee2011-05-27 07:59:25 +02001435module_param(max_part, int, S_IRUGO);
Laurent Vivier476a4812008-03-26 12:11:53 +01001436MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437MODULE_LICENSE("GPL");
1438MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1439
1440int loop_register_transfer(struct loop_func_table *funcs)
1441{
1442 unsigned int n = funcs->number;
1443
1444 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1445 return -EINVAL;
1446 xfer_funcs[n] = funcs;
1447 return 0;
1448}
1449
Kay Sievers34dd82a2011-07-31 22:08:04 +02001450static int unregister_transfer_cb(int id, void *ptr, void *data)
1451{
1452 struct loop_device *lo = ptr;
1453 struct loop_func_table *xfer = data;
1454
1455 mutex_lock(&lo->lo_ctl_mutex);
1456 if (lo->lo_encryption == xfer)
1457 loop_release_xfer(lo);
1458 mutex_unlock(&lo->lo_ctl_mutex);
1459 return 0;
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462int loop_unregister_transfer(int number)
1463{
1464 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct loop_func_table *xfer;
1466
1467 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1468 return -EINVAL;
1469
1470 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001471 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473}
1474
1475EXPORT_SYMBOL(loop_register_transfer);
1476EXPORT_SYMBOL(loop_unregister_transfer);
1477
Ming Leib5dd2f62014-12-31 13:22:57 +00001478static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1479 const struct blk_mq_queue_data *bd)
1480{
1481 struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1482
1483 blk_mq_start_request(bd->rq);
1484
1485 if (cmd->rq->cmd_flags & REQ_WRITE) {
1486 struct loop_device *lo = cmd->rq->q->queuedata;
1487 bool need_sched = true;
1488
1489 spin_lock_irq(&lo->lo_lock);
1490 if (lo->write_started)
1491 need_sched = false;
1492 else
1493 lo->write_started = true;
1494 list_add_tail(&cmd->list, &lo->write_cmd_head);
1495 spin_unlock_irq(&lo->lo_lock);
1496
1497 if (need_sched)
1498 queue_work(loop_wq, &lo->write_work);
1499 } else {
1500 queue_work(loop_wq, &cmd->read_work);
1501 }
1502
1503 return BLK_MQ_RQ_QUEUE_OK;
1504}
1505
1506static void loop_handle_cmd(struct loop_cmd *cmd)
1507{
1508 const bool write = cmd->rq->cmd_flags & REQ_WRITE;
1509 struct loop_device *lo = cmd->rq->q->queuedata;
1510 int ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00001511
1512 if (lo->lo_state != Lo_bound)
1513 goto failed;
1514
1515 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
1516 goto failed;
1517
Ming Lei30112012014-12-31 13:22:58 +00001518 ret = do_req_filebacked(lo, cmd->rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001519
1520 failed:
1521 if (ret)
1522 cmd->rq->errors = -EIO;
1523 blk_mq_complete_request(cmd->rq);
1524}
1525
1526static void loop_queue_write_work(struct work_struct *work)
1527{
1528 struct loop_device *lo =
1529 container_of(work, struct loop_device, write_work);
1530 LIST_HEAD(cmd_list);
1531
1532 spin_lock_irq(&lo->lo_lock);
1533 repeat:
1534 list_splice_init(&lo->write_cmd_head, &cmd_list);
1535 spin_unlock_irq(&lo->lo_lock);
1536
1537 while (!list_empty(&cmd_list)) {
1538 struct loop_cmd *cmd = list_first_entry(&cmd_list,
1539 struct loop_cmd, list);
1540 list_del_init(&cmd->list);
1541 loop_handle_cmd(cmd);
1542 }
1543
1544 spin_lock_irq(&lo->lo_lock);
1545 if (!list_empty(&lo->write_cmd_head))
1546 goto repeat;
1547 lo->write_started = false;
1548 spin_unlock_irq(&lo->lo_lock);
1549}
1550
1551static void loop_queue_read_work(struct work_struct *work)
1552{
1553 struct loop_cmd *cmd =
1554 container_of(work, struct loop_cmd, read_work);
1555
1556 loop_handle_cmd(cmd);
1557}
1558
1559static int loop_init_request(void *data, struct request *rq,
1560 unsigned int hctx_idx, unsigned int request_idx,
1561 unsigned int numa_node)
1562{
1563 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1564
1565 cmd->rq = rq;
1566 INIT_WORK(&cmd->read_work, loop_queue_read_work);
1567
1568 return 0;
1569}
1570
1571static struct blk_mq_ops loop_mq_ops = {
1572 .queue_rq = loop_queue_rq,
1573 .map_queue = blk_mq_map_queue,
1574 .init_request = loop_init_request,
1575};
1576
Kay Sievers34dd82a2011-07-31 22:08:04 +02001577static int loop_add(struct loop_device **l, int i)
Ken Chen73285082007-05-08 00:28:20 -07001578{
1579 struct loop_device *lo;
1580 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001581 int err;
Ken Chen73285082007-05-08 00:28:20 -07001582
Silva Paulo68d740d2012-07-14 15:39:58 -07001583 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001584 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07001585 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07001586 goto out;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001587
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001588 lo->lo_state = Lo_unbound;
1589
Tejun Heoc718aa62013-02-27 17:03:58 -08001590 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001591 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08001592 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1593 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02001594 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001595 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08001596 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001597 }
1598 if (err < 0)
1599 goto out_free_dev;
Tejun Heoc718aa62013-02-27 17:03:58 -08001600 i = err;
Ken Chen73285082007-05-08 00:28:20 -07001601
Wei Yongjun183cfb52013-03-22 08:59:19 -06001602 err = -ENOMEM;
Ming Leib5dd2f62014-12-31 13:22:57 +00001603 lo->tag_set.ops = &loop_mq_ops;
1604 lo->tag_set.nr_hw_queues = 1;
1605 lo->tag_set.queue_depth = 128;
1606 lo->tag_set.numa_node = NUMA_NO_NODE;
1607 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
1608 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
1609 lo->tag_set.driver_data = lo;
1610
1611 err = blk_mq_alloc_tag_set(&lo->tag_set);
1612 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001613 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07001614
Ming Leib5dd2f62014-12-31 13:22:57 +00001615 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
1616 if (IS_ERR_OR_NULL(lo->lo_queue)) {
1617 err = PTR_ERR(lo->lo_queue);
1618 goto out_cleanup_tags;
1619 }
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001620 lo->lo_queue->queuedata = lo;
1621
Ming Leib5dd2f62014-12-31 13:22:57 +00001622 INIT_LIST_HEAD(&lo->write_cmd_head);
1623 INIT_WORK(&lo->write_work, loop_queue_write_work);
1624
Laurent Vivier476a4812008-03-26 12:11:53 +01001625 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07001626 if (!disk)
1627 goto out_free_queue;
1628
Kay Sieverse03c8dd2011-08-23 20:12:04 +02001629 /*
1630 * Disable partition scanning by default. The in-kernel partition
1631 * scanning can be requested individually per-device during its
1632 * setup. Userspace can always add and remove partitions from all
1633 * devices. The needed partition minors are allocated from the
1634 * extended minor space, the main loop device numbers will continue
1635 * to match the loop minors, regardless of the number of partitions
1636 * used.
1637 *
1638 * If max_part is given, partition scanning is globally enabled for
1639 * all loop devices. The minors for the main loop devices will be
1640 * multiples of max_part.
1641 *
1642 * Note: Global-for-all-devices, set-only-at-init, read-only module
1643 * parameteters like 'max_loop' and 'max_part' make things needlessly
1644 * complicated, are too static, inflexible and may surprise
1645 * userspace tools. Parameters like this in general should be avoided.
1646 */
1647 if (!part_shift)
1648 disk->flags |= GENHD_FL_NO_PART_SCAN;
1649 disk->flags |= GENHD_FL_EXT_DEVT;
Ken Chen73285082007-05-08 00:28:20 -07001650 mutex_init(&lo->lo_ctl_mutex);
1651 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07001652 spin_lock_init(&lo->lo_lock);
1653 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01001654 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07001655 disk->fops = &lo_fops;
1656 disk->private_data = lo;
1657 disk->queue = lo->lo_queue;
1658 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001659 add_disk(disk);
1660 *l = lo;
1661 return lo->lo_number;
Ken Chen73285082007-05-08 00:28:20 -07001662
1663out_free_queue:
1664 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001665out_cleanup_tags:
1666 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001667out_free_idr:
1668 idr_remove(&loop_index_idr, i);
Ken Chen73285082007-05-08 00:28:20 -07001669out_free_dev:
1670 kfree(lo);
1671out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001672 return err;
Ken Chen73285082007-05-08 00:28:20 -07001673}
1674
Kay Sievers34dd82a2011-07-31 22:08:04 +02001675static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001676{
Kay Sievers34dd82a2011-07-31 22:08:04 +02001677 del_gendisk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001678 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001679 blk_mq_free_tag_set(&lo->tag_set);
Ken Chen73285082007-05-08 00:28:20 -07001680 put_disk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001681 kfree(lo);
1682}
1683
Kay Sievers770fe302011-07-31 22:08:04 +02001684static int find_free_cb(int id, void *ptr, void *data)
Ken Chena47653f2007-06-08 13:46:44 -07001685{
Kay Sievers770fe302011-07-31 22:08:04 +02001686 struct loop_device *lo = ptr;
1687 struct loop_device **l = data;
Ken Chena47653f2007-06-08 13:46:44 -07001688
Kay Sievers770fe302011-07-31 22:08:04 +02001689 if (lo->lo_state == Lo_unbound) {
1690 *l = lo;
1691 return 1;
Ken Chena47653f2007-06-08 13:46:44 -07001692 }
Kay Sievers770fe302011-07-31 22:08:04 +02001693 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07001694}
1695
Kay Sievers34dd82a2011-07-31 22:08:04 +02001696static int loop_lookup(struct loop_device **l, int i)
Ken Chena47653f2007-06-08 13:46:44 -07001697{
Ken Chena47653f2007-06-08 13:46:44 -07001698 struct loop_device *lo;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001699 int ret = -ENODEV;
Ken Chena47653f2007-06-08 13:46:44 -07001700
Kay Sievers770fe302011-07-31 22:08:04 +02001701 if (i < 0) {
1702 int err;
1703
1704 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1705 if (err == 1) {
1706 *l = lo;
1707 ret = lo->lo_number;
1708 }
1709 goto out;
1710 }
1711
1712 /* lookup and return a specific i */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001713 lo = idr_find(&loop_index_idr, i);
Ken Chena47653f2007-06-08 13:46:44 -07001714 if (lo) {
Kay Sievers34dd82a2011-07-31 22:08:04 +02001715 *l = lo;
1716 ret = lo->lo_number;
Ken Chena47653f2007-06-08 13:46:44 -07001717 }
Kay Sievers770fe302011-07-31 22:08:04 +02001718out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001719 return ret;
Ken Chena47653f2007-06-08 13:46:44 -07001720}
1721
Ken Chen73285082007-05-08 00:28:20 -07001722static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1723{
Al Viro705962c2007-05-13 05:52:32 -04001724 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001725 struct kobject *kobj;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001726 int err;
Ken Chen73285082007-05-08 00:28:20 -07001727
Kay Sievers34dd82a2011-07-31 22:08:04 +02001728 mutex_lock(&loop_index_mutex);
1729 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1730 if (err < 0)
1731 err = loop_add(&lo, MINOR(dev) >> part_shift);
1732 if (err < 0)
Mikulas Patockaa207f592013-10-14 12:13:24 -04001733 kobj = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001734 else
1735 kobj = get_disk(lo->lo_disk);
1736 mutex_unlock(&loop_index_mutex);
Ken Chen73285082007-05-08 00:28:20 -07001737
1738 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001739 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001740}
1741
Kay Sievers770fe302011-07-31 22:08:04 +02001742static long loop_control_ioctl(struct file *file, unsigned int cmd,
1743 unsigned long parm)
1744{
1745 struct loop_device *lo;
1746 int ret = -ENOSYS;
1747
1748 mutex_lock(&loop_index_mutex);
1749 switch (cmd) {
1750 case LOOP_CTL_ADD:
1751 ret = loop_lookup(&lo, parm);
1752 if (ret >= 0) {
1753 ret = -EEXIST;
1754 break;
1755 }
1756 ret = loop_add(&lo, parm);
1757 break;
1758 case LOOP_CTL_REMOVE:
1759 ret = loop_lookup(&lo, parm);
1760 if (ret < 0)
1761 break;
1762 mutex_lock(&lo->lo_ctl_mutex);
1763 if (lo->lo_state != Lo_unbound) {
1764 ret = -EBUSY;
1765 mutex_unlock(&lo->lo_ctl_mutex);
1766 break;
1767 }
1768 if (lo->lo_refcnt > 0) {
1769 ret = -EBUSY;
1770 mutex_unlock(&lo->lo_ctl_mutex);
1771 break;
1772 }
1773 lo->lo_disk->private_data = NULL;
1774 mutex_unlock(&lo->lo_ctl_mutex);
1775 idr_remove(&loop_index_idr, lo->lo_number);
1776 loop_remove(lo);
1777 break;
1778 case LOOP_CTL_GET_FREE:
1779 ret = loop_lookup(&lo, -1);
1780 if (ret >= 0)
1781 break;
1782 ret = loop_add(&lo, -1);
1783 }
1784 mutex_unlock(&loop_index_mutex);
1785
1786 return ret;
1787}
1788
1789static const struct file_operations loop_ctl_fops = {
1790 .open = nonseekable_open,
1791 .unlocked_ioctl = loop_control_ioctl,
1792 .compat_ioctl = loop_control_ioctl,
1793 .owner = THIS_MODULE,
1794 .llseek = noop_llseek,
1795};
1796
1797static struct miscdevice loop_misc = {
1798 .minor = LOOP_CTRL_MINOR,
1799 .name = "loop-control",
1800 .fops = &loop_ctl_fops,
1801};
1802
1803MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1804MODULE_ALIAS("devname:loop-control");
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806static int __init loop_init(void)
1807{
Ken Chena47653f2007-06-08 13:46:44 -07001808 int i, nr;
1809 unsigned long range;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001810 struct loop_device *lo;
Kay Sievers770fe302011-07-31 22:08:04 +02001811 int err;
Ken Chena47653f2007-06-08 13:46:44 -07001812
Kay Sievers770fe302011-07-31 22:08:04 +02001813 err = misc_register(&loop_misc);
1814 if (err < 0)
1815 return err;
Laurent Vivier476a4812008-03-26 12:11:53 +01001816
1817 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001818 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01001819 part_shift = fls(max_part);
1820
Namhyung Kimac04fee2011-05-27 07:59:25 +02001821 /*
1822 * Adjust max_part according to part_shift as it is exported
1823 * to user space so that user can decide correct minor number
1824 * if [s]he want to create more devices.
1825 *
1826 * Note that -1 is required because partition 0 is reserved
1827 * for the whole disk.
1828 */
1829 max_part = (1UL << part_shift) - 1;
1830 }
1831
Guo Chaob1a66502013-02-21 15:16:49 -08001832 if ((1UL << part_shift) > DISK_MAX_PARTS) {
1833 err = -EINVAL;
1834 goto misc_out;
1835 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02001836
Guo Chaob1a66502013-02-21 15:16:49 -08001837 if (max_loop > 1UL << (MINORBITS - part_shift)) {
1838 err = -EINVAL;
1839 goto misc_out;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Kay Sieversd134b002011-07-31 22:08:04 +02001842 /*
1843 * If max_loop is specified, create that many devices upfront.
1844 * This also becomes a hard limit. If max_loop is not specified,
1845 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1846 * init time. Loop devices can be requested on-demand with the
1847 * /dev/loop-control interface, or be instantiated by accessing
1848 * a 'dead' device node.
1849 */
Ken Chen73285082007-05-08 00:28:20 -07001850 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001851 nr = max_loop;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001852 range = max_loop << part_shift;
Ken Chena47653f2007-06-08 13:46:44 -07001853 } else {
Kay Sieversd134b002011-07-31 22:08:04 +02001854 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001855 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Ken Chena47653f2007-06-08 13:46:44 -07001857
Guo Chaob1a66502013-02-21 15:16:49 -08001858 if (register_blkdev(LOOP_MAJOR, "loop")) {
1859 err = -EIO;
1860 goto misc_out;
1861 }
Ken Chena47653f2007-06-08 13:46:44 -07001862
Ming Leib5dd2f62014-12-31 13:22:57 +00001863 loop_wq = alloc_workqueue("kloopd",
1864 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0);
1865 if (!loop_wq) {
1866 err = -ENOMEM;
1867 goto misc_out;
1868 }
1869
Ken Chena47653f2007-06-08 13:46:44 -07001870 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1871 THIS_MODULE, loop_probe, NULL, NULL);
1872
Kay Sieversd134b002011-07-31 22:08:04 +02001873 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001874 mutex_lock(&loop_index_mutex);
1875 for (i = 0; i < nr; i++)
1876 loop_add(&lo, i);
1877 mutex_unlock(&loop_index_mutex);
1878
Ken Chen73285082007-05-08 00:28:20 -07001879 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08001881
1882misc_out:
1883 misc_deregister(&loop_misc);
1884 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001885}
Ken Chena47653f2007-06-08 13:46:44 -07001886
Kay Sievers34dd82a2011-07-31 22:08:04 +02001887static int loop_exit_cb(int id, void *ptr, void *data)
1888{
1889 struct loop_device *lo = ptr;
Ken Chena47653f2007-06-08 13:46:44 -07001890
Kay Sievers34dd82a2011-07-31 22:08:04 +02001891 loop_remove(lo);
1892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Ken Chen73285082007-05-08 00:28:20 -07001895static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Ken Chena47653f2007-06-08 13:46:44 -07001897 unsigned long range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Namhyung Kima1c15c52011-05-24 16:48:55 +02001899 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
Ken Chena47653f2007-06-08 13:46:44 -07001900
Kay Sievers34dd82a2011-07-31 22:08:04 +02001901 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001902 idr_destroy(&loop_index_idr);
Ken Chen73285082007-05-08 00:28:20 -07001903
Ken Chena47653f2007-06-08 13:46:44 -07001904 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001905 unregister_blkdev(LOOP_MAJOR, "loop");
Kay Sievers770fe302011-07-31 22:08:04 +02001906
Ming Leib5dd2f62014-12-31 13:22:57 +00001907 destroy_workqueue(loop_wq);
1908
Kay Sievers770fe302011-07-31 22:08:04 +02001909 misc_deregister(&loop_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
1912module_init(loop_init);
1913module_exit(loop_exit);
1914
1915#ifndef MODULE
1916static int __init max_loop_setup(char *str)
1917{
1918 max_loop = simple_strtol(str, NULL, 0);
1919 return 1;
1920}
1921
1922__setup("max_loop=", max_loop_setup);
1923#endif