mirror of
https://github.com/kdave/btrfs-progs
synced 2025-02-21 12:26:51 +00:00
btrfs-progs: split open/close helpers from utils.c
There's a group of functions that are related to opening filesystem in various modes, this can be moved to a separate file. Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
b19a603d62
commit
7fa07e2abb
3
Makefile
3
Makefile
@ -173,7 +173,8 @@ libbtrfs_objects = common/send-stream.o common/send-utils.o kernel-lib/rbtree.o
|
||||
kernel-lib/raid56.o kernel-lib/tables.o \
|
||||
common/device-scan.o common/path-utils.o \
|
||||
common/utils.o libbtrfsutil/subvolume.o libbtrfsutil/stubs.o \
|
||||
crypto/hash.o crypto/xxhash.o $(CRYPTO_OBJECTS)
|
||||
crypto/hash.o crypto/xxhash.o $(CRYPTO_OBJECTS) \
|
||||
common/open-utils.o
|
||||
libbtrfs_headers = common/send-stream.h common/send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
|
||||
crypto/crc32c.h kernel-lib/list.h kerncompat.h \
|
||||
kernel-lib/radix-tree.h kernel-lib/sizes.h kernel-lib/raid56.h \
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "kernel-lib/list.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/help.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static void print_usage(void)
|
||||
{
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "kernel-shared/transaction.h"
|
||||
#include "common/utils.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "common/help.h"
|
||||
#include "common/box.h"
|
||||
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "check/mode-original.h"
|
||||
#include "check/mode-lowmem.h"
|
||||
#include "check/qgroup-verify.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
u64 bytes_used = 0;
|
||||
u64 total_csum_bytes = 0;
|
||||
|
@ -29,7 +29,7 @@
|
||||
#include "kernel-shared/ctree.h"
|
||||
#include "ioctl.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
|
||||
#include "common/open-utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/help.h"
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "common/device-utils.h"
|
||||
#include "common/device-scan.h"
|
||||
#include "common/format-output.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "mkfs/common.h"
|
||||
|
||||
static const char * const device_cmd_group_usage[] = {
|
||||
|
@ -38,8 +38,8 @@
|
||||
#include "cmds/commands.h"
|
||||
#include "kerncompat.h"
|
||||
#include "kernel-lib/rbtree.h"
|
||||
|
||||
#include "kernel-lib/interval_tree_generic.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "common/help.h"
|
||||
#include "common/fsfeatures.h"
|
||||
|
||||
|
@ -32,10 +32,11 @@
|
||||
#include "cmds/filesystem-usage.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "kernel-shared/disk-io.h"
|
||||
|
||||
#include "common/open-utils.h"
|
||||
#include "version.h"
|
||||
#include "common/help.h"
|
||||
#include "common/device-utils.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
/*
|
||||
* Add the chunk info to the chunk_info list
|
||||
|
@ -43,6 +43,7 @@
|
||||
#include "common/fsfeatures.h"
|
||||
#include "common/path-utils.h"
|
||||
#include "common/device-scan.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
/*
|
||||
* for btrfs fi show, we maintain a hash of fsids we've already printed.
|
||||
|
@ -36,6 +36,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/help.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static int verbose = 0;
|
||||
static int no_pretty = 0;
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "cmds/commands.h"
|
||||
#include "btrfs-list.h"
|
||||
#include "common/help.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static const char * const inspect_cmd_group_usage[] = {
|
||||
"btrfs inspect-internal <command> <args>",
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
#include "kernel-shared/ctree.h"
|
||||
#include "ioctl.h"
|
||||
|
||||
#include "common/open-utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "qgroup.h"
|
||||
#include "common/utils.h"
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "cmds/commands.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/help.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static const char * const quota_cmd_group_usage[] = {
|
||||
"btrfs quota <command> [options] <path>",
|
||||
|
@ -36,7 +36,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "kernel-shared/disk-io.h"
|
||||
|
||||
#include "common/open-utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/help.h"
|
||||
#include "common/path-utils.h"
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "common/help.h"
|
||||
#include "cmds/rescue.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static const char * const rescue_cmd_group_usage[] = {
|
||||
"btrfs rescue <command> [options] <path>",
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/help.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static char fs_name[PATH_MAX];
|
||||
static char path_name[PATH_MAX];
|
||||
|
@ -42,7 +42,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "kernel-shared/disk-io.h"
|
||||
|
||||
#include "common/open-utils.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/help.h"
|
||||
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "common/help.h"
|
||||
#include "common/path-utils.h"
|
||||
#include "common/device-scan.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
static int wait_for_subvolume_cleaning(int fd, size_t count, uint64_t *ids,
|
||||
int sleep_interval)
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "common/messages.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/defs.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "kernel-shared/ctree.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "kernel-shared/disk-io.h"
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define __DEVICE_SCAN_H__
|
||||
|
||||
#include "kerncompat.h"
|
||||
#include <dirent.h>
|
||||
#include "ioctl.h"
|
||||
|
||||
#define BTRFS_SCAN_MOUNTED (1ULL << 0)
|
||||
|
312
common/open-utils.c
Normal file
312
common/open-utils.c
Normal file
@ -0,0 +1,312 @@
|
||||
/*
|
||||
* 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 <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <linux/magic.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <mntent.h>
|
||||
#include "kernel-shared/ctree.h"
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "kernel-shared/disk-io.h"
|
||||
#include "common/messages.h"
|
||||
#include "common/path-utils.h"
|
||||
#include "common/device-scan.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
/*
|
||||
* Check if a file is used (directly or indirectly via a loop device) by a
|
||||
* device in fs_devices
|
||||
*/
|
||||
static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
|
||||
const char* file)
|
||||
{
|
||||
int ret;
|
||||
struct btrfs_device *device;
|
||||
|
||||
list_for_each_entry(device, &fs_devices->devices, dev_list) {
|
||||
if((ret = is_same_loop_file(device->name, file)))
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check_mounted_where(int fd, const char *file, char *where, int size,
|
||||
struct btrfs_fs_devices **fs_dev_ret, unsigned sbflags)
|
||||
{
|
||||
int ret;
|
||||
u64 total_devs = 1;
|
||||
int is_btrfs;
|
||||
struct btrfs_fs_devices *fs_devices_mnt = NULL;
|
||||
FILE *f;
|
||||
struct mntent *mnt;
|
||||
|
||||
/* scan the initial device */
|
||||
ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
|
||||
&total_devs, BTRFS_SUPER_INFO_OFFSET, sbflags);
|
||||
is_btrfs = (ret >= 0);
|
||||
|
||||
/* scan other devices */
|
||||
if (is_btrfs && total_devs > 1) {
|
||||
ret = btrfs_scan_devices(0);
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* iterate over the list of currently mounted filesystems */
|
||||
if ((f = setmntent ("/proc/self/mounts", "r")) == NULL)
|
||||
return -errno;
|
||||
|
||||
while ((mnt = getmntent (f)) != NULL) {
|
||||
if(is_btrfs) {
|
||||
if(strcmp(mnt->mnt_type, "btrfs") != 0)
|
||||
continue;
|
||||
|
||||
ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
|
||||
} else {
|
||||
/* ignore entries in the mount table that are not
|
||||
associated with a file*/
|
||||
if((ret = path_is_reg_or_block_device(mnt->mnt_fsname)) < 0)
|
||||
goto out_mntloop_err;
|
||||
else if(!ret)
|
||||
continue;
|
||||
|
||||
ret = is_same_loop_file(file, mnt->mnt_fsname);
|
||||
}
|
||||
|
||||
if(ret < 0)
|
||||
goto out_mntloop_err;
|
||||
else if(ret)
|
||||
break;
|
||||
}
|
||||
|
||||
/* Did we find an entry in mnt table? */
|
||||
if (mnt && size && where) {
|
||||
strncpy(where, mnt->mnt_dir, size);
|
||||
where[size-1] = 0;
|
||||
}
|
||||
if (fs_dev_ret)
|
||||
*fs_dev_ret = fs_devices_mnt;
|
||||
|
||||
ret = (mnt != NULL);
|
||||
|
||||
out_mntloop_err:
|
||||
endmntent (f);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns 1 if the device was mounted, < 0 on error or 0 if everything
|
||||
* is safe to continue.
|
||||
*/
|
||||
int check_mounted(const char* file)
|
||||
{
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
error("mount check: cannot open %s: %m", file);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
ret = check_mounted_where(fd, file, NULL, 0, NULL, SBREAD_DEFAULT);
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the mount point for a mounted device.
|
||||
* On success, returns 0 with mountpoint in *mp.
|
||||
* On failure, returns -errno (not mounted yields -EINVAL)
|
||||
* Is noisy on failures, expects to be given a mounted device.
|
||||
*/
|
||||
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
|
||||
{
|
||||
int ret;
|
||||
int fd = -1;
|
||||
|
||||
ret = path_is_block_device(dev);
|
||||
if (ret <= 0) {
|
||||
if (!ret) {
|
||||
error("not a block device: %s", dev);
|
||||
ret = -EINVAL;
|
||||
} else {
|
||||
errno = -ret;
|
||||
error("cannot check %s: %m", dev);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
fd = open(dev, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
ret = -errno;
|
||||
error("cannot open %s: %m", dev);
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = check_mounted_where(fd, dev, mp, mp_size, NULL, SBREAD_DEFAULT);
|
||||
if (!ret) {
|
||||
ret = -EINVAL;
|
||||
} else { /* mounted, all good */
|
||||
ret = 0;
|
||||
}
|
||||
out:
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a pathname, return a filehandle to:
|
||||
* the original pathname or,
|
||||
* if the pathname is a mounted btrfs device, to its mountpoint.
|
||||
*
|
||||
* On error, return -1, errno should be set.
|
||||
*/
|
||||
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
char mp[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
if (path_is_block_device(path)) {
|
||||
ret = get_btrfs_mount(path, mp, sizeof(mp));
|
||||
if (ret < 0) {
|
||||
/* not a mounted btrfs dev */
|
||||
error_on(verbose, "'%s' is not a mounted btrfs device",
|
||||
path);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
ret = open_file_or_dir(mp, dirstream);
|
||||
error_on(verbose && ret < 0, "can't access '%s': %m",
|
||||
path);
|
||||
} else {
|
||||
ret = btrfs_open_dir(path, dirstream, 1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do the following checks before calling open_file_or_dir():
|
||||
* 1: path is in a btrfs filesystem
|
||||
* 2: path is a directory if dir_only is 1
|
||||
*/
|
||||
int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
|
||||
{
|
||||
struct statfs stfs;
|
||||
struct stat st;
|
||||
int ret;
|
||||
|
||||
if (stat(path, &st) != 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dir_only && !S_ISDIR(st.st_mode)) {
|
||||
error_on(verbose, "not a directory: %s", path);
|
||||
return -3;
|
||||
}
|
||||
|
||||
if (statfs(path, &stfs) != 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (stfs.f_type != BTRFS_SUPER_MAGIC) {
|
||||
error_on(verbose, "not a btrfs filesystem: %s", path);
|
||||
return -2;
|
||||
}
|
||||
|
||||
ret = open_file_or_dir(path, dirstream);
|
||||
if (ret < 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
return btrfs_open(path, dirstream, verbose, 1);
|
||||
}
|
||||
|
||||
int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
return btrfs_open(path, dirstream, verbose, 0);
|
||||
}
|
||||
|
||||
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags)
|
||||
{
|
||||
int ret;
|
||||
struct stat st;
|
||||
int fd;
|
||||
|
||||
ret = stat(fname, &st);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
*dirstream = opendir(fname);
|
||||
if (!*dirstream)
|
||||
return -1;
|
||||
fd = dirfd(*dirstream);
|
||||
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
|
||||
fd = open(fname, open_flags);
|
||||
} else {
|
||||
/*
|
||||
* we set this on purpose, in case the caller output
|
||||
* strerror(errno) as success
|
||||
*/
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (fd < 0) {
|
||||
fd = -1;
|
||||
if (*dirstream) {
|
||||
closedir(*dirstream);
|
||||
*dirstream = NULL;
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int open_file_or_dir(const char *fname, DIR **dirstream)
|
||||
{
|
||||
return open_file_or_dir3(fname, dirstream, O_RDWR);
|
||||
}
|
||||
|
||||
void close_file_or_dir(int fd, DIR *dirstream)
|
||||
{
|
||||
int old_errno;
|
||||
|
||||
old_errno = errno;
|
||||
if (dirstream) {
|
||||
closedir(dirstream);
|
||||
} else if (fd >= 0) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
errno = old_errno;
|
||||
}
|
||||
|
||||
|
39
common/open-utils.h
Normal file
39
common/open-utils.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 __OPEN_UTILS_H__
|
||||
#define __OPEN_UTILS_H__
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
struct btrfs_fs_devices;
|
||||
|
||||
int check_mounted_where(int fd, const char *file, char *where, int size,
|
||||
struct btrfs_fs_devices **fs_dev_ret, unsigned sbflags);
|
||||
int check_mounted(const char* file);
|
||||
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
|
||||
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
|
||||
|
||||
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags);
|
||||
int open_file_or_dir(const char *fname, DIR **dirstream);
|
||||
|
||||
int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only);
|
||||
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose);
|
||||
int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose);
|
||||
|
||||
void close_file_or_dir(int fd, DIR *dirstream);
|
||||
|
||||
#endif
|
280
common/utils.c
280
common/utils.c
@ -51,6 +51,7 @@
|
||||
#include "kernel-shared/volumes.h"
|
||||
#include "ioctl.h"
|
||||
#include "cmds/commands.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "mkfs/common.h"
|
||||
|
||||
static int rand_seed_initialized = 0;
|
||||
@ -97,231 +98,6 @@ error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the mount point for a mounted device.
|
||||
* On success, returns 0 with mountpoint in *mp.
|
||||
* On failure, returns -errno (not mounted yields -EINVAL)
|
||||
* Is noisy on failures, expects to be given a mounted device.
|
||||
*/
|
||||
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
|
||||
{
|
||||
int ret;
|
||||
int fd = -1;
|
||||
|
||||
ret = path_is_block_device(dev);
|
||||
if (ret <= 0) {
|
||||
if (!ret) {
|
||||
error("not a block device: %s", dev);
|
||||
ret = -EINVAL;
|
||||
} else {
|
||||
errno = -ret;
|
||||
error("cannot check %s: %m", dev);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
fd = open(dev, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
ret = -errno;
|
||||
error("cannot open %s: %m", dev);
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = check_mounted_where(fd, dev, mp, mp_size, NULL, SBREAD_DEFAULT);
|
||||
if (!ret) {
|
||||
ret = -EINVAL;
|
||||
} else { /* mounted, all good */
|
||||
ret = 0;
|
||||
}
|
||||
out:
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a pathname, return a filehandle to:
|
||||
* the original pathname or,
|
||||
* if the pathname is a mounted btrfs device, to its mountpoint.
|
||||
*
|
||||
* On error, return -1, errno should be set.
|
||||
*/
|
||||
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
char mp[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
if (path_is_block_device(path)) {
|
||||
ret = get_btrfs_mount(path, mp, sizeof(mp));
|
||||
if (ret < 0) {
|
||||
/* not a mounted btrfs dev */
|
||||
error_on(verbose, "'%s' is not a mounted btrfs device",
|
||||
path);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
ret = open_file_or_dir(mp, dirstream);
|
||||
error_on(verbose && ret < 0, "can't access '%s': %m",
|
||||
path);
|
||||
} else {
|
||||
ret = btrfs_open_dir(path, dirstream, 1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do the following checks before calling open_file_or_dir():
|
||||
* 1: path is in a btrfs filesystem
|
||||
* 2: path is a directory if dir_only is 1
|
||||
*/
|
||||
int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
|
||||
{
|
||||
struct statfs stfs;
|
||||
struct stat st;
|
||||
int ret;
|
||||
|
||||
if (stat(path, &st) != 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dir_only && !S_ISDIR(st.st_mode)) {
|
||||
error_on(verbose, "not a directory: %s", path);
|
||||
return -3;
|
||||
}
|
||||
|
||||
if (statfs(path, &stfs) != 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (stfs.f_type != BTRFS_SUPER_MAGIC) {
|
||||
error_on(verbose, "not a btrfs filesystem: %s", path);
|
||||
return -2;
|
||||
}
|
||||
|
||||
ret = open_file_or_dir(path, dirstream);
|
||||
if (ret < 0) {
|
||||
error_on(verbose, "cannot access '%s': %m", path);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
return btrfs_open(path, dirstream, verbose, 1);
|
||||
}
|
||||
|
||||
int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose)
|
||||
{
|
||||
return btrfs_open(path, dirstream, verbose, 0);
|
||||
}
|
||||
|
||||
/* Checks if a file is used (directly or indirectly via a loop device)
|
||||
* by a device in fs_devices
|
||||
*/
|
||||
static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
|
||||
const char* file)
|
||||
{
|
||||
int ret;
|
||||
struct btrfs_device *device;
|
||||
|
||||
list_for_each_entry(device, &fs_devices->devices, dev_list) {
|
||||
if((ret = is_same_loop_file(device->name, file)))
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns 1 if the device was mounted, < 0 on error or 0 if everything
|
||||
* is safe to continue.
|
||||
*/
|
||||
int check_mounted(const char* file)
|
||||
{
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
error("mount check: cannot open %s: %m", file);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
ret = check_mounted_where(fd, file, NULL, 0, NULL, SBREAD_DEFAULT);
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int check_mounted_where(int fd, const char *file, char *where, int size,
|
||||
struct btrfs_fs_devices **fs_dev_ret, unsigned sbflags)
|
||||
{
|
||||
int ret;
|
||||
u64 total_devs = 1;
|
||||
int is_btrfs;
|
||||
struct btrfs_fs_devices *fs_devices_mnt = NULL;
|
||||
FILE *f;
|
||||
struct mntent *mnt;
|
||||
|
||||
/* scan the initial device */
|
||||
ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
|
||||
&total_devs, BTRFS_SUPER_INFO_OFFSET, sbflags);
|
||||
is_btrfs = (ret >= 0);
|
||||
|
||||
/* scan other devices */
|
||||
if (is_btrfs && total_devs > 1) {
|
||||
ret = btrfs_scan_devices(0);
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* iterate over the list of currently mounted filesystems */
|
||||
if ((f = setmntent ("/proc/self/mounts", "r")) == NULL)
|
||||
return -errno;
|
||||
|
||||
while ((mnt = getmntent (f)) != NULL) {
|
||||
if(is_btrfs) {
|
||||
if(strcmp(mnt->mnt_type, "btrfs") != 0)
|
||||
continue;
|
||||
|
||||
ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
|
||||
} else {
|
||||
/* ignore entries in the mount table that are not
|
||||
associated with a file*/
|
||||
if((ret = path_is_reg_or_block_device(mnt->mnt_fsname)) < 0)
|
||||
goto out_mntloop_err;
|
||||
else if(!ret)
|
||||
continue;
|
||||
|
||||
ret = is_same_loop_file(file, mnt->mnt_fsname);
|
||||
}
|
||||
|
||||
if(ret < 0)
|
||||
goto out_mntloop_err;
|
||||
else if(ret)
|
||||
break;
|
||||
}
|
||||
|
||||
/* Did we find an entry in mnt table? */
|
||||
if (mnt && size && where) {
|
||||
strncpy(where, mnt->mnt_dir, size);
|
||||
where[size-1] = 0;
|
||||
}
|
||||
if (fs_dev_ret)
|
||||
*fs_dev_ret = fs_devices_mnt;
|
||||
|
||||
ret = (mnt != NULL);
|
||||
|
||||
out_mntloop_err:
|
||||
endmntent (f);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct pending_dir {
|
||||
struct list_head list;
|
||||
char name[PATH_MAX];
|
||||
@ -779,60 +555,6 @@ enum btrfs_csum_type parse_csum_type(const char *s)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags)
|
||||
{
|
||||
int ret;
|
||||
struct stat st;
|
||||
int fd;
|
||||
|
||||
ret = stat(fname, &st);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
*dirstream = opendir(fname);
|
||||
if (!*dirstream)
|
||||
return -1;
|
||||
fd = dirfd(*dirstream);
|
||||
} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
|
||||
fd = open(fname, open_flags);
|
||||
} else {
|
||||
/*
|
||||
* we set this on purpose, in case the caller output
|
||||
* strerror(errno) as success
|
||||
*/
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (fd < 0) {
|
||||
fd = -1;
|
||||
if (*dirstream) {
|
||||
closedir(*dirstream);
|
||||
*dirstream = NULL;
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int open_file_or_dir(const char *fname, DIR **dirstream)
|
||||
{
|
||||
return open_file_or_dir3(fname, dirstream, O_RDWR);
|
||||
}
|
||||
|
||||
void close_file_or_dir(int fd, DIR *dirstream)
|
||||
{
|
||||
int old_errno;
|
||||
|
||||
old_errno = errno;
|
||||
if (dirstream) {
|
||||
closedir(dirstream);
|
||||
} else if (fd >= 0) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
errno = old_errno;
|
||||
}
|
||||
|
||||
int get_device_info(int fd, u64 devid,
|
||||
struct btrfs_ioctl_dev_info_args *di_args)
|
||||
{
|
||||
|
@ -68,10 +68,6 @@ void units_set_base(unsigned *units, unsigned base);
|
||||
|
||||
int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, u64 objectid);
|
||||
int check_mounted(const char *devicename);
|
||||
int check_mounted_where(int fd, const char *file, char *where, int size,
|
||||
struct btrfs_fs_devices **fs_devices_mnt, unsigned sbflags);
|
||||
|
||||
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, unsigned unit_mode);
|
||||
#define pretty_size(size) pretty_size_mode(size, UNITS_DEFAULT)
|
||||
const char *pretty_size_mode(u64 size, unsigned mode);
|
||||
@ -80,9 +76,6 @@ enum btrfs_csum_type parse_csum_type(const char *s);
|
||||
u64 parse_size_from_string(const char *s);
|
||||
u64 parse_qgroupid(const char *p);
|
||||
u64 arg_strtou64(const char *str);
|
||||
int open_file_or_dir(const char *fname, DIR **dirstream);
|
||||
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags);
|
||||
void close_file_or_dir(int fd, DIR *dirstream);
|
||||
int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
|
||||
struct btrfs_ioctl_dev_info_args **di_ret);
|
||||
int get_fsid(const char *path, u8 *fsid, int silent);
|
||||
@ -95,10 +88,6 @@ int get_label(const char *btrfs_dev, char *label);
|
||||
int set_label(const char *btrfs_dev, const char *label);
|
||||
|
||||
int check_arg_type(const char *input);
|
||||
int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
|
||||
int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only);
|
||||
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose);
|
||||
int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose);
|
||||
int get_label_mounted(const char *mount_path, char *labelp);
|
||||
int get_label_unmounted(const char *dev, char *label);
|
||||
int group_profile_max_safe_loss(u64 flags);
|
||||
@ -106,7 +95,6 @@ int csum_tree_block(struct btrfs_fs_info *root, struct extent_buffer *buf,
|
||||
int verify);
|
||||
int ask_user(const char *question);
|
||||
int lookup_path_rootid(int fd, u64 *rootid);
|
||||
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
|
||||
int find_mount_fsroot(const char *subvol, const char *subvolid, char **mount);
|
||||
int find_mount_root(const char *path, char **mount_root);
|
||||
int get_device_info(int fd, u64 devid,
|
||||
|
@ -105,6 +105,7 @@
|
||||
#include "crypto/crc32c.h"
|
||||
#include "common/fsfeatures.h"
|
||||
#include "common/box.h"
|
||||
#include "common/open-utils.h"
|
||||
|
||||
extern const struct btrfs_convert_operations ext2_convert_ops;
|
||||
extern const struct btrfs_convert_operations reiserfs_convert_ops;
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "common/extent-cache.h"
|
||||
#include "common/help.h"
|
||||
#include "common/device-utils.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "image/metadump.h"
|
||||
#include "image/sanitize.h"
|
||||
#include "common/box.h"
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "common/utils.h"
|
||||
#include "common/path-utils.h"
|
||||
#include "common/device-utils.h"
|
||||
#include "common/open-utils.h"
|
||||
#include "mkfs/common.h"
|
||||
|
||||
static u64 reference_root_table[] = {
|
||||
|
Loading…
Reference in New Issue
Block a user