10 #include <sys/socket.h>
11 #include <sys/types.h>
17 * Types of virtuatlization
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 */
28 * Variables associated with a server.
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
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. */
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 int transactionlogfd
;/**< fd for transaction log */
72 int clientfeats
; /**< Features supported by this client */
73 pthread_mutex_t lock
; /**< socket lock */
74 void *tls_session
; /**< TLS session context. Is NULL unless STARTTLS
75 has been negotiated. */
76 int (*socket_read
)(struct _client
*, void* buf
, size_t len
);
77 int (*socket_write
)(struct _client
*, void* buf
, size_t len
);
78 void (*socket_closed
)(struct _client
*);
82 * Variables associated with an open file
85 int fhandle
; /**< file descriptor */
86 off_t startoff
; /**< starting offset of this file */
89 /* Constants and macros */
92 * Error domain common for all NBD server errors.
94 #define NBDS_ERR g_quark_from_static_string("server-error-quark")
97 * NBD server error codes.
100 NBDS_ERR_CFILE_NOTFOUND
, /**< The configuration file is not found */
101 NBDS_ERR_CFILE_MISSING_GENERIC
, /**< The (required) group "generic" is missing */
102 NBDS_ERR_CFILE_KEY_MISSING
, /**< A (required) key is missing */
103 NBDS_ERR_CFILE_VALUE_INVALID
, /**< A value is syntactically invalid */
104 NBDS_ERR_CFILE_VALUE_UNSUPPORTED
, /**< A value is not supported in this build */
105 NBDS_ERR_CFILE_NO_EXPORTS
, /**< A config file was specified that does not
106 define any exports */
107 NBDS_ERR_CFILE_INCORRECT_PORT
, /**< The reserved port was specified for an
109 NBDS_ERR_CFILE_DIR_UNKNOWN
, /**< A directory requested does not exist*/
110 NBDS_ERR_CFILE_READDIR_ERR
, /**< Error occurred during readdir() */
111 NBDS_ERR_CFILE_INVALID_SPLICE
, /**< We can't use splice with the other options
112 specified for the export. */
113 NBDS_ERR_SO_LINGER
, /**< Failed to set SO_LINGER to a socket */
114 NBDS_ERR_SO_REUSEADDR
, /**< Failed to set SO_REUSEADDR to a socket */
115 NBDS_ERR_SO_KEEPALIVE
, /**< Failed to set SO_KEEPALIVE to a socket */
116 NBDS_ERR_GAI
, /**< Failed to get address info */
117 NBDS_ERR_SOCKET
, /**< Failed to create a socket */
118 NBDS_ERR_BIND
, /**< Failed to bind an address to socket */
119 NBDS_ERR_LISTEN
, /**< Failed to start listening on a socket */
120 NBDS_ERR_SYS
, /**< Underlying system call or library error */
121 NBDS_ERR_CFILE_INVALID_WAIT
, /**< We can't use wait with the other options
122 specified for the export. */
128 * @todo remove this. We should use g_log in all cases, and use the
129 * logging mangler to redirect to syslog if and when necessary.
132 #define msg(prio, ...) syslog(prio, __VA_ARGS__)
134 #define msg(prio, ...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
136 #define MY_NAME "nbd_server"
138 /** Per-export flags: */
139 #define F_READONLY 1 /**< flag to tell us a file is readonly */
140 #define F_MULTIFILE 2 /**< flag to tell us a file is exported using -m */
141 #define F_COPYONWRITE 4 /**< flag to tell us a file is exported using
143 #define F_AUTOREADONLY 8 /**< flag to tell us a file is set to autoreadonly */
144 #define F_SPARSE 16 /**< flag to tell us copyronwrite should use a sparse file */
145 #define F_SDP 32 /**< flag to tell us the export should be done using the Socket Direct Protocol for RDMA */
146 #define F_SYNC 64 /**< Whether to fsync() after a write */
147 #define F_FLUSH 128 /**< Whether server wants FLUSH to be sent by the client */
148 #define F_FUA 256 /**< Whether server wants FUA to be sent by the client */
149 #define F_ROTATIONAL 512 /**< Whether server wants the client to implement the elevator algorithm */
150 #define F_TEMPORARY 1024 /**< Whether the backing file is temporary and should be created then unlinked */
151 #define F_TRIM 2048 /**< Whether server wants TRIM (discard) to be sent by the client */
152 #define F_FIXED 4096 /**< Client supports fixed new-style protocol (and can thus send us extra options */
153 #define F_TREEFILES 8192 /**< flag to tell us a file is exported using -t */
154 #define F_FORCEDTLS 16384 /**< TLS is required, either for the server as a whole or for a given export */
155 #define F_SPLICE 32768 /**< flag to tell us to use splice for read/write operations */
156 #define F_WAIT 65536 /**< flag to tell us to wait for file creation */
161 * Check whether a given address matches a given netmask.
163 * @param mask the address or netmask to check against, in ASCII representation
164 * @param addr the address to check
166 * @return true if the address matches the mask, false otherwise; in case of
167 * failure to parse netmask, returns false with err set appropriately.
168 * @todo decide what to do with v6-mapped IPv4 addresses.
170 bool address_matches(const char* mask
, const struct sockaddr
* addr
, GError
** err
);
173 * Gets a byte to allow for address masking.
175 * @param masklen the length of the requested mask.
176 * @return if the length of the mask is 8 or longer, 0xFF. Otherwise, a byte
177 * with `masklen' number of leading bits set to 1, everything else set to 0.
179 uint8_t getmaskbyte(int masklen
) G_GNUC_PURE
;
182 * Check whether a client is allowed to connect. Works with an authorization
183 * file which contains one line per machine or network, with CIDR-style
186 * @param opts The client who's trying to connect.
187 * @return 0 - authorization refused, 1 - OK
189 int authorized_client(CLIENT
*opts
);
193 * @param s the old server we want to duplicate
194 * @return new duplicated server
196 SERVER
* dup_serve(const SERVER
*const s
);
199 * Detect the size of a file.
201 * @param fhandle An open filedescriptor
202 * @return the size of the file, or UINT64_MAX if detection was
205 uint64_t size_autodetect(int fhandle
);
208 * Punch a hole in the backend file (if supported by the current system).
210 * @param req the request for which this is being processed
211 * @param client the client for which we're processing this request
213 int exptrim(struct nbd_request
* req
, CLIENT
* client
);