File: blkpr.c

package info (click to toggle)
util-linux 2.42~rc1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 101,864 kB
  • sloc: ansic: 185,733; sh: 24,450; yacc: 1,288; makefile: 534; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (433 lines) | stat: -rw-r--r-- 11,436 bytes parent folder | download | duplicates (2)
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
/*
 * blkpr.c -- persistent reservations on a block device.
 *
 * Copyright (C) 2021-2022 zhenwei pi <pizhenwei@bytedance.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program uses IOC_PR_XXX ioctl to run persistent reservations
 * command on a block device if the device supports it.
 */
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <locale.h>
#include <sys/ioctl.h>
#include <linux/pr.h>

#include "nls.h"
#include "c.h"
#include "closestream.h"
#include "strutils.h"
#include "xalloc.h"

struct type_string {
	int type;
	char *str;
	char *desc;
};

/* This array should keep align with enum pr_type of linux/types.h */
static const struct type_string pr_type[] = {
	{
		PR_WRITE_EXCLUSIVE, "write-exclusive", N_(
"    Only the initiator that owns the reservation can write to the device. Any\n"
"    initiator can read from the device."
		)
	}, {
		PR_EXCLUSIVE_ACCESS, "exclusive-access", N_(
"    Only the initiator that owns the reservation can access the device."
		)
	}, {
		PR_WRITE_EXCLUSIVE_REG_ONLY, "write-exclusive-reg-only", N_(
"    Only initiators with a registered key can write to the device, any initiator\n"
"    can read from the device.")
	}, {
		PR_EXCLUSIVE_ACCESS_REG_ONLY, "exclusive-access-reg-only", N_(
"    Only initiators with a registered key can access the device."
		)
	}, {
		PR_WRITE_EXCLUSIVE_ALL_REGS, "write-exclusive-all-regs", N_(
"    Only initiators with a registered key can write to the device. Any\n"
"    initiator can read from the device.  All initiators with a registered\n"
"    key are considered reservation holders.  Please, reference the SPC sp:ec\n"
"    on the meaning of a reservation holder if you want to use this type."
		)
	}, {
		PR_EXCLUSIVE_ACCESS_ALL_REGS, "exclusive-access-all-regs", N_(
"    Only initiators with a registered key can access the device. All initiators\n"
"    with a registered key are considered reservation holders. Please reference\n"
"    the SPC spec on the meaning of a reservation holder if you want to use this\n"
"    type."
		)
	}
};

static const struct type_string pr_command[] = {
	{
		IOC_PR_REGISTER, "register", N_(
"    This command registers a new reservation if the key argument\n"
"    is non-null. If no existing reservation exists oldkey must be zero, if\n"
"    an existing reservation should be replaced oldkey must contain the old\n"
"    reservation key. If the key argument is 0 it unregisters the existing\n"
"    reservation passed in oldkey."
		)
	}, {
		IOC_PR_RESERVE, "reserve", N_(
"    This command reserves the device and thus restricts access for other devices\n"
"    based on the type argument.  The key argument must be the existing\n"
"    reservation key for the device as acquired by the register, preempt,\n"
"    preempt-abort commands."
)
	}, {
		IOC_PR_RELEASE, "release", N_(
"    This command releases the reservation specified by key and flags and thus\n"
"    removes any access restriction implied by it."
		)
	}, {
		IOC_PR_PREEMPT, "preempt", N_(
"    This command releases the existing reservation referred to by old_key and\n"
"    replaces it with a new reservation of type for the reservation key key."
		)
	}, {
		IOC_PR_PREEMPT_ABORT, "preempt-abort", N_(
"    This command works like preempt except that it also aborts any outstanding\n"
"    command sent over a connection identified by oldkey."
		)
	}, {
		IOC_PR_CLEAR, "clear", N_(
"    This command unregisters both key and any other reservation key registered\n"
"    with the device and drops any existing reservation."
		)
	},

#ifdef IOC_PR_READ_KEYS
	{
		IOC_PR_READ_KEYS, "read-keys", N_(
"    This command lists reservation keys currently registered with the device."
		)
	},
#endif

#ifdef IOC_PR_READ_RESERVATION
	{
		IOC_PR_READ_RESERVATION, "read-reservation", N_(
"    This command shows the current reservation."
		)
	},
#endif
};

