nbd.c 21 KB

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