btrfs-progs: add sysfs file reading helpers

Add helpers to open and read sysfs files from the per-fs directory.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Goldwyn Rodrigues 2020-08-25 10:03:36 -05:00 committed by David Sterba
parent c4b84f1c5d
commit 34f0594bca
2 changed files with 36 additions and 0 deletions

View File

@ -1892,3 +1892,36 @@ int btrfs_warn_multiple_profiles(int fd)
return 1;
}
/*
* Open a file in fsid directory in sysfs and return the file descriptor or
* error
*/
int sysfs_open_fsid_file(int fd, const char *filename)
{
u8 fsid[BTRFS_UUID_SIZE];
char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
char sysfs_file[PATH_MAX];
int ret;
ret = get_fsid_fd(fd, fsid);
if (ret < 0)
return ret;
uuid_unparse(fsid, fsid_str);
ret = path_cat3_out(sysfs_file, "/sys/fs/btrfs", fsid_str, filename);
if (ret < 0)
return ret;
return open(sysfs_file, O_RDONLY);
}
/*
* Read up to @size bytes to @buf from @fd
*/
int sysfs_read_file(int fd, char *buf, size_t size)
{
lseek(fd, 0, SEEK_SET);
memset(buf, 0, size);
return read(fd, buf, size);
}

View File

@ -153,4 +153,7 @@ void init_rand_seed(u64 seed);
char *btrfs_test_for_multiple_profiles(int fd);
int btrfs_warn_multiple_profiles(int fd);
int sysfs_open_fsid_file(int fd, const char *filename);
int sysfs_read_file(int fd, char *buf, size_t size);
#endif