File: nbd.c

package info (click to toggle)
erofs-utils 1.9-1
  • links: PTS
  • area: main
  • in suites:
  • size: 1,392 kB
  • sloc: ansic: 28,406; makefile: 202; sh: 33
file content (636 lines) | stat: -rw-r--r-- 13,791 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
/*
 * Copyright (C) 2025 Alibaba Cloud
 */
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include "erofs/io.h"
#include "erofs/err.h"
#include "erofs/print.h"
#include "liberofs_nbd.h"

#ifdef HAVE_NETLINK_GENL_GENL_H
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#endif

#define NBD_SET_SOCK		_IO( 0xab, 0 )
#define NBD_SET_BLKSIZE		_IO( 0xab, 1 )
#define NBD_DO_IT		_IO( 0xab, 3 )
#define NBD_CLEAR_SOCK		_IO( 0xab, 4 )
#define NBD_SET_SIZE_BLOCKS     _IO( 0xab, 7 )
#define NBD_DISCONNECT		_IO( 0xab, 8 )
#define NBD_SET_TIMEOUT		_IO( 0xab, 9 )
#define NBD_SET_FLAGS		_IO( 0xab, 10)

#define NBD_REQUEST_MAGIC	0x25609513
#define NBD_REPLY_MAGIC		0x67446698

#define NBD_FLAG_READ_ONLY	(1 << 1)	/* device is read-only */

/*
 * This is the reply packet that nbd-server sends back to the client after
 * it has completed an I/O request (or an error occurs).
 */
struct nbd_reply {
	__be32 magic;		/* NBD_REPLY_MAGIC */
	__be32 error;		/* 0 = ok, else error */
	union {
		__be64 cookie;	/* Opaque identifier from request */
		char handle[8];	/* older spelling of cookie */
	};
} __packed;

long erofs_nbd_in_service(int nbdnum)
{
	int fd, err;
	char s[32];

	(void)snprintf(s, sizeof(s), "/sys/block/nbd%d/size", nbdnum);
	fd = open(s, O_RDONLY);
	if (fd < 0)
		return -errno;
	err = read(fd, s, sizeof(s));
	if (err < 0) {
		err = -errno;
		close(fd);
		return err;
	}
	close(fd);
	if (!memcmp(s, "0\n", sizeof("0\n") - 1))
		return -ENOTCONN;

	(void)snprintf(s, sizeof(s), "/sys/block/nbd%d/pid", nbdnum);
	fd = open(s, O_RDONLY);
	if (fd < 0)
		return -errno;
	err = read(fd, s, sizeof(s));
	if (err < 0) {
		err = -errno;
		close(fd);
		return err;
	}
	close(fd);
	return strtol(s, NULL, 10);
}

int erofs_nbd_devscan(void)
{
	DIR *_dir;
	int err;

	_dir = opendir("/sys/block");
	if (!_dir) {
		fprintf(stderr, "failed to opendir /sys/block: %s\n",
			strerror(errno));
		return -errno;
	}

	while (1) {
		struct dirent *dp;
		char path[64];

		/*
		 * set errno to 0 before calling readdir() in order to
		 * distinguish end of stream and from an error.
		 */
		errno = 0;
		dp = readdir(_dir);
		if (!dp) {
			if (errno)
				err = -errno;
			else
				err = -EBUSY;
			break;
		}

		if (strncmp(dp->d_name, "nbd", 3))
			continue;

		/* Skip nbdX with valid `pid` or `backend` */
		err = snprintf(path, sizeof(path), "%s/pid", dp->d_name);
		if (err < 0)
			continue;
		if (!faccessat(dirfd(_dir), path, F_OK, 0))
			continue;
		err = snprintf(path, sizeof(path), "%s/backend", dp->d_name);
		if (err < 0)
			continue;
		if (!faccessat(dirfd(_dir), path, F_OK, 0))
			continue;
		err = atoi(dp->d_name + 3);
		break;
	}
	closedir(_dir);
	return err;
}

