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