r9: Support for changing blocksize.
[nbd.git] / nbd-client.c
1 /*
2  * Open connection for network block device
3  *
4  * Copyright 1997,1998 Pavel Machek, distribute under GPL
5  *  <pavel@atrey.karlin.mff.cuni.cz>
6  *
7  * Version 1.0 - 64bit issues should be fixed, now
8  * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
9  * Version 1.2 - I added new option '-d' to send the disconnect request
10  */
11
12 #include <asm/page.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <netinet/tcp.h>
18 #include <netinet/in.h>         /* sockaddr_in, htons, in_addr */
19 #include <netdb.h>              /* hostent, gethostby*, getservby* */
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <syslog.h>
23 #include <stdlib.h>
24
25 #define MY_NAME "nbd_client"
26 #ifndef __GNUC__
27 #error I need GCC to work
28 #endif
29
30 #include <linux/ioctl.h>
31 #include "cliserv.h"
32
33 int opennet(char *name, int port)
34 {
35         int sock;
36         struct sockaddr_in xaddrin;
37         int xaddrinlen = sizeof(xaddrin);
38         struct hostent *hostn;
39
40         hostn = gethostbyname(name);
41         if (!hostn)
42                 err("Gethostname failed: %h\n");
43
44         if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
45                 err("Socket failed: %m");
46
47         xaddrin.sin_family = AF_INET;
48         xaddrin.sin_port = htons(port);
49         xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
50         if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
51                 err("Connect: %m");
52
53         setmysockopt(sock);
54         return sock;
55 }
56
57 int main(int argc, char *argv[])
58 {
59         int port, sock, nbd, one = 1;
60         u64 magic, size64;
61         unsigned long size;
62         char buf[256] = "\0\0\0\0\0\0\0\0\0";
63         int blocksize=1024;
64         char *hostname;
65         int swap=0;
66
67         logging();
68
69         if (argc < 3) {
70         errmsg:
71                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] host port nbd_device [-swap]\n");
72                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
73                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
74                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
75                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
76                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
77                 return 1;
78         }
79
80         ++argv; --argc; /* skip programname */
81         
82         if (strcmp(argv[0], "-d")==0) {
83           nbd = open(argv[1], O_RDWR);
84           if (nbd < 0)
85                 err("Can not open NBD: %m");
86           printf("Disconnecting: que, ");
87           if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
88                 err("Ioctl failed: %m\n");
89           printf("disconnect, ");
90 #ifdef NBD_DISCONNECT
91           if (ioctl(nbd, NBD_DISCONNECT)<0)
92                 err("Ioctl failed: %m\n");
93           printf("sock, ");
94 #else
95           fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
96           exit(1);
97 #endif
98           if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
99                 err("Ioctl failed: %m\n");
100           printf("done\n");
101           return 0;
102         }
103
104         if (strncmp(argv[0], "bs=", 3)==0) {
105           blocksize=atoi(argv[0]+3);
106           ++argv; --argc; /* skip blocksize */
107         }
108
109         if (argc==0) goto errmsg;
110         hostname=argv[0];
111         ++argv; --argc; /* skip hostname */
112
113         if (argc==0) goto errmsg;
114         port = atoi(argv[0]);
115         ++argv; --argc; /* skip port */
116
117         if (argc==0) goto errmsg;
118         sock = opennet(hostname, port);
119         nbd = open(argv[0], O_RDWR);
120         if (nbd < 0)
121           err("Can not open NBD: %m");
122         ++argv; --argc; /* skip device */
123
124         if (argc>1) goto errmsg;
125         if (argc!=0) swap=1;
126         argv=NULL; argc=0; /* don't use it later suddenly */
127
128         printf("Negotiation: ");
129         if (read(sock, buf, 8) < 0)
130                 err("Failed/1: %m");
131         if (strcmp(buf, INIT_PASSWD))
132                 err("INIT_PASSWD bad");
133         printf(".");
134         if (read(sock, &magic, sizeof(magic)) < 0)
135                 err("Failed/2: %m");
136         magic = ntohll(magic);
137         if (magic != cliserv_magic)
138                 err("Not enough cliserv_magic");
139         printf(".");
140
141         if (read(sock, &size64, sizeof(size64)) < 0)
142                 err("Failed/3: %m\n");
143         size64 = ntohll(size64);
144
145 #ifdef NBD_SET_SIZE_BLOCKS
146         if ((size64>>10) > (~0UL >> 1)) {
147                 printf("size = %luMB", (unsigned long)(size64>>20));
148                 err("Exported device is too big for me. Get 64-bit machine :-(\n");
149         } else
150                 printf("size = %luKB", (unsigned long)(size64>>10));
151 #else
152         if (size64 > (~0UL >> 1)) {
153                 printf("size = %luKB", (unsigned long)(size64>>10));
154                 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
155         } else
156                 printf("size = %lu", (unsigned long)(size64));
157 #endif
158
159         if (read(sock, &buf, 128) < 0)
160                 err("Failed/4: %m\n");
161         printf("\n");
162
163 #ifdef NBD_SET_SIZE_BLOCKS
164         if (size64/blocksize > (~0UL >> 1))
165                 err("Device too large.\n");
166         else {
167                 int er;
168                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
169                         err("Ioctl/1.1a failed: %m\n");
170                 size = (unsigned long)(size64/blocksize);
171                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
172                         err("Ioctl/1.1b failed: %m\n");
173 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
174         }
175 #else
176         if (size64 > (~0UL >> 1)) {
177                 err("Device too large.\n");
178         } else {
179                 size = (unsigned long)size64;
180                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
181                         err("Ioctl/1 failed: %m\n");
182         }
183 #endif
184
185         ioctl(nbd, NBD_CLEAR_SOCK);
186         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
187                 err("Ioctl/2 failed: %m\n");
188
189 #ifndef SO_SWAPPING
190         if (swap)
191                 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
192 #else
193         if (swap)
194                 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
195                          err("Could not enable swapping: %m");
196 #endif
197
198         /* Go daemon */
199
200         chdir("/");
201         if (fork())
202                 exit(0);
203
204         if (ioctl(nbd, NBD_DO_IT) < 0)
205                 fprintf(stderr, "Kernel call returned: %m");
206         else
207                 fprintf(stderr, "Kernel call returned.");
208         printf("Closing: que, ");
209         ioctl(nbd, NBD_CLEAR_QUE);
210         printf("sock, ");
211         ioctl(nbd, NBD_CLEAR_SOCK);
212         printf("done\n");
213         return 0;
214 }