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 Note that libnbd currently enforces a maximum read buffer of 64MiB,
79 even if the server would permit a larger buffer in a single
80 transaction; attempts to exceed this will result in an "ERANGE" error.
81 The server may enforce a smaller limit, which can be learned with
82 nbd_get_block_size(3).
83
84 The "flags" parameter may be 0 for no flags, or may contain
85 "LIBNBD_CMD_FLAG_DF" meaning that the server should not reply with more
86 than one fragment (if that is supported - some servers cannot do this,
87 see nbd_can_df(3)). Libnbd does not validate that the server actually
88 obeys the flag.
89
90 Note that if this command fails, and nbd_get_pread_initialize(3)
91 returns true, then libnbd sanitized "buf", but it is unspecified
92 whether the contents of "buf" will read as zero or as partial results
93 from the server. If nbd_get_pread_initialize(3) returns false, then
94 libnbd did not sanitize "buf", and the contents are undefined on
95 failure.
96
97 By default, libnbd will reject attempts to use this function with
98 parameters that are likely to result in server failure, such as
99 requesting an unknown command flag. The nbd_set_strict_mode(3)
100 function can be used to alter which scenarios should await a server
101 reply rather than failing fast.
102
104 If the call is successful the function returns 0.
105
107 On error "-1" is returned.
108
109 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
110 of the error.
111
112 The following parameters must not be NULL: "h", "buf". For more
113 information see "Non-NULL parameters" in libnbd(3).
114
116 The handle must be connected with the server, otherwise this call will
117 return an error.
118
120 This function first appeared in libnbd 1.0.
121
122 If you need to test if this function is available at compile time check
123 if the following macro is defined:
124
125 #define LIBNBD_HAVE_NBD_PREAD_STRUCTURED 1
126
128 nbd_aio_pread_structured(3), nbd_can_df(3), nbd_create(3),
129 nbd_get_block_size(3), nbd_get_pread_initialize(3), nbd_pread(3),
130 nbd_set_pread_initialize(3), nbd_set_request_block_size(3),
131 nbd_set_strict_mode(3), libnbd(3).
132
134 Eric Blake
135
136 Richard W.M. Jones
137
139 Copyright (C) 2019-2021 Red Hat Inc.
140
142 This library is free software; you can redistribute it and/or modify it
143 under the terms of the GNU Lesser General Public License as published
144 by the Free Software Foundation; either version 2 of the License, or
145 (at your option) any later version.
146
147 This library is distributed in the hope that it will be useful, but
148 WITHOUT ANY WARRANTY; without even the implied warranty of
149 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
150 Lesser General Public License for more details.
151
152 You should have received a copy of the GNU Lesser General Public
153 License along with this library; if not, write to the Free Software
154 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
155 02110-1301 USA
156
157
158
159libnbd-1.14.2 2023-01-03 nbd_pread_structured(3)