Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[sfrench/cifs-2.6.git] / fs / ocfs2 / dlm / dlmcommon.h
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmcommon.h
5  *
6  * Copyright (C) 2004 Oracle.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 021110-1307, USA.
22  *
23  */
24
25 #ifndef DLMCOMMON_H
26 #define DLMCOMMON_H
27
28 #include <linux/kref.h>
29
30 #define DLM_HB_NODE_DOWN_PRI     (0xf000000)
31 #define DLM_HB_NODE_UP_PRI       (0x8000000)
32
33 #define DLM_LOCKID_NAME_MAX    32
34
35 #define DLM_DOMAIN_NAME_MAX_LEN    255
36 #define DLM_LOCK_RES_OWNER_UNKNOWN     O2NM_MAX_NODES
37 #define DLM_THREAD_SHUFFLE_INTERVAL    5     // flush everything every 5 passes
38 #define DLM_THREAD_MS                  200   // flush at least every 200 ms
39
40 #define DLM_HASH_SIZE_DEFAULT   (1 << 14)
41 #if DLM_HASH_SIZE_DEFAULT < PAGE_SIZE
42 # define DLM_HASH_PAGES         1
43 #else
44 # define DLM_HASH_PAGES         (DLM_HASH_SIZE_DEFAULT / PAGE_SIZE)
45 #endif
46 #define DLM_BUCKETS_PER_PAGE    (PAGE_SIZE / sizeof(struct hlist_head))
47 #define DLM_HASH_BUCKETS        (DLM_HASH_PAGES * DLM_BUCKETS_PER_PAGE)
48
49 /* Intended to make it easier for us to switch out hash functions */
50 #define dlm_lockid_hash(_n, _l) full_name_hash(_n, _l)
51
52 enum dlm_ast_type {
53         DLM_AST = 0,
54         DLM_BAST,
55         DLM_ASTUNLOCK
56 };
57
58
59 #define LKM_VALID_FLAGS (LKM_VALBLK | LKM_CONVERT | LKM_UNLOCK | \
60                          LKM_CANCEL | LKM_INVVALBLK | LKM_FORCE | \
61                          LKM_RECOVERY | LKM_LOCAL | LKM_NOQUEUE)
62
63 #define DLM_RECOVERY_LOCK_NAME       "$RECOVERY"
64 #define DLM_RECOVERY_LOCK_NAME_LEN   9
65
66 static inline int dlm_is_recovery_lock(const char *lock_name, int name_len)
67 {
68         if (name_len == DLM_RECOVERY_LOCK_NAME_LEN &&
69             memcmp(lock_name, DLM_RECOVERY_LOCK_NAME, name_len)==0)
70                 return 1;
71         return 0;
72 }
73
74 #define DLM_RECO_STATE_ACTIVE    0x0001
75 #define DLM_RECO_STATE_FINALIZE  0x0002
76
77 struct dlm_recovery_ctxt
78 {
79         struct list_head resources;
80         struct list_head received;
81         struct list_head node_data;
82         u8  new_master;
83         u8  dead_node;
84         u16 state;
85         unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
86         wait_queue_head_t event;
87 };
88
89 enum dlm_ctxt_state {
90         DLM_CTXT_NEW = 0,
91         DLM_CTXT_JOINED,
92         DLM_CTXT_IN_SHUTDOWN,
93         DLM_CTXT_LEAVING,
94 };
95
96 struct dlm_ctxt
97 {
98         struct list_head list;
99         struct hlist_head **lockres_hash;
100         struct list_head dirty_list;
101         struct list_head purge_list;
102         struct list_head pending_asts;
103         struct list_head pending_basts;
104         unsigned int purge_count;
105         spinlock_t spinlock;
106         spinlock_t ast_lock;
107         char *name;
108         u8 node_num;
109         u32 key;
110         u8  joining_node;
111         wait_queue_head_t dlm_join_events;
112         unsigned long live_nodes_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
113         unsigned long domain_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
114         unsigned long recovery_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
115         struct dlm_recovery_ctxt reco;
116         spinlock_t master_lock;
117         struct list_head master_list;
118         struct list_head mle_hb_events;
119
120         /* these give a really vague idea of the system load */
121         atomic_t local_resources;
122         atomic_t remote_resources;
123         atomic_t unknown_resources;
124
125         /* NOTE: Next three are protected by dlm_domain_lock */
126         struct kref dlm_refs;
127         enum dlm_ctxt_state dlm_state;
128         unsigned int num_joins;
129
130         struct o2hb_callback_func dlm_hb_up;
131         struct o2hb_callback_func dlm_hb_down;
132         struct task_struct *dlm_thread_task;
133         struct task_struct *dlm_reco_thread_task;
134         struct workqueue_struct *dlm_worker;
135         wait_queue_head_t dlm_thread_wq;
136         wait_queue_head_t dlm_reco_thread_wq;
137         wait_queue_head_t ast_wq;
138         wait_queue_head_t migration_wq;
139
140         struct work_struct dispatched_work;
141         struct list_head work_list;
142         spinlock_t work_lock;
143         struct list_head dlm_domain_handlers;
144         struct list_head        dlm_eviction_callbacks;
145
146         /* The filesystem specifies this at domain registration.  We
147          * cache it here to know what to tell other nodes. */
148         struct dlm_protocol_version fs_locking_proto;
149         /* This is the inter-dlm communication version */
150         struct dlm_protocol_version dlm_locking_proto;
151 };
152
153 static inline struct hlist_head *dlm_lockres_hash(struct dlm_ctxt *dlm, unsigned i)
154 {
155         return dlm->lockres_hash[(i / DLM_BUCKETS_PER_PAGE) % DLM_HASH_PAGES] + (i % DLM_BUCKETS_PER_PAGE);
156 }
157
158 /* these keventd work queue items are for less-frequently
159  * called functions that cannot be directly called from the
160  * net message handlers for some reason, usually because
161  * they need to send net messages of their own. */
162 void dlm_dispatch_work(struct work_struct *work);
163
164 struct dlm_lock_resource;
165 struct dlm_work_item;
166
167 typedef void (dlm_workfunc_t)(struct dlm_work_item *, void *);
168
169 struct dlm_request_all_locks_priv
170 {
171         u8 reco_master;
172         u8 dead_node;
173 };
174
175 struct dlm_mig_lockres_priv
176 {
177         struct dlm_lock_resource *lockres;
178         u8 real_master;
179         u8 extra_ref;
180 };
181
182 struct dlm_assert_master_priv
183 {
184         struct dlm_lock_resource *lockres;
185         u8 request_from;
186         u32 flags;
187         unsigned ignore_higher:1;
188 };
189
190 struct dlm_deref_lockres_priv
191 {
192         struct dlm_lock_resource *deref_res;
193         u8 deref_node;
194 };
195
196 struct dlm_work_item
197 {
198         struct list_head list;
199         dlm_workfunc_t *func;
200         struct dlm_ctxt *dlm;
201         void *data;
202         union {
203                 struct dlm_request_all_locks_priv ral;
204                 struct dlm_mig_lockres_priv ml;
205                 struct dlm_assert_master_priv am;
206                 struct dlm_deref_lockres_priv dl;
207         } u;
208 };
209
210 static inline void dlm_init_work_item(struct dlm_ctxt *dlm,
211                                       struct dlm_work_item *i,
212                                       dlm_workfunc_t *f, void *data)
213 {
214         memset(i, 0, sizeof(*i));
215         i->func = f;
216         INIT_LIST_HEAD(&i->list);
217         i->data = data;
218         i->dlm = dlm;  /* must have already done a dlm_grab on this! */
219 }
220
221
222
223 static inline void __dlm_set_joining_node(struct dlm_ctxt *dlm,
224                                           u8 node)
225 {
226         assert_spin_locked(&dlm->spinlock);
227
228         dlm->joining_node = node;
229         wake_up(&dlm->dlm_join_events);
230 }
231
232 #define DLM_LOCK_RES_UNINITED             0x00000001
233 #define DLM_LOCK_RES_RECOVERING           0x00000002
234 #define DLM_LOCK_RES_READY                0x00000004
235 #define DLM_LOCK_RES_DIRTY                0x00000008
236 #define DLM_LOCK_RES_IN_PROGRESS          0x00000010
237 #define DLM_LOCK_RES_MIGRATING            0x00000020
238 #define DLM_LOCK_RES_DROPPING_REF         0x00000040
239 #define DLM_LOCK_RES_BLOCK_DIRTY          0x00001000
240 #define DLM_LOCK_RES_SETREF_INPROG        0x00002000
241
242 /* max milliseconds to wait to sync up a network failure with a node death */
243 #define DLM_NODE_DEATH_WAIT_MAX (5 * 1000)
244
245 #define DLM_PURGE_INTERVAL_MS   (8 * 1000)
246
247 struct dlm_lock_resource
248 {
249         /* WARNING: Please see the comment in dlm_init_lockres before
250          * adding fields here. */
251         struct hlist_node hash_node;
252         struct qstr lockname;
253         struct kref      refs;
254
255         /*
256          * Please keep granted, converting, and blocked in this order,
257          * as some funcs want to iterate over all lists.
258          *
259          * All four lists are protected by the hash's reference.
260          */
261         struct list_head granted;
262         struct list_head converting;
263         struct list_head blocked;
264         struct list_head purge;
265
266         /*
267          * These two lists require you to hold an additional reference
268          * while they are on the list.
269          */
270         struct list_head dirty;
271         struct list_head recovering; // dlm_recovery_ctxt.resources list
272
273         /* unused lock resources have their last_used stamped and are
274          * put on a list for the dlm thread to run. */
275         unsigned long    last_used;
276
277         unsigned migration_pending:1;
278         atomic_t asts_reserved;
279         spinlock_t spinlock;
280         wait_queue_head_t wq;
281         u8  owner;              //node which owns the lock resource, or unknown
282         u16 state;
283         char lvb[DLM_LVB_LEN];
284         unsigned int inflight_locks;
285         unsigned long refmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
286 };
287
288 struct dlm_migratable_lock
289 {
290         __be64 cookie;
291
292         /* these 3 are just padding for the in-memory structure, but
293          * list and flags are actually used when sent over the wire */
294         __be16 pad1;
295         u8 list;  // 0=granted, 1=converting, 2=blocked
296         u8 flags;
297
298         s8 type;
299         s8 convert_type;
300         s8 highest_blocked;
301         u8 node;
302 };  // 16 bytes
303
304 struct dlm_lock
305 {
306         struct dlm_migratable_lock ml;
307
308         struct list_head list;
309         struct list_head ast_list;
310         struct list_head bast_list;
311         struct dlm_lock_resource *lockres;
312         spinlock_t spinlock;
313         struct kref lock_refs;
314
315         // ast and bast must be callable while holding a spinlock!
316         dlm_astlockfunc_t *ast;
317         dlm_bastlockfunc_t *bast;
318         void *astdata;
319         struct dlm_lockstatus *lksb;
320         unsigned ast_pending:1,
321                  bast_pending:1,
322                  convert_pending:1,
323                  lock_pending:1,
324                  cancel_pending:1,
325                  unlock_pending:1,
326                  lksb_kernel_allocated:1;
327 };
328
329
330 #define DLM_LKSB_UNUSED1           0x01
331 #define DLM_LKSB_PUT_LVB           0x02
332 #define DLM_LKSB_GET_LVB           0x04
333 #define DLM_LKSB_UNUSED2           0x08
334 #define DLM_LKSB_UNUSED3           0x10
335 #define DLM_LKSB_UNUSED4           0x20
336 #define DLM_LKSB_UNUSED5           0x40
337 #define DLM_LKSB_UNUSED6           0x80
338
339
340 enum dlm_lockres_list {
341         DLM_GRANTED_LIST = 0,
342         DLM_CONVERTING_LIST,
343         DLM_BLOCKED_LIST
344 };
345
346 static inline int dlm_lvb_is_empty(char *lvb)
347 {
348         int i;
349         for (i=0; i<DLM_LVB_LEN; i++)
350                 if (lvb[i])
351                         return 0;
352         return 1;
353 }
354
355 static inline struct list_head *
356 dlm_list_idx_to_ptr(struct dlm_lock_resource *res, enum dlm_lockres_list idx)
357 {
358         struct list_head *ret = NULL;
359         if (idx == DLM_GRANTED_LIST)
360                 ret = &res->granted;
361         else if (idx == DLM_CONVERTING_LIST)
362                 ret = &res->converting;
363         else if (idx == DLM_BLOCKED_LIST)
364                 ret = &res->blocked;
365         else
366                 BUG();
367         return ret;
368 }
369
370
371
372
373 struct dlm_node_iter
374 {
375         unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
376         int curnode;
377 };
378
379
380 enum {
381         DLM_MASTER_REQUEST_MSG    = 500,
382         DLM_UNUSED_MSG1,         /* 501 */
383         DLM_ASSERT_MASTER_MSG,   /* 502 */
384         DLM_CREATE_LOCK_MSG,     /* 503 */
385         DLM_CONVERT_LOCK_MSG,    /* 504 */
386         DLM_PROXY_AST_MSG,       /* 505 */
387         DLM_UNLOCK_LOCK_MSG,     /* 506 */
388         DLM_DEREF_LOCKRES_MSG,   /* 507 */
389         DLM_MIGRATE_REQUEST_MSG, /* 508 */
390         DLM_MIG_LOCKRES_MSG,     /* 509 */
391         DLM_QUERY_JOIN_MSG,      /* 510 */
392         DLM_ASSERT_JOINED_MSG,   /* 511 */
393         DLM_CANCEL_JOIN_MSG,     /* 512 */
394         DLM_EXIT_DOMAIN_MSG,     /* 513 */
395         DLM_MASTER_REQUERY_MSG,  /* 514 */
396         DLM_LOCK_REQUEST_MSG,    /* 515 */
397         DLM_RECO_DATA_DONE_MSG,  /* 516 */
398         DLM_BEGIN_RECO_MSG,      /* 517 */
399         DLM_FINALIZE_RECO_MSG    /* 518 */
400 };
401
402 struct dlm_reco_node_data
403 {
404         int state;
405         u8 node_num;
406         struct list_head list;
407 };
408
409 enum {
410         DLM_RECO_NODE_DATA_DEAD = -1,
411         DLM_RECO_NODE_DATA_INIT = 0,
412         DLM_RECO_NODE_DATA_REQUESTING,
413         DLM_RECO_NODE_DATA_REQUESTED,
414         DLM_RECO_NODE_DATA_RECEIVING,
415         DLM_RECO_NODE_DATA_DONE,
416         DLM_RECO_NODE_DATA_FINALIZE_SENT,
417 };
418
419
420 enum {
421         DLM_MASTER_RESP_NO = 0,
422         DLM_MASTER_RESP_YES,
423         DLM_MASTER_RESP_MAYBE,
424         DLM_MASTER_RESP_ERROR
425 };
426
427
428 struct dlm_master_request
429 {
430         u8 node_idx;
431         u8 namelen;
432         __be16 pad1;
433         __be32 flags;
434
435         u8 name[O2NM_MAX_NAME_LEN];
436 };
437
438 #define DLM_ASSERT_RESPONSE_REASSERT       0x00000001
439 #define DLM_ASSERT_RESPONSE_MASTERY_REF    0x00000002
440
441 #define DLM_ASSERT_MASTER_MLE_CLEANUP      0x00000001
442 #define DLM_ASSERT_MASTER_REQUERY          0x00000002
443 #define DLM_ASSERT_MASTER_FINISH_MIGRATION 0x00000004
444 struct dlm_assert_master
445 {
446         u8 node_idx;
447         u8 namelen;
448         __be16 pad1;
449         __be32 flags;
450
451         u8 name[O2NM_MAX_NAME_LEN];
452 };
453
454 #define DLM_MIGRATE_RESPONSE_MASTERY_REF   0x00000001
455
456 struct dlm_migrate_request
457 {
458         u8 master;
459         u8 new_master;
460         u8 namelen;
461         u8 pad1;
462         __be32 pad2;
463         u8 name[O2NM_MAX_NAME_LEN];
464 };
465
466 struct dlm_master_requery
467 {
468         u8 pad1;
469         u8 pad2;
470         u8 node_idx;
471         u8 namelen;
472         __be32 pad3;
473         u8 name[O2NM_MAX_NAME_LEN];
474 };
475
476 #define DLM_MRES_RECOVERY   0x01
477 #define DLM_MRES_MIGRATION  0x02
478 #define DLM_MRES_ALL_DONE   0x04
479
480 /*
481  * We would like to get one whole lockres into a single network
482  * message whenever possible.  Generally speaking, there will be
483  * at most one dlm_lock on a lockres for each node in the cluster,
484  * plus (infrequently) any additional locks coming in from userdlm.
485  *
486  * struct _dlm_lockres_page
487  * {
488  *      dlm_migratable_lockres mres;
489  *      dlm_migratable_lock ml[DLM_MAX_MIGRATABLE_LOCKS];
490  *      u8 pad[DLM_MIG_LOCKRES_RESERVED];
491  * };
492  *
493  * from ../cluster/tcp.h
494  *    NET_MAX_PAYLOAD_BYTES  (4096 - sizeof(net_msg))
495  *    (roughly 4080 bytes)
496  * and sizeof(dlm_migratable_lockres) = 112 bytes
497  * and sizeof(dlm_migratable_lock) = 16 bytes
498  *
499  * Choosing DLM_MAX_MIGRATABLE_LOCKS=240 and
500  * DLM_MIG_LOCKRES_RESERVED=128 means we have this:
501  *
502  *  (DLM_MAX_MIGRATABLE_LOCKS * sizeof(dlm_migratable_lock)) +
503  *     sizeof(dlm_migratable_lockres) + DLM_MIG_LOCKRES_RESERVED =
504  *        NET_MAX_PAYLOAD_BYTES
505  *  (240 * 16) + 112 + 128 = 4080
506  *
507  * So a lockres would need more than 240 locks before it would
508  * use more than one network packet to recover.  Not too bad.
509  */
510 #define DLM_MAX_MIGRATABLE_LOCKS   240
511
512 struct dlm_migratable_lockres
513 {
514         u8 master;
515         u8 lockname_len;
516         u8 num_locks;    // locks sent in this structure
517         u8 flags;
518         __be32 total_locks; // locks to be sent for this migration cookie
519         __be64 mig_cookie;  // cookie for this lockres migration
520                          // or zero if not needed
521         // 16 bytes
522         u8 lockname[DLM_LOCKID_NAME_MAX];
523         // 48 bytes
524         u8 lvb[DLM_LVB_LEN];
525         // 112 bytes
526         struct dlm_migratable_lock ml[0];  // 16 bytes each, begins at byte 112
527 };
528 #define DLM_MIG_LOCKRES_MAX_LEN  \
529         (sizeof(struct dlm_migratable_lockres) + \
530          (sizeof(struct dlm_migratable_lock) * \
531           DLM_MAX_MIGRATABLE_LOCKS) )
532
533 /* from above, 128 bytes
534  * for some undetermined future use */
535 #define DLM_MIG_LOCKRES_RESERVED   (NET_MAX_PAYLOAD_BYTES - \
536                                     DLM_MIG_LOCKRES_MAX_LEN)
537
538 struct dlm_create_lock
539 {
540         __be64 cookie;
541
542         __be32 flags;
543         u8 pad1;
544         u8 node_idx;
545         s8 requested_type;
546         u8 namelen;
547
548         u8 name[O2NM_MAX_NAME_LEN];
549 };
550
551 struct dlm_convert_lock
552 {
553         __be64 cookie;
554
555         __be32 flags;
556         u8 pad1;
557         u8 node_idx;
558         s8 requested_type;
559         u8 namelen;
560
561         u8 name[O2NM_MAX_NAME_LEN];
562
563         s8 lvb[0];
564 };
565 #define DLM_CONVERT_LOCK_MAX_LEN  (sizeof(struct dlm_convert_lock)+DLM_LVB_LEN)
566
567 struct dlm_unlock_lock
568 {
569         __be64 cookie;
570
571         __be32 flags;
572         __be16 pad1;
573         u8 node_idx;
574         u8 namelen;
575
576         u8 name[O2NM_MAX_NAME_LEN];
577
578         s8 lvb[0];
579 };
580 #define DLM_UNLOCK_LOCK_MAX_LEN  (sizeof(struct dlm_unlock_lock)+DLM_LVB_LEN)
581
582 struct dlm_proxy_ast
583 {
584         __be64 cookie;
585
586         __be32 flags;
587         u8 node_idx;
588         u8 type;
589         u8 blocked_type;
590         u8 namelen;
591
592         u8 name[O2NM_MAX_NAME_LEN];
593
594         s8 lvb[0];
595 };
596 #define DLM_PROXY_AST_MAX_LEN  (sizeof(struct dlm_proxy_ast)+DLM_LVB_LEN)
597
598 #define DLM_MOD_KEY (0x666c6172)
599 enum dlm_query_join_response_code {
600         JOIN_DISALLOW = 0,
601         JOIN_OK,
602         JOIN_OK_NO_MAP,
603         JOIN_PROTOCOL_MISMATCH,
604 };
605
606 struct dlm_query_join_packet {
607         u8 code;        /* Response code.  dlm_minor and fs_minor
608                            are only valid if this is JOIN_OK */
609         u8 dlm_minor;   /* The minor version of the protocol the
610                            dlm is speaking. */
611         u8 fs_minor;    /* The minor version of the protocol the
612                            filesystem is speaking. */
613         u8 reserved;
614 };
615
616 union dlm_query_join_response {
617         u32 intval;
618         struct dlm_query_join_packet packet;
619 };
620
621 struct dlm_lock_request
622 {
623         u8 node_idx;
624         u8 dead_node;
625         __be16 pad1;
626         __be32 pad2;
627 };
628
629 struct dlm_reco_data_done
630 {
631         u8 node_idx;
632         u8 dead_node;
633         __be16 pad1;
634         __be32 pad2;
635
636         /* unused for now */
637         /* eventually we can use this to attempt
638          * lvb recovery based on each node's info */
639         u8 reco_lvb[DLM_LVB_LEN];
640 };
641
642 struct dlm_begin_reco
643 {
644         u8 node_idx;
645         u8 dead_node;
646         __be16 pad1;
647         __be32 pad2;
648 };
649
650
651 #define BITS_PER_BYTE 8
652 #define BITS_TO_BYTES(bits) (((bits)+BITS_PER_BYTE-1)/BITS_PER_BYTE)
653
654 struct dlm_query_join_request
655 {
656         u8 node_idx;
657         u8 pad1[2];
658         u8 name_len;
659         struct dlm_protocol_version dlm_proto;
660         struct dlm_protocol_version fs_proto;
661         u8 domain[O2NM_MAX_NAME_LEN];
662         u8 node_map[BITS_TO_BYTES(O2NM_MAX_NODES)];
663 };
664
665 struct dlm_assert_joined
666 {
667         u8 node_idx;
668         u8 pad1[2];
669         u8 name_len;
670         u8 domain[O2NM_MAX_NAME_LEN];
671 };
672
673 struct dlm_cancel_join
674 {
675         u8 node_idx;
676         u8 pad1[2];
677         u8 name_len;
678         u8 domain[O2NM_MAX_NAME_LEN];
679 };
680
681 struct dlm_exit_domain
682 {
683         u8 node_idx;
684         u8 pad1[3];
685 };
686
687 struct dlm_finalize_reco
688 {
689         u8 node_idx;
690         u8 dead_node;
691         u8 flags;
692         u8 pad1;
693         __be32 pad2;
694 };
695
696 struct dlm_deref_lockres
697 {
698         u32 pad1;
699         u16 pad2;
700         u8 node_idx;
701         u8 namelen;
702
703         u8 name[O2NM_MAX_NAME_LEN];
704 };
705
706 static inline enum dlm_status
707 __dlm_lockres_state_to_status(struct dlm_lock_resource *res)
708 {
709         enum dlm_status status = DLM_NORMAL;
710
711         assert_spin_locked(&res->spinlock);
712
713         if (res->state & DLM_LOCK_RES_RECOVERING)
714                 status = DLM_RECOVERING;
715         else if (res->state & DLM_LOCK_RES_MIGRATING)
716                 status = DLM_MIGRATING;
717         else if (res->state & DLM_LOCK_RES_IN_PROGRESS)
718                 status = DLM_FORWARD;
719
720         return status;
721 }
722
723 static inline u8 dlm_get_lock_cookie_node(u64 cookie)
724 {
725         u8 ret;
726         cookie >>= 56;
727         ret = (u8)(cookie & 0xffULL);
728         return ret;
729 }
730
731 static inline unsigned long long dlm_get_lock_cookie_seq(u64 cookie)
732 {
733         unsigned long long ret;
734         ret = ((unsigned long long)cookie) & 0x00ffffffffffffffULL;
735         return ret;
736 }
737
738 struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
739                                struct dlm_lockstatus *lksb);
740 void dlm_lock_get(struct dlm_lock *lock);
741 void dlm_lock_put(struct dlm_lock *lock);
742
743 void dlm_lock_attach_lockres(struct dlm_lock *lock,
744                              struct dlm_lock_resource *res);
745
746 int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
747                             void **ret_data);
748 int dlm_convert_lock_handler(struct o2net_msg *msg, u32 len, void *data,
749                              void **ret_data);
750 int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
751                           void **ret_data);
752
753 void dlm_revert_pending_convert(struct dlm_lock_resource *res,
754                                 struct dlm_lock *lock);
755 void dlm_revert_pending_lock(struct dlm_lock_resource *res,
756                              struct dlm_lock *lock);
757
758 int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
759                             void **ret_data);
760 void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
761                                struct dlm_lock *lock);
762 void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
763                                struct dlm_lock *lock);
764
765 int dlm_launch_thread(struct dlm_ctxt *dlm);
766 void dlm_complete_thread(struct dlm_ctxt *dlm);
767 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
768 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
769 void dlm_wait_for_recovery(struct dlm_ctxt *dlm);
770 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
771 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node);
772 int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout);
773 int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout);
774
775 void dlm_put(struct dlm_ctxt *dlm);
776 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm);
777 int dlm_domain_fully_joined(struct dlm_ctxt *dlm);
778
779 void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
780                               struct dlm_lock_resource *res);
781 void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
782                             struct dlm_lock_resource *res);
783 static inline void dlm_lockres_get(struct dlm_lock_resource *res)
784 {
785         /* This is called on every lookup, so it might be worth
786          * inlining. */
787         kref_get(&res->refs);
788 }
789 void dlm_lockres_put(struct dlm_lock_resource *res);
790 void __dlm_unhash_lockres(struct dlm_lock_resource *res);
791 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
792                           struct dlm_lock_resource *res);
793 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
794                                                      const char *name,
795                                                      unsigned int len,
796                                                      unsigned int hash);
797 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
798                                                 const char *name,
799                                                 unsigned int len,
800                                                 unsigned int hash);
801 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
802                                               const char *name,
803                                               unsigned int len);
804
805 int dlm_is_host_down(int errno);
806 void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
807                               struct dlm_lock_resource *res,
808                               u8 owner);
809 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
810                                                  const char *lockid,
811                                                  int namelen,
812                                                  int flags);
813 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
814                                           const char *name,
815                                           unsigned int namelen);
816
817 #define dlm_lockres_set_refmap_bit(bit,res)  \
818         __dlm_lockres_set_refmap_bit(bit,res,__FILE__,__LINE__)
819 #define dlm_lockres_clear_refmap_bit(bit,res)  \
820         __dlm_lockres_clear_refmap_bit(bit,res,__FILE__,__LINE__)
821
822 static inline void __dlm_lockres_set_refmap_bit(int bit,
823                                                 struct dlm_lock_resource *res,
824                                                 const char *file,
825                                                 int line)
826 {
827         //printk("%s:%d:%.*s: setting bit %d\n", file, line,
828         //     res->lockname.len, res->lockname.name, bit);
829         set_bit(bit, res->refmap);
830 }
831
832 static inline void __dlm_lockres_clear_refmap_bit(int bit,
833                                                   struct dlm_lock_resource *res,
834                                                   const char *file,
835                                                   int line)
836 {
837         //printk("%s:%d:%.*s: clearing bit %d\n", file, line,
838         //     res->lockname.len, res->lockname.name, bit);
839         clear_bit(bit, res->refmap);
840 }
841
842 void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
843                                    struct dlm_lock_resource *res,
844                                    const char *file,
845                                    int line);
846 void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
847                                    struct dlm_lock_resource *res,
848                                    int new_lockres,
849                                    const char *file,
850                                    int line);
851 #define dlm_lockres_drop_inflight_ref(d,r)  \
852         __dlm_lockres_drop_inflight_ref(d,r,__FILE__,__LINE__)
853 #define dlm_lockres_grab_inflight_ref(d,r)  \
854         __dlm_lockres_grab_inflight_ref(d,r,0,__FILE__,__LINE__)
855 #define dlm_lockres_grab_inflight_ref_new(d,r)  \
856         __dlm_lockres_grab_inflight_ref(d,r,1,__FILE__,__LINE__)
857
858 void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
859 void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
860 void dlm_do_local_ast(struct dlm_ctxt *dlm,
861                       struct dlm_lock_resource *res,
862                       struct dlm_lock *lock);
863 int dlm_do_remote_ast(struct dlm_ctxt *dlm,
864                       struct dlm_lock_resource *res,
865                       struct dlm_lock *lock);
866 void dlm_do_local_bast(struct dlm_ctxt *dlm,
867                        struct dlm_lock_resource *res,
868                        struct dlm_lock *lock,
869                        int blocked_type);
870 int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm,
871                            struct dlm_lock_resource *res,
872                            struct dlm_lock *lock,
873                            int msg_type,
874                            int blocked_type, int flags);
875 static inline int dlm_send_proxy_bast(struct dlm_ctxt *dlm,
876                                       struct dlm_lock_resource *res,
877                                       struct dlm_lock *lock,
878                                       int blocked_type)
879 {
880         return dlm_send_proxy_ast_msg(dlm, res, lock, DLM_BAST,
881                                       blocked_type, 0);
882 }
883
884 static inline int dlm_send_proxy_ast(struct dlm_ctxt *dlm,
885                                      struct dlm_lock_resource *res,
886                                      struct dlm_lock *lock,
887                                      int flags)
888 {
889         return dlm_send_proxy_ast_msg(dlm, res, lock, DLM_AST,
890                                       0, flags);
891 }
892
893 void dlm_print_one_lock_resource(struct dlm_lock_resource *res);
894 void __dlm_print_one_lock_resource(struct dlm_lock_resource *res);
895
896 u8 dlm_nm_this_node(struct dlm_ctxt *dlm);
897 void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
898 void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
899
900
901 int dlm_nm_init(struct dlm_ctxt *dlm);
902 int dlm_heartbeat_init(struct dlm_ctxt *dlm);
903 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data);
904 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data);
905
906 int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
907 int dlm_finish_migration(struct dlm_ctxt *dlm,
908                          struct dlm_lock_resource *res,
909                          u8 old_master);
910 void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
911                              struct dlm_lock_resource *res);
912 void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res);
913
914 int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
915                                void **ret_data);
916 int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
917                               void **ret_data);
918 void dlm_assert_master_post_handler(int status, void *data, void *ret_data);
919 int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
920                               void **ret_data);
921 int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
922                                 void **ret_data);
923 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
924                             void **ret_data);
925 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
926                                void **ret_data);
927 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
928                                   void **ret_data);
929 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
930                                void **ret_data);
931 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
932                            void **ret_data);
933 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
934                               void **ret_data);
935 int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
936                           u8 nodenum, u8 *real_master);
937
938
939 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
940                                struct dlm_lock_resource *res,
941                                int ignore_higher,
942                                u8 request_from,
943                                u32 flags);
944
945
946 int dlm_send_one_lockres(struct dlm_ctxt *dlm,
947                          struct dlm_lock_resource *res,
948                          struct dlm_migratable_lockres *mres,
949                          u8 send_to,
950                          u8 flags);
951 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
952                                        struct dlm_lock_resource *res);
953
954 /* will exit holding res->spinlock, but may drop in function */
955 void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags);
956 void __dlm_wait_on_lockres_flags_set(struct dlm_lock_resource *res, int flags);
957
958 /* will exit holding res->spinlock, but may drop in function */
959 static inline void __dlm_wait_on_lockres(struct dlm_lock_resource *res)
960 {
961         __dlm_wait_on_lockres_flags(res, (DLM_LOCK_RES_IN_PROGRESS|
962                                           DLM_LOCK_RES_RECOVERING|
963                                           DLM_LOCK_RES_MIGRATING));
964 }
965
966
967 int dlm_init_mle_cache(void);
968 void dlm_destroy_mle_cache(void);
969 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up);
970 int dlm_drop_lockres_ref(struct dlm_ctxt *dlm,
971                          struct dlm_lock_resource *res);
972 void dlm_clean_master_list(struct dlm_ctxt *dlm,
973                            u8 dead_node);
974 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock);
975 int __dlm_lockres_has_locks(struct dlm_lock_resource *res);
976 int __dlm_lockres_unused(struct dlm_lock_resource *res);
977
978 static inline const char * dlm_lock_mode_name(int mode)
979 {
980         switch (mode) {
981                 case LKM_EXMODE:
982                         return "EX";
983                 case LKM_PRMODE:
984                         return "PR";
985                 case LKM_NLMODE:
986                         return "NL";
987         }
988         return "UNKNOWN";
989 }
990
991
992 static inline int dlm_lock_compatible(int existing, int request)
993 {
994         /* NO_LOCK compatible with all */
995         if (request == LKM_NLMODE ||
996             existing == LKM_NLMODE)
997                 return 1;
998
999         /* EX incompatible with all non-NO_LOCK */
1000         if (request == LKM_EXMODE)
1001                 return 0;
1002
1003         /* request must be PR, which is compatible with PR */
1004         if (existing == LKM_PRMODE)
1005                 return 1;
1006
1007         return 0;
1008 }
1009
1010 static inline int dlm_lock_on_list(struct list_head *head,
1011                                    struct dlm_lock *lock)
1012 {
1013         struct list_head *iter;
1014         struct dlm_lock *tmplock;
1015
1016         list_for_each(iter, head) {
1017                 tmplock = list_entry(iter, struct dlm_lock, list);
1018                 if (tmplock == lock)
1019                         return 1;
1020         }
1021         return 0;
1022 }
1023
1024
1025 static inline enum dlm_status dlm_err_to_dlm_status(int err)
1026 {
1027         enum dlm_status ret;
1028         if (err == -ENOMEM)
1029                 ret = DLM_SYSERR;
1030         else if (err == -ETIMEDOUT || o2net_link_down(err, NULL))
1031                 ret = DLM_NOLOCKMGR;
1032         else if (err == -EINVAL)
1033                 ret = DLM_BADPARAM;
1034         else if (err == -ENAMETOOLONG)
1035                 ret = DLM_IVBUFLEN;
1036         else
1037                 ret = DLM_BADARGS;
1038         return ret;
1039 }
1040
1041
1042 static inline void dlm_node_iter_init(unsigned long *map,
1043                                       struct dlm_node_iter *iter)
1044 {
1045         memcpy(iter->node_map, map, sizeof(iter->node_map));
1046         iter->curnode = -1;
1047 }
1048
1049 static inline int dlm_node_iter_next(struct dlm_node_iter *iter)
1050 {
1051         int bit;
1052         bit = find_next_bit(iter->node_map, O2NM_MAX_NODES, iter->curnode+1);
1053         if (bit >= O2NM_MAX_NODES) {
1054                 iter->curnode = O2NM_MAX_NODES;
1055                 return -ENOENT;
1056         }
1057         iter->curnode = bit;
1058         return bit;
1059 }
1060
1061
1062
1063 #endif /* DLMCOMMON_H */