nbd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. request_queue_t *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. unsigned long flags;
  112. sigset_t oldset;
  113. /* Allow interception of SIGKILL only
  114. * Don't allow other signals to interrupt the transmission */
  115. spin_lock_irqsave(&current->sighand->siglock, flags);
  116. oldset = current->blocked;
  117. sigfillset(&current->blocked);
  118. sigdelsetmask(&current->blocked, sigmask(SIGKILL));
  119. recalc_sigpending();
  120. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  121. do {
  122. sock->sk->sk_allocation = GFP_NOIO;
  123. iov.iov_base = buf;
  124. iov.iov_len = size;
  125. msg.msg_name = NULL;
  126. msg.msg_namelen = 0;
  127. msg.msg_control = NULL;
  128. msg.msg_controllen = 0;
  129. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  130. if (send)
  131. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  132. else
  133. result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  134. if (signal_pending(current)) {
  135. siginfo_t info;
  136. spin_lock_irqsave(&current->sighand->siglock, flags);
  137. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  138. current->pid, current->comm,
  139. dequeue_signal(current, &current->blocked, &info));
  140. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  141. result = -EINTR;
  142. break;
  143. }
  144. if (result <= 0) {
  145. if (result == 0)
  146. result = -EPIPE; /* short read */
  147. break;
  148. }
  149. size -= result;
  150. buf += result;
  151. } while (size > 0);
  152. spin_lock_irqsave(&current->sighand->siglock, flags);
  153. current->blocked = oldset;
  154. recalc_sigpending();
  155. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  156. return result;
  157. }
  158. static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  159. int flags)
  160. {
  161. int result;
  162. void *kaddr = kmap(bvec->bv_page);
  163. result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  164. flags);
  165. kunmap(bvec->bv_page);
  166. return result;
  167. }
  168. static int nbd_send_req(struct nbd_device *lo, struct request *req)
  169. {
  170. int result, i, flags;
  171. struct nbd_request request;
  172. unsigned long size = req->nr_sectors << 9;
  173. struct socket *sock = lo->sock;
  174. request.magic = htonl(NBD_REQUEST_MAGIC);
  175. request.type = htonl(nbd_cmd(req));
  176. request.from = cpu_to_be64((u64) req->sector << 9);
  177. request.len = htonl(size);
  178. memcpy(request.handle, &req, sizeof(req));
  179. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
  180. lo->disk->disk_name, req,
  181. nbdcmd_to_ascii(nbd_cmd(req)),
  182. (unsigned long long)req->sector << 9,
  183. req->nr_sectors << 9);
  184. result = sock_xmit(sock, 1, &request, sizeof(request),
  185. (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
  186. if (result <= 0) {
  187. printk(KERN_ERR "%s: Send control failed (result %d)\n",
  188. lo->disk->disk_name, result);
  189. goto error_out;
  190. }
  191. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  192. struct bio *bio;
  193. /*
  194. * we are really probing at internals to determine
  195. * whether to set MSG_MORE or not...
  196. */
  197. rq_for_each_bio(bio, req) {
  198. struct bio_vec *bvec;
  199. bio_for_each_segment(bvec, bio, i) {
  200. flags = 0;
  201. if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
  202. flags = MSG_MORE;
  203. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  204. lo->disk->disk_name, req,
  205. bvec->bv_len);
  206. result = sock_send_bvec(sock, bvec, flags);
  207. if (result <= 0) {
  208. printk(KERN_ERR "%s: Send data failed (result %d)\n",
  209. lo->disk->disk_name,
  210. result);
  211. goto error_out;
  212. }
  213. }
  214. }
  215. }
  216. return 0;
  217. error_out:
  218. return 1;
  219. }
  220. static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  221. {
  222. struct request *req;
  223. struct list_head *tmp;
  224. struct request *xreq;
  225. int err;
  226. memcpy(&xreq, handle, sizeof(xreq));
  227. err = wait_event_interruptible(lo->active_wq, lo->active_req != xreq);
  228. if (unlikely(err))
  229. goto out;
  230. spin_lock(&lo->queue_lock);
  231. list_for_each(tmp, &lo->queue_head) {
  232. req = list_entry(tmp, struct request, queuelist);
  233. if (req != xreq)
  234. continue;
  235. list_del_init(&req->queuelist);
  236. spin_unlock(&lo->queue_lock);
  237. return req;
  238. }
  239. spin_unlock(&lo->queue_lock);
  240. err = -ENOENT;
  241. out:
  242. return ERR_PTR(err);
  243. }
  244. static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
  245. {
  246. int result;
  247. void *kaddr = kmap(bvec->bv_page);
  248. result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  249. MSG_WAITALL);
  250. kunmap(bvec->bv_page);
  251. return result;
  252. }
  253. /* NULL returned = something went wrong, inform userspace */
  254. static struct request *nbd_read_stat(struct nbd_device *lo)
  255. {
  256. int result;
  257. struct nbd_reply reply;
  258. struct request *req;
  259. struct socket *sock = lo->sock;
  260. reply.magic = 0;
  261. result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
  262. if (result <= 0) {
  263. printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  264. lo->disk->disk_name, result);
  265. goto harderror;
  266. }
  267. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  268. printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  269. lo->disk->disk_name,
  270. (unsigned long)ntohl(reply.magic));
  271. result = -EPROTO;
  272. goto harderror;
  273. }
  274. req = nbd_find_request(lo, reply.handle);
  275. if (unlikely(IS_ERR(req))) {
  276. result = PTR_ERR(req);
  277. if (result != -ENOENT)
  278. goto harderror;
  279. printk(KERN_ERR "%s: Unexpected reply (%p)\n",
  280. lo->disk->disk_name, reply.handle);
  281. result = -EBADR;
  282. goto harderror;
  283. }
  284. if (ntohl(reply.error)) {
  285. printk(KERN_ERR "%s: Other side returned error (%d)\n",
  286. lo->disk->disk_name, ntohl(reply.error));
  287. req->errors++;
  288. return req;
  289. }
  290. dprintk(DBG_RX, "%s: request %p: got reply\n",
  291. lo->disk->disk_name, req);
  292. if (nbd_cmd(req) == NBD_CMD_READ) {
  293. int i;
  294. struct bio *bio;
  295. rq_for_each_bio(bio, req) {
  296. struct bio_vec *bvec;
  297. bio_for_each_segment(bvec, bio, i) {
  298. result = sock_recv_bvec(sock, bvec);
  299. if (result <= 0) {
  300. printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  301. lo->disk->disk_name,
  302. result);
  303. req->errors++;
  304. return req;
  305. }
  306. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  307. lo->disk->disk_name, req, bvec->bv_len);
  308. }
  309. }
  310. }
  311. return req;
  312. harderror:
  313. lo->harderror = result;
  314. return NULL;
  315. }
  316. static void nbd_do_it(struct nbd_device *lo)
  317. {
  318. struct request *req;
  319. BUG_ON(lo->magic != LO_MAGIC);
  320. while ((req = nbd_read_stat(lo)) != NULL)
  321. nbd_end_request(req);
  322. return;
  323. }
  324. static void nbd_clear_que(struct nbd_device *lo)
  325. {
  326. struct request *req;
  327. BUG_ON(lo->magic != LO_MAGIC);
  328. /*
  329. * Because we have set lo->sock to NULL under the tx_lock, all
  330. * modifications to the list must have completed by now. For
  331. * the same reason, the active_req must be NULL.
  332. *
  333. * As a consequence, we don't need to take the spin lock while
  334. * purging the list here.
  335. */
  336. BUG_ON(lo->sock);
  337. BUG_ON(lo->active_req);
  338. while (!list_empty(&lo->queue_head)) {
  339. req = list_entry(lo->queue_head.next, struct request,
  340. queuelist);
  341. list_del_init(&req->queuelist);
  342. req->errors++;
  343. nbd_end_request(req);
  344. }
  345. }
  346. /*
  347. * We always wait for result of write, for now. It would be nice to make it optional
  348. * in future
  349. * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
  350. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  351. */
  352. static void do_nbd_request(request_queue_t * q)
  353. {
  354. struct request *req;
  355. while ((req = elv_next_request(q)) != NULL) {
  356. struct nbd_device *lo;
  357. blkdev_dequeue_request(req);
  358. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n",
  359. req->rq_disk->disk_name, req, req->cmd_type);
  360. if (!blk_fs_request(req))
  361. goto error_out;
  362. lo = req->rq_disk->private_data;
  363. BUG_ON(lo->magic != LO_MAGIC);
  364. nbd_cmd(req) = NBD_CMD_READ;
  365. if (rq_data_dir(req) == WRITE) {
  366. nbd_cmd(req) = NBD_CMD_WRITE;
  367. if (lo->flags & NBD_READ_ONLY) {
  368. printk(KERN_ERR "%s: Write on read-only\n",
  369. lo->disk->disk_name);
  370. goto error_out;
  371. }
  372. }
  373. req->errors = 0;
  374. spin_unlock_irq(q->queue_lock);
  375. mutex_lock(&lo->tx_lock);
  376. if (unlikely(!lo->sock)) {
  377. mutex_unlock(&lo->tx_lock);
  378. printk(KERN_ERR "%s: Attempted send on closed socket\n",
  379. lo->disk->disk_name);
  380. req->errors++;
  381. nbd_end_request(req);
  382. spin_lock_irq(q->queue_lock);
  383. continue;
  384. }
  385. lo->active_req = req;
  386. if (nbd_send_req(lo, req) != 0) {
  387. printk(KERN_ERR "%s: Request send failed\n",
  388. lo->disk->disk_name);
  389. req->errors++;
  390. nbd_end_request(req);
  391. } else {
  392. spin_lock(&lo->queue_lock);
  393. list_add(&req->queuelist, &lo->queue_head);
  394. spin_unlock(&lo->queue_lock);
  395. }
  396. lo->active_req = NULL;
  397. mutex_unlock(&lo->tx_lock);
  398. wake_up_all(&lo->active_wq);
  399. spin_lock_irq(q->queue_lock);
  400. continue;
  401. error_out:
  402. req->errors++;
  403. spin_unlock(q->queue_lock);
  404. nbd_end_request(req);
  405. spin_lock(q->queue_lock);
  406. }
  407. return;
  408. }
  409. static int nbd_ioctl(struct inode *inode, struct file *file,
  410. unsigned int cmd, unsigned long arg)
  411. {
  412. struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
  413. int error;
  414. struct request sreq ;
  415. if (!capable(CAP_SYS_ADMIN))
  416. return -EPERM;
  417. BUG_ON(lo->magic != LO_MAGIC);
  418. /* Anyone capable of this syscall can do *real bad* things */
  419. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  420. lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  421. switch (cmd) {
  422. case NBD_DISCONNECT:
  423. printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
  424. sreq.cmd_type = REQ_TYPE_SPECIAL;
  425. nbd_cmd(&sreq) = NBD_CMD_DISC;
  426. /*
  427. * Set these to sane values in case server implementation
  428. * fails to check the request type first and also to keep
  429. * debugging output cleaner.
  430. */
  431. sreq.sector = 0;
  432. sreq.nr_sectors = 0;
  433. if (!lo->sock)
  434. return -EINVAL;
  435. nbd_send_req(lo, &sreq);
  436. return 0;
  437. case NBD_CLEAR_SOCK:
  438. error = 0;
  439. mutex_lock(&lo->tx_lock);
  440. lo->sock = NULL;
  441. mutex_unlock(&lo->tx_lock);
  442. file = lo->file;
  443. lo->file = NULL;
  444. nbd_clear_que(lo);
  445. BUG_ON(!list_empty(&lo->queue_head));
  446. if (file)
  447. fput(file);
  448. return error;
  449. case NBD_SET_SOCK:
  450. if (lo->file)
  451. return -EBUSY;
  452. error = -EINVAL;
  453. file = fget(arg);
  454. if (file) {
  455. inode = file->f_dentry->d_inode;
  456. if (S_ISSOCK(inode->i_mode)) {
  457. lo->file = file;
  458. lo->sock = SOCKET_I(inode);
  459. error = 0;
  460. } else {
  461. fput(file);
  462. }
  463. }
  464. return error;
  465. case NBD_SET_BLKSIZE:
  466. lo->blksize = arg;
  467. lo->bytesize &= ~(lo->blksize-1);
  468. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  469. set_blocksize(inode->i_bdev, lo->blksize);
  470. set_capacity(lo->disk, lo->bytesize >> 9);
  471. return 0;
  472. case NBD_SET_SIZE:
  473. lo->bytesize = arg & ~(lo->blksize-1);
  474. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  475. set_blocksize(inode->i_bdev, lo->blksize);
  476. set_capacity(lo->disk, lo->bytesize >> 9);
  477. return 0;
  478. case NBD_SET_SIZE_BLOCKS:
  479. lo->bytesize = ((u64) arg) * lo->blksize;
  480. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  481. set_blocksize(inode->i_bdev, lo->blksize);
  482. set_capacity(lo->disk, lo->bytesize >> 9);
  483. return 0;
  484. case NBD_DO_IT:
  485. if (!lo->file)
  486. return -EINVAL;
  487. nbd_do_it(lo);
  488. /* on return tidy up in case we have a signal */
  489. /* Forcibly shutdown the socket causing all listeners
  490. * to error
  491. *
  492. * FIXME: This code is duplicated from sys_shutdown, but
  493. * there should be a more generic interface rather than
  494. * calling socket ops directly here */
  495. mutex_lock(&lo->tx_lock);
  496. if (lo->sock) {
  497. printk(KERN_WARNING "%s: shutting down socket\n",
  498. lo->disk->disk_name);
  499. lo->sock->ops->shutdown(lo->sock,
  500. SEND_SHUTDOWN|RCV_SHUTDOWN);
  501. lo->sock = NULL;
  502. }
  503. mutex_unlock(&lo->tx_lock);
  504. file = lo->file;
  505. lo->file = NULL;
  506. nbd_clear_que(lo);
  507. printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
  508. if (file)
  509. fput(file);
  510. return lo->harderror;
  511. case NBD_CLEAR_QUE:
  512. /*
  513. * This is for compatibility only. The queue is always cleared
  514. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  515. */
  516. BUG_ON(!lo->sock && !list_empty(&lo->queue_head));
  517. return 0;
  518. case NBD_PRINT_DEBUG:
  519. printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
  520. inode->i_bdev->bd_disk->disk_name,
  521. lo->queue_head.next, lo->queue_head.prev,
  522. &lo->queue_head);
  523. return 0;
  524. }
  525. return -EINVAL;
  526. }
  527. static struct block_device_operations nbd_fops =
  528. {
  529. .owner = THIS_MODULE,
  530. .ioctl = nbd_ioctl,
  531. };
  532. /*
  533. * And here should be modules and kernel interface
  534. * (Just smiley confuses emacs :-)
  535. */
  536. static int __init nbd_init(void)
  537. {
  538. int err = -ENOMEM;
  539. int i;
  540. BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
  541. if (nbds_max > MAX_NBD) {
  542. printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
  543. nbds_max);
  544. return -EINVAL;
  545. }
  546. for (i = 0; i < nbds_max; i++) {
  547. struct gendisk *disk = alloc_disk(1);
  548. if (!disk)
  549. goto out;
  550. nbd_dev[i].disk = disk;
  551. /*
  552. * The new linux 2.5 block layer implementation requires
  553. * every gendisk to have its very own request_queue struct.
  554. * These structs are big so we dynamically allocate them.
  555. */
  556. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  557. if (!disk->queue) {
  558. put_disk(disk);
  559. goto out;
  560. }
  561. }
  562. if (register_blkdev(NBD_MAJOR, "nbd")) {
  563. err = -EIO;
  564. goto out;
  565. }
  566. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  567. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  568. for (i = 0; i < nbds_max; i++) {
  569. struct gendisk *disk = nbd_dev[i].disk;
  570. nbd_dev[i].file = NULL;
  571. nbd_dev[i].magic = LO_MAGIC;
  572. nbd_dev[i].flags = 0;
  573. spin_lock_init(&nbd_dev[i].queue_lock);
  574. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  575. mutex_init(&nbd_dev[i].tx_lock);
  576. init_waitqueue_head(&nbd_dev[i].active_wq);
  577. nbd_dev[i].blksize = 1024;
  578. nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
  579. disk->major = NBD_MAJOR;
  580. disk->first_minor = i;
  581. disk->fops = &nbd_fops;
  582. disk->private_data = &nbd_dev[i];
  583. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  584. sprintf(disk->disk_name, "nbd%d", i);
  585. set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
  586. add_disk(disk);
  587. }
  588. return 0;
  589. out:
  590. while (i--) {
  591. blk_cleanup_queue(nbd_dev[i].disk->queue);
  592. put_disk(nbd_dev[i].disk);
  593. }
  594. return err;
  595. }
  596. static void __exit nbd_cleanup(void)
  597. {
  598. int i;
  599. for (i = 0; i < nbds_max; i++) {
  600. struct gendisk *disk = nbd_dev[i].disk;
  601. nbd_dev[i].magic = 0;
  602. if (disk) {
  603. del_gendisk(disk);
  604. blk_cleanup_queue(disk->queue);
  605. put_disk(disk);
  606. }
  607. }
  608. unregister_blkdev(NBD_MAJOR, "nbd");
  609. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  610. }
  611. module_init(nbd_init);
  612. module_exit(nbd_cleanup);
  613. MODULE_DESCRIPTION("Network Block Device");
  614. MODULE_LICENSE("GPL");
  615. module_param(nbds_max, int, 0444);
  616. MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
  617. #ifndef NDEBUG
  618. module_param(debugflags, int, 0644);
  619. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  620. #endif