blob: 185a86d73c3fd2901908cda47a51b9bb1a816597 [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>
Ming Leib5dd2f62014-12-31 13:22:57 +000078#include <linux/blk-mq.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{
233 ssize_t bw;
234 mm_segment_t old_fs = get_fs();
235
Al Viro03d95eb2013-03-20 13:04:20 -0400236 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 set_fs(get_ds());
238 bw = file->f_op->write(file, buf, len, &pos);
239 set_fs(old_fs);
Al Viro03d95eb2013-03-20 13:04:20 -0400240 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (likely(bw == len))
242 return 0;
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700243 printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 (unsigned long long)pos, len);
245 if (bw >= 0)
246 bw = -EIO;
247 return bw;
248}
249
250/**
251 * do_lo_send_direct_write - helper for writing data to a loop device
252 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200253 * This is the fast, non-transforming version that does not need double
254 * buffering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256static int do_lo_send_direct_write(struct loop_device *lo,
Al Viro511de732007-10-08 12:10:13 -0400257 struct bio_vec *bvec, loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100260 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 bvec->bv_len, pos);
262 kunmap(bvec->bv_page);
263 cond_resched();
264 return bw;
265}
266
267/**
268 * do_lo_send_write - helper for writing data to a loop device
269 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200270 * This is the slow, transforming version that needs to double buffer the
271 * data as it cannot do the transformations in place without having direct
272 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400275 loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
278 bvec->bv_offset, bvec->bv_len, pos >> 9);
279 if (likely(!ret))
280 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100281 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 pos);
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700283 printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
285 if (ret > 0)
286 ret = -EIO;
287 return ret;
288}
289
Ming Lei30112012014-12-31 13:22:58 +0000290static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Al Viro511de732007-10-08 12:10:13 -0400292 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct page *page);
Kent Overstreet79886132013-11-23 17:19:00 -0800294 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000295 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct page *page = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800297 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Christoph Hellwig456be142011-10-17 12:57:20 +0200299 if (lo->transfer != transfer_none) {
300 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
301 if (unlikely(!page))
302 goto fail;
303 kmap(page);
304 do_lo_send = do_lo_send_write;
305 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 do_lo_send = do_lo_send_direct_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Christoph Hellwig456be142011-10-17 12:57:20 +0200308
Ming Lei30112012014-12-31 13:22:58 +0000309 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800310 ret = do_lo_send(lo, &bvec, pos, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ret < 0)
312 break;
Kent Overstreet79886132013-11-23 17:19:00 -0800313 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 if (page) {
316 kunmap(page);
317 __free_page(page);
318 }
319out:
320 return ret;
321fail:
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700322 printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ret = -ENOMEM;
324 goto out;
325}
326
327struct lo_read_data {
328 struct loop_device *lo;
329 struct page *page;
330 unsigned offset;
331 int bsize;
332};
333
334static int
Jens Axboefd582142007-06-12 21:20:37 +0200335lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
336 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jens Axboefd582142007-06-12 21:20:37 +0200338 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200340 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 sector_t IV;
Jens Axboe3603b8e2010-12-20 09:15:19 +0100342 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jens Axboefd582142007-06-12 21:20:37 +0200344 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
345 (buf->offset >> 9);
346 size = sd->len;
347 if (size > p->bsize)
348 size = p->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jens Axboefd582142007-06-12 21:20:37 +0200350 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700351 printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 page->index);
Jens Axboefd582142007-06-12 21:20:37 +0200353 size = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 flush_dcache_page(p->page);
357
Jens Axboefd582142007-06-12 21:20:37 +0200358 if (size > 0)
359 p->offset += size;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return size;
362}
363
364static int
Jens Axboefd582142007-06-12 21:20:37 +0200365lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
366{
367 return __splice_from_pipe(pipe, sd, lo_splice_actor);
368}
369
Dave Young306df072012-02-08 22:07:19 +0100370static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371do_lo_receive(struct loop_device *lo,
372 struct bio_vec *bvec, int bsize, loff_t pos)
373{
374 struct lo_read_data cookie;
Jens Axboefd582142007-06-12 21:20:37 +0200375 struct splice_desc sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct file *file;
Dave Young306df072012-02-08 22:07:19 +0100377 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 cookie.lo = lo;
380 cookie.page = bvec->bv_page;
381 cookie.offset = bvec->bv_offset;
382 cookie.bsize = bsize;
Jens Axboefd582142007-06-12 21:20:37 +0200383
384 sd.len = 0;
385 sd.total_len = bvec->bv_len;
386 sd.flags = 0;
387 sd.pos = pos;
388 sd.u.data = &cookie;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 file = lo->lo_backing_file;
Jens Axboefd582142007-06-12 21:20:37 +0200391 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
392
Dave Young306df072012-02-08 22:07:19 +0100393 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396static int
Ming Lei30112012014-12-31 13:22:58 +0000397lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Kent Overstreet79886132013-11-23 17:19:00 -0800399 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000400 struct req_iterator iter;
Dave Young306df072012-02-08 22:07:19 +0100401 ssize_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Ming Lei30112012014-12-31 13:22:58 +0000403 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800404 s = do_lo_receive(lo, &bvec, bsize, pos);
Dave Young306df072012-02-08 22:07:19 +0100405 if (s < 0)
406 return s;
407
Kent Overstreet79886132013-11-23 17:19:00 -0800408 if (s != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000409 struct bio *bio;
410
411 __rq_for_each_bio(bio, rq)
412 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
Dave Young306df072012-02-08 22:07:19 +0100414 }
Kent Overstreet79886132013-11-23 17:19:00 -0800415 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Dave Young306df072012-02-08 22:07:19 +0100417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Ming Leicf655d92014-12-31 13:22:59 +0000420static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
421{
422 /*
423 * We use punch hole to reclaim the free space used by the
424 * image a.k.a. discard. However we do not support discard if
425 * encryption is enabled, because it may give an attacker
426 * useful information.
427 */
428 struct file *file = lo->lo_backing_file;
429 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
430 int ret;
431
432 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
433 ret = -EOPNOTSUPP;
434 goto out;
435 }
436
437 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
438 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
439 ret = -EIO;
440 out:
441 return ret;
442}
443
444static int lo_req_flush(struct loop_device *lo, struct request *rq)
445{
446 struct file *file = lo->lo_backing_file;
447 int ret = vfs_fsync(file, 0);
448 if (unlikely(ret && ret != -EINVAL))
449 ret = -EIO;
450
451 return ret;
452}
453
Ming Lei30112012014-12-31 13:22:58 +0000454static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 loff_t pos;
457 int ret;
458
Ming Lei30112012014-12-31 13:22:58 +0000459 pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100460
Ming Lei30112012014-12-31 13:22:58 +0000461 if (rq->cmd_flags & REQ_WRITE) {
Ming Leicf655d92014-12-31 13:22:59 +0000462 if (rq->cmd_flags & REQ_FLUSH)
463 ret = lo_req_flush(lo, rq);
Ming Leiaf65aa82014-12-31 13:23:00 +0000464 else if (rq->cmd_flags & REQ_DISCARD)
Ming Leicf655d92014-12-31 13:22:59 +0000465 ret = lo_discard(lo, rq, pos);
Ming Leiaf65aa82014-12-31 13:23:00 +0000466 else
467 ret = lo_send(lo, rq, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100468 } else
Ming Lei30112012014-12-31 13:22:58 +0000469 ret = lo_receive(lo, rq, lo->lo_blocksize, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474struct switch_request {
475 struct file *file;
476 struct completion wait;
477};
478
Milan Broz14f27932008-12-12 14:48:27 +0100479/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Do the actual switch; called from the BIO completion routine
481 */
482static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
483{
484 struct file *file = p->file;
485 struct file *old_file = lo->lo_backing_file;
Milan Broz14f27932008-12-12 14:48:27 +0100486 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Milan Broz14f27932008-12-12 14:48:27 +0100488 /* if no new file, only flush of queued bios requested */
489 if (!file)
Ming Leib5dd2f62014-12-31 13:22:57 +0000490 return;
Milan Broz14f27932008-12-12 14:48:27 +0100491
492 mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
494 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700495 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
496 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 lo->old_gfp_mask = mapping_gfp_mask(mapping);
498 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Ming Leib5dd2f62014-12-31 13:22:57 +0000501/*
502 * loop_switch performs the hard work of switching a backing store.
503 * First it needs to flush existing IO, it does this by sending a magic
504 * BIO down the pipe. The completion of this BIO does the actual switch.
505 */
506static int loop_switch(struct loop_device *lo, struct file *file)
507{
508 struct switch_request w;
509
510 w.file = file;
511
512 /* freeze queue and wait for completion of scheduled requests */
513 blk_mq_freeze_queue(lo->lo_queue);
514
515 /* do the switch action */
516 do_loop_switch(lo, &w);
517
518 /* unfreeze */
519 blk_mq_unfreeze_queue(lo->lo_queue);
520
521 return 0;
522}
523
524/*
525 * Helper to flush the IOs in loop, but keeping loop thread running
526 */
527static int loop_flush(struct loop_device *lo)
528{
529 return loop_switch(lo, NULL);
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532/*
533 * loop_change_fd switched the backing store of a loopback device to
534 * a new file. This is useful for operating system installers to free up
535 * the original file and in High Availability environments to switch to
536 * an alternative location for the content in case of server meltdown.
537 * This can only work if the loop device is used read-only, and if the
538 * new backing store is the same size and type as the old backing store.
539 */
Al Virobb214882008-03-02 09:29:48 -0500540static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
541 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct file *file, *old_file;
544 struct inode *inode;
545 int error;
546
547 error = -ENXIO;
548 if (lo->lo_state != Lo_bound)
549 goto out;
550
551 /* the loop device has to be read-only */
552 error = -EINVAL;
553 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
554 goto out;
555
556 error = -EBADF;
557 file = fget(arg);
558 if (!file)
559 goto out;
560
561 inode = file->f_mapping->host;
562 old_file = lo->lo_backing_file;
563
564 error = -EINVAL;
565
566 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
567 goto out_putf;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* size of the new backing store needs to be the same */
570 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
571 goto out_putf;
572
573 /* and ... switch */
574 error = loop_switch(lo, file);
575 if (error)
576 goto out_putf;
577
578 fput(old_file);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200579 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100580 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582
583 out_putf:
584 fput(file);
585 out:
586 return error;
587}
588
589static inline int is_loop_device(struct file *file)
590{
591 struct inode *i = file->f_mapping->host;
592
593 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
594}
595
Milan Brozee862732010-08-23 15:16:00 +0200596/* loop sysfs attributes */
597
598static ssize_t loop_attr_show(struct device *dev, char *page,
599 ssize_t (*callback)(struct loop_device *, char *))
600{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200601 struct gendisk *disk = dev_to_disk(dev);
602 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200603
Kay Sievers34dd82a2011-07-31 22:08:04 +0200604 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200605}
606
607#define LOOP_ATTR_RO(_name) \
608static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
609static ssize_t loop_attr_do_show_##_name(struct device *d, \
610 struct device_attribute *attr, char *b) \
611{ \
612 return loop_attr_show(d, b, loop_attr_##_name##_show); \
613} \
614static struct device_attribute loop_attr_##_name = \
615 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
616
617static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
618{
619 ssize_t ret;
620 char *p = NULL;
621
Kay Sievers05eb0f22011-07-31 22:21:35 +0200622 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200623 if (lo->lo_backing_file)
624 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200625 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200626
627 if (IS_ERR_OR_NULL(p))
628 ret = PTR_ERR(p);
629 else {
630 ret = strlen(p);
631 memmove(buf, p, ret);
632 buf[ret++] = '\n';
633 buf[ret] = 0;
634 }
635
636 return ret;
637}
638
639static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
640{
641 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
642}
643
644static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
645{
646 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
647}
648
649static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
650{
651 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
652
653 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
654}
655
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200656static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
657{
658 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
659
660 return sprintf(buf, "%s\n", partscan ? "1" : "0");
661}
662
Milan Brozee862732010-08-23 15:16:00 +0200663LOOP_ATTR_RO(backing_file);
664LOOP_ATTR_RO(offset);
665LOOP_ATTR_RO(sizelimit);
666LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200667LOOP_ATTR_RO(partscan);
Milan Brozee862732010-08-23 15:16:00 +0200668
669static struct attribute *loop_attrs[] = {
670 &loop_attr_backing_file.attr,
671 &loop_attr_offset.attr,
672 &loop_attr_sizelimit.attr,
673 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200674 &loop_attr_partscan.attr,
Milan Brozee862732010-08-23 15:16:00 +0200675 NULL,
676};
677
678static struct attribute_group loop_attribute_group = {
679 .name = "loop",
680 .attrs= loop_attrs,
681};
682
683static int loop_sysfs_init(struct loop_device *lo)
684{
685 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
686 &loop_attribute_group);
687}
688
689static void loop_sysfs_exit(struct loop_device *lo)
690{
691 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
692 &loop_attribute_group);
693}
694
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200695static void loop_config_discard(struct loop_device *lo)
696{
697 struct file *file = lo->lo_backing_file;
698 struct inode *inode = file->f_mapping->host;
699 struct request_queue *q = lo->lo_queue;
700
701 /*
702 * We use punch hole to reclaim the free space used by the
Olaf Hering12a64d22014-01-21 14:39:24 -0800703 * image a.k.a. discard. However we do not support discard if
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200704 * encryption is enabled, because it may give an attacker
705 * useful information.
706 */
707 if ((!file->f_op->fallocate) ||
708 lo->lo_encrypt_key_size) {
709 q->limits.discard_granularity = 0;
710 q->limits.discard_alignment = 0;
711 q->limits.max_discard_sectors = 0;
712 q->limits.discard_zeroes_data = 0;
713 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
714 return;
715 }
716
717 q->limits.discard_granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaf3c02011-12-02 14:47:03 +0100718 q->limits.discard_alignment = 0;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200719 q->limits.max_discard_sectors = UINT_MAX >> 9;
720 q->limits.discard_zeroes_data = 1;
721 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
722}
723
Al Virobb214882008-03-02 09:29:48 -0500724static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct block_device *bdev, unsigned int arg)
726{
727 struct file *file, *f;
728 struct inode *inode;
729 struct address_space *mapping;
730 unsigned lo_blocksize;
731 int lo_flags = 0;
732 int error;
733 loff_t size;
734
735 /* This is safe, since we have a reference from open(). */
736 __module_get(THIS_MODULE);
737
738 error = -EBADF;
739 file = fget(arg);
740 if (!file)
741 goto out;
742
743 error = -EBUSY;
744 if (lo->lo_state != Lo_unbound)
745 goto out_putf;
746
747 /* Avoid recursion */
748 f = file;
749 while (is_loop_device(f)) {
750 struct loop_device *l;
751
Al Virobb214882008-03-02 09:29:48 -0500752 if (f->f_mapping->host->i_bdev == bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto out_putf;
754
755 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
756 if (l->lo_state == Lo_unbound) {
757 error = -EINVAL;
758 goto out_putf;
759 }
760 f = l->lo_backing_file;
761 }
762
763 mapping = file->f_mapping;
764 inode = mapping->host;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 error = -EINVAL;
Christoph Hellwig456be142011-10-17 12:57:20 +0200767 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Christoph Hellwig456be142011-10-17 12:57:20 +0200770 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
771 !file->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 lo_flags |= LO_FLAGS_READ_ONLY;
773
Christoph Hellwig456be142011-10-17 12:57:20 +0200774 lo_blocksize = S_ISBLK(inode->i_mode) ?
775 inode->i_bdev->bd_block_size : PAGE_SIZE;
776
777 error = -EFBIG;
778 size = get_loop_size(lo, file);
779 if ((loff_t)(sector_t)size != size)
780 goto out_putf;
781
782 error = 0;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
785
786 lo->lo_blocksize = lo_blocksize;
787 lo->lo_device = bdev;
788 lo->lo_flags = lo_flags;
789 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700790 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 lo->ioctl = NULL;
792 lo->lo_sizelimit = 0;
793 lo->old_gfp_mask = mapping_gfp_mask(mapping);
794 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
795
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100796 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Tejun Heo4913efe2010-09-03 11:56:16 +0200797 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100798
Ken Chen73285082007-05-08 00:28:20 -0700799 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +0200801 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200802 /* let user-space know about the new size */
803 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 set_blocksize(bdev, lo_blocksize);
806
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700807 lo->lo_state = Lo_bound;
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200808 if (part_shift)
809 lo->lo_flags |= LO_FLAGS_PARTSCAN;
810 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100811 ioctl_by_bdev(bdev, BLKRRPART, 0);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700812
813 /* Grab the block_device to prevent its destruction after we
814 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
815 */
816 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818
819 out_putf:
820 fput(file);
821 out:
822 /* This is safe: open() is still holding a reference. */
823 module_put(THIS_MODULE);
824 return error;
825}
826
827static int
828loop_release_xfer(struct loop_device *lo)
829{
830 int err = 0;
831 struct loop_func_table *xfer = lo->lo_encryption;
832
833 if (xfer) {
834 if (xfer->release)
835 err = xfer->release(lo);
836 lo->transfer = NULL;
837 lo->lo_encryption = NULL;
838 module_put(xfer->owner);
839 }
840 return err;
841}
842
843static int
844loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
845 const struct loop_info64 *i)
846{
847 int err = 0;
848
849 if (xfer) {
850 struct module *owner = xfer->owner;
851
852 if (!try_module_get(owner))
853 return -EINVAL;
854 if (xfer->init)
855 err = xfer->init(lo, i);
856 if (err)
857 module_put(owner);
858 else
859 lo->lo_encryption = xfer;
860 }
861 return err;
862}
863
Ayan George4c823cc2011-09-21 10:02:13 +0200864static int loop_clr_fd(struct loop_device *lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -0400867 gfp_t gfp = lo->old_gfp_mask;
Ayan George4c823cc2011-09-21 10:02:13 +0200868 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (lo->lo_state != Lo_bound)
871 return -ENXIO;
872
Dave Chinnera1ecac32012-09-28 10:42:23 +0200873 /*
874 * If we've explicitly asked to tear down the loop device,
875 * and it has an elevated reference count, set it for auto-teardown when
876 * the last reference goes away. This stops $!~#$@ udev from
877 * preventing teardown because it decided that it needs to run blkid on
878 * the loopback device whenever they appear. xfstests is notorious for
879 * failing tests because blkid via udev races with a losetup
880 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
881 * command to fail with EBUSY.
882 */
883 if (lo->lo_refcnt > 1) {
884 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
885 mutex_unlock(&lo->lo_ctl_mutex);
886 return 0;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (filp == NULL)
890 return -EINVAL;
891
892 spin_lock_irq(&lo->lo_lock);
893 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +0200895 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 loop_release_xfer(lo);
898 lo->transfer = NULL;
899 lo->ioctl = NULL;
900 lo->lo_device = NULL;
901 lo->lo_encryption = NULL;
902 lo->lo_offset = 0;
903 lo->lo_sizelimit = 0;
904 lo->lo_encrypt_key_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
906 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
907 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700908 if (bdev) {
909 bdput(bdev);
Al Virobb214882008-03-02 09:29:48 -0500910 invalidate_bdev(bdev);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700911 }
Ken Chen73285082007-05-08 00:28:20 -0700912 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -0600913 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200914 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -0500915 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200916 /* let user-space know about this change */
917 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 mapping_set_gfp_mask(filp->f_mapping, gfp);
920 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* This is safe: open() is still holding a reference. */
922 module_put(THIS_MODULE);
Jens Axboec2fccc12013-04-08 10:12:11 +0200923 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
924 ioctl_by_bdev(bdev, BLKRRPART, 0);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200925 lo->lo_flags = 0;
926 if (!part_shift)
927 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +0100928 mutex_unlock(&lo->lo_ctl_mutex);
929 /*
930 * Need not hold lo_ctl_mutex to fput backing file.
931 * Calling fput holding lo_ctl_mutex triggers a circular
932 * lock dependency possibility warning as fput can take
933 * bd_mutex which is usually taken before lo_ctl_mutex.
934 */
935 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
939static int
940loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
941{
942 int err;
943 struct loop_func_table *xfer;
Eric W. Biedermane4849732012-02-11 11:23:51 -0800944 kuid_t uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
David Howellsb0fafa82008-11-14 10:38:41 +1100946 if (lo->lo_encrypt_key_size &&
Eric W. Biedermane4849732012-02-11 11:23:51 -0800947 !uid_eq(lo->lo_key_owner, uid) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 !capable(CAP_SYS_ADMIN))
949 return -EPERM;
950 if (lo->lo_state != Lo_bound)
951 return -ENXIO;
952 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
953 return -EINVAL;
954
955 err = loop_release_xfer(lo);
956 if (err)
957 return err;
958
959 if (info->lo_encrypt_type) {
960 unsigned int type = info->lo_encrypt_type;
961
962 if (type >= MAX_LO_CRYPT)
963 return -EINVAL;
964 xfer = xfer_funcs[type];
965 if (xfer == NULL)
966 return -EINVAL;
967 } else
968 xfer = NULL;
969
970 err = loop_init_xfer(lo, xfer, info);
971 if (err)
972 return err;
973
974 if (lo->lo_offset != info->lo_offset ||
Guo Chao7b0576a2013-02-21 15:16:47 -0800975 lo->lo_sizelimit != info->lo_sizelimit)
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100976 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -EFBIG;
Guo Chao541c7422013-02-21 15:16:46 -0800978
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200979 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
982 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
983 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
984 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
985
986 if (!xfer)
987 xfer = &none_funcs;
988 lo->transfer = xfer->transfer;
989 lo->ioctl = xfer->ioctl;
990
David Woodhouse96c58652008-02-06 01:36:27 -0800991 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
992 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
993 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
994
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200995 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
996 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
997 lo->lo_flags |= LO_FLAGS_PARTSCAN;
998 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
999 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1003 lo->lo_init[0] = info->lo_init[0];
1004 lo->lo_init[1] = info->lo_init[1];
1005 if (info->lo_encrypt_key_size) {
1006 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1007 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001008 lo->lo_key_owner = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 return 0;
1012}
1013
1014static int
1015loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1016{
1017 struct file *file = lo->lo_backing_file;
1018 struct kstat stat;
1019 int error;
1020
1021 if (lo->lo_state != Lo_bound)
1022 return -ENXIO;
Al Viro3dadecc2013-01-24 02:18:08 -05001023 error = vfs_getattr(&file->f_path, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (error)
1025 return error;
1026 memset(info, 0, sizeof(*info));
1027 info->lo_number = lo->lo_number;
1028 info->lo_device = huge_encode_dev(stat.dev);
1029 info->lo_inode = stat.ino;
1030 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1031 info->lo_offset = lo->lo_offset;
1032 info->lo_sizelimit = lo->lo_sizelimit;
1033 info->lo_flags = lo->lo_flags;
1034 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1035 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1036 info->lo_encrypt_type =
1037 lo->lo_encryption ? lo->lo_encryption->number : 0;
1038 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1039 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1040 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1041 lo->lo_encrypt_key_size);
1042 }
1043 return 0;
1044}
1045
1046static void
1047loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1048{
1049 memset(info64, 0, sizeof(*info64));
1050 info64->lo_number = info->lo_number;
1051 info64->lo_device = info->lo_device;
1052 info64->lo_inode = info->lo_inode;
1053 info64->lo_rdevice = info->lo_rdevice;
1054 info64->lo_offset = info->lo_offset;
1055 info64->lo_sizelimit = 0;
1056 info64->lo_encrypt_type = info->lo_encrypt_type;
1057 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1058 info64->lo_flags = info->lo_flags;
1059 info64->lo_init[0] = info->lo_init[0];
1060 info64->lo_init[1] = info->lo_init[1];
1061 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1062 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1063 else
1064 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1065 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1066}
1067
1068static int
1069loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1070{
1071 memset(info, 0, sizeof(*info));
1072 info->lo_number = info64->lo_number;
1073 info->lo_device = info64->lo_device;
1074 info->lo_inode = info64->lo_inode;
1075 info->lo_rdevice = info64->lo_rdevice;
1076 info->lo_offset = info64->lo_offset;
1077 info->lo_encrypt_type = info64->lo_encrypt_type;
1078 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1079 info->lo_flags = info64->lo_flags;
1080 info->lo_init[0] = info64->lo_init[0];
1081 info->lo_init[1] = info64->lo_init[1];
1082 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1083 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1084 else
1085 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1086 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1087
1088 /* error in case values were truncated */
1089 if (info->lo_device != info64->lo_device ||
1090 info->lo_rdevice != info64->lo_rdevice ||
1091 info->lo_inode != info64->lo_inode ||
1092 info->lo_offset != info64->lo_offset)
1093 return -EOVERFLOW;
1094
1095 return 0;
1096}
1097
1098static int
1099loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1100{
1101 struct loop_info info;
1102 struct loop_info64 info64;
1103
1104 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1105 return -EFAULT;
1106 loop_info64_from_old(&info, &info64);
1107 return loop_set_status(lo, &info64);
1108}
1109
1110static int
1111loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1112{
1113 struct loop_info64 info64;
1114
1115 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1116 return -EFAULT;
1117 return loop_set_status(lo, &info64);
1118}
1119
1120static int
1121loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1122 struct loop_info info;
1123 struct loop_info64 info64;
1124 int err = 0;
1125
1126 if (!arg)
1127 err = -EINVAL;
1128 if (!err)
1129 err = loop_get_status(lo, &info64);
1130 if (!err)
1131 err = loop_info64_to_old(&info64, &info);
1132 if (!err && copy_to_user(arg, &info, sizeof(info)))
1133 err = -EFAULT;
1134
1135 return err;
1136}
1137
1138static int
1139loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1140 struct loop_info64 info64;
1141 int err = 0;
1142
1143 if (!arg)
1144 err = -EINVAL;
1145 if (!err)
1146 err = loop_get_status(lo, &info64);
1147 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1148 err = -EFAULT;
1149
1150 return err;
1151}
1152
J. R. Okajima53d66602009-03-31 15:23:43 -07001153static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1154{
J. R. Okajima53d66602009-03-31 15:23:43 -07001155 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001156 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001157
Guo Chao7b0576a2013-02-21 15:16:47 -08001158 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
J. R. Okajima53d66602009-03-31 15:23:43 -07001159}
1160
Al Virobb214882008-03-02 09:29:48 -05001161static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 unsigned int cmd, unsigned long arg)
1163{
Al Virobb214882008-03-02 09:29:48 -05001164 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int err;
1166
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001167 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 switch (cmd) {
1169 case LOOP_SET_FD:
Al Virobb214882008-03-02 09:29:48 -05001170 err = loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001173 err = loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 case LOOP_CLR_FD:
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001176 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
Ayan George4c823cc2011-09-21 10:02:13 +02001177 err = loop_clr_fd(lo);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001178 if (!err)
1179 goto out_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001182 err = -EPERM;
1183 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1184 err = loop_set_status_old(lo,
1185 (struct loop_info __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 break;
1187 case LOOP_GET_STATUS:
1188 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1189 break;
1190 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001191 err = -EPERM;
1192 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1193 err = loop_set_status64(lo,
1194 (struct loop_info64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196 case LOOP_GET_STATUS64:
1197 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1198 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001199 case LOOP_SET_CAPACITY:
1200 err = -EPERM;
1201 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1202 err = loop_set_capacity(lo, bdev);
1203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 default:
1205 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1206 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001207 mutex_unlock(&lo->lo_ctl_mutex);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001208
1209out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return err;
1211}
1212
David Howells863d5b822006-08-29 19:06:14 +01001213#ifdef CONFIG_COMPAT
1214struct compat_loop_info {
1215 compat_int_t lo_number; /* ioctl r/o */
1216 compat_dev_t lo_device; /* ioctl r/o */
1217 compat_ulong_t lo_inode; /* ioctl r/o */
1218 compat_dev_t lo_rdevice; /* ioctl r/o */
1219 compat_int_t lo_offset;
1220 compat_int_t lo_encrypt_type;
1221 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1222 compat_int_t lo_flags; /* ioctl r/o */
1223 char lo_name[LO_NAME_SIZE];
1224 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1225 compat_ulong_t lo_init[2];
1226 char reserved[4];
1227};
1228
1229/*
1230 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1231 * - noinlined to reduce stack space usage in main part of driver
1232 */
1233static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001234loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001235 struct loop_info64 *info64)
1236{
1237 struct compat_loop_info info;
1238
1239 if (copy_from_user(&info, arg, sizeof(info)))
1240 return -EFAULT;
1241
1242 memset(info64, 0, sizeof(*info64));
1243 info64->lo_number = info.lo_number;
1244 info64->lo_device = info.lo_device;
1245 info64->lo_inode = info.lo_inode;
1246 info64->lo_rdevice = info.lo_rdevice;
1247 info64->lo_offset = info.lo_offset;
1248 info64->lo_sizelimit = 0;
1249 info64->lo_encrypt_type = info.lo_encrypt_type;
1250 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1251 info64->lo_flags = info.lo_flags;
1252 info64->lo_init[0] = info.lo_init[0];
1253 info64->lo_init[1] = info.lo_init[1];
1254 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1255 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1256 else
1257 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1258 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1259 return 0;
1260}
1261
1262/*
1263 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1264 * - noinlined to reduce stack space usage in main part of driver
1265 */
1266static noinline int
1267loop_info64_to_compat(const struct loop_info64 *info64,
1268 struct compat_loop_info __user *arg)
1269{
1270 struct compat_loop_info info;
1271
1272 memset(&info, 0, sizeof(info));
1273 info.lo_number = info64->lo_number;
1274 info.lo_device = info64->lo_device;
1275 info.lo_inode = info64->lo_inode;
1276 info.lo_rdevice = info64->lo_rdevice;
1277 info.lo_offset = info64->lo_offset;
1278 info.lo_encrypt_type = info64->lo_encrypt_type;
1279 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1280 info.lo_flags = info64->lo_flags;
1281 info.lo_init[0] = info64->lo_init[0];
1282 info.lo_init[1] = info64->lo_init[1];
1283 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1284 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1285 else
1286 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1287 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1288
1289 /* error in case values were truncated */
1290 if (info.lo_device != info64->lo_device ||
1291 info.lo_rdevice != info64->lo_rdevice ||
1292 info.lo_inode != info64->lo_inode ||
1293 info.lo_offset != info64->lo_offset ||
1294 info.lo_init[0] != info64->lo_init[0] ||
1295 info.lo_init[1] != info64->lo_init[1])
1296 return -EOVERFLOW;
1297
1298 if (copy_to_user(arg, &info, sizeof(info)))
1299 return -EFAULT;
1300 return 0;
1301}
1302
1303static int
1304loop_set_status_compat(struct loop_device *lo,
1305 const struct compat_loop_info __user *arg)
1306{
1307 struct loop_info64 info64;
1308 int ret;
1309
1310 ret = loop_info64_from_compat(arg, &info64);
1311 if (ret < 0)
1312 return ret;
1313 return loop_set_status(lo, &info64);
1314}
1315
1316static int
1317loop_get_status_compat(struct loop_device *lo,
1318 struct compat_loop_info __user *arg)
1319{
1320 struct loop_info64 info64;
1321 int err = 0;
1322
1323 if (!arg)
1324 err = -EINVAL;
1325 if (!err)
1326 err = loop_get_status(lo, &info64);
1327 if (!err)
1328 err = loop_info64_to_compat(&info64, arg);
1329 return err;
1330}
1331
Al Virobb214882008-03-02 09:29:48 -05001332static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1333 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001334{
Al Virobb214882008-03-02 09:29:48 -05001335 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001336 int err;
1337
David Howells863d5b822006-08-29 19:06:14 +01001338 switch(cmd) {
1339 case LOOP_SET_STATUS:
1340 mutex_lock(&lo->lo_ctl_mutex);
1341 err = loop_set_status_compat(
1342 lo, (const struct compat_loop_info __user *) arg);
1343 mutex_unlock(&lo->lo_ctl_mutex);
1344 break;
1345 case LOOP_GET_STATUS:
1346 mutex_lock(&lo->lo_ctl_mutex);
1347 err = loop_get_status_compat(
1348 lo, (struct compat_loop_info __user *) arg);
1349 mutex_unlock(&lo->lo_ctl_mutex);
1350 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001351 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001352 case LOOP_CLR_FD:
1353 case LOOP_GET_STATUS64:
1354 case LOOP_SET_STATUS64:
1355 arg = (unsigned long) compat_ptr(arg);
1356 case LOOP_SET_FD:
1357 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001358 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001359 break;
1360 default:
1361 err = -ENOIOCTLCMD;
1362 break;
1363 }
David Howells863d5b822006-08-29 19:06:14 +01001364 return err;
1365}
1366#endif
1367
Al Virobb214882008-03-02 09:29:48 -05001368static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Kay Sievers770fe302011-07-31 22:08:04 +02001370 struct loop_device *lo;
1371 int err = 0;
1372
1373 mutex_lock(&loop_index_mutex);
1374 lo = bdev->bd_disk->private_data;
1375 if (!lo) {
1376 err = -ENXIO;
1377 goto out;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Ingo Molnarf85221d2006-03-23 03:00:38 -08001380 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001382 mutex_unlock(&lo->lo_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02001383out:
1384 mutex_unlock(&loop_index_mutex);
1385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Al Virodb2a1442013-05-05 21:52:57 -04001388static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Al Virobb214882008-03-02 09:29:48 -05001390 struct loop_device *lo = disk->private_data;
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001391 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Ingo Molnarf85221d2006-03-23 03:00:38 -08001393 mutex_lock(&lo->lo_ctl_mutex);
David Woodhouse96c58652008-02-06 01:36:27 -08001394
Milan Broz14f27932008-12-12 14:48:27 +01001395 if (--lo->lo_refcnt)
1396 goto out;
1397
1398 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1399 /*
1400 * In autoclear mode, stop the loop thread
1401 * and remove configuration after last close.
1402 */
Ayan George4c823cc2011-09-21 10:02:13 +02001403 err = loop_clr_fd(lo);
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001404 if (!err)
Al Virodb2a1442013-05-05 21:52:57 -04001405 return;
Milan Broz14f27932008-12-12 14:48:27 +01001406 } else {
1407 /*
1408 * Otherwise keep thread (if running) and config,
1409 * but flush possible ongoing bios in thread.
1410 */
1411 loop_flush(lo);
1412 }
David Woodhouse96c58652008-02-06 01:36:27 -08001413
Milan Broz14f27932008-12-12 14:48:27 +01001414out:
Ingo Molnarf85221d2006-03-23 03:00:38 -08001415 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001418static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001420 .open = lo_open,
1421 .release = lo_release,
1422 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001423#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001424 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426};
1427
1428/*
1429 * And now the modules code and kernel interface.
1430 */
Ken Chen73285082007-05-08 00:28:20 -07001431static int max_loop;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001432module_param(max_loop, int, S_IRUGO);
Ken Chena47653f2007-06-08 13:46:44 -07001433MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Namhyung Kimac04fee2011-05-27 07:59:25 +02001434module_param(max_part, int, S_IRUGO);
Laurent Vivier476a4812008-03-26 12:11:53 +01001435MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436MODULE_LICENSE("GPL");
1437MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1438
1439int loop_register_transfer(struct loop_func_table *funcs)
1440{
1441 unsigned int n = funcs->number;
1442
1443 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1444 return -EINVAL;
1445 xfer_funcs[n] = funcs;
1446 return 0;
1447}
1448
Kay Sievers34dd82a2011-07-31 22:08:04 +02001449static int unregister_transfer_cb(int id, void *ptr, void *data)
1450{
1451 struct loop_device *lo = ptr;
1452 struct loop_func_table *xfer = data;
1453
1454 mutex_lock(&lo->lo_ctl_mutex);
1455 if (lo->lo_encryption == xfer)
1456 loop_release_xfer(lo);
1457 mutex_unlock(&lo->lo_ctl_mutex);
1458 return 0;
1459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461int loop_unregister_transfer(int number)
1462{
1463 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct loop_func_table *xfer;
1465
1466 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1467 return -EINVAL;
1468
1469 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001470 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472}
1473
1474EXPORT_SYMBOL(loop_register_transfer);
1475EXPORT_SYMBOL(loop_unregister_transfer);
1476
Ming Leib5dd2f62014-12-31 13:22:57 +00001477static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1478 const struct blk_mq_queue_data *bd)
1479{
1480 struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1481
1482 blk_mq_start_request(bd->rq);
1483
1484 if (cmd->rq->cmd_flags & REQ_WRITE) {
1485 struct loop_device *lo = cmd->rq->q->queuedata;
1486 bool need_sched = true;
1487
1488 spin_lock_irq(&lo->lo_lock);
1489 if (lo->write_started)
1490 need_sched = false;
1491 else
1492 lo->write_started = true;
1493 list_add_tail(&cmd->list, &lo->write_cmd_head);
1494 spin_unlock_irq(&lo->lo_lock);
1495
1496 if (need_sched)
1497 queue_work(loop_wq, &lo->write_work);
1498 } else {
1499 queue_work(loop_wq, &cmd->read_work);
1500 }
1501
1502 return BLK_MQ_RQ_QUEUE_OK;
1503}
1504
1505static void loop_handle_cmd(struct loop_cmd *cmd)
1506{
1507 const bool write = cmd->rq->cmd_flags & REQ_WRITE;
1508 struct loop_device *lo = cmd->rq->q->queuedata;
1509 int ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00001510
1511 if (lo->lo_state != Lo_bound)
1512 goto failed;
1513
1514 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
1515 goto failed;
1516
Ming Lei30112012014-12-31 13:22:58 +00001517 ret = do_req_filebacked(lo, cmd->rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001518
1519 failed:
1520 if (ret)
1521 cmd->rq->errors = -EIO;
1522 blk_mq_complete_request(cmd->rq);
1523}
1524
1525static void loop_queue_write_work(struct work_struct *work)
1526{
1527 struct loop_device *lo =
1528 container_of(work, struct loop_device, write_work);
1529 LIST_HEAD(cmd_list);
1530
1531 spin_lock_irq(&lo->lo_lock);
1532 repeat:
1533 list_splice_init(&lo->write_cmd_head, &cmd_list);
1534 spin_unlock_irq(&lo->lo_lock);
1535
1536 while (!list_empty(&cmd_list)) {
1537 struct loop_cmd *cmd = list_first_entry(&cmd_list,
1538 struct loop_cmd, list);
1539 list_del_init(&cmd->list);
1540 loop_handle_cmd(cmd);
1541 }
1542
1543 spin_lock_irq(&lo->lo_lock);
1544 if (!list_empty(&lo->write_cmd_head))
1545 goto repeat;
1546 lo->write_started = false;
1547 spin_unlock_irq(&lo->lo_lock);
1548}
1549
1550static void loop_queue_read_work(struct work_struct *work)
1551{
1552 struct loop_cmd *cmd =
1553 container_of(work, struct loop_cmd, read_work);
1554
1555 loop_handle_cmd(cmd);
1556}
1557
1558static int loop_init_request(void *data, struct request *rq,
1559 unsigned int hctx_idx, unsigned int request_idx,
1560 unsigned int numa_node)
1561{
1562 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1563
1564 cmd->rq = rq;
1565 INIT_WORK(&cmd->read_work, loop_queue_read_work);
1566
1567 return 0;
1568}
1569
1570static struct blk_mq_ops loop_mq_ops = {
1571 .queue_rq = loop_queue_rq,
1572 .map_queue = blk_mq_map_queue,
1573 .init_request = loop_init_request,
1574};
1575
Kay Sievers34dd82a2011-07-31 22:08:04 +02001576static int loop_add(struct loop_device **l, int i)
Ken Chen73285082007-05-08 00:28:20 -07001577{
1578 struct loop_device *lo;
1579 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001580 int err;
Ken Chen73285082007-05-08 00:28:20 -07001581
Silva Paulo68d740d2012-07-14 15:39:58 -07001582 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001583 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07001584 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07001585 goto out;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001586
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001587 lo->lo_state = Lo_unbound;
1588
Tejun Heoc718aa62013-02-27 17:03:58 -08001589 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001590 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08001591 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1592 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02001593 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001594 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08001595 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001596 }
1597 if (err < 0)
1598 goto out_free_dev;
Tejun Heoc718aa62013-02-27 17:03:58 -08001599 i = err;
Ken Chen73285082007-05-08 00:28:20 -07001600
Wei Yongjun183cfb52013-03-22 08:59:19 -06001601 err = -ENOMEM;
Ming Leib5dd2f62014-12-31 13:22:57 +00001602 lo->tag_set.ops = &loop_mq_ops;
1603 lo->tag_set.nr_hw_queues = 1;
1604 lo->tag_set.queue_depth = 128;
1605 lo->tag_set.numa_node = NUMA_NO_NODE;
1606 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
1607 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
1608 lo->tag_set.driver_data = lo;
1609
1610 err = blk_mq_alloc_tag_set(&lo->tag_set);
1611 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001612 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07001613
Ming Leib5dd2f62014-12-31 13:22:57 +00001614 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
1615 if (IS_ERR_OR_NULL(lo->lo_queue)) {
1616 err = PTR_ERR(lo->lo_queue);
1617 goto out_cleanup_tags;
1618 }
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001619 lo->lo_queue->queuedata = lo;
1620
Ming Leib5dd2f62014-12-31 13:22:57 +00001621 INIT_LIST_HEAD(&lo->write_cmd_head);
1622 INIT_WORK(&lo->write_work, loop_queue_write_work);
1623
Laurent Vivier476a4812008-03-26 12:11:53 +01001624 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07001625 if (!disk)
1626 goto out_free_queue;
1627
Kay Sieverse03c8dd2011-08-23 20:12:04 +02001628 /*
1629 * Disable partition scanning by default. The in-kernel partition
1630 * scanning can be requested individually per-device during its
1631 * setup. Userspace can always add and remove partitions from all
1632 * devices. The needed partition minors are allocated from the
1633 * extended minor space, the main loop device numbers will continue
1634 * to match the loop minors, regardless of the number of partitions
1635 * used.
1636 *
1637 * If max_part is given, partition scanning is globally enabled for
1638 * all loop devices. The minors for the main loop devices will be
1639 * multiples of max_part.
1640 *
1641 * Note: Global-for-all-devices, set-only-at-init, read-only module
1642 * parameteters like 'max_loop' and 'max_part' make things needlessly
1643 * complicated, are too static, inflexible and may surprise
1644 * userspace tools. Parameters like this in general should be avoided.
1645 */
1646 if (!part_shift)
1647 disk->flags |= GENHD_FL_NO_PART_SCAN;
1648 disk->flags |= GENHD_FL_EXT_DEVT;
Ken Chen73285082007-05-08 00:28:20 -07001649 mutex_init(&lo->lo_ctl_mutex);
1650 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07001651 spin_lock_init(&lo->lo_lock);
1652 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01001653 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07001654 disk->fops = &lo_fops;
1655 disk->private_data = lo;
1656 disk->queue = lo->lo_queue;
1657 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001658 add_disk(disk);
1659 *l = lo;
1660 return lo->lo_number;
Ken Chen73285082007-05-08 00:28:20 -07001661
1662out_free_queue:
1663 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001664out_cleanup_tags:
1665 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001666out_free_idr:
1667 idr_remove(&loop_index_idr, i);
Ken Chen73285082007-05-08 00:28:20 -07001668out_free_dev:
1669 kfree(lo);
1670out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001671 return err;
Ken Chen73285082007-05-08 00:28:20 -07001672}
1673
Kay Sievers34dd82a2011-07-31 22:08:04 +02001674static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001675{
Kay Sievers34dd82a2011-07-31 22:08:04 +02001676 del_gendisk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001677 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001678 blk_mq_free_tag_set(&lo->tag_set);
Ken Chen73285082007-05-08 00:28:20 -07001679 put_disk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001680 kfree(lo);
1681}
1682
Kay Sievers770fe302011-07-31 22:08:04 +02001683static int find_free_cb(int id, void *ptr, void *data)
Ken Chena47653f2007-06-08 13:46:44 -07001684{
Kay Sievers770fe302011-07-31 22:08:04 +02001685 struct loop_device *lo = ptr;
1686 struct loop_device **l = data;
Ken Chena47653f2007-06-08 13:46:44 -07001687
Kay Sievers770fe302011-07-31 22:08:04 +02001688 if (lo->lo_state == Lo_unbound) {
1689 *l = lo;
1690 return 1;
Ken Chena47653f2007-06-08 13:46:44 -07001691 }
Kay Sievers770fe302011-07-31 22:08:04 +02001692 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07001693}
1694
Kay Sievers34dd82a2011-07-31 22:08:04 +02001695static int loop_lookup(struct loop_device **l, int i)
Ken Chena47653f2007-06-08 13:46:44 -07001696{
Ken Chena47653f2007-06-08 13:46:44 -07001697 struct loop_device *lo;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001698 int ret = -ENODEV;
Ken Chena47653f2007-06-08 13:46:44 -07001699
Kay Sievers770fe302011-07-31 22:08:04 +02001700 if (i < 0) {
1701 int err;
1702
1703 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1704 if (err == 1) {
1705 *l = lo;
1706 ret = lo->lo_number;
1707 }
1708 goto out;
1709 }
1710
1711 /* lookup and return a specific i */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001712 lo = idr_find(&loop_index_idr, i);
Ken Chena47653f2007-06-08 13:46:44 -07001713 if (lo) {
Kay Sievers34dd82a2011-07-31 22:08:04 +02001714 *l = lo;
1715 ret = lo->lo_number;
Ken Chena47653f2007-06-08 13:46:44 -07001716 }
Kay Sievers770fe302011-07-31 22:08:04 +02001717out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001718 return ret;
Ken Chena47653f2007-06-08 13:46:44 -07001719}
1720
Ken Chen73285082007-05-08 00:28:20 -07001721static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1722{
Al Viro705962c2007-05-13 05:52:32 -04001723 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001724 struct kobject *kobj;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001725 int err;
Ken Chen73285082007-05-08 00:28:20 -07001726
Kay Sievers34dd82a2011-07-31 22:08:04 +02001727 mutex_lock(&loop_index_mutex);
1728 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1729 if (err < 0)
1730 err = loop_add(&lo, MINOR(dev) >> part_shift);
1731 if (err < 0)
Mikulas Patockaa207f592013-10-14 12:13:24 -04001732 kobj = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001733 else
1734 kobj = get_disk(lo->lo_disk);
1735 mutex_unlock(&loop_index_mutex);
Ken Chen73285082007-05-08 00:28:20 -07001736
1737 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001738 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001739}
1740
Kay Sievers770fe302011-07-31 22:08:04 +02001741static long loop_control_ioctl(struct file *file, unsigned int cmd,
1742 unsigned long parm)
1743{
1744 struct loop_device *lo;
1745 int ret = -ENOSYS;
1746
1747 mutex_lock(&loop_index_mutex);
1748 switch (cmd) {
1749 case LOOP_CTL_ADD:
1750 ret = loop_lookup(&lo, parm);
1751 if (ret >= 0) {
1752 ret = -EEXIST;
1753 break;
1754 }
1755 ret = loop_add(&lo, parm);
1756 break;
1757 case LOOP_CTL_REMOVE:
1758 ret = loop_lookup(&lo, parm);
1759 if (ret < 0)
1760 break;
1761 mutex_lock(&lo->lo_ctl_mutex);
1762 if (lo->lo_state != Lo_unbound) {
1763 ret = -EBUSY;
1764 mutex_unlock(&lo->lo_ctl_mutex);
1765 break;
1766 }
1767 if (lo->lo_refcnt > 0) {
1768 ret = -EBUSY;
1769 mutex_unlock(&lo->lo_ctl_mutex);
1770 break;
1771 }
1772 lo->lo_disk->private_data = NULL;
1773 mutex_unlock(&lo->lo_ctl_mutex);
1774 idr_remove(&loop_index_idr, lo->lo_number);
1775 loop_remove(lo);
1776 break;
1777 case LOOP_CTL_GET_FREE:
1778 ret = loop_lookup(&lo, -1);
1779 if (ret >= 0)
1780 break;
1781 ret = loop_add(&lo, -1);
1782 }
1783 mutex_unlock(&loop_index_mutex);
1784
1785 return ret;
1786}
1787
1788static const struct file_operations loop_ctl_fops = {
1789 .open = nonseekable_open,
1790 .unlocked_ioctl = loop_control_ioctl,
1791 .compat_ioctl = loop_control_ioctl,
1792 .owner = THIS_MODULE,
1793 .llseek = noop_llseek,
1794};
1795
1796static struct miscdevice loop_misc = {
1797 .minor = LOOP_CTRL_MINOR,
1798 .name = "loop-control",
1799 .fops = &loop_ctl_fops,
1800};
1801
1802MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1803MODULE_ALIAS("devname:loop-control");
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static int __init loop_init(void)
1806{
Ken Chena47653f2007-06-08 13:46:44 -07001807 int i, nr;
1808 unsigned long range;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001809 struct loop_device *lo;
Kay Sievers770fe302011-07-31 22:08:04 +02001810 int err;
Ken Chena47653f2007-06-08 13:46:44 -07001811
Kay Sievers770fe302011-07-31 22:08:04 +02001812 err = misc_register(&loop_misc);
1813 if (err < 0)
1814 return err;
Laurent Vivier476a4812008-03-26 12:11:53 +01001815
1816 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001817 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01001818 part_shift = fls(max_part);
1819
Namhyung Kimac04fee2011-05-27 07:59:25 +02001820 /*
1821 * Adjust max_part according to part_shift as it is exported
1822 * to user space so that user can decide correct minor number
1823 * if [s]he want to create more devices.
1824 *
1825 * Note that -1 is required because partition 0 is reserved
1826 * for the whole disk.
1827 */
1828 max_part = (1UL << part_shift) - 1;
1829 }
1830
Guo Chaob1a66502013-02-21 15:16:49 -08001831 if ((1UL << part_shift) > DISK_MAX_PARTS) {
1832 err = -EINVAL;
1833 goto misc_out;
1834 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02001835
Guo Chaob1a66502013-02-21 15:16:49 -08001836 if (max_loop > 1UL << (MINORBITS - part_shift)) {
1837 err = -EINVAL;
1838 goto misc_out;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Kay Sieversd134b002011-07-31 22:08:04 +02001841 /*
1842 * If max_loop is specified, create that many devices upfront.
1843 * This also becomes a hard limit. If max_loop is not specified,
1844 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1845 * init time. Loop devices can be requested on-demand with the
1846 * /dev/loop-control interface, or be instantiated by accessing
1847 * a 'dead' device node.
1848 */
Ken Chen73285082007-05-08 00:28:20 -07001849 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001850 nr = max_loop;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001851 range = max_loop << part_shift;
Ken Chena47653f2007-06-08 13:46:44 -07001852 } else {
Kay Sieversd134b002011-07-31 22:08:04 +02001853 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001854 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Ken Chena47653f2007-06-08 13:46:44 -07001856
Guo Chaob1a66502013-02-21 15:16:49 -08001857 if (register_blkdev(LOOP_MAJOR, "loop")) {
1858 err = -EIO;
1859 goto misc_out;
1860 }
Ken Chena47653f2007-06-08 13:46:44 -07001861
Ming Leib5dd2f62014-12-31 13:22:57 +00001862 loop_wq = alloc_workqueue("kloopd",
1863 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0);
1864 if (!loop_wq) {
1865 err = -ENOMEM;
1866 goto misc_out;
1867 }
1868
Ken Chena47653f2007-06-08 13:46:44 -07001869 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1870 THIS_MODULE, loop_probe, NULL, NULL);
1871
Kay Sieversd134b002011-07-31 22:08:04 +02001872 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001873 mutex_lock(&loop_index_mutex);
1874 for (i = 0; i < nr; i++)
1875 loop_add(&lo, i);
1876 mutex_unlock(&loop_index_mutex);
1877
Ken Chen73285082007-05-08 00:28:20 -07001878 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08001880
1881misc_out:
1882 misc_deregister(&loop_misc);
1883 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001884}
Ken Chena47653f2007-06-08 13:46:44 -07001885
Kay Sievers34dd82a2011-07-31 22:08:04 +02001886static int loop_exit_cb(int id, void *ptr, void *data)
1887{
1888 struct loop_device *lo = ptr;
Ken Chena47653f2007-06-08 13:46:44 -07001889
Kay Sievers34dd82a2011-07-31 22:08:04 +02001890 loop_remove(lo);
1891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Ken Chen73285082007-05-08 00:28:20 -07001894static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Ken Chena47653f2007-06-08 13:46:44 -07001896 unsigned long range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Namhyung Kima1c15c52011-05-24 16:48:55 +02001898 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
Ken Chena47653f2007-06-08 13:46:44 -07001899
Kay Sievers34dd82a2011-07-31 22:08:04 +02001900 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001901 idr_destroy(&loop_index_idr);
Ken Chen73285082007-05-08 00:28:20 -07001902
Ken Chena47653f2007-06-08 13:46:44 -07001903 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001904 unregister_blkdev(LOOP_MAJOR, "loop");
Kay Sievers770fe302011-07-31 22:08:04 +02001905
Ming Leib5dd2f62014-12-31 13:22:57 +00001906 destroy_workqueue(loop_wq);
1907
Kay Sievers770fe302011-07-31 22:08:04 +02001908 misc_deregister(&loop_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911module_init(loop_init);
1912module_exit(loop_exit);
1913
1914#ifndef MODULE
1915static int __init max_loop_setup(char *str)
1916{
1917 max_loop = simple_strtol(str, NULL, 0);
1918 return 1;
1919}
1920
1921__setup("max_loop=", max_loop_setup);
1922#endif