loop.c (51001b7da364a24ed2464f3c22179efdc6b3a960) loop.c (f2c6df7dbf9a60e1cd9941f9fb376d4d9ad1e8dd)
1/*
2 * linux/drivers/block/loop.c
3 *
4 * Written by Theodore Ts'o, 3/29/93
5 *
6 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
7 * permitted under the GNU General Public License.
8 *

--- 207 unchanged lines hidden (view full) ---

216 if (use_dio)
217 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
218 else
219 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
220 blk_mq_unfreeze_queue(lo->lo_queue);
221}
222
223static int
1/*
2 * linux/drivers/block/loop.c
3 *
4 * Written by Theodore Ts'o, 3/29/93
5 *
6 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
7 * permitted under the GNU General Public License.
8 *

--- 207 unchanged lines hidden (view full) ---

216 if (use_dio)
217 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
218 else
219 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
220 blk_mq_unfreeze_queue(lo->lo_queue);
221}
222
223static int
224figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
224figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit,
225 loff_t logical_blocksize)
225{
226 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
227 sector_t x = (sector_t)size;
228 struct block_device *bdev = lo->lo_device;
229
230 if (unlikely((loff_t)x != size))
231 return -EFBIG;
232 if (lo->lo_offset != offset)
233 lo->lo_offset = offset;
234 if (lo->lo_sizelimit != sizelimit)
235 lo->lo_sizelimit = sizelimit;
226{
227 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
228 sector_t x = (sector_t)size;
229 struct block_device *bdev = lo->lo_device;
230
231 if (unlikely((loff_t)x != size))
232 return -EFBIG;
233 if (lo->lo_offset != offset)
234 lo->lo_offset = offset;
235 if (lo->lo_sizelimit != sizelimit)
236 lo->lo_sizelimit = sizelimit;
237 if (lo->lo_flags & LO_FLAGS_BLOCKSIZE) {
238 lo->lo_logical_blocksize = logical_blocksize;
239 blk_queue_physical_block_size(lo->lo_queue, lo->lo_blocksize);
240 blk_queue_logical_block_size(lo->lo_queue,
241 lo->lo_logical_blocksize);
242 }
236 set_capacity(lo->lo_disk, x);
237 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
238 /* let user-space know about the new size */
239 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
240 return 0;
241}
242
243static inline int

--- 561 unchanged lines hidden (view full) ---

805 &loop_attribute_group);
806}
807
808static void loop_config_discard(struct loop_device *lo)
809{
810 struct file *file = lo->lo_backing_file;
811 struct inode *inode = file->f_mapping->host;
812 struct request_queue *q = lo->lo_queue;
243 set_capacity(lo->lo_disk, x);
244 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
245 /* let user-space know about the new size */
246 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
247 return 0;
248}
249
250static inline int

--- 561 unchanged lines hidden (view full) ---

812 &loop_attribute_group);
813}
814
815static void loop_config_discard(struct loop_device *lo)
816{
817 struct file *file = lo->lo_backing_file;
818 struct inode *inode = file->f_mapping->host;
819 struct request_queue *q = lo->lo_queue;
820 int lo_bits = 9;
813
814 /*
815 * We use punch hole to reclaim the free space used by the
816 * image a.k.a. discard. However we do not support discard if
817 * encryption is enabled, because it may give an attacker
818 * useful information.
819 */
820 if ((!file->f_op->fallocate) ||
821 lo->lo_encrypt_key_size) {
822 q->limits.discard_granularity = 0;
823 q->limits.discard_alignment = 0;
824 blk_queue_max_discard_sectors(q, 0);
825 blk_queue_max_write_zeroes_sectors(q, 0);
826 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
827 return;
828 }
829
830 q->limits.discard_granularity = inode->i_sb->s_blocksize;
831 q->limits.discard_alignment = 0;
821
822 /*
823 * We use punch hole to reclaim the free space used by the
824 * image a.k.a. discard. However we do not support discard if
825 * encryption is enabled, because it may give an attacker
826 * useful information.
827 */
828 if ((!file->f_op->fallocate) ||
829 lo->lo_encrypt_key_size) {
830 q->limits.discard_granularity = 0;
831 q->limits.discard_alignment = 0;
832 blk_queue_max_discard_sectors(q, 0);
833 blk_queue_max_write_zeroes_sectors(q, 0);
834 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
835 return;
836 }
837
838 q->limits.discard_granularity = inode->i_sb->s_blocksize;
839 q->limits.discard_alignment = 0;
832 blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
833 blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
840 if (lo->lo_flags & LO_FLAGS_BLOCKSIZE)
841 lo_bits = blksize_bits(lo->lo_logical_blocksize);
842
843 blk_queue_max_discard_sectors(q, UINT_MAX >> lo_bits);
844 blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> lo_bits);
834 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
835}
836
837static void loop_unprepare_queue(struct loop_device *lo)
838{
839 kthread_flush_worker(&lo->worker);
840 kthread_stop(lo->worker_task);
841}

--- 71 unchanged lines hidden (view full) ---

913 goto out_putf;
914
915 error = 0;
916
917 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
918
919 lo->use_dio = false;
920 lo->lo_blocksize = lo_blocksize;
845 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
846}
847
848static void loop_unprepare_queue(struct loop_device *lo)
849{
850 kthread_flush_worker(&lo->worker);
851 kthread_stop(lo->worker_task);
852}

