nbd.c 18 KB

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