summaryrefslogtreecommitdiff
path: root/libstore/nbd.c
blob: df949a7801e362be05d1db34fa72c71f1f4de292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* "Network Block Device" store backend compatible with Linux `nbd' driver
   Copyright (C) 2001, 2002, 2008 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include "store.h"
#include <hurd.h>
#include <hurd/io.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>


// Avoid dragging in the resolver when linking statically.
#pragma weak gethostbyname


/* The nbd protocol, such as it is, is not really specified anywhere.
   These message layouts and constants were culled from the nbd-server and
   Linux kernel nbd module sources.  */

#define NBD_INIT_MAGIC		"NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53"

#define NBD_REQUEST_MAGIC	(htonl (0x25609513))
#define NBD_REPLY_MAGIC		(htonl (0x67446698))

#define NBD_IO_MAX		10240

struct nbd_startup
{
  char magic[16];		/* NBD_INIT_MAGIC */
  uint64_t size;		/* size in bytes, 64 bits in net order */
  char reserved[128];		/* zeros, we don't check it */
};

struct nbd_request
{
  uint32_t magic;		/* NBD_REQUEST_MAGIC */
  uint32_t type;		/* 0 read, 1 write, 2 disconnect */
  uint64_t handle;		/* returned in reply */
  uint64_t from;
  uint32_t len;
} __attribute__ ((packed));

struct nbd_reply
{
  uint32_t magic;		/* NBD_REPLY_MAGIC */
  uint32_t error;
  uint64_t handle;		/* value from request */
} __attribute__ ((packed));


/* i/o functions.  */

#if BYTE_ORDER == BIG_ENDIAN
# define htonll(x)	(x)
#elif BYTE_ORDER == LITTLE_ENDIAN
# include <byteswap.h>
# define htonll(x)	(bswap_64 (x))
#else
# error what endian?
#endif
#define ntohll htonll


static inline error_t
read_reply (struct store *store, uint64_t handle)
{
  struct nbd_reply reply;
  char *buf = (void *) &reply;
  mach_msg_type_number_t cc = sizeof reply;
  error_t err = io_read (store->port, &buf, &cc, -1, cc);
  if (err)
    return err;
  if (buf != (void *) &reply)
    {
      memcpy (&reply, buf, sizeof reply);
      munmap (buf, cc);
    }
  if (cc != sizeof reply)
    return EIO;
  if (reply.magic != NBD_REPLY_MAGIC)
    return EIO;
  if (reply.handle != handle)
    return EIO;
  if (reply.error != 0)
    return EIO;
  return 0;
}

static error_t
nbd_write (struct store *store,
	   store_offset_t addr, size_t index, const void *buf, size_t len,
	   size_t *amount)
{
  struct nbd_request req =
  {
    magic: NBD_REQUEST_MAGIC,
    type: htonl (1),		/* WRITE */
  };
  error_t err;
  vm_size_t cc;

  addr <<= store->log2_block_size;
  *amount = 0;

  do
    {
      size_t chunk = len < NBD_IO_MAX ? len : NBD_IO_MAX, nwrote;
      req.from = htonll (addr);
      req.len = htonl (chunk);

      err = io_write (store->port, (char *) &req, sizeof req, -1, &cc);
      if (err)
	return err;
      if (cc != sizeof req)
	return EIO;

      nwrote = 0;
      do
	{
	  err = io_write (store->port, (char *) buf, chunk - nwrote, -1, &cc);
	  if (err)
	    return err;
	  buf += cc;
	  nwrote += cc;
	} while (nwrote < chunk);

      err = read_reply (store, req.handle);
      if (err)
	return err;

      addr += chunk;
      *amount += chunk;
      len -= chunk;
    } while (len > 0);

  return 0;
}

static error_t
nbd_read (struct store *store,
	  store_offset_t addr, size_t index, size_t amount,
	  void **buf, size_t *len)
{
  struct nbd_request req =
  {
    magic: NBD_REQUEST_MAGIC,
    type: htonl (0),		/* READ */
  };
  error_t err;
  size_t ofs, chunk;
  char *databuf, *piecebuf;
  size_t databuflen;
  mach_msg_type_number_t piecelen;

  /* Send a request for the largest possible piece of remaining data and
     read the first piece of its reply into PIECEBUF, PIECELEN.  The amount
     requested can be found in CHUNK.  */
  inline error_t request_chunk (char **buf, mach_msg_type_number_t *len)
    {
      vm_size_t cc;

      chunk = (amount - ofs) < NBD_IO_MAX ? (amount - ofs) : NBD_IO_MAX;

      req.from = htonll (addr);
      req.len = htonl (chunk);

      /* Advance ADDR immediately, so it always points past what we've
	 already requested.  */
      addr += chunk;

      return (io_write (store->port, (char *) &req, sizeof req, -1, &cc) ?
	      : cc != sizeof req ? EIO
	      : read_reply (store, req.handle) ?
	      : io_read (store->port, buf, len, (off_t) -1, chunk));
    }

  addr <<= store->log2_block_size;

  /* Read the first piece, which can go directly into the caller's buffer.  */
  databuf = *buf;
  piecelen = databuflen = *len;
  err = request_chunk (&databuf, &piecelen);
  if (err)
    return err;
  if (databuflen >= amount)
    {
      /* That got it all.  We're done.  */
      *buf = databuf;
      *len = piecelen;
      return 0;
    }

  /* We haven't read the entire amount yet.  */
  ofs = 0;
  do
    {
      /* Account for what we just read.  */
      ofs += piecelen;
      chunk -= piecelen;
      if (ofs == amount)
	{
	  /* That got it all.  We're done.  */
	  *buf = databuf;
	  *len = ofs;
	  return 0;
	}

      /* Now we'll read another piece of the data, hopefully
	 into the latter part of the existing buffer.  */
      piecebuf = databuf + ofs;
      piecelen = databuflen - ofs;

      if (chunk > 0)
	/* We haven't finishing reading the last chunk we requested.  */
	err = io_read (store->port, &piecebuf, &piecelen,
		       (off_t) -1, chunk);
      else
	/* Request the next chunk from the server.  */
	err = request_chunk (&piecebuf, &piecelen);

      if (!err && piecebuf != databuf + ofs)
	{
	  /* Now we have two discontiguous pieces of the buffer.  */
	  size_t newbuflen = round_page (databuflen + piecelen);
	  char *newbuf = mmap (0, newbuflen,
			       PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
	  if (newbuf == MAP_FAILED)
	    {
	      err = errno;
	      break;
	    }
	  memcpy (newbuf, databuf, ofs);
	  memcpy (newbuf + ofs, piecebuf, piecelen);
	  if (databuf != *buf)
	    munmap (databuf, databuflen);
	  databuf = newbuf;
	  databuflen = newbuflen;
	}
    } while (! err);

  if (databuf != *buf)
    munmap (databuf, databuflen);
  return err;
}

static error_t
nbd_set_size (struct store *store, size_t newsize)
{
  return EOPNOTSUPP;
}



/* Setup hooks.  */

static error_t
nbd_decode (struct store_enc *enc, const struct store_class *const *classes,
	    struct store **store)
{
  return store_std_leaf_decode (enc, _store_nbd_create, store);
}

static error_t
nbd_open (const char *name, int flags,
	  const struct store_class *const *classes,
	  struct store **store)
{
  return store_nbd_open (name, flags, store);
}

static const char url_prefix[] = "nbd://";

/* Valid name syntax is [nbd://]HOSTNAME:PORT[/BLOCKSIZE].
   If "/BLOCKSIZE" is omitted, the block size is 1.  */
static error_t
nbd_validate_name (const char *name,
		   const struct store_class *const *classes)
{
  char *p, *endp;

  if (!strncmp (name, url_prefix, sizeof url_prefix - 1))
    name += sizeof url_prefix - 1;

  p = strchr (name, ':');
  if (p == 0)
    return EINVAL;
  endp = 0;
  strtoul (++p, &endp, 0);
  if (endp == 0 || endp == p)
    return EINVAL;
  switch (*endp)
    {
    default:
      return EINVAL;
    case '\0':
      break;
    case '/':
      p = endp + 1;
      strtoul (p, &endp, 0);
      if (endp == 0 || endp == p)
	return EINVAL;
      if (*endp != '\0')
	return EINVAL;
    }
  return 0;
}

static error_t
nbdopen (const char *name, int *mod_flags,
	 socket_t *sockport, size_t *blocksize, store_offset_t *size)
{
  int sock;
  struct sockaddr_in sin;
  const struct hostent *he;
  char **ap;
  struct nbd_startup ns;
  ssize_t cc;
  size_t ofs;
  unsigned long int port;
  char *hostname, *p, *endp;

  if (!strncmp (name, url_prefix, sizeof url_prefix - 1))
    name += sizeof url_prefix - 1;

  /* First we have to parse the store name to get the host name and TCP
     port number to connect to and the block size to use.  */

  hostname = strdupa (name);
  p = strchr (hostname, ':');

  if (p == 0)
    return EINVAL;
  *p++ = '\0';
  port = strtoul (p, &endp, 0);
  if (endp == 0 || endp == p || port > 0xffffUL)
    return EINVAL;
  switch (*endp)
    {
    default:
      return EINVAL;
    case '\0':
      *blocksize = 1;
      break;
    case '/':
      p = endp + 1;
      *blocksize = strtoul (p, &endp, 0);
      if (endp == 0 || endp == p)
	return EINVAL;
      if (*endp != '\0')
	return EINVAL;
    }

  /* Now look up the host name and get a TCP connection.  */

  he = gethostbyname (hostname);
  if (he == 0)			/* XXX emit an error message? */
    return errno;		/* XXX what value will this have? */

  sock = socket (PF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return errno;

  sin.sin_family = AF_INET;
  sin.sin_port = htons (port);
  for (ap = he->h_addr_list; *ap != 0; ++ap)
    {
      sin.sin_addr = *(const struct in_addr *) *ap;
      errno = 0;
      if (connect (sock, &sin, sizeof sin) == 0 || errno == ECONNREFUSED)
	break;
    }
  if (errno != 0)		/* last connect failed */
    {
      error_t err = errno;
      close (sock);
      return err;
    }

  /* Read the startup packet, which tells us the size of the store.  */
  ofs = 0;
  do {
    cc = read (sock, (char *) &ns + ofs, sizeof ns - ofs);
    if (cc < 0)
      {
	error_t err = errno;
	close (sock);
	return err;
      }
    ofs += cc;
  } while (cc > 0 && ofs < sizeof ns);

  if (ofs != sizeof ns
      || memcmp (ns.magic, NBD_INIT_MAGIC, sizeof ns.magic) != 0)
    {
      close (sock);
      return EGRATUITOUS;	/* ? */
    }

  *size = ntohll (ns.size);
  *sockport = getdport (sock);
  close (sock);

  return 0;
}

static void
nbdclose (struct store *store)
{
  if (store->port != MACH_PORT_NULL)
    {
      /* Send a disconnect message, but don't wait for a reply.  */
      struct nbd_request req =
      {
	magic: NBD_REQUEST_MAGIC,
	type: htonl (2),	/* disconnect */
      };
      vm_size_t cc;
      (void) io_write (store->port, (char *) &req, sizeof req, -1, &cc);

      /* Close the socket.  */
      mach_port_deallocate (mach_task_self (), store->port);
      store->port = MACH_PORT_NULL;
    }
}

static error_t
nbd_set_flags (struct store *store, int flags)
{
  if ((flags & ~STORE_INACTIVE) != 0)
    /* Trying to set flags we don't support.  */
    return EINVAL;

  nbdclose (store);
  store->flags |= STORE_INACTIVE;

  return 0;
}

static error_t
nbd_clear_flags (struct store *store, int flags)
{
  error_t err = 0;
  if ((flags & ~STORE_INACTIVE) != 0)
    err = EINVAL;
  err = store->name
    ? nbdopen (store->name, &store->flags,
	       &store->port, &store->block_size, &store->size)
    : ENOENT;
  if (! err)
    store->flags &= ~STORE_INACTIVE;
  return err;
}

const struct store_class store_nbd_class =
{
  STORAGE_NETWORK, "nbd",
  open: nbd_open,
  validate_name: nbd_validate_name,
  read: nbd_read,
  write: nbd_write,
  set_size: nbd_set_size,
  allocate_encoding: store_std_leaf_allocate_encoding,
  encode: store_std_leaf_encode,
  decode: nbd_decode,
  set_flags: nbd_set_flags, clear_flags: nbd_clear_flags,
};
STORE_STD_CLASS (nbd);

/* Create a store from an existing socket to an nbd server.
   The initial handshake has already been done.  */
error_t
_store_nbd_create (mach_port_t port, int flags, size_t block_size,
		   const struct store_run *runs, size_t num_runs,
		   struct store **store)
{
  return _store_create (&store_nbd_class,
			port, flags, block_size, runs, num_runs, 0, store);
}

/* Open a new store backed by the named nbd server.  */
error_t
store_nbd_open (const char *name, int flags, struct store **store)
{
  error_t err;
  socket_t sock;
  struct store_run run;
  size_t blocksize;

  run.start = 0;
  err = nbdopen (name, &flags, &sock, &blocksize, &run.length);
  if (!err)
    {
      run.length /= blocksize;
      err = _store_nbd_create (sock, flags, blocksize, &run, 1, store);
      if (! err)
	{
	  if (!strncmp (name, url_prefix, sizeof url_prefix - 1))
	    err = store_set_name (*store, name);
	  else
	    asprintf (&(*store)->name, "%s%s", url_prefix, name);
	  if (err)
	    store_free (*store);
	}
      if (err)
	mach_port_deallocate (mach_task_self (), sock);
    }
  return err;
}