nbd.c 21 KB

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