int erofs_nbd_connect(int nbdfd, int blkbits, u64 blocks)
{
	int sv[2], err;

	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
	if (err < 0)
		return -errno;

	err = ioctl(nbdfd, NBD_CLEAR_SOCK, 0);
	if (err < 0)
		goto err_out;

	err = ioctl(nbdfd, NBD_SET_BLKSIZE, 1U << blkbits);
	if (err < 0)
		goto err_out;

	err = ioctl(nbdfd, NBD_SET_SIZE_BLOCKS, blocks);
	if (err < 0)
		goto err_out;

	err = ioctl(nbdfd, NBD_SET_TIMEOUT, 0);
	if (err < 0)
		goto err_out;

	err = ioctl(nbdfd, NBD_SET_FLAGS, NBD_FLAG_READ_ONLY);
	if (err < 0)
		goto err_out;

	err = ioctl(nbdfd, NBD_SET_SOCK, sv[1]);
	if (err < 0)
		goto err_out;
	return sv[0];
err_out:
	close(sv[0]);
	close(sv[1]);
	return err;
}

char *erofs_nbd_get_identifier(int nbdnum)
{
	char s[32], *line = NULL;
	size_t n;
	FILE *f;
	int err;

	(void)snprintf(s, sizeof(s), "/sys/block/nbd%d/backend", nbdnum);
	f = fopen(s, "r");
	if (!f) {
		if (errno == ENOENT)
			return NULL;
		return ERR_PTR(-errno);
	}
	err = getline(&line, &n, f);
	if (err < 0)
		err = -errno;
	fclose(f);
	if (err < 0)
		return ERR_PTR(err);
	if (!err) {
		free(line);
		return NULL;
	}
	if (line[err - 1] == '\n')
		line[err - 1] = '\0';
	return line;
}

int erofs_nbd_get_index_from_minor(int minor)
{
	char s[32], *line = NULL;
	int ret = -ENOENT;
	size_t n;
	FILE *f;

	(void)snprintf(s, sizeof(s),
		       "/sys/dev/block/" __erofs_stringify(EROFS_NBD_MAJOR) ":%d/uevent", minor);
	f = fopen(s, "r");
	if (!f)
		return -errno;

	while (getline(&line, &n, f) >= 0) {
		if (strncmp(line, "DEVNAME=nbd", sizeof("DEVNAME=nbd") - 1))
			continue;
		ret = strtoul(line + sizeof("DEVNAME=nbd") - 1, NULL, 10);
		break;
	}
	free(line);
	return ret;
}

#if defined(HAVE_NETLINK_GENL_GENL_H) && defined(HAVE_LIBNL_GENL_3)
enum {
	NBD_ATTR_UNSPEC,
	NBD_ATTR_INDEX,
	NBD_ATTR_SIZE_BYTES,
	NBD_ATTR_BLOCK_SIZE_BYTES,
	NBD_ATTR_TIMEOUT,
	NBD_ATTR_SERVER_FLAGS,
	NBD_ATTR_CLIENT_FLAGS,
	NBD_ATTR_SOCKETS,
	NBD_ATTR_DEAD_CONN_TIMEOUT,
	NBD_ATTR_DEVICE_LIST,
	NBD_ATTR_BACKEND_IDENTIFIER,
	__NBD_ATTR_MAX,
};
#define NBD_ATTR_MAX (__NBD_ATTR_MAX - 1)

enum {
	NBD_SOCK_ITEM_UNSPEC,
	NBD_SOCK_ITEM,
	__NBD_SOCK_ITEM_MAX,
};
#define NBD_SOCK_ITEM_MAX (__NBD_SOCK_ITEM_MAX - 1)

enum {
	NBD_SOCK_UNSPEC,
	NBD_SOCK_FD,
	__NBD_SOCK_MAX,
};
#define NBD_SOCK_MAX (__NBD_SOCK_MAX - 1)

