Skip to content

Commit 1aee41f

Browse files
committed
Add new disk to clustered array
Algorithm: 1. Node 1 issues mdadm --manage /dev/mdX --add /dev/sdYY which issues ioctl(ADD_NEW_DISC with disc.state set to MD_DISK_CLUSTER_ADD) 2. Node 1 sends NEWDISK with uuid and slot number 3. Other nodes issue kobject_uevent_env with uuid and slot number (Steps 4,5 could be a udev rule) 4. In userspace, the node searches for the disk, perhaps using blkid -t SUB_UUID="" 5. Other nodes issue either of the following depending on whether the disk was found: ioctl(ADD_NEW_DISK with disc.state set to MD_DISK_CANDIDATE and disc.number set to slot number) ioctl(CLUSTERED_DISK_NACK) 6. Other nodes drop lock on no-new-devs (CR) if device is found 7. Node 1 attempts EX lock on no-new-devs 8. If node 1 gets the lock, it sends METADATA_UPDATED after unmarking the disk as SpareLocal 9. If not (get no-new-dev lock), it fails the operation and sends METADATA_UPDATED 10. Other nodes understand if the device is added or not by reading the superblock again after receiving the METADATA_UPDATED message. Signed-off-by: Lidong Zhong <lzhong@suse.com> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
1 parent 7d49ffc commit 1aee41f

7 files changed

Lines changed: 169 additions & 4 deletions

File tree

‎drivers/md/md-cluster.c‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
#include <linux/module.h>
1313
#include <linux/dlm.h>
1414
#include <linux/sched.h>
15+
#include <linux/raid/md_p.h>
1516
#include "md.h"
1617
#include "bitmap.h"
1718
#include "md-cluster.h"
1819

1920
#define LVB_SIZE 64
21+
#define NEW_DEV_TIMEOUT 5000
2022

2123
struct dlm_lock_resource {
2224
dlm_lockspace_t *ls;
@@ -56,19 +58,25 @@ struct md_cluster_info {
5658
struct dlm_lock_resource *ack_lockres;
5759
struct dlm_lock_resource *message_lockres;
5860
struct dlm_lock_resource *token_lockres;
61+
struct dlm_lock_resource *no_new_dev_lockres;
5962
struct md_thread *recv_thread;
63+
struct completion newdisk_completion;
6064
};
6165

6266
enum msg_type {
6367
METADATA_UPDATED = 0,
6468
RESYNCING,
69+
NEWDISK,
6570
};
6671

6772
struct cluster_msg {
6873
int type;
6974
int slot;
75+
/* TODO: Unionize this for smaller footprint */
7076
sector_t low;
7177
sector_t high;
78+
char uuid[16];
79+
int raid_slot;
7280
};
7381

7482
static void sync_ast(void *arg)
@@ -358,20 +366,52 @@ static void process_suspend_info(struct md_cluster_info *cinfo,
358366
spin_unlock_irq(&cinfo->suspend_lock);
359367
}
360368

369+
static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
370+
{
371+
char disk_uuid[64];
372+
struct md_cluster_info *cinfo = mddev->cluster_info;
373+
char event_name[] = "EVENT=ADD_DEVICE";
374+
char raid_slot[16];
375+
char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
376+
int len;
377+
378+
len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
379+
pretty_uuid(disk_uuid + len, cmsg->uuid);
380+
snprintf(raid_slot, 16, "RAID_DISK=%d", cmsg->raid_slot);
381+
pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
382+
init_completion(&cinfo->newdisk_completion);
383+
kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
384+
wait_for_completion_timeout(&cinfo->newdisk_completion,
385+
NEW_DEV_TIMEOUT);
386+
}
387+
388+
389+
static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
390+
{
391+
struct md_cluster_info *cinfo = mddev->cluster_info;
392+
393+
md_reload_sb(mddev);
394+
dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
395+
}
396+
361397
static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
362398
{
363399
switch (msg->type) {
364400
case METADATA_UPDATED:
365401
pr_info("%s: %d Received message: METADATA_UPDATE from %d\n",
366402
__func__, __LINE__, msg->slot);
367-
md_reload_sb(mddev);
403+
process_metadata_update(mddev, msg);
368404
break;
369405
case RESYNCING:
370406
pr_info("%s: %d Received message: RESYNCING from %d\n",
371407
__func__, __LINE__, msg->slot);
372408
process_suspend_info(mddev->cluster_info, msg->slot,
373409
msg->low, msg->high);
374410
break;
411+
case NEWDISK:
412+
pr_info("%s: %d Received message: NEWDISK from %d\n",
413+
__func__, __LINE__, msg->slot);
414+
process_add_new_disk(mddev, msg);
375415
};
376416
}
377417

@@ -593,10 +633,18 @@ static int join(struct mddev *mddev, int nodes)
593633
cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
594634
if (!cinfo->ack_lockres)
595635
goto err;
636+
cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
637+
if (!cinfo->no_new_dev_lockres)
638+
goto err;
639+
596640
/* get sync CR lock on ACK. */
597641
if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
598642
pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
599643
ret);
644+
/* get sync CR lock on no-new-dev. */
645+
if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
646+
pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
647+
600648

