r16: Set SO_REUSEADDR socket option to nbd-server
[nbd.git] / nbd-server.c
1 /*
2  * Network Block Device - server
3  *
4  * Copyright 1996-1998 Pavel Machek, distribute under GPL
5  *  <pavel@atrey.karlin.mff.cuni.cz>
6  *
7  * Version 1.0 - hopefully 64-bit-clean
8  * Version 1.1 - merging enhancements from Josh Parsons, <josh@coombs.anu.edu.au>
9  * Version 1.2 - autodetect size of block devices, thanx to Peter T. Breuer" <ptb@it.uc3m.es>
10  * Version 1.5 - can compile on Unix systems that don't have 64 bit integer
11  *      type, or don't have 64 bit file offsets by defining FS_32BIT
12  *      in compile options for nbd-server *only*. This can be done
13  *      with make FSCHOICE=-DFS_32BIT nbd-server. (I don't have the
14  *      original autoconf input file, or I would make it a configure
15  *      option.) Ken Yap <ken@nlc.net.au>.
16  */
17
18 #define VERSION "1.5"
19 #define GIGA (1*1024*1024*1024)
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/stat.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 <syslog.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <arpa/inet.h>
34 #include <strings.h>
35
36 #define _IO(a,b)
37 // #define ISSERVER
38 #define MY_NAME "nbd_server"
39
40 /* Authorization file should contain lines with IP addresses of 
41    clients authorized to use the server. If it does not exist,
42    access is permitted. */
43 #define AUTH_FILE "nbd_server.allow"
44
45 #include "cliserv.h"
46 #undef _IO
47 /* Deep magic: ioctl.h defines _IO macro (at least on linux) */
48
49
50 /* Debugging macros, now nothing goes to syslog unless you say ISSERVER */
51 #ifdef ISSERVER
52 #define msg2(a,b) syslog(a,b)
53 #define msg3(a,b,c) syslog(a,b,c)
54 #define msg4(a,b,c,d) syslog(a,b,c,d)
55 #else
56 #define msg2(a,b) do { fprintf(stderr,b) ; fputs("\n",stderr) ; } while(0) 
57 #define msg3(a,b,c) do { fprintf(stderr,b,c); fputs("\n",stderr) ; } while(0) 
58 #define msg4(a,b,c,d) do { fprintf(stderr,b,c,d); fputs("\n",stderr) ; } while(0)
59 #endif
60
61
62 #include <sys/ioctl.h>
63 #include <sys/mount.h>          /* For BLKGETSIZE */
64
65 #ifdef  FS_32BIT
66 typedef u32             fsoffset_t;
67 #define htonll          htonl
68 #define ntohll          ntohl
69 #else
70 typedef u64             fsoffset_t;
71 #endif
72
73
74 //#define DODBG
75 #ifdef DODBG
76 #define DEBUG( a ) printf( a )
77 #define DEBUG2( a,b ) printf( a,b )
78 #define DEBUG3( a,b,c ) printf( a,b,c )
79 #else
80 #define DEBUG( a )
81 #define DEBUG2( a,b ) 
82 #define DEBUG3( a,b,c ) 
83 #endif
84
85 #if     defined(HAVE_LLSEEK) && !defined(sun)
86 /* Solaris already has llseek defined in unistd.h */
87 extern long long llseek(unsigned int, long long, unsigned int);
88 #endif
89
90 void serveconnection(int net)  ;
91 void set_peername(int net,char *clientname) ;
92
93 #define LINELEN 256 
94 char difffilename[256] ;
95
96 int authorized_client(char *name)
97 /* 0 - authorization refused, 1 - OK 
98   authorization file contains one line per machine, no wildcards
99 */
100 { FILE *f ;
101    
102   char line[LINELEN] ; 
103
104   if ((f=fopen(AUTH_FILE,"r"))==NULL)
105     { msg4(LOG_INFO,"Can't open authorization file %s (%s).",
106            AUTH_FILE,strerror(errno)) ;
107       return 1 ; 
108     }
109   
110   while (fgets(line,LINELEN,f)!=NULL) {
111     if (strncmp(line,name,strlen(name))==0) { fclose(f)  ; return 1 ; }
112   }
113   fclose(f) ;
114   return 0 ;
115 }
116
117
118 inline void readit(int f, void *buf, int len)
119 {
120         int res;
121         while (len > 0) {
122                 DEBUG("*");
123                 if ((res = read(f, buf, len)) <= 0)
124                         err("Read failed: %m");
125                 len -= res;
126                 buf += res;
127         }
128 }
129
130 inline void writeit(int f, void *buf, int len)
131 {
132         int res;
133         while (len > 0) {
134                 DEBUG("+");
135                 if ((res = write(f, buf, len)) <= 0)
136                         err("Write failed: %m");
137                 len -= res;
138                 buf += res;
139         }
140 }
141
142 int port;                       /* Port I'm listening at */
143 char *exportname;               /* File I'm exporting */
144 fsoffset_t exportsize = ~0, hunksize = ~0;      /* ...and its length */
145 int flags = 0;
146 int export[1024];
147 int difffile=-1 ;
148 u32 difffilelen=0 ; /* number of pages in difffile */
149 u32 *difmap=NULL ;
150 char clientname[256] ;
151
152
153 #define DIFFPAGESIZE 4096 /* diff file uses those chunks */
154
155 #define F_READONLY 1
156 #define F_MULTIFILE 2 
157 #define F_COPYONWRITE 4
158
159 void cmdline(int argc, char *argv[])
160 {
161         int i;
162
163         if (argc < 3) {
164                 printf("This is nbd-server version " VERSION "\n");     
165                 printf("Usage: port file_to_export [size][kKmM] [-r] [-m] [-c]\n"
166                        "        -r read only\n"
167                        "        -m multiple file\n"
168                        "        -c copy on write\n"
169                        "        if port is set to 0, stdin is used (for running from inetd)\n"
170                        "        if file_to_export contains '%%s', it is substituted with IP\n"
171                        "                address of machine trying to connect\n" );
172                 exit(0);
173         }
174         port = atoi(argv[1]);
175         for (i = 3; i < argc; i++) {
176                 if (*argv[i] == '-') {
177                         switch (argv[i][1]) {
178                         case 'r':
179                                 flags |= F_READONLY;
180                                 break;
181                         case 'm':
182                                 flags |= F_MULTIFILE;
183                                 hunksize = 1*GIGA;
184                                 break;
185                         case 'c': flags |=F_COPYONWRITE ;
186                                 break ;
187                         }
188                 } else {
189                         fsoffset_t es;
190                         int last = strlen(argv[i])-1;
191                         char suffix = argv[i][last];
192                         if (suffix == 'k' || suffix == 'K' ||
193                             suffix == 'm' || suffix == 'M')
194                                 argv[i][last] = '\0';
195                         es = (fsoffset_t)atol(argv[i]);
196                         switch (suffix) {
197                                 case 'm':
198                                 case 'M':  es <<= 10;
199                                 case 'k':
200                                 case 'K':  es <<= 10;
201                                 default :  break;
202                         }
203                         exportsize = es;
204                 }
205         }
206
207         exportname = argv[2];
208 }
209
210 void connectme(int port)
211 {
212         struct sockaddr_in addrin;
213         int addrinlen = sizeof(addrin);
214         int net, sock, newpid;
215 #ifndef sun
216         int yes=1;
217 #else
218         char yes='1';
219 #endif
220
221         if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
222                 err("socket: %m");
223
224         /* lose the pesky "Address already in use" error message */
225         if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
226                 err("setsockopt");
227         }
228
229         DEBUG("Waiting for connections... bind, ");
230         addrin.sin_family = AF_INET;
231         addrin.sin_port = htons(port);
232         addrin.sin_addr.s_addr = 0;
233         if (bind(sock, (struct sockaddr *) &addrin, addrinlen) < 0)
234                 err("bind: %m");
235         DEBUG("listen, ");
236         if (listen(sock, 1) < 0)
237                 err("listen: %m");
238         DEBUG("accept, ");
239         for(;;) { /* infinite loop */
240           if ((net = accept(sock, (struct sockaddr *) &addrin, &addrinlen)) < 0)
241             err("accept: %m");
242
243           set_peername(net,clientname) ;
244           if (!authorized_client(clientname)) {
245             msg2(LOG_INFO,"Unauthorized client") ;
246             close(net) ;
247             continue ;
248           }
249           msg2(LOG_INFO,"Authorized client") ;
250           if ((newpid=fork())<0) {
251             msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
252             close(net) ;
253             continue ;
254           }
255           if (newpid>0) { /* parent */
256             close(net) ; continue ; }
257           /* child */
258           close(sock) ;
259           msg2(LOG_INFO,"Starting to serve") ;
260           serveconnection(net) ;        
261         }
262 }
263
264 #define SEND writeit( net, &reply, sizeof( reply ));
265 #define ERROR { reply.error = htonl(-1); SEND; reply.error = 0; lastpoint = -1; }
266
267 fsoffset_t lastpoint = -1;
268
269 void maybeseek(int handle, fsoffset_t a)
270 {
271         if (a > exportsize)
272                 err("Can not happen\n");
273         if (lastpoint != a) {
274 #if     defined(HAVE_LLSEEK) && !defined(FS_32BIT)
275                 if (llseek(handle, a, SEEK_SET) < 0)
276 #else
277                 if (lseek(handle, (long)a, SEEK_SET) < 0)
278 #endif
279                         err("Can not seek locally!\n");
280                 lastpoint = a;
281         } else {
282                 DEBUG("@");
283         }
284 }
285
286 void myseek(int handle,fsoffset_t a)
287 {
288 #if HAVE_LLSEEK && !defined(FS_32BIT)
289   if (llseek(handle, a, SEEK_SET) < 0)
290 #else
291   if (lseek(handle, (long)a, SEEK_SET) < 0)
292 #endif 
293     err("Can not seek locally!\n");
294 }
295
296 char pagebuf[DIFFPAGESIZE] ;
297
298
299 int rawexpread(fsoffset_t a, char *buf, int len)
300 {
301   maybeseek(export[a/hunksize], a%hunksize);
302   return (read(export[a/hunksize], buf, len) != len);
303 }
304
305 int expread(fsoffset_t a, char *buf, int len)
306 { int rdlen ; fsoffset_t mapcnt,mapl,maph ;
307   fsoffset_t pagestart; int offset ;
308  
309   if (flags & F_COPYONWRITE) {
310     DEBUG3("Asked to read %d bytes at %lu.\n",len,(unsigned long)a) ;
311
312     mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
313
314     for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
315       pagestart=mapcnt*DIFFPAGESIZE ;
316       offset=a-pagestart ;
317       rdlen=(len<DIFFPAGESIZE-offset) ? len : DIFFPAGESIZE-offset ;
318       if (difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
319         DEBUG3("Page %d is at %ld\n",(int)mapcnt,(long int)difmap[mapcnt]) ;
320         myseek(difffile,difmap[mapcnt]*DIFFPAGESIZE+offset) ;
321         if (read(difffile, buf, rdlen) != rdlen) return -1 ;
322       } else { /* the block is not there */
323         DEBUG2("Page %d is not here, we read the original one\n",
324               (int)mapcnt) ;
325         if (rawexpread(a,buf,rdlen)) return -1 ;
326       }
327       len-=rdlen ; a+=rdlen ; buf+=rdlen ;
328     }
329   } else return rawexpread(a,buf,len) ;
330   return 0 ;
331 }
332
333 int rawexpwrite(fsoffset_t a, char *buf, int len)
334 {
335         maybeseek(export[a/hunksize], a%hunksize);
336         return (write(export[a/hunksize], buf, len) != len);
337 }
338
339
340 int expwrite(fsoffset_t a, char *buf, int len)
341 {  u32 mapcnt,mapl,maph ; int wrlen,rdlen ; 
342    fsoffset_t pagestart ; int offset ;
343
344   if (flags & F_COPYONWRITE) {
345     DEBUG3("Asked to write %d bytes at %lu.\n",len,(unsigned long)a) ;
346
347     mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
348
349     for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
350       pagestart=mapcnt*DIFFPAGESIZE ;
351       offset=a-pagestart ;
352       wrlen=(len<DIFFPAGESIZE-offset) ? len : DIFFPAGESIZE-offset ;
353
354       if (difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
355         DEBUG3("Page %d is at %ld\n",mapcnt,(long)difmap[mapcnt]) ;
356         myseek(difffile,difmap[mapcnt]*DIFFPAGESIZE+offset) ;
357         if (write(difffile, buf, wrlen) != wrlen) return -1 ;
358       } else { /* the block is not there */
359         myseek(difffile,difffilelen*DIFFPAGESIZE) ;
360         difmap[mapcnt]=difffilelen++ ;
361         DEBUG3("Page %d is not here, we put it at %ld\n",
362               mapcnt,(long)difmap[mapcnt]) ;
363
364         rdlen=DIFFPAGESIZE ;
365         if (rdlen+pagestart%hunksize>hunksize) 
366           rdlen=hunksize-(pagestart%hunksize) ;
367         if (rawexpread(pagestart,pagebuf,rdlen)) return -1 ;
368         memcpy(pagebuf+offset,buf,wrlen) ;
369         if (write(difffile,pagebuf,DIFFPAGESIZE)!=DIFFPAGESIZE) return -1 ;
370       }                                             
371       len-=wrlen ; a+=wrlen ; buf+=wrlen ;
372     }
373   } else return(rawexpwrite(a,buf,len)); 
374   return 0 ;
375 }
376
377 int mainloop(int net)
378 {
379         struct nbd_request request;
380         struct nbd_reply reply;
381         char zeros[300];
382         int i = 0;
383         fsoffset_t size_host;
384
385         memset(zeros, 0, 290);
386         if (write(net, INIT_PASSWD, 8) < 0)
387                 err("Negotiation failed: %m");
388 #ifndef FS_32BIT
389         cliserv_magic = htonll(cliserv_magic);
390 #endif
391         if (write(net, &cliserv_magic, sizeof(cliserv_magic)) < 0)
392                 err("Negotiation failed: %m");
393         size_host = htonll(exportsize);
394 #ifdef  FS_32BIT
395         if (write(net, zeros, 4) < 0 || write(net, &size_host, 4) < 0)
396 #else
397         if (write(net, &size_host, 8) < 0)
398 #endif
399                 err("Negotiation failed: %m");
400         if (write(net, zeros, 128) < 0)
401                 err("Negotiation failed: %m");
402
403         DEBUG("Entering request loop!\n");
404         reply.magic = htonl(NBD_REPLY_MAGIC);
405         reply.error = 0;
406         while (1) {
407 #define BUFSIZE (1024*1024)
408                 char buf[BUFSIZE];
409                 int len;
410
411 #ifdef DODBG
412                 i++;
413                 printf("%d: ", i);
414 #endif
415
416                 readit(net, &request, sizeof(request));
417                 request.from = ntohll(request.from);
418                 request.type = ntohl(request.type);
419
420                 if (request.type==2) { /* Disconnect request */
421                   if (difmap) free(difmap) ;
422                   if (difffile>=0) { 
423                      close(difffile) ; unlink(difffilename) ; }
424                   err("Disconnect request received.") ;
425                 }
426
427                 len = ntohl(request.len);
428
429                 if (request.magic != htonl(NBD_REQUEST_MAGIC))
430                         err("Not enough magic.");
431                 if (len > BUFSIZE)
432                         err("Request too big!");
433 #ifdef DODBG
434                 printf("%s from %d (%d) len %d, ", (request.type ? "WRITE" : "READ"),
435                        (int) request.from, (int) request.from / 512, len);
436 #endif
437                 memcpy(reply.handle, request.handle, sizeof(reply.handle));
438                 if (((request.from + len) > exportsize) ||
439                     ((flags & F_READONLY) && request.type)) {
440                         DEBUG("[RANGE!]");
441                         ERROR;
442                         continue;
443                 }
444                 if (request.type==1) {  /* WRITE */
445                         DEBUG("wr: net->buf, ");
446                         readit(net, buf, len);
447                         DEBUG("buf->exp, ");
448                         if (expwrite(request.from, buf, len)) {
449                                 DEBUG("Write failed: %m" );
450                                 ERROR;
451                                 continue;
452                         }
453                         lastpoint += len;
454                         SEND;
455                         continue;
456                 }
457                 /* READ */
458
459                 DEBUG("exp->buf, ");
460                 if (expread(request.from, buf + sizeof(struct nbd_reply), len)) {
461                         lastpoint = -1;
462                         DEBUG("Read failed: %m");
463                         ERROR;
464                         continue;
465                 }
466                 lastpoint += len;
467
468                 DEBUG("buf->net, ");
469                 memcpy(buf, &reply, sizeof(struct nbd_reply));
470                 writeit(net, buf, len + sizeof(struct nbd_reply));
471                 DEBUG("OK!\n");
472         }
473 }
474
475 char exportname2[1024];
476
477 void set_peername(int net,char *clientname)
478 {
479         struct sockaddr_in addrin;
480         int addrinlen = sizeof( addrin );
481         char *peername ;
482
483         if (getpeername( net, (struct sockaddr *) &addrin, &addrinlen ) < 0)
484                 err("getsockname failed: %m");
485         peername = inet_ntoa(addrin.sin_addr);
486         sprintf(exportname2, exportname, peername);
487
488         msg4(LOG_INFO, "connect from %s, assigned file is %s", peername, exportname2);
489         strncpy(clientname,peername,255) ;
490 }
491
492 fsoffset_t size_autodetect(int export)
493 {
494         fsoffset_t es;
495         DEBUG("looking for export size with lseek SEEK_END\n");
496         if ((int)(es = lseek(export, 0, SEEK_END)) == -1 || es == 0) {
497                 struct stat stat_buf;
498                 int error;
499                 DEBUG("looking for export size with fstat\n");
500                 stat_buf.st_size = 0;
501                 if ((error = fstat(export, &stat_buf)) == -1 || stat_buf.st_size == 0 ) {
502                         DEBUG("looking for export size with ioctl BLKGETSIZE\n");
503 #ifdef BLKGETSIZE
504                         if(ioctl(export, BLKGETSIZE, &es) || es == 0) {
505 #else
506                         if(1){
507 #endif
508                                 err("Could not find size of exported block device: %m");
509                         } else {
510                                 es *= 512; /* assume blocksize 512 */
511                         }
512                 } else {
513                         es = stat_buf.st_size;
514                 }
515         }
516         return es;
517 }
518
519 int main(int argc, char *argv[])
520 {
521         int net;
522         fsoffset_t i;
523
524         if (sizeof( struct nbd_request )!=28) {
525                 fprintf(stderr,"Bad size of structure. Alignment problems?\n");
526                 exit(-1) ;
527         }
528         logging();
529         cmdline(argc, argv);
530         
531         if (!port) return 1 ;
532         connectme(port); /* serve infinitely */
533         return 0 ;
534 }
535
536
537 void serveconnection(int net) 
538 {   
539   u64 i ;
540
541   for (i=0; i<exportsize; i+=hunksize) {
542     char exportname3[1024];
543     
544     sprintf(exportname3, exportname2, i/hunksize);
545     printf( "Opening %s\n", exportname3 );
546     if ((export[i/hunksize] = open(exportname3, (flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1)
547       err("Could not open exported file: %m");
548     }
549         
550     if (exportsize == (u64)~0) {
551       exportsize = size_autodetect(export[0]);
552     }
553     if (exportsize > (~0UL >> 1))
554 #ifdef HAVE_LLSEEK
555     if ((exportsize >> 10) > (~0UL >> 1))
556       msg3(LOG_INFO, "size of exported file/device is %luMB",
557                (unsigned long)(exportsize >> 20));
558     else
559       msg3(LOG_INFO, "size of exported file/device is %luKB",
560            (unsigned long)(exportsize >> 10));
561 #else
562     err("Size of exported file is too big\n");
563 #endif
564     else
565       msg3(LOG_INFO, "size of exported file/device is %lu",
566                        (unsigned long)exportsize);
567
568     if (flags & F_COPYONWRITE) {
569       sprintf(difffilename,"%s-%s-%d.diff",exportname2,clientname,
570               (int)getpid()) ;
571       msg3(LOG_INFO,"About to create map and diff file %s",difffilename) ;
572       difffile=open(difffilename,O_RDWR | O_CREAT | O_TRUNC,0600) ;
573       if (difffile<0) err("Could not create diff file (%m)") ;
574       if ((difmap=calloc(exportsize/DIFFPAGESIZE,sizeof(u32)))==NULL)
575           err("Could not allocate memory") ;
576       for (i=0;i<exportsize/DIFFPAGESIZE;i++) difmap[i]=(u32)-1 ;         
577     }
578     
579     setmysockopt(net);
580       
581     mainloop(net);
582 }