nbd.c 20 KB

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