]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.h
Update bcachefs sources to b1899a0bd9 bcachefs: Move bch2_evict_subvolume_inodes...
[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         u32                     ei_subvol;
49
50         /* copy of inode in btree: */
51         struct bch_inode_unpacked ei_inode;
52 };
53
54 static inline subvol_inum inode_inum(struct bch_inode_info *inode)
55 {
56         return (subvol_inum) {
57                 .subvol = inode->ei_subvol,
58                 .inum   = inode->ei_inode.bi_inum,
59         };
60 }
61
62 /*
63  * Set if we've gotten a btree error for this inode, and thus the vfs inode and
64  * btree inode may be inconsistent:
65  */
66 #define EI_INODE_ERROR                  0
67
68 #define to_bch_ei(_inode)                                       \
69         container_of_or_null(_inode, struct bch_inode_info, v)
70
71 static inline int ptrcmp(void *l, void *r)
72 {
73         return cmp_int(l, r);
74 }
75
76 enum bch_inode_lock_op {
77         INODE_LOCK              = (1U << 0),
78         INODE_PAGECACHE_BLOCK   = (1U << 1),
79         INODE_UPDATE_LOCK       = (1U << 2),
80 };
81
82 #define bch2_lock_inodes(_locks, ...)                                   \
83 do {                                                                    \
84         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
85         unsigned i;                                                     \
86                                                                         \
87         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
88                                                                         \
89         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
90                 if (a[i] != a[i - 1]) {                                 \
91                         if ((_locks) & INODE_LOCK)                      \
92                                 down_write_nested(&a[i]->v.i_rwsem, i); \
93                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
94                                 bch2_pagecache_block_get(&a[i]->ei_pagecache_lock);\
95                         if ((_locks) & INODE_UPDATE_LOCK)                       \
96                                 mutex_lock_nested(&a[i]->ei_update_lock, i);\
97                 }                                                       \
98 } while (0)
99
100 #define bch2_unlock_inodes(_locks, ...)                                 \
101 do {                                                                    \
102         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
103         unsigned i;                                                     \
104                                                                         \
105         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
106                                                                         \
107         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
108                 if (a[i] != a[i - 1]) {                                 \
109                         if ((_locks) & INODE_LOCK)                      \
110                                 up_write(&a[i]->v.i_rwsem);             \
111                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
112                                 bch2_pagecache_block_put(&a[i]->ei_pagecache_lock);\
113                         if ((_locks) & INODE_UPDATE_LOCK)                       \
114                                 mutex_unlock(&a[i]->ei_update_lock);    \
115                 }                                                       \
116 } while (0)
117
118 static inline struct bch_inode_info *file_bch_inode(struct file *file)
119 {
120         return to_bch_ei(file_inode(file));
121 }
122
123 static inline bool inode_attr_changing(struct bch_inode_info *dir,
124                                 struct bch_inode_info *inode,
125                                 enum inode_opt_id id)
126 {
127         return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
128                 bch2_inode_opt_get(&dir->ei_inode, id) !=
129                 bch2_inode_opt_get(&inode->ei_inode, id);
130 }
131
132 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
133                                  struct bch_inode_info *inode)
134 {
135         unsigned id;
136
137         for (id = 0; id < Inode_opt_nr; id++)
138                 if (inode_attr_changing(dir, inode, id))
139                         return true;
140
141         return false;
142 }
143
144 struct bch_inode_unpacked;
145
146 #ifndef NO_BCACHEFS_FS
147
148 struct bch_inode_info *
149 __bch2_create(struct user_namespace *, struct bch_inode_info *,
150               struct dentry *, umode_t, dev_t, subvol_inum, unsigned);
151
152 int bch2_fs_quota_transfer(struct bch_fs *,
153                            struct bch_inode_info *,
154                            struct bch_qid,
155                            unsigned,
156                            enum quota_acct_mode);
157
158 static inline int bch2_set_projid(struct bch_fs *c,
159                                   struct bch_inode_info *inode,
160                                   u32 projid)
161 {
162         struct bch_qid qid = inode->ei_qid;
163
164         qid.q[QTYP_PRJ] = projid;
165
166         return bch2_fs_quota_transfer(c, inode, qid,
167                                       1 << QTYP_PRJ,
168                                       KEY_TYPE_QUOTA_PREALLOC);
169 }
170
171 struct inode *bch2_vfs_inode_get(struct bch_fs *, subvol_inum);
172
173 /* returns 0 if we want to do the update, or error is passed up */
174 typedef int (*inode_set_fn)(struct bch_inode_info *,
175                             struct bch_inode_unpacked *, void *);
176
177 void bch2_inode_update_after_write(struct bch_fs *,
178                                    struct bch_inode_info *,
179                                    struct bch_inode_unpacked *,
180                                    unsigned);
181 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
182                                   inode_set_fn, void *, unsigned);
183
184 int bch2_setattr_nonsize(struct user_namespace *,
185                          struct bch_inode_info *,
186                          struct iattr *);
187 int __bch2_unlink(struct inode *, struct dentry *, bool);
188
189 void bch2_evict_subvolume_inodes(struct bch_fs *, struct snapshot_id_list *);
190
191 void bch2_vfs_exit(void);
192 int bch2_vfs_init(void);
193
194 #else
195
196 static inline void bch2_evict_subvolume_inodes(struct bch_fs *c,
197                                                struct snapshot_id_list *s) {}
198 static inline void bch2_vfs_exit(void) {}
199 static inline int bch2_vfs_init(void) { return 0; }
200
201 #endif /* NO_BCACHEFS_FS */
202
203 #endif /* _BCACHEFS_FS_H */