}
int run_mark_dirty_by_path(const char *path, bool b) {
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
assert(path);
errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0)
- return errno > 0 ? -errno : -ENOMEM;
+ return errno_or_else(ENOMEM);
(void) blkid_probe_enable_superblocks(b, 1);
(void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_UUID);
errno = 0;
r = blkid_do_safeprobe(b);
- if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */
+ if (r == _BLKID_SAFEPROBE_ERROR)
+ return errno_or_else(EIO);
+ if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND))
return -ENOPKG;
- if (r != 0)
- return errno > 0 ? -errno : -EIO;
+
+ assert(r == _BLKID_SAFEPROBE_FOUND);
(void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
if (!fstype)
}
static int probe_file_system_by_path(const char *path, char **ret_fstype, sd_id128_t *ret_uuid) {
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (fd < 0)
}
static int block_get_size_by_path(const char *path, uint64_t *ret) {
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (fd < 0)
errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0)
- return errno > 0 ? -errno : -ENOMEM;
+ return errno_or_else(ENOMEM);
(void) blkid_probe_enable_superblocks(b, 1);
(void) blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
errno = 0;
r = blkid_do_safeprobe(b);
- if (IN_SET(r, -2, 1)) /* nothing found or ambiguous result */
+ if (r == _BLKID_SAFEPROBE_ERROR)
+ return errno_or_else(EIO);
+ if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND))
return -ENOPKG;
- if (r != 0)
- return errno > 0 ? -errno : -EIO;
+
+ assert(r == _BLKID_SAFEPROBE_FOUND);
(void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
if (streq_ptr(fstype, "crypto_LUKS")) {
errno = 0;
pl = blkid_probe_get_partitions(b);
if (!pl)
- return errno > 0 ? -errno : -ENOMEM;
+ return errno_or_else(ENOMEM);
errno = 0;
n = blkid_partlist_numof_partitions(pl);
if (n < 0)
- return errno > 0 ? -errno : -EIO;
+ return errno_or_else(EIO);
for (int i = 0; i < n; i++) {
- blkid_partition pp;
sd_id128_t id = SD_ID128_NULL;
- const char *sid;
+ blkid_partition pp;
errno = 0;
pp = blkid_partlist_get_partition(pl, i);
if (!pp)
- return errno > 0 ? -errno : -EIO;
+ return errno_or_else(EIO);
if (sd_id128_string_equal(blkid_partition_get_type_string(pp), SD_GPT_USER_HOME) <= 0)
continue;
if (!streq_ptr(blkid_partition_get_name(pp), label))
continue;
- sid = blkid_partition_get_uuid(pp);
- if (sid) {
- r = sd_id128_from_string(sid, &id);
- if (r < 0)
- log_debug_errno(r, "Couldn't parse partition UUID %s, weird: %m", sid);
- if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid))
- continue;
- }
+ r = blkid_partition_get_uuid_id128(pp, &id);
+ if (r < 0)
+ log_debug_errno(r, "Failed to read partition UUID, ignoring: %m");
+ else if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid))
+ continue;
if (found)
return -ENOPKG;
}
int run_fallocate_by_path(const char *backing_path) {
- _cleanup_close_ int backing_fd = -1;
+ _cleanup_close_ int backing_fd = -EBADF;
backing_fd = open(backing_path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (backing_fd < 0)
const char *force_image_path,
struct stat *ret_stat) {
- _cleanup_close_ int image_fd = -1;
+ _cleanup_close_ int image_fd = -EBADF;
struct stat st;
const char *ip;
int r;
assert(buffer);
assert(hr);
+ bool benchmark = user_record_luks_pbkdf_force_iterations(hr) == UINT64_MAX;
+
*buffer = (struct crypt_pbkdf_type) {
.hash = user_record_luks_pbkdf_hash_algorithm(hr),
.type = user_record_luks_pbkdf_type(hr),
- .time_ms = user_record_luks_pbkdf_time_cost_usec(hr) / USEC_PER_MSEC,
+ .time_ms = benchmark ? user_record_luks_pbkdf_time_cost_usec(hr) / USEC_PER_MSEC : 0,
+ .iterations = benchmark ? 0 : user_record_luks_pbkdf_force_iterations(hr),
.max_memory_kb = user_record_luks_pbkdf_memory_cost(hr) / 1024,
.parallel_threads = user_record_luks_pbkdf_parallel_threads(hr),
+ .flags = benchmark ? 0 : CRYPT_PBKDF_NO_BENCHMARK,
};
return buffer;
_cleanup_(fdisk_unref_partitionp) struct fdisk_partition *p = NULL, *q = NULL;
_cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
- _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL;
+ _cleanup_free_ char *disk_uuid_as_string = NULL;
uint64_t offset, size, first_lba, start, last_lba, end;
sd_id128_t disk_uuid;
int r;
if (r < 0)
return log_error_errno(r, "Failed to initialize partition type: %m");
- c = fdisk_new_context();
- if (!c)
- return log_oom();
-
- if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
- return log_oom();
-
- r = fdisk_assign_device(c, path, 0);
+ r = fdisk_new_context_fd(fd, /* read_only= */ false, &c);
if (r < 0)
return log_error_errno(r, "Failed to open device: %m");
}
static int wait_for_devlink(const char *path) {
- _cleanup_close_ int inotify_fd = -1;
+ _cleanup_close_ int inotify_fd = -EBADF;
usec_t until;
int r;
if (w >= until)
return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT), "Device link %s still hasn't shown up, giving up.", path);
- r = fd_wait_for_event(inotify_fd, POLLIN, usec_sub_unsigned(until, w));
- if (r < 0)
+ r = fd_wait_for_event(inotify_fd, POLLIN, until - w);
+ if (r < 0) {
+ if (ERRNO_IS_TRANSIENT(r))
+ continue;
return log_error_errno(r, "Failed to watch inotify: %m");
+ }
(void) flush_fd(inotify_fd);
}
return !trunc; /* Return == 0 if we managed to truncate, > 0 if we managed to allocate */
}
+static int mkfs_options_for_fstype(const char *fstype, char ***ret) {
+ _cleanup_(strv_freep) char **l = NULL;
+ const char *e;
+ char *n;
+
+ assert(fstype);
+
+ n = strjoina("SYSTEMD_HOME_MKFS_OPTIONS_", fstype);
+ e = getenv(ascii_strupper(n));
+ if (e) {
+ l = strv_split(e, NULL);
+ if (!l)
+ return -ENOMEM;
+ }
+
+ *ret = TAKE_PTR(l);
+ return 0;
+}
+
int home_create_luks(
UserRecord *h,
HomeSetup *setup,
host_size = 0, partition_offset = 0, partition_size = 0; /* Unnecessary initialization to appease gcc */
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid;
- _cleanup_close_ int mount_fd = -1;
+ _cleanup_close_ int mount_fd = -EBADF;
const char *fstype, *ip;
struct statfs sfs;
int r;
+ _cleanup_strv_free_ char **extra_mkfs_options = NULL;
assert(h);
assert(h->storage < 0 || h->storage == USER_LUKS);
log_info("Setting up LUKS device %s completed.", setup->dm_node);
- r = make_filesystem(setup->dm_node, fstype, user_record_user_name_and_realm(h), NULL, fs_uuid, user_record_luks_discard(h));
+ r = mkfs_options_for_fstype(fstype, &extra_mkfs_options);
+ if (r < 0)
+ return log_error_errno(r, "Failed to determine mkfs command line options for '%s': %m", fstype);
+ r = make_filesystem(setup->dm_node, fstype, user_record_user_name_and_realm(h), NULL, fs_uuid, user_record_luks_discard(h), extra_mkfs_options);
if (r < 0)
return r;
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
_cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
- _cleanup_free_ char *path = NULL, *disk_uuid_as_string = NULL;
+ _cleanup_free_ char *disk_uuid_as_string = NULL;
struct fdisk_partition *found = NULL;
sd_id128_t disk_uuid;
size_t n_partitions;
return 0;
}
- c = fdisk_new_context();
- if (!c)
- return log_oom();
-
- if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
- return log_oom();
-
- r = fdisk_assign_device(c, path, 0);
+ r = fdisk_new_context_fd(fd, /* read_only= */ false, &c);
if (r < 0)
return log_error_errno(r, "Failed to open device: %m");
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
_cleanup_free_ void *two_zero_lbas = NULL;
- _cleanup_free_ char *path = NULL;
ssize_t n;
int r;
if (n != 1024)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while wiping partition table.");
- c = fdisk_new_context();
- if (!c)
- return log_oom();
-
- if (asprintf(&path, "/proc/self/fd/%i", fd) < 0)
- return log_oom();
-
- r = fdisk_assign_device(c, path, 0);
+ r = fdisk_new_context_fd(fd, /* read_only= */ false, &c);
if (r < 0)
return log_error_errno(r, "Failed to open device: %m");
_cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL;
_cleanup_(fdisk_unref_tablep) struct fdisk_table *table = NULL;
struct fdisk_partition *partition = NULL;
- _cleanup_close_ int opened_image_fd = -1;
+ _cleanup_close_ int opened_image_fd = -EBADF;
_cleanup_free_ char *whole_disk = NULL;
- int r, resize_type, image_fd = -1;
+ int r, resize_type, image_fd = -EBADF;
sd_id128_t disk_uuid;
const char *ip, *ipo;
struct statfs sfs;