netfs: Provide tools to create a buffer in an xarray
[linux-block.git] / fs / netfs / internal.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Internal definitions for network filesystem support
3  *
4  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/slab.h>
9 #include <linux/seq_file.h>
10 #include <linux/netfs.h>
11 #include <linux/fscache.h>
12 #include <linux/fscache-cache.h>
13 #include <trace/events/netfs.h>
14 #include <trace/events/fscache.h>
15
16 #ifdef pr_fmt
17 #undef pr_fmt
18 #endif
19
20 #define pr_fmt(fmt) "netfs: " fmt
21
22 /*
23  * buffered_read.c
24  */
25 void netfs_rreq_unlock_folios(struct netfs_io_request *rreq);
26
27 /*
28  * io.c
29  */
30 int netfs_begin_read(struct netfs_io_request *rreq, bool sync);
31
32 /*
33  * main.c
34  */
35 extern unsigned int netfs_debug;
36 extern struct list_head netfs_io_requests;
37 extern spinlock_t netfs_proc_lock;
38
39 #ifdef CONFIG_PROC_FS
40 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq)
41 {
42         spin_lock(&netfs_proc_lock);
43         list_add_tail_rcu(&rreq->proc_link, &netfs_io_requests);
44         spin_unlock(&netfs_proc_lock);
45 }
46 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq)
47 {
48         if (!list_empty(&rreq->proc_link)) {
49                 spin_lock(&netfs_proc_lock);
50                 list_del_rcu(&rreq->proc_link);
51                 spin_unlock(&netfs_proc_lock);
52         }
53 }
54 #else
55 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq) {}
56 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq) {}
57 #endif
58
59 /*
60  * misc.c
61  */
62 #define NETFS_FLAG_PUT_MARK             BIT(0)
63 #define NETFS_FLAG_PAGECACHE_MARK       BIT(1)
64 int netfs_xa_store_and_mark(struct xarray *xa, unsigned long index,
65                             struct folio *folio, unsigned int flags,
66                             gfp_t gfp_mask);
67 int netfs_add_folios_to_buffer(struct xarray *buffer,
68                                struct address_space *mapping,
69                                pgoff_t index, pgoff_t to, gfp_t gfp_mask);
70 void netfs_clear_buffer(struct xarray *buffer);
71
72 /*
73  * objects.c
74  */
75 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
76                                              struct file *file,
77                                              loff_t start, size_t len,
78                                              enum netfs_io_origin origin);
79 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what);
80 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async);
81 void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
82                        enum netfs_rreq_ref_trace what);
83 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq);
84
85 static inline void netfs_see_request(struct netfs_io_request *rreq,
86                                      enum netfs_rreq_ref_trace what)
87 {
88         trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what);
89 }
90
91 /*
92  * stats.c
93  */
94 #ifdef CONFIG_NETFS_STATS
95 extern atomic_t netfs_n_rh_readahead;
96 extern atomic_t netfs_n_rh_readpage;
97 extern atomic_t netfs_n_rh_rreq;
98 extern atomic_t netfs_n_rh_sreq;
99 extern atomic_t netfs_n_rh_download;
100 extern atomic_t netfs_n_rh_download_done;
101 extern atomic_t netfs_n_rh_download_failed;
102 extern atomic_t netfs_n_rh_download_instead;
103 extern atomic_t netfs_n_rh_read;
104 extern atomic_t netfs_n_rh_read_done;
105 extern atomic_t netfs_n_rh_read_failed;
106 extern atomic_t netfs_n_rh_zero;
107 extern atomic_t netfs_n_rh_short_read;
108 extern atomic_t netfs_n_rh_write;
109 extern atomic_t netfs_n_rh_write_begin;
110 extern atomic_t netfs_n_rh_write_done;
111 extern atomic_t netfs_n_rh_write_failed;
112 extern atomic_t netfs_n_rh_write_zskip;
113
114 int netfs_stats_show(struct seq_file *m, void *v);
115
116 static inline void netfs_stat(atomic_t *stat)
117 {
118         atomic_inc(stat);
119 }
120
121 static inline void netfs_stat_d(atomic_t *stat)
122 {
123         atomic_dec(stat);
124 }
125
126 #else
127 #define netfs_stat(x) do {} while(0)
128 #define netfs_stat_d(x) do {} while(0)
129 #endif
130
131 /*
132  * Miscellaneous functions.
133  */
134 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
135 {
136 #if IS_ENABLED(CONFIG_FSCACHE)
137         struct fscache_cookie *cookie = ctx->cache;
138
139         return fscache_cookie_valid(cookie) && cookie->cache_priv &&
140                 fscache_cookie_enabled(cookie);
141 #else
142         return false;
143 #endif
144 }
145
146 /*
147  * fscache-cache.c
148  */
149 #ifdef CONFIG_PROC_FS
150 extern const struct seq_operations fscache_caches_seq_ops;
151 #endif
152 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
153 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
154 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
155 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
156
157 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
158 {
159         return smp_load_acquire(&cache->state);
160 }
161
162 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
163 {
164         return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
165 }
166
167 static inline void fscache_set_cache_state(struct fscache_cache *cache,
168                                            enum fscache_cache_state new_state)
169 {
170         smp_store_release(&cache->state, new_state);
171
172 }
173
174 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
175                                                  enum fscache_cache_state old_state,
176                                                  enum fscache_cache_state new_state)
177 {
178         return try_cmpxchg_release(&cache->state, &old_state, new_state);
179 }
180
181 /*
182  * fscache-cookie.c
183  */
184 extern struct kmem_cache *fscache_cookie_jar;
185 #ifdef CONFIG_PROC_FS
186 extern const struct seq_operations fscache_cookies_seq_ops;
187 #endif
188 extern struct timer_list fscache_cookie_lru_timer;
189
190 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
191 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
192                                         enum fscache_access_trace why);
193
194 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
195                                       enum fscache_cookie_trace where)
196 {
197         trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
198                              where);
199 }
200
201 /*
202  * fscache-main.c
203  */
204 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
205 #ifdef CONFIG_FSCACHE
206 int __init fscache_init(void);
207 void __exit fscache_exit(void);
208 #else
209 static inline int fscache_init(void) { return 0; }
210 static inline void fscache_exit(void) {}
211 #endif
212
213 /*
214  * fscache-proc.c
215  */
216 #ifdef CONFIG_PROC_FS
217 extern int __init fscache_proc_init(void);
218 extern void fscache_proc_cleanup(void);
219 #else
220 #define fscache_proc_init()     (0)
221 #define fscache_proc_cleanup()  do {} while (0)
222 #endif
223
224 /*
225  * fscache-stats.c
226  */
227 #ifdef CONFIG_FSCACHE_STATS
228 extern atomic_t fscache_n_volumes;
229 extern atomic_t fscache_n_volumes_collision;
230 extern atomic_t fscache_n_volumes_nomem;
231 extern atomic_t fscache_n_cookies;
232 extern atomic_t fscache_n_cookies_lru;
233 extern atomic_t fscache_n_cookies_lru_expired;
234 extern atomic_t fscache_n_cookies_lru_removed;
235 extern atomic_t fscache_n_cookies_lru_dropped;
236
237 extern atomic_t fscache_n_acquires;
238 extern atomic_t fscache_n_acquires_ok;
239 extern atomic_t fscache_n_acquires_oom;
240
241 extern atomic_t fscache_n_invalidates;
242
243 extern atomic_t fscache_n_relinquishes;
244 extern atomic_t fscache_n_relinquishes_retire;
245 extern atomic_t fscache_n_relinquishes_dropped;
246
247 extern atomic_t fscache_n_resizes;
248 extern atomic_t fscache_n_resizes_null;
249
250 static inline void fscache_stat(atomic_t *stat)
251 {
252         atomic_inc(stat);
253 }
254
255 static inline void fscache_stat_d(atomic_t *stat)
256 {
257         atomic_dec(stat);
258 }
259
260 #define __fscache_stat(stat) (stat)
261
262 int fscache_stats_show(struct seq_file *m);
263 #else
264
265 #define __fscache_stat(stat) (NULL)
266 #define fscache_stat(stat) do {} while (0)
267 #define fscache_stat_d(stat) do {} while (0)
268
269 static inline int fscache_stats_show(struct seq_file *m) { return 0; }
270 #endif
271
272 /*
273  * fscache-volume.c
274  */
275 #ifdef CONFIG_PROC_FS
276 extern const struct seq_operations fscache_volumes_seq_ops;
277 #endif
278
279 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
280                                           enum fscache_volume_trace where);
281 void fscache_put_volume(struct fscache_volume *volume,
282                         enum fscache_volume_trace where);
283 bool fscache_begin_volume_access(struct fscache_volume *volume,
284                                  struct fscache_cookie *cookie,
285                                  enum fscache_access_trace why);
286 void fscache_create_volume(struct fscache_volume *volume, bool wait);
287
288 /*****************************************************************************/
289 /*
290  * debug tracing
291  */
292 #define dbgprintk(FMT, ...) \
293         printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
294
295 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
296 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
297 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
298
299 #ifdef __KDEBUG
300 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
301 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
302 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
303
304 #elif defined(CONFIG_NETFS_DEBUG)
305 #define _enter(FMT, ...)                        \
306 do {                                            \
307         if (netfs_debug)                        \
308                 kenter(FMT, ##__VA_ARGS__);     \
309 } while (0)
310
311 #define _leave(FMT, ...)                        \
312 do {                                            \
313         if (netfs_debug)                        \
314                 kleave(FMT, ##__VA_ARGS__);     \
315 } while (0)
316
317 #define _debug(FMT, ...)                        \
318 do {                                            \
319         if (netfs_debug)                        \
320                 kdebug(FMT, ##__VA_ARGS__);     \
321 } while (0)
322
323 #else
324 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
325 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
326 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
327 #endif
328
329 /*
330  * assertions
331  */
332 #if 1 /* defined(__KDEBUGALL) */
333
334 #define ASSERT(X)                                                       \
335 do {                                                                    \
336         if (unlikely(!(X))) {                                           \
337                 pr_err("\n");                                   \
338                 pr_err("Assertion failed\n");   \
339                 BUG();                                                  \
340         }                                                               \
341 } while (0)
342
343 #define ASSERTCMP(X, OP, Y)                                             \
344 do {                                                                    \
345         if (unlikely(!((X) OP (Y)))) {                                  \
346                 pr_err("\n");                                   \
347                 pr_err("Assertion failed\n");   \
348                 pr_err("%lx " #OP " %lx is false\n",            \
349                        (unsigned long)(X), (unsigned long)(Y));         \
350                 BUG();                                                  \
351         }                                                               \
352 } while (0)
353
354 #define ASSERTIF(C, X)                                                  \
355 do {                                                                    \
356         if (unlikely((C) && !(X))) {                                    \
357                 pr_err("\n");                                   \
358                 pr_err("Assertion failed\n");   \
359                 BUG();                                                  \
360         }                                                               \
361 } while (0)
362
363 #define ASSERTIFCMP(C, X, OP, Y)                                        \
364 do {                                                                    \
365         if (unlikely((C) && !((X) OP (Y)))) {                           \
366                 pr_err("\n");                                   \
367                 pr_err("Assertion failed\n");   \
368                 pr_err("%lx " #OP " %lx is false\n",            \
369                        (unsigned long)(X), (unsigned long)(Y));         \
370                 BUG();                                                  \
371         }                                                               \
372 } while (0)
373
374 #else
375
376 #define ASSERT(X)                       do {} while (0)
377 #define ASSERTCMP(X, OP, Y)             do {} while (0)
378 #define ASSERTIF(C, X)                  do {} while (0)
379 #define ASSERTIFCMP(C, X, OP, Y)        do {} while (0)
380
381 #endif /* assert or not */