nbd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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_namelen = 0;
  161. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  162. if (send)
  163. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  164. else
  165. result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  166. if (signal_pending(current)) {
  167. siginfo_t info;
  168. spin_lock_irqsave(&current->sighand->siglock, flags);
  169. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  170. current->pid, current->comm,
  171. dequeue_signal(current, &current->blocked, &info));
  172. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  173. result = -EINTR;
  174. break;
  175. }
  176. if (result <= 0) {
  177. if (result == 0)
  178. result = -EPIPE; /* short read */
  179. break;
  180. }
  181. size -= result;
  182. buf += result;
  183. } while (size > 0);
  184. spin_lock_irqsave(&current->sighand->siglock, flags);
  185. current->blocked = oldset;
  186. recalc_sigpending();
  187. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  188. return result;
  189. }
  190. static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  191. int flags)
  192. {
  193. int result;
  194. void *kaddr = kmap(bvec->bv_page);
  195. result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  196. flags);
  197. kunmap(bvec->bv_page);
  198. return result;
  199. }
  200. static int nbd_send_req(struct nbd_device *lo, struct request *req)
  201. {
  202. int result, i, flags;
  203. struct nbd_request request;
  204. unsigned long size = req->nr_sectors << 9;
  205. struct socket *sock = lo->sock;
  206. request.magic = htonl(NBD_REQUEST_MAGIC);
  207. request.type = htonl(nbd_cmd(req));
  208. request.from = cpu_to_be64((u64) req->sector << 9);
  209. request.len = htonl(size);
  210. memcpy(request.handle, &req, sizeof(req));
  211. dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%luB)\n",
  212. lo->disk->disk_name, req,
  213. nbdcmd_to_ascii(nbd_cmd(req)),
  214. (unsigned long long)req->sector << 9,
  215. req->nr_sectors << 9);
  216. result = sock_xmit(sock, 1, &request, sizeof(request),
  217. (nbd_cmd(req) == NBD_CMD_WRITE)? MSG_MORE: 0);
  218. if (result <= 0) {
  219. printk(KERN_ERR "%s: Send control failed (result %d)\n",
  220. lo->disk->disk_name, result);
  221. goto error_out;
  222. }
  223. if (nbd_cmd(req) == NBD_CMD_WRITE) {
  224. struct bio *bio;
  225. /*
  226. * we are really probing at internals to determine
  227. * whether to set MSG_MORE or not...
  228. */
  229. rq_for_each_bio(bio, req) {
  230. struct bio_vec *bvec;
  231. bio_for_each_segment(bvec, bio, i) {
  232. flags = 0;
  233. if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
  234. flags = MSG_MORE;
  235. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  236. lo->disk->disk_name, req,
  237. bvec->bv_len);
  238. result = sock_send_bvec(sock, bvec, flags);
  239. if (result <= 0) {
  240. printk(KERN_ERR "%s: Send data failed (result %d)\n",
  241. lo->disk->disk_name,
  242. result);
  243. goto error_out;
  244. }
  245. }
  246. }
  247. }
  248. return 0;
  249. error_out:
  250. return 1;
  251. }
  252. static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  253. {
  254. struct request *req;
  255. struct list_head *tmp;
  256. struct request *xreq;
  257. int err;
  258. memcpy(&xreq, handle, sizeof(xreq));
  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(tmp, &lo->queue_head) {
  264. req = list_entry(tmp, struct request, queuelist);
  265. if (req != xreq)
  266. continue;
  267. list_del_init(&req->queuelist);
  268. spin_unlock(&lo->queue_lock);
  269. return req;
  270. }
  271. spin_unlock(&lo->queue_lock);
  272. err = -ENOENT;
  273. out:
  274. return ERR_PTR(err);
  275. }
  276. static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
  277. {
  278. int result;
  279. void *kaddr = kmap(bvec->bv_page);
  280. result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  281. MSG_WAITALL);
  282. kunmap(bvec->bv_page);
  283. return result;
  284. }
  285. /* NULL returned = something went wrong, inform userspace */
  286. static struct request *nbd_read_stat(struct nbd_device *lo)
  287. {
  288. int result;
  289. struct nbd_reply reply;
  290. struct request *req;
  291. struct socket *sock = lo->sock;
  292. reply.magic = 0;
  293. result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
  294. if (result <= 0) {
  295. printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  296. lo->disk->disk_name, result);
  297. goto harderror;
  298. }
  299. req = nbd_find_request(lo, reply.handle);
  300. if (unlikely(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.magic) != NBD_REPLY_MAGIC) {
  310. printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  311. lo->disk->disk_name,
  312. (unsigned long)ntohl(reply.magic));
  313. result = -EPROTO;
  314. goto harderror;
  315. }
  316. if (ntohl(reply.error)) {
  317. printk(KERN_ERR "%s: Other side returned error (%d)\n",
  318. lo->disk->disk_name, ntohl(reply.error));
  319. req->errors++;
  320. return req;
  321. }
  322. dprintk(DBG_RX, "%s: request %p: got reply\n",
  323. lo->disk->disk_name, req);
  324. if (nbd_cmd(req) == NBD_CMD_READ) {
  325. int i;
  326. struct bio *bio;
  327. rq_for_each_bio(bio, req) {
  328. struct bio_vec *bvec;
  329. bio_for_each_segment(bvec, bio, i) {
  330. result = sock_recv_bvec(sock, bvec);
  331. if (result <= 0) {
  332. printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  333. lo->disk->disk_name,
  334. result);
  335. goto harderror;
  336. }
  337. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  338. lo->disk->disk_name, req, bvec->bv_len);
  339. }
  340. }
  341. }
  342. return req;
  343. harderror:
  344. lo->harderror = result;
  345. return NULL;
  346. }
  347. static void nbd_do_it(struct nbd_device *lo)
  348. {
  349. struct request *req;
  350. BUG_ON(lo->magic != LO_MAGIC);
  351. while ((req = nbd_read_stat(lo)) != NULL)
  352. nbd_end_request(req);
  353. return;
  354. }
  355. static void nbd_clear_que(struct nbd_device *lo)
  356. {
  357. struct request *req;
  358. BUG_ON(lo->magic != LO_MAGIC);
  359. /*
  360. * Because we have set lo->sock to NULL under the tx_lock, all
  361. * modifications to the list must have completed by now. For
  362. * the same reason, the active_req must be NULL.
  363. *
  364. * As a consequence, we don't need to take the spin lock while
  365. * purging the list here.
  366. */
  367. BUG_ON(lo->sock);
  368. BUG_ON(lo->active_req);
  369. while (!list_empty(&lo->queue_head)) {
  370. req = list_entry(lo->queue_head.next, struct request,
  371. queuelist);
  372. list_del_init(&req->queuelist);
  373. req->errors++;
  374. nbd_end_request(req);
  375. }
  376. }
  377. /*
  378. * We always wait for result of write, for now. It would be nice to make it optional
  379. * in future
  380. * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
  381. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  382. */
  383. static void do_nbd_request(request_queue_t * q)
  384. {
  385. struct request *req;
  386. while ((req = elv_next_request(q)) != NULL) {
  387. struct nbd_device *lo;
  388. blkdev_dequeue_request(req);
  389. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
  390. req->rq_disk->disk_name, req, req->flags);
  391. if (!(req->flags & REQ_CMD))
  392. goto error_out;
  393. lo = req->rq_disk->private_data;
  394. BUG_ON(lo->magic != LO_MAGIC);
  395. nbd_cmd(req) = NBD_CMD_READ;
  396. if (rq_data_dir(req) == WRITE) {
  397. nbd_cmd(req) = NBD_CMD_WRITE;
  398. if (lo->flags & NBD_READ_ONLY) {
  399. printk(KERN_ERR "%s: Write on read-only\n",
  400. lo->disk->disk_name);
  401. goto error_out;
  402. }
  403. }
  404. req->errors = 0;
  405. spin_unlock_irq(q->queue_lock);
  406. down(&lo->tx_lock);
  407. if (unlikely(!lo->sock)) {
  408. up(&lo->tx_lock);
  409. printk(KERN_ERR "%s: Attempted send on closed socket\n",
  410. lo->disk->disk_name);
  411. req->errors++;
  412. nbd_end_request(req);
  413. spin_lock_irq(q->queue_lock);
  414. continue;
  415. }
  416. lo->active_req = req;
  417. if (nbd_send_req(lo, req) != 0) {
  418. printk(KERN_ERR "%s: Request send failed\n",
  419. lo->disk->disk_name);
  420. req->errors++;
  421. nbd_end_request(req);
  422. } else {
  423. spin_lock(&lo->queue_lock);
  424. list_add(&req->queuelist, &lo->queue_head);
  425. spin_unlock(&lo->queue_lock);
  426. }
  427. lo->active_req = NULL;
  428. up(&lo->tx_lock);
  429. wake_up_all(&lo->active_wq);
  430. spin_lock_irq(q->queue_lock);
  431. continue;
  432. error_out:
  433. req->errors++;
  434. spin_unlock(q->queue_lock);
  435. nbd_end_request(req);
  436. spin_lock(q->queue_lock);
  437. }
  438. return;
  439. }
  440. static int nbd_ioctl(struct inode *inode, struct file *file,
  441. unsigned int cmd, unsigned long arg)
  442. {
  443. struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
  444. int error;
  445. struct request sreq ;
  446. if (!capable(CAP_SYS_ADMIN))
  447. return -EPERM;
  448. BUG_ON(lo->magic != LO_MAGIC);
  449. /* Anyone capable of this syscall can do *real bad* things */
  450. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  451. lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  452. switch (cmd) {
  453. case NBD_DISCONNECT:
  454. printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
  455. sreq.flags = REQ_SPECIAL;
  456. nbd_cmd(&sreq) = NBD_CMD_DISC;
  457. /*
  458. * Set these to sane values in case server implementation
  459. * fails to check the request type first and also to keep
  460. * debugging output cleaner.
  461. */
  462. sreq.sector = 0;
  463. sreq.nr_sectors = 0;
  464. if (!lo->sock)
  465. return -EINVAL;
  466. nbd_send_req(lo, &sreq);
  467. return 0;
  468. case NBD_CLEAR_SOCK:
  469. error = 0;
  470. down(&lo->tx_lock);
  471. lo->sock = NULL;
  472. up(&lo->tx_lock);
  473. file = lo->file;
  474. lo->file = NULL;
  475. nbd_clear_que(lo);
  476. BUG_ON(!list_empty(&lo->queue_head));
  477. if (file)
  478. fput(file);
  479. return error;
  480. case NBD_SET_SOCK:
  481. if (lo->file)
  482. return -EBUSY;
  483. error = -EINVAL;
  484. file = fget(arg);
  485. if (file) {
  486. inode = file->f_dentry->d_inode;
  487. if (S_ISSOCK(inode->i_mode)) {
  488. lo->file = file;
  489. lo->sock = SOCKET_I(inode);
  490. error = 0;
  491. } else {
  492. fput(file);
  493. }
  494. }
  495. return error;
  496. case NBD_SET_BLKSIZE:
  497. lo->blksize = arg;
  498. lo->bytesize &= ~(lo->blksize-1);
  499. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  500. set_blocksize(inode->i_bdev, lo->blksize);
  501. set_capacity(lo->disk, lo->bytesize >> 9);
  502. return 0;
  503. case NBD_SET_SIZE:
  504. lo->bytesize = arg & ~(lo->blksize-1);
  505. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  506. set_blocksize(inode->i_bdev, lo->blksize);
  507. set_capacity(lo->disk, lo->bytesize >> 9);
  508. return 0;
  509. case NBD_SET_SIZE_BLOCKS:
  510. lo->bytesize = ((u64) arg) * lo->blksize;
  511. inode->i_bdev->bd_inode->i_size = lo->bytesize;
  512. set_blocksize(inode->i_bdev, lo->blksize);
  513. set_capacity(lo->disk, lo->bytesize >> 9);
  514. return 0;
  515. case NBD_DO_IT:
  516. if (!lo->file)
  517. return -EINVAL;
  518. nbd_do_it(lo);
  519. /* on return tidy up in case we have a signal */
  520. /* Forcibly shutdown the socket causing all listeners
  521. * to error
  522. *
  523. * FIXME: This code is duplicated from sys_shutdown, but
  524. * there should be a more generic interface rather than
  525. * calling socket ops directly here */
  526. down(&lo->tx_lock);
  527. if (lo->sock) {
  528. printk(KERN_WARNING "%s: shutting down socket\n",
  529. lo->disk->disk_name);
  530. lo->sock->ops->shutdown(lo->sock,
  531. SEND_SHUTDOWN|RCV_SHUTDOWN);
  532. lo->sock = NULL;
  533. }
  534. up(&lo->tx_lock);
  535. file = lo->file;
  536. lo->file = NULL;
  537. nbd_clear_que(lo);
  538. printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
  539. if (file)
  540. fput(file);
  541. return lo->harderror;
  542. case NBD_CLEAR_QUE:
  543. /*
  544. * This is for compatibility only. The queue is always cleared
  545. * by NBD_DO_IT or NBD_CLEAR_SOCK.
  546. */
  547. BUG_ON(!lo->sock && !list_empty(&lo->queue_head));
  548. return 0;
  549. case NBD_PRINT_DEBUG:
  550. printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
  551. inode->i_bdev->bd_disk->disk_name,
  552. lo->queue_head.next, lo->queue_head.prev,
  553. &lo->queue_head);
  554. return 0;
  555. }
  556. return -EINVAL;
  557. }
  558. static struct block_device_operations nbd_fops =
  559. {
  560. .owner = THIS_MODULE,
  561. .ioctl = nbd_ioctl,
  562. };
  563. /*
  564. * And here should be modules and kernel interface
  565. * (Just smiley confuses emacs :-)
  566. */
  567. static int __init nbd_init(void)
  568. {
  569. int err = -ENOMEM;
  570. int i;
  571. if (sizeof(struct nbd_request) != 28) {
  572. printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
  573. return -EIO;
  574. }
  575. if (nbds_max > MAX_NBD) {
  576. printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD,
  577. nbds_max);
  578. return -EINVAL;
  579. }
  580. for (i = 0; i < nbds_max; i++) {
  581. struct gendisk *disk = alloc_disk(1);
  582. if (!disk)
  583. goto out;
  584. nbd_dev[i].disk = disk;
  585. /*
  586. * The new linux 2.5 block layer implementation requires
  587. * every gendisk to have its very own request_queue struct.
  588. * These structs are big so we dynamically allocate them.
  589. */
  590. disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
  591. if (!disk->queue) {
  592. put_disk(disk);
  593. goto out;
  594. }
  595. }
  596. if (register_blkdev(NBD_MAJOR, "nbd")) {
  597. err = -EIO;
  598. goto out;
  599. }
  600. printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
  601. dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
  602. devfs_mk_dir("nbd");
  603. for (i = 0; i < nbds_max; i++) {
  604. struct gendisk *disk = nbd_dev[i].disk;
  605. nbd_dev[i].file = NULL;
  606. nbd_dev[i].magic = LO_MAGIC;
  607. nbd_dev[i].flags = 0;
  608. spin_lock_init(&nbd_dev[i].queue_lock);
  609. INIT_LIST_HEAD(&nbd_dev[i].queue_head);
  610. init_MUTEX(&nbd_dev[i].tx_lock);
  611. init_waitqueue_head(&nbd_dev[i].active_wq);
  612. nbd_dev[i].blksize = 1024;
  613. nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
  614. disk->major = NBD_MAJOR;
  615. disk->first_minor = i;
  616. disk->fops = &nbd_fops;
  617. disk->private_data = &nbd_dev[i];
  618. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  619. sprintf(disk->disk_name, "nbd%d", i);
  620. sprintf(disk->devfs_name, "nbd/%d", i);
  621. set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
  622. add_disk(disk);
  623. }
  624. return 0;
  625. out:
  626. while (i--) {
  627. blk_cleanup_queue(nbd_dev[i].disk->queue);
  628. put_disk(nbd_dev[i].disk);
  629. }
  630. return err;
  631. }
  632. static void __exit nbd_cleanup(void)
  633. {
  634. int i;
  635. for (i = 0; i < nbds_max; i++) {
  636. struct gendisk *disk = nbd_dev[i].disk;
  637. nbd_dev[i].magic = 0;
  638. if (disk) {
  639. del_gendisk(disk);
  640. blk_cleanup_queue(disk->queue);
  641. put_disk(disk);
  642. }
  643. }
  644. devfs_remove("nbd");
  645. unregister_blkdev(NBD_MAJOR, "nbd");
  646. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  647. }
  648. module_init(nbd_init);
  649. module_exit(nbd_cleanup);
  650. MODULE_DESCRIPTION("Network Block Device");
  651. MODULE_LICENSE("GPL");
  652. module_param(nbds_max, int, 0444);
  653. MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");
  654. #ifndef NDEBUG
  655. module_param(debugflags, int, 0644);
  656. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  657. #endif