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);
26 int netfs_prefetch_for_write(struct file *file, struct folio *folio,
27 size_t offset, size_t len);
32 int netfs_begin_read(struct netfs_io_request *rreq, bool sync);
37 extern unsigned int netfs_debug;
38 extern struct list_head netfs_io_requests;
39 extern spinlock_t netfs_proc_lock;
42 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq)
44 spin_lock(&netfs_proc_lock);
45 list_add_tail_rcu(&rreq->proc_link, &netfs_io_requests);
46 spin_unlock(&netfs_proc_lock);
48 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq)
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);
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) {}
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,
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);
77 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
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);
87 static inline void netfs_see_request(struct netfs_io_request *rreq,
88 enum netfs_rreq_ref_trace what)
90 trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what);
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);
105 #ifdef CONFIG_NETFS_STATS
106 extern atomic_t netfs_n_rh_dio_read;
107 extern atomic_t netfs_n_rh_readahead;
108 extern atomic_t netfs_n_rh_readpage;
109 extern atomic_t netfs_n_rh_rreq;
110 extern atomic_t netfs_n_rh_sreq;
111 extern atomic_t netfs_n_rh_download;
112 extern atomic_t netfs_n_rh_download_done;
113 extern atomic_t netfs_n_rh_download_failed;
114 extern atomic_t netfs_n_rh_download_instead;
115 extern atomic_t netfs_n_rh_read;
116 extern atomic_t netfs_n_rh_read_done;
117 extern atomic_t netfs_n_rh_read_failed;
118 extern atomic_t netfs_n_rh_zero;
119 extern atomic_t netfs_n_rh_short_read;
120 extern atomic_t netfs_n_rh_write;
121 extern atomic_t netfs_n_rh_write_begin;
122 extern atomic_t netfs_n_rh_write_done;
123 extern atomic_t netfs_n_rh_write_failed;
124 extern atomic_t netfs_n_rh_write_zskip;
125 extern atomic_t netfs_n_wh_upload;
126 extern atomic_t netfs_n_wh_upload_done;
127 extern atomic_t netfs_n_wh_upload_failed;
128 extern atomic_t netfs_n_wh_write;
129 extern atomic_t netfs_n_wh_write_done;
130 extern atomic_t netfs_n_wh_write_failed;
132 int netfs_stats_show(struct seq_file *m, void *v);
134 static inline void netfs_stat(atomic_t *stat)
139 static inline void netfs_stat_d(atomic_t *stat)
145 #define netfs_stat(x) do {} while(0)
146 #define netfs_stat_d(x) do {} while(0)
150 * Miscellaneous functions.
152 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
154 #if IS_ENABLED(CONFIG_FSCACHE)
155 struct fscache_cookie *cookie = ctx->cache;
157 return fscache_cookie_valid(cookie) && cookie->cache_priv &&
158 fscache_cookie_enabled(cookie);
165 * Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap).
167 static inline struct netfs_group *netfs_get_group(struct netfs_group *netfs_group)
170 refcount_inc(&netfs_group->ref);
175 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
177 static inline void netfs_put_group(struct netfs_group *netfs_group)
179 if (netfs_group && refcount_dec_and_test(&netfs_group->ref))
180 netfs_group->free(netfs_group);
184 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
186 static inline void netfs_put_group_many(struct netfs_group *netfs_group, int nr)
188 if (netfs_group && refcount_sub_and_test(nr, &netfs_group->ref))
189 netfs_group->free(netfs_group);
195 #ifdef CONFIG_PROC_FS
196 extern const struct seq_operations fscache_caches_seq_ops;
198 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
199 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
200 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
201 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
203 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
205 return smp_load_acquire(&cache->state);
208 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
210 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
213 static inline void fscache_set_cache_state(struct fscache_cache *cache,
214 enum fscache_cache_state new_state)
216 smp_store_release(&cache->state, new_state);
220 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
221 enum fscache_cache_state old_state,
222 enum fscache_cache_state new_state)
224 return try_cmpxchg_release(&cache->state, &old_state, new_state);
230 extern struct kmem_cache *fscache_cookie_jar;
231 #ifdef CONFIG_PROC_FS
232 extern const struct seq_operations fscache_cookies_seq_ops;
234 extern struct timer_list fscache_cookie_lru_timer;
236 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
237 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
238 enum fscache_access_trace why);
240 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
241 enum fscache_cookie_trace where)
243 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
250 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
251 #ifdef CONFIG_FSCACHE
252 int __init fscache_init(void);
253 void __exit fscache_exit(void);
255 static inline int fscache_init(void) { return 0; }
256 static inline void fscache_exit(void) {}
262 #ifdef CONFIG_PROC_FS
263 extern int __init fscache_proc_init(void);
264 extern void fscache_proc_cleanup(void);
266 #define fscache_proc_init() (0)
267 #define fscache_proc_cleanup() do {} while (0)
273 #ifdef CONFIG_FSCACHE_STATS
274 extern atomic_t fscache_n_volumes;
275 extern atomic_t fscache_n_volumes_collision;
276 extern atomic_t fscache_n_volumes_nomem;
277 extern atomic_t fscache_n_cookies;
278 extern atomic_t fscache_n_cookies_lru;
279 extern atomic_t fscache_n_cookies_lru_expired;
280 extern atomic_t fscache_n_cookies_lru_removed;
281 extern atomic_t fscache_n_cookies_lru_dropped;
283 extern atomic_t fscache_n_acquires;
284 extern atomic_t fscache_n_acquires_ok;
285 extern atomic_t fscache_n_acquires_oom;
287 extern atomic_t fscache_n_invalidates;
289 extern atomic_t fscache_n_relinquishes;
290 extern atomic_t fscache_n_relinquishes_retire;
291 extern atomic_t fscache_n_relinquishes_dropped;
293 extern atomic_t fscache_n_resizes;
294 extern atomic_t fscache_n_resizes_null;
296 static inline void fscache_stat(atomic_t *stat)
301 static inline void fscache_stat_d(atomic_t *stat)
306 #define __fscache_stat(stat) (stat)
308 int fscache_stats_show(struct seq_file *m);
311 #define __fscache_stat(stat) (NULL)
312 #define fscache_stat(stat) do {} while (0)
313 #define fscache_stat_d(stat) do {} while (0)
315 static inline int fscache_stats_show(struct seq_file *m) { return 0; }
321 #ifdef CONFIG_PROC_FS
322 extern const struct seq_operations fscache_volumes_seq_ops;
325 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
326 enum fscache_volume_trace where);
327 void fscache_put_volume(struct fscache_volume *volume,
328 enum fscache_volume_trace where);
329 bool fscache_begin_volume_access(struct fscache_volume *volume,
330 struct fscache_cookie *cookie,
331 enum fscache_access_trace why);
332 void fscache_create_volume(struct fscache_volume *volume, bool wait);
334 /*****************************************************************************/
338 #define dbgprintk(FMT, ...) \
339 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
341 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
342 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
343 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
346 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
347 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
348 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
350 #elif defined(CONFIG_NETFS_DEBUG)
351 #define _enter(FMT, ...) \
354 kenter(FMT, ##__VA_ARGS__); \
357 #define _leave(FMT, ...) \
360 kleave(FMT, ##__VA_ARGS__); \
363 #define _debug(FMT, ...) \
366 kdebug(FMT, ##__VA_ARGS__); \
370 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
371 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
372 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
378 #if 1 /* defined(__KDEBUGALL) */
382 if (unlikely(!(X))) { \
384 pr_err("Assertion failed\n"); \
389 #define ASSERTCMP(X, OP, Y) \
391 if (unlikely(!((X) OP (Y)))) { \
393 pr_err("Assertion failed\n"); \
394 pr_err("%lx " #OP " %lx is false\n", \
395 (unsigned long)(X), (unsigned long)(Y)); \
400 #define ASSERTIF(C, X) \
402 if (unlikely((C) && !(X))) { \
404 pr_err("Assertion failed\n"); \
409 #define ASSERTIFCMP(C, X, OP, Y) \
411 if (unlikely((C) && !((X) OP (Y)))) { \
413 pr_err("Assertion failed\n"); \
414 pr_err("%lx " #OP " %lx is false\n", \
415 (unsigned long)(X), (unsigned long)(Y)); \
422 #define ASSERT(X) do {} while (0)
423 #define ASSERTCMP(X, OP, Y) do {} while (0)
424 #define ASSERTIF(C, X) do {} while (0)
425 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
427 #endif /* assert or not */