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