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