nbd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 <net/sock.h>
  56. #include <linux/devfs_fs_kernel.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/types.h>
  59. #include <linux/nbd.h>
  60. #define LO_MAGIC 0x68797548
  61. #ifdef NDEBUG
  62. #define dprintk(flags, fmt...)
  63. #else /* NDEBUG */
  64. #define dprintk(flags, fmt...) do { \
  65. if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
  66. } while (0)
  67. #define DBG_IOCTL 0x0004
  68. #define DBG_INIT 0x0010
  69. #define DBG_EXIT 0x0020
  70. #define DBG_BLKDEV 0x0100
  71. #define DBG_RX 0x0200
  72. #define DBG_TX 0x0400
  73. static unsigned int debugflags;
  74. #endif /* NDEBUG */
  75. static struct nbd_device nbd_dev[MAX_NBD];
  76. /*
  77. * Use just one lock (or at most 1 per NIC). Two arguments for this:
  78. * 1. Each NIC is essentially a synchronization point for all servers
  79. * accessed through that NIC so there's no need to have more locks
  80. * than NICs anyway.
  81. * 2. More locks lead to more "Dirty cache line bouncing" which will slow
  82. * down each lock to the point where they're actually slower than just
  83. * a single lock.
  84. * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
  85. */
  86. static DEFINE_SPINLOCK(nbd_lock);
  87. #ifndef NDEBUG
  88. static const char *ioctl_cmd_to_ascii(int cmd)
  89. {
  90. switch (cmd) {
  91. case NBD_SET_SOCK: return "set-sock";
  92. case NBD_SET_BLKSIZE: return "set-blksize";
  93. case NBD_SET_SIZE: return "set-size";
  94. case NBD_DO_IT: return "do-it";
  95. case NBD_CLEAR_SOCK: return "clear-sock";
  96. case NBD_CLEAR_QUE: return "clear-que";
  97. case NBD_PRINT_DEBUG: return "print-debug";
  98. case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
  99. case NBD_DISCONNECT: return "disconnect";
  100. case BLKROSET: return "set-read-only";
  101. case BLKFLSBUF: return "flush-buffer-cache";
  102. }
  103. return "unknown";
  104. }
  105. static const char *nbdcmd_to_ascii(int cmd)
  106. {
  107. switch (cmd) {
  108. case NBD_CMD_READ: return "read";
  109. case NBD_CMD_WRITE: return "write";
  110. case NBD_CMD_DISC: return "disconnect";
  111. }
  112. return "invalid";
  113. }
  114. #endif /* NDEBUG */
  115. static void nbd_end_request(struct request *req)
  116. {
  117. int uptodate = (req->errors == 0) ? 1 : 0;
  118. request_queue_t *q = req->q;
  119. unsigned long flags;
  120. dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
  121. req, uptodate? "done": "failed");
  122. spin_lock_irqsave(q->queue_lock, flags);
  123. if (!end_that_request_first(req, uptodate, req->nr_sectors)) {
  124. end_that_request_last(req);
  125. }
  126. spin_unlock_irqrestore(q->queue_lock, flags);
  127. }
  128. /*
  129. * Send or receive packet.
  130. */
  131. static int sock_xmit(struct socket *sock, int send, void *buf, int size,
  132. int msg_flags)
  133. {
  134. int result;
  135. struct msghdr msg;
  136. struct kvec iov;
  137. unsigned long flags;
  138. sigset_t oldset;
  139. /* Allow interception of SIGKILL only
  140. * Don't allow other signals to interrupt the transmission */
  141. spin_lock_irqsave(&current->sighand->siglock, flags);
  142. oldset = current->blocked;
  143. sigfillset(&current->blocked);
  144. sigdelsetmask(&current->blocked, sigmask(SIGKILL));
  145. recalc_sigpending();
  146. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  147. do {
  148. sock->sk->sk_allocation = GFP_NOIO;
  149. iov.iov_base = buf;
  150. iov.iov_len = size;
  151. msg.msg_name = NULL;
  152. msg.msg_namelen = 0;
  153. msg.msg_control = NULL;
  154. msg.msg_controllen = 0;
  155. msg.msg_namelen = 0;
  156. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  157. if (send)
  158. result = kernel_sendmsg(sock, &msg, &iov, 1, size);
  159. else
  160. result = kernel_recvmsg(sock, &msg, &iov, 1, size, 0);
  161. if (signal_pending(current)) {
  162. siginfo_t info;
  163. spin_lock_irqsave(&current->sighand->siglock, flags);
  164. printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
  165. current->pid, current->comm,
  166. dequeue_signal(current, &current->blocked, &info));
  167. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  168. result = -EINTR;
  169. break;
  170. }
  171. if (result <= 0) {
  172. if (result == 0)
  173. result = -EPIPE; /* short read */
  174. break;
  175. }
  176. size -= result;
  177. buf += result;
  178. } while (size > 0);
  179. spin_lock_irqsave(&current->sighand->siglock, flags);
  180. current->blocked = oldset;
  181. recalc_sigpending();
  182. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  183. return result;
  184. }
  185. static inline int sock_send_bvec(struct socket *sock, struct bio_vec *bvec,
  186. int flags)
  187. {
  188. int result;
  189. void *kaddr = kmap(bvec->bv_page);
  190. result = sock_xmit(sock, 1, kaddr + bvec->bv_offset, bvec->bv_len,
  191. flags);
  192. kunmap(bvec->bv_page);
  193. return result;
  194. }
  195. static int nbd_send_req(struct nbd_device *lo, struct request *req)
  196. {
  197. int result, i, flags;
  198. struct nbd_request request;
  199. unsigned long size = req->nr_sectors << 9;
  200. struct socket *sock = lo->sock;
  201. request.magic = htonl(NBD_REQUEST_MAGIC);
  202. request.type = htonl(nbd_cmd(req));
  203. request.from = cpu_to_be64((u64) req->sector << 9);
  204. request.len = htonl(size);
  205. memcpy(request.handle, &req, sizeof(req));
  206. down(&lo->tx_lock);
  207. if (!sock || !lo->sock) {
  208. printk(KERN_ERR "%s: Attempted send on closed socket\n",
  209. lo->disk->disk_name);
  210. goto error_out;
  211. }
  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(sock, 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 bio *bio;
  226. /*
  227. * we are really probing at internals to determine
  228. * whether to set MSG_MORE or not...
  229. */
  230. rq_for_each_bio(bio, req) {
  231. struct bio_vec *bvec;
  232. bio_for_each_segment(bvec, bio, i) {
  233. flags = 0;
  234. if ((i < (bio->bi_vcnt - 1)) || bio->bi_next)
  235. flags = MSG_MORE;
  236. dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
  237. lo->disk->disk_name, req,
  238. bvec->bv_len);
  239. result = sock_send_bvec(sock, bvec, flags);
  240. if (result <= 0) {
  241. printk(KERN_ERR "%s: Send data failed (result %d)\n",
  242. lo->disk->disk_name,
  243. result);
  244. goto error_out;
  245. }
  246. }
  247. }
  248. }
  249. up(&lo->tx_lock);
  250. return 0;
  251. error_out:
  252. up(&lo->tx_lock);
  253. return 1;
  254. }
  255. static struct request *nbd_find_request(struct nbd_device *lo, char *handle)
  256. {
  257. struct request *req;
  258. struct list_head *tmp;
  259. struct request *xreq;
  260. memcpy(&xreq, handle, sizeof(xreq));
  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. return NULL;
  272. }
  273. static inline int sock_recv_bvec(struct socket *sock, struct bio_vec *bvec)
  274. {
  275. int result;
  276. void *kaddr = kmap(bvec->bv_page);
  277. result = sock_xmit(sock, 0, kaddr + bvec->bv_offset, bvec->bv_len,
  278. MSG_WAITALL);
  279. kunmap(bvec->bv_page);
  280. return result;
  281. }
  282. /* NULL returned = something went wrong, inform userspace */
  283. static struct request *nbd_read_stat(struct nbd_device *lo)
  284. {
  285. int result;
  286. struct nbd_reply reply;
  287. struct request *req;
  288. struct socket *sock = lo->sock;
  289. reply.magic = 0;
  290. result = sock_xmit(sock, 0, &reply, sizeof(reply), MSG_WAITALL);
  291. if (result <= 0) {
  292. printk(KERN_ERR "%s: Receive control failed (result %d)\n",
  293. lo->disk->disk_name, result);
  294. goto harderror;
  295. }
  296. req = nbd_find_request(lo, reply.handle);
  297. if (req == NULL) {
  298. printk(KERN_ERR "%s: Unexpected reply (%p)\n",
  299. lo->disk->disk_name, reply.handle);
  300. result = -EBADR;
  301. goto harderror;
  302. }
  303. if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
  304. printk(KERN_ERR "%s: Wrong magic (0x%lx)\n",
  305. lo->disk->disk_name,
  306. (unsigned long)ntohl(reply.magic));
  307. result = -EPROTO;
  308. goto harderror;
  309. }
  310. if (ntohl(reply.error)) {
  311. printk(KERN_ERR "%s: Other side returned error (%d)\n",
  312. lo->disk->disk_name, ntohl(reply.error));
  313. req->errors++;
  314. return req;
  315. }
  316. dprintk(DBG_RX, "%s: request %p: got reply\n",
  317. lo->disk->disk_name, req);
  318. if (nbd_cmd(req) == NBD_CMD_READ) {
  319. int i;
  320. struct bio *bio;
  321. rq_for_each_bio(bio, req) {
  322. struct bio_vec *bvec;
  323. bio_for_each_segment(bvec, bio, i) {
  324. result = sock_recv_bvec(sock, bvec);
  325. if (result <= 0) {
  326. printk(KERN_ERR "%s: Receive data failed (result %d)\n",
  327. lo->disk->disk_name,
  328. result);
  329. goto harderror;
  330. }
  331. dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
  332. lo->disk->disk_name, req, bvec->bv_len);
  333. }
  334. }
  335. }
  336. return req;
  337. harderror:
  338. lo->harderror = result;
  339. return NULL;
  340. }
  341. static void nbd_do_it(struct nbd_device *lo)
  342. {
  343. struct request *req;
  344. BUG_ON(lo->magic != LO_MAGIC);
  345. while ((req = nbd_read_stat(lo)) != NULL)
  346. nbd_end_request(req);
  347. return;
  348. }
  349. static void nbd_clear_que(struct nbd_device *lo)
  350. {
  351. struct request *req;
  352. BUG_ON(lo->magic != LO_MAGIC);
  353. do {
  354. req = NULL;
  355. spin_lock(&lo->queue_lock);
  356. if (!list_empty(&lo->queue_head)) {
  357. req = list_entry(lo->queue_head.next, struct request, queuelist);
  358. list_del_init(&req->queuelist);
  359. }
  360. spin_unlock(&lo->queue_lock);
  361. if (req) {
  362. req->errors++;
  363. nbd_end_request(req);
  364. }
  365. } while (req);
  366. }
  367. /*
  368. * We always wait for result of write, for now. It would be nice to make it optional
  369. * in future
  370. * if ((req->cmd == WRITE) && (lo->flags & NBD_WRITE_NOCHK))
  371. * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  372. */
  373. static void do_nbd_request(request_queue_t * q)
  374. {
  375. struct request *req;
  376. while ((req = elv_next_request(q)) != NULL) {
  377. struct nbd_device *lo;
  378. blkdev_dequeue_request(req);
  379. dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%lx)\n",
  380. req->rq_disk->disk_name, req, req->flags);
  381. if (!(req->flags & REQ_CMD))
  382. goto error_out;
  383. lo = req->rq_disk->private_data;
  384. BUG_ON(lo->magic != LO_MAGIC);
  385. if (!lo->file) {
  386. printk(KERN_ERR "%s: Request when not-ready\n",
  387. lo->disk->disk_name);
  388. goto error_out;
  389. }
  390. nbd_cmd(req) = NBD_CMD_READ;
  391. if (rq_data_dir(req) == WRITE) {
  392. nbd_cmd(req) = NBD_CMD_WRITE;
  393. if (lo->flags & NBD_READ_ONLY) {
  394. printk(KERN_ERR "%s: Write on read-only\n",
  395. lo->disk->disk_name);
  396. goto error_out;
  397. }
  398. }
  399. req->errors = 0;
  400. spin_unlock_irq(q->queue_lock);
  401. spin_lock(&lo->queue_lock);
  402. if (!lo->file) {
  403. spin_unlock(&lo->queue_lock);
  404. printk(KERN_ERR "%s: failed between accept and semaphore, file lost\n",
  405. lo->disk->disk_name);
  406. req->errors++;
  407. nbd_end_request(req);
  408. spin_lock_irq(q->queue_lock);
  409. continue;
  410. }
  411. list_add(&req->queuelist, &lo->queue_head);
  412. spin_unlock(&lo->queue_lock);
  413. if (nbd_send_req(lo, req) != 0) {
  414. printk(KERN_ERR "%s: Request send failed\n",
  415. lo->disk->disk_name);
  416. if (nbd_find_request(lo, (char *)&req) != NULL) {
  417. /* we still own req */
  418. req->errors++;
  419. nbd_end_request(req);
  420. } else /* we're racing with nbd_clear_que */
  421. printk(KERN_DEBUG "nbd: can't find req\n");
  422. }
  423. spin_lock_irq(q->queue_lock);
  424. continue;
  425. error_out:
  426. req->errors++;
  427. spin_unlock(q->queue_lock);
  428. nbd_end_request(req);
  429. spin_lock(q->queue_lock);
  430. }
  431. return;
  432. }
  433. static int nbd_ioctl(struct inode *inode, struct file *file,
  434. unsigned int cmd, unsigned long arg)
  435. {
  436. struct nbd_device *lo = inode->i_bdev->bd_disk->private_data;
  437. int error;
  438. struct request sreq ;
  439. if (!capable(CAP_SYS_ADMIN))
  440. return -EPERM;
  441. BUG_ON(lo->magic != LO_MAGIC);
  442. /* Anyone capable of this syscall can do *real bad* things */
  443. dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
  444. lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
  445. switch (cmd) {
  446. case NBD_DISCONNECT:
  447. printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name);
  448. sreq.flags = REQ_SPECIAL;
  449. nbd_cmd(&sreq) = NBD_CMD_DISC;
  450. /*
  451. * Set these to sane values in case server implementation
  452. * fails to check the request type first and also to keep
  453. * debugging output cleaner.
  454. */
  455. sreq.sector = 0;
  456. sreq.nr_sectors = 0;
  457. if (!lo->sock)
  458. return -EINVAL;
  459. nbd_send_req(lo, &sreq);
  460. return 0;
  461. case NBD_CLEAR_SOCK:
  462. error = 0;
  463. down(&lo->tx_lock);
  464. lo->sock = NULL;
  465. up(&lo->tx_lock);
  466. spin_lock(&lo->queue_lock);
  467. file = lo->file;
  468. lo->file = NULL;
  469. spin_unlock(&lo->queue_lock);
  470. nbd_clear_que(lo);
  471. spin_lock(&lo->queue_lock);
  472. if (!list_empty(&lo->queue_head)) {
  473. printk(KERN_ERR "nbd: disconnect: some requests are in progress -> please try again.\n");
  474. error = -EBUSY;
  475. }
  476. spin_unlock(&lo->queue_lock);
  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. spin_lock(&lo->queue_lock);
  536. file = lo->file;
  537. lo->file = NULL;
  538. spin_unlock(&lo->queue_lock);
  539. nbd_clear_que(lo);
  540. printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
  541. if (file)
  542. fput(file);
  543. return lo->harderror;
  544. case NBD_CLEAR_QUE:
  545. down(&lo->tx_lock);
  546. if (lo->sock) {
  547. up(&lo->tx_lock);
  548. return 0; /* probably should be error, but that would
  549. * break "nbd-client -d", so just return 0 */
  550. }
  551. up(&lo->tx_lock);
  552. nbd_clear_que(lo);
  553. return 0;
  554. case NBD_PRINT_DEBUG:
  555. printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n",
  556. inode->i_bdev->bd_disk->disk_name,
  557. lo->queue_head.next, lo->queue_head.prev,
  558. &lo->queue_head);
  559. return 0;
  560. }
  561. return -EINVAL;
  562. }
  563. static struct block_device_operations nbd_fops =
  564. {
  565. .owner = THIS_MODULE,
  566. .ioctl = nbd_ioctl,
  567. };
  568. /*
  569. * And here should be modules and kernel interface
  570. * (Just smiley confuses emacs :-)
  571. */
  572. static int __init nbd_init(void)
  573. {
  574. int err = -ENOMEM;
  575. int i;
  576. if (sizeof(struct nbd_request) != 28) {
  577. printk(KERN_CRIT "nbd: sizeof nbd_request needs to be 28 in order to work!\n" );
  578. return -EIO;
  579. }
  580. for (i = 0; i < MAX_NBD; 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 < MAX_NBD; 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. nbd_dev[i].blksize = 1024;
  612. nbd_dev[i].bytesize = 0x7ffffc00ULL << 10; /* 2TB */
  613. disk->major = NBD_MAJOR;
  614. disk->first_minor = i;
  615. disk->fops = &nbd_fops;
  616. disk->private_data = &nbd_dev[i];
  617. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  618. sprintf(disk->disk_name, "nbd%d", i);
  619. sprintf(disk->devfs_name, "nbd/%d", i);
  620. set_capacity(disk, 0x7ffffc00ULL << 1); /* 2 TB */
  621. add_disk(disk);
  622. }
  623. return 0;
  624. out:
  625. while (i--) {
  626. blk_cleanup_queue(nbd_dev[i].disk->queue);
  627. put_disk(nbd_dev[i].disk);
  628. }
  629. return err;
  630. }
  631. static void __exit nbd_cleanup(void)
  632. {
  633. int i;
  634. for (i = 0; i < MAX_NBD; i++) {
  635. struct gendisk *disk = nbd_dev[i].disk;
  636. if (disk) {
  637. del_gendisk(disk);
  638. blk_cleanup_queue(disk->queue);
  639. put_disk(disk);
  640. }
  641. }
  642. devfs_remove("nbd");
  643. unregister_blkdev(NBD_MAJOR, "nbd");
  644. printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
  645. }
  646. module_init(nbd_init);
  647. module_exit(nbd_cleanup);
  648. MODULE_DESCRIPTION("Network Block Device");
  649. MODULE_LICENSE("GPL");
  650. #ifndef NDEBUG
  651. module_param(debugflags, int, 0644);
  652. MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
  653. #endif