UPSTREAM: loop: add ioctl for changing logical block size
authorOmar Sandoval <osandov@fb.com>
Thu, 24 Aug 2017 07:03:43 +0000 (00:03 -0700)
committerMichael Benedict <michaelbt@live.com>
Sat, 5 Oct 2019 02:24:46 +0000 (12:24 +1000)
This is a different approach from the first attempt in f2c6df7dbf9a
("loop: support 4k physical blocksize"). Rather than extending
LOOP_{GET,SET}_STATUS, add a separate ioctl just for setting the block
size.

Bug: 117823094
Change-Id: I8e69b8839d7fee3be564cbfce1797ce108e1aa1e
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Hannes Reinecke <hare@suse.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
(cherry picked from commit 89e4fdecb51cf5535867026274bc97de9480ade5)
Signed-off-by: Martijn Coenen <maco@android.com>
drivers/block/loop.c
include/uapi/linux/loop.h

index d02afeb11197d4e9dfe1c1a4f2e6384eae20319c..81cdad14df7920c258062a444bd6f594a9d4f195 100644 (file)
@@ -1061,6 +1061,7 @@ static int loop_clr_fd(struct loop_device *lo)
        memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
        memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
        memset(lo->lo_file_name, 0, LO_NAME_SIZE);
+       blk_queue_logical_block_size(lo->lo_queue, 512);
        if (bdev) {
                bdput(bdev);
                invalidate_bdev(bdev);
@@ -1342,6 +1343,24 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
        return error;
 }
 
+static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
+{
+       if (lo->lo_state != Lo_bound)
+               return -ENXIO;
+
+       if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg))
+               return -EINVAL;
+
+       blk_mq_freeze_queue(lo->lo_queue);
+
+       blk_queue_logical_block_size(lo->lo_queue, arg);
+       loop_update_dio(lo);
+
+       blk_mq_unfreeze_queue(lo->lo_queue);
+
+       return 0;
+}
+
 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
        unsigned int cmd, unsigned long arg)
 {
@@ -1390,6 +1409,11 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
                if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
                        err = loop_set_dio(lo, arg);
                break;
+       case LOOP_SET_BLOCK_SIZE:
+               err = -EPERM;
+               if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
+                       err = loop_set_block_size(lo, arg);
+               break;
        default:
                err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
        }
index c8125ec1f4f2270a5a01a358832425cd57edb11a..23158dbe242483c7d4c35570794dadc2c332bcc0 100644 (file)
@@ -88,6 +88,7 @@ struct loop_info64 {
 #define LOOP_CHANGE_FD         0x4C06
 #define LOOP_SET_CAPACITY      0x4C07
 #define LOOP_SET_DIRECT_IO     0x4C08
+#define LOOP_SET_BLOCK_SIZE    0x4C09
 
 /* /dev/loop-control interface */
 #define LOOP_CTL_ADD           0x4C80