xref: /linux/fs/bcachefs/inode.h (revision 4bd156c4b44ef34bd57d20a0a48aad829e1c54c1)
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"
6*4bd156c4SKent Overstreet #include "bkey_methods.h"
71c6fdbd8SKent Overstreet #include "opts.h"
81c6fdbd8SKent Overstreet 
98726dc93SKent Overstreet enum bkey_invalid_flags;
104d269918SKent Overstreet extern const char * const bch2_inode_opts[];
114d269918SKent Overstreet 
12b65db750SKent Overstreet int bch2_inode_invalid(struct bch_fs *, struct bkey_s_c,
138726dc93SKent Overstreet 		       enum bkey_invalid_flags, struct printbuf *);
14b65db750SKent Overstreet int bch2_inode_v2_invalid(struct bch_fs *, struct bkey_s_c,
158726dc93SKent Overstreet 			  enum bkey_invalid_flags, struct printbuf *);
16b65db750SKent Overstreet int bch2_inode_v3_invalid(struct bch_fs *, struct bkey_s_c,
178726dc93SKent Overstreet 			  enum bkey_invalid_flags, struct printbuf *);
18319f9ac3SKent Overstreet void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
191c6fdbd8SKent Overstreet 
204dc5bb9aSKent Overstreet int bch2_trans_mark_inode(struct btree_trans *, enum btree_id, unsigned,
214dc5bb9aSKent Overstreet 			  struct bkey_s_c, struct bkey_i *, unsigned);
224dc5bb9aSKent Overstreet int bch2_mark_inode(struct btree_trans *, enum btree_id, unsigned,
234dc5bb9aSKent Overstreet 		    struct bkey_s_c, struct bkey_s_c, unsigned);
244dc5bb9aSKent Overstreet 
25a1019576SKent Overstreet #define bch2_bkey_ops_inode ((struct bkey_ops) {	\
261c6fdbd8SKent Overstreet 	.key_invalid	= bch2_inode_invalid,		\
271c6fdbd8SKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
28880e2275SKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
29880e2275SKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
30174f930bSKent Overstreet 	.min_val_size	= 16,				\
31a1019576SKent Overstreet })
321c6fdbd8SKent Overstreet 
33a1019576SKent Overstreet #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {	\
343e52c222SKent Overstreet 	.key_invalid	= bch2_inode_v2_invalid,	\
353e52c222SKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
36880e2275SKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
37880e2275SKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
38174f930bSKent Overstreet 	.min_val_size	= 32,				\
39a1019576SKent Overstreet })
403e52c222SKent Overstreet 
418dd69d9fSKent Overstreet #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {	\
428dd69d9fSKent Overstreet 	.key_invalid	= bch2_inode_v3_invalid,	\
438dd69d9fSKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
448dd69d9fSKent Overstreet 	.trans_trigger	= bch2_trans_mark_inode,	\
458dd69d9fSKent Overstreet 	.atomic_trigger	= bch2_mark_inode,		\
46174f930bSKent Overstreet 	.min_val_size	= 48,				\
478dd69d9fSKent Overstreet })
488dd69d9fSKent Overstreet 
493e52c222SKent Overstreet static inline bool bkey_is_inode(const struct bkey *k)
503e52c222SKent Overstreet {
513e52c222SKent Overstreet 	return  k->type == KEY_TYPE_inode ||
528dd69d9fSKent Overstreet 		k->type == KEY_TYPE_inode_v2 ||
538dd69d9fSKent Overstreet 		k->type == KEY_TYPE_inode_v3;
543e52c222SKent Overstreet }
553e52c222SKent Overstreet 
56b65db750SKent Overstreet int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c,
578726dc93SKent Overstreet 				  enum bkey_invalid_flags, struct printbuf *);
58f0ac7df2SKent Overstreet void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
5926609b61SKent Overstreet 
60a1019576SKent Overstreet #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {	\
6126609b61SKent Overstreet 	.key_invalid	= bch2_inode_generation_invalid,	\
6226609b61SKent Overstreet 	.val_to_text	= bch2_inode_generation_to_text,	\
63174f930bSKent Overstreet 	.min_val_size	= 8,					\
64a1019576SKent Overstreet })
6526609b61SKent Overstreet 
66a3e72262SKent Overstreet #if 0
67a3e72262SKent Overstreet typedef struct {
68a3e72262SKent Overstreet 	u64			lo;
69a3e72262SKent Overstreet 	u32			hi;
70a3e72262SKent Overstreet } __packed __aligned(4) u96;
71a3e72262SKent Overstreet #endif
72a3e72262SKent Overstreet typedef u64 u96;
73a3e72262SKent Overstreet 
741c6fdbd8SKent Overstreet struct bch_inode_unpacked {
751c6fdbd8SKent Overstreet 	u64			bi_inum;
763e52c222SKent Overstreet 	u64			bi_journal_seq;
771c6fdbd8SKent Overstreet 	__le64			bi_hash_seed;
788dd69d9fSKent Overstreet 	u64			bi_size;
798dd69d9fSKent Overstreet 	u64			bi_sectors;
808dd69d9fSKent Overstreet 	u64			bi_version;
811c6fdbd8SKent Overstreet 	u32			bi_flags;
821c6fdbd8SKent Overstreet 	u16			bi_mode;
831c6fdbd8SKent Overstreet 
84a3e70fb2SKent Overstreet #define x(_name, _bits)	u##_bits _name;
858dd69d9fSKent Overstreet 	BCH_INODE_FIELDS_v3()
86a3e70fb2SKent Overstreet #undef  x
871c6fdbd8SKent Overstreet };
881c6fdbd8SKent Overstreet 
891c6fdbd8SKent Overstreet struct bkey_inode_buf {
908dd69d9fSKent Overstreet 	struct bkey_i_inode_v3	inode;
911c6fdbd8SKent Overstreet 
92a3e70fb2SKent Overstreet #define x(_name, _bits)		+ 8 + _bits / 8
938dd69d9fSKent Overstreet 	u8		_pad[0 + BCH_INODE_FIELDS_v3()];
94a3e70fb2SKent Overstreet #undef  x
95fd0c7679SKent Overstreet } __packed __aligned(8);
961c6fdbd8SKent Overstreet 
978dd69d9fSKent Overstreet void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
983e52c222SKent Overstreet int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
998dd69d9fSKent Overstreet struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
1001c6fdbd8SKent Overstreet 
1010f120eacSKent Overstreet void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
1020f120eacSKent Overstreet 
10367e0dd8fSKent Overstreet int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
1046fed42bbSKent Overstreet 		    struct bch_inode_unpacked *, subvol_inum, unsigned);
105*4bd156c4SKent Overstreet 
106*4bd156c4SKent Overstreet int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
107*4bd156c4SKent Overstreet 		     struct bch_inode_unpacked *, enum btree_update_flags);
108*4bd156c4SKent Overstreet 
109*4bd156c4SKent Overstreet static inline int bch2_inode_write(struct btree_trans *trans,
110*4bd156c4SKent Overstreet 		     struct btree_iter *iter,
111*4bd156c4SKent Overstreet 		     struct bch_inode_unpacked *inode)
112*4bd156c4SKent Overstreet {
113*4bd156c4SKent Overstreet 	return bch2_inode_write_flags(trans, iter, inode, 0);
114*4bd156c4SKent Overstreet }
11558677a1dSKent Overstreet 
11696385742SKent Overstreet void bch2_inode_init_early(struct bch_fs *,
11796385742SKent Overstreet 			   struct bch_inode_unpacked *);
11896385742SKent Overstreet void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
11996385742SKent Overstreet 			  uid_t, gid_t, umode_t, dev_t,
12096385742SKent Overstreet 			  struct bch_inode_unpacked *);
1211c6fdbd8SKent Overstreet void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
1221c6fdbd8SKent Overstreet 		     uid_t, gid_t, umode_t, dev_t,
1231c6fdbd8SKent Overstreet 		     struct bch_inode_unpacked *);
1241c6fdbd8SKent Overstreet 
12567e0dd8fSKent Overstreet int bch2_inode_create(struct btree_trans *, struct btree_iter *,
126bff796aeSKent Overstreet 		      struct bch_inode_unpacked *, u32, u64);
1271c6fdbd8SKent Overstreet 
1287c8f6f98SKent Overstreet int bch2_inode_rm(struct bch_fs *, subvol_inum);
1291c6fdbd8SKent Overstreet 
130da187cacSKent Overstreet int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
131da187cacSKent Overstreet 				  subvol_inum,
132da187cacSKent Overstreet 				  struct bch_inode_unpacked *);
13332b26e8cSKent Overstreet int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
13432b26e8cSKent Overstreet 				  struct bch_inode_unpacked *);
1356fed42bbSKent Overstreet int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
1366fed42bbSKent Overstreet 			    struct bch_inode_unpacked *);
1371c6fdbd8SKent Overstreet 
13801ad6737SKent Overstreet #define inode_opt_get(_c, _inode, _name)			\
13901ad6737SKent Overstreet 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
1401c6fdbd8SKent Overstreet 
1414d269918SKent Overstreet static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
1424d269918SKent Overstreet 				      enum inode_opt_id id, u64 v)
1431c6fdbd8SKent Overstreet {
1441c6fdbd8SKent Overstreet 	switch (id) {
145a3e70fb2SKent Overstreet #define x(_name, ...)							\
1464d269918SKent Overstreet 	case Inode_opt_##_name:						\
1471c6fdbd8SKent Overstreet 		inode->bi_##_name = v;					\
1481c6fdbd8SKent Overstreet 		break;
1491c6fdbd8SKent Overstreet 	BCH_INODE_OPTS()
150a3e70fb2SKent Overstreet #undef x
1511c6fdbd8SKent Overstreet 	default:
1521c6fdbd8SKent Overstreet 		BUG();
1531c6fdbd8SKent Overstreet 	}
1541c6fdbd8SKent Overstreet }
1551c6fdbd8SKent Overstreet 
1564d269918SKent Overstreet static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
1574d269918SKent Overstreet 				     enum inode_opt_id id)
1581c6fdbd8SKent Overstreet {
1594d269918SKent Overstreet 	switch (id) {
1604d269918SKent Overstreet #define x(_name, ...)							\
1614d269918SKent Overstreet 	case Inode_opt_##_name:						\
1624d269918SKent Overstreet 		return inode->bi_##_name;
1634d269918SKent Overstreet 	BCH_INODE_OPTS()
1644d269918SKent Overstreet #undef x
1654d269918SKent Overstreet 	default:
1664d269918SKent Overstreet 		BUG();
1671c6fdbd8SKent Overstreet 	}
1681c6fdbd8SKent Overstreet }
1691c6fdbd8SKent Overstreet 
17096385742SKent Overstreet static inline u8 mode_to_type(umode_t mode)
17196385742SKent Overstreet {
17296385742SKent Overstreet 	return (mode >> 12) & 15;
17396385742SKent Overstreet }
17496385742SKent Overstreet 
175285b181aSKent Overstreet static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
176285b181aSKent Overstreet {
177285b181aSKent Overstreet 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
178285b181aSKent Overstreet }
179285b181aSKent Overstreet 
180b43a0f60SKent Overstreet /* i_nlink: */
181b43a0f60SKent Overstreet 
182b43a0f60SKent Overstreet static inline unsigned nlink_bias(umode_t mode)
183b43a0f60SKent Overstreet {
184b43a0f60SKent Overstreet 	return S_ISDIR(mode) ? 2 : 1;
185b43a0f60SKent Overstreet }
186b43a0f60SKent Overstreet 
187b43a0f60SKent Overstreet static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
188b43a0f60SKent Overstreet {
189b43a0f60SKent Overstreet 	return bi->bi_flags & BCH_INODE_UNLINKED
190b43a0f60SKent Overstreet 		  ? 0
191b43a0f60SKent Overstreet 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
192b43a0f60SKent Overstreet }
193b43a0f60SKent Overstreet 
194b43a0f60SKent Overstreet static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
195b43a0f60SKent Overstreet 					unsigned nlink)
196b43a0f60SKent Overstreet {
197b43a0f60SKent Overstreet 	if (nlink) {
198b43a0f60SKent Overstreet 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
199b43a0f60SKent Overstreet 		bi->bi_flags &= ~BCH_INODE_UNLINKED;
200b43a0f60SKent Overstreet 	} else {
201b43a0f60SKent Overstreet 		bi->bi_nlink = 0;
202b43a0f60SKent Overstreet 		bi->bi_flags |= BCH_INODE_UNLINKED;
203b43a0f60SKent Overstreet 	}
204b43a0f60SKent Overstreet }
205b43a0f60SKent Overstreet 
206962ad1a7SKent Overstreet int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
207962ad1a7SKent Overstreet void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
208962ad1a7SKent Overstreet 
209abb936fbSKent Overstreet struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
21001ad6737SKent Overstreet void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
21101ad6737SKent Overstreet 			 struct bch_inode_unpacked *);
21255c11a15SKent Overstreet int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
213abb936fbSKent Overstreet 
2147904c82cSKent Overstreet int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
215dde8cb11SKent Overstreet int bch2_delete_dead_inodes(struct bch_fs *);
2167904c82cSKent Overstreet 
2171c6fdbd8SKent Overstreet #endif /* _BCACHEFS_INODE_H */
218