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