int main(int argc, char *argv[]) { struct btrfs_root *root; unsigned ctree_flags = OPEN_CTREE_WRITES; int success = 0; int total = 0; int seeding_flag = 0; u64 seeding_value = 0; int random_fsid = 0; char *new_fsid_str = NULL; int ret; u64 super_flags = 0; while(1) { static const struct option long_options[] = { { "help", no_argument, NULL, GETOPT_VAL_HELP}, { NULL, 0, NULL, 0 } }; int c = getopt_long(argc, argv, "S:rxfuU:n", long_options, NULL); if (c < 0) break; switch(c) { case 'S': seeding_flag = 1; seeding_value = arg_strtou64(optarg); break; case 'r': super_flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF; break; case 'x': super_flags |= BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA; break; case 'n': super_flags |= BTRFS_FEATURE_INCOMPAT_NO_HOLES; break; case 'f': force = 1; break; case 'U': ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH; new_fsid_str = optarg; break; case 'u': ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH; random_fsid = 1; break; case GETOPT_VAL_HELP: default: print_usage(); return c != GETOPT_VAL_HELP; } } set_argv0(argv); device = argv[optind]; if (check_argc_exact(argc - optind, 1)) { print_usage(); return 1; } if (random_fsid && new_fsid_str) { error("random fsid can't be used with specified fsid"); return 1; } if (!super_flags && !seeding_flag && !(random_fsid || new_fsid_str)) { error("at least one option should be specified"); print_usage(); return 1; } if (new_fsid_str) { uuid_t tmp; ret = uuid_parse(new_fsid_str, tmp); if (ret < 0) { error("could not parse UUID: %s", new_fsid_str); return 1; } if (!test_uuid_unique(new_fsid_str)) { error("fsid %s is not unique", new_fsid_str); return 1; } } ret = check_mounted(device); if (ret < 0) { error("could not check mount status of %s: %s", device, strerror(-ret)); return 1; } else if (ret) { error("%s is mounted", device); return 1; } root = open_ctree(device, 0, ctree_flags); if (!root) { error("open ctree failed"); return 1; } if (seeding_flag) { if (!seeding_value && !force) { warning( "this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!"); ret = ask_user("We are going to clear the seeding flag, are you sure?"); if (!ret) { fprintf(stderr, "Clear seeding flag canceled\n"); ret = 1; goto out; } } ret = update_seeding_flag(root, seeding_value); if (!ret) success++; total++; } if (super_flags) { ret = set_super_incompat_flags(root, super_flags); if (!ret) success++; total++; } if (random_fsid || new_fsid_str) { if (!force) { warning( "it's highly recommended to run 'btrfs check' before this operation"); warning( "also canceling running UUID change progress may cause corruption"); ret = ask_user("We are going to change UUID, are your sure?"); if (!ret) { fprintf(stderr, "UUID change canceled\n"); ret = 1; goto out; } } ret = change_uuid(root->fs_info, new_fsid_str); if (!ret) success++; total++; } if (success == total) { ret = 0; } else { root->fs_info->readonly = 1; ret = 1; error("btrfstune failed"); } out: close_ctree(root); btrfs_close_all_devices(); return ret; }
static int dev_replace_handle_sigint(int fd) { struct sigaction sa = { .sa_handler = fd == -1 ? SIG_DFL : dev_replace_sigint_handler }; dev_replace_cancel_fd = fd; return sigaction(SIGINT, &sa, NULL); } static const char *const cmd_replace_start_usage[] = { "btrfs replace start [-Bfr] <srcdev>|<devid> <targetdev> <mount_point>", "Replace device of a btrfs filesystem.", "On a live filesystem, duplicate the data to the target device which", "is currently stored on the source device. If the source device is not", "available anymore, or if the -r option is set, the data is built", "only using the RAID redundancy mechanisms. After completion of the", "operation, the source device is removed from the filesystem.", "If the <srcdev> is a numerical value, it is assumed to be the device id", "of the filesystem which is mounted at <mount_point>, otherwise it is", "the path to the source device. If the source device is disconnected,", "from the system, you have to use the <devid> parameter format.", "The <targetdev> needs to be same size or larger than the <srcdev>.", "", "-r only read from <srcdev> if no other zero-defect mirror exists", " (enable this if your drive has lots of read errors, the access", " would be very slow)", "-f force using and overwriting <targetdev> even if it looks like", " containing a valid btrfs filesystem. A valid filesystem is", " assumed if a btrfs superblock is found which contains a", " correct checksum. Devices which are currently mounted are", " never allowed to be used as the <targetdev>", "-B do not background", NULL }; static int cmd_replace_start(int argc, char **argv) { struct btrfs_ioctl_dev_replace_args start_args = {0}; struct btrfs_ioctl_dev_replace_args status_args = {0}; int ret; int i; int c; int fdmnt = -1; int fddstdev = -1; char *path; char *srcdev; char *dstdev = NULL; int avoid_reading_from_srcdev = 0; int force_using_targetdev = 0; u64 dstdev_block_count; int do_not_background = 0; DIR *dirstream = NULL; u64 srcdev_size; u64 dstdev_size; while ((c = getopt(argc, argv, "Brf")) != -1) { switch (c) { case 'B': do_not_background = 1; break; case 'r': avoid_reading_from_srcdev = 1; break; case 'f': force_using_targetdev = 1; break; case '?': default: usage(cmd_replace_start_usage); } } start_args.start.cont_reading_from_srcdev_mode = avoid_reading_from_srcdev ? BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID : BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS; if (check_argc_exact(argc - optind, 3)) usage(cmd_replace_start_usage); path = argv[optind + 2]; fdmnt = open_path_or_dev_mnt(path, &dirstream, 1); if (fdmnt < 0) goto leave_with_error; /* check for possible errors before backgrounding */ status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS; status_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT; ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &status_args); if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s", path, strerror(errno)); if (status_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT) fprintf(stderr, ", %s\n", replace_dev_result2string(status_args.result)); else fprintf(stderr, "\n"); goto leave_with_error; } if (status_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) { error("ioctl(DEV_REPLACE_STATUS) on '%s' returns error: %s", path, replace_dev_result2string(status_args.result)); goto leave_with_error; } if (status_args.status.replace_state == BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { error("device replace on '%s' already started", path); goto leave_with_error; } srcdev = argv[optind]; dstdev = canonicalize_path(argv[optind + 1]); if (!dstdev) { error("cannot canonicalize path '%s': %s", argv[optind + 1], strerror(errno)); goto leave_with_error; } if (string_is_numerical(srcdev)) { struct btrfs_ioctl_fs_info_args fi_args; struct btrfs_ioctl_dev_info_args *di_args = NULL; start_args.start.srcdevid = arg_strtou64(srcdev); ret = get_fs_info(path, &fi_args, &di_args); if (ret) { error("failed to get device info: %s", strerror(-ret)); free(di_args); goto leave_with_error; } if (!fi_args.num_devices) { error("no devices found"); free(di_args); goto leave_with_error; } for (i = 0; i < fi_args.num_devices; i++) if (start_args.start.srcdevid == di_args[i].devid) break; srcdev_size = di_args[i].total_bytes; free(di_args); if (i == fi_args.num_devices) { error("'%s' is not a valid devid for filesystem '%s'", srcdev, path); goto leave_with_error; } } else if (is_block_device(srcdev) > 0) { strncpy((char *)start_args.start.srcdev_name, srcdev, BTRFS_DEVICE_PATH_NAME_MAX); start_args.start.srcdevid = 0; srcdev_size = get_partition_size(srcdev); } else { error("source device must be a block device or a devid"); goto leave_with_error; } ret = test_dev_for_mkfs(dstdev, force_using_targetdev); if (ret) goto leave_with_error; dstdev_size = get_partition_size(dstdev); if (srcdev_size > dstdev_size) { error("target device smaller than source device (required %llu bytes)", srcdev_size); goto leave_with_error; } fddstdev = open(dstdev, O_RDWR); if (fddstdev < 0) { error("unable to open %s: %s", dstdev, strerror(errno)); goto leave_with_error; } strncpy((char *)start_args.start.tgtdev_name, dstdev, BTRFS_DEVICE_PATH_NAME_MAX); ret = btrfs_prepare_device(fddstdev, dstdev, &dstdev_block_count, 0, PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE); if (ret) goto leave_with_error; close(fddstdev); fddstdev = -1; free(dstdev); dstdev = NULL; dev_replace_handle_sigint(fdmnt); if (!do_not_background) { if (daemon(0, 0) < 0) { error("backgrounding failed: %s", strerror(errno)); goto leave_with_error; } } start_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_START; start_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT; ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &start_args); if (do_not_background) { if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %s", path, strerror(errno)); if (start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT) fprintf(stderr, ", %s\n", replace_dev_result2string(start_args.result)); else fprintf(stderr, "\n"); if (errno == EOPNOTSUPP) warning("device replace of RAID5/6 not supported with this kernel"); goto leave_with_error; } if (start_args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) { error("ioctl(DEV_REPLACE_START) on '%s' returns error: %s", path, replace_dev_result2string(start_args.result)); goto leave_with_error; } } close_file_or_dir(fdmnt, dirstream); return 0; leave_with_error: if (dstdev) free(dstdev); if (fdmnt != -1) close(fdmnt); if (fddstdev != -1) close(fddstdev); return 1; } static const char *const cmd_replace_status_usage[] = { "btrfs replace status [-1] <mount_point>", "Print status and progress information of a running device replace", "operation", "", "-1 print once instead of print continuously until the replace", " operation finishes (or is canceled)", NULL }; static int cmd_replace_status(int argc, char **argv) { int fd; int c; char *path; int once = 0; int ret; DIR *dirstream = NULL; while ((c = getopt(argc, argv, "1")) != -1) { switch (c) { case '1': once = 1; break; case '?': default: usage(cmd_replace_status_usage); } } if (check_argc_exact(argc - optind, 1)) usage(cmd_replace_status_usage); path = argv[optind]; fd = btrfs_open_dir(path, &dirstream, 1); if (fd < 0) return 1; ret = print_replace_status(fd, path, once); close_file_or_dir(fd, dirstream); return !!ret; }
int main(int argc, char **argv) { struct cache_tree root_cache; struct btrfs_root *root; char *dev; char *output_file = NULL; u64 copy = 0; u64 logical = 0; u64 bytes = 0; u64 cur_logical = 0; u64 cur_len = 0; int out_fd = -1; int found = 0; int ret = 0; while(1) { int c; static const struct option long_options[] = { /* { "byte-count", 1, NULL, 'b' }, */ { "logical", required_argument, NULL, 'l' }, { "copy", required_argument, NULL, 'c' }, { "output", required_argument, NULL, 'o' }, { "bytes", required_argument, NULL, 'b' }, { NULL, 0, NULL, 0} }; c = getopt_long(argc, argv, "l:c:o:b:", long_options, NULL); if (c < 0) break; switch(c) { case 'l': logical = arg_strtou64(optarg); break; case 'c': copy = arg_strtou64(optarg); break; case 'b': bytes = arg_strtou64(optarg); break; case 'o': output_file = strdup(optarg); break; default: print_usage(); } } set_argv0(argv); if (check_argc_min(argc - optind, 1)) print_usage(); if (logical == 0) print_usage(); dev = argv[optind]; radix_tree_init(); cache_tree_init(&root_cache); root = open_ctree(dev, 0, 0); if (!root) { fprintf(stderr, "Open ctree failed\n"); free(output_file); exit(1); } info_file = stdout; if (output_file) { if (strcmp(output_file, "-") == 0) { out_fd = 1; info_file = stderr; } else { out_fd = open(output_file, O_RDWR | O_CREAT, 0600); if (out_fd < 0) goto close; ret = ftruncate(out_fd, 0); if (ret) { ret = 1; close(out_fd); goto close; } info_file = stdout; } } if (bytes == 0) bytes = root->nodesize; cur_logical = logical; cur_len = bytes; /* First find the nearest extent */ ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 0); if (ret < 0) { fprintf(stderr, "Failed to find extent at [%llu,%llu): %s\n", cur_logical, cur_logical + cur_len, strerror(-ret)); goto out_close_fd; } /* * Normally, search backward should be OK, but for special case like * given logical is quite small where no extents are before it, * we need to search forward. */ if (ret > 0) { ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1); if (ret < 0) { fprintf(stderr, "Failed to find extent at [%llu,%llu): %s\n", cur_logical, cur_logical + cur_len, strerror(-ret)); goto out_close_fd; } if (ret > 0) { fprintf(stderr, "Failed to find any extent at [%llu,%llu)\n", cur_logical, cur_logical + cur_len); goto out_close_fd; } } while (cur_logical + cur_len >= logical && cur_logical < logical + bytes) { u64 real_logical; u64 real_len; found = 1; ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1); if (ret < 0) goto out_close_fd; if (ret > 0) break; /* check again if there is overlap. */ if (cur_logical + cur_len < logical || cur_logical >= logical + bytes) break; real_logical = max(logical, cur_logical); real_len = min(logical + bytes, cur_logical + cur_len) - real_logical; ret = print_mapping_info(root->fs_info, real_logical, real_len); if (ret < 0) goto out_close_fd; if (output_file && out_fd != -1) { ret = write_extent_content(root->fs_info, out_fd, real_logical, real_len, copy); if (ret < 0) goto out_close_fd; } cur_logical += cur_len; } if (!found) { fprintf(stderr, "No extent found at range [%llu,%llu)\n", logical, logical + bytes); } out_close_fd: if (output_file && out_fd != 1) close(out_fd); close: free(output_file); close_ctree(root); if (ret < 0) ret = 1; btrfs_close_all_devices(); return ret; }
int cmd_inspect_dump_super(int argc, char **argv) { int all = 0; int full = 0; int force = 0; char *filename; int fd = -1; int i; int ret = 0; u64 arg; u64 sb_bytenr = btrfs_sb_offset(0); while (1) { int c; enum { GETOPT_VAL_BYTENR = 257 }; static const struct option long_options[] = { {"all", no_argument, NULL, 'a'}, {"bytenr", required_argument, NULL, GETOPT_VAL_BYTENR }, {"full", no_argument, NULL, 'f'}, {"force", no_argument, NULL, 'F'}, {"super", required_argument, NULL, 's' }, {NULL, 0, NULL, 0} }; c = getopt_long(argc, argv, "fFai:s:", long_options, NULL); if (c < 0) break; switch (c) { case 'i': warning( "option -i is deprecated, please use -s or --super"); arg = arg_strtou64(optarg); if (arg >= BTRFS_SUPER_MIRROR_MAX) { error("super mirror too big: %llu >= %d", arg, BTRFS_SUPER_MIRROR_MAX); return 1; } sb_bytenr = btrfs_sb_offset(arg); break; case 'a': all = 1; break; case 'f': full = 1; break; case 'F': force = 1; break; case 's': arg = arg_strtou64(optarg); if (BTRFS_SUPER_MIRROR_MAX <= arg) { warning( "deprecated use of -s <bytenr> with %llu, assuming --bytenr", (unsigned long long)arg); sb_bytenr = arg; } else { sb_bytenr = btrfs_sb_offset(arg); } all = 0; break; case GETOPT_VAL_BYTENR: arg = arg_strtou64(optarg); sb_bytenr = arg; all = 0; break; default: usage_unknown_option(cmd_inspect_dump_super_usage, argv); } } if (check_argc_min(argc - optind, 1)) return 1; for (i = optind; i < argc; i++) { filename = argv[i]; fd = open(filename, O_RDONLY); if (fd < 0) { error("cannot open %s: %m", filename); ret = 1; goto out; } if (all) { int idx; for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) { sb_bytenr = btrfs_sb_offset(idx); if (load_and_dump_sb(filename, fd, sb_bytenr, full, force)) { close(fd); ret = 1; goto out; } putchar('\n'); } } else { load_and_dump_sb(filename, fd, sb_bytenr, full, force); putchar('\n'); } close(fd); } out: return ret; }
static int _cmd_device_remove(int argc, char **argv, const char * const *usagestr) { char *mntpnt; int i, fdmnt, ret = 0; DIR *dirstream = NULL; clean_args_no_options(argc, argv, usagestr); if (check_argc_min(argc - optind, 2)) usage(usagestr); mntpnt = argv[argc - 1]; fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1); if (fdmnt < 0) return 1; for(i = optind; i < argc - 1; i++) { struct btrfs_ioctl_vol_args arg; struct btrfs_ioctl_vol_args_v2 argv2 = {0}; int is_devid = 0; int res; if (string_is_numerical(argv[i])) { argv2.devid = arg_strtou64(argv[i]); argv2.flags = BTRFS_DEVICE_SPEC_BY_ID; is_devid = 1; } else if (is_block_device(argv[i]) == 1 || strcmp(argv[i], "missing") == 0) { strncpy_null(argv2.name, argv[i]); } else { error("not a block device: %s", argv[i]); ret++; continue; } /* * Positive values are from BTRFS_ERROR_DEV_*, * otherwise it's a generic error, one of errnos */ res = ioctl(fdmnt, BTRFS_IOC_RM_DEV_V2, &argv2); /* * If BTRFS_IOC_RM_DEV_V2 is not supported we get ENOTTY and if * argv2.flags includes a flag which kernel doesn't understand then * we shall get EOPNOTSUPP */ if (res < 0 && (errno == ENOTTY || errno == EOPNOTSUPP)) { if (is_devid) { error("device delete by id failed: %s", strerror(errno)); ret++; continue; } memset(&arg, 0, sizeof(arg)); strncpy_null(arg.name, argv[i]); res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); } if (res) { const char *msg; if (res > 0) msg = btrfs_err_str(res); else msg = strerror(errno); if (is_devid) { error("error removing devid %llu: %s", (unsigned long long)argv2.devid, msg); } else { error("error removing device '%s': %s", argv[i], msg); } ret++; } } close_file_or_dir(fdmnt, dirstream); return !!ret; }
int main(int ac, char **av) { struct btrfs_root *root; int ret; u64 num = 0; u64 bytenr = 0; while(1) { int c; c = getopt(ac, av, "s:"); if (c < 0) break; switch(c) { case 's': num = arg_strtou64(optarg); if (num >= BTRFS_SUPER_MIRROR_MAX) { fprintf(stderr, "ERROR: super mirror should be less than: %d\n", BTRFS_SUPER_MIRROR_MAX); exit(1); } bytenr = btrfs_sb_offset(((int)num)); break; default: print_usage(); } } set_argv0(av); ac = ac - optind; if (check_argc_exact(ac, 1)) print_usage(); if (bytenr == 0) { fprintf(stderr, "Please select the super copy with -s\n"); print_usage(); } radix_tree_init(); if((ret = check_mounted(av[optind])) < 0) { fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret)); return ret; } else if(ret) { fprintf(stderr, "%s is currently mounted. Aborting.\n", av[optind]); return -EBUSY; } root = open_ctree(av[optind], bytenr, 1); if (!root) { fprintf(stderr, "Open ctree failed\n"); return 1; } /* make the super writing code think we've read the first super */ root->fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET; ret = write_all_supers(root); /* we don't close the ctree or anything, because we don't want a real * transaction commit. We just want the super copy we pulled off the * disk to overwrite all the other copies */ printf("using SB copy %llu, bytenr %llu\n", (unsigned long long)num, (unsigned long long)bytenr); close_ctree(root); btrfs_close_all_devices(); return ret; }
int main(int argc, char **argv) { struct btrfs_fs_info *fs_info; struct btrfs_find_root_filter filter = {0}; struct cache_tree result; struct cache_extent *found; int ret; /* Default to search root tree */ filter.objectid = BTRFS_ROOT_TREE_OBJECTID; filter.match_gen = (u64)-1; filter.match_level = (u8)-1; while (1) { static const struct option long_options[] = { { "help", no_argument, NULL, GETOPT_VAL_HELP}, { NULL, 0, NULL, 0 } }; int c = getopt_long(argc, argv, "al:o:g:", long_options, NULL); if (c < 0) break; switch (c) { case 'a': filter.search_all = 1; break; case 'o': filter.objectid = arg_strtou64(optarg); break; case 'g': filter.generation = arg_strtou64(optarg); break; case 'l': filter.level = arg_strtou64(optarg); break; case GETOPT_VAL_HELP: default: find_root_usage(); exit(c != GETOPT_VAL_HELP); } } set_argv0(argv); if (check_argc_min(argc - optind, 1)) { find_root_usage(); exit(1); } fs_info = open_ctree_fs_info(argv[optind], 0, 0, 0, OPEN_CTREE_CHUNK_ROOT_ONLY | OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR); if (!fs_info) { error("open ctree failed"); exit(1); } cache_tree_init(&result); get_root_gen_and_level(filter.objectid, fs_info, &filter.match_gen, &filter.match_level); ret = btrfs_find_root_search(fs_info, &filter, &result, &found); if (ret < 0) { fprintf(stderr, "Fail to search the tree root: %s\n", strerror(-ret)); goto out; } if (ret > 0) { printf("Found tree root at %llu gen %llu level %u\n", found->start, filter.match_gen, filter.match_level); ret = 0; } print_find_root_result(&result, &filter); out: btrfs_find_root_free(&result); close_ctree_fs_info(fs_info); btrfs_close_all_devices(); return ret; }