2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
22 #include <sys/ioctl.h>
30 static const char * const property_cmd_group_usage[] = {
31 "btrfs property get/set/list [-t <type>] <object> [<name>] [value]",
35 static int parse_prop(const char *arg, const struct prop_handler *props,
36 const struct prop_handler **prop_ret)
38 const struct prop_handler *prop = props;
40 for (; prop->name; prop++) {
41 if (!strcmp(prop->name, arg)) {
50 static int get_fsid(const char *path, u8 *fsid, int silent)
54 struct btrfs_ioctl_fs_info_args args;
56 fd = open(path, O_RDONLY);
60 error("failed to open %s: %s", path,
65 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &args);
71 memcpy(fsid, args.fsid, BTRFS_FSID_SIZE);
80 static int check_btrfs_object(const char *object)
83 u8 fsid[BTRFS_FSID_SIZE];
85 ret = get_fsid(object, fsid, 0);
93 static int check_is_root(const char *object)
96 u8 fsid[BTRFS_FSID_SIZE];
97 u8 fsid2[BTRFS_FSID_SIZE];
101 rp = realpath(object, NULL);
106 if (!strcmp(rp, "/")) {
111 tmp = malloc(strlen(object) + 5);
117 if (tmp[strlen(tmp) - 1] != '/')
121 ret = get_fsid(object, fsid, 0);
123 error("get_fsid for %s failed: %s", object, strerror(-ret));
127 ret = get_fsid(tmp, fsid2, 1);
128 if (ret == -ENOTTY) {
131 } else if (ret == -ENOTDIR) {
134 } else if (ret < 0) {
135 error("get_fsid for %s failed: %s", tmp, strerror(-ret));
139 if (memcmp(fsid, fsid2, BTRFS_FSID_SIZE)) {
152 static int count_bits(int v)
154 unsigned int tmp = (unsigned int)v;
165 static int autodetect_object_types(const char *object, int *types_out)
172 is_btrfs_object = check_btrfs_object(object);
174 ret = lstat(object, &st);
180 if (is_btrfs_object) {
181 types |= prop_object_inode;
182 if (st.st_ino == BTRFS_FIRST_FREE_OBJECTID)
183 types |= prop_object_subvol;
185 ret = check_is_root(object);
189 types |= prop_object_root;
192 if (S_ISBLK(st.st_mode))
193 types |= prop_object_dev;
202 static int print_prop_help(const struct prop_handler *prop)
204 fprintf(stdout, "%-20s%s\n", prop->name, prop->desc);
208 static int dump_prop(const struct prop_handler *prop,
216 if ((types & type) && (prop->types & type)) {
218 ret = prop->handler(type, object, prop->name, NULL);
220 ret = print_prop_help(prop);
225 static int dump_props(int types, const char *object, int name_and_help)
230 const struct prop_handler *prop;
232 for (i = 0; prop_handlers[i].name; i++) {
233 prop = &prop_handlers[i];
234 for (j = 1; j < __prop_object_max; j <<= 1) {
235 ret = dump_prop(prop, object, types, j, name_and_help);
249 static int setget_prop(int types, const char *object,
250 const char *name, const char *value)
253 const struct prop_handler *prop = NULL;
255 ret = parse_prop(name, prop_handlers, &prop);
257 error("unknown property: %s", name);
262 types &= prop->types;
264 error("object is not compatible with property: %s", prop->name);
269 if (count_bits(types) > 1) {
270 error("type of object is ambiguous, please use option -t");
275 if (value && prop->read_only) {
276 error("property is read-only property: %s",
282 ret = prop->handler(types, object, name, value);
294 static void parse_args(int argc, char **argv,
295 const char * const *usage_str,
296 int *types, char **object,
297 char **name, char **value, int min_nonopt_args)
300 char *type_str = NULL;
301 int max_nonopt_args = 0;
305 int c = getopt(argc, argv, "t:");
325 if (check_argc_min(argc - optind, min_nonopt_args) ||
326 check_argc_max(argc - optind, max_nonopt_args))
331 if (!strcmp(type_str, "s") || !strcmp(type_str, "subvol")) {
332 *types = prop_object_subvol;
333 } else if (!strcmp(type_str, "f") ||
334 !strcmp(type_str, "filesystem")) {
335 *types = prop_object_root;
336 } else if (!strcmp(type_str, "i") ||
337 !strcmp(type_str, "inode")) {
338 *types = prop_object_inode;
339 } else if (!strcmp(type_str, "d") ||
340 !strcmp(type_str, "device")) {
341 *types = prop_object_dev;
343 error("invalid object type: %s", type_str);
348 if (object && optind < argc)
349 *object = argv[optind++];
350 if (name && optind < argc)
351 *name = argv[optind++];
352 if (value && optind < argc)
353 *value = argv[optind++];
355 if (optind != argc) {
356 error("unexpected agruments found");
360 if (!*types && object && *object) {
361 ret = autodetect_object_types(*object, types);
363 error("failed to detect object type: %s",
368 error("object is not a btrfs object: %s", *object);
374 static const char * const cmd_property_get_usage[] = {
375 "btrfs property get [-t <type>] <object> [<name>]",
376 "Gets a property from a btrfs object.",
377 "If no name is specified, all properties for the given object are",
379 "A filesystem object can be a the filesystem itself, a subvolume,",
380 "an inode or a device. The '-t <type>' option can be used to explicitly",
381 "specify what type of object you meant. This is only needed when a",
382 "property could be set for more then one object type. Possible types",
383 "are s[ubvol], f[ilesystem], i[node] and d[evice].",
387 static int cmd_property_get(int argc, char **argv)
394 parse_args(argc, argv, cmd_property_get_usage, &types, &object, &name,
397 error("invalid arguments");
398 usage(cmd_property_get_usage);
402 ret = setget_prop(types, object, name, NULL);
404 ret = dump_props(types, object, 0);
409 static const char * const cmd_property_set_usage[] = {
410 "btrfs property set [-t <type>] <object> <name> <value>",
411 "Sets a property on a btrfs object.",
412 "Please see the help of 'btrfs property get' for a description of",
413 "objects and object types.",
417 static int cmd_property_set(int argc, char **argv)
425 parse_args(argc, argv, cmd_property_set_usage, &types,
426 &object, &name, &value, 3);
427 if (!object || !name || !value) {
428 error("invalid arguments");
429 usage(cmd_property_set_usage);
432 ret = setget_prop(types, object, name, value);
437 static const char * const cmd_property_list_usage[] = {
438 "btrfs property list [-t <type>] <object>",
439 "Lists available properties with their descriptions for the given object.",
440 "Please see the help of 'btrfs property get' for a description of",
441 "objects and object types.",
445 static int cmd_property_list(int argc, char **argv)
451 parse_args(argc, argv, cmd_property_list_usage,
452 &types, &object, NULL, NULL, 1);
454 error("invalid arguments");
455 usage(cmd_property_list_usage);
458 ret = dump_props(types, object, 1);
463 static const char property_cmd_group_info[] =
464 "modify properties of filesystem objects";
466 const struct cmd_group property_cmd_group = {
467 property_cmd_group_usage, property_cmd_group_info, {
468 { "get", cmd_property_get,
469 cmd_property_get_usage, NULL, 0 },
470 { "set", cmd_property_set,
471 cmd_property_set_usage, NULL, 0 },
472 { "list", cmd_property_list,
473 cmd_property_list_usage, NULL, 0 },
478 int cmd_property(int argc, char **argv)
480 return handle_command_group(&property_cmd_group, argc, argv);