cde5c73c42df66978fbd5b3b1bfae9a71983be91
[sfrench/cifs-2.6.git] / fs / gfs2 / main.c
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
35 struct workqueue_struct *gfs2_control_wq;
36
37 static 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_qadata = NULL;
45         ip->i_res = NULL;
46         ip->i_hash_cache = NULL;
47 }
48
49 static void gfs2_init_glock_once(void *foo)
50 {
51         struct gfs2_glock *gl = foo;
52
53         INIT_HLIST_BL_NODE(&gl->gl_list);
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
62 static void gfs2_init_gl_aspace_once(void *foo)
63 {
64         struct gfs2_glock *gl = foo;
65         struct address_space *mapping = (struct address_space *)(gl + 1);
66
67         gfs2_init_glock_once(gl);
68         address_space_init_once(mapping);
69 }
70
71 /**
72  * init_gfs2_fs - Register GFS2 as a filesystem
73  *
74  * Returns: 0 on success, error code on failure
75  */
76
77 static int __init init_gfs2_fs(void)
78 {
79         int error;
80
81         gfs2_str2qstr(&gfs2_qdot, ".");
82         gfs2_str2qstr(&gfs2_qdotdot, "..");
83         gfs2_quota_hash_init();
84
85         error = gfs2_sys_init();
86         if (error)
87                 return error;
88
89         error = list_lru_init(&gfs2_qd_lru);
90         if (error)
91                 goto fail_lru;
92
93         error = gfs2_glock_init();
94         if (error)
95                 goto fail;
96
97         error = -ENOMEM;
98         gfs2_glock_cachep = kmem_cache_create("gfs2_glock",
99                                               sizeof(struct gfs2_glock),
100                                               0, 0,
101                                               gfs2_init_glock_once);
102         if (!gfs2_glock_cachep)
103                 goto fail;
104
105         gfs2_glock_aspace_cachep = kmem_cache_create("gfs2_glock(aspace)",
106                                         sizeof(struct gfs2_glock) +
107                                         sizeof(struct address_space),
108                                         0, 0, gfs2_init_gl_aspace_once);
109
110         if (!gfs2_glock_aspace_cachep)
111                 goto fail;
112
113         gfs2_inode_cachep = kmem_cache_create("gfs2_inode",
114                                               sizeof(struct gfs2_inode),
115                                               0,  SLAB_RECLAIM_ACCOUNT|
116                                                   SLAB_MEM_SPREAD,
117                                               gfs2_init_inode_once);
118         if (!gfs2_inode_cachep)
119                 goto fail;
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;
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;
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;
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;
144
145         gfs2_rsrv_cachep = kmem_cache_create("gfs2_mblk",
146                                              sizeof(struct gfs2_blkreserv),
147                                                0, 0, NULL);
148         if (!gfs2_rsrv_cachep)
149                 goto fail;
150
151         register_shrinker(&gfs2_qd_shrinker);
152
153         error = register_filesystem(&gfs2_fs_type);
154         if (error)
155                 goto fail;
156
157         error = register_filesystem(&gfs2meta_fs_type);
158         if (error)
159                 goto fail_unregister;
160
161         error = -ENOMEM;
162         gfs_recovery_wq = alloc_workqueue("gfs_recovery",
163                                           WQ_MEM_RECLAIM | WQ_FREEZABLE, 0);
164         if (!gfs_recovery_wq)
165                 goto fail_wq;
166
167         gfs2_control_wq = alloc_workqueue("gfs2_control",
168                                           WQ_UNBOUND | WQ_FREEZABLE, 0);
169         if (!gfs2_control_wq)
170                 goto fail_recovery;
171
172         gfs2_freeze_wq = alloc_workqueue("freeze_workqueue", 0, 0);
173
174         if (!gfs2_freeze_wq)
175                 goto fail_control;
176
177         gfs2_page_pool = mempool_create_page_pool(64, 0);
178         if (!gfs2_page_pool)
179                 goto fail_freeze;
180
181         gfs2_register_debugfs();
182
183         pr_info("GFS2 installed\n");
184
185         return 0;
186
187 fail_freeze:
188         destroy_workqueue(gfs2_freeze_wq);
189 fail_control:
190         destroy_workqueue(gfs2_control_wq);
191 fail_recovery:
192         destroy_workqueue(gfs_recovery_wq);
193 fail_wq:
194         unregister_filesystem(&gfs2meta_fs_type);
195 fail_unregister:
196         unregister_filesystem(&gfs2_fs_type);
197 fail:
198         list_lru_destroy(&gfs2_qd_lru);
199 fail_lru:
200         unregister_shrinker(&gfs2_qd_shrinker);
201         gfs2_glock_exit();
202
203         if (gfs2_rsrv_cachep)
204                 kmem_cache_destroy(gfs2_rsrv_cachep);
205
206         if (gfs2_qadata_cachep)
207                 kmem_cache_destroy(gfs2_qadata_cachep);
208
209         if (gfs2_quotad_cachep)
210                 kmem_cache_destroy(gfs2_quotad_cachep);
211
212         if (gfs2_rgrpd_cachep)
213                 kmem_cache_destroy(gfs2_rgrpd_cachep);
214
215         if (gfs2_bufdata_cachep)
216                 kmem_cache_destroy(gfs2_bufdata_cachep);
217
218         if (gfs2_inode_cachep)
219                 kmem_cache_destroy(gfs2_inode_cachep);
220
221         if (gfs2_glock_aspace_cachep)
222                 kmem_cache_destroy(gfs2_glock_aspace_cachep);
223
224         if (gfs2_glock_cachep)
225                 kmem_cache_destroy(gfs2_glock_cachep);
226
227         gfs2_sys_uninit();
228         return error;
229 }
230
231 /**
232  * exit_gfs2_fs - Unregister the file system
233  *
234  */
235
236 static void __exit exit_gfs2_fs(void)
237 {
238         unregister_shrinker(&gfs2_qd_shrinker);
239         gfs2_glock_exit();
240         gfs2_unregister_debugfs();
241         unregister_filesystem(&gfs2_fs_type);
242         unregister_filesystem(&gfs2meta_fs_type);
243         destroy_workqueue(gfs_recovery_wq);
244         destroy_workqueue(gfs2_control_wq);
245         destroy_workqueue(gfs2_freeze_wq);
246         list_lru_destroy(&gfs2_qd_lru);
247
248         rcu_barrier();
249
250         mempool_destroy(gfs2_page_pool);
251         kmem_cache_destroy(gfs2_rsrv_cachep);
252         kmem_cache_destroy(gfs2_qadata_cachep);
253         kmem_cache_destroy(gfs2_quotad_cachep);
254         kmem_cache_destroy(gfs2_rgrpd_cachep);
255         kmem_cache_destroy(gfs2_bufdata_cachep);
256         kmem_cache_destroy(gfs2_inode_cachep);
257         kmem_cache_destroy(gfs2_glock_aspace_cachep);
258         kmem_cache_destroy(gfs2_glock_cachep);
259
260         gfs2_sys_uninit();
261 }
262
263 MODULE_DESCRIPTION("Global File System");
264 MODULE_AUTHOR("Red Hat, Inc.");
265 MODULE_LICENSE("GPL");
266
267 module_init(init_gfs2_fs);
268 module_exit(exit_gfs2_fs);
269