nbd.c 19 KB

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