static const struct type_string pr_flag[] = {
	{
		PR_FL_IGNORE_KEY, "ignore-key", N_(
"    Ignore the existing reservation key.  This is commonly supported for\n"
"    register command, and some implementation may support the flag for reserve\n"
"    command."
		)
	}
};

static void print_type(FILE *out, const struct type_string *ts, size_t nmem)
{
	size_t i;


	for (i = 0; i < nmem; i++) {
		if (i)
			fputs(USAGE_SEPARATOR, out);

		fprintf(out, "  * %s:\n", ts[i].str);
		fprintf(out, "%s\n", _(ts[i].desc));
	}
}


static int parse_type_by_str(const struct type_string *ts, int nmem, char *pattern)
{
	int i;

	for (i = 0; i < nmem; i++) {
		if (!strcmp(ts[i].str, pattern))
			return ts[i].type;
	}

	return -1;
}



#define PRINT_SUPPORTED(XX) \
	static void print_##XX(FILE *out) \
	{ print_type(out, XX, ARRAY_SIZE(XX)); }

#define PARSE(XX) \
	static int parse_##XX(char *pattern) \
	{ return parse_type_by_str(XX, ARRAY_SIZE(XX), pattern); }

PRINT_SUPPORTED(pr_type)
PRINT_SUPPORTED(pr_command)
PRINT_SUPPORTED(pr_flag)

PARSE(pr_type)
PARSE(pr_command)
PARSE(pr_flag)

#ifdef IOC_PR_READ_KEYS
static int do_pr_read_keys(int fd)
{
	struct pr_read_keys pr_rk;
	uint32_t num_keys = 8;
	uint64_t *keys = NULL;
	int ret;

	/* Loop to grow keys[] until it is large enough */
	do {
		num_keys *= 2;
		keys = xreallocarray(keys, num_keys, sizeof(keys[0]));

		pr_rk.keys_ptr = (uintptr_t)keys;
		pr_rk.num_keys = num_keys;

		ret = ioctl(fd, IOC_PR_READ_KEYS, &pr_rk);
		if (ret)
			goto out;
	} while (pr_rk.num_keys > num_keys);

	if (pr_rk.num_keys) {
		for (uint32_t i = 0; i < pr_rk.num_keys; i++) {
			printf(_("%#" PRIx64 "\n"), (uint64_t)keys[i]);
		}
	} else {
		printf(_("No registered keys\n"));
	}

out:
	free(keys);
	return ret;
}
#endif /* IOC_PR_READ_KEYS */

#ifdef IOC_PR_READ_RESERVATION
static inline const char *type_to_str(const struct type_string *ts, int nmem,
                                      int type)
{
	int i;

	for (i = 0; i < nmem; i++) {
		if (ts[i].type == type)
			return ts[i].str;
	}
	return "unknown type";
}

static int do_pr_read_reservation(int fd)
{
	struct pr_read_reservation pr_rr;
	const char *type_str;
	int ret;

	ret = ioctl(fd, IOC_PR_READ_RESERVATION, &pr_rr);
	if (ret)
		return ret;

	type_str = type_to_str(pr_type, ARRAY_SIZE(pr_type), pr_rr.type);

	if (pr_rr.key) {
		printf(_("Key: %#" PRIx64 "\n"), (uint64_t)pr_rr.key);
		printf(_("Generation: %#x\n"), pr_rr.generation);
		printf(_("Type: %s\n"), type_str);
	} else {
		printf(_("No reservation\n"));
	}
	return 0;
}
#endif /* IOC_PR_READ_RESERVATION */