601649
pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
602650
snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
@@ -621,6 +669,7 @@ static int join(struct mddev *mddev, int nodes)
621669
lockres_free(cinfo->message_lockres);
622670
lockres_free(cinfo->token_lockres);
623671
lockres_free(cinfo->ack_lockres);
672+
lockres_free(cinfo->no_new_dev_lockres);
624673
lockres_free(cinfo->bitmap_lockres);
625674
lockres_free(cinfo->sb_lock);
626675
if (cinfo->lockspace)
@@ -642,6 +691,7 @@ static int leave(struct mddev *mddev)
642691
lockres_free(cinfo->message_lockres);
643692
lockres_free(cinfo->token_lockres);
644693
lockres_free(cinfo->ack_lockres);
694+
lockres_free(cinfo->no_new_dev_lockres);
645695
lockres_free(cinfo->sb_lock);
646696
lockres_free(cinfo->bitmap_lockres);
647697
dlm_release_lockspace(cinfo->lockspace, 2);
@@ -742,6 +792,55 @@ static int area_resyncing(struct mddev *mddev, sector_t lo, sector_t hi)
742792
return ret;
743793
}
744794

795+
static int add_new_disk_start(struct mddev *mddev, struct md_rdev *rdev)
796+
{
797+
struct md_cluster_info *cinfo = mddev->cluster_info;
798+
struct cluster_msg cmsg;
799+
int ret = 0;
800+
struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
801+
char *uuid = sb->device_uuid;
802+
803+
memset(&cmsg, 0, sizeof(cmsg));
804+
cmsg.type = cpu_to_le32(NEWDISK);
805+
memcpy(cmsg.uuid, uuid, 16);
806+
cmsg.raid_slot = rdev->desc_nr;
807+
lock_comm(cinfo);
808+
ret = __sendmsg(cinfo, &cmsg);
809+
if (ret)
810+
return ret;
811+
cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
812+
ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
813+
cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
814+
/* Some node does not "see" the device */
815+
if (ret == -EAGAIN)
816+
ret = -ENOENT;
817+
else
818+
dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
819+
return ret;
820+
}
821+
822+
static int add_new_disk_finish(struct mddev *mddev)
823+
{
824+
struct cluster_msg cmsg;
825+
struct md_cluster_info *cinfo = mddev->cluster_info;
826+
int ret;
827+
/* Write sb and inform others */
828+
md_update_sb(mddev, 1);
829+
cmsg.type = METADATA_UPDATED;
830+
ret = __sendmsg(cinfo, &cmsg);
831+
unlock_comm(cinfo);
832+
return ret;
833+
}
834+
835+
static void new_disk_ack(struct mddev *mddev, bool ack)
836+
{
837+
struct md_cluster_info *cinfo = mddev->cluster_info;
838+
839+
if (ack)
840+
dlm_unlock_sync(cinfo->no_new_dev_lockres);
841+
complete(&cinfo->newdisk_completion);
842+
}
843+
745844
static struct md_cluster_operations cluster_ops = {
746845
.join = join,
747846
.leave = leave,
@@ -753,6 +852,9 @@ static struct md_cluster_operations cluster_ops = {
753852
.metadata_update_finish = metadata_update_finish,
754853
.metadata_update_cancel = metadata_update_cancel,
755854
.area_resyncing = area_resyncing,
855+
.add_new_disk_start = add_new_disk_start,
856+
.add_new_disk_finish = add_new_disk_finish,
857+
.new_disk_ack = new_disk_ack,
756858
};
757859

758860
static int __init cluster_init(void)

‎drivers/md/md-cluster.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "md.h"
77

88
struct mddev;
9+
struct md_rdev;
910

1011
struct md_cluster_operations {
1112
int (*join)(struct mddev *mddev, int nodes);
@@ -18,6 +19,9 @@ struct md_cluster_operations {
1819
int (*metadata_update_finish)(struct mddev *mddev);
1920
int (*metadata_update_cancel)(struct mddev *mddev);
2021
int (*area_resyncing)(struct mddev *mddev, sector_t lo, sector_t hi);
22+
int (*add_new_disk_start)(struct mddev *mddev, struct md_rdev *rdev);
23+
int (*add_new_disk_finish)(struct mddev *mddev);
24+
void (*new_disk_ack)(struct mddev *mddev, bool ack);
2125
};
2226

2327
#endif /* _MD_CLUSTER_H */

‎drivers/md/md.c‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ static void sync_sbs(struct mddev *mddev, int nospares)
22102210
}
22112211
}
22122212

