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