Rearrange things
[nbd.git] / nbdsrv.h
blobc0de263558b89e380ef13dfe9cbd11140a46d915
1 #ifndef NBDSRV_H
2 #define NBDSRV_H
4 #include "lfs.h"
6 #include <glib.h>
7 #include <stdbool.h>
8 #include <stdint.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include "nbd.h"
14 /* Structures */
16 /**
17 * Types of virtuatlization
18 **/
19 typedef enum {
20 VIRT_NONE=0, /**< No virtualization */
21 VIRT_IPLIT, /**< Literal IP address as part of the filename */
22 VIRT_IPHASH, /**< Replacing all dots in an ip address by a / before
23 doing the same as in IPLIT */
24 VIRT_CIDR, /**< Every subnet in its own directory */
25 } VIRT_STYLE;
27 /**
28 * Variables associated with a server.
29 **/
30 typedef struct {
31 gchar* exportname; /**< (unprocessed) filename of the file we're exporting */
32 uint64_t expected_size; /**< size of the exported file as it was told to
33 us through configuration */
34 gchar* listenaddr; /**< The IP address we're listening on */
35 char* authname; /**< filename of the authorization file */
36 int flags; /**< flags associated with this exported file */
37 VIRT_STYLE virtstyle;/**< The style of virtualization, if any */
38 uint8_t cidrlen; /**< The length of the mask when we use
39 CIDR-style virtualization */
40 gchar* prerun; /**< command to be ran after connecting a client,
41 but before starting to serve */
42 gchar* postrun; /**< command that will be ran after the client
43 disconnects */
44 gchar* servename; /**< name of the export as selected by nbd-client */
45 int max_connections; /**< maximum number of opened connections */
46 int numclients; /**< number of clients connected to this export */
47 gchar* transactionlog;/**< filename for transaction log */
48 gchar* cowdir; /**< directory for copy-on-write diff files. */
49 } SERVER;
51 /**
52 * Variables associated with a client connection
54 typedef struct _client {
55 uint64_t exportsize; /**< size of the file we're exporting */
56 char *clientname; /**< peer, in human-readable format */
57 struct sockaddr_storage clientaddr; /**< peer, in binary format, network byte order */
58 char *exportname; /**< (processed) filename of the file we're exporting */
59 GArray *export; /**< array of FILE_INFO of exported files;
60 array size is always 1 unless we're
61 doing the multiple file option */
62 pthread_rwlock_t export_lock;
63 int net; /**< The actual client socket */
64 SERVER *server; /**< The server this client is getting data from */
65 char* difffilename; /**< filename of the copy-on-write file, if any */
66 int difffile; /**< filedescriptor of copyonwrite file. @todo
67 shouldn't this be an array too? (cfr export) Or
68 make -m and -c mutually exclusive */
69 uint32_t difffilelen; /**< number of pages in difffile */
70 uint32_t *difmap; /**< see comment on the global difmap for this one */
71 gboolean modern; /**< client was negotiated using modern negotiation protocol */
72 int transactionlogfd;/**< fd for transaction log */
73 int clientfeats; /**< Features supported by this client */
74 pthread_mutex_t lock; /**< socket lock */
75 void *tls_session; /**< TLS session context. Is NULL unless STARTTLS
76 has been negotiated. */
77 int (*socket_read)(struct _client*, void* buf, size_t len);
78 int (*socket_write)(struct _client*, void* buf, size_t len);
79 void (*socket_closed)(struct _client*);
80 } CLIENT;
82 /**
83 * Variables associated with an open file
84 **/
85 typedef struct {
86 int fhandle; /**< file descriptor */
87 off_t startoff; /**< starting offset of this file */
88 } FILE_INFO;
90 /* Constants and macros */
92 /**
93 * Error domain common for all NBD server errors.
94 **/
95 #define NBDS_ERR g_quark_from_static_string("server-error-quark")
97 /**
98 * NBD server error codes.
99 **/
100 typedef enum {
101 NBDS_ERR_CFILE_NOTFOUND, /**< The configuration file is not found */
102 NBDS_ERR_CFILE_MISSING_GENERIC, /**< The (required) group "generic" is missing */
103 NBDS_ERR_CFILE_KEY_MISSING, /**< A (required) key is missing */
104 NBDS_ERR_CFILE_VALUE_INVALID, /**< A value is syntactically invalid */
105 NBDS_ERR_CFILE_VALUE_UNSUPPORTED, /**< A value is not supported in this build */
106 NBDS_ERR_CFILE_NO_EXPORTS, /**< A config file was specified that does not
107 define any exports */
108 NBDS_ERR_CFILE_INCORRECT_PORT, /**< The reserved port was specified for an
109 old-style export. */
110 NBDS_ERR_CFILE_DIR_UNKNOWN, /**< A directory requested does not exist*/
111 NBDS_ERR_CFILE_READDIR_ERR, /**< Error occurred during readdir() */
112 NBDS_ERR_CFILE_INVALID_SPLICE, /**< We can't use splice with the other options
113 specified for the export. */
114 NBDS_ERR_SO_LINGER, /**< Failed to set SO_LINGER to a socket */
115 NBDS_ERR_SO_REUSEADDR, /**< Failed to set SO_REUSEADDR to a socket */
116 NBDS_ERR_SO_KEEPALIVE, /**< Failed to set SO_KEEPALIVE to a socket */
117 NBDS_ERR_GAI, /**< Failed to get address info */
118 NBDS_ERR_SOCKET, /**< Failed to create a socket */
119 NBDS_ERR_BIND, /**< Failed to bind an address to socket */
120 NBDS_ERR_LISTEN, /**< Failed to start listening on a socket */
121 NBDS_ERR_SYS, /**< Underlying system call or library error */
122 NBDS_ERR_CFILE_INVALID_WAIT, /**< We can't use wait with the other options
123 specified for the export. */
124 } NBDS_ERRS;
127 * Logging macros.
129 * @todo remove this. We should use g_log in all cases, and use the
130 * logging mangler to redirect to syslog if and when necessary.
132 #ifdef ISSERVER
133 #define msg(prio, ...) syslog(prio, __VA_ARGS__)
134 #else
135 #define msg(prio, ...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
136 #endif
137 #define MY_NAME "nbd_server"
139 /** Per-export flags: */
140 #define F_READONLY 1 /**< flag to tell us a file is readonly */
141 #define F_MULTIFILE 2 /**< flag to tell us a file is exported using -m */
142 #define F_COPYONWRITE 4 /**< flag to tell us a file is exported using
143 copyonwrite */
144 #define F_AUTOREADONLY 8 /**< flag to tell us a file is set to autoreadonly */
145 #define F_SPARSE 16 /**< flag to tell us copyronwrite should use a sparse file */
146 #define F_SDP 32 /**< flag to tell us the export should be done using the Socket Direct Protocol for RDMA */
147 #define F_SYNC 64 /**< Whether to fsync() after a write */
148 #define F_FLUSH 128 /**< Whether server wants FLUSH to be sent by the client */
149 #define F_FUA 256 /**< Whether server wants FUA to be sent by the client */
150 #define F_ROTATIONAL 512 /**< Whether server wants the client to implement the elevator algorithm */
151 #define F_TEMPORARY 1024 /**< Whether the backing file is temporary and should be created then unlinked */
152 #define F_TRIM 2048 /**< Whether server wants TRIM (discard) to be sent by the client */
153 #define F_FIXED 4096 /**< Client supports fixed new-style protocol (and can thus send us extra options */
154 #define F_TREEFILES 8192 /**< flag to tell us a file is exported using -t */
155 #define F_FORCEDTLS 16384 /**< TLS is required, either for the server as a whole or for a given export */
156 #define F_SPLICE 32768 /**< flag to tell us to use splice for read/write operations */
157 #define F_WAIT 65536 /**< flag to tell us to wait for file creation */
159 /* Functions */
162 * Check whether a given address matches a given netmask.
164 * @param mask the address or netmask to check against, in ASCII representation
165 * @param addr the address to check
167 * @return true if the address matches the mask, false otherwise; in case of
168 * failure to parse netmask, returns false with err set appropriately.
169 * @todo decide what to do with v6-mapped IPv4 addresses.
171 bool address_matches(const char* mask, const struct sockaddr* addr, GError** err);
174 * Gets a byte to allow for address masking.
176 * @param masklen the length of the requested mask.
177 * @return if the length of the mask is 8 or longer, 0xFF. Otherwise, a byte
178 * with `masklen' number of leading bits set to 1, everything else set to 0.
180 uint8_t getmaskbyte(int masklen) G_GNUC_PURE;
183 * Check whether a client is allowed to connect. Works with an authorization
184 * file which contains one line per machine or network, with CIDR-style
185 * netmasks.
187 * @param opts The client who's trying to connect.
188 * @return 0 - authorization refused, 1 - OK
190 int authorized_client(CLIENT *opts);
193 * duplicate server
194 * @param s the old server we want to duplicate
195 * @return new duplicated server
197 SERVER* dup_serve(const SERVER *const s);
200 * Detect the size of a file.
202 * @param fhandle An open filedescriptor
203 * @return the size of the file, or UINT64_MAX if detection was
204 * impossible.
206 uint64_t size_autodetect(int fhandle);
209 * Punch a hole in the backend file (if supported by the current system).
211 * @param req the request for which this is being processed
212 * @param client the client for which we're processing this request
214 int exptrim(struct nbd_request* req, CLIENT* client);
215 #endif //NBDSRV_H