1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Internal definitions for network filesystem support
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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>
20 #define pr_fmt(fmt) "netfs: " fmt
25 void netfs_rreq_unlock_folios(struct netfs_io_request *rreq);
30 int netfs_begin_read(struct netfs_io_request *rreq, bool sync);
35 extern unsigned int netfs_debug;
40 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
42 loff_t start, size_t len,
43 enum netfs_io_origin origin);
44 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what);
45 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async);
46 void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
47 enum netfs_rreq_ref_trace what);
48 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq);
50 static inline void netfs_see_request(struct netfs_io_request *rreq,
51 enum netfs_rreq_ref_trace what)
53 trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what);
59 #ifdef CONFIG_NETFS_STATS
60 extern atomic_t netfs_n_rh_readahead;
61 extern atomic_t netfs_n_rh_readpage;
62 extern atomic_t netfs_n_rh_rreq;
63 extern atomic_t netfs_n_rh_sreq;
64 extern atomic_t netfs_n_rh_download;
65 extern atomic_t netfs_n_rh_download_done;
66 extern atomic_t netfs_n_rh_download_failed;
67 extern atomic_t netfs_n_rh_download_instead;
68 extern atomic_t netfs_n_rh_read;
69 extern atomic_t netfs_n_rh_read_done;
70 extern atomic_t netfs_n_rh_read_failed;
71 extern atomic_t netfs_n_rh_zero;
72 extern atomic_t netfs_n_rh_short_read;
73 extern atomic_t netfs_n_rh_write;
74 extern atomic_t netfs_n_rh_write_begin;
75 extern atomic_t netfs_n_rh_write_done;
76 extern atomic_t netfs_n_rh_write_failed;
77 extern atomic_t netfs_n_rh_write_zskip;
80 static inline void netfs_stat(atomic_t *stat)
85 static inline void netfs_stat_d(atomic_t *stat)
91 #define netfs_stat(x) do {} while(0)
92 #define netfs_stat_d(x) do {} while(0)
96 * Miscellaneous functions.
98 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
100 #if IS_ENABLED(CONFIG_FSCACHE)
101 struct fscache_cookie *cookie = ctx->cache;
103 return fscache_cookie_valid(cookie) && cookie->cache_priv &&
104 fscache_cookie_enabled(cookie);
113 #ifdef CONFIG_PROC_FS
114 extern const struct seq_operations fscache_caches_seq_ops;
116 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
117 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
118 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
119 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
121 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
123 return smp_load_acquire(&cache->state);
126 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
128 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
131 static inline void fscache_set_cache_state(struct fscache_cache *cache,
132 enum fscache_cache_state new_state)
134 smp_store_release(&cache->state, new_state);
138 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
139 enum fscache_cache_state old_state,
140 enum fscache_cache_state new_state)
142 return try_cmpxchg_release(&cache->state, &old_state, new_state);
148 extern struct kmem_cache *fscache_cookie_jar;
149 #ifdef CONFIG_PROC_FS
150 extern const struct seq_operations fscache_cookies_seq_ops;
152 extern struct timer_list fscache_cookie_lru_timer;
154 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
155 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
156 enum fscache_access_trace why);
158 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
159 enum fscache_cookie_trace where)
161 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
168 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
173 #ifdef CONFIG_PROC_FS
174 extern int __init fscache_proc_init(void);
175 extern void fscache_proc_cleanup(void);
177 #define fscache_proc_init() (0)
178 #define fscache_proc_cleanup() do {} while (0)
184 #ifdef CONFIG_FSCACHE_STATS
185 extern atomic_t fscache_n_volumes;
186 extern atomic_t fscache_n_volumes_collision;
187 extern atomic_t fscache_n_volumes_nomem;
188 extern atomic_t fscache_n_cookies;
189 extern atomic_t fscache_n_cookies_lru;
190 extern atomic_t fscache_n_cookies_lru_expired;
191 extern atomic_t fscache_n_cookies_lru_removed;
192 extern atomic_t fscache_n_cookies_lru_dropped;
194 extern atomic_t fscache_n_acquires;
195 extern atomic_t fscache_n_acquires_ok;
196 extern atomic_t fscache_n_acquires_oom;
198 extern atomic_t fscache_n_invalidates;
200 extern atomic_t fscache_n_relinquishes;
201 extern atomic_t fscache_n_relinquishes_retire;
202 extern atomic_t fscache_n_relinquishes_dropped;
204 extern atomic_t fscache_n_resizes;
205 extern atomic_t fscache_n_resizes_null;
207 static inline void fscache_stat(atomic_t *stat)
212 static inline void fscache_stat_d(atomic_t *stat)
217 #define __fscache_stat(stat) (stat)
219 int fscache_stats_show(struct seq_file *m, void *v);
222 #define __fscache_stat(stat) (NULL)
223 #define fscache_stat(stat) do {} while (0)
224 #define fscache_stat_d(stat) do {} while (0)
230 #ifdef CONFIG_PROC_FS
231 extern const struct seq_operations fscache_volumes_seq_ops;
234 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
235 enum fscache_volume_trace where);
236 void fscache_put_volume(struct fscache_volume *volume,
237 enum fscache_volume_trace where);
238 bool fscache_begin_volume_access(struct fscache_volume *volume,
239 struct fscache_cookie *cookie,
240 enum fscache_access_trace why);
241 void fscache_create_volume(struct fscache_volume *volume, bool wait);
243 /*****************************************************************************/
247 #define dbgprintk(FMT, ...) \
248 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
250 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
251 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
252 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
255 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
256 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
257 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
259 #elif defined(CONFIG_NETFS_DEBUG)
260 #define _enter(FMT, ...) \
263 kenter(FMT, ##__VA_ARGS__); \
266 #define _leave(FMT, ...) \
269 kleave(FMT, ##__VA_ARGS__); \
272 #define _debug(FMT, ...) \
275 kdebug(FMT, ##__VA_ARGS__); \
279 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
280 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
281 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
287 #if 1 /* defined(__KDEBUGALL) */
291 if (unlikely(!(X))) { \
293 pr_err("Assertion failed\n"); \
298 #define ASSERTCMP(X, OP, Y) \
300 if (unlikely(!((X) OP (Y)))) { \
302 pr_err("Assertion failed\n"); \
303 pr_err("%lx " #OP " %lx is false\n", \
304 (unsigned long)(X), (unsigned long)(Y)); \
309 #define ASSERTIF(C, X) \
311 if (unlikely((C) && !(X))) { \
313 pr_err("Assertion failed\n"); \
318 #define ASSERTIFCMP(C, X, OP, Y) \
320 if (unlikely((C) && !((X) OP (Y)))) { \
322 pr_err("Assertion failed\n"); \
323 pr_err("%lx " #OP " %lx is false\n", \
324 (unsigned long)(X), (unsigned long)(Y)); \
331 #define ASSERT(X) do {} while (0)
332 #define ASSERTCMP(X, OP, Y) do {} while (0)
333 #define ASSERTIF(C, X) do {} while (0)
334 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
336 #endif /* assert or not */