diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2018-02-08 15:30:19 -0500 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2018-02-08 15:33:43 -0500 |
commit | 7875b82630f6431971175e2f4205bb65c2c43488 (patch) | |
tree | f200081697939b7cc4d9f972d0d1e8c894af9640 /cmd_device.c | |
parent | 6976570d670e4053f026d380cfe5397a0a8ed139 (diff) |
New data rereplicate command
Diffstat (limited to 'cmd_device.c')
-rw-r--r-- | cmd_device.c | 261 |
1 files changed, 70 insertions, 191 deletions
diff --git a/cmd_device.c b/cmd_device.c index 390c48ad..5628faec 100644 --- a/cmd_device.c +++ b/cmd_device.c @@ -19,165 +19,6 @@ #include "libbcachefs/opts.h" #include "tools-util.h" -/* This code belongs under show_fs */ -#if 0 - -struct bcache_dev { - unsigned nr; - const char *name; - - unsigned has_metadata; - unsigned has_data; - const char *state; - unsigned tier; - - u64 bucket_size; - u64 first_bucket; - u64 nbuckets; - - /* XXX: dirty != used, it doesn't count metadata */ - u64 bytes_dirty; -}; - -static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir) -{ - return (struct bcache_dev) { - .nr = nr, - .name = dev_name, - - .has_metadata = read_file_u64(dir, "has_metadata"), - .has_data = read_file_u64(dir, "has_data"), - .state = read_file_str(dir, "state"), - .tier = read_file_u64(dir, "tier"), - - .bucket_size = read_file_u64(dir, "bucket_size_bytes"), - .first_bucket = read_file_u64(dir, "first_bucket"), - .nbuckets = read_file_u64(dir, "nbuckets"), - .bytes_dirty = read_file_u64(dir, "dirty_bytes"), - }; -} - -static void show_dev(struct bcache_dev *dev) -{ - u64 capacity = (dev->nbuckets - dev->first_bucket) * - dev->bucket_size; - /* - * XXX: show fragmentation information, cached/dirty information - */ - - printf("Device %u (/dev/%s):\n" - " Has metadata:\t%u\n" - " Has dirty data:\t%u\n" - " State:\t\t%s\n" - " Tier:\t\t%u\n" - " Size:\t\t%llu\n" - " Used:\t\t%llu\n" - " Free:\t\t%llu\n" - " Use%%:\t\t%llu\n", - dev->nr, dev->name, - dev->has_metadata, - dev->has_data, - dev->state, - dev->tier, - capacity, - dev->bytes_dirty, - capacity - dev->bytes_dirty, - (dev->bytes_dirty * 100) / capacity); -} - -int cmd_device_show(int argc, char *argv[]) -{ - int human_readable = 0; - NihOption opts[] = { - // { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter} - - { 'h', "human-readable", N_("print sizes in powers of 1024 (e.g., 1023M)"), - NULL, NULL, &human_readable, NULL}, - NIH_OPTION_LAST - }; - char **args = bch_nih_init(argc, argv, opts); - - if (argc != 2) - die("Please supply a single device"); - - struct bchfs_handle fs = bcache_fs_open(argv[1]); - struct dirent *entry; - - struct bcache_dev devices[256]; - unsigned i, j, nr_devices = 0, nr_active_tiers = 0; - - unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */ - memset(tiers, 0, sizeof(tiers)); - - while ((entry = readdir(fs.sysfs))) { - unsigned nr; - int pos = 0; - - sscanf(entry->d_name, "cache%u%n", &nr, &pos); - if (pos != strlen(entry->d_name)) - continue; - - char link[PATH_MAX]; - if (readlinkat(dirfd(fs.sysfs), entry->d_name, - link, sizeof(link)) < 0) - die("readlink error: %m\n"); - - char *dev_name = basename(dirname(link)); - - int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY); - - devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd); - tiers[devices[nr_devices].tier]++; - nr_devices++; - - close(fd); - } - - for (i = 0; i < BCH_TIER_MAX; i++) - if (tiers[i]) - nr_active_tiers++; - - /* Print out devices sorted by tier: */ - bool first = true; - - for (i = 0; i < BCH_TIER_MAX; i++) { - if (!tiers[i]) - continue; - - if (nr_active_tiers > 1) { - if (!first) - printf("\n"); - first = false; - printf("Tier %u:\n\n", i); - } - - for (j = 0; j < nr_devices; j++) { - if (devices[j].tier != i) - continue; - - if (!first) - printf("\n"); - first = false; - show_dev(&devices[j]); - } - } - - return 0; -} -#endif - -static void disk_ioctl(const char *fs, const char *dev, int cmd, int flags) -{ - struct bch_ioctl_disk i = { .flags = flags, }; - - if (!kstrtoull(dev, 10, &i.dev)) - i.flags |= BCH_BY_INDEX; - else - i.dev = (u64) dev; - - xioctl(bcache_fs_open(fs).ioctl_fd, cmd, &i); -} - static void device_add_usage(void) { puts("bcachefs device add - add a device to an existing filesystem\n" @@ -239,12 +80,20 @@ int cmd_device_add(int argc, char *argv[]) } args_shift(optind); - if (argc != 2) - die("Please supply a filesystem and a device to add"); + char *fs_path = arg_pop(); + if (!fs_path) + die("Please supply a filesystem"); + + char *dev_path = arg_pop(); + if (!dev_path) + die("Please supply a device"); - struct bchfs_handle fs = bcache_fs_open(arg_pop()); + if (argc) + die("too many arguments"); - dev_opts.path = arg_pop(); + struct bchfs_handle fs = bcache_fs_open(fs_path); + + dev_opts.path = dev_path; dev_opts.fd = open_for_format(dev_opts.path, force); format_opts.block_size = @@ -264,7 +113,7 @@ int cmd_device_add(int argc, char *argv[]) static void device_remove_usage(void) { puts("bcachefs device_remove - remove a device from a filesystem\n" - "Usage: bcachefs device remove filesystem device\n" + "Usage: bcachefs device remove device\n" "\n" "Options:\n" " -f, --force Force removal, even if some data\n" @@ -299,10 +148,6 @@ int cmd_device_remove(int argc, char *argv[]) } args_shift(optind); - char *fs = arg_pop(); - if (!fs) - die("Please supply a filesystem"); - char *dev = arg_pop(); if (!dev) die("Please supply a device to remove"); @@ -310,14 +155,16 @@ int cmd_device_remove(int argc, char *argv[]) if (argc) die("too many arguments"); - disk_ioctl(fs, dev, BCH_IOCTL_DISK_REMOVE, flags); + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx); + bchu_disk_remove(fs, dev_idx, flags); return 0; } static void device_online_usage(void) { puts("bcachefs device online - readd a device to a running filesystem\n" - "Usage: bcachefs device online [OPTION]... filesystem device\n" + "Usage: bcachefs device online [OPTION]... device\n" "\n" "Options:\n" " -h, --help Display this help and exit\n" @@ -335,18 +182,25 @@ int cmd_device_online(int argc, char *argv[]) device_online_usage(); exit(EXIT_SUCCESS); } + args_shift(optind); + + char *dev = arg_pop(); + if (!dev) + die("Please supply a device"); - if (argc - optind != 2) - die("Please supply a filesystem and a device"); + if (argc) + die("too many arguments"); - disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_ONLINE, 0); + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx); + bchu_disk_online(fs, dev); return 0; } static void device_offline_usage(void) { puts("bcachefs device offline - take a device offline, without removing it\n" - "Usage: bcachefs device offline [OPTION]... filesystem device\n" + "Usage: bcachefs device offline [OPTION]... device\n" "\n" "Options:\n" " -f, --force Force, if data redundancy will be degraded\n" @@ -373,19 +227,25 @@ int cmd_device_offline(int argc, char *argv[]) device_offline_usage(); exit(EXIT_SUCCESS); } + args_shift(optind); - if (argc - optind != 2) - die("Please supply a filesystem and a device"); + char *dev = arg_pop(); + if (!dev) + die("Please supply a device"); + + if (argc) + die("too many arguments"); - disk_ioctl(argv[optind], argv[optind + 1], - BCH_IOCTL_DISK_OFFLINE, flags); + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx); + bchu_disk_offline(fs, dev_idx, flags); return 0; } static void device_evacuate_usage(void) { puts("bcachefs device evacuate - move data off of a given device\n" - "Usage: bcachefs device evacuate [OPTION]... filesystem device\n" + "Usage: bcachefs device evacuate [OPTION]... device\n" "\n" "Options:\n" " -h, --help Display this help and exit\n" @@ -403,18 +263,30 @@ int cmd_device_evacuate(int argc, char *argv[]) device_evacuate_usage(); exit(EXIT_SUCCESS); } + args_shift(optind); - if (argc - optind != 2) - die("Please supply a filesystem and a device"); + char *dev_path = arg_pop(); + if (!dev_path) + die("Please supply a device"); - disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_EVACUATE, 0); - return 0; + if (argc) + die("too many arguments"); + + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx); + + return bchu_data(fs, (struct bch_ioctl_data) { + .op = BCH_DATA_OP_MIGRATE, + .start = POS_MIN, + .end = POS_MAX, + .migrate.dev = dev_idx, + }); } static void device_set_state_usage(void) { puts("bcachefs device set-state\n" - "Usage: bcachefs device set-state filesystem device new-state\n" + "Usage: bcachefs device set-state device new-state\n" "\n" "Options:\n" " -f, --force Force, if data redundancy will be degraded\n" @@ -440,16 +312,23 @@ int cmd_device_set_state(int argc, char *argv[]) case 'h': device_set_state_usage(); } + args_shift(optind); + + char *dev_path = arg_pop(); + if (!dev_path) + die("Please supply a device"); + + char *new_state_str = arg_pop(); + if (!new_state_str) + die("Please supply a device state"); - if (argc - optind != 3) - die("Please supply a filesystem, device and state"); + unsigned new_state = read_string_list_or_die(new_state_str, + bch2_dev_state, "device state"); - struct bchfs_handle fs = bcache_fs_open(argv[optind]); + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx); - bchu_disk_set_state(fs, argv[optind + 1], - read_string_list_or_die(argv[optind + 2], - bch2_dev_state, "device state"), - flags); + bchu_disk_set_state(fs, dev_idx, new_state, flags); return 0; } |