1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
8 enum bkey_invalid_flags;
9 extern const char * const bch2_inode_opts[];
11 int bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c,
12 enum bkey_invalid_flags, struct printbuf *);
13 int bch2_inode_v2_invalid(const struct bch_fs *, struct bkey_s_c,
14 enum bkey_invalid_flags, struct printbuf *);
15 int bch2_inode_v3_invalid(const struct bch_fs *, struct bkey_s_c,
16 enum bkey_invalid_flags, struct printbuf *);
17 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
19 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
20 .key_invalid = bch2_inode_invalid, \
21 .val_to_text = bch2_inode_to_text, \
22 .trans_trigger = bch2_trans_mark_inode, \
23 .atomic_trigger = bch2_mark_inode, \
27 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
28 .key_invalid = bch2_inode_v2_invalid, \
29 .val_to_text = bch2_inode_to_text, \
30 .trans_trigger = bch2_trans_mark_inode, \
31 .atomic_trigger = bch2_mark_inode, \
35 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
36 .key_invalid = bch2_inode_v3_invalid, \
37 .val_to_text = bch2_inode_to_text, \
38 .trans_trigger = bch2_trans_mark_inode, \
39 .atomic_trigger = bch2_mark_inode, \
43 static inline bool bkey_is_inode(const struct bkey *k)
45 return k->type == KEY_TYPE_inode ||
46 k->type == KEY_TYPE_inode_v2 ||
47 k->type == KEY_TYPE_inode_v3;
50 int bch2_inode_generation_invalid(const struct bch_fs *, struct bkey_s_c,
51 enum bkey_invalid_flags, struct printbuf *);
52 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
54 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
55 .key_invalid = bch2_inode_generation_invalid, \
56 .val_to_text = bch2_inode_generation_to_text, \
64 } __packed __aligned(4) u96;
68 struct bch_inode_unpacked {
78 #define x(_name, _bits) u##_bits _name;
83 struct bkey_inode_buf {
84 struct bkey_i_inode_v3 inode;
86 #define x(_name, _bits) + 8 + _bits / 8
87 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
89 } __packed __aligned(8);
91 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
92 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
93 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
95 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
97 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
98 struct bch_inode_unpacked *, subvol_inum, unsigned);
99 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
100 struct bch_inode_unpacked *);
102 void bch2_inode_init_early(struct bch_fs *,
103 struct bch_inode_unpacked *);
104 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
105 uid_t, gid_t, umode_t, dev_t,
106 struct bch_inode_unpacked *);
107 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
108 uid_t, gid_t, umode_t, dev_t,
109 struct bch_inode_unpacked *);
111 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
112 struct bch_inode_unpacked *, u32, u64);
114 int bch2_inode_rm(struct bch_fs *, subvol_inum);
116 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
117 struct bch_inode_unpacked *);
118 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
119 struct bch_inode_unpacked *);
121 #define inode_opt_get(_c, _inode, _name) \
122 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
124 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
125 enum inode_opt_id id, u64 v)
128 #define x(_name, ...) \
129 case Inode_opt_##_name: \
130 inode->bi_##_name = v; \
139 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
140 enum inode_opt_id id)
143 #define x(_name, ...) \
144 case Inode_opt_##_name: \
145 return inode->bi_##_name;
153 static inline u8 mode_to_type(umode_t mode)
155 return (mode >> 12) & 15;
158 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
160 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
165 static inline unsigned nlink_bias(umode_t mode)
167 return S_ISDIR(mode) ? 2 : 1;
170 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
172 return bi->bi_flags & BCH_INODE_UNLINKED
174 : bi->bi_nlink + nlink_bias(bi->bi_mode);
177 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
181 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
182 bi->bi_flags &= ~BCH_INODE_UNLINKED;
185 bi->bi_flags |= BCH_INODE_UNLINKED;
189 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
190 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
192 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
193 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
194 struct bch_inode_unpacked *);
196 #endif /* _BCACHEFS_INODE_H */