blob: dc552308668ec40abbd2ee370ee4e66f98ccc693 [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>
66#include <linux/loop.h>
David Howells863d5b822006-08-29 19:06:14 +010067#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/suspend.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070069#include <linux/freezer.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020070#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/writeback.h>
72#include <linux/buffer_head.h> /* for invalidate_bdev() */
73#include <linux/completion.h>
74#include <linux/highmem.h>
Serge E. Hallyn6c997912006-09-29 01:59:11 -070075#include <linux/kthread.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020076#include <linux/splice.h>
Milan Brozee862732010-08-23 15:16:00 +020077#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#include <asm/uaccess.h>
80
Ken Chen73285082007-05-08 00:28:20 -070081static LIST_HEAD(loop_devices);
82static DEFINE_MUTEX(loop_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Laurent Vivier476a4812008-03-26 12:11:53 +010084static int max_part;
85static int part_shift;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * Transfer functions
89 */
90static int transfer_none(struct loop_device *lo, int cmd,
91 struct page *raw_page, unsigned raw_off,
92 struct page *loop_page, unsigned loop_off,
93 int size, sector_t real_block)
94{
95 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
96 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
97
98 if (cmd == READ)
99 memcpy(loop_buf, raw_buf, size);
100 else
101 memcpy(raw_buf, loop_buf, size);
102
103 kunmap_atomic(raw_buf, KM_USER0);
104 kunmap_atomic(loop_buf, KM_USER1);
105 cond_resched();
106 return 0;
107}
108
109static int transfer_xor(struct loop_device *lo, int cmd,
110 struct page *raw_page, unsigned raw_off,
111 struct page *loop_page, unsigned loop_off,
112 int size, sector_t real_block)
113{
114 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
115 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
116 char *in, *out, *key;
117 int i, keysize;
118
119 if (cmd == READ) {
120 in = raw_buf;
121 out = loop_buf;
122 } else {
123 in = loop_buf;
124 out = raw_buf;
125 }
126
127 key = lo->lo_encrypt_key;
128 keysize = lo->lo_encrypt_key_size;
129 for (i = 0; i < size; i++)
130 *out++ = *in++ ^ key[(i & 511) % keysize];
131
132 kunmap_atomic(raw_buf, KM_USER0);
133 kunmap_atomic(loop_buf, KM_USER1);
134 cond_resched();
135 return 0;
136}
137
138static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
139{
140 if (unlikely(info->lo_encrypt_key_size <= 0))
141 return -EINVAL;
142 return 0;
143}
144
145static struct loop_func_table none_funcs = {
146 .number = LO_CRYPT_NONE,
147 .transfer = transfer_none,
148};
149
150static struct loop_func_table xor_funcs = {
151 .number = LO_CRYPT_XOR,
152 .transfer = transfer_xor,
153 .init = xor_init
154};
155
156/* xfer_funcs[0] is special - its release function is never called */
157static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
158 &none_funcs,
159 &xor_funcs
160};
161
162static loff_t get_loop_size(struct loop_device *lo, struct file *file)
163{
164 loff_t size, offset, loopsize;
165
166 /* Compute loopsize in bytes */
167 size = i_size_read(file->f_mapping->host);
168 offset = lo->lo_offset;
169 loopsize = size - offset;
170 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
171 loopsize = lo->lo_sizelimit;
172
173 /*
174 * Unfortunately, if we want to do I/O on the device,
175 * the number of 512-byte sectors has to fit into a sector_t.
176 */
177 return loopsize >> 9;
178}
179
180static int
181figure_loop_size(struct loop_device *lo)
182{
183 loff_t size = get_loop_size(lo, lo->lo_backing_file);
184 sector_t x = (sector_t)size;
185
186 if (unlikely((loff_t)x != size))
187 return -EFBIG;
188
Ken Chen73285082007-05-08 00:28:20 -0700189 set_capacity(lo->lo_disk, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191}
192
193static inline int
194lo_do_transfer(struct loop_device *lo, int cmd,
195 struct page *rpage, unsigned roffs,
196 struct page *lpage, unsigned loffs,
197 int size, sector_t rblock)
198{
199 if (unlikely(!lo->transfer))
200 return 0;
201
202 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
203}
204
205/**
206 * do_lo_send_aops - helper for writing data to a loop device
207 *
208 * This is the fast version for backing filesystems which implement the address
Nick Pigginafddba42007-10-16 01:25:01 -0700209 * space operations write_begin and write_end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400212 loff_t pos, struct page *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
215 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 pgoff_t index;
217 unsigned offset, bv_offs;
Zach Brown994fc28c2005-12-15 14:28:17 -0800218 int len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800220 mutex_lock(&mapping->host->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 index = pos >> PAGE_CACHE_SHIFT;
222 offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
223 bv_offs = bvec->bv_offset;
224 len = bvec->bv_len;
225 while (len > 0) {
226 sector_t IV;
Nick Pigginafddba42007-10-16 01:25:01 -0700227 unsigned size, copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int transfer_result;
Nick Pigginafddba42007-10-16 01:25:01 -0700229 struct page *page;
230 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
233 size = PAGE_CACHE_SIZE - offset;
234 if (size > len)
235 size = len;
Nick Pigginafddba42007-10-16 01:25:01 -0700236
237 ret = pagecache_write_begin(file, mapping, pos, size, 0,
238 &page, &fsdata);
239 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -0700241
Nikanth Karthikesan02246c42010-04-08 21:39:31 +0200242 file_update_time(file);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 transfer_result = lo_do_transfer(lo, WRITE, page, offset,
245 bvec->bv_page, bv_offs, size, IV);
Nick Pigginafddba42007-10-16 01:25:01 -0700246 copied = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (unlikely(transfer_result))
Nick Pigginafddba42007-10-16 01:25:01 -0700248 copied = 0;
249
250 ret = pagecache_write_end(file, mapping, pos, size, copied,
251 page, fsdata);
Dmitry Monakhov8268f5a2007-10-16 01:25:02 -0700252 if (ret < 0 || ret != copied)
Nick Pigginafddba42007-10-16 01:25:01 -0700253 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -0700254
255 if (unlikely(transfer_result))
256 goto fail;
257
258 bv_offs += copied;
259 len -= copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 offset = 0;
261 index++;
Nick Pigginafddba42007-10-16 01:25:01 -0700262 pos += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Zach Brown994fc28c2005-12-15 14:28:17 -0800264 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800266 mutex_unlock(&mapping->host->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268fail:
269 ret = -1;
270 goto out;
271}
272
273/**
274 * __do_lo_send_write - helper for writing data to a loop device
275 *
276 * This helper just factors out common code between do_lo_send_direct_write()
277 * and do_lo_send_write().
278 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800279static int __do_lo_send_write(struct file *file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100280 u8 *buf, const int len, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 ssize_t bw;
283 mm_segment_t old_fs = get_fs();
284
285 set_fs(get_ds());
286 bw = file->f_op->write(file, buf, len, &pos);
287 set_fs(old_fs);
288 if (likely(bw == len))
289 return 0;
290 printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
291 (unsigned long long)pos, len);
292 if (bw >= 0)
293 bw = -EIO;
294 return bw;
295}
296
297/**
298 * do_lo_send_direct_write - helper for writing data to a loop device
299 *
300 * This is the fast, non-transforming version for backing filesystems which do
Nick Pigginafddba42007-10-16 01:25:01 -0700301 * not implement the address space operations write_begin and write_end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * It uses the write file operation which should be present on all writeable
303 * filesystems.
304 */
305static int do_lo_send_direct_write(struct loop_device *lo,
Al Viro511de732007-10-08 12:10:13 -0400306 struct bio_vec *bvec, loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100309 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 bvec->bv_len, pos);
311 kunmap(bvec->bv_page);
312 cond_resched();
313 return bw;
314}
315
316/**
317 * do_lo_send_write - helper for writing data to a loop device
318 *
319 * This is the slow, transforming version for filesystems which do not
Nick Pigginafddba42007-10-16 01:25:01 -0700320 * implement the address space operations write_begin and write_end. It
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * uses the write file operation which should be present on all writeable
322 * filesystems.
323 *
324 * Using fops->write is slower than using aops->{prepare,commit}_write in the
325 * transforming case because we need to double buffer the data as we cannot do
326 * the transformations in place as we do not have direct access to the
327 * destination pages of the backing file.
328 */
329static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400330 loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
333 bvec->bv_offset, bvec->bv_len, pos >> 9);
334 if (likely(!ret))
335 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100336 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 pos);
338 printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
339 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
340 if (ret > 0)
341 ret = -EIO;
342 return ret;
343}
344
Al Viro511de732007-10-08 12:10:13 -0400345static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Al Viro511de732007-10-08 12:10:13 -0400347 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct page *page);
349 struct bio_vec *bvec;
350 struct page *page = NULL;
351 int i, ret = 0;
352
353 do_lo_send = do_lo_send_aops;
354 if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
355 do_lo_send = do_lo_send_direct_write;
356 if (lo->transfer != transfer_none) {
357 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
358 if (unlikely(!page))
359 goto fail;
360 kmap(page);
361 do_lo_send = do_lo_send_write;
362 }
363 }
364 bio_for_each_segment(bvec, bio, i) {
Al Viro511de732007-10-08 12:10:13 -0400365 ret = do_lo_send(lo, bvec, pos, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ret < 0)
367 break;
368 pos += bvec->bv_len;
369 }
370 if (page) {
371 kunmap(page);
372 __free_page(page);
373 }
374out:
375 return ret;
376fail:
377 printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
378 ret = -ENOMEM;
379 goto out;
380}
381
382struct lo_read_data {
383 struct loop_device *lo;
384 struct page *page;
385 unsigned offset;
386 int bsize;
387};
388
389static int
Jens Axboefd582142007-06-12 21:20:37 +0200390lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
391 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jens Axboefd582142007-06-12 21:20:37 +0200393 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200395 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sector_t IV;
Roel Kluina3941ec2009-03-05 08:03:53 +0100397 int size, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jens Axboecac36bb02007-06-14 13:10:48 +0200399 ret = buf->ops->confirm(pipe, buf);
Jens Axboefd582142007-06-12 21:20:37 +0200400 if (unlikely(ret))
401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jens Axboefd582142007-06-12 21:20:37 +0200403 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
404 (buf->offset >> 9);
405 size = sd->len;
406 if (size > p->bsize)
407 size = p->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jens Axboefd582142007-06-12 21:20:37 +0200409 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printk(KERN_ERR "loop: transfer error block %ld\n",
411 page->index);
Jens Axboefd582142007-06-12 21:20:37 +0200412 size = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 flush_dcache_page(p->page);
416
Jens Axboefd582142007-06-12 21:20:37 +0200417 if (size > 0)
418 p->offset += size;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return size;
421}
422
423static int
Jens Axboefd582142007-06-12 21:20:37 +0200424lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
425{
426 return __splice_from_pipe(pipe, sd, lo_splice_actor);
427}
428
429static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430do_lo_receive(struct loop_device *lo,
431 struct bio_vec *bvec, int bsize, loff_t pos)
432{
433 struct lo_read_data cookie;
Jens Axboefd582142007-06-12 21:20:37 +0200434 struct splice_desc sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct file *file;
Jens Axboefd582142007-06-12 21:20:37 +0200436 long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 cookie.lo = lo;
439 cookie.page = bvec->bv_page;
440 cookie.offset = bvec->bv_offset;
441 cookie.bsize = bsize;
Jens Axboefd582142007-06-12 21:20:37 +0200442
443 sd.len = 0;
444 sd.total_len = bvec->bv_len;
445 sd.flags = 0;
446 sd.pos = pos;
447 sd.u.data = &cookie;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 file = lo->lo_backing_file;
Jens Axboefd582142007-06-12 21:20:37 +0200450 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
451
452 if (retval < 0)
453 return retval;
454
455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static int
459lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
460{
461 struct bio_vec *bvec;
462 int i, ret = 0;
463
464 bio_for_each_segment(bvec, bio, i) {
465 ret = do_lo_receive(lo, bvec, bsize, pos);
466 if (ret < 0)
467 break;
468 pos += bvec->bv_len;
469 }
470 return ret;
471}
472
473static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
474{
475 loff_t pos;
476 int ret;
477
478 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100479
480 if (bio_rw(bio) == WRITE) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200481 bool barrier = (bio->bi_rw & REQ_HARDBARRIER);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100482 struct file *file = lo->lo_backing_file;
483
484 if (barrier) {
485 if (unlikely(!file->f_op->fsync)) {
486 ret = -EOPNOTSUPP;
487 goto out;
488 }
489
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100490 ret = vfs_fsync(file, 0);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100491 if (unlikely(ret)) {
492 ret = -EIO;
493 goto out;
494 }
495 }
496
Al Viro511de732007-10-08 12:10:13 -0400497 ret = lo_send(lo, bio, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100498
499 if (barrier && !ret) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100500 ret = vfs_fsync(file, 0);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100501 if (unlikely(ret))
502 ret = -EIO;
503 }
504 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100506
507out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return ret;
509}
510
511/*
512 * Add bio to back of pending list
513 */
514static void loop_add_bio(struct loop_device *lo, struct bio *bio)
515{
Akinobu Mitae6863072009-04-17 08:41:21 +0200516 bio_list_add(&lo->lo_bio_list, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/*
520 * Grab first pending buffer
521 */
522static struct bio *loop_get_bio(struct loop_device *lo)
523{
Akinobu Mitae6863072009-04-17 08:41:21 +0200524 return bio_list_pop(&lo->lo_bio_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Jens Axboe165125e2007-07-24 09:28:11 +0200527static int loop_make_request(struct request_queue *q, struct bio *old_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct loop_device *lo = q->queuedata;
530 int rw = bio_rw(old_bio);
531
Nick Piggin35a82d12005-06-23 00:09:06 -0700532 if (rw == READA)
533 rw = READ;
534
535 BUG_ON(!lo || (rw != READ && rw != WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 spin_lock_irq(&lo->lo_lock);
538 if (lo->lo_state != Lo_bound)
Nick Piggin35a82d12005-06-23 00:09:06 -0700539 goto out;
540 if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 loop_add_bio(lo, old_bio);
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700543 wake_up(&lo->lo_event);
Nick Piggin35a82d12005-06-23 00:09:06 -0700544 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
Nick Piggin35a82d12005-06-23 00:09:06 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547out:
Nick Piggin35a82d12005-06-23 00:09:06 -0700548 spin_unlock_irq(&lo->lo_lock);
NeilBrown6712ecf2007-09-27 12:47:43 +0200549 bio_io_error(old_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
554 * kick off io on the underlying address space
555 */
Jens Axboe165125e2007-07-24 09:28:11 +0200556static void loop_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct loop_device *lo = q->queuedata;
559
Nick Piggin75ad23b2008-04-29 14:48:33 +0200560 queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 blk_run_address_space(lo->lo_backing_file->f_mapping);
562}
563
564struct switch_request {
565 struct file *file;
566 struct completion wait;
567};
568
569static void do_loop_switch(struct loop_device *, struct switch_request *);
570
571static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (unlikely(!bio->bi_bdev)) {
574 do_loop_switch(lo, bio->bi_private);
575 bio_put(bio);
576 } else {
Nick Piggin35a82d12005-06-23 00:09:06 -0700577 int ret = do_bio_filebacked(lo, bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200578 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580}
581
582/*
583 * worker thread that handles reads/writes to file backed loop devices,
584 * to avoid blocking in our make_request_fn. it also does loop decrypting
585 * on reads for block backed loop, as that is too heavy to do from
586 * b_end_io context where irqs may be disabled.
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700587 *
588 * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
589 * calling kthread_stop(). Therefore once kthread_should_stop() is
590 * true, make_request will not place any more requests. Therefore
591 * once kthread_should_stop() is true and lo_bio is NULL, we are
592 * done with the loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594static int loop_thread(void *data)
595{
596 struct loop_device *lo = data;
597 struct bio *bio;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 set_user_nice(current, -20);
600
Akinobu Mitae6863072009-04-17 08:41:21 +0200601 while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700603 wait_event_interruptible(lo->lo_event,
Akinobu Mitae6863072009-04-17 08:41:21 +0200604 !bio_list_empty(&lo->lo_bio_list) ||
605 kthread_should_stop());
Linus Torvalds09c0dc62006-06-26 11:55:42 -0700606
Akinobu Mitae6863072009-04-17 08:41:21 +0200607 if (bio_list_empty(&lo->lo_bio_list))
Nick Piggin35a82d12005-06-23 00:09:06 -0700608 continue;
Nick Piggin35a82d12005-06-23 00:09:06 -0700609 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 bio = loop_get_bio(lo);
Nick Piggin35a82d12005-06-23 00:09:06 -0700611 spin_unlock_irq(&lo->lo_lock);
612
613 BUG_ON(!bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 loop_handle_bio(lo, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618}
619
620/*
621 * loop_switch performs the hard work of switching a backing store.
622 * First it needs to flush existing IO, it does this by sending a magic
623 * BIO down the pipe. The completion of this BIO does the actual switch.
624 */
625static int loop_switch(struct loop_device *lo, struct file *file)
626{
627 struct switch_request w;
Jens Axboea24eab12008-01-11 10:14:40 +0100628 struct bio *bio = bio_alloc(GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!bio)
630 return -ENOMEM;
631 init_completion(&w.wait);
632 w.file = file;
633 bio->bi_private = &w;
634 bio->bi_bdev = NULL;
635 loop_make_request(lo->lo_queue, bio);
636 wait_for_completion(&w.wait);
637 return 0;
638}
639
640/*
Milan Broz14f27932008-12-12 14:48:27 +0100641 * Helper to flush the IOs in loop, but keeping loop thread running
642 */
643static int loop_flush(struct loop_device *lo)
644{
645 /* loop not yet configured, no running thread, nothing to flush */
646 if (!lo->lo_thread)
647 return 0;
648
649 return loop_switch(lo, NULL);
650}
651
652/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * Do the actual switch; called from the BIO completion routine
654 */
655static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
656{
657 struct file *file = p->file;
658 struct file *old_file = lo->lo_backing_file;
Milan Broz14f27932008-12-12 14:48:27 +0100659 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Milan Broz14f27932008-12-12 14:48:27 +0100661 /* if no new file, only flush of queued bios requested */
662 if (!file)
663 goto out;
664
665 mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
667 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700668 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
669 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 lo->old_gfp_mask = mapping_gfp_mask(mapping);
671 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
Milan Broz14f27932008-12-12 14:48:27 +0100672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 complete(&p->wait);
674}
675
676
677/*
678 * loop_change_fd switched the backing store of a loopback device to
679 * a new file. This is useful for operating system installers to free up
680 * the original file and in High Availability environments to switch to
681 * an alternative location for the content in case of server meltdown.
682 * This can only work if the loop device is used read-only, and if the
683 * new backing store is the same size and type as the old backing store.
684 */
Al Virobb214882008-03-02 09:29:48 -0500685static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
686 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct file *file, *old_file;
689 struct inode *inode;
690 int error;
691
692 error = -ENXIO;
693 if (lo->lo_state != Lo_bound)
694 goto out;
695
696 /* the loop device has to be read-only */
697 error = -EINVAL;
698 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
699 goto out;
700
701 error = -EBADF;
702 file = fget(arg);
703 if (!file)
704 goto out;
705
706 inode = file->f_mapping->host;
707 old_file = lo->lo_backing_file;
708
709 error = -EINVAL;
710
711 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
712 goto out_putf;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* size of the new backing store needs to be the same */
715 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
716 goto out_putf;
717
718 /* and ... switch */
719 error = loop_switch(lo, file);
720 if (error)
721 goto out_putf;
722
723 fput(old_file);
Laurent Vivier476a4812008-03-26 12:11:53 +0100724 if (max_part > 0)
725 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727
728 out_putf:
729 fput(file);
730 out:
731 return error;
732}
733
734static inline int is_loop_device(struct file *file)
735{
736 struct inode *i = file->f_mapping->host;
737
738 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
739}
740
Milan Brozee862732010-08-23 15:16:00 +0200741/* loop sysfs attributes */
742
743static ssize_t loop_attr_show(struct device *dev, char *page,
744 ssize_t (*callback)(struct loop_device *, char *))
745{
746 struct loop_device *l, *lo = NULL;
747
748 mutex_lock(&loop_devices_mutex);
749 list_for_each_entry(l, &loop_devices, lo_list)
750 if (disk_to_dev(l->lo_disk) == dev) {
751 lo = l;
752 break;
753 }
754 mutex_unlock(&loop_devices_mutex);
755
756 return lo ? callback(lo, page) : -EIO;
757}
758
759#define LOOP_ATTR_RO(_name) \
760static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
761static ssize_t loop_attr_do_show_##_name(struct device *d, \
762 struct device_attribute *attr, char *b) \
763{ \
764 return loop_attr_show(d, b, loop_attr_##_name##_show); \
765} \
766static struct device_attribute loop_attr_##_name = \
767 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
768
769static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
770{
771 ssize_t ret;
772 char *p = NULL;
773
774 mutex_lock(&lo->lo_ctl_mutex);
775 if (lo->lo_backing_file)
776 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
777 mutex_unlock(&lo->lo_ctl_mutex);
778
779 if (IS_ERR_OR_NULL(p))
780 ret = PTR_ERR(p);
781 else {
782 ret = strlen(p);
783 memmove(buf, p, ret);
784 buf[ret++] = '\n';
785 buf[ret] = 0;
786 }
787
788 return ret;
789}
790
791static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
792{
793 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
794}
795
796static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
797{
798 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
799}
800
801static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
802{
803 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
804
805 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
806}
807
808LOOP_ATTR_RO(backing_file);
809LOOP_ATTR_RO(offset);
810LOOP_ATTR_RO(sizelimit);
811LOOP_ATTR_RO(autoclear);
812
813static struct attribute *loop_attrs[] = {
814 &loop_attr_backing_file.attr,
815 &loop_attr_offset.attr,
816 &loop_attr_sizelimit.attr,
817 &loop_attr_autoclear.attr,
818 NULL,
819};
820
821static struct attribute_group loop_attribute_group = {
822 .name = "loop",
823 .attrs= loop_attrs,
824};
825
826static int loop_sysfs_init(struct loop_device *lo)
827{
828 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
829 &loop_attribute_group);
830}
831
832static void loop_sysfs_exit(struct loop_device *lo)
833{
834 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
835 &loop_attribute_group);
836}
837
Al Virobb214882008-03-02 09:29:48 -0500838static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct block_device *bdev, unsigned int arg)
840{
841 struct file *file, *f;
842 struct inode *inode;
843 struct address_space *mapping;
844 unsigned lo_blocksize;
845 int lo_flags = 0;
846 int error;
847 loff_t size;
848
849 /* This is safe, since we have a reference from open(). */
850 __module_get(THIS_MODULE);
851
852 error = -EBADF;
853 file = fget(arg);
854 if (!file)
855 goto out;
856
857 error = -EBUSY;
858 if (lo->lo_state != Lo_unbound)
859 goto out_putf;
860
861 /* Avoid recursion */
862 f = file;
863 while (is_loop_device(f)) {
864 struct loop_device *l;
865
Al Virobb214882008-03-02 09:29:48 -0500866 if (f->f_mapping->host->i_bdev == bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto out_putf;
868
869 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
870 if (l->lo_state == Lo_unbound) {
871 error = -EINVAL;
872 goto out_putf;
873 }
874 f = l->lo_backing_file;
875 }
876
877 mapping = file->f_mapping;
878 inode = mapping->host;
879
880 if (!(file->f_mode & FMODE_WRITE))
881 lo_flags |= LO_FLAGS_READ_ONLY;
882
883 error = -EINVAL;
884 if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700885 const struct address_space_operations *aops = mapping->a_ops;
Miklos Szeredi68181732009-05-07 15:37:36 +0200886
Nick Piggin4e02ed42008-10-29 14:00:55 -0700887 if (aops->write_begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 lo_flags |= LO_FLAGS_USE_AOPS;
889 if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
890 lo_flags |= LO_FLAGS_READ_ONLY;
891
Theodore Ts'oba52de12006-09-27 01:50:49 -0700892 lo_blocksize = S_ISBLK(inode->i_mode) ?
893 inode->i_bdev->bd_block_size : PAGE_SIZE;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 error = 0;
896 } else {
897 goto out_putf;
898 }
899
900 size = get_loop_size(lo, file);
901
902 if ((loff_t)(sector_t)size != size) {
903 error = -EFBIG;
904 goto out_putf;
905 }
906
Al Virobb214882008-03-02 09:29:48 -0500907 if (!(mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 lo_flags |= LO_FLAGS_READ_ONLY;
909
910 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
911
912 lo->lo_blocksize = lo_blocksize;
913 lo->lo_device = bdev;
914 lo->lo_flags = lo_flags;
915 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700916 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 lo->ioctl = NULL;
918 lo->lo_sizelimit = 0;
919 lo->old_gfp_mask = mapping_gfp_mask(mapping);
920 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
921
Akinobu Mitae6863072009-04-17 08:41:21 +0200922 bio_list_init(&lo->lo_bio_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /*
925 * set queue make_request_fn, and add limits based on lower level
926 * device
927 */
928 blk_queue_make_request(lo->lo_queue, loop_make_request);
929 lo->lo_queue->queuedata = lo;
930 lo->lo_queue->unplug_fn = loop_unplug;
931
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100932 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
FUJITA Tomonori00fff262010-07-03 17:45:40 +0900933 blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100934
Ken Chen73285082007-05-08 00:28:20 -0700935 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +0200937 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200938 /* let user-space know about the new size */
939 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 set_blocksize(bdev, lo_blocksize);
942
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700943 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
944 lo->lo_number);
945 if (IS_ERR(lo->lo_thread)) {
946 error = PTR_ERR(lo->lo_thread);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700947 goto out_clr;
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700948 }
949 lo->lo_state = Lo_bound;
950 wake_up_process(lo->lo_thread);
Laurent Vivier476a4812008-03-26 12:11:53 +0100951 if (max_part > 0)
952 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700955out_clr:
Milan Brozee862732010-08-23 15:16:00 +0200956 loop_sysfs_exit(lo);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700957 lo->lo_thread = NULL;
958 lo->lo_device = NULL;
959 lo->lo_backing_file = NULL;
960 lo->lo_flags = 0;
Ken Chen73285082007-05-08 00:28:20 -0700961 set_capacity(lo->lo_disk, 0);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700962 invalidate_bdev(bdev);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700963 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200964 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700965 mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
966 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 out_putf:
968 fput(file);
969 out:
970 /* This is safe: open() is still holding a reference. */
971 module_put(THIS_MODULE);
972 return error;
973}
974
975static int
976loop_release_xfer(struct loop_device *lo)
977{
978 int err = 0;
979 struct loop_func_table *xfer = lo->lo_encryption;
980
981 if (xfer) {
982 if (xfer->release)
983 err = xfer->release(lo);
984 lo->transfer = NULL;
985 lo->lo_encryption = NULL;
986 module_put(xfer->owner);
987 }
988 return err;
989}
990
991static int
992loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
993 const struct loop_info64 *i)
994{
995 int err = 0;
996
997 if (xfer) {
998 struct module *owner = xfer->owner;
999
1000 if (!try_module_get(owner))
1001 return -EINVAL;
1002 if (xfer->init)
1003 err = xfer->init(lo, i);
1004 if (err)
1005 module_put(owner);
1006 else
1007 lo->lo_encryption = xfer;
1008 }
1009 return err;
1010}
1011
1012static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
1013{
1014 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -04001015 gfp_t gfp = lo->old_gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (lo->lo_state != Lo_bound)
1018 return -ENXIO;
1019
1020 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
1021 return -EBUSY;
1022
1023 if (filp == NULL)
1024 return -EINVAL;
1025
1026 spin_lock_irq(&lo->lo_lock);
1027 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 spin_unlock_irq(&lo->lo_lock);
1029
Serge E. Hallyn6c997912006-09-29 01:59:11 -07001030 kthread_stop(lo->lo_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Milan Broz8ae30b82008-12-12 14:50:49 +01001032 lo->lo_queue->unplug_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 lo->lo_backing_file = NULL;
1034
1035 loop_release_xfer(lo);
1036 lo->transfer = NULL;
1037 lo->ioctl = NULL;
1038 lo->lo_device = NULL;
1039 lo->lo_encryption = NULL;
1040 lo->lo_offset = 0;
1041 lo->lo_sizelimit = 0;
1042 lo->lo_encrypt_key_size = 0;
1043 lo->lo_flags = 0;
Serge E. Hallyn6c997912006-09-29 01:59:11 -07001044 lo->lo_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1046 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1047 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Al Virobb214882008-03-02 09:29:48 -05001048 if (bdev)
1049 invalidate_bdev(bdev);
Ken Chen73285082007-05-08 00:28:20 -07001050 set_capacity(lo->lo_disk, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +02001051 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -05001052 bd_set_size(bdev, 0);
Milan Brozee862732010-08-23 15:16:00 +02001053 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +02001054 /* let user-space know about this change */
1055 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 mapping_set_gfp_mask(filp->f_mapping, gfp);
1058 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* This is safe: open() is still holding a reference. */
1060 module_put(THIS_MODULE);
Alexey Dobriyancf6e6932009-10-26 16:49:55 -07001061 if (max_part > 0 && bdev)
Laurent Vivier476a4812008-03-26 12:11:53 +01001062 ioctl_by_bdev(bdev, BLKRRPART, 0);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001063 mutex_unlock(&lo->lo_ctl_mutex);
1064 /*
1065 * Need not hold lo_ctl_mutex to fput backing file.
1066 * Calling fput holding lo_ctl_mutex triggers a circular
1067 * lock dependency possibility warning as fput can take
1068 * bd_mutex which is usually taken before lo_ctl_mutex.
1069 */
1070 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
1072}
1073
1074static int
1075loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1076{
1077 int err;
1078 struct loop_func_table *xfer;
David Howellsb0fafa82008-11-14 10:38:41 +11001079 uid_t uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
David Howellsb0fafa82008-11-14 10:38:41 +11001081 if (lo->lo_encrypt_key_size &&
1082 lo->lo_key_owner != uid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 !capable(CAP_SYS_ADMIN))
1084 return -EPERM;
1085 if (lo->lo_state != Lo_bound)
1086 return -ENXIO;
1087 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
1088 return -EINVAL;
1089
1090 err = loop_release_xfer(lo);
1091 if (err)
1092 return err;
1093
1094 if (info->lo_encrypt_type) {
1095 unsigned int type = info->lo_encrypt_type;
1096
1097 if (type >= MAX_LO_CRYPT)
1098 return -EINVAL;
1099 xfer = xfer_funcs[type];
1100 if (xfer == NULL)
1101 return -EINVAL;
1102 } else
1103 xfer = NULL;
1104
1105 err = loop_init_xfer(lo, xfer, info);
1106 if (err)
1107 return err;
1108
1109 if (lo->lo_offset != info->lo_offset ||
1110 lo->lo_sizelimit != info->lo_sizelimit) {
1111 lo->lo_offset = info->lo_offset;
1112 lo->lo_sizelimit = info->lo_sizelimit;
1113 if (figure_loop_size(lo))
1114 return -EFBIG;
1115 }
1116
1117 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1118 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1119 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1120 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1121
1122 if (!xfer)
1123 xfer = &none_funcs;
1124 lo->transfer = xfer->transfer;
1125 lo->ioctl = xfer->ioctl;
1126
David Woodhouse96c58652008-02-06 01:36:27 -08001127 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
1128 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
1129 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1132 lo->lo_init[0] = info->lo_init[0];
1133 lo->lo_init[1] = info->lo_init[1];
1134 if (info->lo_encrypt_key_size) {
1135 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1136 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001137 lo->lo_key_owner = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
1140 return 0;
1141}
1142
1143static int
1144loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1145{
1146 struct file *file = lo->lo_backing_file;
1147 struct kstat stat;
1148 int error;
1149
1150 if (lo->lo_state != Lo_bound)
1151 return -ENXIO;
Josef Sipek6c648be2006-12-08 02:36:55 -08001152 error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (error)
1154 return error;
1155 memset(info, 0, sizeof(*info));
1156 info->lo_number = lo->lo_number;
1157 info->lo_device = huge_encode_dev(stat.dev);
1158 info->lo_inode = stat.ino;
1159 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1160 info->lo_offset = lo->lo_offset;
1161 info->lo_sizelimit = lo->lo_sizelimit;
1162 info->lo_flags = lo->lo_flags;
1163 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1164 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1165 info->lo_encrypt_type =
1166 lo->lo_encryption ? lo->lo_encryption->number : 0;
1167 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1168 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1169 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1170 lo->lo_encrypt_key_size);
1171 }
1172 return 0;
1173}
1174
1175static void
1176loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1177{
1178 memset(info64, 0, sizeof(*info64));
1179 info64->lo_number = info->lo_number;
1180 info64->lo_device = info->lo_device;
1181 info64->lo_inode = info->lo_inode;
1182 info64->lo_rdevice = info->lo_rdevice;
1183 info64->lo_offset = info->lo_offset;
1184 info64->lo_sizelimit = 0;
1185 info64->lo_encrypt_type = info->lo_encrypt_type;
1186 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1187 info64->lo_flags = info->lo_flags;
1188 info64->lo_init[0] = info->lo_init[0];
1189 info64->lo_init[1] = info->lo_init[1];
1190 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1191 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1192 else
1193 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1194 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1195}
1196
1197static int
1198loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1199{
1200 memset(info, 0, sizeof(*info));
1201 info->lo_number = info64->lo_number;
1202 info->lo_device = info64->lo_device;
1203 info->lo_inode = info64->lo_inode;
1204 info->lo_rdevice = info64->lo_rdevice;
1205 info->lo_offset = info64->lo_offset;
1206 info->lo_encrypt_type = info64->lo_encrypt_type;
1207 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1208 info->lo_flags = info64->lo_flags;
1209 info->lo_init[0] = info64->lo_init[0];
1210 info->lo_init[1] = info64->lo_init[1];
1211 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1212 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1213 else
1214 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1215 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1216
1217 /* error in case values were truncated */
1218 if (info->lo_device != info64->lo_device ||
1219 info->lo_rdevice != info64->lo_rdevice ||
1220 info->lo_inode != info64->lo_inode ||
1221 info->lo_offset != info64->lo_offset)
1222 return -EOVERFLOW;
1223
1224 return 0;
1225}
1226
1227static int
1228loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1229{
1230 struct loop_info info;
1231 struct loop_info64 info64;
1232
1233 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1234 return -EFAULT;
1235 loop_info64_from_old(&info, &info64);
1236 return loop_set_status(lo, &info64);
1237}
1238
1239static int
1240loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1241{
1242 struct loop_info64 info64;
1243
1244 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1245 return -EFAULT;
1246 return loop_set_status(lo, &info64);
1247}
1248
1249static int
1250loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1251 struct loop_info info;
1252 struct loop_info64 info64;
1253 int err = 0;
1254
1255 if (!arg)
1256 err = -EINVAL;
1257 if (!err)
1258 err = loop_get_status(lo, &info64);
1259 if (!err)
1260 err = loop_info64_to_old(&info64, &info);
1261 if (!err && copy_to_user(arg, &info, sizeof(info)))
1262 err = -EFAULT;
1263
1264 return err;
1265}
1266
1267static int
1268loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1269 struct loop_info64 info64;
1270 int err = 0;
1271
1272 if (!arg)
1273 err = -EINVAL;
1274 if (!err)
1275 err = loop_get_status(lo, &info64);
1276 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1277 err = -EFAULT;
1278
1279 return err;
1280}
1281
J. R. Okajima53d66602009-03-31 15:23:43 -07001282static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1283{
1284 int err;
1285 sector_t sec;
1286 loff_t sz;
1287
1288 err = -ENXIO;
1289 if (unlikely(lo->lo_state != Lo_bound))
1290 goto out;
1291 err = figure_loop_size(lo);
1292 if (unlikely(err))
1293 goto out;
1294 sec = get_capacity(lo->lo_disk);
1295 /* the width of sector_t may be narrow for bit-shift */
1296 sz = sec;
1297 sz <<= 9;
1298 mutex_lock(&bdev->bd_mutex);
1299 bd_set_size(bdev, sz);
David Zeuthenc3473c62010-05-03 14:08:59 +02001300 /* let user-space know about the new size */
1301 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
J. R. Okajima53d66602009-03-31 15:23:43 -07001302 mutex_unlock(&bdev->bd_mutex);
1303
1304 out:
1305 return err;
1306}
1307
Al Virobb214882008-03-02 09:29:48 -05001308static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned int cmd, unsigned long arg)
1310{
Al Virobb214882008-03-02 09:29:48 -05001311 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001314 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (cmd) {
1316 case LOOP_SET_FD:
Al Virobb214882008-03-02 09:29:48 -05001317 err = loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001320 err = loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case LOOP_CLR_FD:
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001323 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
Al Virobb214882008-03-02 09:29:48 -05001324 err = loop_clr_fd(lo, bdev);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001325 if (!err)
1326 goto out_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 case LOOP_SET_STATUS:
1329 err = loop_set_status_old(lo, (struct loop_info __user *) arg);
1330 break;
1331 case LOOP_GET_STATUS:
1332 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1333 break;
1334 case LOOP_SET_STATUS64:
1335 err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
1336 break;
1337 case LOOP_GET_STATUS64:
1338 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1339 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001340 case LOOP_SET_CAPACITY:
1341 err = -EPERM;
1342 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1343 err = loop_set_capacity(lo, bdev);
1344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 default:
1346 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1347 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001348 mutex_unlock(&lo->lo_ctl_mutex);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001349
1350out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return err;
1352}
1353
David Howells863d5b822006-08-29 19:06:14 +01001354#ifdef CONFIG_COMPAT
1355struct compat_loop_info {
1356 compat_int_t lo_number; /* ioctl r/o */
1357 compat_dev_t lo_device; /* ioctl r/o */
1358 compat_ulong_t lo_inode; /* ioctl r/o */
1359 compat_dev_t lo_rdevice; /* ioctl r/o */
1360 compat_int_t lo_offset;
1361 compat_int_t lo_encrypt_type;
1362 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1363 compat_int_t lo_flags; /* ioctl r/o */
1364 char lo_name[LO_NAME_SIZE];
1365 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1366 compat_ulong_t lo_init[2];
1367 char reserved[4];
1368};
1369
1370/*
1371 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1372 * - noinlined to reduce stack space usage in main part of driver
1373 */
1374static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001375loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001376 struct loop_info64 *info64)
1377{
1378 struct compat_loop_info info;
1379
1380 if (copy_from_user(&info, arg, sizeof(info)))
1381 return -EFAULT;
1382
1383 memset(info64, 0, sizeof(*info64));
1384 info64->lo_number = info.lo_number;
1385 info64->lo_device = info.lo_device;
1386 info64->lo_inode = info.lo_inode;
1387 info64->lo_rdevice = info.lo_rdevice;
1388 info64->lo_offset = info.lo_offset;
1389 info64->lo_sizelimit = 0;
1390 info64->lo_encrypt_type = info.lo_encrypt_type;
1391 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1392 info64->lo_flags = info.lo_flags;
1393 info64->lo_init[0] = info.lo_init[0];
1394 info64->lo_init[1] = info.lo_init[1];
1395 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1396 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1397 else
1398 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1399 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1400 return 0;
1401}
1402
1403/*
1404 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1405 * - noinlined to reduce stack space usage in main part of driver
1406 */
1407static noinline int
1408loop_info64_to_compat(const struct loop_info64 *info64,
1409 struct compat_loop_info __user *arg)
1410{
1411 struct compat_loop_info info;
1412
1413 memset(&info, 0, sizeof(info));
1414 info.lo_number = info64->lo_number;
1415 info.lo_device = info64->lo_device;
1416 info.lo_inode = info64->lo_inode;
1417 info.lo_rdevice = info64->lo_rdevice;
1418 info.lo_offset = info64->lo_offset;
1419 info.lo_encrypt_type = info64->lo_encrypt_type;
1420 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1421 info.lo_flags = info64->lo_flags;
1422 info.lo_init[0] = info64->lo_init[0];
1423 info.lo_init[1] = info64->lo_init[1];
1424 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1425 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1426 else
1427 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1428 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1429
1430 /* error in case values were truncated */
1431 if (info.lo_device != info64->lo_device ||
1432 info.lo_rdevice != info64->lo_rdevice ||
1433 info.lo_inode != info64->lo_inode ||
1434 info.lo_offset != info64->lo_offset ||
1435 info.lo_init[0] != info64->lo_init[0] ||
1436 info.lo_init[1] != info64->lo_init[1])
1437 return -EOVERFLOW;
1438
1439 if (copy_to_user(arg, &info, sizeof(info)))
1440 return -EFAULT;
1441 return 0;
1442}
1443
1444static int
1445loop_set_status_compat(struct loop_device *lo,
1446 const struct compat_loop_info __user *arg)
1447{
1448 struct loop_info64 info64;
1449 int ret;
1450
1451 ret = loop_info64_from_compat(arg, &info64);
1452 if (ret < 0)
1453 return ret;
1454 return loop_set_status(lo, &info64);
1455}
1456
1457static int
1458loop_get_status_compat(struct loop_device *lo,
1459 struct compat_loop_info __user *arg)
1460{
1461 struct loop_info64 info64;
1462 int err = 0;
1463
1464 if (!arg)
1465 err = -EINVAL;
1466 if (!err)
1467 err = loop_get_status(lo, &info64);
1468 if (!err)
1469 err = loop_info64_to_compat(&info64, arg);
1470 return err;
1471}
1472
Al Virobb214882008-03-02 09:29:48 -05001473static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1474 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001475{
Al Virobb214882008-03-02 09:29:48 -05001476 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001477 int err;
1478
David Howells863d5b822006-08-29 19:06:14 +01001479 switch(cmd) {
1480 case LOOP_SET_STATUS:
1481 mutex_lock(&lo->lo_ctl_mutex);
1482 err = loop_set_status_compat(
1483 lo, (const struct compat_loop_info __user *) arg);
1484 mutex_unlock(&lo->lo_ctl_mutex);
1485 break;
1486 case LOOP_GET_STATUS:
1487 mutex_lock(&lo->lo_ctl_mutex);
1488 err = loop_get_status_compat(
1489 lo, (struct compat_loop_info __user *) arg);
1490 mutex_unlock(&lo->lo_ctl_mutex);
1491 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001492 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001493 case LOOP_CLR_FD:
1494 case LOOP_GET_STATUS64:
1495 case LOOP_SET_STATUS64:
1496 arg = (unsigned long) compat_ptr(arg);
1497 case LOOP_SET_FD:
1498 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001499 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001500 break;
1501 default:
1502 err = -ENOIOCTLCMD;
1503 break;
1504 }
David Howells863d5b822006-08-29 19:06:14 +01001505 return err;
1506}
1507#endif
1508
Al Virobb214882008-03-02 09:29:48 -05001509static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Al Virobb214882008-03-02 09:29:48 -05001511 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001513 lock_kernel();
Ingo Molnarf85221d2006-03-23 03:00:38 -08001514 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001516 mutex_unlock(&lo->lo_ctl_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001517 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 return 0;
1520}
1521
Al Virobb214882008-03-02 09:29:48 -05001522static int lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Al Virobb214882008-03-02 09:29:48 -05001524 struct loop_device *lo = disk->private_data;
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001527 lock_kernel();
Ingo Molnarf85221d2006-03-23 03:00:38 -08001528 mutex_lock(&lo->lo_ctl_mutex);
David Woodhouse96c58652008-02-06 01:36:27 -08001529
Milan Broz14f27932008-12-12 14:48:27 +01001530 if (--lo->lo_refcnt)
1531 goto out;
1532
1533 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1534 /*
1535 * In autoclear mode, stop the loop thread
1536 * and remove configuration after last close.
1537 */
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001538 err = loop_clr_fd(lo, NULL);
1539 if (!err)
1540 goto out_unlocked;
Milan Broz14f27932008-12-12 14:48:27 +01001541 } else {
1542 /*
1543 * Otherwise keep thread (if running) and config,
1544 * but flush possible ongoing bios in thread.
1545 */
1546 loop_flush(lo);
1547 }
David Woodhouse96c58652008-02-06 01:36:27 -08001548
Milan Broz14f27932008-12-12 14:48:27 +01001549out:
Ingo Molnarf85221d2006-03-23 03:00:38 -08001550 mutex_unlock(&lo->lo_ctl_mutex);
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001551out_unlocked:
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001552 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 0;
1554}
1555
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001556static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001558 .open = lo_open,
1559 .release = lo_release,
1560 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001561#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001562 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564};
1565
1566/*
1567 * And now the modules code and kernel interface.
1568 */
Ken Chen73285082007-05-08 00:28:20 -07001569static int max_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570module_param(max_loop, int, 0);
Ken Chena47653f2007-06-08 13:46:44 -07001571MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Laurent Vivier476a4812008-03-26 12:11:53 +01001572module_param(max_part, int, 0);
1573MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574MODULE_LICENSE("GPL");
1575MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1576
1577int loop_register_transfer(struct loop_func_table *funcs)
1578{
1579 unsigned int n = funcs->number;
1580
1581 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1582 return -EINVAL;
1583 xfer_funcs[n] = funcs;
1584 return 0;
1585}
1586
1587int loop_unregister_transfer(int number)
1588{
1589 unsigned int n = number;
1590 struct loop_device *lo;
1591 struct loop_func_table *xfer;
1592
1593 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1594 return -EINVAL;
1595
1596 xfer_funcs[n] = NULL;
1597
Ken Chen73285082007-05-08 00:28:20 -07001598 list_for_each_entry(lo, &loop_devices, lo_list) {
Ingo Molnarf85221d2006-03-23 03:00:38 -08001599 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (lo->lo_encryption == xfer)
1602 loop_release_xfer(lo);
1603
Ingo Molnarf85221d2006-03-23 03:00:38 -08001604 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 return 0;
1608}
1609
1610EXPORT_SYMBOL(loop_register_transfer);
1611EXPORT_SYMBOL(loop_unregister_transfer);
1612
Ken Chena47653f2007-06-08 13:46:44 -07001613static struct loop_device *loop_alloc(int i)
Ken Chen73285082007-05-08 00:28:20 -07001614{
1615 struct loop_device *lo;
1616 struct gendisk *disk;
1617
1618 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
1619 if (!lo)
1620 goto out;
1621
1622 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
1623 if (!lo->lo_queue)
1624 goto out_free_dev;
1625
Laurent Vivier476a4812008-03-26 12:11:53 +01001626 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07001627 if (!disk)
1628 goto out_free_queue;
1629
1630 mutex_init(&lo->lo_ctl_mutex);
1631 lo->lo_number = i;
1632 lo->lo_thread = NULL;
1633 init_waitqueue_head(&lo->lo_event);
1634 spin_lock_init(&lo->lo_lock);
1635 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01001636 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07001637 disk->fops = &lo_fops;
1638 disk->private_data = lo;
1639 disk->queue = lo->lo_queue;
1640 sprintf(disk->disk_name, "loop%d", i);
Ken Chen73285082007-05-08 00:28:20 -07001641 return lo;
1642
1643out_free_queue:
1644 blk_cleanup_queue(lo->lo_queue);
1645out_free_dev:
1646 kfree(lo);
1647out:
Al Viro07002e92007-05-12 16:23:15 -04001648 return NULL;
Ken Chen73285082007-05-08 00:28:20 -07001649}
1650
Ken Chena47653f2007-06-08 13:46:44 -07001651static void loop_free(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001652{
Ken Chen73285082007-05-08 00:28:20 -07001653 blk_cleanup_queue(lo->lo_queue);
1654 put_disk(lo->lo_disk);
1655 list_del(&lo->lo_list);
1656 kfree(lo);
1657}
1658
Ken Chena47653f2007-06-08 13:46:44 -07001659static struct loop_device *loop_init_one(int i)
1660{
1661 struct loop_device *lo;
1662
1663 list_for_each_entry(lo, &loop_devices, lo_list) {
1664 if (lo->lo_number == i)
1665 return lo;
1666 }
1667
1668 lo = loop_alloc(i);
1669 if (lo) {
1670 add_disk(lo->lo_disk);
1671 list_add_tail(&lo->lo_list, &loop_devices);
1672 }
1673 return lo;
1674}
1675
1676static void loop_del_one(struct loop_device *lo)
1677{
1678 del_gendisk(lo->lo_disk);
1679 loop_free(lo);
1680}
1681
Ken Chen73285082007-05-08 00:28:20 -07001682static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1683{
Al Viro705962c2007-05-13 05:52:32 -04001684 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001685 struct kobject *kobj;
Ken Chen73285082007-05-08 00:28:20 -07001686
Al Viro705962c2007-05-13 05:52:32 -04001687 mutex_lock(&loop_devices_mutex);
1688 lo = loop_init_one(dev & MINORMASK);
Al Viro07002e92007-05-12 16:23:15 -04001689 kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
Ken Chen73285082007-05-08 00:28:20 -07001690 mutex_unlock(&loop_devices_mutex);
1691
1692 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001693 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static int __init loop_init(void)
1697{
Ken Chena47653f2007-06-08 13:46:44 -07001698 int i, nr;
1699 unsigned long range;
1700 struct loop_device *lo, *next;
1701
1702 /*
1703 * loop module now has a feature to instantiate underlying device
1704 * structure on-demand, provided that there is an access dev node.
1705 * However, this will not work well with user space tool that doesn't
1706 * know about such "feature". In order to not break any existing
1707 * tool, we do the following:
1708 *
1709 * (1) if max_loop is specified, create that many upfront, and this
1710 * also becomes a hard limit.
1711 * (2) if max_loop is not specified, create 8 loop device on module
1712 * load, user can further extend loop device by create dev node
1713 * themselves and have kernel automatically instantiate actual
1714 * device on-demand.
1715 */
Laurent Vivier476a4812008-03-26 12:11:53 +01001716
1717 part_shift = 0;
1718 if (max_part > 0)
1719 part_shift = fls(max_part);
1720
1721 if (max_loop > 1UL << (MINORBITS - part_shift))
Ken Chena47653f2007-06-08 13:46:44 -07001722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Ken Chen73285082007-05-08 00:28:20 -07001724 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001725 nr = max_loop;
1726 range = max_loop;
1727 } else {
1728 nr = 8;
Laurent Vivier476a4812008-03-26 12:11:53 +01001729 range = 1UL << (MINORBITS - part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Ken Chena47653f2007-06-08 13:46:44 -07001731
1732 if (register_blkdev(LOOP_MAJOR, "loop"))
1733 return -EIO;
1734
1735 for (i = 0; i < nr; i++) {
1736 lo = loop_alloc(i);
1737 if (!lo)
1738 goto Enomem;
1739 list_add_tail(&lo->lo_list, &loop_devices);
1740 }
1741
1742 /* point of no return */
1743
1744 list_for_each_entry(lo, &loop_devices, lo_list)
1745 add_disk(lo->lo_disk);
1746
1747 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1748 THIS_MODULE, loop_probe, NULL, NULL);
1749
Ken Chen73285082007-05-08 00:28:20 -07001750 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07001752
1753Enomem:
1754 printk(KERN_INFO "loop: out of memory\n");
1755
1756 list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
1757 loop_free(lo);
1758
1759 unregister_blkdev(LOOP_MAJOR, "loop");
1760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Ken Chen73285082007-05-08 00:28:20 -07001763static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Ken Chena47653f2007-06-08 13:46:44 -07001765 unsigned long range;
Ken Chen73285082007-05-08 00:28:20 -07001766 struct loop_device *lo, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Laurent Vivier476a4812008-03-26 12:11:53 +01001768 range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift);
Ken Chena47653f2007-06-08 13:46:44 -07001769
Ken Chen73285082007-05-08 00:28:20 -07001770 list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
1771 loop_del_one(lo);
1772
Ken Chena47653f2007-06-08 13:46:44 -07001773 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001774 unregister_blkdev(LOOP_MAJOR, "loop");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777module_init(loop_init);
1778module_exit(loop_exit);
1779
1780#ifndef MODULE
1781static int __init max_loop_setup(char *str)
1782{
1783 max_loop = simple_strtol(str, NULL, 0);
1784 return 1;
1785}
1786
1787__setup("max_loop=", max_loop_setup);
1788#endif