]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.h
rust: Fix ptr casting in Fs::open()
[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 #include "two_state_shared_lock.h"
10
11 #include <linux/seqlock.h>
12 #include <linux/stat.h>
13
14 struct bch_inode_info {
15         struct inode            v;
16         unsigned long           ei_flags;
17
18         struct mutex            ei_update_lock;
19         u64                     ei_quota_reserved;
20         unsigned long           ei_last_dirtied;
21         two_state_lock_t        ei_pagecache_lock;
22
23         struct mutex            ei_quota_lock;
24         struct bch_qid          ei_qid;
25
26         u32                     ei_subvol;
27
28         /*
29          * When we've been doing nocow writes we'll need to issue flushes to the
30          * underlying block devices
31          *
32          * XXX: a device may have had a flush issued by some other codepath. It
33          * would be better to keep for each device a sequence number that's
34          * incremented when we isusue a cache flush, and track here the sequence
35          * number that needs flushing.
36          */
37         struct bch_devs_mask    ei_devs_need_flush;
38
39         /* copy of inode in btree: */
40         struct bch_inode_unpacked ei_inode;
41 };
42
43 #define bch2_pagecache_add_put(i)       bch2_two_state_unlock(&i->ei_pagecache_lock, 0)
44 #define bch2_pagecache_add_tryget(i)    bch2_two_state_trylock(&i->ei_pagecache_lock, 0)
45 #define bch2_pagecache_add_get(i)       bch2_two_state_lock(&i->ei_pagecache_lock, 0)
46
47 #define bch2_pagecache_block_put(i)     bch2_two_state_unlock(&i->ei_pagecache_lock, 1)
48 #define bch2_pagecache_block_get(i)     bch2_two_state_lock(&i->ei_pagecache_lock, 1)
49
50 static inline subvol_inum inode_inum(struct bch_inode_info *inode)
51 {
52         return (subvol_inum) {
53                 .subvol = inode->ei_subvol,
54                 .inum   = inode->ei_inode.bi_inum,
55         };
56 }
57
58 /*
59  * Set if we've gotten a btree error for this inode, and thus the vfs inode and
60  * btree inode may be inconsistent:
61  */
62 #define EI_INODE_ERROR                  0
63
64 /*
65  * Set in the inode is in a snapshot subvolume - we don't do quota accounting in
66  * those:
67  */
68 #define EI_INODE_SNAPSHOT               1
69
70 #define to_bch_ei(_inode)                                       \
71         container_of_or_null(_inode, struct bch_inode_info, v)
72
73 static inline int ptrcmp(void *l, void *r)
74 {
75         return cmp_int(l, r);
76 }
77
78 enum bch_inode_lock_op {
79         INODE_LOCK              = (1U << 0),
80         INODE_PAGECACHE_BLOCK   = (1U << 1),
81         INODE_UPDATE_LOCK       = (1U << 2),
82 };
83
84 #define bch2_lock_inodes(_locks, ...)                                   \
85 do {                                                                    \
86         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
87         unsigned i;                                                     \
88                                                                         \
89         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
90                                                                         \
91         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
92                 if (a[i] != a[i - 1]) {                                 \
93                         if ((_locks) & INODE_LOCK)                      \
94                                 down_write_nested(&a[i]->v.i_rwsem, i); \
95                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
96                                 bch2_pagecache_block_get(a[i]);\
97                         if ((_locks) & INODE_UPDATE_LOCK)                       \
98                                 mutex_lock_nested(&a[i]->ei_update_lock, i);\
99                 }                                                       \
100 } while (0)
101
102 #define bch2_unlock_inodes(_locks, ...)                                 \
103 do {                                                                    \
104         struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
105         unsigned i;                                                     \
106                                                                         \
107         bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp);                  \
108                                                                         \
109         for (i = 1; i < ARRAY_SIZE(a); i++)                             \
110                 if (a[i] != a[i - 1]) {                                 \
111                         if ((_locks) & INODE_LOCK)                      \
112                                 up_write(&a[i]->v.i_rwsem);             \
113                         if ((_locks) & INODE_PAGECACHE_BLOCK)           \
114                                 bch2_pagecache_block_put(a[i]);\
115                         if ((_locks) & INODE_UPDATE_LOCK)                       \
116                                 mutex_unlock(&a[i]->ei_update_lock);    \
117                 }                                                       \
118 } while (0)
119
120 static inline struct bch_inode_info *file_bch_inode(struct file *file)
121 {
122         return to_bch_ei(file_inode(file));
123 }
124
125 static inline bool inode_attr_changing(struct bch_inode_info *dir,
126                                 struct bch_inode_info *inode,
127                                 enum inode_opt_id id)
128 {
129         return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
130                 bch2_inode_opt_get(&dir->ei_inode, id) !=
131                 bch2_inode_opt_get(&inode->ei_inode, id);
132 }
133
134 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
135                                  struct bch_inode_info *inode)
136 {
137         unsigned id;
138
139         for (id = 0; id < Inode_opt_nr; id++)
140                 if (inode_attr_changing(dir, inode, id))
141                         return true;
142
143         return false;
144 }
145
146 struct bch_inode_unpacked;
147
148 #ifndef NO_BCACHEFS_FS
149
150 struct bch_inode_info *
151 __bch2_create(struct user_namespace *, struct bch_inode_info *,
152               struct dentry *, umode_t, dev_t, subvol_inum, unsigned);
153
154 int bch2_fs_quota_transfer(struct bch_fs *,
155                            struct bch_inode_info *,
156                            struct bch_qid,
157                            unsigned,
158                            enum quota_acct_mode);
159
160 static inline int bch2_set_projid(struct bch_fs *c,
161                                   struct bch_inode_info *inode,
162                                   u32 projid)
163 {
164         struct bch_qid qid = inode->ei_qid;
165
166         qid.q[QTYP_PRJ] = projid;
167
168         return bch2_fs_quota_transfer(c, inode, qid,
169                                       1 << QTYP_PRJ,
170                                       KEY_TYPE_QUOTA_PREALLOC);
171 }
172
173 struct inode *bch2_vfs_inode_get(struct bch_fs *, subvol_inum);
174
175 /* returns 0 if we want to do the update, or error is passed up */
176 typedef int (*inode_set_fn)(struct bch_inode_info *,
177                             struct bch_inode_unpacked *, void *);
178
179 void bch2_inode_update_after_write(struct btree_trans *,
180                                    struct bch_inode_info *,
181                                    struct bch_inode_unpacked *,
182                                    unsigned);
183 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
184                                   inode_set_fn, void *, unsigned);
185
186 int bch2_setattr_nonsize(struct user_namespace *,
187                          struct bch_inode_info *,
188                          struct iattr *);
189 int __bch2_unlink(struct inode *, struct dentry *, bool);
190
191 void bch2_evict_subvolume_inodes(struct bch_fs *, snapshot_id_list *);
192
193 void bch2_vfs_exit(void);
194 int bch2_vfs_init(void);
195
196 #else
197
198 static inline void bch2_evict_subvolume_inodes(struct bch_fs *c,
199                                                snapshot_id_list *s) {}
200 static inline void bch2_vfs_exit(void) {}
201 static inline int bch2_vfs_init(void) { return 0; }
202
203 #endif /* NO_BCACHEFS_FS */
204
205 #endif /* _BCACHEFS_FS_H */