1nbd_pread_structured(3) LIBNBD nbd_pread_structured(3)
2
3
4
6 nbd_pread_structured - read from the NBD server
7
9 #include <libnbd.h>
10
11 typedef struct {
12 int (*callback) (void *user_data, const void *subbuf,
13 size_t count, uint64_t offset,
14 unsigned status, int *error);
15 void *user_data;
16 void (*free) (void *user_data);
17 } nbd_chunk_callback;
18
19 int nbd_pread_structured (struct nbd_handle *h, void *buf,
20 size_t count, uint64_t offset,
21 nbd_chunk_callback chunk_callback,
22 uint32_t flags);
23
25 Issue a read command to the NBD server for the range starting at
26 "offset" and ending at "offset" + "count" - 1. The server's response
27 may be subdivided into chunks which may arrive out of order before
28 reassembly into the original buffer; the "chunk" callback is used for
29 notification after each chunk arrives, and may perform additional
30 sanity checking on the server's reply. The callback cannot call "nbd_*"
31 APIs on the same handle since it holds the handle lock and will cause a
32 deadlock. If the callback returns "-1", and no earlier error has been
33 detected, then the overall read command will fail with any non-zero
34 value stored into the callback's "error" parameter (with a default of
35 "EPROTO"); but any further chunks will still invoke the callback.
36
37 The "chunk" function is called once per chunk of data received, with
38 the "user_data" passed to this function. The "subbuf" and "count"
39 parameters represent the subset of the original buffer which has just
40 been populated by results from the server (in C, "subbuf" always points
41 within the original "buf"; but this guarantee may not extend to other
42 language bindings). The "offset" parameter represents the absolute
43 offset at which "subbuf" begins within the image (note that this is not
44 the relative offset of "subbuf" within the original buffer "buf").
45 Changes to "error" on output are ignored unless the callback fails. The
46 input meaning of the "error" parameter is controlled by the "status"
47 parameter, which is one of
48
49 "LIBNBD_READ_DATA" = 1
50 "subbuf" was populated with "count" bytes of data. On input,
51 "error" contains the errno value of any earlier detected error, or
52 zero.
53
54 "LIBNBD_READ_HOLE" = 2
55 "subbuf" represents a hole, and contains "count" NUL bytes. On
56 input, "error" contains the errno value of any earlier detected
57 error, or zero.
58
59 "LIBNBD_READ_ERROR" = 3
60 "count" is 0, so "subbuf" is unusable. On input, "error" contains
61 the errno value reported by the server as occurring while reading
62 that "offset", regardless if any earlier error has been detected.
63
64 Future NBD extensions may permit other values for "status", but those
65 will not be returned to a client that has not opted in to requesting
66 such extensions. If the server is non-compliant, it is possible for the
67 "chunk" function to be called more times than you expect or with
68 "count" 0 for "LIBNBD_READ_DATA" or "LIBNBD_READ_HOLE". It is also
69 possible that the "chunk" function is not called at all (in particular,
70 "LIBNBD_READ_ERROR" is used only when an error is associated with a
71 particular offset, and not when the server reports a generic error),
72 but you are guaranteed that the callback was called at least once if
73 the overall read succeeds. Libnbd does not validate that the server
74 obeyed the requirement that a read call must not have overlapping
75 chunks and must not succeed without enough chunks to cover the entire
76 request.
77
78 The "flags" parameter may be 0 for no flags, or may contain
79 "LIBNBD_CMD_FLAG_DF" meaning that the server should not reply with more
80 than one fragment (if that is supported - some servers cannot do this,
81 see nbd_can_df(3)). Libnbd does not validate that the server actually
82 obeys the flag.
83
84 By default, libnbd will reject attempts to use this function with
85 parameters that are likely to result in server failure, such as
86 requesting an unknown command flag. The nbd_set_strict_mode(3)
87 function can be used to alter which scenarios should await a server
88 reply rather than failing fast.
89
91 If the call is successful the function returns 0.
92
94 On error "-1" is returned.
95
96 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
97 of the error.
98
100 The handle must be connected with the server, otherwise this call will
101 return an error.
102
104 This function first appeared in libnbd 1.0.
105
106 If you need to test if this function is available at compile time check
107 if the following macro is defined:
108
109 #define LIBNBD_HAVE_NBD_PREAD_STRUCTURED 1
110
112 nbd_aio_pread_structured(3), nbd_can_df(3), nbd_create(3),
113 nbd_get_block_size(3), nbd_pread(3), nbd_set_strict_mode(3), libnbd(3).
114
116 Eric Blake
117
118 Richard W.M. Jones
119
121 Copyright (C) 2019-2021 Red Hat Inc.
122
124 This library is free software; you can redistribute it and/or modify it
125 under the terms of the GNU Lesser General Public License as published
126 by the Free Software Foundation; either version 2 of the License, or
127 (at your option) any later version.
128
129 This library is distributed in the hope that it will be useful, but
130 WITHOUT ANY WARRANTY; without even the implied warranty of
131 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
132 Lesser General Public License for more details.
133
134 You should have received a copy of the GNU Lesser General Public
135 License along with this library; if not, write to the Free Software
136 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
137 02110-1301 USA
138
139
140
141libnbd-1.7.12 2021-05-29 nbd_pread_structured(3)