nbd.c 19 KB

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