Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
file: Add debug flag to debug zeroing behaviour.
Use:

  nbdkit -f -v -D file.zero=1 file file=foo

to add very verbose debugging of the zero method.
  • Loading branch information
rwmjones committed Aug 21, 2018
1 parent 10d4dcf commit 96a41cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 24 additions & 4 deletions plugins/file/file.c
Expand Up @@ -62,6 +62,8 @@

static char *filename = NULL;

int file_debug_zero; /* to enable: -D file.zero=1 */

static void
file_unload (void)
{
Expand Down Expand Up @@ -325,8 +327,12 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
if (h->can_punch_hole && may_trim) {
r = do_fallocate (h->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
offset, count);
if (r == 0)
if (r == 0) {
if (file_debug_zero)
nbdkit_debug ("h->can_punch_hole && may_trim: "
"zero succeeded using fallocate");
return 0;
}

if (errno != EOPNOTSUPP) {
nbdkit_error ("zero: %m");
Expand All @@ -340,8 +346,12 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
#ifdef FALLOC_FL_ZERO_RANGE
if (h->can_zero_range) {
r = do_fallocate (h->fd, FALLOC_FL_ZERO_RANGE, offset, count);
if (r == 0)
if (r == 0) {
if (file_debug_zero)
nbdkit_debug ("h->can_zero-range: "
"zero succeeded using fallocate");
return 0;
}

if (errno != EOPNOTSUPP) {
nbdkit_error ("zero: %m");
Expand All @@ -361,8 +371,12 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
offset, count);
if (r == 0) {
r = do_fallocate (h->fd, 0, offset, count);
if (r == 0)
if (r == 0) {
if (file_debug_zero)
nbdkit_debug ("h->can_punch_hole && h->can_fallocate: "
"zero succeeded using fallocate");
return 0;
}

if (errno != EOPNOTSUPP) {
nbdkit_error ("zero: %m");
Expand All @@ -387,8 +401,12 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
uint64_t range[2] = {offset, count};

r = ioctl (h->fd, BLKZEROOUT, &range);
if (r == 0)
if (r == 0) {
if (file_debug_zero)
nbdkit_debug ("h->can_zeroout && is_aligned: "
"zero succeeded using BLKZEROOUT");
return 0;
}

if (errno != ENOTTY) {
nbdkit_error ("zero: %m");
Expand All @@ -400,6 +418,8 @@ file_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
#endif

/* Trigger a fall back to writing */
if (file_debug_zero)
nbdkit_debug ("zero falling back to writing");
errno = EOPNOTSUPP;
return -1;
}
Expand Down
12 changes: 12 additions & 0 deletions plugins/file/nbdkit-file-plugin.pod
Expand Up @@ -68,6 +68,18 @@ block devices.

=back

=head1 DEBUG FLAG

=over 4

=item B<-D file.zero=1>

This enables very verbose debugging of the NBD zero request. This can
be used to tell if the file plugin is able to zero ranges in the file
or block device efficiently or not.

=back

=head1 SEE ALSO

L<nbdkit(1)>,
Expand Down

0 comments on commit 96a41cd

Please sign in to comment.