]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.h
3df85ffb450ccbdb87cbc809d6c5e0efdb2279f1
[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 /*
14  * Two-state lock - can be taken for add or block - both states are shared,
15  * like read side of rwsem, but conflict with other state:
16  */
17 struct pagecache_lock {
18         atomic_long_t           v;
19         wait_queue_head_t       wait;
20 };
21
22 static inline void pagecache_lock_init(struct pagecache_lock *lock)
23 {
24         atomic_long_set(&lock->v, 0);
25         init_waitqueue_head(&lock->wait);
26 }
27
28 void bch2_pagecache_add_put(struct pagecache_lock *);
29 bool bch2_pagecache_add_tryget(struct pagecache_lock *);
30 void bch2_pagecache_add_get(struct pagecache_lock *);
31 void bch2_pagecache_block_put(struct pagecache_lock *);
32 void bch2_pagecache_block_get(struct pagecache_lock *);
33
34 struct bch_inode_info {
35         struct inode            v;
36         unsigned long           ei_flags;
37
38         struct mutex            ei_update_lock;
39         u64                     ei_journal_seq;
40         u64                     ei_quota_reserved;
41         unsigned long           ei_last_dirtied;
42
43         struct pagecache_lock   ei_pagecache_lock;
44
45         struct mutex            ei_quota_lock;
46         struct bch_qid          ei_qid;
47
48         struct bch_hash_info    ei_str_hash;
49
50         /* copy of inode in btree: */
51         struct bch_inode_unpacked ei_inode;
52 };
53
54 /*
55  * Set if we've gotten a btree error for this inode, and thus the vfs inode and
56  * btree inode may be inconsistent:
57  */
58 #define EI_INODE_ERROR                  0
59
60 #define to_bch_ei(_inode)                                       \
61         container_of_or_null(_inode, struct bch_inode_info, v)
62
63 static inline int ptrcmp(void *l, void *r)
64 {
65         return cmp_int(l, r);
66 }
67
68 enum bch_inode_lock_op {
69         INODE_LOCK              = (1U << 0),
70         INODE_PAGECACHE_BLOCK   = (1U << 1),
71         INODE_UPDATE_LOCK       = (1U << 2),
72 };
73
74 #define bch2_lock_inodes(_locks, ...)                                   \
75 do {                                                                    \
76         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
77         unsigned i;                                                     \
78                                                                         \
79         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
80                                                                         \
81         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
82                 if (a[i] != a[i - 1]) {                                 \
83                         if ((_locks) & INODE_LOCK)                      \
84                                 down_write_nested(&a[i]->v.i_rwsem, i); \
85                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
86                                 bch2_pagecache_block_get(&a[i]->ei_pagecache_lock);\
87                         if ((_locks) & INODE_UPDATE_LOCK)                       \
88                                 mutex_lock_nested(&a[i]->ei_update_lock, i);\
89                 }                                                       \
90 } while (0)
91
92 #define bch2_unlock_inodes(_locks, ...)                                 \
93 do {                                                                    \
94         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
95         unsigned i;                                                     \
96                                                                         \
97         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
98                                                                         \
99         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
100                 if (a[i] != a[i - 1]) {                                 \
101                         if ((_locks) & INODE_LOCK)                      \
102                                 up_write(&a[i]->v.i_rwsem);             \
103                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
104                                 bch2_pagecache_block_put(&a[i]->ei_pagecache_lock);\
105                         if ((_locks) & INODE_UPDATE_LOCK)                       \
106                                 mutex_unlock(&a[i]->ei_update_lock);    \
107                 }                                                       \
108 } while (0)
109
110 static inline struct bch_inode_info *file_bch_inode(struct file *file)
111 {
112         return to_bch_ei(file_inode(file));
113 }
114
115 static inline bool inode_attr_changing(struct bch_inode_info *dir,
116                                 struct bch_inode_info *inode,
117                                 enum inode_opt_id id)
118 {
119         return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
120                 bch2_inode_opt_get(&dir->ei_inode, id) !=
121                 bch2_inode_opt_get(&inode->ei_inode, id);
122 }
123
124 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
125                                  struct bch_inode_info *inode)
126 {
127         unsigned id;
128
129         for (id = 0; id < Inode_opt_nr; id++)
130                 if (inode_attr_changing(dir, inode, id))
131                         return true;
132
133         return false;
134 }
135
136 struct bch_inode_unpacked;
137
138 #ifndef NO_BCACHEFS_FS
139
140 int bch2_fs_quota_transfer(struct bch_fs *,
141                            struct bch_inode_info *,
142                            struct bch_qid,
143                            unsigned,
144                            enum quota_acct_mode);
145
146 static inline int bch2_set_projid(struct bch_fs *c,
147                                   struct bch_inode_info *inode,
148                                   u32 projid)
149 {
150         struct bch_qid qid = inode->ei_qid;
151
152         qid.q[QTYP_PRJ] = projid;
153
154         return bch2_fs_quota_transfer(c, inode, qid,
155                                       1 << QTYP_PRJ,
156                                       KEY_TYPE_QUOTA_PREALLOC);
157 }
158
159 struct inode *bch2_vfs_inode_get(struct bch_fs *, u64);
160
161 /* returns 0 if we want to do the update, or error is passed up */
162 typedef int (*inode_set_fn)(struct bch_inode_info *,
163                             struct bch_inode_unpacked *, void *);
164
165 void bch2_inode_update_after_write(struct bch_fs *,
166                                    struct bch_inode_info *,
167                                    struct bch_inode_unpacked *,
168                                    unsigned);
169 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
170                                   inode_set_fn, void *, unsigned);
171
172 void bch2_vfs_exit(void);
173 int bch2_vfs_init(void);
174
175 #else
176
177 static inline void bch2_vfs_exit(void) {}
178 static inline int bch2_vfs_init(void) { return 0; }
179
180 #endif /* NO_BCACHEFS_FS */
181
182 #endif /* _BCACHEFS_FS_H */