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 | * dlmthread.c |
| 5 | * |
| 6 | * standalone DLM module |
| 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/timer.h> |
| 41 | #include <linux/kthread.h> |
Kurt Hackel | 8d79d08 | 2006-04-27 17:58:23 -0700 | [diff] [blame] | 42 | #include <linux/delay.h> |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 43 | |
| 44 | |
| 45 | #include "cluster/heartbeat.h" |
| 46 | #include "cluster/nodemanager.h" |
| 47 | #include "cluster/tcp.h" |
| 48 | |
| 49 | #include "dlmapi.h" |
| 50 | #include "dlmcommon.h" |
| 51 | #include "dlmdomain.h" |
| 52 | |
| 53 | #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD) |
| 54 | #include "cluster/masklog.h" |
| 55 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 56 | static int dlm_thread(void *data); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 57 | static void dlm_flush_asts(struct dlm_ctxt *dlm); |
| 58 | |
| 59 | #define dlm_lock_is_remote(dlm, lock) ((lock)->ml.node != (dlm)->node_num) |
| 60 | |
| 61 | /* will exit holding res->spinlock, but may drop in function */ |
| 62 | /* waits until flags are cleared on res->state */ |
| 63 | void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags) |
| 64 | { |
| 65 | DECLARE_WAITQUEUE(wait, current); |
| 66 | |
| 67 | assert_spin_locked(&res->spinlock); |
| 68 | |
| 69 | add_wait_queue(&res->wq, &wait); |
| 70 | repeat: |
| 71 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 72 | if (res->state & flags) { |
| 73 | spin_unlock(&res->spinlock); |
| 74 | schedule(); |
| 75 | spin_lock(&res->spinlock); |
| 76 | goto repeat; |
| 77 | } |
| 78 | remove_wait_queue(&res->wq, &wait); |
| 79 | current->state = TASK_RUNNING; |
| 80 | } |
| 81 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 82 | int __dlm_lockres_has_locks(struct dlm_lock_resource *res) |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 83 | { |
| 84 | if (list_empty(&res->granted) && |
| 85 | list_empty(&res->converting) && |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 86 | list_empty(&res->blocked)) |
| 87 | return 0; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | /* "unused": the lockres has no locks, is not on the dirty list, |
| 92 | * has no inflight locks (in the gap between mastery and acquiring |
| 93 | * the first lock), and has no bits in its refmap. |
| 94 | * truly ready to be freed. */ |
| 95 | int __dlm_lockres_unused(struct dlm_lock_resource *res) |
| 96 | { |
| 97 | if (!__dlm_lockres_has_locks(res) && |
| 98 | list_empty(&res->dirty)) { |
| 99 | /* try not to scan the bitmap unless the first two |
| 100 | * conditions are already true */ |
| 101 | int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0); |
| 102 | if (bit >= O2NM_MAX_NODES) { |
| 103 | /* since the bit for dlm->node_num is not |
| 104 | * set, inflight_locks better be zero */ |
| 105 | BUG_ON(res->inflight_locks != 0); |
| 106 | return 1; |
| 107 | } |
| 108 | } |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /* Call whenever you may have added or deleted something from one of |
| 114 | * the lockres queue's. This will figure out whether it belongs on the |
| 115 | * unused list or not and does the appropriate thing. */ |
| 116 | void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm, |
| 117 | struct dlm_lock_resource *res) |
| 118 | { |
| 119 | mlog_entry("%.*s\n", res->lockname.len, res->lockname.name); |
| 120 | |
| 121 | assert_spin_locked(&dlm->spinlock); |
| 122 | assert_spin_locked(&res->spinlock); |
| 123 | |
| 124 | if (__dlm_lockres_unused(res)){ |
| 125 | if (list_empty(&res->purge)) { |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 126 | mlog(0, "putting lockres %.*s:%p onto purge list\n", |
| 127 | res->lockname.len, res->lockname.name, res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 128 | |
| 129 | res->last_used = jiffies; |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 130 | dlm_lockres_get(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 131 | list_add_tail(&res->purge, &dlm->purge_list); |
| 132 | dlm->purge_count++; |
| 133 | } |
| 134 | } else if (!list_empty(&res->purge)) { |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 135 | mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n", |
| 136 | res->lockname.len, res->lockname.name, res, res->owner); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 137 | |
| 138 | list_del_init(&res->purge); |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 139 | dlm_lockres_put(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 140 | dlm->purge_count--; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void dlm_lockres_calc_usage(struct dlm_ctxt *dlm, |
| 145 | struct dlm_lock_resource *res) |
| 146 | { |
| 147 | mlog_entry("%.*s\n", res->lockname.len, res->lockname.name); |
| 148 | spin_lock(&dlm->spinlock); |
| 149 | spin_lock(&res->spinlock); |
| 150 | |
| 151 | __dlm_lockres_calc_usage(dlm, res); |
| 152 | |
| 153 | spin_unlock(&res->spinlock); |
| 154 | spin_unlock(&dlm->spinlock); |
| 155 | } |
| 156 | |
Adrian Bunk | faf0ec9 | 2006-12-14 00:17:32 +0100 | [diff] [blame^] | 157 | static int dlm_purge_lockres(struct dlm_ctxt *dlm, |
| 158 | struct dlm_lock_resource *res) |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 159 | { |
| 160 | int master; |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 161 | int ret = 0; |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 162 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 163 | spin_lock(&res->spinlock); |
| 164 | if (!__dlm_lockres_unused(res)) { |
| 165 | spin_unlock(&res->spinlock); |
| 166 | mlog(0, "%s:%.*s: tried to purge but not unused\n", |
| 167 | dlm->name, res->lockname.len, res->lockname.name); |
| 168 | return -ENOTEMPTY; |
| 169 | } |
| 170 | master = (res->owner == dlm->node_num); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 171 | if (!master) |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 172 | res->state |= DLM_LOCK_RES_DROPPING_REF; |
| 173 | spin_unlock(&res->spinlock); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 174 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 175 | mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len, |
| 176 | res->lockname.name, master); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 177 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 178 | if (!master) { |
| 179 | /* drop spinlock to do messaging, retake below */ |
| 180 | spin_unlock(&dlm->spinlock); |
| 181 | /* clear our bit from the master's refmap, ignore errors */ |
| 182 | ret = dlm_drop_lockres_ref(dlm, res); |
| 183 | if (ret < 0) { |
| 184 | mlog_errno(ret); |
| 185 | if (!dlm_is_host_down(ret)) |
| 186 | BUG(); |
| 187 | } |
| 188 | mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n", |
| 189 | dlm->name, res->lockname.len, res->lockname.name, ret); |
| 190 | spin_lock(&dlm->spinlock); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 193 | if (!list_empty(&res->purge)) { |
| 194 | mlog(0, "removing lockres %.*s:%p from purgelist, " |
| 195 | "master = %d\n", res->lockname.len, res->lockname.name, |
| 196 | res, master); |
| 197 | list_del_init(&res->purge); |
| 198 | dlm_lockres_put(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 199 | dlm->purge_count--; |
| 200 | } |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 201 | __dlm_unhash_lockres(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 202 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 203 | /* lockres is not in the hash now. drop the flag and wake up |
| 204 | * any processes waiting in dlm_get_lock_resource. */ |
| 205 | if (!master) { |
| 206 | spin_lock(&res->spinlock); |
| 207 | res->state &= ~DLM_LOCK_RES_DROPPING_REF; |
| 208 | spin_unlock(&res->spinlock); |
| 209 | wake_up(&res->wq); |
Kurt Hackel | 8b21980 | 2006-05-01 11:16:45 -0700 | [diff] [blame] | 210 | } |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 211 | return 0; |
Kurt Hackel | 8b21980 | 2006-05-01 11:16:45 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 214 | static void dlm_run_purge_list(struct dlm_ctxt *dlm, |
| 215 | int purge_now) |
| 216 | { |
| 217 | unsigned int run_max, unused; |
| 218 | unsigned long purge_jiffies; |
| 219 | struct dlm_lock_resource *lockres; |
| 220 | |
| 221 | spin_lock(&dlm->spinlock); |
| 222 | run_max = dlm->purge_count; |
| 223 | |
| 224 | while(run_max && !list_empty(&dlm->purge_list)) { |
| 225 | run_max--; |
| 226 | |
| 227 | lockres = list_entry(dlm->purge_list.next, |
| 228 | struct dlm_lock_resource, purge); |
| 229 | |
| 230 | /* Status of the lockres *might* change so double |
| 231 | * check. If the lockres is unused, holding the dlm |
| 232 | * spinlock will prevent people from getting and more |
| 233 | * refs on it -- there's no need to keep the lockres |
| 234 | * spinlock. */ |
| 235 | spin_lock(&lockres->spinlock); |
| 236 | unused = __dlm_lockres_unused(lockres); |
| 237 | spin_unlock(&lockres->spinlock); |
| 238 | |
| 239 | if (!unused) |
| 240 | continue; |
| 241 | |
| 242 | purge_jiffies = lockres->last_used + |
| 243 | msecs_to_jiffies(DLM_PURGE_INTERVAL_MS); |
| 244 | |
| 245 | /* Make sure that we want to be processing this guy at |
| 246 | * this time. */ |
| 247 | if (!purge_now && time_after(purge_jiffies, jiffies)) { |
| 248 | /* Since resources are added to the purge list |
| 249 | * in tail order, we can stop at the first |
| 250 | * unpurgable resource -- anyone added after |
| 251 | * him will have a greater last_used value */ |
| 252 | break; |
| 253 | } |
| 254 | |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 255 | mlog(0, "removing lockres %.*s:%p from purgelist\n", |
| 256 | lockres->lockname.len, lockres->lockname.name, lockres); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 257 | list_del_init(&lockres->purge); |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 258 | dlm_lockres_put(lockres); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 259 | dlm->purge_count--; |
| 260 | |
| 261 | /* This may drop and reacquire the dlm spinlock if it |
| 262 | * has to do migration. */ |
| 263 | mlog(0, "calling dlm_purge_lockres!\n"); |
Kurt Hackel | ba2bf21 | 2006-12-01 14:47:20 -0800 | [diff] [blame] | 264 | if (dlm_purge_lockres(dlm, lockres)) |
| 265 | BUG(); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 266 | mlog(0, "DONE calling dlm_purge_lockres!\n"); |
| 267 | |
| 268 | /* Avoid adding any scheduling latencies */ |
| 269 | cond_resched_lock(&dlm->spinlock); |
| 270 | } |
| 271 | |
| 272 | spin_unlock(&dlm->spinlock); |
| 273 | } |
| 274 | |
| 275 | static void dlm_shuffle_lists(struct dlm_ctxt *dlm, |
| 276 | struct dlm_lock_resource *res) |
| 277 | { |
| 278 | struct dlm_lock *lock, *target; |
| 279 | struct list_head *iter; |
| 280 | struct list_head *head; |
| 281 | int can_grant = 1; |
| 282 | |
| 283 | //mlog(0, "res->lockname.len=%d\n", res->lockname.len); |
| 284 | //mlog(0, "res->lockname.name=%p\n", res->lockname.name); |
| 285 | //mlog(0, "shuffle res %.*s\n", res->lockname.len, |
| 286 | // res->lockname.name); |
| 287 | |
| 288 | /* because this function is called with the lockres |
| 289 | * spinlock, and because we know that it is not migrating/ |
| 290 | * recovering/in-progress, it is fine to reserve asts and |
| 291 | * basts right before queueing them all throughout */ |
| 292 | assert_spin_locked(&res->spinlock); |
| 293 | BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING| |
| 294 | DLM_LOCK_RES_RECOVERING| |
| 295 | DLM_LOCK_RES_IN_PROGRESS))); |
| 296 | |
| 297 | converting: |
| 298 | if (list_empty(&res->converting)) |
| 299 | goto blocked; |
| 300 | mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len, |
| 301 | res->lockname.name); |
| 302 | |
| 303 | target = list_entry(res->converting.next, struct dlm_lock, list); |
| 304 | if (target->ml.convert_type == LKM_IVMODE) { |
| 305 | mlog(ML_ERROR, "%.*s: converting a lock with no " |
| 306 | "convert_type!\n", res->lockname.len, res->lockname.name); |
| 307 | BUG(); |
| 308 | } |
| 309 | head = &res->granted; |
| 310 | list_for_each(iter, head) { |
| 311 | lock = list_entry(iter, struct dlm_lock, list); |
| 312 | if (lock==target) |
| 313 | continue; |
| 314 | if (!dlm_lock_compatible(lock->ml.type, |
| 315 | target->ml.convert_type)) { |
| 316 | can_grant = 0; |
| 317 | /* queue the BAST if not already */ |
| 318 | if (lock->ml.highest_blocked == LKM_IVMODE) { |
| 319 | __dlm_lockres_reserve_ast(res); |
| 320 | dlm_queue_bast(dlm, lock); |
| 321 | } |
| 322 | /* update the highest_blocked if needed */ |
| 323 | if (lock->ml.highest_blocked < target->ml.convert_type) |
| 324 | lock->ml.highest_blocked = |
| 325 | target->ml.convert_type; |
| 326 | } |
| 327 | } |
| 328 | head = &res->converting; |
| 329 | list_for_each(iter, head) { |
| 330 | lock = list_entry(iter, struct dlm_lock, list); |
| 331 | if (lock==target) |
| 332 | continue; |
| 333 | if (!dlm_lock_compatible(lock->ml.type, |
| 334 | target->ml.convert_type)) { |
| 335 | can_grant = 0; |
| 336 | if (lock->ml.highest_blocked == LKM_IVMODE) { |
| 337 | __dlm_lockres_reserve_ast(res); |
| 338 | dlm_queue_bast(dlm, lock); |
| 339 | } |
| 340 | if (lock->ml.highest_blocked < target->ml.convert_type) |
| 341 | lock->ml.highest_blocked = |
| 342 | target->ml.convert_type; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* we can convert the lock */ |
| 347 | if (can_grant) { |
| 348 | spin_lock(&target->spinlock); |
| 349 | BUG_ON(target->ml.highest_blocked != LKM_IVMODE); |
| 350 | |
| 351 | mlog(0, "calling ast for converting lock: %.*s, have: %d, " |
| 352 | "granting: %d, node: %u\n", res->lockname.len, |
| 353 | res->lockname.name, target->ml.type, |
| 354 | target->ml.convert_type, target->ml.node); |
| 355 | |
| 356 | target->ml.type = target->ml.convert_type; |
| 357 | target->ml.convert_type = LKM_IVMODE; |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 358 | list_move_tail(&target->list, &res->granted); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 359 | |
| 360 | BUG_ON(!target->lksb); |
| 361 | target->lksb->status = DLM_NORMAL; |
| 362 | |
| 363 | spin_unlock(&target->spinlock); |
| 364 | |
| 365 | __dlm_lockres_reserve_ast(res); |
| 366 | dlm_queue_ast(dlm, target); |
| 367 | /* go back and check for more */ |
| 368 | goto converting; |
| 369 | } |
| 370 | |
| 371 | blocked: |
| 372 | if (list_empty(&res->blocked)) |
| 373 | goto leave; |
| 374 | target = list_entry(res->blocked.next, struct dlm_lock, list); |
| 375 | |
| 376 | head = &res->granted; |
| 377 | list_for_each(iter, head) { |
| 378 | lock = list_entry(iter, struct dlm_lock, list); |
| 379 | if (lock==target) |
| 380 | continue; |
| 381 | if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) { |
| 382 | can_grant = 0; |
| 383 | if (lock->ml.highest_blocked == LKM_IVMODE) { |
| 384 | __dlm_lockres_reserve_ast(res); |
| 385 | dlm_queue_bast(dlm, lock); |
| 386 | } |
| 387 | if (lock->ml.highest_blocked < target->ml.type) |
| 388 | lock->ml.highest_blocked = target->ml.type; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | head = &res->converting; |
| 393 | list_for_each(iter, head) { |
| 394 | lock = list_entry(iter, struct dlm_lock, list); |
| 395 | if (lock==target) |
| 396 | continue; |
| 397 | if (!dlm_lock_compatible(lock->ml.type, target->ml.type)) { |
| 398 | can_grant = 0; |
| 399 | if (lock->ml.highest_blocked == LKM_IVMODE) { |
| 400 | __dlm_lockres_reserve_ast(res); |
| 401 | dlm_queue_bast(dlm, lock); |
| 402 | } |
| 403 | if (lock->ml.highest_blocked < target->ml.type) |
| 404 | lock->ml.highest_blocked = target->ml.type; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* we can grant the blocked lock (only |
| 409 | * possible if converting list empty) */ |
| 410 | if (can_grant) { |
| 411 | spin_lock(&target->spinlock); |
| 412 | BUG_ON(target->ml.highest_blocked != LKM_IVMODE); |
| 413 | |
| 414 | mlog(0, "calling ast for blocked lock: %.*s, granting: %d, " |
| 415 | "node: %u\n", res->lockname.len, res->lockname.name, |
| 416 | target->ml.type, target->ml.node); |
| 417 | |
| 418 | // target->ml.type is already correct |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 419 | list_move_tail(&target->list, &res->granted); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 420 | |
| 421 | BUG_ON(!target->lksb); |
| 422 | target->lksb->status = DLM_NORMAL; |
| 423 | |
| 424 | spin_unlock(&target->spinlock); |
| 425 | |
| 426 | __dlm_lockres_reserve_ast(res); |
| 427 | dlm_queue_ast(dlm, target); |
| 428 | /* go back and check for more */ |
| 429 | goto converting; |
| 430 | } |
| 431 | |
| 432 | leave: |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | /* must have NO locks when calling this with res !=NULL * */ |
| 437 | void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) |
| 438 | { |
| 439 | mlog_entry("dlm=%p, res=%p\n", dlm, res); |
| 440 | if (res) { |
| 441 | spin_lock(&dlm->spinlock); |
| 442 | spin_lock(&res->spinlock); |
| 443 | __dlm_dirty_lockres(dlm, res); |
| 444 | spin_unlock(&res->spinlock); |
| 445 | spin_unlock(&dlm->spinlock); |
| 446 | } |
| 447 | wake_up(&dlm->dlm_thread_wq); |
| 448 | } |
| 449 | |
| 450 | void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) |
| 451 | { |
| 452 | mlog_entry("dlm=%p, res=%p\n", dlm, res); |
| 453 | |
| 454 | assert_spin_locked(&dlm->spinlock); |
| 455 | assert_spin_locked(&res->spinlock); |
| 456 | |
| 457 | /* don't shuffle secondary queues */ |
| 458 | if ((res->owner == dlm->node_num) && |
| 459 | !(res->state & DLM_LOCK_RES_DIRTY)) { |
Kurt Hackel | 6ff06a9 | 2006-05-01 11:51:45 -0700 | [diff] [blame] | 460 | /* ref for dirty_list */ |
| 461 | dlm_lockres_get(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 462 | list_add_tail(&res->dirty, &dlm->dirty_list); |
| 463 | res->state |= DLM_LOCK_RES_DIRTY; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* Launch the NM thread for the mounted volume */ |
| 469 | int dlm_launch_thread(struct dlm_ctxt *dlm) |
| 470 | { |
| 471 | mlog(0, "starting dlm thread...\n"); |
| 472 | |
| 473 | dlm->dlm_thread_task = kthread_run(dlm_thread, dlm, "dlm_thread"); |
| 474 | if (IS_ERR(dlm->dlm_thread_task)) { |
| 475 | mlog_errno(PTR_ERR(dlm->dlm_thread_task)); |
| 476 | dlm->dlm_thread_task = NULL; |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | void dlm_complete_thread(struct dlm_ctxt *dlm) |
| 484 | { |
| 485 | if (dlm->dlm_thread_task) { |
| 486 | mlog(ML_KTHREAD, "waiting for dlm thread to exit\n"); |
| 487 | kthread_stop(dlm->dlm_thread_task); |
| 488 | dlm->dlm_thread_task = NULL; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static int dlm_dirty_list_empty(struct dlm_ctxt *dlm) |
| 493 | { |
| 494 | int empty; |
| 495 | |
| 496 | spin_lock(&dlm->spinlock); |
| 497 | empty = list_empty(&dlm->dirty_list); |
| 498 | spin_unlock(&dlm->spinlock); |
| 499 | |
| 500 | return empty; |
| 501 | } |
| 502 | |
| 503 | static void dlm_flush_asts(struct dlm_ctxt *dlm) |
| 504 | { |
| 505 | int ret; |
| 506 | struct dlm_lock *lock; |
| 507 | struct dlm_lock_resource *res; |
| 508 | u8 hi; |
| 509 | |
| 510 | spin_lock(&dlm->ast_lock); |
| 511 | while (!list_empty(&dlm->pending_asts)) { |
| 512 | lock = list_entry(dlm->pending_asts.next, |
| 513 | struct dlm_lock, ast_list); |
| 514 | /* get an extra ref on lock */ |
| 515 | dlm_lock_get(lock); |
| 516 | res = lock->lockres; |
| 517 | mlog(0, "delivering an ast for this lockres\n"); |
| 518 | |
| 519 | BUG_ON(!lock->ast_pending); |
| 520 | |
| 521 | /* remove from list (including ref) */ |
| 522 | list_del_init(&lock->ast_list); |
| 523 | dlm_lock_put(lock); |
| 524 | spin_unlock(&dlm->ast_lock); |
| 525 | |
| 526 | if (lock->ml.node != dlm->node_num) { |
| 527 | ret = dlm_do_remote_ast(dlm, res, lock); |
| 528 | if (ret < 0) |
| 529 | mlog_errno(ret); |
| 530 | } else |
| 531 | dlm_do_local_ast(dlm, res, lock); |
| 532 | |
| 533 | spin_lock(&dlm->ast_lock); |
| 534 | |
| 535 | /* possible that another ast was queued while |
| 536 | * we were delivering the last one */ |
| 537 | if (!list_empty(&lock->ast_list)) { |
| 538 | mlog(0, "aha another ast got queued while " |
| 539 | "we were finishing the last one. will " |
| 540 | "keep the ast_pending flag set.\n"); |
| 541 | } else |
| 542 | lock->ast_pending = 0; |
| 543 | |
| 544 | /* drop the extra ref. |
| 545 | * this may drop it completely. */ |
| 546 | dlm_lock_put(lock); |
| 547 | dlm_lockres_release_ast(dlm, res); |
| 548 | } |
| 549 | |
| 550 | while (!list_empty(&dlm->pending_basts)) { |
| 551 | lock = list_entry(dlm->pending_basts.next, |
| 552 | struct dlm_lock, bast_list); |
| 553 | /* get an extra ref on lock */ |
| 554 | dlm_lock_get(lock); |
| 555 | res = lock->lockres; |
| 556 | |
| 557 | BUG_ON(!lock->bast_pending); |
| 558 | |
| 559 | /* get the highest blocked lock, and reset */ |
| 560 | spin_lock(&lock->spinlock); |
| 561 | BUG_ON(lock->ml.highest_blocked <= LKM_IVMODE); |
| 562 | hi = lock->ml.highest_blocked; |
| 563 | lock->ml.highest_blocked = LKM_IVMODE; |
| 564 | spin_unlock(&lock->spinlock); |
| 565 | |
| 566 | /* remove from list (including ref) */ |
| 567 | list_del_init(&lock->bast_list); |
| 568 | dlm_lock_put(lock); |
| 569 | spin_unlock(&dlm->ast_lock); |
| 570 | |
| 571 | mlog(0, "delivering a bast for this lockres " |
| 572 | "(blocked = %d\n", hi); |
| 573 | |
| 574 | if (lock->ml.node != dlm->node_num) { |
| 575 | ret = dlm_send_proxy_bast(dlm, res, lock, hi); |
| 576 | if (ret < 0) |
| 577 | mlog_errno(ret); |
| 578 | } else |
| 579 | dlm_do_local_bast(dlm, res, lock, hi); |
| 580 | |
| 581 | spin_lock(&dlm->ast_lock); |
| 582 | |
| 583 | /* possible that another bast was queued while |
| 584 | * we were delivering the last one */ |
| 585 | if (!list_empty(&lock->bast_list)) { |
| 586 | mlog(0, "aha another bast got queued while " |
| 587 | "we were finishing the last one. will " |
| 588 | "keep the bast_pending flag set.\n"); |
| 589 | } else |
| 590 | lock->bast_pending = 0; |
| 591 | |
| 592 | /* drop the extra ref. |
| 593 | * this may drop it completely. */ |
| 594 | dlm_lock_put(lock); |
| 595 | dlm_lockres_release_ast(dlm, res); |
| 596 | } |
| 597 | wake_up(&dlm->ast_wq); |
| 598 | spin_unlock(&dlm->ast_lock); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | #define DLM_THREAD_TIMEOUT_MS (4 * 1000) |
| 603 | #define DLM_THREAD_MAX_DIRTY 100 |
| 604 | #define DLM_THREAD_MAX_ASTS 10 |
| 605 | |
| 606 | static int dlm_thread(void *data) |
| 607 | { |
| 608 | struct dlm_lock_resource *res; |
| 609 | struct dlm_ctxt *dlm = data; |
| 610 | unsigned long timeout = msecs_to_jiffies(DLM_THREAD_TIMEOUT_MS); |
| 611 | |
| 612 | mlog(0, "dlm thread running for %s...\n", dlm->name); |
| 613 | |
| 614 | while (!kthread_should_stop()) { |
| 615 | int n = DLM_THREAD_MAX_DIRTY; |
| 616 | |
| 617 | /* dlm_shutting_down is very point-in-time, but that |
| 618 | * doesn't matter as we'll just loop back around if we |
| 619 | * get false on the leading edge of a state |
| 620 | * transition. */ |
| 621 | dlm_run_purge_list(dlm, dlm_shutting_down(dlm)); |
| 622 | |
| 623 | /* We really don't want to hold dlm->spinlock while |
| 624 | * calling dlm_shuffle_lists on each lockres that |
| 625 | * needs to have its queues adjusted and AST/BASTs |
| 626 | * run. So let's pull each entry off the dirty_list |
| 627 | * and drop dlm->spinlock ASAP. Once off the list, |
| 628 | * res->spinlock needs to be taken again to protect |
| 629 | * the queues while calling dlm_shuffle_lists. */ |
| 630 | spin_lock(&dlm->spinlock); |
| 631 | while (!list_empty(&dlm->dirty_list)) { |
| 632 | int delay = 0; |
| 633 | res = list_entry(dlm->dirty_list.next, |
| 634 | struct dlm_lock_resource, dirty); |
| 635 | |
| 636 | /* peel a lockres off, remove it from the list, |
| 637 | * unset the dirty flag and drop the dlm lock */ |
| 638 | BUG_ON(!res); |
| 639 | dlm_lockres_get(res); |
| 640 | |
| 641 | spin_lock(&res->spinlock); |
| 642 | res->state &= ~DLM_LOCK_RES_DIRTY; |
| 643 | list_del_init(&res->dirty); |
| 644 | spin_unlock(&res->spinlock); |
| 645 | spin_unlock(&dlm->spinlock); |
Kurt Hackel | 6ff06a9 | 2006-05-01 11:51:45 -0700 | [diff] [blame] | 646 | /* Drop dirty_list ref */ |
| 647 | dlm_lockres_put(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 648 | |
| 649 | /* lockres can be re-dirtied/re-added to the |
| 650 | * dirty_list in this gap, but that is ok */ |
| 651 | |
| 652 | spin_lock(&res->spinlock); |
| 653 | if (res->owner != dlm->node_num) { |
| 654 | __dlm_print_one_lock_resource(res); |
| 655 | mlog(ML_ERROR, "inprog:%s, mig:%s, reco:%s, dirty:%s\n", |
| 656 | res->state & DLM_LOCK_RES_IN_PROGRESS ? "yes" : "no", |
| 657 | res->state & DLM_LOCK_RES_MIGRATING ? "yes" : "no", |
| 658 | res->state & DLM_LOCK_RES_RECOVERING ? "yes" : "no", |
| 659 | res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no"); |
| 660 | } |
| 661 | BUG_ON(res->owner != dlm->node_num); |
| 662 | |
| 663 | /* it is now ok to move lockreses in these states |
| 664 | * to the dirty list, assuming that they will only be |
| 665 | * dirty for a short while. */ |
| 666 | if (res->state & (DLM_LOCK_RES_IN_PROGRESS | |
| 667 | DLM_LOCK_RES_MIGRATING | |
| 668 | DLM_LOCK_RES_RECOVERING)) { |
| 669 | /* move it to the tail and keep going */ |
| 670 | spin_unlock(&res->spinlock); |
| 671 | mlog(0, "delaying list shuffling for in-" |
| 672 | "progress lockres %.*s, state=%d\n", |
| 673 | res->lockname.len, res->lockname.name, |
| 674 | res->state); |
| 675 | delay = 1; |
| 676 | goto in_progress; |
| 677 | } |
| 678 | |
| 679 | /* at this point the lockres is not migrating/ |
| 680 | * recovering/in-progress. we have the lockres |
| 681 | * spinlock and do NOT have the dlm lock. |
| 682 | * safe to reserve/queue asts and run the lists. */ |
| 683 | |
Kurt Hackel | 8d79d08 | 2006-04-27 17:58:23 -0700 | [diff] [blame] | 684 | mlog(0, "calling dlm_shuffle_lists with dlm=%s, " |
| 685 | "res=%.*s\n", dlm->name, |
| 686 | res->lockname.len, res->lockname.name); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 687 | |
| 688 | /* called while holding lockres lock */ |
| 689 | dlm_shuffle_lists(dlm, res); |
| 690 | spin_unlock(&res->spinlock); |
| 691 | |
| 692 | dlm_lockres_calc_usage(dlm, res); |
| 693 | |
| 694 | in_progress: |
| 695 | |
| 696 | spin_lock(&dlm->spinlock); |
| 697 | /* if the lock was in-progress, stick |
| 698 | * it on the back of the list */ |
| 699 | if (delay) { |
Kurt Hackel | 6ff06a9 | 2006-05-01 11:51:45 -0700 | [diff] [blame] | 700 | /* ref for dirty_list */ |
| 701 | dlm_lockres_get(res); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 702 | spin_lock(&res->spinlock); |
| 703 | list_add_tail(&res->dirty, &dlm->dirty_list); |
| 704 | res->state |= DLM_LOCK_RES_DIRTY; |
| 705 | spin_unlock(&res->spinlock); |
| 706 | } |
| 707 | dlm_lockres_put(res); |
| 708 | |
| 709 | /* unlikely, but we may need to give time to |
| 710 | * other tasks */ |
| 711 | if (!--n) { |
| 712 | mlog(0, "throttling dlm_thread\n"); |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | spin_unlock(&dlm->spinlock); |
| 718 | dlm_flush_asts(dlm); |
| 719 | |
| 720 | /* yield and continue right away if there is more work to do */ |
| 721 | if (!n) { |
Kurt Hackel | f85cd47 | 2006-05-01 14:27:41 -0700 | [diff] [blame] | 722 | cond_resched(); |
Kurt Hackel | 6714d8e | 2005-12-15 14:31:23 -0800 | [diff] [blame] | 723 | continue; |
| 724 | } |
| 725 | |
| 726 | wait_event_interruptible_timeout(dlm->dlm_thread_wq, |
| 727 | !dlm_dirty_list_empty(dlm) || |
| 728 | kthread_should_stop(), |
| 729 | timeout); |
| 730 | } |
| 731 | |
| 732 | mlog(0, "quitting DLM thread\n"); |
| 733 | return 0; |
| 734 | } |