nbd.c 20 KB

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