nbd.c 22 KB

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