1 /* SPDX-License-Identifier: GPL-2.0 */
8 #include "quota_types.h"
10 #include <linux/seqlock.h>
11 #include <linux/stat.h>
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:
17 struct pagecache_lock {
19 wait_queue_head_t wait;
22 static inline void pagecache_lock_init(struct pagecache_lock *lock)
24 atomic_long_set(&lock->v, 0);
25 init_waitqueue_head(&lock->wait);
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 *);
34 struct bch_inode_info {
36 unsigned long ei_flags;
38 struct mutex ei_update_lock;
39 u64 ei_quota_reserved;
40 unsigned long ei_last_dirtied;
42 struct pagecache_lock ei_pagecache_lock;
44 struct mutex ei_quota_lock;
45 struct bch_qid ei_qid;
49 /* copy of inode in btree: */
50 struct bch_inode_unpacked ei_inode;
53 static inline subvol_inum inode_inum(struct bch_inode_info *inode)
55 return (subvol_inum) {
56 .subvol = inode->ei_subvol,
57 .inum = inode->ei_inode.bi_inum,
62 * Set if we've gotten a btree error for this inode, and thus the vfs inode and
63 * btree inode may be inconsistent:
65 #define EI_INODE_ERROR 0
67 #define to_bch_ei(_inode) \
68 container_of_or_null(_inode, struct bch_inode_info, v)
70 static inline int ptrcmp(void *l, void *r)
75 enum bch_inode_lock_op {
76 INODE_LOCK = (1U << 0),
77 INODE_PAGECACHE_BLOCK = (1U << 1),
78 INODE_UPDATE_LOCK = (1U << 2),
81 #define bch2_lock_inodes(_locks, ...) \
83 struct bch_inode_info *a[] = { NULL, __VA_ARGS__ }; \
86 bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp); \
88 for (i = 1; i < ARRAY_SIZE(a); i++) \
89 if (a[i] != a[i - 1]) { \
90 if ((_locks) & INODE_LOCK) \
91 down_write_nested(&a[i]->v.i_rwsem, i); \
92 if ((_locks) & INODE_PAGECACHE_BLOCK) \
93 bch2_pagecache_block_get(&a[i]->ei_pagecache_lock);\
94 if ((_locks) & INODE_UPDATE_LOCK) \
95 mutex_lock_nested(&a[i]->ei_update_lock, i);\
99 #define bch2_unlock_inodes(_locks, ...) \
101 struct bch_inode_info *a[] = { NULL, __VA_ARGS__ }; \
104 bubble_sort(&a[1], ARRAY_SIZE(a) - 1, ptrcmp); \
106 for (i = 1; i < ARRAY_SIZE(a); i++) \
107 if (a[i] != a[i - 1]) { \
108 if ((_locks) & INODE_LOCK) \
109 up_write(&a[i]->v.i_rwsem); \
110 if ((_locks) & INODE_PAGECACHE_BLOCK) \
111 bch2_pagecache_block_put(&a[i]->ei_pagecache_lock);\
112 if ((_locks) & INODE_UPDATE_LOCK) \
113 mutex_unlock(&a[i]->ei_update_lock); \
117 static inline struct bch_inode_info *file_bch_inode(struct file *file)
119 return to_bch_ei(file_inode(file));
122 static inline bool inode_attr_changing(struct bch_inode_info *dir,
123 struct bch_inode_info *inode,
124 enum inode_opt_id id)
126 return !(inode->ei_inode.bi_fields_set & (1 << id)) &&
127 bch2_inode_opt_get(&dir->ei_inode, id) !=
128 bch2_inode_opt_get(&inode->ei_inode, id);
131 static inline bool inode_attrs_changing(struct bch_inode_info *dir,
132 struct bch_inode_info *inode)
136 for (id = 0; id < Inode_opt_nr; id++)
137 if (inode_attr_changing(dir, inode, id))
143 struct bch_inode_unpacked;
145 #ifndef NO_BCACHEFS_FS
147 struct bch_inode_info *
148 __bch2_create(struct user_namespace *, struct bch_inode_info *,
149 struct dentry *, umode_t, dev_t, subvol_inum, unsigned);
151 int bch2_fs_quota_transfer(struct bch_fs *,
152 struct bch_inode_info *,
155 enum quota_acct_mode);
157 static inline int bch2_set_projid(struct bch_fs *c,
158 struct bch_inode_info *inode,
161 struct bch_qid qid = inode->ei_qid;
163 qid.q[QTYP_PRJ] = projid;
165 return bch2_fs_quota_transfer(c, inode, qid,
167 KEY_TYPE_QUOTA_PREALLOC);
170 struct inode *bch2_vfs_inode_get(struct bch_fs *, subvol_inum);
172 /* returns 0 if we want to do the update, or error is passed up */
173 typedef int (*inode_set_fn)(struct bch_inode_info *,
174 struct bch_inode_unpacked *, void *);
176 void bch2_inode_update_after_write(struct btree_trans *,
177 struct bch_inode_info *,
178 struct bch_inode_unpacked *,
180 int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
181 inode_set_fn, void *, unsigned);
183 int bch2_setattr_nonsize(struct user_namespace *,
184 struct bch_inode_info *,
186 int __bch2_unlink(struct inode *, struct dentry *, bool);
188 void bch2_evict_subvolume_inodes(struct bch_fs *, struct snapshot_id_list *);
190 void bch2_vfs_exit(void);
191 int bch2_vfs_init(void);
195 static inline void bch2_evict_subvolume_inodes(struct bch_fs *c,
196 struct snapshot_id_list *s) {}
197 static inline void bch2_vfs_exit(void) {}
198 static inline int bch2_vfs_init(void) { return 0; }
200 #endif /* NO_BCACHEFS_FS */
202 #endif /* _BCACHEFS_FS_H */