xref: /linux/fs/bcachefs/inode.h (revision 5ce42b5de461c3154f61a023b191dd6b77ee66c0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
4 
5 #include "bkey.h"
6 #include "bkey_methods.h"
7 #include "opts.h"
8 
9 enum bch_validate_flags;
10 extern const char * const bch2_inode_opts[];
11 
12 int bch2_inode_validate(struct bch_fs *, struct bkey_s_c,
13 		       enum bch_validate_flags);
14 int bch2_inode_v2_validate(struct bch_fs *, struct bkey_s_c,
15 			  enum bch_validate_flags);
16 int bch2_inode_v3_validate(struct bch_fs *, struct bkey_s_c,
17 			  enum bch_validate_flags);
18 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
19 
20 int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
21 		       struct bkey_s_c, struct bkey_s,
22 		       enum btree_iter_update_trigger_flags);
23 
24 #define bch2_bkey_ops_inode ((struct bkey_ops) {	\
25 	.key_validate	= bch2_inode_validate,		\
26 	.val_to_text	= bch2_inode_to_text,		\
27 	.trigger	= bch2_trigger_inode,		\
28 	.min_val_size	= 16,				\
29 })
30 
31 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {	\
32 	.key_validate	= bch2_inode_v2_validate,	\
33 	.val_to_text	= bch2_inode_to_text,		\
34 	.trigger	= bch2_trigger_inode,		\
35 	.min_val_size	= 32,				\
36 })
37 
38 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {	\
39 	.key_validate	= bch2_inode_v3_validate,	\
40 	.val_to_text	= bch2_inode_to_text,		\
41 	.trigger	= bch2_trigger_inode,		\
42 	.min_val_size	= 48,				\
43 })
44 
45 static inline bool bkey_is_inode(const struct bkey *k)
46 {
47 	return  k->type == KEY_TYPE_inode ||
48 		k->type == KEY_TYPE_inode_v2 ||
49 		k->type == KEY_TYPE_inode_v3;
50 }
51 
52 int bch2_inode_generation_validate(struct bch_fs *, struct bkey_s_c,
53 				  enum bch_validate_flags);
54 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
55 
56 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {	\
57 	.key_validate	= bch2_inode_generation_validate,	\
58 	.val_to_text	= bch2_inode_generation_to_text,	\
59 	.min_val_size	= 8,					\
60 })
61 
62 #if 0
63 typedef struct {
64 	u64			lo;
65 	u32			hi;
66 } __packed __aligned(4) u96;
67 #endif
68 typedef u64 u96;
69 
70 struct bch_inode_unpacked {
71 	u64			bi_inum;
72 	u32			bi_snapshot;
73 	u64			bi_journal_seq;
74 	__le64			bi_hash_seed;
75 	u64			bi_size;
76 	u64			bi_sectors;
77 	u64			bi_version;
78 	u32			bi_flags;
79 	u16			bi_mode;
80 
81 #define x(_name, _bits)	u##_bits _name;
82 	BCH_INODE_FIELDS_v3()
83 #undef  x
84 };
85 
86 struct bkey_inode_buf {
87 	struct bkey_i_inode_v3	inode;
88 
89 #define x(_name, _bits)		+ 8 + _bits / 8
90 	u8		_pad[0 + BCH_INODE_FIELDS_v3()];
91 #undef  x
92 };
93 
94 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
95 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
96 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
97 
98 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
99 
100 int __bch2_inode_peek(struct btree_trans *, struct btree_iter *,
101 		      struct bch_inode_unpacked *, subvol_inum, unsigned, bool);
102 
103 static inline int bch2_inode_peek_nowarn(struct btree_trans *trans,
104 					 struct btree_iter *iter,
105 					 struct bch_inode_unpacked *inode,
106 					 subvol_inum inum, unsigned flags)
107 {
108 	return __bch2_inode_peek(trans, iter, inode, inum, flags, false);
109 }
110 
111 static inline int bch2_inode_peek(struct btree_trans *trans,
112 				  struct btree_iter *iter,
113 				  struct bch_inode_unpacked *inode,
114 				  subvol_inum inum, unsigned flags)
115 {
116 	return __bch2_inode_peek(trans, iter, inode, inum, flags, true);
117 	int ret = bch2_inode_peek_nowarn(trans, iter, inode, inum, flags);
118 	return ret;
119 }
120 
121 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
122 		     struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
123 
124 static inline int bch2_inode_write(struct btree_trans *trans,
125 		     struct btree_iter *iter,
126 		     struct bch_inode_unpacked *inode)
127 {
128 	return bch2_inode_write_flags(trans, iter, inode, 0);
129 }
130 
131 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
132 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
133 
134 void bch2_inode_init_early(struct bch_fs *,
135 			   struct bch_inode_unpacked *);
136 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
137 			  uid_t, gid_t, umode_t, dev_t,
138 			  struct bch_inode_unpacked *);
139 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
140 		     uid_t, gid_t, umode_t, dev_t,
141 		     struct bch_inode_unpacked *);
142 
143 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
144 		      struct bch_inode_unpacked *, u32, u64);
145 
146 int bch2_inode_rm(struct bch_fs *, subvol_inum);
147 
148 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
149 				  subvol_inum,
150 				  struct bch_inode_unpacked *);
151 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
152 				  struct bch_inode_unpacked *);
153 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
154 			    struct bch_inode_unpacked *);
155 
156 #define inode_opt_get(_c, _inode, _name)			\
157 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
158 
159 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
160 				      enum inode_opt_id id, u64 v)
161 {
162 	switch (id) {
163 #define x(_name, ...)							\
164 	case Inode_opt_##_name:						\
165 		inode->bi_##_name = v;					\
166 		break;
167 	BCH_INODE_OPTS()
168 #undef x
169 	default:
170 		BUG();
171 	}
172 }
173 
174 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
175 				     enum inode_opt_id id)
176 {
177 	switch (id) {
178 #define x(_name, ...)							\
179 	case Inode_opt_##_name:						\
180 		return inode->bi_##_name;
181 	BCH_INODE_OPTS()
182 #undef x
183 	default:
184 		BUG();
185 	}
186 }
187 
188 static inline u8 mode_to_type(umode_t mode)
189 {
190 	return (mode >> 12) & 15;
191 }
192 
193 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
194 {
195 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
196 }
197 
198 static inline u32 bch2_inode_flags(struct bkey_s_c k)
199 {
200 	switch (k.k->type) {
201 	case KEY_TYPE_inode:
202 		return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
203 	case KEY_TYPE_inode_v2:
204 		return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
205 	case KEY_TYPE_inode_v3:
206 		return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
207 	default:
208 		return 0;
209 	}
210 }
211 
212 /* i_nlink: */
213 
214 static inline unsigned nlink_bias(umode_t mode)
215 {
216 	return S_ISDIR(mode) ? 2 : 1;
217 }
218 
219 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
220 {
221 	return bi->bi_flags & BCH_INODE_unlinked
222 		  ? 0
223 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
224 }
225 
226 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
227 					unsigned nlink)
228 {
229 	if (nlink) {
230 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
231 		bi->bi_flags &= ~BCH_INODE_unlinked;
232 	} else {
233 		bi->bi_nlink = 0;
234 		bi->bi_flags |= BCH_INODE_unlinked;
235 	}
236 }
237 
238 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
239 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
240 
241 static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode)
242 {
243 	bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset;
244 
245 	return S_ISDIR(inode->bi_mode) ||
246 		(!inode->bi_nlink && inode_has_bp);
247 }
248 
249 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
250 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
251 			 struct bch_inode_unpacked *);
252 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
253 
254 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
255 int bch2_delete_dead_inodes(struct bch_fs *);
256 
257 #endif /* _BCACHEFS_INODE_H */
258