/* Lock the send communication. This is done through *bitmanipulationasopposedtoamutexinorderto *accommodatelockandhold.Seenextcomment.
*/ #define MD_CLUSTER_SEND_LOCK 4 /* If cluster operations (such as adding a disk) must lock the *communicationchannel,soastoperformextraoperations *(updatemetadata)andnootheroperationisallowedonthe *MD.Tokenneedstobelockedandhelduntiltheoperation *completeswithamd_update_sb(),whichwouldeventuallyrelease *thelock.
*/ #define MD_CLUSTER_SEND_LOCKED_ALREADY 5 /* We should receive message after node joined cluster and
* set up all the related infos such as bitmap and personality */ #define MD_CLUSTER_ALREADY_IN_CLUSTER 6 #define MD_CLUSTER_PENDING_RECV_EVENT 7 #define MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD 8 #define MD_CLUSTER_WAITING_FOR_SYNC 9
struct md_cluster_info { struct mddev *mddev; /* the md device which md_cluster_info belongs to */ /* dlm lock space and resources for clustered raid. */
dlm_lockspace_t *lockspace; int slot_number; struct completion completion; struct mutex recv_mutex; struct dlm_lock_resource *bitmap_lockres; struct dlm_lock_resource **other_bitmap_lockres; struct dlm_lock_resource *resync_lockres; struct list_head suspend_list;
spinlock_t suspend_lock; /* record the region which write should be suspended */
sector_t suspend_lo;
sector_t suspend_hi; int suspend_from; /* the slot which broadcast suspend_lo/hi */
struct md_thread __rcu *recovery_thread; unsignedlong recovery_map; /* communication loc resources */ struct dlm_lock_resource *ack_lockres; struct dlm_lock_resource *message_lockres; struct dlm_lock_resource *token_lockres; struct dlm_lock_resource *no_new_dev_lockres; struct md_thread __rcu *recv_thread; struct completion newdisk_completion;
wait_queue_head_t wait; unsignedlong state; /* record the region in RESYNCING message */
sector_t sync_low;
sector_t sync_hi;
};
/* For compatibility, add the new msg_type at the end. */ enum msg_type {
METADATA_UPDATED = 0,
RESYNCING,
NEWDISK,
REMOVE,
RE_ADD,
BITMAP_NEEDS_SYNC,
CHANGE_CAPACITY,
BITMAP_RESIZE,
RESYNCING_START,
};
ret = dlm_lock_sync_interruptible(bm_lockres, DLM_LOCK_PW, mddev); if (ret) {
pr_err("md-cluster: Could not DLM lock %s: %d\n",
str, ret); goto clear_bit;
}
ret = mddev->bitmap_ops->copy_from_slot(mddev, slot, &lo, &hi, true); if (ret) {
pr_err("md-cluster: Could not copy data from bitmap %d\n", slot); goto clear_bit;
}
/* Clear suspend_area associated with the bitmap */
spin_lock_irq(&cinfo->suspend_lock);
cinfo->suspend_hi = 0;
cinfo->suspend_lo = 0;
cinfo->suspend_from = -1;
spin_unlock_irq(&cinfo->suspend_lock);
/* Kick off a reshape if needed */ if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
mddev->reshape_position != MaxSector)
md_wakeup_thread(mddev->sync_thread);
if (hi > 0) { if (lo < mddev->resync_offset)
mddev->resync_offset = lo; /* wake up thread to continue resync in case resync
* is not finished */ if (mddev->resync_offset != MaxSector) { /* *cleartheREMOTEflagsincewewilllaunch *resyncthreadincurrentnode.
*/
clear_bit(MD_RESYNCING_REMOTE,
&mddev->recovery);
set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
md_wakeup_thread(mddev->thread);
}
}
clear_bit:
lockres_free(bm_lockres);
clear_bit(slot, &cinfo->recovery_map);
}
}
pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
mddev->bitmap_info.cluster_name,
slot->nodeid, slot->slot,
cinfo->slot_number); /* deduct one since dlm slot starts from one while the num of
* cluster-md begins with 0 */
__recover_slot(mddev, slot->slot - 1);
}
cinfo->slot_number = our_slot; /* completion is only need to be complete when node join cluster,
* it doesn't need to run during another node's failure */ if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
complete(&cinfo->completion);
clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
}
clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
}
/* the ops is called when node join the cluster, and do lock recovery
* if node failure occurs */ staticconststruct dlm_lockspace_ops md_ls_ops = {
.recover_prep = recover_prep,
.recover_slot = recover_slot,
.recover_done = recover_done,
};
rcu_read_lock();
rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot)); if (rdev && test_bit(Faulty, &rdev->flags))
clear_bit(Faulty, &rdev->flags); else
pr_warn("%s: %d Could not find disk(%d) which is faulty",
__func__, __LINE__, le32_to_cpu(msg->raid_slot));
rcu_read_unlock();
}
staticint process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
{ int ret = 0;
if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot), "node %d received its own msg\n", le32_to_cpu(msg->slot))) return -1; switch (le32_to_cpu(msg->type)) { case METADATA_UPDATED:
process_metadata_update(mddev, msg); break; case CHANGE_CAPACITY:
set_capacity_and_notify(mddev->gendisk, mddev->array_sectors); break; case RESYNCING_START:
clear_bit(MD_CLUSTER_WAITING_FOR_SYNC, &mddev->cluster_info->state); break; case RESYNCING:
set_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
process_suspend_info(mddev, le32_to_cpu(msg->slot),
le64_to_cpu(msg->low),
le64_to_cpu(msg->high)); break; case NEWDISK: if (process_add_new_disk(mddev, msg))
ret = -1; break; case REMOVE:
process_remove_disk(mddev, msg); break; case RE_ADD:
process_readd_disk(mddev, msg); break; case BITMAP_NEEDS_SYNC:
__recover_slot(mddev, le32_to_cpu(msg->slot)); break; case BITMAP_RESIZE: if (le64_to_cpu(msg->high) != mddev->pers->size(mddev, 0, 0))
ret = mddev->bitmap_ops->resize(mddev,
le64_to_cpu(msg->high), 0, false); break; default:
ret = -1;
pr_warn("%s:%d Received unknown message from %d\n",
__func__, __LINE__, msg->slot);
} return ret;
}
mutex_lock(&cinfo->recv_mutex); /*get CR on Message*/ if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
pr_err("md/raid1:failed to get CR on MESSAGE\n");
mutex_unlock(&cinfo->recv_mutex); return;
}
/* read lvb and wake up thread to process this message_lockres */
memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
ret = process_recvd_msg(thread->mddev, &msg); if (ret) goto out;
/*release CR on ack_lockres*/
ret = dlm_unlock_sync(ack_lockres); if (unlikely(ret != 0))
pr_info("unlock ack failed return %d\n", ret); /*up-convert to PR on message_lockres*/
ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR); if (unlikely(ret != 0))
pr_info("lock PR on msg failed return %d\n", ret); /*get CR on ack_lockres again*/
ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR); if (unlikely(ret != 0))
pr_info("lock CR on ack failed return %d\n", ret);
out: /*release CR on message_lockres*/
ret = dlm_unlock_sync(message_lockres); if (unlikely(ret != 0))
pr_info("unlock msg failed return %d\n", ret);
mutex_unlock(&cinfo->recv_mutex);
}
cmsg->slot = cpu_to_le32(slot); /*get EX on Message*/
error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX); if (error) {
pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error); return error;
}
memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg, sizeof(struct cluster_msg)); /*down-convert EX to CW on Message*/
error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW); if (error) {
pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
error); goto failed_ack;
}
/*up-convert CR to EX on Ack*/
error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX); if (error) {
pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
error); goto failed_ack;
}
/*down-convert EX to CR on Ack*/
error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR); if (error) {
pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
error); goto failed_ack;
}
failed_ack: while ((unlock_error = dlm_unlock_sync(cinfo->message_lockres)))
pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
unlock_error);
ret = lock_comm(cinfo, mddev_locked); if (!ret) {
ret = __sendmsg(cinfo, cmsg);
unlock_comm(cinfo);
} return ret;
}
staticint gather_all_resync_info(struct mddev *mddev, int total_slots)
{ struct md_cluster_info *cinfo = mddev->cluster_info; int i, ret = 0; struct dlm_lock_resource *bm_lockres; char str[64];
sector_t lo, hi;
for (i = 0; i < total_slots; i++) {
memset(str, '\0', 64);
snprintf(str, 64, "bitmap%04d", i);
bm_lockres = lockres_init(mddev, str, NULL, 1); if (!bm_lockres) return -ENOMEM; if (i == (cinfo->slot_number - 1)) {
lockres_free(bm_lockres); continue;
}
bm_lockres->flags |= DLM_LKF_NOQUEUE;
ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW); if (ret == -EAGAIN) { if (read_resync_info(mddev, bm_lockres)) {
pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
__func__, __LINE__,
(unsignedlonglong) cinfo->suspend_lo,
(unsignedlonglong) cinfo->suspend_hi,
i);
cinfo->suspend_from = i;
}
ret = 0;
lockres_free(bm_lockres); continue;
} if (ret) {
lockres_free(bm_lockres); goto out;
}
/* Read the disk bitmap sb and check if it needs recovery */
ret = mddev->bitmap_ops->copy_from_slot(mddev, i, &lo, &hi, false); if (ret) {
pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
lockres_free(bm_lockres); continue;
} if ((hi > 0) && (lo < mddev->resync_offset)) {
set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
mddev->resync_offset = lo;
md_check_recovery(mddev);
}
lockres_free(bm_lockres);
}
out: return ret;
}
staticint join(struct mddev *mddev, int nodes)
{ struct md_cluster_info *cinfo; int ret, ops_rv; char str[64];
cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL); if (!cinfo) return -ENOMEM;
memset(str, 0, 64);
sprintf(str, "%pU", mddev->uuid);
ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
DLM_LSFL_SOFTIRQ, LVB_SIZE, &md_ls_ops, mddev,
&ops_rv, &cinfo->lockspace); if (ret) goto err;
wait_for_completion(&cinfo->completion); if (nodes < cinfo->slot_number) {
pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
cinfo->slot_number, nodes);
ret = -ERANGE; goto err;
} /* Initiate the communication resources */
ret = -ENOMEM;
rcu_assign_pointer(cinfo->recv_thread,
md_register_thread(recv_daemon, mddev, "cluster_recv")); if (!cinfo->recv_thread) {
pr_err("md-cluster: cannot allocate memory for recv_thread!\n"); goto err;
}
cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1); if (!cinfo->message_lockres) goto err;
cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0); if (!cinfo->token_lockres) goto err;
cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0); if (!cinfo->no_new_dev_lockres) goto err;
ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX); if (ret) {
ret = -EAGAIN;
pr_err("md-cluster: can't join cluster to avoid lock issue\n"); goto err;
}
cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0); if (!cinfo->ack_lockres) {
ret = -ENOMEM; goto err;
} /* get sync CR lock on ACK. */ if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
ret);
dlm_unlock_sync(cinfo->token_lockres); /* get sync CR lock on no-new-dev. */ if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1); if (!cinfo->bitmap_lockres) {
ret = -ENOMEM; goto err;
} if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
pr_err("Failed to get bitmap lock\n");
ret = -EINVAL; goto err;
}
cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0); if (!cinfo->resync_lockres) {
ret = -ENOMEM; goto err;
}
/* load all the node's bitmap info for resync */ if (gather_all_resync_info(mddev, total_slots))
pr_err("md-cluster: failed to gather all resyn infos\n");
set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state); /* wake up recv thread in case something need to be handled */ if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
md_wakeup_thread(cinfo->recv_thread);
}
/* If token is already locked, return 0 */ if (cinfo->token_lockres->mode == DLM_LOCK_EX) {
clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state); return0;
}
ret = lock_token(cinfo);
clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state); return ret;
}
staticint metadata_update_finish(struct mddev *mddev)
{ struct md_cluster_info *cinfo = mddev->cluster_info; struct cluster_msg cmsg; struct md_rdev *rdev; int ret = 0; int raid_slot = -1;
memset(&cmsg, 0, sizeof(cmsg));
cmsg.type = cpu_to_le32(METADATA_UPDATED); /* Pick up a good active device number to send.
*/
rdev_for_each(rdev, mddev) if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
raid_slot = rdev->desc_nr; break;
} if (raid_slot >= 0) {
cmsg.raid_slot = cpu_to_le32(raid_slot);
ret = __sendmsg(cinfo, &cmsg);
} else
pr_warn("md-cluster: No good device id found to send\n");
clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
unlock_comm(cinfo); return ret;
}
/* do not send zero again, if we have sent before */ if (hi == 0) {
memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info)); if (le64_to_cpu(ri.hi) == 0) return0;
}
/* release other node's bitmap lock if they are existed */ if (cinfo->other_bitmap_lockres) { for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) { if (cinfo->other_bitmap_lockres[i]) {
lockres_free(cinfo->other_bitmap_lockres[i]);
}
}
kfree(cinfo->other_bitmap_lockres);
cinfo->other_bitmap_lockres = NULL;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.