1nbd_aio_in_flight(3) LIBNBD nbd_aio_in_flight(3)
2
3
4
6 nbd_aio_in_flight - check how many aio commands are still in flight
7
9 #include <libnbd.h>
10
11 int nbd_aio_in_flight (struct nbd_handle *h);
12
14 Return the number of in-flight aio commands that are still awaiting a
15 response from the server before they can be retired. If this returns a
16 non-zero value when requesting a disconnect from the server (see
17 nbd_aio_disconnect(3) and nbd_shutdown(3)), libnbd does not try to wait
18 for those commands to complete gracefully; if the server strands
19 commands while shutting down, nbd_aio_command_completed(3) will report
20 those commands as failed with a status of "ENOTCONN".
21
23 This call returns an integer ≥ 0.
24
26 On error "-1" is returned.
27
28 Refer to "ERROR HANDLING" in libnbd(3) for how to get further details
29 of the error.
30
32 The handle must be connected with the server, or shut down, or dead,
33 otherwise this call will return an error.
34
36 This function first appeared in libnbd 1.0.
37
38 If you need to test if this function is available at compile time check
39 if the following macro is defined:
40
41 #define LIBNBD_HAVE_NBD_AIO_IN_FLIGHT 1
42
44 This example is also available as examples/aio-connect-read.c in the
45 libnbd source code.
46
47 /* This example shows how to use the AIO (asynchronous) low
48 * level API to connect to a server and read the disk.
49 *
50 * Here are a few ways to try this example:
51 *
52 * nbdkit -U - linuxdisk . \
53 * --run './aio-connect-read $unixsocket'
54 *
55 * nbdkit -U - floppy . \
56 * --run './aio-connect-read $unixsocket'
57 *
58 * nbdkit -U - pattern size=1M \
59 * --run './aio-connect-read $unixsocket'
60 */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <stdint.h>
65 #include <inttypes.h>
66 #include <errno.h>
67 #include <assert.h>
68
69 #include <libnbd.h>
70
71 #define NR_SECTORS 32
72 #define SECTOR_SIZE 512
73
74 struct data {
75 uint64_t offset;
76 char sector[SECTOR_SIZE];
77 };
78
79 static int
80 hexdump (void *user_data, int *error)
81 {
82 struct data *data = user_data;
83 FILE *pp;
84
85 if (*error) {
86 errno = *error;
87 perror ("failed to read");
88 exit (EXIT_FAILURE);
89 }
90
91 printf ("sector at offset 0x%" PRIx64 ":\n",
92 data->offset);
93 pp = popen ("hexdump -C", "w");
94 if (pp == NULL) {
95 perror ("popen: hexdump");
96 exit (EXIT_FAILURE);
97 }
98 fwrite (data->sector, SECTOR_SIZE, 1, pp);
99 pclose (pp);
100 printf ("\n");
101
102 /* Returning 1 from the callback automatically retires
103 * the command.
104 */
105 return 1;
106 }
107
108 static struct data data[NR_SECTORS];
109
110 int
111 main (int argc, char *argv[])
112 {
113 struct nbd_handle *nbd;
114 size_t i;
115
116 if (argc != 2) {
117 fprintf (stderr, "%s socket\n", argv[0]);
118 exit (EXIT_FAILURE);
119 }
120
121 /* Create the libnbd handle. */
122 nbd = nbd_create ();
123 if (nbd == NULL) {
124 fprintf (stderr, "%s\n", nbd_get_error ());
125 exit (EXIT_FAILURE);
126 }
127
128 /* Connect to the NBD server over a Unix domain socket.
129 * This only starts the connection.
130 */
131 if (nbd_aio_connect_unix (nbd, argv[1]) == -1) {
132 fprintf (stderr, "%s\n", nbd_get_error ());
133 exit (EXIT_FAILURE);
134 }
135
136 /* Wait for the connection to complete. The use of
137 * nbd_poll here is only as an example. You could also
138 * integrate this with poll(2), glib or another main
139 * loop. Read libnbd(3) and the source file lib/poll.c.
140 */
141 while (!nbd_aio_is_ready (nbd)) {
142 if (nbd_poll (nbd, -1) == -1) {
143 fprintf (stderr, "%s\n", nbd_get_error ());
144 exit (EXIT_FAILURE);
145 }
146 }
147
148 assert (nbd_get_size (nbd) >= NR_SECTORS * SECTOR_SIZE);
149
150 /* Issue read commands for the first NR sectors. */
151 for (i = 0; i < NR_SECTORS; ++i) {
152 data[i].offset = i * SECTOR_SIZE;
153
154 /* The callback (hexdump) is called when the command
155 * completes. The buffer must continue to exist while
156 * the command is running.
157 */
158 if (nbd_aio_pread (nbd, data[i].sector, SECTOR_SIZE,
159 data[i].offset,
160 (nbd_completion_callback) {
161 .callback = hexdump,
162 .user_data = &data[i],
163 }, 0) == -1) {
164 fprintf (stderr, "%s\n", nbd_get_error ());
165 exit (EXIT_FAILURE);
166 }
167 }
168
169 /* Run the main loop until all the commands have
170 * completed and retired. Again the use of nbd_poll
171 * here is only as an example.
172 */
173 while (nbd_aio_in_flight (nbd) > 0) {
174 if (nbd_poll (nbd, -1) == -1) {
175 fprintf (stderr, "%s\n", nbd_get_error ());
176 exit (EXIT_FAILURE);
177 }
178 }
179
180 /* Close the libnbd handle. */
181 nbd_close (nbd);
182
183 exit (EXIT_SUCCESS);
184 }
185
187 nbd_aio_command_completed(3), nbd_aio_disconnect(3), nbd_create(3),
188 nbd_shutdown(3), libnbd(3).
189
191 Eric Blake
192
193 Richard W.M. Jones
194
196 Copyright (C) 2019-2021 Red Hat Inc.
197
199 This library is free software; you can redistribute it and/or modify it
200 under the terms of the GNU Lesser General Public License as published
201 by the Free Software Foundation; either version 2 of the License, or
202 (at your option) any later version.
203
204 This library is distributed in the hope that it will be useful, but
205 WITHOUT ANY WARRANTY; without even the implied warranty of
206 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
207 Lesser General Public License for more details.
208
209 You should have received a copy of the GNU Lesser General Public
210 License along with this library; if not, write to the Free Software
211 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
212 02110-1301 USA
213
214
215
216libnbd-1.12.5 2022-07-10 nbd_aio_in_flight(3)