blob: b1c251872800e2bced0183167b2419b2cd4a0358 [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02003 * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved.
NeilBrown9d09e662011-01-13 20:00:02 +00004 *
5 * This file is released under the GPL.
6 */
7
8#include <linux/slab.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -04009#include <linux/module.h>
NeilBrown9d09e662011-01-13 20:00:02 +000010
11#include "md.h"
Jonathan Brassow32737272011-08-02 12:32:07 +010012#include "raid1.h"
NeilBrown9d09e662011-01-13 20:00:02 +000013#include "raid5.h"
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050014#include "raid10.h"
NeilBrown9d09e662011-01-13 20:00:02 +000015#include "bitmap.h"
16
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010017#include <linux/device-mapper.h>
18
NeilBrown9d09e662011-01-13 20:00:02 +000019#define DM_MSG_PREFIX "raid"
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +020020#define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
NeilBrown9d09e662011-01-13 20:00:02 +000021
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +020022/*
23 * Minimum sectors of free reshape space per raid device
24 */
25#define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
26
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +020027static bool devices_handle_discard_safely = false;
28
NeilBrown9d09e662011-01-13 20:00:02 +000029/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010030 * The following flags are used by dm-raid.c to set up the array state.
31 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000032 */
Mike Snitzer43157842016-05-30 13:03:37 -040033#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000034
35struct raid_dev {
36 /*
37 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040038 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000039 * ti->len and give more flexibility in altering size and
40 * characteristics.
41 *
42 * While it is possible for this device to be associated
43 * with a different physical device than the data_dev, it
44 * is intended for it to be the same.
45 * |--------- Physical Device ---------|
46 * |- meta_dev -|------ data_dev ------|
47 */
48 struct dm_dev *meta_dev;
49 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110050 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000051};
52
53/*
Mike Snitzer42863252016-06-02 12:27:46 -040054 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020055 *
56 * 1 = no flag value
57 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000058 */
Mike Snitzer42863252016-06-02 12:27:46 -040059#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
60#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
61#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
62#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
63#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
64#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
65#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
66#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
67#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
68#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
69#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
70#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
Mike Snitzer9b6e5422016-06-02 11:48:09 -040071/* New for v1.9.0 */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +020072#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
Mike Snitzer42863252016-06-02 12:27:46 -040073#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
74#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
75
76/*
77 * Flags for rs->ctr_flags field.
78 */
79#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
80#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
81#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
82#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
83#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
84#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
85#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
86#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
87#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
88#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
89#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
90#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
91#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
92#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
93#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050094
Heinz Mauelshagenf0902792016-05-19 18:49:27 +020095/*
96 * Definitions of various constructor flags to
97 * be used in checks of valid / invalid flags
98 * per raid level.
99 */
100/* Define all any sync flags */
101#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
102
103/* Define flags for options without argument (e.g. 'nosync') */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200104#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
105 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200106
107/* Define flags for options with one argument (e.g. 'delta_disks +2') */
108#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
109 CTR_FLAG_WRITE_MOSTLY | \
110 CTR_FLAG_DAEMON_SLEEP | \
111 CTR_FLAG_MIN_RECOVERY_RATE | \
112 CTR_FLAG_MAX_RECOVERY_RATE | \
113 CTR_FLAG_MAX_WRITE_BEHIND | \
114 CTR_FLAG_STRIPE_CACHE | \
115 CTR_FLAG_REGION_SIZE | \
116 CTR_FLAG_RAID10_COPIES | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200117 CTR_FLAG_RAID10_FORMAT | \
118 CTR_FLAG_DELTA_DISKS | \
119 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200120
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200121/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200122
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200123/* "raid0" does only accept data offset */
124#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200125
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200126/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
127#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
128 CTR_FLAG_REBUILD | \
129 CTR_FLAG_WRITE_MOSTLY | \
130 CTR_FLAG_DAEMON_SLEEP | \
131 CTR_FLAG_MIN_RECOVERY_RATE | \
132 CTR_FLAG_MAX_RECOVERY_RATE | \
133 CTR_FLAG_MAX_WRITE_BEHIND | \
134 CTR_FLAG_REGION_SIZE | \
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +0200135 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200136 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200137
138/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200139#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
140 CTR_FLAG_REBUILD | \
141 CTR_FLAG_DAEMON_SLEEP | \
142 CTR_FLAG_MIN_RECOVERY_RATE | \
143 CTR_FLAG_MAX_RECOVERY_RATE | \
144 CTR_FLAG_REGION_SIZE | \
145 CTR_FLAG_RAID10_COPIES | \
146 CTR_FLAG_RAID10_FORMAT | \
147 CTR_FLAG_DELTA_DISKS | \
148 CTR_FLAG_DATA_OFFSET | \
149 CTR_FLAG_RAID10_USE_NEAR_SETS)
150
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200151/*
152 * "raid4/5/6" do not accept any raid1 or raid10 specific options
153 *
154 * "raid6" does not accept "nosync", because it is not guaranteed
155 * that both parity and q-syndrome are being written properly with
156 * any writes
157 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200158#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
159 CTR_FLAG_REBUILD | \
160 CTR_FLAG_DAEMON_SLEEP | \
161 CTR_FLAG_MIN_RECOVERY_RATE | \
162 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200163 CTR_FLAG_MAX_WRITE_BEHIND | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200164 CTR_FLAG_STRIPE_CACHE | \
165 CTR_FLAG_REGION_SIZE | \
166 CTR_FLAG_DELTA_DISKS | \
167 CTR_FLAG_DATA_OFFSET)
168
169#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
170 CTR_FLAG_REBUILD | \
171 CTR_FLAG_DAEMON_SLEEP | \
172 CTR_FLAG_MIN_RECOVERY_RATE | \
173 CTR_FLAG_MAX_RECOVERY_RATE | \
174 CTR_FLAG_MAX_WRITE_BEHIND | \
175 CTR_FLAG_STRIPE_CACHE | \
176 CTR_FLAG_REGION_SIZE | \
177 CTR_FLAG_DELTA_DISKS | \
178 CTR_FLAG_DATA_OFFSET)
179/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200180
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200181/*
182 * Flags for rs->runtime_flags field
183 * (RT_FLAG prefix meaning "runtime flag")
184 *
185 * These are all internal and used to define runtime state,
186 * e.g. to prevent another resume from preresume processing
187 * the raid set all over again.
188 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200189#define RT_FLAG_RS_PRERESUMED 0
190#define RT_FLAG_RS_RESUMED 1
191#define RT_FLAG_RS_BITMAP_LOADED 2
192#define RT_FLAG_UPDATE_SBS 3
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200193#define RT_FLAG_RESHAPE_RS 4
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200194
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200195/* Array elements of 64 bit needed for rebuild/failed disk bits */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200196#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
197
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200198/*
199 * raid set level, layout and chunk sectors backup/restore
200 */
201struct rs_layout {
202 int new_level;
203 int new_layout;
204 int new_chunk_sectors;
205};
206
NeilBrown9d09e662011-01-13 20:00:02 +0000207struct raid_set {
208 struct dm_target *ti;
209
Jonathan Brassow34f8ac62012-01-27 14:53:53 -0600210 uint32_t bitmap_loaded;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200211 uint32_t stripe_cache_entries;
Mike Snitzer42863252016-06-02 12:27:46 -0400212 unsigned long ctr_flags;
213 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200214
215 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000216
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200217 int raid_disks;
218 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200219 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200220 int raid10_copies;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +0200221 int requested_bitmap_chunk_sectors;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200222
NeilBrownfd01b882011-10-11 16:47:53 +1100223 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000224 struct raid_type *raid_type;
225 struct dm_target_callbacks callbacks;
226
227 struct raid_dev dev[0];
228};
229
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200230static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200231{
232 struct mddev *mddev = &rs->md;
233
234 l->new_level = mddev->new_level;
235 l->new_layout = mddev->new_layout;
236 l->new_chunk_sectors = mddev->new_chunk_sectors;
237}
238
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200239static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200240{
241 struct mddev *mddev = &rs->md;
242
243 mddev->new_level = l->new_level;
244 mddev->new_layout = l->new_layout;
245 mddev->new_chunk_sectors = l->new_chunk_sectors;
246}
247
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200248/* raid10 algorithms (i.e. formats) */
249#define ALGORITHM_RAID10_DEFAULT 0
250#define ALGORITHM_RAID10_NEAR 1
251#define ALGORITHM_RAID10_OFFSET 2
252#define ALGORITHM_RAID10_FAR 3
253
NeilBrown9d09e662011-01-13 20:00:02 +0000254/* Supported raid types and properties. */
255static struct raid_type {
256 const char *name; /* RAID algorithm. */
257 const char *descr; /* Descriptor text for logging. */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200258 const unsigned int parity_devs; /* # of parity devices. */
259 const unsigned int minimal_devs;/* minimal # of devices in set. */
260 const unsigned int level; /* RAID level. */
261 const unsigned int algorithm; /* RAID algorithm. */
NeilBrown9d09e662011-01-13 20:00:02 +0000262} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400263 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
264 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
265 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200266 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400267 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
268 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
269 {"raid4", "raid4 (dedicated last parity disk)", 1, 2, 4, ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */
270 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
271 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
272 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
273 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
274 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
275 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
276 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
277 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
278 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
279 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
280 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
281 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
282 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000283};
284
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200285/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400286static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200287{
288 return v >= min && v <= max;
289}
290
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200291/* All table line arguments are defined here */
292static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400293 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200294 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400295} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200296 { CTR_FLAG_SYNC, "sync"},
297 { CTR_FLAG_NOSYNC, "nosync"},
298 { CTR_FLAG_REBUILD, "rebuild"},
299 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
300 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
301 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
302 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
Heinz Mauelshagen65359ee2016-06-24 21:32:25 +0200303 { CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200304 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
305 { CTR_FLAG_REGION_SIZE, "region_size"},
306 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
307 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200308 { CTR_FLAG_DATA_OFFSET, "data_offset"},
309 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
310 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200311};
312
313/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400314static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200315{
316 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400317 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200318
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400319 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400320 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200321 return anf->name;
322
323 } else
324 DMERR("%s called with more than one flag!", __func__);
325
326 return NULL;
327}
328
329/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200330 * Bool helpers to test for various raid levels of a raid set.
331 * It's level as reported by the superblock rather than
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200332 * the requested raid_type passed to the constructor.
333 */
334/* Return true, if raid set in @rs is raid0 */
335static bool rs_is_raid0(struct raid_set *rs)
336{
337 return !rs->md.level;
338}
339
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200340/* Return true, if raid set in @rs is raid1 */
341static bool rs_is_raid1(struct raid_set *rs)
342{
343 return rs->md.level == 1;
344}
345
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200346/* Return true, if raid set in @rs is raid10 */
347static bool rs_is_raid10(struct raid_set *rs)
348{
349 return rs->md.level == 10;
350}
351
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200352/* Return true, if raid set in @rs is level 6 */
353static bool rs_is_raid6(struct raid_set *rs)
354{
355 return rs->md.level == 6;
356}
357
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200358/* Return true, if raid set in @rs is level 4, 5 or 6 */
359static bool rs_is_raid456(struct raid_set *rs)
360{
361 return __within_range(rs->md.level, 4, 6);
362}
363
364/* Return true, if raid set in @rs is reshapable */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200365static bool __is_raid10_far(int layout);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200366static bool rs_is_reshapable(struct raid_set *rs)
367{
368 return rs_is_raid456(rs) ||
369 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
370}
371
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200372/* Return true, if raid set in @rs is recovering */
373static bool rs_is_recovering(struct raid_set *rs)
374{
Heinz Mauelshagen345a6cd2016-06-25 02:42:54 +0200375 return rs->md.recovery_cp < rs->dev[0].rdev.sectors;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200376}
377
378/* Return true, if raid set in @rs is reshaping */
379static bool rs_is_reshaping(struct raid_set *rs)
380{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200381 return rs->md.reshape_position != MaxSector;
382}
383
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200384/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200385 * bool helpers to test for various raid levels of a raid type @rt
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200386 */
387
388/* Return true, if raid type in @rt is raid0 */
389static bool rt_is_raid0(struct raid_type *rt)
390{
391 return !rt->level;
392}
393
394/* Return true, if raid type in @rt is raid1 */
395static bool rt_is_raid1(struct raid_type *rt)
396{
397 return rt->level == 1;
398}
399
400/* Return true, if raid type in @rt is raid10 */
401static bool rt_is_raid10(struct raid_type *rt)
402{
403 return rt->level == 10;
404}
405
406/* Return true, if raid type in @rt is raid4/5 */
407static bool rt_is_raid45(struct raid_type *rt)
408{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400409 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200410}
411
412/* Return true, if raid type in @rt is raid6 */
413static bool rt_is_raid6(struct raid_type *rt)
414{
415 return rt->level == 6;
416}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200417
418/* Return true, if raid type in @rt is raid4/5/6 */
419static bool rt_is_raid456(struct raid_type *rt)
420{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400421 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200422}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200423/* END: raid level bools */
424
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200425/* Return valid ctr flags for the raid level of @rs */
426static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200427{
428 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200429 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200430 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200431 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200432 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200433 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200434 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200435 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200436 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200437 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200438
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200439 return 0;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200440}
441
442/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200443 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200444 *
445 * Has to be called after parsing of the ctr flags!
446 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200447static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200448{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200449 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400450 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400451 return -EINVAL;
452 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200453
454 return 0;
455}
456
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200457/* MD raid10 bit definitions and helpers */
458#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
459#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
460#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
461#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
462
463/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400464static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200465{
466 return layout & 0xFF;
467}
468
469/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400470static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200471{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400472 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200473}
474
475/* Return true if md raid10 offset for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200476static bool __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200477{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200478 return !!(layout & RAID10_OFFSET);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200479}
480
481/* Return true if md raid10 near for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200482static bool __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200483{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400484 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200485}
486
487/* Return true if md raid10 far for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200488static bool __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200489{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400490 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200491}
492
493/* Return md raid10 layout string for @layout */
494static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100495{
496 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200497 * Bit 16 stands for "offset"
498 * (i.e. adjacent stripes hold copies)
499 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100500 * Refer to MD's raid10.c for details
501 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400502 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100503 return "offset";
504
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400505 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100506 return "near";
507
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400508 WARN_ON(__raid10_far_copies(layout) < 2);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200509
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100510 return "far";
511}
512
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200513/* Return md raid10 algorithm for @name */
Arnd Bergmann68c1c4d2016-06-16 11:03:25 +0200514static int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500515{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200516 if (!strcasecmp(name, "near"))
517 return ALGORITHM_RAID10_NEAR;
518 else if (!strcasecmp(name, "offset"))
519 return ALGORITHM_RAID10_OFFSET;
520 else if (!strcasecmp(name, "far"))
521 return ALGORITHM_RAID10_FAR;
522
523 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500524}
525
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200526/* Return md raid10 copies for @layout */
527static unsigned int raid10_md_layout_to_copies(int layout)
528{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200529 return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200530}
531
532/* Return md raid10 format id for @format string */
533static int raid10_format_to_md_layout(struct raid_set *rs,
534 unsigned int algorithm,
535 unsigned int copies)
536{
537 unsigned int n = 1, f = 1, r = 0;
538
539 /*
540 * MD resilienece flaw:
541 *
542 * enabling use_far_sets for far/offset formats causes copies
543 * to be colocated on the same devs together with their origins!
544 *
545 * -> disable it for now in the definition above
546 */
547 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
548 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100549 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200550
551 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100552 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200553 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400554 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200555 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100556
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200557 } else if (algorithm == ALGORITHM_RAID10_FAR) {
558 f = copies;
559 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400560 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200561 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100562
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200563 } else
564 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100565
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200566 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
567}
568/* END: MD raid10 bit definitions and helpers */
569
570/* Check for any of the raid10 algorithms */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200571static bool __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200572{
573 if (rtp->level == 10) {
574 switch (rtp->algorithm) {
575 case ALGORITHM_RAID10_DEFAULT:
576 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400577 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200578 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400579 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200580 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400581 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200582 default:
583 break;
584 }
585 }
586
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200587 return false;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500588}
589
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200590/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200591static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000592{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200593 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000594
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200595 while (rtp-- > raid_types)
596 if (!strcasecmp(rtp->name, name))
597 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000598
599 return NULL;
600}
601
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200602/* Return raid_type for @name based derived from @level and @layout */
603static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
604{
605 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
606
607 while (rtp-- > raid_types) {
608 /* RAID10 special checks based on @layout flags/properties */
609 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400610 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200611 return rtp;
612 }
613
614 return NULL;
615}
616
617/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200618 * Conditionally change bdev capacity of @rs
619 * in case of a disk add/remove reshape
620 */
621static void rs_set_capacity(struct raid_set *rs)
622{
623 struct mddev *mddev = &rs->md;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200624 struct md_rdev *rdev;
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200625 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200626
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200627 /*
628 * raid10 sets rdev->sector to the device size, which
629 * is unintended in case of out-of-place reshaping
630 */
631 rdev_for_each(rdev, mddev)
632 rdev->sectors = mddev->dev_sectors;
633
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200634 set_capacity(gendisk, mddev->array_sectors);
635 revalidate_disk(gendisk);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200636}
637
638/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200639 * Set the mddev properties in @rs to the current
640 * ones retrieved from the freshest superblock
641 */
642static void rs_set_cur(struct raid_set *rs)
643{
644 struct mddev *mddev = &rs->md;
645
646 mddev->new_level = mddev->level;
647 mddev->new_layout = mddev->layout;
648 mddev->new_chunk_sectors = mddev->chunk_sectors;
649}
650
651/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200652 * Set the mddev properties in @rs to the new
653 * ones requested by the ctr
654 */
655static void rs_set_new(struct raid_set *rs)
656{
657 struct mddev *mddev = &rs->md;
658
659 mddev->level = mddev->new_level;
660 mddev->layout = mddev->new_layout;
661 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200662 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200663 mddev->delta_disks = 0;
664}
665
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400666static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200667 unsigned int raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000668{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200669 unsigned int i;
NeilBrown9d09e662011-01-13 20:00:02 +0000670 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000671
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400672 if (raid_devs <= raid_type->parity_devs) {
673 ti->error = "Insufficient number of devices";
674 return ERR_PTR(-EINVAL);
675 }
NeilBrown9d09e662011-01-13 20:00:02 +0000676
NeilBrown9d09e662011-01-13 20:00:02 +0000677 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400678 if (!rs) {
679 ti->error = "Cannot allocate raid context";
680 return ERR_PTR(-ENOMEM);
681 }
NeilBrown9d09e662011-01-13 20:00:02 +0000682
683 mddev_init(&rs->md);
684
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200685 rs->raid_disks = raid_devs;
686 rs->delta_disks = 0;
687
NeilBrown9d09e662011-01-13 20:00:02 +0000688 rs->ti = ti;
689 rs->raid_type = raid_type;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200690 rs->stripe_cache_entries = 256;
NeilBrown9d09e662011-01-13 20:00:02 +0000691 rs->md.raid_disks = raid_devs;
692 rs->md.level = raid_type->level;
693 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000694 rs->md.layout = raid_type->algorithm;
695 rs->md.new_layout = rs->md.layout;
696 rs->md.delta_disks = 0;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200697 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +0000698
699 for (i = 0; i < raid_devs; i++)
700 md_rdev_init(&rs->dev[i].rdev);
701
702 /*
703 * Remaining items to be initialized by further RAID params:
704 * rs->md.persistent
705 * rs->md.external
706 * rs->md.chunk_sectors
707 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100708 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000709 */
710
711 return rs;
712}
713
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400714static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000715{
716 int i;
717
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200718 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100719 if (rs->dev[i].meta_dev)
720 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000721 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000722 if (rs->dev[i].data_dev)
723 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100724 }
NeilBrown9d09e662011-01-13 20:00:02 +0000725
726 kfree(rs);
727}
728
729/*
730 * For every device we have two words
731 * <meta_dev>: meta device name or '-' if missing
732 * <data_dev>: data device name or '-' if missing
733 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100734 * The following are permitted:
735 * - -
736 * - <data_dev>
737 * <meta_dev> <data_dev>
738 *
739 * The following is not allowed:
740 * <meta_dev> -
741 *
742 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400743 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000744 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200745static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000746{
747 int i;
748 int rebuild = 0;
749 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200750 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200751 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000752
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200753 /* Put off the number of raid devices argument to get to dev pairs */
754 arg = dm_shift_arg(as);
755 if (!arg)
756 return -EINVAL;
757
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200758 for (i = 0; i < rs->raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000759 rs->dev[i].rdev.raid_disk = i;
760
761 rs->dev[i].meta_dev = NULL;
762 rs->dev[i].data_dev = NULL;
763
764 /*
765 * There are no offsets, since there is a separate device
766 * for data and metadata.
767 */
768 rs->dev[i].rdev.data_offset = 0;
769 rs->dev[i].rdev.mddev = &rs->md;
770
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200771 arg = dm_shift_arg(as);
772 if (!arg)
773 return -EINVAL;
774
775 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400776 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
777 &rs->dev[i].meta_dev);
778 if (r) {
779 rs->ti->error = "RAID metadata device lookup failure";
780 return r;
781 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100782
783 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400784 if (!rs->dev[i].rdev.sb_page) {
785 rs->ti->error = "Failed to allocate superblock page";
786 return -ENOMEM;
787 }
NeilBrown9d09e662011-01-13 20:00:02 +0000788 }
789
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200790 arg = dm_shift_arg(as);
791 if (!arg)
792 return -EINVAL;
793
794 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000795 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400796 (!rs->dev[i].rdev.recovery_offset)) {
797 rs->ti->error = "Drive designated for rebuild not specified";
798 return -EINVAL;
799 }
NeilBrown9d09e662011-01-13 20:00:02 +0000800
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400801 if (rs->dev[i].meta_dev) {
802 rs->ti->error = "No data device supplied with metadata device";
803 return -EINVAL;
804 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100805
NeilBrown9d09e662011-01-13 20:00:02 +0000806 continue;
807 }
808
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400809 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
810 &rs->dev[i].data_dev);
811 if (r) {
812 rs->ti->error = "RAID device lookup failure";
813 return r;
814 }
NeilBrown9d09e662011-01-13 20:00:02 +0000815
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100816 if (rs->dev[i].meta_dev) {
817 metadata_available = 1;
818 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
819 }
NeilBrown9d09e662011-01-13 20:00:02 +0000820 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200821 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000822 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
823 rebuild++;
824 }
825
826 if (metadata_available) {
827 rs->md.external = 0;
828 rs->md.persistent = 1;
829 rs->md.major_version = 2;
830 } else if (rebuild && !rs->md.recovery_cp) {
831 /*
832 * Without metadata, we will not be able to tell if the array
833 * is in-sync or not - we must assume it is not. Therefore,
834 * it is impossible to rebuild a drive.
835 *
836 * Even if there is metadata, the on-disk information may
837 * indicate that the array is not in-sync and it will then
838 * fail at that time.
839 *
840 * User could specify 'nosync' option if desperate.
841 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400842 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
843 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000844 }
845
846 return 0;
847}
848
849/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100850 * validate_region_size
851 * @rs
852 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
853 *
854 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
855 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
856 *
857 * Returns: 0 on success, -EINVAL on failure.
858 */
859static int validate_region_size(struct raid_set *rs, unsigned long region_size)
860{
861 unsigned long min_region_size = rs->ti->len / (1 << 21);
862
863 if (!region_size) {
864 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400865 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100866 */
867 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000868 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400869 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100870 DMINFO("Choosing default region size of %lu sectors",
871 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100872 } else {
873 DMINFO("Choosing default region size of 4MiB");
874 region_size = 1 << 13; /* sectors */
875 }
876 } else {
877 /*
878 * Validate user-supplied value.
879 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400880 if (region_size > rs->ti->len) {
881 rs->ti->error = "Supplied region size is too large";
882 return -EINVAL;
883 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100884
885 if (region_size < min_region_size) {
886 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
887 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400888 rs->ti->error = "Supplied region size is too small";
889 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100890 }
891
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400892 if (!is_power_of_2(region_size)) {
893 rs->ti->error = "Region size is not a power of 2";
894 return -EINVAL;
895 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100896
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400897 if (region_size < rs->md.chunk_sectors) {
898 rs->ti->error = "Region size is smaller than the chunk size";
899 return -EINVAL;
900 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100901 }
902
903 /*
904 * Convert sectors to bytes.
905 */
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +0200906 rs->md.bitmap_info.chunksize = to_bytes(region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100907
908 return 0;
909}
910
911/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600912 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100913 * @rs
914 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600915 * Determine if there are enough devices in the array that haven't
916 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100917 *
918 * Returns: 0 on success, -EINVAL on failure.
919 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600920static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100921{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200922 unsigned int i, rebuild_cnt = 0;
923 unsigned int rebuilds_per_group = 0, copies;
924 unsigned int group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100925
Jonathan Brassoweb649122012-10-11 13:40:09 +1100926 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600927 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
928 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100929 rebuild_cnt++;
930
931 switch (rs->raid_type->level) {
932 case 1:
933 if (rebuild_cnt >= rs->md.raid_disks)
934 goto too_many;
935 break;
936 case 4:
937 case 5:
938 case 6:
939 if (rebuild_cnt > rs->raid_type->parity_devs)
940 goto too_many;
941 break;
942 case 10:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200943 copies = raid10_md_layout_to_copies(rs->md.new_layout);
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100944 if (rebuild_cnt < copies)
945 break;
946
947 /*
948 * It is possible to have a higher rebuild count for RAID10,
949 * as long as the failed devices occur in different mirror
950 * groups (i.e. different stripes).
951 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100952 * When checking "near" format, make sure no adjacent devices
953 * have failed beyond what can be handled. In addition to the
954 * simple case where the number of devices is a multiple of the
955 * number of copies, we must also handle cases where the number
956 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -0400957 * E.g. dev1 dev2 dev3 dev4 dev5
958 * A A B B C
959 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100960 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200961 if (__is_raid10_near(rs->md.new_layout)) {
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200962 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100963 if (!(i % copies))
964 rebuilds_per_group = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200965 if ((!rs->dev[i].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200966 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100967 (++rebuilds_per_group >= copies))
968 goto too_many;
969 }
970 break;
971 }
972
973 /*
974 * When checking "far" and "offset" formats, we need to ensure
975 * that the device that holds its copy is not also dead or
976 * being rebuilt. (Note that "far" and "offset" formats only
977 * support two copies right now. These formats also only ever
978 * use the 'use_far_sets' variant.)
979 *
980 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -0400981 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100982 * results in the need to treat the last (potentially larger)
983 * set differently.
984 */
985 group_size = (rs->md.raid_disks / copies);
986 last_group_start = (rs->md.raid_disks / group_size) - 1;
987 last_group_start *= group_size;
988 for (i = 0; i < rs->md.raid_disks; i++) {
989 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600990 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100991 if ((!rs->dev[i].rdev.sb_page ||
992 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100993 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100994 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100995 }
996 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100997 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600998 if (rebuild_cnt)
999 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001000 }
1001
1002 return 0;
1003
1004too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +11001005 return -EINVAL;
1006}
1007
1008/*
NeilBrown9d09e662011-01-13 20:00:02 +00001009 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +00001010 * <chunk_size> [optional_args]
1011 *
Jonathan Brassow32737272011-08-02 12:32:07 +01001012 * Argument definitions
1013 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -04001014 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +01001015 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -04001016 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +00001017 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +01001018 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -04001019 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +00001020 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
1021 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001022 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +00001023 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
1024 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -04001025 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001026 *
1027 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -04001028 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001029 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +00001030 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001031static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001032 unsigned int num_raid_params)
NeilBrown9d09e662011-01-13 20:00:02 +00001033{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001034 int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001035 unsigned int raid10_copies = 2;
1036 unsigned int i, write_mostly = 0;
1037 unsigned int region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +01001038 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001039 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001040 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001041 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001042
1043 arg = dm_shift_arg(as);
1044 num_raid_params--; /* Account for chunk_size argument */
1045
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001046 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001047 rs->ti->error = "Bad numerical argument given for chunk_size";
1048 return -EINVAL;
1049 }
NeilBrown9d09e662011-01-13 20:00:02 +00001050
1051 /*
1052 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001053 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001054 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001055 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001056 if (value)
1057 DMERR("Ignoring chunk size parameter for RAID 1");
1058 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001059 } else if (!is_power_of_2(value)) {
1060 rs->ti->error = "Chunk size must be a power of 2";
1061 return -EINVAL;
1062 } else if (value < 8) {
1063 rs->ti->error = "Chunk size value is too small";
1064 return -EINVAL;
1065 }
NeilBrown9d09e662011-01-13 20:00:02 +00001066
1067 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001068
1069 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001070 * We set each individual device as In_sync with a completed
1071 * 'recovery_offset'. If there has been a device failure or
1072 * replacement then one of the following cases applies:
1073 *
1074 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001075 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001076 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001077 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001078 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001079 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001080 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001081 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001082 *
1083 * What is found in the superblocks of the devices is always
1084 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1085 */
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001086 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001087 set_bit(In_sync, &rs->dev[i].rdev.flags);
1088 rs->dev[i].rdev.recovery_offset = MaxSector;
1089 }
1090
1091 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001092 * Second, parse the unordered optional arguments
1093 */
NeilBrown9d09e662011-01-13 20:00:02 +00001094 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001095 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001096 if (!key) {
1097 rs->ti->error = "Not enough raid parameters given";
1098 return -EINVAL;
1099 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001100
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001101 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001102 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001103 rs->ti->error = "Only one 'nosync' argument allowed";
1104 return -EINVAL;
1105 }
NeilBrown9d09e662011-01-13 20:00:02 +00001106 continue;
1107 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001108 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001109 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001110 rs->ti->error = "Only one 'sync' argument allowed";
1111 return -EINVAL;
1112 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001113 continue;
1114 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001115 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001116 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001117 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1118 return -EINVAL;
1119 }
NeilBrown9d09e662011-01-13 20:00:02 +00001120 continue;
1121 }
1122
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001123 arg = dm_shift_arg(as);
1124 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001125 if (!arg) {
1126 rs->ti->error = "Wrong number of raid parameters given";
1127 return -EINVAL;
1128 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001129
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001130 /*
1131 * Parameters that take a string value are checked here.
1132 */
1133
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001134 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001135 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001136 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1137 return -EINVAL;
1138 }
1139 if (!rt_is_raid10(rt)) {
1140 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1141 return -EINVAL;
1142 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001143 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001144 if (raid10_format < 0) {
1145 rs->ti->error = "Invalid 'raid10_format' value given";
1146 return raid10_format;
1147 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001148 continue;
1149 }
1150
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001151 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001152 rs->ti->error = "Bad numerical argument given in raid params";
1153 return -EINVAL;
1154 }
NeilBrown9d09e662011-01-13 20:00:02 +00001155
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001156 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001157 /*
1158 * "rebuild" is being passed in by userspace to provide
1159 * indexes of replaced devices and to set up additional
1160 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001161 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001162 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001163 rs->ti->error = "Invalid rebuild index given";
1164 return -EINVAL;
1165 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001166
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001167 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1168 rs->ti->error = "rebuild for this index already given";
1169 return -EINVAL;
1170 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001171
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001172 rd = rs->dev + value;
1173 clear_bit(In_sync, &rd->rdev.flags);
1174 clear_bit(Faulty, &rd->rdev.flags);
1175 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001176 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001177 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001178 if (!rt_is_raid1(rt)) {
1179 rs->ti->error = "write_mostly option is only valid for RAID1";
1180 return -EINVAL;
1181 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001182
Mike Snitzerbb91a632016-06-02 12:06:54 -04001183 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001184 rs->ti->error = "Invalid write_mostly index given";
1185 return -EINVAL;
1186 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001187
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001188 write_mostly++;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001189 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001190 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001191 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001192 if (!rt_is_raid1(rt)) {
1193 rs->ti->error = "max_write_behind option is only valid for RAID1";
1194 return -EINVAL;
1195 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001196
Mike Snitzer42863252016-06-02 12:27:46 -04001197 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001198 rs->ti->error = "Only one max_write_behind argument pair allowed";
1199 return -EINVAL;
1200 }
NeilBrown9d09e662011-01-13 20:00:02 +00001201
1202 /*
1203 * In device-mapper, we specify things in sectors, but
1204 * MD records this value in kB
1205 */
1206 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001207 if (value > COUNTER_MAX) {
1208 rs->ti->error = "Max write-behind limit out of range";
1209 return -EINVAL;
1210 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001211
NeilBrown9d09e662011-01-13 20:00:02 +00001212 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001213 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001214 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001215 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1216 return -EINVAL;
1217 }
1218 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1219 rs->ti->error = "daemon sleep period out of range";
1220 return -EINVAL;
1221 }
NeilBrown9d09e662011-01-13 20:00:02 +00001222 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001223 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001224 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001225 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001226 rs->ti->error = "Only one data_offset argument pair allowed";
1227 return -EINVAL;
1228 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001229 /* Ensure sensible data offset */
Heinz Mauelshagen75dd3b92016-06-15 22:27:08 +02001230 if (value < 0 ||
1231 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001232 rs->ti->error = "Bogus data_offset value";
1233 return -EINVAL;
1234 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001235 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001236 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001237 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001238 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001239 rs->ti->error = "Only one delta_disks argument pair allowed";
1240 return -EINVAL;
1241 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001242 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001243 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001244 rs->ti->error = "Too many delta_disk requested";
1245 return -EINVAL;
1246 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001247
1248 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001249 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001250 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001251 rs->ti->error = "Only one stripe_cache argument pair allowed";
1252 return -EINVAL;
1253 }
1254
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001255 if (!rt_is_raid456(rt)) {
1256 rs->ti->error = "Inappropriate argument: stripe_cache";
1257 return -EINVAL;
1258 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001259
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001260 rs->stripe_cache_entries = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001261 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001262 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001263 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1264 return -EINVAL;
1265 }
1266 if (value > INT_MAX) {
1267 rs->ti->error = "min_recovery_rate out of range";
1268 return -EINVAL;
1269 }
NeilBrown9d09e662011-01-13 20:00:02 +00001270 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001271 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Heinz Mauelshagenf15f64d2016-07-27 23:34:01 +02001272 if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001273 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1274 return -EINVAL;
1275 }
1276 if (value > INT_MAX) {
1277 rs->ti->error = "max_recovery_rate out of range";
1278 return -EINVAL;
1279 }
NeilBrown9d09e662011-01-13 20:00:02 +00001280 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001281 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001282 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001283 rs->ti->error = "Only one region_size argument pair allowed";
1284 return -EINVAL;
1285 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001286
Jonathan Brassowc1084562011-08-02 12:32:07 +01001287 region_size = value;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02001288 rs->requested_bitmap_chunk_sectors = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001289 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001290 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001291 rs->ti->error = "Only one raid10_copies argument pair allowed";
1292 return -EINVAL;
1293 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001294
Mike Snitzerbb91a632016-06-02 12:06:54 -04001295 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001296 rs->ti->error = "Bad value for 'raid10_copies'";
1297 return -EINVAL;
1298 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001299
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001300 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001301 } else {
1302 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001303 rs->ti->error = "Unable to parse RAID parameter";
1304 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001305 }
1306 }
1307
Heinz Mauelshagen0d851d12016-06-15 18:43:55 +02001308 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
1309 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
1310 rs->ti->error = "sync and nosync are mutually exclusive";
1311 return -EINVAL;
1312 }
1313
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02001314 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
1315 (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
1316 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
1317 rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
1318 return -EINVAL;
1319 }
1320
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001321 if (write_mostly >= rs->md.raid_disks) {
1322 rs->ti->error = "Can't set all raid1 devices to write_mostly";
1323 return -EINVAL;
1324 }
1325
Jonathan Brassowc1084562011-08-02 12:32:07 +01001326 if (validate_region_size(rs, region_size))
1327 return -EINVAL;
1328
1329 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001330 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001331 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001332 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001333
Mike Snitzer542f9032012-07-27 15:08:00 +01001334 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1335 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001336
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001337 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001338 if (raid10_copies > rs->md.raid_disks) {
1339 rs->ti->error = "Not enough devices to satisfy specification";
1340 return -EINVAL;
1341 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001342
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001343 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001344 if (rs->md.new_layout < 0) {
1345 rs->ti->error = "Error getting raid10 format";
1346 return rs->md.new_layout;
1347 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001348
1349 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001350 if (!rt) {
1351 rs->ti->error = "Failed to recognize new raid10 layout";
1352 return -EINVAL;
1353 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001354
1355 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1356 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001357 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001358 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1359 return -EINVAL;
1360 }
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001361 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001362
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001363 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001364
NeilBrown9d09e662011-01-13 20:00:02 +00001365 /* Assume there are no metadata devices until the drives are parsed */
1366 rs->md.persistent = 0;
1367 rs->md.external = 1;
1368
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001369 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001370 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001371}
1372
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001373/* Set raid4/5/6 cache size */
1374static int rs_set_raid456_stripe_cache(struct raid_set *rs)
1375{
1376 int r;
1377 struct r5conf *conf;
1378 struct mddev *mddev = &rs->md;
1379 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
1380 uint32_t nr_stripes = rs->stripe_cache_entries;
1381
1382 if (!rt_is_raid456(rs->raid_type)) {
1383 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
1384 return -EINVAL;
1385 }
1386
1387 if (nr_stripes < min_stripes) {
1388 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
1389 nr_stripes, min_stripes);
1390 nr_stripes = min_stripes;
1391 }
1392
1393 conf = mddev->private;
1394 if (!conf) {
1395 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
1396 return -EINVAL;
1397 }
1398
1399 /* Try setting number of stripes in raid456 stripe cache */
1400 if (conf->min_nr_stripes != nr_stripes) {
1401 r = raid5_set_cache_size(mddev, nr_stripes);
1402 if (r) {
1403 rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
1404 return r;
1405 }
1406
1407 DMINFO("%u stripe cache entries", nr_stripes);
1408 }
1409
1410 return 0;
1411}
1412
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001413/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1414static unsigned int mddev_data_stripes(struct raid_set *rs)
1415{
1416 return rs->md.raid_disks - rs->raid_type->parity_devs;
1417}
1418
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001419/* Return # of data stripes of @rs (i.e. as of ctr) */
1420static unsigned int rs_data_stripes(struct raid_set *rs)
1421{
1422 return rs->raid_disks - rs->raid_type->parity_devs;
1423}
1424
1425/* Calculate the sectors per device and per array used for @rs */
1426static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1427{
1428 int delta_disks;
1429 unsigned int data_stripes;
1430 struct mddev *mddev = &rs->md;
1431 struct md_rdev *rdev;
1432 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1433
1434 if (use_mddev) {
1435 delta_disks = mddev->delta_disks;
1436 data_stripes = mddev_data_stripes(rs);
1437 } else {
1438 delta_disks = rs->delta_disks;
1439 data_stripes = rs_data_stripes(rs);
1440 }
1441
1442 /* Special raid1 case w/o delta_disks support (yet) */
1443 if (rt_is_raid1(rs->raid_type))
1444 ;
1445 else if (rt_is_raid10(rs->raid_type)) {
1446 if (rs->raid10_copies < 2 ||
1447 delta_disks < 0) {
1448 rs->ti->error = "Bogus raid10 data copies or delta disks";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001449 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001450 }
1451
1452 dev_sectors *= rs->raid10_copies;
1453 if (sector_div(dev_sectors, data_stripes))
1454 goto bad;
1455
1456 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1457 if (sector_div(array_sectors, rs->raid10_copies))
1458 goto bad;
1459
1460 } else if (sector_div(dev_sectors, data_stripes))
1461 goto bad;
1462
1463 else
1464 /* Striped layouts */
1465 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1466
1467 rdev_for_each(rdev, mddev)
1468 rdev->sectors = dev_sectors;
1469
1470 mddev->array_sectors = array_sectors;
1471 mddev->dev_sectors = dev_sectors;
1472
1473 return 0;
1474bad:
1475 rs->ti->error = "Target length not divisible by number of data devices";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001476 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001477}
1478
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001479/* Setup recovery on @rs */
1480static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1481{
1482 /* raid0 does not recover */
1483 if (rs_is_raid0(rs))
1484 rs->md.recovery_cp = MaxSector;
1485 /*
1486 * A raid6 set has to be recovered either
1487 * completely or for the grown part to
1488 * ensure proper parity and Q-Syndrome
1489 */
1490 else if (rs_is_raid6(rs))
1491 rs->md.recovery_cp = dev_sectors;
1492 /*
1493 * Other raid set types may skip recovery
1494 * depending on the 'nosync' flag.
1495 */
1496 else
1497 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
1498 ? MaxSector : dev_sectors;
1499}
1500
1501/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
1502static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1503{
1504 if (!dev_sectors)
1505 /* New raid set or 'sync' flag provided */
1506 __rs_setup_recovery(rs, 0);
1507 else if (dev_sectors == MaxSector)
1508 /* Prevent recovery */
1509 __rs_setup_recovery(rs, MaxSector);
1510 else if (rs->dev[0].rdev.sectors < dev_sectors)
1511 /* Grown raid set */
1512 __rs_setup_recovery(rs, rs->dev[0].rdev.sectors);
1513 else
1514 __rs_setup_recovery(rs, MaxSector);
1515}
1516
NeilBrown9d09e662011-01-13 20:00:02 +00001517static void do_table_event(struct work_struct *ws)
1518{
1519 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1520
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001521 smp_rmb(); /* Make sure we access most actual mddev properties */
1522 if (!rs_is_reshaping(rs))
1523 rs_set_capacity(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001524 dm_table_event(rs->ti->table);
1525}
1526
1527static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1528{
1529 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1530
NeilBrown5c675f82014-12-15 12:56:56 +11001531 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001532}
1533
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001534/*
1535 * Make sure a valid takover (level switch) is being requested on @rs
1536 *
1537 * Conversions of raid sets from one MD personality to another
1538 * have to conform to restrictions which are enforced here.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001539 */
1540static int rs_check_takeover(struct raid_set *rs)
1541{
1542 struct mddev *mddev = &rs->md;
1543 unsigned int near_copies;
1544
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001545 if (rs->md.degraded) {
1546 rs->ti->error = "Can't takeover degraded raid set";
1547 return -EPERM;
1548 }
1549
1550 if (rs_is_reshaping(rs)) {
1551 rs->ti->error = "Can't takeover reshaping raid set";
1552 return -EPERM;
1553 }
1554
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001555 switch (mddev->level) {
1556 case 0:
1557 /* raid0 -> raid1/5 with one disk */
1558 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1559 mddev->raid_disks == 1)
1560 return 0;
1561
1562 /* raid0 -> raid10 */
1563 if (mddev->new_level == 10 &&
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001564 !(rs->raid_disks % mddev->raid_disks))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001565 return 0;
1566
1567 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001568 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001569 mddev->new_layout == ALGORITHM_PARITY_N &&
1570 mddev->raid_disks > 1)
1571 return 0;
1572
1573 break;
1574
1575 case 10:
1576 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001577 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001578 break;
1579
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001580 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001581
1582 /* raid10* -> raid0 */
1583 if (mddev->new_level == 0) {
1584 /* Can takeover raid10_near with raid disks divisable by data copies! */
1585 if (near_copies > 1 &&
1586 !(mddev->raid_disks % near_copies)) {
1587 mddev->raid_disks /= near_copies;
1588 mddev->delta_disks = mddev->raid_disks;
1589 return 0;
1590 }
1591
1592 /* Can takeover raid10_far */
1593 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001594 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001595 return 0;
1596
1597 break;
1598 }
1599
1600 /* raid10_{near,far} -> raid1 */
1601 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001602 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001603 return 0;
1604
1605 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001606 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001607 mddev->raid_disks == 2)
1608 return 0;
1609 break;
1610
1611 case 1:
1612 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001613 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001614 mddev->raid_disks == 2) {
1615 mddev->degraded = 1;
1616 return 0;
1617 }
1618
1619 /* raid1 -> raid0 */
1620 if (mddev->new_level == 0 &&
1621 mddev->raid_disks == 1)
1622 return 0;
1623
1624 /* raid1 -> raid10 */
1625 if (mddev->new_level == 10)
1626 return 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001627 break;
1628
1629 case 4:
1630 /* raid4 -> raid0 */
1631 if (mddev->new_level == 0)
1632 return 0;
1633
1634 /* raid4 -> raid1/5 with 2 disks */
1635 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1636 mddev->raid_disks == 2)
1637 return 0;
1638
1639 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001640 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001641 mddev->layout == ALGORITHM_PARITY_N)
1642 return 0;
1643 break;
1644
1645 case 5:
1646 /* raid5 with parity N -> raid0 */
1647 if (mddev->new_level == 0 &&
1648 mddev->layout == ALGORITHM_PARITY_N)
1649 return 0;
1650
1651 /* raid5 with parity N -> raid4 */
1652 if (mddev->new_level == 4 &&
1653 mddev->layout == ALGORITHM_PARITY_N)
1654 return 0;
1655
1656 /* raid5 with 2 disks -> raid1/4/10 */
1657 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1658 mddev->raid_disks == 2)
1659 return 0;
1660
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001661 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001662 if (mddev->new_level == 6 &&
1663 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001664 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001665 return 0;
1666 break;
1667
1668 case 6:
1669 /* raid6 with parity N -> raid0 */
1670 if (mddev->new_level == 0 &&
1671 mddev->layout == ALGORITHM_PARITY_N)
1672 return 0;
1673
1674 /* raid6 with parity N -> raid4 */
1675 if (mddev->new_level == 4 &&
1676 mddev->layout == ALGORITHM_PARITY_N)
1677 return 0;
1678
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001679 /* raid6_*_n with Q-Syndrome N -> raid5_* */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001680 if (mddev->new_level == 5 &&
1681 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001682 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001683 return 0;
1684
1685 default:
1686 break;
1687 }
1688
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001689 rs->ti->error = "takeover not possible";
1690 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001691}
1692
1693/* True if @rs requested to be taken over */
1694static bool rs_takeover_requested(struct raid_set *rs)
1695{
1696 return rs->md.new_level != rs->md.level;
1697}
1698
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001699/* True if @rs is requested to reshape by ctr */
1700static bool rs_reshape_requested(struct raid_set *rs)
1701{
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001702 bool change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001703 struct mddev *mddev = &rs->md;
1704
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001705 if (rs_takeover_requested(rs))
1706 return false;
1707
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001708 if (!mddev->level)
1709 return false;
1710
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001711 change = mddev->new_layout != mddev->layout ||
1712 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1713 rs->delta_disks;
1714
1715 /* Historical case to support raid1 reshape without delta disks */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001716 if (mddev->level == 1) {
1717 if (rs->delta_disks)
1718 return !!rs->delta_disks;
1719
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001720 return !change &&
1721 mddev->raid_disks != rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001722 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001723
1724 if (mddev->level == 10)
1725 return change &&
1726 !__is_raid10_far(mddev->new_layout) &&
1727 rs->delta_disks >= 0;
1728
1729 return change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001730}
1731
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001732/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001733#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001734
1735/* State flags for sb->flags */
1736#define SB_FLAG_RESHAPE_ACTIVE 0x1
1737#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1738
NeilBrown9d09e662011-01-13 20:00:02 +00001739/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001740 * This structure is never routinely used by userspace, unlike md superblocks.
1741 * Devices with this superblock should only ever be accessed via device-mapper.
1742 */
1743#define DM_RAID_MAGIC 0x64526D44
1744struct dm_raid_superblock {
1745 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001746 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001747
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001748 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1749 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001750
1751 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001752 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001753 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001754
1755 /*
1756 * This offset tracks the progress of the repair or replacement of
1757 * an individual drive.
1758 */
1759 __le64 disk_recovery_offset;
1760
1761 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001762 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001763 * synchronisation/parity calculation.
1764 */
1765 __le64 array_resync_offset;
1766
1767 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001768 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001769 */
1770 __le32 level;
1771 __le32 layout;
1772 __le32 stripe_sectors;
1773
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001774 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001775 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001776 *
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001777 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001778 */
1779
1780 __le32 flags; /* Flags defining array states for reshaping */
1781
1782 /*
1783 * This offset tracks the progress of a raid
1784 * set reshape in order to be able to restart it
1785 */
1786 __le64 reshape_position;
1787
1788 /*
1789 * These define the properties of the array in case of an interrupted reshape
1790 */
1791 __le32 new_level;
1792 __le32 new_layout;
1793 __le32 new_stripe_sectors;
1794 __le32 delta_disks;
1795
1796 __le64 array_sectors; /* Array size in sectors */
1797
1798 /*
1799 * Sector offsets to data on devices (reshaping).
1800 * Needed to support out of place reshaping, thus
1801 * not writing over any stripes whilst converting
1802 * them from old to new layout
1803 */
1804 __le64 data_offset;
1805 __le64 new_data_offset;
1806
1807 __le64 sectors; /* Used device size in sectors */
1808
1809 /*
1810 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001811 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001812 */
1813 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1814
1815 __le32 incompat_features; /* Used to indicate any incompatible features */
1816
1817 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001818} __packed;
1819
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001820/*
1821 * Check for reshape constraints on raid set @rs:
1822 *
1823 * - reshape function non-existent
1824 * - degraded set
1825 * - ongoing recovery
1826 * - ongoing reshape
1827 *
1828 * Returns 0 if none or -EPERM if given constraint
1829 * and error message reference in @errmsg
1830 */
1831static int rs_check_reshape(struct raid_set *rs)
1832{
1833 struct mddev *mddev = &rs->md;
1834
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001835 if (!mddev->pers || !mddev->pers->check_reshape)
1836 rs->ti->error = "Reshape not supported";
1837 else if (mddev->degraded)
1838 rs->ti->error = "Can't reshape degraded raid set";
1839 else if (rs_is_recovering(rs))
1840 rs->ti->error = "Convert request on recovering raid set prohibited";
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001841 else if (rs_is_reshaping(rs))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001842 rs->ti->error = "raid set already reshaping!";
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001843 else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
1844 rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001845 else
1846 return 0;
1847
1848 return -EPERM;
1849}
1850
NeilBrown3cb03002011-10-11 16:45:26 +11001851static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001852{
1853 BUG_ON(!rdev->sb_page);
1854
1855 if (rdev->sb_loaded)
1856 return 0;
1857
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02001858 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01001859 DMERR("Failed to read superblock of device at position %d",
1860 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10001861 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001862 return -EINVAL;
1863 }
1864
1865 rdev->sb_loaded = 1;
1866
1867 return 0;
1868}
1869
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001870static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1871{
1872 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1873 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1874
Mike Snitzer42863252016-06-02 12:27:46 -04001875 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001876 int i = ARRAY_SIZE(sb->extended_failed_devices);
1877
1878 while (i--)
1879 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1880 }
1881}
1882
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001883static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1884{
1885 int i = ARRAY_SIZE(sb->extended_failed_devices);
1886
1887 sb->failed_devices = cpu_to_le64(failed_devices[0]);
1888 while (i--)
1889 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1890}
1891
1892/*
1893 * Synchronize the superblock members with the raid set properties
1894 *
1895 * All superblock data is little endian.
1896 */
NeilBrownfd01b882011-10-11 16:47:53 +11001897static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001898{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001899 bool update_failed_devices = false;
1900 unsigned int i;
1901 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001902 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001903 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001904
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001905 /* No metadata device, no superblock */
1906 if (!rdev->meta_bdev)
1907 return;
1908
1909 BUG_ON(!rdev->sb_page);
1910
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001911 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001912
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001913 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001914
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001915 for (i = 0; i < rs->raid_disks; i++)
1916 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
1917 update_failed_devices = true;
1918 set_bit(i, (void *) failed_devices);
1919 }
1920
1921 if (update_failed_devices)
1922 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001923
1924 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001925 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001926
1927 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1928 sb->array_position = cpu_to_le32(rdev->raid_disk);
1929
1930 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001931
1932 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1933 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1934
1935 sb->level = cpu_to_le32(mddev->level);
1936 sb->layout = cpu_to_le32(mddev->layout);
1937 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001938
1939 sb->new_level = cpu_to_le32(mddev->new_level);
1940 sb->new_layout = cpu_to_le32(mddev->new_layout);
1941 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
1942
1943 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1944
1945 smp_rmb(); /* Make sure we access most recent reshape position */
1946 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1947 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
1948 /* Flag ongoing reshape */
1949 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
1950
1951 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1952 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04001953 } else {
1954 /* Clear reshape flags */
1955 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
1956 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001957
1958 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
1959 sb->data_offset = cpu_to_le64(rdev->data_offset);
1960 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
1961 sb->sectors = cpu_to_le64(rdev->sectors);
Heinz Mauelshagenb2a48722016-08-03 17:47:04 +02001962 sb->incompat_features = cpu_to_le32(0);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001963
1964 /* Zero out the rest of the payload after the size of the superblock */
1965 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001966}
1967
1968/*
1969 * super_load
1970 *
1971 * This function creates a superblock if one is not found on the device
1972 * and will decide which superblock to use if there's a choice.
1973 *
1974 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1975 */
NeilBrown3cb03002011-10-11 16:45:26 +11001976static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001977{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001978 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001979 struct dm_raid_superblock *sb;
1980 struct dm_raid_superblock *refsb;
1981 uint64_t events_sb, events_refsb;
1982
1983 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001984 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1985 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1986 DMERR("superblock size of a logical block is no longer valid");
1987 return -EINVAL;
1988 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001989
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001990 r = read_disk_sb(rdev, rdev->sb_size);
1991 if (r)
1992 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001993
1994 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001995
1996 /*
1997 * Two cases that we want to write new superblocks and rebuild:
1998 * 1) New device (no matching magic number)
1999 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
2000 */
2001 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
2002 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002003 super_sync(rdev->mddev, rdev);
2004
2005 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002006 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002007
2008 /* Force writing of superblocks to disk */
2009 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
2010
2011 /* Any superblock is better than none, choose that if given */
2012 return refdev ? 0 : 1;
2013 }
2014
2015 if (!refdev)
2016 return 1;
2017
2018 events_sb = le64_to_cpu(sb->events);
2019
2020 refsb = page_address(refdev->sb_page);
2021 events_refsb = le64_to_cpu(refsb->events);
2022
2023 return (events_sb > events_refsb) ? 1 : 0;
2024}
2025
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002026static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002027{
2028 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002029 unsigned int d;
2030 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002031 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002032 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002033 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002034 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002035 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002036 struct dm_raid_superblock *sb2;
2037
2038 sb = page_address(rdev->sb_page);
2039 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002040
2041 /*
2042 * Initialise to 1 if this is a new superblock.
2043 */
2044 mddev->events = events_sb ? : 1;
2045
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002046 mddev->reshape_position = MaxSector;
2047
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002048 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002049 * Reshaping is supported, e.g. reshape_position is valid
2050 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002051 */
Mike Snitzer42863252016-06-02 12:27:46 -04002052 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002053 /* Superblock is authoritative wrt given raid set layout! */
2054 mddev->raid_disks = le32_to_cpu(sb->num_devices);
2055 mddev->level = le32_to_cpu(sb->level);
2056 mddev->layout = le32_to_cpu(sb->layout);
2057 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
2058 mddev->new_level = le32_to_cpu(sb->new_level);
2059 mddev->new_layout = le32_to_cpu(sb->new_layout);
2060 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
2061 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
2062 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
2063
2064 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04002065 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
2066 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002067 DMERR("Reshape requested but raid set is still reshaping");
2068 return -EINVAL;
2069 }
2070
2071 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04002072 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002073 mddev->reshape_backwards = 1;
2074 else
2075 mddev->reshape_backwards = 0;
2076
2077 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
2078 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
2079 }
2080
2081 } else {
2082 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002083 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002084 */
2085 if (le32_to_cpu(sb->level) != mddev->level) {
2086 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
2087 return -EINVAL;
2088 }
2089 if (le32_to_cpu(sb->layout) != mddev->layout) {
2090 DMERR("Reshaping raid sets not yet supported. (raid layout change)");
Mike Snitzer43157842016-05-30 13:03:37 -04002091 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
2092 DMERR(" Old layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002093 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
2094 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
Mike Snitzer43157842016-05-30 13:03:37 -04002095 DMERR(" New layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002096 raid10_md_layout_to_format(mddev->layout),
2097 raid10_md_layout_to_copies(mddev->layout));
2098 return -EINVAL;
2099 }
2100 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
2101 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
2102 return -EINVAL;
2103 }
2104
2105 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
2106 if (!rt_is_raid1(rs->raid_type) &&
2107 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
2108 DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
2109 sb->num_devices, mddev->raid_disks);
2110 return -EINVAL;
2111 }
2112
2113 /* Table line is checked vs. authoritative superblock */
2114 rs_set_new(rs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002115 }
2116
Mike Snitzer42863252016-06-02 12:27:46 -04002117 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002118 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
2119
2120 /*
2121 * During load, we set FirstUse if a new superblock was written.
2122 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002123 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04002124 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002125 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002126 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002127 * and the new device needs to be rebuilt - in which
2128 * case the In_sync bit will /not/ be set and
2129 * recovery_cp must be MaxSector.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002130 * 3) This is/are a new device(s) being added to an old
2131 * raid set during takeover to a higher raid level
2132 * to provide capacity for redundancy or during reshape
2133 * to add capacity to grow the raid set.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002134 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002135 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002136 rdev_for_each(r, mddev) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002137 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002138 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002139
2140 if (!test_bit(In_sync, &r->flags)) {
2141 DMINFO("Device %d specified for rebuild; clearing superblock",
2142 r->raid_disk);
2143 rebuilds++;
2144
2145 if (test_bit(FirstUse, &r->flags))
2146 rebuild_and_new++;
2147 }
2148
2149 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002150 }
2151
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002152 if (new_devs == rs->raid_disks || !rebuilds) {
2153 /* Replace a broken device */
2154 if (new_devs == 1 && !rs->delta_disks)
2155 ;
2156 if (new_devs == rs->raid_disks) {
2157 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002158 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002159 } else if (new_devs != rebuilds &&
2160 new_devs != rs->delta_disks) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002161 DMERR("New device injected into existing raid set without "
2162 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002163 return -EINVAL;
2164 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002165 } else if (new_devs && new_devs != rebuilds) {
2166 DMERR("%u 'rebuild' devices cannot be injected into"
2167 " a raid set with %u other first-time devices",
2168 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002169 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002170 } else if (rebuilds) {
2171 if (rebuild_and_new && rebuilds != rebuild_and_new) {
2172 DMERR("new device%s provided without 'rebuild'",
2173 new_devs > 1 ? "s" : "");
2174 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002175 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002176 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
2177 (unsigned long long) mddev->recovery_cp);
2178 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002179 } else if (rs_is_reshaping(rs)) {
2180 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
2181 (unsigned long long) mddev->reshape_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002182 return -EINVAL;
2183 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002184 }
2185
2186 /*
2187 * Now we set the Faulty bit for those devices that are
2188 * recorded in the superblock as failed.
2189 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002190 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11002191 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002192 if (!r->sb_page)
2193 continue;
2194 sb2 = page_address(r->sb_page);
2195 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002196 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002197
2198 /*
2199 * Check for any device re-ordering.
2200 */
2201 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2202 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002203 if (role < 0)
2204 continue;
2205
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002206 if (role != r->raid_disk) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002207 if (__is_raid10_near(mddev->layout)) {
2208 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002209 rs->raid_disks % rs->raid10_copies) {
2210 rs->ti->error =
2211 "Cannot change raid10 near set to odd # of devices!";
2212 return -EINVAL;
2213 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002214
2215 sb2->array_position = cpu_to_le32(r->raid_disk);
2216
2217 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002218 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2219 !rt_is_raid1(rs->raid_type)) {
2220 rs->ti->error = "Cannot change device positions in raid set";
2221 return -EINVAL;
2222 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002223
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002224 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002225 }
2226
2227 /*
2228 * Partial recovery is performed on
2229 * returning failed devices.
2230 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002231 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002232 set_bit(Faulty, &r->flags);
2233 }
2234 }
2235
2236 return 0;
2237}
2238
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002239static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002240{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002241 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002242 struct dm_raid_superblock *sb;
2243
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002244 if (rs_is_raid0(rs) || !rdev->sb_page)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002245 return 0;
2246
2247 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002248
2249 /*
2250 * If mddev->events is not set, we know we have not yet initialized
2251 * the array.
2252 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002253 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002254 return -EINVAL;
2255
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002256 if (le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
2257 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2258 return -EINVAL;
2259 }
2260
2261 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002262 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002263 return -EINVAL;
2264 }
2265
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002266 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002267 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002268 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
2269
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002270 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
2271 /* Retrieve device size stored in superblock to be prepared for shrink */
2272 rdev->sectors = le64_to_cpu(sb->sectors);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002273 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002274 if (rdev->recovery_offset == MaxSector)
2275 set_bit(In_sync, &rdev->flags);
2276 /*
2277 * If no reshape in progress -> we're recovering single
2278 * disk(s) and have to set the device(s) to out-of-sync
2279 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002280 else if (!rs_is_reshaping(rs))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002281 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002282 }
2283
2284 /*
2285 * If a device comes back, set it as not In_sync and no longer faulty.
2286 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002287 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2288 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002289 clear_bit(In_sync, &rdev->flags);
2290 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002291 }
2292
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002293 /* Reshape support -> restore repective data offsets */
2294 rdev->data_offset = le64_to_cpu(sb->data_offset);
2295 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002296
2297 return 0;
2298}
2299
2300/*
2301 * Analyse superblocks and select the freshest.
2302 */
2303static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2304{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002305 int r;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002306 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002307 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002308 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002309
2310 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002311 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11002312 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002313 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002314 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002315 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002316 * of the "sync" directive.
2317 *
2318 * When reshaping capability is added, we must ensure
2319 * that the "sync" directive is disallowed during the
2320 * reshape.
2321 */
Mike Snitzer42863252016-06-02 12:27:46 -04002322 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002323 continue;
2324
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002325 if (!rdev->meta_bdev)
2326 continue;
2327
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002328 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002329
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002330 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002331 case 1:
2332 freshest = rdev;
2333 break;
2334 case 0:
2335 break;
2336 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01002337 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002338 if (dev->meta_dev)
2339 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002340
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002341 dev->meta_dev = NULL;
2342 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002343
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002344 if (rdev->sb_page)
2345 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002346
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002347 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002348
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002349 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002350
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002351 /*
2352 * We might be able to salvage the data device
2353 * even though the meta device has failed. For
2354 * now, we behave as though '- -' had been
2355 * set for this device in the table.
2356 */
2357 if (dev->data_dev)
2358 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002359
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002360 dev->data_dev = NULL;
2361 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002362
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002363 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002364 }
2365 }
2366
2367 if (!freshest)
2368 return 0;
2369
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002370 if (validate_raid_redundancy(rs)) {
2371 rs->ti->error = "Insufficient redundancy to activate array";
2372 return -EINVAL;
2373 }
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002374
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002375 /*
2376 * Validation of the freshest device provides the source of
2377 * validation for the remaining devices.
2378 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002379 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2380 if (super_validate(rs, freshest))
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002381 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002382
NeilBrowndafb20f2012-03-19 12:46:39 +11002383 rdev_for_each(rdev, mddev)
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002384 if ((rdev != freshest) && super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002385 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002386 return 0;
2387}
2388
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002389/*
2390 * Adjust data_offset and new_data_offset on all disk members of @rs
2391 * for out of place reshaping if requested by contructor
2392 *
2393 * We need free space at the beginning of each raid disk for forward
2394 * and at the end for backward reshapes which userspace has to provide
2395 * via remapping/reordering of space.
2396 */
2397static int rs_adjust_data_offsets(struct raid_set *rs)
2398{
2399 sector_t data_offset = 0, new_data_offset = 0;
2400 struct md_rdev *rdev;
2401
2402 /* Constructor did not request data offset change */
2403 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2404 if (!rs_is_reshapable(rs))
2405 goto out;
2406
2407 return 0;
2408 }
2409
2410 /* HM FIXME: get InSync raid_dev? */
2411 rdev = &rs->dev[0].rdev;
2412
2413 if (rs->delta_disks < 0) {
2414 /*
2415 * Removing disks (reshaping backwards):
2416 *
2417 * - before reshape: data is at offset 0 and free space
2418 * is at end of each component LV
2419 *
2420 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2421 */
2422 data_offset = 0;
2423 new_data_offset = rs->data_offset;
2424
2425 } else if (rs->delta_disks > 0) {
2426 /*
2427 * Adding disks (reshaping forwards):
2428 *
2429 * - before reshape: data is at offset rs->data_offset != 0 and
2430 * free space is at begin of each component LV
2431 *
2432 * - after reshape: data is at offset 0 on each component LV
2433 */
2434 data_offset = rs->data_offset;
2435 new_data_offset = 0;
2436
2437 } else {
2438 /*
2439 * User space passes in 0 for data offset after having removed reshape space
2440 *
2441 * - or - (data offset != 0)
2442 *
2443 * Changing RAID layout or chunk size -> toggle offsets
2444 *
2445 * - before reshape: data is at offset rs->data_offset 0 and
2446 * free space is at end of each component LV
2447 * -or-
2448 * data is at offset rs->data_offset != 0 and
2449 * free space is at begin of each component LV
2450 *
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002451 * - after reshape: data is at offset 0 if it was at offset != 0
2452 * or at offset != 0 if it was at offset 0
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002453 * on each component LV
2454 *
2455 */
2456 data_offset = rs->data_offset ? rdev->data_offset : 0;
2457 new_data_offset = data_offset ? 0 : rs->data_offset;
2458 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2459 }
2460
2461 /*
2462 * Make sure we got a minimum amount of free sectors per device
2463 */
2464 if (rs->data_offset &&
2465 to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) {
2466 rs->ti->error = data_offset ? "No space for forward reshape" :
2467 "No space for backward reshape";
2468 return -ENOSPC;
2469 }
2470out:
2471 /* Adjust data offsets on all rdevs */
2472 rdev_for_each(rdev, &rs->md) {
2473 rdev->data_offset = data_offset;
2474 rdev->new_data_offset = new_data_offset;
2475 }
2476
2477 return 0;
2478}
2479
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002480/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002481static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002482{
2483 int i = 0;
2484 struct md_rdev *rdev;
2485
2486 rdev_for_each(rdev, &rs->md) {
2487 rdev->raid_disk = i++;
2488 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2489 }
2490}
2491
2492/*
2493 * Setup @rs for takeover by a different raid level
2494 */
2495static int rs_setup_takeover(struct raid_set *rs)
2496{
2497 struct mddev *mddev = &rs->md;
2498 struct md_rdev *rdev;
2499 unsigned int d = mddev->raid_disks = rs->raid_disks;
2500 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2501
2502 if (rt_is_raid10(rs->raid_type)) {
2503 if (mddev->level == 0) {
2504 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002505 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002506
2507 /* raid0 -> raid10_far layout */
2508 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2509 rs->raid10_copies);
2510 } else if (mddev->level == 1)
2511 /* raid1 -> raid10_near layout */
2512 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2513 rs->raid_disks);
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002514 else
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002515 return -EINVAL;
2516
2517 }
2518
2519 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2520 mddev->recovery_cp = MaxSector;
2521
2522 while (d--) {
2523 rdev = &rs->dev[d].rdev;
2524
2525 if (test_bit(d, (void *) rs->rebuild_disks)) {
2526 clear_bit(In_sync, &rdev->flags);
2527 clear_bit(Faulty, &rdev->flags);
2528 mddev->recovery_cp = rdev->recovery_offset = 0;
2529 /* Bitmap has to be created when we do an "up" takeover */
2530 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2531 }
2532
2533 rdev->new_data_offset = new_data_offset;
2534 }
2535
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002536 return 0;
2537}
2538
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002539/* Prepare @rs for reshape */
2540static int rs_prepare_reshape(struct raid_set *rs)
2541{
2542 bool reshape;
2543 struct mddev *mddev = &rs->md;
2544
2545 if (rs_is_raid10(rs)) {
2546 if (rs->raid_disks != mddev->raid_disks &&
2547 __is_raid10_near(mddev->layout) &&
2548 rs->raid10_copies &&
2549 rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
2550 /*
2551 * raid disk have to be multiple of data copies to allow this conversion,
2552 *
2553 * This is actually not a reshape it is a
2554 * rebuild of any additional mirrors per group
2555 */
2556 if (rs->raid_disks % rs->raid10_copies) {
2557 rs->ti->error = "Can't reshape raid10 mirror groups";
2558 return -EINVAL;
2559 }
2560
2561 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
2562 __reorder_raid_disk_indexes(rs);
2563 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2564 rs->raid10_copies);
2565 mddev->new_layout = mddev->layout;
2566 reshape = false;
2567 } else
2568 reshape = true;
2569
2570 } else if (rs_is_raid456(rs))
2571 reshape = true;
2572
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002573 else if (rs_is_raid1(rs)) {
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002574 if (rs->delta_disks) {
2575 /* Process raid1 via delta_disks */
2576 mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
2577 reshape = true;
2578 } else {
2579 /* Process raid1 without delta_disks */
2580 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002581 reshape = false;
2582 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002583 } else {
2584 rs->ti->error = "Called with bogus raid type";
2585 return -EINVAL;
2586 }
2587
2588 if (reshape) {
2589 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2590 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002591 } else if (mddev->raid_disks < rs->raid_disks)
2592 /* Create new superblocks and bitmaps, if any new disks */
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002593 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002594
2595 return 0;
2596}
2597
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002598/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002599 *
2600 * - change raid layout
2601 * - change chunk size
2602 * - add disks
2603 * - remove disks
2604 */
2605static int rs_setup_reshape(struct raid_set *rs)
2606{
2607 int r = 0;
2608 unsigned int cur_raid_devs, d;
2609 struct mddev *mddev = &rs->md;
2610 struct md_rdev *rdev;
2611
2612 mddev->delta_disks = rs->delta_disks;
2613 cur_raid_devs = mddev->raid_disks;
2614
2615 /* Ignore impossible layout change whilst adding/removing disks */
2616 if (mddev->delta_disks &&
2617 mddev->layout != mddev->new_layout) {
2618 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
2619 mddev->new_layout = mddev->layout;
2620 }
2621
2622 /*
2623 * Adjust array size:
2624 *
2625 * - in case of adding disks, array size has
2626 * to grow after the disk adding reshape,
2627 * which'll hapen in the event handler;
2628 * reshape will happen forward, so space has to
2629 * be available at the beginning of each disk
2630 *
2631 * - in case of removing disks, array size
2632 * has to shrink before starting the reshape,
2633 * which'll happen here;
2634 * reshape will happen backward, so space has to
2635 * be available at the end of each disk
2636 *
2637 * - data_offset and new_data_offset are
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002638 * adjusted for aforementioned out of place
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002639 * reshaping based on userspace passing in
2640 * the "data_offset <sectors>" key/value
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002641 * pair via the constructor
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002642 */
2643
2644 /* Add disk(s) */
2645 if (rs->delta_disks > 0) {
2646 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
2647 for (d = cur_raid_devs; d < rs->raid_disks; d++) {
2648 rdev = &rs->dev[d].rdev;
2649 clear_bit(In_sync, &rdev->flags);
2650
2651 /*
2652 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
2653 * by md, which'll store that erroneously in the superblock on reshape
2654 */
2655 rdev->saved_raid_disk = -1;
2656 rdev->raid_disk = d;
2657
2658 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002659 rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002660 }
2661
2662 mddev->reshape_backwards = 0; /* adding disks -> forward reshape */
2663
2664 /* Remove disk(s) */
2665 } else if (rs->delta_disks < 0) {
2666 r = rs_set_dev_and_array_sectors(rs, true);
2667 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
2668
2669 /* Change layout and/or chunk size */
2670 } else {
2671 /*
2672 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
2673 *
2674 * keeping number of disks and do layout change ->
2675 *
2676 * toggle reshape_backward depending on data_offset:
2677 *
2678 * - free space upfront -> reshape forward
2679 *
2680 * - free space at the end -> reshape backward
2681 *
2682 *
2683 * This utilizes free reshape space avoiding the need
2684 * for userspace to move (parts of) LV segments in
2685 * case of layout/chunksize change (for disk
2686 * adding/removing reshape space has to be at
2687 * the proper address (see above with delta_disks):
2688 *
2689 * add disk(s) -> begin
2690 * remove disk(s)-> end
2691 */
2692 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
2693 }
2694
2695 return r;
2696}
2697
2698/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002699 * Enable/disable discard support on RAID set depending on
2700 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002701 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002702static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002703{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002704 int i;
2705 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002706 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002707
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002708 /* Assume discards not supported until after checks below. */
2709 ti->discards_supported = false;
2710
2711 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002712 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002713
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02002714 for (i = 0; i < rs->raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002715 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002716
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002717 if (!rs->dev[i].rdev.bdev)
2718 continue;
2719
2720 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002721 if (!q || !blk_queue_discard(q))
2722 return;
2723
2724 if (raid456) {
2725 if (!q->limits.discard_zeroes_data)
2726 return;
2727 if (!devices_handle_discard_safely) {
2728 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2729 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2730 return;
2731 }
2732 }
2733 }
2734
2735 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002736 ti->discards_supported = true;
2737
2738 /*
2739 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002740 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002741 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002742 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002743 ti->num_discard_bios = 1;
2744}
2745
2746/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002747 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002748 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002749 * <raid_type> <#raid_params> <raid_params>{0,} \
2750 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002751 *
Mike Snitzer43157842016-05-30 13:03:37 -04002752 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002753 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002754 *
2755 * Userspace is free to initialize the metadata devices, hence the superblocks to
2756 * enforce recreation based on the passed in table parameters.
2757 *
NeilBrown9d09e662011-01-13 20:00:02 +00002758 */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002759static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
NeilBrown9d09e662011-01-13 20:00:02 +00002760{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002761 int r;
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002762 bool resize;
NeilBrown9d09e662011-01-13 20:00:02 +00002763 struct raid_type *rt;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002764 unsigned int num_raid_params, num_raid_devs;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002765 sector_t calculated_dev_sectors;
NeilBrown9d09e662011-01-13 20:00:02 +00002766 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002767 const char *arg;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002768 struct rs_layout rs_layout;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002769 struct dm_arg_set as = { argc, argv }, as_nrd;
2770 struct dm_arg _args[] = {
2771 { 0, as.argc, "Cannot understand number of raid parameters" },
2772 { 1, 254, "Cannot understand number of raid devices parameters" }
2773 };
NeilBrown9d09e662011-01-13 20:00:02 +00002774
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002775 /* Must have <raid_type> */
2776 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002777 if (!arg) {
2778 ti->error = "No arguments";
2779 return -EINVAL;
2780 }
NeilBrown9d09e662011-01-13 20:00:02 +00002781
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002782 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002783 if (!rt) {
2784 ti->error = "Unrecognised raid_type";
2785 return -EINVAL;
2786 }
NeilBrown9d09e662011-01-13 20:00:02 +00002787
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002788 /* Must have <#raid_params> */
2789 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002790 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00002791
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002792 /* number of raid device tupples <meta_dev data_dev> */
2793 as_nrd = as;
2794 dm_consume_args(&as_nrd, num_raid_params);
2795 _args[1].max = (as_nrd.argc - 1) / 2;
2796 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002797 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002798
Mike Snitzerbb91a632016-06-02 12:06:54 -04002799 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002800 ti->error = "Invalid number of supplied raid devices";
2801 return -EINVAL;
2802 }
NeilBrown9d09e662011-01-13 20:00:02 +00002803
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002804 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00002805 if (IS_ERR(rs))
2806 return PTR_ERR(rs);
2807
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002808 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002809 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002810 goto bad;
2811
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02002812 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002813 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002814 goto bad;
2815
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002816 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002817
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002818 /*
2819 * Calculate ctr requested array and device sizes to allow
2820 * for superblock analysis needing device sizes defined.
2821 *
2822 * Any existing superblock will overwrite the array and device sizes
2823 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002824 r = rs_set_dev_and_array_sectors(rs, false);
2825 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002826 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002827
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002828 calculated_dev_sectors = rs->dev[0].rdev.sectors;
2829
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002830 /*
2831 * Backup any new raid set level, layout, ...
2832 * requested to be able to compare to superblock
2833 * members for conversion decisions.
2834 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002835 rs_config_backup(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002836
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002837 r = analyse_superblocks(ti, rs);
2838 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002839 goto bad;
2840
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002841 resize = calculated_dev_sectors != rs->dev[0].rdev.sectors;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002842
NeilBrown9d09e662011-01-13 20:00:02 +00002843 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00002844 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002845 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00002846
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002847 /* Restore any requested new layout for conversion decision */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002848 rs_config_restore(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002849
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002850 /*
2851 * Now that we have any superblock metadata available,
2852 * check for new, recovering, reshaping, to be taken over,
2853 * to be reshaped or an existing, unchanged raid set to
2854 * run in sequence.
2855 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002856 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02002857 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
2858 if (rs_is_raid6(rs) &&
2859 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
2860 ti->error = "'nosync' not allowed for new raid6 set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002861 r = -EINVAL;
2862 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02002863 }
2864 rs_setup_recovery(rs, 0);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002865 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2866 rs_set_new(rs);
2867 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002868 /* A recovering raid set may be resized */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002869 ; /* skip setup rs */
2870 } else if (rs_is_reshaping(rs)) {
2871 /* Have to reject size change request during reshape */
2872 if (resize) {
2873 ti->error = "Can't resize a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002874 r = -EPERM;
2875 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02002876 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002877 /* skip setup rs */
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02002878 } else if (rs_takeover_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002879 if (rs_is_reshaping(rs)) {
2880 ti->error = "Can't takeover a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002881 r = -EPERM;
2882 goto bad;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002883 }
2884
2885 /*
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002886 * If a takeover is needed, userspace sets any additional
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002887 * devices to rebuild and we can check for a valid request here.
2888 *
2889 * If acceptible, set the level to the new requested
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002890 * one, prohibit requesting recovery, allow the raid
2891 * set to run and store superblocks during resume.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002892 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002893 r = rs_check_takeover(rs);
2894 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002895 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002896
2897 r = rs_setup_takeover(rs);
2898 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002899 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002900
Mike Snitzer42863252016-06-02 12:27:46 -04002901 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002902 /* Takeover ain't recovery, so disable recovery */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002903 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002904 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002905 } else if (rs_reshape_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002906 /*
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002907 * We can only prepare for a reshape here, because the
2908 * raid set needs to run to provide the repective reshape
2909 * check functions via its MD personality instance.
2910 *
2911 * So do the reshape check after md_run() succeeded.
2912 */
2913 r = rs_prepare_reshape(rs);
2914 if (r)
2915 return r;
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04002916
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002917 /* Reshaping ain't recovery, so disable recovery */
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002918 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002919 rs_set_cur(rs);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002920 } else {
2921 /* May not set recovery when a device rebuild is requested */
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02002922 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
2923 rs_setup_recovery(rs, MaxSector);
2924 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2925 } else
2926 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02002927 0 : (resize ? calculated_dev_sectors : MaxSector));
2928 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002929 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002930
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002931 /* If constructor requested it, change data and new_data offsets */
2932 r = rs_adjust_data_offsets(rs);
2933 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002934 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002935
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002936 /* Start raid set read-only and assumed clean to change in raid_resume() */
2937 rs->md.ro = 1;
2938 rs->md.in_sync = 1;
2939 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002940
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002941 /* Has to be held on running the array */
2942 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002943 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002944 rs->md.in_sync = 0; /* Assume already marked dirty */
NeilBrown9d09e662011-01-13 20:00:02 +00002945
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002946 if (r) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002947 ti->error = "Failed to run raid array";
2948 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002949 goto bad;
2950 }
2951
2952 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00002953 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
2954
Jonathan Brassow32737272011-08-02 12:32:07 +01002955 mddev_suspend(&rs->md);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002956
2957 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
2958 if (rs_is_raid456(rs)) {
2959 r = rs_set_raid456_stripe_cache(rs);
2960 if (r)
2961 goto bad_stripe_cache;
2962 }
2963
2964 /* Now do an early reshape check */
2965 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
2966 r = rs_check_reshape(rs);
2967 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002968 goto bad_check_reshape;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002969
2970 /* Restore new, ctr requested layout to perform check */
2971 rs_config_restore(rs, &rs_layout);
2972
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002973 if (rs->md.pers->start_reshape) {
2974 r = rs->md.pers->check_reshape(&rs->md);
2975 if (r) {
2976 ti->error = "Reshape check failed";
2977 goto bad_check_reshape;
2978 }
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002979 }
2980 }
2981
2982 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002983 return 0;
2984
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002985bad_stripe_cache:
2986bad_check_reshape:
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002987 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002988bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002989 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002990
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002991 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002992}
2993
2994static void raid_dtr(struct dm_target *ti)
2995{
2996 struct raid_set *rs = ti->private;
2997
2998 list_del_init(&rs->callbacks.list);
2999 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003000 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003001}
3002
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003003static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00003004{
3005 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11003006 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00003007
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003008 /*
3009 * If we're reshaping to add disk(s)), ti->len and
3010 * mddev->array_sectors will differ during the process
3011 * (ti->len > mddev->array_sectors), so we have to requeue
3012 * bios with addresses > mddev->array_sectors here or
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003013 * there will occur accesses past EOD of the component
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003014 * data images thus erroring the raid set.
3015 */
3016 if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
3017 return DM_MAPIO_REQUEUE;
3018
NeilBrown9d09e662011-01-13 20:00:02 +00003019 mddev->pers->make_request(mddev, bio);
3020
3021 return DM_MAPIO_SUBMITTED;
3022}
3023
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003024/* Return string describing the current sync action of @mddev */
Jonathan Brassowbe836512013-04-24 11:42:43 +10003025static const char *decipher_sync_action(struct mddev *mddev)
3026{
3027 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3028 return "frozen";
3029
3030 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3031 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3032 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3033 return "reshape";
3034
3035 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3036 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3037 return "resync";
3038 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3039 return "check";
3040 return "repair";
3041 }
3042
3043 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3044 return "recover";
3045 }
3046
3047 return "idle";
3048}
3049
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003050/*
3051 * Return status string @rdev
3052 *
3053 * Status characters:
3054 *
3055 * 'D' = Dead/Failed device
3056 * 'a' = Alive but not in-sync
3057 * 'A' = Alive and in-sync
3058 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003059static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003060{
3061 if (test_bit(Faulty, &rdev->flags))
3062 return "D";
3063 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags))
3064 return "a";
3065 else
3066 return "A";
3067}
3068
3069/* Helper to return resync/reshape progress for @rs and @array_in_sync */
3070static sector_t rs_get_progress(struct raid_set *rs,
3071 sector_t resync_max_sectors, bool *array_in_sync)
3072{
3073 sector_t r, recovery_cp, curr_resync_completed;
3074 struct mddev *mddev = &rs->md;
3075
3076 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp;
3077 recovery_cp = mddev->recovery_cp;
3078 *array_in_sync = false;
3079
3080 if (rs_is_raid0(rs)) {
3081 r = resync_max_sectors;
3082 *array_in_sync = true;
3083
3084 } else {
3085 r = mddev->reshape_position;
3086
3087 /* Reshape is relative to the array size */
3088 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
3089 r != MaxSector) {
3090 if (r == MaxSector) {
3091 *array_in_sync = true;
3092 r = resync_max_sectors;
3093 } else {
3094 /* Got to reverse on backward reshape */
3095 if (mddev->reshape_backwards)
3096 r = mddev->array_sectors - r;
3097
3098 /* Devide by # of data stripes */
3099 sector_div(r, mddev_data_stripes(rs));
3100 }
3101
3102 /* Sync is relative to the component device size */
3103 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3104 r = curr_resync_completed;
3105 else
3106 r = recovery_cp;
3107
3108 if (r == MaxSector) {
3109 /*
3110 * Sync complete.
3111 */
3112 *array_in_sync = true;
3113 r = resync_max_sectors;
3114 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
3115 /*
3116 * If "check" or "repair" is occurring, the raid set has
3117 * undergone an initial sync and the health characters
3118 * should not be 'a' anymore.
3119 */
3120 *array_in_sync = true;
3121 } else {
3122 struct md_rdev *rdev;
3123
3124 /*
3125 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04003126 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003127 * devices are In_sync, then it is the raid set that is
3128 * being initialized.
3129 */
3130 rdev_for_each(rdev, mddev)
3131 if (!test_bit(In_sync, &rdev->flags))
3132 *array_in_sync = true;
3133#if 0
3134 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */
3135#endif
3136 }
3137 }
3138
3139 return r;
3140}
3141
3142/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003143static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003144{
3145 return dev ? dev->name : "-";
3146}
3147
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003148static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003149 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00003150{
3151 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003152 struct mddev *mddev = &rs->md;
3153 struct r5conf *conf = mddev->private;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003154 int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003155 bool array_in_sync;
3156 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
3157 unsigned int sz = 0;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003158 unsigned int rebuild_disks;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003159 unsigned int write_mostly_params = 0;
3160 sector_t progress, resync_max_sectors, resync_mismatches;
3161 const char *sync_action;
3162 struct raid_type *rt;
3163 struct md_rdev *rdev;
NeilBrown9d09e662011-01-13 20:00:02 +00003164
3165 switch (type) {
3166 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003167 /* *Should* always succeed */
3168 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
3169 if (!rt)
3170 return;
NeilBrown9d09e662011-01-13 20:00:02 +00003171
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003172 DMEMIT("%s %d ", rt->name, mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00003173
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003174 /* Access most recent mddev properties for status output */
3175 smp_rmb();
3176 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04003177 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003178 mddev->resync_max_sectors : mddev->dev_sectors;
3179 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync);
3180 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003181 atomic64_read(&mddev->resync_mismatches) : 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003182 sync_action = decipher_sync_action(&rs->md);
3183
3184 /* HM FIXME: do we want another state char for raid0? It shows 'D' or 'A' now */
Heinz Mauelshagen32682402016-06-30 21:32:20 +02003185 rdev_for_each(rdev, mddev)
3186 DMEMIT(__raid_dev_status(rdev, array_in_sync));
Jonathan Brassowbe836512013-04-24 11:42:43 +10003187
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003188 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003189 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003190 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003191 * - Initializing the raid set
3192 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003193 * The user can distinguish between the two by referring
3194 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003195 *
3196 * The reshape ratio shows the progress of
3197 * changing the raid layout or the number of
3198 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003199 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003200 DMEMIT(" %llu/%llu", (unsigned long long) progress,
3201 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003202
Jonathan Brassowbe836512013-04-24 11:42:43 +10003203 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003204 * v1.5.0+:
3205 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003206 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003207 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10003208 * information on each of these states.
3209 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003210 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10003211
3212 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003213 * v1.5.0+:
3214 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003215 * resync_mismatches/mismatch_cnt
3216 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003217 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10003218 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003219 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
3220
3221 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003222 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003223 *
3224 * data_offset (needed for out of space reshaping)
3225 * This field shows the data offset into the data
3226 * image LV where the first stripes data starts.
3227 *
3228 * We keep data_offset equal on all raid disks of the set,
3229 * so retrieving it from the first raid disk is sufficient.
3230 */
3231 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
NeilBrown9d09e662011-01-13 20:00:02 +00003232 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003233
NeilBrown9d09e662011-01-13 20:00:02 +00003234 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003235 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00003236
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003237 /* Calculate raid parameter count */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003238 for (i = 0; i < rs->raid_disks; i++)
3239 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003240 write_mostly_params += 2;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003241 rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
3242 raid_param_cnt += rebuild_disks * 2 +
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003243 write_mostly_params +
3244 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
3245 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2;
3246 /* Emit table line */
3247 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04003248 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003249 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003250 raid10_md_layout_to_format(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04003251 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003252 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003253 raid10_md_layout_to_copies(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04003254 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003255 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04003256 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003257 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04003258 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003259 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003260 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
Mike Snitzer42863252016-06-02 12:27:46 -04003261 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003262 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003263 (unsigned long long) rs->data_offset);
Mike Snitzer42863252016-06-02 12:27:46 -04003264 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003265 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003266 mddev->bitmap_info.daemon_sleep);
Mike Snitzer42863252016-06-02 12:27:46 -04003267 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003268 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003269 max(rs->delta_disks, mddev->delta_disks));
Mike Snitzer42863252016-06-02 12:27:46 -04003270 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003271 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003272 max_nr_stripes);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003273 if (rebuild_disks)
3274 for (i = 0; i < rs->raid_disks; i++)
3275 if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
3276 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
3277 rs->dev[i].rdev.raid_disk);
3278 if (write_mostly_params)
3279 for (i = 0; i < rs->raid_disks; i++)
3280 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
3281 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
3282 rs->dev[i].rdev.raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04003283 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003284 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003285 mddev->bitmap_info.max_write_behind);
Mike Snitzer42863252016-06-02 12:27:46 -04003286 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003287 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003288 mddev->sync_speed_max);
Mike Snitzer42863252016-06-02 12:27:46 -04003289 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003290 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003291 mddev->sync_speed_min);
3292 DMEMIT(" %d", rs->raid_disks);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003293 for (i = 0; i < rs->raid_disks; i++)
3294 DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
3295 __get_dev_name(rs->dev[i].data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00003296 }
NeilBrown9d09e662011-01-13 20:00:02 +00003297}
3298
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003299static int raid_message(struct dm_target *ti, unsigned int argc, char **argv)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003300{
3301 struct raid_set *rs = ti->private;
3302 struct mddev *mddev = &rs->md;
3303
Jonathan Brassowbe836512013-04-24 11:42:43 +10003304 if (!mddev->pers || !mddev->pers->sync_request)
3305 return -EINVAL;
3306
3307 if (!strcasecmp(argv[0], "frozen"))
3308 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3309 else
3310 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3311
3312 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
3313 if (mddev->sync_thread) {
3314 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3315 md_reap_sync_thread(mddev);
3316 }
3317 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3318 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3319 return -EBUSY;
3320 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003321 ; /* MD_RECOVERY_NEEDED set below */
3322 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003323 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003324 else {
Jonathan Brassowbe836512013-04-24 11:42:43 +10003325 if (!strcasecmp(argv[0], "check"))
3326 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3327 else if (!!strcasecmp(argv[0], "repair"))
3328 return -EINVAL;
3329 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3330 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3331 }
3332 if (mddev->ro == 2) {
3333 /* A write to sync_action is enough to justify
3334 * canceling read-auto mode
3335 */
3336 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003337 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003338 md_wakeup_thread(mddev->sync_thread);
3339 }
3340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003341 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003342 md_wakeup_thread(mddev->thread);
3343
3344 return 0;
3345}
3346
3347static int raid_iterate_devices(struct dm_target *ti,
3348 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00003349{
3350 struct raid_set *rs = ti->private;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003351 unsigned int i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003352 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00003353
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003354 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00003355 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003356 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00003357 rs->dev[i].data_dev,
3358 0, /* No offset on data devs */
3359 rs->md.dev_sectors,
3360 data);
3361
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003362 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003363}
3364
3365static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
3366{
3367 struct raid_set *rs = ti->private;
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003368 unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003369
3370 blk_limits_io_min(limits, chunk_size);
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003371 blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
NeilBrown9d09e662011-01-13 20:00:02 +00003372}
3373
3374static void raid_presuspend(struct dm_target *ti)
3375{
3376 struct raid_set *rs = ti->private;
3377
3378 md_stop_writes(&rs->md);
3379}
3380
3381static void raid_postsuspend(struct dm_target *ti)
3382{
3383 struct raid_set *rs = ti->private;
3384
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003385 if (!rs->md.suspended)
3386 mddev_suspend(&rs->md);
3387
3388 rs->md.ro = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00003389}
3390
Jonathan Brassowf381e712013-05-08 17:57:13 -05003391static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00003392{
Jonathan Brassow9092c022013-05-02 14:19:24 -05003393 int i;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003394 uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassow9092c022013-05-02 14:19:24 -05003395 unsigned long flags;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003396 bool cleared = false;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003397 struct dm_raid_superblock *sb;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003398 struct mddev *mddev = &rs->md;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003399 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00003400
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003401 /* RAID personalities have to provide hot add/remove methods or we need to bail out. */
3402 if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
3403 return;
3404
3405 memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
3406
Jonathan Brassowf381e712013-05-08 17:57:13 -05003407 for (i = 0; i < rs->md.raid_disks; i++) {
3408 r = &rs->dev[i].rdev;
3409 if (test_bit(Faulty, &r->flags) && r->sb_page &&
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02003410 sync_page_io(r, 0, r->sb_size, r->sb_page,
3411 REQ_OP_READ, 0, true)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003412 DMINFO("Faulty %s device #%d has readable super block."
3413 " Attempting to revive it.",
3414 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003415
3416 /*
3417 * Faulty bit may be set, but sometimes the array can
3418 * be suspended before the personalities can respond
3419 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04003420 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003421 * the failed device, its 'raid_disk' number will be
3422 * '>= 0' - meaning we must call this function
3423 * ourselves.
3424 */
3425 if ((r->raid_disk >= 0) &&
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003426 (mddev->pers->hot_remove_disk(mddev, r) != 0))
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003427 /* Failed to revive this device, try next */
3428 continue;
3429
Jonathan Brassowf381e712013-05-08 17:57:13 -05003430 r->raid_disk = i;
3431 r->saved_raid_disk = i;
3432 flags = r->flags;
3433 clear_bit(Faulty, &r->flags);
3434 clear_bit(WriteErrorSeen, &r->flags);
3435 clear_bit(In_sync, &r->flags);
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003436 if (mddev->pers->hot_add_disk(mddev, r)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003437 r->raid_disk = -1;
3438 r->saved_raid_disk = -1;
3439 r->flags = flags;
3440 } else {
3441 r->recovery_offset = 0;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003442 set_bit(i, (void *) cleared_failed_devices);
3443 cleared = true;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003444 }
3445 }
3446 }
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003447
3448 /* If any failed devices could be cleared, update all sbs failed_devices bits */
3449 if (cleared) {
3450 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
3451
Jonathan Brassowf381e712013-05-08 17:57:13 -05003452 rdev_for_each(r, &rs->md) {
3453 sb = page_address(r->sb_page);
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003454 sb_retrieve_failed_devices(sb, failed_devices);
3455
3456 for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
3457 failed_devices[i] &= ~cleared_failed_devices[i];
3458
3459 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003460 }
3461 }
3462}
3463
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003464static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003465{
3466 int r = 0;
3467
3468 /* Try loading the bitmap unless "raid0", which does not have one */
3469 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04003470 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003471 r = bitmap_load(&rs->md);
3472 if (r)
3473 DMERR("Failed to load bitmap");
3474 }
3475
3476 return r;
3477}
3478
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003479/* Enforce updating all superblocks */
3480static void rs_update_sbs(struct raid_set *rs)
3481{
3482 struct mddev *mddev = &rs->md;
3483 int ro = mddev->ro;
3484
3485 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3486 mddev->ro = 0;
3487 md_update_sb(mddev, 1);
3488 mddev->ro = ro;
3489}
3490
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003491/*
3492 * Reshape changes raid algorithm of @rs to new one within personality
3493 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
3494 * disks from a raid set thus growing/shrinking it or resizes the set
3495 *
3496 * Call mddev_lock_nointr() before!
3497 */
3498static int rs_start_reshape(struct raid_set *rs)
3499{
3500 int r;
3501 struct mddev *mddev = &rs->md;
3502 struct md_personality *pers = mddev->pers;
3503
3504 r = rs_setup_reshape(rs);
3505 if (r)
3506 return r;
3507
3508 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
3509 if (mddev->suspended)
3510 mddev_resume(mddev);
3511
3512 /*
3513 * Check any reshape constraints enforced by the personalility
3514 *
3515 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
3516 */
3517 r = pers->check_reshape(mddev);
3518 if (r) {
3519 rs->ti->error = "pers->check_reshape() failed";
3520 return r;
3521 }
3522
3523 /*
3524 * Personality may not provide start reshape method in which
3525 * case check_reshape above has already covered everything
3526 */
3527 if (pers->start_reshape) {
3528 r = pers->start_reshape(mddev);
3529 if (r) {
3530 rs->ti->error = "pers->start_reshape() failed";
3531 return r;
3532 }
3533 }
3534
3535 /* Suspend because a resume will happen in raid_resume() */
3536 if (!mddev->suspended)
3537 mddev_suspend(mddev);
3538
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003539 /*
3540 * Now reshape got set up, update superblocks to
3541 * reflect the fact so that a table reload will
3542 * access proper superblock content in the ctr.
3543 */
3544 rs_update_sbs(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003545
3546 return 0;
3547}
3548
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003549static int raid_preresume(struct dm_target *ti)
3550{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003551 int r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003552 struct raid_set *rs = ti->private;
3553 struct mddev *mddev = &rs->md;
3554
3555 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04003556 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003557 return 0;
3558
3559 /*
3560 * The superblocks need to be updated on disk if the
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003561 * array is new or new devices got added (thus zeroed
3562 * out by userspace) or __load_dirty_region_bitmap
3563 * will overwrite them in core with old data or fail.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003564 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003565 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
3566 rs_update_sbs(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003567
3568 /*
3569 * Disable/enable discard support on raid set after any
3570 * conversion, because devices can have been added
3571 */
3572 configure_discard_support(rs);
3573
3574 /* Load the bitmap from disk unless raid0 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003575 r = __load_dirty_region_bitmap(rs);
3576 if (r)
3577 return r;
3578
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003579 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
3580 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) &&
3581 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
3582 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
3583 to_bytes(rs->requested_bitmap_chunk_sectors), 0);
3584 if (r)
3585 DMERR("Failed to resize bitmap");
3586 }
3587
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003588 /* Check for any resize/reshape on @rs and adjust/initiate */
3589 /* Be prepared for mddev_resume() in raid_resume() */
3590 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3591 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003592 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3593 mddev->resync_min = mddev->recovery_cp;
3594 }
3595
3596 rs_set_capacity(rs);
3597
Heinz Mauelshagen345a6cd2016-06-25 02:42:54 +02003598 /* Check for any reshape request unless new raid set */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003599 if (test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3600 /* Initiate a reshape. */
3601 mddev_lock_nointr(mddev);
3602 r = rs_start_reshape(rs);
3603 mddev_unlock(mddev);
3604 if (r)
3605 DMWARN("Failed to check/start reshape, continuing without change");
3606 r = 0;
3607 }
3608
3609 return r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003610}
3611
Jonathan Brassowf381e712013-05-08 17:57:13 -05003612static void raid_resume(struct dm_target *ti)
3613{
3614 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003615 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003616
Mike Snitzer42863252016-06-02 12:27:46 -04003617 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003618 /*
3619 * A secondary resume while the device is active.
3620 * Take this opportunity to check whether any failed
3621 * devices are reachable again.
3622 */
3623 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003624 }
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003625
3626 mddev->ro = 0;
3627 mddev->in_sync = 0;
3628
3629 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3630
3631 if (mddev->suspended)
3632 mddev_resume(mddev);
NeilBrown9d09e662011-01-13 20:00:02 +00003633}
3634
3635static struct target_type raid_target = {
3636 .name = "raid",
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003637 .version = {1, 9, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00003638 .module = THIS_MODULE,
3639 .ctr = raid_ctr,
3640 .dtr = raid_dtr,
3641 .map = raid_map,
3642 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10003643 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00003644 .iterate_devices = raid_iterate_devices,
3645 .io_hints = raid_io_hints,
3646 .presuspend = raid_presuspend,
3647 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003648 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00003649 .resume = raid_resume,
3650};
3651
3652static int __init dm_raid_init(void)
3653{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11003654 DMINFO("Loading target version %u.%u.%u",
3655 raid_target.version[0],
3656 raid_target.version[1],
3657 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00003658 return dm_register_target(&raid_target);
3659}
3660
3661static void __exit dm_raid_exit(void)
3662{
3663 dm_unregister_target(&raid_target);
3664}
3665
3666module_init(dm_raid_init);
3667module_exit(dm_raid_exit);
3668
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02003669module_param(devices_handle_discard_safely, bool, 0644);
3670MODULE_PARM_DESC(devices_handle_discard_safely,
3671 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
3672
Mike Snitzeref9b85a2016-06-02 12:02:19 -04003673MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
3674MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003675MODULE_ALIAS("dm-raid1");
3676MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00003677MODULE_ALIAS("dm-raid4");
3678MODULE_ALIAS("dm-raid5");
3679MODULE_ALIAS("dm-raid6");
3680MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003681MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00003682MODULE_LICENSE("GPL");