18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* -*- mode: c; c-basic-offset: 8; -*-
38c2ecf20Sopenharmony_ci * vim: noexpandtab sw=8 ts=8 sts=0:
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * dlmlock.c
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * underlying calls for lock creation
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2004 Oracle.  All rights reserved.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/fs.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/highmem.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/sysctl.h>
208c2ecf20Sopenharmony_ci#include <linux/random.h>
218c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
228c2ecf20Sopenharmony_ci#include <linux/socket.h>
238c2ecf20Sopenharmony_ci#include <linux/inet.h>
248c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
258c2ecf20Sopenharmony_ci#include <linux/delay.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "../cluster/heartbeat.h"
298c2ecf20Sopenharmony_ci#include "../cluster/nodemanager.h"
308c2ecf20Sopenharmony_ci#include "../cluster/tcp.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "dlmapi.h"
338c2ecf20Sopenharmony_ci#include "dlmcommon.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "dlmconvert.h"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define MLOG_MASK_PREFIX ML_DLM
388c2ecf20Sopenharmony_ci#include "../cluster/masklog.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct kmem_cache *dlm_lock_cache;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(dlm_cookie_lock);
438c2ecf20Sopenharmony_cistatic u64 dlm_next_cookie = 1;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
468c2ecf20Sopenharmony_ci					       struct dlm_lock_resource *res,
478c2ecf20Sopenharmony_ci					       struct dlm_lock *lock, int flags);
488c2ecf20Sopenharmony_cistatic void dlm_init_lock(struct dlm_lock *newlock, int type,
498c2ecf20Sopenharmony_ci			  u8 node, u64 cookie);
508c2ecf20Sopenharmony_cistatic void dlm_lock_release(struct kref *kref);
518c2ecf20Sopenharmony_cistatic void dlm_lock_detach_lockres(struct dlm_lock *lock);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint dlm_init_lock_cache(void)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	dlm_lock_cache = kmem_cache_create("o2dlm_lock",
568c2ecf20Sopenharmony_ci					   sizeof(struct dlm_lock),
578c2ecf20Sopenharmony_ci					   0, SLAB_HWCACHE_ALIGN, NULL);
588c2ecf20Sopenharmony_ci	if (dlm_lock_cache == NULL)
598c2ecf20Sopenharmony_ci		return -ENOMEM;
608c2ecf20Sopenharmony_ci	return 0;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_civoid dlm_destroy_lock_cache(void)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	kmem_cache_destroy(dlm_lock_cache);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Tell us whether we can grant a new lock request.
698c2ecf20Sopenharmony_ci * locking:
708c2ecf20Sopenharmony_ci *   caller needs:  res->spinlock
718c2ecf20Sopenharmony_ci *   taken:         none
728c2ecf20Sopenharmony_ci *   held on exit:  none
738c2ecf20Sopenharmony_ci * returns: 1 if the lock can be granted, 0 otherwise.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
768c2ecf20Sopenharmony_ci				  struct dlm_lock *lock)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct dlm_lock *tmplock;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	list_for_each_entry(tmplock, &res->granted, list) {
818c2ecf20Sopenharmony_ci		if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
828c2ecf20Sopenharmony_ci			return 0;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	list_for_each_entry(tmplock, &res->converting, list) {
868c2ecf20Sopenharmony_ci		if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
878c2ecf20Sopenharmony_ci			return 0;
888c2ecf20Sopenharmony_ci		if (!dlm_lock_compatible(tmplock->ml.convert_type,
898c2ecf20Sopenharmony_ci					 lock->ml.type))
908c2ecf20Sopenharmony_ci			return 0;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return 1;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* performs lock creation at the lockres master site
978c2ecf20Sopenharmony_ci * locking:
988c2ecf20Sopenharmony_ci *   caller needs:  none
998c2ecf20Sopenharmony_ci *   taken:         takes and drops res->spinlock
1008c2ecf20Sopenharmony_ci *   held on exit:  none
1018c2ecf20Sopenharmony_ci * returns: DLM_NORMAL, DLM_NOTQUEUED
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_cistatic enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
1048c2ecf20Sopenharmony_ci				      struct dlm_lock_resource *res,
1058c2ecf20Sopenharmony_ci				      struct dlm_lock *lock, int flags)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	int call_ast = 0, kick_thread = 0;
1088c2ecf20Sopenharmony_ci	enum dlm_status status = DLM_NORMAL;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	mlog(0, "type=%d\n", lock->ml.type);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	spin_lock(&res->spinlock);
1138c2ecf20Sopenharmony_ci	/* if called from dlm_create_lock_handler, need to
1148c2ecf20Sopenharmony_ci	 * ensure it will not sleep in dlm_wait_on_lockres */
1158c2ecf20Sopenharmony_ci	status = __dlm_lockres_state_to_status(res);
1168c2ecf20Sopenharmony_ci	if (status != DLM_NORMAL &&
1178c2ecf20Sopenharmony_ci	    lock->ml.node != dlm->node_num) {
1188c2ecf20Sopenharmony_ci		/* erf.  state changed after lock was dropped. */
1198c2ecf20Sopenharmony_ci		spin_unlock(&res->spinlock);
1208c2ecf20Sopenharmony_ci		dlm_error(status);
1218c2ecf20Sopenharmony_ci		return status;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci	__dlm_wait_on_lockres(res);
1248c2ecf20Sopenharmony_ci	__dlm_lockres_reserve_ast(res);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (dlm_can_grant_new_lock(res, lock)) {
1278c2ecf20Sopenharmony_ci		mlog(0, "I can grant this lock right away\n");
1288c2ecf20Sopenharmony_ci		/* got it right away */
1298c2ecf20Sopenharmony_ci		lock->lksb->status = DLM_NORMAL;
1308c2ecf20Sopenharmony_ci		status = DLM_NORMAL;
1318c2ecf20Sopenharmony_ci		dlm_lock_get(lock);
1328c2ecf20Sopenharmony_ci		list_add_tail(&lock->list, &res->granted);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		/* for the recovery lock, we can't allow the ast
1358c2ecf20Sopenharmony_ci		 * to be queued since the dlmthread is already
1368c2ecf20Sopenharmony_ci		 * frozen.  but the recovery lock is always locked
1378c2ecf20Sopenharmony_ci		 * with LKM_NOQUEUE so we do not need the ast in
1388c2ecf20Sopenharmony_ci		 * this special case */
1398c2ecf20Sopenharmony_ci		if (!dlm_is_recovery_lock(res->lockname.name,
1408c2ecf20Sopenharmony_ci					  res->lockname.len)) {
1418c2ecf20Sopenharmony_ci			kick_thread = 1;
1428c2ecf20Sopenharmony_ci			call_ast = 1;
1438c2ecf20Sopenharmony_ci		} else {
1448c2ecf20Sopenharmony_ci			mlog(0, "%s: returning DLM_NORMAL to "
1458c2ecf20Sopenharmony_ci			     "node %u for reco lock\n", dlm->name,
1468c2ecf20Sopenharmony_ci			     lock->ml.node);
1478c2ecf20Sopenharmony_ci		}
1488c2ecf20Sopenharmony_ci	} else {
1498c2ecf20Sopenharmony_ci		/* for NOQUEUE request, unless we get the
1508c2ecf20Sopenharmony_ci		 * lock right away, return DLM_NOTQUEUED */
1518c2ecf20Sopenharmony_ci		if (flags & LKM_NOQUEUE) {
1528c2ecf20Sopenharmony_ci			status = DLM_NOTQUEUED;
1538c2ecf20Sopenharmony_ci			if (dlm_is_recovery_lock(res->lockname.name,
1548c2ecf20Sopenharmony_ci						 res->lockname.len)) {
1558c2ecf20Sopenharmony_ci				mlog(0, "%s: returning NOTQUEUED to "
1568c2ecf20Sopenharmony_ci				     "node %u for reco lock\n", dlm->name,
1578c2ecf20Sopenharmony_ci				     lock->ml.node);
1588c2ecf20Sopenharmony_ci			}
1598c2ecf20Sopenharmony_ci		} else {
1608c2ecf20Sopenharmony_ci			status = DLM_NORMAL;
1618c2ecf20Sopenharmony_ci			dlm_lock_get(lock);
1628c2ecf20Sopenharmony_ci			list_add_tail(&lock->list, &res->blocked);
1638c2ecf20Sopenharmony_ci			kick_thread = 1;
1648c2ecf20Sopenharmony_ci		}
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	spin_unlock(&res->spinlock);
1688c2ecf20Sopenharmony_ci	wake_up(&res->wq);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/* either queue the ast or release it */
1718c2ecf20Sopenharmony_ci	if (call_ast)
1728c2ecf20Sopenharmony_ci		dlm_queue_ast(dlm, lock);
1738c2ecf20Sopenharmony_ci	else
1748c2ecf20Sopenharmony_ci		dlm_lockres_release_ast(dlm, res);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	dlm_lockres_calc_usage(dlm, res);
1778c2ecf20Sopenharmony_ci	if (kick_thread)
1788c2ecf20Sopenharmony_ci		dlm_kick_thread(dlm, res);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return status;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_civoid dlm_revert_pending_lock(struct dlm_lock_resource *res,
1848c2ecf20Sopenharmony_ci			     struct dlm_lock *lock)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	/* remove from local queue if it failed */
1878c2ecf20Sopenharmony_ci	list_del_init(&lock->list);
1888c2ecf20Sopenharmony_ci	lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/*
1938c2ecf20Sopenharmony_ci * locking:
1948c2ecf20Sopenharmony_ci *   caller needs:  none
1958c2ecf20Sopenharmony_ci *   taken:         takes and drops res->spinlock
1968c2ecf20Sopenharmony_ci *   held on exit:  none
1978c2ecf20Sopenharmony_ci * returns: DLM_DENIED, DLM_RECOVERING, or net status
1988c2ecf20Sopenharmony_ci */
1998c2ecf20Sopenharmony_cistatic enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
2008c2ecf20Sopenharmony_ci				      struct dlm_lock_resource *res,
2018c2ecf20Sopenharmony_ci				      struct dlm_lock *lock, int flags)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	enum dlm_status status = DLM_DENIED;
2048c2ecf20Sopenharmony_ci	int lockres_changed = 1;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	mlog(0, "type=%d, lockres %.*s, flags = 0x%x\n",
2078c2ecf20Sopenharmony_ci	     lock->ml.type, res->lockname.len,
2088c2ecf20Sopenharmony_ci	     res->lockname.name, flags);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/*
2118c2ecf20Sopenharmony_ci	 * Wait if resource is getting recovered, remastered, etc.
2128c2ecf20Sopenharmony_ci	 * If the resource was remastered and new owner is self, then exit.
2138c2ecf20Sopenharmony_ci	 */
2148c2ecf20Sopenharmony_ci	spin_lock(&res->spinlock);
2158c2ecf20Sopenharmony_ci	__dlm_wait_on_lockres(res);
2168c2ecf20Sopenharmony_ci	if (res->owner == dlm->node_num) {
2178c2ecf20Sopenharmony_ci		spin_unlock(&res->spinlock);
2188c2ecf20Sopenharmony_ci		return DLM_RECOVERING;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci	res->state |= DLM_LOCK_RES_IN_PROGRESS;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/* add lock to local (secondary) queue */
2238c2ecf20Sopenharmony_ci	dlm_lock_get(lock);
2248c2ecf20Sopenharmony_ci	list_add_tail(&lock->list, &res->blocked);
2258c2ecf20Sopenharmony_ci	lock->lock_pending = 1;
2268c2ecf20Sopenharmony_ci	spin_unlock(&res->spinlock);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* spec seems to say that you will get DLM_NORMAL when the lock
2298c2ecf20Sopenharmony_ci	 * has been queued, meaning we need to wait for a reply here. */
2308c2ecf20Sopenharmony_ci	status = dlm_send_remote_lock_request(dlm, res, lock, flags);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	spin_lock(&res->spinlock);
2338c2ecf20Sopenharmony_ci	res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
2348c2ecf20Sopenharmony_ci	lock->lock_pending = 0;
2358c2ecf20Sopenharmony_ci	if (status != DLM_NORMAL) {
2368c2ecf20Sopenharmony_ci		if (status == DLM_RECOVERING &&
2378c2ecf20Sopenharmony_ci		    dlm_is_recovery_lock(res->lockname.name,
2388c2ecf20Sopenharmony_ci					 res->lockname.len)) {
2398c2ecf20Sopenharmony_ci			/* recovery lock was mastered by dead node.
2408c2ecf20Sopenharmony_ci			 * we need to have calc_usage shoot down this
2418c2ecf20Sopenharmony_ci			 * lockres and completely remaster it. */
2428c2ecf20Sopenharmony_ci			mlog(0, "%s: recovery lock was owned by "
2438c2ecf20Sopenharmony_ci			     "dead node %u, remaster it now.\n",
2448c2ecf20Sopenharmony_ci			     dlm->name, res->owner);
2458c2ecf20Sopenharmony_ci		} else if (status != DLM_NOTQUEUED) {
2468c2ecf20Sopenharmony_ci			/*
2478c2ecf20Sopenharmony_ci			 * DO NOT call calc_usage, as this would unhash
2488c2ecf20Sopenharmony_ci			 * the remote lockres before we ever get to use
2498c2ecf20Sopenharmony_ci			 * it.  treat as if we never made any change to
2508c2ecf20Sopenharmony_ci			 * the lockres.
2518c2ecf20Sopenharmony_ci			 */
2528c2ecf20Sopenharmony_ci			lockres_changed = 0;
2538c2ecf20Sopenharmony_ci			dlm_error(status);
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci		dlm_revert_pending_lock(res, lock);
2568c2ecf20Sopenharmony_ci		dlm_lock_put(lock);
2578c2ecf20Sopenharmony_ci	} else if (dlm_is_recovery_lock(res->lockname.name,
2588c2ecf20Sopenharmony_ci					res->lockname.len)) {
2598c2ecf20Sopenharmony_ci		/* special case for the $RECOVERY lock.
2608c2ecf20Sopenharmony_ci		 * there will never be an AST delivered to put
2618c2ecf20Sopenharmony_ci		 * this lock on the proper secondary queue
2628c2ecf20Sopenharmony_ci		 * (granted), so do it manually. */
2638c2ecf20Sopenharmony_ci		mlog(0, "%s: $RECOVERY lock for this node (%u) is "
2648c2ecf20Sopenharmony_ci		     "mastered by %u; got lock, manually granting (no ast)\n",
2658c2ecf20Sopenharmony_ci		     dlm->name, dlm->node_num, res->owner);
2668c2ecf20Sopenharmony_ci		list_move_tail(&lock->list, &res->granted);
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci	spin_unlock(&res->spinlock);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (lockres_changed)
2718c2ecf20Sopenharmony_ci		dlm_lockres_calc_usage(dlm, res);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	wake_up(&res->wq);
2748c2ecf20Sopenharmony_ci	return status;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci/* for remote lock creation.
2798c2ecf20Sopenharmony_ci * locking:
2808c2ecf20Sopenharmony_ci *   caller needs:  none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
2818c2ecf20Sopenharmony_ci *   taken:         none
2828c2ecf20Sopenharmony_ci *   held on exit:  none
2838c2ecf20Sopenharmony_ci * returns: DLM_NOLOCKMGR, or net status
2848c2ecf20Sopenharmony_ci */
2858c2ecf20Sopenharmony_cistatic enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
2868c2ecf20Sopenharmony_ci					       struct dlm_lock_resource *res,
2878c2ecf20Sopenharmony_ci					       struct dlm_lock *lock, int flags)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct dlm_create_lock create;
2908c2ecf20Sopenharmony_ci	int tmpret, status = 0;
2918c2ecf20Sopenharmony_ci	enum dlm_status ret;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	memset(&create, 0, sizeof(create));
2948c2ecf20Sopenharmony_ci	create.node_idx = dlm->node_num;
2958c2ecf20Sopenharmony_ci	create.requested_type = lock->ml.type;
2968c2ecf20Sopenharmony_ci	create.cookie = lock->ml.cookie;
2978c2ecf20Sopenharmony_ci	create.namelen = res->lockname.len;
2988c2ecf20Sopenharmony_ci	create.flags = cpu_to_be32(flags);
2998c2ecf20Sopenharmony_ci	memcpy(create.name, res->lockname.name, create.namelen);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
3028c2ecf20Sopenharmony_ci				    sizeof(create), res->owner, &status);
3038c2ecf20Sopenharmony_ci	if (tmpret >= 0) {
3048c2ecf20Sopenharmony_ci		ret = status;
3058c2ecf20Sopenharmony_ci		if (ret == DLM_REJECTED) {
3068c2ecf20Sopenharmony_ci			mlog(ML_ERROR, "%s: res %.*s, Stale lockres no longer "
3078c2ecf20Sopenharmony_ci			     "owned by node %u. That node is coming back up "
3088c2ecf20Sopenharmony_ci			     "currently.\n", dlm->name, create.namelen,
3098c2ecf20Sopenharmony_ci			     create.name, res->owner);
3108c2ecf20Sopenharmony_ci			dlm_print_one_lock_resource(res);
3118c2ecf20Sopenharmony_ci			BUG();
3128c2ecf20Sopenharmony_ci		}
3138c2ecf20Sopenharmony_ci	} else {
3148c2ecf20Sopenharmony_ci		mlog(ML_ERROR, "%s: res %.*s, Error %d send CREATE LOCK to "
3158c2ecf20Sopenharmony_ci		     "node %u\n", dlm->name, create.namelen, create.name,
3168c2ecf20Sopenharmony_ci		     tmpret, res->owner);
3178c2ecf20Sopenharmony_ci		if (dlm_is_host_down(tmpret))
3188c2ecf20Sopenharmony_ci			ret = DLM_RECOVERING;
3198c2ecf20Sopenharmony_ci		else
3208c2ecf20Sopenharmony_ci			ret = dlm_err_to_dlm_status(tmpret);
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return ret;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_civoid dlm_lock_get(struct dlm_lock *lock)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	kref_get(&lock->lock_refs);
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_civoid dlm_lock_put(struct dlm_lock *lock)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	kref_put(&lock->lock_refs, dlm_lock_release);
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic void dlm_lock_release(struct kref *kref)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct dlm_lock *lock;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	lock = container_of(kref, struct dlm_lock, lock_refs);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&lock->list));
3438c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&lock->ast_list));
3448c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&lock->bast_list));
3458c2ecf20Sopenharmony_ci	BUG_ON(lock->ast_pending);
3468c2ecf20Sopenharmony_ci	BUG_ON(lock->bast_pending);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	dlm_lock_detach_lockres(lock);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (lock->lksb_kernel_allocated) {
3518c2ecf20Sopenharmony_ci		mlog(0, "freeing kernel-allocated lksb\n");
3528c2ecf20Sopenharmony_ci		kfree(lock->lksb);
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci	kmem_cache_free(dlm_lock_cache, lock);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/* associate a lock with it's lockres, getting a ref on the lockres */
3588c2ecf20Sopenharmony_civoid dlm_lock_attach_lockres(struct dlm_lock *lock,
3598c2ecf20Sopenharmony_ci			     struct dlm_lock_resource *res)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	dlm_lockres_get(res);
3628c2ecf20Sopenharmony_ci	lock->lockres = res;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/* drop ref on lockres, if there is still one associated with lock */
3668c2ecf20Sopenharmony_cistatic void dlm_lock_detach_lockres(struct dlm_lock *lock)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct dlm_lock_resource *res;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	res = lock->lockres;
3718c2ecf20Sopenharmony_ci	if (res) {
3728c2ecf20Sopenharmony_ci		lock->lockres = NULL;
3738c2ecf20Sopenharmony_ci		mlog(0, "removing lock's lockres reference\n");
3748c2ecf20Sopenharmony_ci		dlm_lockres_put(res);
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic void dlm_init_lock(struct dlm_lock *newlock, int type,
3798c2ecf20Sopenharmony_ci			  u8 node, u64 cookie)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&newlock->list);
3828c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&newlock->ast_list);
3838c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&newlock->bast_list);
3848c2ecf20Sopenharmony_ci	spin_lock_init(&newlock->spinlock);
3858c2ecf20Sopenharmony_ci	newlock->ml.type = type;
3868c2ecf20Sopenharmony_ci	newlock->ml.convert_type = LKM_IVMODE;
3878c2ecf20Sopenharmony_ci	newlock->ml.highest_blocked = LKM_IVMODE;
3888c2ecf20Sopenharmony_ci	newlock->ml.node = node;
3898c2ecf20Sopenharmony_ci	newlock->ml.pad1 = 0;
3908c2ecf20Sopenharmony_ci	newlock->ml.list = 0;
3918c2ecf20Sopenharmony_ci	newlock->ml.flags = 0;
3928c2ecf20Sopenharmony_ci	newlock->ast = NULL;
3938c2ecf20Sopenharmony_ci	newlock->bast = NULL;
3948c2ecf20Sopenharmony_ci	newlock->astdata = NULL;
3958c2ecf20Sopenharmony_ci	newlock->ml.cookie = cpu_to_be64(cookie);
3968c2ecf20Sopenharmony_ci	newlock->ast_pending = 0;
3978c2ecf20Sopenharmony_ci	newlock->bast_pending = 0;
3988c2ecf20Sopenharmony_ci	newlock->convert_pending = 0;
3998c2ecf20Sopenharmony_ci	newlock->lock_pending = 0;
4008c2ecf20Sopenharmony_ci	newlock->unlock_pending = 0;
4018c2ecf20Sopenharmony_ci	newlock->cancel_pending = 0;
4028c2ecf20Sopenharmony_ci	newlock->lksb_kernel_allocated = 0;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	kref_init(&newlock->lock_refs);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistruct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
4088c2ecf20Sopenharmony_ci			       struct dlm_lockstatus *lksb)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct dlm_lock *lock;
4118c2ecf20Sopenharmony_ci	int kernel_allocated = 0;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	lock = kmem_cache_zalloc(dlm_lock_cache, GFP_NOFS);
4148c2ecf20Sopenharmony_ci	if (!lock)
4158c2ecf20Sopenharmony_ci		return NULL;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (!lksb) {
4188c2ecf20Sopenharmony_ci		/* zero memory only if kernel-allocated */
4198c2ecf20Sopenharmony_ci		lksb = kzalloc(sizeof(*lksb), GFP_NOFS);
4208c2ecf20Sopenharmony_ci		if (!lksb) {
4218c2ecf20Sopenharmony_ci			kmem_cache_free(dlm_lock_cache, lock);
4228c2ecf20Sopenharmony_ci			return NULL;
4238c2ecf20Sopenharmony_ci		}
4248c2ecf20Sopenharmony_ci		kernel_allocated = 1;
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	dlm_init_lock(lock, type, node, cookie);
4288c2ecf20Sopenharmony_ci	if (kernel_allocated)
4298c2ecf20Sopenharmony_ci		lock->lksb_kernel_allocated = 1;
4308c2ecf20Sopenharmony_ci	lock->lksb = lksb;
4318c2ecf20Sopenharmony_ci	lksb->lockid = lock;
4328c2ecf20Sopenharmony_ci	return lock;
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci/* handler for lock creation net message
4368c2ecf20Sopenharmony_ci * locking:
4378c2ecf20Sopenharmony_ci *   caller needs:  none
4388c2ecf20Sopenharmony_ci *   taken:         takes and drops res->spinlock
4398c2ecf20Sopenharmony_ci *   held on exit:  none
4408c2ecf20Sopenharmony_ci * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
4418c2ecf20Sopenharmony_ci */
4428c2ecf20Sopenharmony_ciint dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
4438c2ecf20Sopenharmony_ci			    void **ret_data)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct dlm_ctxt *dlm = data;
4468c2ecf20Sopenharmony_ci	struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
4478c2ecf20Sopenharmony_ci	struct dlm_lock_resource *res = NULL;
4488c2ecf20Sopenharmony_ci	struct dlm_lock *newlock = NULL;
4498c2ecf20Sopenharmony_ci	struct dlm_lockstatus *lksb = NULL;
4508c2ecf20Sopenharmony_ci	enum dlm_status status = DLM_NORMAL;
4518c2ecf20Sopenharmony_ci	char *name;
4528c2ecf20Sopenharmony_ci	unsigned int namelen;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	BUG_ON(!dlm);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (!dlm_grab(dlm))
4578c2ecf20Sopenharmony_ci		return DLM_REJECTED;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	name = create->name;
4608c2ecf20Sopenharmony_ci	namelen = create->namelen;
4618c2ecf20Sopenharmony_ci	status = DLM_REJECTED;
4628c2ecf20Sopenharmony_ci	if (!dlm_domain_fully_joined(dlm)) {
4638c2ecf20Sopenharmony_ci		mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
4648c2ecf20Sopenharmony_ci		     "sending a create_lock message for lock %.*s!\n",
4658c2ecf20Sopenharmony_ci		     dlm->name, create->node_idx, namelen, name);
4668c2ecf20Sopenharmony_ci		dlm_error(status);
4678c2ecf20Sopenharmony_ci		goto leave;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	status = DLM_IVBUFLEN;
4718c2ecf20Sopenharmony_ci	if (namelen > DLM_LOCKID_NAME_MAX) {
4728c2ecf20Sopenharmony_ci		dlm_error(status);
4738c2ecf20Sopenharmony_ci		goto leave;
4748c2ecf20Sopenharmony_ci	}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	status = DLM_SYSERR;
4778c2ecf20Sopenharmony_ci	newlock = dlm_new_lock(create->requested_type,
4788c2ecf20Sopenharmony_ci			       create->node_idx,
4798c2ecf20Sopenharmony_ci			       be64_to_cpu(create->cookie), NULL);
4808c2ecf20Sopenharmony_ci	if (!newlock) {
4818c2ecf20Sopenharmony_ci		dlm_error(status);
4828c2ecf20Sopenharmony_ci		goto leave;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	lksb = newlock->lksb;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
4888c2ecf20Sopenharmony_ci		lksb->flags |= DLM_LKSB_GET_LVB;
4898c2ecf20Sopenharmony_ci		mlog(0, "set DLM_LKSB_GET_LVB flag\n");
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	status = DLM_IVLOCKID;
4938c2ecf20Sopenharmony_ci	res = dlm_lookup_lockres(dlm, name, namelen);
4948c2ecf20Sopenharmony_ci	if (!res) {
4958c2ecf20Sopenharmony_ci		dlm_error(status);
4968c2ecf20Sopenharmony_ci		goto leave;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	spin_lock(&res->spinlock);
5008c2ecf20Sopenharmony_ci	status = __dlm_lockres_state_to_status(res);
5018c2ecf20Sopenharmony_ci	spin_unlock(&res->spinlock);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (status != DLM_NORMAL) {
5048c2ecf20Sopenharmony_ci		mlog(0, "lockres recovering/migrating/in-progress\n");
5058c2ecf20Sopenharmony_ci		goto leave;
5068c2ecf20Sopenharmony_ci	}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	dlm_lock_attach_lockres(newlock, res);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
5118c2ecf20Sopenharmony_cileave:
5128c2ecf20Sopenharmony_ci	if (status != DLM_NORMAL)
5138c2ecf20Sopenharmony_ci		if (newlock)
5148c2ecf20Sopenharmony_ci			dlm_lock_put(newlock);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	if (res)
5178c2ecf20Sopenharmony_ci		dlm_lockres_put(res);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	dlm_put(dlm);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return status;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
5268c2ecf20Sopenharmony_cistatic inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	u64 tmpnode = node_num;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* shift single byte of node num into top 8 bits */
5318c2ecf20Sopenharmony_ci	tmpnode <<= 56;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	spin_lock(&dlm_cookie_lock);
5348c2ecf20Sopenharmony_ci	*cookie = (dlm_next_cookie | tmpnode);
5358c2ecf20Sopenharmony_ci	if (++dlm_next_cookie & 0xff00000000000000ull) {
5368c2ecf20Sopenharmony_ci		mlog(0, "This node's cookie will now wrap!\n");
5378c2ecf20Sopenharmony_ci		dlm_next_cookie = 1;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci	spin_unlock(&dlm_cookie_lock);
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cienum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
5438c2ecf20Sopenharmony_ci			struct dlm_lockstatus *lksb, int flags,
5448c2ecf20Sopenharmony_ci			const char *name, int namelen, dlm_astlockfunc_t *ast,
5458c2ecf20Sopenharmony_ci			void *data, dlm_bastlockfunc_t *bast)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	enum dlm_status status;
5488c2ecf20Sopenharmony_ci	struct dlm_lock_resource *res = NULL;
5498c2ecf20Sopenharmony_ci	struct dlm_lock *lock = NULL;
5508c2ecf20Sopenharmony_ci	int convert = 0, recovery = 0;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* yes this function is a mess.
5538c2ecf20Sopenharmony_ci	 * TODO: clean this up.  lots of common code in the
5548c2ecf20Sopenharmony_ci	 *       lock and convert paths, especially in the retry blocks */
5558c2ecf20Sopenharmony_ci	if (!lksb) {
5568c2ecf20Sopenharmony_ci		dlm_error(DLM_BADARGS);
5578c2ecf20Sopenharmony_ci		return DLM_BADARGS;
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	status = DLM_BADPARAM;
5618c2ecf20Sopenharmony_ci	if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
5628c2ecf20Sopenharmony_ci		dlm_error(status);
5638c2ecf20Sopenharmony_ci		goto error;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (flags & ~LKM_VALID_FLAGS) {
5678c2ecf20Sopenharmony_ci		dlm_error(status);
5688c2ecf20Sopenharmony_ci		goto error;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	convert = (flags & LKM_CONVERT);
5728c2ecf20Sopenharmony_ci	recovery = (flags & LKM_RECOVERY);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	if (recovery &&
5758c2ecf20Sopenharmony_ci	    (!dlm_is_recovery_lock(name, namelen) || convert) ) {
5768c2ecf20Sopenharmony_ci		dlm_error(status);
5778c2ecf20Sopenharmony_ci		goto error;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci	if (convert && (flags & LKM_LOCAL)) {
5808c2ecf20Sopenharmony_ci		mlog(ML_ERROR, "strange LOCAL convert request!\n");
5818c2ecf20Sopenharmony_ci		goto error;
5828c2ecf20Sopenharmony_ci	}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	if (convert) {
5858c2ecf20Sopenharmony_ci		/* CONVERT request */
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		/* if converting, must pass in a valid dlm_lock */
5888c2ecf20Sopenharmony_ci		lock = lksb->lockid;
5898c2ecf20Sopenharmony_ci		if (!lock) {
5908c2ecf20Sopenharmony_ci			mlog(ML_ERROR, "NULL lock pointer in convert "
5918c2ecf20Sopenharmony_ci			     "request\n");
5928c2ecf20Sopenharmony_ci			goto error;
5938c2ecf20Sopenharmony_ci		}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		res = lock->lockres;
5968c2ecf20Sopenharmony_ci		if (!res) {
5978c2ecf20Sopenharmony_ci			mlog(ML_ERROR, "NULL lockres pointer in convert "
5988c2ecf20Sopenharmony_ci			     "request\n");
5998c2ecf20Sopenharmony_ci			goto error;
6008c2ecf20Sopenharmony_ci		}
6018c2ecf20Sopenharmony_ci		dlm_lockres_get(res);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
6048c2ecf20Sopenharmony_ci	 	 * static after the original lock call.  convert requests will
6058c2ecf20Sopenharmony_ci		 * ensure that everything is the same, or return DLM_BADARGS.
6068c2ecf20Sopenharmony_ci	 	 * this means that DLM_DENIED_NOASTS will never be returned.
6078c2ecf20Sopenharmony_ci	 	 */
6088c2ecf20Sopenharmony_ci		if (lock->lksb != lksb || lock->ast != ast ||
6098c2ecf20Sopenharmony_ci		    lock->bast != bast || lock->astdata != data) {
6108c2ecf20Sopenharmony_ci			status = DLM_BADARGS;
6118c2ecf20Sopenharmony_ci			mlog(ML_ERROR, "new args:  lksb=%p, ast=%p, bast=%p, "
6128c2ecf20Sopenharmony_ci			     "astdata=%p\n", lksb, ast, bast, data);
6138c2ecf20Sopenharmony_ci			mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
6148c2ecf20Sopenharmony_ci			     "astdata=%p\n", lock->lksb, lock->ast,
6158c2ecf20Sopenharmony_ci			     lock->bast, lock->astdata);
6168c2ecf20Sopenharmony_ci			goto error;
6178c2ecf20Sopenharmony_ci		}
6188c2ecf20Sopenharmony_ciretry_convert:
6198c2ecf20Sopenharmony_ci		dlm_wait_for_recovery(dlm);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci		if (res->owner == dlm->node_num)
6228c2ecf20Sopenharmony_ci			status = dlmconvert_master(dlm, res, lock, flags, mode);
6238c2ecf20Sopenharmony_ci		else
6248c2ecf20Sopenharmony_ci			status = dlmconvert_remote(dlm, res, lock, flags, mode);
6258c2ecf20Sopenharmony_ci		if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
6268c2ecf20Sopenharmony_ci		    status == DLM_FORWARD) {
6278c2ecf20Sopenharmony_ci			/* for now, see how this works without sleeping
6288c2ecf20Sopenharmony_ci			 * and just retry right away.  I suspect the reco
6298c2ecf20Sopenharmony_ci			 * or migration will complete fast enough that
6308c2ecf20Sopenharmony_ci			 * no waiting will be necessary */
6318c2ecf20Sopenharmony_ci			mlog(0, "retrying convert with migration/recovery/"
6328c2ecf20Sopenharmony_ci			     "in-progress\n");
6338c2ecf20Sopenharmony_ci			msleep(100);
6348c2ecf20Sopenharmony_ci			goto retry_convert;
6358c2ecf20Sopenharmony_ci		}
6368c2ecf20Sopenharmony_ci	} else {
6378c2ecf20Sopenharmony_ci		u64 tmpcookie;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci		/* LOCK request */
6408c2ecf20Sopenharmony_ci		status = DLM_BADARGS;
6418c2ecf20Sopenharmony_ci		if (!name) {
6428c2ecf20Sopenharmony_ci			dlm_error(status);
6438c2ecf20Sopenharmony_ci			goto error;
6448c2ecf20Sopenharmony_ci		}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		status = DLM_IVBUFLEN;
6478c2ecf20Sopenharmony_ci		if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) {
6488c2ecf20Sopenharmony_ci			dlm_error(status);
6498c2ecf20Sopenharmony_ci			goto error;
6508c2ecf20Sopenharmony_ci		}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci		dlm_get_next_cookie(dlm->node_num, &tmpcookie);
6538c2ecf20Sopenharmony_ci		lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
6548c2ecf20Sopenharmony_ci		if (!lock) {
6558c2ecf20Sopenharmony_ci			dlm_error(status);
6568c2ecf20Sopenharmony_ci			goto error;
6578c2ecf20Sopenharmony_ci		}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci		if (!recovery)
6608c2ecf20Sopenharmony_ci			dlm_wait_for_recovery(dlm);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci		/* find or create the lock resource */
6638c2ecf20Sopenharmony_ci		res = dlm_get_lock_resource(dlm, name, namelen, flags);
6648c2ecf20Sopenharmony_ci		if (!res) {
6658c2ecf20Sopenharmony_ci			status = DLM_IVLOCKID;
6668c2ecf20Sopenharmony_ci			dlm_error(status);
6678c2ecf20Sopenharmony_ci			goto error;
6688c2ecf20Sopenharmony_ci		}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci		mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
6718c2ecf20Sopenharmony_ci		mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci		dlm_lock_attach_lockres(lock, res);
6748c2ecf20Sopenharmony_ci		lock->ast = ast;
6758c2ecf20Sopenharmony_ci		lock->bast = bast;
6768c2ecf20Sopenharmony_ci		lock->astdata = data;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ciretry_lock:
6798c2ecf20Sopenharmony_ci		if (flags & LKM_VALBLK) {
6808c2ecf20Sopenharmony_ci			mlog(0, "LKM_VALBLK passed by caller\n");
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci			/* LVB requests for non PR, PW or EX locks are
6838c2ecf20Sopenharmony_ci			 * ignored. */
6848c2ecf20Sopenharmony_ci			if (mode < LKM_PRMODE)
6858c2ecf20Sopenharmony_ci				flags &= ~LKM_VALBLK;
6868c2ecf20Sopenharmony_ci			else {
6878c2ecf20Sopenharmony_ci				flags |= LKM_GET_LVB;
6888c2ecf20Sopenharmony_ci				lock->lksb->flags |= DLM_LKSB_GET_LVB;
6898c2ecf20Sopenharmony_ci			}
6908c2ecf20Sopenharmony_ci		}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		if (res->owner == dlm->node_num)
6938c2ecf20Sopenharmony_ci			status = dlmlock_master(dlm, res, lock, flags);
6948c2ecf20Sopenharmony_ci		else
6958c2ecf20Sopenharmony_ci			status = dlmlock_remote(dlm, res, lock, flags);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci		if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
6988c2ecf20Sopenharmony_ci		    status == DLM_FORWARD) {
6998c2ecf20Sopenharmony_ci			msleep(100);
7008c2ecf20Sopenharmony_ci			if (recovery) {
7018c2ecf20Sopenharmony_ci				if (status != DLM_RECOVERING)
7028c2ecf20Sopenharmony_ci					goto retry_lock;
7038c2ecf20Sopenharmony_ci				/* wait to see the node go down, then
7048c2ecf20Sopenharmony_ci				 * drop down and allow the lockres to
7058c2ecf20Sopenharmony_ci				 * get cleaned up.  need to remaster. */
7068c2ecf20Sopenharmony_ci				dlm_wait_for_node_death(dlm, res->owner,
7078c2ecf20Sopenharmony_ci						DLM_NODE_DEATH_WAIT_MAX);
7088c2ecf20Sopenharmony_ci			} else {
7098c2ecf20Sopenharmony_ci				dlm_wait_for_recovery(dlm);
7108c2ecf20Sopenharmony_ci				goto retry_lock;
7118c2ecf20Sopenharmony_ci			}
7128c2ecf20Sopenharmony_ci		}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci		/* Inflight taken in dlm_get_lock_resource() is dropped here */
7158c2ecf20Sopenharmony_ci		spin_lock(&res->spinlock);
7168c2ecf20Sopenharmony_ci		dlm_lockres_drop_inflight_ref(dlm, res);
7178c2ecf20Sopenharmony_ci		spin_unlock(&res->spinlock);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		dlm_lockres_calc_usage(dlm, res);
7208c2ecf20Sopenharmony_ci		dlm_kick_thread(dlm, res);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		if (status != DLM_NORMAL) {
7238c2ecf20Sopenharmony_ci			lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
7248c2ecf20Sopenharmony_ci			if (status != DLM_NOTQUEUED)
7258c2ecf20Sopenharmony_ci				dlm_error(status);
7268c2ecf20Sopenharmony_ci			goto error;
7278c2ecf20Sopenharmony_ci		}
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cierror:
7318c2ecf20Sopenharmony_ci	if (status != DLM_NORMAL) {
7328c2ecf20Sopenharmony_ci		if (lock && !convert)
7338c2ecf20Sopenharmony_ci			dlm_lock_put(lock);
7348c2ecf20Sopenharmony_ci		// this is kind of unnecessary
7358c2ecf20Sopenharmony_ci		lksb->status = status;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	/* put lockres ref from the convert path
7398c2ecf20Sopenharmony_ci	 * or from dlm_get_lock_resource */
7408c2ecf20Sopenharmony_ci	if (res)
7418c2ecf20Sopenharmony_ci		dlm_lockres_put(res);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	return status;
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(dlmlock);
746