nbd.c 18 KB

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