]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.h
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[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_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
21                           struct bkey_s_c, struct bkey_s, unsigned);
22
23 #define bch2_bkey_ops_inode ((struct bkey_ops) {        \
24         .key_invalid    = bch2_inode_invalid,           \
25         .val_to_text    = bch2_inode_to_text,           \
26         .trigger        = bch2_trigger_inode,           \
27         .min_val_size   = 16,                           \
28 })
29
30 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {     \
31         .key_invalid    = bch2_inode_v2_invalid,        \
32         .val_to_text    = bch2_inode_to_text,           \
33         .trigger        = bch2_trigger_inode,           \
34         .min_val_size   = 32,                           \
35 })
36
37 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {     \
38         .key_invalid    = bch2_inode_v3_invalid,        \
39         .val_to_text    = bch2_inode_to_text,           \
40         .trigger        = bch2_trigger_inode,           \
41         .min_val_size   = 48,                           \
42 })
43
44 static inline bool bkey_is_inode(const struct bkey *k)
45 {
46         return  k->type == KEY_TYPE_inode ||
47                 k->type == KEY_TYPE_inode_v2 ||
48                 k->type == KEY_TYPE_inode_v3;
49 }
50
51 int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c,
52                                   enum bkey_invalid_flags, struct printbuf *);
53 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
54
55 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {     \
56         .key_invalid    = bch2_inode_generation_invalid,        \
57         .val_to_text    = bch2_inode_generation_to_text,        \
58         .min_val_size   = 8,                                    \
59 })
60
61 #if 0
62 typedef struct {
63         u64                     lo;
64         u32                     hi;
65 } __packed __aligned(4) u96;
66 #endif
67 typedef u64 u96;
68
69 struct bch_inode_unpacked {
70         u64                     bi_inum;
71         u64                     bi_journal_seq;
72         __le64                  bi_hash_seed;
73         u64                     bi_size;
74         u64                     bi_sectors;
75         u64                     bi_version;
76         u32                     bi_flags;
77         u16                     bi_mode;
78
79 #define x(_name, _bits) u##_bits _name;
80         BCH_INODE_FIELDS_v3()
81 #undef  x
82 };
83
84 struct bkey_inode_buf {
85         struct bkey_i_inode_v3  inode;
86
87 #define x(_name, _bits)         + 8 + _bits / 8
88         u8              _pad[0 + BCH_INODE_FIELDS_v3()];
89 #undef  x
90 } __packed __aligned(8);
91
92 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
93 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
94 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
95
96 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
97
98 int bch2_inode_peek_nowarn(struct btree_trans *, struct btree_iter *,
99                     struct bch_inode_unpacked *, subvol_inum, unsigned);
100 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
101                     struct bch_inode_unpacked *, subvol_inum, unsigned);
102
103 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
104                      struct bch_inode_unpacked *, enum btree_update_flags);
105
106 static inline int bch2_inode_write(struct btree_trans *trans,
107                      struct btree_iter *iter,
108                      struct bch_inode_unpacked *inode)
109 {
110         return bch2_inode_write_flags(trans, iter, inode, 0);
111 }
112
113 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
114 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
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 static inline u32 bch2_inode_flags(struct bkey_s_c k)
181 {
182         switch (k.k->type) {
183         case KEY_TYPE_inode:
184                 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
185         case KEY_TYPE_inode_v2:
186                 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
187         case KEY_TYPE_inode_v3:
188                 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
189         default:
190                 return 0;
191         }
192 }
193
194 /* i_nlink: */
195
196 static inline unsigned nlink_bias(umode_t mode)
197 {
198         return S_ISDIR(mode) ? 2 : 1;
199 }
200
201 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
202 {
203         return bi->bi_flags & BCH_INODE_unlinked
204                   ? 0
205                   : bi->bi_nlink + nlink_bias(bi->bi_mode);
206 }
207
208 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
209                                         unsigned nlink)
210 {
211         if (nlink) {
212                 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
213                 bi->bi_flags &= ~BCH_INODE_unlinked;
214         } else {
215                 bi->bi_nlink = 0;
216                 bi->bi_flags |= BCH_INODE_unlinked;
217         }
218 }
219
220 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
221 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
222
223 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
224 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
225                          struct bch_inode_unpacked *);
226 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
227
228 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
229 int bch2_delete_dead_inodes(struct bch_fs *);
230
231 #endif /* _BCACHEFS_INODE_H */