xref: /linux/fs/bcachefs/inode.h (revision a3e70fb287ee62ee14512a69e9a3e1870a057e11)
11c6fdbd8SKent Overstreet /* SPDX-License-Identifier: GPL-2.0 */
21c6fdbd8SKent Overstreet #ifndef _BCACHEFS_INODE_H
31c6fdbd8SKent Overstreet #define _BCACHEFS_INODE_H
41c6fdbd8SKent Overstreet 
51c6fdbd8SKent Overstreet #include "opts.h"
61c6fdbd8SKent Overstreet 
71c6fdbd8SKent Overstreet #include <linux/math64.h>
81c6fdbd8SKent Overstreet 
91c6fdbd8SKent Overstreet const char *bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c);
10319f9ac3SKent Overstreet void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
111c6fdbd8SKent Overstreet 
1226609b61SKent Overstreet #define bch2_bkey_ops_inode (struct bkey_ops) {		\
131c6fdbd8SKent Overstreet 	.key_invalid	= bch2_inode_invalid,		\
141c6fdbd8SKent Overstreet 	.val_to_text	= bch2_inode_to_text,		\
151c6fdbd8SKent Overstreet }
161c6fdbd8SKent Overstreet 
1726609b61SKent Overstreet const char *bch2_inode_generation_invalid(const struct bch_fs *,
1826609b61SKent Overstreet 					  struct bkey_s_c);
1926609b61SKent Overstreet void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *,
2026609b61SKent Overstreet 				   struct bkey_s_c);
2126609b61SKent Overstreet 
2226609b61SKent Overstreet #define bch2_bkey_ops_inode_generation (struct bkey_ops) {	\
2326609b61SKent Overstreet 	.key_invalid	= bch2_inode_generation_invalid,	\
2426609b61SKent Overstreet 	.val_to_text	= bch2_inode_generation_to_text,	\
2526609b61SKent Overstreet }
2626609b61SKent Overstreet 
271c6fdbd8SKent Overstreet struct bch_inode_unpacked {
281c6fdbd8SKent Overstreet 	u64			bi_inum;
291c6fdbd8SKent Overstreet 	__le64			bi_hash_seed;
301c6fdbd8SKent Overstreet 	u32			bi_flags;
311c6fdbd8SKent Overstreet 	u16			bi_mode;
321c6fdbd8SKent Overstreet 
33*a3e70fb2SKent Overstreet #define x(_name, _bits)	u##_bits _name;
341c6fdbd8SKent Overstreet 	BCH_INODE_FIELDS()
35*a3e70fb2SKent Overstreet #undef  x
361c6fdbd8SKent Overstreet };
371c6fdbd8SKent Overstreet 
381c6fdbd8SKent Overstreet struct bkey_inode_buf {
391c6fdbd8SKent Overstreet 	struct bkey_i_inode	inode;
401c6fdbd8SKent Overstreet 
41*a3e70fb2SKent Overstreet #define x(_name, _bits)		+ 8 + _bits / 8
421c6fdbd8SKent Overstreet 	u8		_pad[0 + BCH_INODE_FIELDS()];
43*a3e70fb2SKent Overstreet #undef  x
441c6fdbd8SKent Overstreet } __attribute__((packed, aligned(8)));
451c6fdbd8SKent Overstreet 
461c6fdbd8SKent Overstreet void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
471c6fdbd8SKent Overstreet int bch2_inode_unpack(struct bkey_s_c_inode, struct bch_inode_unpacked *);
481c6fdbd8SKent Overstreet 
491c6fdbd8SKent Overstreet void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
501c6fdbd8SKent Overstreet 		     uid_t, gid_t, umode_t, dev_t,
511c6fdbd8SKent Overstreet 		     struct bch_inode_unpacked *);
521c6fdbd8SKent Overstreet 
531c6fdbd8SKent Overstreet int __bch2_inode_create(struct btree_trans *,
541c6fdbd8SKent Overstreet 			struct bch_inode_unpacked *,
551c6fdbd8SKent Overstreet 			u64, u64, u64 *);
561c6fdbd8SKent Overstreet int bch2_inode_create(struct bch_fs *, struct bch_inode_unpacked *,
571c6fdbd8SKent Overstreet 		      u64, u64, u64 *);
581c6fdbd8SKent Overstreet 
591c6fdbd8SKent Overstreet int bch2_inode_rm(struct bch_fs *, u64);
601c6fdbd8SKent Overstreet 
611c6fdbd8SKent Overstreet int bch2_inode_find_by_inum(struct bch_fs *, u64,
621c6fdbd8SKent Overstreet 			   struct bch_inode_unpacked *);
631c6fdbd8SKent Overstreet 
641c6fdbd8SKent Overstreet static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
651c6fdbd8SKent Overstreet {
661c6fdbd8SKent Overstreet 	struct bch_io_opts ret = { 0 };
671c6fdbd8SKent Overstreet 
68*a3e70fb2SKent Overstreet #define x(_name, _bits)					\
691c6fdbd8SKent Overstreet 	if (inode->bi_##_name)						\
701c6fdbd8SKent Overstreet 		opt_set(ret, _name, inode->bi_##_name - 1);
711c6fdbd8SKent Overstreet 	BCH_INODE_OPTS()
72*a3e70fb2SKent Overstreet #undef x
731c6fdbd8SKent Overstreet 	return ret;
741c6fdbd8SKent Overstreet }
751c6fdbd8SKent Overstreet 
761c6fdbd8SKent Overstreet static inline void __bch2_inode_opt_set(struct bch_inode_unpacked *inode,
771c6fdbd8SKent Overstreet 					enum bch_opt_id id, u64 v)
781c6fdbd8SKent Overstreet {
791c6fdbd8SKent Overstreet 	switch (id) {
80*a3e70fb2SKent Overstreet #define x(_name, ...)					\
811c6fdbd8SKent Overstreet 	case Opt_##_name:						\
821c6fdbd8SKent Overstreet 		inode->bi_##_name = v;					\
831c6fdbd8SKent Overstreet 		break;
841c6fdbd8SKent Overstreet 	BCH_INODE_OPTS()
85*a3e70fb2SKent Overstreet #undef x
861c6fdbd8SKent Overstreet 	default:
871c6fdbd8SKent Overstreet 		BUG();
881c6fdbd8SKent Overstreet 	}
891c6fdbd8SKent Overstreet }
901c6fdbd8SKent Overstreet 
911c6fdbd8SKent Overstreet static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
921c6fdbd8SKent Overstreet 				      enum bch_opt_id id, u64 v)
931c6fdbd8SKent Overstreet {
941c6fdbd8SKent Overstreet 	return __bch2_inode_opt_set(inode, id, v + 1);
951c6fdbd8SKent Overstreet }
961c6fdbd8SKent Overstreet 
971c6fdbd8SKent Overstreet static inline void bch2_inode_opt_clear(struct bch_inode_unpacked *inode,
981c6fdbd8SKent Overstreet 					enum bch_opt_id id)
991c6fdbd8SKent Overstreet {
1001c6fdbd8SKent Overstreet 	return __bch2_inode_opt_set(inode, id, 0);
1011c6fdbd8SKent Overstreet }
1021c6fdbd8SKent Overstreet 
1031c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_DEBUG
1041c6fdbd8SKent Overstreet void bch2_inode_pack_test(void);
1051c6fdbd8SKent Overstreet #else
1061c6fdbd8SKent Overstreet static inline void bch2_inode_pack_test(void) {}
1071c6fdbd8SKent Overstreet #endif
1081c6fdbd8SKent Overstreet 
1091c6fdbd8SKent Overstreet #endif /* _BCACHEFS_INODE_H */
110