2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
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>
18 #include <linux/delay.h>
20 #include "lm_interface.h"
23 struct list_head lw_list;
24 struct lm_lockops *lw_ops;
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. */
30 static struct list_head lmh_list;
31 static struct mutex lmh_lock;
34 * gfs_register_lockproto - Register a low-level locking protocol
35 * @proto: the protocol definition
37 * Returns: 0 on success, -EXXX on failure
40 int gfs_register_lockproto(struct lm_lockops *proto)
42 struct lmh_wrapper *lw;
44 mutex_lock(&lmh_lock);
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);
55 lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
57 mutex_unlock(&lmh_lock);
62 list_add(&lw->lw_list, &lmh_list);
64 mutex_unlock(&lmh_lock);
70 * gfs_unregister_lockproto - Unregister a low-level locking protocol
71 * @proto: the protocol definition
75 void gfs_unregister_lockproto(struct lm_lockops *proto)
77 struct lmh_wrapper *lw;
79 mutex_lock(&lmh_lock);
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);
90 mutex_unlock(&lmh_lock);
92 printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
93 proto->lm_proto_name);
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
107 * Returns: 0 on success, -EXXX on failure
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)
116 struct lmh_wrapper *lw = NULL;
121 mutex_lock(&lmh_lock);
124 list_for_each_entry(lw, &lmh_list, lw_list) {
125 if (!strcmp(lw->lw_ops->lm_proto_name, proto_name)) {
132 if (!try && capable(CAP_SYS_MODULE)) {
134 mutex_unlock(&lmh_lock);
135 request_module(proto_name);
138 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
143 if (!try_module_get(lw->lw_ops->lm_owner)) {
145 mutex_unlock(&lmh_lock);
150 error = lw->lw_ops->lm_mount(table_name, host_data, cb, fsdata,
151 min_lvb_size, flags, lockstruct, fskobj);
153 module_put(lw->lw_ops->lm_owner);
155 mutex_unlock(&lmh_lock);
159 void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
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);
169 * gfs2_withdraw_lockproto - abnormally unmount a lock module
170 * @lockstruct: the lockstruct passed into mount
174 void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
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);
183 void __init gfs2_init_lmh(void)
185 mutex_init(&lmh_lock);
186 INIT_LIST_HEAD(&lmh_list);
189 EXPORT_SYMBOL_GPL(gfs_register_lockproto);
190 EXPORT_SYMBOL_GPL(gfs_unregister_lockproto);