2021-04-29 17:52:15 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "kernel-lib/list.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#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,
|
2023-06-13 10:26:57 +00:00
|
|
|
struct btrfs_fs_devices **fs_dev_ret, unsigned sbflags,
|
|
|
|
bool noscan)
|
2021-04-29 17:52:15 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u64 total_devs = 1;
|
2023-06-13 10:26:52 +00:00
|
|
|
bool is_btrfs;
|
2021-04-29 17:52:15 +00:00
|
|
|
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;
|
2023-06-13 10:26:57 +00:00
|
|
|
else if (noscan)
|
|
|
|
btrfs_close_all_devices();
|
2021-04-29 17:52:15 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:26:57 +00:00
|
|
|
ret = check_mounted_where(fd, file, NULL, 0, NULL, SBREAD_DEFAULT, false);
|
2021-04-29 17:52:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:26:57 +00:00
|
|
|
ret = check_mounted_where(fd, dev, mp, mp_size, NULL, SBREAD_DEFAULT, false);
|
2021-04-29 17:52:15 +00:00
|
|
|
if (!ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else { /* mounted, all good */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:19:19 +00:00
|
|
|
/*
|
|
|
|
* Open the given path and check if it's a btrfs filesystem.
|
2024-02-20 10:57:07 +00:00
|
|
|
*
|
|
|
|
* Return the file descriptor or -errno.
|
2024-02-08 20:19:19 +00:00
|
|
|
*/
|
2024-02-20 11:38:36 +00:00
|
|
|
int btrfs_open_fd2(const char *path, bool read_write, bool dir_only)
|
2024-02-08 20:19:19 +00:00
|
|
|
{
|
|
|
|
struct statfs stfs;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
|
2024-02-20 10:57:07 +00:00
|
|
|
if (stat(path, &st) < 0) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("cannot access '%s': %m", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
return -errno;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_only && !S_ISDIR(st.st_mode)) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("not a directory: %s", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
return -ENOTDIR;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:57:07 +00:00
|
|
|
if (statfs(path, &stfs) < 0) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("cannot access '%s': %m", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
return -errno;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
2021-04-29 17:52:15 +00:00
|
|
|
|
2024-02-08 20:19:19 +00:00
|
|
|
if (stfs.f_type != BTRFS_SUPER_MAGIC) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("not a btrfs filesystem: %s", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
return -EINVAL;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode) || !read_write)
|
|
|
|
ret = open(path, O_RDONLY);
|
|
|
|
else
|
|
|
|
ret = open(path, O_RDWR);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("cannot access '%s': %m", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
ret = -errno;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_open_file_or_dir_fd(const char *path)
|
|
|
|
{
|
2024-02-20 11:38:36 +00:00
|
|
|
return btrfs_open_fd2(path, true, false);
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 11:56:24 +00:00
|
|
|
/* Open the path for write and check that it's a directory. */
|
|
|
|
int btrfs_open_dir(const char *path)
|
2024-02-08 20:19:19 +00:00
|
|
|
{
|
2024-02-20 11:38:36 +00:00
|
|
|
return btrfs_open_fd2(path, true, true);
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a path, return a file descriptor to the original path name or, if the
|
|
|
|
* pathname is a mounted btrfs device, to its mountpoint.
|
|
|
|
*
|
2024-02-20 10:57:07 +00:00
|
|
|
* Return the file descriptor or -errno.
|
2024-02-08 20:19:19 +00:00
|
|
|
*/
|
2024-02-20 11:56:24 +00:00
|
|
|
int btrfs_open_mnt(const char *path)
|
2024-02-08 20:19:19 +00:00
|
|
|
{
|
|
|
|
char mp[PATH_MAX];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (path_is_block_device(path)) {
|
|
|
|
ret = get_btrfs_mount(path, mp, sizeof(mp));
|
|
|
|
if (ret < 0) {
|
2024-02-20 11:38:36 +00:00
|
|
|
error("'%s' is not a mounted btrfs device", path);
|
2024-02-20 10:57:07 +00:00
|
|
|
return -EINVAL;
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
2024-02-20 11:56:24 +00:00
|
|
|
ret = btrfs_open_dir(mp);
|
2024-02-08 20:19:19 +00:00
|
|
|
} else {
|
2024-02-20 11:56:24 +00:00
|
|
|
ret = btrfs_open_dir(path);
|
2024-02-08 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|