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