nbd_pread_structured(3)

NAME
nbd_pread_structured - read from the NBD server

SYNOPSIS
 #include <libnbd.h>

 typedef struct {
   int (*callback) (void *user_data, const void *subbuf,
                    size_t count, uint64_t offset,
                    unsigned status, int *error);
   void *user_data;
   void (*free) (void *user_data);
 } nbd_chunk_callback;

 int nbd_pread_structured (
       struct nbd_handle *h, void *buf, size_t count,
       uint64_t offset, nbd_chunk_callback chunk_callback,
       uint32_t flags
     );

DESCRIPTION
Issue a read command to the NBD server for the range starting at "offset" and ending at "offset" + "count" - 1. The server's response may be subdivided into chunks which may arrive out of order before reassembly into the original buffer; the "chunk" callback is used for notification after each chunk arrives, and may perform additional sanity checking on the server's reply. The callback cannot call "nbd_*" APIs on the same handle since it holds the handle lock and will cause a deadlock. If the callback returns -1, and no earlier error has been detected, then the overall read command will fail with any non-zero value stored into the callback's "error" parameter (with a default of "EPROTO"); but any further chunks will still invoke the callback.

The "chunk" function is called once per chunk of data received, with the "user_data" passed to this function. The "subbuf" and "count" parameters represent the subset of the original buffer which has just been populated by results from the server (in C, "subbuf" always points within the original "buf"; but this guarantee may not extend to other language bindings). The "offset" parameter represents the absolute offset at which "subbuf" begins within the image (note that this is not the relative offset of "subbuf" within the original buffer "buf"). Changes to "error" on output are ignored unless the callback fails. The input meaning of the "error" parameter is controlled by the "status" parameter, which is one of

"LIBNBD_READ_DATA" = 1
    "subbuf" was populated with "count" bytes of data. On input, "error" contains the errno value of any earlier detected error, or zero.

"LIBNBD_READ_HOLE" = 2
    "subbuf" represents a hole, and contains "count" NUL bytes. On input, "error" contains the errno value of any earlier detected error, or zero.

"LIBNBD_READ_ERROR" = 3
    "count" is 0, so "subbuf" is unusable. On input, "error" contains the errno value reported by the server as occurring while reading that "offset", regardless if any earlier error has been detected.

Future NBD extensions may permit other values for "status", but those will not be returned to a client that has not opted in to requesting such extensions. If the server is non-compliant, it is possible for the "chunk" function to be called more times than you expect or with "count" 0 for "LIBNBD_READ_DATA" or "LIBNBD_READ_HOLE". It is also possible that the "chunk" function is not called at all (in particular, "LIBNBD_READ_ERROR" is used only when an error is associated with a particular offset, and not when the server reports a generic error), but you are guaranteed that the callback was called at least once if the overall read succeeds. Libnbd does not validate that the server obeyed the requirement that a read call must not have overlapping chunks and must not succeed without enough chunks to cover the entire request.

Note that libnbd currently enforces a maximum read buffer of 64MiB, even if the server would permit a larger buffer in a single transaction; attempts to exceed this will result in an "ERANGE" error. The server may enforce a smaller limit, which can be learned with nbd_get_block_size(3).

The "flags" parameter may be 0 for no flags, or may contain "LIBNBD_CMD_FLAG_DF" meaning that the server should not reply with more than one fragment (if that is supported - some servers cannot do this, see nbd_can_df(3)). Libnbd does not validate that the server actually obeys the flag.

Note that if this command fails, and nbd_get_pread_initialize(3) returns true, then libnbd sanitized "buf", but it is unspecified whether the contents of "buf" will read as zero or as partial results from the server. If nbd_get_pread_initialize(3) returns false, then libnbd did not sanitize "buf", and the contents are undefined on failure.

By default, libnbd will reject attempts to use this function with parameters that are likely to result in server failure, such as requesting an unknown command flag. The nbd_set_strict_mode(3) function can be used to alter which scenarios should await a server reply rather than failing fast.

RETURN VALUE
If the call is successful the function returns 0.

ERRORS
On error -1 is returned.

Refer to "ERROR HANDLING" in libnbd(3) for how to get further details of the error.

The following parameters must not be NULL: "h", "buf". For more information see "Non-NULL parameters" in libnbd(3).

HANDLE STATE
The handle must be connected with the server, otherwise this call will return an error.

VERSION
This function first appeared in libnbd 1.0.

If you need to test if this function is available at compile time check if the following macro is defined:

 #define LIBNBD_HAVE_NBD_PREAD_STRUCTURED 1

SEE ALSO
nbd_aio_pread_structured(3), nbd_can_df(3), nbd_create(3), nbd_get_block_size(3), nbd_get_pread_initialize(3), nbd_pread(3), nbd_set_pread_initialize(3), nbd_set_request_block_size(3), nbd_set_strict_mode(3), libnbd(3).

AUTHORS
Eric Blake

Richard W.M. Jones

COPYRIGHT
Copyright Red Hat

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

This library 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA