nbd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Network block device - make block devices work over TCP
  3. *
  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.
  6. *
  7. * Copyright 1997-2000 Pavel Machek <pavel@ucw.cz>
  8. * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
  9. *
  10. * This file is released under GPLv2 or later.
  11. *
  12. * (part of code stolen from loop.c)
  13. */
  14. #include <linux/major.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/bio.h>
  21. #include <linux/stat.h>
  22. #include <linux/errno.h>
  23. #include <linux/file.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/compiler.h>
  26. #include <linux/err.h>
  27. #include <linux/kernel.h>
  28. #include <net/sock.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/system.h>
  31. #include <asm/types.h>
  32. #include <linux/nbd.h>
  33. #define LO_MAGIC 0x68797548
  34. #ifdef NDEBUG
  35. #define dprintk(flags, fmt...)
  36. #else /* NDEBUG */
  37. #define dprintk(flags, fmt...) do { \
  38. if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
  39. } while (0)
  40. #define DBG_IOCTL 0x0004
  41. #define DBG_INIT 0x0010
  42. #define DBG_EXIT 0x0020
  43. #define DBG_BLKDEV 0x0100
  44. #define DBG_RX 0x0200
  45. #define DBG_TX 0x0400
  46. static unsigned int debugflags;
  47. #endif /* NDEBUG */
  48. static unsigned int nbds_max = 16;
  49. static struct nbd_device nbd_dev[MAX_NBD];
  50. /*
  51. * Use just one lock (or at most 1 per NIC). Two arguments for this:
  52. * 1. Each NIC is essentially a synchronization point for all servers
  53. * accessed through that NIC so there's no need to have more locks
  54. * than NICs anyway.
  55. * 2. More locks lead to more "Dirty cache line bouncing" which will slow
  56. * down each lock to the point where they're actually slower than just
  57. * a single lock.
  58. * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
  59. */
  60. static DEFINE_SPINLOCK(nbd_lock);
  61. #ifndef NDEBUG
  62. static const char *ioctl_cmd_to_ascii(int cmd)
  63. {
  64. switch (cmd) {
  65. case NBD_SET_SOCK: return "set-sock";
  66. case NBD_SET_BLKSIZE: return "set-blksize";
  67. case NBD_SET_SIZE: return "set-size";
  68. case NBD_DO_IT: return "do-it";
  69. case NBD_CLEAR_SOCK: return "clear-sock";
  70. case NBD_CLEAR_QUE: return "clear-que";
  71. case NBD_PRINT_DEBUG: return "print-debug";
  72. case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
  73. case NBD_DISCONNECT: return "disconnect";
  74. case BLKROSET: return "set-read-only";
  75. case BLKFLSBUF: return "flush-buffer-cache";
  76. }
  77. return "unknown";
  78. }
  79. static const char *nbdcmd_to_ascii(int cmd)
  80. {
  81. switch (cmd) {
  82. case NBD_CMD_READ: return "read";
  83. case NBD_CMD_WRITE: return "write";
  84. case NBD_CMD_DISC: return "disconnect";
  85. }
  86. return "invalid";
  87. }
  88. #endif /* NDEBUG */
  89. static void nbd_end_request(struct request *req)
  90. {
  91. int uptodate = (req->errors == 0) ? 1 : 0;
  92. struct request_queue *q = req->q;
  93. unsigned long flags;
  94. dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
  95. req, uptodate? "done": "failed");
  96. spin_lock_irqsave(q->queue_lock, flags);
  97. if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
  98. end_that_request_last(req, uptodate);
  99. }
  100. spin_unlock_irqrestore(q->queue_lock, flags);
  101. }
  102. /*
  103. * Send or receive packet.
  104. */
  105. static int sock_xmit(struct socket *sock, int send, void *buf, int size,
  106. int msg_flags)
  107. {
  108. int result;
  109. struct msghdr msg;
  110. struct kvec iov;
  111. sigset_t blocked, oldset;
  112. /* Allow interception of SIGKILL only
  113. * Don't allow other signals to interrupt the transmission */
  114. siginitsetinv(&blocked, sigmask(SIGKILL));
  115. sigprocmask(SIG_SETMASK, &blocked, &oldset);
  116. do {
  117. sock->sk->sk_allocation = GFP_NOIO;
  118. iov.iov_base = buf;
  119. iov.iov_len = size;
  120. msg.msg_name = NULL;
  121. msg.msg_namelen = 0;
  122. msg.msg_control = NULL;
  123. msg.msg_controllen = 0;
  124. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  125. if (send)
  126. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  127. else
  128. result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  129. if (signal_pending(current)) {
  130. siginfo_t info;
  131. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  132. current->pid, current->comm,
  133. dequeue_signal_lock(current, &current->blocked, &info));
  134. result = -EINTR;
  135. break;
  136. }
  137. if (result <= 0) {
  138. if (result == 0)
  139. result = -EPIPE; /* short read */
  140. break;
  141. }
  142. size -= result;
  143. buf += result;
  144. } while (size > 0);
  145. sigprocmask(SIG_SETMASK, &oldset, NULL);
  146. return result;
  147. }
  148. static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  149. int flags)
  150. {
  151. int result;
  152. void *kaddr = kmap(bvec->bv_page);
  153. result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  154. flags);
  155. kunmap(bvec->bv_page);
  156. return result;
  157. }
  158. static int nbd_send_req(struct nbd_device *lo, struct request *req)
  159. {
  160. int result, flags;
  161. struct nbd_request request;
  162. unsigned long size = req->nr_sectors << 9;
  163. struct socket *sock = lo->sock;
  164. request.magic = htonl(NBD_REQUEST_MAGIC);
  165. request.type = htonl(nbd_cmd(req));
  166. request.from = cpu_to_be64((u64) req->sector << 9);
  167. request.len = htonl(size);
  168. memcpy(request.handle, &req, sizeof(req));
  169. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
  170. lo->disk->disk_name, req,
  171. nbdcmd_to_ascii(nbd_cmd(req)),
  172. (unsigned long long)req->sector << 9,
  173. req->nr_sectors << 9);
  174. result = sock_xmit(sock, 1, &request, sizeof(request),
  175. (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
  176. if (result <= 0) {
  177. printk(KERN_ERR "%s: Send control failed (result %d)\n",
  178. lo->disk->disk_name, result);
  179. goto error_out;
  180. }
  181. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  182. struct req_iterator iter;
  183. struct bio_vec *bvec;
  184. /*
  185. * we are really probing at internals to determine
  186. * whether to set MSG_MORE or not...
  187. */
  188. rq_for_each_segment(bvec, req, iter) {
  189. flags = 0;
  190. if (!rq_iter_last(req, iter))
  191. flags = MSG_MORE;
  192. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  193. lo->disk->disk_name, req, bvec->bv_len);
  194. result = sock_send_bvec(sock, bvec, flags);
  195. if (result <= 0) {
  196. printk(KERN_ERR "%s: Send data failed (result %d)\n",
  197. lo->disk->disk_name, result);
  198. goto error_out;
  199. }
  200. }
  201. }
  202. return 0;
  203. error_out:
  204. return 1;
  205. }
  206. static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  207. {
  208. struct request *req;
  209. struct list_head *tmp;
  210. struct request *xreq;
  211. int err;
  212. memcpy(&xreq, handle, sizeof(xreq));
  213. err = wait_event_interruptible(lo->active_wq, lo->active_req != xreq);
  214. if (unlikely(err))
  215. goto out;
  216. spin_lock(&lo->queue_lock);
  217. list_for_each(tmp, &lo->queue_head) {
  218. req = list_entry(tmp, struct request, queuelist);
  219. if (req != xreq)
  220. continue;
  221. list_del_init(&req->queuelist);
  222. spin_unlock(&lo->queue_lock);
  223. return req;
  224. }
  225. spin_unlock(&lo->queue_lock);
  226. err = -ENOENT;
  227. out:
  228. return ERR_PTR(err);
  229. }
  230. static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
  231. {
  232. int result;
  233. void *kaddr = kmap(bvec->bv_page);
  234. result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  235. MSG_WAITALL);
  236. kunmap(bvec->bv_page);
  237. return result;
  238. }
  239. /* NULL returned = something went wrong, inform userspace */
  240. static struct request *nbd_read_stat(struct nbd_device *lo)
  241. {
  242. int result;
  243. struct nbd_reply reply;
  244. struct request *req;
  245. struct socket *sock = lo->sock;
  246. reply.magic = 0;
  247. result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
  248. if (result <= 0) {
  249. printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  250. lo->disk->disk_name, result);
  251. goto harderror;
  252. }
  253. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  254. printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  255. lo->disk->disk_name,
  256. (unsigned long)ntohl(reply.magic));
  257. result = -EPROTO;
  258. goto harderror;
  259. }
  260. req = nbd_find_request(lo, reply.handle);
  261. if (unlikely(IS_ERR(req))) {
  262. result = PTR_ERR(req);
  263. if (result != -ENOENT)
  264. goto harderror;
  265. printk(KERN_ERR "%s: Unexpected reply (%p)\n",
  266. lo->disk->disk_name, reply.handle);
  267. result = -EBADR;
  268. goto harderror;
  269. }
  270. if (ntohl(reply.error)) {
  271. printk(KERN_ERR "%s: Other side returned error (%d)\n",
  272. lo->disk->disk_name, ntohl(reply.error));
  273. req->errors++;
  274. return req;
  275. }
  276. dprintk(DBG_RX, "%s: request %p: got reply\n",
  277. lo->disk->disk_name, req);
  278. if (nbd_cmd(req) == NBD_CMD_READ) {
  279. struct req_iterator iter;
  280. struct bio_vec *bvec;
  281. rq_for_each_segment(bvec, req, iter) {
  282. result = sock_recv_bvec(sock, bvec);
  283. if (result <= 0) {
  284. printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  285. lo->disk->disk_name, result);
  286. req->errors++;
  287. return req;
  288. }
  289. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  290. lo->disk->disk_name, req, bvec->bv_len);
  291. }
  292. }
  293. return req;
  294. harderror:
  295. lo->harderror = result;
  296. return NULL;
  297. }
  298. static ssize_t pid_show(struct gendisk *disk, char *page)
  299. {
  300. return sprintf(page, "%ld\n",
  301. (long) ((struct nbd_device *)disk->private_data)->pid);
  302. }
  303. static struct disk_attribute pid_attr = {
  304. .attr = { .name = "pid", .mode = S_IRUGO },
  305. .show = pid_show,
  306. };
  307. static int nbd_do_it(struct nbd_device *lo)
  308. {
  309. struct request *req;
  310. int ret;
  311. BUG_ON(lo->magic != LO_MAGIC);
  312. lo->pid = current->pid;
  313. ret = sysfs_create_file(&lo->disk->kobj, &pid_attr.attr);
  314. if (ret) {
  315. printk(KERN_ERR "nbd: sysfs_create_file failed!");
  316. return ret;
  317. }
  318. while ((req = nbd_read_stat(lo)) != NULL)
  319. nbd_end_request(req);
  320. sysfs_remove_file(&lo->disk->kobj, &pid_attr.attr);
  321. return 0;
  322. }
  323. static void nbd_clear_que(struct nbd_device *lo)
  324. {
  325. struct request *req;
  326. BUG_ON(lo->magic != LO_MAGIC);
  327. /*
  328. * Because we have set lo->sock to NULL under the tx_lock, all
  329. * modifications to the list must have completed by now. For
  330. * the same reason, the active_req must be NULL.
  331. *
  332. * As a consequence, we don't need to take the spin lock while
  333. * purging the list here.
  334. */
  335. BUG_ON(lo->sock);
  336. BUG_ON(lo->active_req);
  337. while (!list_empty(&lo->queue_head)) {
  338. req = list_entry(lo->queue_head.next, struct request,
  339. queuelist);
  340. list_del_init(&req->queuelist);
  341. req->errors++;
  342. nbd_end_request(req);
  343. }
  344. }
  345. /*
  346. * We always wait for result of write, for now. It would be nice to make it optional
  347. * in future
  348. * if ((rq_data_dir(req) == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
  349. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  350. */
  351. static void do_nbd_request(struct request_queue * q)
  352. {
  353. struct request *req;
  354. while ((req = elv_next_request(q)) != NULL) {
  355. struct nbd_device *lo;
  356. blkdev_dequeue_request(req);
  357. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n",
  358. req->rq_disk->disk_name, req, req->cmd_type);
  359. if (!blk_fs_request(req))
  360. goto error_out;
  361. lo = req->rq_disk->private_data;
  362. BUG_ON(lo->magic != LO_MAGIC);
  363. nbd_cmd(req) = NBD_CMD_READ;
  364. if (rq_data_dir(req) == WRITE) {
  365. nbd_cmd(req) = NBD_CMD_WRITE;
  366. if (lo->flags & NBD_READ_ONLY) {
  367. printk(KERN_ERR "%s: Write on read-only\n",
  368. lo->disk->disk_name);
  369. goto error_out;
  370. }
  371. }
  372. req->errors = 0;
  373. spin_unlock_irq(q->queue_lock);
  374. mutex_lock(&lo->tx_lock);
  375. if (unlikely(!lo->sock)) {
  376. mutex_unlock(&lo->tx_lock);
  377. printk(KERN_ERR "%s: Attempted send on closed socket\n",
  378. lo->disk->disk_name);
  379. req->errors++;
  380. nbd_end_request(req);
  381. spin_lock_irq(q->queue_lock);
  382. continue;
  383. }
  384. lo->active_req = req;
  385. if (nbd_send_req(lo, req) != 0) {
  386. printk(KERN_ERR "%s: Request send failed\n",
  387. lo->disk->disk_name);
  388. req->errors++;
  389. nbd_end_request(req);
  390. } else {
  391. spin_lock(&lo->queue_lock);
  392. list_add(&req->queuelist, &lo->queue_head);
  393. spin_unlock(&lo->queue_lock);
  394. }
  395. lo->active_req = NULL;
  396. mutex_unlock(&lo->tx_lock);
  397. wake_up_all(&lo->active_wq);
  398. spin_lock_irq(q->queue_lock);
  399. continue;
  400. error_out:
  401. req->errors++;
  402. spin_unlock(q->queue_lock);
  403. nbd_end_request(req);
  404. spin_lock(q->queue_lock);
  405. }
  406. return;
  407. }
  408. static int nbd_ioctl(struct inode *inode, struct file *file,
  409. unsigned int cmd, unsigned long arg)
  410. {
  411. struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
  412. int error;
  413. struct request sreq ;
  414. if (!capable(CAP_SYS_ADMIN))
  415. return -EPERM;
  416. BUG_ON(lo->magic != LO_MAGIC);
  417. /* Anyone capable of this syscall can do *real bad* things */
  418. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  419. lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  420. switch (cmd) {
  421. case NBD_DISCONNECT:
  422. printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
  423. sreq.cmd_type = REQ_TYPE_SPECIAL;
  424. nbd_cmd(&sreq) = NBD_CMD_DISC;
  425. /*
  426. * Set these to sane values in case server implementation
  427. * fails to check the request type first and also to keep
  428. * debugging output cleaner.
  429. */
  430. sreq.sector = 0;
  431. sreq.nr_sectors = 0;
  432. if (!lo->sock)
  433. return -EINVAL;
  434. nbd_send_req(lo, &sreq);
  435. return 0;
  436. case NBD_CLEAR_SOCK:
  437. error = 0;
  438. mutex_lock(&lo->tx_lock);
  439. lo->sock = NULL;
  440. mutex_unlock(&lo->tx_lock);
  441. file = lo->file;
  442. lo->file = NULL;
  443. nbd_clear_que(lo);
  444. BUG_ON(!list_empty(&lo->queue_head));
  445. if (file)
  446. fput(file);
  447. return error;
  448. case NBD_SET_SOCK:
  449. if (lo->file)
  450. return -EBUSY;
  451. error = -EINVAL;
  452. file = fget(arg);
  453. if (file) {
  454. inode = file->f_path.dentry->d_inode;
  455. if (S_ISSOCK(inode->i_mode)) {
  456. lo->file = file;
  457. lo->sock = SOCKET_I(inode);
  458. error = 0;
  459. } else {
  460. fput(file);
  461. }
  462. }
  463. return error;
  464. case NBD_SET_BLKSIZE:
  465. lo->blksize = arg;
  466. lo->bytesize &= ~(lo->blksize-1);
  467. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  468. set_blocksize(inode->i_bdev, lo->blksize);
  469. set_capacity(lo->disk, lo->bytesize >> 9);
  470. return 0;
  471. case NBD_SET_SIZE:
  472. lo->bytesize = arg & ~(lo->blksize-1);
  473. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  474. set_blocksize(inode->i_bdev, lo->blksize);
  475. set_capacity(lo->disk, lo->bytesize >> 9);
  476. return 0;
  477. case NBD_SET_SIZE_BLOCKS:
  478. lo->bytesize = ((u64) arg) * lo->blksize;
  479. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  480. set_blocksize(inode->i_bdev, lo->blksize);
  481. set_capacity(lo->disk, lo->bytesize >> 9);
  482. return 0;
  483. case NBD_DO_IT:
  484. if (!lo->file)
  485. return -EINVAL;
  486. error = nbd_do_it(lo);
  487. if (error)
  488. return error;
  489. /* on return tidy up in case we have a signal */
  490. /* Forcibly shutdown the socket causing all listeners
  491. * to error
  492. *
  493. * FIXME: This code is duplicated from sys_shutdown, but
  494. * there should be a more generic interface rather than
  495. * calling socket ops directly here */
  496. mutex_lock(&lo->tx_lock);
  497. if (lo->sock) {
  498. printk(KERN_WARNING "%s: shutting down socket\n",
  499. lo->disk->disk_name);
  500. lo->sock->ops->shutdown(lo->sock,
  501. SEND_SHUTDOWN|RCV_SHUTDOWN);
  502. lo->sock = NULL;
  503. }
  504. mutex_unlock(&lo->tx_lock);
  505. file = lo->file;
  506. lo->file = NULL;
  507. nbd_clear_que(lo);
  508. printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
  509. if (file)
  510. fput(file);
  511. return lo->harderror;
  512. case NBD_CLEAR_QUE:
  513. /*
  514. * This is for compatibility only. The queue is always cleared
  515. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  516. */
  517. BUG_ON(!lo->sock && !list_empty(&lo->queue_head));
  518. return 0;
  519. case NBD_PRINT_DEBUG:
  520. printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
  521. inode->i_bdev->bd_disk->disk_name,
  522. lo->queue_head.next, lo->queue_head.prev,
  523. &lo->queue_head);
  524. return 0;
  525. }
  526. return -EINVAL;
  527. }
  528. static struct block_device_operations nbd_fops =
  529. {
  530. .owner = THIS_MODULE,
  531. .ioctl = nbd_ioctl,
  532. };
  533. /*
  534. * And here should be modules and kernel interface
  535. * (Just smiley confuses emacs :-)
  536. */
  537. static int __init nbd_init(void)
  538. {
  539. int err = -ENOMEM;
  540. int i;
  541. BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
  542. if (nbds_max > MAX_NBD) {
  543. printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
  544. nbds_max);
  545. return -EINVAL;
  546. }
  547. for (i = 0; i < nbds_max; i++) {
  548. struct gendisk *disk = alloc_disk(1);
  549. if (!disk)
  550. goto out;
  551. nbd_dev[i].disk = disk;
  552. /*
  553. * The new linux 2.5 block layer implementation requires
  554. * every gendisk to have its very own request_queue struct.
  555. * These structs are big so we dynamically allocate them.
  556. */
  557. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  558. if (!disk->queue) {
  559. put_disk(disk);
  560. goto out;
  561. }
  562. }
  563. if (register_blkdev(NBD_MAJOR, "nbd")) {
  564. err = -EIO;
  565. goto out;
  566. }
  567. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  568. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  569. for (i = 0; i < nbds_max; i++) {
  570. struct gendisk *disk = nbd_dev[i].disk;
  571. nbd_dev[i].file = NULL;
  572. nbd_dev[i].magic = LO_MAGIC;
  573. nbd_dev[i].flags = 0;
  574. spin_lock_init(&nbd_dev[i].queue_lock);
  575. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  576. mutex_init(&nbd_dev[i].tx_lock);
  577. init_waitqueue_head(&nbd_dev[i].active_wq);
  578. nbd_dev[i].blksize = 1024;
  579. nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
  580. disk->major = NBD_MAJOR;
  581. disk->first_minor = i;
  582. disk->fops = &nbd_fops;
  583. disk->private_data = &nbd_dev[i];
  584. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  585. sprintf(disk->disk_name, "nbd%d", i);
  586. set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
  587. add_disk(disk);
  588. }
  589. return 0;
  590. out:
  591. while (i--) {
  592. blk_cleanup_queue(nbd_dev[i].disk->queue);
  593. put_disk(nbd_dev[i].disk);
  594. }
  595. return err;
  596. }
  597. static void __exit nbd_cleanup(void)
  598. {
  599. int i;
  600. for (i = 0; i < nbds_max; i++) {
  601. struct gendisk *disk = nbd_dev[i].disk;
  602. nbd_dev[i].magic = 0;
  603. if (disk) {
  604. del_gendisk(disk);
  605. blk_cleanup_queue(disk->queue);
  606. put_disk(disk);
  607. }
  608. }
  609. unregister_blkdev(NBD_MAJOR, "nbd");
  610. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  611. }
  612. module_init(nbd_init);
  613. module_exit(nbd_cleanup);
  614. MODULE_DESCRIPTION("Network Block Device");
  615. MODULE_LICENSE("GPL");
  616. module_param(nbds_max, int, 0444);
  617. MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
  618. #ifndef NDEBUG
  619. module_param(debugflags, int, 0644);
  620. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  621. #endif