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