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