]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.h
Update bcachefs sources to d1fd471830 bcachefs: Add more debug checks
[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
37         struct mutex            ei_update_lock;
38         u64                     ei_journal_seq;
39         u64                     ei_quota_reserved;
40         unsigned long           ei_last_dirtied;
41
42         struct pagecache_lock   ei_pagecache_lock;
43
44         struct mutex            ei_quota_lock;
45         struct bch_qid          ei_qid;
46
47         struct bch_hash_info    ei_str_hash;
48
49         /* copy of inode in btree: */
50         struct bch_inode_unpacked ei_inode;
51 };
52
53 #define to_bch_ei(_inode)                                       \
54         container_of_or_null(_inode, struct bch_inode_info, v)
55
56 static inline int ptrcmp(void *l, void *r)
57 {
58         return cmp_int(l, r);
59 }
60
61 enum bch_inode_lock_op {
62         INODE_LOCK              = (1U << 0),
63         INODE_PAGECACHE_BLOCK   = (1U << 1),
64         INODE_UPDATE_LOCK       = (1U << 2),
65 };
66
67 #define bch2_lock_inodes(_locks, ...)                                   \
68 do {                                                                    \
69         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
70         unsigned i;                                                     \
71                                                                         \
72         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
73                                                                         \
74         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
75                 if (a[i] != a[i - 1]) {                                 \
76                         if ((_locks) & INODE_LOCK)                      \
77                                 down_write_nested(&a[i]->v.i_rwsem, i); \
78                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
79                                 bch2_pagecache_block_get(&a[i]->ei_pagecache_lock);\
80                         if ((_locks) & INODE_UPDATE_LOCK)                       \
81                                 mutex_lock_nested(&a[i]->ei_update_lock, i);\
82                 }                                                       \
83 } while (0)
84
85 #define bch2_unlock_inodes(_locks, ...)                                 \
86 do {                                                                    \
87         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
88         unsigned i;                                                     \
89                                                                         \
90         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
91                                                                         \
92         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
93                 if (a[i] != a[i - 1]) {                                 \
94                         if ((_locks) & INODE_LOCK)                      \
95                                 up_write(&a[i]->v.i_rwsem);             \
96                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
97                                 bch2_pagecache_block_put(&a[i]->ei_pagecache_lock);\
98                         if ((_locks) & INODE_UPDATE_LOCK)                       \
99                                 mutex_unlock(&a[i]->ei_update_lock);    \
100                 }                                                       \
101 } while (0)
102
103 static inline struct bch_inode_info *file_bch_inode(struct file *file)
104 {
105         return to_bch_ei(file_inode(file));
106 }
107
108 static inline bool inode_attr_changing(struct bch_inode_info *dir,
109                                 struct bch_inode_info *inode,
110                                 enum inode_opt_id id)
111 {
112         return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
113                 bch2_inode_opt_get(&dir->ei_inode, id) !=
114                 bch2_inode_opt_get(&inode->ei_inode, id);
115 }
116
117 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
118                                  struct bch_inode_info *inode)
119 {
120         unsigned id;
121
122         for (id = 0; id < Inode_opt_nr; id++)
123                 if (inode_attr_changing(dir, inode, id))
124                         return true;
125
126         return false;
127 }
128
129 struct bch_inode_unpacked;
130
131 #ifndef NO_BCACHEFS_FS
132
133 int bch2_fs_quota_transfer(struct bch_fs *,
134                            struct bch_inode_info *,
135                            struct bch_qid,
136                            unsigned,
137                            enum quota_acct_mode);
138
139 static inline int bch2_set_projid(struct bch_fs *c,
140                                   struct bch_inode_info *inode,
141                                   u32 projid)
142 {
143         struct bch_qid qid = inode->ei_qid;
144
145         qid.q[QTYP_PRJ] = projid;
146
147         return bch2_fs_quota_transfer(c, inode, qid,
148                                       1 << QTYP_PRJ,
149                                       KEY_TYPE_QUOTA_PREALLOC);
150 }
151
152 struct inode *bch2_vfs_inode_get(struct bch_fs *, u64);
153
154 /* returns 0 if we want to do the update, or error is passed up */
155 typedef int (*inode_set_fn)(struct bch_inode_info *,
156                             struct bch_inode_unpacked *, void *);
157
158 void bch2_inode_update_after_write(struct bch_fs *,
159                                    struct bch_inode_info *,
160                                    struct bch_inode_unpacked *,
161                                    unsigned);
162 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
163                                   inode_set_fn, void *, unsigned);
164
165 void bch2_vfs_exit(void);
166 int bch2_vfs_init(void);
167
168 #else
169
170 static inline void bch2_vfs_exit(void) {}
171 static inline int bch2_vfs_init(void) { return 0; }
172
173 #endif /* NO_BCACHEFS_FS */
174
175 #endif /* _BCACHEFS_FS_H */