2 * Network block device - make block devices work over TCP
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
7 * Copyright 1997 Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
9 * (part of code stolen from loop.c)
11 * 97-3-25 compiled 0-th version, not yet tested it
12 * (it did not work, BTW) (later that day) HEY! it works!
13 * (bit later) hmm, not that much... 2:00am next day:
14 * yes, it works, but it gives something like 50kB/sec
15 * 97-4-01 complete rewrite to make it possible for many requests at
16 * once to be processed
17 * 97-4-11 Making protocol independent of endianity etc.
18 * 97-9-13 Cosmetic changes
19 * 98-5-13 Attempt to make 64-bit-clean on 64-bit machines
20 * 99-1-11 Attempt to make 64-bit-clean on 32-bit machines <ankry@mif.pg.gda.pl>
22 * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
23 * why not: would need verify_area and friends, would share yet another
24 * structure with userland
28 #include <linux/major.h>
30 #include <linux/module.h>
32 #include <linux/sched.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/file.h>
37 #include <linux/ioctl.h>
39 #include <asm/segment.h>
40 #include <asm/uaccess.h>
41 #include <asm/types.h>
43 #define MAJOR_NR NBD_MAJOR
44 #include <linux/nbd.h>
46 #define LO_MAGIC 0x68797548
48 static int nbd_blksizes
[MAX_NBD
];
49 static int nbd_blksize_bits
[MAX_NBD
];
50 static int nbd_sizes
[MAX_NBD
];
51 static u64 nbd_bytesizes
[MAX_NBD
];
53 static struct nbd_device nbd_dev
[MAX_NBD
];
56 /* #define DEBUG( s ) printk( s )
60 static int requests_in
;
61 static int requests_out
;
64 static int nbd_open(struct inode
*inode
, struct file
*file
)
67 struct nbd_device
*nbdev
;
71 dev
= MINOR(inode
->i_rdev
);
75 nbdev
= &nbd_dev
[dev
];
76 nbd_dev
[dev
].refcnt
++;
77 if (!(nbdev
->flags
& NBD_INITIALISED
)) {
78 init_MUTEX(&nbdev
->queue_lock
);
79 nbdev
->flags
|= NBD_INITIALISED
;
86 * Send or receive packet.
88 static int nbd_xmit(int send
, struct socket
*sock
, char *buf
, int size
)
100 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
101 oldset
= current
->blocked
;
102 sigfillset(¤t
->blocked
);
103 recalc_sigpending(current
);
104 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
114 msg
.msg_control
= NULL
;
115 msg
.msg_controllen
= 0;
120 result
= sock_sendmsg(sock
, &msg
, size
);
122 result
= sock_recvmsg(sock
, &msg
, size
, 0);
126 printk(KERN_ERR
"NBD: %s - sock=%ld at buf=%ld, size=%d returned %d.\n",
127 send
? "send" : "receive", (long) sock
, (long) buf
, size
, result
);
135 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
136 current
->blocked
= oldset
;
137 recalc_sigpending(current
);
138 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
144 #define FAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); goto error_out; }
146 void nbd_send_req(struct socket
*sock
, struct request
*req
)
149 struct nbd_request request
;
151 DEBUG("NBD: sending control, ");
152 request
.magic
= htonl(NBD_REQUEST_MAGIC
);
153 request
.type
= htonl(req
->cmd
);
154 request
.from
= cpu_to_be64( (u64
) req
->sector
<< 9);
155 request
.len
= htonl(req
->current_nr_sectors
<< 9);
156 memcpy(request
.handle
, &req
, sizeof(req
));
158 result
= nbd_xmit(1, sock
, (char *) &request
, sizeof(request
));
160 FAIL("Sendmsg failed for control.");
162 if (req
->cmd
== WRITE
) {
164 result
= nbd_xmit(1, sock
, req
->buffer
, req
->current_nr_sectors
<< 9);
166 FAIL("Send data failed.");
174 #define HARDFAIL( s ) { printk( KERN_ERR "NBD: " s "(result %d)\n", result ); lo->harderror = result; return NULL; }
175 struct request
*nbd_read_stat(struct nbd_device
*lo
)
176 /* NULL returned = something went wrong, inform userspace */
179 struct nbd_reply reply
;
180 struct request
*xreq
, *req
;
182 DEBUG("reading control, ");
184 result
= nbd_xmit(0, lo
->sock
, (char *) &reply
, sizeof(reply
));
187 HARDFAIL("Recv control failed.");
188 memcpy(&xreq
, reply
.handle
, sizeof(xreq
));
191 FAIL("Unexpected handle received.\n");
194 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
)
195 HARDFAIL("Not enough magic.");
196 if (ntohl(reply
.error
))
197 FAIL("Other side returned error.");
198 if (req
->cmd
== READ
) {
200 result
= nbd_xmit(0, lo
->sock
, req
->buffer
, req
->current_nr_sectors
<< 9);
202 HARDFAIL("Recv data failed.");
207 /* Can we get here? Yes, if other side returns error */
213 void nbd_do_it(struct nbd_device
*lo
)
218 req
= nbd_read_stat(lo
);
221 down (&lo
->queue_lock
);
223 if (req
!= lo
->tail
) {
224 printk(KERN_ALERT
"NBD: I have problem...\n");
226 if (lo
!= &nbd_dev
[MINOR(req
->rq_dev
)]) {
227 printk(KERN_ALERT
"NBD: request corrupted!\n");
230 if (lo
->magic
!= LO_MAGIC
) {
231 printk(KERN_ALERT
"NBD: nbd_dev[] corrupted: Not enough magic\n");
232 up (&lo
->queue_lock
);
236 nbd_end_request(req
);
237 if (lo
->tail
== lo
->head
) {
240 printk(KERN_ERR
"NBD: I did not expect this\n");
244 lo
->tail
= lo
->tail
->next
;
246 up (&lo
->queue_lock
);
250 void nbd_clear_que(struct nbd_device
*lo
)
259 if (lo
!= &nbd_dev
[MINOR(req
->rq_dev
)]) {
260 printk(KERN_ALERT
"NBD: request corrupted when clearing!\n");
263 if (lo
->magic
!= LO_MAGIC
) {
264 printk(KERN_ERR
"NBD: nbd_dev[] corrupted: Not enough magic when clearing!\n");
269 nbd_end_request(req
);
270 if (lo
->tail
== lo
->head
) {
273 printk(KERN_ERR
"NBD: I did not assume this\n");
277 lo
->tail
= lo
->tail
->next
;
282 * We always wait for result of write, for now. It would be nice to make it optional
284 * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
285 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
289 #define FAIL( s ) { printk( KERN_ERR "NBD, minor %d: " s "\n", dev ); goto error_out; }
291 static void do_nbd_request(void)
295 struct nbd_device
*lo
;
299 dev
= MINOR(req
->rq_dev
);
302 FAIL("Minor too big."); /* Probably can not happen */
306 FAIL("Request when not-ready.");
307 if ((req
->cmd
== WRITE
) && (lo
->flags
& NBD_READ_ONLY
))
308 FAIL("Write on read-only");
310 if (lo
->magic
!= LO_MAGIC
)
311 FAIL("nbd[] is not magical!");
315 CURRENT
= CURRENT
->next
;
318 spin_unlock_irq(&io_request_lock
);
319 down (&lo
->queue_lock
);
320 if (lo
->head
== NULL
) {
324 lo
->head
->next
= req
;
328 nbd_send_req(lo
->sock
, req
); /* Why does this block? */
329 up (&lo
->queue_lock
);
330 spin_lock_irq(&io_request_lock
);
335 nbd_end_request(req
);
336 CURRENT
= CURRENT
->next
;
341 static int nbd_ioctl(struct inode
*inode
, struct file
*file
,
342 unsigned int cmd
, unsigned long arg
)
344 struct nbd_device
*lo
;
345 int dev
, error
, temp
;
347 /* Anyone capable of this syscall can do *real bad* things */
349 if (!capable(CAP_SYS_ADMIN
))
353 dev
= MINOR(inode
->i_rdev
);
361 if (lo
->head
|| lo
->tail
) {
362 printk(KERN_ERR
"nbd: Some requests are in progress -> can not turn off.\n");
378 inode
= file
->f_dentry
->d_inode
;
379 /* N.B. Should verify that it's a socket */
381 lo
->sock
= &inode
->u
.socket_i
;
385 case NBD_SET_BLKSIZE
:
386 if ((arg
& (arg
-1)) || (arg
< 512) || (arg
> PAGE_SIZE
))
388 nbd_blksizes
[dev
] = arg
;
390 nbd_blksize_bits
[dev
] = 9;
392 nbd_blksize_bits
[dev
]++;
395 nbd_sizes
[dev
] = nbd_bytesizes
[dev
] >> nbd_blksize_bits
[dev
];
396 nbd_bytesizes
[dev
] = nbd_sizes
[dev
] << nbd_blksize_bits
[dev
];
399 nbd_sizes
[dev
] = arg
>> nbd_blksize_bits
[dev
];
400 nbd_bytesizes
[dev
] = nbd_sizes
[dev
] << nbd_blksize_bits
[dev
];
402 case NBD_SET_SIZE_BLOCKS
:
403 nbd_sizes
[dev
] = arg
;
404 nbd_bytesizes
[dev
] = ((u64
) arg
) << nbd_blksize_bits
[dev
];
410 return lo
->harderror
;
415 case NBD_PRINT_DEBUG
:
416 printk(KERN_INFO
"NBD device %d: head = %lx, tail = %lx. Global: in %d, out %d\n",
417 dev
, (long) lo
->head
, (long) lo
->tail
, requests_in
, requests_out
);
421 return put_user(nbd_bytesizes
[dev
] >> 9, (long *) arg
);
426 static int nbd_release(struct inode
*inode
, struct file
*file
)
428 struct nbd_device
*lo
;
433 dev
= MINOR(inode
->i_rdev
);
436 fsync_dev(inode
->i_rdev
);
437 invalidate_buffers(inode
->i_rdev
);
440 printk(KERN_ALERT
"nbd_release: refcount(%d) <= 0\n", lo
->refcnt
);
442 /* N.B. Doesn't lo->file need an fput?? */
447 static struct file_operations nbd_fops
=
449 NULL
, /* lseek - default */
450 block_read
, /* read - general block-dev read */
451 block_write
, /* write - general block-dev write */
452 NULL
, /* readdir - bad */
454 nbd_ioctl
, /* ioctl */
458 nbd_release
/* release */
462 * And here should be modules and kernel interface
463 * (Just smiley confuses emacs :-)
467 #define nbd_init init_module
474 if (sizeof(struct nbd_request
) != 28) {
475 printk(KERN_CRIT
"Sizeof nbd_request needs to be 28 in order to work!\n" );
479 if (register_blkdev(MAJOR_NR
, "nbd", &nbd_fops
)) {
480 printk("Unable to get major number %d for NBD\n",
485 printk("nbd: registered device at major %d\n", MAJOR_NR
);
487 blksize_size
[MAJOR_NR
] = nbd_blksizes
;
488 blk_size
[MAJOR_NR
] = nbd_sizes
;
489 blk_dev
[MAJOR_NR
].request_fn
= do_nbd_request
;
490 for (i
= 0; i
< MAX_NBD
; i
++) {
491 nbd_dev
[i
].refcnt
= 0;
492 nbd_dev
[i
].file
= NULL
;
493 nbd_dev
[i
].magic
= LO_MAGIC
;
494 nbd_dev
[i
].flags
= 0;
495 nbd_blksizes
[i
] = 1024;
496 nbd_blksize_bits
[i
] = 10;
497 nbd_bytesizes
[i
] = 0x7ffffc00; /* 2GB */
498 nbd_sizes
[i
] = nbd_bytesizes
[i
] >> nbd_blksize_bits
[i
];
504 void cleanup_module(void)
506 if (unregister_blkdev(MAJOR_NR
, "nbd") != 0)
507 printk("nbd: cleanup_module failed\n");
509 printk("nbd: module cleaned up.\n");