52 #include <linux/module.h>
54 #include <linux/sched.h>
57 #include <linux/stat.h>
58 #include <linux/errno.h>
60 #include <linux/wait.h>
65 #include <linux/slab.h>
66 #include <linux/loop.h>
78 #include <linux/falloc.h>
80 #include <asm/uaccess.h>
86 static int part_shift;
92 struct page *raw_page,
unsigned raw_off,
93 struct page *loop_page,
unsigned loop_off,
100 memcpy(loop_buf, raw_buf, size);
102 memcpy(raw_buf, loop_buf, size);
110 static int transfer_xor(
struct loop_device *lo,
int cmd,
111 struct page *raw_page,
unsigned raw_off,
112 struct page *loop_page,
unsigned loop_off,
116 char *loop_buf =
kmap_atomic(loop_page) + loop_off;
130 for (i = 0; i <
size; i++)
131 *out++ = *in++ ^ key[(i & 511) %
keysize];
148 .transfer = transfer_none,
153 .transfer = transfer_xor,
163 static loff_t get_size(loff_t
offset, loff_t sizelimit,
struct file *
file)
165 loff_t
size, loopsize;
168 size = i_size_read(file->
f_mapping->host);
174 if (sizelimit > 0 && sizelimit < loopsize)
175 loopsize = sizelimit;
180 return loopsize >> 9;
183 static loff_t get_loop_size(
struct loop_device *lo,
struct file *file)
189 figure_loop_size(
struct loop_device *lo, loff_t offset, loff_t sizelimit)
206 struct page *rpage,
unsigned roffs,
207 struct page *lpage,
unsigned loffs,
213 return lo->
transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
222 static int __do_lo_send_write(
struct file *file,
229 bw = file->
f_op->write(file, buf, len, &pos);
233 printk(
KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
234 (
unsigned long long)pos, len);
246 static int do_lo_send_direct_write(
struct loop_device *lo,
247 struct bio_vec *bvec, loff_t pos,
struct page *
page)
250 kmap(bvec->bv_page) + bvec->bv_offset,
264 static int do_lo_send_write(
struct loop_device *lo,
struct bio_vec *bvec,
265 loff_t pos,
struct page *page)
267 int ret = lo_do_transfer(lo,
WRITE, page, 0, bvec->bv_page,
268 bvec->bv_offset, bvec->bv_len, pos >> 9);
274 "length %i.\n", (
unsigned long long)pos, bvec->bv_len);
280 static int lo_send(
struct loop_device *lo,
struct bio *bio, loff_t pos)
284 struct bio_vec *bvec;
285 struct page *page =
NULL;
288 if (lo->
transfer != transfer_none) {
293 do_lo_send = do_lo_send_write;
295 do_lo_send = do_lo_send_direct_write;
298 bio_for_each_segment(bvec, bio, i) {
299 ret = do_lo_send(lo, bvec, pos, page);
311 printk(
KERN_ERR "loop: Failed to allocate temporary page for write.\n");
329 struct page *page = buf->
page;
361 struct bio_vec *bvec,
int bsize, loff_t pos)
369 cookie.page = bvec->bv_page;
370 cookie.offset = bvec->bv_offset;
386 lo_receive(
struct loop_device *lo,
struct bio *bio,
int bsize, loff_t pos)
388 struct bio_vec *bvec;
392 bio_for_each_segment(bvec, bio, i) {
393 s = do_lo_receive(lo, bvec, bsize, pos);
397 if (s != bvec->bv_len) {
406 static int do_bio_filebacked(
struct loop_device *lo,
struct bio *bio)
411 pos = ((loff_t) bio->bi_sector << 9) + lo->
lo_offset;
413 if (bio_rw(bio) ==
WRITE) {
434 if ((!file->
f_op->fallocate) ||
439 ret = file->
f_op->fallocate(file, mode, pos,
447 ret = lo_send(lo, bio, pos);
449 if ((bio->bi_rw &
REQ_FUA) && !ret) {
464 static void loop_add_bio(
struct loop_device *lo,
struct bio *bio)
472 static struct bio *loop_get_bio(
struct loop_device *lo)
477 static void loop_make_request(
struct request_queue *
q,
struct bio *old_bio)
480 int rw = bio_rw(old_bio);
492 loop_add_bio(lo, old_bio);
499 bio_io_error(old_bio);
509 static inline void loop_handle_bio(
struct loop_device *lo,
struct bio *bio)
512 do_loop_switch(lo, bio->bi_private);
515 int ret = do_bio_filebacked(lo, bio);
532 static int loop_thread(
void *
data)
548 bio = loop_get_bio(lo);
552 loop_handle_bio(lo, bio);
563 static int loop_switch(
struct loop_device *lo,
struct file *file)
569 init_completion(&
w.wait);
571 bio->bi_private = &
w;
573 loop_make_request(lo->
lo_queue, bio);
587 return loop_switch(lo,
NULL);
595 struct file *file = p->
file;
626 struct file *
file, *old_file;
653 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
657 error = loop_switch(lo, file);
672 static inline int is_loop_device(
struct file *file)
684 struct gendisk *disk = dev_to_disk(dev);
690 #define LOOP_ATTR_RO(_name) \
691 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
692 static ssize_t loop_attr_do_show_##_name(struct device *d, \
693 struct device_attribute *attr, char *b) \
695 return loop_attr_show(d, b, loop_attr_##_name##_show); \
697 static struct device_attribute loop_attr_##_name = \
698 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
710 if (IS_ERR_OR_NULL(p))
736 return sprintf(buf,
"%s\n", autoclear ?
"1" :
"0");
743 return sprintf(buf,
"%s\n", partscan ?
"1" :
"0");
752 static struct attribute *loop_attrs[] = {
753 &loop_attr_backing_file.attr,
754 &loop_attr_offset.attr,
755 &loop_attr_sizelimit.attr,
756 &loop_attr_autoclear.attr,
757 &loop_attr_partscan.attr,
769 &loop_attribute_group);
772 static void loop_sysfs_exit(
struct loop_device *lo)
775 &loop_attribute_group);
778 static void loop_config_discard(
struct loop_device *lo)
781 struct inode *inode = file->
f_mapping->host;
790 if ((!file->
f_op->fallocate) ||
792 q->limits.discard_granularity = 0;
793 q->limits.discard_alignment = 0;
794 q->limits.max_discard_sectors = 0;
795 q->limits.discard_zeroes_data = 0;
796 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
800 q->limits.discard_granularity = inode->
i_sb->s_blocksize;
801 q->limits.discard_alignment = 0;
802 q->limits.max_discard_sectors =
UINT_MAX >> 9;
803 q->limits.discard_zeroes_data = 1;
804 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
810 struct file *
file, *
f;
813 unsigned lo_blocksize;
832 while (is_loop_device(f)) {
838 l = f->
f_mapping->host->i_bdev->bd_disk->private_data;
847 inode = mapping->
host;
861 size = get_loop_size(lo, file);
891 set_capacity(lo->
lo_disk, size);
944 module_put(xfer->
owner);
958 if (!try_module_get(owner))
961 err = xfer->
init(lo, i);
1000 spin_unlock_irq(&lo->
lo_lock);
1006 spin_unlock_irq(&lo->
lo_lock);
1008 loop_release_xfer(lo);
1023 loop_sysfs_exit(lo);
1029 mapping_set_gfp_mask(filp->
f_mapping, gfp);
1037 lo->
lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1065 err = loop_release_xfer(lo);
1074 xfer = xfer_funcs[
type];
1080 err = loop_init_xfer(lo, xfer, info);
1089 loop_config_discard(lo);
1108 lo->
lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1136 memset(info, 0,
sizeof(*info));
1159 memset(info64, 0,
sizeof(*info64));
1181 memset(info, 0,
sizeof(*info));
1216 loop_info64_from_old(&info, &info64);
1217 return loop_set_status(lo, &info64);
1227 return loop_set_status(lo, &info64);
1239 err = loop_get_status(lo, &info64);
1241 err = loop_info64_to_old(&info64, &info);
1256 err = loop_get_status(lo, &info64);
1257 if (!err &&
copy_to_user(arg, &info64,
sizeof(info64)))
1275 sec = get_capacity(lo->
lo_disk);
1290 unsigned int cmd,
unsigned long arg)
1298 err = loop_set_fd(lo, mode, bdev, arg);
1301 err = loop_change_fd(lo, bdev, arg);
1305 err = loop_clr_fd(lo);
1312 err = loop_set_status_old(lo,
1316 err = loop_get_status_old(lo, (
struct loop_info __user *) arg);
1321 err = loop_set_status64(lo,
1325 err = loop_get_status64(lo, (
struct loop_info64 __user *) arg);
1330 err = loop_set_capacity(lo, bdev);
1341 #ifdef CONFIG_COMPAT
1342 struct compat_loop_info {
1362 loop_info64_from_compat(
const struct compat_loop_info __user *arg,
1365 struct compat_loop_info info;
1370 memset(info64, 0,
sizeof(*info64));
1395 loop_info64_to_compat(
const struct loop_info64 *info64,
1396 struct compat_loop_info __user *arg)
1398 struct compat_loop_info info;
1400 memset(&info, 0,
sizeof(info));
1433 const struct compat_loop_info __user *arg)
1438 ret = loop_info64_from_compat(arg, &info64);
1441 return loop_set_status(lo, &info64);
1446 struct compat_loop_info __user *arg)
1454 err = loop_get_status(lo, &info64);
1456 err = loop_info64_to_compat(&info64, arg);
1461 unsigned int cmd,
unsigned long arg)
1469 err = loop_set_status_compat(
1470 lo, (
const struct compat_loop_info __user *) arg);
1475 err = loop_get_status_compat(
1476 lo, (
struct compat_loop_info __user *) arg);
1483 arg = (
unsigned long) compat_ptr(arg);
1486 err = lo_ioctl(bdev, mode, cmd, arg);
1502 lo = bdev->
bd_disk->private_data;
1516 static int lo_release(
struct gendisk *disk,
fmode_t mode)
1531 err = loop_clr_fd(lo);
1548 static const struct block_device_operations lo_fops = {
1551 .release = lo_release,
1553 #ifdef CONFIG_COMPAT
1554 .compat_ioctl = lo_compat_ioctl,
1561 static int max_loop;
1565 MODULE_PARM_DESC(max_part,
"Maximum number of partitions per loop device");
1571 unsigned int n = funcs->
number;
1575 xfer_funcs[
n] = funcs;
1579 static int unregister_transfer_cb(
int id,
void *
ptr,
void *data)
1586 loop_release_xfer(lo);
1599 xfer_funcs[
n] =
NULL;
1600 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
1607 static int loop_add(
struct loop_device **l,
int i)
1610 struct gendisk *disk;
1626 if (err >= 0 && i != m) {
1630 }
else if (i == -1) {
1649 goto out_free_queue;
1670 disk->flags |= GENHD_FL_NO_PART_SCAN;
1671 disk->flags |= GENHD_FL_EXT_DEVT;
1678 disk->first_minor = i << part_shift;
1679 disk->fops = &lo_fops;
1680 disk->private_data = lo;
1682 sprintf(disk->disk_name,
"loop%d", i);
1703 static int find_free_cb(
int id,
void *ptr,
void *data)
1715 static int loop_lookup(
struct loop_device **l,
int i)
1723 err =
idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1748 err = loop_lookup(&lo,
MINOR(dev) >> part_shift);
1750 err = loop_add(&lo,
MINOR(dev) >> part_shift);
1752 kobj = ERR_PTR(err);
1761 static long loop_control_ioctl(
struct file *file,
unsigned int cmd,
1770 ret = loop_lookup(&lo, parm);
1775 ret = loop_add(&lo, parm);
1778 ret = loop_lookup(&lo, parm);
1798 ret = loop_lookup(&lo, -1);
1801 ret = loop_add(&lo, -1);
1810 .unlocked_ioctl = loop_control_ioctl,
1811 .compat_ioctl = loop_control_ioctl,
1818 .name =
"loop-control",
1819 .fops = &loop_ctl_fops,
1825 static int __init loop_init(
void)
1828 unsigned long range;
1838 part_shift = fls(max_part);
1848 max_part = (1
UL << part_shift) - 1;
1851 if ((1
UL << part_shift) > DISK_MAX_PARTS)
1867 range = max_loop << part_shift;
1869 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
1881 for (i = 0; i <
nr; i++)
1889 static int loop_exit_cb(
int id,
void *ptr,
void *data)
1897 static void __exit loop_exit(
void)
1899 unsigned long range;
1901 range = max_loop ? max_loop << part_shift : 1
UL <<
MINORBITS;
1917 static int __init max_loop_setup(
char *
str)
1923 __setup(
"max_loop=", max_loop_setup);