Merge branch 'master'
[linux-2.6] / fs / gfs2 / locking.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 v.2.
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/kmod.h>
17 #include <linux/fs.h>
18 #include <linux/delay.h>
19
20 #include "lm_interface.h"
21
22 struct lmh_wrapper {
23         struct list_head lw_list;
24         struct lm_lockops *lw_ops;
25 };
26
27 /* List of registered low-level locking protocols.  A file system selects one
28    of them by name at mount time, e.g. lock_nolock, lock_dlm. */
29
30 static struct list_head lmh_list;
31 static struct mutex lmh_lock;
32
33 /**
34  * gfs_register_lockproto - Register a low-level locking protocol
35  * @proto: the protocol definition
36  *
37  * Returns: 0 on success, -EXXX on failure
38  */
39
40 int gfs_register_lockproto(struct lm_lockops *proto)
41 {
42         struct lmh_wrapper *lw;
43
44         mutex_lock(&lmh_lock);
45
46         list_for_each_entry(lw, &lmh_list, lw_list) {
47                 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
48                         mutex_unlock(&lmh_lock);
49                         printk(KERN_INFO "GFS2: protocol %s already exists\n",
50                                proto->lm_proto_name);
51                         return -EEXIST;
52                 }
53         }
54
55         lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
56         if (!lw) {
57                 mutex_unlock(&lmh_lock);
58                 return -ENOMEM;
59         }
60
61         lw->lw_ops = proto;
62         list_add(&lw->lw_list, &lmh_list);
63
64         mutex_unlock(&lmh_lock);
65
66         return 0;
67 }
68
69 /**
70  * gfs_unregister_lockproto - Unregister a low-level locking protocol
71  * @proto: the protocol definition
72  *
73  */
74
75 void gfs_unregister_lockproto(struct lm_lockops *proto)
76 {
77         struct lmh_wrapper *lw;
78
79         mutex_lock(&lmh_lock);
80
81         list_for_each_entry(lw, &lmh_list, lw_list) {
82                 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
83                         list_del(&lw->lw_list);
84                         mutex_unlock(&lmh_lock);
85                         kfree(lw);
86                         return;
87                 }
88         }
89
90         mutex_unlock(&lmh_lock);
91
92         printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
93                proto->lm_proto_name);
94 }
95
96 /**
97  * gfs2_mount_lockproto - Mount a lock protocol
98  * @proto_name - the name of the protocol
99  * @table_name - the name of the lock space
100  * @host_data - data specific to this host
101  * @cb - the callback to the code using the lock module
102  * @fsdata - data to pass back with the callback
103  * @min_lvb_size - the mininum LVB size that the caller can deal with
104  * @flags - LM_MFLAG_*
105  * @lockstruct - a structure returned describing the mount
106  *
107  * Returns: 0 on success, -EXXX on failure
108  */
109
110 int gfs2_mount_lockproto(char *proto_name, char *table_name, char *host_data,
111                          lm_callback_t cb, lm_fsdata_t *fsdata,
112                          unsigned int min_lvb_size, int flags,
113                          struct lm_lockstruct *lockstruct,
114                          struct kobject *fskobj)
115 {
116         struct lmh_wrapper *lw = NULL;
117         int try = 0;
118         int error, found;
119
120  retry:
121         mutex_lock(&lmh_lock);
122
123         found = 0;
124         list_for_each_entry(lw, &lmh_list, lw_list) {
125                 if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
126                         found = 1;
127                         break;
128                 }
129         }
130
131         if (!found) {
132                 if (!try && capable(CAP_SYS_MODULE)) {
133                         try = 1;
134                         mutex_unlock(&lmh_lock);
135                         request_module(proto_name);
136                         goto retry;
137                 }
138                 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
139                 error = -ENOENT;
140                 goto out;
141         }
142
143         if (!try_module_get(lw->lw_ops->lm_owner)) {
144                 try = 0;
145                 mutex_unlock(&lmh_lock);
146                 msleep(1000);
147                 goto retry;
148         }
149
150         error = lw->lw_ops->lm_mount(table_name, host_data, cb, fsdata,
151                                      min_lvb_size, flags, lockstruct, fskobj);
152         if (error)
153                 module_put(lw->lw_ops->lm_owner);
154  out:
155         mutex_unlock(&lmh_lock);
156         return error;
157 }
158
159 void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
160 {
161         mutex_lock(&lmh_lock);
162         lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
163         if (lockstruct->ls_ops->lm_owner)
164                 module_put(lockstruct->ls_ops->lm_owner);
165         mutex_unlock(&lmh_lock);
166 }
167
168 /**
169  * gfs2_withdraw_lockproto - abnormally unmount a lock module
170  * @lockstruct: the lockstruct passed into mount
171  *
172  */
173
174 void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
175 {
176         mutex_lock(&lmh_lock);
177         lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
178         if (lockstruct->ls_ops->lm_owner)
179                 module_put(lockstruct->ls_ops->lm_owner);
180         mutex_unlock(&lmh_lock);
181 }
182
183 void __init gfs2_init_lmh(void)
184 {
185         mutex_init(&lmh_lock);
186         INIT_LIST_HEAD(&lmh_list);
187 }
188
189 EXPORT_SYMBOL_GPL(gfs_register_lockproto);
190 EXPORT_SYMBOL_GPL(gfs_unregister_lockproto);
191