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