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