2213-
static void md_update_sb(struct mddev *mddev, int force_change)
2213+
void md_update_sb(struct mddev *mddev, int force_change)
22142214
{
22152215
struct md_rdev *rdev;
22162216
int sync_req;
@@ -2371,6 +2371,7 @@ static void md_update_sb(struct mddev *mddev, int force_change)
23712371
wake_up(&rdev->blocked_wait);
23722372
}
23732373
}
2374+
EXPORT_SYMBOL(md_update_sb);
23742375

23752376
/* words written to sysfs files may, or may not, be \n terminated.
23762377
* We want to accept with case. For this we use cmd_match.
@@ -3151,7 +3152,7 @@ static void analyze_sbs(struct mddev *mddev)
31513152
kick_rdev_from_array(rdev);
31523153
continue;
31533154
}
3154-
if (rdev != freshest)
3155+
if (rdev != freshest) {
31553156
if (super_types[mddev->major_version].
31563157
validate_super(mddev, rdev)) {
31573158
printk(KERN_WARNING "md: kicking non-fresh %s"
@@ -3160,6 +3161,15 @@ static void analyze_sbs(struct mddev *mddev)
31603161
kick_rdev_from_array(rdev);
31613162
continue;
31623163
}
3164+
/* No device should have a Candidate flag
3165+
* when reading devices
3166+
*/
3167+
if (test_bit(Candidate, &rdev->flags)) {
3168+
pr_info("md: kicking Cluster Candidate %s from array!\n",
3169+
bdevname(rdev->bdev, b));
3170+
kick_rdev_from_array(rdev);
3171+
}
3172+
}
31633173
if (mddev->level == LEVEL_MULTIPATH) {
31643174
rdev->desc_nr = i++;
31653175
rdev->raid_disk = rdev->desc_nr;
@@ -5655,7 +5665,6 @@ static int get_array_info(struct mddev *mddev, void __user *arg)
56555665
info.state |= (1<<MD_SB_BITMAP_PRESENT);
56565666
if (mddev_is_clustered(mddev))
56575667
info.state |= (1<<MD_SB_CLUSTERED);
5658-
56595668
info.active_disks = insync;
56605669
info.working_disks = working;
56615670
info.failed_disks = failed;
@@ -5744,6 +5753,13 @@ static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
57445753
struct md_rdev *rdev;
57455754
dev_t dev = MKDEV(info->major,info->minor);
57465755

5756+
if (mddev_is_clustered(mddev) &&
5757+
!(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
5758+
pr_err("%s: Cannot add to clustered mddev. Try --cluster-add\n",
5759+
mdname(mddev));
5760+
return -EINVAL;
5761+
}
5762+
57475763
if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
57485764
return -EOVERFLOW;
57495765

@@ -5830,6 +5846,25 @@ static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
58305846
else
58315847
clear_bit(WriteMostly, &rdev->flags);
58325848

5849+
/*
5850+
* check whether the device shows up in other nodes
5851+
*/
5852+
if (mddev_is_clustered(mddev)) {
5853+
if (info->state & (1 << MD_DISK_CANDIDATE)) {
5854+
/* Through --cluster-confirm */
5855+
set_bit(Candidate, &rdev->flags);
5856+
md_cluster_ops->new_disk_ack(mddev, true);
5857+
} else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5858+
/* --add initiated by this node */
5859+
err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5860+
if (err) {
5861+
md_cluster_ops->add_new_disk_finish(mddev);
5862+
export_rdev(rdev);
5863+
return err;
5864+
}
5865+
}
5866+
}
5867+
58335868
rdev->raid_disk = -1;
58345869
err = bind_rdev_to_array(rdev, mddev);
58355870
if (!err && !mddev->pers->hot_remove_disk) {
@@ -5855,6 +5890,9 @@ static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
58555890
if (!err)
58565891
md_new_event(mddev);
58575892
md_wakeup_thread(mddev->thread);
5893+
if (mddev_is_clustered(mddev) &&
5894+
(info->state & (1 << MD_DISK_CLUSTER_ADD)))
5895+
md_cluster_ops->add_new_disk_finish(mddev);
58585896
return err;
58595897
}
58605898

@@ -6456,6 +6494,7 @@ static inline bool md_ioctl_valid(unsigned int cmd)
64566494
case SET_DISK_FAULTY:
64576495
case STOP_ARRAY:
64586496
case STOP_ARRAY_RO:
6497+
case CLUSTERED_DISK_NACK:
64596498
return true;
64606499
default:
64616500
return false;
@@ -6728,6 +6767,13 @@ static int md_ioctl(struct block_device *bdev, fmode_t mode,
67286767
goto unlock;
67296768
}
67306769

6770+
case CLUSTERED_DISK_NACK:
6771+
if (mddev_is_clustered(mddev))
6772+
md_cluster_ops->new_disk_ack(mddev, false);
6773+
else
6774+
err = -EINVAL;
6775+
goto unlock;
6776+
67316777
case HOT_ADD_DISK:
67326778
err = hot_add_disk(mddev, new_decode_dev(arg));
67336779
goto unlock;

‎drivers/md/md.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ enum flag_bits {
171171
* a want_replacement device with same
172172
* raid_disk number.
173173
*/
174+
Candidate, /* For clustered environments only:
175+
* This device is seen locally but not
176+
* by the whole cluster
177+
*/
174178
};
175179

176180
#define BB_LEN_MASK (0x00000000000001FFULL)
@@ -666,6 +670,7 @@ extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
666670

667671
extern void md_unplug(struct blk_plug_cb *cb, bool from_schedule);
668672
extern void md_reload_sb(struct mddev *mddev);
673+
extern void md_update_sb(struct mddev *mddev, int force);
669674
static inline int mddev_check_plugged(struct mddev *mddev)
670675
{
671676
return !!blk_check_plugged(md_unplug, mddev,

‎drivers/md/raid1.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ static int raid1_spare_active(struct mddev *mddev)
15711571
struct md_rdev *rdev = conf->mirrors[i].rdev;
15721572
struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
15731573
if (repl
1574+
&& !test_bit(Candidate, &repl->flags)
15741575
&& repl->recovery_offset == MaxSector
15751576
&& !test_bit(Faulty, &repl->flags)
15761577
&& !test_and_set_bit(In_sync, &repl->flags)) {

‎include/uapi/linux/raid/md_p.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
#define MD_DISK_ACTIVE 1 /* disk is running or spare disk */
7979
#define MD_DISK_SYNC 2 /* disk is in sync with the raid set */
8080
#define MD_DISK_REMOVED 3 /* disk is in sync with the raid set */
81+
#define MD_DISK_CLUSTER_ADD 4 /* Initiate a disk add across the cluster
82+
* For clustered enviroments only.
83+
*/
84+
#define MD_DISK_CANDIDATE 5 /* disk is added as spare (local) until confirmed
85+
* For clustered enviroments only.
86+
*/
8187

8288
#define MD_DISK_WRITEMOSTLY 9 /* disk is "write-mostly" is RAID1 config.
8389
* read requests will only be sent here in

‎include/uapi/linux/raid/md_u.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define STOP_ARRAY _IO (MD_MAJOR, 0x32)
6363
#define STOP_ARRAY_RO _IO (MD_MAJOR, 0x33)
6464
#define RESTART_ARRAY_RW _IO (MD_MAJOR, 0x34)
65+
#define CLUSTERED_DISK_NACK _IO (MD_MAJOR, 0x35)
6566

6667
/* 63 partitions with the alternate major number (mdp) */
6768
#define MdpMinorShift 6

0 commit comments

Comments
 (0)