enum {
	NBD_CMD_UNSPEC,
	NBD_CMD_CONNECT,
	NBD_CMD_DISCONNECT,
	NBD_CMD_RECONFIGURE,
	__NBD_CMD_MAX,
};

/* client behavior specific flags */
/* delete the nbd device on disconnect */
#define NBD_CFLAG_DESTROY_ON_DISCONNECT		(1 << 0)
/* disconnect the nbd device on close by last opener */
#define NBD_CFLAG_DISCONNECT_ON_CLOSE		(1 << 1)

static struct nl_sock *erofs_nbd_get_nl_sock(int *driver_id)
{
	struct nl_sock *socket;
	int err;

	socket = nl_socket_alloc();
	if (!socket) {
		erofs_err("Couldn't allocate netlink socket");
		return ERR_PTR(-ENOMEM);
	}

	err = genl_connect(socket);
	if (err) {
		erofs_err("Couldn't connect to the generic netlink socket");
		return ERR_PTR(err);
	}

	err = genl_ctrl_resolve(socket, "nbd");
	if (err < 0) {
		erofs_err("Failed to resolve NBD netlink family. Ensure the NBD module is loaded and it supports netlink.");
		return ERR_PTR(err);
	}
	*driver_id = err;
	return socket;
}

struct erofs_nbd_nl_cfg_cbctx {
	int *index;
	int errcode;
};

static int erofs_nbd_nl_cfg_cb(struct nl_msg *msg, void *arg)
{
	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
	struct nlattr *msg_attr[NBD_ATTR_MAX + 1];
	struct erofs_nbd_nl_cfg_cbctx *ctx = arg;
	int err;

	err = nla_parse(msg_attr, NBD_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
			genlmsg_attrlen(gnlh, 0), NULL);
	if (err) {
		erofs_err("Invalid response from the kernel");
		ctx->errcode = err;
	}

	if (!msg_attr[NBD_ATTR_INDEX]) {
		erofs_err("Did not receive index from the kernel");
		ctx->errcode = -EBADMSG;
	}
	*ctx->index = nla_get_u32(msg_attr[NBD_ATTR_INDEX]);
	erofs_dbg("Connected /dev/nbd%d", *ctx->index);
	ctx->errcode = 0;
	return NL_OK;
}

int erofs_nbd_nl_connect(int *index, int blkbits, u64 blocks,
			 const char *identifier)
{
	struct erofs_nbd_nl_cfg_cbctx cbctx = {
		.index = index,
	};
	struct nlattr *sock_attr = NULL, *sock_opt = NULL;
	struct nl_sock *socket;
	struct nl_msg *msg;
	int sv[2], err;
	int driver_id;

	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
	if (err < 0)
		return -errno;

	socket = erofs_nbd_get_nl_sock(&driver_id);
	if (IS_ERR(socket)) {
		err = PTR_ERR(socket);
		goto err_out;
	}
	nl_socket_modify_cb(socket, NL_CB_VALID, NL_CB_CUSTOM,
			    erofs_nbd_nl_cfg_cb, &cbctx);

	msg = nlmsg_alloc();
	if (!msg) {
		erofs_err("Couldn't allocate netlink message");
		err = -ENOMEM;
		goto err_nls_free;
	}

	genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
		    NBD_CMD_CONNECT, 0);
	if (*index >= 0)
		NLA_PUT_U32(msg, NBD_ATTR_INDEX, *index);
	NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, 1u << blkbits);
	NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, blocks << blkbits);
	NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, NBD_FLAG_READ_ONLY);
	NLA_PUT_U64(msg, NBD_ATTR_TIMEOUT, 0);
	NLA_PUT_U64(msg, NBD_ATTR_DEAD_CONN_TIMEOUT, EROFS_NBD_DEAD_CONN_TIMEOUT);
	if (identifier)
		NLA_PUT_STRING(msg, NBD_ATTR_BACKEND_IDENTIFIER, identifier);

	err = -EINVAL;
	sock_attr = nla_nest_start(msg, NBD_ATTR_SOCKETS);
	if (!sock_attr) {
		erofs_err("Couldn't nest the sockets for our connection");
		goto err_nlm_free;
	}

	sock_opt = nla_nest_start(msg, NBD_SOCK_ITEM);
	if (!sock_opt) {
		nla_nest_cancel(msg, sock_attr);
		goto err_nlm_free;
	}
	NLA_PUT_U32(msg, NBD_SOCK_FD, sv[1]);
	nla_nest_end(msg, sock_opt);
	nla_nest_end(msg, sock_attr);

	err = nl_send_sync(socket, msg);
	if (err)
		goto err_out;
	nl_socket_free(socket);
	if (cbctx.errcode)
		return cbctx.errcode;
	return sv[0];

