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