nbd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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, 2008 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/mutex.h>
  26. #include <linux/compiler.h>
  27. #include <linux/err.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <net/sock.h>
  31. #include <linux/net.h>
  32. #include <linux/kthread.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/types.h>
  35. #include <linux/nbd.h>
  36. #define NBD_MAGIC 0x68797548
  37. #ifdef NDEBUG
  38. #define dprintk(flags, fmt...)
  39. #else /* NDEBUG */
  40. #define dprintk(flags, fmt...) do { \
  41. if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
  42. } while (0)
  43. #define DBG_IOCTL 0x0004
  44. #define DBG_INIT 0x0010
  45. #define DBG_EXIT 0x0020
  46. #define DBG_BLKDEV 0x0100
  47. #define DBG_RX 0x0200
  48. #define DBG_TX 0x0400
  49. static unsigned int debugflags;
  50. #endif /* NDEBUG */
  51. static unsigned int nbds_max = 16;
  52. static struct nbd_device *nbd_dev;
  53. static int max_part;
  54. /*
  55. * Use just one lock (or at most 1 per NIC). Two arguments for this:
  56. * 1. Each NIC is essentially a synchronization point for all servers
  57. * accessed through that NIC so there's no need to have more locks
  58. * than NICs anyway.
  59. * 2. More locks lead to more "Dirty cache line bouncing" which will slow
  60. * down each lock to the point where they're actually slower than just
  61. * a single lock.
  62. * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
  63. */
  64. static DEFINE_SPINLOCK(nbd_lock);
  65. #ifndef NDEBUG
  66. static const char *ioctl_cmd_to_ascii(int cmd)
  67. {
  68. switch (cmd) {
  69. case NBD_SET_SOCK: return "set-sock";
  70. case NBD_SET_BLKSIZE: return "set-blksize";
  71. case NBD_SET_SIZE: return "set-size";
  72. case NBD_SET_TIMEOUT: return "set-timeout";
  73. case NBD_SET_FLAGS: return "set-flags";
  74. case NBD_DO_IT: return "do-it";
  75. case NBD_CLEAR_SOCK: return "clear-sock";
  76. case NBD_CLEAR_QUE: return "clear-que";
  77. case NBD_PRINT_DEBUG: return "print-debug";
  78. case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
  79. case NBD_DISCONNECT: return "disconnect";
  80. case BLKROSET: return "set-read-only";
  81. case BLKFLSBUF: return "flush-buffer-cache";
  82. }
  83. return "unknown";
  84. }
  85. static const char *nbdcmd_to_ascii(int cmd)
  86. {
  87. switch (cmd) {
  88. case NBD_CMD_READ: return "read";
  89. case NBD_CMD_WRITE: return "write";
  90. case NBD_CMD_DISC: return "disconnect";
  91. case NBD_CMD_FLUSH: return "flush";
  92. case NBD_CMD_TRIM: return "trim/discard";
  93. }
  94. return "invalid";
  95. }
  96. #endif /* NDEBUG */
  97. static void nbd_end_request(struct request *req)
  98. {
  99. int error = req->errors ? -EIO : 0;
  100. struct request_queue *q = req->q;
  101. unsigned long flags;
  102. dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
  103. req, error ? "failed" : "done");
  104. spin_lock_irqsave(q->queue_lock, flags);
  105. __blk_end_request_all(req, error);
  106. spin_unlock_irqrestore(q->queue_lock, flags);
  107. }
  108. static void sock_shutdown(struct nbd_device *nbd, int lock)
  109. {
  110. /* Forcibly shutdown the socket causing all listeners
  111. * to error
  112. *
  113. * FIXME: This code is duplicated from sys_shutdown, but
  114. * there should be a more generic interface rather than
  115. * calling socket ops directly here */
  116. if (lock)
  117. mutex_lock(&nbd->tx_lock);
  118. if (nbd->sock) {
  119. dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
  120. kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
  121. nbd->sock = NULL;
  122. }
  123. if (lock)
  124. mutex_unlock(&nbd->tx_lock);
  125. }
  126. static void nbd_xmit_timeout(unsigned long arg)
  127. {
  128. struct task_struct *task = (struct task_struct *)arg;
  129. printk(KERN_WARNING "nbd: killing hung xmit (%s, pid: %d)\n",
  130. task->comm, task->pid);
  131. force_sig(SIGKILL, task);
  132. }
  133. /*
  134. * Send or receive packet.
  135. */
  136. static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
  137. int msg_flags)
  138. {
  139. struct socket *sock = nbd->sock;
  140. int result;
  141. struct msghdr msg;
  142. struct kvec iov;
  143. sigset_t blocked, oldset;
  144. unsigned long pflags = current->flags;
  145. if (unlikely(!sock)) {
  146. dev_err(disk_to_dev(nbd->disk),
  147. "Attempted %s on closed socket in sock_xmit\n",
  148. (send ? "send" : "recv"));
  149. return -EINVAL;
  150. }
  151. /* Allow interception of SIGKILL only
  152. * Don't allow other signals to interrupt the transmission */
  153. siginitsetinv(&blocked, sigmask(SIGKILL));
  154. sigprocmask(SIG_SETMASK, &blocked, &oldset);
  155. current->flags |= PF_MEMALLOC;
  156. do {
  157. sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
  158. iov.iov_base = buf;
  159. iov.iov_len = size;
  160. msg.msg_name = NULL;
  161. msg.msg_namelen = 0;
  162. msg.msg_control = NULL;
  163. msg.msg_controllen = 0;
  164. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  165. if (send) {
  166. struct timer_list ti;
  167. if (nbd->xmit_timeout) {
  168. init_timer(&ti);
  169. ti.function = nbd_xmit_timeout;
  170. ti.data = (unsigned long)current;
  171. ti.expires = jiffies + nbd->xmit_timeout;
  172. add_timer(&ti);
  173. }
  174. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  175. if (nbd->xmit_timeout)
  176. del_timer_sync(&ti);
  177. } else
  178. result = kernel_recvmsg(sock, &msg, &iov, 1, size,
  179. msg.msg_flags);
  180. if (signal_pending(current)) {
  181. siginfo_t info;
  182. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  183. task_pid_nr(current), current->comm,
  184. dequeue_signal_lock(current, &current->blocked, &info));
  185. result = -EINTR;
  186. sock_shutdown(nbd, !send);
  187. break;
  188. }
  189. if (result <= 0) {
  190. if (result == 0)
  191. result = -EPIPE; /* short read */
  192. break;
  193. }
  194. size -= result;
  195. buf += result;
  196. } while (size > 0);
  197. sigprocmask(SIG_SETMASK, &oldset, NULL);
  198. tsk_restore_flags(current, pflags, PF_MEMALLOC);
  199. return result;
  200. }
  201. static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
  202. int flags)
  203. {
  204. int result;
  205. void *kaddr = kmap(bvec->bv_page);
  206. result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
  207. bvec->bv_len, flags);
  208. kunmap(bvec->bv_page);
  209. return result;
  210. }
  211. /* always call with the tx_lock held */
  212. static int nbd_send_req(struct nbd_device *nbd, struct request *req)
  213. {
  214. int result, flags;
  215. struct nbd_request request;
  216. unsigned long size = blk_rq_bytes(req);
  217. memset(&request, 0, sizeof(request));
  218. request.magic = htonl(NBD_REQUEST_MAGIC);
  219. request.type = htonl(nbd_cmd(req));
  220. if (nbd_cmd(req) != NBD_CMD_FLUSH && nbd_cmd(req) != NBD_CMD_DISC) {
  221. request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
  222. request.len = htonl(size);
  223. }
  224. memcpy(request.handle, &req, sizeof(req));
  225. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%uB)\n",
  226. nbd->disk->disk_name, req,
  227. nbdcmd_to_ascii(nbd_cmd(req)),
  228. (unsigned long long)blk_rq_pos(req) << 9,
  229. blk_rq_bytes(req));
  230. result = sock_xmit(nbd, 1, &request, sizeof(request),
  231. (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
  232. if (result <= 0) {
  233. dev_err(disk_to_dev(nbd->disk),
  234. "Send control failed (result %d)\n", result);
  235. goto error_out;
  236. }
  237. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  238. struct req_iterator iter;
  239. struct bio_vec bvec;
  240. /*
  241. * we are really probing at internals to determine
  242. * whether to set MSG_MORE or not...
  243. */
  244. rq_for_each_segment(bvec, req, iter) {
  245. flags = 0;
  246. if (!rq_iter_last(bvec, iter))
  247. flags = MSG_MORE;
  248. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  249. nbd->disk->disk_name, req, bvec.bv_len);
  250. result = sock_send_bvec(nbd, &bvec, flags);
  251. if (result <= 0) {
  252. dev_err(disk_to_dev(nbd->disk),
  253. "Send data failed (result %d)\n",
  254. result);
  255. goto error_out;
  256. }
  257. }
  258. }
  259. return 0;
  260. error_out:
  261. return -EIO;
  262. }
  263. static struct request *nbd_find_request(struct nbd_device *nbd,
  264. struct request *xreq)
  265. {
  266. struct request *req, *tmp;
  267. int err;
  268. err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
  269. if (unlikely(err))
  270. goto out;
  271. spin_lock(&nbd->queue_lock);
  272. list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
  273. if (req != xreq)
  274. continue;
  275. list_del_init(&req->queuelist);
  276. spin_unlock(&nbd->queue_lock);
  277. return req;
  278. }
  279. spin_unlock(&nbd->queue_lock);
  280. err = -ENOENT;
  281. out:
  282. return ERR_PTR(err);
  283. }
  284. static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
  285. {
  286. int result;
  287. void *kaddr = kmap(bvec->bv_page);
  288. result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  289. MSG_WAITALL);
  290. kunmap(bvec->bv_page);
  291. return result;
  292. }
  293. /* NULL returned = something went wrong, inform userspace */
  294. static struct request *nbd_read_stat(struct nbd_device *nbd)
  295. {
  296. int result;
  297. struct nbd_reply reply;
  298. struct request *req;
  299. reply.magic = 0;
  300. result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
  301. if (result <= 0) {
  302. dev_err(disk_to_dev(nbd->disk),
  303. "Receive control failed (result %d)\n", result);
  304. goto harderror;
  305. }
  306. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  307. dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
  308. (unsigned long)ntohl(reply.magic));
  309. result = -EPROTO;
  310. goto harderror;
  311. }
  312. req = nbd_find_request(nbd, *(struct request **)reply.handle);
  313. if (IS_ERR(req)) {
  314. result = PTR_ERR(req);
  315. if (result != -ENOENT)
  316. goto harderror;
  317. dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
  318. reply.handle);
  319. result = -EBADR;
  320. goto harderror;
  321. }
  322. if (ntohl(reply.error)) {
  323. dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
  324. ntohl(reply.error));
  325. req->errors++;
  326. return req;
  327. }
  328. dprintk(DBG_RX, "%s: request %p: got reply\n",
  329. nbd->disk->disk_name, req);
  330. if (nbd_cmd(req) == NBD_CMD_READ) {
  331. struct req_iterator iter;
  332. struct bio_vec bvec;
  333. rq_for_each_segment(bvec, req, iter) {
  334. result = sock_recv_bvec(nbd, &bvec);
  335. if (result <= 0) {
  336. dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
  337. result);
  338. req->errors++;
  339. return req;
  340. }
  341. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  342. nbd->disk->disk_name, req, bvec.bv_len);
  343. }
  344. }
  345. return req;
  346. harderror:
  347. nbd->harderror = result;
  348. return NULL;
  349. }
  350. static ssize_t pid_show(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. struct gendisk *disk = dev_to_disk(dev);
  354. return sprintf(buf, "%ld\n",
  355. (long) ((struct nbd_device *)disk->private_data)->pid);
  356. }
  357. static struct device_attribute pid_attr = {
  358. .attr = { .name = "pid", .mode = S_IRUGO},
  359. .show = pid_show,
  360. };
  361. static int nbd_do_it(struct nbd_device *nbd)
  362. {
  363. struct request *req;
  364. int ret;
  365. BUG_ON(nbd->magic != NBD_MAGIC);
  366. sk_set_memalloc(nbd->sock->sk);
  367. nbd->pid = task_pid_nr(current);
  368. ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
  369. if (ret) {
  370. dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
  371. nbd->pid = 0;
  372. return ret;
  373. }
  374. while ((req = nbd_read_stat(nbd)) != NULL)
  375. nbd_end_request(req);
  376. device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
  377. nbd->pid = 0;
  378. return 0;
  379. }
  380. static void nbd_clear_que(struct nbd_device *nbd)
  381. {
  382. struct request *req;
  383. BUG_ON(nbd->magic != NBD_MAGIC);
  384. /*
  385. * Because we have set nbd->sock to NULL under the tx_lock, all
  386. * modifications to the list must have completed by now. For
  387. * the same reason, the active_req must be NULL.
  388. *
  389. * As a consequence, we don't need to take the spin lock while
  390. * purging the list here.
  391. */
  392. BUG_ON(nbd->sock);
  393. BUG_ON(nbd->active_req);
  394. while (!list_empty(&nbd->queue_head)) {
  395. req = list_entry(nbd->queue_head.next, struct request,
  396. queuelist);
  397. list_del_init(&req->queuelist);
  398. req->errors++;
  399. nbd_end_request(req);
  400. }
  401. while (!list_empty(&nbd->waiting_queue)) {
  402. req = list_entry(nbd->waiting_queue.next, struct request,
  403. queuelist);
  404. list_del_init(&req->queuelist);
  405. req->errors++;
  406. nbd_end_request(req);
  407. }
  408. }
  409. static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
  410. {
  411. if (req->cmd_type != REQ_TYPE_FS)
  412. goto error_out;
  413. nbd_cmd(req) = NBD_CMD_READ;
  414. if (rq_data_dir(req) == WRITE) {
  415. if ((req->cmd_flags & REQ_DISCARD)) {
  416. WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
  417. nbd_cmd(req) = NBD_CMD_TRIM;
  418. } else
  419. nbd_cmd(req) = NBD_CMD_WRITE;
  420. if (nbd->flags & NBD_FLAG_READ_ONLY) {
  421. dev_err(disk_to_dev(nbd->disk),
  422. "Write on read-only\n");
  423. goto error_out;
  424. }
  425. }
  426. if (req->cmd_flags & REQ_FLUSH) {
  427. BUG_ON(unlikely(blk_rq_sectors(req)));
  428. nbd_cmd(req) = NBD_CMD_FLUSH;
  429. }
  430. req->errors = 0;
  431. mutex_lock(&nbd->tx_lock);
  432. if (unlikely(!nbd->sock)) {
  433. mutex_unlock(&nbd->tx_lock);
  434. dev_err(disk_to_dev(nbd->disk),
  435. "Attempted send on closed socket\n");
  436. goto error_out;
  437. }
  438. nbd->active_req = req;
  439. if (nbd_send_req(nbd, req) != 0) {
  440. dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
  441. req->errors++;
  442. nbd_end_request(req);
  443. } else {
  444. spin_lock(&nbd->queue_lock);
  445. list_add_tail(&req->queuelist, &nbd->queue_head);
  446. spin_unlock(&nbd->queue_lock);
  447. }
  448. nbd->active_req = NULL;
  449. mutex_unlock(&nbd->tx_lock);
  450. wake_up_all(&nbd->active_wq);
  451. return;
  452. error_out:
  453. req->errors++;
  454. nbd_end_request(req);
  455. }
  456. static int nbd_thread(void *data)
  457. {
  458. struct nbd_device *nbd = data;
  459. struct request *req;
  460. set_user_nice(current, MIN_NICE);
  461. while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
  462. /* wait for something to do */
  463. wait_event_interruptible(nbd->waiting_wq,
  464. kthread_should_stop() ||
  465. !list_empty(&nbd->waiting_queue));
  466. /* extract request */
  467. if (list_empty(&nbd->waiting_queue))
  468. continue;
  469. spin_lock_irq(&nbd->queue_lock);
  470. req = list_entry(nbd->waiting_queue.next, struct request,
  471. queuelist);
  472. list_del_init(&req->queuelist);
  473. spin_unlock_irq(&nbd->queue_lock);
  474. /* handle request */
  475. nbd_handle_req(nbd, req);
  476. }
  477. return 0;
  478. }
  479. /*
  480. * We always wait for result of write, for now. It would be nice to make it optional
  481. * in future
  482. * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
  483. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  484. */
  485. static void do_nbd_request(struct request_queue *q)
  486. __releases(q->queue_lock) __acquires(q->queue_lock)
  487. {
  488. struct request *req;
  489. while ((req = blk_fetch_request(q)) != NULL) {
  490. struct nbd_device *nbd;
  491. spin_unlock_irq(q->queue_lock);
  492. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n",
  493. req->rq_disk->disk_name, req, req->cmd_type);
  494. nbd = req->rq_disk->private_data;
  495. BUG_ON(nbd->magic != NBD_MAGIC);
  496. if (unlikely(!nbd->sock)) {
  497. dev_err(disk_to_dev(nbd->disk),
  498. "Attempted send on closed socket\n");
  499. req->errors++;
  500. nbd_end_request(req);
  501. spin_lock_irq(q->queue_lock);
  502. continue;
  503. }
  504. spin_lock_irq(&nbd->queue_lock);
  505. list_add_tail(&req->queuelist, &nbd->waiting_queue);
  506. spin_unlock_irq(&nbd->queue_lock);
  507. wake_up(&nbd->waiting_wq);
  508. spin_lock_irq(q->queue_lock);
  509. }
  510. }
  511. /* Must be called with tx_lock held */
  512. static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
  513. unsigned int cmd, unsigned long arg)
  514. {
  515. switch (cmd) {
  516. case NBD_DISCONNECT: {
  517. struct request sreq;
  518. dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
  519. if (!nbd->sock)
  520. return -EINVAL;
  521. mutex_unlock(&nbd->tx_lock);
  522. fsync_bdev(bdev);
  523. mutex_lock(&nbd->tx_lock);
  524. blk_rq_init(NULL, &sreq);
  525. sreq.cmd_type = REQ_TYPE_SPECIAL;
  526. nbd_cmd(&sreq) = NBD_CMD_DISC;
  527. /* Check again after getting mutex back. */
  528. if (!nbd->sock)
  529. return -EINVAL;
  530. nbd->disconnect = 1;
  531. nbd_send_req(nbd, &sreq);
  532. return 0;
  533. }
  534. case NBD_CLEAR_SOCK: {
  535. struct socket *sock = nbd->sock;
  536. nbd->sock = NULL;
  537. nbd_clear_que(nbd);
  538. BUG_ON(!list_empty(&nbd->queue_head));
  539. BUG_ON(!list_empty(&nbd->waiting_queue));
  540. kill_bdev(bdev);
  541. if (sock)
  542. sockfd_put(sock);
  543. return 0;
  544. }
  545. case NBD_SET_SOCK: {
  546. struct socket *sock;
  547. int err;
  548. if (nbd->sock)
  549. return -EBUSY;
  550. sock = sockfd_lookup(arg, &err);
  551. if (sock) {
  552. nbd->sock = sock;
  553. if (max_part > 0)
  554. bdev->bd_invalidated = 1;
  555. nbd->disconnect = 0; /* we're connected now */
  556. return 0;
  557. }
  558. return -EINVAL;
  559. }
  560. case NBD_SET_BLKSIZE:
  561. nbd->blksize = arg;
  562. nbd->bytesize &= ~(nbd->blksize-1);
  563. bdev->bd_inode->i_size = nbd->bytesize;
  564. set_blocksize(bdev, nbd->blksize);
  565. set_capacity(nbd->disk, nbd->bytesize >> 9);
  566. return 0;
  567. case NBD_SET_SIZE:
  568. nbd->bytesize = arg & ~(nbd->blksize-1);
  569. bdev->bd_inode->i_size = nbd->bytesize;
  570. set_blocksize(bdev, nbd->blksize);
  571. set_capacity(nbd->disk, nbd->bytesize >> 9);
  572. return 0;
  573. case NBD_SET_TIMEOUT:
  574. nbd->xmit_timeout = arg * HZ;
  575. return 0;
  576. case NBD_SET_FLAGS:
  577. nbd->flags = arg;
  578. return 0;
  579. case NBD_SET_SIZE_BLOCKS:
  580. nbd->bytesize = ((u64) arg) * nbd->blksize;
  581. bdev->bd_inode->i_size = nbd->bytesize;
  582. set_blocksize(bdev, nbd->blksize);
  583. set_capacity(nbd->disk, nbd->bytesize >> 9);
  584. return 0;
  585. case NBD_DO_IT: {
  586. struct task_struct *thread;
  587. struct socket *sock;
  588. int error;
  589. if (nbd->pid)
  590. return -EBUSY;
  591. if (!nbd->sock)
  592. return -EINVAL;
  593. mutex_unlock(&nbd->tx_lock);
  594. if (nbd->flags & NBD_FLAG_READ_ONLY)
  595. set_device_ro(bdev, true);
  596. if (nbd->flags & NBD_FLAG_SEND_TRIM)
  597. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  598. nbd->disk->queue);
  599. if (nbd->flags & NBD_FLAG_SEND_FLUSH)
  600. blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
  601. else
  602. blk_queue_flush(nbd->disk->queue, 0);
  603. thread = kthread_create(nbd_thread, nbd, "%s",
  604. nbd->disk->disk_name);
  605. if (IS_ERR(thread)) {
  606. mutex_lock(&nbd->tx_lock);
  607. return PTR_ERR(thread);
  608. }
  609. wake_up_process(thread);
  610. error = nbd_do_it(nbd);
  611. kthread_stop(thread);
  612. mutex_lock(&nbd->tx_lock);
  613. if (error)
  614. return error;
  615. sock_shutdown(nbd, 0);
  616. sock = nbd->sock;
  617. nbd->sock = NULL;
  618. nbd_clear_que(nbd);
  619. dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
  620. kill_bdev(bdev);
  621. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
  622. set_device_ro(bdev, false);
  623. if (sock)
  624. sockfd_put(sock);
  625. nbd->flags = 0;
  626. nbd->bytesize = 0;
  627. bdev->bd_inode->i_size = 0;
  628. set_capacity(nbd->disk, 0);
  629. if (max_part > 0)
  630. ioctl_by_bdev(bdev, BLKRRPART, 0);
  631. if (nbd->disconnect) /* user requested, ignore socket errors */
  632. return 0;
  633. return nbd->harderror;
  634. }
  635. case NBD_CLEAR_QUE:
  636. /*
  637. * This is for compatibility only. The queue is always cleared
  638. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  639. */
  640. return 0;
  641. case NBD_PRINT_DEBUG:
  642. dev_info(disk_to_dev(nbd->disk),
  643. "next = %p, prev = %p, head = %p\n",
  644. nbd->queue_head.next, nbd->queue_head.prev,
  645. &nbd->queue_head);
  646. return 0;
  647. }
  648. return -ENOTTY;
  649. }
  650. static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
  651. unsigned int cmd, unsigned long arg)
  652. {
  653. struct nbd_device *nbd = bdev->bd_disk->private_data;
  654. int error;
  655. if (!capable(CAP_SYS_ADMIN))
  656. return -EPERM;
  657. BUG_ON(nbd->magic != NBD_MAGIC);
  658. /* Anyone capable of this syscall can do *real bad* things */
  659. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  660. nbd->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  661. mutex_lock(&nbd->tx_lock);
  662. error = __nbd_ioctl(bdev, nbd, cmd, arg);
  663. mutex_unlock(&nbd->tx_lock);
  664. return error;
  665. }
  666. static const struct block_device_operations nbd_fops =
  667. {
  668. .owner = THIS_MODULE,
  669. .ioctl = nbd_ioctl,
  670. };
  671. /*
  672. * And here should be modules and kernel interface
  673. * (Just smiley confuses emacs :-)
  674. */
  675. static int __init nbd_init(void)
  676. {
  677. int err = -ENOMEM;
  678. int i;
  679. int part_shift;
  680. BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
  681. if (max_part < 0) {
  682. printk(KERN_ERR "nbd: max_part must be >= 0\n");
  683. return -EINVAL;
  684. }
  685. part_shift = 0;
  686. if (max_part > 0) {
  687. part_shift = fls(max_part);
  688. /*
  689. * Adjust max_part according to part_shift as it is exported
  690. * to user space so that user can know the max number of
  691. * partition kernel should be able to manage.
  692. *
  693. * Note that -1 is required because partition 0 is reserved
  694. * for the whole disk.
  695. */
  696. max_part = (1UL << part_shift) - 1;
  697. }
  698. if ((1UL << part_shift) > DISK_MAX_PARTS)
  699. return -EINVAL;
  700. if (nbds_max > 1UL << (MINORBITS - part_shift))
  701. return -EINVAL;
  702. nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
  703. if (!nbd_dev)
  704. return -ENOMEM;
  705. for (i = 0; i < nbds_max; i++) {
  706. struct gendisk *disk = alloc_disk(1 << part_shift);
  707. if (!disk)
  708. goto out;
  709. nbd_dev[i].disk = disk;
  710. /*
  711. * The new linux 2.5 block layer implementation requires
  712. * every gendisk to have its very own request_queue struct.
  713. * These structs are big so we dynamically allocate them.
  714. */
  715. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  716. if (!disk->queue) {
  717. put_disk(disk);
  718. goto out;
  719. }
  720. /*
  721. * Tell the block layer that we are not a rotational device
  722. */
  723. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
  724. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
  725. disk->queue->limits.discard_granularity = 512;
  726. disk->queue->limits.max_discard_sectors = UINT_MAX;
  727. disk->queue->limits.discard_zeroes_data = 0;
  728. blk_queue_max_hw_sectors(disk->queue, 65536);
  729. disk->queue->limits.max_sectors = 256;
  730. }
  731. if (register_blkdev(NBD_MAJOR, "nbd")) {
  732. err = -EIO;
  733. goto out;
  734. }
  735. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  736. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  737. for (i = 0; i < nbds_max; i++) {
  738. struct gendisk *disk = nbd_dev[i].disk;
  739. nbd_dev[i].magic = NBD_MAGIC;
  740. INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
  741. spin_lock_init(&nbd_dev[i].queue_lock);
  742. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  743. mutex_init(&nbd_dev[i].tx_lock);
  744. init_waitqueue_head(&nbd_dev[i].active_wq);
  745. init_waitqueue_head(&nbd_dev[i].waiting_wq);
  746. nbd_dev[i].blksize = 1024;
  747. nbd_dev[i].bytesize = 0;
  748. disk->major = NBD_MAJOR;
  749. disk->first_minor = i << part_shift;
  750. disk->fops = &nbd_fops;
  751. disk->private_data = &nbd_dev[i];
  752. sprintf(disk->disk_name, "nbd%d", i);
  753. set_capacity(disk, 0);
  754. add_disk(disk);
  755. }
  756. return 0;
  757. out:
  758. while (i--) {
  759. blk_cleanup_queue(nbd_dev[i].disk->queue);
  760. put_disk(nbd_dev[i].disk);
  761. }
  762. kfree(nbd_dev);
  763. return err;
  764. }
  765. static void __exit nbd_cleanup(void)
  766. {
  767. int i;
  768. for (i = 0; i < nbds_max; i++) {
  769. struct gendisk *disk = nbd_dev[i].disk;
  770. nbd_dev[i].magic = 0;
  771. if (disk) {
  772. del_gendisk(disk);
  773. blk_cleanup_queue(disk->queue);
  774. put_disk(disk);
  775. }
  776. }
  777. unregister_blkdev(NBD_MAJOR, "nbd");
  778. kfree(nbd_dev);
  779. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  780. }
  781. module_init(nbd_init);
  782. module_exit(nbd_cleanup);
  783. MODULE_DESCRIPTION("Network Block Device");
  784. MODULE_LICENSE("GPL");
  785. module_param(nbds_max, int, 0444);
  786. MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
  787. module_param(max_part, int, 0444);
  788. MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");
  789. #ifndef NDEBUG
  790. module_param(debugflags, int, 0644);
  791. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  792. #endif