Network Block Device @PACKAGE_VERSION@
Data Structures | Defines | Enumerations | Functions

nbdsrv.h File Reference

#include "lfs.h"
#include <glib.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "nbd.h"

Go to the source code of this file.

Data Structures

struct  SERVER
 Variables associated with a server. More...
struct  CLIENT
 Variables associated with a client connection. More...
struct  FILE_INFO
 Variables associated with an open file. More...

Defines

#define NBDS_ERR   g_quark_from_static_string("server-error-quark")
 Error domain common for all NBD server errors.
#define msg(prio,...)   g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
 Logging macros.
#define MY_NAME   "nbd_server"
#define F_READONLY   1
 Per-export flags:
#define F_MULTIFILE   2
 flag to tell us a file is exported using -m
#define F_COPYONWRITE   4
 flag to tell us a file is exported using copyonwrite
#define F_AUTOREADONLY   8
 flag to tell us a file is set to autoreadonly
#define F_SPARSE   16
 flag to tell us copyronwrite should use a sparse file
#define F_SDP   32
 flag to tell us the export should be done using the Socket Direct Protocol for RDMA
#define F_SYNC   64
 Whether to fsync() after a write.
#define F_FLUSH   128
 Whether server wants FLUSH to be sent by the client.
#define F_FUA   256
 Whether server wants FUA to be sent by the client.
#define F_ROTATIONAL   512
 Whether server wants the client to implement the elevator algorithm.
#define F_TEMPORARY   1024
 Whether the backing file is temporary and should be created then unlinked.
#define F_TRIM   2048
 Whether server wants TRIM (discard) to be sent by the client.
#define F_FIXED   4096
 Client supports fixed new-style protocol (and can thus send us extra options.
#define F_TREEFILES   8192
 flag to tell us a file is exported using -t

Enumerations

enum  VIRT_STYLE { VIRT_NONE = 0, VIRT_IPLIT, VIRT_IPHASH, VIRT_CIDR }
 

Types of virtuatlization.

More...
enum  NBDS_ERRS {
  NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_KEY_MISSING, NBDS_ERR_CFILE_VALUE_INVALID,
  NBDS_ERR_CFILE_VALUE_UNSUPPORTED, NBDS_ERR_CFILE_NO_EXPORTS, NBDS_ERR_CFILE_INCORRECT_PORT, NBDS_ERR_CFILE_DIR_UNKNOWN,
  NBDS_ERR_CFILE_READDIR_ERR, NBDS_ERR_SO_LINGER, NBDS_ERR_SO_REUSEADDR, NBDS_ERR_SO_KEEPALIVE,
  NBDS_ERR_GAI, NBDS_ERR_SOCKET, NBDS_ERR_BIND, NBDS_ERR_LISTEN,
  NBDS_ERR_SYS
}
 

NBD server error codes.

More...

Functions

bool address_matches (const char *mask, const struct sockaddr *addr, GError **err)
 Check whether a given address matches a given netmask.
uint8_t getmaskbyte (int masklen) G_GNUC_PURE
 Gets a byte to allow for address masking.
int authorized_client (CLIENT *opts)
 Check whether a client is allowed to connect.
SERVERdup_serve (const SERVER *const s)
 duplicate server
uint64_t size_autodetect (int fhandle)
 Detect the size of a file.
int exptrim (struct nbd_request *req, CLIENT *client)
 Punch a hole in the backend file (if supported by the current system).
void myseek (int handle, off_t a)
 seek to a position in a file, with error handling.

Define Documentation

#define F_AUTOREADONLY   8

flag to tell us a file is set to autoreadonly

Definition at line 133 of file nbdsrv.h.

Referenced by handle_write(), mainloop(), and setupexport().

#define F_COPYONWRITE   4

flag to tell us a file is exported using copyonwrite

Definition at line 131 of file nbdsrv.h.

Referenced by cmdline(), dump_section(), expflush(), expread(), exptrim(), expwrite(), mainloop(), parse_cfile(), serveconnection(), and setupexport().

#define F_FIXED   4096

Client supports fixed new-style protocol (and can thus send us extra options.

Definition at line 142 of file nbdsrv.h.

#define F_FLUSH   128

Whether server wants FLUSH to be sent by the client.

Definition at line 137 of file nbdsrv.h.

Referenced by parse_cfile(), and send_export_info().

#define F_FUA   256

Whether server wants FUA to be sent by the client.

Definition at line 138 of file nbdsrv.h.

Referenced by parse_cfile(), and send_export_info().

#define F_MULTIFILE   2

flag to tell us a file is exported using -m

Definition at line 130 of file nbdsrv.h.

Referenced by cmdline(), dump_section(), parse_cfile(), and setupexport().

#define F_READONLY   1

Per-export flags:

flag to tell us a file is readonly

Definition at line 129 of file nbdsrv.h.

Referenced by cmdline(), dump_section(), expflush(), exptrim(), get_filepos(), handle_write(), mainloop(), parse_cfile(), send_export_info(), and setupexport().

#define F_ROTATIONAL   512

Whether server wants the client to implement the elevator algorithm.

Definition at line 139 of file nbdsrv.h.

Referenced by parse_cfile(), and send_export_info().

#define F_SDP   32

flag to tell us the export should be done using the Socket Direct Protocol for RDMA

Definition at line 135 of file nbdsrv.h.

Referenced by parse_cfile().

#define F_SPARSE   16

flag to tell us copyronwrite should use a sparse file

Definition at line 134 of file nbdsrv.h.

Referenced by expwrite(), and parse_cfile().

#define F_SYNC   64

Whether to fsync() after a write.

Definition at line 136 of file nbdsrv.h.

Referenced by expwrite(), parse_cfile(), and rawexpwrite().

#define F_TEMPORARY   1024

Whether the backing file is temporary and should be created then unlinked.

Definition at line 140 of file nbdsrv.h.

Referenced by parse_cfile(), and setupexport().

#define F_TREEFILES   8192

flag to tell us a file is exported using -t

Definition at line 143 of file nbdsrv.h.

Referenced by dump_section(), expflush(), exptrim(), get_filepos(), parse_cfile(), rawexpread(), rawexpwrite(), and setupexport().

#define F_TRIM   2048

Whether server wants TRIM (discard) to be sent by the client.

Definition at line 141 of file nbdsrv.h.

Referenced by parse_cfile(), and send_export_info().

#define msg (   prio,
  ... 
)    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)

