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