]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.h
Update bcachefs sources to 1336a995cbc3 bcachefs: Silence transaction restart error...
[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 enum bkey_invalid_flags;
9 extern const char * const bch2_inode_opts[];
10
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);
18
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);
23
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,              \
29         .min_val_size   = 16,                           \
30 })
31
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,              \
37         .min_val_size   = 32,                           \
38 })
39
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,              \
45         .min_val_size   = 48,                           \
46 })
47
48 static inline bool bkey_is_inode(const struct bkey *k)
49 {
50         return  k->type == KEY_TYPE_inode ||
51                 k->type == KEY_TYPE_inode_v2 ||
52                 k->type == KEY_TYPE_inode_v3;
53 }
54
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);
58
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,        \
62         .min_val_size   = 8,                                    \
63 })
64
65 #if 0
66 typedef struct {
67         u64                     lo;
68         u32                     hi;
69 } __packed __aligned(4) u96;
70 #endif
71 typedef u64 u96;
72
73 struct bch_inode_unpacked {
74         u64                     bi_inum;
75         u64                     bi_journal_seq;
76         __le64                  bi_hash_seed;
77         u64                     bi_size;
78         u64                     bi_sectors;
79         u64                     bi_version;
80         u32                     bi_flags;
81         u16                     bi_mode;
82
83 #define x(_name, _bits) u##_bits _name;
84         BCH_INODE_FIELDS_v3()
85 #undef  x
86 };
87
88 struct bkey_inode_buf {
89         struct bkey_i_inode_v3  inode;
90
91 #define x(_name, _bits)         + 8 + _bits / 8
92         u8              _pad[0 + BCH_INODE_FIELDS_v3()];
93 #undef  x
94 } __packed __aligned(8);
95
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 *);
99
100 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
101
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 *);
106
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 *);
115
116 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
117                       struct bch_inode_unpacked *, u32, u64);
118
119 int bch2_inode_rm(struct bch_fs *, subvol_inum);
120
121 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
122                                   subvol_inum,
123                                   struct bch_inode_unpacked *);
124 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
125                                   struct bch_inode_unpacked *);
126 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
127                             struct bch_inode_unpacked *);
128
129 #define inode_opt_get(_c, _inode, _name)                        \
130         ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
131
132 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
133                                       enum inode_opt_id id, u64 v)
134 {
135         switch (id) {
136 #define x(_name, ...)                                                   \
137         case Inode_opt_##_name:                                         \
138                 inode->bi_##_name = v;                                  \
139                 break;
140         BCH_INODE_OPTS()
141 #undef x
142         default:
143                 BUG();
144         }
145 }
146
147 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
148                                      enum inode_opt_id id)
149 {
150         switch (id) {
151 #define x(_name, ...)                                                   \
152         case Inode_opt_##_name:                                         \
153                 return inode->bi_##_name;
154         BCH_INODE_OPTS()
155 #undef x
156         default:
157                 BUG();
158         }
159 }
160
161 static inline u8 mode_to_type(umode_t mode)
162 {
163         return (mode >> 12) & 15;
164 }
165
166 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
167 {
168         return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
169 }
170
171 /* i_nlink: */
172
173 static inline unsigned nlink_bias(umode_t mode)
174 {
175         return S_ISDIR(mode) ? 2 : 1;
176 }
177
178 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
179 {
180         return bi->bi_flags & BCH_INODE_UNLINKED
181                   ? 0
182                   : bi->bi_nlink + nlink_bias(bi->bi_mode);
183 }
184
185 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
186                                         unsigned nlink)
187 {
188         if (nlink) {
189                 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
190                 bi->bi_flags &= ~BCH_INODE_UNLINKED;
191         } else {
192                 bi->bi_nlink = 0;
193                 bi->bi_flags |= BCH_INODE_UNLINKED;
194         }
195 }
196
197 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
198 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
199
200 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
201 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
202                          struct bch_inode_unpacked *);
203
204 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
205 int bch2_delete_dead_inodes(struct bch_fs *);
206
207 #endif /* _BCACHEFS_INODE_H */