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