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