]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.h
Update bcachefs sources to 380885b0b8 bcachefs: Fix counting iterators for reflink...
[bcachefs-tools-debian] / libbcachefs / fs.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_FS_H
3 #define _BCACHEFS_FS_H
4
5 #include "inode.h"
6 #include "opts.h"
7 #include "str_hash.h"
8 #include "quota_types.h"
9
10 #include <linux/seqlock.h>
11 #include <linux/stat.h>
12
13 struct bch_inode_info {
14         struct inode            v;
15
16         struct mutex            ei_update_lock;
17         u64                     ei_journal_seq;
18         u64                     ei_quota_reserved;
19         unsigned long           ei_last_dirtied;
20
21         struct mutex            ei_quota_lock;
22         struct bch_qid          ei_qid;
23
24         struct bch_hash_info    ei_str_hash;
25
26         /* copy of inode in btree: */
27         struct bch_inode_unpacked ei_inode;
28 };
29
30 #define to_bch_ei(_inode)                                       \
31         container_of_or_null(_inode, struct bch_inode_info, v)
32
33 static inline int ptrcmp(void *l, void *r)
34 {
35         return cmp_int(l, r);
36 }
37
38 enum bch_inode_lock_op {
39         INODE_LOCK              = (1U << 0),
40         INODE_UPDATE_LOCK       = (1U << 1),
41 };
42
43 #define bch2_lock_inodes(_locks, ...)                                   \
44 do {                                                                    \
45         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
46         unsigned i;                                                     \
47                                                                         \
48         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
49                                                                         \
50         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
51                 if (a[i] != a[i - 1]) {                                 \
52                         if (_locks & INODE_LOCK)                        \
53                                 down_write_nested(&a[i]->v.i_rwsem, i); \
54                         if (_locks & INODE_UPDATE_LOCK)                 \
55                                 mutex_lock_nested(&a[i]->ei_update_lock, i);\
56                 }                                                       \
57 } while (0)
58
59 #define bch2_unlock_inodes(_locks, ...)                                 \
60 do {                                                                    \
61         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
62         unsigned i;                                                     \
63                                                                         \
64         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
65                                                                         \
66         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
67                 if (a[i] != a[i - 1]) {                                 \
68                         if (_locks & INODE_LOCK)                        \
69                                 up_write(&a[i]->v.i_rwsem);             \
70                         if (_locks & INODE_UPDATE_LOCK)                 \
71                                 mutex_unlock(&a[i]->ei_update_lock);    \
72                 }                                                       \
73 } while (0)
74
75 static inline struct bch_inode_info *file_bch_inode(struct file *file)
76 {
77         return to_bch_ei(file_inode(file));
78 }
79
80 static inline u8 mode_to_type(umode_t mode)
81 {
82         return (mode >> 12) & 15;
83 }
84
85 static inline bool inode_attr_changing(struct bch_inode_info *dir,
86                                 struct bch_inode_info *inode,
87                                 enum inode_opt_id id)
88 {
89         return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
90                 bch2_inode_opt_get(&dir->ei_inode, id) !=
91                 bch2_inode_opt_get(&inode->ei_inode, id);
92 }
93
94 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
95                                  struct bch_inode_info *inode)
96 {
97         unsigned id;
98
99         for (id = 0; id < Inode_opt_nr; id++)
100                 if (inode_attr_changing(dir, inode, id))
101                         return true;
102
103         return false;
104 }
105
106 struct bch_inode_unpacked;
107
108 #ifndef NO_BCACHEFS_FS
109
110 int bch2_fs_quota_transfer(struct bch_fs *,
111                            struct bch_inode_info *,
112                            struct bch_qid,
113                            unsigned,
114                            enum quota_acct_mode);
115
116 static inline int bch2_set_projid(struct bch_fs *c,
117                                   struct bch_inode_info *inode,
118                                   u32 projid)
119 {
120         struct bch_qid qid = inode->ei_qid;
121
122         qid.q[QTYP_PRJ] = projid;
123
124         return bch2_fs_quota_transfer(c, inode, qid,
125                                       1 << QTYP_PRJ,
126                                       KEY_TYPE_QUOTA_PREALLOC);
127 }
128
129 struct inode *bch2_vfs_inode_get(struct bch_fs *, u64);
130
131 /* returns 0 if we want to do the update, or error is passed up */
132 typedef int (*inode_set_fn)(struct bch_inode_info *,
133                             struct bch_inode_unpacked *, void *);
134
135 void bch2_inode_update_after_write(struct bch_fs *,
136                                    struct bch_inode_info *,
137                                    struct bch_inode_unpacked *,
138                                    unsigned);
139 int __must_check bch2_write_inode_trans(struct btree_trans *,
140                                 struct bch_inode_info *,
141                                 struct bch_inode_unpacked *,
142                                 inode_set_fn, void *);
143 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
144                                   inode_set_fn, void *, unsigned);
145
146 int bch2_reinherit_attrs_fn(struct bch_inode_info *,
147                             struct bch_inode_unpacked *,
148                             void *);
149
150 void bch2_vfs_exit(void);
151 int bch2_vfs_init(void);
152
153 #else
154
155 static inline void bch2_vfs_exit(void) {}
156 static inline int bch2_vfs_init(void) { return 0; }
157
158 #endif /* NO_BCACHEFS_FS */
159
160 #endif /* _BCACHEFS_FS_H */