Document -noswap 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         int nofork=0;
213         u64 size64;
214         u32 flags;
215
216         logging();
217
218         if (argc < 3) {
219         errmsg:
220                 fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
221                 fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork]\n");
222                 fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
223                 fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
224                 fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
225                 fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
226                 fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
227                 fprintf(stderr, "blocksizes other than 1024 without patches\n");
228                 return 1;
229         }
230
231         ++argv; --argc; /* skip programname */
232
233         if (strcmp(argv[0], "-d")==0) {
234                 nbd = open(argv[1], O_RDWR);
235                 if (nbd < 0)
236                         err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
237                 printf("Disconnecting: que, ");
238                 if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
239                         err("Ioctl failed: %m\n");
240                 printf("disconnect, ");
241 #ifdef NBD_DISCONNECT
242                 if (ioctl(nbd, NBD_DISCONNECT)<0)
243                         err("Ioctl failed: %m\n");
244                 printf("sock, ");
245 #else
246                 fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
247                 exit(1);
248 #endif
249                 if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
250                         err("Ioctl failed: %m\n");
251                 printf("done\n");
252                 return 0;
253         }
254         if(strcmp(argv[0], "-c")==0) {
255                 return check_conn(argv[1], 1);
256         }
257         
258         if (strncmp(argv[0], "bs=", 3)==0) {
259                 blocksize=atoi(argv[0]+3);
260                 ++argv; --argc; /* skip blocksize */
261         }
262
263         if (strncmp(argv[0], "timeout=", 8)==0) {
264                 timeout=atoi(argv[0]+8);
265                 ++argv; --argc; /* skip timeout */
266         }
267         
268         if (argc==0) goto errmsg;
269         hostname=argv[0];
270         ++argv; --argc; /* skip hostname */
271
272         if (argc==0) goto errmsg;
273         port = atoi(argv[0]);
274         ++argv; --argc; /* skip port */
275
276         if (argc==0) goto errmsg;
277         nbddev = argv[0];
278         nbd = open(nbddev, O_RDWR);
279         if (nbd < 0)
280           err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
281         ++argv; --argc; /* skip device */
282
283         if (argc>3) goto errmsg;
284         if (argc) {
285                 if(strncmp(argv[0], "-swap", 5)==0) {
286                         swap=1;
287                         ++argv;--argc;
288                 }
289         }
290         if (argc) {
291                 if(strncmp(argv[0], "-persist", 8)==0) {
292                         cont=1;
293                         ++argv;--argc;
294                 }
295         }
296         if (argc) {
297                 if(strncmp(argv[0], "-sdp", 4)==0) {
298                         sdp=1;
299                         ++argv;--argc;
300                 }
301         }
302         if (argc) {
303                 if(strncmp(argv[0], "-nofork", 7)==0) {
304                         nofork=1;
305                         ++argv;--argc;
306                 }
307         }
308         if(argc) goto errmsg;
309         sock = opennet(hostname, port, sdp);
310         argv=NULL; argc=0; /* don't use it later suddenly */
311
312         negotiate(sock, &size64, &flags);
313         setsizes(nbd, size64, blocksize, flags);
314         set_timeout(nbd, timeout);
315         finish_sock(sock, nbd, swap);
316
317         /* Go daemon */
318         
319         chdir("/");
320         do {
321 #ifndef NOFORK
322                 if (!nofork) {
323                         if (fork()) {
324                                 while(check_conn(nbddev, 0)) {
325                                         sleep(1);
326                                 }
327                                 open(nbddev, O_RDONLY);
328                                 exit(0);
329                         }
330                 }
331 #endif
332
333                 if (ioctl(nbd, NBD_DO_IT) < 0) {
334                         fprintf(stderr, "Kernel call returned: %m");
335                         if(errno==EBADR) {
336                                 /* The user probably did 'nbd-client -d' on us.
337                                  * quit */
338                                 cont=0;
339                         } else {
340                                 if(cont) {
341                                         u64 new_size;
342                                         u32 new_flags;
343
344                                         fprintf(stderr, " Reconnecting\n");
345                                         close(sock); close(nbd);
346                                         sock = opennet(hostname, port, sdp);
347                                         nbd = open(nbddev, O_RDWR);
348                                         negotiate(sock, &new_size, &new_flags);
349                                         if (size64 != new_size) {
350                                                 err("Size of the device changed. Bye");
351                                         }
352                                         setsizes(nbd, size64, blocksize,
353                                                                 new_flags);
354
355                                         set_timeout(nbd, timeout);
356                                         finish_sock(sock,nbd,swap);
357                                 }
358                         }
359                 } else {
360                         /* We're on 2.4. It's not clearly defined what exactly
361                          * happened at this point. Probably best to quit, now
362                          */
363                         fprintf(stderr, "Kernel call returned.");
364                         cont=0;
365                 }
366         } while(cont);
367         printf("Closing: que, ");
368         ioctl(nbd, NBD_CLEAR_QUE);
369         printf("sock, ");
370         ioctl(nbd, NBD_CLEAR_SOCK);
371         printf("done\n");
372         return 0;
373 }