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