Network Block Device  @PACKAGE_VERSION@
Data Structures | Macros | 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...
 

Macros

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

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. More...
 
uint8_t getmaskbyte (int masklen) G_GNUC_PURE
 Gets a byte to allow for address masking. More...
 
int authorized_client (CLIENT *opts)
 Check whether a client is allowed to connect. More...
 
SERVERdup_serve (const SERVER *const s)
 duplicate server More...
 
uint64_t size_autodetect (int fhandle)
 Detect the size of a file. More...
 
int exptrim (struct nbd_request *req, CLIENT *client)
 Punch a hole in the backend file (if supported by the current system). More...
 
void myseek (int handle, off_t a)
 seek to a position in a file, with error handling. More...
 

Macro Definition Documentation

#define F_AUTOREADONLY   8

flag to tell us a file is set to autoreadonly

Definition at line 134 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(), 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 143 of file nbdsrv.h.

#define F_FLUSH   128

Whether server wants FLUSH to be sent by the client.

Definition at line 138 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 139 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(), 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 140 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 136 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 135 of file nbdsrv.h.

Referenced by expwrite(), and parse_cfile().

#define F_SYNC   64

Whether to fsync() after a write.

Definition at line 137 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 141 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 144 of file nbdsrv.h.

Referenced by dump_section(), expflush(), 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 142 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 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 30 of file nbdsrv.c.

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 110 of file nbdsrv.c.

References msg.

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 154 of file nbdsrv.c.

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 237 of file nbdsrv.c.

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 98 of file nbdsrv.c.

Referenced by 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 289 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 196 of file nbdsrv.c.

Referenced by setupexport().