Logging macros.

Todo:
remove this. We should use g_log in all cases, and use the logging mangler to redirect to syslog if and when necessary.

Definition at line 124 of file nbdsrv.h.

Referenced by authorized_client(), copyonwrite_prepare(), handle_modern_connection(), handle_request(), mainloop(), serveloop(), set_peername(), setup_servers(), setupexport(), and spawn_child().

#define MY_NAME   "nbd_server"

Definition at line 126 of file nbdsrv.h.

#define NBDS_ERR   g_quark_from_static_string("server-error-quark")

Error domain common for all NBD server errors.

Definition at line 88 of file nbdsrv.h.

Referenced by address_matches(), do_cfile_dir(), dosockopts(), main(), open_modern(), open_unix(), and parse_cfile().


Enumeration Type Documentation

enum NBDS_ERRS

NBD server error codes.

Enumerator:
NBDS_ERR_CFILE_NOTFOUND 

The configuration file is not found.

NBDS_ERR_CFILE_MISSING_GENERIC 

The (required) group "generic" is missing.

NBDS_ERR_CFILE_KEY_MISSING 

A (required) key is missing.

NBDS_ERR_CFILE_VALUE_INVALID 

A value is syntactically invalid.

NBDS_ERR_CFILE_VALUE_UNSUPPORTED 

A value is not supported in this build.

NBDS_ERR_CFILE_NO_EXPORTS 

A config file was specified that does not define any exports.

NBDS_ERR_CFILE_INCORRECT_PORT 

The reserved port was specified for an old-style export.

NBDS_ERR_CFILE_DIR_UNKNOWN 

A directory requested does not exist.

NBDS_ERR_CFILE_READDIR_ERR 

Error occurred during readdir()

NBDS_ERR_SO_LINGER 

Failed to set SO_LINGER to a socket.

NBDS_ERR_SO_REUSEADDR 

