]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/inode.h
Update bcachefs sources to 33a60d9b05 bcachefs: Assorted fixes for clang
[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_trans(struct btree_trans *, subvol_inum,
122                                   struct bch_inode_unpacked *);
123 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
124                             struct bch_inode_unpacked *);
125
126 #define inode_opt_get(_c, _inode, _name)                        \
127         ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
128
129 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
130                                       enum inode_opt_id id, u64 v)
131 {
132         switch (id) {
133 #define x(_name, ...)                                                   \
134         case Inode_opt_##_name:                                         \
135                 inode->bi_##_name = v;                                  \
136                 break;
137         BCH_INODE_OPTS()
138 #undef x
139         default:
140                 BUG();
141         }
142 }
143
144 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
145                                      enum inode_opt_id id)
146 {
147         switch (id) {
148 #define x(_name, ...)                                                   \
149         case Inode_opt_##_name:                                         \
150                 return inode->bi_##_name;
151         BCH_INODE_OPTS()
152 #undef x
153         default:
154                 BUG();
155         }
156 }
157
158 static inline u8 mode_to_type(umode_t mode)
159 {
160         return (mode >> 12) & 15;
161 }
162
163 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
164 {
165         return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
166 }
167
168 /* i_nlink: */
169
170 static inline unsigned nlink_bias(umode_t mode)
171 {
172         return S_ISDIR(mode) ? 2 : 1;
173 }
174
175 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
176 {
177         return bi->bi_flags & BCH_INODE_UNLINKED
178                   ? 0
179                   : bi->bi_nlink + nlink_bias(bi->bi_mode);
180 }
181
182 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
183                                         unsigned nlink)
184 {
185         if (nlink) {
186                 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
187                 bi->bi_flags &= ~BCH_INODE_UNLINKED;
188         } else {
189                 bi->bi_nlink = 0;
190                 bi->bi_flags |= BCH_INODE_UNLINKED;
191         }
192 }
193
194 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
195 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
196
197 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
198 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
199                          struct bch_inode_unpacked *);
200
201 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
202
203 #endif /* _BCACHEFS_INODE_H */