inode.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_INODE_H
  3. #define _BCACHEFS_INODE_H
  4. #include "bkey.h"
  5. #include "bkey_methods.h"
  6. #include "opts.h"
  7. #include "snapshot.h"
  8. enum bch_validate_flags;
  9. extern const char * const bch2_inode_opts[];
  10. int bch2_inode_validate(struct bch_fs *, struct bkey_s_c,
  11. enum bch_validate_flags);
  12. int bch2_inode_v2_validate(struct bch_fs *, struct bkey_s_c,
  13. enum bch_validate_flags);
  14. int bch2_inode_v3_validate(struct bch_fs *, struct bkey_s_c,
  15. enum bch_validate_flags);
  16. void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
  17. int __bch2_inode_has_child_snapshots(struct btree_trans *, struct bpos);
  18. static inline int bch2_inode_has_child_snapshots(struct btree_trans *trans, struct bpos pos)
  19. {
  20. return bch2_snapshot_is_leaf(trans->c, pos.snapshot) <= 0
  21. ? __bch2_inode_has_child_snapshots(trans, pos)
  22. : 0;
  23. }
  24. int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
  25. struct bkey_s_c, struct bkey_s,
  26. enum btree_iter_update_trigger_flags);
  27. #define bch2_bkey_ops_inode ((struct bkey_ops) { \
  28. .key_validate = bch2_inode_validate, \
  29. .val_to_text = bch2_inode_to_text, \
  30. .trigger = bch2_trigger_inode, \
  31. .min_val_size = 16, \
  32. })
  33. #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
  34. .key_validate = bch2_inode_v2_validate, \
  35. .val_to_text = bch2_inode_to_text, \
  36. .trigger = bch2_trigger_inode, \
  37. .min_val_size = 32, \
  38. })
  39. #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
  40. .key_validate = bch2_inode_v3_validate, \
  41. .val_to_text = bch2_inode_to_text, \
  42. .trigger = bch2_trigger_inode, \
  43. .min_val_size = 48, \
  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. int bch2_inode_generation_validate(struct bch_fs *, struct bkey_s_c,
  52. enum bch_validate_flags);
  53. void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
  54. #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
  55. .key_validate = bch2_inode_generation_validate, \
  56. .val_to_text = bch2_inode_generation_to_text, \
  57. .min_val_size = 8, \
  58. })
  59. #if 0
  60. typedef struct {
  61. u64 lo;
  62. u32 hi;
  63. } __packed __aligned(4) u96;
  64. #endif
  65. typedef u64 u96;
  66. struct bch_inode_unpacked {
  67. u64 bi_inum;
  68. u32 bi_snapshot;
  69. u64 bi_journal_seq;
  70. __le64 bi_hash_seed;
  71. u64 bi_size;
  72. u64 bi_sectors;
  73. u64 bi_version;
  74. u32 bi_flags;
  75. u16 bi_mode;
  76. #define x(_name, _bits) u##_bits _name;
  77. BCH_INODE_FIELDS_v3()
  78. #undef x
  79. };
  80. BITMASK(INODE_STR_HASH, struct bch_inode_unpacked, bi_flags, 20, 24);
  81. struct bkey_inode_buf {
  82. struct bkey_i_inode_v3 inode;
  83. #define x(_name, _bits) + 8 + _bits / 8
  84. u8 _pad[0 + BCH_INODE_FIELDS_v3()];
  85. #undef x
  86. };
  87. void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
  88. int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
  89. struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
  90. void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
  91. int __bch2_inode_peek(struct btree_trans *, struct btree_iter *,
  92. struct bch_inode_unpacked *, subvol_inum, unsigned, bool);
  93. static inline int bch2_inode_peek_nowarn(struct btree_trans *trans,
  94. struct btree_iter *iter,
  95. struct bch_inode_unpacked *inode,
  96. subvol_inum inum, unsigned flags)
  97. {
  98. return __bch2_inode_peek(trans, iter, inode, inum, flags, false);
  99. }
  100. static inline int bch2_inode_peek(struct btree_trans *trans,
  101. struct btree_iter *iter,
  102. struct bch_inode_unpacked *inode,
  103. subvol_inum inum, unsigned flags)
  104. {
  105. return __bch2_inode_peek(trans, iter, inode, inum, flags, true);
  106. int ret = bch2_inode_peek_nowarn(trans, iter, inode, inum, flags);
  107. return ret;
  108. }
  109. int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
  110. struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
  111. static inline int bch2_inode_write(struct btree_trans *trans,
  112. struct btree_iter *iter,
  113. struct bch_inode_unpacked *inode)
  114. {
  115. return bch2_inode_write_flags(trans, iter, inode, 0);
  116. }
  117. int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
  118. int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
  119. void bch2_inode_init_early(struct bch_fs *,
  120. struct bch_inode_unpacked *);
  121. void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
  122. uid_t, gid_t, umode_t, dev_t,
  123. struct bch_inode_unpacked *);
  124. void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
  125. uid_t, gid_t, umode_t, dev_t,
  126. struct bch_inode_unpacked *);
  127. int bch2_inode_create(struct btree_trans *, struct btree_iter *,
  128. struct bch_inode_unpacked *, u32, u64);
  129. int bch2_inode_rm(struct bch_fs *, subvol_inum);
  130. int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
  131. subvol_inum,
  132. struct bch_inode_unpacked *);
  133. int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
  134. struct bch_inode_unpacked *);
  135. int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
  136. struct bch_inode_unpacked *);
  137. #define inode_opt_get(_c, _inode, _name) \
  138. ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
  139. static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
  140. enum inode_opt_id id, u64 v)
  141. {
  142. switch (id) {
  143. #define x(_name, ...) \
  144. case Inode_opt_##_name: \
  145. inode->bi_##_name = v; \
  146. break;
  147. BCH_INODE_OPTS()
  148. #undef x
  149. default:
  150. BUG();
  151. }
  152. }
  153. static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
  154. enum inode_opt_id id)
  155. {
  156. switch (id) {
  157. #define x(_name, ...) \
  158. case Inode_opt_##_name: \
  159. return inode->bi_##_name;
  160. BCH_INODE_OPTS()
  161. #undef x
  162. default:
  163. BUG();
  164. }
  165. }
  166. static inline u8 mode_to_type(umode_t mode)
  167. {
  168. return (mode >> 12) & 15;
  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. static inline u32 bch2_inode_flags(struct bkey_s_c k)
  175. {
  176. switch (k.k->type) {
  177. case KEY_TYPE_inode:
  178. return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
  179. case KEY_TYPE_inode_v2:
  180. return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
  181. case KEY_TYPE_inode_v3:
  182. return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
  183. default:
  184. return 0;
  185. }
  186. }
  187. /* i_nlink: */
  188. static inline unsigned nlink_bias(umode_t mode)
  189. {
  190. return S_ISDIR(mode) ? 2 : 1;
  191. }
  192. static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
  193. {
  194. return bi->bi_flags & BCH_INODE_unlinked
  195. ? 0
  196. : bi->bi_nlink + nlink_bias(bi->bi_mode);
  197. }
  198. static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
  199. unsigned nlink)
  200. {
  201. if (nlink) {
  202. bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
  203. bi->bi_flags &= ~BCH_INODE_unlinked;
  204. } else {
  205. bi->bi_nlink = 0;
  206. bi->bi_flags |= BCH_INODE_unlinked;
  207. }
  208. }
  209. int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
  210. void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
  211. static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode)
  212. {
  213. bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset;
  214. return S_ISDIR(inode->bi_mode) ||
  215. (!inode->bi_nlink && inode_has_bp);
  216. }
  217. struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
  218. void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
  219. struct bch_inode_unpacked *);
  220. int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
  221. int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
  222. int bch2_delete_dead_inodes(struct bch_fs *);
  223. #endif /* _BCACHEFS_INODE_H */