1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
* 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>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/limits.h>
#ifdef BTRFS_ZONED
#include <linux/blkzoned.h>
#endif
#include <linux/fs.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <blkid/blkid.h>
#include "kernel-lib/sizes.h"
#include "kernel-shared/disk-io.h"
#include "kernel-shared/ctree.h"
#include "kernel-shared/zoned.h"
#include "kernel-shared/uapi/btrfs.h"
#include "kernel-shared/uapi/btrfs_tree.h"
#include "common/device-utils.h"
#include "common/sysfs-utils.h"
#include "common/path-utils.h"
#include "common/internal.h"
#include "common/messages.h"
#include "common/units.h"
#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;
}
/*
* Discard blocks in the given range in 1G chunks, the process is interruptible
*/
int device_discard_blocks(int fd, u64 start, u64 len)
{
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;
}
static void prepare_discard_device(const char *filename, int fd, u64 byte_count, unsigned opflags)
{
u64 cur = 0;
while (cur < byte_count) {
/* 1G granularity */
u64 chunk_size = (cur == 0) ? SZ_1M : min_t(u64, byte_count - cur, SZ_1G);
int ret;
ret = discard_range(fd, cur, chunk_size);
if (ret)
return;
/*
* The first range discarded successfully, meaning the device supports
* discard.
*/
if (opflags & PREP_DEVICE_VERBOSE && cur == 0)
printf("Performing full device TRIM %s (%s) ...\n",
filename, pretty_size(byte_count));
cur += chunk_size;
}
}
/*
* Write zeros to the given range [start, start + len)
*/
int device_zero_blocks(int fd, off_t start, size_t len, bool direct)
{
char *buf = malloc(len);
int ret = 0;
ssize_t written;
if (!buf)
return -ENOMEM;
memset(buf, 0, len);
written = btrfs_pwrite(fd, buf, len, start, direct);
if (written != len) {
error_msg(ERROR_MSG_WRITE, "zeroing range from %llu: %m",
(unsigned long long)start);
ret = -EIO;
}
free(buf);
return ret;
}
#define ZERO_DEV_BYTES SZ_2M
/*
* Zero blocks in the range from start but not after the given device size.
* (On SPARC the disk labels are preserved too.)
*/
static int zero_dev_clamped(int fd, struct btrfs_zoned_device_info *zinfo,
off_t start, ssize_t len, u64 dev_size)
{
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);
if (zinfo && zinfo->model == ZONED_HOST_MANAGED)
return zero_zone_blocks(fd, zinfo, start, end - start);
return device_zero_blocks(fd, start, end - start, false);
}
/*
* Find all magic signatures known to blkid and remove them
*/
static int btrfs_wipe_existing_sb(int fd, struct btrfs_zoned_device_info *zinfo)
{
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);
if (!zone_is_sequential(zinfo, offset)) {
const bool direct = zinfo && zinfo->model == ZONED_HOST_MANAGED;
memset(buf, 0, len);
ret = btrfs_pwrite(fd, buf, len, offset, direct);
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;
}
}
fsync(fd);
out:
blkid_free_probe(pr);
return ret;
}
/*
* Prepare a device before it's added to the filesystem. Optionally:
* - remove old superblocks
* - discard
* - reset zones
* - delete end of the device
*/
int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
u64 max_byte_count, unsigned opflags)
{
struct btrfs_zoned_device_info *zinfo = NULL;
u64 byte_count;
struct stat st;
int i, ret;
ret = fstat(fd, &st);
if (ret < 0) {
error("unable to stat %s: %m", file);
return 1;
}
ret = device_get_partition_size_fd_stat(fd, &st, &byte_count);
if (ret < 0) {
errno = -ret;
error("unable to determine size of %s: %m", file);
return 1;
}
if (max_byte_count)
byte_count = min(byte_count, max_byte_count);
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;
}
if (!zinfo->emulated) {
if (opflags & PREP_DEVICE_VERBOSE)
printf("Resetting device zones %s (%llu zones) ...\n",
file, byte_count / zinfo->zone_size);
/*
* 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.
*/
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);
}
goto err;
}
}
}
ret = zero_dev_clamped(fd, zinfo, 0, ZERO_DEV_BYTES, byte_count);
for (i = 0 ; !ret && i < BTRFS_SUPER_MIRROR_MAX; i++)
ret = zero_dev_clamped(fd, zinfo, btrfs_sb_offset(i),
BTRFS_SUPER_INFO_SIZE, byte_count);
if (!ret && (opflags & PREP_DEVICE_ZERO_END))
ret = zero_dev_clamped(fd, zinfo, byte_count - ZERO_DEV_BYTES,
ZERO_DEV_BYTES, byte_count);
if (ret < 0) {
errno = -ret;
error("failed to zero device '%s': %m", file);
goto err;
}
if (!(opflags & PREP_DEVICE_ZONED) && (opflags & PREP_DEVICE_DISCARD))
prepare_discard_device(file, fd, byte_count, opflags);
ret = btrfs_wipe_existing_sb(fd, zinfo);
if (ret < 0) {
error("cannot wipe superblocks on %s", file);
goto err;
}
free(zinfo);
*byte_count_ret = byte_count;
return 0;
err:
free(zinfo);
return 1;
}
int device_get_partition_size_fd_stat(int fd, const struct stat *st, u64 *size_ret)
{
if (S_ISREG(st->st_mode)) {
*size_ret = st->st_size;
return 0;
}
if (!S_ISBLK(st->st_mode))
return -EINVAL;
if (ioctl(fd, BLKGETSIZE64, size_ret) < 0)
return -errno;
return 0;
}
static int device_get_partition_size_sysfs(const char *dev, u64 *size_ret)
{
int ret;
char path[PATH_MAX] = {};
char sysfs[PATH_MAX] = {};
char sizebuf[128] = {};
const char *name = NULL;
int sysfd;
unsigned long long size = 0;
name = realpath(dev, path);
if (!name)
return -errno;
name = path_basename(path);
ret = path_cat3_out(sysfs, "/sys/class/block", name, "size");
if (ret < 0)
return ret;
sysfd = open(sysfs, O_RDONLY);
if (sysfd < 0)
return -errno;
ret = sysfs_read_file(sysfd, sizebuf, sizeof(sizebuf));
close(sysfd);
if (ret < 0)
return ret;
errno = 0;
size = strtoull(sizebuf, NULL, 10);
if (size == ULLONG_MAX && errno == ERANGE)
return -ERANGE;
/* Extra overflow check. */
if (size > ULLONG_MAX >> SECTOR_SHIFT)
return -ERANGE;
*size_ret = size << SECTOR_SHIFT;
return 0;
}
int device_get_partition_size(const char *dev, u64 *size_ret)
{
u64 result;
int fd = open(dev, O_RDONLY);
if (fd < 0)
return device_get_partition_size_sysfs(dev, size_ret);
if (ioctl(fd, BLKGETSIZE64, &result) < 0) {
close(fd);
return -errno;
}
close(fd);
*size_ret = result;
return 0;
}
/*
* Get a device request queue parameter from sysfs.
*/
int device_get_queue_param(const char *file, const char *param, char *buf, size_t len)
{
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;
}
/*
* Read value of zone_unusable from sysfs for given block group type in flags
*/
u64 device_get_zone_unusable(int fd, u64 flags)
{
int ret;
u64 unusable;
/* Don't report it for a regular fs */
ret = sysfs_open_fsid_file(fd, "features/zoned");
if (ret < 0)
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
close(ret);
ret = -1;
if ((flags & BTRFS_BLOCK_GROUP_DATA) == BTRFS_BLOCK_GROUP_DATA)
ret = sysfs_read_fsid_file_u64(fd, "allocation/data/bytes_zone_unusable", &unusable);
else if ((flags & BTRFS_BLOCK_GROUP_METADATA) == BTRFS_BLOCK_GROUP_METADATA)
ret = sysfs_read_fsid_file_u64(fd, "allocation/metadata/bytes_zone_unusable", &unusable);
else if ((flags & BTRFS_BLOCK_GROUP_SYSTEM) == BTRFS_BLOCK_GROUP_SYSTEM)
ret = sysfs_read_fsid_file_u64(fd, "allocation/system/bytes_zone_unusable", &unusable);
if (ret < 0)
return DEVICE_ZONE_UNUSABLE_UNKNOWN;
return unusable;
}
/*
* 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) {
queue_fd = -1;
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;
}
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');
}
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;
}
ssize_t btrfs_direct_pread(int fd, 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 pread(fd, buf, count, offset);
iosize = round_up(count, alignment);
ret = posix_memalign(&bounce_buf, alignment, iosize);
if (ret) {
error_mem("bounce buffer");
errno = ret;
return -ret;
}
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);
/* Cannot do anything if the write size is not aligned */
if (!IS_ALIGNED(count, alignment)) {
error("%zu is not aligned to %d", count, alignment);
return -EINVAL;
}
iosize = round_up(count, alignment);
ret = posix_memalign(&bounce_buf, alignment, iosize);
if (ret) {
error_mem("bounce buffer");
errno = ret;
return -ret;
}
UASSERT(iosize == count);
memcpy(bounce_buf, buf, count);
ret = pwrite(fd, bounce_buf, iosize, offset);
free(bounce_buf);
return ret;
}
/* Sort devices by devid, ascending */
int cmp_device_id(void *priv, struct list_head *a, struct list_head *b)
{
const struct btrfs_device *da = list_entry(a, struct btrfs_device,
dev_list);
const struct btrfs_device *db = list_entry(b, struct btrfs_device,
dev_list);
return da->devid < db->devid ? -1 :
da->devid > db->devid ? 1 : 0;
}
|