static int cmd_inspect_inode_resolve(int argc, char **argv) { int fd; int verbose = 0; int ret; DIR *dirstream = NULL; optind = 1; while (1) { int c = getopt(argc, argv, "v"); if (c < 0) break; switch (c) { case 'v': verbose = 1; break; default: usage(cmd_inspect_inode_resolve_usage); } } if (check_argc_exact(argc - optind, 2)) usage(cmd_inspect_inode_resolve_usage); fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1); if (fd < 0) return 1; ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose, argv[optind+1]); close_file_or_dir(fd, dirstream); return !!ret; }
int open_by_inode(int atfd, uint64_t inum, uint64_t root) { int path_fd=get_rootfd(atfd, root); if (0 > path_fd) return path_fd; int ret=__ino_to_path_fd(inum, path_fd); return ret; }
static int cmd_inspect_logical_resolve(int argc, char **argv) { int ret; int fd; int i; int verbose = 0; int getpath = 1; int bytes_left; struct btrfs_ioctl_logical_ino_args loi; struct btrfs_data_container *inodes; u64 size = 4096; char full_path[4096]; char *path_ptr; DIR *dirstream = NULL; optind = 1; while (1) { int c = getopt(argc, argv, "Pvs:"); if (c < 0) break; switch (c) { case 'P': getpath = 0; break; case 'v': verbose = 1; break; case 's': size = arg_strtou64(optarg); break; default: usage(cmd_inspect_logical_resolve_usage); } } if (check_argc_exact(argc - optind, 2)) usage(cmd_inspect_logical_resolve_usage); size = min(size, (u64)64 * 1024); inodes = malloc(size); if (!inodes) return 1; memset(inodes, 0, sizeof(*inodes)); loi.logical = arg_strtou64(argv[optind]); loi.size = size; loi.inodes = ptr_to_u64(inodes); fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1); if (fd < 0) { ret = 12; goto out; } ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi); if (ret < 0) { error("logical ino ioctl: %s", strerror(errno)); goto out; } if (verbose) printf("ioctl ret=%d, total_size=%llu, bytes_left=%lu, " "bytes_missing=%lu, cnt=%d, missed=%d\n", ret, size, (unsigned long)inodes->bytes_left, (unsigned long)inodes->bytes_missing, inodes->elem_cnt, inodes->elem_missed); bytes_left = sizeof(full_path); ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]); path_ptr = full_path + ret; bytes_left -= ret + 1; BUG_ON(bytes_left < 0); for (i = 0; i < inodes->elem_cnt; i += 3) { u64 inum = inodes->val[i]; u64 offset = inodes->val[i+1]; u64 root = inodes->val[i+2]; int path_fd; char *name; DIR *dirs = NULL; if (getpath) { name = btrfs_list_path_for_root(fd, root); if (IS_ERR(name)) { ret = PTR_ERR(name); goto out; } if (!name) { path_ptr[-1] = '\0'; path_fd = fd; } else { path_ptr[-1] = '/'; ret = snprintf(path_ptr, bytes_left, "%s", name); BUG_ON(ret >= bytes_left); free(name); path_fd = btrfs_open_dir(full_path, &dirs, 1); if (path_fd < 0) { ret = -ENOENT; goto out; } } __ino_to_path_fd(inum, path_fd, verbose, full_path); if (path_fd != fd) close_file_or_dir(path_fd, dirs); } else { printf("inode %llu offset %llu root %llu\n", inum, offset, root); } } out: close_file_or_dir(fd, dirstream); free(inodes); return !!ret; }