blob: d1f168b73634321dadb6c57571c0cd8a9de8304b [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 Viro83a87612013-05-12 10:14:07 -040078#include "loop.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#include <asm/uaccess.h>
81
Kay Sievers34dd82a2011-07-31 22:08:04 +020082static DEFINE_IDR(loop_index_idr);
83static DEFINE_MUTEX(loop_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Laurent Vivier476a4812008-03-26 12:11:53 +010085static int max_part;
86static int part_shift;
87
Ming Leib5dd2f62014-12-31 13:22:57 +000088static struct workqueue_struct *loop_wq;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Transfer functions
92 */
93static int transfer_none(struct loop_device *lo, int cmd,
94 struct page *raw_page, unsigned raw_off,
95 struct page *loop_page, unsigned loop_off,
96 int size, sector_t real_block)
97{
Cong Wangcfd80052011-11-25 23:14:18 +080098 char *raw_buf = kmap_atomic(raw_page) + raw_off;
99 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (cmd == READ)
102 memcpy(loop_buf, raw_buf, size);
103 else
104 memcpy(raw_buf, loop_buf, size);
105
Cong Wangcfd80052011-11-25 23:14:18 +0800106 kunmap_atomic(loop_buf);
107 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 cond_resched();
109 return 0;
110}
111
112static int transfer_xor(struct loop_device *lo, int cmd,
113 struct page *raw_page, unsigned raw_off,
114 struct page *loop_page, unsigned loop_off,
115 int size, sector_t real_block)
116{
Cong Wangcfd80052011-11-25 23:14:18 +0800117 char *raw_buf = kmap_atomic(raw_page) + raw_off;
118 char *loop_buf = kmap_atomic(loop_page) + loop_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char *in, *out, *key;
120 int i, keysize;
121
122 if (cmd == READ) {
123 in = raw_buf;
124 out = loop_buf;
125 } else {
126 in = loop_buf;
127 out = raw_buf;
128 }
129
130 key = lo->lo_encrypt_key;
131 keysize = lo->lo_encrypt_key_size;
132 for (i = 0; i < size; i++)
133 *out++ = *in++ ^ key[(i & 511) % keysize];
134
Cong Wangcfd80052011-11-25 23:14:18 +0800135 kunmap_atomic(loop_buf);
136 kunmap_atomic(raw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 cond_resched();
138 return 0;
139}
140
141static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
142{
143 if (unlikely(info->lo_encrypt_key_size <= 0))
144 return -EINVAL;
145 return 0;
146}
147
148static struct loop_func_table none_funcs = {
149 .number = LO_CRYPT_NONE,
150 .transfer = transfer_none,
151};
152
153static struct loop_func_table xor_funcs = {
154 .number = LO_CRYPT_XOR,
155 .transfer = transfer_xor,
156 .init = xor_init
157};
158
159/* xfer_funcs[0] is special - its release function is never called */
160static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
161 &none_funcs,
162 &xor_funcs
163};
164
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100165static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Guo Chaob7a1da62013-02-21 15:16:50 -0800167 loff_t loopsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* Compute loopsize in bytes */
Guo Chaob7a1da62013-02-21 15:16:50 -0800170 loopsize = i_size_read(file->f_mapping->host);
171 if (offset > 0)
172 loopsize -= offset;
173 /* offset is beyond i_size, weird but possible */
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100174 if (loopsize < 0)
175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100177 if (sizelimit > 0 && sizelimit < loopsize)
178 loopsize = sizelimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Unfortunately, if we want to do I/O on the device,
181 * the number of 512-byte sectors has to fit into a sector_t.
182 */
183 return loopsize >> 9;
184}
185
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100186static loff_t get_loop_size(struct loop_device *lo, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100188 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
189}
190
191static int
192figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
193{
194 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 sector_t x = (sector_t)size;
Guo Chao7b0576a2013-02-21 15:16:47 -0800196 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (unlikely((loff_t)x != size))
199 return -EFBIG;
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100200 if (lo->lo_offset != offset)
201 lo->lo_offset = offset;
202 if (lo->lo_sizelimit != sizelimit)
203 lo->lo_sizelimit = sizelimit;
Ken Chen73285082007-05-08 00:28:20 -0700204 set_capacity(lo->lo_disk, x);
Guo Chao7b0576a2013-02-21 15:16:47 -0800205 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
206 /* let user-space know about the new size */
207 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static inline int
212lo_do_transfer(struct loop_device *lo, int cmd,
213 struct page *rpage, unsigned roffs,
214 struct page *lpage, unsigned loffs,
215 int size, sector_t rblock)
216{
217 if (unlikely(!lo->transfer))
218 return 0;
219
220 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
221}
222
223/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * __do_lo_send_write - helper for writing data to a loop device
225 *
226 * This helper just factors out common code between do_lo_send_direct_write()
227 * and do_lo_send_write().
228 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800229static int __do_lo_send_write(struct file *file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100230 u8 *buf, const int len, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 ssize_t bw;
233 mm_segment_t old_fs = get_fs();
234
Al Viro03d95eb2013-03-20 13:04:20 -0400235 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 set_fs(get_ds());
237 bw = file->f_op->write(file, buf, len, &pos);
238 set_fs(old_fs);
Al Viro03d95eb2013-03-20 13:04:20 -0400239 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (likely(bw == len))
241 return 0;
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700242 printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 (unsigned long long)pos, len);
244 if (bw >= 0)
245 bw = -EIO;
246 return bw;
247}
248
249/**
250 * do_lo_send_direct_write - helper for writing data to a loop device
251 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200252 * This is the fast, non-transforming version that does not need double
253 * buffering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255static int do_lo_send_direct_write(struct loop_device *lo,
Al Viro511de732007-10-08 12:10:13 -0400256 struct bio_vec *bvec, loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100259 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 bvec->bv_len, pos);
261 kunmap(bvec->bv_page);
262 cond_resched();
263 return bw;
264}
265
266/**
267 * do_lo_send_write - helper for writing data to a loop device
268 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200269 * This is the slow, transforming version that needs to double buffer the
270 * data as it cannot do the transformations in place without having direct
271 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400274 loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
277 bvec->bv_offset, bvec->bv_len, pos >> 9);
278 if (likely(!ret))
279 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100280 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 pos);
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700282 printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
284 if (ret > 0)
285 ret = -EIO;
286 return ret;
287}
288
Ming Lei30112012014-12-31 13:22:58 +0000289static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Al Viro511de732007-10-08 12:10:13 -0400291 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct page *page);
Kent Overstreet79886132013-11-23 17:19:00 -0800293 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000294 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct page *page = NULL;
Kent Overstreet79886132013-11-23 17:19:00 -0800296 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christoph Hellwig456be142011-10-17 12:57:20 +0200298 if (lo->transfer != transfer_none) {
299 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
300 if (unlikely(!page))
301 goto fail;
302 kmap(page);
303 do_lo_send = do_lo_send_write;
304 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 do_lo_send = do_lo_send_direct_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Christoph Hellwig456be142011-10-17 12:57:20 +0200307
Ming Lei30112012014-12-31 13:22:58 +0000308 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800309 ret = do_lo_send(lo, &bvec, pos, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (ret < 0)
311 break;
Kent Overstreet79886132013-11-23 17:19:00 -0800312 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 if (page) {
315 kunmap(page);
316 __free_page(page);
317 }
318out:
319 return ret;
320fail:
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700321 printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 ret = -ENOMEM;
323 goto out;
324}
325
326struct lo_read_data {
327 struct loop_device *lo;
328 struct page *page;
329 unsigned offset;
330 int bsize;
331};
332
333static int
Jens Axboefd582142007-06-12 21:20:37 +0200334lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
335 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Jens Axboefd582142007-06-12 21:20:37 +0200337 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200339 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sector_t IV;
Jens Axboe3603b8e2010-12-20 09:15:19 +0100341 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jens Axboefd582142007-06-12 21:20:37 +0200343 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
344 (buf->offset >> 9);
345 size = sd->len;
346 if (size > p->bsize)
347 size = p->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jens Axboefd582142007-06-12 21:20:37 +0200349 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
Mike Galbraith44bd70c2014-04-08 13:43:52 -0700350 printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 page->index);
Jens Axboefd582142007-06-12 21:20:37 +0200352 size = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 flush_dcache_page(p->page);
356
Jens Axboefd582142007-06-12 21:20:37 +0200357 if (size > 0)
358 p->offset += size;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return size;
361}
362
363static int
Jens Axboefd582142007-06-12 21:20:37 +0200364lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
365{
366 return __splice_from_pipe(pipe, sd, lo_splice_actor);
367}
368
Dave Young306df072012-02-08 22:07:19 +0100369static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370do_lo_receive(struct loop_device *lo,
371 struct bio_vec *bvec, int bsize, loff_t pos)
372{
373 struct lo_read_data cookie;
Jens Axboefd582142007-06-12 21:20:37 +0200374 struct splice_desc sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct file *file;
Dave Young306df072012-02-08 22:07:19 +0100376 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 cookie.lo = lo;
379 cookie.page = bvec->bv_page;
380 cookie.offset = bvec->bv_offset;
381 cookie.bsize = bsize;
Jens Axboefd582142007-06-12 21:20:37 +0200382
383 sd.len = 0;
384 sd.total_len = bvec->bv_len;
385 sd.flags = 0;
386 sd.pos = pos;
387 sd.u.data = &cookie;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 file = lo->lo_backing_file;
Jens Axboefd582142007-06-12 21:20:37 +0200390 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
391
Dave Young306df072012-02-08 22:07:19 +0100392 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static int
Ming Lei30112012014-12-31 13:22:58 +0000396lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Kent Overstreet79886132013-11-23 17:19:00 -0800398 struct bio_vec bvec;
Ming Lei30112012014-12-31 13:22:58 +0000399 struct req_iterator iter;
Dave Young306df072012-02-08 22:07:19 +0100400 ssize_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ming Lei30112012014-12-31 13:22:58 +0000402 rq_for_each_segment(bvec, rq, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800403 s = do_lo_receive(lo, &bvec, bsize, pos);
Dave Young306df072012-02-08 22:07:19 +0100404 if (s < 0)
405 return s;
406
Kent Overstreet79886132013-11-23 17:19:00 -0800407 if (s != bvec.bv_len) {
Ming Lei30112012014-12-31 13:22:58 +0000408 struct bio *bio;
409
410 __rq_for_each_bio(bio, rq)
411 zero_fill_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
Dave Young306df072012-02-08 22:07:19 +0100413 }
Kent Overstreet79886132013-11-23 17:19:00 -0800414 pos += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Dave Young306df072012-02-08 22:07:19 +0100416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Ming Leicf655d92014-12-31 13:22:59 +0000419static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
420{
421 /*
422 * We use punch hole to reclaim the free space used by the
423 * image a.k.a. discard. However we do not support discard if
424 * encryption is enabled, because it may give an attacker
425 * useful information.
426 */
427 struct file *file = lo->lo_backing_file;
428 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
429 int ret;
430
431 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
432 ret = -EOPNOTSUPP;
433 goto out;
434 }
435
436 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
437 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
438 ret = -EIO;
439 out:
440 return ret;
441}
442
443static int lo_req_flush(struct loop_device *lo, struct request *rq)
444{
445 struct file *file = lo->lo_backing_file;
446 int ret = vfs_fsync(file, 0);
447 if (unlikely(ret && ret != -EINVAL))
448 ret = -EIO;
449
450 return ret;
451}
452
Ming Lei30112012014-12-31 13:22:58 +0000453static int do_req_filebacked(struct loop_device *lo, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 loff_t pos;
456 int ret;
457
Ming Lei30112012014-12-31 13:22:58 +0000458 pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100459
Ming Lei30112012014-12-31 13:22:58 +0000460 if (rq->cmd_flags & REQ_WRITE) {
Ming Leicf655d92014-12-31 13:22:59 +0000461 if (rq->cmd_flags & REQ_FLUSH)
462 ret = lo_req_flush(lo, rq);
Ming Leiaf65aa82014-12-31 13:23:00 +0000463 else if (rq->cmd_flags & REQ_DISCARD)
Ming Leicf655d92014-12-31 13:22:59 +0000464 ret = lo_discard(lo, rq, pos);
Ming Leiaf65aa82014-12-31 13:23:00 +0000465 else
466 ret = lo_send(lo, rq, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100467 } else
Ming Lei30112012014-12-31 13:22:58 +0000468 ret = lo_receive(lo, rq, lo->lo_blocksize, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return ret;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473struct switch_request {
474 struct file *file;
475 struct completion wait;
476};
477
Milan Broz14f27932008-12-12 14:48:27 +0100478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * Do the actual switch; called from the BIO completion routine
480 */
481static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
482{
483 struct file *file = p->file;
484 struct file *old_file = lo->lo_backing_file;
Milan Broz14f27932008-12-12 14:48:27 +0100485 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Milan Broz14f27932008-12-12 14:48:27 +0100487 /* if no new file, only flush of queued bios requested */
488 if (!file)
Ming Leib5dd2f62014-12-31 13:22:57 +0000489 return;
Milan Broz14f27932008-12-12 14:48:27 +0100490
491 mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
493 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700494 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
495 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 lo->old_gfp_mask = mapping_gfp_mask(mapping);
497 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Ming Leib5dd2f62014-12-31 13:22:57 +0000500/*
501 * loop_switch performs the hard work of switching a backing store.
502 * First it needs to flush existing IO, it does this by sending a magic
503 * BIO down the pipe. The completion of this BIO does the actual switch.
504 */
505static int loop_switch(struct loop_device *lo, struct file *file)
506{
507 struct switch_request w;
508
509 w.file = file;
510
511 /* freeze queue and wait for completion of scheduled requests */
512 blk_mq_freeze_queue(lo->lo_queue);
513
514 /* do the switch action */
515 do_loop_switch(lo, &w);
516
517 /* unfreeze */
518 blk_mq_unfreeze_queue(lo->lo_queue);
519
520 return 0;
521}
522
523/*
524 * Helper to flush the IOs in loop, but keeping loop thread running
525 */
526static int loop_flush(struct loop_device *lo)
527{
528 return loop_switch(lo, NULL);
529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531/*
532 * loop_change_fd switched the backing store of a loopback device to
533 * a new file. This is useful for operating system installers to free up
534 * the original file and in High Availability environments to switch to
535 * an alternative location for the content in case of server meltdown.
536 * This can only work if the loop device is used read-only, and if the
537 * new backing store is the same size and type as the old backing store.
538 */
Al Virobb214882008-03-02 09:29:48 -0500539static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
540 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct file *file, *old_file;
543 struct inode *inode;
544 int error;
545
546 error = -ENXIO;
547 if (lo->lo_state != Lo_bound)
548 goto out;
549
550 /* the loop device has to be read-only */
551 error = -EINVAL;
552 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
553 goto out;
554
555 error = -EBADF;
556 file = fget(arg);
557 if (!file)
558 goto out;
559
560 inode = file->f_mapping->host;
561 old_file = lo->lo_backing_file;
562
563 error = -EINVAL;
564
565 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
566 goto out_putf;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* size of the new backing store needs to be the same */
569 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
570 goto out_putf;
571
572 /* and ... switch */
573 error = loop_switch(lo, file);
574 if (error)
575 goto out_putf;
576
577 fput(old_file);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200578 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100579 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581
582 out_putf:
583 fput(file);
584 out:
585 return error;
586}
587
588static inline int is_loop_device(struct file *file)
589{
590 struct inode *i = file->f_mapping->host;
591
592 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
593}
594
Milan Brozee862732010-08-23 15:16:00 +0200595/* loop sysfs attributes */
596
597static ssize_t loop_attr_show(struct device *dev, char *page,
598 ssize_t (*callback)(struct loop_device *, char *))
599{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200600 struct gendisk *disk = dev_to_disk(dev);
601 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200602
Kay Sievers34dd82a2011-07-31 22:08:04 +0200603 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200604}
605
606#define LOOP_ATTR_RO(_name) \
607static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
608static ssize_t loop_attr_do_show_##_name(struct device *d, \
609 struct device_attribute *attr, char *b) \
610{ \
611 return loop_attr_show(d, b, loop_attr_##_name##_show); \
612} \
613static struct device_attribute loop_attr_##_name = \
614 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
615
616static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
617{
618 ssize_t ret;
619 char *p = NULL;
620
Kay Sievers05eb0f22011-07-31 22:21:35 +0200621 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200622 if (lo->lo_backing_file)
623 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200624 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200625
626 if (IS_ERR_OR_NULL(p))
627 ret = PTR_ERR(p);
628 else {
629 ret = strlen(p);
630 memmove(buf, p, ret);
631 buf[ret++] = '\n';
632 buf[ret] = 0;
633 }
634
635 return ret;
636}
637
638static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
639{
640 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
641}
642
643static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
644{
645 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
646}
647
648static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
649{
650 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
651
652 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
653}
654
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200655static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
656{
657 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
658
659 return sprintf(buf, "%s\n", partscan ? "1" : "0");
660}
661
Milan Brozee862732010-08-23 15:16:00 +0200662LOOP_ATTR_RO(backing_file);
663LOOP_ATTR_RO(offset);
664LOOP_ATTR_RO(sizelimit);
665LOOP_ATTR_RO(autoclear);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200666LOOP_ATTR_RO(partscan);
Milan Brozee862732010-08-23 15:16:00 +0200667
668static struct attribute *loop_attrs[] = {
669 &loop_attr_backing_file.attr,
670 &loop_attr_offset.attr,
671 &loop_attr_sizelimit.attr,
672 &loop_attr_autoclear.attr,
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200673 &loop_attr_partscan.attr,
Milan Brozee862732010-08-23 15:16:00 +0200674 NULL,
675};
676
677static struct attribute_group loop_attribute_group = {
678 .name = "loop",
679 .attrs= loop_attrs,
680};
681
682static int loop_sysfs_init(struct loop_device *lo)
683{
684 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
685 &loop_attribute_group);
686}
687
688static void loop_sysfs_exit(struct loop_device *lo)
689{
690 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
691 &loop_attribute_group);
692}
693
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200694static void loop_config_discard(struct loop_device *lo)
695{
696 struct file *file = lo->lo_backing_file;
697 struct inode *inode = file->f_mapping->host;
698 struct request_queue *q = lo->lo_queue;
699
700 /*
701 * We use punch hole to reclaim the free space used by the
Olaf Hering12a64d22014-01-21 14:39:24 -0800702 * image a.k.a. discard. However we do not support discard if
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200703 * encryption is enabled, because it may give an attacker
704 * useful information.
705 */
706 if ((!file->f_op->fallocate) ||
707 lo->lo_encrypt_key_size) {
708 q->limits.discard_granularity = 0;
709 q->limits.discard_alignment = 0;
710 q->limits.max_discard_sectors = 0;
711 q->limits.discard_zeroes_data = 0;
712 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
713 return;
714 }
715
716 q->limits.discard_granularity = inode->i_sb->s_blocksize;
Lukas Czernerdfaf3c02011-12-02 14:47:03 +0100717 q->limits.discard_alignment = 0;
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200718 q->limits.max_discard_sectors = UINT_MAX >> 9;
719 q->limits.discard_zeroes_data = 1;
720 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
721}
722
Al Virobb214882008-03-02 09:29:48 -0500723static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct block_device *bdev, unsigned int arg)
725{
726 struct file *file, *f;
727 struct inode *inode;
728 struct address_space *mapping;
729 unsigned lo_blocksize;
730 int lo_flags = 0;
731 int error;
732 loff_t size;
733
734 /* This is safe, since we have a reference from open(). */
735 __module_get(THIS_MODULE);
736
737 error = -EBADF;
738 file = fget(arg);
739 if (!file)
740 goto out;
741
742 error = -EBUSY;
743 if (lo->lo_state != Lo_unbound)
744 goto out_putf;
745
746 /* Avoid recursion */
747 f = file;
748 while (is_loop_device(f)) {
749 struct loop_device *l;
750
Al Virobb214882008-03-02 09:29:48 -0500751 if (f->f_mapping->host->i_bdev == bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto out_putf;
753
754 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
755 if (l->lo_state == Lo_unbound) {
756 error = -EINVAL;
757 goto out_putf;
758 }
759 f = l->lo_backing_file;
760 }
761
762 mapping = file->f_mapping;
763 inode = mapping->host;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 error = -EINVAL;
Christoph Hellwig456be142011-10-17 12:57:20 +0200766 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Christoph Hellwig456be142011-10-17 12:57:20 +0200769 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
770 !file->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 lo_flags |= LO_FLAGS_READ_ONLY;
772
Christoph Hellwig456be142011-10-17 12:57:20 +0200773 lo_blocksize = S_ISBLK(inode->i_mode) ?
774 inode->i_bdev->bd_block_size : PAGE_SIZE;
775
776 error = -EFBIG;
777 size = get_loop_size(lo, file);
778 if ((loff_t)(sector_t)size != size)
779 goto out_putf;
780
781 error = 0;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
784
785 lo->lo_blocksize = lo_blocksize;
786 lo->lo_device = bdev;
787 lo->lo_flags = lo_flags;
788 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700789 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 lo->ioctl = NULL;
791 lo->lo_sizelimit = 0;
792 lo->old_gfp_mask = mapping_gfp_mask(mapping);
793 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
794
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100795 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Tejun Heo4913efe2010-09-03 11:56:16 +0200796 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100797
Ken Chen73285082007-05-08 00:28:20 -0700798 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +0200800 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200801 /* let user-space know about the new size */
802 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 set_blocksize(bdev, lo_blocksize);
805
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700806 lo->lo_state = Lo_bound;
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200807 if (part_shift)
808 lo->lo_flags |= LO_FLAGS_PARTSCAN;
809 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
Laurent Vivier476a4812008-03-26 12:11:53 +0100810 ioctl_by_bdev(bdev, BLKRRPART, 0);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700811
812 /* Grab the block_device to prevent its destruction after we
813 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
814 */
815 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817
818 out_putf:
819 fput(file);
820 out:
821 /* This is safe: open() is still holding a reference. */
822 module_put(THIS_MODULE);
823 return error;
824}
825
826static int
827loop_release_xfer(struct loop_device *lo)
828{
829 int err = 0;
830 struct loop_func_table *xfer = lo->lo_encryption;
831
832 if (xfer) {
833 if (xfer->release)
834 err = xfer->release(lo);
835 lo->transfer = NULL;
836 lo->lo_encryption = NULL;
837 module_put(xfer->owner);
838 }
839 return err;
840}
841
842static int
843loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
844 const struct loop_info64 *i)
845{
846 int err = 0;
847
848 if (xfer) {
849 struct module *owner = xfer->owner;
850
851 if (!try_module_get(owner))
852 return -EINVAL;
853 if (xfer->init)
854 err = xfer->init(lo, i);
855 if (err)
856 module_put(owner);
857 else
858 lo->lo_encryption = xfer;
859 }
860 return err;
861}
862
Ayan George4c823cc2011-09-21 10:02:13 +0200863static int loop_clr_fd(struct loop_device *lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -0400866 gfp_t gfp = lo->old_gfp_mask;
Ayan George4c823cc2011-09-21 10:02:13 +0200867 struct block_device *bdev = lo->lo_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (lo->lo_state != Lo_bound)
870 return -ENXIO;
871
Dave Chinnera1ecac32012-09-28 10:42:23 +0200872 /*
873 * If we've explicitly asked to tear down the loop device,
874 * and it has an elevated reference count, set it for auto-teardown when
875 * the last reference goes away. This stops $!~#$@ udev from
876 * preventing teardown because it decided that it needs to run blkid on
877 * the loopback device whenever they appear. xfstests is notorious for
878 * failing tests because blkid via udev races with a losetup
879 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
880 * command to fail with EBUSY.
881 */
882 if (lo->lo_refcnt > 1) {
883 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
884 mutex_unlock(&lo->lo_ctl_mutex);
885 return 0;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (filp == NULL)
889 return -EINVAL;
890
891 spin_lock_irq(&lo->lo_lock);
892 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +0200894 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 loop_release_xfer(lo);
897 lo->transfer = NULL;
898 lo->ioctl = NULL;
899 lo->lo_device = NULL;
900 lo->lo_encryption = NULL;
901 lo->lo_offset = 0;
902 lo->lo_sizelimit = 0;
903 lo->lo_encrypt_key_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
905 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
906 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700907 if (bdev) {
908 bdput(bdev);
Al Virobb214882008-03-02 09:29:48 -0500909 invalidate_bdev(bdev);
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700910 }
Ken Chen73285082007-05-08 00:28:20 -0700911 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -0600912 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200913 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -0500914 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200915 /* let user-space know about this change */
916 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 mapping_set_gfp_mask(filp->f_mapping, gfp);
919 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* This is safe: open() is still holding a reference. */
921 module_put(THIS_MODULE);
Jens Axboec2fccc12013-04-08 10:12:11 +0200922 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
923 ioctl_by_bdev(bdev, BLKRRPART, 0);
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200924 lo->lo_flags = 0;
925 if (!part_shift)
926 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +0100927 mutex_unlock(&lo->lo_ctl_mutex);
928 /*
929 * Need not hold lo_ctl_mutex to fput backing file.
930 * Calling fput holding lo_ctl_mutex triggers a circular
931 * lock dependency possibility warning as fput can take
932 * bd_mutex which is usually taken before lo_ctl_mutex.
933 */
934 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
937
938static int
939loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
940{
941 int err;
942 struct loop_func_table *xfer;
Eric W. Biedermane4849732012-02-11 11:23:51 -0800943 kuid_t uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
David Howellsb0fafa82008-11-14 10:38:41 +1100945 if (lo->lo_encrypt_key_size &&
Eric W. Biedermane4849732012-02-11 11:23:51 -0800946 !uid_eq(lo->lo_key_owner, uid) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 !capable(CAP_SYS_ADMIN))
948 return -EPERM;
949 if (lo->lo_state != Lo_bound)
950 return -ENXIO;
951 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
952 return -EINVAL;
953
954 err = loop_release_xfer(lo);
955 if (err)
956 return err;
957
958 if (info->lo_encrypt_type) {
959 unsigned int type = info->lo_encrypt_type;
960
961 if (type >= MAX_LO_CRYPT)
962 return -EINVAL;
963 xfer = xfer_funcs[type];
964 if (xfer == NULL)
965 return -EINVAL;
966 } else
967 xfer = NULL;
968
969 err = loop_init_xfer(lo, xfer, info);
970 if (err)
971 return err;
972
973 if (lo->lo_offset != info->lo_offset ||
Guo Chao7b0576a2013-02-21 15:16:47 -0800974 lo->lo_sizelimit != info->lo_sizelimit)
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +0100975 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -EFBIG;
Guo Chao541c7422013-02-21 15:16:46 -0800977
Lukas Czernerdfaa2ef2011-08-19 14:50:46 +0200978 loop_config_discard(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
981 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
982 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
983 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
984
985 if (!xfer)
986 xfer = &none_funcs;
987 lo->transfer = xfer->transfer;
988 lo->ioctl = xfer->ioctl;
989
David Woodhouse96c58652008-02-06 01:36:27 -0800990 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
991 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
992 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
993
Kay Sieverse03c8dd2011-08-23 20:12:04 +0200994 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
995 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
996 lo->lo_flags |= LO_FLAGS_PARTSCAN;
997 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
998 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
999 }
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1002 lo->lo_init[0] = info->lo_init[0];
1003 lo->lo_init[1] = info->lo_init[1];
1004 if (info->lo_encrypt_key_size) {
1005 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1006 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001007 lo->lo_key_owner = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
1010 return 0;
1011}
1012
1013static int
1014loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1015{
1016 struct file *file = lo->lo_backing_file;
1017 struct kstat stat;
1018 int error;
1019
1020 if (lo->lo_state != Lo_bound)
1021 return -ENXIO;
Al Viro3dadecc2013-01-24 02:18:08 -05001022 error = vfs_getattr(&file->f_path, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (error)
1024 return error;
1025 memset(info, 0, sizeof(*info));
1026 info->lo_number = lo->lo_number;
1027 info->lo_device = huge_encode_dev(stat.dev);
1028 info->lo_inode = stat.ino;
1029 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1030 info->lo_offset = lo->lo_offset;
1031 info->lo_sizelimit = lo->lo_sizelimit;
1032 info->lo_flags = lo->lo_flags;
1033 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1034 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1035 info->lo_encrypt_type =
1036 lo->lo_encryption ? lo->lo_encryption->number : 0;
1037 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1038 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1039 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1040 lo->lo_encrypt_key_size);
1041 }
1042 return 0;
1043}
1044
1045static void
1046loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1047{
1048 memset(info64, 0, sizeof(*info64));
1049 info64->lo_number = info->lo_number;
1050 info64->lo_device = info->lo_device;
1051 info64->lo_inode = info->lo_inode;
1052 info64->lo_rdevice = info->lo_rdevice;
1053 info64->lo_offset = info->lo_offset;
1054 info64->lo_sizelimit = 0;
1055 info64->lo_encrypt_type = info->lo_encrypt_type;
1056 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1057 info64->lo_flags = info->lo_flags;
1058 info64->lo_init[0] = info->lo_init[0];
1059 info64->lo_init[1] = info->lo_init[1];
1060 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1061 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1062 else
1063 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1064 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1065}
1066
1067static int
1068loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1069{
1070 memset(info, 0, sizeof(*info));
1071 info->lo_number = info64->lo_number;
1072 info->lo_device = info64->lo_device;
1073 info->lo_inode = info64->lo_inode;
1074 info->lo_rdevice = info64->lo_rdevice;
1075 info->lo_offset = info64->lo_offset;
1076 info->lo_encrypt_type = info64->lo_encrypt_type;
1077 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1078 info->lo_flags = info64->lo_flags;
1079 info->lo_init[0] = info64->lo_init[0];
1080 info->lo_init[1] = info64->lo_init[1];
1081 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1082 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1083 else
1084 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1085 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1086
1087 /* error in case values were truncated */
1088 if (info->lo_device != info64->lo_device ||
1089 info->lo_rdevice != info64->lo_rdevice ||
1090 info->lo_inode != info64->lo_inode ||
1091 info->lo_offset != info64->lo_offset)
1092 return -EOVERFLOW;
1093
1094 return 0;
1095}
1096
1097static int
1098loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1099{
1100 struct loop_info info;
1101 struct loop_info64 info64;
1102
1103 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1104 return -EFAULT;
1105 loop_info64_from_old(&info, &info64);
1106 return loop_set_status(lo, &info64);
1107}
1108
1109static int
1110loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1111{
1112 struct loop_info64 info64;
1113
1114 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1115 return -EFAULT;
1116 return loop_set_status(lo, &info64);
1117}
1118
1119static int
1120loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1121 struct loop_info info;
1122 struct loop_info64 info64;
1123 int err = 0;
1124
1125 if (!arg)
1126 err = -EINVAL;
1127 if (!err)
1128 err = loop_get_status(lo, &info64);
1129 if (!err)
1130 err = loop_info64_to_old(&info64, &info);
1131 if (!err && copy_to_user(arg, &info, sizeof(info)))
1132 err = -EFAULT;
1133
1134 return err;
1135}
1136
1137static int
1138loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1139 struct loop_info64 info64;
1140 int err = 0;
1141
1142 if (!arg)
1143 err = -EINVAL;
1144 if (!err)
1145 err = loop_get_status(lo, &info64);
1146 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1147 err = -EFAULT;
1148
1149 return err;
1150}
1151
J. R. Okajima53d66602009-03-31 15:23:43 -07001152static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1153{
J. R. Okajima53d66602009-03-31 15:23:43 -07001154 if (unlikely(lo->lo_state != Lo_bound))
Guo Chao7b0576a2013-02-21 15:16:47 -08001155 return -ENXIO;
J. R. Okajima53d66602009-03-31 15:23:43 -07001156
Guo Chao7b0576a2013-02-21 15:16:47 -08001157 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
J. R. Okajima53d66602009-03-31 15:23:43 -07001158}
1159
Al Virobb214882008-03-02 09:29:48 -05001160static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned int cmd, unsigned long arg)
1162{
Al Virobb214882008-03-02 09:29:48 -05001163 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int err;
1165
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001166 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 switch (cmd) {
1168 case LOOP_SET_FD:
Al Virobb214882008-03-02 09:29:48 -05001169 err = loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001172 err = loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 case LOOP_CLR_FD:
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001175 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
Ayan George4c823cc2011-09-21 10:02:13 +02001176 err = loop_clr_fd(lo);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001177 if (!err)
1178 goto out_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 case LOOP_SET_STATUS:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001181 err = -EPERM;
1182 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1183 err = loop_set_status_old(lo,
1184 (struct loop_info __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 case LOOP_GET_STATUS:
1187 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1188 break;
1189 case LOOP_SET_STATUS64:
Dmitry Monakhov7035b5d2011-11-16 09:21:49 +01001190 err = -EPERM;
1191 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1192 err = loop_set_status64(lo,
1193 (struct loop_info64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
1195 case LOOP_GET_STATUS64:
1196 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1197 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001198 case LOOP_SET_CAPACITY:
1199 err = -EPERM;
1200 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1201 err = loop_set_capacity(lo, bdev);
1202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 default:
1204 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1205 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001206 mutex_unlock(&lo->lo_ctl_mutex);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001207
1208out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return err;
1210}
1211
David Howells863d5b822006-08-29 19:06:14 +01001212#ifdef CONFIG_COMPAT
1213struct compat_loop_info {
1214 compat_int_t lo_number; /* ioctl r/o */
1215 compat_dev_t lo_device; /* ioctl r/o */
1216 compat_ulong_t lo_inode; /* ioctl r/o */
1217 compat_dev_t lo_rdevice; /* ioctl r/o */
1218 compat_int_t lo_offset;
1219 compat_int_t lo_encrypt_type;
1220 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1221 compat_int_t lo_flags; /* ioctl r/o */
1222 char lo_name[LO_NAME_SIZE];
1223 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1224 compat_ulong_t lo_init[2];
1225 char reserved[4];
1226};
1227
1228/*
1229 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1230 * - noinlined to reduce stack space usage in main part of driver
1231 */
1232static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001233loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001234 struct loop_info64 *info64)
1235{
1236 struct compat_loop_info info;
1237
1238 if (copy_from_user(&info, arg, sizeof(info)))
1239 return -EFAULT;
1240
1241 memset(info64, 0, sizeof(*info64));
1242 info64->lo_number = info.lo_number;
1243 info64->lo_device = info.lo_device;
1244 info64->lo_inode = info.lo_inode;
1245 info64->lo_rdevice = info.lo_rdevice;
1246 info64->lo_offset = info.lo_offset;
1247 info64->lo_sizelimit = 0;
1248 info64->lo_encrypt_type = info.lo_encrypt_type;
1249 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1250 info64->lo_flags = info.lo_flags;
1251 info64->lo_init[0] = info.lo_init[0];
1252 info64->lo_init[1] = info.lo_init[1];
1253 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1254 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1255 else
1256 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1257 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1258 return 0;
1259}
1260
1261/*
1262 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1263 * - noinlined to reduce stack space usage in main part of driver
1264 */
1265static noinline int
1266loop_info64_to_compat(const struct loop_info64 *info64,
1267 struct compat_loop_info __user *arg)
1268{
1269 struct compat_loop_info info;
1270
1271 memset(&info, 0, sizeof(info));
1272 info.lo_number = info64->lo_number;
1273 info.lo_device = info64->lo_device;
1274 info.lo_inode = info64->lo_inode;
1275 info.lo_rdevice = info64->lo_rdevice;
1276 info.lo_offset = info64->lo_offset;
1277 info.lo_encrypt_type = info64->lo_encrypt_type;
1278 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1279 info.lo_flags = info64->lo_flags;
1280 info.lo_init[0] = info64->lo_init[0];
1281 info.lo_init[1] = info64->lo_init[1];
1282 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1283 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1284 else
1285 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1286 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1287
1288 /* error in case values were truncated */
1289 if (info.lo_device != info64->lo_device ||
1290 info.lo_rdevice != info64->lo_rdevice ||
1291 info.lo_inode != info64->lo_inode ||
1292 info.lo_offset != info64->lo_offset ||
1293 info.lo_init[0] != info64->lo_init[0] ||
1294 info.lo_init[1] != info64->lo_init[1])
1295 return -EOVERFLOW;
1296
1297 if (copy_to_user(arg, &info, sizeof(info)))
1298 return -EFAULT;
1299 return 0;
1300}
1301
1302static int
1303loop_set_status_compat(struct loop_device *lo,
1304 const struct compat_loop_info __user *arg)
1305{
1306 struct loop_info64 info64;
1307 int ret;
1308
1309 ret = loop_info64_from_compat(arg, &info64);
1310 if (ret < 0)
1311 return ret;
1312 return loop_set_status(lo, &info64);
1313}
1314
1315static int
1316loop_get_status_compat(struct loop_device *lo,
1317 struct compat_loop_info __user *arg)
1318{
1319 struct loop_info64 info64;
1320 int err = 0;
1321
1322 if (!arg)
1323 err = -EINVAL;
1324 if (!err)
1325 err = loop_get_status(lo, &info64);
1326 if (!err)
1327 err = loop_info64_to_compat(&info64, arg);
1328 return err;
1329}
1330
Al Virobb214882008-03-02 09:29:48 -05001331static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1332 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001333{
Al Virobb214882008-03-02 09:29:48 -05001334 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001335 int err;
1336
David Howells863d5b822006-08-29 19:06:14 +01001337 switch(cmd) {
1338 case LOOP_SET_STATUS:
1339 mutex_lock(&lo->lo_ctl_mutex);
1340 err = loop_set_status_compat(
1341 lo, (const struct compat_loop_info __user *) arg);
1342 mutex_unlock(&lo->lo_ctl_mutex);
1343 break;
1344 case LOOP_GET_STATUS:
1345 mutex_lock(&lo->lo_ctl_mutex);
1346 err = loop_get_status_compat(
1347 lo, (struct compat_loop_info __user *) arg);
1348 mutex_unlock(&lo->lo_ctl_mutex);
1349 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001350 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001351 case LOOP_CLR_FD:
1352 case LOOP_GET_STATUS64:
1353 case LOOP_SET_STATUS64:
1354 arg = (unsigned long) compat_ptr(arg);
1355 case LOOP_SET_FD:
1356 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001357 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001358 break;
1359 default:
1360 err = -ENOIOCTLCMD;
1361 break;
1362 }
David Howells863d5b822006-08-29 19:06:14 +01001363 return err;
1364}
1365#endif
1366
Al Virobb214882008-03-02 09:29:48 -05001367static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Kay Sievers770fe302011-07-31 22:08:04 +02001369 struct loop_device *lo;
1370 int err = 0;
1371
1372 mutex_lock(&loop_index_mutex);
1373 lo = bdev->bd_disk->private_data;
1374 if (!lo) {
1375 err = -ENXIO;
1376 goto out;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Ingo Molnarf85221d2006-03-23 03:00:38 -08001379 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001381 mutex_unlock(&lo->lo_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02001382out:
1383 mutex_unlock(&loop_index_mutex);
1384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Al Virodb2a1442013-05-05 21:52:57 -04001387static void lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Al Virobb214882008-03-02 09:29:48 -05001389 struct loop_device *lo = disk->private_data;
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001390 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Ingo Molnarf85221d2006-03-23 03:00:38 -08001392 mutex_lock(&lo->lo_ctl_mutex);
David Woodhouse96c58652008-02-06 01:36:27 -08001393
Milan Broz14f27932008-12-12 14:48:27 +01001394 if (--lo->lo_refcnt)
1395 goto out;
1396
1397 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1398 /*
1399 * In autoclear mode, stop the loop thread
1400 * and remove configuration after last close.
1401 */
Ayan George4c823cc2011-09-21 10:02:13 +02001402 err = loop_clr_fd(lo);
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001403 if (!err)
Al Virodb2a1442013-05-05 21:52:57 -04001404 return;
Milan Broz14f27932008-12-12 14:48:27 +01001405 } else {
1406 /*
1407 * Otherwise keep thread (if running) and config,
1408 * but flush possible ongoing bios in thread.
1409 */
1410 loop_flush(lo);
1411 }
David Woodhouse96c58652008-02-06 01:36:27 -08001412
Milan Broz14f27932008-12-12 14:48:27 +01001413out:
Ingo Molnarf85221d2006-03-23 03:00:38 -08001414 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001417static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001419 .open = lo_open,
1420 .release = lo_release,
1421 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001422#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001423 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425};
1426
1427/*
1428 * And now the modules code and kernel interface.
1429 */
Ken Chen73285082007-05-08 00:28:20 -07001430static int max_loop;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001431module_param(max_loop, int, S_IRUGO);
Ken Chena47653f2007-06-08 13:46:44 -07001432MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Namhyung Kimac04fee2011-05-27 07:59:25 +02001433module_param(max_part, int, S_IRUGO);
Laurent Vivier476a4812008-03-26 12:11:53 +01001434MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435MODULE_LICENSE("GPL");
1436MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1437
1438int loop_register_transfer(struct loop_func_table *funcs)
1439{
1440 unsigned int n = funcs->number;
1441
1442 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1443 return -EINVAL;
1444 xfer_funcs[n] = funcs;
1445 return 0;
1446}
1447
Kay Sievers34dd82a2011-07-31 22:08:04 +02001448static int unregister_transfer_cb(int id, void *ptr, void *data)
1449{
1450 struct loop_device *lo = ptr;
1451 struct loop_func_table *xfer = data;
1452
1453 mutex_lock(&lo->lo_ctl_mutex);
1454 if (lo->lo_encryption == xfer)
1455 loop_release_xfer(lo);
1456 mutex_unlock(&lo->lo_ctl_mutex);
1457 return 0;
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460int loop_unregister_transfer(int number)
1461{
1462 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct loop_func_table *xfer;
1464
1465 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1466 return -EINVAL;
1467
1468 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001469 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473EXPORT_SYMBOL(loop_register_transfer);
1474EXPORT_SYMBOL(loop_unregister_transfer);
1475
Ming Leib5dd2f62014-12-31 13:22:57 +00001476static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1477 const struct blk_mq_queue_data *bd)
1478{
1479 struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1480
1481 blk_mq_start_request(bd->rq);
1482
1483 if (cmd->rq->cmd_flags & REQ_WRITE) {
1484 struct loop_device *lo = cmd->rq->q->queuedata;
1485 bool need_sched = true;
1486
1487 spin_lock_irq(&lo->lo_lock);
1488 if (lo->write_started)
1489 need_sched = false;
1490 else
1491 lo->write_started = true;
1492 list_add_tail(&cmd->list, &lo->write_cmd_head);
1493 spin_unlock_irq(&lo->lo_lock);
1494
1495 if (need_sched)
1496 queue_work(loop_wq, &lo->write_work);
1497 } else {
1498 queue_work(loop_wq, &cmd->read_work);
1499 }
1500
1501 return BLK_MQ_RQ_QUEUE_OK;
1502}
1503
1504static void loop_handle_cmd(struct loop_cmd *cmd)
1505{
1506 const bool write = cmd->rq->cmd_flags & REQ_WRITE;
1507 struct loop_device *lo = cmd->rq->q->queuedata;
1508 int ret = -EIO;
Ming Leib5dd2f62014-12-31 13:22:57 +00001509
1510 if (lo->lo_state != Lo_bound)
1511 goto failed;
1512
1513 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
1514 goto failed;
1515
Ming Lei30112012014-12-31 13:22:58 +00001516 ret = do_req_filebacked(lo, cmd->rq);
Ming Leib5dd2f62014-12-31 13:22:57 +00001517
1518 failed:
1519 if (ret)
1520 cmd->rq->errors = -EIO;
1521 blk_mq_complete_request(cmd->rq);
1522}
1523
1524static void loop_queue_write_work(struct work_struct *work)
1525{
1526 struct loop_device *lo =
1527 container_of(work, struct loop_device, write_work);
1528 LIST_HEAD(cmd_list);
1529
1530 spin_lock_irq(&lo->lo_lock);
1531 repeat:
1532 list_splice_init(&lo->write_cmd_head, &cmd_list);
1533 spin_unlock_irq(&lo->lo_lock);
1534
1535 while (!list_empty(&cmd_list)) {
1536 struct loop_cmd *cmd = list_first_entry(&cmd_list,
1537 struct loop_cmd, list);
1538 list_del_init(&cmd->list);
1539 loop_handle_cmd(cmd);
1540 }
1541
1542 spin_lock_irq(&lo->lo_lock);
1543 if (!list_empty(&lo->write_cmd_head))
1544 goto repeat;
1545 lo->write_started = false;
1546 spin_unlock_irq(&lo->lo_lock);
1547}
1548
1549static void loop_queue_read_work(struct work_struct *work)
1550{
1551 struct loop_cmd *cmd =
1552 container_of(work, struct loop_cmd, read_work);
1553
1554 loop_handle_cmd(cmd);
1555}
1556
1557static int loop_init_request(void *data, struct request *rq,
1558 unsigned int hctx_idx, unsigned int request_idx,
1559 unsigned int numa_node)
1560{
1561 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1562
1563 cmd->rq = rq;
1564 INIT_WORK(&cmd->read_work, loop_queue_read_work);
1565
1566 return 0;
1567}
1568
1569static struct blk_mq_ops loop_mq_ops = {
1570 .queue_rq = loop_queue_rq,
1571 .map_queue = blk_mq_map_queue,
1572 .init_request = loop_init_request,
1573};
1574
Kay Sievers34dd82a2011-07-31 22:08:04 +02001575static int loop_add(struct loop_device **l, int i)
Ken Chen73285082007-05-08 00:28:20 -07001576{
1577 struct loop_device *lo;
1578 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001579 int err;
Ken Chen73285082007-05-08 00:28:20 -07001580
Silva Paulo68d740d2012-07-14 15:39:58 -07001581 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001582 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Silva Paulo68d740d2012-07-14 15:39:58 -07001583 if (!lo)
Ken Chen73285082007-05-08 00:28:20 -07001584 goto out;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001585
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001586 lo->lo_state = Lo_unbound;
1587
Tejun Heoc718aa62013-02-27 17:03:58 -08001588 /* allocate id, if @id >= 0, we're requesting that specific id */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001589 if (i >= 0) {
Tejun Heoc718aa62013-02-27 17:03:58 -08001590 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1591 if (err == -ENOSPC)
Kay Sievers34dd82a2011-07-31 22:08:04 +02001592 err = -EEXIST;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001593 } else {
Tejun Heoc718aa62013-02-27 17:03:58 -08001594 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001595 }
1596 if (err < 0)
1597 goto out_free_dev;
Tejun Heoc718aa62013-02-27 17:03:58 -08001598 i = err;
Ken Chen73285082007-05-08 00:28:20 -07001599
Wei Yongjun183cfb52013-03-22 08:59:19 -06001600 err = -ENOMEM;
Ming Leib5dd2f62014-12-31 13:22:57 +00001601 lo->tag_set.ops = &loop_mq_ops;
1602 lo->tag_set.nr_hw_queues = 1;
1603 lo->tag_set.queue_depth = 128;
1604 lo->tag_set.numa_node = NUMA_NO_NODE;
1605 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
1606 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
1607 lo->tag_set.driver_data = lo;
1608
1609 err = blk_mq_alloc_tag_set(&lo->tag_set);
1610 if (err)
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001611 goto out_free_idr;
Ken Chen73285082007-05-08 00:28:20 -07001612
Ming Leib5dd2f62014-12-31 13:22:57 +00001613 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
1614 if (IS_ERR_OR_NULL(lo->lo_queue)) {
1615 err = PTR_ERR(lo->lo_queue);
1616 goto out_cleanup_tags;
1617 }
Mikulas Patockaef7e7c82013-10-15 14:14:38 -06001618 lo->lo_queue->queuedata = lo;
1619
Ming Leib5dd2f62014-12-31 13:22:57 +00001620 INIT_LIST_HEAD(&lo->write_cmd_head);
1621 INIT_WORK(&lo->write_work, loop_queue_write_work);
1622
Laurent Vivier476a4812008-03-26 12:11:53 +01001623 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07001624 if (!disk)
1625 goto out_free_queue;
1626
Kay Sieverse03c8dd2011-08-23 20:12:04 +02001627 /*
1628 * Disable partition scanning by default. The in-kernel partition
1629 * scanning can be requested individually per-device during its
1630 * setup. Userspace can always add and remove partitions from all
1631 * devices. The needed partition minors are allocated from the
1632 * extended minor space, the main loop device numbers will continue
1633 * to match the loop minors, regardless of the number of partitions
1634 * used.
1635 *
1636 * If max_part is given, partition scanning is globally enabled for
1637 * all loop devices. The minors for the main loop devices will be
1638 * multiples of max_part.
1639 *
1640 * Note: Global-for-all-devices, set-only-at-init, read-only module
1641 * parameteters like 'max_loop' and 'max_part' make things needlessly
1642 * complicated, are too static, inflexible and may surprise
1643 * userspace tools. Parameters like this in general should be avoided.
1644 */
1645 if (!part_shift)
1646 disk->flags |= GENHD_FL_NO_PART_SCAN;
1647 disk->flags |= GENHD_FL_EXT_DEVT;
Ken Chen73285082007-05-08 00:28:20 -07001648 mutex_init(&lo->lo_ctl_mutex);
1649 lo->lo_number = i;
Ken Chen73285082007-05-08 00:28:20 -07001650 spin_lock_init(&lo->lo_lock);
1651 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01001652 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07001653 disk->fops = &lo_fops;
1654 disk->private_data = lo;
1655 disk->queue = lo->lo_queue;
1656 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001657 add_disk(disk);
1658 *l = lo;
1659 return lo->lo_number;
Ken Chen73285082007-05-08 00:28:20 -07001660
1661out_free_queue:
1662 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001663out_cleanup_tags:
1664 blk_mq_free_tag_set(&lo->tag_set);
Mikulas Patocka3ec981e2013-10-14 12:12:24 -04001665out_free_idr:
1666 idr_remove(&loop_index_idr, i);
Ken Chen73285082007-05-08 00:28:20 -07001667out_free_dev:
1668 kfree(lo);
1669out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001670 return err;
Ken Chen73285082007-05-08 00:28:20 -07001671}
1672
Kay Sievers34dd82a2011-07-31 22:08:04 +02001673static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001674{
Kay Sievers34dd82a2011-07-31 22:08:04 +02001675 del_gendisk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001676 blk_cleanup_queue(lo->lo_queue);
Ming Leib5dd2f62014-12-31 13:22:57 +00001677 blk_mq_free_tag_set(&lo->tag_set);
Ken Chen73285082007-05-08 00:28:20 -07001678 put_disk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001679 kfree(lo);
1680}
1681
Kay Sievers770fe302011-07-31 22:08:04 +02001682static int find_free_cb(int id, void *ptr, void *data)
Ken Chena47653f2007-06-08 13:46:44 -07001683{
Kay Sievers770fe302011-07-31 22:08:04 +02001684 struct loop_device *lo = ptr;
1685 struct loop_device **l = data;
Ken Chena47653f2007-06-08 13:46:44 -07001686
Kay Sievers770fe302011-07-31 22:08:04 +02001687 if (lo->lo_state == Lo_unbound) {
1688 *l = lo;
1689 return 1;
Ken Chena47653f2007-06-08 13:46:44 -07001690 }
Kay Sievers770fe302011-07-31 22:08:04 +02001691 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07001692}
1693
Kay Sievers34dd82a2011-07-31 22:08:04 +02001694static int loop_lookup(struct loop_device **l, int i)
Ken Chena47653f2007-06-08 13:46:44 -07001695{
Ken Chena47653f2007-06-08 13:46:44 -07001696 struct loop_device *lo;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001697 int ret = -ENODEV;
Ken Chena47653f2007-06-08 13:46:44 -07001698
Kay Sievers770fe302011-07-31 22:08:04 +02001699 if (i < 0) {
1700 int err;
1701
1702 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1703 if (err == 1) {
1704 *l = lo;
1705 ret = lo->lo_number;
1706 }
1707 goto out;
1708 }
1709
1710 /* lookup and return a specific i */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001711 lo = idr_find(&loop_index_idr, i);
Ken Chena47653f2007-06-08 13:46:44 -07001712 if (lo) {
Kay Sievers34dd82a2011-07-31 22:08:04 +02001713 *l = lo;
1714 ret = lo->lo_number;
Ken Chena47653f2007-06-08 13:46:44 -07001715 }
Kay Sievers770fe302011-07-31 22:08:04 +02001716out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001717 return ret;
Ken Chena47653f2007-06-08 13:46:44 -07001718}
1719
Ken Chen73285082007-05-08 00:28:20 -07001720static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1721{
Al Viro705962c2007-05-13 05:52:32 -04001722 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001723 struct kobject *kobj;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001724 int err;
Ken Chen73285082007-05-08 00:28:20 -07001725
Kay Sievers34dd82a2011-07-31 22:08:04 +02001726 mutex_lock(&loop_index_mutex);
1727 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1728 if (err < 0)
1729 err = loop_add(&lo, MINOR(dev) >> part_shift);
1730 if (err < 0)
Mikulas Patockaa207f592013-10-14 12:13:24 -04001731 kobj = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001732 else
1733 kobj = get_disk(lo->lo_disk);
1734 mutex_unlock(&loop_index_mutex);
Ken Chen73285082007-05-08 00:28:20 -07001735
1736 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001737 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001738}
1739
Kay Sievers770fe302011-07-31 22:08:04 +02001740static long loop_control_ioctl(struct file *file, unsigned int cmd,
1741 unsigned long parm)
1742{
1743 struct loop_device *lo;
1744 int ret = -ENOSYS;
1745
1746 mutex_lock(&loop_index_mutex);
1747 switch (cmd) {
1748 case LOOP_CTL_ADD:
1749 ret = loop_lookup(&lo, parm);
1750 if (ret >= 0) {
1751 ret = -EEXIST;
1752 break;
1753 }
1754 ret = loop_add(&lo, parm);
1755 break;
1756 case LOOP_CTL_REMOVE:
1757 ret = loop_lookup(&lo, parm);
1758 if (ret < 0)
1759 break;
1760 mutex_lock(&lo->lo_ctl_mutex);
1761 if (lo->lo_state != Lo_unbound) {
1762 ret = -EBUSY;
1763 mutex_unlock(&lo->lo_ctl_mutex);
1764 break;
1765 }
1766 if (lo->lo_refcnt > 0) {
1767 ret = -EBUSY;
1768 mutex_unlock(&lo->lo_ctl_mutex);
1769 break;
1770 }
1771 lo->lo_disk->private_data = NULL;
1772 mutex_unlock(&lo->lo_ctl_mutex);
1773 idr_remove(&loop_index_idr, lo->lo_number);
1774 loop_remove(lo);
1775 break;
1776 case LOOP_CTL_GET_FREE:
1777 ret = loop_lookup(&lo, -1);
1778 if (ret >= 0)
1779 break;
1780 ret = loop_add(&lo, -1);
1781 }
1782 mutex_unlock(&loop_index_mutex);
1783
1784 return ret;
1785}
1786
1787static const struct file_operations loop_ctl_fops = {
1788 .open = nonseekable_open,
1789 .unlocked_ioctl = loop_control_ioctl,
1790 .compat_ioctl = loop_control_ioctl,
1791 .owner = THIS_MODULE,
1792 .llseek = noop_llseek,
1793};
1794
1795static struct miscdevice loop_misc = {
1796 .minor = LOOP_CTRL_MINOR,
1797 .name = "loop-control",
1798 .fops = &loop_ctl_fops,
1799};
1800
1801MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1802MODULE_ALIAS("devname:loop-control");
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804static int __init loop_init(void)
1805{
Ken Chena47653f2007-06-08 13:46:44 -07001806 int i, nr;
1807 unsigned long range;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001808 struct loop_device *lo;
Kay Sievers770fe302011-07-31 22:08:04 +02001809 int err;
Ken Chena47653f2007-06-08 13:46:44 -07001810
Kay Sievers770fe302011-07-31 22:08:04 +02001811 err = misc_register(&loop_misc);
1812 if (err < 0)
1813 return err;
Laurent Vivier476a4812008-03-26 12:11:53 +01001814
1815 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001816 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01001817 part_shift = fls(max_part);
1818
Namhyung Kimac04fee2011-05-27 07:59:25 +02001819 /*
1820 * Adjust max_part according to part_shift as it is exported
1821 * to user space so that user can decide correct minor number
1822 * if [s]he want to create more devices.
1823 *
1824 * Note that -1 is required because partition 0 is reserved
1825 * for the whole disk.
1826 */
1827 max_part = (1UL << part_shift) - 1;
1828 }
1829
Guo Chaob1a66502013-02-21 15:16:49 -08001830 if ((1UL << part_shift) > DISK_MAX_PARTS) {
1831 err = -EINVAL;
1832 goto misc_out;
1833 }
Namhyung Kim78f4bb32011-05-24 16:48:54 +02001834
Guo Chaob1a66502013-02-21 15:16:49 -08001835 if (max_loop > 1UL << (MINORBITS - part_shift)) {
1836 err = -EINVAL;
1837 goto misc_out;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Kay Sieversd134b002011-07-31 22:08:04 +02001840 /*
1841 * If max_loop is specified, create that many devices upfront.
1842 * This also becomes a hard limit. If max_loop is not specified,
1843 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1844 * init time. Loop devices can be requested on-demand with the
1845 * /dev/loop-control interface, or be instantiated by accessing
1846 * a 'dead' device node.
1847 */
Ken Chen73285082007-05-08 00:28:20 -07001848 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001849 nr = max_loop;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001850 range = max_loop << part_shift;
Ken Chena47653f2007-06-08 13:46:44 -07001851 } else {
Kay Sieversd134b002011-07-31 22:08:04 +02001852 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001853 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Ken Chena47653f2007-06-08 13:46:44 -07001855
Guo Chaob1a66502013-02-21 15:16:49 -08001856 if (register_blkdev(LOOP_MAJOR, "loop")) {
1857 err = -EIO;
1858 goto misc_out;
1859 }
Ken Chena47653f2007-06-08 13:46:44 -07001860
Ming Leib5dd2f62014-12-31 13:22:57 +00001861 loop_wq = alloc_workqueue("kloopd",
1862 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0);
1863 if (!loop_wq) {
1864 err = -ENOMEM;
1865 goto misc_out;
1866 }
1867
Ken Chena47653f2007-06-08 13:46:44 -07001868 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1869 THIS_MODULE, loop_probe, NULL, NULL);
1870
Kay Sieversd134b002011-07-31 22:08:04 +02001871 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001872 mutex_lock(&loop_index_mutex);
1873 for (i = 0; i < nr; i++)
1874 loop_add(&lo, i);
1875 mutex_unlock(&loop_index_mutex);
1876
Ken Chen73285082007-05-08 00:28:20 -07001877 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 0;
Guo Chaob1a66502013-02-21 15:16:49 -08001879
1880misc_out:
1881 misc_deregister(&loop_misc);
1882 return err;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001883}
Ken Chena47653f2007-06-08 13:46:44 -07001884
Kay Sievers34dd82a2011-07-31 22:08:04 +02001885static int loop_exit_cb(int id, void *ptr, void *data)
1886{
1887 struct loop_device *lo = ptr;
Ken Chena47653f2007-06-08 13:46:44 -07001888
Kay Sievers34dd82a2011-07-31 22:08:04 +02001889 loop_remove(lo);
1890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Ken Chen73285082007-05-08 00:28:20 -07001893static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Ken Chena47653f2007-06-08 13:46:44 -07001895 unsigned long range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Namhyung Kima1c15c52011-05-24 16:48:55 +02001897 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
Ken Chena47653f2007-06-08 13:46:44 -07001898
Kay Sievers34dd82a2011-07-31 22:08:04 +02001899 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001900 idr_destroy(&loop_index_idr);
Ken Chen73285082007-05-08 00:28:20 -07001901
Ken Chena47653f2007-06-08 13:46:44 -07001902 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001903 unregister_blkdev(LOOP_MAJOR, "loop");
Kay Sievers770fe302011-07-31 22:08:04 +02001904
Ming Leib5dd2f62014-12-31 13:22:57 +00001905 destroy_workqueue(loop_wq);
1906
Kay Sievers770fe302011-07-31 22:08:04 +02001907 misc_deregister(&loop_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910module_init(loop_init);
1911module_exit(loop_exit);
1912
1913#ifndef MODULE
1914static int __init max_loop_setup(char *str)
1915{
1916 max_loop = simple_strtol(str, NULL, 0);
1917 return 1;
1918}
1919
1920__setup("max_loop=", max_loop_setup);
1921#endif