1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * underlying calls for lock creation
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/module.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>
44 #include "cluster/heartbeat.h"
45 #include "cluster/nodemanager.h"
46 #include "cluster/tcp.h"
49 #include "dlmcommon.h"
51 #include "dlmconvert.h"
53 #define MLOG_MASK_PREFIX ML_DLM
54 #include "cluster/masklog.h"
56 static spinlock_t dlm_cookie_lock = SPIN_LOCK_UNLOCKED;
57 static u64 dlm_next_cookie = 1;
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,
64 static void dlm_lock_release(struct kref *kref);
65 static void dlm_lock_detach_lockres(struct dlm_lock *lock);
67 /* Tell us whether we can grant a new lock request.
69 * caller needs: res->spinlock
72 * returns: 1 if the lock can be granted, 0 otherwise.
74 static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
75 struct dlm_lock *lock)
77 struct list_head *iter;
78 struct dlm_lock *tmplock;
80 list_for_each(iter, &res->granted) {
81 tmplock = list_entry(iter, struct dlm_lock, list);
83 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
87 list_for_each(iter, &res->converting) {
88 tmplock = list_entry(iter, struct dlm_lock, list);
90 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
97 /* performs lock creation at the lockres master site
100 * taken: takes and drops res->spinlock
102 * returns: DLM_NORMAL, DLM_NOTQUEUED
104 static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
105 struct dlm_lock_resource *res,
106 struct dlm_lock *lock, int flags)
108 int call_ast = 0, kick_thread = 0;
109 enum dlm_status status = DLM_NORMAL;
111 mlog_entry("type=%d\n", lock->ml.type);
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);
124 __dlm_wait_on_lockres(res);
125 __dlm_lockres_reserve_ast(res);
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;
133 list_add_tail(&lock->list, &res->granted);
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)) {
146 /* for NOQUEUE request, unless we get the
147 * lock right away, return DLM_NOTQUEUED */
148 if (flags & LKM_NOQUEUE)
149 status = DLM_NOTQUEUED;
152 list_add_tail(&lock->list, &res->blocked);
157 spin_unlock(&res->spinlock);
160 /* either queue the ast or release it */
162 dlm_queue_ast(dlm, lock);
164 dlm_lockres_release_ast(dlm, res);
166 dlm_lockres_calc_usage(dlm, res);
168 dlm_kick_thread(dlm, res);
173 void dlm_revert_pending_lock(struct dlm_lock_resource *res,
174 struct dlm_lock *lock)
176 /* remove from local queue if it failed */
177 list_del_init(&lock->list);
178 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
185 * taken: takes and drops res->spinlock
187 * returns: DLM_DENIED, DLM_RECOVERING, or net status
189 static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
190 struct dlm_lock_resource *res,
191 struct dlm_lock *lock, int flags)
193 enum dlm_status status = DLM_DENIED;
195 mlog_entry("type=%d\n", lock->ml.type);
196 mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
197 res->lockname.name, flags);
199 spin_lock(&res->spinlock);
201 /* will exit this call with spinlock held */
202 __dlm_wait_on_lockres(res);
203 res->state |= DLM_LOCK_RES_IN_PROGRESS;
205 /* add lock to local (secondary) queue */
207 list_add_tail(&lock->list, &res->blocked);
208 lock->lock_pending = 1;
209 spin_unlock(&res->spinlock);
211 /* spec seems to say that you will get DLM_NORMAL when the lock
212 * has been queued, meaning we need to wait for a reply here. */
213 status = dlm_send_remote_lock_request(dlm, res, lock, flags);
215 spin_lock(&res->spinlock);
216 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
217 lock->lock_pending = 0;
218 if (status != DLM_NORMAL) {
219 if (status != DLM_NOTQUEUED)
221 dlm_revert_pending_lock(res, lock);
224 spin_unlock(&res->spinlock);
226 dlm_lockres_calc_usage(dlm, res);
233 /* for remote lock creation.
235 * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
238 * returns: DLM_NOLOCKMGR, or net status
240 static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
241 struct dlm_lock_resource *res,
242 struct dlm_lock *lock, int flags)
244 struct dlm_create_lock create;
245 int tmpret, status = 0;
250 memset(&create, 0, sizeof(create));
251 create.node_idx = dlm->node_num;
252 create.requested_type = lock->ml.type;
253 create.cookie = lock->ml.cookie;
254 create.namelen = res->lockname.len;
255 create.flags = cpu_to_be32(flags);
256 memcpy(create.name, res->lockname.name, create.namelen);
258 tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
259 sizeof(create), res->owner, &status);
261 // successfully sent and received
262 ret = status; // this is already a dlm_status
265 if (dlm_is_host_down(tmpret)) {
266 ret = DLM_RECOVERING;
267 mlog(0, "node %u died so returning DLM_RECOVERING "
268 "from lock message!\n", res->owner);
270 ret = dlm_err_to_dlm_status(tmpret);
277 void dlm_lock_get(struct dlm_lock *lock)
279 kref_get(&lock->lock_refs);
282 void dlm_lock_put(struct dlm_lock *lock)
284 kref_put(&lock->lock_refs, dlm_lock_release);
287 static void dlm_lock_release(struct kref *kref)
289 struct dlm_lock *lock;
291 lock = container_of(kref, struct dlm_lock, lock_refs);
293 BUG_ON(!list_empty(&lock->list));
294 BUG_ON(!list_empty(&lock->ast_list));
295 BUG_ON(!list_empty(&lock->bast_list));
296 BUG_ON(lock->ast_pending);
297 BUG_ON(lock->bast_pending);
299 dlm_lock_detach_lockres(lock);
301 if (lock->lksb_kernel_allocated) {
302 mlog(0, "freeing kernel-allocated lksb\n");
308 /* associate a lock with it's lockres, getting a ref on the lockres */
309 void dlm_lock_attach_lockres(struct dlm_lock *lock,
310 struct dlm_lock_resource *res)
312 dlm_lockres_get(res);
316 /* drop ref on lockres, if there is still one associated with lock */
317 static void dlm_lock_detach_lockres(struct dlm_lock *lock)
319 struct dlm_lock_resource *res;
323 lock->lockres = NULL;
324 mlog(0, "removing lock's lockres reference\n");
325 dlm_lockres_put(res);
329 static void dlm_init_lock(struct dlm_lock *newlock, int type,
332 INIT_LIST_HEAD(&newlock->list);
333 INIT_LIST_HEAD(&newlock->ast_list);
334 INIT_LIST_HEAD(&newlock->bast_list);
335 spin_lock_init(&newlock->spinlock);
336 newlock->ml.type = type;
337 newlock->ml.convert_type = LKM_IVMODE;
338 newlock->ml.highest_blocked = LKM_IVMODE;
339 newlock->ml.node = node;
340 newlock->ml.pad1 = 0;
341 newlock->ml.list = 0;
342 newlock->ml.flags = 0;
344 newlock->bast = NULL;
345 newlock->astdata = NULL;
346 newlock->ml.cookie = cpu_to_be64(cookie);
347 newlock->ast_pending = 0;
348 newlock->bast_pending = 0;
349 newlock->convert_pending = 0;
350 newlock->lock_pending = 0;
351 newlock->unlock_pending = 0;
352 newlock->cancel_pending = 0;
353 newlock->lksb_kernel_allocated = 0;
355 kref_init(&newlock->lock_refs);
358 struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
359 struct dlm_lockstatus *lksb)
361 struct dlm_lock *lock;
362 int kernel_allocated = 0;
364 lock = kcalloc(1, sizeof(*lock), GFP_KERNEL);
369 /* zero memory only if kernel-allocated */
370 lksb = kcalloc(1, sizeof(*lksb), GFP_KERNEL);
375 kernel_allocated = 1;
378 dlm_init_lock(lock, type, node, cookie);
379 if (kernel_allocated)
380 lock->lksb_kernel_allocated = 1;
386 /* handler for lock creation net message
389 * taken: takes and drops res->spinlock
391 * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
393 int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data)
395 struct dlm_ctxt *dlm = data;
396 struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
397 struct dlm_lock_resource *res = NULL;
398 struct dlm_lock *newlock = NULL;
399 struct dlm_lockstatus *lksb = NULL;
400 enum dlm_status status = DLM_NORMAL;
402 unsigned int namelen;
411 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
412 "Domain %s not fully joined!\n", dlm->name);
415 namelen = create->namelen;
417 status = DLM_IVBUFLEN;
418 if (namelen > DLM_LOCKID_NAME_MAX) {
424 newlock = dlm_new_lock(create->requested_type,
426 be64_to_cpu(create->cookie), NULL);
432 lksb = newlock->lksb;
434 if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
435 lksb->flags |= DLM_LKSB_GET_LVB;
436 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
439 status = DLM_IVLOCKID;
440 res = dlm_lookup_lockres(dlm, name, namelen);
446 spin_lock(&res->spinlock);
447 status = __dlm_lockres_state_to_status(res);
448 spin_unlock(&res->spinlock);
450 if (status != DLM_NORMAL) {
451 mlog(0, "lockres recovering/migrating/in-progress\n");
455 dlm_lock_attach_lockres(newlock, res);
457 status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
459 if (status != DLM_NORMAL)
461 dlm_lock_put(newlock);
464 dlm_lockres_put(res);
472 /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
473 static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
475 u64 tmpnode = node_num;
477 /* shift single byte of node num into top 8 bits */
480 spin_lock(&dlm_cookie_lock);
481 *cookie = (dlm_next_cookie | tmpnode);
482 if (++dlm_next_cookie & 0xff00000000000000ull) {
483 mlog(0, "This node's cookie will now wrap!\n");
486 spin_unlock(&dlm_cookie_lock);
489 enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
490 struct dlm_lockstatus *lksb, int flags,
491 const char *name, dlm_astlockfunc_t *ast, void *data,
492 dlm_bastlockfunc_t *bast)
494 enum dlm_status status;
495 struct dlm_lock_resource *res = NULL;
496 struct dlm_lock *lock = NULL;
497 int convert = 0, recovery = 0;
499 /* yes this function is a mess.
500 * TODO: clean this up. lots of common code in the
501 * lock and convert paths, especially in the retry blocks */
503 dlm_error(DLM_BADARGS);
507 status = DLM_BADPARAM;
508 if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
513 if (flags & ~LKM_VALID_FLAGS) {
518 convert = (flags & LKM_CONVERT);
519 recovery = (flags & LKM_RECOVERY);
522 (!dlm_is_recovery_lock(name, strlen(name)) || convert) ) {
526 if (convert && (flags & LKM_LOCAL)) {
527 mlog(ML_ERROR, "strange LOCAL convert request!\n");
532 /* CONVERT request */
534 /* if converting, must pass in a valid dlm_lock */
537 mlog(ML_ERROR, "NULL lock pointer in convert "
544 mlog(ML_ERROR, "NULL lockres pointer in convert "
548 dlm_lockres_get(res);
550 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
551 * static after the original lock call. convert requests will
552 * ensure that everything is the same, or return DLM_BADARGS.
553 * this means that DLM_DENIED_NOASTS will never be returned.
555 if (lock->lksb != lksb || lock->ast != ast ||
556 lock->bast != bast || lock->astdata != data) {
557 status = DLM_BADARGS;
558 mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
559 "astdata=%p\n", lksb, ast, bast, data);
560 mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
561 "astdata=%p\n", lock->lksb, lock->ast,
562 lock->bast, lock->astdata);
566 dlm_wait_for_recovery(dlm);
568 if (res->owner == dlm->node_num)
569 status = dlmconvert_master(dlm, res, lock, flags, mode);
571 status = dlmconvert_remote(dlm, res, lock, flags, mode);
572 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
573 status == DLM_FORWARD) {
574 /* for now, see how this works without sleeping
575 * and just retry right away. I suspect the reco
576 * or migration will complete fast enough that
577 * no waiting will be necessary */
578 mlog(0, "retrying convert with migration/recovery/"
587 status = DLM_BADARGS;
593 status = DLM_IVBUFLEN;
594 if (strlen(name) > DLM_LOCKID_NAME_MAX || strlen(name) < 1) {
599 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
600 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
607 dlm_wait_for_recovery(dlm);
609 /* find or create the lock resource */
610 res = dlm_get_lock_resource(dlm, name, flags);
612 status = DLM_IVLOCKID;
617 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
618 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
620 dlm_lock_attach_lockres(lock, res);
623 lock->astdata = data;
626 if (flags & LKM_VALBLK) {
627 mlog(0, "LKM_VALBLK passed by caller\n");
629 /* LVB requests for non PR, PW or EX locks are
631 if (mode < LKM_PRMODE)
632 flags &= ~LKM_VALBLK;
634 flags |= LKM_GET_LVB;
635 lock->lksb->flags |= DLM_LKSB_GET_LVB;
639 if (res->owner == dlm->node_num)
640 status = dlmlock_master(dlm, res, lock, flags);
642 status = dlmlock_remote(dlm, res, lock, flags);
644 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
645 status == DLM_FORWARD) {
646 mlog(0, "retrying lock with migration/"
647 "recovery/in progress\n");
649 dlm_wait_for_recovery(dlm);
653 if (status != DLM_NORMAL) {
654 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
655 if (status != DLM_NOTQUEUED)
662 if (status != DLM_NORMAL) {
663 if (lock && !convert)
665 // this is kind of unnecessary
666 lksb->status = status;
669 /* put lockres ref from the convert path
670 * or from dlm_get_lock_resource */
672 dlm_lockres_put(res);
676 EXPORT_SYMBOL_GPL(dlmlock);