File: split.c

package info (click to toggle)
nbdkit 1.42.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,696 kB
  • sloc: ansic: 59,224; sh: 16,793; makefile: 6,463; python: 1,837; cpp: 1,116; ml: 504; perl: 502; tcl: 62
file content (467 lines) | stat: -rw-r--r-- 11,142 bytes parent folder | download | duplicates (2)
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
/* nbdkit
 * Copyright Red Hat
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Red Hat nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <stdbool.h>
#include <pthread.h>

#include <nbdkit-plugin.h>

#include "cleanup.h"
#include "pread.h"
#include "pwrite.h"
#include "string-vector.h"
#include "utils.h"
#include "windows-compat.h"

/* The files. */
static string_vector filenames = empty_vector;

#ifdef SEEK_HOLE
/* Any callbacks using lseek must be protected by this lock. */
static pthread_mutex_t lseek_lock = PTHREAD_MUTEX_INITIALIZER;
#endif

static void
split_unload (void)
{
  string_vector_empty (&filenames);
}

static int
split_config (const char *key, const char *value)
{
  char *s;

  if (strcmp (key, "file") == 0) {
    s = nbdkit_realpath (value);
    if (s == NULL)
      return -1;
    if (string_vector_append (&filenames, s) == -1) {
      nbdkit_error ("realloc: %m");
      return -1;
    }
  }
  else {
    nbdkit_error ("unknown parameter '%s'", key);
    return -1;
  }

  return 0;
}

#define split_config_help \
  "file=<FILENAME>  (required) File(s) to serve."

/* The per-connection handle. */
struct handle {
  struct file *files;
  uint64_t size;                /* Total concatenated size. */
};

struct file {
  uint64_t offset, size;
  int fd;
  bool can_extents;
};

/* Create the per-connection handle. */
static void *
split_open (int readonly)
{
  struct handle *h;
  int flags;
  size_t i;
  uint64_t offset;
#ifdef SEEK_HOLE
  off_t r;
#endif

  h = malloc (sizeof *h);
  if (h == NULL) {
    nbdkit_error ("malloc: %m");
    return NULL;
  }

  h->files = malloc (filenames.len * sizeof (struct file));
  if (h->files == NULL) {
    nbdkit_error ("malloc: %m");
    free (h);
    return NULL;
  }
  for (i = 0; i < filenames.len; ++i)
    h->files[i].fd = -1;

  /* Open the files. */
  flags = O_CLOEXEC|O_NOCTTY;
  if (readonly)
    flags |= O_RDONLY;
  else
    flags |= O_RDWR;

  for (i = 0; i < filenames.len; ++i) {
    h->files[i].fd = open (filenames.ptr[i], flags);
    if (h->files[i].fd == -1) {
      nbdkit_error ("open: %s: %m", filenames.ptr[i]);
      goto err;
    }
  }

  offset = 0;
  for (i = 0; i < filenames.len; ++i) {
    h->files[i].offset = offset;

    h->files[i].size = device_size (h->files[i].fd, NULL);
    if (h->files[i].size == -1) {
      nbdkit_error ("%s: device_size: %m", filenames.ptr[i]);
      goto err;
    }
    offset += h->files[i].size;

    nbdkit_debug ("file[%zu]=%s: offset=%" PRIu64 ", size=%" PRIu64,
                  i, filenames.ptr[i], h->files[i].offset, h->files[i].size);

#ifdef SEEK_HOLE
    /* Test if this file supports extents. */
    ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lseek_lock);
    r = lseek (h->files[i].fd, 0, SEEK_DATA);
    if (r == -1 && errno != ENXIO) {
      nbdkit_debug ("disabling extents: lseek on %s: %m", filenames.ptr[i]);
      h->files[i].can_extents = false;
    }
    else
      h->files[i].can_extents = true;
#else
    h->files[i].can_extents = false;
#endif
  }
  h->size = offset;
  nbdkit_debug ("total size=%" PRIu64, h->size);

  return h;

 err:
  for (i = 0; i < filenames.len; ++i) {
    if (h->files[i].fd >= 0)
      close (h->files[i].fd);
  }
  free (h->files);
  free (h);
  return NULL;
}

/* Free up the per-connection handle. */
static void
split_close (void *handle)
{
  struct handle *h = handle;
  size_t i;

  for (i = 0; i < filenames.len; ++i)
    close (h->files[i].fd);
  free (h->files);
  free (h);
}

#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS

/* Get the disk size. */
static int64_t
split_get_size (void *handle)
{
  struct handle *h = handle;

  return (int64_t) h->size;
}

static int
split_can_cache (void *handle)
{
  /* Prefer posix_fadvise(), but letting nbdkit call .pread on our
   * behalf also tends to work well for the local file system
   * cache.
   */
#if HAVE_POSIX_FADVISE
  return NBDKIT_FUA_NATIVE;
#else
  return NBDKIT_FUA_EMULATE;
#endif
}

/* Helper function to map the offset to the correct file. */
static int
compare_offset (const void *offsetp, const void *filep)
{
  const uint64_t offset = *(uint64_t *)offsetp;
  const struct file *file = (struct file *)filep;

  if (offset < file->offset) return -1;
  if (offset >= file->offset + file->size) return 1;
  return 0;
}

static struct file *
get_file (struct handle *h, uint64_t offset)
{
  return bsearch (&offset, h->files,
                  filenames.len, sizeof (struct file),
                  compare_offset);
}

