blob: 46cdd69455575f3fb74e0efe3e91dfb1626d8422 [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 Bergmann2a48fc02010-06-02 14:28:52 +020070#include <linux/mutex.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>
Kay Sievers770fe302011-07-31 22:08:04 +020078#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <asm/uaccess.h>
80
Kay Sievers34dd82a2011-07-31 22:08:04 +020081static DEFINE_IDR(loop_index_idr);
82static DEFINE_MUTEX(loop_index_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 kunmap_atomic(loop_buf, KM_USER1);
Peter Zijlstra61ecdb802010-10-26 14:21:47 -0700104 kunmap_atomic(raw_buf, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 kunmap_atomic(loop_buf, KM_USER1);
Peter Zijlstra61ecdb802010-10-26 14:21:47 -0700133 kunmap_atomic(raw_buf, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * __do_lo_send_write - helper for writing data to a loop device
207 *
208 * This helper just factors out common code between do_lo_send_direct_write()
209 * and do_lo_send_write().
210 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800211static int __do_lo_send_write(struct file *file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100212 u8 *buf, const int len, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 ssize_t bw;
215 mm_segment_t old_fs = get_fs();
216
217 set_fs(get_ds());
218 bw = file->f_op->write(file, buf, len, &pos);
219 set_fs(old_fs);
220 if (likely(bw == len))
221 return 0;
222 printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
223 (unsigned long long)pos, len);
224 if (bw >= 0)
225 bw = -EIO;
226 return bw;
227}
228
229/**
230 * do_lo_send_direct_write - helper for writing data to a loop device
231 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200232 * This is the fast, non-transforming version that does not need double
233 * buffering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235static int do_lo_send_direct_write(struct loop_device *lo,
Al Viro511de732007-10-08 12:10:13 -0400236 struct bio_vec *bvec, loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100239 kmap(bvec->bv_page) + bvec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 bvec->bv_len, pos);
241 kunmap(bvec->bv_page);
242 cond_resched();
243 return bw;
244}
245
246/**
247 * do_lo_send_write - helper for writing data to a loop device
248 *
Christoph Hellwig456be142011-10-17 12:57:20 +0200249 * This is the slow, transforming version that needs to double buffer the
250 * data as it cannot do the transformations in place without having direct
251 * access to the destination pages of the backing file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
Al Viro511de732007-10-08 12:10:13 -0400254 loff_t pos, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
257 bvec->bv_offset, bvec->bv_len, pos >> 9);
258 if (likely(!ret))
259 return __do_lo_send_write(lo->lo_backing_file,
Al Viro98ae6ccd2006-10-10 22:45:07 +0100260 page_address(page), bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 pos);
262 printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
263 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
264 if (ret > 0)
265 ret = -EIO;
266 return ret;
267}
268
Al Viro511de732007-10-08 12:10:13 -0400269static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Al Viro511de732007-10-08 12:10:13 -0400271 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct page *page);
273 struct bio_vec *bvec;
274 struct page *page = NULL;
275 int i, ret = 0;
276
Christoph Hellwig456be142011-10-17 12:57:20 +0200277 if (lo->transfer != transfer_none) {
278 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
279 if (unlikely(!page))
280 goto fail;
281 kmap(page);
282 do_lo_send = do_lo_send_write;
283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 do_lo_send = do_lo_send_direct_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Christoph Hellwig456be142011-10-17 12:57:20 +0200286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 bio_for_each_segment(bvec, bio, i) {
Al Viro511de732007-10-08 12:10:13 -0400288 ret = do_lo_send(lo, bvec, pos, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (ret < 0)
290 break;
291 pos += bvec->bv_len;
292 }
293 if (page) {
294 kunmap(page);
295 __free_page(page);
296 }
297out:
298 return ret;
299fail:
300 printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
301 ret = -ENOMEM;
302 goto out;
303}
304
305struct lo_read_data {
306 struct loop_device *lo;
307 struct page *page;
308 unsigned offset;
309 int bsize;
310};
311
312static int
Jens Axboefd582142007-06-12 21:20:37 +0200313lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
314 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jens Axboefd582142007-06-12 21:20:37 +0200316 struct lo_read_data *p = sd->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct loop_device *lo = p->lo;
Jens Axboefd582142007-06-12 21:20:37 +0200318 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sector_t IV;
Jens Axboe3603b8e2010-12-20 09:15:19 +0100320 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jens Axboefd582142007-06-12 21:20:37 +0200322 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
323 (buf->offset >> 9);
324 size = sd->len;
325 if (size > p->bsize)
326 size = p->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jens Axboefd582142007-06-12 21:20:37 +0200328 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 printk(KERN_ERR "loop: transfer error block %ld\n",
330 page->index);
Jens Axboefd582142007-06-12 21:20:37 +0200331 size = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 flush_dcache_page(p->page);
335
Jens Axboefd582142007-06-12 21:20:37 +0200336 if (size > 0)
337 p->offset += size;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return size;
340}
341
342static int
Jens Axboefd582142007-06-12 21:20:37 +0200343lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
344{
345 return __splice_from_pipe(pipe, sd, lo_splice_actor);
346}
347
348static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349do_lo_receive(struct loop_device *lo,
350 struct bio_vec *bvec, int bsize, loff_t pos)
351{
352 struct lo_read_data cookie;
Jens Axboefd582142007-06-12 21:20:37 +0200353 struct splice_desc sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct file *file;
Jens Axboefd582142007-06-12 21:20:37 +0200355 long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 cookie.lo = lo;
358 cookie.page = bvec->bv_page;
359 cookie.offset = bvec->bv_offset;
360 cookie.bsize = bsize;
Jens Axboefd582142007-06-12 21:20:37 +0200361
362 sd.len = 0;
363 sd.total_len = bvec->bv_len;
364 sd.flags = 0;
365 sd.pos = pos;
366 sd.u.data = &cookie;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 file = lo->lo_backing_file;
Jens Axboefd582142007-06-12 21:20:37 +0200369 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
370
371 if (retval < 0)
372 return retval;
373
374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static int
378lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
379{
380 struct bio_vec *bvec;
381 int i, ret = 0;
382
383 bio_for_each_segment(bvec, bio, i) {
384 ret = do_lo_receive(lo, bvec, bsize, pos);
385 if (ret < 0)
386 break;
387 pos += bvec->bv_len;
388 }
389 return ret;
390}
391
392static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
393{
394 loff_t pos;
395 int ret;
396
397 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100398
399 if (bio_rw(bio) == WRITE) {
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100400 struct file *file = lo->lo_backing_file;
401
Tejun Heo6259f282010-09-03 11:56:17 +0200402 if (bio->bi_rw & REQ_FLUSH) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100403 ret = vfs_fsync(file, 0);
Tejun Heo6259f282010-09-03 11:56:17 +0200404 if (unlikely(ret && ret != -EINVAL)) {
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100405 ret = -EIO;
406 goto out;
407 }
408 }
409
Al Viro511de732007-10-08 12:10:13 -0400410 ret = lo_send(lo, bio, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100411
Tejun Heo6259f282010-09-03 11:56:17 +0200412 if ((bio->bi_rw & REQ_FUA) && !ret) {
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100413 ret = vfs_fsync(file, 0);
Tejun Heo6259f282010-09-03 11:56:17 +0200414 if (unlikely(ret && ret != -EINVAL))
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100415 ret = -EIO;
416 }
417 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100419
420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return ret;
422}
423
424/*
425 * Add bio to back of pending list
426 */
427static void loop_add_bio(struct loop_device *lo, struct bio *bio)
428{
Akinobu Mitae6863072009-04-17 08:41:21 +0200429 bio_list_add(&lo->lo_bio_list, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/*
433 * Grab first pending buffer
434 */
435static struct bio *loop_get_bio(struct loop_device *lo)
436{
Akinobu Mitae6863072009-04-17 08:41:21 +0200437 return bio_list_pop(&lo->lo_bio_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Jens Axboe165125e2007-07-24 09:28:11 +0200440static int loop_make_request(struct request_queue *q, struct bio *old_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct loop_device *lo = q->queuedata;
443 int rw = bio_rw(old_bio);
444
Nick Piggin35a82d12005-06-23 00:09:06 -0700445 if (rw == READA)
446 rw = READ;
447
448 BUG_ON(!lo || (rw != READ && rw != WRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 spin_lock_irq(&lo->lo_lock);
451 if (lo->lo_state != Lo_bound)
Nick Piggin35a82d12005-06-23 00:09:06 -0700452 goto out;
453 if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 loop_add_bio(lo, old_bio);
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700456 wake_up(&lo->lo_event);
Nick Piggin35a82d12005-06-23 00:09:06 -0700457 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
Nick Piggin35a82d12005-06-23 00:09:06 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460out:
Nick Piggin35a82d12005-06-23 00:09:06 -0700461 spin_unlock_irq(&lo->lo_lock);
NeilBrown6712ecf2007-09-27 12:47:43 +0200462 bio_io_error(old_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466struct switch_request {
467 struct file *file;
468 struct completion wait;
469};
470
471static void do_loop_switch(struct loop_device *, struct switch_request *);
472
473static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (unlikely(!bio->bi_bdev)) {
476 do_loop_switch(lo, bio->bi_private);
477 bio_put(bio);
478 } else {
Nick Piggin35a82d12005-06-23 00:09:06 -0700479 int ret = do_bio_filebacked(lo, bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200480 bio_endio(bio, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482}
483
484/*
485 * worker thread that handles reads/writes to file backed loop devices,
486 * to avoid blocking in our make_request_fn. it also does loop decrypting
487 * on reads for block backed loop, as that is too heavy to do from
488 * b_end_io context where irqs may be disabled.
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700489 *
490 * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
491 * calling kthread_stop(). Therefore once kthread_should_stop() is
492 * true, make_request will not place any more requests. Therefore
493 * once kthread_should_stop() is true and lo_bio is NULL, we are
494 * done with the loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496static int loop_thread(void *data)
497{
498 struct loop_device *lo = data;
499 struct bio *bio;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 set_user_nice(current, -20);
502
Akinobu Mitae6863072009-04-17 08:41:21 +0200503 while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700505 wait_event_interruptible(lo->lo_event,
Akinobu Mitae6863072009-04-17 08:41:21 +0200506 !bio_list_empty(&lo->lo_bio_list) ||
507 kthread_should_stop());
Linus Torvalds09c0dc62006-06-26 11:55:42 -0700508
Akinobu Mitae6863072009-04-17 08:41:21 +0200509 if (bio_list_empty(&lo->lo_bio_list))
Nick Piggin35a82d12005-06-23 00:09:06 -0700510 continue;
Nick Piggin35a82d12005-06-23 00:09:06 -0700511 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 bio = loop_get_bio(lo);
Nick Piggin35a82d12005-06-23 00:09:06 -0700513 spin_unlock_irq(&lo->lo_lock);
514
515 BUG_ON(!bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 loop_handle_bio(lo, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520}
521
522/*
523 * loop_switch performs the hard work of switching a backing store.
524 * First it needs to flush existing IO, it does this by sending a magic
525 * BIO down the pipe. The completion of this BIO does the actual switch.
526 */
527static int loop_switch(struct loop_device *lo, struct file *file)
528{
529 struct switch_request w;
Jens Axboea24eab12008-01-11 10:14:40 +0100530 struct bio *bio = bio_alloc(GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!bio)
532 return -ENOMEM;
533 init_completion(&w.wait);
534 w.file = file;
535 bio->bi_private = &w;
536 bio->bi_bdev = NULL;
537 loop_make_request(lo->lo_queue, bio);
538 wait_for_completion(&w.wait);
539 return 0;
540}
541
542/*
Milan Broz14f27932008-12-12 14:48:27 +0100543 * Helper to flush the IOs in loop, but keeping loop thread running
544 */
545static int loop_flush(struct loop_device *lo)
546{
547 /* loop not yet configured, no running thread, nothing to flush */
548 if (!lo->lo_thread)
549 return 0;
550
551 return loop_switch(lo, NULL);
552}
553
554/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * Do the actual switch; called from the BIO completion routine
556 */
557static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
558{
559 struct file *file = p->file;
560 struct file *old_file = lo->lo_backing_file;
Milan Broz14f27932008-12-12 14:48:27 +0100561 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Milan Broz14f27932008-12-12 14:48:27 +0100563 /* if no new file, only flush of queued bios requested */
564 if (!file)
565 goto out;
566
567 mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
569 lo->lo_backing_file = file;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700570 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
571 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 lo->old_gfp_mask = mapping_gfp_mask(mapping);
573 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
Milan Broz14f27932008-12-12 14:48:27 +0100574out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 complete(&p->wait);
576}
577
578
579/*
580 * loop_change_fd switched the backing store of a loopback device to
581 * a new file. This is useful for operating system installers to free up
582 * the original file and in High Availability environments to switch to
583 * an alternative location for the content in case of server meltdown.
584 * This can only work if the loop device is used read-only, and if the
585 * new backing store is the same size and type as the old backing store.
586 */
Al Virobb214882008-03-02 09:29:48 -0500587static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
588 unsigned int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct file *file, *old_file;
591 struct inode *inode;
592 int error;
593
594 error = -ENXIO;
595 if (lo->lo_state != Lo_bound)
596 goto out;
597
598 /* the loop device has to be read-only */
599 error = -EINVAL;
600 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
601 goto out;
602
603 error = -EBADF;
604 file = fget(arg);
605 if (!file)
606 goto out;
607
608 inode = file->f_mapping->host;
609 old_file = lo->lo_backing_file;
610
611 error = -EINVAL;
612
613 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
614 goto out_putf;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* size of the new backing store needs to be the same */
617 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
618 goto out_putf;
619
620 /* and ... switch */
621 error = loop_switch(lo, file);
622 if (error)
623 goto out_putf;
624
625 fput(old_file);
Laurent Vivier476a4812008-03-26 12:11:53 +0100626 if (max_part > 0)
627 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629
630 out_putf:
631 fput(file);
632 out:
633 return error;
634}
635
636static inline int is_loop_device(struct file *file)
637{
638 struct inode *i = file->f_mapping->host;
639
640 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
641}
642
Milan Brozee862732010-08-23 15:16:00 +0200643/* loop sysfs attributes */
644
645static ssize_t loop_attr_show(struct device *dev, char *page,
646 ssize_t (*callback)(struct loop_device *, char *))
647{
Kay Sievers34dd82a2011-07-31 22:08:04 +0200648 struct gendisk *disk = dev_to_disk(dev);
649 struct loop_device *lo = disk->private_data;
Milan Brozee862732010-08-23 15:16:00 +0200650
Kay Sievers34dd82a2011-07-31 22:08:04 +0200651 return callback(lo, page);
Milan Brozee862732010-08-23 15:16:00 +0200652}
653
654#define LOOP_ATTR_RO(_name) \
655static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
656static ssize_t loop_attr_do_show_##_name(struct device *d, \
657 struct device_attribute *attr, char *b) \
658{ \
659 return loop_attr_show(d, b, loop_attr_##_name##_show); \
660} \
661static struct device_attribute loop_attr_##_name = \
662 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
663
664static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
665{
666 ssize_t ret;
667 char *p = NULL;
668
Kay Sievers05eb0f22011-07-31 22:21:35 +0200669 spin_lock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200670 if (lo->lo_backing_file)
671 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
Kay Sievers05eb0f22011-07-31 22:21:35 +0200672 spin_unlock_irq(&lo->lo_lock);
Milan Brozee862732010-08-23 15:16:00 +0200673
674 if (IS_ERR_OR_NULL(p))
675 ret = PTR_ERR(p);
676 else {
677 ret = strlen(p);
678 memmove(buf, p, ret);
679 buf[ret++] = '\n';
680 buf[ret] = 0;
681 }
682
683 return ret;
684}
685
686static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
687{
688 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
689}
690
691static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
692{
693 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
694}
695
696static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
697{
698 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
699
700 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
701}
702
703LOOP_ATTR_RO(backing_file);
704LOOP_ATTR_RO(offset);
705LOOP_ATTR_RO(sizelimit);
706LOOP_ATTR_RO(autoclear);
707
708static struct attribute *loop_attrs[] = {
709 &loop_attr_backing_file.attr,
710 &loop_attr_offset.attr,
711 &loop_attr_sizelimit.attr,
712 &loop_attr_autoclear.attr,
713 NULL,
714};
715
716static struct attribute_group loop_attribute_group = {
717 .name = "loop",
718 .attrs= loop_attrs,
719};
720
721static int loop_sysfs_init(struct loop_device *lo)
722{
723 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
724 &loop_attribute_group);
725}
726
727static void loop_sysfs_exit(struct loop_device *lo)
728{
729 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
730 &loop_attribute_group);
731}
732
Al Virobb214882008-03-02 09:29:48 -0500733static int loop_set_fd(struct loop_device *lo, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct block_device *bdev, unsigned int arg)
735{
736 struct file *file, *f;
737 struct inode *inode;
738 struct address_space *mapping;
739 unsigned lo_blocksize;
740 int lo_flags = 0;
741 int error;
742 loff_t size;
743
744 /* This is safe, since we have a reference from open(). */
745 __module_get(THIS_MODULE);
746
747 error = -EBADF;
748 file = fget(arg);
749 if (!file)
750 goto out;
751
752 error = -EBUSY;
753 if (lo->lo_state != Lo_unbound)
754 goto out_putf;
755
756 /* Avoid recursion */
757 f = file;
758 while (is_loop_device(f)) {
759 struct loop_device *l;
760
Al Virobb214882008-03-02 09:29:48 -0500761 if (f->f_mapping->host->i_bdev == bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto out_putf;
763
764 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
765 if (l->lo_state == Lo_unbound) {
766 error = -EINVAL;
767 goto out_putf;
768 }
769 f = l->lo_backing_file;
770 }
771
772 mapping = file->f_mapping;
773 inode = mapping->host;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 error = -EINVAL;
Christoph Hellwig456be142011-10-17 12:57:20 +0200776 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Christoph Hellwig456be142011-10-17 12:57:20 +0200779 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
780 !file->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 lo_flags |= LO_FLAGS_READ_ONLY;
782
Christoph Hellwig456be142011-10-17 12:57:20 +0200783 lo_blocksize = S_ISBLK(inode->i_mode) ?
784 inode->i_bdev->bd_block_size : PAGE_SIZE;
785
786 error = -EFBIG;
787 size = get_loop_size(lo, file);
788 if ((loff_t)(sector_t)size != size)
789 goto out_putf;
790
791 error = 0;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
794
795 lo->lo_blocksize = lo_blocksize;
796 lo->lo_device = bdev;
797 lo->lo_flags = lo_flags;
798 lo->lo_backing_file = file;
Constantine Sapuntzakiseefe85e2006-06-23 02:06:08 -0700799 lo->transfer = transfer_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 lo->ioctl = NULL;
801 lo->lo_sizelimit = 0;
802 lo->old_gfp_mask = mapping_gfp_mask(mapping);
803 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
804
Akinobu Mitae6863072009-04-17 08:41:21 +0200805 bio_list_init(&lo->lo_bio_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /*
808 * set queue make_request_fn, and add limits based on lower level
809 * device
810 */
811 blk_queue_make_request(lo->lo_queue, loop_make_request);
812 lo->lo_queue->queuedata = lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100814 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
Tejun Heo4913efe2010-09-03 11:56:16 +0200815 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
Nikanth Karthikesan68db1962009-03-24 12:29:54 +0100816
Ken Chen73285082007-05-08 00:28:20 -0700817 set_capacity(lo->lo_disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 bd_set_size(bdev, size << 9);
Milan Brozee862732010-08-23 15:16:00 +0200819 loop_sysfs_init(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200820 /* let user-space know about the new size */
821 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 set_blocksize(bdev, lo_blocksize);
824
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700825 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
826 lo->lo_number);
827 if (IS_ERR(lo->lo_thread)) {
828 error = PTR_ERR(lo->lo_thread);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700829 goto out_clr;
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700830 }
831 lo->lo_state = Lo_bound;
832 wake_up_process(lo->lo_thread);
Laurent Vivier476a4812008-03-26 12:11:53 +0100833 if (max_part > 0)
834 ioctl_by_bdev(bdev, BLKRRPART, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700837out_clr:
Milan Brozee862732010-08-23 15:16:00 +0200838 loop_sysfs_exit(lo);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700839 lo->lo_thread = NULL;
840 lo->lo_device = NULL;
841 lo->lo_backing_file = NULL;
842 lo->lo_flags = 0;
Ken Chen73285082007-05-08 00:28:20 -0700843 set_capacity(lo->lo_disk, 0);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700844 invalidate_bdev(bdev);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700845 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200846 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
Serge E. Hallyna7422bf2006-09-29 02:01:18 -0700847 mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
848 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 out_putf:
850 fput(file);
851 out:
852 /* This is safe: open() is still holding a reference. */
853 module_put(THIS_MODULE);
854 return error;
855}
856
857static int
858loop_release_xfer(struct loop_device *lo)
859{
860 int err = 0;
861 struct loop_func_table *xfer = lo->lo_encryption;
862
863 if (xfer) {
864 if (xfer->release)
865 err = xfer->release(lo);
866 lo->transfer = NULL;
867 lo->lo_encryption = NULL;
868 module_put(xfer->owner);
869 }
870 return err;
871}
872
873static int
874loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
875 const struct loop_info64 *i)
876{
877 int err = 0;
878
879 if (xfer) {
880 struct module *owner = xfer->owner;
881
882 if (!try_module_get(owner))
883 return -EINVAL;
884 if (xfer->init)
885 err = xfer->init(lo, i);
886 if (err)
887 module_put(owner);
888 else
889 lo->lo_encryption = xfer;
890 }
891 return err;
892}
893
894static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
895{
896 struct file *filp = lo->lo_backing_file;
Al Virob4e3ca12005-10-21 03:22:34 -0400897 gfp_t gfp = lo->old_gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (lo->lo_state != Lo_bound)
900 return -ENXIO;
901
902 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
903 return -EBUSY;
904
905 if (filp == NULL)
906 return -EINVAL;
907
908 spin_lock_irq(&lo->lo_lock);
909 lo->lo_state = Lo_rundown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 spin_unlock_irq(&lo->lo_lock);
911
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700912 kthread_stop(lo->lo_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Kay Sievers05eb0f22011-07-31 22:21:35 +0200914 spin_lock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 lo->lo_backing_file = NULL;
Kay Sievers05eb0f22011-07-31 22:21:35 +0200916 spin_unlock_irq(&lo->lo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 loop_release_xfer(lo);
919 lo->transfer = NULL;
920 lo->ioctl = NULL;
921 lo->lo_device = NULL;
922 lo->lo_encryption = NULL;
923 lo->lo_offset = 0;
924 lo->lo_sizelimit = 0;
925 lo->lo_encrypt_key_size = 0;
926 lo->lo_flags = 0;
Serge E. Hallyn6c997912006-09-29 01:59:11 -0700927 lo->lo_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
929 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
930 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
Al Virobb214882008-03-02 09:29:48 -0500931 if (bdev)
932 invalidate_bdev(bdev);
Ken Chen73285082007-05-08 00:28:20 -0700933 set_capacity(lo->lo_disk, 0);
Milan Broz51a0bb02010-10-27 19:51:30 -0600934 loop_sysfs_exit(lo);
David Zeuthenc3473c62010-05-03 14:08:59 +0200935 if (bdev) {
Al Virobb214882008-03-02 09:29:48 -0500936 bd_set_size(bdev, 0);
David Zeuthenc3473c62010-05-03 14:08:59 +0200937 /* let user-space know about this change */
938 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 mapping_set_gfp_mask(filp->f_mapping, gfp);
941 lo->lo_state = Lo_unbound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* This is safe: open() is still holding a reference. */
943 module_put(THIS_MODULE);
Alexey Dobriyancf6e6932009-10-26 16:49:55 -0700944 if (max_part > 0 && bdev)
Laurent Vivier476a4812008-03-26 12:11:53 +0100945 ioctl_by_bdev(bdev, BLKRRPART, 0);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +0100946 mutex_unlock(&lo->lo_ctl_mutex);
947 /*
948 * Need not hold lo_ctl_mutex to fput backing file.
949 * Calling fput holding lo_ctl_mutex triggers a circular
950 * lock dependency possibility warning as fput can take
951 * bd_mutex which is usually taken before lo_ctl_mutex.
952 */
953 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
957static int
958loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
959{
960 int err;
961 struct loop_func_table *xfer;
David Howellsb0fafa82008-11-14 10:38:41 +1100962 uid_t uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
David Howellsb0fafa82008-11-14 10:38:41 +1100964 if (lo->lo_encrypt_key_size &&
965 lo->lo_key_owner != uid &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 !capable(CAP_SYS_ADMIN))
967 return -EPERM;
968 if (lo->lo_state != Lo_bound)
969 return -ENXIO;
970 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
971 return -EINVAL;
972
973 err = loop_release_xfer(lo);
974 if (err)
975 return err;
976
977 if (info->lo_encrypt_type) {
978 unsigned int type = info->lo_encrypt_type;
979
980 if (type >= MAX_LO_CRYPT)
981 return -EINVAL;
982 xfer = xfer_funcs[type];
983 if (xfer == NULL)
984 return -EINVAL;
985 } else
986 xfer = NULL;
987
988 err = loop_init_xfer(lo, xfer, info);
989 if (err)
990 return err;
991
992 if (lo->lo_offset != info->lo_offset ||
993 lo->lo_sizelimit != info->lo_sizelimit) {
994 lo->lo_offset = info->lo_offset;
995 lo->lo_sizelimit = info->lo_sizelimit;
996 if (figure_loop_size(lo))
997 return -EFBIG;
998 }
999
1000 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1001 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1002 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1003 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1004
1005 if (!xfer)
1006 xfer = &none_funcs;
1007 lo->transfer = xfer->transfer;
1008 lo->ioctl = xfer->ioctl;
1009
David Woodhouse96c58652008-02-06 01:36:27 -08001010 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
1011 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
1012 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1015 lo->lo_init[0] = info->lo_init[0];
1016 lo->lo_init[1] = info->lo_init[1];
1017 if (info->lo_encrypt_key_size) {
1018 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1019 info->lo_encrypt_key_size);
David Howellsb0fafa82008-11-14 10:38:41 +11001020 lo->lo_key_owner = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022
1023 return 0;
1024}
1025
1026static int
1027loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1028{
1029 struct file *file = lo->lo_backing_file;
1030 struct kstat stat;
1031 int error;
1032
1033 if (lo->lo_state != Lo_bound)
1034 return -ENXIO;
Josef Sipek6c648be2006-12-08 02:36:55 -08001035 error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (error)
1037 return error;
1038 memset(info, 0, sizeof(*info));
1039 info->lo_number = lo->lo_number;
1040 info->lo_device = huge_encode_dev(stat.dev);
1041 info->lo_inode = stat.ino;
1042 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1043 info->lo_offset = lo->lo_offset;
1044 info->lo_sizelimit = lo->lo_sizelimit;
1045 info->lo_flags = lo->lo_flags;
1046 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1047 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1048 info->lo_encrypt_type =
1049 lo->lo_encryption ? lo->lo_encryption->number : 0;
1050 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1051 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1052 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1053 lo->lo_encrypt_key_size);
1054 }
1055 return 0;
1056}
1057
1058static void
1059loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1060{
1061 memset(info64, 0, sizeof(*info64));
1062 info64->lo_number = info->lo_number;
1063 info64->lo_device = info->lo_device;
1064 info64->lo_inode = info->lo_inode;
1065 info64->lo_rdevice = info->lo_rdevice;
1066 info64->lo_offset = info->lo_offset;
1067 info64->lo_sizelimit = 0;
1068 info64->lo_encrypt_type = info->lo_encrypt_type;
1069 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1070 info64->lo_flags = info->lo_flags;
1071 info64->lo_init[0] = info->lo_init[0];
1072 info64->lo_init[1] = info->lo_init[1];
1073 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1074 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1075 else
1076 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1077 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1078}
1079
1080static int
1081loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1082{
1083 memset(info, 0, sizeof(*info));
1084 info->lo_number = info64->lo_number;
1085 info->lo_device = info64->lo_device;
1086 info->lo_inode = info64->lo_inode;
1087 info->lo_rdevice = info64->lo_rdevice;
1088 info->lo_offset = info64->lo_offset;
1089 info->lo_encrypt_type = info64->lo_encrypt_type;
1090 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1091 info->lo_flags = info64->lo_flags;
1092 info->lo_init[0] = info64->lo_init[0];
1093 info->lo_init[1] = info64->lo_init[1];
1094 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1095 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1096 else
1097 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1098 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1099
1100 /* error in case values were truncated */
1101 if (info->lo_device != info64->lo_device ||
1102 info->lo_rdevice != info64->lo_rdevice ||
1103 info->lo_inode != info64->lo_inode ||
1104 info->lo_offset != info64->lo_offset)
1105 return -EOVERFLOW;
1106
1107 return 0;
1108}
1109
1110static int
1111loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1112{
1113 struct loop_info info;
1114 struct loop_info64 info64;
1115
1116 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1117 return -EFAULT;
1118 loop_info64_from_old(&info, &info64);
1119 return loop_set_status(lo, &info64);
1120}
1121
1122static int
1123loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1124{
1125 struct loop_info64 info64;
1126
1127 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1128 return -EFAULT;
1129 return loop_set_status(lo, &info64);
1130}
1131
1132static int
1133loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1134 struct loop_info info;
1135 struct loop_info64 info64;
1136 int err = 0;
1137
1138 if (!arg)
1139 err = -EINVAL;
1140 if (!err)
1141 err = loop_get_status(lo, &info64);
1142 if (!err)
1143 err = loop_info64_to_old(&info64, &info);
1144 if (!err && copy_to_user(arg, &info, sizeof(info)))
1145 err = -EFAULT;
1146
1147 return err;
1148}
1149
1150static int
1151loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1152 struct loop_info64 info64;
1153 int err = 0;
1154
1155 if (!arg)
1156 err = -EINVAL;
1157 if (!err)
1158 err = loop_get_status(lo, &info64);
1159 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1160 err = -EFAULT;
1161
1162 return err;
1163}
1164
J. R. Okajima53d66602009-03-31 15:23:43 -07001165static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1166{
1167 int err;
1168 sector_t sec;
1169 loff_t sz;
1170
1171 err = -ENXIO;
1172 if (unlikely(lo->lo_state != Lo_bound))
1173 goto out;
1174 err = figure_loop_size(lo);
1175 if (unlikely(err))
1176 goto out;
1177 sec = get_capacity(lo->lo_disk);
1178 /* the width of sector_t may be narrow for bit-shift */
1179 sz = sec;
1180 sz <<= 9;
1181 mutex_lock(&bdev->bd_mutex);
1182 bd_set_size(bdev, sz);
David Zeuthenc3473c62010-05-03 14:08:59 +02001183 /* let user-space know about the new size */
1184 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
J. R. Okajima53d66602009-03-31 15:23:43 -07001185 mutex_unlock(&bdev->bd_mutex);
1186
1187 out:
1188 return err;
1189}
1190
Al Virobb214882008-03-02 09:29:48 -05001191static int lo_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 unsigned int cmd, unsigned long arg)
1193{
Al Virobb214882008-03-02 09:29:48 -05001194 struct loop_device *lo = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int err;
1196
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001197 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 switch (cmd) {
1199 case LOOP_SET_FD:
Al Virobb214882008-03-02 09:29:48 -05001200 err = loop_set_fd(lo, mode, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001203 err = loop_change_fd(lo, bdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
1205 case LOOP_CLR_FD:
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001206 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
Al Virobb214882008-03-02 09:29:48 -05001207 err = loop_clr_fd(lo, bdev);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001208 if (!err)
1209 goto out_unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 break;
1211 case LOOP_SET_STATUS:
1212 err = loop_set_status_old(lo, (struct loop_info __user *) arg);
1213 break;
1214 case LOOP_GET_STATUS:
1215 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1216 break;
1217 case LOOP_SET_STATUS64:
1218 err = loop_set_status64(lo, (struct loop_info64 __user *) arg);
1219 break;
1220 case LOOP_GET_STATUS64:
1221 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1222 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001223 case LOOP_SET_CAPACITY:
1224 err = -EPERM;
1225 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1226 err = loop_set_capacity(lo, bdev);
1227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 default:
1229 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1230 }
Ingo Molnarf85221d2006-03-23 03:00:38 -08001231 mutex_unlock(&lo->lo_ctl_mutex);
Nikanth Karthikesanf028f3b2009-03-24 12:33:41 +01001232
1233out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return err;
1235}
1236
David Howells863d5b822006-08-29 19:06:14 +01001237#ifdef CONFIG_COMPAT
1238struct compat_loop_info {
1239 compat_int_t lo_number; /* ioctl r/o */
1240 compat_dev_t lo_device; /* ioctl r/o */
1241 compat_ulong_t lo_inode; /* ioctl r/o */
1242 compat_dev_t lo_rdevice; /* ioctl r/o */
1243 compat_int_t lo_offset;
1244 compat_int_t lo_encrypt_type;
1245 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1246 compat_int_t lo_flags; /* ioctl r/o */
1247 char lo_name[LO_NAME_SIZE];
1248 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1249 compat_ulong_t lo_init[2];
1250 char reserved[4];
1251};
1252
1253/*
1254 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1255 * - noinlined to reduce stack space usage in main part of driver
1256 */
1257static noinline int
Al Viroba674cf2006-10-10 22:48:27 +01001258loop_info64_from_compat(const struct compat_loop_info __user *arg,
David Howells863d5b822006-08-29 19:06:14 +01001259 struct loop_info64 *info64)
1260{
1261 struct compat_loop_info info;
1262
1263 if (copy_from_user(&info, arg, sizeof(info)))
1264 return -EFAULT;
1265
1266 memset(info64, 0, sizeof(*info64));
1267 info64->lo_number = info.lo_number;
1268 info64->lo_device = info.lo_device;
1269 info64->lo_inode = info.lo_inode;
1270 info64->lo_rdevice = info.lo_rdevice;
1271 info64->lo_offset = info.lo_offset;
1272 info64->lo_sizelimit = 0;
1273 info64->lo_encrypt_type = info.lo_encrypt_type;
1274 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1275 info64->lo_flags = info.lo_flags;
1276 info64->lo_init[0] = info.lo_init[0];
1277 info64->lo_init[1] = info.lo_init[1];
1278 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1279 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1280 else
1281 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1282 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1283 return 0;
1284}
1285
1286/*
1287 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1288 * - noinlined to reduce stack space usage in main part of driver
1289 */
1290static noinline int
1291loop_info64_to_compat(const struct loop_info64 *info64,
1292 struct compat_loop_info __user *arg)
1293{
1294 struct compat_loop_info info;
1295
1296 memset(&info, 0, sizeof(info));
1297 info.lo_number = info64->lo_number;
1298 info.lo_device = info64->lo_device;
1299 info.lo_inode = info64->lo_inode;
1300 info.lo_rdevice = info64->lo_rdevice;
1301 info.lo_offset = info64->lo_offset;
1302 info.lo_encrypt_type = info64->lo_encrypt_type;
1303 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1304 info.lo_flags = info64->lo_flags;
1305 info.lo_init[0] = info64->lo_init[0];
1306 info.lo_init[1] = info64->lo_init[1];
1307 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1308 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1309 else
1310 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1311 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1312
1313 /* error in case values were truncated */
1314 if (info.lo_device != info64->lo_device ||
1315 info.lo_rdevice != info64->lo_rdevice ||
1316 info.lo_inode != info64->lo_inode ||
1317 info.lo_offset != info64->lo_offset ||
1318 info.lo_init[0] != info64->lo_init[0] ||
1319 info.lo_init[1] != info64->lo_init[1])
1320 return -EOVERFLOW;
1321
1322 if (copy_to_user(arg, &info, sizeof(info)))
1323 return -EFAULT;
1324 return 0;
1325}
1326
1327static int
1328loop_set_status_compat(struct loop_device *lo,
1329 const struct compat_loop_info __user *arg)
1330{
1331 struct loop_info64 info64;
1332 int ret;
1333
1334 ret = loop_info64_from_compat(arg, &info64);
1335 if (ret < 0)
1336 return ret;
1337 return loop_set_status(lo, &info64);
1338}
1339
1340static int
1341loop_get_status_compat(struct loop_device *lo,
1342 struct compat_loop_info __user *arg)
1343{
1344 struct loop_info64 info64;
1345 int err = 0;
1346
1347 if (!arg)
1348 err = -EINVAL;
1349 if (!err)
1350 err = loop_get_status(lo, &info64);
1351 if (!err)
1352 err = loop_info64_to_compat(&info64, arg);
1353 return err;
1354}
1355
Al Virobb214882008-03-02 09:29:48 -05001356static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1357 unsigned int cmd, unsigned long arg)
David Howells863d5b822006-08-29 19:06:14 +01001358{
Al Virobb214882008-03-02 09:29:48 -05001359 struct loop_device *lo = bdev->bd_disk->private_data;
David Howells863d5b822006-08-29 19:06:14 +01001360 int err;
1361
David Howells863d5b822006-08-29 19:06:14 +01001362 switch(cmd) {
1363 case LOOP_SET_STATUS:
1364 mutex_lock(&lo->lo_ctl_mutex);
1365 err = loop_set_status_compat(
1366 lo, (const struct compat_loop_info __user *) arg);
1367 mutex_unlock(&lo->lo_ctl_mutex);
1368 break;
1369 case LOOP_GET_STATUS:
1370 mutex_lock(&lo->lo_ctl_mutex);
1371 err = loop_get_status_compat(
1372 lo, (struct compat_loop_info __user *) arg);
1373 mutex_unlock(&lo->lo_ctl_mutex);
1374 break;
J. R. Okajima53d66602009-03-31 15:23:43 -07001375 case LOOP_SET_CAPACITY:
David Howells863d5b822006-08-29 19:06:14 +01001376 case LOOP_CLR_FD:
1377 case LOOP_GET_STATUS64:
1378 case LOOP_SET_STATUS64:
1379 arg = (unsigned long) compat_ptr(arg);
1380 case LOOP_SET_FD:
1381 case LOOP_CHANGE_FD:
Al Virobb214882008-03-02 09:29:48 -05001382 err = lo_ioctl(bdev, mode, cmd, arg);
David Howells863d5b822006-08-29 19:06:14 +01001383 break;
1384 default:
1385 err = -ENOIOCTLCMD;
1386 break;
1387 }
David Howells863d5b822006-08-29 19:06:14 +01001388 return err;
1389}
1390#endif
1391
Al Virobb214882008-03-02 09:29:48 -05001392static int lo_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Kay Sievers770fe302011-07-31 22:08:04 +02001394 struct loop_device *lo;
1395 int err = 0;
1396
1397 mutex_lock(&loop_index_mutex);
1398 lo = bdev->bd_disk->private_data;
1399 if (!lo) {
1400 err = -ENXIO;
1401 goto out;
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Ingo Molnarf85221d2006-03-23 03:00:38 -08001404 mutex_lock(&lo->lo_ctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 lo->lo_refcnt++;
Ingo Molnarf85221d2006-03-23 03:00:38 -08001406 mutex_unlock(&lo->lo_ctl_mutex);
Kay Sievers770fe302011-07-31 22:08:04 +02001407out:
1408 mutex_unlock(&loop_index_mutex);
1409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Al Virobb214882008-03-02 09:29:48 -05001412static int lo_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Al Virobb214882008-03-02 09:29:48 -05001414 struct loop_device *lo = disk->private_data;
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001415 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Ingo Molnarf85221d2006-03-23 03:00:38 -08001417 mutex_lock(&lo->lo_ctl_mutex);
David Woodhouse96c58652008-02-06 01:36:27 -08001418
Milan Broz14f27932008-12-12 14:48:27 +01001419 if (--lo->lo_refcnt)
1420 goto out;
1421
1422 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1423 /*
1424 * In autoclear mode, stop the loop thread
1425 * and remove configuration after last close.
1426 */
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001427 err = loop_clr_fd(lo, NULL);
1428 if (!err)
1429 goto out_unlocked;
Milan Broz14f27932008-12-12 14:48:27 +01001430 } else {
1431 /*
1432 * Otherwise keep thread (if running) and config,
1433 * but flush possible ongoing bios in thread.
1434 */
1435 loop_flush(lo);
1436 }
David Woodhouse96c58652008-02-06 01:36:27 -08001437
Milan Broz14f27932008-12-12 14:48:27 +01001438out:
Ingo Molnarf85221d2006-03-23 03:00:38 -08001439 mutex_unlock(&lo->lo_ctl_mutex);
Alexander Beregalovffcd7dc2009-04-07 13:48:21 +02001440out_unlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442}
1443
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001444static const struct block_device_operations lo_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .owner = THIS_MODULE,
Al Virobb214882008-03-02 09:29:48 -05001446 .open = lo_open,
1447 .release = lo_release,
1448 .ioctl = lo_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001449#ifdef CONFIG_COMPAT
Al Virobb214882008-03-02 09:29:48 -05001450 .compat_ioctl = lo_compat_ioctl,
David Howells863d5b822006-08-29 19:06:14 +01001451#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452};
1453
1454/*
1455 * And now the modules code and kernel interface.
1456 */
Ken Chen73285082007-05-08 00:28:20 -07001457static int max_loop;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001458module_param(max_loop, int, S_IRUGO);
Ken Chena47653f2007-06-08 13:46:44 -07001459MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
Namhyung Kimac04fee2011-05-27 07:59:25 +02001460module_param(max_part, int, S_IRUGO);
Laurent Vivier476a4812008-03-26 12:11:53 +01001461MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462MODULE_LICENSE("GPL");
1463MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1464
1465int loop_register_transfer(struct loop_func_table *funcs)
1466{
1467 unsigned int n = funcs->number;
1468
1469 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1470 return -EINVAL;
1471 xfer_funcs[n] = funcs;
1472 return 0;
1473}
1474
Kay Sievers34dd82a2011-07-31 22:08:04 +02001475static int unregister_transfer_cb(int id, void *ptr, void *data)
1476{
1477 struct loop_device *lo = ptr;
1478 struct loop_func_table *xfer = data;
1479
1480 mutex_lock(&lo->lo_ctl_mutex);
1481 if (lo->lo_encryption == xfer)
1482 loop_release_xfer(lo);
1483 mutex_unlock(&lo->lo_ctl_mutex);
1484 return 0;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487int loop_unregister_transfer(int number)
1488{
1489 unsigned int n = number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct loop_func_table *xfer;
1491
1492 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1493 return -EINVAL;
1494
1495 xfer_funcs[n] = NULL;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001496 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
1500EXPORT_SYMBOL(loop_register_transfer);
1501EXPORT_SYMBOL(loop_unregister_transfer);
1502
Kay Sievers34dd82a2011-07-31 22:08:04 +02001503static int loop_add(struct loop_device **l, int i)
Ken Chen73285082007-05-08 00:28:20 -07001504{
1505 struct loop_device *lo;
1506 struct gendisk *disk;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001507 int err;
Ken Chen73285082007-05-08 00:28:20 -07001508
1509 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001510 if (!lo) {
1511 err = -ENOMEM;
Ken Chen73285082007-05-08 00:28:20 -07001512 goto out;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001513 }
1514
1515 err = idr_pre_get(&loop_index_idr, GFP_KERNEL);
1516 if (err < 0)
1517 goto out_free_dev;
1518
1519 if (i >= 0) {
1520 int m;
1521
1522 /* create specific i in the index */
1523 err = idr_get_new_above(&loop_index_idr, lo, i, &m);
1524 if (err >= 0 && i != m) {
1525 idr_remove(&loop_index_idr, m);
1526 err = -EEXIST;
1527 }
Kay Sievers770fe302011-07-31 22:08:04 +02001528 } else if (i == -1) {
1529 int m;
1530
1531 /* get next free nr */
1532 err = idr_get_new(&loop_index_idr, lo, &m);
1533 if (err >= 0)
1534 i = m;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001535 } else {
1536 err = -EINVAL;
1537 }
1538 if (err < 0)
1539 goto out_free_dev;
Ken Chen73285082007-05-08 00:28:20 -07001540
1541 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
1542 if (!lo->lo_queue)
1543 goto out_free_dev;
1544
Laurent Vivier476a4812008-03-26 12:11:53 +01001545 disk = lo->lo_disk = alloc_disk(1 << part_shift);
Ken Chen73285082007-05-08 00:28:20 -07001546 if (!disk)
1547 goto out_free_queue;
1548
1549 mutex_init(&lo->lo_ctl_mutex);
1550 lo->lo_number = i;
1551 lo->lo_thread = NULL;
1552 init_waitqueue_head(&lo->lo_event);
1553 spin_lock_init(&lo->lo_lock);
1554 disk->major = LOOP_MAJOR;
Laurent Vivier476a4812008-03-26 12:11:53 +01001555 disk->first_minor = i << part_shift;
Ken Chen73285082007-05-08 00:28:20 -07001556 disk->fops = &lo_fops;
1557 disk->private_data = lo;
1558 disk->queue = lo->lo_queue;
1559 sprintf(disk->disk_name, "loop%d", i);
Kay Sievers34dd82a2011-07-31 22:08:04 +02001560 add_disk(disk);
1561 *l = lo;
1562 return lo->lo_number;
Ken Chen73285082007-05-08 00:28:20 -07001563
1564out_free_queue:
1565 blk_cleanup_queue(lo->lo_queue);
1566out_free_dev:
1567 kfree(lo);
1568out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001569 return err;
Ken Chen73285082007-05-08 00:28:20 -07001570}
1571
Kay Sievers34dd82a2011-07-31 22:08:04 +02001572static void loop_remove(struct loop_device *lo)
Ken Chen73285082007-05-08 00:28:20 -07001573{
Kay Sievers34dd82a2011-07-31 22:08:04 +02001574 del_gendisk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001575 blk_cleanup_queue(lo->lo_queue);
1576 put_disk(lo->lo_disk);
Ken Chen73285082007-05-08 00:28:20 -07001577 kfree(lo);
1578}
1579
Kay Sievers770fe302011-07-31 22:08:04 +02001580static int find_free_cb(int id, void *ptr, void *data)
1581{
1582 struct loop_device *lo = ptr;
1583 struct loop_device **l = data;
1584
1585 if (lo->lo_state == Lo_unbound) {
1586 *l = lo;
1587 return 1;
1588 }
1589 return 0;
1590}
1591
Kay Sievers34dd82a2011-07-31 22:08:04 +02001592static int loop_lookup(struct loop_device **l, int i)
Ken Chena47653f2007-06-08 13:46:44 -07001593{
1594 struct loop_device *lo;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001595 int ret = -ENODEV;
Ken Chena47653f2007-06-08 13:46:44 -07001596
Kay Sievers770fe302011-07-31 22:08:04 +02001597 if (i < 0) {
1598 int err;
1599
1600 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1601 if (err == 1) {
1602 *l = lo;
1603 ret = lo->lo_number;
1604 }
1605 goto out;
1606 }
1607
1608 /* lookup and return a specific i */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001609 lo = idr_find(&loop_index_idr, i);
Ken Chena47653f2007-06-08 13:46:44 -07001610 if (lo) {
Kay Sievers34dd82a2011-07-31 22:08:04 +02001611 *l = lo;
1612 ret = lo->lo_number;
Ken Chena47653f2007-06-08 13:46:44 -07001613 }
Kay Sievers770fe302011-07-31 22:08:04 +02001614out:
Kay Sievers34dd82a2011-07-31 22:08:04 +02001615 return ret;
Ken Chena47653f2007-06-08 13:46:44 -07001616}
1617
Ken Chen73285082007-05-08 00:28:20 -07001618static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1619{
Al Viro705962c2007-05-13 05:52:32 -04001620 struct loop_device *lo;
Al Viro07002e92007-05-12 16:23:15 -04001621 struct kobject *kobj;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001622 int err;
Ken Chen73285082007-05-08 00:28:20 -07001623
Kay Sievers34dd82a2011-07-31 22:08:04 +02001624 mutex_lock(&loop_index_mutex);
1625 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1626 if (err < 0)
1627 err = loop_add(&lo, MINOR(dev) >> part_shift);
1628 if (err < 0)
1629 kobj = ERR_PTR(err);
1630 else
1631 kobj = get_disk(lo->lo_disk);
1632 mutex_unlock(&loop_index_mutex);
Ken Chen73285082007-05-08 00:28:20 -07001633
1634 *part = 0;
Al Viro07002e92007-05-12 16:23:15 -04001635 return kobj;
Ken Chen73285082007-05-08 00:28:20 -07001636}
1637
Kay Sievers770fe302011-07-31 22:08:04 +02001638static long loop_control_ioctl(struct file *file, unsigned int cmd,
1639 unsigned long parm)
1640{
1641 struct loop_device *lo;
1642 int ret = -ENOSYS;
1643
1644 mutex_lock(&loop_index_mutex);
1645 switch (cmd) {
1646 case LOOP_CTL_ADD:
1647 ret = loop_lookup(&lo, parm);
1648 if (ret >= 0) {
1649 ret = -EEXIST;
1650 break;
1651 }
1652 ret = loop_add(&lo, parm);
1653 break;
1654 case LOOP_CTL_REMOVE:
1655 ret = loop_lookup(&lo, parm);
1656 if (ret < 0)
1657 break;
1658 mutex_lock(&lo->lo_ctl_mutex);
1659 if (lo->lo_state != Lo_unbound) {
1660 ret = -EBUSY;
1661 mutex_unlock(&lo->lo_ctl_mutex);
1662 break;
1663 }
1664 if (lo->lo_refcnt > 0) {
1665 ret = -EBUSY;
1666 mutex_unlock(&lo->lo_ctl_mutex);
1667 break;
1668 }
1669 lo->lo_disk->private_data = NULL;
1670 mutex_unlock(&lo->lo_ctl_mutex);
1671 idr_remove(&loop_index_idr, lo->lo_number);
1672 loop_remove(lo);
1673 break;
1674 case LOOP_CTL_GET_FREE:
1675 ret = loop_lookup(&lo, -1);
1676 if (ret >= 0)
1677 break;
1678 ret = loop_add(&lo, -1);
1679 }
1680 mutex_unlock(&loop_index_mutex);
1681
1682 return ret;
1683}
1684
1685static const struct file_operations loop_ctl_fops = {
1686 .open = nonseekable_open,
1687 .unlocked_ioctl = loop_control_ioctl,
1688 .compat_ioctl = loop_control_ioctl,
1689 .owner = THIS_MODULE,
1690 .llseek = noop_llseek,
1691};
1692
1693static struct miscdevice loop_misc = {
1694 .minor = LOOP_CTRL_MINOR,
1695 .name = "loop-control",
1696 .fops = &loop_ctl_fops,
1697};
1698
1699MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1700MODULE_ALIAS("devname:loop-control");
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702static int __init loop_init(void)
1703{
Ken Chena47653f2007-06-08 13:46:44 -07001704 int i, nr;
1705 unsigned long range;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001706 struct loop_device *lo;
Kay Sievers770fe302011-07-31 22:08:04 +02001707 int err;
Ken Chena47653f2007-06-08 13:46:44 -07001708
Kay Sievers770fe302011-07-31 22:08:04 +02001709 err = misc_register(&loop_misc);
1710 if (err < 0)
1711 return err;
1712
Laurent Vivier476a4812008-03-26 12:11:53 +01001713 part_shift = 0;
Namhyung Kimac04fee2011-05-27 07:59:25 +02001714 if (max_part > 0) {
Laurent Vivier476a4812008-03-26 12:11:53 +01001715 part_shift = fls(max_part);
1716
Namhyung Kimac04fee2011-05-27 07:59:25 +02001717 /*
1718 * Adjust max_part according to part_shift as it is exported
1719 * to user space so that user can decide correct minor number
1720 * if [s]he want to create more devices.
1721 *
1722 * Note that -1 is required because partition 0 is reserved
1723 * for the whole disk.
1724 */
1725 max_part = (1UL << part_shift) - 1;
1726 }
1727
Namhyung Kim78f4bb32011-05-24 16:48:54 +02001728 if ((1UL << part_shift) > DISK_MAX_PARTS)
1729 return -EINVAL;
1730
Laurent Vivier476a4812008-03-26 12:11:53 +01001731 if (max_loop > 1UL << (MINORBITS - part_shift))
Ken Chena47653f2007-06-08 13:46:44 -07001732 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Kay Sieversd134b002011-07-31 22:08:04 +02001734 /*
1735 * If max_loop is specified, create that many devices upfront.
1736 * This also becomes a hard limit. If max_loop is not specified,
1737 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1738 * init time. Loop devices can be requested on-demand with the
1739 * /dev/loop-control interface, or be instantiated by accessing
1740 * a 'dead' device node.
1741 */
Ken Chen73285082007-05-08 00:28:20 -07001742 if (max_loop) {
Ken Chena47653f2007-06-08 13:46:44 -07001743 nr = max_loop;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001744 range = max_loop << part_shift;
Ken Chena47653f2007-06-08 13:46:44 -07001745 } else {
Kay Sieversd134b002011-07-31 22:08:04 +02001746 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
Namhyung Kima1c15c52011-05-24 16:48:55 +02001747 range = 1UL << MINORBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Ken Chena47653f2007-06-08 13:46:44 -07001749
1750 if (register_blkdev(LOOP_MAJOR, "loop"))
1751 return -EIO;
1752
Ken Chena47653f2007-06-08 13:46:44 -07001753 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1754 THIS_MODULE, loop_probe, NULL, NULL);
1755
Kay Sieversd134b002011-07-31 22:08:04 +02001756 /* pre-create number of devices given by config or max_loop */
Kay Sievers34dd82a2011-07-31 22:08:04 +02001757 mutex_lock(&loop_index_mutex);
1758 for (i = 0; i < nr; i++)
1759 loop_add(&lo, i);
1760 mutex_unlock(&loop_index_mutex);
1761
Ken Chen73285082007-05-08 00:28:20 -07001762 printk(KERN_INFO "loop: module loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
Kay Sievers34dd82a2011-07-31 22:08:04 +02001764}
Ken Chena47653f2007-06-08 13:46:44 -07001765
Kay Sievers34dd82a2011-07-31 22:08:04 +02001766static int loop_exit_cb(int id, void *ptr, void *data)
1767{
1768 struct loop_device *lo = ptr;
Ken Chena47653f2007-06-08 13:46:44 -07001769
Kay Sievers34dd82a2011-07-31 22:08:04 +02001770 loop_remove(lo);
1771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Ken Chen73285082007-05-08 00:28:20 -07001774static void __exit loop_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Ken Chena47653f2007-06-08 13:46:44 -07001776 unsigned long range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Namhyung Kima1c15c52011-05-24 16:48:55 +02001778 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
Ken Chena47653f2007-06-08 13:46:44 -07001779
Kay Sievers34dd82a2011-07-31 22:08:04 +02001780 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
1781 idr_remove_all(&loop_index_idr);
1782 idr_destroy(&loop_index_idr);
Ken Chen73285082007-05-08 00:28:20 -07001783
Ken Chena47653f2007-06-08 13:46:44 -07001784 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
Akinobu Mita00d59402007-07-17 04:03:46 -07001785 unregister_blkdev(LOOP_MAJOR, "loop");
Kay Sievers770fe302011-07-31 22:08:04 +02001786
1787 misc_deregister(&loop_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
1790module_init(loop_init);
1791module_exit(loop_exit);
1792
1793#ifndef MODULE
1794static int __init max_loop_setup(char *str)
1795{
1796 max_loop = simple_strtol(str, NULL, 0);
1797 return 1;
1798}
1799
1800__setup("max_loop=", max_loop_setup);
1801#endif