nbd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 Pavel Machek <pavel@ucw.cz>
  8. * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
  9. *
  10. * (part of code stolen from loop.c)
  11. *
  12. * 97-3-25 compiled 0-th version, not yet tested it
  13. * (it did not work, BTW) (later that day) HEY! it works!
  14. * (bit later) hmm, not that much... 2:00am next day:
  15. * yes, it works, but it gives something like 50kB/sec
  16. * 97-4-01 complete rewrite to make it possible for many requests at
  17. * once to be processed
  18. * 97-4-11 Making protocol independent of endianity etc.
  19. * 97-9-13 Cosmetic changes
  20. * 98-5-13 Attempt to make 64-bit-clean on 64-bit machines
  21. * 99-1-11 Attempt to make 64-bit-clean on 32-bit machines <ankry@mif.pg.gda.pl>
  22. * 01-2-27 Fix to store proper blockcount for kernel (calculated using
  23. * BLOCK_SIZE_BITS, not device blocksize) <aga@permonline.ru>
  24. * 01-3-11 Make nbd work with new Linux block layer code. It now supports
  25. * plugging like all the other block devices. Also added in MSG_MORE to
  26. * reduce number of partial TCP segments sent. <steve@chygwyn.com>
  27. * 01-12-6 Fix deadlock condition by making queue locks independent of
  28. * the transmit lock. <steve@chygwyn.com>
  29. * 02-10-11 Allow hung xmit to be aborted via SIGKILL & various fixes.
  30. * <Paul.Clements@SteelEye.com> <James.Bottomley@SteelEye.com>
  31. * 03-06-22 Make nbd work with new linux 2.5 block layer design. This fixes
  32. * memory corruption from module removal and possible memory corruption
  33. * from sending/receiving disk data. <ldl@aros.net>
  34. * 03-06-23 Cosmetic changes. <ldl@aros.net>
  35. * 03-06-23 Enhance diagnostics support. <ldl@aros.net>
  36. * 03-06-24 Remove unneeded blksize_bits field from nbd_device struct.
  37. * <ldl@aros.net>
  38. * 03-06-24 Cleanup PARANOIA usage & code. <ldl@aros.net>
  39. * 04-02-19 Remove PARANOIA, plus various cleanups (Paul Clements)
  40. * possible FIXME: make set_sock / set_blksize / set_size / do_it one syscall
  41. * why not: would need access_ok and friends, would share yet another
  42. * structure with userland
  43. */
  44. #include <linux/major.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/module.h>
  47. #include <linux/init.h>
  48. #include <linux/sched.h>
  49. #include <linux/fs.h>
  50. #include <linux/bio.h>
  51. #include <linux/stat.h>
  52. #include <linux/errno.h>
  53. #include <linux/file.h>
  54. #include <linux/ioctl.h>
  55. #include <linux/compiler.h>
  56. #include <linux/err.h>
  57. #include <linux/kernel.h>
  58. #include <net/sock.h>
  59. #include <linux/devfs_fs_kernel.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/system.h>
  62. #include <asm/types.h>
  63. #include <linux/nbd.h>
  64. #define LO_MAGIC 0x68797548
  65. #ifdef NDEBUG
  66. #define dprintk(flags, fmt...)
  67. #else /* NDEBUG */
  68. #define dprintk(flags, fmt...) do { \
  69. if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
  70. } while (0)
  71. #define DBG_IOCTL 0x0004
  72. #define DBG_INIT 0x0010
  73. #define DBG_EXIT 0x0020
  74. #define DBG_BLKDEV 0x0100
  75. #define DBG_RX 0x0200
  76. #define DBG_TX 0x0400
  77. static unsigned int debugflags;
  78. static unsigned int nbds_max = 16;
  79. #endif /* NDEBUG */
  80. static struct nbd_device nbd_dev[MAX_NBD];
  81. /*
  82. * Use just one lock (or at most 1 per NIC). Two arguments for this:
  83. * 1. Each NIC is essentially a synchronization point for all servers
  84. * accessed through that NIC so there's no need to have more locks
  85. * than NICs anyway.
  86. * 2. More locks lead to more "Dirty cache line bouncing" which will slow
  87. * down each lock to the point where they're actually slower than just
  88. * a single lock.
  89. * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
  90. */
  91. static DEFINE_SPINLOCK(nbd_lock);
  92. #ifndef NDEBUG
  93. static const char *ioctl_cmd_to_ascii(int cmd)
  94. {
  95. switch (cmd) {
  96. case NBD_SET_SOCK: return "set-sock";
  97. case NBD_SET_BLKSIZE: return "set-blksize";
  98. case NBD_SET_SIZE: return "set-size";
  99. case NBD_DO_IT: return "do-it";
  100. case NBD_CLEAR_SOCK: return "clear-sock";
  101. case NBD_CLEAR_QUE: return "clear-que";
  102. case NBD_PRINT_DEBUG: return "print-debug";
  103. case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
  104. case NBD_DISCONNECT: return "disconnect";
  105. case BLKROSET: return "set-read-only";
  106. case BLKFLSBUF: return "flush-buffer-cache";
  107. }
  108. return "unknown";
  109. }
  110. static const char *nbdcmd_to_ascii(int cmd)
  111. {
  112. switch (cmd) {
  113. case NBD_CMD_READ: return "read";
  114. case NBD_CMD_WRITE: return "write";
  115. case NBD_CMD_DISC: return "disconnect";
  116. }
  117. return "invalid";
  118. }
  119. #endif /* NDEBUG */
  120. static void nbd_end_request(struct request *req)
  121. {
  122. int uptodate = (req->errors == 0) ? 1 : 0;
  123. request_queue_t *q = req->q;
  124. unsigned long flags;
  125. dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
  126. req, uptodate? "done": "failed");
  127. spin_lock_irqsave(q->queue_lock, flags);
  128. if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
  129. end_that_request_last(req, uptodate);
  130. }
  131. spin_unlock_irqrestore(q->queue_lock, flags);
  132. }
  133. /*
  134. * Send or receive packet.
  135. */
  136. static int sock_xmit(struct socket *sock, int send, void *buf, int size,
  137. int msg_flags)
  138. {
  139. int result;
  140. struct msghdr msg;
  141. struct kvec iov;
  142. unsigned long flags;
  143. sigset_t oldset;
  144. /* Allow interception of SIGKILL only
  145. * Don't allow other signals to interrupt the transmission */
  146. spin_lock_irqsave(&current->sighand->siglock, flags);
  147. oldset = current->blocked;
  148. sigfillset(&current->blocked);
  149. sigdelsetmask(&current->blocked, sigmask(SIGKILL));
  150. recalc_sigpending();
  151. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  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. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  163. else
  164. result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  165. if (signal_pending(current)) {
  166. siginfo_t info;
  167. spin_lock_irqsave(&current->sighand->siglock, flags);
  168. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  169. current->pid, current->comm,
  170. dequeue_signal(current, &current->blocked, &info));
  171. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  172. result = -EINTR;
  173. break;
  174. }
  175. if (result <= 0) {
  176. if (result == 0)
  177. result = -EPIPE; /* short read */
  178. break;
  179. }
  180. size -= result;
  181. buf += result;
  182. } while (size > 0);
  183. spin_lock_irqsave(&current->sighand->siglock, flags);
  184. current->blocked = oldset;
  185. recalc_sigpending();
  186. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  187. return result;
  188. }
  189. static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  190. int flags)
  191. {
  192. int result;
  193. void *kaddr = kmap(bvec->bv_page);
  194. result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  195. flags);
  196. kunmap(bvec->bv_page);
  197. return result;
  198. }
  199. static int nbd_send_req(struct nbd_device *lo, struct request *req)
  200. {
  201. int result, i, flags;
  202. struct nbd_request request;
  203. unsigned long size = req->nr_sectors << 9;
  204. struct socket *sock = lo->sock;
  205. request.magic = htonl(NBD_REQUEST_MAGIC);
  206. request.type = htonl(nbd_cmd(req));
  207. request.from = cpu_to_be64((u64) req->sector << 9);
  208. request.len = htonl(size);
  209. memcpy(request.handle, &req, sizeof(req));
  210. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
  211. lo->disk->disk_name, req,
  212. nbdcmd_to_ascii(nbd_cmd(req)),
  213. (unsigned long long)req->sector << 9,
  214. req->nr_sectors << 9);
  215. result = sock_xmit(sock, 1, &request, sizeof(request),
  216. (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
  217. if (result <= 0) {
  218. printk(KERN_ERR "%s: Send control failed (result %d)\n",
  219. lo->disk->disk_name, result);
  220. goto error_out;
  221. }
  222. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  223. struct bio *bio;
  224. /*
  225. * we are really probing at internals to determine
  226. * whether to set MSG_MORE or not...
  227. */
  228. rq_for_each_bio(bio, req) {
  229. struct bio_vec *bvec;
  230. bio_for_each_segment(bvec, bio, i) {
  231. flags = 0;
  232. if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
  233. flags = MSG_MORE;
  234. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  235. lo->disk->disk_name, req,
  236. bvec->bv_len);
  237. result = sock_send_bvec(sock, bvec, flags);
  238. if (result <= 0) {
  239. printk(KERN_ERR "%s: Send data failed (result %d)\n",
  240. lo->disk->disk_name,
  241. result);
  242. goto error_out;
  243. }
  244. }
  245. }
  246. }
  247. return 0;
  248. error_out:
  249. return 1;
  250. }
  251. static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  252. {
  253. struct request *req;
  254. struct list_head *tmp;
  255. struct request *xreq;
  256. int err;
  257. memcpy(&xreq, handle, sizeof(xreq));
  258. err = wait_event_interruptible(lo->active_wq, lo->active_req != xreq);
  259. if (unlikely(err))
  260. goto out;
  261. spin_lock(&lo->queue_lock);
  262. list_for_each(tmp, &lo->queue_head) {
  263. req = list_entry(tmp, struct request, 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 socket *sock, struct bio_vec *bvec)
  276. {
  277. int result;
  278. void *kaddr = kmap(bvec->bv_page);
  279. result = sock_xmit(sock, 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. struct socket *sock = lo->sock;
  291. reply.magic = 0;
  292. result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
  293. if (result <= 0) {
  294. printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  295. lo->disk->disk_name, result);
  296. goto harderror;
  297. }
  298. req = nbd_find_request(lo, reply.handle);
  299. if (unlikely(IS_ERR(req))) {
  300. result = PTR_ERR(req);
  301. if (result != -ENOENT)
  302. goto harderror;
  303. printk(KERN_ERR "%s: Unexpected reply (%p)\n",
  304. lo->disk->disk_name, reply.handle);
  305. result = -EBADR;
  306. goto harderror;
  307. }
  308. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  309. printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  310. lo->disk->disk_name,
  311. (unsigned long)ntohl(reply.magic));
  312. result = -EPROTO;
  313. goto harderror;
  314. }
  315. if (ntohl(reply.error)) {
  316. printk(KERN_ERR "%s: Other side returned error (%d)\n",
  317. lo->disk->disk_name, ntohl(reply.error));
  318. req->errors++;
  319. return req;
  320. }
  321. dprintk(DBG_RX, "%s: request %p: got reply\n",
  322. lo->disk->disk_name, req);
  323. if (nbd_cmd(req) == NBD_CMD_READ) {
  324. int i;
  325. struct bio *bio;
  326. rq_for_each_bio(bio, req) {
  327. struct bio_vec *bvec;
  328. bio_for_each_segment(bvec, bio, i) {
  329. result = sock_recv_bvec(sock, bvec);
  330. if (result <= 0) {
  331. printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  332. lo->disk->disk_name,
  333. result);
  334. goto harderror;
  335. }
  336. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  337. lo->disk->disk_name, req, bvec->bv_len);
  338. }
  339. }
  340. }
  341. return req;
  342. harderror:
  343. lo->harderror = result;
  344. return NULL;
  345. }
  346. static void nbd_do_it(struct nbd_device *lo)
  347. {
  348. struct request *req;
  349. BUG_ON(lo->magic != LO_MAGIC);
  350. while ((req = nbd_read_stat(lo)) != NULL)
  351. nbd_end_request(req);
  352. return;
  353. }
  354. static void nbd_clear_que(struct nbd_device *lo)
  355. {
  356. struct request *req;
  357. BUG_ON(lo->magic != LO_MAGIC);
  358. /*
  359. * Because we have set lo->sock to NULL under the tx_lock, all
  360. * modifications to the list must have completed by now. For
  361. * the same reason, the active_req must be NULL.
  362. *
  363. * As a consequence, we don't need to take the spin lock while
  364. * purging the list here.
  365. */
  366. BUG_ON(lo->sock);
  367. BUG_ON(lo->active_req);
  368. while (!list_empty(&lo->queue_head)) {
  369. req = list_entry(lo->queue_head.next, struct request,
  370. queuelist);
  371. list_del_init(&req->queuelist);
  372. req->errors++;
  373. nbd_end_request(req);
  374. }
  375. }
  376. /*
  377. * We always wait for result of write, for now. It would be nice to make it optional
  378. * in future
  379. * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
  380. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  381. */
  382. static void do_nbd_request(request_queue_t * q)
  383. {
  384. struct request *req;
  385. while ((req = elv_next_request(q)) != NULL) {
  386. struct nbd_device *lo;
  387. blkdev_dequeue_request(req);
  388. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
  389. req->rq_disk->disk_name, req, req->flags);
  390. if (!(req->flags & REQ_CMD))
  391. goto error_out;
  392. lo = req->rq_disk->private_data;
  393. BUG_ON(lo->magic != LO_MAGIC);
  394. nbd_cmd(req) = NBD_CMD_READ;
  395. if (rq_data_dir(req) == WRITE) {
  396. nbd_cmd(req) = NBD_CMD_WRITE;
  397. if (lo->flags & NBD_READ_ONLY) {
  398. printk(KERN_ERR "%s: Write on read-only\n",
  399. lo->disk->disk_name);
  400. goto error_out;
  401. }
  402. }
  403. req->errors = 0;
  404. spin_unlock_irq(q->queue_lock);
  405. mutex_lock(&lo->tx_lock);
  406. if (unlikely(!lo->sock)) {
  407. mutex_unlock(&lo->tx_lock);
  408. printk(KERN_ERR "%s: Attempted send on closed socket\n",
  409. lo->disk->disk_name);
  410. req->errors++;
  411. nbd_end_request(req);
  412. spin_lock_irq(q->queue_lock);
  413. continue;
  414. }
  415. lo->active_req = req;
  416. if (nbd_send_req(lo, req) != 0) {
  417. printk(KERN_ERR "%s: Request send failed\n",
  418. lo->disk->disk_name);
  419. req->errors++;
  420. nbd_end_request(req);
  421. } else {
  422. spin_lock(&lo->queue_lock);
  423. list_add(&req->queuelist, &lo->queue_head);
  424. spin_unlock(&lo->queue_lock);
  425. }
  426. lo->active_req = NULL;
  427. mutex_unlock(&lo->tx_lock);
  428. wake_up_all(&lo->active_wq);
  429. spin_lock_irq(q->queue_lock);
  430. continue;
  431. error_out:
  432. req->errors++;
  433. spin_unlock(q->queue_lock);
  434. nbd_end_request(req);
  435. spin_lock(q->queue_lock);
  436. }
  437. return;
  438. }
  439. static int nbd_ioctl(struct inode *inode, struct file *file,
  440. unsigned int cmd, unsigned long arg)
  441. {
  442. struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
  443. int error;
  444. struct request sreq ;
  445. if (!capable(CAP_SYS_ADMIN))
  446. return -EPERM;
  447. BUG_ON(lo->magic != LO_MAGIC);
  448. /* Anyone capable of this syscall can do *real bad* things */
  449. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  450. lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  451. switch (cmd) {
  452. case NBD_DISCONNECT:
  453. printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
  454. sreq.flags = REQ_SPECIAL;
  455. nbd_cmd(&sreq) = NBD_CMD_DISC;
  456. /*
  457. * Set these to sane values in case server implementation
  458. * fails to check the request type first and also to keep
  459. * debugging output cleaner.
  460. */
  461. sreq.sector = 0;
  462. sreq.nr_sectors = 0;
  463. if (!lo->sock)
  464. return -EINVAL;
  465. nbd_send_req(lo, &sreq);
  466. return 0;
  467. case NBD_CLEAR_SOCK:
  468. error = 0;
  469. mutex_lock(&lo->tx_lock);
  470. lo->sock = NULL;
  471. mutex_unlock(&lo->tx_lock);
  472. file = lo->file;
  473. lo->file = NULL;
  474. nbd_clear_que(lo);
  475. BUG_ON(!list_empty(&lo->queue_head));
  476. if (file)
  477. fput(file);
  478. return error;
  479. case NBD_SET_SOCK:
  480. if (lo->file)
  481. return -EBUSY;
  482. error = -EINVAL;
  483. file = fget(arg);
  484. if (file) {
  485. inode = file->f_dentry->d_inode;
  486. if (S_ISSOCK(inode->i_mode)) {
  487. lo->file = file;
  488. lo->sock = SOCKET_I(inode);
  489. error = 0;
  490. } else {
  491. fput(file);
  492. }
  493. }
  494. return error;
  495. case NBD_SET_BLKSIZE:
  496. lo->blksize = arg;
  497. lo->bytesize &= ~(lo->blksize-1);
  498. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  499. set_blocksize(inode->i_bdev, lo->blksize);
  500. set_capacity(lo->disk, lo->bytesize >> 9);
  501. return 0;
  502. case NBD_SET_SIZE:
  503. lo->bytesize = arg & ~(lo->blksize-1);
  504. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  505. set_blocksize(inode->i_bdev, lo->blksize);
  506. set_capacity(lo->disk, lo->bytesize >> 9);
  507. return 0;
  508. case NBD_SET_SIZE_BLOCKS:
  509. lo->bytesize = ((u64) arg) * lo->blksize;
  510. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  511. set_blocksize(inode->i_bdev, lo->blksize);
  512. set_capacity(lo->disk, lo->bytesize >> 9);
  513. return 0;
  514. case NBD_DO_IT:
  515. if (!lo->file)
  516. return -EINVAL;
  517. nbd_do_it(lo);
  518. /* on return tidy up in case we have a signal */
  519. /* Forcibly shutdown the socket causing all listeners
  520. * to error
  521. *
  522. * FIXME: This code is duplicated from sys_shutdown, but
  523. * there should be a more generic interface rather than
  524. * calling socket ops directly here */
  525. mutex_lock(&lo->tx_lock);
  526. if (lo->sock) {
  527. printk(KERN_WARNING "%s: shutting down socket\n",
  528. lo->disk->disk_name);
  529. lo->sock->ops->shutdown(lo->sock,
  530. SEND_SHUTDOWN|RCV_SHUTDOWN);
  531. lo->sock = NULL;
  532. }
  533. mutex_unlock(&lo->tx_lock);
  534. file = lo->file;
  535. lo->file = NULL;
  536. nbd_clear_que(lo);
  537. printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
  538. if (file)
  539. fput(file);
  540. return lo->harderror;
  541. case NBD_CLEAR_QUE:
  542. /*
  543. * This is for compatibility only. The queue is always cleared
  544. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  545. */
  546. BUG_ON(!lo->sock && !list_empty(&lo->queue_head));
  547. return 0;
  548. case NBD_PRINT_DEBUG:
  549. printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
  550. inode->i_bdev->bd_disk->disk_name,
  551. lo->queue_head.next, lo->queue_head.prev,
  552. &lo->queue_head);
  553. return 0;
  554. }
  555. return -EINVAL;
  556. }
  557. static struct block_device_operations nbd_fops =
  558. {
  559. .owner = THIS_MODULE,
  560. .ioctl = nbd_ioctl,
  561. };
  562. /*
  563. * And here should be modules and kernel interface
  564. * (Just smiley confuses emacs :-)
  565. */
  566. static int __init nbd_init(void)
  567. {
  568. int err = -ENOMEM;
  569. int i;
  570. BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
  571. if (nbds_max > MAX_NBD) {
  572. printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
  573. nbds_max);
  574. return -EINVAL;
  575. }
  576. for (i = 0; i < nbds_max; i++) {
  577. struct gendisk *disk = alloc_disk(1);
  578. if (!disk)
  579. goto out;
  580. nbd_dev[i].disk = disk;
  581. /*
  582. * The new linux 2.5 block layer implementation requires
  583. * every gendisk to have its very own request_queue struct.
  584. * These structs are big so we dynamically allocate them.
  585. */
  586. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  587. if (!disk->queue) {
  588. put_disk(disk);
  589. goto out;
  590. }
  591. }
  592. if (register_blkdev(NBD_MAJOR, "nbd")) {
  593. err = -EIO;
  594. goto out;
  595. }
  596. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  597. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  598. devfs_mk_dir("nbd");
  599. for (i = 0; i < nbds_max; i++) {
  600. struct gendisk *disk = nbd_dev[i].disk;
  601. nbd_dev[i].file = NULL;
  602. nbd_dev[i].magic = LO_MAGIC;
  603. nbd_dev[i].flags = 0;
  604. spin_lock_init(&nbd_dev[i].queue_lock);
  605. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  606. mutex_init(&nbd_dev[i].tx_lock);
  607. init_waitqueue_head(&nbd_dev[i].active_wq);
  608. nbd_dev[i].blksize = 1024;
  609. nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
  610. disk->major = NBD_MAJOR;
  611. disk->first_minor = i;
  612. disk->fops = &nbd_fops;
  613. disk->private_data = &nbd_dev[i];
  614. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  615. sprintf(disk->disk_name, "nbd%d", i);
  616. sprintf(disk->devfs_name, "nbd/%d", i);
  617. set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
  618. add_disk(disk);
  619. }
  620. return 0;
  621. out:
  622. while (i--) {
  623. blk_cleanup_queue(nbd_dev[i].disk->queue);
  624. put_disk(nbd_dev[i].disk);
  625. }
  626. return err;
  627. }
  628. static void __exit nbd_cleanup(void)
  629. {
  630. int i;
  631. for (i = 0; i < nbds_max; i++) {
  632. struct gendisk *disk = nbd_dev[i].disk;
  633. nbd_dev[i].magic = 0;
  634. if (disk) {
  635. del_gendisk(disk);
  636. blk_cleanup_queue(disk->queue);
  637. put_disk(disk);
  638. }
  639. }
  640. devfs_remove("nbd");
  641. unregister_blkdev(NBD_MAJOR, "nbd");
  642. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  643. }
  644. module_init(nbd_init);
  645. module_exit(nbd_cleanup);
  646. MODULE_DESCRIPTION("Network Block Device");
  647. MODULE_LICENSE("GPL");
  648. module_param(nbds_max, int, 0444);
  649. MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
  650. #ifndef NDEBUG
  651. module_param(debugflags, int, 0644);
  652. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  653. #endif