aboutsummaryrefslogtreecommitdiffstats
path: root/cmd_option.c
blob: 6ce34016f5dba7f0dca4e376c2893c0d6c84c1c8 (plain)
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
/*
 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
 *
 * GPLv2
 */
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <uuid/uuid.h>

#include "cmds.h"
#include "libbcachefs.h"
#include "libbcachefs/errcode.h"
#include "libbcachefs/opts.h"
#include "libbcachefs/super-io.h"

static void set_option_usage(void)
{
	puts("bcachefs set-option \n"
	     "Usage: bcachefs set-option [OPTION].. device\n"
	     "\n"
	     "Options:\n");
	bch2_opts_usage(OPT_MOUNT);
	puts("  -h, --help                  display this help and exit\n"
	     "Report bugs to <linux-bcachefs@vger.kernel.org>");
	exit(EXIT_SUCCESS);
}

int cmd_set_option(int argc, char *argv[])
{
	struct bch_opt_strs new_opt_strs = bch2_cmdline_opts_get(&argc, argv, OPT_MOUNT);
	struct bch_opts new_opts = bch2_parse_opts(new_opt_strs);
	struct bch_opts open_opts = bch2_opts_empty();
	unsigned i;
	int opt, ret = 0;

	opt_set(open_opts, nostart, true);

	while ((opt = getopt(argc, argv, "h")) != -1)
		switch (opt) {
		case 'h':
			set_option_usage();
			break;
		}
	args_shift(optind);

	if (!argc) {
		fprintf(stderr, "Please supply device(s)\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < argc; i++)
		if (dev_mounted(argv[i]))
			goto online;

	struct bch_fs *c = bch2_fs_open(argv, argc, open_opts);
	if (IS_ERR(c)) {
		fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < bch2_opts_nr; i++) {
		u64 v = bch2_opt_get_by_id(&new_opts, i);

		if (!bch2_opt_defined_by_id(&new_opts, i))
			continue;

		ret = bch2_opt_check_may_set(c, i, v);
		if (ret < 0) {
			fprintf(stderr, "error setting %s: %i\n",
				bch2_opt_table[i].attr.name, ret);
			break;
		}

		bch2_opt_set_sb(c, bch2_opt_table + i, v);
		bch2_opt_set_by_id(&c->opts, i, v);
	}

	bch2_fs_stop(c);
	return ret;
online:
	{
		unsigned dev_idx;
		struct bchfs_handle fs = bchu_fs_open_by_dev(argv[i], &dev_idx);

		for (i = 0; i < bch2_opts_nr; i++) {
			if (!new_opt_strs.by_id[i])
				continue;

			char *path = mprintf("options/%s", bch2_opt_table[i].attr.name);

			write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]);
			free(path);
		}
	}
	return 0;
}