1 /* nbdkit
2  * Copyright (C) 2013-2020 Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * * Neither the name of Red Hat nor the names of its contributors may be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef NBDKIT_COMMON_H
34 #define NBDKIT_COMMON_H
35 
36 #if !defined (NBDKIT_PLUGIN_H) && !defined (NBDKIT_FILTER_H)
37 #error this header file should not be directly included
38 #endif
39 
40 #include <stdarg.h>
41 #include <stdint.h>
42 #include <errno.h>
43 #include <sys/socket.h>
44 
45 #include <nbdkit-version.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #if defined(__GNUC__) || defined(__clang__)
52 #define ATTRIBUTE_FORMAT_PRINTF(fmtpos, argpos) \
53   __attribute__((__format__ (__printf__, fmtpos, argpos)))
54 #else
55 #define ATTRIBUTE_FORMAT_PRINTF(fmtpos, argpos)
56 #endif
57 
58 #define NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS     0
59 #define NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS    1
60 #define NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS        2
61 #define NBDKIT_THREAD_MODEL_PARALLEL                  3
62 
63 #define NBDKIT_FLAG_MAY_TRIM  (1<<0) /* Maps to !NBD_CMD_FLAG_NO_HOLE */
64 #define NBDKIT_FLAG_FUA       (1<<1) /* Maps to NBD_CMD_FLAG_FUA */
65 #define NBDKIT_FLAG_REQ_ONE   (1<<2) /* Maps to NBD_CMD_FLAG_REQ_ONE */
66 #define NBDKIT_FLAG_FAST_ZERO (1<<3) /* Maps to NBD_CMD_FLAG_FAST_ZERO */
67 
68 #define NBDKIT_FUA_NONE       0
69 #define NBDKIT_FUA_EMULATE    1
70 #define NBDKIT_FUA_NATIVE     2
71 
72 #define NBDKIT_CACHE_NONE     0
73 #define NBDKIT_CACHE_EMULATE  1
74 #define NBDKIT_CACHE_NATIVE   2
75 
76 #define NBDKIT_EXTENT_HOLE    (1<<0) /* Same as NBD_STATE_HOLE */
77 #define NBDKIT_EXTENT_ZERO    (1<<1) /* Same as NBD_STATE_ZERO */
78 
79 extern void nbdkit_error (const char *msg, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
80 extern void nbdkit_verror (const char *msg, va_list args)
81   ATTRIBUTE_FORMAT_PRINTF (1, 0);
82 extern void nbdkit_debug (const char *msg, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
83 extern void nbdkit_vdebug (const char *msg, va_list args)
84   ATTRIBUTE_FORMAT_PRINTF (1, 0);
85 
86 extern char *nbdkit_absolute_path (const char *path);
87 extern int64_t nbdkit_parse_size (const char *str);
88 extern int nbdkit_parse_bool (const char *str);
89 extern int nbdkit_parse_int (const char *what, const char *str,
90                              int *r);
91 extern int nbdkit_parse_unsigned (const char *what, const char *str,
92                                   unsigned *r);
93 extern int nbdkit_parse_int8_t (const char *what, const char *str,
94                                 int8_t *r);
95 extern int nbdkit_parse_uint8_t (const char *what, const char *str,
96                                  uint8_t *r);
97 extern int nbdkit_parse_int16_t (const char *what, const char *str,
98                                  int16_t *r);
99 extern int nbdkit_parse_uint16_t (const char *what, const char *str,
100                                   uint16_t *r);
101 extern int nbdkit_parse_int32_t (const char *what, const char *str,
102                                  int32_t *r);
103 extern int nbdkit_parse_uint32_t (const char *what, const char *str,
104                                   uint32_t *r);
105 extern int nbdkit_parse_int64_t (const char *what, const char *str,
106                                  int64_t *r);
107 extern int nbdkit_parse_uint64_t (const char *what, const char *str,
108                                   uint64_t *r);
109 extern int nbdkit_stdio_safe (void);
110 extern int nbdkit_read_password (const char *value, char **password);
111 extern char *nbdkit_realpath (const char *path);
112 extern int nbdkit_nanosleep (unsigned sec, unsigned nsec);
113 extern const char *nbdkit_export_name (void);
114 extern int nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen);
115 extern void nbdkit_shutdown (void);
116 
117 struct nbdkit_extents;
118 extern int nbdkit_add_extent (struct nbdkit_extents *,
119                               uint64_t offset, uint64_t length, uint32_t type);
120 
121 /* A static non-NULL pointer which can be used when you don't need a
122  * per-connection handle.
123  */
124 #define NBDKIT_HANDLE_NOT_NEEDED (&errno)
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #ifdef __cplusplus
131 #define NBDKIT_CXX_LANG_C extern "C"
132 #else
133 #define NBDKIT_CXX_LANG_C /* nothing */
134 #endif
135 
136 #endif /* NBDKIT_COMMON_H */
137