This option enables support for network filesystems, including
          helpers for high-level buffered I/O, abstracting out read
          segmentation, local caching and transparent huge page support.
+
+config NETFS_STATS
+       bool "Gather statistical information on local caching"
+       depends on NETFS_SUPPORT && PROC_FS
+       help
+         This option causes statistical information to be gathered on local
+         caching and exported through file:
+
+               /proc/fs/fscache/stats
+
+         The gathering of statistics adds a certain amount of overhead to
+         execution as there are a quite a few stats gathered, and on a
+         multi-CPU system these may be on cachelines that keep bouncing
+         between CPUs.  On the other hand, the stats are very useful for
+         debugging purposes.  Saying 'Y' here is recommended.
 
 # SPDX-License-Identifier: GPL-2.0
 
-netfs-y := \
-       read_helper.o
+netfs-y := read_helper.o stats.o
 
 obj-$(CONFIG_NETFS_SUPPORT) := netfs.o
 
  */
 extern unsigned int netfs_debug;
 
+/*
+ * stats.c
+ */
+#ifdef CONFIG_NETFS_STATS
+extern atomic_t netfs_n_rh_readahead;
+extern atomic_t netfs_n_rh_readpage;
+extern atomic_t netfs_n_rh_rreq;
+extern atomic_t netfs_n_rh_sreq;
+extern atomic_t netfs_n_rh_download;
+extern atomic_t netfs_n_rh_download_done;
+extern atomic_t netfs_n_rh_download_failed;
+extern atomic_t netfs_n_rh_download_instead;
+extern atomic_t netfs_n_rh_read;
+extern atomic_t netfs_n_rh_read_done;
+extern atomic_t netfs_n_rh_read_failed;
+extern atomic_t netfs_n_rh_zero;
+extern atomic_t netfs_n_rh_short_read;
+extern atomic_t netfs_n_rh_write;
+extern atomic_t netfs_n_rh_write_done;
+extern atomic_t netfs_n_rh_write_failed;
+
+
+static inline void netfs_stat(atomic_t *stat)
+{
+       atomic_inc(stat);
+}
+
+static inline void netfs_stat_d(atomic_t *stat)
+{
+       atomic_dec(stat);
+}
+
+#else
 #define netfs_stat(x) do {} while(0)
 #define netfs_stat_d(x) do {} while(0)
