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;
36 extern struct list_head netfs_io_requests;
37 extern spinlock_t netfs_proc_lock;
40 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq)
42 spin_lock(&netfs_proc_lock);
43 list_add_tail_rcu(&rreq->proc_link, &netfs_io_requests);
44 spin_unlock(&netfs_proc_lock);
46 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq)
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);
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) {}
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,
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);
75 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
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);
85 static inline void netfs_see_request(struct netfs_io_request *rreq,
86 enum netfs_rreq_ref_trace what)
88 trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what);
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;
114 int netfs_stats_show(struct seq_file *m, void *v);
116 static inline void netfs_stat(atomic_t *stat)
121 static inline void netfs_stat_d(atomic_t *stat)
127 #define netfs_stat(x) do {} while(0)
128 #define netfs_stat_d(x) do {} while(0)
132 * Miscellaneous functions.
134 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
136 #if IS_ENABLED(CONFIG_FSCACHE)
137 struct fscache_cookie *cookie = ctx->cache;
139 return fscache_cookie_valid(cookie) && cookie->cache_priv &&
140 fscache_cookie_enabled(cookie);
149 #ifdef CONFIG_PROC_FS
150 extern const struct seq_operations fscache_caches_seq_ops;
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);
157 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
159 return smp_load_acquire(&cache->state);
162 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
164 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
167 static inline void fscache_set_cache_state(struct fscache_cache *cache,
168 enum fscache_cache_state new_state)
170 smp_store_release(&cache->state, new_state);
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)
178 return try_cmpxchg_release(&cache->state, &old_state, new_state);
184 extern struct kmem_cache *fscache_cookie_jar;
185 #ifdef CONFIG_PROC_FS
186 extern const struct seq_operations fscache_cookies_seq_ops;
188 extern struct timer_list fscache_cookie_lru_timer;
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);
194 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
195 enum fscache_cookie_trace where)
197 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
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);
209 static inline int fscache_init(void) { return 0; }
210 static inline void fscache_exit(void) {}
216 #ifdef CONFIG_PROC_FS
217 extern int __init fscache_proc_init(void);
218 extern void fscache_proc_cleanup(void);
220 #define fscache_proc_init() (0)
221 #define fscache_proc_cleanup() do {} while (0)
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;
237 extern atomic_t fscache_n_acquires;
238 extern atomic_t fscache_n_acquires_ok;
239 extern atomic_t fscache_n_acquires_oom;
241 extern atomic_t fscache_n_invalidates;
243 extern atomic_t fscache_n_relinquishes;
244 extern atomic_t fscache_n_relinquishes_retire;
245 extern atomic_t fscache_n_relinquishes_dropped;
247 extern atomic_t fscache_n_resizes;
248 extern atomic_t fscache_n_resizes_null;
250 static inline void fscache_stat(atomic_t *stat)
255 static inline void fscache_stat_d(atomic_t *stat)
260 #define __fscache_stat(stat) (stat)
262 int fscache_stats_show(struct seq_file *m);
265 #define __fscache_stat(stat) (NULL)
266 #define fscache_stat(stat) do {} while (0)
267 #define fscache_stat_d(stat) do {} while (0)
269 static inline int fscache_stats_show(struct seq_file *m) { return 0; }
275 #ifdef CONFIG_PROC_FS
276 extern const struct seq_operations fscache_volumes_seq_ops;
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);
288 /*****************************************************************************/
292 #define dbgprintk(FMT, ...) \
293 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
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__)
300 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
301 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
302 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
304 #elif defined(CONFIG_NETFS_DEBUG)
305 #define _enter(FMT, ...) \
308 kenter(FMT, ##__VA_ARGS__); \
311 #define _leave(FMT, ...) \
314 kleave(FMT, ##__VA_ARGS__); \
317 #define _debug(FMT, ...) \
320 kdebug(FMT, ##__VA_ARGS__); \
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__)
332 #if 1 /* defined(__KDEBUGALL) */
336 if (unlikely(!(X))) { \
338 pr_err("Assertion failed\n"); \
343 #define ASSERTCMP(X, OP, Y) \
345 if (unlikely(!((X) OP (Y)))) { \
347 pr_err("Assertion failed\n"); \
348 pr_err("%lx " #OP " %lx is false\n", \
349 (unsigned long)(X), (unsigned long)(Y)); \
354 #define ASSERTIF(C, X) \
356 if (unlikely((C) && !(X))) { \
358 pr_err("Assertion failed\n"); \
363 #define ASSERTIFCMP(C, X, OP, Y) \
365 if (unlikely((C) && !((X) OP (Y)))) { \
367 pr_err("Assertion failed\n"); \
368 pr_err("%lx " #OP " %lx is false\n", \
369 (unsigned long)(X), (unsigned long)(Y)); \
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)
381 #endif /* assert or not */