]>
Commit | Line | Data |
---|---|---|
869d81df DT |
1 | /* |
2 | * Copyright (C) 2005 Red Hat, Inc. All rights reserved. | |
3 | * | |
4 | * This copyrighted material is made available to anyone wishing to use, | |
5 | * modify, copy, or redistribute it subject to the terms and conditions | |
6 | * of the GNU General Public License v.2. | |
7 | */ | |
8 | ||
9 | #include <linux/miscdevice.h> | |
10 | #include <linux/lock_dlm_plock.h> | |
11 | ||
12 | #include "lock_dlm.h" | |
13 | ||
14 | ||
15 | static spinlock_t ops_lock; | |
16 | static struct list_head send_list; | |
17 | static struct list_head recv_list; | |
18 | static wait_queue_head_t send_wq; | |
19 | static wait_queue_head_t recv_wq; | |
20 | ||
21 | struct plock_op { | |
22 | struct list_head list; | |
23 | int done; | |
24 | struct gdlm_plock_info info; | |
25 | }; | |
26 | ||
27 | static inline void set_version(struct gdlm_plock_info *info) | |
28 | { | |
29 | info->version[0] = GDLM_PLOCK_VERSION_MAJOR; | |
30 | info->version[1] = GDLM_PLOCK_VERSION_MINOR; | |
31 | info->version[2] = GDLM_PLOCK_VERSION_PATCH; | |
32 | } | |
33 | ||
34 | static int check_version(struct gdlm_plock_info *info) | |
35 | { | |
36 | if ((GDLM_PLOCK_VERSION_MAJOR != info->version[0]) || | |
37 | (GDLM_PLOCK_VERSION_MINOR < info->version[1])) { | |
38 | log_error("plock device version mismatch: " | |
39 | "kernel (%u.%u.%u), user (%u.%u.%u)", | |
40 | GDLM_PLOCK_VERSION_MAJOR, | |
41 | GDLM_PLOCK_VERSION_MINOR, | |
42 | GDLM_PLOCK_VERSION_PATCH, | |
43 | info->version[0], | |
44 | info->version[1], | |
45 | info->version[2]); | |
46 | return -EINVAL; | |
47 | } | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static void send_op(struct plock_op *op) | |
52 | { | |
53 | set_version(&op->info); | |
54 | INIT_LIST_HEAD(&op->list); | |
55 | spin_lock(&ops_lock); | |
56 | list_add_tail(&op->list, &send_list); | |
57 | spin_unlock(&ops_lock); | |
58 | wake_up(&send_wq); | |
59 | } | |
60 | ||
61 | int gdlm_plock(lm_lockspace_t *lockspace, struct lm_lockname *name, | |
62 | struct file *file, int cmd, struct file_lock *fl) | |
63 | { | |
64 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; | |
65 | struct plock_op *op; | |
66 | int rv; | |
67 | ||
68 | op = kzalloc(sizeof(*op), GFP_KERNEL); | |
69 | if (!op) | |
70 | return -ENOMEM; | |
71 | ||
72 | op->info.optype = GDLM_PLOCK_OP_LOCK; | |
73 | op->info.pid = (uint32_t) fl->fl_owner; | |
74 | op->info.ex = (fl->fl_type == F_WRLCK); | |
75 | op->info.wait = IS_SETLKW(cmd); | |
76 | op->info.fsid = ls->id; | |
77 | op->info.number = name->ln_number; | |
78 | op->info.start = fl->fl_start; | |
79 | op->info.end = fl->fl_end; | |
80 | ||
81 | send_op(op); | |
82 | wait_event(recv_wq, (op->done != 0)); | |
83 | ||
84 | spin_lock(&ops_lock); | |
85 | if (!list_empty(&op->list)) { | |
d92a8d48 | 86 | printk(KERN_INFO "plock op on list\n"); |
869d81df DT |
87 | list_del(&op->list); |
88 | } | |
89 | spin_unlock(&ops_lock); | |
90 | ||
91 | rv = op->info.rv; | |
92 | ||
93 | if (!rv) { | |
94 | if (posix_lock_file_wait(file, fl) < 0) | |
95 | log_error("gdlm_plock: vfs lock error %x,%llx", | |
96 | name->ln_type, name->ln_number); | |
97 | } | |
98 | ||
99 | kfree(op); | |
100 | return rv; | |
101 | } | |
102 | ||
103 | int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name, | |
104 | struct file *file, struct file_lock *fl) | |
105 | { | |
106 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; | |
107 | struct plock_op *op; | |
108 | int rv; | |
109 | ||
110 | op = kzalloc(sizeof(*op), GFP_KERNEL); | |
111 | if (!op) | |
112 | return -ENOMEM; | |
113 | ||
114 | if (posix_lock_file_wait(file, fl) < 0) | |
115 | log_error("gdlm_punlock: vfs unlock error %x,%llx", | |
116 | name->ln_type, name->ln_number); | |
117 | ||
118 | op->info.optype = GDLM_PLOCK_OP_UNLOCK; | |
119 | op->info.pid = (uint32_t) fl->fl_owner; | |
120 | op->info.fsid = ls->id; | |
121 | op->info.number = name->ln_number; | |
122 | op->info.start = fl->fl_start; | |
123 | op->info.end = fl->fl_end; | |
124 | ||
125 | send_op(op); | |
126 | wait_event(recv_wq, (op->done != 0)); | |
127 | ||
128 | spin_lock(&ops_lock); | |
129 | if (!list_empty(&op->list)) { | |
d92a8d48 | 130 | printk(KERN_INFO "punlock op on list\n"); |
869d81df DT |
131 | list_del(&op->list); |
132 | } | |
133 | spin_unlock(&ops_lock); | |
134 | ||
135 | rv = op->info.rv; | |
136 | ||
137 | kfree(op); | |
138 | return rv; | |
139 | } | |
140 | ||
141 | int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name, | |
142 | struct file *file, struct file_lock *fl) | |
143 | { | |
144 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; | |
145 | struct plock_op *op; | |
146 | int rv; | |
147 | ||
148 | op = kzalloc(sizeof(*op), GFP_KERNEL); | |
149 | if (!op) | |
150 | return -ENOMEM; | |
151 | ||
152 | op->info.optype = GDLM_PLOCK_OP_GET; | |
153 | op->info.pid = (uint32_t) fl->fl_owner; | |
154 | op->info.ex = (fl->fl_type == F_WRLCK); | |
155 | op->info.fsid = ls->id; | |
156 | op->info.number = name->ln_number; | |
157 | op->info.start = fl->fl_start; | |
158 | op->info.end = fl->fl_end; | |
159 | ||
160 | send_op(op); | |
161 | wait_event(recv_wq, (op->done != 0)); | |
162 | ||
163 | spin_lock(&ops_lock); | |
164 | if (!list_empty(&op->list)) { | |
d92a8d48 | 165 | printk(KERN_INFO "plock_get op on list\n"); |
869d81df DT |
166 | list_del(&op->list); |
167 | } | |
168 | spin_unlock(&ops_lock); | |
169 | ||
170 | rv = op->info.rv; | |
171 | ||
172 | if (rv == 0) | |
173 | fl->fl_type = F_UNLCK; | |
174 | else if (rv > 0) { | |
175 | fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK; | |
176 | fl->fl_pid = op->info.pid; | |
177 | fl->fl_start = op->info.start; | |
178 | fl->fl_end = op->info.end; | |
179 | } | |
180 | ||
181 | kfree(op); | |
182 | return rv; | |
183 | } | |
184 | ||
185 | /* a read copies out one plock request from the send list */ | |
186 | static ssize_t dev_read(struct file *file, char __user *u, size_t count, | |
187 | loff_t *ppos) | |
188 | { | |
189 | struct gdlm_plock_info info; | |
190 | struct plock_op *op = NULL; | |
191 | ||
192 | if (count < sizeof(info)) | |
193 | return -EINVAL; | |
194 | ||
195 | spin_lock(&ops_lock); | |
196 | if (!list_empty(&send_list)) { | |
197 | op = list_entry(send_list.next, struct plock_op, list); | |
198 | list_move(&op->list, &recv_list); | |
199 | memcpy(&info, &op->info, sizeof(info)); | |
200 | } | |
201 | spin_unlock(&ops_lock); | |
202 | ||
203 | if (!op) | |
204 | return -EAGAIN; | |
205 | ||
206 | if (copy_to_user(u, &info, sizeof(info))) | |
207 | return -EFAULT; | |
208 | return sizeof(info); | |
209 | } | |
210 | ||
211 | /* a write copies in one plock result that should match a plock_op | |
212 | on the recv list */ | |
213 | static ssize_t dev_write(struct file *file, const char __user *u, size_t count, | |
214 | loff_t *ppos) | |
215 | { | |
216 | struct gdlm_plock_info info; | |
217 | struct plock_op *op; | |
218 | int found = 0; | |
219 | ||
220 | if (count != sizeof(info)) | |
221 | return -EINVAL; | |
222 | ||
223 | if (copy_from_user(&info, u, sizeof(info))) | |
224 | return -EFAULT; | |
225 | ||
226 | if (check_version(&info)) | |
227 | return -EINVAL; | |
228 | ||
229 | spin_lock(&ops_lock); | |
230 | list_for_each_entry(op, &recv_list, list) { | |
231 | if (op->info.fsid == info.fsid && | |
232 | op->info.number == info.number) { | |
233 | list_del_init(&op->list); | |
234 | found = 1; | |
235 | op->done = 1; | |
236 | memcpy(&op->info, &info, sizeof(info)); | |
237 | break; | |
238 | } | |
239 | } | |
240 | spin_unlock(&ops_lock); | |
241 | ||
242 | if (found) | |
243 | wake_up(&recv_wq); | |
244 | else | |
d92a8d48 | 245 | printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid, |
869d81df DT |
246 | info.number); |
247 | return count; | |
248 | } | |
249 | ||
250 | static unsigned int dev_poll(struct file *file, poll_table *wait) | |
251 | { | |
252 | poll_wait(file, &send_wq, wait); | |
253 | ||
254 | spin_lock(&ops_lock); | |
255 | if (!list_empty(&send_list)) { | |
256 | spin_unlock(&ops_lock); | |
257 | return POLLIN | POLLRDNORM; | |
258 | } | |
259 | spin_unlock(&ops_lock); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static struct file_operations dev_fops = { | |
264 | .read = dev_read, | |
265 | .write = dev_write, | |
266 | .poll = dev_poll, | |
267 | .owner = THIS_MODULE | |
268 | }; | |
269 | ||
270 | static struct miscdevice plock_dev_misc = { | |
271 | .minor = MISC_DYNAMIC_MINOR, | |
272 | .name = GDLM_PLOCK_MISC_NAME, | |
273 | .fops = &dev_fops | |
274 | }; | |
275 | ||
276 | int gdlm_plock_init(void) | |
277 | { | |
278 | int rv; | |
279 | ||
280 | spin_lock_init(&ops_lock); | |
281 | INIT_LIST_HEAD(&send_list); | |
282 | INIT_LIST_HEAD(&recv_list); | |
283 | init_waitqueue_head(&send_wq); | |
284 | init_waitqueue_head(&recv_wq); | |
285 | ||
286 | rv = misc_register(&plock_dev_misc); | |
287 | if (rv) | |
d92a8d48 SW |
288 | printk(KERN_INFO "gdlm_plock_init: misc_register failed %d", |
289 | rv); | |
869d81df DT |
290 | return rv; |
291 | } | |
292 | ||
293 | void gdlm_plock_exit(void) | |
294 | { | |
295 | if (misc_deregister(&plock_dev_misc) < 0) | |
d92a8d48 | 296 | printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed"); |
869d81df DT |
297 | } |
298 |