xref: /linux-6.15/include/uapi/linux/nbd.h (revision e49dacc7)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-1.0+ WITH Linux-syscall-note */
2607ca46eSDavid Howells /*
3607ca46eSDavid Howells  * 1999 Copyright (C) Pavel Machek, [email protected]. This code is GPL.
4607ca46eSDavid Howells  * 1999/11/04 Copyright (C) 1999 VMware, Inc. (Regis "HPReg" Duchesne)
5607ca46eSDavid Howells  *            Made nbd_end_request() use the io_request_lock
6607ca46eSDavid Howells  * 2001 Copyright (C) Steven Whitehouse
7607ca46eSDavid Howells  *            New nbd_end_request() for compatibility with new linux block
8607ca46eSDavid Howells  *            layer code.
9607ca46eSDavid Howells  * 2003/06/24 Louis D. Langholtz <[email protected]>
10607ca46eSDavid Howells  *            Removed unneeded blksize_bits field from nbd_device struct.
11607ca46eSDavid Howells  *            Cleanup PARANOIA usage & code.
12607ca46eSDavid Howells  * 2004/02/19 Paul Clements
13607ca46eSDavid Howells  *            Removed PARANOIA, plus various cleanup and comments
14daf376a3SEric Blake  * 2023 Copyright Red Hat
152686eb84SEric Blake  *            Link to userspace extensions, favor cookie over handle.
16607ca46eSDavid Howells  */
17607ca46eSDavid Howells 
18607ca46eSDavid Howells #ifndef _UAPILINUX_NBD_H
19607ca46eSDavid Howells #define _UAPILINUX_NBD_H
20607ca46eSDavid Howells 
21607ca46eSDavid Howells #include <linux/types.h>
22607ca46eSDavid Howells 
23607ca46eSDavid Howells #define NBD_SET_SOCK	_IO( 0xab, 0 )
24607ca46eSDavid Howells #define NBD_SET_BLKSIZE	_IO( 0xab, 1 )
25607ca46eSDavid Howells #define NBD_SET_SIZE	_IO( 0xab, 2 )
26607ca46eSDavid Howells #define NBD_DO_IT	_IO( 0xab, 3 )
27607ca46eSDavid Howells #define NBD_CLEAR_SOCK	_IO( 0xab, 4 )
28607ca46eSDavid Howells #define NBD_CLEAR_QUE	_IO( 0xab, 5 )
29607ca46eSDavid Howells #define NBD_PRINT_DEBUG	_IO( 0xab, 6 )
30607ca46eSDavid Howells #define NBD_SET_SIZE_BLOCKS	_IO( 0xab, 7 )
31607ca46eSDavid Howells #define NBD_DISCONNECT  _IO( 0xab, 8 )
32607ca46eSDavid Howells #define NBD_SET_TIMEOUT _IO( 0xab, 9 )
33607ca46eSDavid Howells #define NBD_SET_FLAGS   _IO( 0xab, 10)
34607ca46eSDavid Howells 
35daf376a3SEric Blake /*
36daf376a3SEric Blake  * See also https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md
37daf376a3SEric Blake  * for additional userspace extensions not yet utilized in the kernel module.
38daf376a3SEric Blake  */
39daf376a3SEric Blake 
40607ca46eSDavid Howells enum {
41607ca46eSDavid Howells 	NBD_CMD_READ = 0,
42607ca46eSDavid Howells 	NBD_CMD_WRITE = 1,
43607ca46eSDavid Howells 	NBD_CMD_DISC = 2,
4475f187abSAlex Bligh 	NBD_CMD_FLUSH = 3,
45*e49dacc7SWouter Verhelst 	NBD_CMD_TRIM = 4,
46daf376a3SEric Blake 	/* userspace defines additional extension commands */
47*e49dacc7SWouter Verhelst 	NBD_CMD_WRITE_ZEROES = 6,
48607ca46eSDavid Howells };
49607ca46eSDavid Howells 
50a2c97909SJosef Bacik /* values for flags field, these are server interaction specific. */
51607ca46eSDavid Howells #define NBD_FLAG_HAS_FLAGS	(1 << 0) /* nbd-server supports flags */
52607ca46eSDavid Howells #define NBD_FLAG_READ_ONLY	(1 << 1) /* device is read-only */
5375f187abSAlex Bligh #define NBD_FLAG_SEND_FLUSH	(1 << 2) /* can flush writeback cache */
54685c9b24SShaun McDowell #define NBD_FLAG_SEND_FUA	(1 << 3) /* send FUA (forced unit access) */
557543ae22SWouter Verhelst #define NBD_FLAG_ROTATIONAL	(1 << 4) /* device is rotational */
56607ca46eSDavid Howells #define NBD_FLAG_SEND_TRIM	(1 << 5) /* send trim/discard */
57*e49dacc7SWouter Verhelst #define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* supports WRITE_ZEROES */
587543ae22SWouter Verhelst /* there is a gap here to match userspace */
5963db89eaSJens Axboe #define NBD_FLAG_CAN_MULTI_CONN	(1 << 8)	/* Server supports multiple connections per export. */
60607ca46eSDavid Howells 
61685c9b24SShaun McDowell /* values for cmd flags in the upper 16 bits of request type */
62685c9b24SShaun McDowell #define NBD_CMD_FLAG_FUA	(1 << 16) /* FUA (forced unit access) op */
63*e49dacc7SWouter Verhelst #define NBD_CMD_FLAG_NO_HOLE	(1 << 17) /* Do not punch a hole for WRITE_ZEROES */
64685c9b24SShaun McDowell 
65a2c97909SJosef Bacik /* These are client behavior specific flags. */
66a2c97909SJosef Bacik #define NBD_CFLAG_DESTROY_ON_DISCONNECT	(1 << 0) /* delete the nbd device on
67a2c97909SJosef Bacik 						    disconnect. */
6808ba91eeSDoron Roberts-Kedes #define NBD_CFLAG_DISCONNECT_ON_CLOSE (1 << 1) /* disconnect the nbd device on
6908ba91eeSDoron Roberts-Kedes 						*  close by last opener.
7008ba91eeSDoron Roberts-Kedes 						*/
71a2c97909SJosef Bacik 
72607ca46eSDavid Howells /* userspace doesn't need the nbd_device structure */
73607ca46eSDavid Howells 
74607ca46eSDavid Howells /* These are sent over the network in the request/reply magic fields */
75607ca46eSDavid Howells 
76607ca46eSDavid Howells #define NBD_REQUEST_MAGIC 0x25609513
77607ca46eSDavid Howells #define NBD_REPLY_MAGIC 0x67446698
78607ca46eSDavid Howells /* Do *not* use magics: 0x12560953 0x96744668. */
79daf376a3SEric Blake /* magic 0x668e33ef for structured reply not supported by kernel yet */
80607ca46eSDavid Howells 
81607ca46eSDavid Howells /*
82607ca46eSDavid Howells  * This is the packet used for communication between client and
83607ca46eSDavid Howells  * server. All data are in network byte order.
84607ca46eSDavid Howells  */
85607ca46eSDavid Howells struct nbd_request {
86daf376a3SEric Blake 	__be32 magic;	/* NBD_REQUEST_MAGIC	*/
87daf376a3SEric Blake 	__be32 type;	/* See NBD_CMD_*	*/
882686eb84SEric Blake 	union {
892686eb84SEric Blake 		__be64 cookie;	/* Opaque identifier for request	*/
902686eb84SEric Blake 		char handle[8];	/* older spelling of cookie		*/
912686eb84SEric Blake 	};
92607ca46eSDavid Howells 	__be64 from;
93607ca46eSDavid Howells 	__be32 len;
94607ca46eSDavid Howells } __attribute__((packed));
95607ca46eSDavid Howells 
96607ca46eSDavid Howells /*
97607ca46eSDavid Howells  * This is the reply packet that nbd-server sends back to the client after
98607ca46eSDavid Howells  * it has completed an I/O request (or an error occurs).
99607ca46eSDavid Howells  */
100607ca46eSDavid Howells struct nbd_reply {
101daf376a3SEric Blake 	__be32 magic;		/* NBD_REPLY_MAGIC	*/
102607ca46eSDavid Howells 	__be32 error;		/* 0 = ok, else error	*/
1032686eb84SEric Blake 	union {
1042686eb84SEric Blake 		__be64 cookie;	/* Opaque identifier from request	*/
1052686eb84SEric Blake 		char handle[8];	/* older spelling of cookie		*/
1062686eb84SEric Blake 	};
107607ca46eSDavid Howells };
108607ca46eSDavid Howells #endif /* _UAPILINUX_NBD_H */
109