Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
file: Add extra information to --dump-plugin about punch holes / zero…
… range support.

On Linux:

  $ nbdkit file --dump-plugin
  [...]
  file_blksszget=yes
  file_blkzeroout=yes
  file_falloc_fl_punch_hole=yes
  file_falloc_fl_zero_range=yes

As an incidental change I moved the do_fallocate function closer to
where it is actually used.
  • Loading branch information
rwmjones committed Aug 21, 2018
1 parent c0e856a commit 10d4dcf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
47 changes: 33 additions & 14 deletions plugins/file/file.c
Expand Up @@ -62,20 +62,6 @@

static char *filename = NULL;

#if defined(FALLOC_FL_PUNCH_HOLE) || defined(FALLOC_FL_ZERO_RANGE)
static int
do_fallocate(int fd, int mode, off_t offset, off_t len)
{
int r = fallocate (fd, mode, offset, len);
if (r == -1 && errno == ENODEV) {
/* kernel 3.10 fails with ENODEV for block device. Kernel >= 4.9 fails
with EOPNOTSUPP in this case. Normalize errno to simplify callers. */
errno = EOPNOTSUPP;
}
return r;
}
#endif

static void
file_unload (void)
{
Expand Down Expand Up @@ -123,6 +109,24 @@ file_config_complete (void)
#define file_config_help \
"file=<FILENAME> (required) The filename to serve." \

/* Print some extra information about how the plugin was compiled. */
static void
file_dump_plugin (void)
{
#ifdef BLKSSZGET
printf ("file_blksszget=yes\n");
#endif
#ifdef BLKZEROOUT
printf ("file_blkzeroout=yes\n");
#endif
#ifdef FALLOC_FL_PUNCH_HOLE
printf ("file_falloc_fl_punch_hole=yes\n");
#endif
#ifdef FALLOC_FL_ZERO_RANGE
printf ("file_falloc_fl_zero_range=yes\n");
#endif
}

/* The per-connection handle. */
struct handle {
int fd;
Expand Down Expand Up @@ -296,6 +300,20 @@ file_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset)
return 0;
}

#if defined(FALLOC_FL_PUNCH_HOLE) || defined(FALLOC_FL_ZERO_RANGE)
static int
do_fallocate(int fd, int mode, off_t offset, off_t len)
{
int r = fallocate (fd, mode, offset, len);
if (r == -1 && errno == ENODEV) {
/* kernel 3.10 fails with ENODEV for block device. Kernel >= 4.9 fails
with EOPNOTSUPP in this case. Normalize errno to simplify callers. */
errno = EOPNOTSUPP;
}
return r;
}
#endif

/* Write zeroes to the file. */
static int
file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
Expand Down Expand Up @@ -438,6 +456,7 @@ static struct nbdkit_plugin plugin = {
.config = file_config,
.config_complete = file_config_complete,
.config_help = file_config_help,
.dump_plugin = file_dump_plugin,
.open = file_open,
.close = file_close,
.get_size = file_get_size,
Expand Down
32 changes: 32 additions & 0 deletions plugins/file/nbdkit-file-plugin.pod
Expand Up @@ -36,6 +36,38 @@ I<--filter=delay> in order to use these parameters.

=back

=head1 DUMP-PLUGIN OUTPUT

You can obtain extra information about how the file plugin was
compiled by doing:

nbdkit file --dump-plugin

Some of the fields which may appear are listed below. Note these are
for information only and may be changed or removed at any time in the
future.

=over 4

=item C<file_blksszget=yes>

=item C<file_blkzeroout=yes>

If both set, the plugin may be able to efficiently zero ranges of
block devices, where the driver and block device itself supports this.

=item C<file_falloc_fl_punch_hole=yes>

If set, the plugin may be able to punch holes (make sparse) files and
block devices.

=item C<file_falloc_fl_zero_range=yes>

If set, the plugin may be able to efficiently zero ranges of files and
block devices.

=back

=head1 SEE ALSO

L<nbdkit(1)>,
Expand Down

0 comments on commit 10d4dcf

Please sign in to comment.