nla_put_failure:
	if (sock_opt)
		nla_nest_cancel(msg, sock_opt);
	if (sock_attr)
		nla_nest_cancel(msg, sock_attr);
err_nlm_free:
	nlmsg_free(msg);
err_nls_free:
	nl_socket_free(socket);
err_out:
	close(sv[0]);
	close(sv[1]);
	return err;
}

int erofs_nbd_nl_reconnect(int index, const char *identifier)
{
	struct nlattr *sock_attr = NULL, *sock_opt = NULL;
	struct nl_sock *socket;
	struct nl_msg *msg;
	int sv[2], err;
	int driver_id;

	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
	if (err < 0)
		return -errno;

	socket = erofs_nbd_get_nl_sock(&driver_id);
	if (IS_ERR(socket)) {
		err = PTR_ERR(socket);
		goto err_out;
	}

	msg = nlmsg_alloc();
	if (!msg) {
		erofs_err("Couldn't allocate netlink message");
		err = -ENOMEM;
		goto err_nls_free;
	}

	genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
		    NBD_CMD_RECONFIGURE, 0);
	NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
	if (identifier)
		NLA_PUT_STRING(msg, NBD_ATTR_BACKEND_IDENTIFIER, identifier);

	err = -EINVAL;
	sock_attr = nla_nest_start(msg, NBD_ATTR_SOCKETS);
	if (!sock_attr) {
		erofs_err("Couldn't nest the sockets for our connection");
		goto err_nlm_free;
	}

	sock_opt = nla_nest_start(msg, NBD_SOCK_ITEM);
	if (!sock_opt) {
		nla_nest_cancel(msg, sock_attr);
		goto err_nlm_free;
	}
	NLA_PUT_U32(msg, NBD_SOCK_FD, sv[1]);
	nla_nest_end(msg, sock_opt);
	nla_nest_end(msg, sock_attr);

	err = nl_send_sync(socket, msg);
	if (err)
		goto err_out;
	nl_socket_free(socket);
	return sv[0];

nla_put_failure:
	if (sock_opt)
		nla_nest_cancel(msg, sock_opt);
	if (sock_attr)
		nla_nest_cancel(msg, sock_attr);
err_nlm_free:
	nlmsg_free(msg);
err_nls_free:
	nl_socket_free(socket);
err_out:
	close(sv[0]);
	close(sv[1]);
	return err;
}

int erofs_nbd_nl_reconfigure(int index, const char *identifier,
			     bool autoclear)
{
	struct nl_sock *socket;
	struct nl_msg *msg;
	int err, driver_id;
	unsigned int cflags;

	socket = erofs_nbd_get_nl_sock(&driver_id);
	if (IS_ERR(socket))
		return PTR_ERR(socket);

	msg = nlmsg_alloc();
	if (!msg) {
		erofs_err("Couldn't allocate netlink message");
		err = -ENOMEM;
		goto err_nls_free;
	}

	err = -EINVAL;
	genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
		    NBD_CMD_RECONFIGURE, 0);
	NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
	if (identifier)
		NLA_PUT_STRING(msg, NBD_ATTR_BACKEND_IDENTIFIER, identifier);

	cflags = (autoclear ? NBD_CFLAG_DISCONNECT_ON_CLOSE : 0);
	NLA_PUT_U64(msg, NBD_ATTR_CLIENT_FLAGS, cflags);

	err = nl_send_sync(socket, msg);
	nl_socket_free(socket);
	return err;

