btrfs-progs: move sysfs related helpers to own file

The sysfs could use more convenience helpers so move the current code to
own file before adding more helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-08-21 21:13:24 +02:00
parent 6abbad4d1b
commit 4a96a935ad
9 changed files with 127 additions and 77 deletions

View File

@ -214,6 +214,7 @@ objects = \
common/sort-utils.o \
common/string-table.o \
common/string-utils.o \
common/sysfs-utils.o \
common/task-utils.o \
common/units.o \
common/utils.o \

View File

@ -38,6 +38,7 @@
#include "common/units.h"
#include "common/help.h"
#include "common/device-utils.h"
#include "common/sysfs-utils.h"
#include "common/messages.h"
#include "common/path-utils.h"
#include "cmds/filesystem-usage.h"

View File

@ -33,6 +33,7 @@
#include "common/send-utils.h"
#include "common/help.h"
#include "common/path-utils.h"
#include "common/sysfs-utils.h"
#include "common/string-utils.h"
#include "common/messages.h"
#include "cmds/commands.h"

View File

@ -36,6 +36,7 @@
#include "kernel-shared/ctree.h"
#include "kernel-shared/zoned.h"
#include "common/device-utils.h"
#include "common/sysfs-utils.h"
#include "common/path-utils.h"
#include "common/internal.h"
#include "common/messages.h"

View File

@ -28,6 +28,7 @@
#include "kernel-shared/ctree.h"
#include "common/fsfeatures.h"
#include "common/string-utils.h"
#include "common/sysfs-utils.h"
#include "common/utils.h"
#include "common/messages.h"

96
common/sysfs-utils.c Normal file
View File

@ -0,0 +1,96 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "kerncompat.h"
#include <unistd.h>
#include <fcntl.h>
#include <uuid/uuid.h>
#include "common/sysfs-utils.h"
#include "common/path-utils.h"
#include "common/utils.h"
/*
* 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);
}
/*
* Open a file in the toplevel sysfs directory and return the file descriptor
* or error.
*/
int sysfs_open_file(const char *name)
{
char path[PATH_MAX];
int ret;
ret = path_cat_out(path, "/sys/fs/btrfs", name);
if (ret < 0)
return ret;
return open(path, O_RDONLY);
}
/*
* Open a directory by name in fsid directory in sysfs and return the file
* descriptor or error, filedescriptor suitable for fdreaddir. The @dirname
* must be a directory name.
*/
int sysfs_open_fsid_dir(int fd, const char *dirname)
{
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, dirname);
if (ret < 0)
return ret;
return open(sysfs_file, O_DIRECTORY | 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);
}

25
common/sysfs-utils.h Normal file
View File

@ -0,0 +1,25 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#ifndef __COMMON_SYSFS_UTILS__
#define __COMMON_SYSFS_UTILS__
int sysfs_open_file(const char *name);
int sysfs_open_fsid_file(int fd, const char *filename);
int sysfs_read_file(int fd, char *buf, size_t size);
int sysfs_open_fsid_dir(int fd, const char *dirname);
#endif

View File

@ -36,6 +36,7 @@
#include "common/utils.h"
#include "common/path-utils.h"
#include "common/open-utils.h"
#include "common/sysfs-utils.h"
#include "common/messages.h"
#include "cmds/commands.h"
#include "mkfs/common.h"
@ -1160,78 +1161,6 @@ void btrfs_warn_experimental(const char *str)
#endif
}
/*
* 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);
}
/*
* Open a file in the toplevel sysfs directory and return the file descriptor
* or error.
*/
int sysfs_open_file(const char *name)
{
char path[PATH_MAX];
int ret;
ret = path_cat_out(path, "/sys/fs/btrfs", name);
if (ret < 0)
return ret;
return open(path, O_RDONLY);
}
/*
* Open a directory by name in fsid directory in sysfs and return the file
* descriptor or error, filedescriptor suitable for fdreaddir. The @dirname
* must be a directory name.
*/
int sysfs_open_fsid_dir(int fd, const char *dirname)
{
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, dirname);
if (ret < 0)
return ret;
return open(sysfs_file, O_DIRECTORY | 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);
}
static const char exclop_def[][16] = {
[BTRFS_EXCLOP_NONE] = "none",
[BTRFS_EXCLOP_BALANCE] = "balance",

View File

@ -112,11 +112,6 @@ char *btrfs_test_for_multiple_profiles(int fd);
int btrfs_warn_multiple_profiles(int fd);
void btrfs_warn_experimental(const char *str);
int sysfs_open_file(const char *name);
int sysfs_open_fsid_file(int fd, const char *filename);
int sysfs_read_file(int fd, char *buf, size_t size);
int sysfs_open_fsid_dir(int fd, const char *dirname);
/* An error code to error string mapping for the kernel error codes */
static inline char *btrfs_err_str(enum btrfs_err_code err_code)
{