nbd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. request.magic = htonl(NBD_REQUEST_MAGIC);
  218. request.type = htonl(nbd_cmd(req));
  219. if (nbd_cmd(req) == NBD_CMD_FLUSH) {
  220. /* Other values are reserved for FLUSH requests. */
  221. request.from = 0;
  222. request.len = 0;
  223. } else {
  224. request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
  225. request.len = htonl(size);
  226. }
  227. memcpy(request.handle, &req, sizeof(req));
  228. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%uB)\n",
  229. nbd->disk->disk_name, req,
  230. nbdcmd_to_ascii(nbd_cmd(req)),
  231. (unsigned long long)blk_rq_pos(req) << 9,
  232. blk_rq_bytes(req));
  233. result = sock_xmit(nbd, 1, &request, sizeof(request),
  234. (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
  235. if (result <= 0) {
  236. dev_err(disk_to_dev(nbd->disk),
  237. "Send control failed (result %d)\n", result);
  238. goto error_out;
  239. }
  240. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  241. struct req_iterator iter;
  242. struct bio_vec *bvec;
  243. /*
  244. * we are really probing at internals to determine
  245. * whether to set MSG_MORE or not...
  246. */
  247. rq_for_each_segment(bvec, req, iter) {
  248. flags = 0;
  249. if (!rq_iter_last(req, iter))
  250. flags = MSG_MORE;
  251. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  252. nbd->disk->disk_name, req, bvec->bv_len);
  253. result = sock_send_bvec(nbd, bvec, flags);
  254. if (result <= 0) {
  255. dev_err(disk_to_dev(nbd->disk),
  256. "Send data failed (result %d)\n",
  257. result);
  258. goto error_out;
  259. }
  260. }
  261. }
  262. return 0;
  263. error_out:
  264. return -EIO;
  265. }
  266. static struct request *nbd_find_request(struct nbd_device *nbd,
  267. struct request *xreq)
  268. {
  269. struct request *req, *tmp;
  270. int err;
  271. err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
  272. if (unlikely(err))
  273. goto out;
  274. spin_lock(&nbd->queue_lock);
  275. list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
  276. if (req != xreq)
  277. continue;
  278. list_del_init(&req->queuelist);
  279. spin_unlock(&nbd->queue_lock);
  280. return req;
  281. }
  282. spin_unlock(&nbd->queue_lock);
  283. err = -ENOENT;
  284. out:
  285. return ERR_PTR(err);
  286. }
  287. static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
  288. {
  289. int result;
  290. void *kaddr = kmap(bvec->bv_page);
  291. result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  292. MSG_WAITALL);
  293. kunmap(bvec->bv_page);
  294. return result;
  295. }
  296. /* NULL returned = something went wrong, inform userspace */
  297. static struct request *nbd_read_stat(struct nbd_device *nbd)
  298. {
  299. int result;
  300. struct nbd_reply reply;
  301. struct request *req;
  302. reply.magic = 0;
  303. result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
  304. if (result <= 0) {
  305. dev_err(disk_to_dev(nbd->disk),
  306. "Receive control failed (result %d)\n", result);
  307. goto harderror;
  308. }
  309. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  310. dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
  311. (unsigned long)ntohl(reply.magic));
  312. result = -EPROTO;
  313. goto harderror;
  314. }
  315. req = nbd_find_request(nbd, *(struct request **)reply.handle);
  316. if (IS_ERR(req)) {
  317. result = PTR_ERR(req);
  318. if (result != -ENOENT)
  319. goto harderror;
  320. dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
  321. reply.handle);
  322. result = -EBADR;
  323. goto harderror;
  324. }
  325. if (ntohl(reply.error)) {
  326. dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
  327. ntohl(reply.error));
  328. req->errors++;
  329. return req;
  330. }
  331. dprintk(DBG_RX, "%s: request %p: got reply\n",
  332. nbd->disk->disk_name, req);
  333. if (nbd_cmd(req) == NBD_CMD_READ) {
  334. struct req_iterator iter;
  335. struct bio_vec *bvec;
  336. rq_for_each_segment(bvec, req, iter) {
  337. result = sock_recv_bvec(nbd, bvec);
  338. if (result <= 0) {
  339. dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
  340. result);
  341. req->errors++;
  342. return req;
  343. }
  344. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  345. nbd->disk->disk_name, req, bvec->bv_len);
  346. }
  347. }
  348. return req;
  349. harderror:
  350. nbd->harderror = result;
  351. return NULL;
  352. }
  353. static ssize_t pid_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. struct gendisk *disk = dev_to_disk(dev);
  357. return sprintf(buf, "%ld\n",
  358. (long) ((struct nbd_device *)disk->private_data)->pid);
  359. }
  360. static struct device_attribute pid_attr = {
  361. .attr = { .name = "pid", .mode = S_IRUGO},
  362. .show = pid_show,
  363. };
  364. static int nbd_do_it(struct nbd_device *nbd)
  365. {
  366. struct request *req;
  367. int ret;
  368. BUG_ON(nbd->magic != NBD_MAGIC);
  369. sk_set_memalloc(nbd->sock->sk);
  370. nbd->pid = task_pid_nr(current);
  371. ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
  372. if (ret) {
  373. dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
  374. nbd->pid = 0;
  375. return ret;
  376. }
  377. while ((req = nbd_read_stat(nbd)) != NULL)
  378. nbd_end_request(req);
  379. device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
  380. nbd->pid = 0;
  381. return 0;
  382. }
  383. static void nbd_clear_que(struct nbd_device *nbd)
  384. {
  385. struct request *req;
  386. BUG_ON(nbd->magic != NBD_MAGIC);
  387. /*
  388. * Because we have set nbd->sock to NULL under the tx_lock, all
  389. * modifications to the list must have completed by now. For
  390. * the same reason, the active_req must be NULL.
  391. *
  392. * As a consequence, we don't need to take the spin lock while
  393. * purging the list here.
  394. */
  395. BUG_ON(nbd->sock);
  396. BUG_ON(nbd->active_req);
  397. while (!list_empty(&nbd->queue_head)) {
  398. req = list_entry(nbd->queue_head.next, struct request,
  399. queuelist);
  400. list_del_init(&req->queuelist);
  401. req->errors++;
  402. nbd_end_request(req);
  403. }
  404. while (!list_empty(&nbd->waiting_queue)) {
  405. req = list_entry(nbd->waiting_queue.next, struct request,
  406. queuelist);
  407. list_del_init(&req->queuelist);
  408. req->errors++;
  409. nbd_end_request(req);
  410. }
  411. }
  412. static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
  413. {
  414. if (req->cmd_type != REQ_TYPE_FS)
  415. goto error_out;
  416. nbd_cmd(req) = NBD_CMD_READ;
  417. if (rq_data_dir(req) == WRITE) {
  418. if ((req->cmd_flags & REQ_DISCARD)) {
  419. WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
  420. nbd_cmd(req) = NBD_CMD_TRIM;
  421. } else
  422. nbd_cmd(req) = NBD_CMD_WRITE;
  423. if (nbd->flags & NBD_FLAG_READ_ONLY) {
  424. dev_err(disk_to_dev(nbd->disk),
  425. "Write on read-only\n");
  426. goto error_out;
  427. }
  428. }
  429. if (req->cmd_flags & REQ_FLUSH) {
  430. BUG_ON(unlikely(blk_rq_sectors(req)));
  431. nbd_cmd(req) = NBD_CMD_FLUSH;
  432. }
  433. req->errors = 0;
  434. mutex_lock(&nbd->tx_lock);
  435. if (unlikely(!nbd->sock)) {
  436. mutex_unlock(&nbd->tx_lock);
  437. dev_err(disk_to_dev(nbd->disk),
  438. "Attempted send on closed socket\n");
  439. goto error_out;
  440. }
  441. nbd->active_req = req;
  442. if (nbd_send_req(nbd, req) != 0) {
  443. dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
  444. req->errors++;
  445. nbd_end_request(req);
  446. } else {
  447. spin_lock(&nbd->queue_lock);
  448. list_add_tail(&req->queuelist, &nbd->queue_head);
  449. spin_unlock(&nbd->queue_lock);
  450. }
  451. nbd->active_req = NULL;
  452. mutex_unlock(&nbd->tx_lock);
  453. wake_up_all(&nbd->active_wq);
  454. return;
  455. error_out:
  456. req->errors++;
  457. nbd_end_request(req);
  458. }
  459. static int nbd_thread(void *data)
  460. {
  461. struct nbd_device *nbd = data;
  462. struct request *req;
  463. set_user_nice(current, -20);
  464. while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
  465. /* wait for something to do */
  466. wait_event_interruptible(nbd->waiting_wq,
  467. kthread_should_stop() ||
  468. !list_empty(&nbd->waiting_queue));
  469. /* extract request */
  470. if (list_empty(&nbd->waiting_queue))
  471. continue;
  472. spin_lock_irq(&nbd->queue_lock);
  473. req = list_entry(nbd->waiting_queue.next, struct request,
  474. queuelist);
  475. list_del_init(&req->queuelist);
  476. spin_unlock_irq(&nbd->queue_lock);
  477. /* handle request */
  478. nbd_handle_req(nbd, req);
  479. }
  480. return 0;
  481. }
  482. /*
  483. * We always wait for result of write, for now. It would be nice to make it optional
  484. * in future
  485. * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
  486. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  487. */
  488. static void do_nbd_request(struct request_queue *q)
  489. __releases(q->queue_lock) __acquires(q->queue_lock)
  490. {
  491. struct request *req;
  492. while ((req = blk_fetch_request(q)) != NULL) {
  493. struct nbd_device *nbd;
  494. spin_unlock_irq(q->queue_lock);
  495. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n",
  496. req->rq_disk->disk_name, req, req->cmd_type);
  497. nbd = req->rq_disk->private_data;
  498. BUG_ON(nbd->magic != NBD_MAGIC);
  499. if (unlikely(!nbd->sock)) {
  500. dev_err(disk_to_dev(nbd->disk),
  501. "Attempted send on closed socket\n");
  502. req->errors++;
  503. nbd_end_request(req);
  504. spin_lock_irq(q->queue_lock);
  505. continue;
  506. }
  507. spin_lock_irq(&nbd->queue_lock);
  508. list_add_tail(&req->queuelist, &nbd->waiting_queue);
  509. spin_unlock_irq(&nbd->queue_lock);
  510. wake_up(&nbd->waiting_wq);
  511. spin_lock_irq(q->queue_lock);
  512. }
  513. }
  514. /* Must be called with tx_lock held */
  515. static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
  516. unsigned int cmd, unsigned long arg)
  517. {
  518. switch (cmd) {
  519. case NBD_DISCONNECT: {
  520. struct request sreq;
  521. dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
  522. if (!nbd->sock)
  523. return -EINVAL;
  524. mutex_unlock(&nbd->tx_lock);
  525. fsync_bdev(bdev);
  526. mutex_lock(&nbd->tx_lock);
  527. blk_rq_init(NULL, &sreq);
  528. sreq.cmd_type = REQ_TYPE_SPECIAL;
  529. nbd_cmd(&sreq) = NBD_CMD_DISC;
  530. /* Check again after getting mutex back. */
  531. if (!nbd->sock)
  532. return -EINVAL;
  533. nbd_send_req(nbd, &sreq);
  534. return 0;
  535. }
  536. case NBD_CLEAR_SOCK: {
  537. struct file *file;
  538. nbd->sock = NULL;
  539. file = nbd->file;
  540. nbd->file = NULL;
  541. nbd_clear_que(nbd);
  542. BUG_ON(!list_empty(&nbd->queue_head));
  543. BUG_ON(!list_empty(&nbd->waiting_queue));
  544. kill_bdev(bdev);
  545. if (file)
  546. fput(file);
  547. return 0;
  548. }
  549. case NBD_SET_SOCK: {
  550. struct file *file;
  551. if (nbd->file)
  552. return -EBUSY;
  553. file = fget(arg);
  554. if (file) {
  555. struct inode *inode = file_inode(file);
  556. if (S_ISSOCK(inode->i_mode)) {
  557. nbd->file = file;
  558. nbd->sock = SOCKET_I(inode);
  559. if (max_part > 0)
  560. bdev->bd_invalidated = 1;
  561. return 0;
  562. } else {
  563. fput(file);
  564. }
  565. }
  566. return -EINVAL;
  567. }
  568. case NBD_SET_BLKSIZE:
  569. nbd->blksize = arg;
  570. nbd->bytesize &= ~(nbd->blksize-1);
  571. bdev->bd_inode->i_size = nbd->bytesize;
  572. set_blocksize(bdev, nbd->blksize);
  573. set_capacity(nbd->disk, nbd->bytesize >> 9);
  574. return 0;
  575. case NBD_SET_SIZE:
  576. nbd->bytesize = arg & ~(nbd->blksize-1);
  577. bdev->bd_inode->i_size = nbd->bytesize;
  578. set_blocksize(bdev, nbd->blksize);
  579. set_capacity(nbd->disk, nbd->bytesize >> 9);
  580. return 0;
  581. case NBD_SET_TIMEOUT:
  582. nbd->xmit_timeout = arg * HZ;
  583. return 0;
  584. case NBD_SET_FLAGS:
  585. nbd->flags = arg;
  586. return 0;
  587. case NBD_SET_SIZE_BLOCKS:
  588. nbd->bytesize = ((u64) arg) * nbd->blksize;
  589. bdev->bd_inode->i_size = nbd->bytesize;
  590. set_blocksize(bdev, nbd->blksize);
  591. set_capacity(nbd->disk, nbd->bytesize >> 9);
  592. return 0;
  593. case NBD_DO_IT: {
  594. struct task_struct *thread;
  595. struct file *file;
  596. int error;
  597. if (nbd->pid)
  598. return -EBUSY;
  599. if (!nbd->file)
  600. return -EINVAL;
  601. mutex_unlock(&nbd->tx_lock);
  602. if (nbd->flags & NBD_FLAG_READ_ONLY)
  603. set_device_ro(bdev, true);
  604. if (nbd->flags & NBD_FLAG_SEND_TRIM)
  605. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  606. nbd->disk->queue);
  607. if (nbd->flags & NBD_FLAG_SEND_FLUSH)
  608. blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
  609. else
  610. blk_queue_flush(nbd->disk->queue, 0);
  611. thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name);
  612. if (IS_ERR(thread)) {
  613. mutex_lock(&nbd->tx_lock);
  614. return PTR_ERR(thread);
  615. }
  616. wake_up_process(thread);
  617. error = nbd_do_it(nbd);
  618. kthread_stop(thread);
  619. mutex_lock(&nbd->tx_lock);
  620. if (error)
  621. return error;
  622. sock_shutdown(nbd, 0);
  623. file = nbd->file;
  624. nbd->file = NULL;
  625. nbd_clear_que(nbd);
  626. dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
  627. kill_bdev(bdev);
  628. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
  629. set_device_ro(bdev, false);
  630. if (file)
  631. fput(file);
  632. nbd->flags = 0;
  633. nbd->bytesize = 0;
  634. bdev->bd_inode->i_size = 0;
  635. set_capacity(nbd->disk, 0);
  636. if (max_part > 0)
  637. ioctl_by_bdev(bdev, BLKRRPART, 0);
  638. return nbd->harderror;
  639. }
  640. case NBD_CLEAR_QUE:
  641. /*
  642. * This is for compatibility only. The queue is always cleared
  643. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  644. */
  645. BUG_ON(!nbd->sock && !list_empty(&nbd->queue_head));
  646. return 0;
  647. case NBD_PRINT_DEBUG:
  648. dev_info(disk_to_dev(nbd->disk),
  649. "next = %p, prev = %p, head = %p\n",
  650. nbd->queue_head.next, nbd->queue_head.prev,
  651. &nbd->queue_head);
  652. return 0;
  653. }
  654. return -ENOTTY;
  655. }
  656. static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
  657. unsigned int cmd, unsigned long arg)
  658. {
  659. struct nbd_device *nbd = bdev->bd_disk->private_data;
  660. int error;
  661. if (!capable(CAP_SYS_ADMIN))
  662. return -EPERM;
  663. BUG_ON(nbd->magic != NBD_MAGIC);
  664. /* Anyone capable of this syscall can do *real bad* things */
  665. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  666. nbd->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  667. mutex_lock(&nbd->tx_lock);
  668. error = __nbd_ioctl(bdev, nbd, cmd, arg);
  669. mutex_unlock(&nbd->tx_lock);
  670. return error;
  671. }
  672. static const struct block_device_operations nbd_fops =
  673. {
  674. .owner = THIS_MODULE,
  675. .ioctl = nbd_ioctl,
  676. };
  677. /*
  678. * And here should be modules and kernel interface
  679. * (Just smiley confuses emacs :-)
  680. */
  681. static int __init nbd_init(void)
  682. {
  683. int err = -ENOMEM;
  684. int i;
  685. int part_shift;
  686. BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
  687. if (max_part < 0) {
  688. printk(KERN_ERR "nbd: max_part must be >= 0\n");
  689. return -EINVAL;
  690. }
  691. nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
  692. if (!nbd_dev)
  693. return -ENOMEM;
  694. part_shift = 0;
  695. if (max_part > 0) {
  696. part_shift = fls(max_part);
  697. /*
  698. * Adjust max_part according to part_shift as it is exported
  699. * to user space so that user can know the max number of
  700. * partition kernel should be able to manage.
  701. *
  702. * Note that -1 is required because partition 0 is reserved
  703. * for the whole disk.
  704. */
  705. max_part = (1UL << part_shift) - 1;
  706. }
  707. if ((1UL << part_shift) > DISK_MAX_PARTS)
  708. return -EINVAL;
  709. if (nbds_max > 1UL << (MINORBITS - part_shift))
  710. return -EINVAL;
  711. for (i = 0; i < nbds_max; i++) {
  712. struct gendisk *disk = alloc_disk(1 << part_shift);
  713. if (!disk)
  714. goto out;
  715. nbd_dev[i].disk = disk;
  716. /*
  717. * The new linux 2.5 block layer implementation requires
  718. * every gendisk to have its very own request_queue struct.
  719. * These structs are big so we dynamically allocate them.
  720. */
  721. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  722. if (!disk->queue) {
  723. put_disk(disk);
  724. goto out;
  725. }
  726. /*
  727. * Tell the block layer that we are not a rotational device
  728. */
  729. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
  730. disk->queue->limits.discard_granularity = 512;
  731. disk->queue->limits.max_discard_sectors = UINT_MAX;
  732. disk->queue->limits.discard_zeroes_data = 0;
  733. }
  734. if (register_blkdev(NBD_MAJOR, "nbd")) {
  735. err = -EIO;
  736. goto out;
  737. }
  738. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  739. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  740. for (i = 0; i < nbds_max; i++) {
  741. struct gendisk *disk = nbd_dev[i].disk;
  742. nbd_dev[i].file = NULL;
  743. nbd_dev[i].magic = NBD_MAGIC;
  744. nbd_dev[i].flags = 0;
  745. INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
  746. spin_lock_init(&nbd_dev[i].queue_lock);
  747. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  748. mutex_init(&nbd_dev[i].tx_lock);
  749. init_waitqueue_head(&nbd_dev[i].active_wq);
  750. init_waitqueue_head(&nbd_dev[i].waiting_wq);
  751. nbd_dev[i].blksize = 1024;
  752. nbd_dev[i].bytesize = 0;
  753. disk->major = NBD_MAJOR;
  754. disk->first_minor = i << part_shift;
  755. disk->fops = &nbd_fops;
  756. disk->private_data = &nbd_dev[i];
  757. sprintf(disk->disk_name, "nbd%d", i);
  758. set_capacity(disk, 0);
  759. add_disk(disk);
  760. }
  761. return 0;
  762. out:
  763. while (i--) {
  764. blk_cleanup_queue(nbd_dev[i].disk->queue);
  765. put_disk(nbd_dev[i].disk);
  766. }
  767. kfree(nbd_dev);
  768. return err;
  769. }
  770. static void __exit nbd_cleanup(void)
  771. {
  772. int i;
  773. for (i = 0; i < nbds_max; i++) {
  774. struct gendisk *disk = nbd_dev[i].disk;
  775. nbd_dev[i].magic = 0;
  776. if (disk) {
  777. del_gendisk(disk);
  778. blk_cleanup_queue(disk->queue);
  779. put_disk(disk);
  780. }
  781. }
  782. unregister_blkdev(NBD_MAJOR, "nbd");
  783. kfree(nbd_dev);
  784. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  785. }
  786. module_init(nbd_init);
  787. module_exit(nbd_cleanup);
  788. MODULE_DESCRIPTION("Network Block Device");
  789. MODULE_LICENSE("GPL");
  790. module_param(nbds_max, int, 0444);
  791. MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
  792. module_param(max_part, int, 0444);
  793. MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");
  794. #ifndef NDEBUG
  795. module_param(debugflags, int, 0644);
  796. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  797. #endif