1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include <linux/completion.h>
12 #include <linux/buffer_head.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/rcupdate.h>
17 #include <linux/rculist_bl.h>
18 #include <linux/atomic.h>
19 #include <linux/mempool.h>
20 
21 #include "gfs2.h"
22 #include "incore.h"
23 #include "super.h"
24 #include "sys.h"
25 #include "util.h"
26 #include "glock.h"
27 #include "quota.h"
28 #include "recovery.h"
29 #include "dir.h"
30 #include "glops.h"
31 
32 struct workqueue_struct *gfs2_control_wq;
33 
gfs2_init_inode_once(void * foo)34 static void gfs2_init_inode_once(void *foo)
35 {
36 	struct gfs2_inode *ip = foo;
37 
38 	inode_init_once(&ip->i_inode);
39 	atomic_set(&ip->i_sizehint, 0);
40 	init_rwsem(&ip->i_rw_mutex);
41 	INIT_LIST_HEAD(&ip->i_ordered);
42 	ip->i_qadata = NULL;
43 	gfs2_holder_mark_uninitialized(&ip->i_rgd_gh);
44 	memset(&ip->i_res, 0, sizeof(ip->i_res));
45 	RB_CLEAR_NODE(&ip->i_res.rs_node);
46 	ip->i_hash_cache = NULL;
47 	gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
48 }
49 
gfs2_init_glock_once(void * foo)50 static void gfs2_init_glock_once(void *foo)
51 {
52 	struct gfs2_glock *gl = foo;
53 
54 	spin_lock_init(&gl->gl_lockref.lock);
55 	INIT_LIST_HEAD(&gl->gl_holders);
56 	INIT_LIST_HEAD(&gl->gl_lru);
57 	INIT_LIST_HEAD(&gl->gl_ail_list);
58 	atomic_set(&gl->gl_ail_count, 0);
59 	atomic_set(&gl->gl_revokes, 0);
60 }
61 
gfs2_init_gl_aspace_once(void * foo)62 static void gfs2_init_gl_aspace_once(void *foo)
63 {
64 	struct gfs2_glock_aspace *gla = foo;
65 
66 	gfs2_init_glock_once(&gla->glock);
67 	address_space_init_once(&gla->mapping);
68 }
69 
70 /**
71  * init_gfs2_fs - Register GFS2 as a filesystem
72  *
73  * Returns: 0 on success, error code on failure
74  */
75 
init_gfs2_fs(void)76 static int __init init_gfs2_fs(void)
77 {
78 	int error;
79 
80 	gfs2_str2qstr(&gfs2_qdot, ".");
81 	gfs2_str2qstr(&gfs2_qdotdot, "..");
82 	gfs2_quota_hash_init();
83 
84 	error = gfs2_sys_init();
85 	if (error)
86 		return error;
87 
88 	error = list_lru_init(&gfs2_qd_lru);
89 	if (error)
90 		goto fail_lru;
91 
92 	error = gfs2_glock_init();
93 	if (error)
94 		goto fail_glock;
95 
96 	error = -ENOMEM;
97 	gfs2_glock_cachep = kmem_cache_create("gfs2_glock",
98 					      sizeof(struct gfs2_glock),
99 					      0, SLAB_RECLAIM_ACCOUNT,
100 					      gfs2_init_glock_once);
101 	if (!gfs2_glock_cachep)
102 		goto fail_cachep1;
103 
104 	gfs2_glock_aspace_cachep = kmem_cache_create("gfs2_glock(aspace)",
105 					sizeof(struct gfs2_glock_aspace),
106 					0, 0, gfs2_init_gl_aspace_once);
107 
108 	if (!gfs2_glock_aspace_cachep)
109 		goto fail_cachep2;
110 
111 	gfs2_inode_cachep = kmem_cache_create("gfs2_inode",
112 					      sizeof(struct gfs2_inode),
113 					      0,  SLAB_RECLAIM_ACCOUNT|
114 						  SLAB_MEM_SPREAD|
115 						  SLAB_ACCOUNT,
116 					      gfs2_init_inode_once);
117 	if (!gfs2_inode_cachep)
118 		goto fail_cachep3;
119 
120 	gfs2_bufdata_cachep = kmem_cache_create("gfs2_bufdata",
121 						sizeof(struct gfs2_bufdata),
122 					        0, 0, NULL);
123 	if (!gfs2_bufdata_cachep)
124 		goto fail_cachep4;
125 
126 	gfs2_rgrpd_cachep = kmem_cache_create("gfs2_rgrpd",
127 					      sizeof(struct gfs2_rgrpd),
128 					      0, 0, NULL);
129 	if (!gfs2_rgrpd_cachep)
130 		goto fail_cachep5;
131 
132 	gfs2_quotad_cachep = kmem_cache_create("gfs2_quotad",
133 					       sizeof(struct gfs2_quota_data),
134 					       0, SLAB_RECLAIM_ACCOUNT, NULL);
135 	if (!gfs2_quotad_cachep)
136 		goto fail_cachep6;
137 
138 	gfs2_qadata_cachep = kmem_cache_create("gfs2_qadata",
139 					       sizeof(struct gfs2_qadata),
140 					       0, 0, NULL);
141 	if (!gfs2_qadata_cachep)
142 		goto fail_cachep7;
143 
144 	gfs2_trans_cachep = kmem_cache_create("gfs2_trans",
145 					       sizeof(struct gfs2_trans),
146 					       0, 0, NULL);
147 	if (!gfs2_trans_cachep)
148 		goto fail_cachep8;
149 
150 	error = register_shrinker(&gfs2_qd_shrinker, "gfs2-qd");
151 	if (error)
152 		goto fail_shrinker;
153 
154 	error = -ENOMEM;
155 	gfs_recovery_wq = alloc_workqueue("gfs_recovery",
156 					  WQ_MEM_RECLAIM | WQ_FREEZABLE, 0);
157 	if (!gfs_recovery_wq)
158 		goto fail_wq1;
159 
160 	gfs2_control_wq = alloc_workqueue("gfs2_control",
161 					  WQ_UNBOUND | WQ_FREEZABLE, 0);
162 	if (!gfs2_control_wq)
163 		goto fail_wq2;
164 
165 	gfs2_freeze_wq = alloc_workqueue("freeze_workqueue", 0, 0);
166 
167 	if (!gfs2_freeze_wq)
168 		goto fail_wq3;
169 
170 	gfs2_page_pool = mempool_create_page_pool(64, 0);
171 	if (!gfs2_page_pool)
172 		goto fail_mempool;
173 
174 	gfs2_register_debugfs();
175 	error = register_filesystem(&gfs2_fs_type);
176 	if (error)
177 		goto fail_fs1;
178 
179 	error = register_filesystem(&gfs2meta_fs_type);
180 	if (error)
181 		goto fail_fs2;
182 
183 
184 	pr_info("GFS2 installed\n");
185 
186 	return 0;
187 
188 fail_fs2:
189 	unregister_filesystem(&gfs2_fs_type);
190 fail_fs1:
191 	mempool_destroy(gfs2_page_pool);
192 fail_mempool:
193 	destroy_workqueue(gfs2_freeze_wq);
194 fail_wq3:
195 	destroy_workqueue(gfs2_control_wq);
196 fail_wq2:
197 	destroy_workqueue(gfs_recovery_wq);
198 fail_wq1:
199 	unregister_shrinker(&gfs2_qd_shrinker);
200 fail_shrinker:
201 	kmem_cache_destroy(gfs2_trans_cachep);
202 fail_cachep8:
203 	kmem_cache_destroy(gfs2_qadata_cachep);
204 fail_cachep7:
205 	kmem_cache_destroy(gfs2_quotad_cachep);
206 fail_cachep6:
207 	kmem_cache_destroy(gfs2_rgrpd_cachep);
208 fail_cachep5:
209 	kmem_cache_destroy(gfs2_bufdata_cachep);
210 fail_cachep4:
211 	kmem_cache_destroy(gfs2_inode_cachep);
212 fail_cachep3:
213 	kmem_cache_destroy(gfs2_glock_aspace_cachep);
214 fail_cachep2:
215 	kmem_cache_destroy(gfs2_glock_cachep);
216 fail_cachep1:
217 	gfs2_glock_exit();
218 fail_glock:
219 	list_lru_destroy(&gfs2_qd_lru);
220 fail_lru:
221 	gfs2_sys_uninit();
222 	return error;
223 }
224 
225 /**
226  * exit_gfs2_fs - Unregister the file system
227  *
228  */
229 
exit_gfs2_fs(void)230 static void __exit exit_gfs2_fs(void)
231 {
232 	unregister_shrinker(&gfs2_qd_shrinker);
233 	gfs2_glock_exit();
234 	gfs2_unregister_debugfs();
235 	unregister_filesystem(&gfs2_fs_type);
236 	unregister_filesystem(&gfs2meta_fs_type);
237 	destroy_workqueue(gfs_recovery_wq);
238 	destroy_workqueue(gfs2_control_wq);
239 	destroy_workqueue(gfs2_freeze_wq);
240 	list_lru_destroy(&gfs2_qd_lru);
241 
242 	rcu_barrier();
243 
244 	mempool_destroy(gfs2_page_pool);
245 	kmem_cache_destroy(gfs2_trans_cachep);
246 	kmem_cache_destroy(gfs2_qadata_cachep);
247 	kmem_cache_destroy(gfs2_quotad_cachep);
248 	kmem_cache_destroy(gfs2_rgrpd_cachep);
249 	kmem_cache_destroy(gfs2_bufdata_cachep);
250 	kmem_cache_destroy(gfs2_inode_cachep);
251 	kmem_cache_destroy(gfs2_glock_aspace_cachep);
252 	kmem_cache_destroy(gfs2_glock_cachep);
253 
254 	gfs2_sys_uninit();
255 }
256 
257 MODULE_DESCRIPTION("Global File System");
258 MODULE_AUTHOR("Red Hat, Inc.");
259 MODULE_LICENSE("GPL");
260 
261 module_init(init_gfs2_fs);
262 module_exit(exit_gfs2_fs);
263 
264