Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * dlmlock.c |
| 5 | * |
| 6 | * underlying calls for lock creation |
| 7 | * |
| 8 | * Copyright (C) 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public |
| 21 | * License along with this program; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 021110-1307, USA. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/highmem.h> |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 33 | #include <linux/init.h> |
| 34 | #include <linux/sysctl.h> |
| 35 | #include <linux/random.h> |
| 36 | #include <linux/blkdev.h> |
| 37 | #include <linux/socket.h> |
| 38 | #include <linux/inet.h> |
| 39 | #include <linux/spinlock.h> |
| 40 | #include <linux/delay.h> |
| 41 | |
| 42 | |
| 43 | #include "cluster/heartbeat.h" |
| 44 | #include "cluster/nodemanager.h" |
| 45 | #include "cluster/tcp.h" |
| 46 | |
| 47 | #include "dlmapi.h" |
| 48 | #include "dlmcommon.h" |
| 49 | |
| 50 | #include "dlmconvert.h" |
| 51 | |
| 52 | #define MLOG_MASK_PREFIX ML_DLM |
| 53 | #include "cluster/masklog.h" |
| 54 | |
Sunil Mushran | 724bdca | 2008-03-10 15:16:20 -0700 | [diff] [blame] | 55 | static struct kmem_cache *dlm_lock_cache = NULL; |
| 56 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 57 | static DEFINE_SPINLOCK(dlm_cookie_lock); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 58 | static u64 dlm_next_cookie = 1; |
| 59 | |
| 60 | static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, |
| 61 | struct dlm_lock_resource *res, |
| 62 | struct dlm_lock *lock, int flags); |
| 63 | static void dlm_init_lock(struct dlm_lock *newlock, int type, |
| 64 | u8 node, u64 cookie); |
| 65 | static void dlm_lock_release(struct kref *kref); |
| 66 | static void dlm_lock_detach_lockres(struct dlm_lock *lock); |
| 67 | |
Sunil Mushran | 724bdca | 2008-03-10 15:16:20 -0700 | [diff] [blame] | 68 | int dlm_init_lock_cache(void) |
| 69 | { |
| 70 | dlm_lock_cache = kmem_cache_create("o2dlm_lock", |
| 71 | sizeof(struct dlm_lock), |
| 72 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 73 | if (dlm_lock_cache == NULL) |
| 74 | return -ENOMEM; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | void dlm_destroy_lock_cache(void) |
| 79 | { |
| 80 | if (dlm_lock_cache) |
| 81 | kmem_cache_destroy(dlm_lock_cache); |
| 82 | } |
| 83 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 84 | /* Tell us whether we can grant a new lock request. |
| 85 | * locking: |
| 86 | * caller needs: res->spinlock |
| 87 | * taken: none |
| 88 | * held on exit: none |
| 89 | * returns: 1 if the lock can be granted, 0 otherwise. |
| 90 | */ |
| 91 | static int dlm_can_grant_new_lock(struct dlm_lock_resource *res, |
| 92 | struct dlm_lock *lock) |
| 93 | { |
| 94 | struct list_head *iter; |
| 95 | struct dlm_lock *tmplock; |
| 96 | |
| 97 | list_for_each(iter, &res->granted) { |
| 98 | tmplock = list_entry(iter, struct dlm_lock, list); |
| 99 | |
| 100 | if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | list_for_each(iter, &res->converting) { |
| 105 | tmplock = list_entry(iter, struct dlm_lock, list); |
| 106 | |
| 107 | if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) |
| 108 | return 0; |
Wengang Wang | 66f4500 | 2010-12-08 20:34:39 +0800 | [diff] [blame] | 109 | if (!dlm_lock_compatible(tmplock->ml.convert_type, |
| 110 | lock->ml.type)) |
| 111 | return 0; |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | /* performs lock creation at the lockres master site |
| 118 | * locking: |
| 119 | * caller needs: none |
| 120 | * taken: takes and drops res->spinlock |
| 121 | * held on exit: none |
| 122 | * returns: DLM_NORMAL, DLM_NOTQUEUED |
| 123 | */ |
| 124 | static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm, |
| 125 | struct dlm_lock_resource *res, |
| 126 | struct dlm_lock *lock, int flags) |
| 127 | { |
| 128 | int call_ast = 0, kick_thread = 0; |
| 129 | enum dlm_status status = DLM_NORMAL; |
| 130 | |
Tao Ma | ef6b689 | 2011-02-21 11:10:44 +0800 | [diff] [blame] | 131 | mlog(0, "type=%d\n", lock->ml.type); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 132 | |
| 133 | spin_lock(&res->spinlock); |
| 134 | /* if called from dlm_create_lock_handler, need to |
| 135 | * ensure it will not sleep in dlm_wait_on_lockres */ |
| 136 | status = __dlm_lockres_state_to_status(res); |
| 137 | if (status != DLM_NORMAL && |
| 138 | lock->ml.node != dlm->node_num) { |
| 139 | /* erf. state changed after lock was dropped. */ |
| 140 | spin_unlock(&res->spinlock); |
| 141 | dlm_error(status); |
| 142 | return status; |
| 143 | } |
| 144 | __dlm_wait_on_lockres(res); |
| 145 | __dlm_lockres_reserve_ast(res); |
| 146 | |
| 147 | if (dlm_can_grant_new_lock(res, lock)) { |
| 148 | mlog(0, "I can grant this lock right away\n"); |
| 149 | /* got it right away */ |
| 150 | lock->lksb->status = DLM_NORMAL; |
| 151 | status = DLM_NORMAL; |
| 152 | dlm_lock_get(lock); |
| 153 | list_add_tail(&lock->list, &res->granted); |
| 154 | |
| 155 | /* for the recovery lock, we can't allow the ast |
| 156 | * to be queued since the dlmthread is already |
| 157 | * frozen. but the recovery lock is always locked |
| 158 | * with LKM_NOQUEUE so we do not need the ast in |
| 159 | * this special case */ |
| 160 | if (!dlm_is_recovery_lock(res->lockname.name, |
| 161 | res->lockname.len)) { |
| 162 | kick_thread = 1; |
| 163 | call_ast = 1; |
Kurt Hackel | c03872f | 2006-03-06 14:08:49 -0800 | [diff] [blame] | 164 | } else { |
| 165 | mlog(0, "%s: returning DLM_NORMAL to " |
| 166 | "node %u for reco lock\n", dlm->name, |
| 167 | lock->ml.node); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 168 | } |
| 169 | } else { |
| 170 | /* for NOQUEUE request, unless we get the |
| 171 | * lock right away, return DLM_NOTQUEUED */ |
Kurt Hackel | c03872f | 2006-03-06 14:08:49 -0800 | [diff] [blame] | 172 | if (flags & LKM_NOQUEUE) { |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 173 | status = DLM_NOTQUEUED; |
Kurt Hackel | c03872f | 2006-03-06 14:08:49 -0800 | [diff] [blame] | 174 | if (dlm_is_recovery_lock(res->lockname.name, |
| 175 | res->lockname.len)) { |
| 176 | mlog(0, "%s: returning NOTQUEUED to " |
| 177 | "node %u for reco lock\n", dlm->name, |
| 178 | lock->ml.node); |
| 179 | } |
| 180 | } else { |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 181 | dlm_lock_get(lock); |
| 182 | list_add_tail(&lock->list, &res->blocked); |
| 183 | kick_thread = 1; |
| 184 | } |
| 185 | } |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 186 | /* reduce the inflight count, this may result in the lockres |
| 187 | * being purged below during calc_usage */ |
| 188 | if (lock->ml.node == dlm->node_num) |
| 189 | dlm_lockres_drop_inflight_ref(dlm, res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 190 | |
| 191 | spin_unlock(&res->spinlock); |
| 192 | wake_up(&res->wq); |
| 193 | |
| 194 | /* either queue the ast or release it */ |
| 195 | if (call_ast) |
| 196 | dlm_queue_ast(dlm, lock); |
| 197 | else |
| 198 | dlm_lockres_release_ast(dlm, res); |
| 199 | |
| 200 | dlm_lockres_calc_usage(dlm, res); |
| 201 | if (kick_thread) |
| 202 | dlm_kick_thread(dlm, res); |
| 203 | |
| 204 | return status; |
| 205 | } |
| 206 | |
| 207 | void dlm_revert_pending_lock(struct dlm_lock_resource *res, |
| 208 | struct dlm_lock *lock) |
| 209 | { |
| 210 | /* remove from local queue if it failed */ |
| 211 | list_del_init(&lock->list); |
| 212 | lock->lksb->flags &= ~DLM_LKSB_GET_LVB; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* |
| 217 | * locking: |
| 218 | * caller needs: none |
| 219 | * taken: takes and drops res->spinlock |
| 220 | * held on exit: none |
| 221 | * returns: DLM_DENIED, DLM_RECOVERING, or net status |
| 222 | */ |
| 223 | static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm, |
| 224 | struct dlm_lock_resource *res, |
| 225 | struct dlm_lock *lock, int flags) |
| 226 | { |
| 227 | enum dlm_status status = DLM_DENIED; |
Kurt Hackel | 2abaf97 | 2006-05-01 13:27:10 -0700 | [diff] [blame] | 228 | int lockres_changed = 1; |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 229 | |
Tao Ma | ef6b689 | 2011-02-21 11:10:44 +0800 | [diff] [blame] | 230 | mlog(0, "type=%d, lockres %.*s, flags = 0x%x\n", |
| 231 | lock->ml.type, res->lockname.len, |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 232 | res->lockname.name, flags); |
| 233 | |
| 234 | spin_lock(&res->spinlock); |
| 235 | |
| 236 | /* will exit this call with spinlock held */ |
| 237 | __dlm_wait_on_lockres(res); |
| 238 | res->state |= DLM_LOCK_RES_IN_PROGRESS; |
| 239 | |
| 240 | /* add lock to local (secondary) queue */ |
| 241 | dlm_lock_get(lock); |
| 242 | list_add_tail(&lock->list, &res->blocked); |
| 243 | lock->lock_pending = 1; |
| 244 | spin_unlock(&res->spinlock); |
| 245 | |
| 246 | /* spec seems to say that you will get DLM_NORMAL when the lock |
| 247 | * has been queued, meaning we need to wait for a reply here. */ |
| 248 | status = dlm_send_remote_lock_request(dlm, res, lock, flags); |
| 249 | |
| 250 | spin_lock(&res->spinlock); |
| 251 | res->state &= ~DLM_LOCK_RES_IN_PROGRESS; |
| 252 | lock->lock_pending = 0; |
| 253 | if (status != DLM_NORMAL) { |
Kurt Hackel | c8df412 | 2006-05-01 13:47:50 -0700 | [diff] [blame] | 254 | if (status == DLM_RECOVERING && |
| 255 | dlm_is_recovery_lock(res->lockname.name, |
| 256 | res->lockname.len)) { |
| 257 | /* recovery lock was mastered by dead node. |
| 258 | * we need to have calc_usage shoot down this |
| 259 | * lockres and completely remaster it. */ |
| 260 | mlog(0, "%s: recovery lock was owned by " |
| 261 | "dead node %u, remaster it now.\n", |
| 262 | dlm->name, res->owner); |
| 263 | } else if (status != DLM_NOTQUEUED) { |
Kurt Hackel | c87a9ae | 2006-05-01 13:30:49 -0700 | [diff] [blame] | 264 | /* |
| 265 | * DO NOT call calc_usage, as this would unhash |
| 266 | * the remote lockres before we ever get to use |
| 267 | * it. treat as if we never made any change to |
| 268 | * the lockres. |
| 269 | */ |
| 270 | lockres_changed = 0; |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 271 | dlm_error(status); |
Kurt Hackel | c87a9ae | 2006-05-01 13:30:49 -0700 | [diff] [blame] | 272 | } |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 273 | dlm_revert_pending_lock(res, lock); |
| 274 | dlm_lock_put(lock); |
Sunil Mushran | 2bd6321 | 2010-01-25 16:57:38 -0800 | [diff] [blame] | 275 | } else if (dlm_is_recovery_lock(res->lockname.name, |
Kurt Hackel | 558c70c | 2006-01-18 17:07:47 -0800 | [diff] [blame] | 276 | res->lockname.len)) { |
| 277 | /* special case for the $RECOVERY lock. |
| 278 | * there will never be an AST delivered to put |
| 279 | * this lock on the proper secondary queue |
| 280 | * (granted), so do it manually. */ |
| 281 | mlog(0, "%s: $RECOVERY lock for this node (%u) is " |
| 282 | "mastered by %u; got lock, manually granting (no ast)\n", |
| 283 | dlm->name, dlm->node_num, res->owner); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 284 | list_move_tail(&lock->list, &res->granted); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 285 | } |
| 286 | spin_unlock(&res->spinlock); |
| 287 | |
Kurt Hackel | 2abaf97 | 2006-05-01 13:27:10 -0700 | [diff] [blame] | 288 | if (lockres_changed) |
| 289 | dlm_lockres_calc_usage(dlm, res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 290 | |
| 291 | wake_up(&res->wq); |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /* for remote lock creation. |
| 297 | * locking: |
| 298 | * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS |
| 299 | * taken: none |
| 300 | * held on exit: none |
| 301 | * returns: DLM_NOLOCKMGR, or net status |
| 302 | */ |
| 303 | static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, |
| 304 | struct dlm_lock_resource *res, |
| 305 | struct dlm_lock *lock, int flags) |
| 306 | { |
| 307 | struct dlm_create_lock create; |
| 308 | int tmpret, status = 0; |
| 309 | enum dlm_status ret; |
| 310 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 311 | memset(&create, 0, sizeof(create)); |
| 312 | create.node_idx = dlm->node_num; |
| 313 | create.requested_type = lock->ml.type; |
| 314 | create.cookie = lock->ml.cookie; |
| 315 | create.namelen = res->lockname.len; |
| 316 | create.flags = cpu_to_be32(flags); |
| 317 | memcpy(create.name, res->lockname.name, create.namelen); |
| 318 | |
| 319 | tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create, |
| 320 | sizeof(create), res->owner, &status); |
| 321 | if (tmpret >= 0) { |
Sunil Mushran | 8decab3 | 2011-07-24 10:23:54 -0700 | [diff] [blame^] | 322 | ret = status; |
Kurt Hackel | 495ac96 | 2006-05-01 14:34:08 -0700 | [diff] [blame] | 323 | if (ret == DLM_REJECTED) { |
Sunil Mushran | 8decab3 | 2011-07-24 10:23:54 -0700 | [diff] [blame^] | 324 | mlog(ML_ERROR, "%s: res %.*s, Stale lockres no longer " |
| 325 | "owned by node %u. That node is coming back up " |
| 326 | "currently.\n", dlm->name, create.namelen, |
Kurt Hackel | e4eb036 | 2006-05-01 11:46:59 -0700 | [diff] [blame] | 327 | create.name, res->owner); |
| 328 | dlm_print_one_lock_resource(res); |
| 329 | BUG(); |
| 330 | } |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 331 | } else { |
Sunil Mushran | 8decab3 | 2011-07-24 10:23:54 -0700 | [diff] [blame^] | 332 | mlog(ML_ERROR, "%s: res %.*s, Error %d send CREATE LOCK to " |
| 333 | "node %u\n", dlm->name, create.namelen, create.name, |
| 334 | tmpret, res->owner); |
| 335 | if (dlm_is_host_down(tmpret)) |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 336 | ret = DLM_RECOVERING; |
Sunil Mushran | 8decab3 | 2011-07-24 10:23:54 -0700 | [diff] [blame^] | 337 | else |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 338 | ret = dlm_err_to_dlm_status(tmpret); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | void dlm_lock_get(struct dlm_lock *lock) |
| 345 | { |
| 346 | kref_get(&lock->lock_refs); |
| 347 | } |
| 348 | |
| 349 | void dlm_lock_put(struct dlm_lock *lock) |
| 350 | { |
| 351 | kref_put(&lock->lock_refs, dlm_lock_release); |
| 352 | } |
| 353 | |
| 354 | static void dlm_lock_release(struct kref *kref) |
| 355 | { |
| 356 | struct dlm_lock *lock; |
| 357 | |
| 358 | lock = container_of(kref, struct dlm_lock, lock_refs); |
| 359 | |
| 360 | BUG_ON(!list_empty(&lock->list)); |
| 361 | BUG_ON(!list_empty(&lock->ast_list)); |
| 362 | BUG_ON(!list_empty(&lock->bast_list)); |
| 363 | BUG_ON(lock->ast_pending); |
| 364 | BUG_ON(lock->bast_pending); |
| 365 | |
| 366 | dlm_lock_detach_lockres(lock); |
| 367 | |
| 368 | if (lock->lksb_kernel_allocated) { |
| 369 | mlog(0, "freeing kernel-allocated lksb\n"); |
| 370 | kfree(lock->lksb); |
| 371 | } |
Sunil Mushran | 724bdca | 2008-03-10 15:16:20 -0700 | [diff] [blame] | 372 | kmem_cache_free(dlm_lock_cache, lock); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* associate a lock with it's lockres, getting a ref on the lockres */ |
| 376 | void dlm_lock_attach_lockres(struct dlm_lock *lock, |
| 377 | struct dlm_lock_resource *res) |
| 378 | { |
| 379 | dlm_lockres_get(res); |
| 380 | lock->lockres = res; |
| 381 | } |
| 382 | |
| 383 | /* drop ref on lockres, if there is still one associated with lock */ |
| 384 | static void dlm_lock_detach_lockres(struct dlm_lock *lock) |
| 385 | { |
| 386 | struct dlm_lock_resource *res; |
| 387 | |
| 388 | res = lock->lockres; |
| 389 | if (res) { |
| 390 | lock->lockres = NULL; |
| 391 | mlog(0, "removing lock's lockres reference\n"); |
| 392 | dlm_lockres_put(res); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static void dlm_init_lock(struct dlm_lock *newlock, int type, |
| 397 | u8 node, u64 cookie) |
| 398 | { |
| 399 | INIT_LIST_HEAD(&newlock->list); |
| 400 | INIT_LIST_HEAD(&newlock->ast_list); |
| 401 | INIT_LIST_HEAD(&newlock->bast_list); |
| 402 | spin_lock_init(&newlock->spinlock); |
| 403 | newlock->ml.type = type; |
| 404 | newlock->ml.convert_type = LKM_IVMODE; |
| 405 | newlock->ml.highest_blocked = LKM_IVMODE; |
| 406 | newlock->ml.node = node; |
| 407 | newlock->ml.pad1 = 0; |
| 408 | newlock->ml.list = 0; |
| 409 | newlock->ml.flags = 0; |
| 410 | newlock->ast = NULL; |
| 411 | newlock->bast = NULL; |
| 412 | newlock->astdata = NULL; |
| 413 | newlock->ml.cookie = cpu_to_be64(cookie); |
| 414 | newlock->ast_pending = 0; |
| 415 | newlock->bast_pending = 0; |
| 416 | newlock->convert_pending = 0; |
| 417 | newlock->lock_pending = 0; |
| 418 | newlock->unlock_pending = 0; |
| 419 | newlock->cancel_pending = 0; |
| 420 | newlock->lksb_kernel_allocated = 0; |
| 421 | |
| 422 | kref_init(&newlock->lock_refs); |
| 423 | } |
| 424 | |
| 425 | struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie, |
| 426 | struct dlm_lockstatus *lksb) |
| 427 | { |
| 428 | struct dlm_lock *lock; |
| 429 | int kernel_allocated = 0; |
| 430 | |
Julia Lawall | 3914ed0 | 2010-05-11 20:28:14 +0200 | [diff] [blame] | 431 | lock = kmem_cache_zalloc(dlm_lock_cache, GFP_NOFS); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 432 | if (!lock) |
| 433 | return NULL; |
| 434 | |
| 435 | if (!lksb) { |
| 436 | /* zero memory only if kernel-allocated */ |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 437 | lksb = kzalloc(sizeof(*lksb), GFP_NOFS); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 438 | if (!lksb) { |
| 439 | kfree(lock); |
| 440 | return NULL; |
| 441 | } |
| 442 | kernel_allocated = 1; |
| 443 | } |
| 444 | |
| 445 | dlm_init_lock(lock, type, node, cookie); |
| 446 | if (kernel_allocated) |
| 447 | lock->lksb_kernel_allocated = 1; |
| 448 | lock->lksb = lksb; |
| 449 | lksb->lockid = lock; |
| 450 | return lock; |
| 451 | } |
| 452 | |
| 453 | /* handler for lock creation net message |
| 454 | * locking: |
| 455 | * caller needs: none |
| 456 | * taken: takes and drops res->spinlock |
| 457 | * held on exit: none |
| 458 | * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED |
| 459 | */ |
Kurt Hackel | d74c980 | 2007-01-17 17:04:25 -0800 | [diff] [blame] | 460 | int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data, |
| 461 | void **ret_data) |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 462 | { |
| 463 | struct dlm_ctxt *dlm = data; |
| 464 | struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf; |
| 465 | struct dlm_lock_resource *res = NULL; |
| 466 | struct dlm_lock *newlock = NULL; |
| 467 | struct dlm_lockstatus *lksb = NULL; |
| 468 | enum dlm_status status = DLM_NORMAL; |
| 469 | char *name; |
| 470 | unsigned int namelen; |
| 471 | |
| 472 | BUG_ON(!dlm); |
| 473 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 474 | if (!dlm_grab(dlm)) |
| 475 | return DLM_REJECTED; |
| 476 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 477 | name = create->name; |
| 478 | namelen = create->namelen; |
Kurt Hackel | 495ac96 | 2006-05-01 14:34:08 -0700 | [diff] [blame] | 479 | status = DLM_REJECTED; |
Kurt Hackel | e4eb036 | 2006-05-01 11:46:59 -0700 | [diff] [blame] | 480 | if (!dlm_domain_fully_joined(dlm)) { |
| 481 | mlog(ML_ERROR, "Domain %s not fully joined, but node %u is " |
| 482 | "sending a create_lock message for lock %.*s!\n", |
| 483 | dlm->name, create->node_idx, namelen, name); |
| 484 | dlm_error(status); |
| 485 | goto leave; |
| 486 | } |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 487 | |
| 488 | status = DLM_IVBUFLEN; |
| 489 | if (namelen > DLM_LOCKID_NAME_MAX) { |
| 490 | dlm_error(status); |
| 491 | goto leave; |
| 492 | } |
| 493 | |
| 494 | status = DLM_SYSERR; |
| 495 | newlock = dlm_new_lock(create->requested_type, |
| 496 | create->node_idx, |
| 497 | be64_to_cpu(create->cookie), NULL); |
| 498 | if (!newlock) { |
| 499 | dlm_error(status); |
| 500 | goto leave; |
| 501 | } |
| 502 | |
| 503 | lksb = newlock->lksb; |
| 504 | |
| 505 | if (be32_to_cpu(create->flags) & LKM_GET_LVB) { |
| 506 | lksb->flags |= DLM_LKSB_GET_LVB; |
| 507 | mlog(0, "set DLM_LKSB_GET_LVB flag\n"); |
| 508 | } |
| 509 | |
| 510 | status = DLM_IVLOCKID; |
| 511 | res = dlm_lookup_lockres(dlm, name, namelen); |
| 512 | if (!res) { |
| 513 | dlm_error(status); |
| 514 | goto leave; |
| 515 | } |
| 516 | |
| 517 | spin_lock(&res->spinlock); |
| 518 | status = __dlm_lockres_state_to_status(res); |
| 519 | spin_unlock(&res->spinlock); |
| 520 | |
| 521 | if (status != DLM_NORMAL) { |
| 522 | mlog(0, "lockres recovering/migrating/in-progress\n"); |
| 523 | goto leave; |
| 524 | } |
| 525 | |
| 526 | dlm_lock_attach_lockres(newlock, res); |
| 527 | |
| 528 | status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags)); |
| 529 | leave: |
| 530 | if (status != DLM_NORMAL) |
| 531 | if (newlock) |
| 532 | dlm_lock_put(newlock); |
| 533 | |
| 534 | if (res) |
| 535 | dlm_lockres_put(res); |
| 536 | |
| 537 | dlm_put(dlm); |
| 538 | |
| 539 | return status; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */ |
| 544 | static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie) |
| 545 | { |
| 546 | u64 tmpnode = node_num; |
| 547 | |
| 548 | /* shift single byte of node num into top 8 bits */ |
| 549 | tmpnode <<= 56; |
| 550 | |
| 551 | spin_lock(&dlm_cookie_lock); |
| 552 | *cookie = (dlm_next_cookie | tmpnode); |
| 553 | if (++dlm_next_cookie & 0xff00000000000000ull) { |
| 554 | mlog(0, "This node's cookie will now wrap!\n"); |
| 555 | dlm_next_cookie = 1; |
| 556 | } |
| 557 | spin_unlock(&dlm_cookie_lock); |
| 558 | } |
| 559 | |
| 560 | enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode, |
| 561 | struct dlm_lockstatus *lksb, int flags, |
Mark Fasheh | 3384f3d | 2006-09-08 11:38:29 -0700 | [diff] [blame] | 562 | const char *name, int namelen, dlm_astlockfunc_t *ast, |
| 563 | void *data, dlm_bastlockfunc_t *bast) |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 564 | { |
| 565 | enum dlm_status status; |
| 566 | struct dlm_lock_resource *res = NULL; |
| 567 | struct dlm_lock *lock = NULL; |
| 568 | int convert = 0, recovery = 0; |
| 569 | |
| 570 | /* yes this function is a mess. |
| 571 | * TODO: clean this up. lots of common code in the |
| 572 | * lock and convert paths, especially in the retry blocks */ |
| 573 | if (!lksb) { |
| 574 | dlm_error(DLM_BADARGS); |
| 575 | return DLM_BADARGS; |
| 576 | } |
| 577 | |
| 578 | status = DLM_BADPARAM; |
| 579 | if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) { |
| 580 | dlm_error(status); |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | if (flags & ~LKM_VALID_FLAGS) { |
| 585 | dlm_error(status); |
| 586 | goto error; |
| 587 | } |
| 588 | |
| 589 | convert = (flags & LKM_CONVERT); |
| 590 | recovery = (flags & LKM_RECOVERY); |
| 591 | |
| 592 | if (recovery && |
Mark Fasheh | 3384f3d | 2006-09-08 11:38:29 -0700 | [diff] [blame] | 593 | (!dlm_is_recovery_lock(name, namelen) || convert) ) { |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 594 | dlm_error(status); |
| 595 | goto error; |
| 596 | } |
| 597 | if (convert && (flags & LKM_LOCAL)) { |
| 598 | mlog(ML_ERROR, "strange LOCAL convert request!\n"); |
| 599 | goto error; |
| 600 | } |
| 601 | |
| 602 | if (convert) { |
| 603 | /* CONVERT request */ |
| 604 | |
| 605 | /* if converting, must pass in a valid dlm_lock */ |
| 606 | lock = lksb->lockid; |
| 607 | if (!lock) { |
| 608 | mlog(ML_ERROR, "NULL lock pointer in convert " |
| 609 | "request\n"); |
| 610 | goto error; |
| 611 | } |
| 612 | |
| 613 | res = lock->lockres; |
| 614 | if (!res) { |
| 615 | mlog(ML_ERROR, "NULL lockres pointer in convert " |
| 616 | "request\n"); |
| 617 | goto error; |
| 618 | } |
| 619 | dlm_lockres_get(res); |
| 620 | |
| 621 | /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are |
| 622 | * static after the original lock call. convert requests will |
| 623 | * ensure that everything is the same, or return DLM_BADARGS. |
| 624 | * this means that DLM_DENIED_NOASTS will never be returned. |
| 625 | */ |
| 626 | if (lock->lksb != lksb || lock->ast != ast || |
| 627 | lock->bast != bast || lock->astdata != data) { |
| 628 | status = DLM_BADARGS; |
| 629 | mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, " |
| 630 | "astdata=%p\n", lksb, ast, bast, data); |
| 631 | mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, " |
| 632 | "astdata=%p\n", lock->lksb, lock->ast, |
| 633 | lock->bast, lock->astdata); |
| 634 | goto error; |
| 635 | } |
| 636 | retry_convert: |
| 637 | dlm_wait_for_recovery(dlm); |
| 638 | |
| 639 | if (res->owner == dlm->node_num) |
| 640 | status = dlmconvert_master(dlm, res, lock, flags, mode); |
| 641 | else |
| 642 | status = dlmconvert_remote(dlm, res, lock, flags, mode); |
| 643 | if (status == DLM_RECOVERING || status == DLM_MIGRATING || |
| 644 | status == DLM_FORWARD) { |
| 645 | /* for now, see how this works without sleeping |
| 646 | * and just retry right away. I suspect the reco |
| 647 | * or migration will complete fast enough that |
| 648 | * no waiting will be necessary */ |
| 649 | mlog(0, "retrying convert with migration/recovery/" |
| 650 | "in-progress\n"); |
| 651 | msleep(100); |
| 652 | goto retry_convert; |
| 653 | } |
| 654 | } else { |
| 655 | u64 tmpcookie; |
| 656 | |
| 657 | /* LOCK request */ |
| 658 | status = DLM_BADARGS; |
| 659 | if (!name) { |
| 660 | dlm_error(status); |
| 661 | goto error; |
| 662 | } |
| 663 | |
| 664 | status = DLM_IVBUFLEN; |
Mark Fasheh | 3384f3d | 2006-09-08 11:38:29 -0700 | [diff] [blame] | 665 | if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) { |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 666 | dlm_error(status); |
| 667 | goto error; |
| 668 | } |
| 669 | |
| 670 | dlm_get_next_cookie(dlm->node_num, &tmpcookie); |
| 671 | lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb); |
| 672 | if (!lock) { |
| 673 | dlm_error(status); |
| 674 | goto error; |
| 675 | } |
| 676 | |
| 677 | if (!recovery) |
| 678 | dlm_wait_for_recovery(dlm); |
| 679 | |
| 680 | /* find or create the lock resource */ |
Mark Fasheh | 3384f3d | 2006-09-08 11:38:29 -0700 | [diff] [blame] | 681 | res = dlm_get_lock_resource(dlm, name, namelen, flags); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 682 | if (!res) { |
| 683 | status = DLM_IVLOCKID; |
| 684 | dlm_error(status); |
| 685 | goto error; |
| 686 | } |
| 687 | |
| 688 | mlog(0, "type=%d, flags = 0x%x\n", mode, flags); |
| 689 | mlog(0, "creating lock: lock=%p res=%p\n", lock, res); |
| 690 | |
| 691 | dlm_lock_attach_lockres(lock, res); |
| 692 | lock->ast = ast; |
| 693 | lock->bast = bast; |
| 694 | lock->astdata = data; |
| 695 | |
| 696 | retry_lock: |
| 697 | if (flags & LKM_VALBLK) { |
| 698 | mlog(0, "LKM_VALBLK passed by caller\n"); |
| 699 | |
| 700 | /* LVB requests for non PR, PW or EX locks are |
| 701 | * ignored. */ |
| 702 | if (mode < LKM_PRMODE) |
| 703 | flags &= ~LKM_VALBLK; |
| 704 | else { |
| 705 | flags |= LKM_GET_LVB; |
| 706 | lock->lksb->flags |= DLM_LKSB_GET_LVB; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | if (res->owner == dlm->node_num) |
| 711 | status = dlmlock_master(dlm, res, lock, flags); |
| 712 | else |
| 713 | status = dlmlock_remote(dlm, res, lock, flags); |
| 714 | |
| 715 | if (status == DLM_RECOVERING || status == DLM_MIGRATING || |
| 716 | status == DLM_FORWARD) { |
| 717 | mlog(0, "retrying lock with migration/" |
| 718 | "recovery/in progress\n"); |
| 719 | msleep(100); |
Kurt Hackel | 44465a7 | 2006-01-18 17:05:38 -0800 | [diff] [blame] | 720 | /* no waiting for dlm_reco_thread */ |
| 721 | if (recovery) { |
Kurt Hackel | c8df412 | 2006-05-01 13:47:50 -0700 | [diff] [blame] | 722 | if (status != DLM_RECOVERING) |
| 723 | goto retry_lock; |
| 724 | |
| 725 | mlog(0, "%s: got RECOVERING " |
| 726 | "for $RECOVERY lock, master " |
| 727 | "was %u\n", dlm->name, |
| 728 | res->owner); |
| 729 | /* wait to see the node go down, then |
| 730 | * drop down and allow the lockres to |
| 731 | * get cleaned up. need to remaster. */ |
| 732 | dlm_wait_for_node_death(dlm, res->owner, |
| 733 | DLM_NODE_DEATH_WAIT_MAX); |
Kurt Hackel | 44465a7 | 2006-01-18 17:05:38 -0800 | [diff] [blame] | 734 | } else { |
| 735 | dlm_wait_for_recovery(dlm); |
Kurt Hackel | c8df412 | 2006-05-01 13:47:50 -0700 | [diff] [blame] | 736 | goto retry_lock; |
Kurt Hackel | 44465a7 | 2006-01-18 17:05:38 -0800 | [diff] [blame] | 737 | } |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if (status != DLM_NORMAL) { |
| 741 | lock->lksb->flags &= ~DLM_LKSB_GET_LVB; |
| 742 | if (status != DLM_NOTQUEUED) |
| 743 | dlm_error(status); |
| 744 | goto error; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | error: |
| 749 | if (status != DLM_NORMAL) { |
| 750 | if (lock && !convert) |
| 751 | dlm_lock_put(lock); |
| 752 | // this is kind of unnecessary |
| 753 | lksb->status = status; |
| 754 | } |
| 755 | |
| 756 | /* put lockres ref from the convert path |
| 757 | * or from dlm_get_lock_resource */ |
| 758 | if (res) |
| 759 | dlm_lockres_put(res); |
| 760 | |
| 761 | return status; |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(dlmlock); |