nbd.c 21 KB

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