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;
216 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
219 DEBUG("Waiting for connections... bind, ");
220 addrin.sin_family = AF_INET;
221 addrin.sin_port = htons(port);
222 addrin.sin_addr.s_addr = 0;
223 if (bind(sock, (struct sockaddr *) &addrin, addrinlen) < 0)
226 if (listen(sock, 1) < 0)
229 for(;;) { /* infinite loop */
230 if ((net = accept(sock, (struct sockaddr *) &addrin, &addrinlen)) < 0)
233 set_peername(net,clientname) ;
234 if (!authorized_client(clientname)) {
235 msg2(LOG_INFO,"Unauthorized client") ;
239 msg2(LOG_INFO,"Authorized client") ;
240 if ((newpid=fork())<0) {
241 msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
245 if (newpid>0) { /* parent */
246 close(net) ; continue ; }
249 msg2(LOG_INFO,"Starting to serve") ;
250 serveconnection(net) ;
254 #define SEND writeit( net, &reply, sizeof( reply ));
255 #define ERROR { reply.error = htonl(-1); SEND; reply.error = 0; lastpoint = -1; }
257 fsoffset_t lastpoint = -1;
259 void maybeseek(int handle, fsoffset_t a)
262 err("Can not happen\n");
263 if (lastpoint != a) {
264 #if defined(HAVE_LLSEEK) && !defined(FS_32BIT)
265 if (llseek(handle, a, SEEK_SET) < 0)
267 if (lseek(handle, (long)a, SEEK_SET) < 0)
269 err("Can not seek locally!\n");
276 void myseek(int handle,fsoffset_t a)
278 #if HAVE_LLSEEK && !defined(FS_32BIT)
279 if (llseek(handle, a, SEEK_SET) < 0)
281 if (lseek(handle, (long)a, SEEK_SET) < 0)
283 err("Can not seek locally!\n");
286 char pagebuf[DIFFPAGESIZE] ;
289 int rawexpread(fsoffset_t a, char *buf, int len)
291 maybeseek(export[a/hunksize], a%hunksize);
292 return (read(export[a/hunksize], buf, len) != len);
295 int expread(fsoffset_t a, char *buf, int len)
296 { int rdlen ; fsoffset_t mapcnt,mapl,maph ;
297 fsoffset_t pagestart; int offset ;
299 if (flags & F_COPYONWRITE) {
300 DEBUG3("Asked to read %d bytes at %lu.\n",len,(unsigned long)a) ;
302 mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
304 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
305 pagestart=mapcnt*DIFFPAGESIZE ;
307 rdlen=(len<DIFFPAGESIZE-offset) ? len : DIFFPAGESIZE-offset ;
308 if (difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
309 DEBUG3("Page %d is at %ld\n",(int)mapcnt,(long int)difmap[mapcnt]) ;
310 myseek(difffile,difmap[mapcnt]*DIFFPAGESIZE+offset) ;
311 if (read(difffile, buf, rdlen) != rdlen) return -1 ;
312 } else { /* the block is not there */
313 DEBUG2("Page %d is not here, we read the original one\n",
315 if (rawexpread(a,buf,rdlen)) return -1 ;
317 len-=rdlen ; a+=rdlen ; buf+=rdlen ;
319 } else return rawexpread(a,buf,len) ;
323 int rawexpwrite(fsoffset_t a, char *buf, int len)
325 maybeseek(export[a/hunksize], a%hunksize);
326 return (write(export[a/hunksize], buf, len) != len);
330 int expwrite(fsoffset_t a, char *buf, int len)
331 { u32 mapcnt,mapl,maph ; int wrlen,rdlen ;
332 fsoffset_t pagestart ; int offset ;
334 if (flags & F_COPYONWRITE) {
335 DEBUG3("Asked to write %d bytes at %lu.\n",len,(unsigned long)a) ;
337 mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
339 for (mapcnt=mapl;mapcnt<=maph;mapcnt++) {
340 pagestart=mapcnt*DIFFPAGESIZE ;
342 wrlen=(len<DIFFPAGESIZE-offset) ? len : DIFFPAGESIZE-offset ;
344 if (difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
345 DEBUG3("Page %d is at %ld\n",mapcnt,(long)difmap[mapcnt]) ;
346 myseek(difffile,difmap[mapcnt]*DIFFPAGESIZE+offset) ;
347 if (write(difffile, buf, wrlen) != wrlen) return -1 ;
348 } else { /* the block is not there */
349 myseek(difffile,difffilelen*DIFFPAGESIZE) ;
350 difmap[mapcnt]=difffilelen++ ;
351 DEBUG3("Page %d is not here, we put it at %ld\n",
352 mapcnt,(long)difmap[mapcnt]) ;
355 if (rdlen+pagestart%hunksize>hunksize)
356 rdlen=hunksize-(pagestart%hunksize) ;
357 if (rawexpread(pagestart,pagebuf,rdlen)) return -1 ;
358 memcpy(pagebuf+offset,buf,wrlen) ;
359 if (write(difffile,pagebuf,DIFFPAGESIZE)!=DIFFPAGESIZE) return -1 ;
361 len-=wrlen ; a+=wrlen ; buf+=wrlen ;
363 } else return(rawexpwrite(a,buf,len));
367 int mainloop(int net)
369 struct nbd_request request;
370 struct nbd_reply reply;
373 fsoffset_t size_host;
375 memset(zeros, 0, 290);
376 if (write(net, INIT_PASSWD, 8) < 0)
377 err("Negotiation failed: %m");
379 cliserv_magic = htonll(cliserv_magic);
381 if (write(net, &cliserv_magic, sizeof(cliserv_magic)) < 0)
382 err("Negotiation failed: %m");
383 size_host = htonll(exportsize);
385 if (write(net, zeros, 4) < 0 || write(net, &size_host, 4) < 0)
387 if (write(net, &size_host, 8) < 0)
389 err("Negotiation failed: %m");
390 if (write(net, zeros, 128) < 0)
391 err("Negotiation failed: %m");
393 DEBUG("Entering request loop!\n");
394 reply.magic = htonl(NBD_REPLY_MAGIC);
397 #define BUFSIZE (1024*1024)
406 readit(net, &request, sizeof(request));
407 request.from = ntohll(request.from);
408 request.type = ntohl(request.type);
410 if (request.type==2) { /* Disconnect request */
411 if (difmap) free(difmap) ;
413 close(difffile) ; unlink(difffilename) ; }
414 err("Disconnect request received.") ;
417 len = ntohl(request.len);
419 if (request.magic != htonl(NBD_REQUEST_MAGIC))
420 err("Not enough magic.");
422 err("Request too big!");
424 printf("%s from %d (%d) len %d, ", (request.type ? "WRITE" : "READ"),
425 (int) request.from, (int) request.from / 512, len);
427 memcpy(reply.handle, request.handle, sizeof(reply.handle));
428 if (((request.from + len) > exportsize) ||
429 ((flags & F_READONLY) && request.type)) {
434 if (request.type==1) { /* WRITE */
435 DEBUG("wr: net->buf, ");
436 readit(net, buf, len);
438 if (expwrite(request.from, buf, len)) {
439 DEBUG("Write failed: %m" );
450 if (expread(request.from, buf + sizeof(struct nbd_reply), len)) {
452 DEBUG("Read failed: %m");
459 memcpy(buf, &reply, sizeof(struct nbd_reply));
460 writeit(net, buf, len + sizeof(struct nbd_reply));
465 char exportname2[1024];
467 void set_peername(int net,char *clientname)
469 struct sockaddr_in addrin;
470 int addrinlen = sizeof( addrin );
473 if (getpeername( net, (struct sockaddr *) &addrin, &addrinlen ) < 0)
474 err("getsockname failed: %m");
475 peername = inet_ntoa(addrin.sin_addr);
476 sprintf(exportname2, exportname, peername);
478 msg4(LOG_INFO, "connect from %s, assigned file is %s", peername, exportname2);
479 strncpy(clientname,peername,255) ;
482 fsoffset_t size_autodetect(int export)
485 DEBUG("looking for export size with lseek SEEK_END\n");
486 if ((int)(es = lseek(export, 0, SEEK_END)) == -1 || es == 0) {
487 struct stat stat_buf;
489 DEBUG("looking for export size with fstat\n");
490 stat_buf.st_size = 0;
491 if ((error = fstat(export, &stat_buf)) == -1 || stat_buf.st_size == 0 ) {
492 DEBUG("looking for export size with ioctl BLKGETSIZE\n");
494 if(ioctl(export, BLKGETSIZE, &es) || es == 0) {
498 err("Could not find size of exported block device: %m");
500 es *= 512; /* assume blocksize 512 */
503 es = stat_buf.st_size;
509 int main(int argc, char *argv[])
514 if (sizeof( struct nbd_request )!=28) {
515 fprintf(stderr,"Bad size of structure. Alignment problems?\n");
521 if (!port) return 1 ;
522 connectme(port); /* serve infinitely */
527 void serveconnection(int net)
531 for (i=0; i<exportsize; i+=hunksize) {
532 char exportname3[1024];
534 sprintf(exportname3, exportname2, i/hunksize);
535 printf( "Opening %s\n", exportname3 );
536 if ((export[i/hunksize] = open(exportname3, (flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1)
537 err("Could not open exported file: %m");
540 if (exportsize == (u64)~0) {
541 exportsize = size_autodetect(export[0]);
543 if (exportsize > (~0UL >> 1))
545 if ((exportsize >> 10) > (~0UL >> 1))
546 msg3(LOG_INFO, "size of exported file/device is %luMB",
547 (unsigned long)(exportsize >> 20));
549 msg3(LOG_INFO, "size of exported file/device is %luKB",
550 (unsigned long)(exportsize >> 10));
552 err("Size of exported file is too big\n");
555 msg3(LOG_INFO, "size of exported file/device is %lu",
556 (unsigned long)exportsize);
558 if (flags & F_COPYONWRITE) {
559 sprintf(difffilename,"%s-%s-%d.diff",exportname2,clientname,
561 msg3(LOG_INFO,"About to create map and diff file %s",difffilename) ;
562 difffile=open(difffilename,O_RDWR | O_CREAT | O_TRUNC,0600) ;
563 if (difffile<0) err("Could not create diff file (%m)") ;
564 if ((difmap=calloc(exportsize/DIFFPAGESIZE,sizeof(u32)))==NULL)
565 err("Could not allocate memory") ;
566 for (i=0;i<exportsize/DIFFPAGESIZE;i++) difmap[i]=(u32)-1 ;