xref: /linux/fs/netfs/main.c (revision c434e25b62f8efcfbb6bf1f7ce55960206c1137e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Miscellaneous bits for the netfs support library.
3  *
4  * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/export.h>
10 #include <linux/mempool.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include "internal.h"
14 #define CREATE_TRACE_POINTS
15 #include <trace/events/netfs.h>
16 
17 MODULE_DESCRIPTION("Network fs support");
18 MODULE_AUTHOR("Red Hat, Inc.");
19 MODULE_LICENSE("GPL");
20 
21 EXPORT_TRACEPOINT_SYMBOL(netfs_sreq);
22 
23 static struct kmem_cache *netfs_request_slab;
24 static struct kmem_cache *netfs_subrequest_slab;
25 mempool_t netfs_request_pool;
26 mempool_t netfs_subrequest_pool;
27 
28 #ifdef CONFIG_PROC_FS
29 LIST_HEAD(netfs_io_requests);
30 DEFINE_SPINLOCK(netfs_proc_lock);
31 
32 static const char *netfs_origins[nr__netfs_io_origin] = {
33 	[NETFS_READAHEAD]		= "RA",
34 	[NETFS_READPAGE]		= "RP",
35 	[NETFS_READ_FOR_WRITE]		= "RW",
36 	[NETFS_COPY_TO_CACHE]		= "CC",
37 	[NETFS_WRITEBACK]		= "WB",
38 	[NETFS_WRITETHROUGH]		= "WT",
39 	[NETFS_UNBUFFERED_WRITE]	= "UW",
40 	[NETFS_DIO_READ]		= "DR",
41 	[NETFS_DIO_WRITE]		= "DW",
42 };
43 
44 /*
45  * Generate a list of I/O requests in /proc/fs/netfs/requests
46  */
47 static int netfs_requests_seq_show(struct seq_file *m, void *v)
48 {
49 	struct netfs_io_request *rreq;
50 
51 	if (v == &netfs_io_requests) {
52 		seq_puts(m,
53 			 "REQUEST  OR REF FL ERR  OPS COVERAGE\n"
54 			 "======== == === == ==== === =========\n"
55 			 );
56 		return 0;
57 	}
58 
59 	rreq = list_entry(v, struct netfs_io_request, proc_link);
60 	seq_printf(m,
61 		   "%08x %s %3d %2lx %4d %3d @%04llx %llx/%llx",
62 		   rreq->debug_id,
63 		   netfs_origins[rreq->origin],
64 		   refcount_read(&rreq->ref),
65 		   rreq->flags,
66 		   rreq->error,
67 		   atomic_read(&rreq->nr_outstanding),
68 		   rreq->start, rreq->submitted, rreq->len);
69 	seq_putc(m, '\n');
70 	return 0;
71 }
72 
73 static void *netfs_requests_seq_start(struct seq_file *m, loff_t *_pos)
74 	__acquires(rcu)
75 {
76 	rcu_read_lock();
77 	return seq_list_start_head(&netfs_io_requests, *_pos);
78 }
79 
80 static void *netfs_requests_seq_next(struct seq_file *m, void *v, loff_t *_pos)
81 {
82 	return seq_list_next(v, &netfs_io_requests, _pos);
83 }
84 
85 static void netfs_requests_seq_stop(struct seq_file *m, void *v)
86 	__releases(rcu)
87 {
88 	rcu_read_unlock();
89 }
90 
91 static const struct seq_operations netfs_requests_seq_ops = {
92 	.start  = netfs_requests_seq_start,
93 	.next   = netfs_requests_seq_next,
94 	.stop   = netfs_requests_seq_stop,
95 	.show   = netfs_requests_seq_show,
96 };
97 #endif /* CONFIG_PROC_FS */
98 
99 static int __init netfs_init(void)
100 {
101 	int ret = -ENOMEM;
102 
103 	netfs_request_slab = kmem_cache_create("netfs_request",
104 					       sizeof(struct netfs_io_request), 0,
105 					       SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
106 					       NULL);
107 	if (!netfs_request_slab)
108 		goto error_req;
109 
110 	if (mempool_init_slab_pool(&netfs_request_pool, 100, netfs_request_slab) < 0)
111 		goto error_reqpool;
112 
113 	netfs_subrequest_slab = kmem_cache_create("netfs_subrequest",
114 						  sizeof(struct netfs_io_subrequest), 0,
115 						  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
116 						  NULL);
117 	if (!netfs_subrequest_slab)
118 		goto error_subreq;
119 
120 	if (mempool_init_slab_pool(&netfs_subrequest_pool, 100, netfs_subrequest_slab) < 0)
121 		goto error_subreqpool;
122 
123 	if (!proc_mkdir("fs/netfs", NULL))
124 		goto error_proc;
125 	if (!proc_create_seq("fs/netfs/requests", S_IFREG | 0444, NULL,
126 			     &netfs_requests_seq_ops))
127 		goto error_procfile;
128 #ifdef CONFIG_FSCACHE_STATS
129 	if (!proc_create_single("fs/netfs/stats", S_IFREG | 0444, NULL,
130 				netfs_stats_show))
131 		goto error_procfile;
132 #endif
133 
134 	ret = fscache_init();
135 	if (ret < 0)
136 		goto error_fscache;
137 	return 0;
138 
139 error_fscache:
140 error_procfile:
141 	remove_proc_entry("fs/netfs", NULL);
142 error_proc:
143 	mempool_exit(&netfs_subrequest_pool);
144 error_subreqpool:
145 	kmem_cache_destroy(netfs_subrequest_slab);
146 error_subreq:
147 	mempool_exit(&netfs_request_pool);
148 error_reqpool:
149 	kmem_cache_destroy(netfs_request_slab);
150 error_req:
151 	return ret;
152 }
153 fs_initcall(netfs_init);
154 
155 static void __exit netfs_exit(void)
156 {
157 	fscache_exit();
158 	remove_proc_entry("fs/netfs", NULL);
159 	mempool_exit(&netfs_subrequest_pool);
160 	kmem_cache_destroy(netfs_subrequest_slab);
161 	mempool_exit(&netfs_request_pool);
162 	kmem_cache_destroy(netfs_request_slab);
163 }
164 module_exit(netfs_exit);
165