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/folio_queue.h> 11 #include <linux/netfs.h> 12 #include <linux/fscache.h> 13 #include <linux/fscache-cache.h> 14 #include <trace/events/netfs.h> 15 #include <trace/events/fscache.h> 16 17 #ifdef pr_fmt 18 #undef pr_fmt 19 #endif 20 21 #define pr_fmt(fmt) "netfs: " fmt 22 23 /* 24 * buffered_read.c 25 */ 26 void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error, bool was_async); 27 int netfs_prefetch_for_write(struct file *file, struct folio *folio, 28 size_t offset, size_t len); 29 30 /* 31 * main.c 32 */ 33 extern unsigned int netfs_debug; 34 extern struct list_head netfs_io_requests; 35 extern spinlock_t netfs_proc_lock; 36 extern mempool_t netfs_request_pool; 37 extern mempool_t netfs_subrequest_pool; 38 39 #ifdef CONFIG_PROC_FS 40 static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq) 41 { 42 spin_lock(&netfs_proc_lock); 43 list_add_tail_rcu(&rreq->proc_link, &netfs_io_requests); 44 spin_unlock(&netfs_proc_lock); 45 } 46 static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq) 47 { 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); 52 } 53 } 54 #else 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) {} 57 #endif 58 59 /* 60 * misc.c 61 */ 62 struct folio_queue *netfs_buffer_make_space(struct netfs_io_request *rreq, 63 enum netfs_folioq_trace trace); 64 void netfs_reset_iter(struct netfs_io_subrequest *subreq); 65 66 /* 67 * objects.c 68 */ 69 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping, 70 struct file *file, 71 loff_t start, size_t len, 72 enum netfs_io_origin origin); 73 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what); 74 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async); 75 void netfs_put_request(struct netfs_io_request *rreq, bool was_async, 76 enum netfs_rreq_ref_trace what); 77 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq); 78 79 static inline void netfs_see_request(struct netfs_io_request *rreq, 80 enum netfs_rreq_ref_trace what) 81 { 82 trace_netfs_rreq_ref(rreq->debug_id, refcount_read(&rreq->ref), what); 83 } 84 85 static inline void netfs_see_subrequest(struct netfs_io_subrequest *subreq, 86 enum netfs_sreq_ref_trace what) 87 { 88 trace_netfs_sreq_ref(subreq->rreq->debug_id, subreq->debug_index, 89 refcount_read(&subreq->ref), what); 90 } 91 92 /* 93 * read_collect.c 94 */ 95 void netfs_read_collection_worker(struct work_struct *work); 96 void netfs_wake_read_collector(struct netfs_io_request *rreq); 97 void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error, bool was_async); 98 ssize_t netfs_wait_for_read(struct netfs_io_request *rreq); 99 void netfs_wait_for_pause(struct netfs_io_request *rreq); 100 101 /* 102 * read_pgpriv2.c 103 */ 104 void netfs_pgpriv2_copy_to_cache(struct netfs_io_request *rreq, struct folio *folio); 105 void netfs_pgpriv2_end_copy_to_cache(struct netfs_io_request *rreq); 106 bool netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request *wreq); 107 108 /* 109 * read_retry.c 110 */ 111 void netfs_retry_reads(struct netfs_io_request *rreq); 112 void netfs_unlock_abandoned_read_pages(struct netfs_io_request *rreq); 113 114 /* 115 * stats.c 116 */ 117 #ifdef CONFIG_NETFS_STATS 118 extern atomic_t netfs_n_rh_dio_read; 119 extern atomic_t netfs_n_rh_readahead; 120 extern atomic_t netfs_n_rh_read_folio; 121 extern atomic_t netfs_n_rh_read_single; 122 extern atomic_t netfs_n_rh_rreq; 123 extern atomic_t netfs_n_rh_sreq; 124 extern atomic_t netfs_n_rh_download; 125 extern atomic_t netfs_n_rh_download_done; 126 extern atomic_t netfs_n_rh_download_failed; 127 extern atomic_t netfs_n_rh_download_instead; 128 extern atomic_t netfs_n_rh_read; 129 extern atomic_t netfs_n_rh_read_done; 130 extern atomic_t netfs_n_rh_read_failed; 131 extern atomic_t netfs_n_rh_zero; 132 extern atomic_t netfs_n_rh_short_read; 133 extern atomic_t netfs_n_rh_write; 134 extern atomic_t netfs_n_rh_write_begin; 135 extern atomic_t netfs_n_rh_write_done; 136 extern atomic_t netfs_n_rh_write_failed; 137 extern atomic_t netfs_n_rh_write_zskip; 138 extern atomic_t netfs_n_rh_retry_read_req; 139 extern atomic_t netfs_n_rh_retry_read_subreq; 140 extern atomic_t netfs_n_wh_buffered_write; 141 extern atomic_t netfs_n_wh_writethrough; 142 extern atomic_t netfs_n_wh_dio_write; 143 extern atomic_t netfs_n_wh_writepages; 144 extern atomic_t netfs_n_wh_copy_to_cache; 145 extern atomic_t netfs_n_wh_wstream_conflict; 146 extern atomic_t netfs_n_wh_upload; 147 extern atomic_t netfs_n_wh_upload_done; 148 extern atomic_t netfs_n_wh_upload_failed; 149 extern atomic_t netfs_n_wh_write; 150 extern atomic_t netfs_n_wh_write_done; 151 extern atomic_t netfs_n_wh_write_failed; 152 extern atomic_t netfs_n_wh_retry_write_req; 153 extern atomic_t netfs_n_wh_retry_write_subreq; 154 extern atomic_t netfs_n_wb_lock_skip; 155 extern atomic_t netfs_n_wb_lock_wait; 156 extern atomic_t netfs_n_folioq; 157 158 int netfs_stats_show(struct seq_file *m, void *v); 159 160 static inline void netfs_stat(atomic_t *stat) 161 { 162 atomic_inc(stat); 163 } 164 165 static inline void netfs_stat_d(atomic_t *stat) 166 { 167 atomic_dec(stat); 168 } 169 170 #else 171 #define netfs_stat(x) do {} while(0) 172 #define netfs_stat_d(x) do {} while(0) 173 #endif 174 175 /* 176 * write_collect.c 177 */ 178 int netfs_folio_written_back(struct folio *folio); 179 void netfs_write_collection_worker(struct work_struct *work); 180 void netfs_wake_write_collector(struct netfs_io_request *wreq, bool was_async); 181 182 /* 183 * write_issue.c 184 */ 185 struct netfs_io_request *netfs_create_write_req(struct address_space *mapping, 186 struct file *file, 187 loff_t start, 188 enum netfs_io_origin origin); 189 void netfs_reissue_write(struct netfs_io_stream *stream, 190 struct netfs_io_subrequest *subreq, 191 struct iov_iter *source); 192 void netfs_issue_write(struct netfs_io_request *wreq, 193 struct netfs_io_stream *stream); 194 size_t netfs_advance_write(struct netfs_io_request *wreq, 195 struct netfs_io_stream *stream, 196 loff_t start, size_t len, bool to_eof); 197 struct netfs_io_request *netfs_begin_writethrough(struct kiocb *iocb, size_t len); 198 int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc, 199 struct folio *folio, size_t copied, bool to_page_end, 200 struct folio **writethrough_cache); 201 int netfs_end_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc, 202 struct folio *writethrough_cache); 203 int netfs_unbuffered_write(struct netfs_io_request *wreq, bool may_wait, size_t len); 204 205 /* 206 * write_retry.c 207 */ 208 void netfs_retry_writes(struct netfs_io_request *wreq); 209 210 /* 211 * Miscellaneous functions. 212 */ 213 static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx) 214 { 215 #if IS_ENABLED(CONFIG_FSCACHE) 216 struct fscache_cookie *cookie = ctx->cache; 217 218 return fscache_cookie_valid(cookie) && cookie->cache_priv && 219 fscache_cookie_enabled(cookie); 220 #else 221 return false; 222 #endif 223 } 224 225 /* 226 * Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap). 227 */ 228 static inline struct netfs_group *netfs_get_group(struct netfs_group *netfs_group) 229 { 230 if (netfs_group && netfs_group != NETFS_FOLIO_COPY_TO_CACHE) 231 refcount_inc(&netfs_group->ref); 232 return netfs_group; 233 } 234 235 /* 236 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap). 237 */ 238 static inline void netfs_put_group(struct netfs_group *netfs_group) 239 { 240 if (netfs_group && 241 netfs_group != NETFS_FOLIO_COPY_TO_CACHE && 242 refcount_dec_and_test(&netfs_group->ref)) 243 netfs_group->free(netfs_group); 244 } 245 246 /* 247 * Dispose of a netfs group attached to a dirty page (e.g. a ceph snap). 248 */ 249 static inline void netfs_put_group_many(struct netfs_group *netfs_group, int nr) 250 { 251 if (netfs_group && 252 netfs_group != NETFS_FOLIO_COPY_TO_CACHE && 253 refcount_sub_and_test(nr, &netfs_group->ref)) 254 netfs_group->free(netfs_group); 255 } 256 257 /* 258 * fscache-cache.c 259 */ 260 #ifdef CONFIG_PROC_FS 261 extern const struct seq_operations fscache_caches_seq_ops; 262 #endif 263 bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); 264 void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); 265 struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache); 266 void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where); 267 268 static inline enum fscache_cache_state fscache_cache_state(const struct fscache_cache *cache) 269 { 270 return smp_load_acquire(&cache->state); 271 } 272 273 static inline bool fscache_cache_is_live(const struct fscache_cache *cache) 274 { 275 return fscache_cache_state(cache) == FSCACHE_CACHE_IS_ACTIVE; 276 } 277 278 static inline void fscache_set_cache_state(struct fscache_cache *cache, 279 enum fscache_cache_state new_state) 280 { 281 smp_store_release(&cache->state, new_state); 282 283 } 284 285 static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache, 286 enum fscache_cache_state old_state, 287 enum fscache_cache_state new_state) 288 { 289 return try_cmpxchg_release(&cache->state, &old_state, new_state); 290 } 291 292 /* 293 * fscache-cookie.c 294 */ 295 extern struct kmem_cache *fscache_cookie_jar; 296 #ifdef CONFIG_PROC_FS 297 extern const struct seq_operations fscache_cookies_seq_ops; 298 #endif 299 extern struct timer_list fscache_cookie_lru_timer; 300 301 extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix); 302 extern bool fscache_begin_cookie_access(struct fscache_cookie *cookie, 303 enum fscache_access_trace why); 304 305 static inline void fscache_see_cookie(struct fscache_cookie *cookie, 306 enum fscache_cookie_trace where) 307 { 308 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref), 309 where); 310 } 311 312 /* 313 * fscache-main.c 314 */ 315 extern unsigned int fscache_hash(unsigned int salt, const void *data, size_t len); 316 #ifdef CONFIG_FSCACHE 317 int __init fscache_init(void); 318 void __exit fscache_exit(void); 319 #else 320 static inline int fscache_init(void) { return 0; } 321 static inline void fscache_exit(void) {} 322 #endif 323 324 /* 325 * fscache-proc.c 326 */ 327 #ifdef CONFIG_PROC_FS 328 extern int __init fscache_proc_init(void); 329 extern void fscache_proc_cleanup(void); 330 #else 331 #define fscache_proc_init() (0) 332 #define fscache_proc_cleanup() do {} while (0) 333 #endif 334 335 /* 336 * fscache-stats.c 337 */ 338 #ifdef CONFIG_FSCACHE_STATS 339 extern atomic_t fscache_n_volumes; 340 extern atomic_t fscache_n_volumes_collision; 341 extern atomic_t fscache_n_volumes_nomem; 342 extern atomic_t fscache_n_cookies; 343 extern atomic_t fscache_n_cookies_lru; 344 extern atomic_t fscache_n_cookies_lru_expired; 345 extern atomic_t fscache_n_cookies_lru_removed; 346 extern atomic_t fscache_n_cookies_lru_dropped; 347 348 extern atomic_t fscache_n_acquires; 349 extern atomic_t fscache_n_acquires_ok; 350 extern atomic_t fscache_n_acquires_oom; 351 352 extern atomic_t fscache_n_invalidates; 353 354 extern atomic_t fscache_n_relinquishes; 355 extern atomic_t fscache_n_relinquishes_retire; 356 extern atomic_t fscache_n_relinquishes_dropped; 357 358 extern atomic_t fscache_n_resizes; 359 extern atomic_t fscache_n_resizes_null; 360 361 static inline void fscache_stat(atomic_t *stat) 362 { 363 atomic_inc(stat); 364 } 365 366 static inline void fscache_stat_d(atomic_t *stat) 367 { 368 atomic_dec(stat); 369 } 370 371 #define __fscache_stat(stat) (stat) 372 373 int fscache_stats_show(struct seq_file *m); 374 #else 375 376 #define __fscache_stat(stat) (NULL) 377 #define fscache_stat(stat) do {} while (0) 378 #define fscache_stat_d(stat) do {} while (0) 379 380 static inline int fscache_stats_show(struct seq_file *m) { return 0; } 381 #endif 382 383 /* 384 * fscache-volume.c 385 */ 386 #ifdef CONFIG_PROC_FS 387 extern const struct seq_operations fscache_volumes_seq_ops; 388 #endif 389 390 struct fscache_volume *fscache_get_volume(struct fscache_volume *volume, 391 enum fscache_volume_trace where); 392 bool fscache_begin_volume_access(struct fscache_volume *volume, 393 struct fscache_cookie *cookie, 394 enum fscache_access_trace why); 395 void fscache_create_volume(struct fscache_volume *volume, bool wait); 396 397 /*****************************************************************************/ 398 /* 399 * debug tracing 400 */ 401 #define dbgprintk(FMT, ...) \ 402 printk("[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__) 403 404 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__) 405 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__) 406 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__) 407 408 #ifdef __KDEBUG 409 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__) 410 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__) 411 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__) 412 413 #elif defined(CONFIG_NETFS_DEBUG) 414 #define _enter(FMT, ...) \ 415 do { \ 416 if (netfs_debug) \ 417 kenter(FMT, ##__VA_ARGS__); \ 418 } while (0) 419 420 #define _leave(FMT, ...) \ 421 do { \ 422 if (netfs_debug) \ 423 kleave(FMT, ##__VA_ARGS__); \ 424 } while (0) 425 426 #define _debug(FMT, ...) \ 427 do { \ 428 if (netfs_debug) \ 429 kdebug(FMT, ##__VA_ARGS__); \ 430 } while (0) 431 432 #else 433 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__) 434 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__) 435 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__) 436 #endif 437 438 /* 439 * assertions 440 */ 441 #if 1 /* defined(__KDEBUGALL) */ 442 443 #define ASSERT(X) \ 444 do { \ 445 if (unlikely(!(X))) { \ 446 pr_err("\n"); \ 447 pr_err("Assertion failed\n"); \ 448 BUG(); \ 449 } \ 450 } while (0) 451 452 #define ASSERTCMP(X, OP, Y) \ 453 do { \ 454 if (unlikely(!((X) OP (Y)))) { \ 455 pr_err("\n"); \ 456 pr_err("Assertion failed\n"); \ 457 pr_err("%lx " #OP " %lx is false\n", \ 458 (unsigned long)(X), (unsigned long)(Y)); \ 459 BUG(); \ 460 } \ 461 } while (0) 462 463 #define ASSERTIF(C, X) \ 464 do { \ 465 if (unlikely((C) && !(X))) { \ 466 pr_err("\n"); \ 467 pr_err("Assertion failed\n"); \ 468 BUG(); \ 469 } \ 470 } while (0) 471 472 #define ASSERTIFCMP(C, X, OP, Y) \ 473 do { \ 474 if (unlikely((C) && !((X) OP (Y)))) { \ 475 pr_err("\n"); \ 476 pr_err("Assertion failed\n"); \ 477 pr_err("%lx " #OP " %lx is false\n", \ 478 (unsigned long)(X), (unsigned long)(Y)); \ 479 BUG(); \ 480 } \ 481 } while (0) 482 483 #else 484 485 #define ASSERT(X) do {} while (0) 486 #define ASSERTCMP(X, OP, Y) do {} while (0) 487 #define ASSERTIF(C, X) do {} while (0) 488 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0) 489 490 #endif /* assert or not */ 491