xref: /linux/fs/bcachefs/inode.h (revision c4bbe83d27c2446a033cc0381c3fb6be5e8c41c7)
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 bkey_invalid_flags;
10 extern const char * const bch2_inode_opts[];
11 
12 int bch2_inode_invalid(struct bch_fs *, struct bkey_s_c,
13 		       enum bkey_invalid_flags, struct printbuf *);
14 int bch2_inode_v2_invalid(struct bch_fs *, struct bkey_s_c,
15 			  enum bkey_invalid_flags, struct printbuf *);
16 int bch2_inode_v3_invalid(struct bch_fs *, struct bkey_s_c,
17 			  enum bkey_invalid_flags, struct printbuf *);
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, unsigned);
22 
23 #define bch2_bkey_ops_inode ((struct bkey_ops) {	\
24 	.key_invalid	= bch2_inode_invalid,		\
25 	.val_to_text	= bch2_inode_to_text,		\
26 	.trigger	= bch2_trigger_inode,		\
27 	.min_val_size	= 16,				\
28 })
29 
30 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {	\
31 	.key_invalid	= bch2_inode_v2_invalid,	\
32 	.val_to_text	= bch2_inode_to_text,		\
33 	.trigger	= bch2_trigger_inode,		\
34 	.min_val_size	= 32,				\
35 })
36 
37 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {	\
38 	.key_invalid	= bch2_inode_v3_invalid,	\
39 	.val_to_text	= bch2_inode_to_text,		\
40 	.trigger	= bch2_trigger_inode,		\
41 	.min_val_size	= 48,				\
42 })
43 
44 static inline bool bkey_is_inode(const struct bkey *k)
45 {
46 	return  k->type == KEY_TYPE_inode ||
47 		k->type == KEY_TYPE_inode_v2 ||
48 		k->type == KEY_TYPE_inode_v3;
49 }
50 
51 int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c,
52 				  enum bkey_invalid_flags, struct printbuf *);
53 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
54 
55 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {	\
56 	.key_invalid	= bch2_inode_generation_invalid,	\
57 	.val_to_text	= bch2_inode_generation_to_text,	\
58 	.min_val_size	= 8,					\
59 })
60 
61 #if 0
62 typedef struct {
63 	u64			lo;
64 	u32			hi;
65 } __packed __aligned(4) u96;
66 #endif
67 typedef u64 u96;
68 
69 struct bch_inode_unpacked {
70 	u64			bi_inum;
71 	u64			bi_journal_seq;
72 	__le64			bi_hash_seed;
73 	u64			bi_size;
74 	u64			bi_sectors;
75 	u64			bi_version;
76 	u32			bi_flags;
77 	u16			bi_mode;
78 
79 #define x(_name, _bits)	u##_bits _name;
80 	BCH_INODE_FIELDS_v3()
81 #undef  x
82 };
83 
84 struct bkey_inode_buf {
85 	struct bkey_i_inode_v3	inode;
86 
87 #define x(_name, _bits)		+ 8 + _bits / 8
88 	u8		_pad[0 + BCH_INODE_FIELDS_v3()];
89 #undef  x
90 } __packed __aligned(8);
91 
92 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
93 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
94 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
95 
96 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
97 
98 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
99 		    struct bch_inode_unpacked *, subvol_inum, unsigned);
100 
101 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
102 		     struct bch_inode_unpacked *, enum btree_update_flags);
103 
104 static inline int bch2_inode_write(struct btree_trans *trans,
105 		     struct btree_iter *iter,
106 		     struct bch_inode_unpacked *inode)
107 {
108 	return bch2_inode_write_flags(trans, iter, inode, 0);
109 }
110 
111 void bch2_inode_init_early(struct bch_fs *,
112 			   struct bch_inode_unpacked *);
113 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
114 			  uid_t, gid_t, umode_t, dev_t,
115 			  struct bch_inode_unpacked *);
116 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
117 		     uid_t, gid_t, umode_t, dev_t,
118 		     struct bch_inode_unpacked *);
119 
120 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
121 		      struct bch_inode_unpacked *, u32, u64);
122 
123 int bch2_inode_rm(struct bch_fs *, subvol_inum);
124 
125 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
126 				  subvol_inum,
127 				  struct bch_inode_unpacked *);
128 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
129 				  struct bch_inode_unpacked *);
130 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
131 			    struct bch_inode_unpacked *);
132 
133 #define inode_opt_get(_c, _inode, _name)			\
134 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
135 
136 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
137 				      enum inode_opt_id id, u64 v)
138 {
139 	switch (id) {
140 #define x(_name, ...)							\
141 	case Inode_opt_##_name:						\
142 		inode->bi_##_name = v;					\
143 		break;
144 	BCH_INODE_OPTS()
145 #undef x
146 	default:
147 		BUG();
148 	}
149 }
150 
151 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
152 				     enum inode_opt_id id)
153 {
154 	switch (id) {
155 #define x(_name, ...)							\
156 	case Inode_opt_##_name:						\
157 		return inode->bi_##_name;
158 	BCH_INODE_OPTS()
159 #undef x
160 	default:
161 		BUG();
162 	}
163 }
164 
165 static inline u8 mode_to_type(umode_t mode)
166 {
167 	return (mode >> 12) & 15;
168 }
169 
170 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
171 {
172 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
173 }
174 
175 /* i_nlink: */
176 
177 static inline unsigned nlink_bias(umode_t mode)
178 {
179 	return S_ISDIR(mode) ? 2 : 1;
180 }
181 
182 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
183 {
184 	return bi->bi_flags & BCH_INODE_unlinked
185 		  ? 0
186 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
187 }
188 
189 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
190 					unsigned nlink)
191 {
192 	if (nlink) {
193 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
194 		bi->bi_flags &= ~BCH_INODE_unlinked;
195 	} else {
196 		bi->bi_nlink = 0;
197 		bi->bi_flags |= BCH_INODE_unlinked;
198 	}
199 }
200 
201 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
202 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
203 
204 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
205 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
206 			 struct bch_inode_unpacked *);
207 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
208 
209 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
210 int bch2_delete_dead_inodes(struct bch_fs *);
211 
212 #endif /* _BCACHEFS_INODE_H */
213