| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/btrfs.h> |
| 4 |
#include <linux/fs.h> |
| 5 |
#include <linux/magic.h> |
| 6 |
#include <linux/xfs.h> |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <sys/vfs.h> |
| 9 |
|
| 10 |
#include "resize-fs.h" |
| 11 |
#include "stat-util.h" |
| 12 |
#include "stdio-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
|
| 15 |
int resize_fs(int fd, uint64_t sz, uint64_t *ret_size) { |
| 16 |
struct statfs sfs; |
| 17 |
|
| 18 |
assert(fd >= 0); |
| 19 |
|
| 20 |
/* Rounds down to next block size */ |
| 21 |
|
| 22 |
if (sz <= 0 || sz == UINT64_MAX) |
| 23 |
return -ERANGE; |
| 24 |
|
| 25 |
if (fstatfs(fd, &sfs) < 0) |
| 26 |
return -errno; |
| 27 |
|
| 28 |
if (is_fs_type(&sfs, EXT4_SUPER_MAGIC)) { |
| 29 |
uint64_t u; |
| 30 |
|
| 31 |
if (sz < EXT4_MINIMAL_SIZE) |
| 32 |
return -ERANGE; |
| 33 |
|
| 34 |
u = sz / sfs.f_bsize; |
| 35 |
|
| 36 |
if (ioctl(fd, EXT4_IOC_RESIZE_FS, &u) < 0) |
| 37 |
return -errno; |
| 38 |
|
| 39 |
if (ret_size) |
| 40 |
*ret_size = u * sfs.f_bsize; |
| 41 |
|
| 42 |
} else if (is_fs_type(&sfs, BTRFS_SUPER_MAGIC)) { |
| 43 |
struct btrfs_ioctl_vol_args args = {}; |
| 44 |
|
| 45 |
/* 256M is the minimize size enforced by the btrfs kernel code when resizing (which is |
| 46 |
* strange btw, as mkfs.btrfs is fine creating file systems > 109M). It will return EINVAL in |
| 47 |
* that case, let's catch this error beforehand though, and report a more explanatory |
| 48 |
* error. */ |
| 49 |
|
| 50 |
if (sz < BTRFS_MINIMAL_SIZE) |
| 51 |
return -ERANGE; |
| 52 |
|
| 53 |
sz -= sz % sfs.f_bsize; |
| 54 |
|
| 55 |
xsprintf(args.name, "%" PRIu64, sz); |
| 56 |
|
| 57 |
if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0) |
| 58 |
return -errno; |
| 59 |
|
| 60 |
if (ret_size) |
| 61 |
*ret_size = sz; |
| 62 |
|
| 63 |
} else if (is_fs_type(&sfs, XFS_SUPER_MAGIC)) { |
| 64 |
xfs_fsop_geom_t geo; |
| 65 |
xfs_growfs_data_t d; |
| 66 |
|
| 67 |
if (sz < XFS_MINIMAL_SIZE) |
| 68 |
return -ERANGE; |
| 69 |
|
| 70 |
if (ioctl(fd, XFS_IOC_FSGEOMETRY, &geo) < 0) |
| 71 |
return -errno; |
| 72 |
|
| 73 |
d = (xfs_growfs_data_t) { |
| 74 |
.imaxpct = geo.imaxpct, |
| 75 |
.newblocks = sz / geo.blocksize, |
| 76 |
}; |
| 77 |
|
| 78 |
if (ioctl(fd, XFS_IOC_FSGROWFSDATA, &d) < 0) |
| 79 |
return -errno; |
| 80 |
|
| 81 |
if (ret_size) |
| 82 |
*ret_size = d.newblocks * geo.blocksize; |
| 83 |
|
| 84 |
} else |
| 85 |
return -EOPNOTSUPP; |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
uint64_t minimal_size_by_fs_magic(statfs_f_type_t magic) { |
| 91 |
|
| 92 |
switch (magic) { |
| 93 |
|
| 94 |
case (statfs_f_type_t) EXT4_SUPER_MAGIC: |
| 95 |
return EXT4_MINIMAL_SIZE; |
| 96 |
|
| 97 |
case (statfs_f_type_t) XFS_SUPER_MAGIC: |
| 98 |
return XFS_MINIMAL_SIZE; |
| 99 |
|
| 100 |
case (statfs_f_type_t) BTRFS_SUPER_MAGIC: |
| 101 |
return BTRFS_MINIMAL_SIZE; |
| 102 |
|
| 103 |
default: |
| 104 |
return UINT64_MAX; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
uint64_t minimal_size_by_fs_name(const char *name) { |
| 109 |
|
| 110 |
if (streq_ptr(name, "ext4")) |
| 111 |
return EXT4_MINIMAL_SIZE; |
| 112 |
|
| 113 |
if (streq_ptr(name, "xfs")) |
| 114 |
return XFS_MINIMAL_SIZE; |
| 115 |
|
| 116 |
if (streq_ptr(name, "btrfs")) |
| 117 |
return BTRFS_MINIMAL_SIZE; |
| 118 |
|
| 119 |
return UINT64_MAX; |
| 120 |
} |
| 121 |
|
| 122 |
/* Returns true for the only fs that can online shrink *and* grow */ |
| 123 |
bool fs_can_online_shrink_and_grow(statfs_f_type_t magic) { |
| 124 |
return magic == (statfs_f_type_t) BTRFS_SUPER_MAGIC; |
| 125 |
} |