1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _BCACHEFS_INODE_H 3#define _BCACHEFS_INODE_H 4 5#include "bkey.h" 6#include "bkey_methods.h" 7#include "opts.h" 8 9enum bch_validate_flags; 10extern const char * const bch2_inode_opts[]; 11 12int bch2_inode_invalid(struct bch_fs *, struct bkey_s_c, 13 enum bch_validate_flags, struct printbuf *); 14int bch2_inode_v2_invalid(struct bch_fs *, struct bkey_s_c, 15 enum bch_validate_flags, struct printbuf *); 16int bch2_inode_v3_invalid(struct bch_fs *, struct bkey_s_c, 17 enum bch_validate_flags, struct printbuf *); 18void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 19 20int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned, 21 struct bkey_s_c, struct bkey_s, 22 enum btree_iter_update_trigger_flags); 23 24#define bch2_bkey_ops_inode ((struct bkey_ops) { \ 25 .key_invalid = bch2_inode_invalid, \ 26 .val_to_text = bch2_inode_to_text, \ 27 .trigger = bch2_trigger_inode, \ 28 .min_val_size = 16, \ 29}) 30 31#define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \ 32 .key_invalid = bch2_inode_v2_invalid, \ 33 .val_to_text = bch2_inode_to_text, \ 34 .trigger = bch2_trigger_inode, \ 35 .min_val_size = 32, \ 36}) 37 38#define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \ 39 .key_invalid = bch2_inode_v3_invalid, \ 40 .val_to_text = bch2_inode_to_text, \ 41 .trigger = bch2_trigger_inode, \ 42 .min_val_size = 48, \ 43}) 44 45static inline bool bkey_is_inode(const struct bkey *k) 46{ 47 return k->type == KEY_TYPE_inode || 48 k->type == KEY_TYPE_inode_v2 || 49 k->type == KEY_TYPE_inode_v3; 50} 51 52int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c, 53 enum bch_validate_flags, struct printbuf *); 54void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 55 56#define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \ 57 .key_invalid = bch2_inode_generation_invalid, \ 58 .val_to_text = bch2_inode_generation_to_text, \ 59 .min_val_size = 8, \ 60}) 61 62#if 0 63typedef struct { 64 u64 lo; 65 u32 hi; 66} __packed __aligned(4) u96; 67#endif 68typedef u64 u96; 69 70struct bch_inode_unpacked { 71 u64 bi_inum; 72 u64 bi_journal_seq; 73 __le64 bi_hash_seed; 74 u64 bi_size; 75 u64 bi_sectors; 76 u64 bi_version; 77 u32 bi_flags; 78 u16 bi_mode; 79 80#define x(_name, _bits) u##_bits _name; 81 BCH_INODE_FIELDS_v3() 82#undef x 83}; 84 85struct bkey_inode_buf { 86 struct bkey_i_inode_v3 inode; 87 88#define x(_name, _bits) + 8 + _bits / 8 89 u8 _pad[0 + BCH_INODE_FIELDS_v3()]; 90#undef x 91} __packed __aligned(8); 92 93void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *); 94int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *); 95struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *); 96 97void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *); 98 99int bch2_inode_peek_nowarn(struct btree_trans *, struct btree_iter *, 100 struct bch_inode_unpacked *, subvol_inum, unsigned); 101int bch2_inode_peek(struct btree_trans *, struct btree_iter *, 102 struct bch_inode_unpacked *, subvol_inum, unsigned); 103 104int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *, 105 struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags); 106 107static inline int bch2_inode_write(struct btree_trans *trans, 108 struct btree_iter *iter, 109 struct bch_inode_unpacked *inode) 110{ 111 return bch2_inode_write_flags(trans, iter, inode, 0); 112} 113 114int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32); 115int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32); 116 117void bch2_inode_init_early(struct bch_fs *, 118 struct bch_inode_unpacked *); 119void bch2_inode_init_late(struct bch_inode_unpacked *, u64, 120 uid_t, gid_t, umode_t, dev_t, 121 struct bch_inode_unpacked *); 122void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *, 123 uid_t, gid_t, umode_t, dev_t, 124 struct bch_inode_unpacked *); 125 126int bch2_inode_create(struct btree_trans *, struct btree_iter *, 127 struct bch_inode_unpacked *, u32, u64); 128 129int bch2_inode_rm(struct bch_fs *, subvol_inum); 130 131int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *, 132 subvol_inum, 133 struct bch_inode_unpacked *); 134int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum, 135 struct bch_inode_unpacked *); 136int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum, 137 struct bch_inode_unpacked *); 138 139#define inode_opt_get(_c, _inode, _name) \ 140 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name) 141 142static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode, 143 enum inode_opt_id id, u64 v) 144{ 145 switch (id) { 146#define x(_name, ...) \ 147 case Inode_opt_##_name: \ 148 inode->bi_##_name = v; \ 149 break; 150 BCH_INODE_OPTS() 151#undef x 152 default: 153 BUG(); 154 } 155} 156 157static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode, 158 enum inode_opt_id id) 159{ 160 switch (id) { 161#define x(_name, ...) \ 162 case Inode_opt_##_name: \ 163 return inode->bi_##_name; 164 BCH_INODE_OPTS() 165#undef x 166 default: 167 BUG(); 168 } 169} 170 171static inline u8 mode_to_type(umode_t mode) 172{ 173 return (mode >> 12) & 15; 174} 175 176static inline u8 inode_d_type(struct bch_inode_unpacked *inode) 177{ 178 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode); 179} 180 181static inline u32 bch2_inode_flags(struct bkey_s_c k) 182{ 183 switch (k.k->type) { 184 case KEY_TYPE_inode: 185 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags); 186 case KEY_TYPE_inode_v2: 187 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags); 188 case KEY_TYPE_inode_v3: 189 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags); 190 default: 191 return 0; 192 } 193} 194 195/* i_nlink: */ 196 197static inline unsigned nlink_bias(umode_t mode) 198{ 199 return S_ISDIR(mode) ? 2 : 1; 200} 201 202static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi) 203{ 204 return bi->bi_flags & BCH_INODE_unlinked 205 ? 0 206 : bi->bi_nlink + nlink_bias(bi->bi_mode); 207} 208 209static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi, 210 unsigned nlink) 211{ 212 if (nlink) { 213 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode); 214 bi->bi_flags &= ~BCH_INODE_unlinked; 215 } else { 216 bi->bi_nlink = 0; 217 bi->bi_flags |= BCH_INODE_unlinked; 218 } 219} 220 221int bch2_inode_nlink_inc(struct bch_inode_unpacked *); 222void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *); 223 224static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode) 225{ 226 bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset; 227 228 return S_ISDIR(inode->bi_mode) || 229 (!inode->bi_nlink && inode_has_bp); 230} 231 232struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *); 233void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *, 234 struct bch_inode_unpacked *); 235int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *); 236 237int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32); 238int bch2_delete_dead_inodes(struct bch_fs *); 239 240#endif /* _BCACHEFS_INODE_H */ 241