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 int bch2_trans_mark_inode(struct btree_trans *, enum btree_id, unsigned,
20 struct bkey_s_c, struct bkey_i *, unsigned);
21 int bch2_mark_inode(struct btree_trans *, enum btree_id, unsigned,
22 struct bkey_s_c, struct bkey_s_c, unsigned);
24 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
25 .key_invalid = bch2_inode_invalid, \
26 .val_to_text = bch2_inode_to_text, \
27 .trans_trigger = bch2_trans_mark_inode, \
28 .atomic_trigger = bch2_mark_inode, \
32 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
33 .key_invalid = bch2_inode_v2_invalid, \
34 .val_to_text = bch2_inode_to_text, \
35 .trans_trigger = bch2_trans_mark_inode, \
36 .atomic_trigger = bch2_mark_inode, \
40 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
41 .key_invalid = bch2_inode_v3_invalid, \
42 .val_to_text = bch2_inode_to_text, \
43 .trans_trigger = bch2_trans_mark_inode, \
44 .atomic_trigger = bch2_mark_inode, \
48 static inline bool bkey_is_inode(const struct bkey *k)
50 return k->type == KEY_TYPE_inode ||
51 k->type == KEY_TYPE_inode_v2 ||
52 k->type == KEY_TYPE_inode_v3;
55 int bch2_inode_generation_invalid(const struct bch_fs *, struct bkey_s_c,
56 enum bkey_invalid_flags, struct printbuf *);
57 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
59 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
60 .key_invalid = bch2_inode_generation_invalid, \
61 .val_to_text = bch2_inode_generation_to_text, \
69 } __packed __aligned(4) u96;
73 struct bch_inode_unpacked {
83 #define x(_name, _bits) u##_bits _name;
88 struct bkey_inode_buf {
89 struct bkey_i_inode_v3 inode;
91 #define x(_name, _bits) + 8 + _bits / 8
92 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
94 } __packed __aligned(8);
96 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
97 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
98 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
100 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
102 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
103 struct bch_inode_unpacked *, subvol_inum, unsigned);
104 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
105 struct bch_inode_unpacked *);
107 void bch2_inode_init_early(struct bch_fs *,
108 struct bch_inode_unpacked *);
109 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
110 uid_t, gid_t, umode_t, dev_t,
111 struct bch_inode_unpacked *);
112 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
113 uid_t, gid_t, umode_t, dev_t,
114 struct bch_inode_unpacked *);
116 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
117 struct bch_inode_unpacked *, u32, u64);
119 int bch2_inode_rm(struct bch_fs *, subvol_inum);
121 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
122 struct bch_inode_unpacked *);
123 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
124 struct bch_inode_unpacked *);
126 #define inode_opt_get(_c, _inode, _name) \
127 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
129 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
130 enum inode_opt_id id, u64 v)
133 #define x(_name, ...) \
134 case Inode_opt_##_name: \
135 inode->bi_##_name = v; \
144 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
145 enum inode_opt_id id)
148 #define x(_name, ...) \
149 case Inode_opt_##_name: \
150 return inode->bi_##_name;
158 static inline u8 mode_to_type(umode_t mode)
160 return (mode >> 12) & 15;
163 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
165 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
170 static inline unsigned nlink_bias(umode_t mode)
172 return S_ISDIR(mode) ? 2 : 1;
175 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
177 return bi->bi_flags & BCH_INODE_UNLINKED
179 : bi->bi_nlink + nlink_bias(bi->bi_mode);
182 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
186 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
187 bi->bi_flags &= ~BCH_INODE_UNLINKED;
190 bi->bi_flags |= BCH_INODE_UNLINKED;
194 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
195 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
197 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
198 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
199 struct bch_inode_unpacked *);
201 #endif /* _BCACHEFS_INODE_H */