2019-07-01 22:42:23 +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 <sys/ioctl.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <sys/stat.h>
|
2021-06-24 20:38:59 +00:00
|
|
|
#include <sys/types.h>
|
2022-09-14 15:06:52 +00:00
|
|
|
#include <linux/limits.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#ifdef BTRFS_ZONED
|
|
|
|
#include <linux/blkzoned.h>
|
|
|
|
#endif
|
2022-09-14 15:06:52 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <limits.h>
|
2019-07-01 22:42:23 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2021-06-24 20:38:59 +00:00
|
|
|
#include <dirent.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <errno.h>
|
2019-07-01 22:42:23 +00:00
|
|
|
#include <blkid/blkid.h>
|
|
|
|
#include "kernel-lib/sizes.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2021-04-26 06:27:33 +00:00
|
|
|
#include "kernel-shared/zoned.h"
|
2019-07-01 22:42:23 +00:00
|
|
|
#include "common/device-utils.h"
|
2021-06-24 20:38:59 +00:00
|
|
|
#include "common/path-utils.h"
|
2019-07-01 22:42:23 +00:00
|
|
|
#include "common/internal.h"
|
|
|
|
#include "common/messages.h"
|
|
|
|
#include "common/utils.h"
|
2021-04-29 21:43:24 +00:00
|
|
|
#include "common/units.h"
|
2019-07-01 22:42:23 +00: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-27 16:22:38 +00: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-13 22:43:20 +00:00
|
|
|
if (atoi(buf) == 0) {
|
|
|
|
pr_verbose(3, "%s: discard_granularity %s", device, buf);
|
2021-09-27 16:22:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:42:23 +00:00
|
|
|
/*
|
|
|
|
* Discard blocks in the given range in 1G chunks, the process is interruptible
|
|
|
|
*/
|
2021-04-29 16:34:05 +00:00
|
|
|
int device_discard_blocks(int fd, u64 start, u64 len)
|
2019-07-01 22:42:23 +00: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-29 16:36:44 +00:00
|
|
|
/*
|
|
|
|
* Write zeros to the given range [start, start + len)
|
|
|
|
*/
|
2021-10-05 06:23:02 +00:00
|
|
|
int device_zero_blocks(int fd, off_t start, size_t len, bool direct)
|
2019-07-01 22:42:23 +00:00
|
|
|
{
|
|
|
|
char *buf = malloc(len);
|
|
|
|
int ret = 0;
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
memset(buf, 0, len);
|
2021-10-05 06:23:02 +00:00
|
|
|
written = btrfs_pwrite(fd, buf, len, start, direct);
|
2019-07-01 22:42:23 +00:00
|
|
|
if (written != len)
|
|
|
|
ret = -EIO;
|
|
|
|
free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ZERO_DEV_BYTES SZ_2M
|
|
|
|
|
2021-04-29 16:56:00 +00: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 06:27:34 +00: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-01 22:42:23 +00: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 06:27:34 +00:00
|
|
|
if (zinfo && zinfo->model == ZONED_HOST_MANAGED)
|
|
|
|
return zero_zone_blocks(fd, zinfo, start, end - start);
|
|
|
|
|
2021-10-05 06:23:02 +00:00
|
|
|
return device_zero_blocks(fd, start, end - start, false);
|
2019-07-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 16:56:00 +00:00
|
|
|
/*
|
|
|
|
* Find all magic signatures known to blkid and remove them
|
|
|
|
*/
|
2021-04-26 06:27:35 +00:00
|
|
|
static int btrfs_wipe_existing_sb(int fd, struct btrfs_zoned_device_info *zinfo)
|
2019-07-01 22:42:23 +00: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 06:27:35 +00:00
|
|
|
if (!zone_is_sequential(zinfo, offset)) {
|
2021-10-05 06:23:02 +00:00
|
|
|
const bool direct = zinfo && zinfo->model == ZONED_HOST_MANAGED;
|
|
|
|
|
2021-04-26 06:27:35 +00:00
|
|
|
memset(buf, 0, len);
|
2021-10-05 06:23:02 +00:00
|
|
|
ret = btrfs_pwrite(fd, buf, len, offset, direct);
|
2021-04-26 06:27:35 +00: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-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
fsync(fd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
blkid_free_probe(pr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:56:00 +00:00
|
|
|
/*
|
|
|
|
* Prepare a device before it's added to the filesystem. Optionally:
|
|
|
|
* - remove old superblocks
|
|
|
|
* - discard
|
|
|
|
* - reset zones
|
|
|
|
* - delete end of the device
|
|
|
|
*/
|
2019-07-01 22:42:23 +00:00
|
|
|
int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
|
|
|
|
u64 max_block_count, unsigned opflags)
|
|
|
|
{
|
2021-04-26 06:27:33 +00:00
|
|
|
struct btrfs_zoned_device_info *zinfo = NULL;
|
2019-07-01 22:42:23 +00:00
|
|
|
u64 block_count;
|
|
|
|
struct stat st;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
ret = fstat(fd, &st);
|
|
|
|
if (ret < 0) {
|
|
|
|
error("unable to stat %s: %m", file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:36:16 +00:00
|
|
|
block_count = device_get_partition_size_fd_stat(fd, &st);
|
2019-07-01 22:42:23 +00:00
|
|
|
if (block_count == 0) {
|
|
|
|
error("unable to determine size of %s", file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (max_block_count)
|
|
|
|
block_count = min(block_count, max_block_count);
|
|
|
|
|
2021-04-26 06:27:33 +00: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 07:53:43 +00:00
|
|
|
|
|
|
|
if (!zinfo->emulated) {
|
|
|
|
if (opflags & PREP_DEVICE_VERBOSE)
|
|
|
|
printf("Resetting device zones %s (%u zones) ...\n",
|
|
|
|
file, zinfo->nr_zones);
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (btrfs_reset_all_zones(fd, zinfo)) {
|
|
|
|
error("zoned: failed to reset device '%s' zones: %m",
|
|
|
|
file);
|
|
|
|
goto err;
|
|
|
|
}
|
2021-04-26 06:27:33 +00:00
|
|
|
}
|
|
|
|
} else if (opflags & PREP_DEVICE_DISCARD) {
|
2019-07-01 22:42:23 +00:00
|
|
|
/*
|
|
|
|
* We intentionally ignore errors from the discard ioctl. It
|
|
|
|
* is not necessary for the mkfs functionality but just an
|
|
|
|
* optimization.
|
|
|
|
*/
|
2021-09-27 16:22:38 +00:00
|
|
|
if (discard_supported(file)) {
|
2019-07-01 22:42:23 +00:00
|
|
|
if (opflags & PREP_DEVICE_VERBOSE)
|
|
|
|
printf("Performing full device TRIM %s (%s) ...\n",
|
|
|
|
file, pretty_size(block_count));
|
2021-04-29 16:34:05 +00:00
|
|
|
device_discard_blocks(fd, 0, block_count);
|
2019-07-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 06:27:34 +00:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, 0, ZERO_DEV_BYTES, block_count);
|
2019-07-01 22:42:23 +00:00
|
|
|
for (i = 0 ; !ret && i < BTRFS_SUPER_MIRROR_MAX; i++)
|
2021-04-26 06:27:34 +00:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, btrfs_sb_offset(i),
|
2019-07-01 22:42:23 +00:00
|
|
|
BTRFS_SUPER_INFO_SIZE, block_count);
|
|
|
|
if (!ret && (opflags & PREP_DEVICE_ZERO_END))
|
2021-04-26 06:27:34 +00:00
|
|
|
ret = zero_dev_clamped(fd, zinfo, block_count - ZERO_DEV_BYTES,
|
2019-07-01 22:42:23 +00:00
|
|
|
ZERO_DEV_BYTES, block_count);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
errno = -ret;
|
|
|
|
error("failed to zero device '%s': %m", file);
|
2021-04-26 06:27:33 +00:00
|
|
|
goto err;
|
2019-07-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 06:27:35 +00:00
|
|
|
ret = btrfs_wipe_existing_sb(fd, zinfo);
|
2019-07-01 22:42:23 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
error("cannot wipe superblocks on %s", file);
|
2021-04-26 06:27:33 +00:00
|
|
|
goto err;
|
2019-07-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 06:27:33 +00:00
|
|
|
free(zinfo);
|
2019-07-01 22:42:23 +00:00
|
|
|
*block_count_ret = block_count;
|
|
|
|
return 0;
|
2021-04-26 06:27:33 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
free(zinfo);
|
|
|
|
return 1;
|
2019-07-01 22:42:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:36:16 +00:00
|
|
|
u64 device_get_partition_size_fd_stat(int fd, const struct stat *st)
|
2019-07-01 22:42:23 +00:00
|
|
|
{
|
|
|
|
u64 size;
|
2022-09-30 13:36:16 +00:00
|
|
|
|
|
|
|
if (S_ISREG(st->st_mode))
|
2019-07-01 22:42:23 +00:00
|
|
|
return st->st_size;
|
2022-09-30 13:36:16 +00:00
|
|
|
if (!S_ISBLK(st->st_mode))
|
2019-07-01 22:42:23 +00:00
|
|
|
return 0;
|
2022-09-30 13:36:16 +00:00
|
|
|
if (ioctl(fd, BLKGETSIZE64, &size) >= 0)
|
2019-07-01 22:42:23 +00:00
|
|
|
return size;
|
2022-09-30 13:36:16 +00:00
|
|
|
|
2019-07-01 22:42:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:36:44 +00:00
|
|
|
/*
|
|
|
|
* Read partition size using the low-level ioctl
|
|
|
|
*/
|
2021-04-29 18:13:49 +00:00
|
|
|
u64 device_get_partition_size_fd(int fd)
|
|
|
|
{
|
|
|
|
u64 result;
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, &result) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-03 06:03:42 +00:00
|
|
|
static u64 device_get_partition_size_sysfs(const char *dev)
|
2021-09-10 10:45:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char path[PATH_MAX] = {};
|
|
|
|
char sysfs[PATH_MAX] = {};
|
|
|
|
char sizebuf[128] = {};
|
|
|
|
char *name = NULL;
|
|
|
|
int sysfd;
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
|
|
|
name = realpath(dev, path);
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
name = basename(path);
|
|
|
|
|
|
|
|
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-29 16:36:44 +00:00
|
|
|
u64 device_get_partition_size(const char *dev)
|
2019-07-01 22:42:23 +00:00
|
|
|
{
|
|
|
|
u64 result;
|
|
|
|
int fd = open(dev, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
2021-09-10 10:45:19 +00:00
|
|
|
return device_get_partition_size_sysfs(dev);
|
|
|
|
|
2019-07-01 22:42:23 +00:00
|
|
|
if (ioctl(fd, BLKGETSIZE64, &result) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-26 06:27:17 +00:00
|
|
|
/*
|
|
|
|
* Get a device request queue parameter from sysfs.
|
|
|
|
*/
|
2021-04-29 16:31:33 +00:00
|
|
|
int device_get_queue_param(const char *file, const char *param, char *buf, size_t len)
|
2021-04-26 06:27:17 +00: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-06 22:34:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read value of zone_unusable from sysfs for given block group type in flags
|
|
|
|
*/
|
|
|
|
u64 device_get_zone_unusable(int fd, u64 flags)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
int sys_fd;
|
|
|
|
u64 unusable = DEVICE_ZONE_UNUSABLE_UNKNOWN;
|
|
|
|
|
|
|
|
/* Don't report it for a regular fs */
|
|
|
|
sys_fd = sysfs_open_fsid_file(fd, "features/zoned");
|
|
|
|
if (sys_fd < 0)
|
|
|
|
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
|
|
|
|
close(sys_fd);
|
|
|
|
sys_fd = -1;
|
|
|
|
|
|
|
|
if ((flags & BTRFS_BLOCK_GROUP_DATA) == BTRFS_BLOCK_GROUP_DATA)
|
|
|
|
sys_fd = sysfs_open_fsid_file(fd, "allocation/data/bytes_zone_unusable");
|
|
|
|
else if ((flags & BTRFS_BLOCK_GROUP_METADATA) == BTRFS_BLOCK_GROUP_METADATA)
|
|
|
|
sys_fd = sysfs_open_fsid_file(fd, "allocation/metadata/bytes_zone_unusable");
|
|
|
|
else if ((flags & BTRFS_BLOCK_GROUP_SYSTEM) == BTRFS_BLOCK_GROUP_SYSTEM)
|
|
|
|
sys_fd = sysfs_open_fsid_file(fd, "allocation/system/bytes_zone_unusable");
|
|
|
|
|
|
|
|
if (sys_fd < 0)
|
|
|
|
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
|
|
|
|
sysfs_read_file(sys_fd, buf, sizeof(buf));
|
|
|
|
unusable = strtoull(buf, NULL, 10);
|
|
|
|
close(sys_fd);
|
|
|
|
|
|
|
|
return unusable;
|
|
|
|
}
|
2021-06-24 20:38:59 +00: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) {
|
|
|
|
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 06:23:02 +00:00
|
|
|
|
2022-09-14 14:42:53 +00: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-05-09 11:48:38 +00:00
|
|
|
ssize_t btrfs_direct_pread(int fd, void *buf, size_t count, off_t offset)
|
2021-10-05 06:23:02 +00: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 11:48:38 +00:00
|
|
|
return -errno;
|
2021-10-05 06:23:02 +00: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 11:48:38 +00:00
|
|
|
return -errno;
|
2021-10-05 06:23:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 11:48:38 +00: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 06:23:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 11:48:38 +00: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 06:23:02 +00:00
|
|
|
/* Cannot do anything if the write size is not aligned */
|
2023-05-09 11:48:38 +00:00
|
|
|
if (!IS_ALIGNED(count, alignment)) {
|
2021-10-05 06:23:02 +00:00
|
|
|
error("%zu is not aligned to %d", count, alignment);
|
2023-05-09 11:48:38 +00:00
|
|
|
return -EINVAL;
|
2021-10-05 06:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iosize = round_up(count, alignment);
|
|
|
|
|
|
|
|
ret = posix_memalign(&bounce_buf, alignment, iosize);
|
|
|
|
if (ret) {
|
2022-09-30 07:12:06 +00:00
|
|
|
error_msg(ERROR_MSG_MEMORY, "bounce buffer");
|
2021-10-05 06:23:02 +00:00
|
|
|
errno = ret;
|
2023-05-09 11:48:38 +00:00
|
|
|
return -ret;
|
2021-10-05 06:23:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 11:48:38 +00:00
|
|
|
UASSERT(iosize == count);
|
|
|
|
memcpy(bounce_buf, buf, count);
|
|
|
|
ret = pwrite(fd, bounce_buf, iosize, offset);
|
2021-10-05 06:23:02 +00:00
|
|
|
|
|
|
|
free(bounce_buf);
|
2023-05-09 11:48:38 +00:00
|
|
|
return ret;
|
2021-10-05 06:23:02 +00:00
|
|
|
}
|