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