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