From 7fbcc39c3075b88718bcb3e8e6f3ff599a4e9f86 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 18 Sep 2013 18:19:30 +0200 Subject: btrfs-progs: look up the containing tree root id Find the tree id of the containing subvolume for a given file or directory. For subvolume return it's own id. $ btrfs inspect-internal rootid Signed-off-by: David Sterba Signed-off-by: Chris Mason --- utils.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 86147f29..58d56f5d 100644 --- a/utils.c +++ b/utils.c @@ -1975,3 +1975,33 @@ int ask_user(char *question) (answer = strtok_r(buf, " \t\n\r", &saveptr)) && (!strcasecmp(answer, "yes") || !strcasecmp(answer, "y")); } + +/* + * For a given: + * - file or directory return the containing tree root id + * - subvolume return it's own tree id + * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is + * undefined and function returns -1 + */ +int lookup_ino_rootid(int fd, u64 *rootid) +{ + struct btrfs_ioctl_ino_lookup_args args; + int ret; + int e; + + memset(&args, 0, sizeof(args)); + args.treeid = 0; + args.objectid = BTRFS_FIRST_FREE_OBJECTID; + + ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); + e = errno; + if (ret) { + fprintf(stderr, "ERROR: Failed to lookup root id - %s\n", + strerror(e)); + return ret; + } + + *rootid = args.treeid; + + return 0; +} -- cgit v1.2.1