2019-07-02 06:42:23 +08: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 <sys/ioctl.h>
|
2022-09-15 19:59:39 +08:00
|
|
|
#include <sys/stat.h>
|
2021-06-25 04:38:59 +08:00
|
|
|
#include <sys/types.h>
|
2022-09-14 23:06:52 +08:00
|
|
|
#include <linux/limits.h>
|
2022-09-15 19:59:39 +08:00
|
|
|
#ifdef BTRFS_ZONED
|
|
|
|
#include <linux/blkzoned.h>
|
|
|
|
#endif
|
2022-09-14 23:06:52 +08:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <limits.h>
|
2019-07-02 06:42:23 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2021-06-25 04:38:59 +08:00
|
|
|
#include <dirent.h>
|
2022-09-15 19:59:39 +08:00
|
|
|
#include <errno.h>
|
2019-07-02 06:42:23 +08:00
|
|
|
#include <blkid/blkid.h>
|
|
|
|
#include "kernel-lib/sizes.h"
|
2020-08-18 21:56:04 +08:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2022-09-15 19:59:39 +08:00
|
|
|
#include "kernel-shared/ctree.h"
|
2021-04-26 14:27:33 +08:00
|
|
|
#include "kernel-shared/zoned.h"
|
2023-08-29 04:12:13 +08:00
|
|
|
#include "kernel-shared/uapi/btrfs.h"
|
|
|
|
#include "kernel-shared/uapi/btrfs_tree.h"
|
2019-07-02 06:42:23 +08:00
|
|
|
#include "common/device-utils.h"
|
2023-08-22 03:13:24 +08:00
|
|
|
#include "common/sysfs-utils.h"
|
2021-06-25 04:38:59 +08:00
|
|
|
#include "common/path-utils.h"
|
2019-07-02 06:42:23 +08:00
|
|
|
#include "common/internal.h"
|
|
|
|
#include "common/messages.h"
|
2021-04-30 05:43:24 +08:00
|
|
|
#include "common/units.h"
|
2019-07-02 06:42:23 +08:00
|
|
|
|
|
|
|
#ifndef BLKDISCARD
|
|
|
|
#define BLKDISCARD _IO(0x12,119)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard the given range in one go
|
|
|
|
*/
|
|
|
|
static int discard_range(int fd, u64 start, u64 len)
|
|
|
|
{
|
|
|
|
u64 range[2] = { start, len };
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKDISCARD, &range) < 0)
|
|
|
|
return errno;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-28 00:22:38 +08:00
|
|
|
static int discard_supported(const char *device)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[128] = {};
|
|
|
|
|
|
|
|
ret = device_get_queue_param(device, "discard_granularity", buf, sizeof(buf));
|
|
|
|
if (ret == 0) {
|
|
|
|
pr_verbose(3, "cannot read discard_granularity for %s\n", device);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2021-11-14 06:43:20 +08:00
|
|
|
if (atoi(buf) == 0) {
|
|
|
|
pr_verbose(3, "%s: discard_granularity %s", device, buf);
|
2021-09-28 00:22:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-02 06:42:23 +08:00
|
|
|
/*
|
|
|
|
* Discard blocks in the given range in 1G chunks, the process is interruptible
|
|
|
|
*/
|
2021-04-30 00:34:05 +08:00
|
|
|
int device_discard_blocks(int fd, u64 start, u64 len)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
while (len > 0) {
|
|
|
|
/* 1G granularity */
|
|
|
|
u64 chunk_size = min_t(u64, len, SZ_1G);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = discard_range(fd, start, chunk_size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
len -= chunk_size;
|
|
|
|
start += chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:36:44 +08:00
|
|
|
/*
|
|
|
|
* Write zeros to the given range [start, start + len)
|
|
|
|
*/
|
2021-10-05 14:23:02 +08:00
|
|
|
int device_zero_blocks(int fd, off_t start, size_t len, bool direct)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
char *buf = malloc(len);
|
|
|
|
int ret = 0;
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
memset(buf, 0, len);
|
2021-10-05 14:23:02 +08:00
|
|
|
written = btrfs_pwrite(fd, buf, len, start, direct);
|
2024-03-17 01:55:37 +08:00
|
|
|
if (written != len) {
|
|
|
|
error_msg(ERROR_MSG_WRITE, "zeroing range from %llu: %m",
|
|
|
|
(unsigned long long)start);
|
2019-07-02 06:42:23 +08:00
|
|
|
ret = -EIO;
|
2024-03-17 01:55:37 +08:00
|
|
|
}
|
2019-07-02 06:42:23 +08:00
|
|
|
free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ZERO_DEV_BYTES SZ_2M
|
|
|
|
|
2021-04-30 00:56:00 +08:00
|
|
|
/*
|
|
|
|
* Zero blocks in the range from start but not after the given device size.
|
|
|
|
* (On SPARC the disk labels are preserved too.)
|
|
|
|
*/
|
2021-04-26 14:27:34 +08:00
|
|
|
static int zero_dev_clamped(int fd, struct btrfs_zoned_device_info *zinfo,
|
|
|
|
off_t start, ssize_t len, u64 dev_size)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
off_t end = max(start, start + len);
|
|
|
|
|
|
|
|
#ifdef __sparc__
|
|
|
|
/* and don't overwrite the disk labels on sparc */
|
|
|
|
start = max(start, 1024);
|
|
|
|
end = max(end, 1024);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
start = min_t(u64, start, dev_size);
|
|
|
|
end = min_t(u64, end, dev_size);
|
|
|
|
|
2021-04-26 14:27:34 +08:00
|
|
|
if (zinfo && zinfo->model == ZONED_HOST_MANAGED)
|
|
|
|
return zero_zone_blocks(fd, zinfo, start, end - start);
|
|
|
|
|
2021-10-05 14:23:02 +08:00
|
|
|
return device_zero_blocks(fd, start, end - start, false);
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 00:56:00 +08:00
|
|
|
/*
|
|
|
|
* Find all magic signatures known to blkid and remove them
|
|
|
|
*/
|
2021-04-26 14:27:35 +08:00
|
|
|
static int btrfs_wipe_existing_sb(int fd, struct btrfs_zoned_device_info *zinfo)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
const char *off = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
loff_t offset;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int ret = 0;
|
|
|
|
blkid_probe pr = NULL;
|
|
|
|
|
|
|
|
pr = blkid_new_probe();
|
|
|
|
if (!pr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (blkid_probe_set_device(pr, fd, 0, 0)) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
|
|
|
|
if (!ret)
|
|
|
|
ret = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
|
|
|
|
|
|
|
|
if (ret || len == 0 || off == NULL) {
|
|
|
|
/*
|
|
|
|
* If lookup fails, the probe did not find any values, eg. for
|
|
|
|
* a file image or a loop device. Soft error.
|
|
|
|
*/
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = strtoll(off, NULL, 10);
|
|
|
|
if (len > sizeof(buf))
|
|
|
|
len = sizeof(buf);
|
|
|
|
|
2021-04-26 14:27:35 +08:00
|
|
|
if (!zone_is_sequential(zinfo, offset)) {
|
2021-10-05 14:23:02 +08:00
|
|
|
const bool direct = zinfo && zinfo->model == ZONED_HOST_MANAGED;
|
|
|
|
|
2021-04-26 14:27:35 +08:00
|
|
|
memset(buf, 0, len);
|
2021-10-05 14:23:02 +08:00
|
|
|
ret = btrfs_pwrite(fd, buf, len, offset, direct);
|
2021-04-26 14:27:35 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot wipe existing superblock: %m");
|
|
|
|
ret = -1;
|
|
|
|
} else if (ret != len) {
|
|
|
|
error("cannot wipe existing superblock: wrote %d of %zd",
|
|
|
|
ret, len);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct blk_zone *zone = &zinfo->zones[offset / zinfo->zone_size];
|
|
|
|
|
|
|
|
ret = btrfs_reset_dev_zone(fd, zone);
|
|
|
|
if (ret < 0) {
|
|
|
|
error(
|
|
|
|
"zoned: failed to wipe zones containing superblock: %m");
|
|
|
|
ret = -1;
|
|
|
|
}
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
fsync(fd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
blkid_free_probe(pr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:56:00 +08:00
|
|
|
/*
|
|
|
|
* Prepare a device before it's added to the filesystem. Optionally:
|
|
|
|
* - remove old superblocks
|
|
|
|
* - discard
|
|
|
|
* - reset zones
|
|
|
|
* - delete end of the device
|
|
|
|
*/
|
2024-05-29 15:13:16 +08:00
|
|
|
int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
|
|
|
|
u64 max_byte_count, unsigned opflags)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
2021-04-26 14:27:33 +08:00
|
|
|
struct btrfs_zoned_device_info *zinfo = NULL;
|
2024-05-29 15:13:16 +08:00
|
|
|
u64 byte_count;
|
2019-07-02 06:42:23 +08:00
|
|
|
struct stat st;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
ret = fstat(fd, &st);
|
|
|
|
if (ret < 0) {
|
|
|
|
error("unable to stat %s: %m", file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:13:16 +08:00
|
|
|
byte_count = device_get_partition_size_fd_stat(fd, &st);
|
|
|
|
if (byte_count == 0) {
|
2019-07-02 06:42:23 +08:00
|
|
|
error("unable to determine size of %s", file);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-05-29 15:13:16 +08:00
|
|
|
if (max_byte_count)
|
|
|
|
byte_count = min(byte_count, max_byte_count);
|
2019-07-02 06:42:23 +08:00
|
|
|
|
2021-04-26 14:27:33 +08:00
|
|
|
if (opflags & PREP_DEVICE_ZONED) {
|
|
|
|
ret = btrfs_get_zone_info(fd, file, &zinfo);
|
|
|
|
if (ret < 0 || !zinfo) {
|
|
|
|
error("zoned: unable to load zone information of %s",
|
|
|
|
file);
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-22 15:53:43 +08:00
|
|
|
|
|
|
|
if (!zinfo->emulated) {
|
|
|
|
if (opflags & PREP_DEVICE_VERBOSE)
|
2024-05-29 15:13:21 +08:00
|
|
|
printf("Resetting device zones %s (%llu zones) ...\n",
|
|
|
|
file, byte_count / zinfo->zone_size);
|
2021-09-22 15:53:43 +08:00
|
|
|
/*
|
|
|
|
* We cannot ignore zone reset errors for a zoned block
|
|
|
|
* device as this could result in the inability to write
|
|
|
|
* to non-empty sequential zones of the device.
|
|
|
|
*/
|
2024-05-29 15:13:21 +08:00
|
|
|
ret = btrfs_reset_zones(fd, zinfo, byte_count);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == EBUSY) {
|
|
|
|
error("zoned: device '%s' contains an active zone outside of fs range", file);
|
|
|
|
error("zoned: btrfs needs full control of active zones");
|
|
|
|
} else {
|
|
|
|
error("zoned: failed to reset device '%s' zones: %m", file);
|
|
|
|
}
|
2021-09-22 15:53:43 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2021-04-26 14:27:33 +08:00
|
|
|
}
|
|
|
|
} else if (opflags & PREP_DEVICE_DISCARD) {
|
2019-07-02 06:42:23 +08:00
|
|
|
/*
|
|
|
|
* We intentionally ignore errors from the discard ioctl. It
|
|
|
|
* is not necessary for the mkfs functionality but just an
|
|
|
|
* optimization.
|
|
|
|
*/
|
2021-09-28 00:22:38 +08:00
|
|
|
if (discard_supported(file)) {
|
2019-07-02 06:42:23 +08:00
|
|
|
if (opflags & PREP_DEVICE_VERBOSE)
|
|
|
|
printf("Performing full device TRIM %s (%s) ...\n",
|
2024-05-29 15:13:16 +08:00
|
|
|
file, pretty_size(byte_count));
|
|
|
|
device_discard_blocks(fd, 0, byte_count);
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:13:16 +08:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, 0, ZERO_DEV_BYTES, byte_count);
|
2019-07-02 06:42:23 +08:00
|
|
|
for (i = 0 ; !ret && i < BTRFS_SUPER_MIRROR_MAX; i++)
|
2021-04-26 14:27:34 +08:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, btrfs_sb_offset(i),
|
2024-05-29 15:13:16 +08:00
|
|
|
BTRFS_SUPER_INFO_SIZE, byte_count);
|
2019-07-02 06:42:23 +08:00
|
|
|
if (!ret && (opflags & PREP_DEVICE_ZERO_END))
|
2024-05-29 15:13:16 +08:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, byte_count - ZERO_DEV_BYTES,
|
|
|
|
ZERO_DEV_BYTES, byte_count);
|
2019-07-02 06:42:23 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = -ret;
|
|
|
|
error("failed to zero device '%s': %m", file);
|
2021-04-26 14:27:33 +08:00
|
|
|
goto err;
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:27:35 +08:00
|
|
|
ret = btrfs_wipe_existing_sb(fd, zinfo);
|
2019-07-02 06:42:23 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot wipe superblocks on %s", file);
|
2021-04-26 14:27:33 +08:00
|
|
|
goto err;
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:27:33 +08:00
|
|
|
free(zinfo);
|
2024-05-29 15:13:16 +08:00
|
|
|
*byte_count_ret = byte_count;
|
2019-07-02 06:42:23 +08:00
|
|
|
return 0;
|
2021-04-26 14:27:33 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
free(zinfo);
|
|
|
|
return 1;
|
2019-07-02 06:42:23 +08:00
|
|
|
}
|
|
|
|
|
2022-09-30 21:36:16 +08:00
|
|
|
u64 device_get_partition_size_fd_stat(int fd, const struct stat *st)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
u64 size;
|
2022-09-30 21:36:16 +08:00
|
|
|
|
|
|
|
if (S_ISREG(st->st_mode))
|
2019-07-02 06:42:23 +08:00
|
|
|
return st->st_size;
|
2022-09-30 21:36:16 +08:00
|
|
|
if (!S_ISBLK(st->st_mode))
|
2019-07-02 06:42:23 +08:00
|
|
|
return 0;
|
2022-09-30 21:36:16 +08:00
|
|
|
if (ioctl(fd, BLKGETSIZE64, &size) >= 0)
|
2019-07-02 06:42:23 +08:00
|
|
|
return size;
|
2022-09-30 21:36:16 +08:00
|
|
|
|
2019-07-02 06:42:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:36:44 +08:00
|
|
|
/*
|
|
|
|
* Read partition size using the low-level ioctl
|
|
|
|
*/
|
2021-04-30 02:13:49 +08:00
|
|
|
u64 device_get_partition_size_fd(int fd)
|
|
|
|
{
|
|
|
|
u64 result;
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, &result) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-03 14:03:42 +08:00
|
|
|
static u64 device_get_partition_size_sysfs(const char *dev)
|
2021-09-10 18:45:19 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char path[PATH_MAX] = {};
|
|
|
|
char sysfs[PATH_MAX] = {};
|
|
|
|
char sizebuf[128] = {};
|
2024-04-19 00:57:08 +08:00
|
|
|
const char *name = NULL;
|
2021-09-10 18:45:19 +08:00
|
|
|
int sysfd;
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
|
|
|
name = realpath(dev, path);
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
2024-04-19 00:57:08 +08:00
|
|
|
name = path_basename(path);
|
2021-09-10 18:45:19 +08:00
|
|
|
|
|
|
|
ret = path_cat3_out(sysfs, "/sys/class/block", name, "size");
|
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
sysfd = open(sysfs, O_RDONLY);
|
|
|
|
if (sysfd < 0)
|
|
|
|
return 0;
|
|
|
|
ret = sysfs_read_file(sysfd, sizebuf, sizeof(sizebuf));
|
|
|
|
if (ret < 0) {
|
|
|
|
close(sysfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
size = strtoull(sizebuf, NULL, 10);
|
|
|
|
if (size == ULLONG_MAX && errno == ERANGE) {
|
|
|
|
close(sysfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(sysfd);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:36:44 +08:00
|
|
|
u64 device_get_partition_size(const char *dev)
|
2019-07-02 06:42:23 +08:00
|
|
|
{
|
|
|
|
u64 result;
|
|
|
|
int fd = open(dev, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
2021-09-10 18:45:19 +08:00
|
|
|
return device_get_partition_size_sysfs(dev);
|
|
|
|
|
2019-07-02 06:42:23 +08:00
|
|
|
if (ioctl(fd, BLKGETSIZE64, &result) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-26 14:27:17 +08:00
|
|
|
/*
|
|
|
|
* Get a device request queue parameter from sysfs.
|
|
|
|
*/
|
2021-04-30 00:31:33 +08:00
|
|
|
int device_get_queue_param(const char *file, const char *param, char *buf, size_t len)
|
2021-04-26 14:27:17 +08:00
|
|
|
{
|
|
|
|
blkid_probe probe;
|
|
|
|
char wholedisk[PATH_MAX];
|
|
|
|
char sysfs_path[PATH_MAX];
|
|
|
|
dev_t devno;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
probe = blkid_new_probe_from_filename(file);
|
|
|
|
if (!probe)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Device number of this disk (possibly a partition) */
|
|
|
|
devno = blkid_probe_get_devno(probe);
|
|
|
|
if (!devno) {
|
|
|
|
blkid_free_probe(probe);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get whole disk name (not full path) for this devno */
|
|
|
|
ret = blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);
|
|
|
|
if (ret) {
|
|
|
|
blkid_free_probe(probe);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/%s",
|
|
|
|
wholedisk, param);
|
|
|
|
|
|
|
|
blkid_free_probe(probe);
|
|
|
|
|
|
|
|
fd = open(sysfs_path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = read(fd, buf, len);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2021-05-07 06:34:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read value of zone_unusable from sysfs for given block group type in flags
|
|
|
|
*/
|
|
|
|
u64 device_get_zone_unusable(int fd, u64 flags)
|
|
|
|
{
|
2023-12-01 08:58:56 +08:00
|
|
|
int ret;
|
|
|
|
u64 unusable;
|
2021-05-07 06:34:26 +08:00
|
|
|
|
|
|
|
/* Don't report it for a regular fs */
|
2023-12-01 08:58:56 +08:00
|
|
|
ret = sysfs_open_fsid_file(fd, "features/zoned");
|
|
|
|
if (ret < 0)
|
2021-05-07 06:34:26 +08:00
|
|
|
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
|
2023-12-01 08:58:56 +08:00
|
|
|
close(ret);
|
|
|
|
ret = -1;
|
2021-05-07 06:34:26 +08:00
|
|
|
|
|
|
|
if ((flags & BTRFS_BLOCK_GROUP_DATA) == BTRFS_BLOCK_GROUP_DATA)
|
2023-12-01 08:58:56 +08:00
|
|
|
ret = sysfs_read_fsid_file_u64(fd, "allocation/data/bytes_zone_unusable", &unusable);
|
2021-05-07 06:34:26 +08:00
|
|
|
else if ((flags & BTRFS_BLOCK_GROUP_METADATA) == BTRFS_BLOCK_GROUP_METADATA)
|
2023-12-01 08:58:56 +08:00
|
|
|
ret = sysfs_read_fsid_file_u64(fd, "allocation/metadata/bytes_zone_unusable", &unusable);
|
2021-05-07 06:34:26 +08:00
|
|
|
else if ((flags & BTRFS_BLOCK_GROUP_SYSTEM) == BTRFS_BLOCK_GROUP_SYSTEM)
|
2023-12-01 08:58:56 +08:00
|
|
|
ret = sysfs_read_fsid_file_u64(fd, "allocation/system/bytes_zone_unusable", &unusable);
|
2021-05-07 06:34:26 +08:00
|
|
|
|
2023-12-01 08:58:56 +08:00
|
|
|
if (ret < 0)
|
2021-05-07 06:34:26 +08:00
|
|
|
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
|
|
|
|
|
|
|
|
return unusable;
|
|
|
|
}
|
2021-06-25 04:38:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read information about zone size of the given device (short @name) from a
|
|
|
|
* given filesystem fd
|
|
|
|
*/
|
|
|
|
u64 device_get_zone_size(int fd, const char *name)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
int sysfs_fd;
|
|
|
|
u64 ret = 0;
|
|
|
|
|
|
|
|
sysfs_fd = sysfs_open_fsid_dir(fd, "devices");
|
|
|
|
if (sysfs_fd < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dir = fdopendir(sysfs_fd);
|
|
|
|
if (!dir) {
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
int queue_fd;
|
|
|
|
char queue[PATH_MAX];
|
|
|
|
char buf[128] = {0};
|
|
|
|
|
|
|
|
de = readdir(dir);
|
|
|
|
if (!de) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(name, de->d_name) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path_cat3_out(queue, "devices", de->d_name, "queue/chunk_sectors");
|
|
|
|
/* /sys/fs/btrfs/FSID/devices/NAME/queue/chunk_sectors */
|
|
|
|
queue_fd = sysfs_open_fsid_file(fd, queue);
|
|
|
|
if (queue_fd < 0) {
|
2023-12-09 05:50:57 +08:00
|
|
|
queue_fd = -1;
|
2021-06-25 04:38:59 +08:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysfs_read_file(queue_fd, buf, sizeof(buf));
|
|
|
|
ret = atoll(buf);
|
|
|
|
close(queue_fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
out:
|
|
|
|
close(sysfs_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-10-05 14:23:02 +08:00
|
|
|
|
2022-09-14 22:42:53 +08:00
|
|
|
int device_get_rotational(const char *file)
|
|
|
|
{
|
|
|
|
char rotational;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device_get_queue_param(file, "rotational", &rotational, 1);
|
|
|
|
if (ret < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (rotational == '0');
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:07:13 +08:00
|
|
|
int device_get_info(int fd, u64 devid, struct btrfs_ioctl_dev_info_args *di_args)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
di_args->devid = devid;
|
|
|
|
memset(&di_args->uuid, 0, sizeof(di_args->uuid));
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args);
|
|
|
|
|
|
|
|
return ret < 0 ? -errno : 0;
|
|
|
|
}
|
|
|
|
|
2023-05-09 19:48:38 +08:00
|
|
|
ssize_t btrfs_direct_pread(int fd, void *buf, size_t count, off_t offset)
|
2021-10-05 14:23:02 +08:00
|
|
|
{
|
|
|
|
int alignment;
|
|
|
|
size_t iosize;
|
|
|
|
void *bounce_buf = NULL;
|
|
|
|
struct stat stat_buf;
|
|
|
|
unsigned long req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fstat(fd, &stat_buf) == -1) {
|
|
|
|
error("fstat failed: %m");
|
2023-05-09 19:48:38 +08:00
|
|
|
return -errno;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((stat_buf.st_mode & S_IFMT) == S_IFBLK)
|
|
|
|
req = BLKSSZGET;
|
|
|
|
else
|
|
|
|
req = FIGETBSZ;
|
|
|
|
|
|
|
|
if (ioctl(fd, req, &alignment)) {
|
|
|
|
error("failed to get block size: %m");
|
2023-05-09 19:48:38 +08:00
|
|
|
return -errno;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:48:38 +08:00
|
|
|
if (IS_ALIGNED((size_t)buf, alignment) && IS_ALIGNED(count, alignment))
|
|
|
|
return pread(fd, buf, count, offset);
|
|
|
|
|
|
|
|
iosize = round_up(count, alignment);
|
|
|
|
|
|
|
|
ret = posix_memalign(&bounce_buf, alignment, iosize);
|
|
|
|
if (ret) {
|
|
|
|
error_msg(ERROR_MSG_MEMORY, "bounce buffer");
|
|
|
|
errno = ret;
|
|
|
|
return -ret;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:48:38 +08:00
|
|
|
ret = pread(fd, bounce_buf, iosize, offset);
|
|
|
|
if (ret >= count)
|
|
|
|
ret = count;
|
|
|
|
memcpy(buf, bounce_buf, count);
|
|
|
|
|
|
|
|
free(bounce_buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t btrfs_direct_pwrite(int fd, const void *buf, size_t count, off_t offset)
|
|
|
|
{
|
|
|
|
int alignment;
|
|
|
|
size_t iosize;
|
|
|
|
void *bounce_buf = NULL;
|
|
|
|
struct stat stat_buf;
|
|
|
|
unsigned long req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fstat(fd, &stat_buf) == -1) {
|
|
|
|
error("fstat failed: %m");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((stat_buf.st_mode & S_IFMT) == S_IFBLK)
|
|
|
|
req = BLKSSZGET;
|
|
|
|
else
|
|
|
|
req = FIGETBSZ;
|
|
|
|
|
|
|
|
if (ioctl(fd, req, &alignment)) {
|
|
|
|
error("failed to get block size: %m");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ALIGNED((size_t)buf, alignment) && IS_ALIGNED(count, alignment))
|
|
|
|
return pwrite(fd, buf, count, offset);
|
|
|
|
|
2021-10-05 14:23:02 +08:00
|
|
|
/* Cannot do anything if the write size is not aligned */
|
2023-05-09 19:48:38 +08:00
|
|
|
if (!IS_ALIGNED(count, alignment)) {
|
2021-10-05 14:23:02 +08:00
|
|
|
error("%zu is not aligned to %d", count, alignment);
|
2023-05-09 19:48:38 +08:00
|
|
|
return -EINVAL;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iosize = round_up(count, alignment);
|
|
|
|
|
|
|
|
ret = posix_memalign(&bounce_buf, alignment, iosize);
|
|
|
|
if (ret) {
|
2022-09-30 15:12:06 +08:00
|
|
|
error_msg(ERROR_MSG_MEMORY, "bounce buffer");
|
2021-10-05 14:23:02 +08:00
|
|
|
errno = ret;
|
2023-05-09 19:48:38 +08:00
|
|
|
return -ret;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:48:38 +08:00
|
|
|
UASSERT(iosize == count);
|
|
|
|
memcpy(bounce_buf, buf, count);
|
|
|
|
ret = pwrite(fd, bounce_buf, iosize, offset);
|
2021-10-05 14:23:02 +08:00
|
|
|
|
|
|
|
free(bounce_buf);
|
2023-05-09 19:48:38 +08:00
|
|
|
return ret;
|
2021-10-05 14:23:02 +08:00
|
|
|
}
|