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