static int do_pr(char *path, uint64_t key, uint64_t oldkey, int op, int type, int flag)
{
	struct pr_registration pr_reg;
	struct pr_reservation pr_res;
	struct pr_preempt pr_prt;
	struct pr_clear pr_clr;
	int fd, ret;

	fd = open(path, O_RDWR);
	if (fd < 0)
		err(EXIT_FAILURE, _("cannot open %s"), path);

	switch (op) {
	case IOC_PR_REGISTER:
		pr_reg.old_key = oldkey;
		pr_reg.new_key = key;
		pr_reg.flags = flag;
		ret = ioctl(fd, op, &pr_reg);
		break;
	case IOC_PR_RESERVE:
	case IOC_PR_RELEASE:
		pr_res.key = key;
		pr_res.type = type;
		pr_res.flags = flag;
		ret = ioctl(fd, op, &pr_res);
		break;
	case IOC_PR_PREEMPT:
	case IOC_PR_PREEMPT_ABORT:
		pr_prt.old_key = oldkey;
		pr_prt.new_key = key;
		pr_prt.type = type;
		pr_prt.flags = flag;
		ret = ioctl(fd, op, &pr_prt);
		break;
	case IOC_PR_CLEAR:
		pr_clr.key = key;
		pr_clr.flags = flag;
		ret = ioctl(fd, op, &pr_clr);
		break;
#ifdef IOC_PR_READ_KEYS
	case IOC_PR_READ_KEYS:
		ret = do_pr_read_keys(fd);
		break;
#endif
#ifdef IOC_PR_READ_RESERVATION
	case IOC_PR_READ_RESERVATION:
		ret = do_pr_read_reservation(fd);
		break;
#endif
	default:
		errno = EINVAL;
		err(EXIT_FAILURE, _("unknown command"));
	}

	close(fd);
	if (ret < 0)
		err(EXIT_FAILURE, _("pr ioctl failed"));
	if (ret > 0)
		errx(EXIT_FAILURE, _("error code 0x%x, for more detailed information see specification of device model."), ret);

	return ret;
}

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;

	fputs(USAGE_HEADER, out);
	fprintf(out,
	      _(" %s [options] <device>\n"), program_invocation_short_name);

	fputs(USAGE_SEPARATOR, out);
	fputs(_("Manage persistent reservations on a device.\n"), out);

	fputs(USAGE_OPTIONS, out);
	fputs(_(" -c, --command <cmd>      command for persistent reservations\n"), out);
	fputs(_(" -k, --key <num>          key to operate on\n"), out);
	fputs(_(" -K, --oldkey <num>       old key to operate on\n"), out);
	fputs(_(" -f, --flag <flag>        command flag\n"), out);
	fputs(_(" -t, --type <type>        command type\n"), out);

	fputs(USAGE_SEPARATOR, out);
	fprintf(out, USAGE_HELP_OPTIONS(26));

	fputs(USAGE_ARGUMENTS, out);

	fputs(USAGE_SEPARATOR, out);
	fputs(_(" <cmd> is a command; available commands are:\n"), out);
	print_pr_command(out);

	fputs(USAGE_SEPARATOR, out);
	fputs(_(" <flag> is a command flag; available flags are:\n"), out);
	print_pr_flag(out);

	fputs(USAGE_SEPARATOR, out);
	fputs(_(" <type> is a command type; available types are:\n"), out);
	print_pr_type(out);

	fprintf(out, USAGE_MAN_TAIL("blkpr(8)"));
	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	int c;
	char *path;
	uint64_t key = 0, oldkey = 0;
	int command = -1, type = -1, flag = 0;

	static const struct option longopts[] = {
	    { "help",            no_argument,       NULL, 'h' },
	    { "version",         no_argument,       NULL, 'V' },
	    { "command",         required_argument, NULL, 'c' },
	    { "key",             required_argument, NULL, 'k' },
	    { "oldkey",          required_argument, NULL, 'K' },
	    { "flag",            required_argument, NULL, 'f' },
	    { "type",            required_argument, NULL, 't' },
	    { NULL, 0, NULL, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();

	errno = EINVAL;
	while ((c = getopt_long(argc, argv, "hVc:k:K:f:t:", longopts, NULL)) != -1) {
		switch(c) {
		case 'k':
			key = strtosize_or_err(optarg,
					_("failed to parse key"));
			break;
		case 'K':
			oldkey = strtosize_or_err(optarg,
					_("failed to parse old key"));
			break;
		case 'c':
			command = parse_pr_command(optarg);
			if (command == -1)
				err(EXIT_FAILURE, _("unknown command"));
			break;
		case 't':
			type = parse_pr_type(optarg);
			if (type < 0)
				err(EXIT_FAILURE, _("unknown type"));
			break;
		case 'f':
			flag = parse_pr_flag(optarg);
			if (flag < 0)
				err(EXIT_FAILURE, _("unknown flag"));
			break;

		case 'h':
			usage();
		case 'V':
			print_version(EXIT_SUCCESS);
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if (optind == argc)
		errx(EXIT_FAILURE, _("no device specified"));

	path = argv[optind++];
	if (optind != argc) {
		warnx(_("unexpected number of arguments"));
		errtryhelp(EXIT_FAILURE);
	}

	do_pr(path, key, oldkey, command, type, flag);

	return EXIT_SUCCESS;
}