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