loop.c (b74b953b998bcc2db91b694446f3a2619ec32de6) | loop.c (51a0bb0c2e614659132fce272c8a1815a12c5c2a) |
---|---|
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 * --- 53 unchanged lines hidden (view full) --- 62#include <linux/blkpg.h> 63#include <linux/init.h> 64#include <linux/swap.h> 65#include <linux/slab.h> 66#include <linux/loop.h> 67#include <linux/compat.h> 68#include <linux/suspend.h> 69#include <linux/freezer.h> | 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 * --- 53 unchanged lines hidden (view full) --- 62#include <linux/blkpg.h> 63#include <linux/init.h> 64#include <linux/swap.h> 65#include <linux/slab.h> 66#include <linux/loop.h> 67#include <linux/compat.h> 68#include <linux/suspend.h> 69#include <linux/freezer.h> |
70#include <linux/smp_lock.h> | 70#include <linux/mutex.h> |
71#include <linux/writeback.h> 72#include <linux/buffer_head.h> /* for invalidate_bdev() */ 73#include <linux/completion.h> 74#include <linux/highmem.h> 75#include <linux/kthread.h> 76#include <linux/splice.h> | 71#include <linux/writeback.h> 72#include <linux/buffer_head.h> /* for invalidate_bdev() */ 73#include <linux/completion.h> 74#include <linux/highmem.h> 75#include <linux/kthread.h> 76#include <linux/splice.h> |
77#include <linux/sysfs.h> |
|
77 78#include <asm/uaccess.h> 79 | 78 79#include <asm/uaccess.h> 80 |
81static DEFINE_MUTEX(loop_mutex); |
|
80static LIST_HEAD(loop_devices); 81static DEFINE_MUTEX(loop_devices_mutex); 82 83static int max_part; 84static int part_shift; 85 86/* 87 * Transfer functions --- 6 unchanged lines hidden (view full) --- 94 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 95 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 96 97 if (cmd == READ) 98 memcpy(loop_buf, raw_buf, size); 99 else 100 memcpy(raw_buf, loop_buf, size); 101 | 82static LIST_HEAD(loop_devices); 83static DEFINE_MUTEX(loop_devices_mutex); 84 85static int max_part; 86static int part_shift; 87 88/* 89 * Transfer functions --- 6 unchanged lines hidden (view full) --- 96 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 97 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 98 99 if (cmd == READ) 100 memcpy(loop_buf, raw_buf, size); 101 else 102 memcpy(raw_buf, loop_buf, size); 103 |
102 kunmap_atomic(raw_buf, KM_USER0); | |
103 kunmap_atomic(loop_buf, KM_USER1); | 104 kunmap_atomic(loop_buf, KM_USER1); |
105 kunmap_atomic(raw_buf, KM_USER0); |
|
104 cond_resched(); 105 return 0; 106} 107 108static int transfer_xor(struct loop_device *lo, int cmd, 109 struct page *raw_page, unsigned raw_off, 110 struct page *loop_page, unsigned loop_off, 111 int size, sector_t real_block) --- 11 unchanged lines hidden (view full) --- 123 out = raw_buf; 124 } 125 126 key = lo->lo_encrypt_key; 127 keysize = lo->lo_encrypt_key_size; 128 for (i = 0; i < size; i++) 129 *out++ = *in++ ^ key[(i & 511) % keysize]; 130 | 106 cond_resched(); 107 return 0; 108} 109 110static 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, 113 int size, sector_t real_block) --- 11 unchanged lines hidden (view full) --- 125 out = raw_buf; 126 } 127 128 key = lo->lo_encrypt_key; 129 keysize = lo->lo_encrypt_key_size; 130 for (i = 0; i < size; i++) 131 *out++ = *in++ ^ key[(i & 511) % keysize]; 132 |
131 kunmap_atomic(raw_buf, KM_USER0); | |
132 kunmap_atomic(loop_buf, KM_USER1); | 133 kunmap_atomic(loop_buf, KM_USER1); |
134 kunmap_atomic(raw_buf, KM_USER0); |
|
133 cond_resched(); 134 return 0; 135} 136 137static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 138{ 139 if (unlikely(info->lo_encrypt_key_size <= 0)) 140 return -EINVAL; --- 331 unchanged lines hidden (view full) --- 472static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 473{ 474 loff_t pos; 475 int ret; 476 477 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 478 479 if (bio_rw(bio) == WRITE) { | 135 cond_resched(); 136 return 0; 137} 138 139static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 140{ 141 if (unlikely(info->lo_encrypt_key_size <= 0)) 142 return -EINVAL; --- 331 unchanged lines hidden (view full) --- 474static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 475{ 476 loff_t pos; 477 int ret; 478 479 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 480 481 if (bio_rw(bio) == WRITE) { |
480 bool barrier = !!(bio->bi_rw & REQ_HARDBARRIER); | |
481 struct file *file = lo->lo_backing_file; 482 | 482 struct file *file = lo->lo_backing_file; 483 |
483 if (barrier) { 484 if (unlikely(!file->f_op->fsync)) { 485 ret = -EOPNOTSUPP; 486 goto out; 487 } | 484 /* REQ_HARDBARRIER is deprecated */ 485 if (bio->bi_rw & REQ_HARDBARRIER) { 486 ret = -EOPNOTSUPP; 487 goto out; 488 } |
488 | 489 |
490 if (bio->bi_rw & REQ_FLUSH) { |
|
489 ret = vfs_fsync(file, 0); | 491 ret = vfs_fsync(file, 0); |
490 if (unlikely(ret)) { | 492 if (unlikely(ret && ret != -EINVAL)) { |
491 ret = -EIO; 492 goto out; 493 } 494 } 495 496 ret = lo_send(lo, bio, pos); 497 | 493 ret = -EIO; 494 goto out; 495 } 496 } 497 498 ret = lo_send(lo, bio, pos); 499 |
498 if (barrier && !ret) { | 500 if ((bio->bi_rw & REQ_FUA) && !ret) { |
499 ret = vfs_fsync(file, 0); | 501 ret = vfs_fsync(file, 0); |
500 if (unlikely(ret)) | 502 if (unlikely(ret && ret != -EINVAL)) |
501 ret = -EIO; 502 } 503 } else 504 ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 505 506out: 507 return ret; 508} --- 223 unchanged lines hidden (view full) --- 732 733static inline int is_loop_device(struct file *file) 734{ 735 struct inode *i = file->f_mapping->host; 736 737 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 738} 739 | 503 ret = -EIO; 504 } 505 } else 506 ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 507 508out: 509 return ret; 510} --- 223 unchanged lines hidden (view full) --- 734 735static inline int is_loop_device(struct file *file) 736{ 737 struct inode *i = file->f_mapping->host; 738 739 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 740} 741 |
742/* loop sysfs attributes */ 743 744static ssize_t loop_attr_show(struct device *dev, char *page, 745 ssize_t (*callback)(struct loop_device *, char *)) 746{ 747 struct loop_device *l, *lo = NULL; 748 749 mutex_lock(&loop_devices_mutex); 750 list_for_each_entry(l, &loop_devices, lo_list) 751 if (disk_to_dev(l->lo_disk) == dev) { 752 lo = l; 753 break; 754 } 755 mutex_unlock(&loop_devices_mutex); 756 757 return lo ? callback(lo, page) : -EIO; 758} 759 760#define LOOP_ATTR_RO(_name) \ 761static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 762static ssize_t loop_attr_do_show_##_name(struct device *d, \ 763 struct device_attribute *attr, char *b) \ 764{ \ 765 return loop_attr_show(d, b, loop_attr_##_name##_show); \ 766} \ 767static struct device_attribute loop_attr_##_name = \ 768 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL); 769 770static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 771{ 772 ssize_t ret; 773 char *p = NULL; 774 775 mutex_lock(&lo->lo_ctl_mutex); 776 if (lo->lo_backing_file) 777 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1); 778 mutex_unlock(&lo->lo_ctl_mutex); 779 780 if (IS_ERR_OR_NULL(p)) 781 ret = PTR_ERR(p); 782 else { 783 ret = strlen(p); 784 memmove(buf, p, ret); 785 buf[ret++] = '\n'; 786 buf[ret] = 0; 787 } 788 789 return ret; 790} 791 792static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 793{ 794 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 795} 796 797static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 798{ 799 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 800} 801 802static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 803{ 804 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 805 806 return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 807} 808 809LOOP_ATTR_RO(backing_file); 810LOOP_ATTR_RO(offset); 811LOOP_ATTR_RO(sizelimit); 812LOOP_ATTR_RO(autoclear); 813 814static struct attribute *loop_attrs[] = { 815 &loop_attr_backing_file.attr, 816 &loop_attr_offset.attr, 817 &loop_attr_sizelimit.attr, 818 &loop_attr_autoclear.attr, 819 NULL, 820}; 821 822static struct attribute_group loop_attribute_group = { 823 .name = "loop", 824 .attrs= loop_attrs, 825}; 826 827static int loop_sysfs_init(struct loop_device *lo) 828{ 829 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 830 &loop_attribute_group); 831} 832 833static void loop_sysfs_exit(struct loop_device *lo) 834{ 835 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 836 &loop_attribute_group); 837} 838 |
|
740static int loop_set_fd(struct loop_device *lo, fmode_t mode, 741 struct block_device *bdev, unsigned int arg) 742{ 743 struct file *file, *f; 744 struct inode *inode; 745 struct address_space *mapping; 746 unsigned lo_blocksize; 747 int lo_flags = 0; --- 79 unchanged lines hidden (view full) --- 827 * set queue make_request_fn, and add limits based on lower level 828 * device 829 */ 830 blk_queue_make_request(lo->lo_queue, loop_make_request); 831 lo->lo_queue->queuedata = lo; 832 lo->lo_queue->unplug_fn = loop_unplug; 833 834 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) | 839static int loop_set_fd(struct loop_device *lo, fmode_t mode, 840 struct block_device *bdev, unsigned int arg) 841{ 842 struct file *file, *f; 843 struct inode *inode; 844 struct address_space *mapping; 845 unsigned lo_blocksize; 846 int lo_flags = 0; --- 79 unchanged lines hidden (view full) --- 926 * set queue make_request_fn, and add limits based on lower level 927 * device 928 */ 929 blk_queue_make_request(lo->lo_queue, loop_make_request); 930 lo->lo_queue->queuedata = lo; 931 lo->lo_queue->unplug_fn = loop_unplug; 932 933 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) |
835 blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN); | 934 blk_queue_flush(lo->lo_queue, REQ_FLUSH); |
836 837 set_capacity(lo->lo_disk, size); 838 bd_set_size(bdev, size << 9); | 935 936 set_capacity(lo->lo_disk, size); 937 bd_set_size(bdev, size << 9); |
938 loop_sysfs_init(lo); |
|
839 /* let user-space know about the new size */ 840 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 841 842 set_blocksize(bdev, lo_blocksize); 843 844 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 845 lo->lo_number); 846 if (IS_ERR(lo->lo_thread)) { 847 error = PTR_ERR(lo->lo_thread); 848 goto out_clr; 849 } 850 lo->lo_state = Lo_bound; 851 wake_up_process(lo->lo_thread); 852 if (max_part > 0) 853 ioctl_by_bdev(bdev, BLKRRPART, 0); 854 return 0; 855 856out_clr: | 939 /* let user-space know about the new size */ 940 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 941 942 set_blocksize(bdev, lo_blocksize); 943 944 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 945 lo->lo_number); 946 if (IS_ERR(lo->lo_thread)) { 947 error = PTR_ERR(lo->lo_thread); 948 goto out_clr; 949 } 950 lo->lo_state = Lo_bound; 951 wake_up_process(lo->lo_thread); 952 if (max_part > 0) 953 ioctl_by_bdev(bdev, BLKRRPART, 0); 954 return 0; 955 956out_clr: |
957 loop_sysfs_exit(lo); |
|
857 lo->lo_thread = NULL; 858 lo->lo_device = NULL; 859 lo->lo_backing_file = NULL; 860 lo->lo_flags = 0; 861 set_capacity(lo->lo_disk, 0); 862 invalidate_bdev(bdev); 863 bd_set_size(bdev, 0); 864 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); --- 78 unchanged lines hidden (view full) --- 943 lo->lo_flags = 0; 944 lo->lo_thread = NULL; 945 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 946 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 947 memset(lo->lo_file_name, 0, LO_NAME_SIZE); 948 if (bdev) 949 invalidate_bdev(bdev); 950 set_capacity(lo->lo_disk, 0); | 958 lo->lo_thread = NULL; 959 lo->lo_device = NULL; 960 lo->lo_backing_file = NULL; 961 lo->lo_flags = 0; 962 set_capacity(lo->lo_disk, 0); 963 invalidate_bdev(bdev); 964 bd_set_size(bdev, 0); 965 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); --- 78 unchanged lines hidden (view full) --- 1044 lo->lo_flags = 0; 1045 lo->lo_thread = NULL; 1046 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 1047 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 1048 memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1049 if (bdev) 1050 invalidate_bdev(bdev); 1051 set_capacity(lo->lo_disk, 0); |
1052 loop_sysfs_exit(lo); |
|
951 if (bdev) { 952 bd_set_size(bdev, 0); 953 /* let user-space know about this change */ 954 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 955 } 956 mapping_set_gfp_mask(filp->f_mapping, gfp); 957 lo->lo_state = Lo_unbound; 958 /* This is safe: open() is still holding a reference. */ --- 445 unchanged lines hidden (view full) --- 1404 return err; 1405} 1406#endif 1407 1408static int lo_open(struct block_device *bdev, fmode_t mode) 1409{ 1410 struct loop_device *lo = bdev->bd_disk->private_data; 1411 | 1053 if (bdev) { 1054 bd_set_size(bdev, 0); 1055 /* let user-space know about this change */ 1056 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1057 } 1058 mapping_set_gfp_mask(filp->f_mapping, gfp); 1059 lo->lo_state = Lo_unbound; 1060 /* This is safe: open() is still holding a reference. */ --- 445 unchanged lines hidden (view full) --- 1506 return err; 1507} 1508#endif 1509 1510static int lo_open(struct block_device *bdev, fmode_t mode) 1511{ 1512 struct loop_device *lo = bdev->bd_disk->private_data; 1513 |
1412 lock_kernel(); | 1514 mutex_lock(&loop_mutex); |
1413 mutex_lock(&lo->lo_ctl_mutex); 1414 lo->lo_refcnt++; 1415 mutex_unlock(&lo->lo_ctl_mutex); | 1515 mutex_lock(&lo->lo_ctl_mutex); 1516 lo->lo_refcnt++; 1517 mutex_unlock(&lo->lo_ctl_mutex); |
1416 unlock_kernel(); | 1518 mutex_unlock(&loop_mutex); |
1417 1418 return 0; 1419} 1420 1421static int lo_release(struct gendisk *disk, fmode_t mode) 1422{ 1423 struct loop_device *lo = disk->private_data; 1424 int err; 1425 | 1519 1520 return 0; 1521} 1522 1523static int lo_release(struct gendisk *disk, fmode_t mode) 1524{ 1525 struct loop_device *lo = disk->private_data; 1526 int err; 1527 |
1426 lock_kernel(); | 1528 mutex_lock(&loop_mutex); |
1427 mutex_lock(&lo->lo_ctl_mutex); 1428 1429 if (--lo->lo_refcnt) 1430 goto out; 1431 1432 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1433 /* 1434 * In autoclear mode, stop the loop thread --- 8 unchanged lines hidden (view full) --- 1443 * but flush possible ongoing bios in thread. 1444 */ 1445 loop_flush(lo); 1446 } 1447 1448out: 1449 mutex_unlock(&lo->lo_ctl_mutex); 1450out_unlocked: | 1529 mutex_lock(&lo->lo_ctl_mutex); 1530 1531 if (--lo->lo_refcnt) 1532 goto out; 1533 1534 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1535 /* 1536 * In autoclear mode, stop the loop thread --- 8 unchanged lines hidden (view full) --- 1545 * but flush possible ongoing bios in thread. 1546 */ 1547 loop_flush(lo); 1548 } 1549 1550out: 1551 mutex_unlock(&lo->lo_ctl_mutex); 1552out_unlocked: |
1451 lock_kernel(); | 1553 mutex_unlock(&loop_mutex); |
1452 return 0; 1453} 1454 1455static const struct block_device_operations lo_fops = { 1456 .owner = THIS_MODULE, 1457 .open = lo_open, 1458 .release = lo_release, 1459 .ioctl = lo_ioctl, --- 228 unchanged lines hidden --- | 1554 return 0; 1555} 1556 1557static const struct block_device_operations lo_fops = { 1558 .owner = THIS_MODULE, 1559 .open = lo_open, 1560 .release = lo_release, 1561 .ioctl = lo_ioctl, --- 228 unchanged lines hidden --- |