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