+#endif
 
 /*****************************************************************************/
 /*
 
                refcount_set(&rreq->usage, 1);
                __set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
                ops->init_rreq(rreq, file);
+               netfs_stat(&netfs_n_rh_rreq);
        }
 
        return rreq;
                rreq->netfs_ops->cleanup(rreq->mapping, rreq->netfs_priv);
        trace_netfs_rreq(rreq, netfs_rreq_trace_free);
        kfree(rreq);
+       netfs_stat_d(&netfs_n_rh_rreq);
 }
 
 static void netfs_put_read_request(struct netfs_read_request *rreq, bool was_async)
                refcount_set(&subreq->usage, 2);
                subreq->rreq = rreq;
                netfs_get_read_request(rreq);
+               netfs_stat(&netfs_n_rh_sreq);
        }
 
        return subreq;
 
        trace_netfs_sreq(subreq, netfs_sreq_trace_free);
        kfree(subreq);
+       netfs_stat_d(&netfs_n_rh_sreq);
        netfs_put_read_request(rreq, was_async);
 }
 
 static void netfs_fill_with_zeroes(struct netfs_read_request *rreq,
                                   struct netfs_read_subrequest *subreq)
 {
+       netfs_stat(&netfs_n_rh_zero);
        __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
        netfs_subreq_terminated(subreq, 0, false);
 }
 static void netfs_read_from_server(struct netfs_read_request *rreq,
                                   struct netfs_read_subrequest *subreq)
 {
+       netfs_stat(&netfs_n_rh_download);
        rreq->netfs_ops->issue_op(subreq);
 }
 
        __clear_bit(NETFS_SREQ_SHORT_READ, &subreq->flags);
        __set_bit(NETFS_SREQ_SEEK_DATA_READ, &subreq->flags);
 
+       netfs_stat(&netfs_n_rh_short_read);
        trace_netfs_sreq(subreq, netfs_sreq_trace_resubmit_short);
 
        netfs_get_read_subrequest(subreq);
                                break;
                        subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
                        subreq->error = 0;
+                       netfs_stat(&netfs_n_rh_download_instead);
                        trace_netfs_sreq(subreq, netfs_sreq_trace_download_instead);
                        netfs_get_read_subrequest(subreq);
                        atomic_inc(&rreq->nr_rd_ops);
               subreq->debug_index, subreq->start, subreq->flags,
               transferred_or_error);
 
+       switch (subreq->source) {
+       case NETFS_READ_FROM_CACHE:
+               netfs_stat(&netfs_n_rh_read_done);
+               break;
+       case NETFS_DOWNLOAD_FROM_SERVER:
+               netfs_stat(&netfs_n_rh_download_done);
+               break;
+       default:
+               break;
+       }
+
        if (IS_ERR_VALUE(transferred_or_error)) {
                subreq->error = transferred_or_error;
                goto failed;
 
 failed:
        if (subreq->source == NETFS_READ_FROM_CACHE) {
+               netfs_stat(&netfs_n_rh_read_failed);
                set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
        } else {
+               netfs_stat(&netfs_n_rh_download_failed);
                set_bit(NETFS_RREQ_FAILED, &rreq->flags);
                rreq->error = subreq->error;
        }
        rreq->start     = readahead_pos(ractl);
        rreq->len       = readahead_length(ractl);
 
+       netfs_stat(&netfs_n_rh_readahead);
        trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
                         netfs_read_trace_readahead);
 
        rreq->start     = page_index(page) * PAGE_SIZE;
        rreq->len       = thp_size(page);
 
+       netfs_stat(&netfs_n_rh_readpage);
        trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
 
        netfs_get_read_request(rreq);
 
--- /dev/null
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Netfs support statistics
+ *
+ * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ */
+
+#include <linux/export.h>
+#include <linux/seq_file.h>
+#include <linux/netfs.h>
+#include "internal.h"
+
+atomic_t netfs_n_rh_readahead;
+atomic_t netfs_n_rh_readpage;
+atomic_t netfs_n_rh_rreq;
+atomic_t netfs_n_rh_sreq;
+atomic_t netfs_n_rh_download;
+atomic_t netfs_n_rh_download_done;
+atomic_t netfs_n_rh_download_failed;
+atomic_t netfs_n_rh_download_instead;
+atomic_t netfs_n_rh_read;
+atomic_t netfs_n_rh_read_done;
+atomic_t netfs_n_rh_read_failed;
+atomic_t netfs_n_rh_zero;
+atomic_t netfs_n_rh_short_read;
+atomic_t netfs_n_rh_write;
+atomic_t netfs_n_rh_write_done;
+atomic_t netfs_n_rh_write_failed;
+
+void netfs_stats_show(struct seq_file *m)
+{
+       seq_printf(m, "RdHelp : RA=%u RP=%u rr=%u sr=%u\n",
+                  atomic_read(&netfs_n_rh_readahead),
+                  atomic_read(&netfs_n_rh_readpage),
+                  atomic_read(&netfs_n_rh_rreq),
+                  atomic_read(&netfs_n_rh_sreq));
+       seq_printf(m, "RdHelp : ZR=%u sh=%u\n",
+                  atomic_read(&netfs_n_rh_zero),
+                  atomic_read(&netfs_n_rh_short_read));
+       seq_printf(m, "RdHelp : DL=%u ds=%u df=%u di=%u\n",
+                  atomic_read(&netfs_n_rh_download),
+                  atomic_read(&netfs_n_rh_download_done),
+                  atomic_read(&netfs_n_rh_download_failed),
+                  atomic_read(&netfs_n_rh_download_instead));
+       seq_printf(m, "RdHelp : RD=%u rs=%u rf=%u\n",
+                  atomic_read(&netfs_n_rh_read),
+                  atomic_read(&netfs_n_rh_read_done),
+                  atomic_read(&netfs_n_rh_read_failed));
+       seq_printf(m, "RdHelp : WR=%u ws=%u wf=%u\n",
+                  atomic_read(&netfs_n_rh_write),
+                  atomic_read(&netfs_n_rh_write_done),
+                  atomic_read(&netfs_n_rh_write_failed));
+}
+EXPORT_SYMBOL(netfs_stats_show);
 
                          void *);
 
 extern void netfs_subreq_terminated(struct netfs_read_subrequest *, ssize_t, bool);
+extern void netfs_stats_show(struct seq_file *);
 
 #endif /* _LINUX_NETFS_H */