| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/mount.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "chase.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fileio.h" |
| 11 |
#include "filesystems.h" |
| 12 |
#include "fs-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "mountpoint-util.h" |
| 15 |
#include "nulstr-util.h" |
| 16 |
#include "path-util.h" |
| 17 |
#include "stat-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "unaligned.h" |
| 21 |
|
| 22 |
/* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of |
| 23 |
* any more currently defined value to future-proof things: if the size is increased in the API headers, and our code |
| 24 |
* is recompiled then it would cease working on old kernels, as those refuse any sizes larger than this value with |
| 25 |
* EINVAL right-away. Hence, let's disconnect ourselves from any such API changes, and stick to the original definition |
| 26 |
* from when it was introduced. We use it as a start value only anyway (see below), and hence should be able to deal |
| 27 |
* with large file handles anyway. */ |
| 28 |
#define ORIGINAL_MAX_HANDLE_SZ 128 |
| 29 |
|
| 30 |
bool is_name_to_handle_at_fatal_error(int err) { |
| 31 |
/* name_to_handle_at() can return "acceptable" errors that are due to the context. For example |
| 32 |
* the file system does not support name_to_handle_at() (EOPNOTSUPP), or the syscall was blocked |
| 33 |
* (EACCES/EPERM; maybe through seccomp, because we are running inside of a container), or |
| 34 |
* the mount point is not triggered yet (EOVERFLOW, think autofs+nfs4), or some general name_to_handle_at() |
| 35 |
* flakiness (EINVAL). However other errors are not supposed to happen and therefore are considered |
| 36 |
* fatal ones. */ |
| 37 |
|
| 38 |
assert(err < 0); |
| 39 |
|
| 40 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(err)) |
| 41 |
return false; |
| 42 |
if (ERRNO_IS_NEG_PRIVILEGE(err)) |
| 43 |
return false; |
| 44 |
|
| 45 |
return !IN_SET(err, -EOVERFLOW, -EINVAL); |
| 46 |
} |
| 47 |
|
| 48 |
int name_to_handle_at_loop( |
| 49 |
int fd, |
| 50 |
const char *path, |
| 51 |
struct file_handle **ret_handle, |
| 52 |
int *ret_mnt_id, |
| 53 |
uint64_t *ret_unique_mnt_id, |
| 54 |
int flags) { |
| 55 |
|
| 56 |
int r; |
| 57 |
|
| 58 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 59 |
assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|AT_HANDLE_FID)) == 0); |
| 60 |
|
| 61 |
/* We need to invoke name_to_handle_at() in a loop, given that it might return EOVERFLOW when the specified |
| 62 |
* buffer is too small. Note that in contrast to what the docs might suggest, MAX_HANDLE_SZ is only good as a |
| 63 |
* start value, it is not an upper bound on the buffer size required. |
| 64 |
* |
| 65 |
* This improves on raw name_to_handle_at() also in one other regard: ret_handle and ret_mnt_id can be passed |
| 66 |
* as NULL if there's no interest in either. |
| 67 |
* |
| 68 |
* If unique mount id is requested via ret_unique_mnt_id, try AT_HANDLE_MNT_ID_UNIQUE flag first |
| 69 |
* (needs kernel v6.12), and fall back to statx() if not supported. If neither worked, and caller |
| 70 |
* also specifies ret_mnt_id, then the old-style mount id is returned, -EUNATCH otherwise. */ |
| 71 |
|
| 72 |
if (isempty(path)) { |
| 73 |
flags |= AT_EMPTY_PATH; |
| 74 |
path = ""; |
| 75 |
} |
| 76 |
|
| 77 |
for (size_t n = ORIGINAL_MAX_HANDLE_SZ;;) { |
| 78 |
_cleanup_free_ struct file_handle *h = NULL; |
| 79 |
|
| 80 |
h = malloc0(offsetof(struct file_handle, f_handle) + n); |
| 81 |
if (!h) |
| 82 |
return -ENOMEM; |
| 83 |
|
| 84 |
h->handle_bytes = n; |
| 85 |
|
| 86 |
if (ret_unique_mnt_id) { |
| 87 |
/* Here, explicitly initialize mnt_id, otherwise valgrind complains: |
| 88 |
* |
| 89 |
* ==175708== Conditional jump or move depends on uninitialised value(s) |
| 90 |
* ==175708== at 0x4BC33D1: inode_same_at (stat-util.c:610) |
| 91 |
* ==175708== by 0x4BF1972: inode_same (stat-util.h:86) |
| 92 |
*/ |
| 93 |
uint64_t mnt_id = 0; |
| 94 |
|
| 95 |
/* The kernel will still use this as uint64_t pointer */ |
| 96 |
r = name_to_handle_at(fd, path, h, (int *) &mnt_id, flags|AT_HANDLE_MNT_ID_UNIQUE); |
| 97 |
if (r >= 0) { |
| 98 |
if (ret_handle) |
| 99 |
*ret_handle = TAKE_PTR(h); |
| 100 |
|
| 101 |
*ret_unique_mnt_id = mnt_id; |
| 102 |
|
| 103 |
if (ret_mnt_id) |
| 104 |
*ret_mnt_id = -1; |
| 105 |
|
| 106 |
return 1; |
| 107 |
} |
| 108 |
if (errno == EOVERFLOW) |
| 109 |
goto grow; |
| 110 |
if (errno != EINVAL) |
| 111 |
return -errno; |
| 112 |
} |
| 113 |
|
| 114 |
int mnt_id; |
| 115 |
r = name_to_handle_at(fd, path, h, &mnt_id, flags); |
| 116 |
if (r >= 0) { |
| 117 |
if (ret_unique_mnt_id) { |
| 118 |
/* Hmm, AT_HANDLE_MNT_ID_UNIQUE is not supported? Let's try to acquire |
| 119 |
* the unique mount id from statx() then, which has a slightly lower |
| 120 |
* kernel version requirement (6.8 vs 6.12). */ |
| 121 |
|
| 122 |
struct statx sx; |
| 123 |
r = xstatx(fd, path, |
| 124 |
at_flags_normalize_nofollow(flags & (AT_SYMLINK_FOLLOW|AT_EMPTY_PATH))|AT_STATX_DONT_SYNC, |
| 125 |
STATX_MNT_ID_UNIQUE, |
| 126 |
&sx); |
| 127 |
if (r >= 0) { |
| 128 |
if (ret_handle) |
| 129 |
*ret_handle = TAKE_PTR(h); |
| 130 |
|
| 131 |
*ret_unique_mnt_id = sx.stx_mnt_id; |
| 132 |
|
| 133 |
if (ret_mnt_id) |
| 134 |
*ret_mnt_id = -1; |
| 135 |
|
| 136 |
return 1; |
| 137 |
} |
| 138 |
if (r != -EUNATCH || !ret_mnt_id) |
| 139 |
return r; |
| 140 |
|
| 141 |
*ret_unique_mnt_id = 0; |
| 142 |
} |
| 143 |
|
| 144 |
if (ret_handle) |
| 145 |
*ret_handle = TAKE_PTR(h); |
| 146 |
|
| 147 |
if (ret_mnt_id) |
| 148 |
*ret_mnt_id = mnt_id; |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |
| 152 |
if (errno != EOVERFLOW) |
| 153 |
return -errno; |
| 154 |
|
| 155 |
grow: |
| 156 |
/* If name_to_handle_at() didn't increase the byte size, then this EOVERFLOW is caused by |
| 157 |
* something else (apparently EOVERFLOW is returned for untriggered nfs4 autofs mounts |
| 158 |
* sometimes), not by the too small buffer. In that case propagate EOVERFLOW */ |
| 159 |
if (h->handle_bytes <= n) |
| 160 |
return -EOVERFLOW; |
| 161 |
|
| 162 |
/* The buffer was too small. Size the new buffer by what name_to_handle_at() returned. */ |
| 163 |
n = h->handle_bytes; |
| 164 |
|
| 165 |
/* paranoia: check for overflow (note that .handle_bytes is unsigned only) */ |
| 166 |
if (n > UINT_MAX - offsetof(struct file_handle, f_handle)) |
| 167 |
return -EOVERFLOW; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
int name_to_handle_at_try_fid( |
| 172 |
int fd, |
| 173 |
const char *path, |
| 174 |
struct file_handle **ret_handle, |
| 175 |
int *ret_mnt_id, |
| 176 |
uint64_t *ret_unique_mnt_id, |
| 177 |
int flags) { |
| 178 |
|
| 179 |
int r; |
| 180 |
|
| 181 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 182 |
|
| 183 |
/* First issues name_to_handle_at() with AT_HANDLE_FID. If this fails and this is not a fatal error |
| 184 |
* we'll try without the flag, in order to support older kernels that didn't have AT_HANDLE_FID |
| 185 |
* (i.e. older than Linux 6.5). */ |
| 186 |
|
| 187 |
r = name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags | AT_HANDLE_FID); |
| 188 |
if (r >= 0 || is_name_to_handle_at_fatal_error(r)) |
| 189 |
return r; |
| 190 |
|
| 191 |
return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags & ~AT_HANDLE_FID); |
| 192 |
} |
| 193 |
|
| 194 |
int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret) { |
| 195 |
_cleanup_free_ struct file_handle *h = NULL; |
| 196 |
int r; |
| 197 |
|
| 198 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 199 |
|
| 200 |
/* This provides the first 64bit of the file handle. */ |
| 201 |
|
| 202 |
r = name_to_handle_at_loop(fd, path, &h, /* ret_mnt_id= */ NULL, /* ret_unique_mnt_id= */ NULL, /* flags= */ 0); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
if (h->handle_bytes < sizeof(uint64_t)) |
| 206 |
return -EBADMSG; |
| 207 |
|
| 208 |
if (ret) |
| 209 |
/* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */ |
| 210 |
*ret = unaligned_read_ne64(h->f_handle); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) { |
| 216 |
if (a == b) |
| 217 |
return true; |
| 218 |
if (!a != !b) |
| 219 |
return false; |
| 220 |
if (a->handle_type != b->handle_type) |
| 221 |
return false; |
| 222 |
|
| 223 |
return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0; |
| 224 |
} |
| 225 |
|
| 226 |
struct file_handle* file_handle_dup(const struct file_handle *fh) { |
| 227 |
_cleanup_free_ struct file_handle *fh_copy = NULL; |
| 228 |
|
| 229 |
assert(fh); |
| 230 |
|
| 231 |
fh_copy = malloc0(offsetof(struct file_handle, f_handle) + fh->handle_bytes); |
| 232 |
if (!fh_copy) |
| 233 |
return NULL; |
| 234 |
|
| 235 |
fh_copy->handle_bytes = fh->handle_bytes; |
| 236 |
fh_copy->handle_type = fh->handle_type; |
| 237 |
memcpy(fh_copy->f_handle, fh->f_handle, fh->handle_bytes); |
| 238 |
|
| 239 |
return TAKE_PTR(fh_copy); |
| 240 |
} |
| 241 |
|
| 242 |
int is_mount_point_at(int dir_fd, const char *path, int flags) { |
| 243 |
int r; |
| 244 |
|
| 245 |
assert(wildcard_fd_is_valid(dir_fd)); |
| 246 |
assert((flags & ~AT_SYMLINK_FOLLOW) == 0); |
| 247 |
|
| 248 |
if (path_equal(path, "/")) |
| 249 |
return true; |
| 250 |
|
| 251 |
if (dir_fd == XAT_FDROOT && isempty(path)) |
| 252 |
return true; |
| 253 |
|
| 254 |
struct statx sx; |
| 255 |
r = xstatx_full(dir_fd, path, |
| 256 |
at_flags_normalize_nofollow(flags) | |
| 257 |
AT_NO_AUTOMOUNT | /* don't trigger automounts – mounts are a local concept, hence no need to trigger automounts to determine STATX_ATTR_MOUNT_ROOT */ |
| 258 |
AT_STATX_DONT_SYNC, /* don't go to the network for this – for similar reasons */ |
| 259 |
/* xstatx_flags = */ 0, |
| 260 |
STATX_TYPE|STATX_INO, |
| 261 |
/* optional_mask = */ 0, |
| 262 |
STATX_ATTR_MOUNT_ROOT, |
| 263 |
&sx); |
| 264 |
if (r < 0) |
| 265 |
return r; |
| 266 |
|
| 267 |
if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) |
| 268 |
return true; |
| 269 |
|
| 270 |
/* When running on chroot environment, the root may not be a mount point, but we unconditionally |
| 271 |
* return true when the input is "/" in the above, but the shortcut may not work e.g. when the path |
| 272 |
* is relative. */ |
| 273 |
struct statx sx2; |
| 274 |
r = xstatx(AT_FDCWD, |
| 275 |
"/", |
| 276 |
AT_STATX_DONT_SYNC, |
| 277 |
STATX_TYPE|STATX_INO, |
| 278 |
&sx2); |
| 279 |
if (r < 0) |
| 280 |
return r; |
| 281 |
|
| 282 |
return statx_inode_same(&sx, &sx2); |
| 283 |
} |
| 284 |
|
| 285 |
/* flags can be AT_SYMLINK_FOLLOW or 0 */ |
| 286 |
int path_is_mount_point_full(const char *path, const char *root, int flags) { |
| 287 |
_cleanup_close_ int dir_fd = -EBADF; |
| 288 |
int r; |
| 289 |
|
| 290 |
assert(path); |
| 291 |
assert((flags & ~AT_SYMLINK_FOLLOW) == 0); |
| 292 |
|
| 293 |
if (empty_or_root(root)) |
| 294 |
return is_mount_point_at(AT_FDCWD, path, flags); |
| 295 |
|
| 296 |
r = chase(path, root, |
| 297 |
FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : CHASE_NOFOLLOW, |
| 298 |
/* ret_path= */ NULL, &dir_fd); |
| 299 |
if (r < 0) |
| 300 |
return r; |
| 301 |
|
| 302 |
return is_mount_point_at(dir_fd, /* path= */ NULL, flags); |
| 303 |
} |
| 304 |
|
| 305 |
static int path_get_mnt_id_at_internal(int dir_fd, const char *path, bool unique, uint64_t *ret) { |
| 306 |
struct statx sx; |
| 307 |
int r; |
| 308 |
|
| 309 |
assert(wildcard_fd_is_valid(dir_fd)); |
| 310 |
assert(ret); |
| 311 |
|
| 312 |
r = xstatx(dir_fd, path, |
| 313 |
AT_SYMLINK_NOFOLLOW | |
| 314 |
AT_NO_AUTOMOUNT | /* don't trigger automounts, mnt_id is a local concept */ |
| 315 |
AT_STATX_DONT_SYNC, /* don't go to the network, mnt_id is a local concept */ |
| 316 |
unique ? STATX_MNT_ID_UNIQUE : STATX_MNT_ID, |
| 317 |
&sx); |
| 318 |
if (r < 0) |
| 319 |
return r; |
| 320 |
|
| 321 |
*ret = sx.stx_mnt_id; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) { |
| 326 |
uint64_t mnt_id; |
| 327 |
int r; |
| 328 |
|
| 329 |
assert(ret); |
| 330 |
|
| 331 |
r = path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ false, &mnt_id); |
| 332 |
if (r < 0) |
| 333 |
return r; |
| 334 |
|
| 335 |
assert(mnt_id <= INT_MAX); |
| 336 |
*ret = (int) mnt_id; |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
int path_get_unique_mnt_id_at(int dir_fd, const char *path, uint64_t *ret) { |
| 341 |
return path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ true, ret); |
| 342 |
} |
| 343 |
|
| 344 |
bool fstype_is_network(const char *fstype) { |
| 345 |
const char *x; |
| 346 |
|
| 347 |
x = startswith(fstype, "fuse."); |
| 348 |
if (x) |
| 349 |
fstype = x; |
| 350 |
|
| 351 |
if (nulstr_contains(filesystem_sets[FILESYSTEM_SET_NETWORK].value, fstype)) |
| 352 |
return true; |
| 353 |
|
| 354 |
/* Filesystems not present in the internal database */ |
| 355 |
return STR_IN_SET(fstype, |
| 356 |
"davfs", |
| 357 |
"glusterfs", |
| 358 |
"lustre", |
| 359 |
"sshfs"); |
| 360 |
} |
| 361 |
|
| 362 |
bool fstype_needs_quota(const char *fstype) { |
| 363 |
/* 1. quotacheck needs to be run for some filesystems after they are mounted |
| 364 |
* if the filesystem was not unmounted cleanly. |
| 365 |
* 2. You may need to run quotaon to enable quota usage tracking and/or |
| 366 |
* enforcement. |
| 367 |
* ext2 - needs 1) and 2) |
| 368 |
* ext3 - needs 2) if configured using usrjquota/grpjquota mount options |
| 369 |
* ext4 - needs 1) if created without journal, needs 2) if created without QUOTA |
| 370 |
* filesystem feature |
| 371 |
* reiserfs - needs 2). |
| 372 |
* jfs - needs 2) |
| 373 |
* f2fs - needs 2) if configured using usrjquota/grpjquota/prjjquota mount options |
| 374 |
* xfs - nothing needed |
| 375 |
* gfs2 - nothing needed |
| 376 |
* ocfs2 - nothing needed |
| 377 |
* btrfs - nothing needed |
| 378 |
* for reference see filesystem and quota manpages */ |
| 379 |
return STR_IN_SET(fstype, |
| 380 |
"ext2", |
| 381 |
"ext3", |
| 382 |
"ext4", |
| 383 |
"reiserfs", |
| 384 |
"jfs", |
| 385 |
"f2fs"); |
| 386 |
} |
| 387 |
|
| 388 |
bool fstype_has_internal_quota(const char *fstype) { |
| 389 |
/* These filesystems have built-in quota support and do not need |
| 390 |
* external quotacheck/quotaon services - see the "nothing needed" |
| 391 |
* entries in fstype_needs_quota() above. */ |
| 392 |
return STR_IN_SET(fstype, |
| 393 |
"xfs", |
| 394 |
"gfs2", |
| 395 |
"ocfs2", |
| 396 |
"btrfs"); |
| 397 |
} |
| 398 |
|
| 399 |
bool fstype_is_api_vfs(const char *fstype) { |
| 400 |
assert(fstype); |
| 401 |
|
| 402 |
const FilesystemSet *fs; |
| 403 |
FOREACH_ARGUMENT(fs, |
| 404 |
filesystem_sets + FILESYSTEM_SET_BASIC_API, |
| 405 |
filesystem_sets + FILESYSTEM_SET_AUXILIARY_API, |
| 406 |
filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API, |
| 407 |
filesystem_sets + FILESYSTEM_SET_TEMPORARY) |
| 408 |
if (nulstr_contains(fs->value, fstype)) |
| 409 |
return true; |
| 410 |
|
| 411 |
/* Filesystems not present in the internal database */ |
| 412 |
return STR_IN_SET(fstype, |
| 413 |
"autofs", |
| 414 |
"cpuset"); |
| 415 |
} |
| 416 |
|
| 417 |
bool fstype_is_blockdev_backed(const char *fstype) { |
| 418 |
const char *x; |
| 419 |
|
| 420 |
x = startswith(fstype, "fuse."); |
| 421 |
if (x) |
| 422 |
fstype = x; |
| 423 |
|
| 424 |
return !STR_IN_SET(fstype, "9p", "overlay") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype); |
| 425 |
} |
| 426 |
|
| 427 |
bool fstype_is_ro(const char *fstype) { |
| 428 |
/* All Linux file systems that are necessarily read-only */ |
| 429 |
return STR_IN_SET(fstype, |
| 430 |
"DM_verity_hash", |
| 431 |
"cramfs", |
| 432 |
"erofs", |
| 433 |
"iso9660", |
| 434 |
"squashfs"); |
| 435 |
} |
| 436 |
|
| 437 |
bool fstype_can_discard(const char *fstype) { |
| 438 |
assert(fstype); |
| 439 |
|
| 440 |
/* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might |
| 441 |
* not be allowed in our MAC context. */ |
| 442 |
if (STR_IN_SET(fstype, "btrfs", "f2fs", "ext4", "vfat", "xfs")) |
| 443 |
return true; |
| 444 |
|
| 445 |
/* On new kernels we can just ask the kernel */ |
| 446 |
return mount_option_supported(fstype, "discard", NULL) > 0; |
| 447 |
} |
| 448 |
|
| 449 |
const char* fstype_norecovery_option(const char *fstype) { |
| 450 |
int r; |
| 451 |
|
| 452 |
assert(fstype); |
| 453 |
|
| 454 |
/* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might |
| 455 |
* not be allowed in our MAC context. */ |
| 456 |
if (STR_IN_SET(fstype, "ext3", "ext4", "xfs")) |
| 457 |
return "norecovery"; |
| 458 |
|
| 459 |
/* btrfs dropped support for the "norecovery" option in 6.8 |
| 460 |
* (https://github.com/torvalds/linux/commit/a1912f712188291f9d7d434fba155461f1ebef66) and replaced |
| 461 |
* it with rescue=nologreplay, so we check for the new name first and fall back to checking for the |
| 462 |
* old name if the new name doesn't work. */ |
| 463 |
if (streq(fstype, "btrfs")) { |
| 464 |
r = mount_option_supported(fstype, "rescue=nologreplay", NULL); |
| 465 |
if (r == -EAGAIN) { |
| 466 |
log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming old kernel with 'norecovery': %m"); |
| 467 |
return "norecovery"; |
| 468 |
} |
| 469 |
if (r < 0) |
| 470 |
log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming it is not supported: %m"); |
| 471 |
if (r > 0) |
| 472 |
return "rescue=nologreplay"; |
| 473 |
} |
| 474 |
|
| 475 |
/* On new kernels we can just ask the kernel */ |
| 476 |
return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL; |
| 477 |
} |
| 478 |
|
| 479 |
bool fstype_can_fmask_dmask(const char *fstype) { |
| 480 |
assert(fstype); |
| 481 |
|
| 482 |
/* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might |
| 483 |
* not be allowed in our MAC context. If we don't know ourselves, on new kernels we can just ask the |
| 484 |
* kernel. */ |
| 485 |
return streq(fstype, "vfat") || (mount_option_supported(fstype, "fmask", "0177") > 0 && mount_option_supported(fstype, "dmask", "0077") > 0); |
| 486 |
} |
| 487 |
|
| 488 |
bool fstype_can_ownership(const char *fstype) { |
| 489 |
/* File systems which are not known to not support uid/gid ownership. |
| 490 |
* For some types, this can be a bit murky. So just exclude the ones that for sure |
| 491 |
* don't support with the current implementations in Linux. */ |
| 492 |
|
| 493 |
return !STR_IN_SET(ASSERT_PTR(fstype), |
| 494 |
"adfs", |
| 495 |
"exfat", |
| 496 |
"fat", |
| 497 |
"hfs", |
| 498 |
"hpfs", |
| 499 |
"msdos", |
| 500 |
"ntfs", |
| 501 |
"vfat"); |
| 502 |
} |
| 503 |
|
| 504 |
bool fstype_can_uid_gid(const char *fstype) { |
| 505 |
/* All file systems that have a uid=/gid= mount option that fixates the owners of all files and |
| 506 |
* directories, current and future. Note that this does *not* ask the kernel via |
| 507 |
* mount_option_supported() here because the uid=/gid= setting of various file systems mean different |
| 508 |
* things: some apply it only to the root dir inode, others to all inodes in the file system. Thus we |
| 509 |
* maintain the curated list below. 😢 */ |
| 510 |
|
| 511 |
return STR_IN_SET(fstype, |
| 512 |
"adfs", |
| 513 |
"exfat", |
| 514 |
"fat", |
| 515 |
"hfs", |
| 516 |
"hpfs", |
| 517 |
"iso9660", |
| 518 |
"msdos", |
| 519 |
"ntfs", |
| 520 |
"vfat"); |
| 521 |
} |
| 522 |
|
| 523 |
int dev_is_devtmpfs(void) { |
| 524 |
_cleanup_fclose_ FILE *proc_self_mountinfo = NULL; |
| 525 |
int mount_id, r; |
| 526 |
char *e; |
| 527 |
|
| 528 |
r = path_get_mnt_id("/dev", &mount_id); |
| 529 |
if (r < 0) |
| 530 |
return r; |
| 531 |
|
| 532 |
r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo); |
| 533 |
if (r == -ENOENT) |
| 534 |
return proc_mounted() > 0 ? -ENOENT : -ENOSYS; |
| 535 |
if (r < 0) |
| 536 |
return r; |
| 537 |
|
| 538 |
for (;;) { |
| 539 |
_cleanup_free_ char *line = NULL; |
| 540 |
int mid; |
| 541 |
|
| 542 |
r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line); |
| 543 |
if (r < 0) |
| 544 |
return r; |
| 545 |
if (r == 0) |
| 546 |
break; |
| 547 |
|
| 548 |
if (sscanf(line, "%i", &mid) != 1) |
| 549 |
continue; |
| 550 |
|
| 551 |
if (mid != mount_id) |
| 552 |
continue; |
| 553 |
|
| 554 |
e = strstrafter(line, " - "); |
| 555 |
if (!e) |
| 556 |
continue; |
| 557 |
|
| 558 |
/* accept any name that starts with the currently expected type */ |
| 559 |
if (startswith(e, "devtmpfs")) |
| 560 |
return true; |
| 561 |
} |
| 562 |
|
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
static int mount_fd( |
| 567 |
const char *source, |
| 568 |
int target_fd, |
| 569 |
const char *filesystemtype, |
| 570 |
unsigned long mountflags, |
| 571 |
const void *data) { |
| 572 |
|
| 573 |
assert(target_fd >= 0); |
| 574 |
|
| 575 |
if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) { |
| 576 |
if (errno != ENOENT) |
| 577 |
return -errno; |
| 578 |
|
| 579 |
/* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't |
| 580 |
* mounted. Check for the latter to generate better error messages. */ |
| 581 |
if (proc_mounted() == 0) |
| 582 |
return -ENOSYS; |
| 583 |
|
| 584 |
return -ENOENT; |
| 585 |
} |
| 586 |
|
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
int mount_nofollow( |
| 591 |
const char *source, |
| 592 |
const char *target, |
| 593 |
const char *filesystemtype, |
| 594 |
unsigned long mountflags, |
| 595 |
const void *data) { |
| 596 |
|
| 597 |
_cleanup_close_ int fd = -EBADF; |
| 598 |
|
| 599 |
assert(target); |
| 600 |
|
| 601 |
/* In almost all cases we want to manipulate the mount table without following symlinks, hence |
| 602 |
* mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is |
| 603 |
* not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early |
| 604 |
* initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the |
| 605 |
* fs to mount) we can only use traditional mount() directly. |
| 606 |
* |
| 607 |
* Note that this disables following only for the final component of the target, i.e symlinks within |
| 608 |
* the path of the target are honoured, as are symlinks in the source path everywhere. */ |
| 609 |
|
| 610 |
fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW); |
| 611 |
if (fd < 0) |
| 612 |
return -errno; |
| 613 |
|
| 614 |
return mount_fd(source, fd, filesystemtype, mountflags, data); |
| 615 |
} |
| 616 |
|
| 617 |
const char* mount_propagation_flag_to_string(unsigned long flags) { |
| 618 |
|
| 619 |
switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) { |
| 620 |
case 0: |
| 621 |
return ""; |
| 622 |
case MS_SHARED: |
| 623 |
return "shared"; |
| 624 |
case MS_SLAVE: |
| 625 |
return "slave"; |
| 626 |
case MS_PRIVATE: |
| 627 |
return "private"; |
| 628 |
} |
| 629 |
|
| 630 |
return NULL; |
| 631 |
} |
| 632 |
|
| 633 |
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) { |
| 634 |
|
| 635 |
POINTER_MAY_BE_NULL(name); |
| 636 |
assert(ret); |
| 637 |
|
| 638 |
if (isempty(name)) |
| 639 |
*ret = 0; |
| 640 |
else if (streq(name, "shared")) |
| 641 |
*ret = MS_SHARED; |
| 642 |
else if (streq(name, "slave")) |
| 643 |
*ret = MS_SLAVE; |
| 644 |
else if (streq(name, "private")) |
| 645 |
*ret = MS_PRIVATE; |
| 646 |
else |
| 647 |
return -EINVAL; |
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
bool mount_propagation_flag_is_valid(unsigned long flag) { |
| 652 |
return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE); |
| 653 |
} |
| 654 |
|
| 655 |
bool mount_new_api_supported(void) { |
| 656 |
static int cache = -1; |
| 657 |
int r; |
| 658 |
|
| 659 |
if (cache >= 0) |
| 660 |
return cache; |
| 661 |
|
| 662 |
/* This is the newest API among the ones we use, so use it as boundary */ |
| 663 |
r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0)); |
| 664 |
if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */ |
| 665 |
return (cache = false); |
| 666 |
|
| 667 |
return (cache = true); |
| 668 |
} |
| 669 |
|
| 670 |
int mount_option_supported(const char *fstype, const char *key, const char *value) { |
| 671 |
_cleanup_close_ int fd = -EBADF; |
| 672 |
int r; |
| 673 |
|
| 674 |
/* Checks if the specified file system supports a mount option. Returns > 0 if it supports it, == 0 if |
| 675 |
* it does not. Return -EAGAIN if we can't determine it. And any other error otherwise. */ |
| 676 |
|
| 677 |
assert(fstype); |
| 678 |
assert(key); |
| 679 |
|
| 680 |
fd = fsopen(fstype, FSOPEN_CLOEXEC); |
| 681 |
if (fd < 0) |
| 682 |
return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype); |
| 683 |
|
| 684 |
/* Various file systems support fs context only in recent kernels (e.g. btrfs). For older kernels |
| 685 |
* fsconfig() with FSCONFIG_SET_STRING/FSCONFIG_SET_FLAG never fail. Which sucks, because we want to |
| 686 |
* use it for testing support, after all. Let's hence do a check if the file system got converted yet |
| 687 |
* first. */ |
| 688 |
if (fsconfig(fd, FSCONFIG_SET_FD, "adefinitelynotexistingmountoption", NULL, fd) < 0) { |
| 689 |
/* If FSCONFIG_SET_FD is not supported for the fs, then the file system was not converted to |
| 690 |
* the new mount API yet. If it returns EINVAL the mount option doesn't exist, but the fstype |
| 691 |
* is converted. */ |
| 692 |
if (errno == EOPNOTSUPP) |
| 693 |
return -EAGAIN; /* fs not converted to new mount API → don't know */ |
| 694 |
if (errno != EINVAL) |
| 695 |
return log_debug_errno(errno, "Failed to check if file system '%s' has been converted to new mount API: %m", fstype); |
| 696 |
|
| 697 |
/* So FSCONFIG_SET_FD worked, but the option didn't exist (we got EINVAL), this means the fs |
| 698 |
* is converted. Let's now ask the actual question we wonder about. */ |
| 699 |
} else |
| 700 |
return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "FSCONFIG_SET_FD worked unexpectedly for '%s', whoa!", fstype); |
| 701 |
|
| 702 |
if (value) |
| 703 |
r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0); |
| 704 |
else |
| 705 |
r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0); |
| 706 |
if (r < 0) { |
| 707 |
if (errno == EINVAL) |
| 708 |
return false; /* EINVAL means option not supported. */ |
| 709 |
|
| 710 |
return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m", |
| 711 |
key, value ? "=" : "", strempty(value), fstype); |
| 712 |
} |
| 713 |
|
| 714 |
return true; /* works! */ |
| 715 |
} |
| 716 |
|
| 717 |
bool path_below_api_vfs(const char *p) { |
| 718 |
assert(p); |
| 719 |
|
| 720 |
/* API VFS are either directly mounted on any of these three paths, or below it. */ |
| 721 |
return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc"); |
| 722 |
} |