netfs: Count DIO writes
[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 struct netfs_io_request *netfs_begin_writethrough(struct kiocb *iocb, size_t len);
99 int netfs_advance_writethrough(struct netfs_io_request *wreq, size_t copied, bool to_page_end);
100 int netfs_end_writethrough(struct netfs_io_request *wreq, struct kiocb *iocb);
101
102 /*
103  * stats.c
104  */
105 #ifdef CONFIG_NETFS_STATS
106 extern atomic_t netfs_n_rh_dio_read;
107 extern atomic_t netfs_n_rh_dio_write;
108 extern atomic_t netfs_n_rh_readahead;
109 extern atomic_t netfs_n_rh_readpage;
110 extern atomic_t netfs_n_rh_rreq;
111 extern atomic_t netfs_n_rh_sreq;
112 extern atomic_t netfs_n_rh_download;
113 extern atomic_t netfs_n_rh_download_done;
114 extern atomic_t netfs_n_rh_download_failed;
115 extern atomic_t netfs_n_rh_download_instead;
116 extern atomic_t netfs_n_rh_read;
117 extern atomic_t netfs_n_rh_read_done;
118 extern atomic_t netfs_n_rh_read_failed;
119 extern atomic_t netfs_n_rh_zero;
120 extern atomic_t netfs_n_rh_short_read;
121 extern atomic_t netfs_n_rh_write;
122 extern atomic_t netfs_n_rh_write_begin;
123 extern atomic_t netfs_n_rh_write_done;
124 extern atomic_t netfs_n_rh_write_failed;
125 extern atomic_t netfs_n_rh_write_zskip;
126 extern atomic_t netfs_n_wh_upload;
127 extern atomic_t netfs_n_wh_upload_done;
128 extern atomic_t netfs_n_wh_upload_failed;
129 extern atomic_t netfs_n_wh_write;
130 extern atomic_t netfs_n_wh_write_done;
131 extern atomic_t netfs_n_wh_write_failed;
132
133 int netfs_stats_show(struct seq_file *m, void *v);
134
135 static inline void netfs_stat(atomic_t *stat)
136 {
137         atomic_inc(stat);
138 }
139
140 static inline void netfs_stat_d(atomic_t *stat)
141 {
142         atomic_dec(stat);
143 }
144
145 #else
146 #define netfs_stat(x) do {} while(0)
147 #define netfs_stat_d(x) do {} while(0)
148 #endif
149
150 /*
151  * Miscellaneous functions.
152  */
153 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
154 {
155 #if IS_ENABLED(CONFIG_FSCACHE)
156         struct fscache_cookie *cookie = ctx->cache;
157
158         return fscache_cookie_valid(cookie) && cookie->cache_priv &&
159                 fscache_cookie_enabled(cookie);
160 #else
161         return false;
162 #endif
163 }
164
165 /*
166  * Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap).
167  */
168 static inline struct netfs_group *netfs_get_group(struct netfs_group *netfs_group)
169 {
170         if (netfs_group)
171                 refcount_inc(&netfs_group->ref);
172         return netfs_group;
173 }
174
175 /*
176  * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
177  */
178 static inline void netfs_put_group(struct netfs_group *netfs_group)
179 {
180         if (netfs_group && refcount_dec_and_test(&netfs_group->ref))
181                 netfs_group->free(netfs_group);
182 }
183
184 /*
185  * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
186  */
187 static inline void netfs_put_group_many(struct netfs_group *netfs_group, int nr)
188 {
189         if (netfs_group && refcount_sub_and_test(nr, &netfs_group->ref))
190                 netfs_group->free(netfs_group);
191 }
192
193 /*
194  * fscache-cache.c
195  */
196 #ifdef CONFIG_PROC_FS
197 extern const struct seq_operations fscache_caches_seq_ops;
198 #endif
199 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
200 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
201 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
202 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
203
204 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
205 {
206         return smp_load_acquire(&cache->state);
207 }
208
209 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
210 {
211         return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
212 }
213
214 static inline void fscache_set_cache_state(struct fscache_cache *cache,
215                                            enum fscache_cache_state new_state)
216 {
217         smp_store_release(&cache->state, new_state);
218
219 }
220
221 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
222                                                  enum fscache_cache_state old_state,
223                                                  enum fscache_cache_state new_state)
224 {
225         return try_cmpxchg_release(&cache->state, &old_state, new_state);
226 }
227
228 /*
229  * fscache-cookie.c
230  */
231 extern struct kmem_cache *fscache_cookie_jar;
232 #ifdef CONFIG_PROC_FS
233 extern const struct seq_operations fscache_cookies_seq_ops;
234 #endif
235 extern struct timer_list fscache_cookie_lru_timer;
236
237 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
238 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
239                                         enum fscache_access_trace why);
240
241 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
242                                       enum fscache_cookie_trace where)
243 {
244         trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
245                              where);
246 }
247
248 /*
249  * fscache-main.c
250  */
251 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
252 #ifdef CONFIG_FSCACHE
253 int __init fscache_init(void);
254 void __exit fscache_exit(void);
255 #else
256 static inline int fscache_init(void) { return 0; }
257 static inline void fscache_exit(void) {}
258 #endif
259
260 /*
261  * fscache-proc.c
262  */
263 #ifdef CONFIG_PROC_FS
264 extern int __init fscache_proc_init(void);
265 extern void fscache_proc_cleanup(void);
266 #else
267 #define fscache_proc_init()     (0)
268 #define fscache_proc_cleanup()  do {} while (0)
269 #endif
270
271 /*
272  * fscache-stats.c
273  */
274 #ifdef CONFIG_FSCACHE_STATS
275 extern atomic_t fscache_n_volumes;
276 extern atomic_t fscache_n_volumes_collision;
277 extern atomic_t fscache_n_volumes_nomem;
278 extern atomic_t fscache_n_cookies;
279 extern atomic_t fscache_n_cookies_lru;
280 extern atomic_t fscache_n_cookies_lru_expired;
281 extern atomic_t fscache_n_cookies_lru_removed;
282 extern atomic_t fscache_n_cookies_lru_dropped;
283
284 extern atomic_t fscache_n_acquires;
285 extern atomic_t fscache_n_acquires_ok;
286 extern atomic_t fscache_n_acquires_oom;
287
288 extern atomic_t fscache_n_invalidates;
289
290 extern atomic_t fscache_n_relinquishes;
291 extern atomic_t fscache_n_relinquishes_retire;
292 extern atomic_t fscache_n_relinquishes_dropped;
293
294 extern atomic_t fscache_n_resizes;
295 extern atomic_t fscache_n_resizes_null;
296
297 static inline void fscache_stat(atomic_t *stat)
298 {
299         atomic_inc(stat);
300 }
301
302 static inline void fscache_stat_d(atomic_t *stat)
303 {
304         atomic_dec(stat);
305 }
306
307 #define __fscache_stat(stat) (stat)
308
309 int fscache_stats_show(struct seq_file *m);
310 #else
311
312 #define __fscache_stat(stat) (NULL)
313 #define fscache_stat(stat) do {} while (0)
314 #define fscache_stat_d(stat) do {} while (0)
315
316 static inline int fscache_stats_show(struct seq_file *m) { return 0; }
317 #endif
318
319 /*
320  * fscache-volume.c
321  */
322 #ifdef CONFIG_PROC_FS
323 extern const struct seq_operations fscache_volumes_seq_ops;
324 #endif
325
326 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
327                                           enum fscache_volume_trace where);
328 void fscache_put_volume(struct fscache_volume *volume,
329                         enum fscache_volume_trace where);
330 bool fscache_begin_volume_access(struct fscache_volume *volume,
331                                  struct fscache_cookie *cookie,
332                                  enum fscache_access_trace why);
333 void fscache_create_volume(struct fscache_volume *volume, bool wait);
334
335 /*****************************************************************************/
336 /*
337  * debug tracing
338  */
339 #define dbgprintk(FMT, ...) \
340         printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
341
342 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
343 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
344 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
345
346 #ifdef __KDEBUG
347 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
348 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
349 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
350
351 #elif defined(CONFIG_NETFS_DEBUG)
352 #define _enter(FMT, ...)                        \
353 do {                                            \
354         if (netfs_debug)                        \
355                 kenter(FMT, ##__VA_ARGS__);     \
356 } while (0)
357
358 #define _leave(FMT, ...)                        \
359 do {                                            \
360         if (netfs_debug)                        \
361                 kleave(FMT, ##__VA_ARGS__);     \
362 } while (0)
363
364 #define _debug(FMT, ...)                        \
365 do {                                            \
366         if (netfs_debug)                        \
367                 kdebug(FMT, ##__VA_ARGS__);     \
368 } while (0)
369
370 #else
371 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
372 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
373 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
374 #endif
375
376 /*
377  * assertions
378  */
379 #if 1 /* defined(__KDEBUGALL) */
380
381 #define ASSERT(X)                                                       \
382 do {                                                                    \
383         if (unlikely(!(X))) {                                           \
384                 pr_err("\n");                                   \
385                 pr_err("Assertion failed\n");   \
386                 BUG();                                                  \
387         }                                                               \
388 } while (0)
389
390 #define ASSERTCMP(X, OP, Y)                                             \
391 do {                                                                    \
392         if (unlikely(!((X) OP (Y)))) {                                  \
393                 pr_err("\n");                                   \
394                 pr_err("Assertion failed\n");   \
395                 pr_err("%lx " #OP " %lx is false\n",            \
396                        (unsigned long)(X), (unsigned long)(Y));         \
397                 BUG();                                                  \
398         }                                                               \
399 } while (0)
400
401 #define ASSERTIF(C, X)                                                  \
402 do {                                                                    \
403         if (unlikely((C) && !(X))) {                                    \
404                 pr_err("\n");                                   \
405                 pr_err("Assertion failed\n");   \
406                 BUG();                                                  \
407         }                                                               \
408 } while (0)
409
410 #define ASSERTIFCMP(C, X, OP, Y)                                        \
411 do {                                                                    \
412         if (unlikely((C) && !((X) OP (Y)))) {                           \
413                 pr_err("\n");                                   \
414                 pr_err("Assertion failed\n");   \
415                 pr_err("%lx " #OP " %lx is false\n",            \
416                        (unsigned long)(X), (unsigned long)(Y));         \
417                 BUG();                                                  \
418         }                                                               \
419 } while (0)
420
421 #else
422
423 #define ASSERT(X)                       do {} while (0)
424 #define ASSERTCMP(X, OP, Y)             do {} while (0)
425 #define ASSERTIF(C, X)                  do {} while (0)
426 #define ASSERTIFCMP(C, X, OP, Y)        do {} while (0)
427
428 #endif /* assert or not */