Failed to set SO_REUSEADDR to a socket.

NBDS_ERR_SO_KEEPALIVE 

Failed to set SO_KEEPALIVE to a socket.

NBDS_ERR_GAI 

Failed to get address info.

NBDS_ERR_SOCKET 

Failed to create a socket.

NBDS_ERR_BIND 

Failed to bind an address to socket.

NBDS_ERR_LISTEN 

Failed to start listening on a socket.

NBDS_ERR_SYS 

Underlying system call or library error.

Definition at line 93 of file nbdsrv.h.

enum VIRT_STYLE

Types of virtuatlization.

Enumerator:
VIRT_NONE 

No virtualization.

VIRT_IPLIT 

Literal IP address as part of the filename.

VIRT_IPHASH 

Replacing all dots in an ip address by a / before doing the same as in IPLIT.

VIRT_CIDR 

Every subnet in its own directory.

Definition at line 19 of file nbdsrv.h.


Function Documentation

bool address_matches ( const char *  mask,
const struct sockaddr *  addr,
GError **  err 
)

Check whether a given address matches a given netmask.

Parameters:
maskthe address or netmask to check against, in ASCII representation
addrthe address to check
Returns:
true if the address matches the mask, false otherwise; in case of failure to parse netmask, returns false with err set appropriately.
Todo:
decide what to do with v6-mapped IPv4 addresses.

Definition at line 29 of file nbdsrv.c.

References getmaskbyte(), NBDS_ERR, and NBDS_ERR_GAI.

Referenced by authorized_client().

int authorized_client ( CLIENT opts)

Check whether a client is allowed to connect.

Works with an authorization file which contains one line per machine or network, with CIDR-style netmasks.

Parameters:
optsThe client who's trying to connect.
Returns:
0 - authorization refused, 1 - OK

Definition at line 113 of file nbdsrv.c.

References address_matches(), SERVER::authname, CLIENT::clientaddr, LINELEN, msg, and CLIENT::server.

Referenced by handle_modern_connection().

SERVER* dup_serve ( const SERVER *const  s)

duplicate server

Parameters:
sthe old server we want to duplicate
Returns:
new duplicated server

Definition at line 157 of file nbdsrv.c.

References SERVER::authname, SERVER::cidrlen, SERVER::cowdir, SERVER::expected_size, SERVER::exportname, SERVER::flags, SERVER::listenaddr, SERVER::max_connections, SERVER::postrun, SERVER::prerun, SERVER::servename, SERVER::transactionlog, and SERVER::virtstyle.

int exptrim ( struct nbd_request req,
CLIENT client 
)

Punch a hole in the backend file (if supported by the current system).

Parameters:
reqthe request for which this is being processed
clientthe client for which we're processing this request

Definition at line 240 of file nbdsrv.c.

References DEBUG, delete_treefile(), CLIENT::export, CLIENT::exportname, CLIENT::exportsize, F_COPYONWRITE, F_READONLY, F_TREEFILES, FILE_INFO::fhandle, SERVER::flags, nbd_request::from, nbd_request::len, punch_hole(), CLIENT::server, FILE_INFO::startoff, and TREEPAGESIZE.

Referenced by handle_trim(), and mainloop().

uint8_t getmaskbyte ( int  masklen)

Gets a byte to allow for address masking.

Parameters:
masklenthe length of the requested mask.
Returns:
if the length of the mask is 8 or longer, 0xFF. Otherwise, a byte with `masklen' number of leading bits set to 1, everything else set to 0.

Definition at line 101 of file nbdsrv.c.

Referenced by address_matches(), and set_peername().

void myseek ( int  handle,
off_t  a 
)

seek to a position in a file, with error handling.

Parameters:
handlea filedescriptor
aposition to seek to
Todo:
get rid of this.

Definition at line 292 of file nbdsrv.c.

References err().

Referenced by expread(), expwrite(), and open_treefile().

uint64_t size_autodetect ( int  fhandle)

Detect the size of a file.

Parameters:
fhandleAn open filedescriptor
Returns:
the size of the file, or UINT64_MAX if detection was impossible.

Definition at line 199 of file nbdsrv.c.

References __attribute__, and DEBUG.

Referenced by setupexport().