1/*
2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/completion.h>
15#include <linux/buffer_head.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/gfs2_ondisk.h>
19#include <linux/rcupdate.h>
20#include <linux/rculist_bl.h>
21#include <linux/atomic.h>
22#include <linux/mempool.h>
23
24#include "gfs2.h"
25#include "incore.h"
26#include "super.h"
27#include "sys.h"
28#include "util.h"
29#include "glock.h"
30#include "quota.h"
31#include "recovery.h"
32#include "dir.h"
33#include "glops.h"
34
35struct workqueue_struct *gfs2_control_wq;
36
37static void gfs2_init_inode_once(void *foo)
38{
39	struct gfs2_inode *ip = foo;
40
41	inode_init_once(&ip->i_inode);
42	init_rwsem(&ip->i_rw_mutex);
43	INIT_LIST_HEAD(&ip->i_trunc_list);
44	ip->i_res = NULL;
45	ip->i_hash_cache = NULL;
46}
47
48static void gfs2_init_glock_once(void *foo)
49{
50	struct gfs2_glock *gl = foo;
51
52	INIT_HLIST_BL_NODE(&gl->gl_list);
53	spin_lock_init(&gl->gl_spin);
54	INIT_LIST_HEAD(&gl->gl_holders);
55	INIT_LIST_HEAD(&gl->gl_lru);
56	INIT_LIST_HEAD(&gl->gl_ail_list);
57	atomic_set(&gl->gl_ail_count, 0);
58	atomic_set(&gl->gl_revokes, 0);
59}
60
61static 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
76static 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;
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;
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;
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					      gfs2_init_inode_once);
117	if (!gfs2_inode_cachep)
118		goto fail;
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;
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;
131
132	gfs2_quotad_cachep = kmem_cache_create("gfs2_quotad",
133					       sizeof(struct gfs2_quota_data),
134					       0, 0, NULL);
135	if (!gfs2_quotad_cachep)
136		goto fail;
137
138	gfs2_rsrv_cachep = kmem_cache_create("gfs2_mblk",
139					     sizeof(struct gfs2_blkreserv),
140					       0, 0, NULL);
141	if (!gfs2_rsrv_cachep)
142		goto fail;
143
144	register_shrinker(&gfs2_qd_shrinker);
145
146	error = register_filesystem(&gfs2_fs_type);
147	if (error)
148		goto fail;
149
150	error = register_filesystem(&gfs2meta_fs_type);
151	if (error)
152		goto fail_unregister;
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_wq;
159
160	gfs2_control_wq = alloc_workqueue("gfs2_control",
161					  WQ_UNBOUND | WQ_FREEZABLE, 0);
162	if (!gfs2_control_wq)
163		goto fail_recovery;
164
165	gfs2_freeze_wq = alloc_workqueue("freeze_workqueue", 0, 0);
166
167	if (!gfs2_freeze_wq)
168		goto fail_control;
169
170	gfs2_page_pool = mempool_create_page_pool(64, 0);
171	if (!gfs2_page_pool)
172		goto fail_freeze;
173
174	gfs2_register_debugfs();
175
176	pr_info("GFS2 installed\n");
177
178	return 0;
179
180fail_freeze:
181	destroy_workqueue(gfs2_freeze_wq);
182fail_control:
183	destroy_workqueue(gfs2_control_wq);
184fail_recovery:
185	destroy_workqueue(gfs_recovery_wq);
186fail_wq:
187	unregister_filesystem(&gfs2meta_fs_type);
188fail_unregister:
189	unregister_filesystem(&gfs2_fs_type);
190fail:
191	list_lru_destroy(&gfs2_qd_lru);
192fail_lru:
193	unregister_shrinker(&gfs2_qd_shrinker);
194	gfs2_glock_exit();
195
196	if (gfs2_rsrv_cachep)
197		kmem_cache_destroy(gfs2_rsrv_cachep);
198
199	if (gfs2_quotad_cachep)
200		kmem_cache_destroy(gfs2_quotad_cachep);
201
202	if (gfs2_rgrpd_cachep)
203		kmem_cache_destroy(gfs2_rgrpd_cachep);
204
205	if (gfs2_bufdata_cachep)
206		kmem_cache_destroy(gfs2_bufdata_cachep);
207
208	if (gfs2_inode_cachep)
209		kmem_cache_destroy(gfs2_inode_cachep);
210
211	if (gfs2_glock_aspace_cachep)
212		kmem_cache_destroy(gfs2_glock_aspace_cachep);
213
214	if (gfs2_glock_cachep)
215		kmem_cache_destroy(gfs2_glock_cachep);
216
217	gfs2_sys_uninit();
218	return error;
219}
220
221/**
222 * exit_gfs2_fs - Unregister the file system
223 *
224 */
225
226static void __exit exit_gfs2_fs(void)
227{
228	unregister_shrinker(&gfs2_qd_shrinker);
229	gfs2_glock_exit();
230	gfs2_unregister_debugfs();
231	unregister_filesystem(&gfs2_fs_type);
232	unregister_filesystem(&gfs2meta_fs_type);
233	destroy_workqueue(gfs_recovery_wq);
234	destroy_workqueue(gfs2_control_wq);
235	destroy_workqueue(gfs2_freeze_wq);
236	list_lru_destroy(&gfs2_qd_lru);
237
238	rcu_barrier();
239
240	mempool_destroy(gfs2_page_pool);
241	kmem_cache_destroy(gfs2_rsrv_cachep);
242	kmem_cache_destroy(gfs2_quotad_cachep);
243	kmem_cache_destroy(gfs2_rgrpd_cachep);
244	kmem_cache_destroy(gfs2_bufdata_cachep);
245	kmem_cache_destroy(gfs2_inode_cachep);
246	kmem_cache_destroy(gfs2_glock_aspace_cachep);
247	kmem_cache_destroy(gfs2_glock_cachep);
248
249	gfs2_sys_uninit();
250}
251
252MODULE_DESCRIPTION("Global File System");
253MODULE_AUTHOR("Red Hat, Inc.");
254MODULE_LICENSE("GPL");
255
256module_init(init_gfs2_fs);
257module_exit(exit_gfs2_fs);
258
259