xref: /linux/fs/netfs/main.c (revision 9e6869be49064915edb6c8776b27c376cfdb0df5)
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 unsigned netfs_debug;
24 module_param_named(debug, netfs_debug, uint, S_IWUSR | S_IRUGO);
25 MODULE_PARM_DESC(netfs_debug, "Netfs support debugging mask");
26 
27 static struct kmem_cache *netfs_request_slab;
28 static struct kmem_cache *netfs_subrequest_slab;
29 mempool_t netfs_request_pool;
30 mempool_t netfs_subrequest_pool;
31 mempool_t netfs_folioq_pool;
32 
33 #ifdef CONFIG_PROC_FS
34 LIST_HEAD(netfs_io_requests);
35 DEFINE_SPINLOCK(netfs_proc_lock);
36 
37 static const char *netfs_origins[nr__netfs_io_origin] = {
38 	[NETFS_READAHEAD]		= "RA",
39 	[NETFS_READPAGE]		= "RP",
40 	[NETFS_READ_GAPS]		= "RG",
41 	[NETFS_READ_SINGLE]		= "R1",
42 	[NETFS_READ_FOR_WRITE]		= "RW",
43 	[NETFS_UNBUFFERED_READ]		= "UR",
44 	[NETFS_DIO_READ]		= "DR",
45 	[NETFS_WRITEBACK]		= "WB",
46 	[NETFS_WRITEBACK_SINGLE]	= "W1",
47 	[NETFS_WRITETHROUGH]		= "WT",
48 	[NETFS_UNBUFFERED_WRITE]	= "UW",
49 	[NETFS_DIO_WRITE]		= "DW",
50 	[NETFS_PGPRIV2_COPY_TO_CACHE]	= "2C",
51 };
52 
53 /*
54  * Generate a list of I/O requests in /proc/fs/netfs/requests
55  */
56 static int netfs_requests_seq_show(struct seq_file *m, void *v)
57 {
58 	struct netfs_io_request *rreq;
59 
60 	if (v == &netfs_io_requests) {
61 		seq_puts(m,
62 			 "REQUEST  OR REF FLAG ERR  OPS COVERAGE\n"
63 			 "======== == === ==== ==== === =========\n"
64 			 );
65 		return 0;
66 	}
67 
68 	rreq = list_entry(v, struct netfs_io_request, proc_link);
69 	seq_printf(m,
70 		   "%08x %s %3d %4lx %4ld %3d @%04llx %llx/%llx",
71 		   rreq->debug_id,
72 		   netfs_origins[rreq->origin],
73 		   refcount_read(&rreq->ref),
74 		   rreq->flags,
75 		   rreq->error,
76 		   0,
77 		   rreq->start, rreq->submitted, rreq->len);
78 	seq_putc(m, '\n');
79 	return 0;
80 }
81 
82 static void *netfs_requests_seq_start(struct seq_file *m, loff_t *_pos)
83 	__acquires(rcu)
84 {
85 	rcu_read_lock();
86 	return seq_list_start_head(&netfs_io_requests, *_pos);
87 }
88 
89 static void *netfs_requests_seq_next(struct seq_file *m, void *v, loff_t *_pos)
90 {
91 	return seq_list_next(v, &netfs_io_requests, _pos);
92 }
93 
94 static void netfs_requests_seq_stop(struct seq_file *m, void *v)
95 	__releases(rcu)
96 {
97 	rcu_read_unlock();
98 }
99 
100 static const struct seq_operations netfs_requests_seq_ops = {
101 	.start  = netfs_requests_seq_start,
102 	.next   = netfs_requests_seq_next,
103 	.stop   = netfs_requests_seq_stop,
104 	.show   = netfs_requests_seq_show,
105 };
106 #endif /* CONFIG_PROC_FS */
107 
108 static int __init netfs_init(void)
109 {
110 	int ret = -ENOMEM;
111 
112 	if (mempool_init_kmalloc_pool(&netfs_folioq_pool, 100, sizeof(struct folio_queue)) < 0)
113 		goto error_folioq_pool;
114 
115 	netfs_request_slab = kmem_cache_create("netfs_request",
116 					       sizeof(struct netfs_io_request), 0,
117 					       SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
118 					       NULL);
119 	if (!netfs_request_slab)
120 		goto error_req;
121 
122 	if (mempool_init_slab_pool(&netfs_request_pool, 100, netfs_request_slab) < 0)
123 		goto error_reqpool;
124 
125 	netfs_subrequest_slab = kmem_cache_create("netfs_subrequest",
126 						  sizeof(struct netfs_io_subrequest) + 16, 0,
127 						  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
128 						  NULL);
129 	if (!netfs_subrequest_slab)
130 		goto error_subreq;
131 
132 	if (mempool_init_slab_pool(&netfs_subrequest_pool, 100, netfs_subrequest_slab) < 0)
133 		goto error_subreqpool;
134 
135 #ifdef CONFIG_PROC_FS
136 	if (!proc_mkdir("fs/netfs", NULL))
137 		goto error_proc;
138 	if (!proc_create_seq("fs/netfs/requests", S_IFREG | 0444, NULL,
139 			     &netfs_requests_seq_ops))
140 		goto error_procfile;
141 #endif
142 #ifdef CONFIG_FSCACHE_STATS
143 	if (!proc_create_single("fs/netfs/stats", S_IFREG | 0444, NULL,
144 				netfs_stats_show))
145 		goto error_procfile;
146 #endif
147 
148 	ret = fscache_init();
149 	if (ret < 0)
150 		goto error_fscache;
151 	return 0;
152 
153 error_fscache:
154 #ifdef CONFIG_PROC_FS
155 error_procfile:
156 	remove_proc_subtree("fs/netfs", NULL);
157 error_proc:
158 #endif
159 	mempool_exit(&netfs_subrequest_pool);
160 error_subreqpool:
161 	kmem_cache_destroy(netfs_subrequest_slab);
162 error_subreq:
163 	mempool_exit(&netfs_request_pool);
164 error_reqpool:
165 	kmem_cache_destroy(netfs_request_slab);
166 error_req:
167 	mempool_exit(&netfs_folioq_pool);
168 error_folioq_pool:
169 	return ret;
170 }
171 fs_initcall(netfs_init);
172 
173 static void __exit netfs_exit(void)
174 {
175 	fscache_exit();
176 	remove_proc_subtree("fs/netfs", NULL);
177 	mempool_exit(&netfs_subrequest_pool);
178 	kmem_cache_destroy(netfs_subrequest_slab);
179 	mempool_exit(&netfs_request_pool);
180 	kmem_cache_destroy(netfs_request_slab);
181 	mempool_exit(&netfs_folioq_pool);
182 }
183 module_exit(netfs_exit);
184