xref: /openbmc/linux/fs/gfs2/main.c (revision 27ba4deb)
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 
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_trunc_list);
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 
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 }
60 
61 static void gfs2_init_gl_aspace_once(void *foo)
62 {
63 	struct gfs2_glock *gl = foo;
64 	struct address_space *mapping = (struct address_space *)(gl + 1);
65 
66 	gfs2_init_glock_once(gl);
67 	address_space_init_once(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 
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, 0,
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) +
106 					sizeof(struct address_space),
107 					0, 0, gfs2_init_gl_aspace_once);
108 
109 	if (!gfs2_glock_aspace_cachep)
110 		goto fail_cachep2;
111 
112 	gfs2_inode_cachep = kmem_cache_create("gfs2_inode",
113 					      sizeof(struct gfs2_inode),
114 					      0,  SLAB_RECLAIM_ACCOUNT|
115 						  SLAB_MEM_SPREAD|
116 						  SLAB_ACCOUNT,
117 					      gfs2_init_inode_once);
118 	if (!gfs2_inode_cachep)
119 		goto fail_cachep3;
120 
121 	gfs2_bufdata_cachep = kmem_cache_create("gfs2_bufdata",
122 						sizeof(struct gfs2_bufdata),
123 					        0, 0, NULL);
124 	if (!gfs2_bufdata_cachep)
125 		goto fail_cachep4;
126 
127 	gfs2_rgrpd_cachep = kmem_cache_create("gfs2_rgrpd",
128 					      sizeof(struct gfs2_rgrpd),
129 					      0, 0, NULL);
130 	if (!gfs2_rgrpd_cachep)
131 		goto fail_cachep5;
132 
133 	gfs2_quotad_cachep = kmem_cache_create("gfs2_quotad",
134 					       sizeof(struct gfs2_quota_data),
135 					       0, 0, NULL);
136 	if (!gfs2_quotad_cachep)
137 		goto fail_cachep6;
138 
139 	gfs2_qadata_cachep = kmem_cache_create("gfs2_qadata",
140 					       sizeof(struct gfs2_qadata),
141 					       0, 0, NULL);
142 	if (!gfs2_qadata_cachep)
143 		goto fail_cachep7;
144 
145 	error = register_shrinker(&gfs2_qd_shrinker);
146 	if (error)
147 		goto fail_shrinker;
148 
149 	error = register_filesystem(&gfs2_fs_type);
150 	if (error)
151 		goto fail_fs1;
152 
153 	error = register_filesystem(&gfs2meta_fs_type);
154 	if (error)
155 		goto fail_fs2;
156 
157 	error = -ENOMEM;
158 	gfs_recovery_wq = alloc_workqueue("gfs_recovery",
159 					  WQ_MEM_RECLAIM | WQ_FREEZABLE, 0);
160 	if (!gfs_recovery_wq)
161 		goto fail_wq1;
162 
163 	gfs2_control_wq = alloc_workqueue("gfs2_control",
164 					  WQ_UNBOUND | WQ_FREEZABLE, 0);
165 	if (!gfs2_control_wq)
166 		goto fail_wq2;
167 
168 	gfs2_freeze_wq = alloc_workqueue("freeze_workqueue", 0, 0);
169 
170 	if (!gfs2_freeze_wq)
171 		goto fail_wq3;
172 
173 	gfs2_page_pool = mempool_create_page_pool(64, 0);
174 	if (!gfs2_page_pool)
175 		goto fail_mempool;
176 
177 	gfs2_register_debugfs();
178 
179 	pr_info("GFS2 installed\n");
180 
181 	return 0;
182 
183 fail_mempool:
184 	destroy_workqueue(gfs2_freeze_wq);
185 fail_wq3:
186 	destroy_workqueue(gfs2_control_wq);
187 fail_wq2:
188 	destroy_workqueue(gfs_recovery_wq);
189 fail_wq1:
190 	unregister_filesystem(&gfs2meta_fs_type);
191 fail_fs2:
192 	unregister_filesystem(&gfs2_fs_type);
193 fail_fs1:
194 	unregister_shrinker(&gfs2_qd_shrinker);
195 fail_shrinker:
196 	kmem_cache_destroy(gfs2_qadata_cachep);
197 fail_cachep7:
198 	kmem_cache_destroy(gfs2_quotad_cachep);
199 fail_cachep6:
200 	kmem_cache_destroy(gfs2_rgrpd_cachep);
201 fail_cachep5:
202 	kmem_cache_destroy(gfs2_bufdata_cachep);
203 fail_cachep4:
204 	kmem_cache_destroy(gfs2_inode_cachep);
205 fail_cachep3:
206 	kmem_cache_destroy(gfs2_glock_aspace_cachep);
207 fail_cachep2:
208 	kmem_cache_destroy(gfs2_glock_cachep);
209 fail_cachep1:
210 	gfs2_glock_exit();
211 fail_glock:
212 	list_lru_destroy(&gfs2_qd_lru);
213 fail_lru:
214 	gfs2_sys_uninit();
215 	return error;
216 }
217 
218 /**
219  * exit_gfs2_fs - Unregister the file system
220  *
221  */
222 
223 static void __exit exit_gfs2_fs(void)
224 {
225 	unregister_shrinker(&gfs2_qd_shrinker);
226 	gfs2_glock_exit();
227 	gfs2_unregister_debugfs();
228 	unregister_filesystem(&gfs2_fs_type);
229 	unregister_filesystem(&gfs2meta_fs_type);
230 	destroy_workqueue(gfs_recovery_wq);
231 	destroy_workqueue(gfs2_control_wq);
232 	destroy_workqueue(gfs2_freeze_wq);
233 	list_lru_destroy(&gfs2_qd_lru);
234 
235 	rcu_barrier();
236 
237 	mempool_destroy(gfs2_page_pool);
238 	kmem_cache_destroy(gfs2_qadata_cachep);
239 	kmem_cache_destroy(gfs2_quotad_cachep);
240 	kmem_cache_destroy(gfs2_rgrpd_cachep);
241 	kmem_cache_destroy(gfs2_bufdata_cachep);
242 	kmem_cache_destroy(gfs2_inode_cachep);
243 	kmem_cache_destroy(gfs2_glock_aspace_cachep);
244 	kmem_cache_destroy(gfs2_glock_cachep);
245 
246 	gfs2_sys_uninit();
247 }
248 
249 MODULE_DESCRIPTION("Global File System");
250 MODULE_AUTHOR("Red Hat, Inc.");
251 MODULE_LICENSE("GPL");
252 
253 module_init(init_gfs2_fs);
254 module_exit(exit_gfs2_fs);
255 
256