--- 71 unchanged lines hidden (view full) ---

924 goto out_putf;
925
926 error = 0;
927
928 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
929
930 lo->use_dio = false;
931 lo->lo_blocksize = lo_blocksize;
932 lo->lo_logical_blocksize = 512;
921 lo->lo_device = bdev;
922 lo->lo_flags = lo_flags;
923 lo->lo_backing_file = file;
924 lo->transfer = NULL;
925 lo->ioctl = NULL;
926 lo->lo_sizelimit = 0;
927 lo->old_gfp_mask = mapping_gfp_mask(mapping);
928 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));

--- 149 unchanged lines hidden (view full) ---

1078}
1079
1080static int
1081loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1082{
1083 int err;
1084 struct loop_func_table *xfer;
1085 kuid_t uid = current_uid();
933 lo->lo_device = bdev;
934 lo->lo_flags = lo_flags;
935 lo->lo_backing_file = file;
936 lo->transfer = NULL;
937 lo->ioctl = NULL;
938 lo->lo_sizelimit = 0;
939 lo->old_gfp_mask = mapping_gfp_mask(mapping);
940 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));

--- 149 unchanged lines hidden (view full) ---

1090}
1091
1092static int
1093loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1094{
1095 int err;
1096 struct loop_func_table *xfer;
1097 kuid_t uid = current_uid();
1098 int lo_flags = lo->lo_flags;
1086
1087 if (lo->lo_encrypt_key_size &&
1088 !uid_eq(lo->lo_key_owner, uid) &&
1089 !capable(CAP_SYS_ADMIN))
1090 return -EPERM;
1091 if (lo->lo_state != Lo_bound)
1092 return -ENXIO;
1093 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)

--- 16 unchanged lines hidden (view full) ---

1110 return -EINVAL;
1111 } else
1112 xfer = NULL;
1113
1114 err = loop_init_xfer(lo, xfer, info);
1115 if (err)
1116 goto exit;
1117
1099
1100 if (lo->lo_encrypt_key_size &&
1101 !uid_eq(lo->lo_key_owner, uid) &&
1102 !capable(CAP_SYS_ADMIN))
1103 return -EPERM;
1104 if (lo->lo_state != Lo_bound)
1105 return -ENXIO;
1106 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)

--- 16 unchanged lines hidden (view full) ---

1123 return -EINVAL;
1124 } else
1125 xfer = NULL;
1126
1127 err = loop_init_xfer(lo, xfer, info);
1128 if (err)
1129 goto exit;
1130
1131 if (info->lo_flags & LO_FLAGS_BLOCKSIZE) {
1132 if (!(lo->lo_flags & LO_FLAGS_BLOCKSIZE))
1133 lo->lo_logical_blocksize = 512;
1134 lo->lo_flags |= LO_FLAGS_BLOCKSIZE;
1135 if (LO_INFO_BLOCKSIZE(info) != 512 &&
1136 LO_INFO_BLOCKSIZE(info) != 1024 &&
1137 LO_INFO_BLOCKSIZE(info) != 2048 &&
1138 LO_INFO_BLOCKSIZE(info) != 4096)
1139 return -EINVAL;
1140 if (LO_INFO_BLOCKSIZE(info) > lo->lo_blocksize)
1141 return -EINVAL;
1142 }
1143
1118 if (lo->lo_offset != info->lo_offset ||
1144 if (lo->lo_offset != info->lo_offset ||
1119 lo->lo_sizelimit != info->lo_sizelimit)
1120 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) {
1145 lo->lo_sizelimit != info->lo_sizelimit ||
1146 lo->lo_flags != lo_flags ||
1147 ((lo->lo_flags & LO_FLAGS_BLOCKSIZE) &&
1148 lo->lo_logical_blocksize != LO_INFO_BLOCKSIZE(info))) {
1149 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit,
1150 LO_INFO_BLOCKSIZE(info)))
1121 err = -EFBIG;
1122 goto exit;
1123 }
1124
1125 loop_config_discard(lo);
1126
1127 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1128 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);

--- 174 unchanged lines hidden (view full) ---

1303 return err;
1304}
1305
1306static int loop_set_capacity(struct loop_device *lo)
1307{
1308 if (unlikely(lo->lo_state != Lo_bound))
1309 return -ENXIO;
1310
1151 err = -EFBIG;
1152 goto exit;
1153 }
1154
1155 loop_config_discard(lo);
1156
1157 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1158 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);

--- 174 unchanged lines hidden (view full) ---

1333 return err;
1334}
1335
1336static int loop_set_capacity(struct loop_device *lo)
1337{
1338 if (unlikely(lo->lo_state != Lo_bound))
1339 return -ENXIO;
1340
1311 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
1341 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit,
1342 lo->lo_logical_blocksize);
1312}
1313
1314static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1315{
1316 int error = -ENXIO;
1317 if (lo->lo_state != Lo_bound)
1318 goto out;
1319

--- 740 unchanged lines hidden ---
1343}
1344
1345static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1346{
1347 int error = -ENXIO;
1348 if (lo->lo_state != Lo_bound)
1349 goto out;
1350

--- 740 unchanged lines hidden ---