1nbd_aio_in_flight(3)                LIBNBD                nbd_aio_in_flight(3)
2
3
4

NAME

6       nbd_aio_in_flight - check how many aio commands are still in flight
7

SYNOPSIS

9        #include <libnbd.h>
10
11        int nbd_aio_in_flight (struct nbd_handle *h);
12

DESCRIPTION

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

RETURN VALUE

23       This call returns an integer ≥ 0.
24

ERRORS

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
31       The following parameters must not be NULL: "h".  For more information
32       see "Non-NULL parameters" in libnbd(3).
33

HANDLE STATE

35       The handle must be connected with the server, or shut down, or dead,
36       otherwise this call will return an error.
37

VERSION

39       This function first appeared in libnbd 1.0.
40
41       If you need to test if this function is available at compile time check
42       if the following macro is defined:
43
44        #define LIBNBD_HAVE_NBD_AIO_IN_FLIGHT 1
45

EXAMPLE

47       This example is also available as examples/aio-connect-read.c in the
48       libnbd source code.
49
50        /* This example shows how to use the AIO (asynchronous) low
51         * level API to connect to a server and read the disk.
52         *
53         * Here are a few ways to try this example:
54         *
55         * nbdkit -U - linuxdisk . \
56         *   --run './aio-connect-read $unixsocket'
57         *
58         * nbdkit -U - floppy . \
59         *   --run './aio-connect-read $unixsocket'
60         *
61         * nbdkit -U - pattern size=1M \
62         *   --run './aio-connect-read $unixsocket'
63         */
64
65        #include <stdio.h>
66        #include <stdlib.h>
67        #include <stdint.h>
68        #include <inttypes.h>
69        #include <errno.h>
70        #include <assert.h>
71
72        #include <libnbd.h>
73
74        #define NR_SECTORS 32
75        #define SECTOR_SIZE 512
76
77        struct data {
78          uint64_t offset;
79          char sector[SECTOR_SIZE];
80        };
81
82        static int
83        hexdump (void *user_data, int *error)
84        {
85          struct data *data = user_data;
86          FILE *pp;
87
88          if (*error) {
89            errno = *error;
90            perror ("failed to read");
91            exit (EXIT_FAILURE);
92          }
93
94          printf ("sector at offset 0x%" PRIx64 ":\n",
95                  data->offset);
96          pp = popen ("hexdump -C", "w");
97          if (pp == NULL) {
98            perror ("popen: hexdump");
99            exit (EXIT_FAILURE);
100          }
101          fwrite (data->sector, SECTOR_SIZE, 1, pp);
102          pclose (pp);
103          printf ("\n");
104
105          /* Returning 1 from the callback automatically retires
106           * the command.
107           */
108          return 1;
109        }
110
111        static struct data data[NR_SECTORS];
112
113        int
114        main (int argc, char *argv[])
115        {
116          struct nbd_handle *nbd;
117          size_t i;
118
119          if (argc != 2) {
120            fprintf (stderr, "%s socket\n", argv[0]);
121            exit (EXIT_FAILURE);
122          }
123
124          /* Create the libnbd handle. */
125          nbd = nbd_create ();
126          if (nbd == NULL) {
127            fprintf (stderr, "%s\n", nbd_get_error ());
128            exit (EXIT_FAILURE);
129          }
130
131          /* Connect to the NBD server over a Unix domain socket.
132           * This only starts the connection.
133           */
134          if (nbd_aio_connect_unix (nbd, argv[1]) == -1) {
135            fprintf (stderr, "%s\n", nbd_get_error ());
136            exit (EXIT_FAILURE);
137          }
138
139          /* Wait for the connection to complete.  The use of
140           * nbd_poll here is only as an example.  You could also
141           * integrate this with poll(2), glib or another main
142           * loop.  Read libnbd(3) and the source file lib/poll.c.
143           */
144          while (!nbd_aio_is_ready (nbd)) {
145            if (nbd_poll (nbd, -1) == -1) {
146              fprintf (stderr, "%s\n", nbd_get_error ());
147              exit (EXIT_FAILURE);
148            }
149          }
150
151          assert (nbd_get_size (nbd) >= NR_SECTORS * SECTOR_SIZE);
152
153          /* Issue read commands for the first NR sectors. */
154          for (i = 0; i < NR_SECTORS; ++i) {
155            data[i].offset = i * SECTOR_SIZE;
156
157            /* The callback (hexdump) is called when the command
158             * completes.  The buffer must continue to exist while
159             * the command is running.
160             */
161            if (nbd_aio_pread (nbd, data[i].sector, SECTOR_SIZE,
162                               data[i].offset,
163                               (nbd_completion_callback) {
164                                 .callback = hexdump,
165                                 .user_data = &data[i],
166                               }, 0) == -1) {
167              fprintf (stderr, "%s\n", nbd_get_error ());
168              exit (EXIT_FAILURE);
169            }
170          }
171
172          /* Run the main loop until all the commands have
173           * completed and retired.  Again the use of nbd_poll
174           * here is only as an example.
175           */
176          while (nbd_aio_in_flight (nbd) > 0) {
177            if (nbd_poll (nbd, -1) == -1) {
178              fprintf (stderr, "%s\n", nbd_get_error ());
179              exit (EXIT_FAILURE);
180            }
181          }
182
183          /* Close the libnbd handle. */
184          nbd_close (nbd);
185
186          exit (EXIT_SUCCESS);
187        }
188

SEE ALSO

190       nbd_aio_command_completed(3), nbd_aio_disconnect(3), nbd_create(3),
191       nbd_shutdown(3), libnbd(3).
192

AUTHORS

194       Eric Blake
195
196       Richard W.M. Jones
197
199       Copyright (C) 2019-2021 Red Hat Inc.
200

LICENSE

202       This library is free software; you can redistribute it and/or modify it
203       under the terms of the GNU Lesser General Public License as published
204       by the Free Software Foundation; either version 2 of the License, or
205       (at your option) any later version.
206
207       This library is distributed in the hope that it will be useful, but
208       WITHOUT ANY WARRANTY; without even the implied warranty of
209       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
210       Lesser General Public License for more details.
211
212       You should have received a copy of the GNU Lesser General Public
213       License along with this library; if not, write to the Free Software
214       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
215       02110-1301 USA
216
217
218
219libnbd-1.14.2                     2023-01-03              nbd_aio_in_flight(3)
Impressum