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.
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;
40 extern mempool_t netfs_request_pool;
41 extern mempool_t netfs_subrequest_pool;
44 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq)
46 spin_lock(&netfs_proc_lock);
47 list_add_tail_rcu(&rreq->proc_link, &netfs_io_requests);
48 spin_unlock(&netfs_proc_lock);
50 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq)
52 if (!list_empty(&rreq->proc_link)) {
53 spin_lock(&netfs_proc_lock);
54 list_del_rcu(&rreq->proc_link);
55 spin_unlock(&netfs_proc_lock);
59 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq) {}
60 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq) {}
66 #define NETFS_FLAG_PUT_MARK BIT(0)
67 #define NETFS_FLAG_PAGECACHE_MARK BIT(1)
68 int netfs_xa_store_and_mark(struct xarray *xa, unsigned long index,
69 struct folio *folio, unsigned int flags,
71 int netfs_add_folios_to_buffer(struct xarray *buffer,
72 struct address_space *mapping,
73 pgoff_t index, pgoff_t to, gfp_t gfp_mask);
74 void netfs_clear_buffer(struct xarray *buffer);
79 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
81 loff_t start, size_t len,
82 enum netfs_io_origin origin);
83 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what);
84 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async);
85 void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
86 enum netfs_rreq_ref_trace what);
87 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq);
89 static inline void netfs_see_request(struct netfs_io_request *rreq,
90 enum netfs_rreq_ref_trace what)
92 trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what);
98 #ifdef CONFIG_NETFS_STATS
99 extern atomic_t netfs_n_rh_dio_read;
100 extern atomic_t netfs_n_rh_readahead;
101 extern atomic_t netfs_n_rh_read_folio;
102 extern atomic_t netfs_n_rh_rreq;
103 extern atomic_t netfs_n_rh_sreq;
104 extern atomic_t netfs_n_rh_download;
105 extern atomic_t netfs_n_rh_download_done;
106 extern atomic_t netfs_n_rh_download_failed;
107 extern atomic_t netfs_n_rh_download_instead;
108 extern atomic_t netfs_n_rh_read;
109 extern atomic_t netfs_n_rh_read_done;
110 extern atomic_t netfs_n_rh_read_failed;
111 extern atomic_t netfs_n_rh_zero;
112 extern atomic_t netfs_n_rh_short_read;
113 extern atomic_t netfs_n_rh_write;
114 extern atomic_t netfs_n_rh_write_begin;
115 extern atomic_t netfs_n_rh_write_done;
116 extern atomic_t netfs_n_rh_write_failed;
117 extern atomic_t netfs_n_rh_write_zskip;
118 extern atomic_t netfs_n_wh_buffered_write;
119 extern atomic_t netfs_n_wh_writethrough;
120 extern atomic_t netfs_n_wh_dio_write;
121 extern atomic_t netfs_n_wh_writepages;
122 extern atomic_t netfs_n_wh_wstream_conflict;
123 extern atomic_t netfs_n_wh_upload;
124 extern atomic_t netfs_n_wh_upload_done;
125 extern atomic_t netfs_n_wh_upload_failed;
126 extern atomic_t netfs_n_wh_write;
127 extern atomic_t netfs_n_wh_write_done;
128 extern atomic_t netfs_n_wh_write_failed;
130 int netfs_stats_show(struct seq_file *m, void *v);
132 static inline void netfs_stat(atomic_t *stat)
137 static inline void netfs_stat_d(atomic_t *stat)
143 #define netfs_stat(x) do {} while(0)
144 #define netfs_stat_d(x) do {} while(0)
150 int netfs_folio_written_back(struct folio *folio);
151 void netfs_write_collection_worker(struct work_struct *work);
152 void netfs_wake_write_collector(struct netfs_io_request *wreq, bool was_async);
157 struct netfs_io_request *netfs_create_write_req(struct address_space *mapping,
160 enum netfs_io_origin origin);
161 void netfs_reissue_write(struct netfs_io_stream *stream,
162 struct netfs_io_subrequest *subreq);
163 int netfs_advance_write(struct netfs_io_request *wreq,
164 struct netfs_io_stream *stream,
165 loff_t start, size_t len, bool to_eof);
166 struct netfs_io_request *netfs_begin_writethrough(struct kiocb *iocb, size_t len);
167 int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
168 struct folio *folio, size_t copied, bool to_page_end,
169 struct folio **writethrough_cache);
170 int netfs_end_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
171 struct folio *writethrough_cache);
172 int netfs_unbuffered_write(struct netfs_io_request *wreq, bool may_wait, size_t len);
175 * Miscellaneous functions.
177 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
179 #if IS_ENABLED(CONFIG_FSCACHE)
180 struct fscache_cookie *cookie = ctx->cache;
182 return fscache_cookie_valid(cookie) && cookie->cache_priv &&
183 fscache_cookie_enabled(cookie);
190 * Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap).
192 static inline struct netfs_group *netfs_get_group(struct netfs_group *netfs_group)
194 if (netfs_group && netfs_group != NETFS_FOLIO_COPY_TO_CACHE)
195 refcount_inc(&netfs_group->ref);
200 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
202 static inline void netfs_put_group(struct netfs_group *netfs_group)
205 netfs_group != NETFS_FOLIO_COPY_TO_CACHE &&
206 refcount_dec_and_test(&netfs_group->ref))
207 netfs_group->free(netfs_group);
211 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
213 static inline void netfs_put_group_many(struct netfs_group *netfs_group, int nr)
216 netfs_group != NETFS_FOLIO_COPY_TO_CACHE &&
217 refcount_sub_and_test(nr, &netfs_group->ref))
218 netfs_group->free(netfs_group);
224 #ifdef CONFIG_PROC_FS
225 extern const struct seq_operations fscache_caches_seq_ops;
227 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
228 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why);
229 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache);
230 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where);
232 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache)
234 return smp_load_acquire(&cache->state);
237 static inline bool fscache_cache_is_live(const struct fscache_cache *cache)
239 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE;
242 static inline void fscache_set_cache_state(struct fscache_cache *cache,
243 enum fscache_cache_state new_state)
245 smp_store_release(&cache->state, new_state);
249 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
250 enum fscache_cache_state old_state,
251 enum fscache_cache_state new_state)
253 return try_cmpxchg_release(&cache->state, &old_state, new_state);
259 extern struct kmem_cache *fscache_cookie_jar;
260 #ifdef CONFIG_PROC_FS
261 extern const struct seq_operations fscache_cookies_seq_ops;
263 extern struct timer_list fscache_cookie_lru_timer;
265 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
266 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
267 enum fscache_access_trace why);
269 static inline void fscache_see_cookie(struct fscache_cookie *cookie,
270 enum fscache_cookie_trace where)
272 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
279 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len);
280 #ifdef CONFIG_FSCACHE
281 int __init fscache_init(void);
282 void __exit fscache_exit(void);
284 static inline int fscache_init(void) { return 0; }
285 static inline void fscache_exit(void) {}
291 #ifdef CONFIG_PROC_FS
292 extern int __init fscache_proc_init(void);
293 extern void fscache_proc_cleanup(void);
295 #define fscache_proc_init() (0)
296 #define fscache_proc_cleanup() do {} while (0)
302 #ifdef CONFIG_FSCACHE_STATS
303 extern atomic_t fscache_n_volumes;
304 extern atomic_t fscache_n_volumes_collision;
305 extern atomic_t fscache_n_volumes_nomem;
306 extern atomic_t fscache_n_cookies;
307 extern atomic_t fscache_n_cookies_lru;
308 extern atomic_t fscache_n_cookies_lru_expired;
309 extern atomic_t fscache_n_cookies_lru_removed;
310 extern atomic_t fscache_n_cookies_lru_dropped;
312 extern atomic_t fscache_n_acquires;
313 extern atomic_t fscache_n_acquires_ok;
314 extern atomic_t fscache_n_acquires_oom;
316 extern atomic_t fscache_n_invalidates;
318 extern atomic_t fscache_n_relinquishes;
319 extern atomic_t fscache_n_relinquishes_retire;
320 extern atomic_t fscache_n_relinquishes_dropped;
322 extern atomic_t fscache_n_resizes;
323 extern atomic_t fscache_n_resizes_null;
325 static inline void fscache_stat(atomic_t *stat)
330 static inline void fscache_stat_d(atomic_t *stat)
335 #define __fscache_stat(stat) (stat)
337 int fscache_stats_show(struct seq_file *m);
340 #define __fscache_stat(stat) (NULL)
341 #define fscache_stat(stat) do {} while (0)
342 #define fscache_stat_d(stat) do {} while (0)
344 static inline int fscache_stats_show(struct seq_file *m) { return 0; }
350 #ifdef CONFIG_PROC_FS
351 extern const struct seq_operations fscache_volumes_seq_ops;
354 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume,
355 enum fscache_volume_trace where);
356 void fscache_put_volume(struct fscache_volume *volume,
357 enum fscache_volume_trace where);
358 bool fscache_begin_volume_access(struct fscache_volume *volume,
359 struct fscache_cookie *cookie,
360 enum fscache_access_trace why);
361 void fscache_create_volume(struct fscache_volume *volume, bool wait);
363 /*****************************************************************************/
367 #define dbgprintk(FMT, ...) \
368 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
370 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
371 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
372 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
375 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
376 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
377 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
379 #elif defined(CONFIG_NETFS_DEBUG)
380 #define _enter(FMT, ...) \
383 kenter(FMT, ##__VA_ARGS__); \
386 #define _leave(FMT, ...) \
389 kleave(FMT, ##__VA_ARGS__); \
392 #define _debug(FMT, ...) \
395 kdebug(FMT, ##__VA_ARGS__); \
399 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
400 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
401 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
407 #if 1 /* defined(__KDEBUGALL) */
411 if (unlikely(!(X))) { \
413 pr_err("Assertion failed\n"); \
418 #define ASSERTCMP(X, OP, Y) \
420 if (unlikely(!((X) OP (Y)))) { \
422 pr_err("Assertion failed\n"); \
423 pr_err("%lx " #OP " %lx is false\n", \
424 (unsigned long)(X), (unsigned long)(Y)); \
429 #define ASSERTIF(C, X) \
431 if (unlikely((C) && !(X))) { \
433 pr_err("Assertion failed\n"); \
438 #define ASSERTIFCMP(C, X, OP, Y) \
440 if (unlikely((C) && !((X) OP (Y)))) { \
442 pr_err("Assertion failed\n"); \
443 pr_err("%lx " #OP " %lx is false\n", \
444 (unsigned long)(X), (unsigned long)(Y)); \
451 #define ASSERT(X) do {} while (0)
452 #define ASSERTCMP(X, OP, Y) do {} while (0)
453 #define ASSERTIF(C, X) do {} while (0)
454 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
456 #endif /* assert or not */