xref: /linux/fs/bcachefs/inode.h (revision 870b7fdc660b38c4e1bd8bf48e62aa352ddf8f42)
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_nowarn(struct btree_trans *, struct btree_iter *,
101 		    struct bch_inode_unpacked *, subvol_inum, unsigned);
102 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
103 		    struct bch_inode_unpacked *, subvol_inum, unsigned);
104 
105 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
106 		     struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
107 
108 static inline int bch2_inode_write(struct btree_trans *trans,
109 		     struct btree_iter *iter,
110 		     struct bch_inode_unpacked *inode)
111 {
112 	return bch2_inode_write_flags(trans, iter, inode, 0);
113 }
114 
115 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
116 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
117 
118 void bch2_inode_init_early(struct bch_fs *,
119 			   struct bch_inode_unpacked *);
120 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
121 			  uid_t, gid_t, umode_t, dev_t,
122 			  struct bch_inode_unpacked *);
123 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
124 		     uid_t, gid_t, umode_t, dev_t,
125 		     struct bch_inode_unpacked *);
126 
127 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
128 		      struct bch_inode_unpacked *, u32, u64);
129 
130 int bch2_inode_rm(struct bch_fs *, subvol_inum);
131 
132 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
133 				  subvol_inum,
134 				  struct bch_inode_unpacked *);
135 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
136 				  struct bch_inode_unpacked *);
137 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
138 			    struct bch_inode_unpacked *);
139 
140 #define inode_opt_get(_c, _inode, _name)			\
141 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
142 
143 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
144 				      enum inode_opt_id id, u64 v)
145 {
146 	switch (id) {
147 #define x(_name, ...)							\
148 	case Inode_opt_##_name:						\
149 		inode->bi_##_name = v;					\
150 		break;
151 	BCH_INODE_OPTS()
152 #undef x
153 	default:
154 		BUG();
155 	}
156 }
157 
158 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
159 				     enum inode_opt_id id)
160 {
161 	switch (id) {
162 #define x(_name, ...)							\
163 	case Inode_opt_##_name:						\
164 		return inode->bi_##_name;
165 	BCH_INODE_OPTS()
166 #undef x
167 	default:
168 		BUG();
169 	}
170 }
171 
172 static inline u8 mode_to_type(umode_t mode)
173 {
174 	return (mode >> 12) & 15;
175 }
176 
177 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
178 {
179 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
180 }
181 
182 static inline u32 bch2_inode_flags(struct bkey_s_c k)
183 {
184 	switch (k.k->type) {
185 	case KEY_TYPE_inode:
186 		return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
187 	case KEY_TYPE_inode_v2:
188 		return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
189 	case KEY_TYPE_inode_v3:
190 		return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
191 	default:
192 		return 0;
193 	}
194 }
195 
196 /* i_nlink: */
197 
198 static inline unsigned nlink_bias(umode_t mode)
199 {
200 	return S_ISDIR(mode) ? 2 : 1;
201 }
202 
203 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
204 {
205 	return bi->bi_flags & BCH_INODE_unlinked
206 		  ? 0
207 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
208 }
209 
210 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
211 					unsigned nlink)
212 {
213 	if (nlink) {
214 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
215 		bi->bi_flags &= ~BCH_INODE_unlinked;
216 	} else {
217 		bi->bi_nlink = 0;
218 		bi->bi_flags |= BCH_INODE_unlinked;
219 	}
220 }
221 
222 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
223 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
224 
225 static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode)
226 {
227 	bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset;
228 
229 	return S_ISDIR(inode->bi_mode) ||
230 		(!inode->bi_nlink && inode_has_bp);
231 }
232 
233 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
234 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
235 			 struct bch_inode_unpacked *);
236 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
237 
238 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
239 int bch2_delete_dead_inodes(struct bch_fs *);
240 
241 #endif /* _BCACHEFS_INODE_H */
242