David Teigland | 29b7998 | 2006-01-16 16:52:38 +0000 | [diff] [blame^] | 1 | /****************************************************************************** |
| 2 | ******************************************************************************* |
| 3 | ** |
| 4 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 5 | ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 6 | ** |
| 7 | ** This copyrighted material is made available to anyone wishing to use, |
| 8 | ** modify, copy, or redistribute it subject to the terms and conditions |
| 9 | ** of the GNU General Public License v.2. |
| 10 | ** |
| 11 | ******************************************************************************* |
| 12 | ******************************************************************************/ |
| 13 | |
| 14 | #include "lock_dlm.h" |
| 15 | |
| 16 | int gdlm_drop_count; |
| 17 | int gdlm_drop_period; |
| 18 | struct lm_lockops gdlm_ops; |
| 19 | |
| 20 | |
| 21 | static struct gdlm_ls *init_gdlm(lm_callback_t cb, lm_fsdata_t *fsdata, |
| 22 | int flags, char *table_name) |
| 23 | { |
| 24 | struct gdlm_ls *ls; |
| 25 | char buf[256], *p; |
| 26 | |
| 27 | ls = kmalloc(sizeof(struct gdlm_ls), GFP_KERNEL); |
| 28 | if (!ls) |
| 29 | return NULL; |
| 30 | |
| 31 | memset(ls, 0, sizeof(struct gdlm_ls)); |
| 32 | |
| 33 | ls->drop_locks_count = gdlm_drop_count; |
| 34 | ls->drop_locks_period = gdlm_drop_period; |
| 35 | |
| 36 | ls->fscb = cb; |
| 37 | ls->fsdata = fsdata; |
| 38 | ls->fsflags = flags; |
| 39 | |
| 40 | spin_lock_init(&ls->async_lock); |
| 41 | |
| 42 | INIT_LIST_HEAD(&ls->complete); |
| 43 | INIT_LIST_HEAD(&ls->blocking); |
| 44 | INIT_LIST_HEAD(&ls->delayed); |
| 45 | INIT_LIST_HEAD(&ls->submit); |
| 46 | INIT_LIST_HEAD(&ls->all_locks); |
| 47 | |
| 48 | init_waitqueue_head(&ls->thread_wait); |
| 49 | init_waitqueue_head(&ls->wait_control); |
| 50 | ls->thread1 = NULL; |
| 51 | ls->thread2 = NULL; |
| 52 | ls->drop_time = jiffies; |
| 53 | ls->jid = -1; |
| 54 | |
| 55 | strncpy(buf, table_name, 256); |
| 56 | buf[255] = '\0'; |
| 57 | |
| 58 | p = strstr(buf, ":"); |
| 59 | if (!p) { |
| 60 | printk("lock_dlm: invalid table_name \"%s\"\n", table_name); |
| 61 | kfree(ls); |
| 62 | return NULL; |
| 63 | } |
| 64 | *p = '\0'; |
| 65 | p++; |
| 66 | |
| 67 | strncpy(ls->clustername, buf, 128); |
| 68 | strncpy(ls->fsname, p, 128); |
| 69 | |
| 70 | return ls; |
| 71 | } |
| 72 | |
| 73 | static int gdlm_mount(char *table_name, char *host_data, |
| 74 | lm_callback_t cb, lm_fsdata_t *fsdata, |
| 75 | unsigned int min_lvb_size, int flags, |
| 76 | struct lm_lockstruct *lockstruct) |
| 77 | { |
| 78 | struct gdlm_ls *ls; |
| 79 | int error = -ENOMEM; |
| 80 | |
| 81 | if (min_lvb_size > GDLM_LVB_SIZE) |
| 82 | goto out; |
| 83 | |
| 84 | ls = init_gdlm(cb, fsdata, flags, table_name); |
| 85 | if (!ls) |
| 86 | goto out; |
| 87 | |
| 88 | error = gdlm_init_threads(ls); |
| 89 | if (error) |
| 90 | goto out_free; |
| 91 | |
| 92 | error = dlm_new_lockspace(ls->fsname, strlen(ls->fsname), |
| 93 | &ls->dlm_lockspace, 0, GDLM_LVB_SIZE); |
| 94 | if (error) { |
| 95 | printk("lock_dlm: dlm_new_lockspace error %d\n", error); |
| 96 | goto out_thread; |
| 97 | } |
| 98 | |
| 99 | error = gdlm_kobject_setup(ls); |
| 100 | if (error) |
| 101 | goto out_dlm; |
| 102 | kobject_uevent(&ls->kobj, KOBJ_MOUNT, NULL); |
| 103 | |
| 104 | /* Now we depend on userspace to notice the new mount, |
| 105 | join the appropriate group, and do a write to our sysfs |
| 106 | "mounted" or "terminate" file. Before the start, userspace |
| 107 | must set "jid" and "first". */ |
| 108 | |
| 109 | error = wait_event_interruptible(ls->wait_control, |
| 110 | test_bit(DFL_JOIN_DONE, &ls->flags)); |
| 111 | if (error) |
| 112 | goto out_sysfs; |
| 113 | |
| 114 | if (test_bit(DFL_TERMINATE, &ls->flags)) { |
| 115 | error = -ERESTARTSYS; |
| 116 | goto out_sysfs; |
| 117 | } |
| 118 | |
| 119 | lockstruct->ls_jid = ls->jid; |
| 120 | lockstruct->ls_first = ls->first; |
| 121 | lockstruct->ls_lockspace = ls; |
| 122 | lockstruct->ls_ops = &gdlm_ops; |
| 123 | lockstruct->ls_flags = 0; |
| 124 | lockstruct->ls_lvb_size = GDLM_LVB_SIZE; |
| 125 | return 0; |
| 126 | |
| 127 | out_sysfs: |
| 128 | gdlm_kobject_release(ls); |
| 129 | out_dlm: |
| 130 | dlm_release_lockspace(ls->dlm_lockspace, 2); |
| 131 | out_thread: |
| 132 | gdlm_release_threads(ls); |
| 133 | out_free: |
| 134 | kfree(ls); |
| 135 | out: |
| 136 | return error; |
| 137 | } |
| 138 | |
| 139 | static void gdlm_unmount(lm_lockspace_t *lockspace) |
| 140 | { |
| 141 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; |
| 142 | int rv; |
| 143 | |
| 144 | log_debug("unmount flags %lx", ls->flags); |
| 145 | |
| 146 | if (test_bit(DFL_WITHDRAW, &ls->flags)) { |
| 147 | gdlm_kobject_release(ls); |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | kobject_uevent(&ls->kobj, KOBJ_UMOUNT, NULL); |
| 152 | |
| 153 | wait_event_interruptible(ls->wait_control, |
| 154 | test_bit(DFL_LEAVE_DONE, &ls->flags)); |
| 155 | |
| 156 | gdlm_kobject_release(ls); |
| 157 | dlm_release_lockspace(ls->dlm_lockspace, 2); |
| 158 | gdlm_release_threads(ls); |
| 159 | rv = gdlm_release_all_locks(ls); |
| 160 | if (rv) |
| 161 | log_all("lm_dlm_unmount: %d stray locks freed", rv); |
| 162 | out: |
| 163 | kfree(ls); |
| 164 | } |
| 165 | |
| 166 | static void gdlm_recovery_done(lm_lockspace_t *lockspace, unsigned int jid, |
| 167 | unsigned int message) |
| 168 | { |
| 169 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; |
| 170 | ls->recover_done = jid; |
| 171 | kobject_uevent(&ls->kobj, KOBJ_CHANGE, NULL); |
| 172 | } |
| 173 | |
| 174 | static void gdlm_others_may_mount(lm_lockspace_t *lockspace) |
| 175 | { |
| 176 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; |
| 177 | ls->first_done = 1; |
| 178 | kobject_uevent(&ls->kobj, KOBJ_CHANGE, NULL); |
| 179 | } |
| 180 | |
| 181 | static void gdlm_withdraw(lm_lockspace_t *lockspace) |
| 182 | { |
| 183 | struct gdlm_ls *ls = (struct gdlm_ls *) lockspace; |
| 184 | |
| 185 | /* userspace suspends locking on all other members */ |
| 186 | |
| 187 | kobject_uevent(&ls->kobj, KOBJ_OFFLINE, NULL); |
| 188 | |
| 189 | wait_event_interruptible(ls->wait_control, |
| 190 | test_bit(DFL_WITHDRAW, &ls->flags)); |
| 191 | |
| 192 | dlm_release_lockspace(ls->dlm_lockspace, 2); |
| 193 | gdlm_release_threads(ls); |
| 194 | gdlm_release_all_locks(ls); |
| 195 | |
| 196 | kobject_uevent(&ls->kobj, KOBJ_UMOUNT, NULL); |
| 197 | |
| 198 | /* userspace leaves the mount group, we don't need to wait for |
| 199 | that to complete */ |
| 200 | } |
| 201 | |
| 202 | int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name, |
| 203 | struct file *file, struct file_lock *fl) |
| 204 | { |
| 205 | return -ENOSYS; |
| 206 | } |
| 207 | |
| 208 | int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name, |
| 209 | struct file *file, struct file_lock *fl) |
| 210 | { |
| 211 | return -ENOSYS; |
| 212 | } |
| 213 | |
| 214 | int gdlm_plock(lm_lockspace_t *lockspace, struct lm_lockname *name, |
| 215 | struct file *file, int cmd, struct file_lock *fl) |
| 216 | { |
| 217 | return -ENOSYS; |
| 218 | } |
| 219 | |
| 220 | struct lm_lockops gdlm_ops = { |
| 221 | lm_proto_name:"lock_dlm", |
| 222 | lm_mount:gdlm_mount, |
| 223 | lm_others_may_mount:gdlm_others_may_mount, |
| 224 | lm_unmount:gdlm_unmount, |
| 225 | lm_withdraw:gdlm_withdraw, |
| 226 | lm_get_lock:gdlm_get_lock, |
| 227 | lm_put_lock:gdlm_put_lock, |
| 228 | lm_lock:gdlm_lock, |
| 229 | lm_unlock:gdlm_unlock, |
| 230 | lm_plock:gdlm_plock, |
| 231 | lm_punlock:gdlm_punlock, |
| 232 | lm_plock_get:gdlm_plock_get, |
| 233 | lm_cancel:gdlm_cancel, |
| 234 | lm_hold_lvb:gdlm_hold_lvb, |
| 235 | lm_unhold_lvb:gdlm_unhold_lvb, |
| 236 | lm_sync_lvb:gdlm_sync_lvb, |
| 237 | lm_recovery_done:gdlm_recovery_done, |
| 238 | lm_owner:THIS_MODULE, |
| 239 | }; |
| 240 | |