nbd.c 21 KB

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