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