blob: a5f993ac28ddce4e4bf670b06ebc8c321011bdc3 [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.
32 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
33 *
34 * Completely rewrite request handling to be make_request_fn style and
35 * non blocking, pushing work to a helper thread. Lots of fixes from
36 * Al Viro too.
37 * Jens Axboe <axboe@suse.de>, Nov 2000
38 *
39 * Support up to 256 loop devices
40 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
41 *
42 * Support for falling back on the write file operation when the address space
43 * operations prepare_write and/or commit_write are not available on the
44 * backing filesystem.
45 * Anton Altaparmakov, 16 Feb 2005
46 *
47 * Still To Fix:
48 * - Advisory locking is ignored here.
49 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
50 *
51 */
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/module.h>
54#include <linux/moduleparam.h>
55#include <linux/sched.h>
56#include <linux/fs.h>
57#include <linux/file.h>
58#include <linux/stat.h>
59#include <linux/errno.h>
60#include <linux/major.h>
61#include <linux/wait.h>
62#include <linux/blkdev.h>
63#include <linux/blkpg.h>
64#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/smp_lock.h>
66#include <linux/swap.h>
67#include <linux/slab.h>
68#include <linux/loop.h>
David Howells863d5b822006-08-29 19:06:14 +010069#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/suspend.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070071#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/writeback.h>
73#include <linux/buffer_head.h> /* for invalidate_bdev() */
74#include <linux/completion.h>
75#include <linux/highmem.h>
76#include <linux/gfp.h>
Serge E. Hallyn6c997912006-09-29 01:59:11 -070077#include <linux/kthread.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020078#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#include <asm/uaccess.h>
81
Ken Chen73285082007-05-08 00:28:20 -070082static LIST_HEAD(loop_devices);
83static DEFINE_MUTEX(loop_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Transfer functions
87 */
88static int transfer_none(struct loop_device *lo, int cmd,
89 struct page *raw_page, unsigned raw_off,
90 struct page *loop_page, unsigned loop_off,
91 int size, sector_t real_block)
92{
93 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
94 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
95
96 if (cmd == READ)
97 memcpy(loop_buf, raw_buf, size);
98 else
99 memcpy(raw_buf, loop_buf, size);
100
101 kunmap_atomic(raw_buf, KM_USER0);
102 kunmap_atomic(loop_buf, KM_USER1);
103 cond_resched();
104 return 0;
105}
106
107static int transfer_xor(struct loop_device *lo, int cmd,
108 struct page *raw_page, unsigned raw_off,
109 struct page *loop_page, unsigned loop_off,
110 int size, sector_t real_block)
111{
112 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off;
113 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off;
114 char *in, *out, *key;
115 int i, keysize;
116
117 if (cmd == READ) {
118 in = raw_buf;
119 out = loop_buf;
120 } else {
121 in = loop_buf;
122 out = raw_buf;
123 }
124
125 key = lo->lo_encrypt_key;
126 keysize = lo->lo_encrypt_key_size;
127 for (i = 0; i < size; i++)
128 *out++ = *in++ ^ key[(i & 511) % keysize];
129
130 kunmap_atomic(raw_buf, KM_USER0);
131 kunmap_atomic(loop_buf, KM_USER1);
132 cond_resched();
133 return 0;
134}
135
136static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
137{
138 if (unlikely(info->lo_encrypt_key_size <= 0))
139 return -EINVAL;
140 return 0;
141}
142
143static struct loop_func_table none_funcs = {
144 .number = LO_CRYPT_NONE,
145 .transfer = transfer_none,
146};
147
148static struct loop_func_table xor_funcs = {
149 .number = LO_CRYPT_XOR,
150 .transfer = transfer_xor,
151 .init = xor_init
152};
153
154/* xfer_funcs[0] is special - its release function is never called */
155static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
156 &none_funcs,
157 &xor_funcs
158};
159
160static loff_t get_loop_size(struct loop_device *lo, struct file *file)
161{
162 loff_t size, offset, loopsize;
163
164 /* Compute loopsize in bytes */
165 size = i_size_read(file->f_mapping->host);
166 offset = lo->lo_offset;
167 loopsize = size - offset;
168 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
169 loopsize = lo->lo_sizelimit;
170
171 /*
172 * Unfortunately, if we want to do I/O on the device,
173 * the number of 512-byte sectors has to fit into a sector_t.
174 */
175 return loopsize >> 9;
176}
177
178static int
179figure_loop_size(struct loop_device *lo)
180{
181 loff_t size = get_loop_size(lo, lo->lo_backing_file);
182 sector_t x = (sector_t)size;
183
184 if (unlikely((loff_t)x != size))
185 return -EFBIG;
186
Ken Chen73285082007-05-08 00:28:20 -0700187 set_capacity(lo->lo_disk, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189}
190
191static inline int
192lo_do_transfer(struct loop_device *lo, int cmd,
193 struct page *rpage, unsigned roffs,
194 struct page *lpage, unsigned loffs,
195 int size, sector_t rblock)
196{
197 if (unlikely(!lo->transfer))
198 return 0;
199
200 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
201}
202
203/**
204 * do_lo_send_aops - helper for writing data to a loop device
205 *
206 * This is the fast version for backing filesystems which implement the address
Nick Pigginafddba42007-10-16 01:25:01 -0700207 * space operations write_begin and write_end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec,
Nick Pigginafddba42007-10-16 01:25:01 -0700210 int bsize, loff_t pos, struct page *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
213 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 pgoff_t index;
215 unsigned offset, bv_offs;
Zach Brown994fc28c2005-12-15 14:28:17 -0800216 int len, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800218 mutex_lock(&mapping->host->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 index = pos >> PAGE_CACHE_SHIFT;
220 offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
221 bv_offs = bvec->bv_offset;
222 len = bvec->bv_len;
223 while (len > 0) {
224 sector_t IV;
Nick Pigginafddba42007-10-16 01:25:01 -0700225 unsigned size, copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int transfer_result;
Nick Pigginafddba42007-10-16 01:25:01 -0700227 struct page *page;
228 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
231 size = PAGE_CACHE_SIZE - offset;
232 if (size > len)
233 size = len;
Nick Pigginafddba42007-10-16 01:25:01 -0700234
235 ret = pagecache_write_begin(file, mapping, pos, size, 0,
236 &page, &fsdata);
237 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 transfer_result = lo_do_transfer(lo, WRITE, page, offset,
241 bvec->bv_page, bv_offs, size, IV);
Nick Pigginafddba42007-10-16 01:25:01 -0700242 copied = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (unlikely(transfer_result))
Nick Pigginafddba42007-10-16 01:25:01 -0700244 copied = 0;
245
246 ret = pagecache_write_end(file, mapping, pos, size, copied,
247 page, fsdata);
248 if (ret < 0)
249 goto fail;
250 if (ret < copied)
251 copied = ret;
252
253 if (unlikely(transfer_result))
254 goto fail;
255
256 bv_offs += copied;
257 len -= copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 offset = 0;
259 index++;
Nick Pigginafddba42007-10-16 01:25:01 -0700260 pos += copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Zach Brown994fc28c2005-12-15 14:28:17 -0800262 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800264 mutex_unlock(&mapping->host->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266fail:
267 ret = -1;
268 goto out;
269}
270
271/**
272 * __do_lo_send_write - helper for writing data to a loop device
273 *
274 * This helper just factors out common code between do_lo_send_direct_write()
275 * and do_lo_send_write().
276 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800277static int __do_lo_send_write(struct file *file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100278 u8 *buf, const int len, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 ssize_t bw;
281 mm_segment_t old_fs = get_fs();
282
283 set_fs(get_ds());
284 bw = file->f_op->write(file, buf, len, &pos);
285 set_fs(old_fs);
286 if (likely(bw == len))
287 return 0;
288 printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
289 (unsigned long long)pos, len);
290 if (bw >= 0)
291 bw = -EIO;
292 return bw;
293}
294
295/**
296 * do_lo_send_direct_write - helper for writing data to a loop device
297 *
298 * This is the fast, non-transforming version for backing filesystems which do
Nick Pigginafddba42007-10-16 01:25:01 -0700299 * not implement the address space operations write_begin and write_end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * It uses the write file operation which should be present on all writeable
301 * filesystems.
302 */
303static int do_lo_send_direct_write(struct loop_device *lo,
304 struct bio_vec *bvec, int bsize, loff_t pos, struct page *page)
305{
306 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100307 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 bvec->bv_len, pos);
309 kunmap(bvec->bv_page);
310 cond_resched();
311 return bw;
312}
313
314/**
315 * do_lo_send_write - helper for writing data to a loop device
316 *
317 * This is the slow, transforming version for filesystems which do not
Nick Pigginafddba42007-10-16 01:25:01 -0700318 * implement the address space operations write_begin and write_end. It
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * uses the write file operation which should be present on all writeable
320 * filesystems.
321 *
322 * Using fops->write is slower than using aops->{prepare,commit}_write in the
323 * transforming case because we need to double buffer the data as we cannot do
324 * the transformations in place as we do not have direct access to the
325 * destination pages of the backing file.
326 */
327static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
328 int bsize, loff_t pos, struct page *page)
329{
330 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
331 bvec->bv_offset, bvec->bv_len, pos >> 9);
332 if (likely(!ret))
333 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100334 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 pos);
336 printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
337 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
338 if (ret > 0)
339 ret = -EIO;
340 return ret;
341}
342
343static int lo_send(struct loop_device *lo, struct bio *bio, int bsize,
344 loff_t pos)
345{
346 int (*do_lo_send)(struct loop_device *, struct bio_vec *, int, loff_t,
347 struct page *page);
348 struct bio_vec *bvec;
349 struct page *page = NULL;
350 int i, ret = 0;
351
352 do_lo_send = do_lo_send_aops;
353 if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) {
354 do_lo_send = do_lo_send_direct_write;
355 if (lo->transfer != transfer_none) {
356 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
357 if (unlikely(!page))
358 goto fail;
359 kmap(page);
360 do_lo_send = do_lo_send_write;
361 }
362 }
363 bio_for_each_segment(bvec, bio, i) {
364 ret = do_lo_send(lo, bvec, bsize, pos, page);
365 if (ret < 0)
366 break;
367 pos += bvec->bv_len;
368 }
369 if (page) {
370 kunmap(page);
371 __free_page(page);
372 }
373out:
374 return ret;
375fail:
376 printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
377 ret = -ENOMEM;
378 goto out;
379}
380
381struct lo_read_data {
382 struct loop_device *lo;
383 struct page *page;
384 unsigned offset;
385 int bsize;
386};
387
388static int
Jens Axboefd582142007-06-12 21:20:37 +0200389lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
390 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jens Axboefd582142007-06-12 21:20:37 +0200392 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200394 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sector_t IV;
Jens Axboefd582142007-06-12 21:20:37 +0200396 size_t size;
397 int 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;
479 if (bio_rw(bio) == WRITE)
480 ret = lo_send(lo, bio, lo->lo_blocksize, pos);
481 else
482 ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
483 return ret;
484}
485
486/*
487 * Add bio to back of pending list
488 */
489static void loop_add_bio(struct loop_device *lo, struct bio *bio)
490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (lo->lo_biotail) {
492 lo->lo_biotail->bi_next = bio;
493 lo->lo_biotail = bio;
494 } else
495 lo->lo_bio = lo->lo_biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * Grab first pending buffer
500 */
501static struct bio *loop_get_bio(struct loop_device *lo)
502{
503 struct bio *bio;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if ((bio = lo->lo_bio)) {
506 if (bio == lo->lo_biotail)
507 lo->lo_biotail = NULL;
508 lo->lo_bio = bio->bi_next;
509 bio->bi_next = NULL;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return bio;
513}
514
Jens Axboe165125e2007-07-24 09:28:11 +0200515static int loop_make_request(struct request_queue *q, struct bio *old_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct loop_device *lo = q->queuedata;
518 int rw = bio_rw(old_bio);
519
Nick Piggin35a82d12005-06-23 00:09:06 -0700520 if (rw == READA)
521 rw = READ;
522
523 BUG_ON(!lo || (rw != READ && rw != WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 spin_lock_irq(&lo->lo_lock);
526 if (lo->lo_state != Lo_bound)
Nick Piggin35a82d12005-06-23 00:09:06 -0700527 goto out;
528 if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
529 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 loop_add_bio(lo, old_bio);
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700531 wake_up(&lo->lo_event);
Nick Piggin35a82d12005-06-23 00:09:06 -0700532 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
Nick Piggin35a82d12005-06-23 00:09:06 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535out:
Nick Piggin35a82d12005-06-23 00:09:06 -0700536 spin_unlock_irq(&lo->lo_lock);
NeilBrown6712ecf2007-09-27 12:47:43 +0200537 bio_io_error(old_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/*
542 * kick off io on the underlying address space
543 */
Jens Axboe165125e2007-07-24 09:28:11 +0200544static void loop_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct loop_device *lo = q->queuedata;
547
548 clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
549 blk_run_address_space(lo->lo_backing_file->f_mapping);
550}
551
552struct switch_request {
553 struct file *file;
554 struct completion wait;
555};
556
557static void do_loop_switch(struct loop_device *, struct switch_request *);
558
559static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (unlikely(!bio->bi_bdev)) {
562 do_loop_switch(lo, bio->bi_private);
563 bio_put(bio);
564 } else {
Nick Piggin35a82d12005-06-23 00:09:06 -0700565 int ret = do_bio_filebacked(lo, bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200566 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568}
569
570/*
571 * worker thread that handles reads/writes to file backed loop devices,
572 * to avoid blocking in our make_request_fn. it also does loop decrypting
573 * on reads for block backed loop, as that is too heavy to do from
574 * b_end_io context where irqs may be disabled.
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700575 *
576 * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
577 * calling kthread_stop(). Therefore once kthread_should_stop() is
578 * true, make_request will not place any more requests. Therefore
579 * once kthread_should_stop() is true and lo_bio is NULL, we are
580 * done with the loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582static int loop_thread(void *data)
583{
584 struct loop_device *lo = data;
585 struct bio *bio;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 set_user_nice(current, -20);
588
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700589 while (!kthread_should_stop() || lo->lo_bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700591 wait_event_interruptible(lo->lo_event,
592 lo->lo_bio || kthread_should_stop());
Linus Torvalds09c0dc62006-06-26 11:55:42 -0700593
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700594 if (!lo->lo_bio)
Nick Piggin35a82d12005-06-23 00:09:06 -0700595 continue;
Nick Piggin35a82d12005-06-23 00:09:06 -0700596 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 bio = loop_get_bio(lo);
Nick Piggin35a82d12005-06-23 00:09:06 -0700598 spin_unlock_irq(&lo->lo_lock);
599
600 BUG_ON(!bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 loop_handle_bio(lo, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605}
606
607/*
608 * loop_switch performs the hard work of switching a backing store.
609 * First it needs to flush existing IO, it does this by sending a magic
610 * BIO down the pipe. The completion of this BIO does the actual switch.
611 */
612static int loop_switch(struct loop_device *lo, struct file *file)
613{
614 struct switch_request w;
615 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
616 if (!bio)
617 return -ENOMEM;
618 init_completion(&w.wait);
619 w.file = file;
620 bio->bi_private = &w;
621 bio->bi_bdev = NULL;
622 loop_make_request(lo->lo_queue, bio);
623 wait_for_completion(&w.wait);
624 return 0;
625}
626
627/*
628 * Do the actual switch; called from the BIO completion routine
629 */
630static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
631{
632 struct file *file = p->file;
633 struct file *old_file = lo->lo_backing_file;
634 struct address_space *mapping = file->f_mapping;
635
636 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
637 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700638 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
639 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 lo->old_gfp_mask = mapping_gfp_mask(mapping);
641 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
642 complete(&p->wait);
643}
644
645
646/*
647 * loop_change_fd switched the backing store of a loopback device to
648 * a new file. This is useful for operating system installers to free up
649 * the original file and in High Availability environments to switch to
650 * an alternative location for the content in case of server meltdown.
651 * This can only work if the loop device is used read-only, and if the
652 * new backing store is the same size and type as the old backing store.
653 */
654static int loop_change_fd(struct loop_device *lo, struct file *lo_file,
655 struct block_device *bdev, unsigned int arg)
656{
657 struct file *file, *old_file;
658 struct inode *inode;
659 int error;
660
661 error = -ENXIO;
662 if (lo->lo_state != Lo_bound)
663 goto out;
664
665 /* the loop device has to be read-only */
666 error = -EINVAL;
667 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
668 goto out;
669
670 error = -EBADF;
671 file = fget(arg);
672 if (!file)
673 goto out;
674
675 inode = file->f_mapping->host;
676 old_file = lo->lo_backing_file;
677
678 error = -EINVAL;
679
680 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
681 goto out_putf;
682
Jens Axboefd582142007-06-12 21:20:37 +0200683 /* new backing store needs to support loop (eg splice_read) */
684 if (!inode->i_fop->splice_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto out_putf;
686
687 /* size of the new backing store needs to be the same */
688 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
689 goto out_putf;
690
691 /* and ... switch */
692 error = loop_switch(lo, file);
693 if (error)
694 goto out_putf;
695
696 fput(old_file);
697 return 0;
698
699 out_putf:
700 fput(file);
701 out:
702 return error;
703}
704
705static inline int is_loop_device(struct file *file)
706{
707 struct inode *i = file->f_mapping->host;
708
709 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
710}
711
712static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
713 struct block_device *bdev, unsigned int arg)
714{
715 struct file *file, *f;
716 struct inode *inode;
717 struct address_space *mapping;
718 unsigned lo_blocksize;
719 int lo_flags = 0;
720 int error;
721 loff_t size;
722
723 /* This is safe, since we have a reference from open(). */
724 __module_get(THIS_MODULE);
725
726 error = -EBADF;
727 file = fget(arg);
728 if (!file)
729 goto out;
730
731 error = -EBUSY;
732 if (lo->lo_state != Lo_unbound)
733 goto out_putf;
734
735 /* Avoid recursion */
736 f = file;
737 while (is_loop_device(f)) {
738 struct loop_device *l;
739
740 if (f->f_mapping->host->i_rdev == lo_file->f_mapping->host->i_rdev)
741 goto out_putf;
742
743 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
744 if (l->lo_state == Lo_unbound) {
745 error = -EINVAL;
746 goto out_putf;
747 }
748 f = l->lo_backing_file;
749 }
750
751 mapping = file->f_mapping;
752 inode = mapping->host;
753
754 if (!(file->f_mode & FMODE_WRITE))
755 lo_flags |= LO_FLAGS_READ_ONLY;
756
757 error = -EINVAL;
758 if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700759 const struct address_space_operations *aops = mapping->a_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * If we can't read - sorry. If we only can't write - well,
762 * it's going to be read-only.
763 */
Jens Axboefd582142007-06-12 21:20:37 +0200764 if (!file->f_op->splice_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out_putf;
Nick Pigginafddba42007-10-16 01:25:01 -0700766 if (aops->prepare_write || aops->write_begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 lo_flags |= LO_FLAGS_USE_AOPS;
768 if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write)
769 lo_flags |= LO_FLAGS_READ_ONLY;
770
Theodore Ts'oba52de12006-09-27 01:50:49 -0700771 lo_blocksize = S_ISBLK(inode->i_mode) ?
772 inode->i_bdev->bd_block_size : PAGE_SIZE;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 error = 0;
775 } else {
776 goto out_putf;
777 }
778
779 size = get_loop_size(lo, file);
780
781 if ((loff_t)(sector_t)size != size) {
782 error = -EFBIG;
783 goto out_putf;
784 }
785
786 if (!(lo_file->f_mode & FMODE_WRITE))
787 lo_flags |= LO_FLAGS_READ_ONLY;
788
789 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
790
791 lo->lo_blocksize = lo_blocksize;
792 lo->lo_device = bdev;
793 lo->lo_flags = lo_flags;
794 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700795 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 lo->ioctl = NULL;
797 lo->lo_sizelimit = 0;
798 lo->old_gfp_mask = mapping_gfp_mask(mapping);
799 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
800
801 lo->lo_bio = lo->lo_biotail = NULL;
802
803 /*
804 * set queue make_request_fn, and add limits based on lower level
805 * device
806 */
807 blk_queue_make_request(lo->lo_queue, loop_make_request);
808 lo->lo_queue->queuedata = lo;
809 lo->lo_queue->unplug_fn = loop_unplug;
810
Ken Chen73285082007-05-08 00:28:20 -0700811 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 bd_set_size(bdev, size << 9);
813
814 set_blocksize(bdev, lo_blocksize);
815
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700816 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
817 lo->lo_number);
818 if (IS_ERR(lo->lo_thread)) {
819 error = PTR_ERR(lo->lo_thread);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700820 goto out_clr;
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700821 }
822 lo->lo_state = Lo_bound;
823 wake_up_process(lo->lo_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700826out_clr:
827 lo->lo_thread = NULL;
828 lo->lo_device = NULL;
829 lo->lo_backing_file = NULL;
830 lo->lo_flags = 0;
Ken Chen73285082007-05-08 00:28:20 -0700831 set_capacity(lo->lo_disk, 0);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700832 invalidate_bdev(bdev);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700833 bd_set_size(bdev, 0);
834 mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
835 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 out_putf:
837 fput(file);
838 out:
839 /* This is safe: open() is still holding a reference. */
840 module_put(THIS_MODULE);
841 return error;
842}
843
844static int
845loop_release_xfer(struct loop_device *lo)
846{
847 int err = 0;
848 struct loop_func_table *xfer = lo->lo_encryption;
849
850 if (xfer) {
851 if (xfer->release)
852 err = xfer->release(lo);
853 lo->transfer = NULL;
854 lo->lo_encryption = NULL;
855 module_put(xfer->owner);
856 }
857 return err;
858}
859
860static int
861loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
862 const struct loop_info64 *i)
863{
864 int err = 0;
865
866 if (xfer) {
867 struct module *owner = xfer->owner;
868
869 if (!try_module_get(owner))
870 return -EINVAL;
871 if (xfer->init)
872 err = xfer->init(lo, i);
873 if (err)
874 module_put(owner);
875 else
876 lo->lo_encryption = xfer;
877 }
878 return err;
879}
880
881static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
882{
883 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -0400884 gfp_t gfp = lo->old_gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (lo->lo_state != Lo_bound)
887 return -ENXIO;
888
889 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
890 return -EBUSY;
891
892 if (filp == NULL)
893 return -EINVAL;
894
895 spin_lock_irq(&lo->lo_lock);
896 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 spin_unlock_irq(&lo->lo_lock);
898
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700899 kthread_stop(lo->lo_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 lo->lo_backing_file = NULL;
902
903 loop_release_xfer(lo);
904 lo->transfer = NULL;
905 lo->ioctl = NULL;
906 lo->lo_device = NULL;
907 lo->lo_encryption = NULL;
908 lo->lo_offset = 0;
909 lo->lo_sizelimit = 0;
910 lo->lo_encrypt_key_size = 0;
911 lo->lo_flags = 0;
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700912 lo->lo_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
914 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
915 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700916 invalidate_bdev(bdev);
Ken Chen73285082007-05-08 00:28:20 -0700917 set_capacity(lo->lo_disk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 bd_set_size(bdev, 0);
919 mapping_set_gfp_mask(filp->f_mapping, gfp);
920 lo->lo_state = Lo_unbound;
921 fput(filp);
922 /* This is safe: open() is still holding a reference. */
923 module_put(THIS_MODULE);
924 return 0;
925}
926
927static int
928loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
929{
930 int err;
931 struct loop_func_table *xfer;
932
933 if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
934 !capable(CAP_SYS_ADMIN))
935 return -EPERM;
936 if (lo->lo_state != Lo_bound)
937 return -ENXIO;
938 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
939 return -EINVAL;
940
941 err = loop_release_xfer(lo);
942 if (err)
943 return err;
944
945 if (info->lo_encrypt_type) {
946 unsigned int type = info->lo_encrypt_type;
947
948 if (type >= MAX_LO_CRYPT)
949 return -EINVAL;
950 xfer = xfer_funcs[type];
951 if (xfer == NULL)
952 return -EINVAL;
953 } else
954 xfer = NULL;
955
956 err = loop_init_xfer(lo, xfer, info);
957 if (err)
958 return err;
959
960 if (lo->lo_offset != info->lo_offset ||
961 lo->lo_sizelimit != info->lo_sizelimit) {
962 lo->lo_offset = info->lo_offset;
963 lo->lo_sizelimit = info->lo_sizelimit;
964 if (figure_loop_size(lo))
965 return -EFBIG;
966 }
967
968 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
969 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
970 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
971 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
972
973 if (!xfer)
974 xfer = &none_funcs;
975 lo->transfer = xfer->transfer;
976 lo->ioctl = xfer->ioctl;
977
978 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
979 lo->lo_init[0] = info->lo_init[0];
980 lo->lo_init[1] = info->lo_init[1];
981 if (info->lo_encrypt_key_size) {
982 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
983 info->lo_encrypt_key_size);
984 lo->lo_key_owner = current->uid;
985 }
986
987 return 0;
988}
989
990static int
991loop_get_status(struct loop_device *lo, struct loop_info64 *info)
992{
993 struct file *file = lo->lo_backing_file;
994 struct kstat stat;
995 int error;
996
997 if (lo->lo_state != Lo_bound)
998 return -ENXIO;
Josef Sipek6c648be2006-12-08 02:36:55 -0800999 error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (error)
1001 return error;
1002 memset(info, 0, sizeof(*info));
1003 info->lo_number = lo->lo_number;
1004 info->lo_device = huge_encode_dev(stat.dev);
1005 info->lo_inode = stat.ino;
1006 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1007 info->lo_offset = lo->lo_offset;
1008 info->lo_sizelimit = lo->lo_sizelimit;
1009 info->lo_flags = lo->lo_flags;
1010 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1011 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1012 info->lo_encrypt_type =
1013 lo->lo_encryption ? lo->lo_encryption->number : 0;
1014 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1015 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1016 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1017 lo->lo_encrypt_key_size);
1018 }
1019 return 0;
1020}
1021
1022static void
1023loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1024{
1025 memset(info64, 0, sizeof(*info64));
1026 info64->lo_number = info->lo_number;
1027 info64->lo_device = info->lo_device;
1028 info64->lo_inode = info->lo_inode;
1029 info64->lo_rdevice = info->lo_rdevice;
1030 info64->lo_offset = info->lo_offset;
1031 info64->lo_sizelimit = 0;
1032 info64->lo_encrypt_type = info->lo_encrypt_type;
1033 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1034 info64->lo_flags = info->lo_flags;
1035 info64->lo_init[0] = info->lo_init[0];
1036 info64->lo_init[1] = info->lo_init[1];
1037 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1038 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1039 else
1040 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1041 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1042}
1043
1044static int
1045loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1046{
1047 memset(info, 0, sizeof(*info));
1048 info->lo_number = info64->lo_number;
1049 info->lo_device = info64->lo_device;
1050 info->lo_inode = info64->lo_inode;
1051 info->lo_rdevice = info64->lo_rdevice;
1052 info->lo_offset = info64->lo_offset;
1053 info->lo_encrypt_type = info64->lo_encrypt_type;
1054 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1055 info->lo_flags = info64->lo_flags;
1056 info->lo_init[0] = info64->lo_init[0];
1057 info->lo_init[1] = info64->lo_init[1];
1058 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1059 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1060 else
1061 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1062 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1063
1064 /* error in case values were truncated */
1065 if (info->lo_device != info64->lo_device ||
1066 info->lo_rdevice != info64->lo_rdevice ||
1067 info->lo_inode != info64->lo_inode ||
1068 info->lo_offset != info64->lo_offset)
1069 return -EOVERFLOW;
1070
1071 return 0;
1072}
1073
1074static int
1075loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1076{
1077 struct loop_info info;
1078 struct loop_info64 info64;
1079
1080 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1081 return -EFAULT;
1082 loop_info64_from_old(&info, &info64);
1083 return loop_set_status(lo, &info64);
1084}
1085
1086static int
1087loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1088{
1089 struct loop_info64 info64;
1090
1091 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1092 return -EFAULT;
1093 return loop_set_status(lo, &info64);
1094}
1095
1096static int
1097loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1098 struct loop_info info;
1099 struct loop_info64 info64;
1100 int err = 0;
1101
1102 if (!arg)
1103 err = -EINVAL;
1104 if (!err)
1105 err = loop_get_status(lo, &info64);
1106 if (!err)
1107 err = loop_info64_to_old(&info64, &info);
1108 if (!err && copy_to_user(arg, &info, sizeof(info)))
1109 err = -EFAULT;
1110
1111 return err;
1112}
1113
1114static int
1115loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1116 struct loop_info64 info64;
1117 int err = 0;
1118
1119 if (!arg)
1120 err = -EINVAL;
1121 if (!err)
1122 err = loop_get_status(lo, &info64);
1123 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1124 err = -EFAULT;
1125
1126 return err;
1127}
1128
1129static int lo_ioctl(struct inode * inode, struct file * file,
1130 unsigned int cmd, unsigned long arg)
1131{
1132 struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
1133 int err;
1134
Ingo Molnarf85221d2006-03-23 03:00:38 -08001135 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 switch (cmd) {
1137 case LOOP_SET_FD:
1138 err = loop_set_fd(lo, file, inode->i_bdev, arg);
1139 break;
1140 case LOOP_CHANGE_FD:
1141 err = loop_change_fd(lo, file, inode->i_bdev, arg);
1142 break;
1143 case LOOP_CLR_FD:
1144 err = loop_clr_fd(lo, inode->i_bdev);
1145 break;
1146 case LOOP_SET_STATUS:
1147 err = loop_set_status_old(lo, (struct loop_info __user *) arg);
1148 break;
1149 case LOOP_GET_STATUS:
1150 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1151 break;
1152 case LOOP_SET_STATUS64:
1153 err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
1154 break;
1155 case LOOP_GET_STATUS64:
1156 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1157 break;
1158 default:
1159 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1160 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001161 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return err;
1163}
1164
David Howells863d5b822006-08-29 19:06:14 +01001165#ifdef CONFIG_COMPAT
1166struct compat_loop_info {
1167 compat_int_t lo_number; /* ioctl r/o */
1168 compat_dev_t lo_device; /* ioctl r/o */
1169 compat_ulong_t lo_inode; /* ioctl r/o */
1170 compat_dev_t lo_rdevice; /* ioctl r/o */
1171 compat_int_t lo_offset;
1172 compat_int_t lo_encrypt_type;
1173 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1174 compat_int_t lo_flags; /* ioctl r/o */
1175 char lo_name[LO_NAME_SIZE];
1176 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1177 compat_ulong_t lo_init[2];
1178 char reserved[4];
1179};
1180
1181/*
1182 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1183 * - noinlined to reduce stack space usage in main part of driver
1184 */
1185static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001186loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001187 struct loop_info64 *info64)
1188{
1189 struct compat_loop_info info;
1190
1191 if (copy_from_user(&info, arg, sizeof(info)))
1192 return -EFAULT;
1193
1194 memset(info64, 0, sizeof(*info64));
1195 info64->lo_number = info.lo_number;
1196 info64->lo_device = info.lo_device;
1197 info64->lo_inode = info.lo_inode;
1198 info64->lo_rdevice = info.lo_rdevice;
1199 info64->lo_offset = info.lo_offset;
1200 info64->lo_sizelimit = 0;
1201 info64->lo_encrypt_type = info.lo_encrypt_type;
1202 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1203 info64->lo_flags = info.lo_flags;
1204 info64->lo_init[0] = info.lo_init[0];
1205 info64->lo_init[1] = info.lo_init[1];
1206 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1207 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1208 else
1209 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1210 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1211 return 0;
1212}
1213
1214/*
1215 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1216 * - noinlined to reduce stack space usage in main part of driver
1217 */
1218static noinline int
1219loop_info64_to_compat(const struct loop_info64 *info64,
1220 struct compat_loop_info __user *arg)
1221{
1222 struct compat_loop_info info;
1223
1224 memset(&info, 0, sizeof(info));
1225 info.lo_number = info64->lo_number;
1226 info.lo_device = info64->lo_device;
1227 info.lo_inode = info64->lo_inode;
1228 info.lo_rdevice = info64->lo_rdevice;
1229 info.lo_offset = info64->lo_offset;
1230 info.lo_encrypt_type = info64->lo_encrypt_type;
1231 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1232 info.lo_flags = info64->lo_flags;
1233 info.lo_init[0] = info64->lo_init[0];
1234 info.lo_init[1] = info64->lo_init[1];
1235 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1236 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1237 else
1238 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1239 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1240
1241 /* error in case values were truncated */
1242 if (info.lo_device != info64->lo_device ||
1243 info.lo_rdevice != info64->lo_rdevice ||
1244 info.lo_inode != info64->lo_inode ||
1245 info.lo_offset != info64->lo_offset ||
1246 info.lo_init[0] != info64->lo_init[0] ||
1247 info.lo_init[1] != info64->lo_init[1])
1248 return -EOVERFLOW;
1249
1250 if (copy_to_user(arg, &info, sizeof(info)))
1251 return -EFAULT;
1252 return 0;
1253}
1254
1255static int
1256loop_set_status_compat(struct loop_device *lo,
1257 const struct compat_loop_info __user *arg)
1258{
1259 struct loop_info64 info64;
1260 int ret;
1261
1262 ret = loop_info64_from_compat(arg, &info64);
1263 if (ret < 0)
1264 return ret;
1265 return loop_set_status(lo, &info64);
1266}
1267
1268static int
1269loop_get_status_compat(struct loop_device *lo,
1270 struct compat_loop_info __user *arg)
1271{
1272 struct loop_info64 info64;
1273 int err = 0;
1274
1275 if (!arg)
1276 err = -EINVAL;
1277 if (!err)
1278 err = loop_get_status(lo, &info64);
1279 if (!err)
1280 err = loop_info64_to_compat(&info64, arg);
1281 return err;
1282}
1283
1284static long lo_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1285{
Josef Sipek6c648be2006-12-08 02:36:55 -08001286 struct inode *inode = file->f_path.dentry->d_inode;
David Howells863d5b822006-08-29 19:06:14 +01001287 struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
1288 int err;
1289
1290 lock_kernel();
1291 switch(cmd) {
1292 case LOOP_SET_STATUS:
1293 mutex_lock(&lo->lo_ctl_mutex);
1294 err = loop_set_status_compat(
1295 lo, (const struct compat_loop_info __user *) arg);
1296 mutex_unlock(&lo->lo_ctl_mutex);
1297 break;
1298 case LOOP_GET_STATUS:
1299 mutex_lock(&lo->lo_ctl_mutex);
1300 err = loop_get_status_compat(
1301 lo, (struct compat_loop_info __user *) arg);
1302 mutex_unlock(&lo->lo_ctl_mutex);
1303 break;
1304 case LOOP_CLR_FD:
1305 case LOOP_GET_STATUS64:
1306 case LOOP_SET_STATUS64:
1307 arg = (unsigned long) compat_ptr(arg);
1308 case LOOP_SET_FD:
1309 case LOOP_CHANGE_FD:
1310 err = lo_ioctl(inode, file, cmd, arg);
1311 break;
1312 default:
1313 err = -ENOIOCTLCMD;
1314 break;
1315 }
1316 unlock_kernel();
1317 return err;
1318}
1319#endif
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321static int lo_open(struct inode *inode, struct file *file)
1322{
1323 struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
1324
Ingo Molnarf85221d2006-03-23 03:00:38 -08001325 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001327 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 return 0;
1330}
1331
1332static int lo_release(struct inode *inode, struct file *file)
1333{
1334 struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
1335
Ingo Molnarf85221d2006-03-23 03:00:38 -08001336 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 --lo->lo_refcnt;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001338 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 return 0;
1341}
1342
1343static struct block_device_operations lo_fops = {
1344 .owner = THIS_MODULE,
1345 .open = lo_open,
1346 .release = lo_release,
1347 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001348#ifdef CONFIG_COMPAT
1349 .compat_ioctl = lo_compat_ioctl,
1350#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351};
1352
1353/*
1354 * And now the modules code and kernel interface.
1355 */
Ken Chen73285082007-05-08 00:28:20 -07001356static int max_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357module_param(max_loop, int, 0);
Ken Chena47653f2007-06-08 13:46:44 -07001358MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359MODULE_LICENSE("GPL");
1360MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1361
1362int loop_register_transfer(struct loop_func_table *funcs)
1363{
1364 unsigned int n = funcs->number;
1365
1366 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1367 return -EINVAL;
1368 xfer_funcs[n] = funcs;
1369 return 0;
1370}
1371
1372int loop_unregister_transfer(int number)
1373{
1374 unsigned int n = number;
1375 struct loop_device *lo;
1376 struct loop_func_table *xfer;
1377
1378 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1379 return -EINVAL;
1380
1381 xfer_funcs[n] = NULL;
1382
Ken Chen73285082007-05-08 00:28:20 -07001383 list_for_each_entry(lo, &loop_devices, lo_list) {
Ingo Molnarf85221d2006-03-23 03:00:38 -08001384 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (lo->lo_encryption == xfer)
1387 loop_release_xfer(lo);
1388
Ingo Molnarf85221d2006-03-23 03:00:38 -08001389 mutex_unlock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 return 0;
1393}
1394
1395EXPORT_SYMBOL(loop_register_transfer);
1396EXPORT_SYMBOL(loop_unregister_transfer);
1397
Ken Chena47653f2007-06-08 13:46:44 -07001398static struct loop_device *loop_alloc(int i)
Ken Chen73285082007-05-08 00:28:20 -07001399{
1400 struct loop_device *lo;
1401 struct gendisk *disk;
1402
1403 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
1404 if (!lo)
1405 goto out;
1406
1407 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
1408 if (!lo->lo_queue)
1409 goto out_free_dev;
1410
1411 disk = lo->lo_disk = alloc_disk(1);
1412 if (!disk)
1413 goto out_free_queue;
1414
1415 mutex_init(&lo->lo_ctl_mutex);
1416 lo->lo_number = i;
1417 lo->lo_thread = NULL;
1418 init_waitqueue_head(&lo->lo_event);
1419 spin_lock_init(&lo->lo_lock);
1420 disk->major = LOOP_MAJOR;
1421 disk->first_minor = i;
1422 disk->fops = &lo_fops;
1423 disk->private_data = lo;
1424 disk->queue = lo->lo_queue;
1425 sprintf(disk->disk_name, "loop%d", i);
Ken Chen73285082007-05-08 00:28:20 -07001426 return lo;
1427
1428out_free_queue:
1429 blk_cleanup_queue(lo->lo_queue);
1430out_free_dev:
1431 kfree(lo);
1432out:
Al Viro07002e92007-05-12 16:23:15 -04001433 return NULL;
Ken Chen73285082007-05-08 00:28:20 -07001434}
1435
Ken Chena47653f2007-06-08 13:46:44 -07001436static void loop_free(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001437{
Ken Chen73285082007-05-08 00:28:20 -07001438 blk_cleanup_queue(lo->lo_queue);
1439 put_disk(lo->lo_disk);
1440 list_del(&lo->lo_list);
1441 kfree(lo);
1442}
1443
Ken Chena47653f2007-06-08 13:46:44 -07001444static struct loop_device *loop_init_one(int i)
1445{
1446 struct loop_device *lo;
1447
1448 list_for_each_entry(lo, &loop_devices, lo_list) {
1449 if (lo->lo_number == i)
1450 return lo;
1451 }
1452
1453 lo = loop_alloc(i);
1454 if (lo) {
1455 add_disk(lo->lo_disk);
1456 list_add_tail(&lo->lo_list, &loop_devices);
1457 }
1458 return lo;
1459}
1460
1461static void loop_del_one(struct loop_device *lo)
1462{
1463 del_gendisk(lo->lo_disk);
1464 loop_free(lo);
1465}
1466
Ken Chen73285082007-05-08 00:28:20 -07001467static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1468{
Al Viro705962c2007-05-13 05:52:32 -04001469 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001470 struct kobject *kobj;
Ken Chen73285082007-05-08 00:28:20 -07001471
Al Viro705962c2007-05-13 05:52:32 -04001472 mutex_lock(&loop_devices_mutex);
1473 lo = loop_init_one(dev & MINORMASK);
Al Viro07002e92007-05-12 16:23:15 -04001474 kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
Ken Chen73285082007-05-08 00:28:20 -07001475 mutex_unlock(&loop_devices_mutex);
1476
1477 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001478 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481static int __init loop_init(void)
1482{
Ken Chena47653f2007-06-08 13:46:44 -07001483 int i, nr;
1484 unsigned long range;
1485 struct loop_device *lo, *next;
1486
1487 /*
1488 * loop module now has a feature to instantiate underlying device
1489 * structure on-demand, provided that there is an access dev node.
1490 * However, this will not work well with user space tool that doesn't
1491 * know about such "feature". In order to not break any existing
1492 * tool, we do the following:
1493 *
1494 * (1) if max_loop is specified, create that many upfront, and this
1495 * also becomes a hard limit.
1496 * (2) if max_loop is not specified, create 8 loop device on module
1497 * load, user can further extend loop device by create dev node
1498 * themselves and have kernel automatically instantiate actual
1499 * device on-demand.
1500 */
1501 if (max_loop > 1UL << MINORBITS)
1502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Ken Chen73285082007-05-08 00:28:20 -07001504 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001505 nr = max_loop;
1506 range = max_loop;
1507 } else {
1508 nr = 8;
1509 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Ken Chena47653f2007-06-08 13:46:44 -07001511
1512 if (register_blkdev(LOOP_MAJOR, "loop"))
1513 return -EIO;
1514
1515 for (i = 0; i < nr; i++) {
1516 lo = loop_alloc(i);
1517 if (!lo)
1518 goto Enomem;
1519 list_add_tail(&lo->lo_list, &loop_devices);
1520 }
1521
1522 /* point of no return */
1523
1524 list_for_each_entry(lo, &loop_devices, lo_list)
1525 add_disk(lo->lo_disk);
1526
1527 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1528 THIS_MODULE, loop_probe, NULL, NULL);
1529
Ken Chen73285082007-05-08 00:28:20 -07001530 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return 0;
Ken Chena47653f2007-06-08 13:46:44 -07001532
1533Enomem:
1534 printk(KERN_INFO "loop: out of memory\n");
1535
1536 list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
1537 loop_free(lo);
1538
1539 unregister_blkdev(LOOP_MAJOR, "loop");
1540 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Ken Chen73285082007-05-08 00:28:20 -07001543static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Ken Chena47653f2007-06-08 13:46:44 -07001545 unsigned long range;
Ken Chen73285082007-05-08 00:28:20 -07001546 struct loop_device *lo, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Ken Chena47653f2007-06-08 13:46:44 -07001548 range = max_loop ? max_loop : 1UL << MINORBITS;
1549
Ken Chen73285082007-05-08 00:28:20 -07001550 list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
1551 loop_del_one(lo);
1552
Ken Chena47653f2007-06-08 13:46:44 -07001553 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001554 unregister_blkdev(LOOP_MAJOR, "loop");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557module_init(loop_init);
1558module_exit(loop_exit);
1559
1560#ifndef MODULE
1561static int __init max_loop_setup(char *str)
1562{
1563 max_loop = simple_strtol(str, NULL, 0);
1564 return 1;
1565}
1566
1567__setup("max_loop=", max_loop_setup);
1568#endif