12522fe45SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e7fd4179SDavid Teigland /****************************************************************************** 3e7fd4179SDavid Teigland ******************************************************************************* 4e7fd4179SDavid Teigland ** 5e7fd4179SDavid Teigland ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 6dbcfc347SDavid Teigland ** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved. 7e7fd4179SDavid Teigland ** 8e7fd4179SDavid Teigland ** 9e7fd4179SDavid Teigland ******************************************************************************* 10e7fd4179SDavid Teigland ******************************************************************************/ 11e7fd4179SDavid Teigland 12e7fd4179SDavid Teigland #include "dlm_internal.h" 13e7fd4179SDavid Teigland #include "lockspace.h" 14e7fd4179SDavid Teigland #include "member.h" 15e7fd4179SDavid Teigland #include "lowcomms.h" 16e7fd4179SDavid Teigland #include "midcomms.h" 17e7fd4179SDavid Teigland #include "rcom.h" 18e7fd4179SDavid Teigland #include "recover.h" 19e7fd4179SDavid Teigland #include "dir.h" 20e7fd4179SDavid Teigland #include "config.h" 21e7fd4179SDavid Teigland #include "memory.h" 22e7fd4179SDavid Teigland #include "lock.h" 23e7fd4179SDavid Teigland #include "util.h" 24e7fd4179SDavid Teigland 25e7fd4179SDavid Teigland static int rcom_response(struct dlm_ls *ls) 26e7fd4179SDavid Teigland { 27e7fd4179SDavid Teigland return test_bit(LSFL_RCOM_READY, &ls->ls_flags); 28e7fd4179SDavid Teigland } 29e7fd4179SDavid Teigland 30a070a91cSAlexander Aring static void _create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len, 31a070a91cSAlexander Aring struct dlm_rcom **rc_ret, char *mb, int mb_len) 32e7fd4179SDavid Teigland { 33e7fd4179SDavid Teigland struct dlm_rcom *rc; 34e7fd4179SDavid Teigland 35e7fd4179SDavid Teigland rc = (struct dlm_rcom *) mb; 36e7fd4179SDavid Teigland 373428785aSAlexander Aring rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR); 383428785aSAlexander Aring rc->rc_header.u.h_lockspace = cpu_to_le32(ls->ls_global_id); 393428785aSAlexander Aring rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid()); 403428785aSAlexander Aring rc->rc_header.h_length = cpu_to_le16(mb_len); 41e7fd4179SDavid Teigland rc->rc_header.h_cmd = DLM_RCOM; 42e7fd4179SDavid Teigland 432f9dbedaSAlexander Aring rc->rc_type = cpu_to_le32(type); 44e7fd4179SDavid Teigland 4538aa8b0cSDavid Teigland spin_lock(&ls->ls_recover_lock); 462f9dbedaSAlexander Aring rc->rc_seq = cpu_to_le64(ls->ls_recover_seq); 4738aa8b0cSDavid Teigland spin_unlock(&ls->ls_recover_lock); 4838aa8b0cSDavid Teigland 49e7fd4179SDavid Teigland *rc_ret = rc; 50a070a91cSAlexander Aring } 51a070a91cSAlexander Aring 52a070a91cSAlexander Aring static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len, 53a070a91cSAlexander Aring struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret) 54a070a91cSAlexander Aring { 55a070a91cSAlexander Aring int mb_len = sizeof(struct dlm_rcom) + len; 56a070a91cSAlexander Aring struct dlm_mhandle *mh; 57a070a91cSAlexander Aring char *mb; 58a070a91cSAlexander Aring 59a070a91cSAlexander Aring mh = dlm_midcomms_get_mhandle(to_nodeid, mb_len, GFP_NOFS, &mb); 60a070a91cSAlexander Aring if (!mh) { 61a070a91cSAlexander Aring log_print("%s to %d type %d len %d ENOBUFS", 62a070a91cSAlexander Aring __func__, to_nodeid, type, len); 63a070a91cSAlexander Aring return -ENOBUFS; 64a070a91cSAlexander Aring } 65a070a91cSAlexander Aring 66a070a91cSAlexander Aring _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len); 67a070a91cSAlexander Aring *mh_ret = mh; 68e7fd4179SDavid Teigland return 0; 69e7fd4179SDavid Teigland } 70e7fd4179SDavid Teigland 71a070a91cSAlexander Aring static int create_rcom_stateless(struct dlm_ls *ls, int to_nodeid, int type, 72a070a91cSAlexander Aring int len, struct dlm_rcom **rc_ret, 738f2dc78dSAlexander Aring struct dlm_msg **msg_ret) 74a070a91cSAlexander Aring { 75a070a91cSAlexander Aring int mb_len = sizeof(struct dlm_rcom) + len; 768f2dc78dSAlexander Aring struct dlm_msg *msg; 77a070a91cSAlexander Aring char *mb; 78a070a91cSAlexander Aring 798f2dc78dSAlexander Aring msg = dlm_lowcomms_new_msg(to_nodeid, mb_len, GFP_NOFS, &mb, 808f2dc78dSAlexander Aring NULL, NULL); 818f2dc78dSAlexander Aring if (!msg) { 82a070a91cSAlexander Aring log_print("create_rcom to %d type %d len %d ENOBUFS", 83a070a91cSAlexander Aring to_nodeid, type, len); 84a070a91cSAlexander Aring return -ENOBUFS; 85a070a91cSAlexander Aring } 86a070a91cSAlexander Aring 87a070a91cSAlexander Aring _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len); 888f2dc78dSAlexander Aring *msg_ret = msg; 89a070a91cSAlexander Aring return 0; 90a070a91cSAlexander Aring } 91a070a91cSAlexander Aring 9288aa023aSAlexander Aring static void send_rcom(struct dlm_mhandle *mh, struct dlm_rcom *rc) 93a070a91cSAlexander Aring { 94*e01c4b7bSAlexander Aring dlm_midcomms_commit_mhandle(mh, NULL, 0); 95a070a91cSAlexander Aring } 96a070a91cSAlexander Aring 9788aa023aSAlexander Aring static void send_rcom_stateless(struct dlm_msg *msg, struct dlm_rcom *rc) 98a070a91cSAlexander Aring { 998f2dc78dSAlexander Aring dlm_lowcomms_commit_msg(msg); 1008f2dc78dSAlexander Aring dlm_lowcomms_put_msg(msg); 101e7fd4179SDavid Teigland } 102e7fd4179SDavid Teigland 103757a4271SDavid Teigland static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs, 104757a4271SDavid Teigland uint32_t flags) 105757a4271SDavid Teigland { 106757a4271SDavid Teigland rs->rs_flags = cpu_to_le32(flags); 107757a4271SDavid Teigland } 108757a4271SDavid Teigland 109e7fd4179SDavid Teigland /* When replying to a status request, a node also sends back its 110e7fd4179SDavid Teigland configuration values. The requesting node then checks that the remote 111e7fd4179SDavid Teigland node is configured the same way as itself. */ 112e7fd4179SDavid Teigland 113757a4271SDavid Teigland static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf, 114757a4271SDavid Teigland uint32_t num_slots) 115e7fd4179SDavid Teigland { 11693ff2971SAl Viro rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen); 11793ff2971SAl Viro rf->rf_lsflags = cpu_to_le32(ls->ls_exflags); 118757a4271SDavid Teigland 119757a4271SDavid Teigland rf->rf_our_slot = cpu_to_le16(ls->ls_slot); 120757a4271SDavid Teigland rf->rf_num_slots = cpu_to_le16(num_slots); 121757a4271SDavid Teigland rf->rf_generation = cpu_to_le32(ls->ls_generation); 122e7fd4179SDavid Teigland } 123e7fd4179SDavid Teigland 124757a4271SDavid Teigland static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid) 125e7fd4179SDavid Teigland { 1269e971b71SDavid Teigland struct rcom_config *rf = (struct rcom_config *) rc->rc_buf; 1279e971b71SDavid Teigland 1283428785aSAlexander Aring if ((le32_to_cpu(rc->rc_header.h_version) & 0xFFFF0000) != DLM_HEADER_MAJOR) { 1299e971b71SDavid Teigland log_error(ls, "version mismatch: %x nodeid %d: %x", 1309e971b71SDavid Teigland DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid, 1313428785aSAlexander Aring le32_to_cpu(rc->rc_header.h_version)); 1328b0e7b2cSDavid Teigland return -EPROTO; 1339e971b71SDavid Teigland } 1349e971b71SDavid Teigland 13593ff2971SAl Viro if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen || 13693ff2971SAl Viro le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) { 137e7fd4179SDavid Teigland log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x", 13893ff2971SAl Viro ls->ls_lvblen, ls->ls_exflags, nodeid, 13993ff2971SAl Viro le32_to_cpu(rf->rf_lvblen), 14093ff2971SAl Viro le32_to_cpu(rf->rf_lsflags)); 1418b0e7b2cSDavid Teigland return -EPROTO; 142e7fd4179SDavid Teigland } 143e7fd4179SDavid Teigland return 0; 144e7fd4179SDavid Teigland } 145e7fd4179SDavid Teigland 1462f9dbedaSAlexander Aring static void allow_sync_reply(struct dlm_ls *ls, __le64 *new_seq) 14798f176fbSDavid Teigland { 14898f176fbSDavid Teigland spin_lock(&ls->ls_rcom_spin); 1492f9dbedaSAlexander Aring *new_seq = cpu_to_le64(++ls->ls_rcom_seq); 15098f176fbSDavid Teigland set_bit(LSFL_RCOM_WAIT, &ls->ls_flags); 15198f176fbSDavid Teigland spin_unlock(&ls->ls_rcom_spin); 15298f176fbSDavid Teigland } 15398f176fbSDavid Teigland 15498f176fbSDavid Teigland static void disallow_sync_reply(struct dlm_ls *ls) 15598f176fbSDavid Teigland { 15698f176fbSDavid Teigland spin_lock(&ls->ls_rcom_spin); 15798f176fbSDavid Teigland clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags); 15898f176fbSDavid Teigland clear_bit(LSFL_RCOM_READY, &ls->ls_flags); 15998f176fbSDavid Teigland spin_unlock(&ls->ls_rcom_spin); 16098f176fbSDavid Teigland } 16198f176fbSDavid Teigland 162757a4271SDavid Teigland /* 163757a4271SDavid Teigland * low nodeid gathers one slot value at a time from each node. 164757a4271SDavid Teigland * it sets need_slots=0, and saves rf_our_slot returned from each 165757a4271SDavid Teigland * rcom_config. 166757a4271SDavid Teigland * 167757a4271SDavid Teigland * other nodes gather all slot values at once from the low nodeid. 168757a4271SDavid Teigland * they set need_slots=1, and ignore the rf_our_slot returned from each 169757a4271SDavid Teigland * rcom_config. they use the rf_num_slots returned from the low 170757a4271SDavid Teigland * node's rcom_config. 171757a4271SDavid Teigland */ 172757a4271SDavid Teigland 173757a4271SDavid Teigland int dlm_rcom_status(struct dlm_ls *ls, int nodeid, uint32_t status_flags) 174e7fd4179SDavid Teigland { 175e7fd4179SDavid Teigland struct dlm_rcom *rc; 1768f2dc78dSAlexander Aring struct dlm_msg *msg; 177e7fd4179SDavid Teigland int error = 0; 178e7fd4179SDavid Teigland 179faa0f267SDavid Teigland ls->ls_recover_nodeid = nodeid; 180e7fd4179SDavid Teigland 181e7fd4179SDavid Teigland if (nodeid == dlm_our_nodeid()) { 1824007685cSAl Viro rc = ls->ls_recover_buf; 1832f9dbedaSAlexander Aring rc->rc_result = cpu_to_le32(dlm_recover_status(ls)); 184e7fd4179SDavid Teigland goto out; 185e7fd4179SDavid Teigland } 186e7fd4179SDavid Teigland 18759661212Stsutomu.owa@toshiba.co.jp retry: 188a070a91cSAlexander Aring error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS, 1898f2dc78dSAlexander Aring sizeof(struct rcom_status), &rc, &msg); 190e7fd4179SDavid Teigland if (error) 191e7fd4179SDavid Teigland goto out; 19298f176fbSDavid Teigland 193757a4271SDavid Teigland set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags); 194757a4271SDavid Teigland 19598f176fbSDavid Teigland allow_sync_reply(ls, &rc->rc_id); 196d10a0b88SAlexander Aring memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE); 197e7fd4179SDavid Teigland 19888aa023aSAlexander Aring send_rcom_stateless(msg, rc); 199e7fd4179SDavid Teigland 200e7fd4179SDavid Teigland error = dlm_wait_function(ls, &rcom_response); 20198f176fbSDavid Teigland disallow_sync_reply(ls); 20259661212Stsutomu.owa@toshiba.co.jp if (error == -ETIMEDOUT) 20359661212Stsutomu.owa@toshiba.co.jp goto retry; 204e7fd4179SDavid Teigland if (error) 205e7fd4179SDavid Teigland goto out; 206e7fd4179SDavid Teigland 2074007685cSAl Viro rc = ls->ls_recover_buf; 208e7fd4179SDavid Teigland 2092f9dbedaSAlexander Aring if (rc->rc_result == cpu_to_le32(-ESRCH)) { 210e7fd4179SDavid Teigland /* we pretend the remote lockspace exists with 0 status */ 211e7fd4179SDavid Teigland log_debug(ls, "remote node %d not ready", nodeid); 212e7fd4179SDavid Teigland rc->rc_result = 0; 213757a4271SDavid Teigland error = 0; 214757a4271SDavid Teigland } else { 215757a4271SDavid Teigland error = check_rcom_config(ls, rc, nodeid); 216757a4271SDavid Teigland } 217757a4271SDavid Teigland 218e7fd4179SDavid Teigland /* the caller looks at rc_result for the remote recovery status */ 219e7fd4179SDavid Teigland out: 220e7fd4179SDavid Teigland return error; 221e7fd4179SDavid Teigland } 222e7fd4179SDavid Teigland 223e7fd4179SDavid Teigland static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in) 224e7fd4179SDavid Teigland { 225e7fd4179SDavid Teigland struct dlm_rcom *rc; 226757a4271SDavid Teigland struct rcom_status *rs; 227757a4271SDavid Teigland uint32_t status; 2283428785aSAlexander Aring int nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid); 229757a4271SDavid Teigland int len = sizeof(struct rcom_config); 2308f2dc78dSAlexander Aring struct dlm_msg *msg; 231757a4271SDavid Teigland int num_slots = 0; 232757a4271SDavid Teigland int error; 233e7fd4179SDavid Teigland 234757a4271SDavid Teigland if (!dlm_slots_version(&rc_in->rc_header)) { 235757a4271SDavid Teigland status = dlm_recover_status(ls); 236757a4271SDavid Teigland goto do_create; 237757a4271SDavid Teigland } 238757a4271SDavid Teigland 239757a4271SDavid Teigland rs = (struct rcom_status *)rc_in->rc_buf; 240757a4271SDavid Teigland 241c07127b4SNeale Ferguson if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) { 242757a4271SDavid Teigland status = dlm_recover_status(ls); 243757a4271SDavid Teigland goto do_create; 244757a4271SDavid Teigland } 245757a4271SDavid Teigland 246757a4271SDavid Teigland spin_lock(&ls->ls_recover_lock); 247757a4271SDavid Teigland status = ls->ls_recover_status; 248757a4271SDavid Teigland num_slots = ls->ls_num_slots; 249757a4271SDavid Teigland spin_unlock(&ls->ls_recover_lock); 250757a4271SDavid Teigland len += num_slots * sizeof(struct rcom_slot); 251757a4271SDavid Teigland 252757a4271SDavid Teigland do_create: 253a070a91cSAlexander Aring error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS_REPLY, 2548f2dc78dSAlexander Aring len, &rc, &msg); 255e7fd4179SDavid Teigland if (error) 256e7fd4179SDavid Teigland return; 257757a4271SDavid Teigland 2584a99c3d9SDavid Teigland rc->rc_id = rc_in->rc_id; 25938aa8b0cSDavid Teigland rc->rc_seq_reply = rc_in->rc_seq; 2602f9dbedaSAlexander Aring rc->rc_result = cpu_to_le32(status); 261e7fd4179SDavid Teigland 262757a4271SDavid Teigland set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots); 263757a4271SDavid Teigland 264757a4271SDavid Teigland if (!num_slots) 265757a4271SDavid Teigland goto do_send; 266757a4271SDavid Teigland 267757a4271SDavid Teigland spin_lock(&ls->ls_recover_lock); 268757a4271SDavid Teigland if (ls->ls_num_slots != num_slots) { 269757a4271SDavid Teigland spin_unlock(&ls->ls_recover_lock); 270757a4271SDavid Teigland log_debug(ls, "receive_rcom_status num_slots %d to %d", 271757a4271SDavid Teigland num_slots, ls->ls_num_slots); 272757a4271SDavid Teigland rc->rc_result = 0; 273757a4271SDavid Teigland set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0); 274757a4271SDavid Teigland goto do_send; 275757a4271SDavid Teigland } 276757a4271SDavid Teigland 277757a4271SDavid Teigland dlm_slots_copy_out(ls, rc); 278757a4271SDavid Teigland spin_unlock(&ls->ls_recover_lock); 279757a4271SDavid Teigland 280757a4271SDavid Teigland do_send: 28188aa023aSAlexander Aring send_rcom_stateless(msg, rc); 282e7fd4179SDavid Teigland } 283e7fd4179SDavid Teigland 2844a99c3d9SDavid Teigland static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in) 285e7fd4179SDavid Teigland { 28698f176fbSDavid Teigland spin_lock(&ls->ls_rcom_spin); 28798f176fbSDavid Teigland if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) || 2882f9dbedaSAlexander Aring le64_to_cpu(rc_in->rc_id) != ls->ls_rcom_seq) { 28998f176fbSDavid Teigland log_debug(ls, "reject reply %d from %d seq %llx expect %llx", 2902f9dbedaSAlexander Aring le32_to_cpu(rc_in->rc_type), 2913428785aSAlexander Aring le32_to_cpu(rc_in->rc_header.h_nodeid), 2922f9dbedaSAlexander Aring (unsigned long long)le64_to_cpu(rc_in->rc_id), 29357adf7eeSRyusuke Konishi (unsigned long long)ls->ls_rcom_seq); 29498f176fbSDavid Teigland goto out; 2954a99c3d9SDavid Teigland } 2963428785aSAlexander Aring memcpy(ls->ls_recover_buf, rc_in, 2973428785aSAlexander Aring le16_to_cpu(rc_in->rc_header.h_length)); 298e7fd4179SDavid Teigland set_bit(LSFL_RCOM_READY, &ls->ls_flags); 29998f176fbSDavid Teigland clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags); 300e7fd4179SDavid Teigland wake_up(&ls->ls_wait_general); 30198f176fbSDavid Teigland out: 30298f176fbSDavid Teigland spin_unlock(&ls->ls_rcom_spin); 303e7fd4179SDavid Teigland } 304e7fd4179SDavid Teigland 305e7fd4179SDavid Teigland int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len) 306e7fd4179SDavid Teigland { 307e7fd4179SDavid Teigland struct dlm_rcom *rc; 3088f2dc78dSAlexander Aring struct dlm_msg *msg; 3094007685cSAl Viro int error = 0; 310e7fd4179SDavid Teigland 311faa0f267SDavid Teigland ls->ls_recover_nodeid = nodeid; 312e7fd4179SDavid Teigland 31359661212Stsutomu.owa@toshiba.co.jp retry: 314a070a91cSAlexander Aring error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES, last_len, 3158f2dc78dSAlexander Aring &rc, &msg); 316e7fd4179SDavid Teigland if (error) 317e7fd4179SDavid Teigland goto out; 318e7fd4179SDavid Teigland memcpy(rc->rc_buf, last_name, last_len); 31998f176fbSDavid Teigland 32098f176fbSDavid Teigland allow_sync_reply(ls, &rc->rc_id); 321d10a0b88SAlexander Aring memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE); 322e7fd4179SDavid Teigland 32388aa023aSAlexander Aring send_rcom_stateless(msg, rc); 324e7fd4179SDavid Teigland 325e7fd4179SDavid Teigland error = dlm_wait_function(ls, &rcom_response); 32698f176fbSDavid Teigland disallow_sync_reply(ls); 32759661212Stsutomu.owa@toshiba.co.jp if (error == -ETIMEDOUT) 32859661212Stsutomu.owa@toshiba.co.jp goto retry; 329e7fd4179SDavid Teigland out: 330e7fd4179SDavid Teigland return error; 331e7fd4179SDavid Teigland } 332e7fd4179SDavid Teigland 333e7fd4179SDavid Teigland static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in) 334e7fd4179SDavid Teigland { 335e7fd4179SDavid Teigland struct dlm_rcom *rc; 33638aa8b0cSDavid Teigland int error, inlen, outlen, nodeid; 3378f2dc78dSAlexander Aring struct dlm_msg *msg; 338e7fd4179SDavid Teigland 3393428785aSAlexander Aring nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid); 3403428785aSAlexander Aring inlen = le16_to_cpu(rc_in->rc_header.h_length) - 3413428785aSAlexander Aring sizeof(struct dlm_rcom); 342d10a0b88SAlexander Aring outlen = DLM_MAX_APP_BUFSIZE - sizeof(struct dlm_rcom); 343e7fd4179SDavid Teigland 344a070a91cSAlexander Aring error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, 3458f2dc78dSAlexander Aring &rc, &msg); 346e7fd4179SDavid Teigland if (error) 347e7fd4179SDavid Teigland return; 3484a99c3d9SDavid Teigland rc->rc_id = rc_in->rc_id; 34938aa8b0cSDavid Teigland rc->rc_seq_reply = rc_in->rc_seq; 350e7fd4179SDavid Teigland 351e7fd4179SDavid Teigland dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen, 352e7fd4179SDavid Teigland nodeid); 35388aa023aSAlexander Aring send_rcom_stateless(msg, rc); 354e7fd4179SDavid Teigland } 355e7fd4179SDavid Teigland 356e7fd4179SDavid Teigland int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid) 357e7fd4179SDavid Teigland { 358e7fd4179SDavid Teigland struct dlm_rcom *rc; 359e7fd4179SDavid Teigland struct dlm_mhandle *mh; 360e7fd4179SDavid Teigland struct dlm_ls *ls = r->res_ls; 361e7fd4179SDavid Teigland int error; 362e7fd4179SDavid Teigland 363e7fd4179SDavid Teigland error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length, 364e7fd4179SDavid Teigland &rc, &mh); 365e7fd4179SDavid Teigland if (error) 366e7fd4179SDavid Teigland goto out; 367e7fd4179SDavid Teigland memcpy(rc->rc_buf, r->res_name, r->res_length); 3682f9dbedaSAlexander Aring rc->rc_id = cpu_to_le64(r->res_id); 369e7fd4179SDavid Teigland 37088aa023aSAlexander Aring send_rcom(mh, rc); 371e7fd4179SDavid Teigland out: 372e7fd4179SDavid Teigland return error; 373e7fd4179SDavid Teigland } 374e7fd4179SDavid Teigland 375e7fd4179SDavid Teigland static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in) 376e7fd4179SDavid Teigland { 377e7fd4179SDavid Teigland struct dlm_rcom *rc; 378e7fd4179SDavid Teigland struct dlm_mhandle *mh; 3793428785aSAlexander Aring int error, ret_nodeid, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid); 3803428785aSAlexander Aring int len = le16_to_cpu(rc_in->rc_header.h_length) - 3813428785aSAlexander Aring sizeof(struct dlm_rcom); 382e7fd4179SDavid Teigland 3839250e523SDavid Teigland /* Old code would send this special id to trigger a debug dump. */ 3842f9dbedaSAlexander Aring if (rc_in->rc_id == cpu_to_le64(0xFFFFFFFF)) { 385c04fecb4SDavid Teigland log_error(ls, "receive_rcom_lookup dump from %d", nodeid); 386c04fecb4SDavid Teigland dlm_dump_rsb_name(ls, rc_in->rc_buf, len); 387c04fecb4SDavid Teigland return; 388c04fecb4SDavid Teigland } 389c04fecb4SDavid Teigland 390f6089981SColin Ian King error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh); 391f6089981SColin Ian King if (error) 392f6089981SColin Ian King return; 393f6089981SColin Ian King 394c04fecb4SDavid Teigland error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len, 395c04fecb4SDavid Teigland DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL); 396e7fd4179SDavid Teigland if (error) 397e7fd4179SDavid Teigland ret_nodeid = error; 3982f9dbedaSAlexander Aring rc->rc_result = cpu_to_le32(ret_nodeid); 399e7fd4179SDavid Teigland rc->rc_id = rc_in->rc_id; 40038aa8b0cSDavid Teigland rc->rc_seq_reply = rc_in->rc_seq; 401e7fd4179SDavid Teigland 40288aa023aSAlexander Aring send_rcom(mh, rc); 403e7fd4179SDavid Teigland } 404e7fd4179SDavid Teigland 405e7fd4179SDavid Teigland static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in) 406e7fd4179SDavid Teigland { 407e7fd4179SDavid Teigland dlm_recover_master_reply(ls, rc_in); 408e7fd4179SDavid Teigland } 409e7fd4179SDavid Teigland 410e7fd4179SDavid Teigland static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb, 411e7fd4179SDavid Teigland struct rcom_lock *rl) 412e7fd4179SDavid Teigland { 413e7fd4179SDavid Teigland memset(rl, 0, sizeof(*rl)); 414e7fd4179SDavid Teigland 415163a1859SAl Viro rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid); 416163a1859SAl Viro rl->rl_lkid = cpu_to_le32(lkb->lkb_id); 417163a1859SAl Viro rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags); 418163a1859SAl Viro rl->rl_flags = cpu_to_le32(lkb->lkb_flags); 419163a1859SAl Viro rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq); 420e7fd4179SDavid Teigland rl->rl_rqmode = lkb->lkb_rqmode; 421e7fd4179SDavid Teigland rl->rl_grmode = lkb->lkb_grmode; 422e7fd4179SDavid Teigland rl->rl_status = lkb->lkb_status; 423163a1859SAl Viro rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type); 424e7fd4179SDavid Teigland 425e5dae548SDavid Teigland if (lkb->lkb_bastfn) 4268304d6f2SDavid Teigland rl->rl_asts |= DLM_CB_BAST; 427e5dae548SDavid Teigland if (lkb->lkb_astfn) 4288304d6f2SDavid Teigland rl->rl_asts |= DLM_CB_CAST; 429e7fd4179SDavid Teigland 430163a1859SAl Viro rl->rl_namelen = cpu_to_le16(r->res_length); 431e7fd4179SDavid Teigland memcpy(rl->rl_name, r->res_name, r->res_length); 432e7fd4179SDavid Teigland 433e7fd4179SDavid Teigland /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ? 434e7fd4179SDavid Teigland If so, receive_rcom_lock_args() won't take this copy. */ 435e7fd4179SDavid Teigland 436e7fd4179SDavid Teigland if (lkb->lkb_lvbptr) 437e7fd4179SDavid Teigland memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen); 438e7fd4179SDavid Teigland } 439e7fd4179SDavid Teigland 440e7fd4179SDavid Teigland int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb) 441e7fd4179SDavid Teigland { 442e7fd4179SDavid Teigland struct dlm_ls *ls = r->res_ls; 443e7fd4179SDavid Teigland struct dlm_rcom *rc; 444e7fd4179SDavid Teigland struct dlm_mhandle *mh; 445e7fd4179SDavid Teigland struct rcom_lock *rl; 446e7fd4179SDavid Teigland int error, len = sizeof(struct rcom_lock); 447e7fd4179SDavid Teigland 448e7fd4179SDavid Teigland if (lkb->lkb_lvbptr) 449e7fd4179SDavid Teigland len += ls->ls_lvblen; 450e7fd4179SDavid Teigland 451e7fd4179SDavid Teigland error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh); 452e7fd4179SDavid Teigland if (error) 453e7fd4179SDavid Teigland goto out; 454e7fd4179SDavid Teigland 455e7fd4179SDavid Teigland rl = (struct rcom_lock *) rc->rc_buf; 456e7fd4179SDavid Teigland pack_rcom_lock(r, lkb, rl); 457e425ac99SAlexander Aring rc->rc_id = cpu_to_le64((uintptr_t)r); 458e7fd4179SDavid Teigland 45988aa023aSAlexander Aring send_rcom(mh, rc); 460e7fd4179SDavid Teigland out: 461e7fd4179SDavid Teigland return error; 462e7fd4179SDavid Teigland } 463e7fd4179SDavid Teigland 464ae773d0bSAl Viro /* needs at least dlm_rcom + rcom_lock */ 465e7fd4179SDavid Teigland static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in) 466e7fd4179SDavid Teigland { 467e7fd4179SDavid Teigland struct dlm_rcom *rc; 468e7fd4179SDavid Teigland struct dlm_mhandle *mh; 4693428785aSAlexander Aring int error, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid); 470e7fd4179SDavid Teigland 471e7fd4179SDavid Teigland dlm_recover_master_copy(ls, rc_in); 472e7fd4179SDavid Teigland 473e7fd4179SDavid Teigland error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY, 474e7fd4179SDavid Teigland sizeof(struct rcom_lock), &rc, &mh); 475e7fd4179SDavid Teigland if (error) 476e7fd4179SDavid Teigland return; 477e7fd4179SDavid Teigland 478e7fd4179SDavid Teigland /* We send back the same rcom_lock struct we received, but 479e7fd4179SDavid Teigland dlm_recover_master_copy() has filled in rl_remid and rl_result */ 480e7fd4179SDavid Teigland 481e7fd4179SDavid Teigland memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock)); 482e7fd4179SDavid Teigland rc->rc_id = rc_in->rc_id; 48338aa8b0cSDavid Teigland rc->rc_seq_reply = rc_in->rc_seq; 484e7fd4179SDavid Teigland 48588aa023aSAlexander Aring send_rcom(mh, rc); 486e7fd4179SDavid Teigland } 487e7fd4179SDavid Teigland 488c36258b5SDavid Teigland /* If the lockspace doesn't exist then still send a status message 489c36258b5SDavid Teigland back; it's possible that it just doesn't have its global_id yet. */ 490c36258b5SDavid Teigland 491c36258b5SDavid Teigland int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in) 492e7fd4179SDavid Teigland { 493e7fd4179SDavid Teigland struct dlm_rcom *rc; 4941babdb45SDavid Teigland struct rcom_config *rf; 495e7fd4179SDavid Teigland struct dlm_mhandle *mh; 496e7fd4179SDavid Teigland char *mb; 4971babdb45SDavid Teigland int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config); 498e7fd4179SDavid Teigland 499a070a91cSAlexander Aring mh = dlm_midcomms_get_mhandle(nodeid, mb_len, GFP_NOFS, &mb); 500e7fd4179SDavid Teigland if (!mh) 501e7fd4179SDavid Teigland return -ENOBUFS; 502e7fd4179SDavid Teigland 503e7fd4179SDavid Teigland rc = (struct dlm_rcom *) mb; 504e7fd4179SDavid Teigland 5053428785aSAlexander Aring rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR); 5068e2e4086SAlexander Aring rc->rc_header.u.h_lockspace = rc_in->rc_header.u.h_lockspace; 5073428785aSAlexander Aring rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid()); 5083428785aSAlexander Aring rc->rc_header.h_length = cpu_to_le16(mb_len); 509e7fd4179SDavid Teigland rc->rc_header.h_cmd = DLM_RCOM; 510e7fd4179SDavid Teigland 5112f9dbedaSAlexander Aring rc->rc_type = cpu_to_le32(DLM_RCOM_STATUS_REPLY); 512f5888750SDavid Teigland rc->rc_id = rc_in->rc_id; 51338aa8b0cSDavid Teigland rc->rc_seq_reply = rc_in->rc_seq; 5142f9dbedaSAlexander Aring rc->rc_result = cpu_to_le32(-ESRCH); 515e7fd4179SDavid Teigland 5161babdb45SDavid Teigland rf = (struct rcom_config *) rc->rc_buf; 51793ff2971SAl Viro rf->rf_lvblen = cpu_to_le32(~0U); 5181babdb45SDavid Teigland 519*e01c4b7bSAlexander Aring dlm_midcomms_commit_mhandle(mh, NULL, 0); 520e7fd4179SDavid Teigland 521e7fd4179SDavid Teigland return 0; 522e7fd4179SDavid Teigland } 523e7fd4179SDavid Teigland 524c04fecb4SDavid Teigland /* 525c04fecb4SDavid Teigland * Ignore messages for stage Y before we set 526c04fecb4SDavid Teigland * recover_status bit for stage X: 527c04fecb4SDavid Teigland * 528c04fecb4SDavid Teigland * recover_status = 0 529c04fecb4SDavid Teigland * 530c04fecb4SDavid Teigland * dlm_recover_members() 531c04fecb4SDavid Teigland * - send nothing 532c04fecb4SDavid Teigland * - recv nothing 533c04fecb4SDavid Teigland * - ignore NAMES, NAMES_REPLY 534c04fecb4SDavid Teigland * - ignore LOOKUP, LOOKUP_REPLY 535c04fecb4SDavid Teigland * - ignore LOCK, LOCK_REPLY 536c04fecb4SDavid Teigland * 537c04fecb4SDavid Teigland * recover_status |= NODES 538c04fecb4SDavid Teigland * 539c04fecb4SDavid Teigland * dlm_recover_members_wait() 540c04fecb4SDavid Teigland * 541c04fecb4SDavid Teigland * dlm_recover_directory() 542c04fecb4SDavid Teigland * - send NAMES 543c04fecb4SDavid Teigland * - recv NAMES_REPLY 544c04fecb4SDavid Teigland * - ignore LOOKUP, LOOKUP_REPLY 545c04fecb4SDavid Teigland * - ignore LOCK, LOCK_REPLY 546c04fecb4SDavid Teigland * 547c04fecb4SDavid Teigland * recover_status |= DIR 548c04fecb4SDavid Teigland * 549c04fecb4SDavid Teigland * dlm_recover_directory_wait() 550c04fecb4SDavid Teigland * 551c04fecb4SDavid Teigland * dlm_recover_masters() 552c04fecb4SDavid Teigland * - send LOOKUP 553c04fecb4SDavid Teigland * - recv LOOKUP_REPLY 554c04fecb4SDavid Teigland * 555c04fecb4SDavid Teigland * dlm_recover_locks() 556c04fecb4SDavid Teigland * - send LOCKS 557c04fecb4SDavid Teigland * - recv LOCKS_REPLY 558c04fecb4SDavid Teigland * 559c04fecb4SDavid Teigland * recover_status |= LOCKS 560c04fecb4SDavid Teigland * 561c04fecb4SDavid Teigland * dlm_recover_locks_wait() 562c04fecb4SDavid Teigland * 563c04fecb4SDavid Teigland * recover_status |= DONE 564c04fecb4SDavid Teigland */ 565c04fecb4SDavid Teigland 566c36258b5SDavid Teigland /* Called by dlm_recv; corresponds to dlm_receive_message() but special 567e7fd4179SDavid Teigland recovery-only comms are sent through here. */ 568e7fd4179SDavid Teigland 569c36258b5SDavid Teigland void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid) 570e7fd4179SDavid Teigland { 571ae773d0bSAl Viro int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock); 572c04fecb4SDavid Teigland int stop, reply = 0, names = 0, lookup = 0, lock = 0; 5734875647aSDavid Teigland uint32_t status; 574d6e24788SDavid Teigland uint64_t seq; 575ae773d0bSAl Viro 576d6e24788SDavid Teigland switch (rc->rc_type) { 5772f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_STATUS_REPLY): 578c04fecb4SDavid Teigland reply = 1; 579c04fecb4SDavid Teigland break; 5802f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_NAMES): 581c04fecb4SDavid Teigland names = 1; 582c04fecb4SDavid Teigland break; 5832f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_NAMES_REPLY): 584c04fecb4SDavid Teigland names = 1; 585c04fecb4SDavid Teigland reply = 1; 586c04fecb4SDavid Teigland break; 5872f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOOKUP): 588c04fecb4SDavid Teigland lookup = 1; 589c04fecb4SDavid Teigland break; 5902f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY): 591c04fecb4SDavid Teigland lookup = 1; 592c04fecb4SDavid Teigland reply = 1; 593c04fecb4SDavid Teigland break; 5942f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOCK): 5954875647aSDavid Teigland lock = 1; 5964875647aSDavid Teigland break; 5972f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOCK_REPLY): 5984875647aSDavid Teigland lock = 1; 5994875647aSDavid Teigland reply = 1; 6004875647aSDavid Teigland break; 60190db4f8bSWu Bo } 602d6e24788SDavid Teigland 603d6e24788SDavid Teigland spin_lock(&ls->ls_recover_lock); 6044875647aSDavid Teigland status = ls->ls_recover_status; 6053e973671SAlexander Aring stop = dlm_recovery_stopped(ls); 606d6e24788SDavid Teigland seq = ls->ls_recover_seq; 607d6e24788SDavid Teigland spin_unlock(&ls->ls_recover_lock); 608d6e24788SDavid Teigland 6092f9dbedaSAlexander Aring if (stop && (rc->rc_type != cpu_to_le32(DLM_RCOM_STATUS))) 610c04fecb4SDavid Teigland goto ignore; 611c04fecb4SDavid Teigland 6122f9dbedaSAlexander Aring if (reply && (le64_to_cpu(rc->rc_seq_reply) != seq)) 613c04fecb4SDavid Teigland goto ignore; 614c04fecb4SDavid Teigland 615c04fecb4SDavid Teigland if (!(status & DLM_RS_NODES) && (names || lookup || lock)) 616c04fecb4SDavid Teigland goto ignore; 617c04fecb4SDavid Teigland 618c04fecb4SDavid Teigland if (!(status & DLM_RS_DIR) && (lookup || lock)) 619c04fecb4SDavid Teigland goto ignore; 620e7fd4179SDavid Teigland 621e7fd4179SDavid Teigland switch (rc->rc_type) { 6222f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_STATUS): 623e7fd4179SDavid Teigland receive_rcom_status(ls, rc); 624e7fd4179SDavid Teigland break; 625e7fd4179SDavid Teigland 6262f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_NAMES): 627e7fd4179SDavid Teigland receive_rcom_names(ls, rc); 628e7fd4179SDavid Teigland break; 629e7fd4179SDavid Teigland 6302f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOOKUP): 631e7fd4179SDavid Teigland receive_rcom_lookup(ls, rc); 632e7fd4179SDavid Teigland break; 633e7fd4179SDavid Teigland 6342f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOCK): 6353428785aSAlexander Aring if (le16_to_cpu(rc->rc_header.h_length) < lock_size) 636ae773d0bSAl Viro goto Eshort; 637e7fd4179SDavid Teigland receive_rcom_lock(ls, rc); 638e7fd4179SDavid Teigland break; 639e7fd4179SDavid Teigland 6402f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_STATUS_REPLY): 641dbcfc347SDavid Teigland receive_sync_reply(ls, rc); 642e7fd4179SDavid Teigland break; 643e7fd4179SDavid Teigland 6442f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_NAMES_REPLY): 645dbcfc347SDavid Teigland receive_sync_reply(ls, rc); 646e7fd4179SDavid Teigland break; 647e7fd4179SDavid Teigland 6482f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY): 649e7fd4179SDavid Teigland receive_rcom_lookup_reply(ls, rc); 650e7fd4179SDavid Teigland break; 651e7fd4179SDavid Teigland 6522f9dbedaSAlexander Aring case cpu_to_le32(DLM_RCOM_LOCK_REPLY): 6533428785aSAlexander Aring if (le16_to_cpu(rc->rc_header.h_length) < lock_size) 654ae773d0bSAl Viro goto Eshort; 655dbcfc347SDavid Teigland dlm_recover_process_copy(ls, rc); 656e7fd4179SDavid Teigland break; 657e7fd4179SDavid Teigland 658e7fd4179SDavid Teigland default: 6592f9dbedaSAlexander Aring log_error(ls, "receive_rcom bad type %d", 6602f9dbedaSAlexander Aring le32_to_cpu(rc->rc_type)); 661e7fd4179SDavid Teigland } 662c04fecb4SDavid Teigland return; 663c04fecb4SDavid Teigland 664c04fecb4SDavid Teigland ignore: 665c04fecb4SDavid Teigland log_limit(ls, "dlm_receive_rcom ignore msg %d " 666c04fecb4SDavid Teigland "from %d %llu %llu recover seq %llu sts %x gen %u", 6672f9dbedaSAlexander Aring le32_to_cpu(rc->rc_type), 668c04fecb4SDavid Teigland nodeid, 6692f9dbedaSAlexander Aring (unsigned long long)le64_to_cpu(rc->rc_seq), 6702f9dbedaSAlexander Aring (unsigned long long)le64_to_cpu(rc->rc_seq_reply), 671c04fecb4SDavid Teigland (unsigned long long)seq, 672c04fecb4SDavid Teigland status, ls->ls_generation); 673c36258b5SDavid Teigland return; 674ae773d0bSAl Viro Eshort: 675c04fecb4SDavid Teigland log_error(ls, "recovery message %d from %d is too short", 6762f9dbedaSAlexander Aring le32_to_cpu(rc->rc_type), nodeid); 677e7fd4179SDavid Teigland } 678e7fd4179SDavid Teigland 679