1nbd_pwrite(3) LIBNBD nbd_pwrite(3)
2
3
4
6 nbd_pwrite - write to the NBD server
7
9 #include <libnbd.h>
10
11 int nbd_pwrite (
12 struct nbd_handle *h, const void *buf, size_t count,
13 uint64_t offset, uint32_t flags
14 );
15
17 Issue a write command to the NBD server, writing the data in "buf" to
18 the range starting at "offset" and ending at "offset" + "count" - 1.
19 NBD can only write all or nothing using this call. The call returns
20 when the command has been acknowledged by the server, or there is an
21 error. Note this will generally return an error if nbd_is_read_only(3)
22 is true.
23
24 Note that libnbd defaults to enforcing a maximum write buffer of the
25 lesser of 64MiB or any maximum payload size advertised by the server;
26 attempts to exceed this will generally result in a client-side "ERANGE"
27 error, rather than a server-side disconnection. The actual limit can
28 be learned with nbd_get_block_size(3).
29
30 The "flags" parameter may be 0 for no flags, or may contain
31 "LIBNBD_CMD_FLAG_FUA" meaning that the server should not return until
32 the data has been committed to permanent storage (if that is supported
33 - some servers cannot do this, see nbd_can_fua(3)).
34
35 By default, libnbd will reject attempts to use this function with
36 parameters that are likely to result in server failure, such as
37 requesting an unknown command flag. The nbd_set_strict_mode(3)
38 function can be used to alter which scenarios should await a server
39 reply rather than failing fast.
40
42 If the call is successful the function returns 0.
43
45 On error -1 is returned.
46
47 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
48 of the error.
49
50 The following parameters must not be NULL: "h", "buf". For more
51 information see "Non-NULL parameters" in libnbd(3).
52
54 The handle must be connected with the server, otherwise this call will
55 return an error.
56
58 This function first appeared in libnbd 1.0.
59
60 If you need to test if this function is available at compile time check
61 if the following macro is defined:
62
63 #define LIBNBD_HAVE_NBD_PWRITE 1
64
66 This example is also available as examples/reads-and-writes.c in the
67 libnbd source code.
68
69 /* This example shows how to do synchronous reads
70 * and writes randomly over the first megabyte of an
71 * NBD server. Note this will destroy any existing
72 * content on the NBD server.
73 *
74 * To test it with nbdkit and a RAM disk:
75 *
76 * nbdkit -U - memory 1M \
77 * --run './simple-reads-and-writes $unixsocket'
78 */
79
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <inttypes.h>
83 #include <assert.h>
84 #include <time.h>
85
86 #include <libnbd.h>
87
88 int
89 main (int argc, char *argv[])
90 {
91 struct nbd_handle *nbd;
92 char buf[512];
93 size_t i;
94 int64_t exportsize;
95 uint64_t offset;
96
97 srand (time (NULL));
98
99 if (argc != 2) {
100 fprintf (stderr, "%s socket\n", argv[0]);
101 exit (EXIT_FAILURE);
102 }
103
104 /* Create the libnbd handle. */
105 nbd = nbd_create ();
106 if (nbd == NULL) {
107 fprintf (stderr, "%s\n", nbd_get_error ());
108 exit (EXIT_FAILURE);
109 }
110
111 /* Connect to the NBD server over a
112 * Unix domain socket.
113 */
114 if (nbd_connect_unix (nbd, argv[1]) == -1) {
115 fprintf (stderr, "%s\n", nbd_get_error ());
116 exit (EXIT_FAILURE);
117 }
118
119 /* Get the size of the disk and check
120 * it's large enough.
121 */
122 exportsize = nbd_get_size (nbd);
123 if (exportsize == -1) {
124 fprintf (stderr, "%s\n", nbd_get_error ());
125 exit (EXIT_FAILURE);
126 }
127 assert (exportsize >= sizeof buf);
128
129 /* Check that the server is writable. */
130 if (nbd_is_read_only (nbd) == 1) {
131 fprintf (stderr, "%s: "
132 "error: this NBD export is read-only\n",
133 argv[0]);
134 exit (EXIT_FAILURE);
135 }
136
137 for (i = 0; i < sizeof buf; ++i)
138 buf[i] = rand ();
139
140 /* 1000 writes. */
141 for (i = 0; i < 1000; ++i) {
142 offset = rand () % (exportsize - sizeof buf);
143
144 if (nbd_pwrite (nbd, buf, sizeof buf,
145 offset, 0) == -1) {
146 fprintf (stderr, "%s\n", nbd_get_error ());
147 exit (EXIT_FAILURE);
148 }
149 }
150
151 /* 1000 reads and writes. */
152 for (i = 0; i < 1000; ++i) {
153 offset = rand () % (exportsize - sizeof buf);
154 if (nbd_pread (nbd, buf, sizeof buf,
155 offset, 0) == -1) {
156 fprintf (stderr, "%s\n", nbd_get_error ());
157 exit (EXIT_FAILURE);
158 }
159
160 offset = rand () % (exportsize - sizeof buf);
161 if (nbd_pwrite (nbd, buf, sizeof buf,
162 offset, 0) == -1) {
163 fprintf (stderr, "%s\n", nbd_get_error ());
164 exit (EXIT_FAILURE);
165 }
166 }
167
168 /* Sends a graceful shutdown to the server. */
169 if (nbd_shutdown (nbd, 0) == -1) {
170 fprintf (stderr, "%s\n", nbd_get_error ());
171 exit (EXIT_FAILURE);
172 }
173
174 nbd_close (nbd);
175
176 exit (EXIT_SUCCESS);
177 }
178
180 nbd_aio_pwrite(3), nbd_can_fua(3), nbd_create(3),
181 nbd_get_block_size(3), nbd_is_read_only(3), nbd_set_strict_mode(3),
182 libnbd(3).
183
185 Eric Blake
186
187 Richard W.M. Jones
188
190 Copyright Red Hat
191
193 This library is free software; you can redistribute it and/or modify it
194 under the terms of the GNU Lesser General Public License as published
195 by the Free Software Foundation; either version 2 of the License, or
196 (at your option) any later version.
197
198 This library is distributed in the hope that it will be useful, but
199 WITHOUT ANY WARRANTY; without even the implied warranty of
200 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
201 Lesser General Public License for more details.
202
203 You should have received a copy of the GNU Lesser General Public
204 License along with this library; if not, write to the Free Software
205 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
206 02110-1301 USA
207
208
209
210libnbd-1.16.5 2023-09-26 nbd_pwrite(3)