Change meaning of -swap option
[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 "config.h"
17 #include "lfs.h"
18
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <netinet/tcp.h>
24 #include <netinet/in.h>         /* sockaddr_in, htons, in_addr */
25 #include <netdb.h>              /* hostent, gethostby*, getservby* */
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <syslog.h>
29 #include <stdlib.h>
30 #include <sys/mount.h>
31 #include <sys/mman.h>
32 #include <errno.h>
33
34 #ifndef __GNUC__
35 #error I need GCC to work
36 #endif
37
38 #include <linux/ioctl.h>
39 #define MY_NAME "nbd_client"
40 #include "cliserv.h"
41
42 int check_conn(char* devname, int do_print) {
43         char buf[256];
44         int fd;
45         int len;
46         if(!strncmp(devname, "/dev/", 5)) {
47                 devname+=5;
48         }
49         snprintf(buf, 256, "/sys/block/%s/pid", devname);
50         if((fd=open(buf, O_RDONLY))<0) {
51                 if(errno==ENOENT) {
52                         return 1;
53                 } else {
54                         return 2;
55                 }
56         }
57         len=read(fd, buf, 256);
58         buf[len-1]='\0';
59         if(do_print) printf("%s\n", buf);
60         return 0;
61 }
62
63 int opennet(char *name, int port, int sdp) {
64         int sock;
65         struct sockaddr_in xaddrin;
66         int xaddrinlen = sizeof(xaddrin);
67         struct hostent *hostn;
68         int af;
69
70         hostn = gethostbyname(name);
71         if (!hostn)
72                 err("Gethostname failed: %h\n");
73
74         af = AF_INET;
75         if(sdp) {
76 #ifdef WITH_SDP
77                 af = AF_INET_SDP;
78 #else
79                 err("Can't do SDP: I was not compiled with SDP support!");
80 #endif
81         }
82         if ((sock = socket(af, SOCK_STREAM, IPPROTO_TCP)) < 0)
83                 err("Socket failed: %m");
84
85         xaddrin.sin_family = af;
86         xaddrin.sin_port = htons(port);
87         xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
88         if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
89                 err("Connect: %m");
90
91         setmysockopt(sock);
92         return sock;
93 }
94
95 void negotiate(int sock, u64 *rsize64, u32 *flags) {
96         u64 magic, size64;
97         char buf[256] = "\0\0\0\0\0\0\0\0\0";
98
99         printf("Negotiation: ");
100         if (read(sock, buf, 8) < 0)
101                 err("Failed/1: %m");
102         if (strlen(buf)==0)
103                 err("Server closed connection");
104         if (strcmp(buf, INIT_PASSWD))
105                 err("INIT_PASSWD bad");
106         printf(".");
107         if (read(sock, &magic, sizeof(magic)) < 0)
108                 err("Failed/2: %m");
109         magic = ntohll(magic);
110         if (magic != cliserv_magic)
111                 err("Not enough cliserv_magic");
112         printf(".");
113
114         if (read(sock, &size64, sizeof(size64)) < 0)
115                 err("Failed/3: %m\n");
116         size64 = ntohll(size64);
117
118 #ifdef NBD_SET_SIZE_BLOCKS
119         if ((size64>>10) > (~0UL >> 1)) {
120                 printf("size = %luMB", (unsigned long)(size64>>20));
121                 err("Exported device is too big for me. Get 64-bit machine :-(\n");
122         } else
123                 printf("size = %luKB", (unsigned long)(size64>>10));
124 #else
125         if (size64 > (~0UL >> 1)) {
126                 printf("size = %luKB", (unsigned long)(size64>>10));
127                 err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
128         } else
129                 printf("size = %lu", (unsigned long)(size64));
130 #endif
131
132         if (read(sock, flags, sizeof(*flags)) < 0)
133                 err("Failed/4: %m\n");
134         *flags = ntohl(*flags);
135
136         if (read(sock, &buf, 124) < 0)
137                 err("Failed/5: %m\n");
138         printf("\n");
139
140         *rsize64 = size64;
141 }
142
143 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
144         unsigned long size;
145         int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
146
147 #ifdef NBD_SET_SIZE_BLOCKS
148         if (size64/blocksize > (~0UL >> 1))
149                 err("Device too large.\n");
150         else {
151                 int er;
152                 if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
153                         err("Ioctl/1.1a failed: %m\n");
154                 size = (unsigned long)(size64/blocksize);
155                 if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
156                         err("Ioctl/1.1b failed: %m\n");
157                 fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
158         }
159 #else
160         if (size64 > (~0UL >> 1)) {
161                 err("Device too large.\n");
162         } else {
163                 size = (unsigned long)size64;
164                 if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
165                         err("Ioctl NBD_SET_SIZE failed: %m\n");
166         }
167 #endif
168
169         ioctl(nbd, NBD_CLEAR_SOCK);
170
171         if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
172                 err("Unable to set read-only attribute for device");
173 }
174
175 void set_timeout(int nbd, int timeout) {
176 #ifdef NBD_SET_TIMEOUT
177         if (timeout) {
178                 if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
179                         err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
180                 fprintf(stderr, "timeout=%d\n", timeout);
181         }
182 #endif
183 }
184
185 void finish_sock(int sock, int nbd, int swap) {
186         if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
187                 err("Ioctl NBD_SET_SOCK failed: %m\n");
188
189 /*
190  * If anyone ever forward-patches this patch, I'll happily re-enable
191  * this code. Until then...
192 #ifndef SO_SWAPPING
193         if (swap)
194                 err("You have to compile me on machine with swapping patch enabled in order to use it later.");
195 #else
196         if (swap)
197                 if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
198                         err("Could not enable swapping: %m");
199 #endif
200 */
201         mlockall(MCL_CURRENT | MCL_FUTURE);
202 }
203
204 int main(int argc, char *argv[]) {
205         int port, sock, nbd;
206         int blocksize=1024;
207         char *hostname, *nbddev;
208         int swap=0;
209         int cont=0;
210         int timeout=0;
211         int sdp=0;
212         u64 size64;
213         u32 flags;
214
215         logging();
216
217         if (argc < 3) {
218         errmsg:
219                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
220                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist]\n");
221                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
222                 fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
223                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
224                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
225                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
226                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
227                 return 1;
228         }
229
230         ++argv; --argc; /* skip programname */
231
232         if (strcmp(argv[0], "-d")==0) {
233                 nbd = open(argv[1], O_RDWR);
234                 if (nbd < 0)
235                         err("Can not open NBD: %m");
236                 printf("Disconnecting: que, ");
237                 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
238                         err("Ioctl failed: %m\n");
239                 printf("disconnect, ");
240 #ifdef NBD_DISCONNECT
241                 if (ioctl(nbd, NBD_DISCONNECT)<0)
242                         err("Ioctl failed: %m\n");
243                 printf("sock, ");
244 #else
245                 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
246                 exit(1);
247 #endif
248                 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
249                         err("Ioctl failed: %m\n");
250                 printf("done\n");
251                 return 0;
252         }
253         if(strcmp(argv[0], "-c")==0) {
254                 return check_conn(argv[1], 1);
255         }
256         
257         if (strncmp(argv[0], "bs=", 3)==0) {
258                 blocksize=atoi(argv[0]+3);
259                 ++argv; --argc; /* skip blocksize */
260         }
261
262         if (strncmp(argv[0], "timeout=", 8)==0) {
263                 timeout=atoi(argv[0]+8);
264                 ++argv; --argc; /* skip timeout */
265         }
266         
267         if (argc==0) goto errmsg;
268         hostname=argv[0];
269         ++argv; --argc; /* skip hostname */
270
271         if (argc==0) goto errmsg;
272         port = atoi(argv[0]);
273         ++argv; --argc; /* skip port */
274
275         if (argc==0) goto errmsg;
276         nbddev = argv[0];
277         nbd = open(nbddev, O_RDWR);
278         if (nbd < 0)
279           err("Can not open NBD: %m");
280         ++argv; --argc; /* skip device */
281
282         if (argc>3) goto errmsg;
283         if (argc) {
284                 if(strncmp(argv[0], "-swap", 5)==0) {
285                         swap=1;
286                         ++argv;--argc;
287                 }
288         }
289         if (argc) {
290                 if(strncmp(argv[0], "-persist", 8)==0) {
291                         cont=1;
292                         ++argv;--argc;
293                 }
294         }
295         if (argc) {
296                 if(strncmp(argv[0], "-sdp", 4)==0) {
297                         sdp=1;
298                         ++argv;--argc;
299                 }
300         }
301         if(argc) goto errmsg;
302         sock = opennet(hostname, port, sdp);
303         argv=NULL; argc=0; /* don't use it later suddenly */
304
305         negotiate(sock, &size64, &flags);
306         setsizes(nbd, size64, blocksize, flags);
307         set_timeout(nbd, timeout);
308         finish_sock(sock, nbd, swap);
309
310         /* Go daemon */
311         
312         chdir("/");
313         do {
314 #ifndef NOFORK
315                 if (fork()) {
316                         while(check_conn(nbddev, 0)) {
317                                 sleep(1);
318                         }
319                         open(nbddev, O_RDONLY);
320                         exit(0);
321                 }
322 #endif
323
324                 if (ioctl(nbd, NBD_DO_IT) < 0) {
325                         fprintf(stderr, "Kernel call returned: %m");
326                         if(errno==EBADR) {
327                                 /* The user probably did 'nbd-client -d' on us.
328                                  * quit */
329                                 cont=0;
330                         } else {
331                                 if(cont) {
332                                         u64 new_size;
333                                         u32 new_flags;
334
335                                         fprintf(stderr, " Reconnecting\n");
336                                         close(sock); close(nbd);
337                                         sock = opennet(hostname, port, sdp);
338                                         nbd = open(nbddev, O_RDWR);
339                                         negotiate(sock, &new_size, &new_flags);
340                                         if (size64 != new_size) {
341                                                 err("Size of the device changed. Bye");
342                                         }
343                                         setsizes(nbd, size64, blocksize,
344                                                                 new_flags);
345
346                                         set_timeout(nbd, timeout);
347                                         finish_sock(sock,nbd,swap);
348                                 }
349                         }
350                 } else {
351                         /* We're on 2.4. It's not clearly defined what exactly
352                          * happened at this point. Probably best to quit, now
353                          */
354                         fprintf(stderr, "Kernel call returned.");
355                         cont=0;
356                 }
357         } while(cont);
358         printf("Closing: que, ");
359         ioctl(nbd, NBD_CLEAR_QUE);
360         printf("sock, ");
361         ioctl(nbd, NBD_CLEAR_SOCK);
362         printf("done\n");
363         return 0;
364 }