Add a IOCTL to negotiate features between the server and the kernel
module, with mediation from the userspace client.  Features are stored
in the pre-existing (but so far never written to) flags field; bits for
the flags field are adjusted to actually match the network protocol.

Also, zero out the flags when NBD_DO_IT completes.

Cc: Paul Clements <[email protected]>
Cc: <[email protected]>
Cc: Alex Bligh <[email protected]>
Reviewed-by: Paul Clements <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
---
        This is my version of the NBD_SET_FLAGS patch.

 drivers/block/nbd.c |   11 ++++++++++-
 include/linux/nbd.h |   29 +++++++++++++++--------------
 2 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 0eeed20..616c982 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -89,6 +89,8 @@ static const char *ioctl_cmd_to_ascii(int cmd)
        case NBD_PRINT_DEBUG: return "print-debug";
        case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
        case NBD_DISCONNECT: return "disconnect";
+       case NBD_SET_TIMEOUT: return "set-timeout";
+       case NBD_SET_FLAGS: return "set-flags";
        case BLKROSET: return "set-read-only";
        case BLKFLSBUF: return "flush-buffer-cache";
        }
@@ -459,7 +461,7 @@ static void nbd_handle_req(struct nbd_device *lo, struct 
request *req)
        nbd_cmd(req) = NBD_CMD_READ;
        if (rq_data_dir(req) == WRITE) {
                nbd_cmd(req) = NBD_CMD_WRITE;
-               if (lo->flags & NBD_READ_ONLY) {
+               if (lo->flags & NBD_FLAG_READ_ONLY) {
                        printk(KERN_ERR "%s: Write on read-only\n",
                                        lo->disk->disk_name);
                        goto error_out;
@@ -642,6 +644,12 @@ static int __nbd_ioctl(struct block_device *bdev, struct 
nbd_device *lo,
                lo->xmit_timeout = arg * HZ;
                return 0;
 
+       case NBD_SET_FLAGS:
+               if (((u64) arg) & (-1ULL << 32))
+                       return -EINVAL;
+               lo->flags = arg;
+               return 0;
+
        case NBD_SET_SIZE_BLOCKS:
                lo->bytesize = ((u64) arg) * lo->blksize;
                bdev->bd_inode->i_size = lo->bytesize;
@@ -680,6 +688,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct 
nbd_device *lo,
                printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
                if (file)
                        fput(file);
+               lo->flags = 0;
                lo->bytesize = 0;
                bdev->bd_inode->i_size = 0;
                set_capacity(lo->disk, 0);
diff --git a/include/linux/nbd.h b/include/linux/nbd.h
index d146ca1..3e5a05e 100644
--- a/include/linux/nbd.h
+++ b/include/linux/nbd.h
@@ -17,16 +17,17 @@
 
 #include <linux/types.h>
 
-#define NBD_SET_SOCK   _IO( 0xab, 0 )
-#define NBD_SET_BLKSIZE        _IO( 0xab, 1 )
-#define NBD_SET_SIZE   _IO( 0xab, 2 )
-#define NBD_DO_IT      _IO( 0xab, 3 )
-#define NBD_CLEAR_SOCK _IO( 0xab, 4 )
-#define NBD_CLEAR_QUE  _IO( 0xab, 5 )
-#define NBD_PRINT_DEBUG        _IO( 0xab, 6 )
-#define NBD_SET_SIZE_BLOCKS    _IO( 0xab, 7 )
-#define NBD_DISCONNECT  _IO( 0xab, 8 )
-#define NBD_SET_TIMEOUT _IO( 0xab, 9 )
+#define NBD_SET_SOCK           _IO(0xab, 0)
+#define NBD_SET_BLKSIZE                _IO(0xab, 1)
+#define NBD_SET_SIZE           _IO(0xab, 2)
+#define NBD_DO_IT              _IO(0xab, 3)
+#define NBD_CLEAR_SOCK         _IO(0xab, 4)
+#define NBD_CLEAR_QUE          _IO(0xab, 5)
+#define NBD_PRINT_DEBUG                _IO(0xab, 6)
+#define NBD_SET_SIZE_BLOCKS    _IO(0xab, 7)
+#define NBD_DISCONNECT         _IO(0xab, 8)
+#define NBD_SET_TIMEOUT                _IO(0xab, 9)
+#define NBD_SET_FLAGS          _IO(0xab, 10)
 
 enum {
        NBD_CMD_READ = 0,
@@ -34,6 +35,10 @@ enum {
        NBD_CMD_DISC = 2
 };
 
+/* values for flags field */
+#define NBD_FLAG_HAS_FLAGS     (1 << 0)        /* Flags are there */
+#define NBD_FLAG_READ_ONLY     (1 << 1)        /* Device is read only */
+
 #define nbd_cmd(req) ((req)->cmd[0])
 
 /* userspace doesn't need the nbd_device structure */
@@ -42,10 +47,6 @@ enum {
 #include <linux/wait.h>
 #include <linux/mutex.h>
 
-/* values for flags field */
-#define NBD_READ_ONLY 0x0001
-#define NBD_WRITE_NOCHK 0x0002
-
 struct request;
 
 struct nbd_device {
-- 
1.7.6



------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the 
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Nbd-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nbd-general

Reply via email to