1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
7 extern const char * const bch2_inode_opts[];
9 const char *bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c);
10 const char *bch2_inode_v2_invalid(const struct bch_fs *, struct bkey_s_c);
11 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
13 #define bch2_bkey_ops_inode (struct bkey_ops) { \
14 .key_invalid = bch2_inode_invalid, \
15 .val_to_text = bch2_inode_to_text, \
18 #define bch2_bkey_ops_inode_v2 (struct bkey_ops) { \
19 .key_invalid = bch2_inode_v2_invalid, \
20 .val_to_text = bch2_inode_to_text, \
23 static inline bool bkey_is_inode(const struct bkey *k)
25 return k->type == KEY_TYPE_inode ||
26 k->type == KEY_TYPE_inode_v2;
29 const char *bch2_inode_generation_invalid(const struct bch_fs *,
31 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *,
34 #define bch2_bkey_ops_inode_generation (struct bkey_ops) { \
35 .key_invalid = bch2_inode_generation_invalid, \
36 .val_to_text = bch2_inode_generation_to_text, \
43 } __packed __aligned(4) u96;
47 struct bch_inode_unpacked {
54 #define x(_name, _bits) u##_bits _name;
59 struct bkey_inode_buf {
60 struct bkey_i_inode_v2 inode;
62 #define x(_name, _bits) + 8 + _bits / 8
63 u8 _pad[0 + BCH_INODE_FIELDS()];
65 } __attribute__((packed, aligned(8)));
67 void bch2_inode_pack(struct bch_fs *, struct bkey_inode_buf *,
68 const struct bch_inode_unpacked *);
69 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
71 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
73 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
74 struct bch_inode_unpacked *, subvol_inum, unsigned);
75 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
76 struct bch_inode_unpacked *);
78 void bch2_inode_init_early(struct bch_fs *,
79 struct bch_inode_unpacked *);
80 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
81 uid_t, gid_t, umode_t, dev_t,
82 struct bch_inode_unpacked *);
83 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
84 uid_t, gid_t, umode_t, dev_t,
85 struct bch_inode_unpacked *);
87 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
88 struct bch_inode_unpacked *, u32, u64);
90 int bch2_inode_rm(struct bch_fs *, subvol_inum);
92 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
93 struct bch_inode_unpacked *);
94 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
95 struct bch_inode_unpacked *);
97 static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
99 struct bch_io_opts ret = { 0 };
101 #define x(_name, _bits) \
102 if (inode->bi_##_name) \
103 opt_set(ret, _name, inode->bi_##_name - 1);
109 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
110 enum inode_opt_id id, u64 v)
113 #define x(_name, ...) \
114 case Inode_opt_##_name: \
115 inode->bi_##_name = v; \
124 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
125 enum inode_opt_id id)
128 #define x(_name, ...) \
129 case Inode_opt_##_name: \
130 return inode->bi_##_name;
138 static inline struct bch_io_opts
139 io_opts(struct bch_fs *c, struct bch_inode_unpacked *inode)
141 struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts);
143 bch2_io_opts_apply(&opts, bch2_inode_opts_get(inode));
147 static inline u8 mode_to_type(umode_t mode)
149 return (mode >> 12) & 15;
152 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
154 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
159 static inline unsigned nlink_bias(umode_t mode)
161 return S_ISDIR(mode) ? 2 : 1;
164 static inline void bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
166 if (bi->bi_flags & BCH_INODE_UNLINKED)
167 bi->bi_flags &= ~BCH_INODE_UNLINKED;
172 static inline void bch2_inode_nlink_dec(struct bch_inode_unpacked *bi)
174 BUG_ON(bi->bi_flags & BCH_INODE_UNLINKED);
178 bi->bi_flags |= BCH_INODE_UNLINKED;
181 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
183 return bi->bi_flags & BCH_INODE_UNLINKED
185 : bi->bi_nlink + nlink_bias(bi->bi_mode);
188 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
192 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
193 bi->bi_flags &= ~BCH_INODE_UNLINKED;
196 bi->bi_flags |= BCH_INODE_UNLINKED;
200 #endif /* _BCACHEFS_INODE_H */