2 * Copyright (C) 2005 Red Hat, Inc. All rights reserved.
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.
9 #include <linux/miscdevice.h>
10 #include <linux/lock_dlm_plock.h>
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;
22 struct list_head list;
24 struct gdlm_plock_info info;
27 static inline void set_version(struct gdlm_plock_info *info)
29 info->version[0] = GDLM_PLOCK_VERSION_MAJOR;
30 info->version[1] = GDLM_PLOCK_VERSION_MINOR;
31 info->version[2] = GDLM_PLOCK_VERSION_PATCH;
34 static int check_version(struct gdlm_plock_info *info)
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,
51 static void send_op(struct plock_op *op)
53 set_version(&op->info);
54 INIT_LIST_HEAD(&op->list);
56 list_add_tail(&op->list, &send_list);
57 spin_unlock(&ops_lock);
61 int gdlm_plock(lm_lockspace_t *lockspace, struct lm_lockname *name,
62 struct file *file, int cmd, struct file_lock *fl)
64 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
68 op = kzalloc(sizeof(*op), GFP_KERNEL);
72 op->info.optype = GDLM_PLOCK_OP_LOCK;
73 op->info.pid = fl->fl_pid;
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;
82 wait_event(recv_wq, (op->done != 0));
85 if (!list_empty(&op->list)) {
86 printk(KERN_INFO "plock op on list\n");
89 spin_unlock(&ops_lock);
94 if (posix_lock_file_wait(file, fl) < 0)
95 log_error("gdlm_plock: vfs lock error %x,%llx",
97 (unsigned long long)name->ln_number);
104 int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name,
105 struct file *file, struct file_lock *fl)
107 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
111 op = kzalloc(sizeof(*op), GFP_KERNEL);
115 if (posix_lock_file_wait(file, fl) < 0)
116 log_error("gdlm_punlock: vfs unlock error %x,%llx",
117 name->ln_type, (unsigned long long)name->ln_number);
119 op->info.optype = GDLM_PLOCK_OP_UNLOCK;
120 op->info.pid = fl->fl_pid;
121 op->info.fsid = ls->id;
122 op->info.number = name->ln_number;
123 op->info.start = fl->fl_start;
124 op->info.end = fl->fl_end;
127 wait_event(recv_wq, (op->done != 0));
129 spin_lock(&ops_lock);
130 if (!list_empty(&op->list)) {
131 printk(KERN_INFO "punlock op on list\n");
134 spin_unlock(&ops_lock);
142 int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name,
143 struct file *file, struct file_lock *fl)
145 struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
149 op = kzalloc(sizeof(*op), GFP_KERNEL);
153 op->info.optype = GDLM_PLOCK_OP_GET;
154 op->info.pid = fl->fl_pid;
155 op->info.ex = (fl->fl_type == F_WRLCK);
156 op->info.fsid = ls->id;
157 op->info.number = name->ln_number;
158 op->info.start = fl->fl_start;
159 op->info.end = fl->fl_end;
162 wait_event(recv_wq, (op->done != 0));
164 spin_lock(&ops_lock);
165 if (!list_empty(&op->list)) {
166 printk(KERN_INFO "plock_get op on list\n");
169 spin_unlock(&ops_lock);
174 fl->fl_type = F_UNLCK;
176 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
177 fl->fl_pid = op->info.pid;
178 fl->fl_start = op->info.start;
179 fl->fl_end = op->info.end;
186 /* a read copies out one plock request from the send list */
187 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
190 struct gdlm_plock_info info;
191 struct plock_op *op = NULL;
193 if (count < sizeof(info))
196 spin_lock(&ops_lock);
197 if (!list_empty(&send_list)) {
198 op = list_entry(send_list.next, struct plock_op, list);
199 list_move(&op->list, &recv_list);
200 memcpy(&info, &op->info, sizeof(info));
202 spin_unlock(&ops_lock);
207 if (copy_to_user(u, &info, sizeof(info)))
212 /* a write copies in one plock result that should match a plock_op
214 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
217 struct gdlm_plock_info info;
221 if (count != sizeof(info))
224 if (copy_from_user(&info, u, sizeof(info)))
227 if (check_version(&info))
230 spin_lock(&ops_lock);
231 list_for_each_entry(op, &recv_list, list) {
232 if (op->info.fsid == info.fsid &&
233 op->info.number == info.number) {
234 list_del_init(&op->list);
237 memcpy(&op->info, &info, sizeof(info));
241 spin_unlock(&ops_lock);
246 printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
247 (unsigned long long)info.number);
251 static unsigned int dev_poll(struct file *file, poll_table *wait)
253 poll_wait(file, &send_wq, wait);
255 spin_lock(&ops_lock);
256 if (!list_empty(&send_list)) {
257 spin_unlock(&ops_lock);
258 return POLLIN | POLLRDNORM;
260 spin_unlock(&ops_lock);
264 static struct file_operations dev_fops = {
271 static struct miscdevice plock_dev_misc = {
272 .minor = MISC_DYNAMIC_MINOR,
273 .name = GDLM_PLOCK_MISC_NAME,
277 int gdlm_plock_init(void)
281 spin_lock_init(&ops_lock);
282 INIT_LIST_HEAD(&send_list);
283 INIT_LIST_HEAD(&recv_list);
284 init_waitqueue_head(&send_wq);
285 init_waitqueue_head(&recv_wq);
287 rv = misc_register(&plock_dev_misc);
289 printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
294 void gdlm_plock_exit(void)
296 if (misc_deregister(&plock_dev_misc) < 0)
297 printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");