2 * Network Block Device - server
4 * Copyright 1996-1998 Pavel Machek, distribute under GPL
5 * <pavel@atrey.karlin.mff.cuni.cz>
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>.
19 #define GIGA (1*1024*1024*1024)
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/tcp.h>
25 #include <netinet/in.h> /* sockaddr_in, htons, in_addr */
26 #include <netdb.h> /* hostent, gethostby*, getservby* */
33 #include <arpa/inet.h>
38 #define MY_NAME "nbd_server"
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"
47 /* Deep magic: ioctl.h defines _IO macro (at least on linux) */
50 /* Debugging macros, now nothing goes to syslog unless you say 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)
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)
62 #include <sys/ioctl.h>
63 #include <sys/mount.h> /* For BLKGETSIZE */
66 typedef u32 fsoffset_t;
70 typedef u64 fsoffset_t;
76 #define DEBUG( a ) printf( a )
77 #define DEBUG2( a,b ) printf( a,b )
78 #define DEBUG3( a,b,c ) printf( a,b,c )
82 #define DEBUG3( a,b,c )
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);
90 void serveconnection(int net) ;
91 void set_peername(int net,char *clientname) ;
94 char difffilename[256] ;
96 int authorized_client(char *name)
97 /* 0 - authorization refused, 1 - OK
98 authorization file contains one line per machine, no wildcards
104 if ((f=fopen(AUTH_FILE,"r"))==NULL)
105 { msg4(LOG_INFO,"Can't open authorization file %s (%s).",
106 AUTH_FILE,strerror(errno)) ;
110 while (fgets(line,LINELEN,f)!=NULL) {
111 if (strncmp(line,name,strlen(name))==0) { fclose(f) ; return 1 ; }
118 inline void readit(int f, void *buf, int len)
123 if ((res = read(f, buf, len)) <= 0)
124 err("Read failed: %m");
130 inline void writeit(int f, void *buf, int len)
135 if ((res = write(f, buf, len)) <= 0)
136 err("Write failed: %m");
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 */
148 u32 difffilelen=0 ; /* number of pages in difffile */
150 char clientname[256] ;
153 #define DIFFPAGESIZE 4096 /* diff file uses those chunks */
156 #define F_MULTIFILE 2
157 #define F_COPYONWRITE 4
159 void cmdline(int argc, char *argv[])
164 printf("This is nbd-server version " VERSION "\n");
165 printf("Usage: port file_to_export [size][kKmM] [-r] [-m] [-c]\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" );
174 port = atoi(argv[1]);
175 for (i = 3; i < argc; i++) {
176 if (*argv[i] == '-') {
177 switch (argv[i][1]) {
182 flags |= F_MULTIFILE;
185 case 'c': flags |=F_COPYONWRITE ;
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]);
207 exportname = argv[2];
210 void connectme(int port)
212 struct sockaddr_in addrin;
213 int addrinlen = sizeof(addrin);
214 int net, sock, newpid;
221 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
224 /* lose the pesky "Address already in use" error message */
225 if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
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)
236 if (listen(sock, 1) < 0)
239 for(;;) { /* infinite loop */
240 if ((net = accept(sock, (struct sockaddr *) &addrin, &addrinlen)) < 0)
243 set_peername(net,clientname) ;
244 if (!authorized_client(clientname)) {
245 msg2(LOG_INFO,"Unauthorized client") ;
249 msg2(LOG_INFO,"Authorized client") ;
250 if ((newpid=fork())<0) {
251 msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
255 if (newpid>0) { /* parent */
256 close(net) ; continue ; }
259 msg2(LOG_INFO,"Starting to serve") ;
260 serveconnection(net) ;
264 #define SEND writeit( net, &reply, sizeof( reply ));
265 #define ERROR { reply.error = htonl(-1); SEND; reply.error = 0; lastpoint = -1; }
267 fsoffset_t lastpoint = -1;
269 void maybeseek(int handle, fsoffset_t a)
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)
277 if (lseek(handle, (long)a, SEEK_SET) < 0)
279 err("Can not seek locally!\n");
286 void myseek(int handle,fsoffset_t a)
288 #if HAVE_LLSEEK && !defined(FS_32BIT)
289 if (llseek(handle, a, SEEK_SET) < 0)
291 if (lseek(handle, (long)a, SEEK_SET) < 0)
293 err("Can not seek locally!\n");
296 char pagebuf[DIFFPAGESIZE] ;
299 int rawexpread(fsoffset_t a, char *buf, int len)
301 maybeseek(export[a/hunksize], a%hunksize);
302 return (read(export[a/hunksize], buf, len) != len);
305 int expread(fsoffset_t a, char *buf, int len)
306 { int rdlen ; fsoffset_t mapcnt,mapl,maph ;
307 fsoffset_t pagestart; int offset ;
309 if (flags & F_COPYONWRITE) {
310 DEBUG3("Asked to read %d bytes at %lu.\n",len,(unsigned long)a) ;
312 mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
314 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
315 pagestart=mapcnt*DIFFPAGESIZE ;
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",
325 if (rawexpread(a,buf,rdlen)) return -1 ;
327 len-=rdlen ; a+=rdlen ; buf+=rdlen ;
329 } else return rawexpread(a,buf,len) ;
333 int rawexpwrite(fsoffset_t a, char *buf, int len)
335 maybeseek(export[a/hunksize], a%hunksize);
336 return (write(export[a/hunksize], buf, len) != len);
340 int expwrite(fsoffset_t a, char *buf, int len)
341 { u32 mapcnt,mapl,maph ; int wrlen,rdlen ;
342 fsoffset_t pagestart ; int offset ;
344 if (flags & F_COPYONWRITE) {
345 DEBUG3("Asked to write %d bytes at %lu.\n",len,(unsigned long)a) ;
347 mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
349 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
350 pagestart=mapcnt*DIFFPAGESIZE ;
352 wrlen=(len<DIFFPAGESIZE-offset) ? len : DIFFPAGESIZE-offset ;
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]) ;
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 ;
371 len-=wrlen ; a+=wrlen ; buf+=wrlen ;
373 } else return(rawexpwrite(a,buf,len));
377 int mainloop(int net)
379 struct nbd_request request;
380 struct nbd_reply reply;
383 fsoffset_t size_host;
385 memset(zeros, 0, 290);
386 if (write(net, INIT_PASSWD, 8) < 0)
387 err("Negotiation failed: %m");
389 cliserv_magic = htonll(cliserv_magic);
391 if (write(net, &cliserv_magic, sizeof(cliserv_magic)) < 0)
392 err("Negotiation failed: %m");
393 size_host = htonll(exportsize);
395 if (write(net, zeros, 4) < 0 || write(net, &size_host, 4) < 0)
397 if (write(net, &size_host, 8) < 0)
399 err("Negotiation failed: %m");
400 if (write(net, zeros, 128) < 0)
401 err("Negotiation failed: %m");
403 DEBUG("Entering request loop!\n");
404 reply.magic = htonl(NBD_REPLY_MAGIC);
407 #define BUFSIZE (1024*1024)
416 readit(net, &request, sizeof(request));
417 request.from = ntohll(request.from);
418 request.type = ntohl(request.type);
420 if (request.type==2) { /* Disconnect request */
421 if (difmap) free(difmap) ;
423 close(difffile) ; unlink(difffilename) ; }
424 err("Disconnect request received.") ;
427 len = ntohl(request.len);
429 if (request.magic != htonl(NBD_REQUEST_MAGIC))
430 err("Not enough magic.");
432 err("Request too big!");
434 printf("%s from %d (%d) len %d, ", (request.type ? "WRITE" : "READ"),
435 (int) request.from, (int) request.from / 512, len);
437 memcpy(reply.handle, request.handle, sizeof(reply.handle));
438 if (((request.from + len) > exportsize) ||
439 ((flags & F_READONLY) && request.type)) {
444 if (request.type==1) { /* WRITE */
445 DEBUG("wr: net->buf, ");
446 readit(net, buf, len);
448 if (expwrite(request.from, buf, len)) {
449 DEBUG("Write failed: %m" );
460 if (expread(request.from, buf + sizeof(struct nbd_reply), len)) {
462 DEBUG("Read failed: %m");
469 memcpy(buf, &reply, sizeof(struct nbd_reply));
470 writeit(net, buf, len + sizeof(struct nbd_reply));
475 char exportname2[1024];
477 void set_peername(int net,char *clientname)
479 struct sockaddr_in addrin;
480 int addrinlen = sizeof( addrin );
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);
488 msg4(LOG_INFO, "connect from %s, assigned file is %s", peername, exportname2);
489 strncpy(clientname,peername,255) ;
492 fsoffset_t size_autodetect(int export)
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;
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");
504 if(ioctl(export, BLKGETSIZE, &es) || es == 0) {
508 err("Could not find size of exported block device: %m");
510 es *= 512; /* assume blocksize 512 */
513 es = stat_buf.st_size;
519 int main(int argc, char *argv[])
524 if (sizeof( struct nbd_request )!=28) {
525 fprintf(stderr,"Bad size of structure. Alignment problems?\n");
531 if (!port) return 1 ;
532 connectme(port); /* serve infinitely */
537 void serveconnection(int net)
541 for (i=0; i<exportsize; i+=hunksize) {
542 char exportname3[1024];
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");
550 if (exportsize == (u64)~0) {
551 exportsize = size_autodetect(export[0]);
553 if (exportsize > (~0UL >> 1))
555 if ((exportsize >> 10) > (~0UL >> 1))
556 msg3(LOG_INFO, "size of exported file/device is %luMB",
557 (unsigned long)(exportsize >> 20));
559 msg3(LOG_INFO, "size of exported file/device is %luKB",
560 (unsigned long)(exportsize >> 10));
562 err("Size of exported file is too big\n");
565 msg3(LOG_INFO, "size of exported file/device is %lu",
566 (unsigned long)exportsize);
568 if (flags & F_COPYONWRITE) {
569 sprintf(difffilename,"%s-%s-%d.diff",exportname2,clientname,
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 ;