blob: 11c4068105ccb7dd04779555701d0f21d80dd010 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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
22struct 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
30static struct list_head lmh_list;
Steven Whitehousea74604b2006-04-21 15:10:46 -040031static struct mutex lmh_lock;
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33/**
Steven Whitehouse2b557f62006-08-07 11:12:30 -040034 * gfs2_register_lockproto - Register a low-level locking protocol
David Teiglandb3b94fa2006-01-16 16:50:04 +000035 * @proto: the protocol definition
36 *
37 * Returns: 0 on success, -EXXX on failure
38 */
39
Steven Whitehouse2b557f62006-08-07 11:12:30 -040040int gfs2_register_lockproto(struct lm_lockops *proto)
David Teiglandb3b94fa2006-01-16 16:50:04 +000041{
42 struct lmh_wrapper *lw;
43
Steven Whitehousea74604b2006-04-21 15:10:46 -040044 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000045
46 list_for_each_entry(lw, &lmh_list, lw_list) {
47 if (!strcmp(lw->lw_ops->lm_proto_name, proto->lm_proto_name)) {
Steven Whitehousea74604b2006-04-21 15:10:46 -040048 mutex_unlock(&lmh_lock);
Steven Whitehoused92a8d42006-02-27 10:57:14 -050049 printk(KERN_INFO "GFS2: protocol %s already exists\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +000050 proto->lm_proto_name);
51 return -EEXIST;
52 }
53 }
54
Steven Whitehoused92a8d42006-02-27 10:57:14 -050055 lw = kzalloc(sizeof(struct lmh_wrapper), GFP_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 if (!lw) {
Steven Whitehousea74604b2006-04-21 15:10:46 -040057 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 return -ENOMEM;
59 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000060
61 lw->lw_ops = proto;
62 list_add(&lw->lw_list, &lmh_list);
63
Steven Whitehousea74604b2006-04-21 15:10:46 -040064 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66 return 0;
67}
68
69/**
Steven Whitehouse2b557f62006-08-07 11:12:30 -040070 * gfs2_unregister_lockproto - Unregister a low-level locking protocol
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 * @proto: the protocol definition
72 *
73 */
74
Steven Whitehouse2b557f62006-08-07 11:12:30 -040075void gfs2_unregister_lockproto(struct lm_lockops *proto)
David Teiglandb3b94fa2006-01-16 16:50:04 +000076{
77 struct lmh_wrapper *lw;
78
Steven Whitehousea74604b2006-04-21 15:10:46 -040079 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000080
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);
Steven Whitehousea74604b2006-04-21 15:10:46 -040084 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 kfree(lw);
86 return;
87 }
88 }
89
Steven Whitehousea74604b2006-04-21 15:10:46 -040090 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
Steven Whitehoused92a8d42006-02-27 10:57:14 -050092 printk(KERN_WARNING "GFS2: can't unregister lock protocol %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 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
110int 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
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400120retry:
Steven Whitehousea74604b2006-04-21 15:10:46 -0400121 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122
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;
Steven Whitehousea74604b2006-04-21 15:10:46 -0400134 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 request_module(proto_name);
136 goto retry;
137 }
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500138 printk(KERN_INFO "GFS2: can't find protocol %s\n", proto_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 error = -ENOENT;
140 goto out;
141 }
142
143 if (!try_module_get(lw->lw_ops->lm_owner)) {
144 try = 0;
Steven Whitehousea74604b2006-04-21 15:10:46 -0400145 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 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);
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400154out:
Steven Whitehousea74604b2006-04-21 15:10:46 -0400155 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 return error;
157}
158
159void gfs2_unmount_lockproto(struct lm_lockstruct *lockstruct)
160{
Steven Whitehousea74604b2006-04-21 15:10:46 -0400161 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 lockstruct->ls_ops->lm_unmount(lockstruct->ls_lockspace);
163 if (lockstruct->ls_ops->lm_owner)
164 module_put(lockstruct->ls_ops->lm_owner);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400165 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166}
167
168/**
169 * gfs2_withdraw_lockproto - abnormally unmount a lock module
170 * @lockstruct: the lockstruct passed into mount
171 *
172 */
173
174void gfs2_withdraw_lockproto(struct lm_lockstruct *lockstruct)
175{
Steven Whitehousea74604b2006-04-21 15:10:46 -0400176 mutex_lock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 lockstruct->ls_ops->lm_withdraw(lockstruct->ls_lockspace);
178 if (lockstruct->ls_ops->lm_owner)
179 module_put(lockstruct->ls_ops->lm_owner);
Steven Whitehousea74604b2006-04-21 15:10:46 -0400180 mutex_unlock(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181}
182
183void __init gfs2_init_lmh(void)
184{
Steven Whitehousea74604b2006-04-21 15:10:46 -0400185 mutex_init(&lmh_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 INIT_LIST_HEAD(&lmh_list);
187}
188
Steven Whitehouse2b557f62006-08-07 11:12:30 -0400189EXPORT_SYMBOL_GPL(gfs2_register_lockproto);
190EXPORT_SYMBOL_GPL(gfs2_unregister_lockproto);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191