nla_put_failure:
	nlmsg_free(msg);
err_nls_free:
	nl_socket_free(socket);
	return err;
}

int erofs_nbd_nl_disconnect(int index)
{
	struct nl_sock *socket;
	struct nl_msg *msg;
	int driver_id, err;

	socket= erofs_nbd_get_nl_sock(&driver_id);
	if (IS_ERR(socket))
		return PTR_ERR(socket);
	msg = nlmsg_alloc();
	if (!msg) {
		erofs_err("Couldn't allocate netlink message");
		err = -ENOMEM;
		goto err_nls_free;
	}

	err = -EINVAL;
	genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, 0,
		    NBD_CMD_DISCONNECT, 0);
	NLA_PUT_U32(msg, NBD_ATTR_INDEX, index);
	err = nl_send_sync(socket, msg);
	if (err < 0)
		erofs_err("Failed to disconnect device %d, check dmesg", err);
	nl_socket_free(socket);
	return err;
nla_put_failure:
	erofs_err("Failed to create netlink message");
	nlmsg_free(msg);
err_nls_free:
	nl_socket_free(socket);
	return err;
}
#else
int erofs_nbd_nl_connect(int *index, int blkbits, u64 blocks,
			 const char *identifier)
{
	return -EOPNOTSUPP;
}

int erofs_nbd_nl_reconnect(int index, const char *identifier)
{
	return -EOPNOTSUPP;
}

int erofs_nbd_nl_reconfigure(int index, const char *identifier,
			     bool autoclear)
{
	return -EOPNOTSUPP;
}
int erofs_nbd_nl_disconnect(int index)
{
	return -EOPNOTSUPP;
}
#endif

int erofs_nbd_do_it(int nbdfd)
{
	int err;

	err = ioctl(nbdfd, NBD_DO_IT, 0);
	if (err < 0) {
		if (errno == EPIPE)
			/*
			 * `ioctl(NBD_DO_IT)` normally returns EPIPE when someone has
			 * disconnected the socket via NBD_DISCONNECT.  We do not want
			 * to return 1 in that case.
			*/
			err = 0;
		else
			err = -errno;
	}
	if (err)
		erofs_err("NBD_DO_IT ends with %s", erofs_strerror(err));
	close(nbdfd);
	return err;
}

int erofs_nbd_get_request(int skfd, struct erofs_nbd_request *rq)
{
	struct erofs_vfile vf = { .fd = skfd };
	int err;

	err = erofs_io_read(&vf, rq, sizeof(*rq));
	if (err < sizeof(*rq))
		return -EPIPE;

	if (rq->magic != cpu_to_be32(NBD_REQUEST_MAGIC))
		return -EIO;

	rq->type = be32_to_cpu((__be32)rq->type);
	rq->from = be64_to_cpu((__be64)rq->from);
	rq->len = be32_to_cpu((__be32)rq->len);
	return 0;
}

int erofs_nbd_send_reply_header(int skfd, __le64 cookie, int err)
{
	struct nbd_reply reply = {
		.magic = cpu_to_be32(NBD_REPLY_MAGIC),
		.error = cpu_to_be32(err),
		.cookie = cookie,
	};
	int ret;

	ret = write(skfd, &reply, sizeof(reply));
	if (ret == sizeof(reply))
		return 0;
	return ret < 0 ? -errno : -EIO;
}

int erofs_nbd_disconnect(int nbdfd)
{
	int err, err2;

	err = ioctl(nbdfd, NBD_DISCONNECT);
	err2 = ioctl(nbdfd, NBD_CLEAR_SOCK);
	return err ?: err2;
}