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