]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.h
New upstream release
[bcachefs-tools-debian] / libbcachefs / inode.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
4
5 #include "bkey.h"
6 #include "opts.h"
7
8 extern const char * const bch2_inode_opts[];
9
10 int bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
11 int bch2_inode_v2_invalid(const struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
12 int bch2_inode_v3_invalid(const struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
13 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
14
15 #define bch2_bkey_ops_inode ((struct bkey_ops) {        \
16         .key_invalid    = bch2_inode_invalid,           \
17         .val_to_text    = bch2_inode_to_text,           \
18         .trans_trigger  = bch2_trans_mark_inode,        \
19         .atomic_trigger = bch2_mark_inode,              \
20 })
21
22 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {     \
23         .key_invalid    = bch2_inode_v2_invalid,        \
24         .val_to_text    = bch2_inode_to_text,           \
25         .trans_trigger  = bch2_trans_mark_inode,        \
26         .atomic_trigger = bch2_mark_inode,              \
27 })
28
29 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {     \
30         .key_invalid    = bch2_inode_v3_invalid,        \
31         .val_to_text    = bch2_inode_to_text,           \
32         .trans_trigger  = bch2_trans_mark_inode,        \
33         .atomic_trigger = bch2_mark_inode,              \
34 })
35
36 static inline bool bkey_is_inode(const struct bkey *k)
37 {
38         return  k->type == KEY_TYPE_inode ||
39                 k->type == KEY_TYPE_inode_v2 ||
40                 k->type == KEY_TYPE_inode_v3;
41 }
42
43 int bch2_inode_generation_invalid(const struct bch_fs *, struct bkey_s_c,
44                                   int, struct printbuf *);
45 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
46
47 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {     \
48         .key_invalid    = bch2_inode_generation_invalid,        \
49         .val_to_text    = bch2_inode_generation_to_text,        \
50 })
51
52 #if 0
53 typedef struct {
54         u64                     lo;
55         u32                     hi;
56 } __packed __aligned(4) u96;
57 #endif
58 typedef u64 u96;
59
60 struct bch_inode_unpacked {
61         u64                     bi_inum;
62         u64                     bi_journal_seq;
63         __le64                  bi_hash_seed;
64         u64                     bi_size;
65         u64                     bi_sectors;
66         u64                     bi_version;
67         u32                     bi_flags;
68         u16                     bi_mode;
69
70 #define x(_name, _bits) u##_bits _name;
71         BCH_INODE_FIELDS_v3()
72 #undef  x
73 };
74
75 struct bkey_inode_buf {
76         struct bkey_i_inode_v3  inode;
77
78 #define x(_name, _bits)         + 8 + _bits / 8
79         u8              _pad[0 + BCH_INODE_FIELDS_v3()];
80 #undef  x
81 } __packed __aligned(8);
82
83 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
84 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
85 struct bkey_s_c bch2_inode_to_v3(struct btree_trans *, struct bkey_s_c);
86
87 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
88
89 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
90                     struct bch_inode_unpacked *, subvol_inum, unsigned);
91 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
92                      struct bch_inode_unpacked *);
93
94 void bch2_inode_init_early(struct bch_fs *,
95                            struct bch_inode_unpacked *);
96 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
97                           uid_t, gid_t, umode_t, dev_t,
98                           struct bch_inode_unpacked *);
99 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
100                      uid_t, gid_t, umode_t, dev_t,
101                      struct bch_inode_unpacked *);
102
103 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
104                       struct bch_inode_unpacked *, u32, u64);
105
106 int bch2_inode_rm(struct bch_fs *, subvol_inum);
107
108 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
109                                   struct bch_inode_unpacked *);
110 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
111                             struct bch_inode_unpacked *);
112
113 static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
114 {
115         struct bch_io_opts ret = { 0 };
116
117 #define x(_name, _bits)                                 \
118         if (inode->bi_##_name)                                          \
119                 opt_set(ret, _name, inode->bi_##_name - 1);
120         BCH_INODE_OPTS()
121 #undef x
122         return ret;
123 }
124
125 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
126                                       enum inode_opt_id id, u64 v)
127 {
128         switch (id) {
129 #define x(_name, ...)                                                   \
130         case Inode_opt_##_name:                                         \
131                 inode->bi_##_name = v;                                  \
132                 break;
133         BCH_INODE_OPTS()
134 #undef x
135         default:
136                 BUG();
137         }
138 }
139
140 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
141                                      enum inode_opt_id id)
142 {
143         switch (id) {
144 #define x(_name, ...)                                                   \
145         case Inode_opt_##_name:                                         \
146                 return inode->bi_##_name;
147         BCH_INODE_OPTS()
148 #undef x
149         default:
150                 BUG();
151         }
152 }
153
154 static inline struct bch_io_opts
155 io_opts(struct bch_fs *c, struct bch_inode_unpacked *inode)
156 {
157         struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts);
158
159         bch2_io_opts_apply(&opts, bch2_inode_opts_get(inode));
160         if (opts.nocow)
161                 opts.compression = opts.background_compression = opts.data_checksum = opts.erasure_code;
162         return opts;
163 }
164
165 static inline u8 mode_to_type(umode_t mode)
166 {
167         return (mode >> 12) & 15;
168 }
169
170 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
171 {
172         return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
173 }
174
175 /* i_nlink: */
176
177 static inline unsigned nlink_bias(umode_t mode)
178 {
179         return S_ISDIR(mode) ? 2 : 1;
180 }
181
182 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
183 {
184         return bi->bi_flags & BCH_INODE_UNLINKED
185                   ? 0
186                   : bi->bi_nlink + nlink_bias(bi->bi_mode);
187 }
188
189 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
190                                         unsigned nlink)
191 {
192         if (nlink) {
193                 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
194                 bi->bi_flags &= ~BCH_INODE_UNLINKED;
195         } else {
196                 bi->bi_nlink = 0;
197                 bi->bi_flags |= BCH_INODE_UNLINKED;
198         }
199 }
200
201 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
202 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
203
204 #endif /* _BCACHEFS_INODE_H */