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

HANDLE STATE

32       The handle must be connected with the server, or shut down, or dead,
33       otherwise this call will return an error.
34

VERSION

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

EXAMPLE

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 <assert.h>
67
68        #include <libnbd.h>
69
70        #define NR_SECTORS 32
71        #define SECTOR_SIZE 512
72
73        struct data {
74          uint64_t offset;
75          char sector[SECTOR_SIZE];
76        };
77
78        static int
79        hexdump (void *user_data, int *error)
80        {
81          struct data *data = user_data;
82          FILE *pp;
83
84          printf ("sector at offset 0x%" PRIx64 ":\n",
85                  data->offset);
86          pp = popen ("hexdump -C", "w");
87          if (pp == NULL) {
88            perror ("popen: hexdump");
89            exit (EXIT_FAILURE);
90          }
91          fwrite (data->sector, SECTOR_SIZE, 1, pp);
92          pclose (pp);
93          printf ("\n");
94
95          /* Returning 1 from the callback automatically retires
96           * the command.
97           */
98          return 1;
99        }
100
101        static struct data data[NR_SECTORS];
102
103        int
104        main (int argc, char *argv[])
105        {
106          struct nbd_handle *nbd;
107          size_t i;
108
109          if (argc != 2) {
110            fprintf (stderr, "%s socket\n", argv[0]);
111            exit (EXIT_FAILURE);
112          }
113
114          /* Create the libnbd handle. */
115          nbd = nbd_create ();
116          if (nbd == NULL) {
117            fprintf (stderr, "%s\n", nbd_get_error ());
118            exit (EXIT_FAILURE);
119          }
120
121          /* Connect to the NBD server over a Unix domain socket.
122           * This only starts the connection.
123           */
124          if (nbd_aio_connect_unix (nbd, argv[1]) == -1) {
125            fprintf (stderr, "%s\n", nbd_get_error ());
126            exit (EXIT_FAILURE);
127          }
128
129          /* Wait for the connection to complete.  The use of
130           * nbd_poll here is only as an example.  You could also
131           * integrate this with poll(2), glib or another main
132           * loop.  Read libnbd(3) and the source file lib/poll.c.
133           */
134          while (!nbd_aio_is_ready (nbd)) {
135            if (nbd_poll (nbd, -1) == -1) {
136              fprintf (stderr, "%s\n", nbd_get_error ());
137              exit (EXIT_FAILURE);
138            }
139          }
140
141          assert (nbd_get_size (nbd) >= NR_SECTORS * SECTOR_SIZE);
142
143          /* Issue read commands for the first NR sectors. */
144          for (i = 0; i < NR_SECTORS; ++i) {
145            data[i].offset = i * SECTOR_SIZE;
146
147            /* The callback (hexdump) is called when the command
148             * completes.  The buffer must continue to exist while
149             * the command is running.
150             */
151            if (nbd_aio_pread (nbd, data[i].sector, SECTOR_SIZE,
152                               data[i].offset,
153                               (nbd_completion_callback) {
154                                 .callback = hexdump,
155                                 .user_data = &data[i],
156                               }, 0) == -1) {
157              fprintf (stderr, "%s\n", nbd_get_error ());
158              exit (EXIT_FAILURE);
159            }
160          }
161
162          /* Run the main loop until all the commands have
163           * completed and retired.  Again the use of nbd_poll
164           * here is only as an example.
165           */
166          while (nbd_aio_in_flight (nbd) > 0) {
167            if (nbd_poll (nbd, -1) == -1) {
168              fprintf (stderr, "%s\n", nbd_get_error ());
169              exit (EXIT_FAILURE);
170            }
171          }
172
173          /* Close the libnbd handle. */
174          nbd_close (nbd);
175
176          exit (EXIT_SUCCESS);
177        }
178

SEE ALSO

180       nbd_aio_command_completed(3), nbd_aio_disconnect(3), nbd_create(3),
181       nbd_shutdown(3), libnbd(3).
182

AUTHORS

184       Eric Blake
185
186       Richard W.M. Jones
187
189       Copyright (C) 2019-2021 Red Hat Inc.
190

LICENSE

192       This library is free software; you can redistribute it and/or modify it
193       under the terms of the GNU Lesser General Public License as published
194       by the Free Software Foundation; either version 2 of the License, or
195       (at your option) any later version.
196
197       This library is distributed in the hope that it will be useful, but
198       WITHOUT ANY WARRANTY; without even the implied warranty of
199       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
200       Lesser General Public License for more details.
201
202       You should have received a copy of the GNU Lesser General Public
203       License along with this library; if not, write to the Free Software
204       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
205       02110-1301 USA
206
207
208
209libnbd-1.10.1                     2021-10-25              nbd_aio_in_flight(3)
Impressum