xref: /OK3568_Linux_fs/kernel/fs/ocfs2/cluster/heartbeat.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun  * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2004, 2005 Oracle.  All rights reserved.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/jiffies.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/bio.h>
14*4882a593Smuzhiyun #include <linux/blkdev.h>
15*4882a593Smuzhiyun #include <linux/delay.h>
16*4882a593Smuzhiyun #include <linux/file.h>
17*4882a593Smuzhiyun #include <linux/kthread.h>
18*4882a593Smuzhiyun #include <linux/configfs.h>
19*4882a593Smuzhiyun #include <linux/random.h>
20*4882a593Smuzhiyun #include <linux/crc32.h>
21*4882a593Smuzhiyun #include <linux/time.h>
22*4882a593Smuzhiyun #include <linux/debugfs.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/bitmap.h>
25*4882a593Smuzhiyun #include <linux/ktime.h>
26*4882a593Smuzhiyun #include "heartbeat.h"
27*4882a593Smuzhiyun #include "tcp.h"
28*4882a593Smuzhiyun #include "nodemanager.h"
29*4882a593Smuzhiyun #include "quorum.h"
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "masklog.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * The first heartbeat pass had one global thread that would serialize all hb
36*4882a593Smuzhiyun  * callback calls.  This global serializing sem should only be removed once
37*4882a593Smuzhiyun  * we've made sure that all callees can deal with being called concurrently
38*4882a593Smuzhiyun  * from multiple hb region threads.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun static DECLARE_RWSEM(o2hb_callback_sem);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * multiple hb threads are watching multiple regions.  A node is live
44*4882a593Smuzhiyun  * whenever any of the threads sees activity from the node in its region.
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun static DEFINE_SPINLOCK(o2hb_live_lock);
47*4882a593Smuzhiyun static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
48*4882a593Smuzhiyun static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
49*4882a593Smuzhiyun static LIST_HEAD(o2hb_node_events);
50*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * In global heartbeat, we maintain a series of region bitmaps.
54*4882a593Smuzhiyun  * 	- o2hb_region_bitmap allows us to limit the region number to max region.
55*4882a593Smuzhiyun  * 	- o2hb_live_region_bitmap tracks live regions (seen steady iterations).
56*4882a593Smuzhiyun  * 	- o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
57*4882a593Smuzhiyun  * 		heartbeat on it.
58*4882a593Smuzhiyun  * 	- o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
61*4882a593Smuzhiyun static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
62*4882a593Smuzhiyun static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
63*4882a593Smuzhiyun static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #define O2HB_DB_TYPE_LIVENODES		0
66*4882a593Smuzhiyun #define O2HB_DB_TYPE_LIVEREGIONS	1
67*4882a593Smuzhiyun #define O2HB_DB_TYPE_QUORUMREGIONS	2
68*4882a593Smuzhiyun #define O2HB_DB_TYPE_FAILEDREGIONS	3
69*4882a593Smuzhiyun #define O2HB_DB_TYPE_REGION_LIVENODES	4
70*4882a593Smuzhiyun #define O2HB_DB_TYPE_REGION_NUMBER	5
71*4882a593Smuzhiyun #define O2HB_DB_TYPE_REGION_ELAPSED_TIME	6
72*4882a593Smuzhiyun #define O2HB_DB_TYPE_REGION_PINNED	7
73*4882a593Smuzhiyun struct o2hb_debug_buf {
74*4882a593Smuzhiyun 	int db_type;
75*4882a593Smuzhiyun 	int db_size;
76*4882a593Smuzhiyun 	int db_len;
77*4882a593Smuzhiyun 	void *db_data;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static struct o2hb_debug_buf *o2hb_db_livenodes;
81*4882a593Smuzhiyun static struct o2hb_debug_buf *o2hb_db_liveregions;
82*4882a593Smuzhiyun static struct o2hb_debug_buf *o2hb_db_quorumregions;
83*4882a593Smuzhiyun static struct o2hb_debug_buf *o2hb_db_failedregions;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define O2HB_DEBUG_DIR			"o2hb"
86*4882a593Smuzhiyun #define O2HB_DEBUG_LIVENODES		"livenodes"
87*4882a593Smuzhiyun #define O2HB_DEBUG_LIVEREGIONS		"live_regions"
88*4882a593Smuzhiyun #define O2HB_DEBUG_QUORUMREGIONS	"quorum_regions"
89*4882a593Smuzhiyun #define O2HB_DEBUG_FAILEDREGIONS	"failed_regions"
90*4882a593Smuzhiyun #define O2HB_DEBUG_REGION_NUMBER	"num"
91*4882a593Smuzhiyun #define O2HB_DEBUG_REGION_ELAPSED_TIME	"elapsed_time_in_ms"
92*4882a593Smuzhiyun #define O2HB_DEBUG_REGION_PINNED	"pinned"
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun static struct dentry *o2hb_debug_dir;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static LIST_HEAD(o2hb_all_regions);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun static struct o2hb_callback {
99*4882a593Smuzhiyun 	struct list_head list;
100*4882a593Smuzhiyun } o2hb_callbacks[O2HB_NUM_CB];
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun enum o2hb_heartbeat_modes {
105*4882a593Smuzhiyun 	O2HB_HEARTBEAT_LOCAL		= 0,
106*4882a593Smuzhiyun 	O2HB_HEARTBEAT_GLOBAL,
107*4882a593Smuzhiyun 	O2HB_HEARTBEAT_NUM_MODES,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun static const char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
111*4882a593Smuzhiyun 	"local",	/* O2HB_HEARTBEAT_LOCAL */
112*4882a593Smuzhiyun 	"global",	/* O2HB_HEARTBEAT_GLOBAL */
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
116*4882a593Smuzhiyun static unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun  * o2hb_dependent_users tracks the number of registered callbacks that depend
120*4882a593Smuzhiyun  * on heartbeat. o2net and o2dlm are two entities that register this callback.
121*4882a593Smuzhiyun  * However only o2dlm depends on the heartbeat. It does not want the heartbeat
122*4882a593Smuzhiyun  * to stop while a dlm domain is still active.
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun static unsigned int o2hb_dependent_users;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * In global heartbeat mode, all regions are pinned if there are one or more
128*4882a593Smuzhiyun  * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All
129*4882a593Smuzhiyun  * regions are unpinned if the region count exceeds the cut off or the number
130*4882a593Smuzhiyun  * of dependent users falls to zero.
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun #define O2HB_PIN_CUT_OFF		3
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun  * In local heartbeat mode, we assume the dlm domain name to be the same as
136*4882a593Smuzhiyun  * region uuid. This is true for domains created for the file system but not
137*4882a593Smuzhiyun  * necessarily true for userdlm domains. This is a known limitation.
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * In global heartbeat mode, we pin/unpin all o2hb regions. This solution
140*4882a593Smuzhiyun  * works for both file system and userdlm domains.
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun static int o2hb_region_pin(const char *region_uuid);
143*4882a593Smuzhiyun static void o2hb_region_unpin(const char *region_uuid);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /* Only sets a new threshold if there are no active regions.
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * No locking or otherwise interesting code is required for reading
148*4882a593Smuzhiyun  * o2hb_dead_threshold as it can't change once regions are active and
149*4882a593Smuzhiyun  * it's not interesting to anyone until then anyway. */
o2hb_dead_threshold_set(unsigned int threshold)150*4882a593Smuzhiyun static void o2hb_dead_threshold_set(unsigned int threshold)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
153*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
154*4882a593Smuzhiyun 		if (list_empty(&o2hb_all_regions))
155*4882a593Smuzhiyun 			o2hb_dead_threshold = threshold;
156*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
o2hb_global_heartbeat_mode_set(unsigned int hb_mode)160*4882a593Smuzhiyun static int o2hb_global_heartbeat_mode_set(unsigned int hb_mode)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	int ret = -1;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
165*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
166*4882a593Smuzhiyun 		if (list_empty(&o2hb_all_regions)) {
167*4882a593Smuzhiyun 			o2hb_heartbeat_mode = hb_mode;
168*4882a593Smuzhiyun 			ret = 0;
169*4882a593Smuzhiyun 		}
170*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return ret;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun struct o2hb_node_event {
177*4882a593Smuzhiyun 	struct list_head        hn_item;
178*4882a593Smuzhiyun 	enum o2hb_callback_type hn_event_type;
179*4882a593Smuzhiyun 	struct o2nm_node        *hn_node;
180*4882a593Smuzhiyun 	int                     hn_node_num;
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun struct o2hb_disk_slot {
184*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *ds_raw_block;
185*4882a593Smuzhiyun 	u8			ds_node_num;
186*4882a593Smuzhiyun 	u64			ds_last_time;
187*4882a593Smuzhiyun 	u64			ds_last_generation;
188*4882a593Smuzhiyun 	u16			ds_equal_samples;
189*4882a593Smuzhiyun 	u16			ds_changed_samples;
190*4882a593Smuzhiyun 	struct list_head	ds_live_item;
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /* each thread owns a region.. when we're asked to tear down the region
194*4882a593Smuzhiyun  * we ask the thread to stop, who cleans up the region */
195*4882a593Smuzhiyun struct o2hb_region {
196*4882a593Smuzhiyun 	struct config_item	hr_item;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	struct list_head	hr_all_item;
199*4882a593Smuzhiyun 	unsigned		hr_unclean_stop:1,
200*4882a593Smuzhiyun 				hr_aborted_start:1,
201*4882a593Smuzhiyun 				hr_item_pinned:1,
202*4882a593Smuzhiyun 				hr_item_dropped:1,
203*4882a593Smuzhiyun 				hr_node_deleted:1;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* protected by the hr_callback_sem */
206*4882a593Smuzhiyun 	struct task_struct 	*hr_task;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	unsigned int		hr_blocks;
209*4882a593Smuzhiyun 	unsigned long long	hr_start_block;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	unsigned int		hr_block_bits;
212*4882a593Smuzhiyun 	unsigned int		hr_block_bytes;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	unsigned int		hr_slots_per_page;
215*4882a593Smuzhiyun 	unsigned int		hr_num_pages;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	struct page             **hr_slot_data;
218*4882a593Smuzhiyun 	struct block_device	*hr_bdev;
219*4882a593Smuzhiyun 	struct o2hb_disk_slot	*hr_slots;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* live node map of this region */
222*4882a593Smuzhiyun 	unsigned long		hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
223*4882a593Smuzhiyun 	unsigned int		hr_region_num;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	struct dentry		*hr_debug_dir;
226*4882a593Smuzhiyun 	struct o2hb_debug_buf	*hr_db_livenodes;
227*4882a593Smuzhiyun 	struct o2hb_debug_buf	*hr_db_regnum;
228*4882a593Smuzhiyun 	struct o2hb_debug_buf	*hr_db_elapsed_time;
229*4882a593Smuzhiyun 	struct o2hb_debug_buf	*hr_db_pinned;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* let the person setting up hb wait for it to return until it
232*4882a593Smuzhiyun 	 * has reached a 'steady' state.  This will be fixed when we have
233*4882a593Smuzhiyun 	 * a more complete api that doesn't lead to this sort of fragility. */
234*4882a593Smuzhiyun 	atomic_t		hr_steady_iterations;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/* terminate o2hb thread if it does not reach steady state
237*4882a593Smuzhiyun 	 * (hr_steady_iterations == 0) within hr_unsteady_iterations */
238*4882a593Smuzhiyun 	atomic_t		hr_unsteady_iterations;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	char			hr_dev_name[BDEVNAME_SIZE];
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	unsigned int		hr_timeout_ms;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* randomized as the region goes up and down so that a node
245*4882a593Smuzhiyun 	 * recognizes a node going up and down in one iteration */
246*4882a593Smuzhiyun 	u64			hr_generation;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	struct delayed_work	hr_write_timeout_work;
249*4882a593Smuzhiyun 	unsigned long		hr_last_timeout_start;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* negotiate timer, used to negotiate extending hb timeout. */
252*4882a593Smuzhiyun 	struct delayed_work	hr_nego_timeout_work;
253*4882a593Smuzhiyun 	unsigned long		hr_nego_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* Used during o2hb_check_slot to hold a copy of the block
256*4882a593Smuzhiyun 	 * being checked because we temporarily have to zero out the
257*4882a593Smuzhiyun 	 * crc field. */
258*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *hr_tmp_block;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* Message key for negotiate timeout message. */
261*4882a593Smuzhiyun 	unsigned int		hr_key;
262*4882a593Smuzhiyun 	struct list_head	hr_handler_list;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/* last hb status, 0 for success, other value for error. */
265*4882a593Smuzhiyun 	int			hr_last_hb_status;
266*4882a593Smuzhiyun };
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun struct o2hb_bio_wait_ctxt {
269*4882a593Smuzhiyun 	atomic_t          wc_num_reqs;
270*4882a593Smuzhiyun 	struct completion wc_io_complete;
271*4882a593Smuzhiyun 	int               wc_error;
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun #define O2HB_NEGO_TIMEOUT_MS (O2HB_MAX_WRITE_TIMEOUT_MS/2)
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun enum {
277*4882a593Smuzhiyun 	O2HB_NEGO_TIMEOUT_MSG = 1,
278*4882a593Smuzhiyun 	O2HB_NEGO_APPROVE_MSG = 2,
279*4882a593Smuzhiyun };
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun struct o2hb_nego_msg {
282*4882a593Smuzhiyun 	u8 node_num;
283*4882a593Smuzhiyun };
284*4882a593Smuzhiyun 
o2hb_write_timeout(struct work_struct * work)285*4882a593Smuzhiyun static void o2hb_write_timeout(struct work_struct *work)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	int failed, quorum;
288*4882a593Smuzhiyun 	struct o2hb_region *reg =
289*4882a593Smuzhiyun 		container_of(work, struct o2hb_region,
290*4882a593Smuzhiyun 			     hr_write_timeout_work.work);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
293*4882a593Smuzhiyun 	     "milliseconds\n", reg->hr_dev_name,
294*4882a593Smuzhiyun 	     jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active()) {
297*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
298*4882a593Smuzhiyun 		if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
299*4882a593Smuzhiyun 			set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
300*4882a593Smuzhiyun 		failed = bitmap_weight(o2hb_failed_region_bitmap,
301*4882a593Smuzhiyun 					O2NM_MAX_REGIONS);
302*4882a593Smuzhiyun 		quorum = bitmap_weight(o2hb_quorum_region_bitmap,
303*4882a593Smuzhiyun 					O2NM_MAX_REGIONS);
304*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
307*4882a593Smuzhiyun 		     quorum, failed);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 		/*
310*4882a593Smuzhiyun 		 * Fence if the number of failed regions >= half the number
311*4882a593Smuzhiyun 		 * of  quorum regions
312*4882a593Smuzhiyun 		 */
313*4882a593Smuzhiyun 		if ((failed << 1) < quorum)
314*4882a593Smuzhiyun 			return;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	o2quo_disk_timeout();
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
o2hb_arm_timeout(struct o2hb_region * reg)320*4882a593Smuzhiyun static void o2hb_arm_timeout(struct o2hb_region *reg)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	/* Arm writeout only after thread reaches steady state */
323*4882a593Smuzhiyun 	if (atomic_read(&reg->hr_steady_iterations) != 0)
324*4882a593Smuzhiyun 		return;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
327*4882a593Smuzhiyun 	     O2HB_MAX_WRITE_TIMEOUT_MS);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active()) {
330*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
331*4882a593Smuzhiyun 		clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
332*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 	cancel_delayed_work(&reg->hr_write_timeout_work);
335*4882a593Smuzhiyun 	schedule_delayed_work(&reg->hr_write_timeout_work,
336*4882a593Smuzhiyun 			      msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	cancel_delayed_work(&reg->hr_nego_timeout_work);
339*4882a593Smuzhiyun 	/* negotiate timeout must be less than write timeout. */
340*4882a593Smuzhiyun 	schedule_delayed_work(&reg->hr_nego_timeout_work,
341*4882a593Smuzhiyun 			      msecs_to_jiffies(O2HB_NEGO_TIMEOUT_MS));
342*4882a593Smuzhiyun 	memset(reg->hr_nego_node_bitmap, 0, sizeof(reg->hr_nego_node_bitmap));
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
o2hb_disarm_timeout(struct o2hb_region * reg)345*4882a593Smuzhiyun static void o2hb_disarm_timeout(struct o2hb_region *reg)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	cancel_delayed_work_sync(&reg->hr_write_timeout_work);
348*4882a593Smuzhiyun 	cancel_delayed_work_sync(&reg->hr_nego_timeout_work);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
o2hb_send_nego_msg(int key,int type,u8 target)351*4882a593Smuzhiyun static int o2hb_send_nego_msg(int key, int type, u8 target)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	struct o2hb_nego_msg msg;
354*4882a593Smuzhiyun 	int status, ret;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	msg.node_num = o2nm_this_node();
357*4882a593Smuzhiyun again:
358*4882a593Smuzhiyun 	ret = o2net_send_message(type, key, &msg, sizeof(msg),
359*4882a593Smuzhiyun 			target, &status);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (ret == -EAGAIN || ret == -ENOMEM) {
362*4882a593Smuzhiyun 		msleep(100);
363*4882a593Smuzhiyun 		goto again;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return ret;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
o2hb_nego_timeout(struct work_struct * work)369*4882a593Smuzhiyun static void o2hb_nego_timeout(struct work_struct *work)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
372*4882a593Smuzhiyun 	int master_node, i, ret;
373*4882a593Smuzhiyun 	struct o2hb_region *reg;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	reg = container_of(work, struct o2hb_region, hr_nego_timeout_work.work);
376*4882a593Smuzhiyun 	/* don't negotiate timeout if last hb failed since it is very
377*4882a593Smuzhiyun 	 * possible io failed. Should let write timeout fence self.
378*4882a593Smuzhiyun 	 */
379*4882a593Smuzhiyun 	if (reg->hr_last_hb_status)
380*4882a593Smuzhiyun 		return;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
383*4882a593Smuzhiyun 	/* lowest node as master node to make negotiate decision. */
384*4882a593Smuzhiyun 	master_node = find_next_bit(live_node_bitmap, O2NM_MAX_NODES, 0);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (master_node == o2nm_this_node()) {
387*4882a593Smuzhiyun 		if (!test_bit(master_node, reg->hr_nego_node_bitmap)) {
388*4882a593Smuzhiyun 			printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%s).\n",
389*4882a593Smuzhiyun 				o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000,
390*4882a593Smuzhiyun 				config_item_name(&reg->hr_item), reg->hr_dev_name);
391*4882a593Smuzhiyun 			set_bit(master_node, reg->hr_nego_node_bitmap);
392*4882a593Smuzhiyun 		}
393*4882a593Smuzhiyun 		if (memcmp(reg->hr_nego_node_bitmap, live_node_bitmap,
394*4882a593Smuzhiyun 				sizeof(reg->hr_nego_node_bitmap))) {
395*4882a593Smuzhiyun 			/* check negotiate bitmap every second to do timeout
396*4882a593Smuzhiyun 			 * approve decision.
397*4882a593Smuzhiyun 			 */
398*4882a593Smuzhiyun 			schedule_delayed_work(&reg->hr_nego_timeout_work,
399*4882a593Smuzhiyun 				msecs_to_jiffies(1000));
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 			return;
402*4882a593Smuzhiyun 		}
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 		printk(KERN_NOTICE "o2hb: all nodes hb write hung, maybe region %s (%s) is down.\n",
405*4882a593Smuzhiyun 			config_item_name(&reg->hr_item), reg->hr_dev_name);
406*4882a593Smuzhiyun 		/* approve negotiate timeout request. */
407*4882a593Smuzhiyun 		o2hb_arm_timeout(reg);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 		i = -1;
410*4882a593Smuzhiyun 		while ((i = find_next_bit(live_node_bitmap,
411*4882a593Smuzhiyun 				O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
412*4882a593Smuzhiyun 			if (i == master_node)
413*4882a593Smuzhiyun 				continue;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 			mlog(ML_HEARTBEAT, "send NEGO_APPROVE msg to node %d\n", i);
416*4882a593Smuzhiyun 			ret = o2hb_send_nego_msg(reg->hr_key,
417*4882a593Smuzhiyun 					O2HB_NEGO_APPROVE_MSG, i);
418*4882a593Smuzhiyun 			if (ret)
419*4882a593Smuzhiyun 				mlog(ML_ERROR, "send NEGO_APPROVE msg to node %d fail %d\n",
420*4882a593Smuzhiyun 					i, ret);
421*4882a593Smuzhiyun 		}
422*4882a593Smuzhiyun 	} else {
423*4882a593Smuzhiyun 		/* negotiate timeout with master node. */
424*4882a593Smuzhiyun 		printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%s), negotiate timeout with node %d.\n",
425*4882a593Smuzhiyun 			o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000, config_item_name(&reg->hr_item),
426*4882a593Smuzhiyun 			reg->hr_dev_name, master_node);
427*4882a593Smuzhiyun 		ret = o2hb_send_nego_msg(reg->hr_key, O2HB_NEGO_TIMEOUT_MSG,
428*4882a593Smuzhiyun 				master_node);
429*4882a593Smuzhiyun 		if (ret)
430*4882a593Smuzhiyun 			mlog(ML_ERROR, "send NEGO_TIMEOUT msg to node %d fail %d\n",
431*4882a593Smuzhiyun 				master_node, ret);
432*4882a593Smuzhiyun 	}
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun 
o2hb_nego_timeout_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)435*4882a593Smuzhiyun static int o2hb_nego_timeout_handler(struct o2net_msg *msg, u32 len, void *data,
436*4882a593Smuzhiyun 				void **ret_data)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	struct o2hb_region *reg = data;
439*4882a593Smuzhiyun 	struct o2hb_nego_msg *nego_msg;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	nego_msg = (struct o2hb_nego_msg *)msg->buf;
442*4882a593Smuzhiyun 	printk(KERN_NOTICE "o2hb: receive negotiate timeout message from node %d on region %s (%s).\n",
443*4882a593Smuzhiyun 		nego_msg->node_num, config_item_name(&reg->hr_item), reg->hr_dev_name);
444*4882a593Smuzhiyun 	if (nego_msg->node_num < O2NM_MAX_NODES)
445*4882a593Smuzhiyun 		set_bit(nego_msg->node_num, reg->hr_nego_node_bitmap);
446*4882a593Smuzhiyun 	else
447*4882a593Smuzhiyun 		mlog(ML_ERROR, "got nego timeout message from bad node.\n");
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	return 0;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun 
o2hb_nego_approve_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)452*4882a593Smuzhiyun static int o2hb_nego_approve_handler(struct o2net_msg *msg, u32 len, void *data,
453*4882a593Smuzhiyun 				void **ret_data)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun 	struct o2hb_region *reg = data;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	printk(KERN_NOTICE "o2hb: negotiate timeout approved by master node on region %s (%s).\n",
458*4882a593Smuzhiyun 		config_item_name(&reg->hr_item), reg->hr_dev_name);
459*4882a593Smuzhiyun 	o2hb_arm_timeout(reg);
460*4882a593Smuzhiyun 	return 0;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt * wc)463*4882a593Smuzhiyun static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	atomic_set(&wc->wc_num_reqs, 1);
466*4882a593Smuzhiyun 	init_completion(&wc->wc_io_complete);
467*4882a593Smuzhiyun 	wc->wc_error = 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun /* Used in error paths too */
o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt * wc,unsigned int num)471*4882a593Smuzhiyun static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
472*4882a593Smuzhiyun 				     unsigned int num)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	/* sadly atomic_sub_and_test() isn't available on all platforms.  The
475*4882a593Smuzhiyun 	 * good news is that the fast path only completes one at a time */
476*4882a593Smuzhiyun 	while(num--) {
477*4882a593Smuzhiyun 		if (atomic_dec_and_test(&wc->wc_num_reqs)) {
478*4882a593Smuzhiyun 			BUG_ON(num > 0);
479*4882a593Smuzhiyun 			complete(&wc->wc_io_complete);
480*4882a593Smuzhiyun 		}
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
o2hb_wait_on_io(struct o2hb_bio_wait_ctxt * wc)484*4882a593Smuzhiyun static void o2hb_wait_on_io(struct o2hb_bio_wait_ctxt *wc)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun 	o2hb_bio_wait_dec(wc, 1);
487*4882a593Smuzhiyun 	wait_for_completion(&wc->wc_io_complete);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
o2hb_bio_end_io(struct bio * bio)490*4882a593Smuzhiyun static void o2hb_bio_end_io(struct bio *bio)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	if (bio->bi_status) {
495*4882a593Smuzhiyun 		mlog(ML_ERROR, "IO Error %d\n", bio->bi_status);
496*4882a593Smuzhiyun 		wc->wc_error = blk_status_to_errno(bio->bi_status);
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	o2hb_bio_wait_dec(wc, 1);
500*4882a593Smuzhiyun 	bio_put(bio);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /* Setup a Bio to cover I/O against num_slots slots starting at
504*4882a593Smuzhiyun  * start_slot. */
o2hb_setup_one_bio(struct o2hb_region * reg,struct o2hb_bio_wait_ctxt * wc,unsigned int * current_slot,unsigned int max_slots,int op,int op_flags)505*4882a593Smuzhiyun static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
506*4882a593Smuzhiyun 				      struct o2hb_bio_wait_ctxt *wc,
507*4882a593Smuzhiyun 				      unsigned int *current_slot,
508*4882a593Smuzhiyun 				      unsigned int max_slots, int op,
509*4882a593Smuzhiyun 				      int op_flags)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	int len, current_page;
512*4882a593Smuzhiyun 	unsigned int vec_len, vec_start;
513*4882a593Smuzhiyun 	unsigned int bits = reg->hr_block_bits;
514*4882a593Smuzhiyun 	unsigned int spp = reg->hr_slots_per_page;
515*4882a593Smuzhiyun 	unsigned int cs = *current_slot;
516*4882a593Smuzhiyun 	struct bio *bio;
517*4882a593Smuzhiyun 	struct page *page;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/* Testing has shown this allocation to take long enough under
520*4882a593Smuzhiyun 	 * GFP_KERNEL that the local node can get fenced. It would be
521*4882a593Smuzhiyun 	 * nicest if we could pre-allocate these bios and avoid this
522*4882a593Smuzhiyun 	 * all together. */
523*4882a593Smuzhiyun 	bio = bio_alloc(GFP_ATOMIC, 16);
524*4882a593Smuzhiyun 	if (!bio) {
525*4882a593Smuzhiyun 		mlog(ML_ERROR, "Could not alloc slots BIO!\n");
526*4882a593Smuzhiyun 		bio = ERR_PTR(-ENOMEM);
527*4882a593Smuzhiyun 		goto bail;
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* Must put everything in 512 byte sectors for the bio... */
531*4882a593Smuzhiyun 	bio->bi_iter.bi_sector = (reg->hr_start_block + cs) << (bits - 9);
532*4882a593Smuzhiyun 	bio_set_dev(bio, reg->hr_bdev);
533*4882a593Smuzhiyun 	bio->bi_private = wc;
534*4882a593Smuzhiyun 	bio->bi_end_io = o2hb_bio_end_io;
535*4882a593Smuzhiyun 	bio_set_op_attrs(bio, op, op_flags);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	vec_start = (cs << bits) % PAGE_SIZE;
538*4882a593Smuzhiyun 	while(cs < max_slots) {
539*4882a593Smuzhiyun 		current_page = cs / spp;
540*4882a593Smuzhiyun 		page = reg->hr_slot_data[current_page];
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 		vec_len = min(PAGE_SIZE - vec_start,
543*4882a593Smuzhiyun 			      (max_slots-cs) * (PAGE_SIZE/spp) );
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
546*4882a593Smuzhiyun 		     current_page, vec_len, vec_start);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		len = bio_add_page(bio, page, vec_len, vec_start);
549*4882a593Smuzhiyun 		if (len != vec_len) break;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		cs += vec_len / (PAGE_SIZE/spp);
552*4882a593Smuzhiyun 		vec_start = 0;
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun bail:
556*4882a593Smuzhiyun 	*current_slot = cs;
557*4882a593Smuzhiyun 	return bio;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
o2hb_read_slots(struct o2hb_region * reg,unsigned int begin_slot,unsigned int max_slots)560*4882a593Smuzhiyun static int o2hb_read_slots(struct o2hb_region *reg,
561*4882a593Smuzhiyun 			   unsigned int begin_slot,
562*4882a593Smuzhiyun 			   unsigned int max_slots)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	unsigned int current_slot = begin_slot;
565*4882a593Smuzhiyun 	int status;
566*4882a593Smuzhiyun 	struct o2hb_bio_wait_ctxt wc;
567*4882a593Smuzhiyun 	struct bio *bio;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	o2hb_bio_wait_init(&wc);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	while(current_slot < max_slots) {
572*4882a593Smuzhiyun 		bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots,
573*4882a593Smuzhiyun 					 REQ_OP_READ, 0);
574*4882a593Smuzhiyun 		if (IS_ERR(bio)) {
575*4882a593Smuzhiyun 			status = PTR_ERR(bio);
576*4882a593Smuzhiyun 			mlog_errno(status);
577*4882a593Smuzhiyun 			goto bail_and_wait;
578*4882a593Smuzhiyun 		}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 		atomic_inc(&wc.wc_num_reqs);
581*4882a593Smuzhiyun 		submit_bio(bio);
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	status = 0;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun bail_and_wait:
587*4882a593Smuzhiyun 	o2hb_wait_on_io(&wc);
588*4882a593Smuzhiyun 	if (wc.wc_error && !status)
589*4882a593Smuzhiyun 		status = wc.wc_error;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	return status;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
o2hb_issue_node_write(struct o2hb_region * reg,struct o2hb_bio_wait_ctxt * write_wc)594*4882a593Smuzhiyun static int o2hb_issue_node_write(struct o2hb_region *reg,
595*4882a593Smuzhiyun 				 struct o2hb_bio_wait_ctxt *write_wc)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun 	int status;
598*4882a593Smuzhiyun 	unsigned int slot;
599*4882a593Smuzhiyun 	struct bio *bio;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	o2hb_bio_wait_init(write_wc);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	slot = o2nm_this_node();
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1, REQ_OP_WRITE,
606*4882a593Smuzhiyun 				 REQ_SYNC);
607*4882a593Smuzhiyun 	if (IS_ERR(bio)) {
608*4882a593Smuzhiyun 		status = PTR_ERR(bio);
609*4882a593Smuzhiyun 		mlog_errno(status);
610*4882a593Smuzhiyun 		goto bail;
611*4882a593Smuzhiyun 	}
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	atomic_inc(&write_wc->wc_num_reqs);
614*4882a593Smuzhiyun 	submit_bio(bio);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	status = 0;
617*4882a593Smuzhiyun bail:
618*4882a593Smuzhiyun 	return status;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
o2hb_compute_block_crc_le(struct o2hb_region * reg,struct o2hb_disk_heartbeat_block * hb_block)621*4882a593Smuzhiyun static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
622*4882a593Smuzhiyun 				     struct o2hb_disk_heartbeat_block *hb_block)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	__le32 old_cksum;
625*4882a593Smuzhiyun 	u32 ret;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	/* We want to compute the block crc with a 0 value in the
628*4882a593Smuzhiyun 	 * hb_cksum field. Save it off here and replace after the
629*4882a593Smuzhiyun 	 * crc. */
630*4882a593Smuzhiyun 	old_cksum = hb_block->hb_cksum;
631*4882a593Smuzhiyun 	hb_block->hb_cksum = 0;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	hb_block->hb_cksum = old_cksum;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	return ret;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
o2hb_dump_slot(struct o2hb_disk_heartbeat_block * hb_block)640*4882a593Smuzhiyun static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
643*4882a593Smuzhiyun 	     "cksum = 0x%x, generation 0x%llx\n",
644*4882a593Smuzhiyun 	     (long long)le64_to_cpu(hb_block->hb_seq),
645*4882a593Smuzhiyun 	     hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
646*4882a593Smuzhiyun 	     (long long)le64_to_cpu(hb_block->hb_generation));
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
o2hb_verify_crc(struct o2hb_region * reg,struct o2hb_disk_heartbeat_block * hb_block)649*4882a593Smuzhiyun static int o2hb_verify_crc(struct o2hb_region *reg,
650*4882a593Smuzhiyun 			   struct o2hb_disk_heartbeat_block *hb_block)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	u32 read, computed;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	read = le32_to_cpu(hb_block->hb_cksum);
655*4882a593Smuzhiyun 	computed = o2hb_compute_block_crc_le(reg, hb_block);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	return read == computed;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun /*
661*4882a593Smuzhiyun  * Compare the slot data with what we wrote in the last iteration.
662*4882a593Smuzhiyun  * If the match fails, print an appropriate error message. This is to
663*4882a593Smuzhiyun  * detect errors like... another node hearting on the same slot,
664*4882a593Smuzhiyun  * flaky device that is losing writes, etc.
665*4882a593Smuzhiyun  * Returns 1 if check succeeds, 0 otherwise.
666*4882a593Smuzhiyun  */
o2hb_check_own_slot(struct o2hb_region * reg)667*4882a593Smuzhiyun static int o2hb_check_own_slot(struct o2hb_region *reg)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	struct o2hb_disk_slot *slot;
670*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *hb_block;
671*4882a593Smuzhiyun 	char *errstr;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	slot = &reg->hr_slots[o2nm_this_node()];
674*4882a593Smuzhiyun 	/* Don't check on our 1st timestamp */
675*4882a593Smuzhiyun 	if (!slot->ds_last_time)
676*4882a593Smuzhiyun 		return 0;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	hb_block = slot->ds_raw_block;
679*4882a593Smuzhiyun 	if (le64_to_cpu(hb_block->hb_seq) == slot->ds_last_time &&
680*4882a593Smuzhiyun 	    le64_to_cpu(hb_block->hb_generation) == slot->ds_last_generation &&
681*4882a593Smuzhiyun 	    hb_block->hb_node == slot->ds_node_num)
682*4882a593Smuzhiyun 		return 1;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun #define ERRSTR1		"Another node is heartbeating on device"
685*4882a593Smuzhiyun #define ERRSTR2		"Heartbeat generation mismatch on device"
686*4882a593Smuzhiyun #define ERRSTR3		"Heartbeat sequence mismatch on device"
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	if (hb_block->hb_node != slot->ds_node_num)
689*4882a593Smuzhiyun 		errstr = ERRSTR1;
690*4882a593Smuzhiyun 	else if (le64_to_cpu(hb_block->hb_generation) !=
691*4882a593Smuzhiyun 		 slot->ds_last_generation)
692*4882a593Smuzhiyun 		errstr = ERRSTR2;
693*4882a593Smuzhiyun 	else
694*4882a593Smuzhiyun 		errstr = ERRSTR3;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	mlog(ML_ERROR, "%s (%s): expected(%u:0x%llx, 0x%llx), "
697*4882a593Smuzhiyun 	     "ondisk(%u:0x%llx, 0x%llx)\n", errstr, reg->hr_dev_name,
698*4882a593Smuzhiyun 	     slot->ds_node_num, (unsigned long long)slot->ds_last_generation,
699*4882a593Smuzhiyun 	     (unsigned long long)slot->ds_last_time, hb_block->hb_node,
700*4882a593Smuzhiyun 	     (unsigned long long)le64_to_cpu(hb_block->hb_generation),
701*4882a593Smuzhiyun 	     (unsigned long long)le64_to_cpu(hb_block->hb_seq));
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	return 0;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun 
o2hb_prepare_block(struct o2hb_region * reg,u64 generation)706*4882a593Smuzhiyun static inline void o2hb_prepare_block(struct o2hb_region *reg,
707*4882a593Smuzhiyun 				      u64 generation)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	int node_num;
710*4882a593Smuzhiyun 	u64 cputime;
711*4882a593Smuzhiyun 	struct o2hb_disk_slot *slot;
712*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *hb_block;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	node_num = o2nm_this_node();
715*4882a593Smuzhiyun 	slot = &reg->hr_slots[node_num];
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
718*4882a593Smuzhiyun 	memset(hb_block, 0, reg->hr_block_bytes);
719*4882a593Smuzhiyun 	/* TODO: time stuff */
720*4882a593Smuzhiyun 	cputime = ktime_get_real_seconds();
721*4882a593Smuzhiyun 	if (!cputime)
722*4882a593Smuzhiyun 		cputime = 1;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	hb_block->hb_seq = cpu_to_le64(cputime);
725*4882a593Smuzhiyun 	hb_block->hb_node = node_num;
726*4882a593Smuzhiyun 	hb_block->hb_generation = cpu_to_le64(generation);
727*4882a593Smuzhiyun 	hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	/* This step must always happen last! */
730*4882a593Smuzhiyun 	hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
731*4882a593Smuzhiyun 								   hb_block));
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
734*4882a593Smuzhiyun 	     (long long)generation,
735*4882a593Smuzhiyun 	     le32_to_cpu(hb_block->hb_cksum));
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
o2hb_fire_callbacks(struct o2hb_callback * hbcall,struct o2nm_node * node,int idx)738*4882a593Smuzhiyun static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
739*4882a593Smuzhiyun 				struct o2nm_node *node,
740*4882a593Smuzhiyun 				int idx)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	struct o2hb_callback_func *f;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	list_for_each_entry(f, &hbcall->list, hc_item) {
745*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
746*4882a593Smuzhiyun 		(f->hc_func)(node, idx, f->hc_data);
747*4882a593Smuzhiyun 	}
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun /* Will run the list in order until we process the passed event */
o2hb_run_event_list(struct o2hb_node_event * queued_event)751*4882a593Smuzhiyun static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	struct o2hb_callback *hbcall;
754*4882a593Smuzhiyun 	struct o2hb_node_event *event;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	/* Holding callback sem assures we don't alter the callback
757*4882a593Smuzhiyun 	 * lists when doing this, and serializes ourselves with other
758*4882a593Smuzhiyun 	 * processes wanting callbacks. */
759*4882a593Smuzhiyun 	down_write(&o2hb_callback_sem);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
762*4882a593Smuzhiyun 	while (!list_empty(&o2hb_node_events)
763*4882a593Smuzhiyun 	       && !list_empty(&queued_event->hn_item)) {
764*4882a593Smuzhiyun 		event = list_entry(o2hb_node_events.next,
765*4882a593Smuzhiyun 				   struct o2hb_node_event,
766*4882a593Smuzhiyun 				   hn_item);
767*4882a593Smuzhiyun 		list_del_init(&event->hn_item);
768*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Node %s event for %d\n",
771*4882a593Smuzhiyun 		     event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
772*4882a593Smuzhiyun 		     event->hn_node_num);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		hbcall = hbcall_from_type(event->hn_event_type);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 		/* We should *never* have gotten on to the list with a
777*4882a593Smuzhiyun 		 * bad type... This isn't something that we should try
778*4882a593Smuzhiyun 		 * to recover from. */
779*4882a593Smuzhiyun 		BUG_ON(IS_ERR(hbcall));
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	up_write(&o2hb_callback_sem);
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
o2hb_queue_node_event(struct o2hb_node_event * event,enum o2hb_callback_type type,struct o2nm_node * node,int node_num)790*4882a593Smuzhiyun static void o2hb_queue_node_event(struct o2hb_node_event *event,
791*4882a593Smuzhiyun 				  enum o2hb_callback_type type,
792*4882a593Smuzhiyun 				  struct o2nm_node *node,
793*4882a593Smuzhiyun 				  int node_num)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	assert_spin_locked(&o2hb_live_lock);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	event->hn_event_type = type;
800*4882a593Smuzhiyun 	event->hn_node = node;
801*4882a593Smuzhiyun 	event->hn_node_num = node_num;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
804*4882a593Smuzhiyun 	     type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	list_add_tail(&event->hn_item, &o2hb_node_events);
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun 
o2hb_shutdown_slot(struct o2hb_disk_slot * slot)809*4882a593Smuzhiyun static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	struct o2hb_node_event event =
812*4882a593Smuzhiyun 		{ .hn_item = LIST_HEAD_INIT(event.hn_item), };
813*4882a593Smuzhiyun 	struct o2nm_node *node;
814*4882a593Smuzhiyun 	int queued = 0;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	node = o2nm_get_node_by_num(slot->ds_node_num);
817*4882a593Smuzhiyun 	if (!node)
818*4882a593Smuzhiyun 		return;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
821*4882a593Smuzhiyun 	if (!list_empty(&slot->ds_live_item)) {
822*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
823*4882a593Smuzhiyun 		     slot->ds_node_num);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 		list_del_init(&slot->ds_live_item);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 		if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
828*4882a593Smuzhiyun 			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
831*4882a593Smuzhiyun 					      slot->ds_node_num);
832*4882a593Smuzhiyun 			queued = 1;
833*4882a593Smuzhiyun 		}
834*4882a593Smuzhiyun 	}
835*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	if (queued)
838*4882a593Smuzhiyun 		o2hb_run_event_list(&event);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	o2nm_node_put(node);
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
o2hb_set_quorum_device(struct o2hb_region * reg)843*4882a593Smuzhiyun static void o2hb_set_quorum_device(struct o2hb_region *reg)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	if (!o2hb_global_heartbeat_active())
846*4882a593Smuzhiyun 		return;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	/* Prevent race with o2hb_heartbeat_group_drop_item() */
849*4882a593Smuzhiyun 	if (kthread_should_stop())
850*4882a593Smuzhiyun 		return;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	/* Tag region as quorum only after thread reaches steady state */
853*4882a593Smuzhiyun 	if (atomic_read(&reg->hr_steady_iterations) != 0)
854*4882a593Smuzhiyun 		return;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
859*4882a593Smuzhiyun 		goto unlock;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	/*
862*4882a593Smuzhiyun 	 * A region can be added to the quorum only when it sees all
863*4882a593Smuzhiyun 	 * live nodes heartbeat on it. In other words, the region has been
864*4882a593Smuzhiyun 	 * added to all nodes.
865*4882a593Smuzhiyun 	 */
866*4882a593Smuzhiyun 	if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
867*4882a593Smuzhiyun 		   sizeof(o2hb_live_node_bitmap)))
868*4882a593Smuzhiyun 		goto unlock;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	printk(KERN_NOTICE "o2hb: Region %s (%s) is now a quorum device\n",
871*4882a593Smuzhiyun 	       config_item_name(&reg->hr_item), reg->hr_dev_name);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	/*
876*4882a593Smuzhiyun 	 * If global heartbeat active, unpin all regions if the
877*4882a593Smuzhiyun 	 * region count > CUT_OFF
878*4882a593Smuzhiyun 	 */
879*4882a593Smuzhiyun 	if (bitmap_weight(o2hb_quorum_region_bitmap,
880*4882a593Smuzhiyun 			   O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
881*4882a593Smuzhiyun 		o2hb_region_unpin(NULL);
882*4882a593Smuzhiyun unlock:
883*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun 
o2hb_check_slot(struct o2hb_region * reg,struct o2hb_disk_slot * slot)886*4882a593Smuzhiyun static int o2hb_check_slot(struct o2hb_region *reg,
887*4882a593Smuzhiyun 			   struct o2hb_disk_slot *slot)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun 	int changed = 0, gen_changed = 0;
890*4882a593Smuzhiyun 	struct o2hb_node_event event =
891*4882a593Smuzhiyun 		{ .hn_item = LIST_HEAD_INIT(event.hn_item), };
892*4882a593Smuzhiyun 	struct o2nm_node *node;
893*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
894*4882a593Smuzhiyun 	u64 cputime;
895*4882a593Smuzhiyun 	unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
896*4882a593Smuzhiyun 	unsigned int slot_dead_ms;
897*4882a593Smuzhiyun 	int tmp;
898*4882a593Smuzhiyun 	int queued = 0;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	/*
903*4882a593Smuzhiyun 	 * If a node is no longer configured but is still in the livemap, we
904*4882a593Smuzhiyun 	 * may need to clear that bit from the livemap.
905*4882a593Smuzhiyun 	 */
906*4882a593Smuzhiyun 	node = o2nm_get_node_by_num(slot->ds_node_num);
907*4882a593Smuzhiyun 	if (!node) {
908*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
909*4882a593Smuzhiyun 		tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
910*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
911*4882a593Smuzhiyun 		if (!tmp)
912*4882a593Smuzhiyun 			return 0;
913*4882a593Smuzhiyun 	}
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	if (!o2hb_verify_crc(reg, hb_block)) {
916*4882a593Smuzhiyun 		/* all paths from here will drop o2hb_live_lock for
917*4882a593Smuzhiyun 		 * us. */
918*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 		/* Don't print an error on the console in this case -
921*4882a593Smuzhiyun 		 * a freshly formatted heartbeat area will not have a
922*4882a593Smuzhiyun 		 * crc set on it. */
923*4882a593Smuzhiyun 		if (list_empty(&slot->ds_live_item))
924*4882a593Smuzhiyun 			goto out;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 		/* The node is live but pushed out a bad crc. We
927*4882a593Smuzhiyun 		 * consider it a transient miss but don't populate any
928*4882a593Smuzhiyun 		 * other values as they may be junk. */
929*4882a593Smuzhiyun 		mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
930*4882a593Smuzhiyun 		     slot->ds_node_num, reg->hr_dev_name);
931*4882a593Smuzhiyun 		o2hb_dump_slot(hb_block);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 		slot->ds_equal_samples++;
934*4882a593Smuzhiyun 		goto fire_callbacks;
935*4882a593Smuzhiyun 	}
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	/* we don't care if these wrap.. the state transitions below
938*4882a593Smuzhiyun 	 * clear at the right places */
939*4882a593Smuzhiyun 	cputime = le64_to_cpu(hb_block->hb_seq);
940*4882a593Smuzhiyun 	if (slot->ds_last_time != cputime)
941*4882a593Smuzhiyun 		slot->ds_changed_samples++;
942*4882a593Smuzhiyun 	else
943*4882a593Smuzhiyun 		slot->ds_equal_samples++;
944*4882a593Smuzhiyun 	slot->ds_last_time = cputime;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	/* The node changed heartbeat generations. We assume this to
947*4882a593Smuzhiyun 	 * mean it dropped off but came back before we timed out. We
948*4882a593Smuzhiyun 	 * want to consider it down for the time being but don't want
949*4882a593Smuzhiyun 	 * to lose any changed_samples state we might build up to
950*4882a593Smuzhiyun 	 * considering it live again. */
951*4882a593Smuzhiyun 	if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
952*4882a593Smuzhiyun 		gen_changed = 1;
953*4882a593Smuzhiyun 		slot->ds_equal_samples = 0;
954*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
955*4882a593Smuzhiyun 		     "to 0x%llx)\n", slot->ds_node_num,
956*4882a593Smuzhiyun 		     (long long)slot->ds_last_generation,
957*4882a593Smuzhiyun 		     (long long)le64_to_cpu(hb_block->hb_generation));
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
963*4882a593Smuzhiyun 	     "seq %llu last %llu changed %u equal %u\n",
964*4882a593Smuzhiyun 	     slot->ds_node_num, (long long)slot->ds_last_generation,
965*4882a593Smuzhiyun 	     le32_to_cpu(hb_block->hb_cksum),
966*4882a593Smuzhiyun 	     (unsigned long long)le64_to_cpu(hb_block->hb_seq),
967*4882a593Smuzhiyun 	     (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
968*4882a593Smuzhiyun 	     slot->ds_equal_samples);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun fire_callbacks:
973*4882a593Smuzhiyun 	/* dead nodes only come to life after some number of
974*4882a593Smuzhiyun 	 * changes at any time during their dead time */
975*4882a593Smuzhiyun 	if (list_empty(&slot->ds_live_item) &&
976*4882a593Smuzhiyun 	    slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
977*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
978*4882a593Smuzhiyun 		     slot->ds_node_num, (long long)slot->ds_last_generation);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 		set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 		/* first on the list generates a callback */
983*4882a593Smuzhiyun 		if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
984*4882a593Smuzhiyun 			mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
985*4882a593Smuzhiyun 			     "bitmap\n", slot->ds_node_num);
986*4882a593Smuzhiyun 			set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 			o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
989*4882a593Smuzhiyun 					      slot->ds_node_num);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 			changed = 1;
992*4882a593Smuzhiyun 			queued = 1;
993*4882a593Smuzhiyun 		}
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 		list_add_tail(&slot->ds_live_item,
996*4882a593Smuzhiyun 			      &o2hb_live_slots[slot->ds_node_num]);
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 		slot->ds_equal_samples = 0;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 		/* We want to be sure that all nodes agree on the
1001*4882a593Smuzhiyun 		 * number of milliseconds before a node will be
1002*4882a593Smuzhiyun 		 * considered dead. The self-fencing timeout is
1003*4882a593Smuzhiyun 		 * computed from this value, and a discrepancy might
1004*4882a593Smuzhiyun 		 * result in heartbeat calling a node dead when it
1005*4882a593Smuzhiyun 		 * hasn't self-fenced yet. */
1006*4882a593Smuzhiyun 		slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
1007*4882a593Smuzhiyun 		if (slot_dead_ms && slot_dead_ms != dead_ms) {
1008*4882a593Smuzhiyun 			/* TODO: Perhaps we can fail the region here. */
1009*4882a593Smuzhiyun 			mlog(ML_ERROR, "Node %d on device %s has a dead count "
1010*4882a593Smuzhiyun 			     "of %u ms, but our count is %u ms.\n"
1011*4882a593Smuzhiyun 			     "Please double check your configuration values "
1012*4882a593Smuzhiyun 			     "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
1013*4882a593Smuzhiyun 			     slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
1014*4882a593Smuzhiyun 			     dead_ms);
1015*4882a593Smuzhiyun 		}
1016*4882a593Smuzhiyun 		goto out;
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	/* if the list is dead, we're done.. */
1020*4882a593Smuzhiyun 	if (list_empty(&slot->ds_live_item))
1021*4882a593Smuzhiyun 		goto out;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	/* live nodes only go dead after enough consequtive missed
1024*4882a593Smuzhiyun 	 * samples..  reset the missed counter whenever we see
1025*4882a593Smuzhiyun 	 * activity */
1026*4882a593Smuzhiyun 	if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
1027*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT, "Node %d left my region\n",
1028*4882a593Smuzhiyun 		     slot->ds_node_num);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 		clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 		/* last off the live_slot generates a callback */
1033*4882a593Smuzhiyun 		list_del_init(&slot->ds_live_item);
1034*4882a593Smuzhiyun 		if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
1035*4882a593Smuzhiyun 			mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
1036*4882a593Smuzhiyun 			     "nodes bitmap\n", slot->ds_node_num);
1037*4882a593Smuzhiyun 			clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 			/* node can be null */
1040*4882a593Smuzhiyun 			o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
1041*4882a593Smuzhiyun 					      node, slot->ds_node_num);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 			changed = 1;
1044*4882a593Smuzhiyun 			queued = 1;
1045*4882a593Smuzhiyun 		}
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 		/* We don't clear this because the node is still
1048*4882a593Smuzhiyun 		 * actually writing new blocks. */
1049*4882a593Smuzhiyun 		if (!gen_changed)
1050*4882a593Smuzhiyun 			slot->ds_changed_samples = 0;
1051*4882a593Smuzhiyun 		goto out;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 	if (slot->ds_changed_samples) {
1054*4882a593Smuzhiyun 		slot->ds_changed_samples = 0;
1055*4882a593Smuzhiyun 		slot->ds_equal_samples = 0;
1056*4882a593Smuzhiyun 	}
1057*4882a593Smuzhiyun out:
1058*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	if (queued)
1061*4882a593Smuzhiyun 		o2hb_run_event_list(&event);
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	if (node)
1064*4882a593Smuzhiyun 		o2nm_node_put(node);
1065*4882a593Smuzhiyun 	return changed;
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun 
o2hb_highest_node(unsigned long * nodes,int numbits)1068*4882a593Smuzhiyun static int o2hb_highest_node(unsigned long *nodes, int numbits)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun 	return find_last_bit(nodes, numbits);
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun 
o2hb_lowest_node(unsigned long * nodes,int numbits)1073*4882a593Smuzhiyun static int o2hb_lowest_node(unsigned long *nodes, int numbits)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	return find_first_bit(nodes, numbits);
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun 
o2hb_do_disk_heartbeat(struct o2hb_region * reg)1078*4882a593Smuzhiyun static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun 	int i, ret, highest_node, lowest_node;
1081*4882a593Smuzhiyun 	int membership_change = 0, own_slot_ok = 0;
1082*4882a593Smuzhiyun 	unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
1083*4882a593Smuzhiyun 	unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
1084*4882a593Smuzhiyun 	struct o2hb_bio_wait_ctxt write_wc;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	ret = o2nm_configured_node_map(configured_nodes,
1087*4882a593Smuzhiyun 				       sizeof(configured_nodes));
1088*4882a593Smuzhiyun 	if (ret) {
1089*4882a593Smuzhiyun 		mlog_errno(ret);
1090*4882a593Smuzhiyun 		goto bail;
1091*4882a593Smuzhiyun 	}
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	/*
1094*4882a593Smuzhiyun 	 * If a node is not configured but is in the livemap, we still need
1095*4882a593Smuzhiyun 	 * to read the slot so as to be able to remove it from the livemap.
1096*4882a593Smuzhiyun 	 */
1097*4882a593Smuzhiyun 	o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
1098*4882a593Smuzhiyun 	i = -1;
1099*4882a593Smuzhiyun 	while ((i = find_next_bit(live_node_bitmap,
1100*4882a593Smuzhiyun 				  O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
1101*4882a593Smuzhiyun 		set_bit(i, configured_nodes);
1102*4882a593Smuzhiyun 	}
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
1105*4882a593Smuzhiyun 	lowest_node = o2hb_lowest_node(configured_nodes, O2NM_MAX_NODES);
1106*4882a593Smuzhiyun 	if (highest_node >= O2NM_MAX_NODES || lowest_node >= O2NM_MAX_NODES) {
1107*4882a593Smuzhiyun 		mlog(ML_NOTICE, "o2hb: No configured nodes found!\n");
1108*4882a593Smuzhiyun 		ret = -EINVAL;
1109*4882a593Smuzhiyun 		goto bail;
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	/* No sense in reading the slots of nodes that don't exist
1113*4882a593Smuzhiyun 	 * yet. Of course, if the node definitions have holes in them
1114*4882a593Smuzhiyun 	 * then we're reading an empty slot anyway... Consider this
1115*4882a593Smuzhiyun 	 * best-effort. */
1116*4882a593Smuzhiyun 	ret = o2hb_read_slots(reg, lowest_node, highest_node + 1);
1117*4882a593Smuzhiyun 	if (ret < 0) {
1118*4882a593Smuzhiyun 		mlog_errno(ret);
1119*4882a593Smuzhiyun 		goto bail;
1120*4882a593Smuzhiyun 	}
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	/* With an up to date view of the slots, we can check that no
1123*4882a593Smuzhiyun 	 * other node has been improperly configured to heartbeat in
1124*4882a593Smuzhiyun 	 * our slot. */
1125*4882a593Smuzhiyun 	own_slot_ok = o2hb_check_own_slot(reg);
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	/* fill in the proper info for our next heartbeat */
1128*4882a593Smuzhiyun 	o2hb_prepare_block(reg, reg->hr_generation);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	ret = o2hb_issue_node_write(reg, &write_wc);
1131*4882a593Smuzhiyun 	if (ret < 0) {
1132*4882a593Smuzhiyun 		mlog_errno(ret);
1133*4882a593Smuzhiyun 		goto bail;
1134*4882a593Smuzhiyun 	}
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	i = -1;
1137*4882a593Smuzhiyun 	while((i = find_next_bit(configured_nodes,
1138*4882a593Smuzhiyun 				 O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
1139*4882a593Smuzhiyun 		membership_change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
1140*4882a593Smuzhiyun 	}
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	/*
1143*4882a593Smuzhiyun 	 * We have to be sure we've advertised ourselves on disk
1144*4882a593Smuzhiyun 	 * before we can go to steady state.  This ensures that
1145*4882a593Smuzhiyun 	 * people we find in our steady state have seen us.
1146*4882a593Smuzhiyun 	 */
1147*4882a593Smuzhiyun 	o2hb_wait_on_io(&write_wc);
1148*4882a593Smuzhiyun 	if (write_wc.wc_error) {
1149*4882a593Smuzhiyun 		/* Do not re-arm the write timeout on I/O error - we
1150*4882a593Smuzhiyun 		 * can't be sure that the new block ever made it to
1151*4882a593Smuzhiyun 		 * disk */
1152*4882a593Smuzhiyun 		mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
1153*4882a593Smuzhiyun 		     write_wc.wc_error, reg->hr_dev_name);
1154*4882a593Smuzhiyun 		ret = write_wc.wc_error;
1155*4882a593Smuzhiyun 		goto bail;
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	/* Skip disarming the timeout if own slot has stale/bad data */
1159*4882a593Smuzhiyun 	if (own_slot_ok) {
1160*4882a593Smuzhiyun 		o2hb_set_quorum_device(reg);
1161*4882a593Smuzhiyun 		o2hb_arm_timeout(reg);
1162*4882a593Smuzhiyun 		reg->hr_last_timeout_start = jiffies;
1163*4882a593Smuzhiyun 	}
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun bail:
1166*4882a593Smuzhiyun 	/* let the person who launched us know when things are steady */
1167*4882a593Smuzhiyun 	if (atomic_read(&reg->hr_steady_iterations) != 0) {
1168*4882a593Smuzhiyun 		if (!ret && own_slot_ok && !membership_change) {
1169*4882a593Smuzhiyun 			if (atomic_dec_and_test(&reg->hr_steady_iterations))
1170*4882a593Smuzhiyun 				wake_up(&o2hb_steady_queue);
1171*4882a593Smuzhiyun 		}
1172*4882a593Smuzhiyun 	}
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	if (atomic_read(&reg->hr_steady_iterations) != 0) {
1175*4882a593Smuzhiyun 		if (atomic_dec_and_test(&reg->hr_unsteady_iterations)) {
1176*4882a593Smuzhiyun 			printk(KERN_NOTICE "o2hb: Unable to stabilize "
1177*4882a593Smuzhiyun 			       "heartbeat on region %s (%s)\n",
1178*4882a593Smuzhiyun 			       config_item_name(&reg->hr_item),
1179*4882a593Smuzhiyun 			       reg->hr_dev_name);
1180*4882a593Smuzhiyun 			atomic_set(&reg->hr_steady_iterations, 0);
1181*4882a593Smuzhiyun 			reg->hr_aborted_start = 1;
1182*4882a593Smuzhiyun 			wake_up(&o2hb_steady_queue);
1183*4882a593Smuzhiyun 			ret = -EIO;
1184*4882a593Smuzhiyun 		}
1185*4882a593Smuzhiyun 	}
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 	return ret;
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun /*
1191*4882a593Smuzhiyun  * we ride the region ref that the region dir holds.  before the region
1192*4882a593Smuzhiyun  * dir is removed and drops it ref it will wait to tear down this
1193*4882a593Smuzhiyun  * thread.
1194*4882a593Smuzhiyun  */
o2hb_thread(void * data)1195*4882a593Smuzhiyun static int o2hb_thread(void *data)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun 	int i, ret;
1198*4882a593Smuzhiyun 	struct o2hb_region *reg = data;
1199*4882a593Smuzhiyun 	struct o2hb_bio_wait_ctxt write_wc;
1200*4882a593Smuzhiyun 	ktime_t before_hb, after_hb;
1201*4882a593Smuzhiyun 	unsigned int elapsed_msec;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	set_user_nice(current, MIN_NICE);
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	/* Pin node */
1208*4882a593Smuzhiyun 	ret = o2nm_depend_this_node();
1209*4882a593Smuzhiyun 	if (ret) {
1210*4882a593Smuzhiyun 		mlog(ML_ERROR, "Node has been deleted, ret = %d\n", ret);
1211*4882a593Smuzhiyun 		reg->hr_node_deleted = 1;
1212*4882a593Smuzhiyun 		wake_up(&o2hb_steady_queue);
1213*4882a593Smuzhiyun 		return 0;
1214*4882a593Smuzhiyun 	}
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	while (!kthread_should_stop() &&
1217*4882a593Smuzhiyun 	       !reg->hr_unclean_stop && !reg->hr_aborted_start) {
1218*4882a593Smuzhiyun 		/* We track the time spent inside
1219*4882a593Smuzhiyun 		 * o2hb_do_disk_heartbeat so that we avoid more than
1220*4882a593Smuzhiyun 		 * hr_timeout_ms between disk writes. On busy systems
1221*4882a593Smuzhiyun 		 * this should result in a heartbeat which is less
1222*4882a593Smuzhiyun 		 * likely to time itself out. */
1223*4882a593Smuzhiyun 		before_hb = ktime_get_real();
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		ret = o2hb_do_disk_heartbeat(reg);
1226*4882a593Smuzhiyun 		reg->hr_last_hb_status = ret;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 		after_hb = ktime_get_real();
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 		elapsed_msec = (unsigned int)
1231*4882a593Smuzhiyun 				ktime_ms_delta(after_hb, before_hb);
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT,
1234*4882a593Smuzhiyun 		     "start = %lld, end = %lld, msec = %u, ret = %d\n",
1235*4882a593Smuzhiyun 		     before_hb, after_hb, elapsed_msec, ret);
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 		if (!kthread_should_stop() &&
1238*4882a593Smuzhiyun 		    elapsed_msec < reg->hr_timeout_ms) {
1239*4882a593Smuzhiyun 			/* the kthread api has blocked signals for us so no
1240*4882a593Smuzhiyun 			 * need to record the return value. */
1241*4882a593Smuzhiyun 			msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
1242*4882a593Smuzhiyun 		}
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	o2hb_disarm_timeout(reg);
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	/* unclean stop is only used in very bad situation */
1248*4882a593Smuzhiyun 	for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
1249*4882a593Smuzhiyun 		o2hb_shutdown_slot(&reg->hr_slots[i]);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* Explicit down notification - avoid forcing the other nodes
1252*4882a593Smuzhiyun 	 * to timeout on this region when we could just as easily
1253*4882a593Smuzhiyun 	 * write a clear generation - thus indicating to them that
1254*4882a593Smuzhiyun 	 * this node has left this region.
1255*4882a593Smuzhiyun 	 */
1256*4882a593Smuzhiyun 	if (!reg->hr_unclean_stop && !reg->hr_aborted_start) {
1257*4882a593Smuzhiyun 		o2hb_prepare_block(reg, 0);
1258*4882a593Smuzhiyun 		ret = o2hb_issue_node_write(reg, &write_wc);
1259*4882a593Smuzhiyun 		if (ret == 0)
1260*4882a593Smuzhiyun 			o2hb_wait_on_io(&write_wc);
1261*4882a593Smuzhiyun 		else
1262*4882a593Smuzhiyun 			mlog_errno(ret);
1263*4882a593Smuzhiyun 	}
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	/* Unpin node */
1266*4882a593Smuzhiyun 	o2nm_undepend_this_node();
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb thread exiting\n");
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	return 0;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
o2hb_debug_open(struct inode * inode,struct file * file)1274*4882a593Smuzhiyun static int o2hb_debug_open(struct inode *inode, struct file *file)
1275*4882a593Smuzhiyun {
1276*4882a593Smuzhiyun 	struct o2hb_debug_buf *db = inode->i_private;
1277*4882a593Smuzhiyun 	struct o2hb_region *reg;
1278*4882a593Smuzhiyun 	unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1279*4882a593Smuzhiyun 	unsigned long lts;
1280*4882a593Smuzhiyun 	char *buf = NULL;
1281*4882a593Smuzhiyun 	int i = -1;
1282*4882a593Smuzhiyun 	int out = 0;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	/* max_nodes should be the largest bitmap we pass here */
1285*4882a593Smuzhiyun 	BUG_ON(sizeof(map) < db->db_size);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1288*4882a593Smuzhiyun 	if (!buf)
1289*4882a593Smuzhiyun 		goto bail;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	switch (db->db_type) {
1292*4882a593Smuzhiyun 	case O2HB_DB_TYPE_LIVENODES:
1293*4882a593Smuzhiyun 	case O2HB_DB_TYPE_LIVEREGIONS:
1294*4882a593Smuzhiyun 	case O2HB_DB_TYPE_QUORUMREGIONS:
1295*4882a593Smuzhiyun 	case O2HB_DB_TYPE_FAILEDREGIONS:
1296*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
1297*4882a593Smuzhiyun 		memcpy(map, db->db_data, db->db_size);
1298*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
1299*4882a593Smuzhiyun 		break;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	case O2HB_DB_TYPE_REGION_LIVENODES:
1302*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
1303*4882a593Smuzhiyun 		reg = (struct o2hb_region *)db->db_data;
1304*4882a593Smuzhiyun 		memcpy(map, reg->hr_live_node_bitmap, db->db_size);
1305*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
1306*4882a593Smuzhiyun 		break;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	case O2HB_DB_TYPE_REGION_NUMBER:
1309*4882a593Smuzhiyun 		reg = (struct o2hb_region *)db->db_data;
1310*4882a593Smuzhiyun 		out += scnprintf(buf + out, PAGE_SIZE - out, "%d\n",
1311*4882a593Smuzhiyun 				reg->hr_region_num);
1312*4882a593Smuzhiyun 		goto done;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
1315*4882a593Smuzhiyun 		reg = (struct o2hb_region *)db->db_data;
1316*4882a593Smuzhiyun 		lts = reg->hr_last_timeout_start;
1317*4882a593Smuzhiyun 		/* If 0, it has never been set before */
1318*4882a593Smuzhiyun 		if (lts)
1319*4882a593Smuzhiyun 			lts = jiffies_to_msecs(jiffies - lts);
1320*4882a593Smuzhiyun 		out += scnprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts);
1321*4882a593Smuzhiyun 		goto done;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	case O2HB_DB_TYPE_REGION_PINNED:
1324*4882a593Smuzhiyun 		reg = (struct o2hb_region *)db->db_data;
1325*4882a593Smuzhiyun 		out += scnprintf(buf + out, PAGE_SIZE - out, "%u\n",
1326*4882a593Smuzhiyun 				!!reg->hr_item_pinned);
1327*4882a593Smuzhiyun 		goto done;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	default:
1330*4882a593Smuzhiyun 		goto done;
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
1334*4882a593Smuzhiyun 		out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
1335*4882a593Smuzhiyun 	out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun done:
1338*4882a593Smuzhiyun 	i_size_write(inode, out);
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	file->private_data = buf;
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	return 0;
1343*4882a593Smuzhiyun bail:
1344*4882a593Smuzhiyun 	return -ENOMEM;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun 
o2hb_debug_release(struct inode * inode,struct file * file)1347*4882a593Smuzhiyun static int o2hb_debug_release(struct inode *inode, struct file *file)
1348*4882a593Smuzhiyun {
1349*4882a593Smuzhiyun 	kfree(file->private_data);
1350*4882a593Smuzhiyun 	return 0;
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun 
o2hb_debug_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)1353*4882a593Smuzhiyun static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1354*4882a593Smuzhiyun 				 size_t nbytes, loff_t *ppos)
1355*4882a593Smuzhiyun {
1356*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
1357*4882a593Smuzhiyun 				       i_size_read(file->f_mapping->host));
1358*4882a593Smuzhiyun }
1359*4882a593Smuzhiyun #else
o2hb_debug_open(struct inode * inode,struct file * file)1360*4882a593Smuzhiyun static int o2hb_debug_open(struct inode *inode, struct file *file)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun 	return 0;
1363*4882a593Smuzhiyun }
o2hb_debug_release(struct inode * inode,struct file * file)1364*4882a593Smuzhiyun static int o2hb_debug_release(struct inode *inode, struct file *file)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun 	return 0;
1367*4882a593Smuzhiyun }
o2hb_debug_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)1368*4882a593Smuzhiyun static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
1369*4882a593Smuzhiyun 			       size_t nbytes, loff_t *ppos)
1370*4882a593Smuzhiyun {
1371*4882a593Smuzhiyun 	return 0;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun #endif  /* CONFIG_DEBUG_FS */
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun static const struct file_operations o2hb_debug_fops = {
1376*4882a593Smuzhiyun 	.open =		o2hb_debug_open,
1377*4882a593Smuzhiyun 	.release =	o2hb_debug_release,
1378*4882a593Smuzhiyun 	.read =		o2hb_debug_read,
1379*4882a593Smuzhiyun 	.llseek =	generic_file_llseek,
1380*4882a593Smuzhiyun };
1381*4882a593Smuzhiyun 
o2hb_exit(void)1382*4882a593Smuzhiyun void o2hb_exit(void)
1383*4882a593Smuzhiyun {
1384*4882a593Smuzhiyun 	debugfs_remove_recursive(o2hb_debug_dir);
1385*4882a593Smuzhiyun 	kfree(o2hb_db_livenodes);
1386*4882a593Smuzhiyun 	kfree(o2hb_db_liveregions);
1387*4882a593Smuzhiyun 	kfree(o2hb_db_quorumregions);
1388*4882a593Smuzhiyun 	kfree(o2hb_db_failedregions);
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun 
o2hb_debug_create(const char * name,struct dentry * dir,struct o2hb_debug_buf ** db,int db_len,int type,int size,int len,void * data)1391*4882a593Smuzhiyun static void o2hb_debug_create(const char *name, struct dentry *dir,
1392*4882a593Smuzhiyun 			      struct o2hb_debug_buf **db, int db_len, int type,
1393*4882a593Smuzhiyun 			      int size, int len, void *data)
1394*4882a593Smuzhiyun {
1395*4882a593Smuzhiyun 	*db = kmalloc(db_len, GFP_KERNEL);
1396*4882a593Smuzhiyun 	if (!*db)
1397*4882a593Smuzhiyun 		return;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	(*db)->db_type = type;
1400*4882a593Smuzhiyun 	(*db)->db_size = size;
1401*4882a593Smuzhiyun 	(*db)->db_len = len;
1402*4882a593Smuzhiyun 	(*db)->db_data = data;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db, &o2hb_debug_fops);
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun 
o2hb_debug_init(void)1407*4882a593Smuzhiyun static void o2hb_debug_init(void)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun 	o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_LIVENODES, o2hb_debug_dir,
1412*4882a593Smuzhiyun 			  &o2hb_db_livenodes, sizeof(*o2hb_db_livenodes),
1413*4882a593Smuzhiyun 			  O2HB_DB_TYPE_LIVENODES, sizeof(o2hb_live_node_bitmap),
1414*4882a593Smuzhiyun 			  O2NM_MAX_NODES, o2hb_live_node_bitmap);
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS, o2hb_debug_dir,
1417*4882a593Smuzhiyun 			  &o2hb_db_liveregions, sizeof(*o2hb_db_liveregions),
1418*4882a593Smuzhiyun 			  O2HB_DB_TYPE_LIVEREGIONS,
1419*4882a593Smuzhiyun 			  sizeof(o2hb_live_region_bitmap), O2NM_MAX_REGIONS,
1420*4882a593Smuzhiyun 			  o2hb_live_region_bitmap);
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS, o2hb_debug_dir,
1423*4882a593Smuzhiyun 			  &o2hb_db_quorumregions,
1424*4882a593Smuzhiyun 			  sizeof(*o2hb_db_quorumregions),
1425*4882a593Smuzhiyun 			  O2HB_DB_TYPE_QUORUMREGIONS,
1426*4882a593Smuzhiyun 			  sizeof(o2hb_quorum_region_bitmap), O2NM_MAX_REGIONS,
1427*4882a593Smuzhiyun 			  o2hb_quorum_region_bitmap);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS, o2hb_debug_dir,
1430*4882a593Smuzhiyun 			  &o2hb_db_failedregions,
1431*4882a593Smuzhiyun 			  sizeof(*o2hb_db_failedregions),
1432*4882a593Smuzhiyun 			  O2HB_DB_TYPE_FAILEDREGIONS,
1433*4882a593Smuzhiyun 			  sizeof(o2hb_failed_region_bitmap), O2NM_MAX_REGIONS,
1434*4882a593Smuzhiyun 			  o2hb_failed_region_bitmap);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
o2hb_init(void)1437*4882a593Smuzhiyun void o2hb_init(void)
1438*4882a593Smuzhiyun {
1439*4882a593Smuzhiyun 	int i;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
1442*4882a593Smuzhiyun 		INIT_LIST_HEAD(&o2hb_callbacks[i].list);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
1445*4882a593Smuzhiyun 		INIT_LIST_HEAD(&o2hb_live_slots[i]);
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	INIT_LIST_HEAD(&o2hb_node_events);
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
1450*4882a593Smuzhiyun 	memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
1451*4882a593Smuzhiyun 	memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
1452*4882a593Smuzhiyun 	memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
1453*4882a593Smuzhiyun 	memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	o2hb_dependent_users = 0;
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	o2hb_debug_init();
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun /* if we're already in a callback then we're already serialized by the sem */
o2hb_fill_node_map_from_callback(unsigned long * map,unsigned bytes)1461*4882a593Smuzhiyun static void o2hb_fill_node_map_from_callback(unsigned long *map,
1462*4882a593Smuzhiyun 					     unsigned bytes)
1463*4882a593Smuzhiyun {
1464*4882a593Smuzhiyun 	BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 	memcpy(map, &o2hb_live_node_bitmap, bytes);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun /*
1470*4882a593Smuzhiyun  * get a map of all nodes that are heartbeating in any regions
1471*4882a593Smuzhiyun  */
o2hb_fill_node_map(unsigned long * map,unsigned bytes)1472*4882a593Smuzhiyun void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
1473*4882a593Smuzhiyun {
1474*4882a593Smuzhiyun 	/* callers want to serialize this map and callbacks so that they
1475*4882a593Smuzhiyun 	 * can trust that they don't miss nodes coming to the party */
1476*4882a593Smuzhiyun 	down_read(&o2hb_callback_sem);
1477*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
1478*4882a593Smuzhiyun 	o2hb_fill_node_map_from_callback(map, bytes);
1479*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1480*4882a593Smuzhiyun 	up_read(&o2hb_callback_sem);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun /*
1485*4882a593Smuzhiyun  * heartbeat configfs bits.  The heartbeat set is a default set under
1486*4882a593Smuzhiyun  * the cluster set in nodemanager.c.
1487*4882a593Smuzhiyun  */
1488*4882a593Smuzhiyun 
to_o2hb_region(struct config_item * item)1489*4882a593Smuzhiyun static struct o2hb_region *to_o2hb_region(struct config_item *item)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun 	return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun /* drop_item only drops its ref after killing the thread, nothing should
1495*4882a593Smuzhiyun  * be using the region anymore.  this has to clean up any state that
1496*4882a593Smuzhiyun  * attributes might have built up. */
o2hb_region_release(struct config_item * item)1497*4882a593Smuzhiyun static void o2hb_region_release(struct config_item *item)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun 	int i;
1500*4882a593Smuzhiyun 	struct page *page;
1501*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "hb region release (%s)\n", reg->hr_dev_name);
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	kfree(reg->hr_tmp_block);
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	if (reg->hr_slot_data) {
1508*4882a593Smuzhiyun 		for (i = 0; i < reg->hr_num_pages; i++) {
1509*4882a593Smuzhiyun 			page = reg->hr_slot_data[i];
1510*4882a593Smuzhiyun 			if (page)
1511*4882a593Smuzhiyun 				__free_page(page);
1512*4882a593Smuzhiyun 		}
1513*4882a593Smuzhiyun 		kfree(reg->hr_slot_data);
1514*4882a593Smuzhiyun 	}
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	if (reg->hr_bdev)
1517*4882a593Smuzhiyun 		blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	kfree(reg->hr_slots);
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	debugfs_remove_recursive(reg->hr_debug_dir);
1522*4882a593Smuzhiyun 	kfree(reg->hr_db_livenodes);
1523*4882a593Smuzhiyun 	kfree(reg->hr_db_regnum);
1524*4882a593Smuzhiyun 	kfree(reg->hr_db_elapsed_time);
1525*4882a593Smuzhiyun 	kfree(reg->hr_db_pinned);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
1528*4882a593Smuzhiyun 	list_del(&reg->hr_all_item);
1529*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	o2net_unregister_handler_list(&reg->hr_handler_list);
1532*4882a593Smuzhiyun 	kfree(reg);
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun 
o2hb_read_block_input(struct o2hb_region * reg,const char * page,unsigned long * ret_bytes,unsigned int * ret_bits)1535*4882a593Smuzhiyun static int o2hb_read_block_input(struct o2hb_region *reg,
1536*4882a593Smuzhiyun 				 const char *page,
1537*4882a593Smuzhiyun 				 unsigned long *ret_bytes,
1538*4882a593Smuzhiyun 				 unsigned int *ret_bits)
1539*4882a593Smuzhiyun {
1540*4882a593Smuzhiyun 	unsigned long bytes;
1541*4882a593Smuzhiyun 	char *p = (char *)page;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	bytes = simple_strtoul(p, &p, 0);
1544*4882a593Smuzhiyun 	if (!p || (*p && (*p != '\n')))
1545*4882a593Smuzhiyun 		return -EINVAL;
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	/* Heartbeat and fs min / max block sizes are the same. */
1548*4882a593Smuzhiyun 	if (bytes > 4096 || bytes < 512)
1549*4882a593Smuzhiyun 		return -ERANGE;
1550*4882a593Smuzhiyun 	if (hweight16(bytes) != 1)
1551*4882a593Smuzhiyun 		return -EINVAL;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	if (ret_bytes)
1554*4882a593Smuzhiyun 		*ret_bytes = bytes;
1555*4882a593Smuzhiyun 	if (ret_bits)
1556*4882a593Smuzhiyun 		*ret_bits = ffs(bytes) - 1;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	return 0;
1559*4882a593Smuzhiyun }
1560*4882a593Smuzhiyun 
o2hb_region_block_bytes_show(struct config_item * item,char * page)1561*4882a593Smuzhiyun static ssize_t o2hb_region_block_bytes_show(struct config_item *item,
1562*4882a593Smuzhiyun 					    char *page)
1563*4882a593Smuzhiyun {
1564*4882a593Smuzhiyun 	return sprintf(page, "%u\n", to_o2hb_region(item)->hr_block_bytes);
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun 
o2hb_region_block_bytes_store(struct config_item * item,const char * page,size_t count)1567*4882a593Smuzhiyun static ssize_t o2hb_region_block_bytes_store(struct config_item *item,
1568*4882a593Smuzhiyun 					     const char *page,
1569*4882a593Smuzhiyun 					     size_t count)
1570*4882a593Smuzhiyun {
1571*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1572*4882a593Smuzhiyun 	int status;
1573*4882a593Smuzhiyun 	unsigned long block_bytes;
1574*4882a593Smuzhiyun 	unsigned int block_bits;
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun 	if (reg->hr_bdev)
1577*4882a593Smuzhiyun 		return -EINVAL;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	status = o2hb_read_block_input(reg, page, &block_bytes,
1580*4882a593Smuzhiyun 				       &block_bits);
1581*4882a593Smuzhiyun 	if (status)
1582*4882a593Smuzhiyun 		return status;
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	reg->hr_block_bytes = (unsigned int)block_bytes;
1585*4882a593Smuzhiyun 	reg->hr_block_bits = block_bits;
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	return count;
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun 
o2hb_region_start_block_show(struct config_item * item,char * page)1590*4882a593Smuzhiyun static ssize_t o2hb_region_start_block_show(struct config_item *item,
1591*4882a593Smuzhiyun 					    char *page)
1592*4882a593Smuzhiyun {
1593*4882a593Smuzhiyun 	return sprintf(page, "%llu\n", to_o2hb_region(item)->hr_start_block);
1594*4882a593Smuzhiyun }
1595*4882a593Smuzhiyun 
o2hb_region_start_block_store(struct config_item * item,const char * page,size_t count)1596*4882a593Smuzhiyun static ssize_t o2hb_region_start_block_store(struct config_item *item,
1597*4882a593Smuzhiyun 					     const char *page,
1598*4882a593Smuzhiyun 					     size_t count)
1599*4882a593Smuzhiyun {
1600*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1601*4882a593Smuzhiyun 	unsigned long long tmp;
1602*4882a593Smuzhiyun 	char *p = (char *)page;
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	if (reg->hr_bdev)
1605*4882a593Smuzhiyun 		return -EINVAL;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	tmp = simple_strtoull(p, &p, 0);
1608*4882a593Smuzhiyun 	if (!p || (*p && (*p != '\n')))
1609*4882a593Smuzhiyun 		return -EINVAL;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	reg->hr_start_block = tmp;
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	return count;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun 
o2hb_region_blocks_show(struct config_item * item,char * page)1616*4882a593Smuzhiyun static ssize_t o2hb_region_blocks_show(struct config_item *item, char *page)
1617*4882a593Smuzhiyun {
1618*4882a593Smuzhiyun 	return sprintf(page, "%d\n", to_o2hb_region(item)->hr_blocks);
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
o2hb_region_blocks_store(struct config_item * item,const char * page,size_t count)1621*4882a593Smuzhiyun static ssize_t o2hb_region_blocks_store(struct config_item *item,
1622*4882a593Smuzhiyun 					const char *page,
1623*4882a593Smuzhiyun 					size_t count)
1624*4882a593Smuzhiyun {
1625*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1626*4882a593Smuzhiyun 	unsigned long tmp;
1627*4882a593Smuzhiyun 	char *p = (char *)page;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	if (reg->hr_bdev)
1630*4882a593Smuzhiyun 		return -EINVAL;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	tmp = simple_strtoul(p, &p, 0);
1633*4882a593Smuzhiyun 	if (!p || (*p && (*p != '\n')))
1634*4882a593Smuzhiyun 		return -EINVAL;
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	if (tmp > O2NM_MAX_NODES || tmp == 0)
1637*4882a593Smuzhiyun 		return -ERANGE;
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	reg->hr_blocks = (unsigned int)tmp;
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	return count;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
o2hb_region_dev_show(struct config_item * item,char * page)1644*4882a593Smuzhiyun static ssize_t o2hb_region_dev_show(struct config_item *item, char *page)
1645*4882a593Smuzhiyun {
1646*4882a593Smuzhiyun 	unsigned int ret = 0;
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	if (to_o2hb_region(item)->hr_bdev)
1649*4882a593Smuzhiyun 		ret = sprintf(page, "%s\n", to_o2hb_region(item)->hr_dev_name);
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	return ret;
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun 
o2hb_init_region_params(struct o2hb_region * reg)1654*4882a593Smuzhiyun static void o2hb_init_region_params(struct o2hb_region *reg)
1655*4882a593Smuzhiyun {
1656*4882a593Smuzhiyun 	reg->hr_slots_per_page = PAGE_SIZE >> reg->hr_block_bits;
1657*4882a593Smuzhiyun 	reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
1660*4882a593Smuzhiyun 	     reg->hr_start_block, reg->hr_blocks);
1661*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
1662*4882a593Smuzhiyun 	     reg->hr_block_bytes, reg->hr_block_bits);
1663*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
1664*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
1665*4882a593Smuzhiyun }
1666*4882a593Smuzhiyun 
o2hb_map_slot_data(struct o2hb_region * reg)1667*4882a593Smuzhiyun static int o2hb_map_slot_data(struct o2hb_region *reg)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun 	int i, j;
1670*4882a593Smuzhiyun 	unsigned int last_slot;
1671*4882a593Smuzhiyun 	unsigned int spp = reg->hr_slots_per_page;
1672*4882a593Smuzhiyun 	struct page *page;
1673*4882a593Smuzhiyun 	char *raw;
1674*4882a593Smuzhiyun 	struct o2hb_disk_slot *slot;
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
1677*4882a593Smuzhiyun 	if (reg->hr_tmp_block == NULL)
1678*4882a593Smuzhiyun 		return -ENOMEM;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	reg->hr_slots = kcalloc(reg->hr_blocks,
1681*4882a593Smuzhiyun 				sizeof(struct o2hb_disk_slot), GFP_KERNEL);
1682*4882a593Smuzhiyun 	if (reg->hr_slots == NULL)
1683*4882a593Smuzhiyun 		return -ENOMEM;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	for(i = 0; i < reg->hr_blocks; i++) {
1686*4882a593Smuzhiyun 		slot = &reg->hr_slots[i];
1687*4882a593Smuzhiyun 		slot->ds_node_num = i;
1688*4882a593Smuzhiyun 		INIT_LIST_HEAD(&slot->ds_live_item);
1689*4882a593Smuzhiyun 		slot->ds_raw_block = NULL;
1690*4882a593Smuzhiyun 	}
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
1693*4882a593Smuzhiyun 	mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
1694*4882a593Smuzhiyun 			   "at %u blocks per page\n",
1695*4882a593Smuzhiyun 	     reg->hr_num_pages, reg->hr_blocks, spp);
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
1698*4882a593Smuzhiyun 				    GFP_KERNEL);
1699*4882a593Smuzhiyun 	if (!reg->hr_slot_data)
1700*4882a593Smuzhiyun 		return -ENOMEM;
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	for(i = 0; i < reg->hr_num_pages; i++) {
1703*4882a593Smuzhiyun 		page = alloc_page(GFP_KERNEL);
1704*4882a593Smuzhiyun 		if (!page)
1705*4882a593Smuzhiyun 			return -ENOMEM;
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 		reg->hr_slot_data[i] = page;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 		last_slot = i * spp;
1710*4882a593Smuzhiyun 		raw = page_address(page);
1711*4882a593Smuzhiyun 		for (j = 0;
1712*4882a593Smuzhiyun 		     (j < spp) && ((j + last_slot) < reg->hr_blocks);
1713*4882a593Smuzhiyun 		     j++) {
1714*4882a593Smuzhiyun 			BUG_ON((j + last_slot) >= reg->hr_blocks);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 			slot = &reg->hr_slots[j + last_slot];
1717*4882a593Smuzhiyun 			slot->ds_raw_block =
1718*4882a593Smuzhiyun 				(struct o2hb_disk_heartbeat_block *) raw;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 			raw += reg->hr_block_bytes;
1721*4882a593Smuzhiyun 		}
1722*4882a593Smuzhiyun 	}
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	return 0;
1725*4882a593Smuzhiyun }
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun /* Read in all the slots available and populate the tracking
1728*4882a593Smuzhiyun  * structures so that we can start with a baseline idea of what's
1729*4882a593Smuzhiyun  * there. */
o2hb_populate_slot_data(struct o2hb_region * reg)1730*4882a593Smuzhiyun static int o2hb_populate_slot_data(struct o2hb_region *reg)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun 	int ret, i;
1733*4882a593Smuzhiyun 	struct o2hb_disk_slot *slot;
1734*4882a593Smuzhiyun 	struct o2hb_disk_heartbeat_block *hb_block;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	ret = o2hb_read_slots(reg, 0, reg->hr_blocks);
1737*4882a593Smuzhiyun 	if (ret)
1738*4882a593Smuzhiyun 		goto out;
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	/* We only want to get an idea of the values initially in each
1741*4882a593Smuzhiyun 	 * slot, so we do no verification - o2hb_check_slot will
1742*4882a593Smuzhiyun 	 * actually determine if each configured slot is valid and
1743*4882a593Smuzhiyun 	 * whether any values have changed. */
1744*4882a593Smuzhiyun 	for(i = 0; i < reg->hr_blocks; i++) {
1745*4882a593Smuzhiyun 		slot = &reg->hr_slots[i];
1746*4882a593Smuzhiyun 		hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 		/* Only fill the values that o2hb_check_slot uses to
1749*4882a593Smuzhiyun 		 * determine changing slots */
1750*4882a593Smuzhiyun 		slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
1751*4882a593Smuzhiyun 		slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
1752*4882a593Smuzhiyun 	}
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun out:
1755*4882a593Smuzhiyun 	return ret;
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
o2hb_region_dev_store(struct config_item * item,const char * page,size_t count)1759*4882a593Smuzhiyun static ssize_t o2hb_region_dev_store(struct config_item *item,
1760*4882a593Smuzhiyun 				     const char *page,
1761*4882a593Smuzhiyun 				     size_t count)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1764*4882a593Smuzhiyun 	struct task_struct *hb_task;
1765*4882a593Smuzhiyun 	long fd;
1766*4882a593Smuzhiyun 	int sectsize;
1767*4882a593Smuzhiyun 	char *p = (char *)page;
1768*4882a593Smuzhiyun 	struct fd f;
1769*4882a593Smuzhiyun 	ssize_t ret = -EINVAL;
1770*4882a593Smuzhiyun 	int live_threshold;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	if (reg->hr_bdev)
1773*4882a593Smuzhiyun 		goto out;
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 	/* We can't heartbeat without having had our node number
1776*4882a593Smuzhiyun 	 * configured yet. */
1777*4882a593Smuzhiyun 	if (o2nm_this_node() == O2NM_MAX_NODES)
1778*4882a593Smuzhiyun 		goto out;
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 	fd = simple_strtol(p, &p, 0);
1781*4882a593Smuzhiyun 	if (!p || (*p && (*p != '\n')))
1782*4882a593Smuzhiyun 		goto out;
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	if (fd < 0 || fd >= INT_MAX)
1785*4882a593Smuzhiyun 		goto out;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 	f = fdget(fd);
1788*4882a593Smuzhiyun 	if (f.file == NULL)
1789*4882a593Smuzhiyun 		goto out;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
1792*4882a593Smuzhiyun 	    reg->hr_block_bytes == 0)
1793*4882a593Smuzhiyun 		goto out2;
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	if (!S_ISBLK(f.file->f_mapping->host->i_mode))
1796*4882a593Smuzhiyun 		goto out2;
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun 	reg->hr_bdev = blkdev_get_by_dev(f.file->f_mapping->host->i_rdev,
1799*4882a593Smuzhiyun 					 FMODE_WRITE | FMODE_READ, NULL);
1800*4882a593Smuzhiyun 	if (IS_ERR(reg->hr_bdev)) {
1801*4882a593Smuzhiyun 		ret = PTR_ERR(reg->hr_bdev);
1802*4882a593Smuzhiyun 		reg->hr_bdev = NULL;
1803*4882a593Smuzhiyun 		goto out2;
1804*4882a593Smuzhiyun 	}
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	bdevname(reg->hr_bdev, reg->hr_dev_name);
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	sectsize = bdev_logical_block_size(reg->hr_bdev);
1809*4882a593Smuzhiyun 	if (sectsize != reg->hr_block_bytes) {
1810*4882a593Smuzhiyun 		mlog(ML_ERROR,
1811*4882a593Smuzhiyun 		     "blocksize %u incorrect for device, expected %d",
1812*4882a593Smuzhiyun 		     reg->hr_block_bytes, sectsize);
1813*4882a593Smuzhiyun 		ret = -EINVAL;
1814*4882a593Smuzhiyun 		goto out3;
1815*4882a593Smuzhiyun 	}
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	o2hb_init_region_params(reg);
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	/* Generation of zero is invalid */
1820*4882a593Smuzhiyun 	do {
1821*4882a593Smuzhiyun 		get_random_bytes(&reg->hr_generation,
1822*4882a593Smuzhiyun 				 sizeof(reg->hr_generation));
1823*4882a593Smuzhiyun 	} while (reg->hr_generation == 0);
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 	ret = o2hb_map_slot_data(reg);
1826*4882a593Smuzhiyun 	if (ret) {
1827*4882a593Smuzhiyun 		mlog_errno(ret);
1828*4882a593Smuzhiyun 		goto out3;
1829*4882a593Smuzhiyun 	}
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun 	ret = o2hb_populate_slot_data(reg);
1832*4882a593Smuzhiyun 	if (ret) {
1833*4882a593Smuzhiyun 		mlog_errno(ret);
1834*4882a593Smuzhiyun 		goto out3;
1835*4882a593Smuzhiyun 	}
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
1838*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&reg->hr_nego_timeout_work, o2hb_nego_timeout);
1839*4882a593Smuzhiyun 
1840*4882a593Smuzhiyun 	/*
1841*4882a593Smuzhiyun 	 * A node is considered live after it has beat LIVE_THRESHOLD
1842*4882a593Smuzhiyun 	 * times.  We're not steady until we've given them a chance
1843*4882a593Smuzhiyun 	 * _after_ our first read.
1844*4882a593Smuzhiyun 	 * The default threshold is bare minimum so as to limit the delay
1845*4882a593Smuzhiyun 	 * during mounts. For global heartbeat, the threshold doubled for the
1846*4882a593Smuzhiyun 	 * first region.
1847*4882a593Smuzhiyun 	 */
1848*4882a593Smuzhiyun 	live_threshold = O2HB_LIVE_THRESHOLD;
1849*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active()) {
1850*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
1851*4882a593Smuzhiyun 		if (bitmap_weight(o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
1852*4882a593Smuzhiyun 			live_threshold <<= 1;
1853*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
1854*4882a593Smuzhiyun 	}
1855*4882a593Smuzhiyun 	++live_threshold;
1856*4882a593Smuzhiyun 	atomic_set(&reg->hr_steady_iterations, live_threshold);
1857*4882a593Smuzhiyun 	/* unsteady_iterations is triple the steady_iterations */
1858*4882a593Smuzhiyun 	atomic_set(&reg->hr_unsteady_iterations, (live_threshold * 3));
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
1861*4882a593Smuzhiyun 			      reg->hr_item.ci_name);
1862*4882a593Smuzhiyun 	if (IS_ERR(hb_task)) {
1863*4882a593Smuzhiyun 		ret = PTR_ERR(hb_task);
1864*4882a593Smuzhiyun 		mlog_errno(ret);
1865*4882a593Smuzhiyun 		goto out3;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
1869*4882a593Smuzhiyun 	reg->hr_task = hb_task;
1870*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	ret = wait_event_interruptible(o2hb_steady_queue,
1873*4882a593Smuzhiyun 				atomic_read(&reg->hr_steady_iterations) == 0 ||
1874*4882a593Smuzhiyun 				reg->hr_node_deleted);
1875*4882a593Smuzhiyun 	if (ret) {
1876*4882a593Smuzhiyun 		atomic_set(&reg->hr_steady_iterations, 0);
1877*4882a593Smuzhiyun 		reg->hr_aborted_start = 1;
1878*4882a593Smuzhiyun 	}
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	if (reg->hr_aborted_start) {
1881*4882a593Smuzhiyun 		ret = -EIO;
1882*4882a593Smuzhiyun 		goto out3;
1883*4882a593Smuzhiyun 	}
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 	if (reg->hr_node_deleted) {
1886*4882a593Smuzhiyun 		ret = -EINVAL;
1887*4882a593Smuzhiyun 		goto out3;
1888*4882a593Smuzhiyun 	}
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	/* Ok, we were woken.  Make sure it wasn't by drop_item() */
1891*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
1892*4882a593Smuzhiyun 	hb_task = reg->hr_task;
1893*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active())
1894*4882a593Smuzhiyun 		set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
1895*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1896*4882a593Smuzhiyun 
1897*4882a593Smuzhiyun 	if (hb_task)
1898*4882a593Smuzhiyun 		ret = count;
1899*4882a593Smuzhiyun 	else
1900*4882a593Smuzhiyun 		ret = -EIO;
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	if (hb_task && o2hb_global_heartbeat_active())
1903*4882a593Smuzhiyun 		printk(KERN_NOTICE "o2hb: Heartbeat started on region %s (%s)\n",
1904*4882a593Smuzhiyun 		       config_item_name(&reg->hr_item), reg->hr_dev_name);
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun out3:
1907*4882a593Smuzhiyun 	if (ret < 0) {
1908*4882a593Smuzhiyun 		blkdev_put(reg->hr_bdev, FMODE_READ | FMODE_WRITE);
1909*4882a593Smuzhiyun 		reg->hr_bdev = NULL;
1910*4882a593Smuzhiyun 	}
1911*4882a593Smuzhiyun out2:
1912*4882a593Smuzhiyun 	fdput(f);
1913*4882a593Smuzhiyun out:
1914*4882a593Smuzhiyun 	return ret;
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun 
o2hb_region_pid_show(struct config_item * item,char * page)1917*4882a593Smuzhiyun static ssize_t o2hb_region_pid_show(struct config_item *item, char *page)
1918*4882a593Smuzhiyun {
1919*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
1920*4882a593Smuzhiyun 	pid_t pid = 0;
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
1923*4882a593Smuzhiyun 	if (reg->hr_task)
1924*4882a593Smuzhiyun 		pid = task_pid_nr(reg->hr_task);
1925*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	if (!pid)
1928*4882a593Smuzhiyun 		return 0;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	return sprintf(page, "%u\n", pid);
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_region_, block_bytes);
1934*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_region_, start_block);
1935*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_region_, blocks);
1936*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_region_, dev);
1937*4882a593Smuzhiyun CONFIGFS_ATTR_RO(o2hb_region_, pid);
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun static struct configfs_attribute *o2hb_region_attrs[] = {
1940*4882a593Smuzhiyun 	&o2hb_region_attr_block_bytes,
1941*4882a593Smuzhiyun 	&o2hb_region_attr_start_block,
1942*4882a593Smuzhiyun 	&o2hb_region_attr_blocks,
1943*4882a593Smuzhiyun 	&o2hb_region_attr_dev,
1944*4882a593Smuzhiyun 	&o2hb_region_attr_pid,
1945*4882a593Smuzhiyun 	NULL,
1946*4882a593Smuzhiyun };
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun static struct configfs_item_operations o2hb_region_item_ops = {
1949*4882a593Smuzhiyun 	.release		= o2hb_region_release,
1950*4882a593Smuzhiyun };
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun static const struct config_item_type o2hb_region_type = {
1953*4882a593Smuzhiyun 	.ct_item_ops	= &o2hb_region_item_ops,
1954*4882a593Smuzhiyun 	.ct_attrs	= o2hb_region_attrs,
1955*4882a593Smuzhiyun 	.ct_owner	= THIS_MODULE,
1956*4882a593Smuzhiyun };
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun /* heartbeat set */
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun struct o2hb_heartbeat_group {
1961*4882a593Smuzhiyun 	struct config_group hs_group;
1962*4882a593Smuzhiyun 	/* some stuff? */
1963*4882a593Smuzhiyun };
1964*4882a593Smuzhiyun 
to_o2hb_heartbeat_group(struct config_group * group)1965*4882a593Smuzhiyun static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
1966*4882a593Smuzhiyun {
1967*4882a593Smuzhiyun 	return group ?
1968*4882a593Smuzhiyun 		container_of(group, struct o2hb_heartbeat_group, hs_group)
1969*4882a593Smuzhiyun 		: NULL;
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun 
o2hb_debug_region_init(struct o2hb_region * reg,struct dentry * parent)1972*4882a593Smuzhiyun static void o2hb_debug_region_init(struct o2hb_region *reg,
1973*4882a593Smuzhiyun 				   struct dentry *parent)
1974*4882a593Smuzhiyun {
1975*4882a593Smuzhiyun 	struct dentry *dir;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	dir = debugfs_create_dir(config_item_name(&reg->hr_item), parent);
1978*4882a593Smuzhiyun 	reg->hr_debug_dir = dir;
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_LIVENODES, dir, &(reg->hr_db_livenodes),
1981*4882a593Smuzhiyun 			  sizeof(*(reg->hr_db_livenodes)),
1982*4882a593Smuzhiyun 			  O2HB_DB_TYPE_REGION_LIVENODES,
1983*4882a593Smuzhiyun 			  sizeof(reg->hr_live_node_bitmap), O2NM_MAX_NODES,
1984*4882a593Smuzhiyun 			  reg);
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER, dir, &(reg->hr_db_regnum),
1987*4882a593Smuzhiyun 			  sizeof(*(reg->hr_db_regnum)),
1988*4882a593Smuzhiyun 			  O2HB_DB_TYPE_REGION_NUMBER, 0, O2NM_MAX_NODES, reg);
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME, dir,
1991*4882a593Smuzhiyun 			  &(reg->hr_db_elapsed_time),
1992*4882a593Smuzhiyun 			  sizeof(*(reg->hr_db_elapsed_time)),
1993*4882a593Smuzhiyun 			  O2HB_DB_TYPE_REGION_ELAPSED_TIME, 0, 0, reg);
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 	o2hb_debug_create(O2HB_DEBUG_REGION_PINNED, dir, &(reg->hr_db_pinned),
1996*4882a593Smuzhiyun 			  sizeof(*(reg->hr_db_pinned)),
1997*4882a593Smuzhiyun 			  O2HB_DB_TYPE_REGION_PINNED, 0, 0, reg);
1998*4882a593Smuzhiyun 
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun 
o2hb_heartbeat_group_make_item(struct config_group * group,const char * name)2001*4882a593Smuzhiyun static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
2002*4882a593Smuzhiyun 							  const char *name)
2003*4882a593Smuzhiyun {
2004*4882a593Smuzhiyun 	struct o2hb_region *reg = NULL;
2005*4882a593Smuzhiyun 	int ret;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
2008*4882a593Smuzhiyun 	if (reg == NULL)
2009*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
2012*4882a593Smuzhiyun 		ret = -ENAMETOOLONG;
2013*4882a593Smuzhiyun 		goto free;
2014*4882a593Smuzhiyun 	}
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2017*4882a593Smuzhiyun 	reg->hr_region_num = 0;
2018*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active()) {
2019*4882a593Smuzhiyun 		reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
2020*4882a593Smuzhiyun 							 O2NM_MAX_REGIONS);
2021*4882a593Smuzhiyun 		if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
2022*4882a593Smuzhiyun 			spin_unlock(&o2hb_live_lock);
2023*4882a593Smuzhiyun 			ret = -EFBIG;
2024*4882a593Smuzhiyun 			goto free;
2025*4882a593Smuzhiyun 		}
2026*4882a593Smuzhiyun 		set_bit(reg->hr_region_num, o2hb_region_bitmap);
2027*4882a593Smuzhiyun 	}
2028*4882a593Smuzhiyun 	list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
2029*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
2032*4882a593Smuzhiyun 
2033*4882a593Smuzhiyun 	/* this is the same way to generate msg key as dlm, for local heartbeat,
2034*4882a593Smuzhiyun 	 * name is also the same, so make initial crc value different to avoid
2035*4882a593Smuzhiyun 	 * message key conflict.
2036*4882a593Smuzhiyun 	 */
2037*4882a593Smuzhiyun 	reg->hr_key = crc32_le(reg->hr_region_num + O2NM_MAX_REGIONS,
2038*4882a593Smuzhiyun 		name, strlen(name));
2039*4882a593Smuzhiyun 	INIT_LIST_HEAD(&reg->hr_handler_list);
2040*4882a593Smuzhiyun 	ret = o2net_register_handler(O2HB_NEGO_TIMEOUT_MSG, reg->hr_key,
2041*4882a593Smuzhiyun 			sizeof(struct o2hb_nego_msg),
2042*4882a593Smuzhiyun 			o2hb_nego_timeout_handler,
2043*4882a593Smuzhiyun 			reg, NULL, &reg->hr_handler_list);
2044*4882a593Smuzhiyun 	if (ret)
2045*4882a593Smuzhiyun 		goto remove_item;
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	ret = o2net_register_handler(O2HB_NEGO_APPROVE_MSG, reg->hr_key,
2048*4882a593Smuzhiyun 			sizeof(struct o2hb_nego_msg),
2049*4882a593Smuzhiyun 			o2hb_nego_approve_handler,
2050*4882a593Smuzhiyun 			reg, NULL, &reg->hr_handler_list);
2051*4882a593Smuzhiyun 	if (ret)
2052*4882a593Smuzhiyun 		goto unregister_handler;
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	o2hb_debug_region_init(reg, o2hb_debug_dir);
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 	return &reg->hr_item;
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun unregister_handler:
2059*4882a593Smuzhiyun 	o2net_unregister_handler_list(&reg->hr_handler_list);
2060*4882a593Smuzhiyun remove_item:
2061*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2062*4882a593Smuzhiyun 	list_del(&reg->hr_all_item);
2063*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active())
2064*4882a593Smuzhiyun 		clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2065*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2066*4882a593Smuzhiyun free:
2067*4882a593Smuzhiyun 	kfree(reg);
2068*4882a593Smuzhiyun 	return ERR_PTR(ret);
2069*4882a593Smuzhiyun }
2070*4882a593Smuzhiyun 
o2hb_heartbeat_group_drop_item(struct config_group * group,struct config_item * item)2071*4882a593Smuzhiyun static void o2hb_heartbeat_group_drop_item(struct config_group *group,
2072*4882a593Smuzhiyun 					   struct config_item *item)
2073*4882a593Smuzhiyun {
2074*4882a593Smuzhiyun 	struct task_struct *hb_task;
2075*4882a593Smuzhiyun 	struct o2hb_region *reg = to_o2hb_region(item);
2076*4882a593Smuzhiyun 	int quorum_region = 0;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	/* stop the thread when the user removes the region dir */
2079*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2080*4882a593Smuzhiyun 	hb_task = reg->hr_task;
2081*4882a593Smuzhiyun 	reg->hr_task = NULL;
2082*4882a593Smuzhiyun 	reg->hr_item_dropped = 1;
2083*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2084*4882a593Smuzhiyun 
2085*4882a593Smuzhiyun 	if (hb_task)
2086*4882a593Smuzhiyun 		kthread_stop(hb_task);
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	if (o2hb_global_heartbeat_active()) {
2089*4882a593Smuzhiyun 		spin_lock(&o2hb_live_lock);
2090*4882a593Smuzhiyun 		clear_bit(reg->hr_region_num, o2hb_region_bitmap);
2091*4882a593Smuzhiyun 		clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
2092*4882a593Smuzhiyun 		if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
2093*4882a593Smuzhiyun 			quorum_region = 1;
2094*4882a593Smuzhiyun 		clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
2095*4882a593Smuzhiyun 		spin_unlock(&o2hb_live_lock);
2096*4882a593Smuzhiyun 		printk(KERN_NOTICE "o2hb: Heartbeat %s on region %s (%s)\n",
2097*4882a593Smuzhiyun 		       ((atomic_read(&reg->hr_steady_iterations) == 0) ?
2098*4882a593Smuzhiyun 			"stopped" : "start aborted"), config_item_name(item),
2099*4882a593Smuzhiyun 		       reg->hr_dev_name);
2100*4882a593Smuzhiyun 	}
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	/*
2103*4882a593Smuzhiyun 	 * If we're racing a dev_write(), we need to wake them.  They will
2104*4882a593Smuzhiyun 	 * check reg->hr_task
2105*4882a593Smuzhiyun 	 */
2106*4882a593Smuzhiyun 	if (atomic_read(&reg->hr_steady_iterations) != 0) {
2107*4882a593Smuzhiyun 		reg->hr_aborted_start = 1;
2108*4882a593Smuzhiyun 		atomic_set(&reg->hr_steady_iterations, 0);
2109*4882a593Smuzhiyun 		wake_up(&o2hb_steady_queue);
2110*4882a593Smuzhiyun 	}
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	config_item_put(item);
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun 	if (!o2hb_global_heartbeat_active() || !quorum_region)
2115*4882a593Smuzhiyun 		return;
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	/*
2118*4882a593Smuzhiyun 	 * If global heartbeat active and there are dependent users,
2119*4882a593Smuzhiyun 	 * pin all regions if quorum region count <= CUT_OFF
2120*4882a593Smuzhiyun 	 */
2121*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	if (!o2hb_dependent_users)
2124*4882a593Smuzhiyun 		goto unlock;
2125*4882a593Smuzhiyun 
2126*4882a593Smuzhiyun 	if (bitmap_weight(o2hb_quorum_region_bitmap,
2127*4882a593Smuzhiyun 			   O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2128*4882a593Smuzhiyun 		o2hb_region_pin(NULL);
2129*4882a593Smuzhiyun 
2130*4882a593Smuzhiyun unlock:
2131*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2132*4882a593Smuzhiyun }
2133*4882a593Smuzhiyun 
o2hb_heartbeat_group_dead_threshold_show(struct config_item * item,char * page)2134*4882a593Smuzhiyun static ssize_t o2hb_heartbeat_group_dead_threshold_show(struct config_item *item,
2135*4882a593Smuzhiyun 		char *page)
2136*4882a593Smuzhiyun {
2137*4882a593Smuzhiyun 	return sprintf(page, "%u\n", o2hb_dead_threshold);
2138*4882a593Smuzhiyun }
2139*4882a593Smuzhiyun 
o2hb_heartbeat_group_dead_threshold_store(struct config_item * item,const char * page,size_t count)2140*4882a593Smuzhiyun static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *item,
2141*4882a593Smuzhiyun 		const char *page, size_t count)
2142*4882a593Smuzhiyun {
2143*4882a593Smuzhiyun 	unsigned long tmp;
2144*4882a593Smuzhiyun 	char *p = (char *)page;
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	tmp = simple_strtoul(p, &p, 10);
2147*4882a593Smuzhiyun 	if (!p || (*p && (*p != '\n')))
2148*4882a593Smuzhiyun                 return -EINVAL;
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	/* this will validate ranges for us. */
2151*4882a593Smuzhiyun 	o2hb_dead_threshold_set((unsigned int) tmp);
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	return count;
2154*4882a593Smuzhiyun }
2155*4882a593Smuzhiyun 
o2hb_heartbeat_group_mode_show(struct config_item * item,char * page)2156*4882a593Smuzhiyun static ssize_t o2hb_heartbeat_group_mode_show(struct config_item *item,
2157*4882a593Smuzhiyun 		char *page)
2158*4882a593Smuzhiyun {
2159*4882a593Smuzhiyun 	return sprintf(page, "%s\n",
2160*4882a593Smuzhiyun 		       o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
2161*4882a593Smuzhiyun }
2162*4882a593Smuzhiyun 
o2hb_heartbeat_group_mode_store(struct config_item * item,const char * page,size_t count)2163*4882a593Smuzhiyun static ssize_t o2hb_heartbeat_group_mode_store(struct config_item *item,
2164*4882a593Smuzhiyun 		const char *page, size_t count)
2165*4882a593Smuzhiyun {
2166*4882a593Smuzhiyun 	unsigned int i;
2167*4882a593Smuzhiyun 	int ret;
2168*4882a593Smuzhiyun 	size_t len;
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun 	len = (page[count - 1] == '\n') ? count - 1 : count;
2171*4882a593Smuzhiyun 	if (!len)
2172*4882a593Smuzhiyun 		return -EINVAL;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
2175*4882a593Smuzhiyun 		if (strncasecmp(page, o2hb_heartbeat_mode_desc[i], len))
2176*4882a593Smuzhiyun 			continue;
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 		ret = o2hb_global_heartbeat_mode_set(i);
2179*4882a593Smuzhiyun 		if (!ret)
2180*4882a593Smuzhiyun 			printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
2181*4882a593Smuzhiyun 			       o2hb_heartbeat_mode_desc[i]);
2182*4882a593Smuzhiyun 		return count;
2183*4882a593Smuzhiyun 	}
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun 	return -EINVAL;
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun }
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_heartbeat_group_, dead_threshold);
2190*4882a593Smuzhiyun CONFIGFS_ATTR(o2hb_heartbeat_group_, mode);
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
2193*4882a593Smuzhiyun 	&o2hb_heartbeat_group_attr_dead_threshold,
2194*4882a593Smuzhiyun 	&o2hb_heartbeat_group_attr_mode,
2195*4882a593Smuzhiyun 	NULL,
2196*4882a593Smuzhiyun };
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
2199*4882a593Smuzhiyun 	.make_item	= o2hb_heartbeat_group_make_item,
2200*4882a593Smuzhiyun 	.drop_item	= o2hb_heartbeat_group_drop_item,
2201*4882a593Smuzhiyun };
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun static const struct config_item_type o2hb_heartbeat_group_type = {
2204*4882a593Smuzhiyun 	.ct_group_ops	= &o2hb_heartbeat_group_group_ops,
2205*4882a593Smuzhiyun 	.ct_attrs	= o2hb_heartbeat_group_attrs,
2206*4882a593Smuzhiyun 	.ct_owner	= THIS_MODULE,
2207*4882a593Smuzhiyun };
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun /* this is just here to avoid touching group in heartbeat.h which the
2210*4882a593Smuzhiyun  * entire damn world #includes */
o2hb_alloc_hb_set(void)2211*4882a593Smuzhiyun struct config_group *o2hb_alloc_hb_set(void)
2212*4882a593Smuzhiyun {
2213*4882a593Smuzhiyun 	struct o2hb_heartbeat_group *hs = NULL;
2214*4882a593Smuzhiyun 	struct config_group *ret = NULL;
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
2217*4882a593Smuzhiyun 	if (hs == NULL)
2218*4882a593Smuzhiyun 		goto out;
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun 	config_group_init_type_name(&hs->hs_group, "heartbeat",
2221*4882a593Smuzhiyun 				    &o2hb_heartbeat_group_type);
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	ret = &hs->hs_group;
2224*4882a593Smuzhiyun out:
2225*4882a593Smuzhiyun 	if (ret == NULL)
2226*4882a593Smuzhiyun 		kfree(hs);
2227*4882a593Smuzhiyun 	return ret;
2228*4882a593Smuzhiyun }
2229*4882a593Smuzhiyun 
o2hb_free_hb_set(struct config_group * group)2230*4882a593Smuzhiyun void o2hb_free_hb_set(struct config_group *group)
2231*4882a593Smuzhiyun {
2232*4882a593Smuzhiyun 	struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
2233*4882a593Smuzhiyun 	kfree(hs);
2234*4882a593Smuzhiyun }
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun /* hb callback registration and issuing */
2237*4882a593Smuzhiyun 
hbcall_from_type(enum o2hb_callback_type type)2238*4882a593Smuzhiyun static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
2239*4882a593Smuzhiyun {
2240*4882a593Smuzhiyun 	if (type == O2HB_NUM_CB)
2241*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	return &o2hb_callbacks[type];
2244*4882a593Smuzhiyun }
2245*4882a593Smuzhiyun 
o2hb_setup_callback(struct o2hb_callback_func * hc,enum o2hb_callback_type type,o2hb_cb_func * func,void * data,int priority)2246*4882a593Smuzhiyun void o2hb_setup_callback(struct o2hb_callback_func *hc,
2247*4882a593Smuzhiyun 			 enum o2hb_callback_type type,
2248*4882a593Smuzhiyun 			 o2hb_cb_func *func,
2249*4882a593Smuzhiyun 			 void *data,
2250*4882a593Smuzhiyun 			 int priority)
2251*4882a593Smuzhiyun {
2252*4882a593Smuzhiyun 	INIT_LIST_HEAD(&hc->hc_item);
2253*4882a593Smuzhiyun 	hc->hc_func = func;
2254*4882a593Smuzhiyun 	hc->hc_data = data;
2255*4882a593Smuzhiyun 	hc->hc_priority = priority;
2256*4882a593Smuzhiyun 	hc->hc_type = type;
2257*4882a593Smuzhiyun 	hc->hc_magic = O2HB_CB_MAGIC;
2258*4882a593Smuzhiyun }
2259*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_setup_callback);
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun /*
2262*4882a593Smuzhiyun  * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2263*4882a593Smuzhiyun  * In global heartbeat mode, region_uuid passed is NULL.
2264*4882a593Smuzhiyun  *
2265*4882a593Smuzhiyun  * In local, we only pin the matching region. In global we pin all the active
2266*4882a593Smuzhiyun  * regions.
2267*4882a593Smuzhiyun  */
o2hb_region_pin(const char * region_uuid)2268*4882a593Smuzhiyun static int o2hb_region_pin(const char *region_uuid)
2269*4882a593Smuzhiyun {
2270*4882a593Smuzhiyun 	int ret = 0, found = 0;
2271*4882a593Smuzhiyun 	struct o2hb_region *reg;
2272*4882a593Smuzhiyun 	char *uuid;
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 	assert_spin_locked(&o2hb_live_lock);
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2277*4882a593Smuzhiyun 		if (reg->hr_item_dropped)
2278*4882a593Smuzhiyun 			continue;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 		uuid = config_item_name(&reg->hr_item);
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 		/* local heartbeat */
2283*4882a593Smuzhiyun 		if (region_uuid) {
2284*4882a593Smuzhiyun 			if (strcmp(region_uuid, uuid))
2285*4882a593Smuzhiyun 				continue;
2286*4882a593Smuzhiyun 			found = 1;
2287*4882a593Smuzhiyun 		}
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 		if (reg->hr_item_pinned || reg->hr_item_dropped)
2290*4882a593Smuzhiyun 			goto skip_pin;
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 		/* Ignore ENOENT only for local hb (userdlm domain) */
2293*4882a593Smuzhiyun 		ret = o2nm_depend_item(&reg->hr_item);
2294*4882a593Smuzhiyun 		if (!ret) {
2295*4882a593Smuzhiyun 			mlog(ML_CLUSTER, "Pin region %s\n", uuid);
2296*4882a593Smuzhiyun 			reg->hr_item_pinned = 1;
2297*4882a593Smuzhiyun 		} else {
2298*4882a593Smuzhiyun 			if (ret == -ENOENT && found)
2299*4882a593Smuzhiyun 				ret = 0;
2300*4882a593Smuzhiyun 			else {
2301*4882a593Smuzhiyun 				mlog(ML_ERROR, "Pin region %s fails with %d\n",
2302*4882a593Smuzhiyun 				     uuid, ret);
2303*4882a593Smuzhiyun 				break;
2304*4882a593Smuzhiyun 			}
2305*4882a593Smuzhiyun 		}
2306*4882a593Smuzhiyun skip_pin:
2307*4882a593Smuzhiyun 		if (found)
2308*4882a593Smuzhiyun 			break;
2309*4882a593Smuzhiyun 	}
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun 	return ret;
2312*4882a593Smuzhiyun }
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun /*
2315*4882a593Smuzhiyun  * In local heartbeat mode, region_uuid passed matches the dlm domain name.
2316*4882a593Smuzhiyun  * In global heartbeat mode, region_uuid passed is NULL.
2317*4882a593Smuzhiyun  *
2318*4882a593Smuzhiyun  * In local, we only unpin the matching region. In global we unpin all the
2319*4882a593Smuzhiyun  * active regions.
2320*4882a593Smuzhiyun  */
o2hb_region_unpin(const char * region_uuid)2321*4882a593Smuzhiyun static void o2hb_region_unpin(const char *region_uuid)
2322*4882a593Smuzhiyun {
2323*4882a593Smuzhiyun 	struct o2hb_region *reg;
2324*4882a593Smuzhiyun 	char *uuid;
2325*4882a593Smuzhiyun 	int found = 0;
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	assert_spin_locked(&o2hb_live_lock);
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2330*4882a593Smuzhiyun 		if (reg->hr_item_dropped)
2331*4882a593Smuzhiyun 			continue;
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 		uuid = config_item_name(&reg->hr_item);
2334*4882a593Smuzhiyun 		if (region_uuid) {
2335*4882a593Smuzhiyun 			if (strcmp(region_uuid, uuid))
2336*4882a593Smuzhiyun 				continue;
2337*4882a593Smuzhiyun 			found = 1;
2338*4882a593Smuzhiyun 		}
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun 		if (reg->hr_item_pinned) {
2341*4882a593Smuzhiyun 			mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
2342*4882a593Smuzhiyun 			o2nm_undepend_item(&reg->hr_item);
2343*4882a593Smuzhiyun 			reg->hr_item_pinned = 0;
2344*4882a593Smuzhiyun 		}
2345*4882a593Smuzhiyun 		if (found)
2346*4882a593Smuzhiyun 			break;
2347*4882a593Smuzhiyun 	}
2348*4882a593Smuzhiyun }
2349*4882a593Smuzhiyun 
o2hb_region_inc_user(const char * region_uuid)2350*4882a593Smuzhiyun static int o2hb_region_inc_user(const char *region_uuid)
2351*4882a593Smuzhiyun {
2352*4882a593Smuzhiyun 	int ret = 0;
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2355*4882a593Smuzhiyun 
2356*4882a593Smuzhiyun 	/* local heartbeat */
2357*4882a593Smuzhiyun 	if (!o2hb_global_heartbeat_active()) {
2358*4882a593Smuzhiyun 	    ret = o2hb_region_pin(region_uuid);
2359*4882a593Smuzhiyun 	    goto unlock;
2360*4882a593Smuzhiyun 	}
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun 	/*
2363*4882a593Smuzhiyun 	 * if global heartbeat active and this is the first dependent user,
2364*4882a593Smuzhiyun 	 * pin all regions if quorum region count <= CUT_OFF
2365*4882a593Smuzhiyun 	 */
2366*4882a593Smuzhiyun 	o2hb_dependent_users++;
2367*4882a593Smuzhiyun 	if (o2hb_dependent_users > 1)
2368*4882a593Smuzhiyun 		goto unlock;
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun 	if (bitmap_weight(o2hb_quorum_region_bitmap,
2371*4882a593Smuzhiyun 			   O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
2372*4882a593Smuzhiyun 		ret = o2hb_region_pin(NULL);
2373*4882a593Smuzhiyun 
2374*4882a593Smuzhiyun unlock:
2375*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2376*4882a593Smuzhiyun 	return ret;
2377*4882a593Smuzhiyun }
2378*4882a593Smuzhiyun 
o2hb_region_dec_user(const char * region_uuid)2379*4882a593Smuzhiyun static void o2hb_region_dec_user(const char *region_uuid)
2380*4882a593Smuzhiyun {
2381*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 	/* local heartbeat */
2384*4882a593Smuzhiyun 	if (!o2hb_global_heartbeat_active()) {
2385*4882a593Smuzhiyun 	    o2hb_region_unpin(region_uuid);
2386*4882a593Smuzhiyun 	    goto unlock;
2387*4882a593Smuzhiyun 	}
2388*4882a593Smuzhiyun 
2389*4882a593Smuzhiyun 	/*
2390*4882a593Smuzhiyun 	 * if global heartbeat active and there are no dependent users,
2391*4882a593Smuzhiyun 	 * unpin all quorum regions
2392*4882a593Smuzhiyun 	 */
2393*4882a593Smuzhiyun 	o2hb_dependent_users--;
2394*4882a593Smuzhiyun 	if (!o2hb_dependent_users)
2395*4882a593Smuzhiyun 		o2hb_region_unpin(NULL);
2396*4882a593Smuzhiyun 
2397*4882a593Smuzhiyun unlock:
2398*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2399*4882a593Smuzhiyun }
2400*4882a593Smuzhiyun 
o2hb_register_callback(const char * region_uuid,struct o2hb_callback_func * hc)2401*4882a593Smuzhiyun int o2hb_register_callback(const char *region_uuid,
2402*4882a593Smuzhiyun 			   struct o2hb_callback_func *hc)
2403*4882a593Smuzhiyun {
2404*4882a593Smuzhiyun 	struct o2hb_callback_func *f;
2405*4882a593Smuzhiyun 	struct o2hb_callback *hbcall;
2406*4882a593Smuzhiyun 	int ret;
2407*4882a593Smuzhiyun 
2408*4882a593Smuzhiyun 	BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2409*4882a593Smuzhiyun 	BUG_ON(!list_empty(&hc->hc_item));
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 	hbcall = hbcall_from_type(hc->hc_type);
2412*4882a593Smuzhiyun 	if (IS_ERR(hbcall)) {
2413*4882a593Smuzhiyun 		ret = PTR_ERR(hbcall);
2414*4882a593Smuzhiyun 		goto out;
2415*4882a593Smuzhiyun 	}
2416*4882a593Smuzhiyun 
2417*4882a593Smuzhiyun 	if (region_uuid) {
2418*4882a593Smuzhiyun 		ret = o2hb_region_inc_user(region_uuid);
2419*4882a593Smuzhiyun 		if (ret) {
2420*4882a593Smuzhiyun 			mlog_errno(ret);
2421*4882a593Smuzhiyun 			goto out;
2422*4882a593Smuzhiyun 		}
2423*4882a593Smuzhiyun 	}
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 	down_write(&o2hb_callback_sem);
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun 	list_for_each_entry(f, &hbcall->list, hc_item) {
2428*4882a593Smuzhiyun 		if (hc->hc_priority < f->hc_priority) {
2429*4882a593Smuzhiyun 			list_add_tail(&hc->hc_item, &f->hc_item);
2430*4882a593Smuzhiyun 			break;
2431*4882a593Smuzhiyun 		}
2432*4882a593Smuzhiyun 	}
2433*4882a593Smuzhiyun 	if (list_empty(&hc->hc_item))
2434*4882a593Smuzhiyun 		list_add_tail(&hc->hc_item, &hbcall->list);
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun 	up_write(&o2hb_callback_sem);
2437*4882a593Smuzhiyun 	ret = 0;
2438*4882a593Smuzhiyun out:
2439*4882a593Smuzhiyun 	mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
2440*4882a593Smuzhiyun 	     ret, __builtin_return_address(0), hc);
2441*4882a593Smuzhiyun 	return ret;
2442*4882a593Smuzhiyun }
2443*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_register_callback);
2444*4882a593Smuzhiyun 
o2hb_unregister_callback(const char * region_uuid,struct o2hb_callback_func * hc)2445*4882a593Smuzhiyun void o2hb_unregister_callback(const char *region_uuid,
2446*4882a593Smuzhiyun 			      struct o2hb_callback_func *hc)
2447*4882a593Smuzhiyun {
2448*4882a593Smuzhiyun 	BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 	mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
2451*4882a593Smuzhiyun 	     __builtin_return_address(0), hc);
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun 	/* XXX Can this happen _with_ a region reference? */
2454*4882a593Smuzhiyun 	if (list_empty(&hc->hc_item))
2455*4882a593Smuzhiyun 		return;
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	if (region_uuid)
2458*4882a593Smuzhiyun 		o2hb_region_dec_user(region_uuid);
2459*4882a593Smuzhiyun 
2460*4882a593Smuzhiyun 	down_write(&o2hb_callback_sem);
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 	list_del_init(&hc->hc_item);
2463*4882a593Smuzhiyun 
2464*4882a593Smuzhiyun 	up_write(&o2hb_callback_sem);
2465*4882a593Smuzhiyun }
2466*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
2467*4882a593Smuzhiyun 
o2hb_check_node_heartbeating_no_sem(u8 node_num)2468*4882a593Smuzhiyun int o2hb_check_node_heartbeating_no_sem(u8 node_num)
2469*4882a593Smuzhiyun {
2470*4882a593Smuzhiyun 	unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2473*4882a593Smuzhiyun 	o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2474*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2475*4882a593Smuzhiyun 	if (!test_bit(node_num, testing_map)) {
2476*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT,
2477*4882a593Smuzhiyun 		     "node (%u) does not have heartbeating enabled.\n",
2478*4882a593Smuzhiyun 		     node_num);
2479*4882a593Smuzhiyun 		return 0;
2480*4882a593Smuzhiyun 	}
2481*4882a593Smuzhiyun 
2482*4882a593Smuzhiyun 	return 1;
2483*4882a593Smuzhiyun }
2484*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_no_sem);
2485*4882a593Smuzhiyun 
o2hb_check_node_heartbeating_from_callback(u8 node_num)2486*4882a593Smuzhiyun int o2hb_check_node_heartbeating_from_callback(u8 node_num)
2487*4882a593Smuzhiyun {
2488*4882a593Smuzhiyun 	unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
2491*4882a593Smuzhiyun 	if (!test_bit(node_num, testing_map)) {
2492*4882a593Smuzhiyun 		mlog(ML_HEARTBEAT,
2493*4882a593Smuzhiyun 		     "node (%u) does not have heartbeating enabled.\n",
2494*4882a593Smuzhiyun 		     node_num);
2495*4882a593Smuzhiyun 		return 0;
2496*4882a593Smuzhiyun 	}
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	return 1;
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun /*
2503*4882a593Smuzhiyun  * this is just a hack until we get the plumbing which flips file systems
2504*4882a593Smuzhiyun  * read only and drops the hb ref instead of killing the node dead.
2505*4882a593Smuzhiyun  */
o2hb_stop_all_regions(void)2506*4882a593Smuzhiyun void o2hb_stop_all_regions(void)
2507*4882a593Smuzhiyun {
2508*4882a593Smuzhiyun 	struct o2hb_region *reg;
2509*4882a593Smuzhiyun 
2510*4882a593Smuzhiyun 	mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
2515*4882a593Smuzhiyun 		reg->hr_unclean_stop = 1;
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2518*4882a593Smuzhiyun }
2519*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
2520*4882a593Smuzhiyun 
o2hb_get_all_regions(char * region_uuids,u8 max_regions)2521*4882a593Smuzhiyun int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
2522*4882a593Smuzhiyun {
2523*4882a593Smuzhiyun 	struct o2hb_region *reg;
2524*4882a593Smuzhiyun 	int numregs = 0;
2525*4882a593Smuzhiyun 	char *p;
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	spin_lock(&o2hb_live_lock);
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	p = region_uuids;
2530*4882a593Smuzhiyun 	list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
2531*4882a593Smuzhiyun 		if (reg->hr_item_dropped)
2532*4882a593Smuzhiyun 			continue;
2533*4882a593Smuzhiyun 
2534*4882a593Smuzhiyun 		mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
2535*4882a593Smuzhiyun 		if (numregs < max_regions) {
2536*4882a593Smuzhiyun 			memcpy(p, config_item_name(&reg->hr_item),
2537*4882a593Smuzhiyun 			       O2HB_MAX_REGION_NAME_LEN);
2538*4882a593Smuzhiyun 			p += O2HB_MAX_REGION_NAME_LEN;
2539*4882a593Smuzhiyun 		}
2540*4882a593Smuzhiyun 		numregs++;
2541*4882a593Smuzhiyun 	}
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	spin_unlock(&o2hb_live_lock);
2544*4882a593Smuzhiyun 
2545*4882a593Smuzhiyun 	return numregs;
2546*4882a593Smuzhiyun }
2547*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
2548*4882a593Smuzhiyun 
o2hb_global_heartbeat_active(void)2549*4882a593Smuzhiyun int o2hb_global_heartbeat_active(void)
2550*4882a593Smuzhiyun {
2551*4882a593Smuzhiyun 	return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun EXPORT_SYMBOL(o2hb_global_heartbeat_active);
2554