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