Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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. | |
96de0e25 | 32 | * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999 |
1da177e4 LT |
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 | |
4e02ed4b | 43 | * operations write_begin is not available on the backing filesystem. |
1da177e4 LT |
44 | * 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 | ||
1da177e4 LT |
52 | #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> | |
1da177e4 LT |
64 | #include <linux/swap.h> |
65 | #include <linux/slab.h> | |
863d5b82 | 66 | #include <linux/compat.h> |
1da177e4 | 67 | #include <linux/suspend.h> |
83144186 | 68 | #include <linux/freezer.h> |
2a48fc0a | 69 | #include <linux/mutex.h> |
1da177e4 | 70 | #include <linux/writeback.h> |
1da177e4 LT |
71 | #include <linux/completion.h> |
72 | #include <linux/highmem.h> | |
6c997918 | 73 | #include <linux/kthread.h> |
d6b29d7c | 74 | #include <linux/splice.h> |
ee862730 | 75 | #include <linux/sysfs.h> |
770fe30a | 76 | #include <linux/miscdevice.h> |
dfaa2ef6 | 77 | #include <linux/falloc.h> |
283e7e5d | 78 | #include <linux/uio.h> |
d9a08a9e AM |
79 | #include <linux/ioprio.h> |
80 | ||
83a87611 | 81 | #include "loop.h" |
1da177e4 | 82 | |
7c0f6ba6 | 83 | #include <linux/uaccess.h> |
1da177e4 | 84 | |
34dd82af | 85 | static DEFINE_IDR(loop_index_idr); |
310ca162 | 86 | static DEFINE_MUTEX(loop_ctl_mutex); |
1da177e4 | 87 | |
476a4813 LV |
88 | static int max_part; |
89 | static int part_shift; | |
90 | ||
1da177e4 LT |
91 | static int transfer_xor(struct loop_device *lo, int cmd, |
92 | struct page *raw_page, unsigned raw_off, | |
93 | struct page *loop_page, unsigned loop_off, | |
94 | int size, sector_t real_block) | |
95 | { | |
cfd8005c CW |
96 | char *raw_buf = kmap_atomic(raw_page) + raw_off; |
97 | char *loop_buf = kmap_atomic(loop_page) + loop_off; | |
1da177e4 LT |
98 | char *in, *out, *key; |
99 | int i, keysize; | |
100 | ||
101 | if (cmd == READ) { | |
102 | in = raw_buf; | |
103 | out = loop_buf; | |
104 | } else { | |
105 | in = loop_buf; | |
106 | out = raw_buf; | |
107 | } | |
108 | ||
109 | key = lo->lo_encrypt_key; | |
110 | keysize = lo->lo_encrypt_key_size; | |
111 | for (i = 0; i < size; i++) | |
112 | *out++ = *in++ ^ key[(i & 511) % keysize]; | |
113 | ||
cfd8005c CW |
114 | kunmap_atomic(loop_buf); |
115 | kunmap_atomic(raw_buf); | |
1da177e4 LT |
116 | cond_resched(); |
117 | return 0; | |
118 | } | |
119 | ||
120 | static int xor_init(struct loop_device *lo, const struct loop_info64 *info) | |
121 | { | |
122 | if (unlikely(info->lo_encrypt_key_size <= 0)) | |
123 | return -EINVAL; | |
124 | return 0; | |
125 | } | |
126 | ||
127 | static struct loop_func_table none_funcs = { | |
128 | .number = LO_CRYPT_NONE, | |
aa4d8616 | 129 | }; |
1da177e4 LT |
130 | |
131 | static struct loop_func_table xor_funcs = { | |
132 | .number = LO_CRYPT_XOR, | |
133 | .transfer = transfer_xor, | |
134 | .init = xor_init | |
aa4d8616 | 135 | }; |
1da177e4 LT |
136 | |
137 | /* xfer_funcs[0] is special - its release function is never called */ | |
138 | static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { | |
139 | &none_funcs, | |
140 | &xor_funcs | |
141 | }; | |
142 | ||
7035b5df | 143 | static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) |
1da177e4 | 144 | { |
b7a1da69 | 145 | loff_t loopsize; |
1da177e4 LT |
146 | |
147 | /* Compute loopsize in bytes */ | |
b7a1da69 GC |
148 | loopsize = i_size_read(file->f_mapping->host); |
149 | if (offset > 0) | |
150 | loopsize -= offset; | |
151 | /* offset is beyond i_size, weird but possible */ | |
7035b5df DM |
152 | if (loopsize < 0) |
153 | return 0; | |
1da177e4 | 154 | |
7035b5df DM |
155 | if (sizelimit > 0 && sizelimit < loopsize) |
156 | loopsize = sizelimit; | |
1da177e4 LT |
157 | /* |
158 | * Unfortunately, if we want to do I/O on the device, | |
159 | * the number of 512-byte sectors has to fit into a sector_t. | |
160 | */ | |
161 | return loopsize >> 9; | |
162 | } | |
163 | ||
7035b5df DM |
164 | static loff_t get_loop_size(struct loop_device *lo, struct file *file) |
165 | { | |
166 | return get_size(lo->lo_offset, lo->lo_sizelimit, file); | |
167 | } | |
168 | ||
2e5ab5f3 ML |
169 | static void __loop_update_dio(struct loop_device *lo, bool dio) |
170 | { | |
171 | struct file *file = lo->lo_backing_file; | |
172 | struct address_space *mapping = file->f_mapping; | |
173 | struct inode *inode = mapping->host; | |
174 | unsigned short sb_bsize = 0; | |
175 | unsigned dio_align = 0; | |
176 | bool use_dio; | |
177 | ||
178 | if (inode->i_sb->s_bdev) { | |
179 | sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); | |
180 | dio_align = sb_bsize - 1; | |
181 | } | |
182 | ||
183 | /* | |
184 | * We support direct I/O only if lo_offset is aligned with the | |
185 | * logical I/O size of backing device, and the logical block | |
186 | * size of loop is bigger than the backing device's and the loop | |
187 | * needn't transform transfer. | |
188 | * | |
189 | * TODO: the above condition may be loosed in the future, and | |
190 | * direct I/O may be switched runtime at that time because most | |
89d790ab | 191 | * of requests in sane applications should be PAGE_SIZE aligned |
2e5ab5f3 ML |
192 | */ |
193 | if (dio) { | |
194 | if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && | |
195 | !(lo->lo_offset & dio_align) && | |
196 | mapping->a_ops->direct_IO && | |
197 | !lo->transfer) | |
198 | use_dio = true; | |
199 | else | |
200 | use_dio = false; | |
201 | } else { | |
202 | use_dio = false; | |
203 | } | |
204 | ||
205 | if (lo->use_dio == use_dio) | |
206 | return; | |
207 | ||
208 | /* flush dirty pages before changing direct IO */ | |
209 | vfs_fsync(file, 0); | |
210 | ||
211 | /* | |
212 | * The flag of LO_FLAGS_DIRECT_IO is handled similarly with | |
213 | * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup | |
214 | * will get updated by ioctl(LOOP_GET_STATUS) | |
215 | */ | |
216 | blk_mq_freeze_queue(lo->lo_queue); | |
217 | lo->use_dio = use_dio; | |
40326d8a | 218 | if (use_dio) { |
8b904b5b | 219 | blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
2e5ab5f3 | 220 | lo->lo_flags |= LO_FLAGS_DIRECT_IO; |
40326d8a | 221 | } else { |
8b904b5b | 222 | blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
2e5ab5f3 | 223 | lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; |
40326d8a | 224 | } |
2e5ab5f3 ML |
225 | blk_mq_unfreeze_queue(lo->lo_queue); |
226 | } | |
227 | ||
1da177e4 | 228 | static int |
1e6ec9ea | 229 | figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) |
1da177e4 | 230 | { |
7035b5df | 231 | loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); |
1da177e4 | 232 | sector_t x = (sector_t)size; |
7b0576a3 | 233 | struct block_device *bdev = lo->lo_device; |
1da177e4 LT |
234 | |
235 | if (unlikely((loff_t)x != size)) | |
236 | return -EFBIG; | |
7035b5df DM |
237 | if (lo->lo_offset != offset) |
238 | lo->lo_offset = offset; | |
239 | if (lo->lo_sizelimit != sizelimit) | |
240 | lo->lo_sizelimit = sizelimit; | |
73285082 | 241 | set_capacity(lo->lo_disk, x); |
7b0576a3 GC |
242 | bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); |
243 | /* let user-space know about the new size */ | |
244 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); | |
7035b5df | 245 | return 0; |
1da177e4 LT |
246 | } |
247 | ||
248 | static inline int | |
249 | lo_do_transfer(struct loop_device *lo, int cmd, | |
250 | struct page *rpage, unsigned roffs, | |
251 | struct page *lpage, unsigned loffs, | |
252 | int size, sector_t rblock) | |
253 | { | |
aa4d8616 CH |
254 | int ret; |
255 | ||
256 | ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); | |
257 | if (likely(!ret)) | |
1da177e4 LT |
258 | return 0; |
259 | ||
aa4d8616 CH |
260 | printk_ratelimited(KERN_ERR |
261 | "loop: Transfer error at byte offset %llu, length %i.\n", | |
262 | (unsigned long long)rblock << 9, size); | |
263 | return ret; | |
1da177e4 LT |
264 | } |
265 | ||
aa4d8616 | 266 | static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) |
1da177e4 | 267 | { |
aa4d8616 | 268 | struct iov_iter i; |
1da177e4 | 269 | ssize_t bw; |
283e7e5d | 270 | |
aa563d7b | 271 | iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); |
1da177e4 | 272 | |
03d95eb2 | 273 | file_start_write(file); |
abbb6589 | 274 | bw = vfs_iter_write(file, &i, ppos, 0); |
03d95eb2 | 275 | file_end_write(file); |
aa4d8616 CH |
276 | |
277 | if (likely(bw == bvec->bv_len)) | |
1da177e4 | 278 | return 0; |
aa4d8616 CH |
279 | |
280 | printk_ratelimited(KERN_ERR | |
281 | "loop: Write error at byte offset %llu, length %i.\n", | |
282 | (unsigned long long)*ppos, bvec->bv_len); | |
1da177e4 LT |
283 | if (bw >= 0) |
284 | bw = -EIO; | |
285 | return bw; | |
286 | } | |
287 | ||
aa4d8616 CH |
288 | static int lo_write_simple(struct loop_device *lo, struct request *rq, |
289 | loff_t pos) | |
1da177e4 | 290 | { |
aa4d8616 CH |
291 | struct bio_vec bvec; |
292 | struct req_iterator iter; | |
293 | int ret = 0; | |
294 | ||
295 | rq_for_each_segment(bvec, rq, iter) { | |
296 | ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); | |
297 | if (ret < 0) | |
298 | break; | |
299 | cond_resched(); | |
300 | } | |
301 | ||
302 | return ret; | |
1da177e4 LT |
303 | } |
304 | ||
aa4d8616 | 305 | /* |
456be148 CH |
306 | * This is the slow, transforming version that needs to double buffer the |
307 | * data as it cannot do the transformations in place without having direct | |
308 | * access to the destination pages of the backing file. | |
1da177e4 | 309 | */ |
aa4d8616 CH |
310 | static int lo_write_transfer(struct loop_device *lo, struct request *rq, |
311 | loff_t pos) | |
1da177e4 | 312 | { |
aa4d8616 | 313 | struct bio_vec bvec, b; |
30112013 | 314 | struct req_iterator iter; |
aa4d8616 | 315 | struct page *page; |
7988613b | 316 | int ret = 0; |
1da177e4 | 317 | |
aa4d8616 CH |
318 | page = alloc_page(GFP_NOIO); |
319 | if (unlikely(!page)) | |
320 | return -ENOMEM; | |
456be148 | 321 | |
30112013 | 322 | rq_for_each_segment(bvec, rq, iter) { |
aa4d8616 CH |
323 | ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, |
324 | bvec.bv_offset, bvec.bv_len, pos >> 9); | |
325 | if (unlikely(ret)) | |
326 | break; | |
327 | ||
328 | b.bv_page = page; | |
329 | b.bv_offset = 0; | |
330 | b.bv_len = bvec.bv_len; | |
331 | ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); | |
1da177e4 LT |
332 | if (ret < 0) |
333 | break; | |
1da177e4 | 334 | } |
aa4d8616 CH |
335 | |
336 | __free_page(page); | |
1da177e4 | 337 | return ret; |
1da177e4 LT |
338 | } |
339 | ||
aa4d8616 CH |
340 | static int lo_read_simple(struct loop_device *lo, struct request *rq, |
341 | loff_t pos) | |
1da177e4 | 342 | { |
aa4d8616 CH |
343 | struct bio_vec bvec; |
344 | struct req_iterator iter; | |
345 | struct iov_iter i; | |
346 | ssize_t len; | |
1da177e4 | 347 | |
aa4d8616 | 348 | rq_for_each_segment(bvec, rq, iter) { |
aa563d7b | 349 | iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); |
18e9710e | 350 | len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); |
aa4d8616 CH |
351 | if (len < 0) |
352 | return len; | |
1da177e4 | 353 | |
aa4d8616 | 354 | flush_dcache_page(bvec.bv_page); |
fd582140 | 355 | |
aa4d8616 CH |
356 | if (len != bvec.bv_len) { |
357 | struct bio *bio; | |
1da177e4 | 358 | |
aa4d8616 CH |
359 | __rq_for_each_bio(bio, rq) |
360 | zero_fill_bio(bio); | |
361 | break; | |
362 | } | |
363 | cond_resched(); | |
364 | } | |
365 | ||
366 | return 0; | |
fd582140 JA |
367 | } |
368 | ||
aa4d8616 CH |
369 | static int lo_read_transfer(struct loop_device *lo, struct request *rq, |
370 | loff_t pos) | |
1da177e4 | 371 | { |
aa4d8616 CH |
372 | struct bio_vec bvec, b; |
373 | struct req_iterator iter; | |
374 | struct iov_iter i; | |
375 | struct page *page; | |
376 | ssize_t len; | |
377 | int ret = 0; | |
1da177e4 | 378 | |
aa4d8616 CH |
379 | page = alloc_page(GFP_NOIO); |
380 | if (unlikely(!page)) | |
381 | return -ENOMEM; | |
fd582140 | 382 | |
aa4d8616 CH |
383 | rq_for_each_segment(bvec, rq, iter) { |
384 | loff_t offset = pos; | |
fd582140 | 385 | |
aa4d8616 CH |
386 | b.bv_page = page; |
387 | b.bv_offset = 0; | |
388 | b.bv_len = bvec.bv_len; | |
fd582140 | 389 | |
aa563d7b | 390 | iov_iter_bvec(&i, READ, &b, 1, b.bv_len); |
18e9710e | 391 | len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); |
aa4d8616 CH |
392 | if (len < 0) { |
393 | ret = len; | |
394 | goto out_free_page; | |
395 | } | |
1da177e4 | 396 | |
aa4d8616 CH |
397 | ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, |
398 | bvec.bv_offset, len, offset >> 9); | |
399 | if (ret) | |
400 | goto out_free_page; | |
1da177e4 | 401 | |
aa4d8616 | 402 | flush_dcache_page(bvec.bv_page); |
306df071 | 403 | |
aa4d8616 | 404 | if (len != bvec.bv_len) { |
30112013 ML |
405 | struct bio *bio; |
406 | ||
407 | __rq_for_each_bio(bio, rq) | |
408 | zero_fill_bio(bio); | |
1da177e4 | 409 | break; |
306df071 | 410 | } |
1da177e4 | 411 | } |
aa4d8616 CH |
412 | |
413 | ret = 0; | |
414 | out_free_page: | |
415 | __free_page(page); | |
416 | return ret; | |
1da177e4 LT |
417 | } |
418 | ||
cf655d95 ML |
419 | static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) |
420 | { | |
421 | /* | |
422 | * We use punch hole to reclaim the free space used by the | |
423 | * image a.k.a. discard. However we do not support discard if | |
424 | * encryption is enabled, because it may give an attacker | |
425 | * useful information. | |
426 | */ | |
427 | struct file *file = lo->lo_backing_file; | |
428 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; | |
429 | int ret; | |
430 | ||
431 | if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { | |
432 | ret = -EOPNOTSUPP; | |
433 | goto out; | |
434 | } | |
435 | ||
436 | ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); | |
437 | if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) | |
438 | ret = -EIO; | |
439 | out: | |
440 | return ret; | |
441 | } | |
442 | ||
443 | static int lo_req_flush(struct loop_device *lo, struct request *rq) | |
444 | { | |
445 | struct file *file = lo->lo_backing_file; | |
446 | int ret = vfs_fsync(file, 0); | |
447 | if (unlikely(ret && ret != -EINVAL)) | |
448 | ret = -EIO; | |
449 | ||
450 | return ret; | |
451 | } | |
452 | ||
fe2cb290 | 453 | static void lo_complete_rq(struct request *rq) |
bc07c10a | 454 | { |
fe2cb290 | 455 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); |
f9de14bc | 456 | blk_status_t ret = BLK_STS_OK; |
bc07c10a | 457 | |
f9de14bc JA |
458 | if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || |
459 | req_op(rq) != REQ_OP_READ) { | |
460 | if (cmd->ret < 0) | |
461 | ret = BLK_STS_IOERR; | |
462 | goto end_io; | |
bc07c10a | 463 | } |
fe2cb290 | 464 | |
f9de14bc JA |
465 | /* |
466 | * Short READ - if we got some data, advance our request and | |
467 | * retry it. If we got no data, end the rest with EIO. | |
468 | */ | |
469 | if (cmd->ret) { | |
470 | blk_update_request(rq, BLK_STS_OK, cmd->ret); | |
471 | cmd->ret = 0; | |
472 | blk_mq_requeue_request(rq, true); | |
473 | } else { | |
474 | if (cmd->use_aio) { | |
475 | struct bio *bio = rq->bio; | |
476 | ||
477 | while (bio) { | |
478 | zero_fill_bio(bio); | |
479 | bio = bio->bi_next; | |
480 | } | |
481 | } | |
482 | ret = BLK_STS_IOERR; | |
483 | end_io: | |
484 | blk_mq_end_request(rq, ret); | |
485 | } | |
bc07c10a ML |
486 | } |
487 | ||
92d77332 SL |
488 | static void lo_rw_aio_do_completion(struct loop_cmd *cmd) |
489 | { | |
1894e916 JA |
490 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
491 | ||
92d77332 SL |
492 | if (!atomic_dec_and_test(&cmd->ref)) |
493 | return; | |
494 | kfree(cmd->bvec); | |
495 | cmd->bvec = NULL; | |
1894e916 | 496 | blk_mq_complete_request(rq); |
92d77332 SL |
497 | } |
498 | ||
bc07c10a ML |
499 | static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) |
500 | { | |
501 | struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); | |
bc07c10a | 502 | |
d4478e92 SL |
503 | if (cmd->css) |
504 | css_put(cmd->css); | |
fe2cb290 | 505 | cmd->ret = ret; |
92d77332 | 506 | lo_rw_aio_do_completion(cmd); |
bc07c10a ML |
507 | } |
508 | ||
509 | static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, | |
510 | loff_t pos, bool rw) | |
511 | { | |
512 | struct iov_iter iter; | |
513 | struct bio_vec *bvec; | |
1894e916 | 514 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
40326d8a | 515 | struct bio *bio = rq->bio; |
bc07c10a | 516 | struct file *file = lo->lo_backing_file; |
40326d8a SL |
517 | unsigned int offset; |
518 | int segments = 0; | |
bc07c10a ML |
519 | int ret; |
520 | ||
40326d8a SL |
521 | if (rq->bio != rq->biotail) { |
522 | struct req_iterator iter; | |
523 | struct bio_vec tmp; | |
524 | ||
525 | __rq_for_each_bio(bio, rq) | |
526 | segments += bio_segments(bio); | |
6da2ec56 KC |
527 | bvec = kmalloc_array(segments, sizeof(struct bio_vec), |
528 | GFP_NOIO); | |
40326d8a SL |
529 | if (!bvec) |
530 | return -EIO; | |
531 | cmd->bvec = bvec; | |
532 | ||
533 | /* | |
534 | * The bios of the request may be started from the middle of | |
535 | * the 'bvec' because of bio splitting, so we can't directly | |
536 | * copy bio->bi_iov_vec to new bvec. The rq_for_each_segment | |
537 | * API will take care of all details for us. | |
538 | */ | |
539 | rq_for_each_segment(tmp, rq, iter) { | |
540 | *bvec = tmp; | |
541 | bvec++; | |
542 | } | |
543 | bvec = cmd->bvec; | |
544 | offset = 0; | |
545 | } else { | |
546 | /* | |
547 | * Same here, this bio may be started from the middle of the | |
548 | * 'bvec' because of bio splitting, so offset from the bvec | |
549 | * must be passed to iov iterator | |
550 | */ | |
551 | offset = bio->bi_iter.bi_bvec_done; | |
552 | bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); | |
553 | segments = bio_segments(bio); | |
554 | } | |
92d77332 | 555 | atomic_set(&cmd->ref, 2); |
bc07c10a | 556 | |
aa563d7b | 557 | iov_iter_bvec(&iter, rw, bvec, segments, blk_rq_bytes(rq)); |
40326d8a | 558 | iter.iov_offset = offset; |
bc07c10a ML |
559 | |
560 | cmd->iocb.ki_pos = pos; | |
561 | cmd->iocb.ki_filp = file; | |
562 | cmd->iocb.ki_complete = lo_rw_aio_complete; | |
563 | cmd->iocb.ki_flags = IOCB_DIRECT; | |
d9a08a9e | 564 | cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); |
d4478e92 SL |
565 | if (cmd->css) |
566 | kthread_associate_blkcg(cmd->css); | |
bc07c10a ML |
567 | |
568 | if (rw == WRITE) | |
bb7462b6 | 569 | ret = call_write_iter(file, &cmd->iocb, &iter); |
bc07c10a | 570 | else |
bb7462b6 | 571 | ret = call_read_iter(file, &cmd->iocb, &iter); |
bc07c10a | 572 | |
92d77332 | 573 | lo_rw_aio_do_completion(cmd); |
d4478e92 | 574 | kthread_associate_blkcg(NULL); |
92d77332 | 575 | |
bc07c10a ML |
576 | if (ret != -EIOCBQUEUED) |
577 | cmd->iocb.ki_complete(&cmd->iocb, ret, 0); | |
578 | return 0; | |
579 | } | |
580 | ||
c1c87c2b | 581 | static int do_req_filebacked(struct loop_device *lo, struct request *rq) |
bc07c10a ML |
582 | { |
583 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); | |
c1c87c2b | 584 | loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; |
bc07c10a ML |
585 | |
586 | /* | |
587 | * lo_write_simple and lo_read_simple should have been covered | |
588 | * by io submit style function like lo_rw_aio(), one blocker | |
589 | * is that lo_read_simple() need to call flush_dcache_page after | |
590 | * the page is written from kernel, and it isn't easy to handle | |
591 | * this in io submit style function which submits all segments | |
592 | * of the req at one time. And direct read IO doesn't need to | |
593 | * run flush_dcache_page(). | |
594 | */ | |
c1c87c2b CH |
595 | switch (req_op(rq)) { |
596 | case REQ_OP_FLUSH: | |
597 | return lo_req_flush(lo, rq); | |
598 | case REQ_OP_DISCARD: | |
19372e27 | 599 | case REQ_OP_WRITE_ZEROES: |
c1c87c2b CH |
600 | return lo_discard(lo, rq, pos); |
601 | case REQ_OP_WRITE: | |
602 | if (lo->transfer) | |
603 | return lo_write_transfer(lo, rq, pos); | |
604 | else if (cmd->use_aio) | |
605 | return lo_rw_aio(lo, cmd, pos, WRITE); | |
af65aa8e | 606 | else |
c1c87c2b CH |
607 | return lo_write_simple(lo, rq, pos); |
608 | case REQ_OP_READ: | |
aa4d8616 | 609 | if (lo->transfer) |
c1c87c2b CH |
610 | return lo_read_transfer(lo, rq, pos); |
611 | else if (cmd->use_aio) | |
612 | return lo_rw_aio(lo, cmd, pos, READ); | |
aa4d8616 | 613 | else |
c1c87c2b CH |
614 | return lo_read_simple(lo, rq, pos); |
615 | default: | |
616 | WARN_ON_ONCE(1); | |
617 | return -EIO; | |
618 | break; | |
aa4d8616 | 619 | } |
1da177e4 LT |
620 | } |
621 | ||
2e5ab5f3 ML |
622 | static inline void loop_update_dio(struct loop_device *lo) |
623 | { | |
624 | __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | | |
625 | lo->use_dio); | |
626 | } | |
627 | ||
06f0e9e6 ML |
628 | static void loop_reread_partitions(struct loop_device *lo, |
629 | struct block_device *bdev) | |
630 | { | |
631 | int rc; | |
632 | ||
633 | /* | |
634 | * bd_mutex has been held already in release path, so don't | |
635 | * acquire it if this function is called in such case. | |
636 | * | |
637 | * If the reread partition isn't from release path, lo_refcnt | |
638 | * must be at least one and it can only become zero when the | |
639 | * current holder is released. | |
640 | */ | |
641 | if (!atomic_read(&lo->lo_refcnt)) | |
642 | rc = __blkdev_reread_part(bdev); | |
643 | else | |
644 | rc = blkdev_reread_part(bdev); | |
645 | if (rc) | |
646 | pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", | |
647 | __func__, lo->lo_number, lo->lo_file_name, rc); | |
648 | } | |
649 | ||
d2ac838e TT |
650 | static inline int is_loop_device(struct file *file) |
651 | { | |
652 | struct inode *i = file->f_mapping->host; | |
653 | ||
654 | return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; | |
655 | } | |
656 | ||
657 | static int loop_validate_file(struct file *file, struct block_device *bdev) | |
658 | { | |
659 | struct inode *inode = file->f_mapping->host; | |
660 | struct file *f = file; | |
661 | ||
662 | /* Avoid recursion */ | |
663 | while (is_loop_device(f)) { | |
664 | struct loop_device *l; | |
665 | ||
666 | if (f->f_mapping->host->i_bdev == bdev) | |
667 | return -EBADF; | |
668 | ||
669 | l = f->f_mapping->host->i_bdev->bd_disk->private_data; | |
670 | if (l->lo_state == Lo_unbound) { | |
671 | return -EINVAL; | |
672 | } | |
673 | f = l->lo_backing_file; | |
674 | } | |
675 | if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) | |
676 | return -EINVAL; | |
677 | return 0; | |
678 | } | |
679 | ||
1da177e4 LT |
680 | /* |
681 | * loop_change_fd switched the backing store of a loopback device to | |
682 | * a new file. This is useful for operating system installers to free up | |
683 | * the original file and in High Availability environments to switch to | |
684 | * an alternative location for the content in case of server meltdown. | |
685 | * This can only work if the loop device is used read-only, and if the | |
686 | * new backing store is the same size and type as the old backing store. | |
687 | */ | |
bb214884 AV |
688 | static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, |
689 | unsigned int arg) | |
1da177e4 LT |
690 | { |
691 | struct file *file, *old_file; | |
1da177e4 LT |
692 | int error; |
693 | ||
694 | error = -ENXIO; | |
695 | if (lo->lo_state != Lo_bound) | |
696 | goto out; | |
697 | ||
698 | /* the loop device has to be read-only */ | |
699 | error = -EINVAL; | |
700 | if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) | |
701 | goto out; | |
702 | ||
703 | error = -EBADF; | |
704 | file = fget(arg); | |
705 | if (!file) | |
706 | goto out; | |
707 | ||
d2ac838e TT |
708 | error = loop_validate_file(file, bdev); |
709 | if (error) | |
710 | goto out_putf; | |
711 | ||
1da177e4 LT |
712 | old_file = lo->lo_backing_file; |
713 | ||
714 | error = -EINVAL; | |
715 | ||
1da177e4 LT |
716 | /* size of the new backing store needs to be the same */ |
717 | if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) | |
718 | goto out_putf; | |
719 | ||
720 | /* and ... switch */ | |
43cade80 OS |
721 | blk_mq_freeze_queue(lo->lo_queue); |
722 | mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); | |
723 | lo->lo_backing_file = file; | |
724 | lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); | |
725 | mapping_set_gfp_mask(file->f_mapping, | |
726 | lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | |
727 | loop_update_dio(lo); | |
728 | blk_mq_unfreeze_queue(lo->lo_queue); | |
1da177e4 LT |
729 | |
730 | fput(old_file); | |
e03c8dd1 | 731 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) |
06f0e9e6 | 732 | loop_reread_partitions(lo, bdev); |
1da177e4 LT |
733 | return 0; |
734 | ||
735 | out_putf: | |
736 | fput(file); | |
737 | out: | |
738 | return error; | |
739 | } | |
740 | ||
ee862730 MB |
741 | /* loop sysfs attributes */ |
742 | ||
743 | static ssize_t loop_attr_show(struct device *dev, char *page, | |
744 | ssize_t (*callback)(struct loop_device *, char *)) | |
745 | { | |
34dd82af KS |
746 | struct gendisk *disk = dev_to_disk(dev); |
747 | struct loop_device *lo = disk->private_data; | |
ee862730 | 748 | |
34dd82af | 749 | return callback(lo, page); |
ee862730 MB |
750 | } |
751 | ||
752 | #define LOOP_ATTR_RO(_name) \ | |
753 | static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ | |
754 | static ssize_t loop_attr_do_show_##_name(struct device *d, \ | |
755 | struct device_attribute *attr, char *b) \ | |
756 | { \ | |
757 | return loop_attr_show(d, b, loop_attr_##_name##_show); \ | |
758 | } \ | |
759 | static struct device_attribute loop_attr_##_name = \ | |
5657a819 | 760 | __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); |
ee862730 MB |
761 | |
762 | static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) | |
763 | { | |
764 | ssize_t ret; | |
765 | char *p = NULL; | |
766 | ||
05eb0f25 | 767 | spin_lock_irq(&lo->lo_lock); |
ee862730 | 768 | if (lo->lo_backing_file) |
9bf39ab2 | 769 | p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); |
05eb0f25 | 770 | spin_unlock_irq(&lo->lo_lock); |
ee862730 MB |
771 | |
772 | if (IS_ERR_OR_NULL(p)) | |
773 | ret = PTR_ERR(p); | |
774 | else { | |
775 | ret = strlen(p); | |
776 | memmove(buf, p, ret); | |
777 | buf[ret++] = '\n'; | |
778 | buf[ret] = 0; | |
779 | } | |
780 | ||
781 | return ret; | |
782 | } | |
783 | ||
784 | static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) | |
785 | { | |
786 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); | |
787 | } | |
788 | ||
789 | static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) | |
790 | { | |
791 | return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); | |
792 | } | |
793 | ||
794 | static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) | |
795 | { | |
796 | int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); | |
797 | ||
798 | return sprintf(buf, "%s\n", autoclear ? "1" : "0"); | |
799 | } | |
800 | ||
e03c8dd1 KS |
801 | static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) |
802 | { | |
803 | int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); | |
804 | ||
805 | return sprintf(buf, "%s\n", partscan ? "1" : "0"); | |
806 | } | |
807 | ||
2e5ab5f3 ML |
808 | static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) |
809 | { | |
810 | int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); | |
811 | ||
812 | return sprintf(buf, "%s\n", dio ? "1" : "0"); | |
813 | } | |
814 | ||
ee862730 MB |
815 | LOOP_ATTR_RO(backing_file); |
816 | LOOP_ATTR_RO(offset); | |
817 | LOOP_ATTR_RO(sizelimit); | |
818 | LOOP_ATTR_RO(autoclear); | |
e03c8dd1 | 819 | LOOP_ATTR_RO(partscan); |
2e5ab5f3 | 820 | LOOP_ATTR_RO(dio); |
ee862730 MB |
821 | |
822 | static struct attribute *loop_attrs[] = { | |
823 | &loop_attr_backing_file.attr, | |
824 | &loop_attr_offset.attr, | |
825 | &loop_attr_sizelimit.attr, | |
826 | &loop_attr_autoclear.attr, | |
e03c8dd1 | 827 | &loop_attr_partscan.attr, |
2e5ab5f3 | 828 | &loop_attr_dio.attr, |
ee862730 MB |
829 | NULL, |
830 | }; | |
831 | ||
832 | static struct attribute_group loop_attribute_group = { | |
833 | .name = "loop", | |
834 | .attrs= loop_attrs, | |
835 | }; | |
836 | ||
d3349b6b | 837 | static void loop_sysfs_init(struct loop_device *lo) |
ee862730 | 838 | { |
d3349b6b TH |
839 | lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, |
840 | &loop_attribute_group); | |
ee862730 MB |
841 | } |
842 | ||
843 | static void loop_sysfs_exit(struct loop_device *lo) | |
844 | { | |
d3349b6b TH |
845 | if (lo->sysfs_inited) |
846 | sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, | |
847 | &loop_attribute_group); | |
ee862730 MB |
848 | } |
849 | ||
dfaa2ef6 LC |
850 | static void loop_config_discard(struct loop_device *lo) |
851 | { | |
852 | struct file *file = lo->lo_backing_file; | |
853 | struct inode *inode = file->f_mapping->host; | |
854 | struct request_queue *q = lo->lo_queue; | |
855 | ||
856 | /* | |
857 | * We use punch hole to reclaim the free space used by the | |
12a64d2f | 858 | * image a.k.a. discard. However we do not support discard if |
dfaa2ef6 LC |
859 | * encryption is enabled, because it may give an attacker |
860 | * useful information. | |
861 | */ | |
862 | if ((!file->f_op->fallocate) || | |
863 | lo->lo_encrypt_key_size) { | |
864 | q->limits.discard_granularity = 0; | |
865 | q->limits.discard_alignment = 0; | |
2bb4cd5c | 866 | blk_queue_max_discard_sectors(q, 0); |
19372e27 | 867 | blk_queue_max_write_zeroes_sectors(q, 0); |
8b904b5b | 868 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); |
dfaa2ef6 LC |
869 | return; |
870 | } | |
871 | ||
872 | q->limits.discard_granularity = inode->i_sb->s_blocksize; | |
dfaf3c03 | 873 | q->limits.discard_alignment = 0; |
f2c6df7d | 874 | |
1e6ec9ea OS |
875 | blk_queue_max_discard_sectors(q, UINT_MAX >> 9); |
876 | blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); | |
8b904b5b | 877 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
dfaa2ef6 LC |
878 | } |
879 | ||
e03a3d7a ML |
880 | static void loop_unprepare_queue(struct loop_device *lo) |
881 | { | |
3989144f | 882 | kthread_flush_worker(&lo->worker); |
e03a3d7a ML |
883 | kthread_stop(lo->worker_task); |
884 | } | |
885 | ||
b2ee7d46 N |
886 | static int loop_kthread_worker_fn(void *worker_ptr) |
887 | { | |
888 | current->flags |= PF_LESS_THROTTLE; | |
889 | return kthread_worker_fn(worker_ptr); | |
890 | } | |
891 | ||
e03a3d7a ML |
892 | static int loop_prepare_queue(struct loop_device *lo) |
893 | { | |
3989144f | 894 | kthread_init_worker(&lo->worker); |
b2ee7d46 | 895 | lo->worker_task = kthread_run(loop_kthread_worker_fn, |
e03a3d7a ML |
896 | &lo->worker, "loop%d", lo->lo_number); |
897 | if (IS_ERR(lo->worker_task)) | |
898 | return -ENOMEM; | |
899 | set_user_nice(lo->worker_task, MIN_NICE); | |
900 | return 0; | |
901 | } | |
902 | ||
bb214884 | 903 | static int loop_set_fd(struct loop_device *lo, fmode_t mode, |
1da177e4 LT |
904 | struct block_device *bdev, unsigned int arg) |
905 | { | |
d2ac838e | 906 | struct file *file; |
1da177e4 LT |
907 | struct inode *inode; |
908 | struct address_space *mapping; | |
1da177e4 LT |
909 | int lo_flags = 0; |
910 | int error; | |
911 | loff_t size; | |
912 | ||
913 | /* This is safe, since we have a reference from open(). */ | |
914 | __module_get(THIS_MODULE); | |
915 | ||
916 | error = -EBADF; | |
917 | file = fget(arg); | |
918 | if (!file) | |
919 | goto out; | |
920 | ||
921 | error = -EBUSY; | |
922 | if (lo->lo_state != Lo_unbound) | |
923 | goto out_putf; | |
924 | ||
d2ac838e TT |
925 | error = loop_validate_file(file, bdev); |
926 | if (error) | |
927 | goto out_putf; | |
1da177e4 LT |
928 | |
929 | mapping = file->f_mapping; | |
930 | inode = mapping->host; | |
931 | ||
456be148 | 932 | if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || |
283e7e5d | 933 | !file->f_op->write_iter) |
456be148 | 934 | lo_flags |= LO_FLAGS_READ_ONLY; |
ba52de12 | 935 | |
456be148 | 936 | error = -EFBIG; |
1da177e4 | 937 | size = get_loop_size(lo, file); |
456be148 | 938 | if ((loff_t)(sector_t)size != size) |
1da177e4 | 939 | goto out_putf; |
e03a3d7a ML |
940 | error = loop_prepare_queue(lo); |
941 | if (error) | |
f4aa4c7b | 942 | goto out_putf; |
1da177e4 | 943 | |
456be148 | 944 | error = 0; |
1da177e4 LT |
945 | |
946 | set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); | |
947 | ||
2e5ab5f3 | 948 | lo->use_dio = false; |
1da177e4 LT |
949 | lo->lo_device = bdev; |
950 | lo->lo_flags = lo_flags; | |
951 | lo->lo_backing_file = file; | |
aa4d8616 | 952 | lo->transfer = NULL; |
1da177e4 LT |
953 | lo->ioctl = NULL; |
954 | lo->lo_sizelimit = 0; | |
955 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | |
956 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | |
957 | ||
68db1961 | 958 | if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) |
21d0727f | 959 | blk_queue_write_cache(lo->lo_queue, true, false); |
68db1961 | 960 | |
2e5ab5f3 | 961 | loop_update_dio(lo); |
73285082 | 962 | set_capacity(lo->lo_disk, size); |
1da177e4 | 963 | bd_set_size(bdev, size << 9); |
ee862730 | 964 | loop_sysfs_init(lo); |
c3473c63 DZ |
965 | /* let user-space know about the new size */ |
966 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); | |
1da177e4 | 967 | |
8a0740c4 OS |
968 | set_blocksize(bdev, S_ISBLK(inode->i_mode) ? |
969 | block_size(inode->i_bdev) : PAGE_SIZE); | |
1da177e4 | 970 | |
6c997918 | 971 | lo->lo_state = Lo_bound; |
e03c8dd1 KS |
972 | if (part_shift) |
973 | lo->lo_flags |= LO_FLAGS_PARTSCAN; | |
974 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) | |
06f0e9e6 | 975 | loop_reread_partitions(lo, bdev); |
c1681bf8 AP |
976 | |
977 | /* Grab the block_device to prevent its destruction after we | |
a2505b79 | 978 | * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). |
c1681bf8 AP |
979 | */ |
980 | bdgrab(bdev); | |
1da177e4 LT |
981 | return 0; |
982 | ||
983 | out_putf: | |
984 | fput(file); | |
985 | out: | |
986 | /* This is safe: open() is still holding a reference. */ | |
987 | module_put(THIS_MODULE); | |
988 | return error; | |
989 | } | |
990 | ||
991 | static int | |
992 | loop_release_xfer(struct loop_device *lo) | |
993 | { | |
994 | int err = 0; | |
995 | struct loop_func_table *xfer = lo->lo_encryption; | |
996 | ||
997 | if (xfer) { | |
998 | if (xfer->release) | |
999 | err = xfer->release(lo); | |
1000 | lo->transfer = NULL; | |
1001 | lo->lo_encryption = NULL; | |
1002 | module_put(xfer->owner); | |
1003 | } | |
1004 | return err; | |
1005 | } | |
1006 | ||
1007 | static int | |
1008 | loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, | |
1009 | const struct loop_info64 *i) | |
1010 | { | |
1011 | int err = 0; | |
1012 | ||
1013 | if (xfer) { | |
1014 | struct module *owner = xfer->owner; | |
1015 | ||
1016 | if (!try_module_get(owner)) | |
1017 | return -EINVAL; | |
1018 | if (xfer->init) | |
1019 | err = xfer->init(lo, i); | |
1020 | if (err) | |
1021 | module_put(owner); | |
1022 | else | |
1023 | lo->lo_encryption = xfer; | |
1024 | } | |
1025 | return err; | |
1026 | } | |
1027 | ||
a2505b79 | 1028 | static int __loop_clr_fd(struct loop_device *lo) |
1da177e4 LT |
1029 | { |
1030 | struct file *filp = lo->lo_backing_file; | |
b4e3ca1a | 1031 | gfp_t gfp = lo->old_gfp_mask; |
4c823cc3 | 1032 | struct block_device *bdev = lo->lo_device; |
1da177e4 | 1033 | |
a2505b79 | 1034 | if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) |
1da177e4 LT |
1035 | return -ENXIO; |
1036 | ||
1da177e4 LT |
1037 | if (filp == NULL) |
1038 | return -EINVAL; | |
1039 | ||
f8933667 ML |
1040 | /* freeze request queue during the transition */ |
1041 | blk_mq_freeze_queue(lo->lo_queue); | |
1042 | ||
1da177e4 | 1043 | spin_lock_irq(&lo->lo_lock); |
1da177e4 | 1044 | lo->lo_backing_file = NULL; |
05eb0f25 | 1045 | spin_unlock_irq(&lo->lo_lock); |
1da177e4 LT |
1046 | |
1047 | loop_release_xfer(lo); | |
1048 | lo->transfer = NULL; | |
1049 | lo->ioctl = NULL; | |
1050 | lo->lo_device = NULL; | |
1051 | lo->lo_encryption = NULL; | |
1052 | lo->lo_offset = 0; | |
1053 | lo->lo_sizelimit = 0; | |
1054 | lo->lo_encrypt_key_size = 0; | |
1da177e4 LT |
1055 | memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); |
1056 | memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); | |
1057 | memset(lo->lo_file_name, 0, LO_NAME_SIZE); | |
89e4fdec | 1058 | blk_queue_logical_block_size(lo->lo_queue, 512); |
bf093753 OS |
1059 | blk_queue_physical_block_size(lo->lo_queue, 512); |
1060 | blk_queue_io_min(lo->lo_queue, 512); | |
c1681bf8 AP |
1061 | if (bdev) { |
1062 | bdput(bdev); | |
bb214884 | 1063 | invalidate_bdev(bdev); |
eedffa28 | 1064 | bdev->bd_inode->i_mapping->wb_err = 0; |
c1681bf8 | 1065 | } |
73285082 | 1066 | set_capacity(lo->lo_disk, 0); |
51a0bb0c | 1067 | loop_sysfs_exit(lo); |
c3473c63 | 1068 | if (bdev) { |
bb214884 | 1069 | bd_set_size(bdev, 0); |
c3473c63 DZ |
1070 | /* let user-space know about this change */ |
1071 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); | |
1072 | } | |
1da177e4 LT |
1073 | mapping_set_gfp_mask(filp->f_mapping, gfp); |
1074 | lo->lo_state = Lo_unbound; | |
1da177e4 LT |
1075 | /* This is safe: open() is still holding a reference. */ |
1076 | module_put(THIS_MODULE); | |
f8933667 ML |
1077 | blk_mq_unfreeze_queue(lo->lo_queue); |
1078 | ||
c2fccc1c | 1079 | if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) |
06f0e9e6 | 1080 | loop_reread_partitions(lo, bdev); |
e03c8dd1 KS |
1081 | lo->lo_flags = 0; |
1082 | if (!part_shift) | |
1083 | lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; | |
e03a3d7a | 1084 | loop_unprepare_queue(lo); |
310ca162 | 1085 | mutex_unlock(&loop_ctl_mutex); |
f028f3b2 | 1086 | /* |
310ca162 TH |
1087 | * Need not hold loop_ctl_mutex to fput backing file. |
1088 | * Calling fput holding loop_ctl_mutex triggers a circular | |
f028f3b2 | 1089 | * lock dependency possibility warning as fput can take |
310ca162 | 1090 | * bd_mutex which is usually taken before loop_ctl_mutex. |
f028f3b2 NK |
1091 | */ |
1092 | fput(filp); | |
1da177e4 LT |
1093 | return 0; |
1094 | } | |
1095 | ||
a2505b79 JK |
1096 | static int loop_clr_fd(struct loop_device *lo) |
1097 | { | |
1098 | if (lo->lo_state != Lo_bound) | |
1099 | return -ENXIO; | |
1100 | /* | |
1101 | * If we've explicitly asked to tear down the loop device, | |
1102 | * and it has an elevated reference count, set it for auto-teardown when | |
1103 | * the last reference goes away. This stops $!~#$@ udev from | |
1104 | * preventing teardown because it decided that it needs to run blkid on | |
1105 | * the loopback device whenever they appear. xfstests is notorious for | |
1106 | * failing tests because blkid via udev races with a losetup | |
1107 | * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d | |
1108 | * command to fail with EBUSY. | |
1109 | */ | |
1110 | if (atomic_read(&lo->lo_refcnt) > 1) { | |
1111 | lo->lo_flags |= LO_FLAGS_AUTOCLEAR; | |
1112 | mutex_unlock(&loop_ctl_mutex); | |
1113 | return 0; | |
1114 | } | |
1115 | lo->lo_state = Lo_rundown; | |
1116 | ||
1117 | return __loop_clr_fd(lo); | |
1118 | } | |
1119 | ||
1da177e4 LT |
1120 | static int |
1121 | loop_set_status(struct loop_device *lo, const struct loop_info64 *info) | |
1122 | { | |
1123 | int err; | |
1124 | struct loop_func_table *xfer; | |
e4849737 | 1125 | kuid_t uid = current_uid(); |
1da177e4 | 1126 | |
b0fafa81 | 1127 | if (lo->lo_encrypt_key_size && |
e4849737 | 1128 | !uid_eq(lo->lo_key_owner, uid) && |
1da177e4 LT |
1129 | !capable(CAP_SYS_ADMIN)) |
1130 | return -EPERM; | |
1131 | if (lo->lo_state != Lo_bound) | |
1132 | return -ENXIO; | |
1133 | if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) | |
1134 | return -EINVAL; | |
1135 | ||
ecdd0959 ML |
1136 | /* I/O need to be drained during transfer transition */ |
1137 | blk_mq_freeze_queue(lo->lo_queue); | |
1138 | ||
1da177e4 LT |
1139 | err = loop_release_xfer(lo); |
1140 | if (err) | |
ecdd0959 | 1141 | goto exit; |
1da177e4 LT |
1142 | |
1143 | if (info->lo_encrypt_type) { | |
1144 | unsigned int type = info->lo_encrypt_type; | |
1145 | ||
1e047eaa TH |
1146 | if (type >= MAX_LO_CRYPT) { |
1147 | err = -EINVAL; | |
1148 | goto exit; | |
1149 | } | |
1da177e4 | 1150 | xfer = xfer_funcs[type]; |
1e047eaa TH |
1151 | if (xfer == NULL) { |
1152 | err = -EINVAL; | |
1153 | goto exit; | |
1154 | } | |
1da177e4 LT |
1155 | } else |
1156 | xfer = NULL; | |
1157 | ||
1158 | err = loop_init_xfer(lo, xfer, info); | |
1159 | if (err) | |
ecdd0959 | 1160 | goto exit; |
1da177e4 LT |
1161 | |
1162 | if (lo->lo_offset != info->lo_offset || | |
1e6ec9ea OS |
1163 | lo->lo_sizelimit != info->lo_sizelimit) { |
1164 | if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { | |
ecdd0959 ML |
1165 | err = -EFBIG; |
1166 | goto exit; | |
1167 | } | |
b040ad9c | 1168 | } |
541c742a | 1169 | |
dfaa2ef6 | 1170 | loop_config_discard(lo); |
1da177e4 LT |
1171 | |
1172 | memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); | |
1173 | memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); | |
1174 | lo->lo_file_name[LO_NAME_SIZE-1] = 0; | |
1175 | lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; | |
1176 | ||
1177 | if (!xfer) | |
1178 | xfer = &none_funcs; | |
1179 | lo->transfer = xfer->transfer; | |
1180 | lo->ioctl = xfer->ioctl; | |
1181 | ||
96c58655 DW |
1182 | if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != |
1183 | (info->lo_flags & LO_FLAGS_AUTOCLEAR)) | |
1184 | lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; | |
1185 | ||
1da177e4 LT |
1186 | lo->lo_encrypt_key_size = info->lo_encrypt_key_size; |
1187 | lo->lo_init[0] = info->lo_init[0]; | |
1188 | lo->lo_init[1] = info->lo_init[1]; | |
1189 | if (info->lo_encrypt_key_size) { | |
1190 | memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, | |
1191 | info->lo_encrypt_key_size); | |
b0fafa81 | 1192 | lo->lo_key_owner = uid; |
aa4d8616 | 1193 | } |
1da177e4 | 1194 | |
2e5ab5f3 ML |
1195 | /* update dio if lo_offset or transfer is changed */ |
1196 | __loop_update_dio(lo, lo->use_dio); | |
1197 | ||
ecdd0959 ML |
1198 | exit: |
1199 | blk_mq_unfreeze_queue(lo->lo_queue); | |
e02898b4 OS |
1200 | |
1201 | if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && | |
1202 | !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { | |
1203 | lo->lo_flags |= LO_FLAGS_PARTSCAN; | |
1204 | lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; | |
1205 | loop_reread_partitions(lo, lo->lo_device); | |
1206 | } | |
1207 | ||
ecdd0959 | 1208 | return err; |
1da177e4 LT |
1209 | } |
1210 | ||
1211 | static int | |
1212 | loop_get_status(struct loop_device *lo, struct loop_info64 *info) | |
1213 | { | |
b1ab5fa3 | 1214 | struct path path; |
1da177e4 | 1215 | struct kstat stat; |
2d1d4c1e | 1216 | int ret; |
1da177e4 | 1217 | |
2d1d4c1e | 1218 | if (lo->lo_state != Lo_bound) { |
310ca162 | 1219 | mutex_unlock(&loop_ctl_mutex); |
1da177e4 | 1220 | return -ENXIO; |
2d1d4c1e OS |
1221 | } |
1222 | ||
1da177e4 LT |
1223 | memset(info, 0, sizeof(*info)); |
1224 | info->lo_number = lo->lo_number; | |
1da177e4 LT |
1225 | info->lo_offset = lo->lo_offset; |
1226 | info->lo_sizelimit = lo->lo_sizelimit; | |
1227 | info->lo_flags = lo->lo_flags; | |
1228 | memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); | |
1229 | memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); | |
1230 | info->lo_encrypt_type = | |
1231 | lo->lo_encryption ? lo->lo_encryption->number : 0; | |
1232 | if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { | |
1233 | info->lo_encrypt_key_size = lo->lo_encrypt_key_size; | |
1234 | memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, | |
1235 | lo->lo_encrypt_key_size); | |
1236 | } | |
2d1d4c1e | 1237 | |
310ca162 | 1238 | /* Drop loop_ctl_mutex while we call into the filesystem. */ |
b1ab5fa3 TH |
1239 | path = lo->lo_backing_file->f_path; |
1240 | path_get(&path); | |
310ca162 | 1241 | mutex_unlock(&loop_ctl_mutex); |
b1ab5fa3 | 1242 | ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); |
2d1d4c1e OS |
1243 | if (!ret) { |
1244 | info->lo_device = huge_encode_dev(stat.dev); | |
1245 | info->lo_inode = stat.ino; | |
1246 | info->lo_rdevice = huge_encode_dev(stat.rdev); | |
1247 | } | |
b1ab5fa3 | 1248 | path_put(&path); |
2d1d4c1e | 1249 | return ret; |
1da177e4 LT |
1250 | } |
1251 | ||
1252 | static void | |
1253 | loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) | |
1254 | { | |
1255 | memset(info64, 0, sizeof(*info64)); | |
1256 | info64->lo_number = info->lo_number; | |
1257 | info64->lo_device = info->lo_device; | |
1258 | info64->lo_inode = info->lo_inode; | |
1259 | info64->lo_rdevice = info->lo_rdevice; | |
1260 | info64->lo_offset = info->lo_offset; | |
1261 | info64->lo_sizelimit = 0; | |
1262 | info64->lo_encrypt_type = info->lo_encrypt_type; | |
1263 | info64->lo_encrypt_key_size = info->lo_encrypt_key_size; | |
1264 | info64->lo_flags = info->lo_flags; | |
1265 | info64->lo_init[0] = info->lo_init[0]; | |
1266 | info64->lo_init[1] = info->lo_init[1]; | |
1267 | if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) | |
1268 | memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); | |
1269 | else | |
1270 | memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); | |
1271 | memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); | |
1272 | } | |
1273 | ||
1274 | static int | |
1275 | loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) | |
1276 | { | |
1277 | memset(info, 0, sizeof(*info)); | |
1278 | info->lo_number = info64->lo_number; | |
1279 | info->lo_device = info64->lo_device; | |
1280 | info->lo_inode = info64->lo_inode; | |
1281 | info->lo_rdevice = info64->lo_rdevice; | |
1282 | info->lo_offset = info64->lo_offset; | |
1283 | info->lo_encrypt_type = info64->lo_encrypt_type; | |
1284 | info->lo_encrypt_key_size = info64->lo_encrypt_key_size; | |
1285 | info->lo_flags = info64->lo_flags; | |
1286 | info->lo_init[0] = info64->lo_init[0]; | |
1287 | info->lo_init[1] = info64->lo_init[1]; | |
1288 | if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) | |
1289 | memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); | |
1290 | else | |
1291 | memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); | |
1292 | memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); | |
1293 | ||
1294 | /* error in case values were truncated */ | |
1295 | if (info->lo_device != info64->lo_device || | |
1296 | info->lo_rdevice != info64->lo_rdevice || | |
1297 | info->lo_inode != info64->lo_inode || | |
1298 | info->lo_offset != info64->lo_offset) | |
1299 | return -EOVERFLOW; | |
1300 | ||
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | static int | |
1305 | loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) | |
1306 | { | |
1307 | struct loop_info info; | |
1308 | struct loop_info64 info64; | |
1309 | ||
1310 | if (copy_from_user(&info, arg, sizeof (struct loop_info))) | |
1311 | return -EFAULT; | |
1312 | loop_info64_from_old(&info, &info64); | |
1313 | return loop_set_status(lo, &info64); | |
1314 | } | |
1315 | ||
1316 | static int | |
1317 | loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) | |
1318 | { | |
1319 | struct loop_info64 info64; | |
1320 | ||
1321 | if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) | |
1322 | return -EFAULT; | |
1323 | return loop_set_status(lo, &info64); | |
1324 | } | |
1325 | ||
1326 | static int | |
1327 | loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { | |
1328 | struct loop_info info; | |
1329 | struct loop_info64 info64; | |
bdac616d | 1330 | int err; |
1da177e4 | 1331 | |
bdac616d | 1332 | if (!arg) { |
310ca162 | 1333 | mutex_unlock(&loop_ctl_mutex); |
bdac616d OS |
1334 | return -EINVAL; |
1335 | } | |
1336 | err = loop_get_status(lo, &info64); | |
1da177e4 LT |
1337 | if (!err) |
1338 | err = loop_info64_to_old(&info64, &info); | |
1339 | if (!err && copy_to_user(arg, &info, sizeof(info))) | |
1340 | err = -EFAULT; | |
1341 | ||
1342 | return err; | |
1343 | } | |
1344 | ||
1345 | static int | |
1346 | loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { | |
1347 | struct loop_info64 info64; | |
bdac616d | 1348 | int err; |
1da177e4 | 1349 | |
bdac616d | 1350 | if (!arg) { |
310ca162 | 1351 | mutex_unlock(&loop_ctl_mutex); |
bdac616d OS |
1352 | return -EINVAL; |
1353 | } | |
1354 | err = loop_get_status(lo, &info64); | |
1da177e4 LT |
1355 | if (!err && copy_to_user(arg, &info64, sizeof(info64))) |
1356 | err = -EFAULT; | |
1357 | ||
1358 | return err; | |
1359 | } | |
1360 | ||
51001b7d | 1361 | static int loop_set_capacity(struct loop_device *lo) |
53d66608 | 1362 | { |
53d66608 | 1363 | if (unlikely(lo->lo_state != Lo_bound)) |
7b0576a3 | 1364 | return -ENXIO; |
53d66608 | 1365 | |
1e6ec9ea | 1366 | return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); |
53d66608 O |
1367 | } |
1368 | ||
ab1cb278 ML |
1369 | static int loop_set_dio(struct loop_device *lo, unsigned long arg) |
1370 | { | |
1371 | int error = -ENXIO; | |
1372 | if (lo->lo_state != Lo_bound) | |
1373 | goto out; | |
1374 | ||
1375 | __loop_update_dio(lo, !!arg); | |
1376 | if (lo->use_dio == !!arg) | |
1377 | return 0; | |
1378 | error = -EINVAL; | |
1379 | out: | |
1380 | return error; | |
1381 | } | |
1382 | ||
89e4fdec OS |
1383 | static int loop_set_block_size(struct loop_device *lo, unsigned long arg) |
1384 | { | |
1385 | if (lo->lo_state != Lo_bound) | |
1386 | return -ENXIO; | |
1387 | ||
1388 | if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) | |
1389 | return -EINVAL; | |
1390 | ||
1391 | blk_mq_freeze_queue(lo->lo_queue); | |
1392 | ||
1393 | blk_queue_logical_block_size(lo->lo_queue, arg); | |
bf093753 OS |
1394 | blk_queue_physical_block_size(lo->lo_queue, arg); |
1395 | blk_queue_io_min(lo->lo_queue, arg); | |
89e4fdec OS |
1396 | loop_update_dio(lo); |
1397 | ||
1398 | blk_mq_unfreeze_queue(lo->lo_queue); | |
1399 | ||
1400 | return 0; | |
1401 | } | |
1402 | ||
a1316544 JK |
1403 | static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, |
1404 | unsigned long arg) | |
1da177e4 | 1405 | { |
1da177e4 LT |
1406 | int err; |
1407 | ||
310ca162 | 1408 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
3148ffbd | 1409 | if (err) |
a1316544 JK |
1410 | return err; |
1411 | switch (cmd) { | |
1412 | case LOOP_SET_CAPACITY: | |
1413 | err = loop_set_capacity(lo); | |
1414 | break; | |
1415 | case LOOP_SET_DIRECT_IO: | |
1416 | err = loop_set_dio(lo, arg); | |
1417 | break; | |
1418 | case LOOP_SET_BLOCK_SIZE: | |
1419 | err = loop_set_block_size(lo, arg); | |
1420 | break; | |
1421 | default: | |
1422 | err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; | |
1423 | } | |
1424 | mutex_unlock(&loop_ctl_mutex); | |
1425 | return err; | |
1426 | } | |
1427 | ||
1428 | static int lo_ioctl(struct block_device *bdev, fmode_t mode, | |
1429 | unsigned int cmd, unsigned long arg) | |
1430 | { | |
1431 | struct loop_device *lo = bdev->bd_disk->private_data; | |
1432 | int err; | |
3148ffbd | 1433 | |
1da177e4 LT |
1434 | switch (cmd) { |
1435 | case LOOP_SET_FD: | |
a1316544 JK |
1436 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
1437 | if (err) | |
1438 | return err; | |
bb214884 | 1439 | err = loop_set_fd(lo, mode, bdev, arg); |
a1316544 | 1440 | mutex_unlock(&loop_ctl_mutex); |
1da177e4 LT |
1441 | break; |
1442 | case LOOP_CHANGE_FD: | |
a1316544 JK |
1443 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
1444 | if (err) | |
1445 | return err; | |
bb214884 | 1446 | err = loop_change_fd(lo, bdev, arg); |
a1316544 | 1447 | mutex_unlock(&loop_ctl_mutex); |
1da177e4 LT |
1448 | break; |
1449 | case LOOP_CLR_FD: | |
a1316544 JK |
1450 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
1451 | if (err) | |
1452 | return err; | |
310ca162 | 1453 | /* loop_clr_fd would have unlocked loop_ctl_mutex on success */ |
4c823cc3 | 1454 | err = loop_clr_fd(lo); |
a1316544 JK |
1455 | if (err) |
1456 | mutex_unlock(&loop_ctl_mutex); | |
1da177e4 LT |
1457 | break; |
1458 | case LOOP_SET_STATUS: | |
7035b5df | 1459 | err = -EPERM; |
a1316544 JK |
1460 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { |
1461 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); | |
1462 | if (err) | |
1463 | return err; | |
7035b5df DM |
1464 | err = loop_set_status_old(lo, |
1465 | (struct loop_info __user *)arg); | |
a1316544 JK |
1466 | mutex_unlock(&loop_ctl_mutex); |
1467 | } | |
1da177e4 LT |
1468 | break; |
1469 | case LOOP_GET_STATUS: | |
a1316544 JK |
1470 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
1471 | if (err) | |
1472 | return err; | |
1da177e4 | 1473 | err = loop_get_status_old(lo, (struct loop_info __user *) arg); |
310ca162 | 1474 | /* loop_get_status() unlocks loop_ctl_mutex */ |
a1316544 | 1475 | break; |
1da177e4 | 1476 | case LOOP_SET_STATUS64: |
7035b5df | 1477 | err = -EPERM; |
a1316544 JK |
1478 | if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { |
1479 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); | |
1480 | if (err) | |
1481 | return err; | |
7035b5df DM |
1482 | err = loop_set_status64(lo, |
1483 | (struct loop_info64 __user *) arg); | |
a1316544 JK |
1484 | mutex_unlock(&loop_ctl_mutex); |
1485 | } | |
1da177e4 LT |
1486 | break; |
1487 | case LOOP_GET_STATUS64: | |
a1316544 JK |
1488 | err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); |
1489 | if (err) | |
1490 | return err; | |
1da177e4 | 1491 | err = loop_get_status64(lo, (struct loop_info64 __user *) arg); |
310ca162 | 1492 | /* loop_get_status() unlocks loop_ctl_mutex */ |
53d66608 | 1493 | break; |
a1316544 | 1494 | case LOOP_SET_CAPACITY: |
ab1cb278 | 1495 | case LOOP_SET_DIRECT_IO: |
89e4fdec | 1496 | case LOOP_SET_BLOCK_SIZE: |
a1316544 JK |
1497 | if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) |
1498 | return -EPERM; | |
1499 | /* Fall through */ | |
1da177e4 | 1500 | default: |
a1316544 JK |
1501 | err = lo_simple_ioctl(lo, cmd, arg); |
1502 | break; | |
1da177e4 | 1503 | } |
f028f3b2 | 1504 | |
1da177e4 LT |
1505 | return err; |
1506 | } | |
1507 | ||
863d5b82 DH |
1508 | #ifdef CONFIG_COMPAT |
1509 | struct compat_loop_info { | |
1510 | compat_int_t lo_number; /* ioctl r/o */ | |
1511 | compat_dev_t lo_device; /* ioctl r/o */ | |
1512 | compat_ulong_t lo_inode; /* ioctl r/o */ | |
1513 | compat_dev_t lo_rdevice; /* ioctl r/o */ | |
1514 | compat_int_t lo_offset; | |
1515 | compat_int_t lo_encrypt_type; | |
1516 | compat_int_t lo_encrypt_key_size; /* ioctl w/o */ | |
1517 | compat_int_t lo_flags; /* ioctl r/o */ | |
1518 | char lo_name[LO_NAME_SIZE]; | |
1519 | unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ | |
1520 | compat_ulong_t lo_init[2]; | |
1521 | char reserved[4]; | |
1522 | }; | |
1523 | ||
1524 | /* | |
1525 | * Transfer 32-bit compatibility structure in userspace to 64-bit loop info | |
1526 | * - noinlined to reduce stack space usage in main part of driver | |
1527 | */ | |
1528 | static noinline int | |
ba674cfc | 1529 | loop_info64_from_compat(const struct compat_loop_info __user *arg, |
863d5b82 DH |
1530 | struct loop_info64 *info64) |
1531 | { | |
1532 | struct compat_loop_info info; | |
1533 | ||
1534 | if (copy_from_user(&info, arg, sizeof(info))) | |
1535 | return -EFAULT; | |
1536 | ||
1537 | memset(info64, 0, sizeof(*info64)); | |
1538 | info64->lo_number = info.lo_number; | |
1539 | info64->lo_device = info.lo_device; | |
1540 | info64->lo_inode = info.lo_inode; | |
1541 | info64->lo_rdevice = info.lo_rdevice; | |
1542 | info64->lo_offset = info.lo_offset; | |
1543 | info64->lo_sizelimit = 0; | |
1544 | info64->lo_encrypt_type = info.lo_encrypt_type; | |
1545 | info64->lo_encrypt_key_size = info.lo_encrypt_key_size; | |
1546 | info64->lo_flags = info.lo_flags; | |
1547 | info64->lo_init[0] = info.lo_init[0]; | |
1548 | info64->lo_init[1] = info.lo_init[1]; | |
1549 | if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) | |
1550 | memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); | |
1551 | else | |
1552 | memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); | |
1553 | memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); | |
1554 | return 0; | |
1555 | } | |
1556 | ||
1557 | /* | |
1558 | * Transfer 64-bit loop info to 32-bit compatibility structure in userspace | |
1559 | * - noinlined to reduce stack space usage in main part of driver | |
1560 | */ | |
1561 | static noinline int | |
1562 | loop_info64_to_compat(const struct loop_info64 *info64, | |
1563 | struct compat_loop_info __user *arg) | |
1564 | { | |
1565 | struct compat_loop_info info; | |
1566 | ||
1567 | memset(&info, 0, sizeof(info)); | |
1568 | info.lo_number = info64->lo_number; | |
1569 | info.lo_device = info64->lo_device; | |
1570 | info.lo_inode = info64->lo_inode; | |
1571 | info.lo_rdevice = info64->lo_rdevice; | |
1572 | info.lo_offset = info64->lo_offset; | |
1573 | info.lo_encrypt_type = info64->lo_encrypt_type; | |
1574 | info.lo_encrypt_key_size = info64->lo_encrypt_key_size; | |
1575 | info.lo_flags = info64->lo_flags; | |
1576 | info.lo_init[0] = info64->lo_init[0]; | |
1577 | info.lo_init[1] = info64->lo_init[1]; | |
1578 | if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) | |
1579 | memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); | |
1580 | else | |
1581 | memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); | |
1582 | memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); | |
1583 | ||
1584 | /* error in case values were truncated */ | |
1585 | if (info.lo_device != info64->lo_device || | |
1586 | info.lo_rdevice != info64->lo_rdevice || | |
1587 | info.lo_inode != info64->lo_inode || | |
1588 | info.lo_offset != info64->lo_offset || | |
1589 | info.lo_init[0] != info64->lo_init[0] || | |
1590 | info.lo_init[1] != info64->lo_init[1]) | |
1591 | return -EOVERFLOW; | |
1592 | ||
1593 | if (copy_to_user(arg, &info, sizeof(info))) | |
1594 | return -EFAULT; | |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | static int | |
1599 | loop_set_status_compat(struct loop_device *lo, | |
1600 | const struct compat_loop_info __user *arg) | |
1601 | { | |
1602 | struct loop_info64 info64; | |
1603 | int ret; | |
1604 | ||
1605 | ret = loop_info64_from_compat(arg, &info64); | |
1606 | if (ret < 0) | |
1607 | return ret; | |
1608 | return loop_set_status(lo, &info64); | |
1609 | } | |
1610 | ||
1611 | static int | |
1612 | loop_get_status_compat(struct loop_device *lo, | |
1613 | struct compat_loop_info __user *arg) | |
1614 | { | |
1615 | struct loop_info64 info64; | |
bdac616d | 1616 | int err; |
863d5b82 | 1617 | |
bdac616d | 1618 | if (!arg) { |
310ca162 | 1619 | mutex_unlock(&loop_ctl_mutex); |
bdac616d OS |
1620 | return -EINVAL; |
1621 | } | |
1622 | err = loop_get_status(lo, &info64); | |
863d5b82 DH |
1623 | if (!err) |
1624 | err = loop_info64_to_compat(&info64, arg); | |
1625 | return err; | |
1626 | } | |
1627 | ||
bb214884 AV |
1628 | static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, |
1629 | unsigned int cmd, unsigned long arg) | |
863d5b82 | 1630 | { |
bb214884 | 1631 | struct loop_device *lo = bdev->bd_disk->private_data; |
863d5b82 DH |
1632 | int err; |
1633 | ||
863d5b82 DH |
1634 | switch(cmd) { |
1635 | case LOOP_SET_STATUS: | |
310ca162 | 1636 | err = mutex_lock_killable(&loop_ctl_mutex); |
3148ffbd OS |
1637 | if (!err) { |
1638 | err = loop_set_status_compat(lo, | |
1639 | (const struct compat_loop_info __user *)arg); | |
310ca162 | 1640 | mutex_unlock(&loop_ctl_mutex); |
3148ffbd | 1641 | } |
863d5b82 DH |
1642 | break; |
1643 | case LOOP_GET_STATUS: | |
310ca162 | 1644 | err = mutex_lock_killable(&loop_ctl_mutex); |
3148ffbd OS |
1645 | if (!err) { |
1646 | err = loop_get_status_compat(lo, | |
1647 | (struct compat_loop_info __user *)arg); | |
310ca162 | 1648 | /* loop_get_status() unlocks loop_ctl_mutex */ |
3148ffbd | 1649 | } |
863d5b82 | 1650 | break; |
53d66608 | 1651 | case LOOP_SET_CAPACITY: |
863d5b82 DH |
1652 | case LOOP_CLR_FD: |
1653 | case LOOP_GET_STATUS64: | |
1654 | case LOOP_SET_STATUS64: | |
1655 | arg = (unsigned long) compat_ptr(arg); | |
d893ff86 | 1656 | /* fall through */ |
863d5b82 DH |
1657 | case LOOP_SET_FD: |
1658 | case LOOP_CHANGE_FD: | |
9fea4b39 | 1659 | case LOOP_SET_BLOCK_SIZE: |
bb214884 | 1660 | err = lo_ioctl(bdev, mode, cmd, arg); |
863d5b82 DH |
1661 | break; |
1662 | default: | |
1663 | err = -ENOIOCTLCMD; | |
1664 | break; | |
1665 | } | |
863d5b82 DH |
1666 | return err; |
1667 | } | |
1668 | #endif | |
1669 | ||
bb214884 | 1670 | static int lo_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 1671 | { |
770fe30a | 1672 | struct loop_device *lo; |
0a42e99b | 1673 | int err; |
770fe30a | 1674 | |
0a42e99b JK |
1675 | err = mutex_lock_killable(&loop_ctl_mutex); |
1676 | if (err) | |
1677 | return err; | |
770fe30a KS |
1678 | lo = bdev->bd_disk->private_data; |
1679 | if (!lo) { | |
1680 | err = -ENXIO; | |
1681 | goto out; | |
1682 | } | |
1da177e4 | 1683 | |
f8933667 | 1684 | atomic_inc(&lo->lo_refcnt); |
770fe30a | 1685 | out: |
0a42e99b | 1686 | mutex_unlock(&loop_ctl_mutex); |
770fe30a | 1687 | return err; |
1da177e4 LT |
1688 | } |
1689 | ||
967d1dc1 | 1690 | static void lo_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 1691 | { |
967d1dc1 | 1692 | struct loop_device *lo; |
ffcd7dca | 1693 | int err; |
1da177e4 | 1694 | |
0a42e99b | 1695 | mutex_lock(&loop_ctl_mutex); |
967d1dc1 | 1696 | lo = disk->private_data; |
f8933667 | 1697 | if (atomic_dec_return(&lo->lo_refcnt)) |
0a42e99b | 1698 | goto out_unlock; |
14f27939 MB |
1699 | |
1700 | if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { | |
a2505b79 JK |
1701 | if (lo->lo_state != Lo_bound) |
1702 | goto out_unlock; | |
1703 | lo->lo_state = Lo_rundown; | |
14f27939 MB |
1704 | /* |
1705 | * In autoclear mode, stop the loop thread | |
1706 | * and remove configuration after last close. | |
1707 | */ | |
a2505b79 | 1708 | err = __loop_clr_fd(lo); |
ffcd7dca | 1709 | if (!err) |
0a42e99b | 1710 | return; |
43cade80 | 1711 | } else if (lo->lo_state == Lo_bound) { |
14f27939 MB |
1712 | /* |
1713 | * Otherwise keep thread (if running) and config, | |
1714 | * but flush possible ongoing bios in thread. | |
1715 | */ | |
43cade80 OS |
1716 | blk_mq_freeze_queue(lo->lo_queue); |
1717 | blk_mq_unfreeze_queue(lo->lo_queue); | |
14f27939 | 1718 | } |
96c58655 | 1719 | |
0a42e99b | 1720 | out_unlock: |
310ca162 | 1721 | mutex_unlock(&loop_ctl_mutex); |
ae665016 LT |
1722 | } |
1723 | ||
83d5cde4 | 1724 | static const struct block_device_operations lo_fops = { |
1da177e4 | 1725 | .owner = THIS_MODULE, |
bb214884 AV |
1726 | .open = lo_open, |
1727 | .release = lo_release, | |
1728 | .ioctl = lo_ioctl, | |
863d5b82 | 1729 | #ifdef CONFIG_COMPAT |
bb214884 | 1730 | .compat_ioctl = lo_compat_ioctl, |
863d5b82 | 1731 | #endif |
1da177e4 LT |
1732 | }; |
1733 | ||
1734 | /* | |
1735 | * And now the modules code and kernel interface. | |
1736 | */ | |
73285082 | 1737 | static int max_loop; |
5657a819 | 1738 | module_param(max_loop, int, 0444); |
a47653fc | 1739 | MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); |
5657a819 | 1740 | module_param(max_part, int, 0444); |
476a4813 | 1741 | MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); |
1da177e4 LT |
1742 | MODULE_LICENSE("GPL"); |
1743 | MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); | |
1744 | ||
1745 | int loop_register_transfer(struct loop_func_table *funcs) | |
1746 | { | |
1747 | unsigned int n = funcs->number; | |
1748 | ||
1749 | if (n >= MAX_LO_CRYPT || xfer_funcs[n]) | |
1750 | return -EINVAL; | |
1751 | xfer_funcs[n] = funcs; | |
1752 | return 0; | |
1753 | } | |
1754 | ||
34dd82af KS |
1755 | static int unregister_transfer_cb(int id, void *ptr, void *data) |
1756 | { | |
1757 | struct loop_device *lo = ptr; | |
1758 | struct loop_func_table *xfer = data; | |
1759 | ||
310ca162 | 1760 | mutex_lock(&loop_ctl_mutex); |
34dd82af KS |
1761 | if (lo->lo_encryption == xfer) |
1762 | loop_release_xfer(lo); | |
310ca162 | 1763 | mutex_unlock(&loop_ctl_mutex); |
34dd82af KS |
1764 | return 0; |
1765 | } | |
1766 | ||
1da177e4 LT |
1767 | int loop_unregister_transfer(int number) |
1768 | { | |
1769 | unsigned int n = number; | |
1da177e4 LT |
1770 | struct loop_func_table *xfer; |
1771 | ||
1772 | if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) | |
1773 | return -EINVAL; | |
1774 | ||
1775 | xfer_funcs[n] = NULL; | |
34dd82af | 1776 | idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); |
1da177e4 LT |
1777 | return 0; |
1778 | } | |
1779 | ||
1780 | EXPORT_SYMBOL(loop_register_transfer); | |
1781 | EXPORT_SYMBOL(loop_unregister_transfer); | |
1782 | ||
fc17b653 | 1783 | static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, |
b5dd2f60 ML |
1784 | const struct blk_mq_queue_data *bd) |
1785 | { | |
1894e916 JA |
1786 | struct request *rq = bd->rq; |
1787 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); | |
1788 | struct loop_device *lo = rq->q->queuedata; | |
b5dd2f60 | 1789 | |
1894e916 | 1790 | blk_mq_start_request(rq); |
b5dd2f60 | 1791 | |
f4aa4c7b | 1792 | if (lo->lo_state != Lo_bound) |
fc17b653 | 1793 | return BLK_STS_IOERR; |
f4aa4c7b | 1794 | |
1894e916 | 1795 | switch (req_op(rq)) { |
f0225cac CH |
1796 | case REQ_OP_FLUSH: |
1797 | case REQ_OP_DISCARD: | |
19372e27 | 1798 | case REQ_OP_WRITE_ZEROES: |
bc07c10a | 1799 | cmd->use_aio = false; |
f0225cac CH |
1800 | break; |
1801 | default: | |
1802 | cmd->use_aio = lo->use_dio; | |
1803 | break; | |
1804 | } | |
bc07c10a | 1805 | |
d4478e92 | 1806 | /* always use the first bio's css */ |
0b508bc9 | 1807 | #ifdef CONFIG_BLK_CGROUP |
b5f2954d DZ |
1808 | if (cmd->use_aio && rq->bio && rq->bio->bi_css) { |
1809 | cmd->css = rq->bio->bi_css; | |
d4478e92 SL |
1810 | css_get(cmd->css); |
1811 | } else | |
1812 | #endif | |
1813 | cmd->css = NULL; | |
3989144f | 1814 | kthread_queue_work(&lo->worker, &cmd->work); |
b5dd2f60 | 1815 | |
fc17b653 | 1816 | return BLK_STS_OK; |
b5dd2f60 ML |
1817 | } |
1818 | ||
1819 | static void loop_handle_cmd(struct loop_cmd *cmd) | |
1820 | { | |
1894e916 JA |
1821 | struct request *rq = blk_mq_rq_from_pdu(cmd); |
1822 | const bool write = op_is_write(req_op(rq)); | |
1823 | struct loop_device *lo = rq->q->queuedata; | |
f4829a9b | 1824 | int ret = 0; |
b5dd2f60 | 1825 | |
f4829a9b CH |
1826 | if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { |
1827 | ret = -EIO; | |
b5dd2f60 | 1828 | goto failed; |
f4829a9b | 1829 | } |
b5dd2f60 | 1830 | |
1894e916 | 1831 | ret = do_req_filebacked(lo, rq); |
b5dd2f60 | 1832 | failed: |
bc07c10a | 1833 | /* complete non-aio request */ |
fe2cb290 CH |
1834 | if (!cmd->use_aio || ret) { |
1835 | cmd->ret = ret ? -EIO : 0; | |
1894e916 | 1836 | blk_mq_complete_request(rq); |
fe2cb290 | 1837 | } |
b5dd2f60 ML |
1838 | } |
1839 | ||
e03a3d7a | 1840 | static void loop_queue_work(struct kthread_work *work) |
b5dd2f60 ML |
1841 | { |
1842 | struct loop_cmd *cmd = | |
e03a3d7a | 1843 | container_of(work, struct loop_cmd, work); |
b5dd2f60 ML |
1844 | |
1845 | loop_handle_cmd(cmd); | |
1846 | } | |
1847 | ||
d6296d39 CH |
1848 | static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, |
1849 | unsigned int hctx_idx, unsigned int numa_node) | |
b5dd2f60 ML |
1850 | { |
1851 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); | |
1852 | ||
3989144f | 1853 | kthread_init_work(&cmd->work, loop_queue_work); |
b5dd2f60 ML |
1854 | return 0; |
1855 | } | |
1856 | ||
f363b089 | 1857 | static const struct blk_mq_ops loop_mq_ops = { |
b5dd2f60 | 1858 | .queue_rq = loop_queue_rq, |
b5dd2f60 | 1859 | .init_request = loop_init_request, |
fe2cb290 | 1860 | .complete = lo_complete_rq, |
b5dd2f60 ML |
1861 | }; |
1862 | ||
34dd82af | 1863 | static int loop_add(struct loop_device **l, int i) |
73285082 KC |
1864 | { |
1865 | struct loop_device *lo; | |
1866 | struct gendisk *disk; | |
34dd82af | 1867 | int err; |
73285082 | 1868 | |
68d740d7 | 1869 | err = -ENOMEM; |
73285082 | 1870 | lo = kzalloc(sizeof(*lo), GFP_KERNEL); |
68d740d7 | 1871 | if (!lo) |
73285082 | 1872 | goto out; |
34dd82af | 1873 | |
ef7e7c82 MP |
1874 | lo->lo_state = Lo_unbound; |
1875 | ||
c718aa65 | 1876 | /* allocate id, if @id >= 0, we're requesting that specific id */ |
34dd82af | 1877 | if (i >= 0) { |
c718aa65 TH |
1878 | err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); |
1879 | if (err == -ENOSPC) | |
34dd82af | 1880 | err = -EEXIST; |
34dd82af | 1881 | } else { |
c718aa65 | 1882 | err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); |
34dd82af KS |
1883 | } |
1884 | if (err < 0) | |
1885 | goto out_free_dev; | |
c718aa65 | 1886 | i = err; |
73285082 | 1887 | |
183cfb57 | 1888 | err = -ENOMEM; |
b5dd2f60 ML |
1889 | lo->tag_set.ops = &loop_mq_ops; |
1890 | lo->tag_set.nr_hw_queues = 1; | |
1891 | lo->tag_set.queue_depth = 128; | |
1892 | lo->tag_set.numa_node = NUMA_NO_NODE; | |
1893 | lo->tag_set.cmd_size = sizeof(struct loop_cmd); | |
1894 | lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; | |
1895 | lo->tag_set.driver_data = lo; | |
1896 | ||
1897 | err = blk_mq_alloc_tag_set(&lo->tag_set); | |
1898 | if (err) | |
3ec981e3 | 1899 | goto out_free_idr; |
73285082 | 1900 | |
b5dd2f60 ML |
1901 | lo->lo_queue = blk_mq_init_queue(&lo->tag_set); |
1902 | if (IS_ERR_OR_NULL(lo->lo_queue)) { | |
1903 | err = PTR_ERR(lo->lo_queue); | |
1904 | goto out_cleanup_tags; | |
1905 | } | |
ef7e7c82 MP |
1906 | lo->lo_queue->queuedata = lo; |
1907 | ||
54bb0ade | 1908 | blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); |
40326d8a | 1909 | |
5b5e20f4 | 1910 | /* |
40326d8a SL |
1911 | * By default, we do buffer IO, so it doesn't make sense to enable |
1912 | * merge because the I/O submitted to backing file is handled page by | |
1913 | * page. For directio mode, merge does help to dispatch bigger request | |
1914 | * to underlayer disk. We will enable merge once directio is enabled. | |
5b5e20f4 | 1915 | */ |
8b904b5b | 1916 | blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); |
5b5e20f4 | 1917 | |
7a649737 | 1918 | err = -ENOMEM; |
476a4813 | 1919 | disk = lo->lo_disk = alloc_disk(1 << part_shift); |
73285082 KC |
1920 | if (!disk) |
1921 | goto out_free_queue; | |
1922 | ||
e03c8dd1 KS |
1923 | /* |
1924 | * Disable partition scanning by default. The in-kernel partition | |
1925 | * scanning can be requested individually per-device during its | |
1926 | * setup. Userspace can always add and remove partitions from all | |
1927 | * devices. The needed partition minors are allocated from the | |
1928 | * extended minor space, the main loop device numbers will continue | |
1929 | * to match the loop minors, regardless of the number of partitions | |
1930 | * used. | |
1931 | * | |
1932 | * If max_part is given, partition scanning is globally enabled for | |
1933 | * all loop devices. The minors for the main loop devices will be | |
1934 | * multiples of max_part. | |
1935 | * | |
1936 | * Note: Global-for-all-devices, set-only-at-init, read-only module | |
1937 | * parameteters like 'max_loop' and 'max_part' make things needlessly | |
1938 | * complicated, are too static, inflexible and may surprise | |
1939 | * userspace tools. Parameters like this in general should be avoided. | |
1940 | */ | |
1941 | if (!part_shift) | |
1942 | disk->flags |= GENHD_FL_NO_PART_SCAN; | |
1943 | disk->flags |= GENHD_FL_EXT_DEVT; | |
f8933667 | 1944 | atomic_set(&lo->lo_refcnt, 0); |
73285082 | 1945 | lo->lo_number = i; |
73285082 KC |
1946 | spin_lock_init(&lo->lo_lock); |
1947 | disk->major = LOOP_MAJOR; | |
476a4813 | 1948 | disk->first_minor = i << part_shift; |
73285082 KC |
1949 | disk->fops = &lo_fops; |
1950 | disk->private_data = lo; | |
1951 | disk->queue = lo->lo_queue; | |
1952 | sprintf(disk->disk_name, "loop%d", i); | |
34dd82af KS |
1953 | add_disk(disk); |
1954 | *l = lo; | |
1955 | return lo->lo_number; | |
73285082 KC |
1956 | |
1957 | out_free_queue: | |
1958 | blk_cleanup_queue(lo->lo_queue); | |
b5dd2f60 ML |
1959 | out_cleanup_tags: |
1960 | blk_mq_free_tag_set(&lo->tag_set); | |
3ec981e3 MP |
1961 | out_free_idr: |
1962 | idr_remove(&loop_index_idr, i); | |
73285082 KC |
1963 | out_free_dev: |
1964 | kfree(lo); | |
1965 | out: | |
34dd82af | 1966 | return err; |
73285082 KC |
1967 | } |
1968 | ||
34dd82af | 1969 | static void loop_remove(struct loop_device *lo) |
1da177e4 | 1970 | { |
6cd18e71 | 1971 | del_gendisk(lo->lo_disk); |
0fa8ebdd | 1972 | blk_cleanup_queue(lo->lo_queue); |
b5dd2f60 | 1973 | blk_mq_free_tag_set(&lo->tag_set); |
73285082 | 1974 | put_disk(lo->lo_disk); |
73285082 KC |
1975 | kfree(lo); |
1976 | } | |
1da177e4 | 1977 | |
770fe30a KS |
1978 | static int find_free_cb(int id, void *ptr, void *data) |
1979 | { | |
1980 | struct loop_device *lo = ptr; | |
1981 | struct loop_device **l = data; | |
1982 | ||
1983 | if (lo->lo_state == Lo_unbound) { | |
1984 | *l = lo; | |
1985 | return 1; | |
1986 | } | |
1987 | return 0; | |
1988 | } | |
1989 | ||
34dd82af | 1990 | static int loop_lookup(struct loop_device **l, int i) |
a47653fc KC |
1991 | { |
1992 | struct loop_device *lo; | |
34dd82af | 1993 | int ret = -ENODEV; |
a47653fc | 1994 | |
770fe30a KS |
1995 | if (i < 0) { |
1996 | int err; | |
1997 | ||
1998 | err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); | |
1999 | if (err == 1) { | |
2000 | *l = lo; | |
2001 | ret = lo->lo_number; | |
2002 | } | |
2003 | goto out; | |
a47653fc KC |
2004 | } |
2005 | ||
770fe30a | 2006 | /* lookup and return a specific i */ |
34dd82af | 2007 | lo = idr_find(&loop_index_idr, i); |
a47653fc | 2008 | if (lo) { |
34dd82af KS |
2009 | *l = lo; |
2010 | ret = lo->lo_number; | |
a47653fc | 2011 | } |
770fe30a | 2012 | out: |
34dd82af | 2013 | return ret; |
a47653fc KC |
2014 | } |
2015 | ||
73285082 KC |
2016 | static struct kobject *loop_probe(dev_t dev, int *part, void *data) |
2017 | { | |
705962cc | 2018 | struct loop_device *lo; |
07002e99 | 2019 | struct kobject *kobj; |
34dd82af | 2020 | int err; |
73285082 | 2021 | |
0a42e99b | 2022 | mutex_lock(&loop_ctl_mutex); |
34dd82af KS |
2023 | err = loop_lookup(&lo, MINOR(dev) >> part_shift); |
2024 | if (err < 0) | |
2025 | err = loop_add(&lo, MINOR(dev) >> part_shift); | |
2026 | if (err < 0) | |
a207f593 | 2027 | kobj = NULL; |
34dd82af | 2028 | else |
3079c22e | 2029 | kobj = get_disk_and_module(lo->lo_disk); |
0a42e99b | 2030 | mutex_unlock(&loop_ctl_mutex); |
73285082 KC |
2031 | |
2032 | *part = 0; | |
07002e99 | 2033 | return kobj; |
73285082 KC |
2034 | } |
2035 | ||
770fe30a KS |
2036 | static long loop_control_ioctl(struct file *file, unsigned int cmd, |
2037 | unsigned long parm) | |
2038 | { | |
2039 | struct loop_device *lo; | |
0a42e99b | 2040 | int ret; |
770fe30a | 2041 | |
0a42e99b JK |
2042 | ret = mutex_lock_killable(&loop_ctl_mutex); |
2043 | if (ret) | |
2044 | return ret; | |
2045 | ||
2046 | ret = -ENOSYS; | |
770fe30a KS |
2047 | switch (cmd) { |
2048 | case LOOP_CTL_ADD: | |
2049 | ret = loop_lookup(&lo, parm); | |
2050 | if (ret >= 0) { | |
2051 | ret = -EEXIST; | |
2052 | break; | |
2053 | } | |
2054 | ret = loop_add(&lo, parm); | |
2055 | break; | |
2056 | case LOOP_CTL_REMOVE: | |
2057 | ret = loop_lookup(&lo, parm); | |
2058 | if (ret < 0) | |
2059 | break; | |
770fe30a KS |
2060 | if (lo->lo_state != Lo_unbound) { |
2061 | ret = -EBUSY; | |
310ca162 | 2062 | mutex_unlock(&loop_ctl_mutex); |
770fe30a KS |
2063 | break; |
2064 | } | |
f8933667 | 2065 | if (atomic_read(&lo->lo_refcnt) > 0) { |
770fe30a | 2066 | ret = -EBUSY; |
310ca162 | 2067 | mutex_unlock(&loop_ctl_mutex); |
770fe30a KS |
2068 | break; |
2069 | } | |
2070 | lo->lo_disk->private_data = NULL; | |
770fe30a KS |
2071 | idr_remove(&loop_index_idr, lo->lo_number); |
2072 | loop_remove(lo); | |
2073 | break; | |
2074 | case LOOP_CTL_GET_FREE: | |
2075 | ret = loop_lookup(&lo, -1); | |
2076 | if (ret >= 0) | |
2077 | break; | |
2078 | ret = loop_add(&lo, -1); | |
2079 | } | |
0a42e99b | 2080 | mutex_unlock(&loop_ctl_mutex); |
770fe30a KS |
2081 | |
2082 | return ret; | |
2083 | } | |
2084 | ||
2085 | static const struct file_operations loop_ctl_fops = { | |
2086 | .open = nonseekable_open, | |
2087 | .unlocked_ioctl = loop_control_ioctl, | |
2088 | .compat_ioctl = loop_control_ioctl, | |
2089 | .owner = THIS_MODULE, | |
2090 | .llseek = noop_llseek, | |
2091 | }; | |
2092 | ||
2093 | static struct miscdevice loop_misc = { | |
2094 | .minor = LOOP_CTRL_MINOR, | |
2095 | .name = "loop-control", | |
2096 | .fops = &loop_ctl_fops, | |
2097 | }; | |
2098 | ||
2099 | MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); | |
2100 | MODULE_ALIAS("devname:loop-control"); | |
2101 | ||
73285082 KC |
2102 | static int __init loop_init(void) |
2103 | { | |
a47653fc KC |
2104 | int i, nr; |
2105 | unsigned long range; | |
34dd82af | 2106 | struct loop_device *lo; |
770fe30a | 2107 | int err; |
a47653fc | 2108 | |
476a4813 | 2109 | part_shift = 0; |
ac04fee0 | 2110 | if (max_part > 0) { |
476a4813 LV |
2111 | part_shift = fls(max_part); |
2112 | ||
ac04fee0 NK |
2113 | /* |
2114 | * Adjust max_part according to part_shift as it is exported | |
2115 | * to user space so that user can decide correct minor number | |
2116 | * if [s]he want to create more devices. | |
2117 | * | |
2118 | * Note that -1 is required because partition 0 is reserved | |
2119 | * for the whole disk. | |
2120 | */ | |
2121 | max_part = (1UL << part_shift) - 1; | |
2122 | } | |
2123 | ||
b1a66504 GC |
2124 | if ((1UL << part_shift) > DISK_MAX_PARTS) { |
2125 | err = -EINVAL; | |
a8c1d064 | 2126 | goto err_out; |
b1a66504 | 2127 | } |
78f4bb36 | 2128 | |
b1a66504 GC |
2129 | if (max_loop > 1UL << (MINORBITS - part_shift)) { |
2130 | err = -EINVAL; | |
a8c1d064 | 2131 | goto err_out; |
b1a66504 | 2132 | } |
1da177e4 | 2133 | |
d134b00b KS |
2134 | /* |
2135 | * If max_loop is specified, create that many devices upfront. | |
2136 | * This also becomes a hard limit. If max_loop is not specified, | |
2137 | * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module | |
2138 | * init time. Loop devices can be requested on-demand with the | |
2139 | * /dev/loop-control interface, or be instantiated by accessing | |
2140 | * a 'dead' device node. | |
2141 | */ | |
73285082 | 2142 | if (max_loop) { |
a47653fc | 2143 | nr = max_loop; |
a1c15c59 | 2144 | range = max_loop << part_shift; |
a47653fc | 2145 | } else { |
d134b00b | 2146 | nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; |
a1c15c59 | 2147 | range = 1UL << MINORBITS; |
a47653fc KC |
2148 | } |
2149 | ||
a8c1d064 AV |
2150 | err = misc_register(&loop_misc); |
2151 | if (err < 0) | |
2152 | goto err_out; | |
2153 | ||
2154 | ||
b1a66504 GC |
2155 | if (register_blkdev(LOOP_MAJOR, "loop")) { |
2156 | err = -EIO; | |
2157 | goto misc_out; | |
2158 | } | |
1da177e4 | 2159 | |
a47653fc KC |
2160 | blk_register_region(MKDEV(LOOP_MAJOR, 0), range, |
2161 | THIS_MODULE, loop_probe, NULL, NULL); | |
2162 | ||
d134b00b | 2163 | /* pre-create number of devices given by config or max_loop */ |
0a42e99b | 2164 | mutex_lock(&loop_ctl_mutex); |
34dd82af KS |
2165 | for (i = 0; i < nr; i++) |
2166 | loop_add(&lo, i); | |
0a42e99b | 2167 | mutex_unlock(&loop_ctl_mutex); |
34dd82af | 2168 | |
73285082 | 2169 | printk(KERN_INFO "loop: module loaded\n"); |
1da177e4 | 2170 | return 0; |
b1a66504 GC |
2171 | |
2172 | misc_out: | |
2173 | misc_deregister(&loop_misc); | |
a8c1d064 | 2174 | err_out: |
b1a66504 | 2175 | return err; |
34dd82af | 2176 | } |
a47653fc | 2177 | |
34dd82af KS |
2178 | static int loop_exit_cb(int id, void *ptr, void *data) |
2179 | { | |
2180 | struct loop_device *lo = ptr; | |
a47653fc | 2181 | |
34dd82af KS |
2182 | loop_remove(lo); |
2183 | return 0; | |
1da177e4 LT |
2184 | } |
2185 | ||
73285082 | 2186 | static void __exit loop_exit(void) |
1da177e4 | 2187 | { |
a47653fc | 2188 | unsigned long range; |
1da177e4 | 2189 | |
a1c15c59 | 2190 | range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; |
a47653fc | 2191 | |
34dd82af | 2192 | idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); |
34dd82af | 2193 | idr_destroy(&loop_index_idr); |
73285082 | 2194 | |
a47653fc | 2195 | blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); |
00d59405 | 2196 | unregister_blkdev(LOOP_MAJOR, "loop"); |
770fe30a KS |
2197 | |
2198 | misc_deregister(&loop_misc); | |
1da177e4 LT |
2199 | } |
2200 | ||
2201 | module_init(loop_init); | |
2202 | module_exit(loop_exit); | |
2203 | ||
2204 | #ifndef MODULE | |
2205 | static int __init max_loop_setup(char *str) | |
2206 | { | |
2207 | max_loop = simple_strtol(str, NULL, 0); | |
2208 | return 1; | |
2209 | } | |
2210 | ||
2211 | __setup("max_loop=", max_loop_setup); | |
2212 | #endif |