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