|
Network Block Device
@PACKAGE_VERSION@
|
#include "lfs.h"#include <assert.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <sys/select.h>#include <sys/wait.h>#include <sys/un.h>#include <sys/ioctl.h>#include <sys/param.h>#include <signal.h>#include <errno.h>#include <libgen.h>#include <netinet/tcp.h>#include <netinet/in.h>#include <netdb.h>#include <syslog.h>#include <unistd.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <linux/falloc.h>#include <arpa/inet.h>#include <strings.h>#include <dirent.h>#include <getopt.h>#include <pwd.h>#include <grp.h>#include <ctype.h>#include <inttypes.h>#include <glib.h>#include "cliserv.h"#include "nbd-debug.h"#include "netdb-compat.h"#include "backend.h"#include "treefiles.h"#include <nbdsrv.h>Go to the source code of this file.
Data Structures | |
| struct | work_package |
| struct | PARAM |
| Configuration file values. More... | |
| struct | generic_conf |
| Configuration file values of the "generic" section. More... | |
Macros | |
| #define | _XOPEN_SOURCE 500 /* to get pread/pwrite */ |
| #define | _BSD_SOURCE /* to get DT_* macros */ |
| #define | _DARWIN_C_SOURCE /* to get DT_* macros on OS X */ |
| #define | MY_NAME "nbd_server" |
| #define | SYSCONFDIR "/etc" |
| Default position of the config file. More... | |
| #define | CFILE SYSCONFDIR "/nbd-server/config" |
| #define | OFFT_MAX ~((off_t)1<<(sizeof(off_t)*8-1)) |
| The highest value a variable of type off_t can reach. More... | |
| #define | BUFSIZE ((1024*1024)+sizeof(struct nbd_reply)) |
| Size of buffer that can hold requests. More... | |
| #define | DIFFPAGESIZE 4096 |
| diff file uses those chunks More... | |
| #define | F_OLDSTYLE 1 |
| Global flags: More... | |
| #define | F_LIST 2 |
| Allow clients to list the exports on a server. More... | |
| #define | F_NO_ZEROES 4 |
| Do not send zeros to client. More... | |
| #define | NEG_INIT (1 << 0) |
| #define | NEG_OLD (1 << 1) |
| #define | NEG_MODERN (1 << 2) |
| #define | NBD_D_TYPE de->d_type |
| #define | SEND(net, reply) |
| sending macro. More... | |
| #define | ERROR(client, reply, errcode) { reply.error = nbd_errno(errcode); SEND(client->net,reply); reply.error = 0; } |
| error macro. More... | |
Enumerations | |
| enum | PARAM_TYPE { PARAM_INT, PARAM_INT64, PARAM_STRING, PARAM_BOOL } |
| Type of configuration file values. More... | |
Functions | |
| static const char * | getcommandname (uint64_t command) |
| Translate a command name into human readable form. More... | |
| static void | consume (int f, void *buf, size_t len, size_t bufsiz) |
| Consume data from an FD that we don't want. More... | |
| static void | writeit (int f, void *buf, size_t len) |
| Write data from a buffer into a filedescriptor. More... | |
| void | usage () |
| Print out a message about how to use nbd-server. More... | |
| void | dump_section (SERVER *serve, gchar *section_header) |
| SERVER * | cmdline (int argc, char *argv[], struct generic_conf *genconf) |
| Parse the command line. More... | |
| GArray * | parse_cfile (gchar *f, struct generic_conf *const genconf, bool expect_generic, GError **e) |
| Parse the config file. More... | |
| GArray * | do_cfile_dir (gchar *dir, struct generic_conf *const genconf, GError **e) |
| Parse config file snippets in a directory. More... | |
| static void | sigchld_handler (const int s G_GNUC_UNUSED) |
| Handle SIGCHLD by setting atomically a flag which will be evaluated in the main loop of the root server process. More... | |
| void | killchild (gpointer key, gpointer value, gpointer user_data) |
| Kill a child. More... | |
| static void | sigterm_handler (const int s G_GNUC_UNUSED) |
| Handle SIGTERM by setting atomically a flag which will be evaluated in the main loop of the root server process. More... | |
| static void | sighup_handler (const int s G_GNUC_UNUSED) |
| Handle SIGHUP by setting atomically a flag which will be evaluated in the main loop of the root server process. More... | |
| int | get_filepos (CLIENT *client, off_t a, int *fhandle, off_t *foffset, size_t *maxbytes) |
| Get the file handle and offset, given an export offset. More... | |
| ssize_t | rawexpwrite (off_t a, char *buf, size_t len, CLIENT *client, int fua) |
| Write an amount of bytes at a given offset to the right file. More... | |
| int | rawexpwrite_fully (off_t a, char *buf, size_t len, CLIENT *client, int fua) |
| Call rawexpwrite repeatedly until all data has been written. More... | |
| ssize_t | rawexpread (off_t a, char *buf, size_t len, CLIENT *client) |
| Read an amount of bytes at a given offset from the right file. More... | |
| int | rawexpread_fully (off_t a, char *buf, size_t len, CLIENT *client) |
| Call rawexpread repeatedly until all data has been read. More... | |
| int | expread (off_t a, char *buf, size_t len, CLIENT *client) |
| Read an amount of bytes at a given offset from the right file. More... | |
| int | expwrite (off_t a, char *buf, size_t len, CLIENT *client, int fua) |
| Write an amount of bytes at a given offset to the right file. More... | |
| int | expflush (CLIENT *client) |
| Flush data to a client. More... | |
| void | punch_hole (int fd, off_t off, off_t len) |
| static void | send_reply (uint32_t opt, int net, uint32_t reply_type, size_t datasize, void *data) |
| static CLIENT * | handle_export_name (uint32_t opt, int net, GArray *servers, uint32_t cflags) |
| static void | handle_list (uint32_t opt, int net, GArray *servers, uint32_t cflags) |
| CLIENT * | negotiate (int net, GArray *servers) |
| Do the initial negotiation. More... | |
| void | send_export_info (CLIENT *client) |
| static int | nbd_errno (int errcode) |
| static void | package_dispose (struct work_package *package) |
| struct work_package * | package_create (CLIENT *client, struct nbd_request *req) |
| static void | setup_reply (struct nbd_reply *rep, struct nbd_request *req) |
| static void | handle_read (CLIENT *client, struct nbd_request *req) |
| static void | handle_write (CLIENT *client, struct nbd_request *req, void *data) |
| static void | handle_flush (CLIENT *client, struct nbd_request *req) |
| static void | handle_trim (CLIENT *client, struct nbd_request *req) |
| static void | handle_request (gpointer data, gpointer user_data) |
| static int | mainloop_threaded (CLIENT *client) |
| int | mainloop (CLIENT *client) |
| Serve a file to a single client. More... | |
| void | setupexport (CLIENT *client) |
| Set up client export array, which is an array of FILE_INFO. More... | |
| int | copyonwrite_prepare (CLIENT *client) |
| int | do_run (gchar *command, gchar *file) |
| Run a command. More... | |
| void | serveconnection (CLIENT *client) |
| Serve a connection. More... | |
| int | set_peername (int net, CLIENT *client) |
| Find the name of the file we have to serve. More... | |
| void | destroy_pid_t (gpointer data) |
| Destroy a pid_t*. More... | |
| static pid_t | spawn_child () |
| static int | socket_accept (const int sock) |
| static void | handle_modern_connection (GArray *const servers, const int sock) |
| static int | get_index_by_servename (const gchar *const servename, const GArray *const servers) |
| Return the index of the server whose servename matches the given name. More... | |
| static int | append_new_servers (GArray *const servers, GError **const gerror) |
| Parse configuration files and add servers to the array if they don't already exist there. More... | |
| void | serveloop (GArray *servers) |
| Loop through the available servers, and serve them. More... | |
| int | dosockopts (const int socket, GError **const gerror) |
| Set server socket options. More... | |
| int | open_unix (const gchar *const sockname, GError **const gerror) |
| int | open_modern (const gchar *const addr, const gchar *const port, GError **const gerror) |
| void | setup_servers (GArray *const servers, const gchar *const modernaddr, const gchar *const modernport, const gchar *unixsock) |
| Connect our servers. More... | |
| void | daemonize () |
| Go daemon (unless we specified at compile time that we didn't want this) More... | |
| void | dousers (const gchar *const username, const gchar *const groupname) |
| Set up user-ID and/or group-ID. More... | |
| void | glib_message_syslog_redirect (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) |
| int | main (int argc, char *argv[]) |
| Main entry point... More... | |
Variables | |
| gchar * | config_file_pos |
| Where our config file actually is. More... | |
| int | glob_flags =0 |
| global flags More... | |
| int | dontfork = 0 |
| GHashTable * | children |
| char | pidfname [256] |
| name of our PID file More... | |
| char | default_authname [] = SYSCONFDIR "/nbd-server/allow" |
| default name of allow file More... | |
| GThreadPool * | tpool |
| static volatile sig_atomic_t | is_sigchld_caught |
| Flag set by SIGCHLD handler to mark a child exit. More... | |
| static volatile sig_atomic_t | is_sigterm_caught |
| Flag set by SIGTERM handler to mark a exit request. More... | |
| static volatile sig_atomic_t | is_sighup_caught |
| Flag set by SIGHUP handler to mark a reconfiguration request. More... | |
| GArray * | modernsocks |
| Sockets for the modern handler. More... | |
| bool | logged_oversized =false |
| whether we logged oversized requests already More... | |
| #define _BSD_SOURCE /* to get DT_* macros */ |
Definition at line 62 of file nbd-server.c.
| #define _DARWIN_C_SOURCE /* to get DT_* macros on OS X */ |
Definition at line 63 of file nbd-server.c.
| #define _XOPEN_SOURCE 500 /* to get pread/pwrite */ |
Definition at line 61 of file nbd-server.c.
| #define BUFSIZE ((1024*1024)+sizeof(struct nbd_reply)) |
Size of buffer that can hold requests.
Definition at line 152 of file nbd-server.c.
Referenced by mainloop().
| #define CFILE SYSCONFDIR "/nbd-server/config" |
Definition at line 136 of file nbd-server.c.
| #define DIFFPAGESIZE 4096 |
diff file uses those chunks
Definition at line 153 of file nbd-server.c.
Referenced by copyonwrite_prepare(), expread(), and expwrite().
| #define F_LIST 2 |
Allow clients to list the exports on a server.
Definition at line 157 of file nbd-server.c.
Referenced by handle_list(), and parse_cfile().
| #define F_NO_ZEROES 4 |
Do not send zeros to client.
Definition at line 158 of file nbd-server.c.
Referenced by negotiate(), and send_export_info().
| #define F_OLDSTYLE 1 |
Global flags:
Allow oldstyle (port-based) exports
Definition at line 156 of file nbd-server.c.
Referenced by parse_cfile().
| #define MY_NAME "nbd_server" |
Definition at line 112 of file nbd-server.c.
Referenced by main().
| #define NBD_D_TYPE de->d_type |
Definition at line 507 of file nbd-server.c.
Referenced by do_cfile_dir().
| #define NEG_INIT (1 << 0) |
Definition at line 163 of file nbd-server.c.
| #define NEG_MODERN (1 << 2) |
Definition at line 165 of file nbd-server.c.
| #define NEG_OLD (1 << 1) |
Definition at line 164 of file nbd-server.c.
| #define OFFT_MAX ~((off_t)1<<(sizeof(off_t)*8-1)) |
The highest value a variable of type off_t can reach.
This is a signed integer, so set all bits except for the leftmost one.
Definition at line 151 of file nbd-server.c.
Referenced by handle_export_name(), and main().
| #define SEND | ( | net, | |
| reply | |||
| ) |
sending macro.
Definition at line 1585 of file nbd-server.c.
Referenced by mainloop().
| #define SYSCONFDIR "/etc" |
Default position of the config file.
Definition at line 134 of file nbd-server.c.
Referenced by get_from_config().
| enum PARAM_TYPE |
Type of configuration file values.
| Enumerator | |
|---|---|
| PARAM_INT |
This parameter is an integer. |
| PARAM_INT64 |
This parameter is an integer. |
| PARAM_STRING |
This parameter is a string. |
| PARAM_BOOL |
This parameter is a boolean. |
Definition at line 209 of file nbd-server.c.
|
static |
Parse configuration files and add servers to the array if they don't already exist there.
The existence is tested by comparing servenames. A server is appended to the array only if its servename is unique among all other servers.
| servers | an array of servers |
Definition at line 2266 of file nbd-server.c.
References config_file_pos, get_index_by_servename(), parse_cfile(), SERVER::servename, generic_conf::threads, and tpool.
Referenced by serveloop().
| SERVER* cmdline | ( | int | argc, |
| char * | argv[], | ||
| struct generic_conf * | genconf | ||
| ) |
Parse the command line.
Definition at line 359 of file nbd-server.c.
References SERVER::authname, config_file_pos, default_authname, dontfork, dump_section(), SERVER::expected_size, SERVER::exportname, F_COPYONWRITE, F_MULTIFILE, F_READONLY, SERVER::flags, SERVER::max_connections, generic_conf::modernaddr, generic_conf::modernport, pidfname, SERVER::servename, usage(), VERSION, VIRT_IPLIT, and SERVER::virtstyle.
Referenced by main().
|
inlinestatic |
Consume data from an FD that we don't want.
| f | a file descriptor |
| buf | a buffer |
| len | the number of bytes to consume |
| bufsiz | the size of the buffer |
Definition at line 277 of file nbd-server.c.
References readit().
Referenced by mainloop().
| int copyonwrite_prepare | ( | CLIENT * | client | ) |
Definition at line 1881 of file nbd-server.c.
References CLIENT::clientname, SERVER::cowdir, CLIENT::difffile, CLIENT::difffilename, DIFFPAGESIZE, CLIENT::difmap, err(), CLIENT::exportname, CLIENT::exportsize, msg, and CLIENT::server.
Referenced by serveconnection().
| void daemonize | ( | ) |
Go daemon (unless we specified at compile time that we didn't want this)
| serve | the first server of our configuration. If its port is zero, then do not daemonize, because we're doing inetd then. This parameter is only used to create a PID file of the form /var/run/nbd-server.<port>.pid; it's not modified in any way. |
Definition at line 2656 of file nbd-server.c.
References err(), and pidfname.
Referenced by main().
| void destroy_pid_t | ( | gpointer | data | ) |
Destroy a pid_t*.
| data | a pointer to pid_t which should be freed |
Definition at line 2077 of file nbd-server.c.
Referenced by setup_servers().
| GArray* do_cfile_dir | ( | gchar * | dir, |
| struct generic_conf *const | genconf, | ||
| GError ** | e | ||
| ) |
Parse config file snippets in a directory.
Uses readdir() and friends to find files and open them, then passes them on to parse_cfile with have_global set false
Definition at line 519 of file nbd-server.c.
References NBD_D_TYPE, NBDS_ERR, NBDS_ERR_CFILE_DIR_UNKNOWN, NBDS_ERR_CFILE_READDIR_ERR, and parse_cfile().
Referenced by parse_cfile().
| int do_run | ( | gchar * | command, |
| gchar * | file | ||
| ) |
Run a command.
This is used for the prerun'' andpostrun'' config file options
| command | the command to be ran. Read from the config file |
| file | the file name we're about to export |
Definition at line 1912 of file nbd-server.c.
Referenced by serveconnection().
| int dosockopts | ( | const int | socket, |
| GError **const | gerror | ||
| ) |
Set server socket options.
| socket | a socket descriptor of the server |
| gerror | a pointer to an error object pointer used for reporting errors. On error, if gerror is not NULL, *gerror is set and -1 is returned. |
Definition at line 2425 of file nbd-server.c.
References NBDS_ERR, NBDS_ERR_SO_KEEPALIVE, NBDS_ERR_SO_LINGER, and NBDS_ERR_SO_REUSEADDR.
Referenced by open_modern().
| void dousers | ( | const gchar *const | username, |
| const gchar *const | groupname | ||
| ) |
Set up user-ID and/or group-ID.
Definition at line 2686 of file nbd-server.c.
References err().
Referenced by main().
| void dump_section | ( | SERVER * | serve, |
| gchar * | section_header | ||
| ) |
Definition at line 328 of file nbd-server.c.
References SERVER::authname, SERVER::expected_size, SERVER::exportname, F_COPYONWRITE, F_MULTIFILE, F_READONLY, F_TREEFILES, SERVER::flags, and SERVER::listenaddr.
Referenced by cmdline().
| int expflush | ( | CLIENT * | client | ) |
Flush data to a client.
| client | The client we're going to write for. |
Definition at line 1192 of file nbd-server.c.
References CLIENT::difffile, CLIENT::export, F_COPYONWRITE, F_READONLY, F_TREEFILES, FILE_INFO::fhandle, SERVER::flags, and CLIENT::server.
Referenced by handle_flush(), and mainloop().
| int expread | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client | ||
| ) |
Read an amount of bytes at a given offset from the right file.
This abstracts the read-side of the copyonwrite stuff, and calls rawexpread() with the right parameters to do the actual work.
| a | The offset where the read should start |
| buf | A buffer to read into |
| len | The size of buf |
| client | The client we're going to read for |
Definition at line 1092 of file nbd-server.c.
References DEBUG, CLIENT::difffile, DIFFPAGESIZE, CLIENT::difmap, F_COPYONWRITE, SERVER::flags, myseek(), rawexpread_fully(), and CLIENT::server.
Referenced by handle_read(), and mainloop().
| int expwrite | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client, | ||
| int | fua | ||
| ) |
Write an amount of bytes at a given offset to the right file.
This abstracts the write-side of the copyonwrite option, and calls rawexpwrite() with the right parameters to do the actual work.
| a | The offset where the write should start |
| buf | The buffer to write from |
| len | The length of buf |
| client | The client we're going to write for. |
| fua | Flag to indicate 'Force Unit Access' |
Definition at line 1134 of file nbd-server.c.
References DEBUG, CLIENT::difffile, CLIENT::difffilelen, DIFFPAGESIZE, CLIENT::difmap, F_COPYONWRITE, F_SPARSE, F_SYNC, fdatasync, SERVER::flags, myseek(), rawexpread_fully(), rawexpwrite_fully(), and CLIENT::server.
Referenced by handle_write(), and mainloop().
| int get_filepos | ( | CLIENT * | client, |
| off_t | a, | ||
| int * | fhandle, | ||
| off_t * | foffset, | ||
| size_t * | maxbytes | ||
| ) |
Get the file handle and offset, given an export offset.
| client | The client we're serving for |
| a | The offset to get corresponding file/offset for |
| fhandle | [out] File descriptor |
| foffset | [out] Offset into fhandle |
| maxbytes | [out] Tells how many bytes can be read/written from fhandle starting at foffset (0 if there is no limit) |
Definition at line 893 of file nbd-server.c.
References CLIENT::export, CLIENT::exportname, CLIENT::exportsize, F_READONLY, F_TREEFILES, FILE_INFO::fhandle, SERVER::flags, CLIENT::lock, open_treefile(), CLIENT::server, FILE_INFO::startoff, and TREEPAGESIZE.
Referenced by rawexpread(), and rawexpwrite().
|
static |
Return the index of the server whose servename matches the given name.
| servename | a string to match |
| servers | an array of servers |
Definition at line 2242 of file nbd-server.c.
References SERVER::servename.
Referenced by append_new_servers().
|
inlinestatic |
Translate a command name into human readable form.
| command | The command number (after applying NBD_CMD_MASK_COMMAND) |
Definition at line 252 of file nbd-server.c.
References NBD_CMD_DISC, NBD_CMD_FLUSH, NBD_CMD_READ, NBD_CMD_TRIM, and NBD_CMD_WRITE.
Referenced by mainloop().
| void glib_message_syslog_redirect | ( | const gchar * | log_domain, |
| GLogLevelFlags | log_level, | ||
| const gchar * | message, | ||
| gpointer | user_data | ||
| ) |
Definition at line 2713 of file nbd-server.c.
|
static |
Definition at line 1251 of file nbd-server.c.
References CLIENT::clientfeats, err(), CLIENT::exportsize, CLIENT::lock, CLIENT::modern, CLIENT::net, OFFT_MAX, SERVER::servename, CLIENT::server, and CLIENT::transactionlogfd.
Referenced by negotiate().
|
static |
Definition at line 1494 of file nbd-server.c.
References DEBUG, nbd_reply::error, expflush(), CLIENT::lock, nbd_errno(), CLIENT::net, setup_reply(), and writeit().
Referenced by handle_request().
|
static |
Definition at line 1292 of file nbd-server.c.
References err(), err_nonfatal(), F_LIST, glob_flags, len, NBD_REP_ACK, NBD_REP_ERR_INVALID, NBD_REP_ERR_POLICY, NBD_REP_SERVER, send_reply(), and SERVER::servename.
Referenced by negotiate().
|
static |
Definition at line 2133 of file nbd-server.c.
References authorized_client(), children, CLIENT::clientname, dontfork, SERVER::max_connections, modernsocks, msg, negotiate(), serveconnection(), CLIENT::server, set_peername(), socket_accept(), and spawn_child().
Referenced by serveloop().
|
static |
Definition at line 1455 of file nbd-server.c.
References DEBUG, err(), nbd_reply::error, expread(), nbd_request::from, nbd_request::len, CLIENT::lock, nbd_errno(), CLIENT::net, setup_reply(), and writeit().
Referenced by handle_request().
|
static |
Definition at line 1520 of file nbd-server.c.
References nbd_reply::error, handle_flush(), handle_read(), handle_trim(), handle_write(), msg, NBD_CMD_FLUSH, NBD_CMD_MASK_COMMAND, NBD_CMD_READ, NBD_CMD_TRIM, NBD_CMD_WRITE, nbd_errno(), package_dispose(), setup_reply(), and writeit().
Referenced by main().
|
static |
Definition at line 1507 of file nbd-server.c.
References DEBUG, nbd_reply::error, exptrim(), CLIENT::lock, nbd_errno(), CLIENT::net, setup_reply(), and writeit().
Referenced by handle_request().
|
static |
Definition at line 1474 of file nbd-server.c.
References DEBUG, nbd_reply::error, expwrite(), F_AUTOREADONLY, F_READONLY, SERVER::flags, nbd_request::from, nbd_request::len, CLIENT::lock, NBD_CMD_MASK_COMMAND, nbd_errno(), CLIENT::net, CLIENT::server, setup_reply(), nbd_request::type, and writeit().
Referenced by handle_request().
| void killchild | ( | gpointer | key, |
| gpointer | value, | ||
| gpointer | user_data | ||
| ) |
Kill a child.
Called from sigterm_handler::g_hash_table_foreach.
| key | the key |
| value | the value corresponding to the above key |
| user_data | a pointer which we always set to 1, so that we know what will happen next. |
Definition at line 850 of file nbd-server.c.
Referenced by serveloop().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Main entry point...
Definition at line 2747 of file nbd-server.c.
References CFILE, cmdline(), config_file_pos, daemonize(), dontfork, dousers(), err(), CLIENT::exportsize, generic_conf::flags, glob_flags, generic_conf::group, handle_request(), logging(), CLIENT::modern, generic_conf::modernaddr, generic_conf::modernport, modernsocks, MY_NAME, NBDS_ERR, NBDS_ERR_CFILE_NOTFOUND, CLIENT::net, OFFT_MAX, parse_cfile(), serveconnection(), serveloop(), CLIENT::server, set_peername(), setup_servers(), generic_conf::threads, tpool, generic_conf::unixsock, and generic_conf::user.
| int mainloop | ( | CLIENT * | client | ) |
Serve a file to a single client.
| client | The client we're going to serve to. |
Definition at line 1599 of file nbd-server.c.
References BUFSIZE, consume(), DEBUG, CLIENT::difffile, CLIENT::difffilename, CLIENT::difmap, err(), nbd_reply::error, ERROR, expflush(), CLIENT::exportsize, expread(), exptrim(), expwrite(), F_AUTOREADONLY, F_COPYONWRITE, F_READONLY, SERVER::flags, nbd_request::from, getcommandname(), nbd_request::handle, nbd_reply::handle, len, nbd_request::len, logged_oversized, nbd_request::magic, nbd_reply::magic, msg, NBD_CMD_DISC, NBD_CMD_FLAG_FUA, NBD_CMD_FLUSH, NBD_CMD_MASK_COMMAND, NBD_CMD_READ, NBD_CMD_TRIM, NBD_CMD_WRITE, NBD_REPLY_MAGIC, NBD_REQUEST_MAGIC, CLIENT::net, ntohll(), readit(), SEND, send_export_info(), CLIENT::server, CLIENT::transactionlogfd, nbd_request::type, and writeit().
|
static |
Definition at line 1550 of file nbd-server.c.
References work_package::data, DEBUG, err(), nbd_request::from, nbd_request::len, nbd_request::magic, NBD_CMD_DISC, NBD_CMD_MASK_COMMAND, NBD_CMD_WRITE, NBD_REQUEST_MAGIC, CLIENT::net, ntohll(), package_create(), readit(), send_export_info(), tpool, CLIENT::transactionlogfd, nbd_request::type, and writeit().
Referenced by serveconnection().
|
static |
Definition at line 1410 of file nbd-server.c.
Referenced by handle_flush(), handle_read(), handle_request(), handle_trim(), and handle_write().
| CLIENT* negotiate | ( | int | net, |
| GArray * | servers | ||
| ) |
Do the initial negotiation.
| client | The client we're negotiating with. |
Definition at line 1324 of file nbd-server.c.
References err_nonfatal(), F_NO_ZEROES, glob_flags, handle_export_name(), handle_list(), htonll, INIT_PASSWD, magic, NBD_FLAG_C_NO_ZEROES, NBD_FLAG_FIXED_NEWSTYLE, NBD_FLAG_NO_ZEROES, NBD_OPT_ABORT, NBD_OPT_EXPORT_NAME, NBD_OPT_LIST, NBD_REP_ERR_UNSUP, ntohll(), opts_magic, and send_reply().
Referenced by handle_modern_connection().
| int open_modern | ( | const gchar *const | addr, |
| const gchar *const | port, | ||
| GError **const | gerror | ||
| ) |
Definition at line 2498 of file nbd-server.c.
References dosockopts(), modernsocks, NBD_DEFAULT_PORT, NBDS_ERR, NBDS_ERR_BIND, NBDS_ERR_GAI, and NBDS_ERR_SOCKET.
Referenced by setup_servers().
| int open_unix | ( | const gchar *const | sockname, |
| GError **const | gerror | ||
| ) |
Definition at line 2458 of file nbd-server.c.
References modernsocks, NBDS_ERR, NBDS_ERR_BIND, and NBDS_ERR_SOCKET.
Referenced by setup_servers().
| struct work_package* package_create | ( | CLIENT * | client, |
| struct nbd_request * | req | ||
| ) |
Definition at line 1437 of file nbd-server.c.
References work_package::client, work_package::data, nbd_request::len, NBD_CMD_MASK_COMMAND, NBD_CMD_WRITE, work_package::req, and nbd_request::type.
Referenced by mainloop_threaded().
|
static |
Definition at line 1431 of file nbd-server.c.
References work_package::data, and work_package::req.
Referenced by handle_request().
| GArray * parse_cfile | ( | gchar * | f, |
| struct generic_conf *const | genconf, | ||
| bool | expect_generic, | ||
| GError ** | e | ||
| ) |
Parse the config file.
| f | the name of the config file |
| genconf | a pointer to generic configuration which will get updated with parsed values. If NULL, then parsed generic configuration values are safely and silently discarded. |
| e | a GError. Error code can be any of the following: NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_VALUE_INVALID, NBDS_ERR_CFILE_VALUE_UNSUPPORTED or NBDS_ERR_CFILE_NO_EXPORTS. |
| expect_generic | if true, we expect a configuration file that contains a [generic] section. If false, we don't. |
Definition at line 600 of file nbd-server.c.
References SERVER::authname, SERVER::cidrlen, SERVER::cowdir, do_cfile_dir(), err(), SERVER::expected_size, SERVER::exportname, F_COPYONWRITE, F_FLUSH, F_FUA, F_LIST, F_MULTIFILE, F_OLDSTYLE, F_READONLY, F_ROTATIONAL, F_SDP, F_SPARSE, F_SYNC, F_TEMPORARY, F_TREEFILES, F_TRIM, SERVER::flags, generic_conf::flags, generic_conf::group, SERVER::listenaddr, SERVER::max_connections, generic_conf::modernaddr, generic_conf::modernport, NBDS_ERR, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_NO_EXPORTS, NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_VALUE_INVALID, NBDS_ERR_CFILE_VALUE_UNSUPPORTED, PARAM_BOOL, PARAM_INT, PARAM_INT64, PARAM_OFFT, PARAM_STRING, SERVER::postrun, SERVER::prerun, SERVER::servename, generic_conf::threads, SERVER::transactionlog, generic_conf::unixsock, generic_conf::user, VIRT_CIDR, VIRT_IPHASH, VIRT_IPLIT, VIRT_NONE, and SERVER::virtstyle.
Referenced by append_new_servers(), do_cfile_dir(), and main().
| void punch_hole | ( | int | fd, |
| off_t | off, | ||
| off_t | len | ||
| ) |
Definition at line 1216 of file nbd-server.c.
| ssize_t rawexpread | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client | ||
| ) |
Read an amount of bytes at a given offset from the right file.
This abstracts the read-side of the multiple files option.
| a | The offset where the read should start |
| buf | A buffer to read into |
| len | The size of buf |
| client | The client we're serving for |
Definition at line 1047 of file nbd-server.c.
References DEBUG, F_TREEFILES, SERVER::flags, get_filepos(), and CLIENT::server.
Referenced by rawexpread_fully().
| int rawexpread_fully | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client | ||
| ) |
Call rawexpread repeatedly until all data has been read.
Definition at line 1071 of file nbd-server.c.
References rawexpread().
Referenced by expread(), and expwrite().
| ssize_t rawexpwrite | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client, | ||
| int | fua | ||
| ) |
Write an amount of bytes at a given offset to the right file.
This abstracts the write-side of the multiple file option.
| a | The offset where the write should start |
| buf | The buffer to write from |
| len | The length of buf |
| client | The client we're serving for |
| fua | Flag to indicate 'Force Unit Access' |
Definition at line 952 of file nbd-server.c.
References DEBUG, F_SYNC, F_TREEFILES, fdatasync, SERVER::flags, get_filepos(), and CLIENT::server.
Referenced by rawexpwrite_fully().
| int rawexpwrite_fully | ( | off_t | a, |
| char * | buf, | ||
| size_t | len, | ||
| CLIENT * | client, | ||
| int | fua | ||
| ) |
Call rawexpwrite repeatedly until all data has been written.
| a | The offset where the write should start |
| buf | The buffer to write from |
| len | The length of buf |
| client | The client we're serving for |
| fua | Flag to indicate 'Force Unit Access' |
Definition at line 1025 of file nbd-server.c.
References rawexpwrite().
Referenced by expwrite().
| void send_export_info | ( | CLIENT * | client | ) |
Definition at line 1383 of file nbd-server.c.
References err(), CLIENT::exportsize, F_FLUSH, F_FUA, F_NO_ZEROES, F_READONLY, F_ROTATIONAL, F_TRIM, SERVER::flags, glob_flags, htonll, NBD_FLAG_HAS_FLAGS, NBD_FLAG_READ_ONLY, NBD_FLAG_ROTATIONAL, NBD_FLAG_SEND_FLUSH, NBD_FLAG_SEND_FUA, NBD_FLAG_SEND_TRIM, CLIENT::net, and CLIENT::server.
Referenced by mainloop(), and mainloop_threaded().
|
static |
Definition at line 1232 of file nbd-server.c.
Referenced by handle_list(), and negotiate().
| void serveconnection | ( | CLIENT * | client | ) |
Serve a connection.
| client | a connected client |
Definition at line 1932 of file nbd-server.c.
References copyonwrite_prepare(), do_run(), CLIENT::exportname, F_COPYONWRITE, SERVER::flags, mainloop_threaded(), CLIENT::net, SERVER::postrun, SERVER::prerun, CLIENT::server, setmysockopt(), setupexport(), SERVER::transactionlog, and CLIENT::transactionlogfd.
Referenced by handle_modern_connection(), and main().
| void serveloop | ( | GArray * | servers | ) |
Loop through the available servers, and serve them.
Never returns.
Definition at line 2298 of file nbd-server.c.
References append_new_servers(), children, DEBUG, err(), handle_modern_connection(), is_sigchld_caught, is_sighup_caught, is_sigterm_caught, killchild(), len, modernsocks, msg, pidfname, and SERVER::servename.
Referenced by main().
| int set_peername | ( | int | net, |
| CLIENT * | client | ||
| ) |
Find the name of the file we have to serve.
This will use g_strdup_printf to put the IP address of the client inside a filename containing "%s" (in the form as specified by the "virtstyle" option). That name is then written to client->exportname.
| net | A socket connected to an nbd client |
| client | information about the client. The IP address in human-readable format will be written to a new char* buffer, the address of which will be stored in client->clientname. |
Definition at line 1975 of file nbd-server.c.
References SERVER::cidrlen, CLIENT::clientaddr, CLIENT::clientname, SERVER::exportname, CLIENT::exportname, getmaskbyte(), msg, CLIENT::server, VIRT_CIDR, VIRT_IPHASH, VIRT_IPLIT, VIRT_NONE, and SERVER::virtstyle.
Referenced by handle_modern_connection(), and main().
|
static |
Definition at line 1449 of file nbd-server.c.
References nbd_reply::error, nbd_request::handle, nbd_reply::handle, nbd_reply::magic, and NBD_REPLY_MAGIC.
Referenced by handle_flush(), handle_read(), handle_request(), handle_trim(), and handle_write().
| void setup_servers | ( | GArray *const | servers, |
| const gchar *const | modernaddr, | ||
| const gchar *const | modernport, | ||
| const gchar * | unixsock | ||
| ) |
Connect our servers.
Definition at line 2605 of file nbd-server.c.
References children, destroy_pid_t(), err(), msg, open_modern(), open_unix(), sigchld_handler(), sighup_handler(), and sigterm_handler().
Referenced by main().
| void setupexport | ( | CLIENT * | client | ) |
Set up client export array, which is an array of FILE_INFO.
Also, split a single exportfile into multiple ones, if that was asked.
| client | information on the client which we want to setup export for |
Definition at line 1769 of file nbd-server.c.
References DEBUG, err(), SERVER::expected_size, CLIENT::export, CLIENT::exportname, CLIENT::exportsize, F_AUTOREADONLY, F_COPYONWRITE, F_MULTIFILE, F_READONLY, F_TEMPORARY, F_TREEFILES, FILE_INFO::fhandle, SERVER::flags, msg, CLIENT::server, size_autodetect(), FILE_INFO::startoff, and TREEPAGESIZE.
Referenced by serveconnection().
|
static |
Handle SIGCHLD by setting atomically a flag which will be evaluated in the main loop of the root server process.
This allows us to separate the signal catching from th actual task triggered by SIGCHLD and hence processing in the interrupt context is kept as minimial as possible.
| s | the signal we're handling (must be SIGCHLD, or something is severely wrong) |
Definition at line 838 of file nbd-server.c.
References is_sigchld_caught.
Referenced by setup_servers().
|
static |
Handle SIGHUP by setting atomically a flag which will be evaluated in the main loop of the root server process.
This allows us to separate the signal catching from th actual task triggered by SIGHUP and hence processing in the interrupt context is kept as minimial as possible.
| s | the signal we're handling (must be SIGHUP, or something is severely wrong). |
Definition at line 878 of file nbd-server.c.
References is_sighup_caught.
Referenced by setup_servers().
|
static |
Handle SIGTERM by setting atomically a flag which will be evaluated in the main loop of the root server process.
This allows us to separate the signal catching from th actual task triggered by SIGTERM and hence processing in the interrupt context is kept as minimial as possible.
| s | the signal we're handling (must be SIGTERM, or something is severely wrong). |
Definition at line 865 of file nbd-server.c.
References is_sigterm_caught.
Referenced by setup_servers().
|
static |
Definition at line 2118 of file nbd-server.c.
References err_nonfatal().
Referenced by handle_modern_connection().
|
static |
Definition at line 2082 of file nbd-server.c.
Referenced by handle_modern_connection().
| void usage | ( | ) |
Print out a message about how to use nbd-server.
Split out to a separate function so that we can call it from multiple places
Definition at line 308 of file nbd-server.c.
References CFILE, and VERSION.
Referenced by cmdline().
|
inlinestatic |
Write data from a buffer into a filedescriptor.
| f | a file descriptor |
| buf | a buffer containing data |
| len | the number of bytes to be written |
Definition at line 293 of file nbd-server.c.
Referenced by handle_flush(), handle_read(), handle_request(), handle_trim(), handle_write(), mainloop(), and mainloop_threaded().
| GHashTable* children |
Definition at line 159 of file nbd-server.c.
Referenced by handle_modern_connection(), serveloop(), setup_servers(), and spawn_child().
| gchar* config_file_pos |
Where our config file actually is.
Definition at line 139 of file nbd-server.c.
Referenced by append_new_servers(), cmdline(), and main().
| char default_authname[] = SYSCONFDIR "/nbd-server/allow" |
| int dontfork = 0 |
Definition at line 145 of file nbd-server.c.
Referenced by cmdline(), handle_modern_connection(), and main().
| int glob_flags =0 |
global flags
Definition at line 142 of file nbd-server.c.
Referenced by handle_list(), main(), negotiate(), and send_export_info().
|
static |
Flag set by SIGCHLD handler to mark a child exit.
Definition at line 179 of file nbd-server.c.
Referenced by serveloop(), and sigchld_handler().
|
static |
Flag set by SIGHUP handler to mark a reconfiguration request.
Definition at line 189 of file nbd-server.c.
Referenced by serveloop(), and sighup_handler().
|
static |
Flag set by SIGTERM handler to mark a exit request.
Definition at line 184 of file nbd-server.c.
Referenced by serveloop(), and sigterm_handler().
| bool logged_oversized =false |
whether we logged oversized requests already
Definition at line 204 of file nbd-server.c.
Referenced by mainloop().
| GArray* modernsocks |
Sockets for the modern handler.
Not used if a client was only specified on the command line; only port used if oldstyle is set to false (and then the command-line client isn't used, gna gna). This may be more than one socket on systems that don't support serving IPv4 and IPv6 from the same socket (like, e.g., FreeBSD)
Definition at line 194 of file nbd-server.c.
Referenced by handle_modern_connection(), main(), open_modern(), open_unix(), and serveloop().
| char pidfname[256] |
name of our PID file
Definition at line 160 of file nbd-server.c.
Referenced by cmdline(), daemonize(), and serveloop().
| GThreadPool* tpool |
Definition at line 170 of file nbd-server.c.
Referenced by append_new_servers(), main(), and mainloop_threaded().
1.8.8