Refactor user util functions for device size

Split the existing device_size() into get_device_size() and
limit_device_size().  An upcoming command wants to get the device size
without applying limiting policy.

Signed-off-by: Zach Brown <zab@versity.com>
This commit is contained in:
Zach Brown
2023-03-14 10:58:52 -07:00
parent d6863d6832
commit b76e22ffcf
3 changed files with 24 additions and 15 deletions

View File

@@ -12,13 +12,10 @@
#include "sparse.h" #include "sparse.h"
#include "dev.h" #include "dev.h"
int device_size(char *path, int fd, int get_device_size(char *path, int fd, u64 *size_ret)
u64 min_size, u64 max_size, bool allow_small_size,
char *use_type, u64 *size_ret)
{ {
struct stat st; struct stat st;
u64 size; u64 size;
char *target_type;
int ret; int ret;
if (fstat(fd, &st)) { if (fstat(fd, &st)) {
@@ -30,7 +27,6 @@ int device_size(char *path, int fd,
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
size = st.st_size; size = st.st_size;
target_type = "file";
} else if (S_ISBLK(st.st_mode)) { } else if (S_ISBLK(st.st_mode)) {
if (ioctl(fd, BLKGETSIZE64, &size)) { if (ioctl(fd, BLKGETSIZE64, &size)) {
ret = -errno; ret = -errno;
@@ -38,13 +34,26 @@ int device_size(char *path, int fd,
path, strerror(errno), errno); path, strerror(errno), errno);
return ret; return ret;
} }
target_type = "device";
} else { } else {
fprintf(stderr, "path isn't regular or device file '%s'\n", fprintf(stderr, "path isn't regular or device file '%s'\n",
path); path);
return -EINVAL; return -EINVAL;
} }
*size_ret = size;
return 0;
}
int limit_device_size(char *path, int fd, u64 min_size, u64 max_size, bool allow_small_size,
char *use_type, u64 *size_ret)
{
u64 size;
int ret;
ret = get_device_size(path, fd, &size);
if (ret < 0)
return ret;
if (max_size) { if (max_size) {
if (size > max_size) { if (size > max_size) {
printf("Limiting use of "BASE_SIZE_FMT printf("Limiting use of "BASE_SIZE_FMT
@@ -64,9 +73,9 @@ int device_size(char *path, int fd,
if (size < min_size) { if (size < min_size) {
fprintf(stderr, fprintf(stderr,
BASE_SIZE_FMT" %s too small for min " BASE_SIZE_FMT" too small for min "
BASE_SIZE_FMT" %s device%s\n", BASE_SIZE_FMT" %s device%s\n",
BASE_SIZE_ARGS(size), target_type, BASE_SIZE_ARGS(size),
BASE_SIZE_ARGS(min_size), use_type, BASE_SIZE_ARGS(min_size), use_type,
allow_small_size ? ", allowing with -A" : ""); allow_small_size ? ", allowing with -A" : "");

View File

@@ -9,9 +9,9 @@
#define SIZE_FMT "%llu (%.2f %s)" #define SIZE_FMT "%llu (%.2f %s)"
#define SIZE_ARGS(nr, sz) (nr), size_flt(nr, sz), size_str(nr, sz) #define SIZE_ARGS(nr, sz) (nr), size_flt(nr, sz), size_str(nr, sz)
int device_size(char *path, int fd, int get_device_size(char *path, int fd, u64 *size_ret);
u64 min_size, u64 max_size, bool allow_small_size, int limit_device_size(char *path, int fd, u64 min_size, u64 max_size, bool allow_small_size,
char *use_type, u64 *size_ret); char *use_type, u64 *size_ret);
float size_flt(u64 nr, unsigned size); float size_flt(u64 nr, unsigned size);
char *size_str(u64 nr, unsigned size); char *size_str(u64 nr, unsigned size);
int flush_device(int fd); int flush_device(int fd);

View File

@@ -206,14 +206,14 @@ static int do_mkfs(struct mkfs_args *args)
} }
/* minumum meta device size to make reserved blocks reasonably large */ /* minumum meta device size to make reserved blocks reasonably large */
ret = device_size(args->meta_device, meta_fd, 64ULL * (1024 * 1024 * 1024), ret = limit_device_size(args->meta_device, meta_fd, 64ULL * (1024 * 1024 * 1024),
args->max_meta_size, args->allow_small_size, "meta", &meta_size); args->max_meta_size, args->allow_small_size, "meta", &meta_size);
if (ret) if (ret)
goto out; goto out;
/* .. then arbitrarily the same minimum data device size */ /* .. then arbitrarily the same minimum data device size */
ret = device_size(args->data_device, data_fd, 64ULL * (1024 * 1024 * 1024), ret = limit_device_size(args->data_device, data_fd, 64ULL * (1024 * 1024 * 1024),
args->max_data_size, args->allow_small_size, "data", &data_size); args->max_data_size, args->allow_small_size, "data", &data_size);
if (ret) if (ret)
goto out; goto out;