Lines Matching refs:lo

92 static int transfer_xor(struct loop_device *lo, int cmd,
110 key = lo->lo_encrypt_key;
111 keysize = lo->lo_encrypt_key_size;
121 static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
165 static loff_t get_loop_size(struct loop_device *lo, struct file *file)
167 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
170 static void __loop_update_dio(struct loop_device *lo, bool dio)
172 struct file *file = lo->lo_backing_file;
195 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
196 !(lo->lo_offset & dio_align) &&
198 !lo->transfer)
206 if (lo->use_dio == use_dio)
217 if (lo->lo_state == Lo_bound)
218 blk_mq_freeze_queue(lo->lo_queue);
219 lo->use_dio = use_dio;
221 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
222 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
224 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
225 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
227 if (lo->lo_state == Lo_bound)
228 blk_mq_unfreeze_queue(lo->lo_queue);
233 * @lo: struct loop_device to set the size for
239 static void loop_set_size(struct loop_device *lo, loff_t size)
241 struct block_device *bdev = lo->lo_device;
245 if (!set_capacity_revalidate_and_notify(lo->lo_disk, size, false))
250 lo_do_transfer(struct loop_device *lo, int cmd,
257 ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
289 static int lo_write_simple(struct loop_device *lo, struct request *rq,
297 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
311 static int lo_write_transfer(struct loop_device *lo, struct request *rq,
324 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
332 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
341 static int lo_read_simple(struct loop_device *lo, struct request *rq,
351 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
370 static int lo_read_transfer(struct loop_device *lo, struct request *rq,
392 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
398 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
420 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
429 struct file *file = lo->lo_backing_file;
430 struct request_queue *q = lo->lo_queue;
447 static int lo_req_flush(struct loop_device *lo, struct request *rq)
449 struct file *file = lo->lo_backing_file;
514 static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
522 struct file *file = lo->lo_backing_file;
586 static int do_req_filebacked(struct loop_device *lo, struct request *rq)
589 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
602 return lo_req_flush(lo, rq);
608 return lo_fallocate(lo, rq, pos,
613 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
615 if (lo->transfer)
616 return lo_write_transfer(lo, rq, pos);
618 return lo_rw_aio(lo, cmd, pos, WRITE);
620 return lo_write_simple(lo, rq, pos);
622 if (lo->transfer)
623 return lo_read_transfer(lo, rq, pos);
625 return lo_rw_aio(lo, cmd, pos, READ);
627 return lo_read_simple(lo, rq, pos);
634 static inline void loop_update_dio(struct loop_device *lo)
636 __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
637 lo->use_dio);
640 static void loop_reread_partitions(struct loop_device *lo,
650 __func__, lo->lo_number, lo->lo_file_name, rc);
691 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
702 if (lo->lo_state != Lo_bound)
707 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
719 old_file = lo->lo_backing_file;
724 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
728 blk_mq_freeze_queue(lo->lo_queue);
729 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
730 lo->lo_backing_file = file;
731 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
733 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
734 loop_update_dio(lo);
735 blk_mq_unfreeze_queue(lo->lo_queue);
736 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
745 loop_reread_partitions(lo, bdev);
761 struct loop_device *lo = disk->private_data;
763 return callback(lo, page);
776 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
781 spin_lock_irq(&lo->lo_lock);
782 if (lo->lo_backing_file)
783 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
784 spin_unlock_irq(&lo->lo_lock);
798 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
800 return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset);
803 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
805 return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
808 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
810 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
815 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
817 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
822 static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
824 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
851 static void loop_sysfs_init(struct loop_device *lo)
853 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
857 static void loop_sysfs_exit(struct loop_device *lo)
859 if (lo->sysfs_inited)
860 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
864 static void loop_config_discard(struct loop_device *lo)
866 struct file *file = lo->lo_backing_file;
868 struct request_queue *q = lo->lo_queue;
878 if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) {
893 } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) {
916 static void loop_unprepare_queue(struct loop_device *lo)
918 kthread_flush_worker(&lo->worker);
919 kthread_stop(lo->worker_task);
928 static int loop_prepare_queue(struct loop_device *lo)
930 kthread_init_worker(&lo->worker);
931 lo->worker_task = kthread_run(loop_kthread_worker_fn,
932 &lo->worker, "loop%d", lo->lo_number);
933 if (IS_ERR(lo->worker_task))
935 set_user_nice(lo->worker_task, MIN_NICE);
939 static void loop_update_rotational(struct loop_device *lo)
941 struct file *file = lo->lo_backing_file;
944 struct request_queue *q = lo->lo_queue;
958 loop_release_xfer(struct loop_device *lo)
961 struct loop_func_table *xfer = lo->lo_encryption;
965 err = xfer->release(lo);
966 lo->transfer = NULL;
967 lo->lo_encryption = NULL;
974 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
985 err = xfer->init(lo, i);
989 lo->lo_encryption = xfer;
996 * @lo: struct loop_device to configure
1003 loop_set_status_from_info(struct loop_device *lo,
1013 err = loop_release_xfer(lo);
1028 err = loop_init_xfer(lo, xfer, info);
1036 lo->lo_offset = info->lo_offset;
1037 lo->lo_sizelimit = info->lo_sizelimit;
1039 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1040 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1041 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1042 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1046 lo->transfer = xfer->transfer;
1047 lo->ioctl = xfer->ioctl;
1049 lo->lo_flags = info->lo_flags;
1051 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1052 lo->lo_init[0] = info->lo_init[0];
1053 lo->lo_init[1] = info->lo_init[1];
1055 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1057 lo->lo_key_owner = uid;
1063 static int loop_configure(struct loop_device *lo, fmode_t mode,
1100 if (lo->lo_state != Lo_unbound)
1121 error = loop_set_status_from_info(lo, &config->info);
1127 lo->lo_flags |= LO_FLAGS_READ_ONLY;
1129 error = loop_prepare_queue(lo);
1133 set_device_ro(bdev, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
1135 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
1136 lo->lo_device = bdev;
1137 lo->lo_backing_file = file;
1138 lo->old_gfp_mask = mapping_gfp_mask(mapping);
1139 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1141 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
1142 blk_queue_write_cache(lo->lo_queue, true, false);
1146 else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
1152 blk_queue_logical_block_size(lo->lo_queue, bsize);
1153 blk_queue_physical_block_size(lo->lo_queue, bsize);
1154 blk_queue_io_min(lo->lo_queue, bsize);
1156 loop_config_discard(lo);
1157 loop_update_rotational(lo);
1158 loop_update_dio(lo);
1159 loop_sysfs_init(lo);
1161 size = get_loop_size(lo, file);
1162 loop_set_size(lo, size);
1167 lo->lo_state = Lo_bound;
1169 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1170 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
1172 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1180 loop_reread_partitions(lo, bdev);
1198 static int __loop_clr_fd(struct loop_device *lo, bool release)
1201 gfp_t gfp = lo->old_gfp_mask;
1202 struct block_device *bdev = lo->lo_device;
1208 if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1213 filp = lo->lo_backing_file;
1219 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1220 blk_queue_write_cache(lo->lo_queue, false, false);
1223 blk_mq_freeze_queue(lo->lo_queue);
1225 spin_lock_irq(&lo->lo_lock);
1226 lo->lo_backing_file = NULL;
1227 spin_unlock_irq(&lo->lo_lock);
1229 loop_release_xfer(lo);
1230 lo->transfer = NULL;
1231 lo->ioctl = NULL;
1232 lo->lo_device = NULL;
1233 lo->lo_encryption = NULL;
1234 lo->lo_offset = 0;
1235 lo->lo_sizelimit = 0;
1236 lo->lo_encrypt_key_size = 0;
1237 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1238 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1239 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
1240 blk_queue_logical_block_size(lo->lo_queue, 512);
1241 blk_queue_physical_block_size(lo->lo_queue, 512);
1242 blk_queue_io_min(lo->lo_queue, 512);
1248 set_capacity(lo->lo_disk, 0);
1249 loop_sysfs_exit(lo);
1258 blk_mq_unfreeze_queue(lo->lo_queue);
1260 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1261 lo_number = lo->lo_number;
1262 loop_unprepare_queue(lo);
1287 * lo->lo_state is set to Lo_unbound here after above partscan has
1291 * lo->lo_backing_file is already cleared and Lo_rundown state
1292 * protects us from all the other places trying to change the 'lo'
1296 lo->lo_flags = 0;
1298 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1299 lo->lo_state = Lo_unbound;
1313 static int loop_clr_fd(struct loop_device *lo)
1320 if (lo->lo_state != Lo_bound) {
1334 if (atomic_read(&lo->lo_refcnt) > 1) {
1335 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1339 lo->lo_state = Lo_rundown;
1342 return __loop_clr_fd(lo, false);
1346 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1358 if (lo->lo_encrypt_key_size &&
1359 !uid_eq(lo->lo_key_owner, uid) &&
1364 if (lo->lo_state != Lo_bound) {
1369 if (lo->lo_offset != info->lo_offset ||
1370 lo->lo_sizelimit != info->lo_sizelimit) {
1372 sync_blockdev(lo->lo_device);
1373 invalidate_bdev(lo->lo_device);
1377 blk_mq_freeze_queue(lo->lo_queue);
1379 if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) {
1383 __func__, lo->lo_number, lo->lo_file_name,
1384 lo->lo_device->bd_inode->i_mapping->nrpages);
1388 prev_lo_flags = lo->lo_flags;
1390 err = loop_set_status_from_info(lo, info);
1395 lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
1397 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1399 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
1402 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1403 lo->lo_backing_file);
1404 loop_set_size(lo, new_size);
1407 loop_config_discard(lo);
1410 __loop_update_dio(lo, lo->use_dio);
1413 blk_mq_unfreeze_queue(lo->lo_queue);
1415 if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1417 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1418 bdev = lo->lo_device;
1424 loop_reread_partitions(lo, bdev);
1430 loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1439 if (lo->lo_state != Lo_bound) {
1445 info->lo_number = lo->lo_number;
1446 info->lo_offset = lo->lo_offset;
1447 info->lo_sizelimit = lo->lo_sizelimit;
1448 info->lo_flags = lo->lo_flags;
1449 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1450 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1452 lo->lo_encryption ? lo->lo_encryption->number : 0;
1453 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1454 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1455 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1456 lo->lo_encrypt_key_size);
1460 path = lo->lo_backing_file->f_path;
1526 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1534 return loop_set_status(lo, &info64);
1538 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1544 return loop_set_status(lo, &info64);
1548 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1555 err = loop_get_status(lo, &info64);
1565 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1571 err = loop_get_status(lo, &info64);
1578 static int loop_set_capacity(struct loop_device *lo)
1582 if (unlikely(lo->lo_state != Lo_bound))
1585 size = get_loop_size(lo, lo->lo_backing_file);
1586 loop_set_size(lo, size);
1591 static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1594 if (lo->lo_state != Lo_bound)
1597 __loop_update_dio(lo, !!arg);
1598 if (lo->use_dio == !!arg)
1605 static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1609 if (lo->lo_state != Lo_bound)
1616 if (lo->lo_queue->limits.logical_block_size == arg)
1619 sync_blockdev(lo->lo_device);
1620 invalidate_bdev(lo->lo_device);
1622 blk_mq_freeze_queue(lo->lo_queue);
1625 if (lo->lo_device->bd_inode->i_mapping->nrpages) {
1628 __func__, lo->lo_number, lo->lo_file_name,
1629 lo->lo_device->bd_inode->i_mapping->nrpages);
1633 blk_queue_logical_block_size(lo->lo_queue, arg);
1634 blk_queue_physical_block_size(lo->lo_queue, arg);
1635 blk_queue_io_min(lo->lo_queue, arg);
1636 loop_update_dio(lo);
1638 blk_mq_unfreeze_queue(lo->lo_queue);
1643 static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1653 err = loop_set_capacity(lo);
1656 err = loop_set_dio(lo, arg);
1659 err = loop_set_block_size(lo, arg);
1662 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1671 struct loop_device *lo = bdev->bd_disk->private_data;
1687 return loop_configure(lo, mode, bdev, &config);
1695 return loop_configure(lo, mode, bdev, &config);
1698 return loop_change_fd(lo, bdev, arg);
1700 return loop_clr_fd(lo);
1704 err = loop_set_status_old(lo, argp);
1708 return loop_get_status_old(lo, argp);
1712 err = loop_set_status64(lo, argp);
1716 return loop_get_status64(lo, argp);
1724 err = lo_simple_ioctl(lo, cmd, arg);
1822 loop_set_status_compat(struct loop_device *lo,
1831 return loop_set_status(lo, &info64);
1835 loop_get_status_compat(struct loop_device *lo,
1843 err = loop_get_status(lo, &info64);
1852 struct loop_device *lo = bdev->bd_disk->private_data;
1857 err = loop_set_status_compat(lo,
1861 err = loop_get_status_compat(lo,
1887 struct loop_device *lo;
1893 lo = bdev->bd_disk->private_data;
1894 if (!lo) {
1899 atomic_inc(&lo->lo_refcnt);
1907 struct loop_device *lo;
1910 lo = disk->private_data;
1911 if (atomic_dec_return(&lo->lo_refcnt))
1914 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1915 if (lo->lo_state != Lo_bound)
1917 lo->lo_state = Lo_rundown;
1923 __loop_clr_fd(lo, true);
1925 } else if (lo->lo_state == Lo_bound) {
1930 blk_mq_freeze_queue(lo->lo_queue);
1931 blk_mq_unfreeze_queue(lo->lo_queue);
1971 struct loop_device *lo = ptr;
1975 if (lo->lo_encryption == xfer)
1976 loop_release_xfer(lo);
2002 struct loop_device *lo = rq->q->queuedata;
2006 if (lo->lo_state != Lo_bound)
2016 cmd->use_aio = lo->use_dio;
2028 kthread_queue_work(&lo->worker, &cmd->work);
2037 struct loop_device *lo = rq->q->queuedata;
2040 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
2045 ret = do_req_filebacked(lo, rq);
2083 struct loop_device *lo;
2088 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
2089 if (!lo)
2092 lo->lo_state = Lo_unbound;
2096 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2100 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
2107 lo->tag_set.ops = &loop_mq_ops;
2108 lo->tag_set.nr_hw_queues = 1;
2109 lo->tag_set.queue_depth = 128;
2110 lo->tag_set.numa_node = NUMA_NO_NODE;
2111 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
2112 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
2114 lo->tag_set.driver_data = lo;
2116 err = blk_mq_alloc_tag_set(&lo->tag_set);
2120 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
2121 if (IS_ERR(lo->lo_queue)) {
2122 err = PTR_ERR(lo->lo_queue);
2125 lo->lo_queue->queuedata = lo;
2127 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
2135 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
2138 disk = lo->lo_disk = alloc_disk(1 << part_shift);
2163 atomic_set(&lo->lo_refcnt, 0);
2164 lo->lo_number = i;
2165 spin_lock_init(&lo->lo_lock);
2169 disk->private_data = lo;
2170 disk->queue = lo->lo_queue;
2173 *l = lo;
2174 return lo->lo_number;
2177 blk_cleanup_queue(lo->lo_queue);
2179 blk_mq_free_tag_set(&lo->tag_set);
2183 kfree(lo);
2188 static void loop_remove(struct loop_device *lo)
2190 del_gendisk(lo->lo_disk);
2191 blk_cleanup_queue(lo->lo_queue);
2192 blk_mq_free_tag_set(&lo->tag_set);
2193 put_disk(lo->lo_disk);
2194 kfree(lo);
2199 struct loop_device *lo = ptr;
2202 if (lo->lo_state == Lo_unbound) {
2203 *l = lo;
2211 struct loop_device *lo;
2217 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
2219 *l = lo;
2220 ret = lo->lo_number;
2226 lo = idr_find(&loop_index_idr, i);
2227 if (lo) {
2228 *l = lo;
2229 ret = lo->lo_number;
2237 struct loop_device *lo;
2242 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
2244 err = loop_add(&lo, MINOR(dev) >> part_shift);
2248 kobj = get_disk_and_module(lo->lo_disk);
2258 struct loop_device *lo;
2268 ret = loop_lookup(&lo, parm);
2273 ret = loop_add(&lo, parm);
2276 ret = loop_lookup(&lo, parm);
2279 if (lo->lo_state != Lo_unbound) {
2283 if (atomic_read(&lo->lo_refcnt) > 0) {
2287 lo->lo_disk->private_data = NULL;
2288 idr_remove(&loop_index_idr, lo->lo_number);
2289 loop_remove(lo);
2292 ret = loop_lookup(&lo, -1);
2295 ret = loop_add(&lo, -1);
2323 struct loop_device *lo;
2383 loop_add(&lo, i);
2397 struct loop_device *lo = ptr;
2399 loop_remove(lo);