/* Read data. */
static int
split_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
{
  struct handle *h = handle;

  while (count > 0) {
    struct file *file = get_file (h, offset);
    uint64_t foffs = offset - file->offset;
    uint64_t max;
    ssize_t r;

    max = file->size - foffs;
    if (max > count)
      max = count;

    r = pread (file->fd, buf, max, foffs);
    if (r == -1) {
      nbdkit_error ("pread: %m");
      return -1;
    }
    if (r == 0) {
      nbdkit_error ("pread: unexpected end of file");
      return -1;
    }
    buf += r;
    count -= r;
    offset += r;
  }

  return 0;
}

/* Write data to the file. */
static int
split_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset)
{
  struct handle *h = handle;

  while (count > 0) {
    struct file *file = get_file (h, offset);
    uint64_t foffs = offset - file->offset;
    uint64_t max;
    ssize_t r;

    max = file->size - foffs;
    if (max > count)
      max = count;

    r = pwrite (file->fd, buf, max, offset);
    if (r == -1) {
      nbdkit_error ("pwrite: %m");
      return -1;
    }
    buf += r;
    count -= r;
    offset += r;
  }

  return 0;
}

#if HAVE_POSIX_FADVISE
/* Caching. */
static int
split_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
{
  struct handle *h = handle;

  /* Cache is advisory, we don't care if this fails */
  while (count > 0) {
    struct file *file = get_file (h, offset);
    uint64_t foffs = offset - file->offset;
    uint64_t max;
    int r;

    max = file->size - foffs;
    if (max > count)
      max = count;

    r = posix_fadvise (file->fd, offset, max, POSIX_FADV_WILLNEED);
    if (r) {
      errno = r;
      nbdkit_error ("posix_fadvise: %m");
      return -1;
    }
    count -= r;
    offset += r;
  }

  return 0;
}
#endif /* HAVE_POSIX_FADVISE */

#ifdef SEEK_HOLE
static int64_t
do_extents (struct file *file, uint32_t count, uint64_t offset,
            bool req_one, struct nbdkit_extents *extents)
{
  int64_t r = 0;
  uint64_t end = offset + count;

  do {
    off_t pos;

    pos = lseek (file->fd, offset, SEEK_DATA);
    if (pos == -1) {
      if (errno == ENXIO) {
        /* The current man page does not describe this situation well,
         * but a proposed change to POSIX adds these words for ENXIO:
         * "or the whence argument is SEEK_DATA and the offset falls
         * within the final hole of the file."
         */
        pos = end;
      }
      else {
        nbdkit_error ("lseek: SEEK_DATA: %" PRIu64 ": %m", offset);
        return -1;
      }
    }

    /* We know there is a hole from offset to pos-1. */
    if (pos > offset) {
      if (nbdkit_add_extent (extents, offset + file->offset, pos - offset,
                             NBDKIT_EXTENT_HOLE | NBDKIT_EXTENT_ZERO) == -1)
        return -1;
      r += pos - offset;
      if (req_one)
        break;
    }

    offset = pos;
    if (offset >= end)
      break;

    pos = lseek (file->fd, offset, SEEK_HOLE);
    if (pos == -1) {
      nbdkit_error ("lseek: SEEK_HOLE: %" PRIu64 ": %m", offset);
      return -1;
    }

    /* We know there is data from offset to pos-1. */
    if (pos > offset) {
      if (nbdkit_add_extent (extents, offset + file->offset, pos - offset,
                             0 /* allocated data */) == -1)
        return -1;
      r += pos - offset;
      if (req_one)
        break;
    }

    offset = pos;
  } while (offset < end);

  return r;
}

static int
split_extents (void *handle, uint32_t count, uint64_t offset,
               uint32_t flags, struct nbdkit_extents *extents)
{
  struct handle *h = handle;
  const bool req_one = flags & NBDKIT_FLAG_REQ_ONE;

  while (count > 0) {
    struct file *file = get_file (h, offset);
    uint64_t foffs = offset - file->offset;
    uint64_t max;
    int64_t r;

    max = file->size - foffs;
    if (max > count)
      max = count;

    if (file->can_extents) {
      ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lseek_lock);
      max = r = do_extents (file, max, foffs, req_one, extents);
    }
    else
      r = nbdkit_add_extent (extents, offset, max, 0 /* allocated data */);
    if (r == -1)
      return -1;
    count -= max;
    offset += max;
    if (req_one)
      break;
  }

  return 0;
}
#endif /* SEEK_HOLE */

static struct nbdkit_plugin plugin = {
  .name              = "split",
  .version           = PACKAGE_VERSION,
  .unload            = split_unload,
  .config            = split_config,
  .config_help       = split_config_help,
  .magic_config_key  = "file",
  .open              = split_open,
  .close             = split_close,
  .get_size          = split_get_size,
  .can_cache         = split_can_cache,
  .pread             = split_pread,
  .pwrite            = split_pwrite,
#if HAVE_POSIX_FADVISE
  .cache             = split_cache,
#endif
#ifdef SEEK_HOLE
  .extents           = split_extents,
#endif
  /* In this plugin, errno is preserved properly along error return
   * paths from failed system calls.
   */
  .errno_is_preserved = 1,
};

NBDKIT_REGISTER_PLUGIN (plugin)