xref: /linux/fs/bcachefs/inode.h (revision dde8cb11645b7d95766dfd13f273facde27923a9)
11c6fdbd8SKent Overstreet /* SPDX-License-Identifier: GPL-2.0 */
21c6fdbd8SKent Overstreet #ifndef _BCACHEFS_INODE_H
31c6fdbd8SKent Overstreet #define _BCACHEFS_INODE_H
41c6fdbd8SKent Overstreet 
577671e8fSKent Overstreet #include "bkey.h"
61c6fdbd8SKent Overstreet #include "opts.h"
71c6fdbd8SKent Overstreet 
88726dc93SKent Overstreet enum bkey_invalid_flags;
94d269918SKent Overstreet extern const char * const bch2_inode_opts[];
104d269918SKent Overstreet 
118726dc93SKent Overstreet int bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c,
128726dc93SKent Overstreet 		       enum bkey_invalid_flags, struct printbuf *);
138726dc93SKent Overstreet int bch2_inode_v2_invalid(const struct bch_fs *, struct bkey_s_c,
148726dc93SKent Overstreet 			  enum bkey_invalid_flags, struct printbuf *);
158726dc93SKent Overstreet int bch2_inode_v3_invalid(const struct bch_fs *, struct bkey_s_c,
168726dc93SKent Overstreet 			  enum bkey_invalid_flags, struct printbuf *);
17319f9ac3SKent Overstreet void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
181c6fdbd8SKent Overstreet 
194dc5bb9aSKent Overstreet int bch2_trans_mark_inode(struct btree_trans *, enum btree_id, unsigned,
204dc5bb9aSKent Overstreet 			  struct bkey_s_c, struct bkey_i *, unsigned);
214dc5bb9aSKent Overstreet int bch2_mark_inode(struct btree_trans *, enum btree_id, unsigned,
224dc5bb9aSKent Overstreet 		    struct bkey_s_c, struct bkey_s_c, unsigned);
234dc5bb9aSKent Overstreet 
24a1019576SKent Overstreet #define bch2_bkey_ops_inode ((struct bkey_ops) {	\
251c6fdbd8SKent Overstreet 	.key_invalid	= bch2_inode_invalid,		\
261c6fdbd8SKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
27880e2275SKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
28880e2275SKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
29174f930bSKent Overstreet 	.min_val_size	= 16,				\
30a1019576SKent Overstreet })
311c6fdbd8SKent Overstreet 
32a1019576SKent Overstreet #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {	\
333e52c222SKent Overstreet 	.key_invalid	= bch2_inode_v2_invalid,	\
343e52c222SKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
35880e2275SKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
36880e2275SKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
37174f930bSKent Overstreet 	.min_val_size	= 32,				\
38a1019576SKent Overstreet })
393e52c222SKent Overstreet 
408dd69d9fSKent Overstreet #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {	\
418dd69d9fSKent Overstreet 	.key_invalid	= bch2_inode_v3_invalid,	\
428dd69d9fSKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
438dd69d9fSKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
448dd69d9fSKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
45174f930bSKent Overstreet 	.min_val_size	= 48,				\
468dd69d9fSKent Overstreet })
478dd69d9fSKent Overstreet 
483e52c222SKent Overstreet static inline bool bkey_is_inode(const struct bkey *k)
493e52c222SKent Overstreet {
503e52c222SKent Overstreet 	return  k->type == KEY_TYPE_inode ||
518dd69d9fSKent Overstreet 		k->type == KEY_TYPE_inode_v2 ||
528dd69d9fSKent Overstreet 		k->type == KEY_TYPE_inode_v3;
533e52c222SKent Overstreet }
543e52c222SKent Overstreet 
55275c8426SKent Overstreet int bch2_inode_generation_invalid(const struct bch_fs *, struct bkey_s_c,
568726dc93SKent Overstreet 				  enum bkey_invalid_flags, struct printbuf *);
57f0ac7df2SKent Overstreet void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
5826609b61SKent Overstreet 
59a1019576SKent Overstreet #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {	\
6026609b61SKent Overstreet 	.key_invalid	= bch2_inode_generation_invalid,	\
6126609b61SKent Overstreet 	.val_to_text	= bch2_inode_generation_to_text,	\
62174f930bSKent Overstreet 	.min_val_size	= 8,					\
63a1019576SKent Overstreet })
6426609b61SKent Overstreet 
65a3e72262SKent Overstreet #if 0
66a3e72262SKent Overstreet typedef struct {
67a3e72262SKent Overstreet 	u64			lo;
68a3e72262SKent Overstreet 	u32			hi;
69a3e72262SKent Overstreet } __packed __aligned(4) u96;
70a3e72262SKent Overstreet #endif
71a3e72262SKent Overstreet typedef u64 u96;
72a3e72262SKent Overstreet 
731c6fdbd8SKent Overstreet struct bch_inode_unpacked {
741c6fdbd8SKent Overstreet 	u64			bi_inum;
753e52c222SKent Overstreet 	u64			bi_journal_seq;
761c6fdbd8SKent Overstreet 	__le64			bi_hash_seed;
778dd69d9fSKent Overstreet 	u64			bi_size;
788dd69d9fSKent Overstreet 	u64			bi_sectors;
798dd69d9fSKent Overstreet 	u64			bi_version;
801c6fdbd8SKent Overstreet 	u32			bi_flags;
811c6fdbd8SKent Overstreet 	u16			bi_mode;
821c6fdbd8SKent Overstreet 
83a3e70fb2SKent Overstreet #define x(_name, _bits)	u##_bits _name;
848dd69d9fSKent Overstreet 	BCH_INODE_FIELDS_v3()
85a3e70fb2SKent Overstreet #undef  x
861c6fdbd8SKent Overstreet };
871c6fdbd8SKent Overstreet 
881c6fdbd8SKent Overstreet struct bkey_inode_buf {
898dd69d9fSKent Overstreet 	struct bkey_i_inode_v3	inode;
901c6fdbd8SKent Overstreet 
91a3e70fb2SKent Overstreet #define x(_name, _bits)		+ 8 + _bits / 8
928dd69d9fSKent Overstreet 	u8		_pad[0 + BCH_INODE_FIELDS_v3()];
93a3e70fb2SKent Overstreet #undef  x
94fd0c7679SKent Overstreet } __packed __aligned(8);
951c6fdbd8SKent Overstreet 
968dd69d9fSKent Overstreet void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
973e52c222SKent Overstreet int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
988dd69d9fSKent Overstreet struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
991c6fdbd8SKent Overstreet 
1000f120eacSKent Overstreet void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
1010f120eacSKent Overstreet 
10267e0dd8fSKent Overstreet int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
1036fed42bbSKent Overstreet 		    struct bch_inode_unpacked *, subvol_inum, unsigned);
10458677a1dSKent Overstreet int bch2_inode_write(struct btree_trans *, struct btree_iter *,
10558677a1dSKent Overstreet 		     struct bch_inode_unpacked *);
10658677a1dSKent Overstreet 
10796385742SKent Overstreet void bch2_inode_init_early(struct bch_fs *,
10896385742SKent Overstreet 			   struct bch_inode_unpacked *);
10996385742SKent Overstreet void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
11096385742SKent Overstreet 			  uid_t, gid_t, umode_t, dev_t,
11196385742SKent Overstreet 			  struct bch_inode_unpacked *);
1121c6fdbd8SKent Overstreet void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
1131c6fdbd8SKent Overstreet 		     uid_t, gid_t, umode_t, dev_t,
1141c6fdbd8SKent Overstreet 		     struct bch_inode_unpacked *);
1151c6fdbd8SKent Overstreet 
11667e0dd8fSKent Overstreet int bch2_inode_create(struct btree_trans *, struct btree_iter *,
117bff796aeSKent Overstreet 		      struct bch_inode_unpacked *, u32, u64);
1181c6fdbd8SKent Overstreet 
1197c8f6f98SKent Overstreet int bch2_inode_rm(struct bch_fs *, subvol_inum);
1201c6fdbd8SKent Overstreet 
12132b26e8cSKent Overstreet int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
12232b26e8cSKent Overstreet 				  struct bch_inode_unpacked *);
1236fed42bbSKent Overstreet int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
1246fed42bbSKent Overstreet 			    struct bch_inode_unpacked *);
1251c6fdbd8SKent Overstreet 
12601ad6737SKent Overstreet #define inode_opt_get(_c, _inode, _name)			\
12701ad6737SKent Overstreet 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
1281c6fdbd8SKent Overstreet 
1294d269918SKent Overstreet static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
1304d269918SKent Overstreet 				      enum inode_opt_id id, u64 v)
1311c6fdbd8SKent Overstreet {
1321c6fdbd8SKent Overstreet 	switch (id) {
133a3e70fb2SKent Overstreet #define x(_name, ...)							\
1344d269918SKent Overstreet 	case Inode_opt_##_name:						\
1351c6fdbd8SKent Overstreet 		inode->bi_##_name = v;					\
1361c6fdbd8SKent Overstreet 		break;
1371c6fdbd8SKent Overstreet 	BCH_INODE_OPTS()
138a3e70fb2SKent Overstreet #undef x
1391c6fdbd8SKent Overstreet 	default:
1401c6fdbd8SKent Overstreet 		BUG();
1411c6fdbd8SKent Overstreet 	}
1421c6fdbd8SKent Overstreet }
1431c6fdbd8SKent Overstreet 
1444d269918SKent Overstreet static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
1454d269918SKent Overstreet 				     enum inode_opt_id id)
1461c6fdbd8SKent Overstreet {
1474d269918SKent Overstreet 	switch (id) {
1484d269918SKent Overstreet #define x(_name, ...)							\
1494d269918SKent Overstreet 	case Inode_opt_##_name:						\
1504d269918SKent Overstreet 		return inode->bi_##_name;
1514d269918SKent Overstreet 	BCH_INODE_OPTS()
1524d269918SKent Overstreet #undef x
1534d269918SKent Overstreet 	default:
1544d269918SKent Overstreet 		BUG();
1551c6fdbd8SKent Overstreet 	}
1561c6fdbd8SKent Overstreet }
1571c6fdbd8SKent Overstreet 
15896385742SKent Overstreet static inline u8 mode_to_type(umode_t mode)
15996385742SKent Overstreet {
16096385742SKent Overstreet 	return (mode >> 12) & 15;
16196385742SKent Overstreet }
16296385742SKent Overstreet 
163285b181aSKent Overstreet static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
164285b181aSKent Overstreet {
165285b181aSKent Overstreet 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
166285b181aSKent Overstreet }
167285b181aSKent Overstreet 
168b43a0f60SKent Overstreet /* i_nlink: */
169b43a0f60SKent Overstreet 
170b43a0f60SKent Overstreet static inline unsigned nlink_bias(umode_t mode)
171b43a0f60SKent Overstreet {
172b43a0f60SKent Overstreet 	return S_ISDIR(mode) ? 2 : 1;
173b43a0f60SKent Overstreet }
174b43a0f60SKent Overstreet 
175b43a0f60SKent Overstreet static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
176b43a0f60SKent Overstreet {
177b43a0f60SKent Overstreet 	return bi->bi_flags & BCH_INODE_UNLINKED
178b43a0f60SKent Overstreet 		  ? 0
179b43a0f60SKent Overstreet 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
180b43a0f60SKent Overstreet }
181b43a0f60SKent Overstreet 
182b43a0f60SKent Overstreet static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
183b43a0f60SKent Overstreet 					unsigned nlink)
184b43a0f60SKent Overstreet {
185b43a0f60SKent Overstreet 	if (nlink) {
186b43a0f60SKent Overstreet 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
187b43a0f60SKent Overstreet 		bi->bi_flags &= ~BCH_INODE_UNLINKED;
188b43a0f60SKent Overstreet 	} else {
189b43a0f60SKent Overstreet 		bi->bi_nlink = 0;
190b43a0f60SKent Overstreet 		bi->bi_flags |= BCH_INODE_UNLINKED;
191b43a0f60SKent Overstreet 	}
192b43a0f60SKent Overstreet }
193b43a0f60SKent Overstreet 
194962ad1a7SKent Overstreet int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
195962ad1a7SKent Overstreet void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
196962ad1a7SKent Overstreet 
197abb936fbSKent Overstreet struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
19801ad6737SKent Overstreet void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
19901ad6737SKent Overstreet 			 struct bch_inode_unpacked *);
200abb936fbSKent Overstreet 
2017904c82cSKent Overstreet int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
202*dde8cb11SKent Overstreet int bch2_delete_dead_inodes(struct bch_fs *);
2037904c82cSKent Overstreet 
2041c6fdbd8SKent Overstreet #endif /* _BCACHEFS_INODE_H */
205