| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <linux/fs.h> |
| 5 |
|
| 6 |
#include "basic-forward.h" |
| 7 |
|
| 8 |
/* The chattr() flags to apply when creating a new file *before* writing to it. In particular, flags such as |
| 9 |
* FS_NOCOW_FL don't work if applied a-posteriori. All other flags are fine (or even necessary, think |
| 10 |
* FS_IMMUTABLE_FL!) to apply after writing to the files. */ |
| 11 |
#define CHATTR_EARLY_FL \ |
| 12 |
(FS_NOATIME_FL | \ |
| 13 |
FS_COMPR_FL | \ |
| 14 |
FS_NOCOW_FL | \ |
| 15 |
FS_NOCOMP_FL | \ |
| 16 |
FS_PROJINHERIT_FL) |
| 17 |
|
| 18 |
#define CHATTR_ALL_FL \ |
| 19 |
(FS_NOATIME_FL | \ |
| 20 |
FS_SYNC_FL | \ |
| 21 |
FS_DIRSYNC_FL | \ |
| 22 |
FS_APPEND_FL | \ |
| 23 |
FS_COMPR_FL | \ |
| 24 |
FS_NODUMP_FL | \ |
| 25 |
FS_EXTENT_FL | \ |
| 26 |
FS_IMMUTABLE_FL | \ |
| 27 |
FS_JOURNAL_DATA_FL | \ |
| 28 |
FS_SECRM_FL | \ |
| 29 |
FS_UNRM_FL | \ |
| 30 |
FS_NOTAIL_FL | \ |
| 31 |
FS_TOPDIR_FL | \ |
| 32 |
FS_NOCOW_FL | \ |
| 33 |
FS_PROJINHERIT_FL) |
| 34 |
|
| 35 |
typedef enum ChattrApplyFlags { |
| 36 |
CHATTR_FALLBACK_BITWISE = 1 << 0, |
| 37 |
CHATTR_WARN_UNSUPPORTED_FLAGS = 1 << 1, |
| 38 |
} ChattrApplyFlags; |
| 39 |
|
| 40 |
int chattr_full(int dir_fd, const char *path, unsigned value, unsigned mask, unsigned *ret_previous, unsigned *ret_final, ChattrApplyFlags flags); |
| 41 |
static inline int chattr_at(int dir_fd, const char *path, unsigned value, unsigned mask) { |
| 42 |
return chattr_full(dir_fd, path, value, mask, NULL, NULL, 0); |
| 43 |
} |
| 44 |
static inline int chattr_fd(int fd, unsigned value, unsigned mask) { |
| 45 |
return chattr_full(fd, NULL, value, mask, NULL, NULL, 0); |
| 46 |
} |
| 47 |
static inline int chattr_path(const char *path, unsigned value, unsigned mask) { |
| 48 |
return chattr_full(AT_FDCWD, path, value, mask, NULL, NULL, 0); |
| 49 |
} |
| 50 |
|
| 51 |
int read_attr_fd(int fd, unsigned *ret); |
| 52 |
int read_attr_at(int dir_fd, const char *path, unsigned *ret); |
| 53 |
int read_fs_xattr_fd(int fd, uint32_t *ret_xflags, uint32_t *ret_projid); |
| 54 |
|
| 55 |
int set_proj_id(int fd, uint32_t proj_id); |
| 56 |
int set_proj_id_recursive(int fd, uint32_t proj_id); |
| 57 |
|
| 58 |
/* Combination of chattr flags, that should be appropriate for secrets stored on disk: Secure Remove + |
| 59 |
* Exclusion from Dumping + Synchronous Writing (i.e. not caching in memory) + In-Place Updating (i.e. not |
| 60 |
* spurious copies). */ |
| 61 |
#define CHATTR_SECRET_FLAGS (FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL) |
| 62 |
|
| 63 |
static inline int chattr_secret(int fd, ChattrApplyFlags flags) { |
| 64 |
return chattr_full(fd, NULL, CHATTR_SECRET_FLAGS, CHATTR_SECRET_FLAGS, NULL, NULL, flags|CHATTR_FALLBACK_BITWISE); |
| 65 |
} |
| 66 |
|
| 67 |
bool inode_type_can_chattr(mode_t mode); |