blob: 8118f1e0218b29008865b1bed9ae2ae71a1d9f1e [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 */
Mike Snitzer42863252016-06-02 12:27:46 -040072#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid4/5/6/10! */
73#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 Mauelshagen33e53f02016-05-19 18:49:30 +0200135 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200136
137/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200138#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
139 CTR_FLAG_REBUILD | \
140 CTR_FLAG_DAEMON_SLEEP | \
141 CTR_FLAG_MIN_RECOVERY_RATE | \
142 CTR_FLAG_MAX_RECOVERY_RATE | \
143 CTR_FLAG_REGION_SIZE | \
144 CTR_FLAG_RAID10_COPIES | \
145 CTR_FLAG_RAID10_FORMAT | \
146 CTR_FLAG_DELTA_DISKS | \
147 CTR_FLAG_DATA_OFFSET | \
148 CTR_FLAG_RAID10_USE_NEAR_SETS)
149
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200150/*
151 * "raid4/5/6" do not accept any raid1 or raid10 specific options
152 *
153 * "raid6" does not accept "nosync", because it is not guaranteed
154 * that both parity and q-syndrome are being written properly with
155 * any writes
156 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200157#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
158 CTR_FLAG_REBUILD | \
159 CTR_FLAG_DAEMON_SLEEP | \
160 CTR_FLAG_MIN_RECOVERY_RATE | \
161 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200162 CTR_FLAG_MAX_WRITE_BEHIND | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200163 CTR_FLAG_STRIPE_CACHE | \
164 CTR_FLAG_REGION_SIZE | \
165 CTR_FLAG_DELTA_DISKS | \
166 CTR_FLAG_DATA_OFFSET)
167
168#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
169 CTR_FLAG_REBUILD | \
170 CTR_FLAG_DAEMON_SLEEP | \
171 CTR_FLAG_MIN_RECOVERY_RATE | \
172 CTR_FLAG_MAX_RECOVERY_RATE | \
173 CTR_FLAG_MAX_WRITE_BEHIND | \
174 CTR_FLAG_STRIPE_CACHE | \
175 CTR_FLAG_REGION_SIZE | \
176 CTR_FLAG_DELTA_DISKS | \
177 CTR_FLAG_DATA_OFFSET)
178/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200179
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200180/*
181 * Flags for rs->runtime_flags field
182 * (RT_FLAG prefix meaning "runtime flag")
183 *
184 * These are all internal and used to define runtime state,
185 * e.g. to prevent another resume from preresume processing
186 * the raid set all over again.
187 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200188#define RT_FLAG_RS_PRERESUMED 0
189#define RT_FLAG_RS_RESUMED 1
190#define RT_FLAG_RS_BITMAP_LOADED 2
191#define RT_FLAG_UPDATE_SBS 3
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200192#define RT_FLAG_RESHAPE_RS 4
Heinz Mauelshagen6e209022016-06-14 15:23:13 -0400193#define RT_FLAG_KEEP_RS_FROZEN 5
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200194
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200195/* Array elements of 64 bit needed for rebuild/write_mostly bits */
196#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 Brassow34f8ac6d2012-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. */
258 const unsigned parity_devs; /* # of parity devices. */
259 const unsigned minimal_devs; /* minimal # of devices in set. */
260 const unsigned level; /* RAID level. */
261 const unsigned algorithm; /* RAID algorithm. */
262} 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"},
303 { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
304 { 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 Mauelshagen33e53f02016-05-19 18:49:30 +0200330 * bool helpers to test for various raid levels of a raid set,
331 * is. it's level as reported by the superblock rather than
332 * 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 */
365static unsigned int __is_raid10_far(int layout);
366static 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 Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200375 return rs->md.recovery_cp != MaxSector;
376}
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 Mauelshagenf0902792016-05-19 18:49:27 +0200385 * bool helpers to test for various raid levels of a raid type
386 */
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
439 return ~0;
440}
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 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400476static unsigned int __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200477{
478 return layout & RAID10_OFFSET;
479}
480
481/* Return true if md raid10 near for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400482static unsigned int __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 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400488static unsigned int __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{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400529 return __raid10_near_copies(layout) > 1 ?
530 __raid10_near_copies(layout) : __raid10_far_copies(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200531}
532
533/* Return md raid10 format id for @format string */
534static int raid10_format_to_md_layout(struct raid_set *rs,
535 unsigned int algorithm,
536 unsigned int copies)
537{
538 unsigned int n = 1, f = 1, r = 0;
539
540 /*
541 * MD resilienece flaw:
542 *
543 * enabling use_far_sets for far/offset formats causes copies
544 * to be colocated on the same devs together with their origins!
545 *
546 * -> disable it for now in the definition above
547 */
548 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
549 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100550 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200551
552 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100553 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200554 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400555 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200556 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100557
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200558 } else if (algorithm == ALGORITHM_RAID10_FAR) {
559 f = copies;
560 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400561 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200562 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100563
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200564 } else
565 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100566
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200567 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
568}
569/* END: MD raid10 bit definitions and helpers */
570
571/* Check for any of the raid10 algorithms */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400572static int __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200573{
574 if (rtp->level == 10) {
575 switch (rtp->algorithm) {
576 case ALGORITHM_RAID10_DEFAULT:
577 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400578 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200579 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400580 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200581 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400582 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200583 default:
584 break;
585 }
586 }
587
588 return 0;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500589}
590
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200591/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200592static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000593{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200594 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000595
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200596 while (rtp-- > raid_types)
597 if (!strcasecmp(rtp->name, name))
598 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000599
600 return NULL;
601}
602
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200603/* Return raid_type for @name based derived from @level and @layout */
604static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
605{
606 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
607
608 while (rtp-- > raid_types) {
609 /* RAID10 special checks based on @layout flags/properties */
610 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400611 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200612 return rtp;
613 }
614
615 return NULL;
616}
617
618/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200619 * Conditionally change bdev capacity of @rs
620 * in case of a disk add/remove reshape
621 */
622static void rs_set_capacity(struct raid_set *rs)
623{
624 struct mddev *mddev = &rs->md;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200625 struct md_rdev *rdev;
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200626 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200627
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200628 /*
629 * raid10 sets rdev->sector to the device size, which
630 * is unintended in case of out-of-place reshaping
631 */
632 rdev_for_each(rdev, mddev)
633 rdev->sectors = mddev->dev_sectors;
634
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200635 set_capacity(gendisk, mddev->array_sectors);
636 revalidate_disk(gendisk);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200637}
638
639/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200640 * Set the mddev properties in @rs to the current
641 * ones retrieved from the freshest superblock
642 */
643static void rs_set_cur(struct raid_set *rs)
644{
645 struct mddev *mddev = &rs->md;
646
647 mddev->new_level = mddev->level;
648 mddev->new_layout = mddev->layout;
649 mddev->new_chunk_sectors = mddev->chunk_sectors;
650}
651
652/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200653 * Set the mddev properties in @rs to the new
654 * ones requested by the ctr
655 */
656static void rs_set_new(struct raid_set *rs)
657{
658 struct mddev *mddev = &rs->md;
659
660 mddev->level = mddev->new_level;
661 mddev->layout = mddev->new_layout;
662 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200663 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200664 mddev->delta_disks = 0;
665}
666
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400667static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
668 unsigned raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000669{
670 unsigned i;
671 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000672
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400673 if (raid_devs <= raid_type->parity_devs) {
674 ti->error = "Insufficient number of devices";
675 return ERR_PTR(-EINVAL);
676 }
NeilBrown9d09e662011-01-13 20:00:02 +0000677
NeilBrown9d09e662011-01-13 20:00:02 +0000678 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400679 if (!rs) {
680 ti->error = "Cannot allocate raid context";
681 return ERR_PTR(-ENOMEM);
682 }
NeilBrown9d09e662011-01-13 20:00:02 +0000683
684 mddev_init(&rs->md);
685
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200686 rs->raid_disks = raid_devs;
687 rs->delta_disks = 0;
688
NeilBrown9d09e662011-01-13 20:00:02 +0000689 rs->ti = ti;
690 rs->raid_type = raid_type;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200691 rs->stripe_cache_entries = 256;
NeilBrown9d09e662011-01-13 20:00:02 +0000692 rs->md.raid_disks = raid_devs;
693 rs->md.level = raid_type->level;
694 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000695 rs->md.layout = raid_type->algorithm;
696 rs->md.new_layout = rs->md.layout;
697 rs->md.delta_disks = 0;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200698 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +0000699
700 for (i = 0; i < raid_devs; i++)
701 md_rdev_init(&rs->dev[i].rdev);
702
703 /*
704 * Remaining items to be initialized by further RAID params:
705 * rs->md.persistent
706 * rs->md.external
707 * rs->md.chunk_sectors
708 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100709 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000710 */
711
712 return rs;
713}
714
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400715static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000716{
717 int i;
718
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100719 for (i = 0; i < rs->md.raid_disks; i++) {
720 if (rs->dev[i].meta_dev)
721 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000722 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000723 if (rs->dev[i].data_dev)
724 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100725 }
NeilBrown9d09e662011-01-13 20:00:02 +0000726
727 kfree(rs);
728}
729
730/*
731 * For every device we have two words
732 * <meta_dev>: meta device name or '-' if missing
733 * <data_dev>: data device name or '-' if missing
734 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100735 * The following are permitted:
736 * - -
737 * - <data_dev>
738 * <meta_dev> <data_dev>
739 *
740 * The following is not allowed:
741 * <meta_dev> -
742 *
743 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400744 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000745 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200746static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000747{
748 int i;
749 int rebuild = 0;
750 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200751 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200752 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000753
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200754 /* Put off the number of raid devices argument to get to dev pairs */
755 arg = dm_shift_arg(as);
756 if (!arg)
757 return -EINVAL;
758
759 for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000760 rs->dev[i].rdev.raid_disk = i;
761
762 rs->dev[i].meta_dev = NULL;
763 rs->dev[i].data_dev = NULL;
764
765 /*
766 * There are no offsets, since there is a separate device
767 * for data and metadata.
768 */
769 rs->dev[i].rdev.data_offset = 0;
770 rs->dev[i].rdev.mddev = &rs->md;
771
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200772 arg = dm_shift_arg(as);
773 if (!arg)
774 return -EINVAL;
775
776 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400777 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
778 &rs->dev[i].meta_dev);
779 if (r) {
780 rs->ti->error = "RAID metadata device lookup failure";
781 return r;
782 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100783
784 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400785 if (!rs->dev[i].rdev.sb_page) {
786 rs->ti->error = "Failed to allocate superblock page";
787 return -ENOMEM;
788 }
NeilBrown9d09e662011-01-13 20:00:02 +0000789 }
790
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200791 arg = dm_shift_arg(as);
792 if (!arg)
793 return -EINVAL;
794
795 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000796 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400797 (!rs->dev[i].rdev.recovery_offset)) {
798 rs->ti->error = "Drive designated for rebuild not specified";
799 return -EINVAL;
800 }
NeilBrown9d09e662011-01-13 20:00:02 +0000801
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400802 if (rs->dev[i].meta_dev) {
803 rs->ti->error = "No data device supplied with metadata device";
804 return -EINVAL;
805 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100806
NeilBrown9d09e662011-01-13 20:00:02 +0000807 continue;
808 }
809
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400810 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
811 &rs->dev[i].data_dev);
812 if (r) {
813 rs->ti->error = "RAID device lookup failure";
814 return r;
815 }
NeilBrown9d09e662011-01-13 20:00:02 +0000816
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100817 if (rs->dev[i].meta_dev) {
818 metadata_available = 1;
819 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
820 }
NeilBrown9d09e662011-01-13 20:00:02 +0000821 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200822 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000823 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
824 rebuild++;
825 }
826
827 if (metadata_available) {
828 rs->md.external = 0;
829 rs->md.persistent = 1;
830 rs->md.major_version = 2;
831 } else if (rebuild && !rs->md.recovery_cp) {
832 /*
833 * Without metadata, we will not be able to tell if the array
834 * is in-sync or not - we must assume it is not. Therefore,
835 * it is impossible to rebuild a drive.
836 *
837 * Even if there is metadata, the on-disk information may
838 * indicate that the array is not in-sync and it will then
839 * fail at that time.
840 *
841 * User could specify 'nosync' option if desperate.
842 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400843 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
844 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000845 }
846
847 return 0;
848}
849
850/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100851 * validate_region_size
852 * @rs
853 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
854 *
855 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
856 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
857 *
858 * Returns: 0 on success, -EINVAL on failure.
859 */
860static int validate_region_size(struct raid_set *rs, unsigned long region_size)
861{
862 unsigned long min_region_size = rs->ti->len / (1 << 21);
863
864 if (!region_size) {
865 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400866 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100867 */
868 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000869 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400870 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100871 DMINFO("Choosing default region size of %lu sectors",
872 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100873 } else {
874 DMINFO("Choosing default region size of 4MiB");
875 region_size = 1 << 13; /* sectors */
876 }
877 } else {
878 /*
879 * Validate user-supplied value.
880 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400881 if (region_size > rs->ti->len) {
882 rs->ti->error = "Supplied region size is too large";
883 return -EINVAL;
884 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100885
886 if (region_size < min_region_size) {
887 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
888 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400889 rs->ti->error = "Supplied region size is too small";
890 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100891 }
892
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400893 if (!is_power_of_2(region_size)) {
894 rs->ti->error = "Region size is not a power of 2";
895 return -EINVAL;
896 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100897
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400898 if (region_size < rs->md.chunk_sectors) {
899 rs->ti->error = "Region size is smaller than the chunk size";
900 return -EINVAL;
901 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100902 }
903
904 /*
905 * Convert sectors to bytes.
906 */
907 rs->md.bitmap_info.chunksize = (region_size << 9);
908
909 return 0;
910}
911
912/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600913 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100914 * @rs
915 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600916 * Determine if there are enough devices in the array that haven't
917 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100918 *
919 * Returns: 0 on success, -EINVAL on failure.
920 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600921static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100922{
923 unsigned i, rebuild_cnt = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200924 unsigned rebuilds_per_group = 0, copies;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100925 unsigned group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100926
Jonathan Brassoweb649122012-10-11 13:40:09 +1100927 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600928 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
929 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100930 rebuild_cnt++;
931
932 switch (rs->raid_type->level) {
933 case 1:
934 if (rebuild_cnt >= rs->md.raid_disks)
935 goto too_many;
936 break;
937 case 4:
938 case 5:
939 case 6:
940 if (rebuild_cnt > rs->raid_type->parity_devs)
941 goto too_many;
942 break;
943 case 10:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200944 copies = raid10_md_layout_to_copies(rs->md.new_layout);
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100945 if (rebuild_cnt < copies)
946 break;
947
948 /*
949 * It is possible to have a higher rebuild count for RAID10,
950 * as long as the failed devices occur in different mirror
951 * groups (i.e. different stripes).
952 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100953 * When checking "near" format, make sure no adjacent devices
954 * have failed beyond what can be handled. In addition to the
955 * simple case where the number of devices is a multiple of the
956 * number of copies, we must also handle cases where the number
957 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -0400958 * E.g. dev1 dev2 dev3 dev4 dev5
959 * A A B B C
960 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100961 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200962 if (__is_raid10_near(rs->md.new_layout)) {
963 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100964 if (!(i % copies))
965 rebuilds_per_group = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200966 if ((!rs->dev[i].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200967 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100968 (++rebuilds_per_group >= copies))
969 goto too_many;
970 }
971 break;
972 }
973
974 /*
975 * When checking "far" and "offset" formats, we need to ensure
976 * that the device that holds its copy is not also dead or
977 * being rebuilt. (Note that "far" and "offset" formats only
978 * support two copies right now. These formats also only ever
979 * use the 'use_far_sets' variant.)
980 *
981 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -0400982 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100983 * results in the need to treat the last (potentially larger)
984 * set differently.
985 */
986 group_size = (rs->md.raid_disks / copies);
987 last_group_start = (rs->md.raid_disks / group_size) - 1;
988 last_group_start *= group_size;
989 for (i = 0; i < rs->md.raid_disks; i++) {
990 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600991 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100992 if ((!rs->dev[i].rdev.sb_page ||
993 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100994 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100995 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100996 }
997 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100998 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600999 if (rebuild_cnt)
1000 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001001 }
1002
1003 return 0;
1004
1005too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +11001006 return -EINVAL;
1007}
1008
1009/*
NeilBrown9d09e662011-01-13 20:00:02 +00001010 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +00001011 * <chunk_size> [optional_args]
1012 *
Jonathan Brassow32737272011-08-02 12:32:07 +01001013 * Argument definitions
1014 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -04001015 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +01001016 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -04001017 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +00001018 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +01001019 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -04001020 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +00001021 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
1022 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001023 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +00001024 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
1025 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -04001026 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001027 *
1028 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -04001029 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001030 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +00001031 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001032static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown9d09e662011-01-13 20:00:02 +00001033 unsigned num_raid_params)
1034{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001035 int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001036 unsigned raid10_copies = 2;
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001037 unsigned i, write_mostly = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001038 unsigned region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +01001039 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001040 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001041 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001042 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001043
1044 arg = dm_shift_arg(as);
1045 num_raid_params--; /* Account for chunk_size argument */
1046
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001047 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001048 rs->ti->error = "Bad numerical argument given for chunk_size";
1049 return -EINVAL;
1050 }
NeilBrown9d09e662011-01-13 20:00:02 +00001051
1052 /*
1053 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001054 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001055 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001056 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001057 if (value)
1058 DMERR("Ignoring chunk size parameter for RAID 1");
1059 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001060 } else if (!is_power_of_2(value)) {
1061 rs->ti->error = "Chunk size must be a power of 2";
1062 return -EINVAL;
1063 } else if (value < 8) {
1064 rs->ti->error = "Chunk size value is too small";
1065 return -EINVAL;
1066 }
NeilBrown9d09e662011-01-13 20:00:02 +00001067
1068 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001069
1070 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001071 * We set each individual device as In_sync with a completed
1072 * 'recovery_offset'. If there has been a device failure or
1073 * replacement then one of the following cases applies:
1074 *
1075 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001076 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001077 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001078 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001079 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001080 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001081 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001082 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001083 *
1084 * What is found in the superblocks of the devices is always
1085 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1086 */
1087 for (i = 0; i < rs->md.raid_disks; i++) {
1088 set_bit(In_sync, &rs->dev[i].rdev.flags);
1089 rs->dev[i].rdev.recovery_offset = MaxSector;
1090 }
1091
1092 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001093 * Second, parse the unordered optional arguments
1094 */
NeilBrown9d09e662011-01-13 20:00:02 +00001095 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001096 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001097 if (!key) {
1098 rs->ti->error = "Not enough raid parameters given";
1099 return -EINVAL;
1100 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001101
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001102 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001103 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001104 rs->ti->error = "Only one 'nosync' argument allowed";
1105 return -EINVAL;
1106 }
NeilBrown9d09e662011-01-13 20:00:02 +00001107 continue;
1108 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001109 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001110 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001111 rs->ti->error = "Only one 'sync' argument allowed";
1112 return -EINVAL;
1113 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001114 continue;
1115 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001116 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001117 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001118 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1119 return -EINVAL;
1120 }
NeilBrown9d09e662011-01-13 20:00:02 +00001121 continue;
1122 }
1123
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001124 arg = dm_shift_arg(as);
1125 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001126 if (!arg) {
1127 rs->ti->error = "Wrong number of raid parameters given";
1128 return -EINVAL;
1129 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001130
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001131 /*
1132 * Parameters that take a string value are checked here.
1133 */
1134
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001135 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001136 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001137 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1138 return -EINVAL;
1139 }
1140 if (!rt_is_raid10(rt)) {
1141 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1142 return -EINVAL;
1143 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001144 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001145 if (raid10_format < 0) {
1146 rs->ti->error = "Invalid 'raid10_format' value given";
1147 return raid10_format;
1148 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001149 continue;
1150 }
1151
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001152 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001153 rs->ti->error = "Bad numerical argument given in raid params";
1154 return -EINVAL;
1155 }
NeilBrown9d09e662011-01-13 20:00:02 +00001156
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001157 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001158 /*
1159 * "rebuild" is being passed in by userspace to provide
1160 * indexes of replaced devices and to set up additional
1161 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001162 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001163 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001164 rs->ti->error = "Invalid rebuild index given";
1165 return -EINVAL;
1166 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001167
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001168 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1169 rs->ti->error = "rebuild for this index already given";
1170 return -EINVAL;
1171 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001172
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001173 rd = rs->dev + value;
1174 clear_bit(In_sync, &rd->rdev.flags);
1175 clear_bit(Faulty, &rd->rdev.flags);
1176 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001177 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001178 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001179 if (!rt_is_raid1(rt)) {
1180 rs->ti->error = "write_mostly option is only valid for RAID1";
1181 return -EINVAL;
1182 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001183
Mike Snitzerbb91a632016-06-02 12:06:54 -04001184 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001185 rs->ti->error = "Invalid write_mostly index given";
1186 return -EINVAL;
1187 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001188
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001189 write_mostly++;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001190 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001191 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001192 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001193 if (!rt_is_raid1(rt)) {
1194 rs->ti->error = "max_write_behind option is only valid for RAID1";
1195 return -EINVAL;
1196 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001197
Mike Snitzer42863252016-06-02 12:27:46 -04001198 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001199 rs->ti->error = "Only one max_write_behind argument pair allowed";
1200 return -EINVAL;
1201 }
NeilBrown9d09e662011-01-13 20:00:02 +00001202
1203 /*
1204 * In device-mapper, we specify things in sectors, but
1205 * MD records this value in kB
1206 */
1207 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001208 if (value > COUNTER_MAX) {
1209 rs->ti->error = "Max write-behind limit out of range";
1210 return -EINVAL;
1211 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001212
NeilBrown9d09e662011-01-13 20:00:02 +00001213 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001214 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001215 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001216 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1217 return -EINVAL;
1218 }
1219 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1220 rs->ti->error = "daemon sleep period out of range";
1221 return -EINVAL;
1222 }
NeilBrown9d09e662011-01-13 20:00:02 +00001223 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001224 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001225 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001226 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001227 rs->ti->error = "Only one data_offset argument pair allowed";
1228 return -EINVAL;
1229 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001230 /* Ensure sensible data offset */
Heinz Mauelshagen75dd3b92016-06-15 22:27:08 +02001231 if (value < 0 ||
1232 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001233 rs->ti->error = "Bogus data_offset value";
1234 return -EINVAL;
1235 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001236 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001237 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001238 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001239 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001240 rs->ti->error = "Only one delta_disks argument pair allowed";
1241 return -EINVAL;
1242 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001243 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001244 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001245 rs->ti->error = "Too many delta_disk requested";
1246 return -EINVAL;
1247 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001248
1249 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001250 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001251 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001252 rs->ti->error = "Only one stripe_cache argument pair allowed";
1253 return -EINVAL;
1254 }
1255
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001256 if (!rt_is_raid456(rt)) {
1257 rs->ti->error = "Inappropriate argument: stripe_cache";
1258 return -EINVAL;
1259 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001260
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001261 rs->stripe_cache_entries = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001262 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001263 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001264 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1265 return -EINVAL;
1266 }
1267 if (value > INT_MAX) {
1268 rs->ti->error = "min_recovery_rate out of range";
1269 return -EINVAL;
1270 }
NeilBrown9d09e662011-01-13 20:00:02 +00001271 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001272 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001273 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001274 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1275 return -EINVAL;
1276 }
1277 if (value > INT_MAX) {
1278 rs->ti->error = "max_recovery_rate out of range";
1279 return -EINVAL;
1280 }
NeilBrown9d09e662011-01-13 20:00:02 +00001281 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001282 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001283 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001284 rs->ti->error = "Only one region_size argument pair allowed";
1285 return -EINVAL;
1286 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001287
Jonathan Brassowc1084562011-08-02 12:32:07 +01001288 region_size = value;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02001289 rs->requested_bitmap_chunk_sectors = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001290 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001291 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001292 rs->ti->error = "Only one raid10_copies argument pair allowed";
1293 return -EINVAL;
1294 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001295
Mike Snitzerbb91a632016-06-02 12:06:54 -04001296 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001297 rs->ti->error = "Bad value for 'raid10_copies'";
1298 return -EINVAL;
1299 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001300
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001301 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001302 } else {
1303 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001304 rs->ti->error = "Unable to parse RAID parameter";
1305 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001306 }
1307 }
1308
Heinz Mauelshagen0d851d12016-06-15 18:43:55 +02001309 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
1310 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
1311 rs->ti->error = "sync and nosync are mutually exclusive";
1312 return -EINVAL;
1313 }
1314
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001315 if (write_mostly >= rs->md.raid_disks) {
1316 rs->ti->error = "Can't set all raid1 devices to write_mostly";
1317 return -EINVAL;
1318 }
1319
Jonathan Brassowc1084562011-08-02 12:32:07 +01001320 if (validate_region_size(rs, region_size))
1321 return -EINVAL;
1322
1323 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001324 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001325 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001326 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001327
Mike Snitzer542f9032012-07-27 15:08:00 +01001328 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1329 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001330
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001331 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001332 if (raid10_copies > rs->md.raid_disks) {
1333 rs->ti->error = "Not enough devices to satisfy specification";
1334 return -EINVAL;
1335 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001336
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001337 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001338 if (rs->md.new_layout < 0) {
1339 rs->ti->error = "Error getting raid10 format";
1340 return rs->md.new_layout;
1341 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001342
1343 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001344 if (!rt) {
1345 rs->ti->error = "Failed to recognize new raid10 layout";
1346 return -EINVAL;
1347 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001348
1349 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1350 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001351 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001352 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1353 return -EINVAL;
1354 }
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001355 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001356
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001357 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001358
NeilBrown9d09e662011-01-13 20:00:02 +00001359 /* Assume there are no metadata devices until the drives are parsed */
1360 rs->md.persistent = 0;
1361 rs->md.external = 1;
1362
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001363 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001364 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001365}
1366
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001367/* Set raid4/5/6 cache size */
1368static int rs_set_raid456_stripe_cache(struct raid_set *rs)
1369{
1370 int r;
1371 struct r5conf *conf;
1372 struct mddev *mddev = &rs->md;
1373 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
1374 uint32_t nr_stripes = rs->stripe_cache_entries;
1375
1376 if (!rt_is_raid456(rs->raid_type)) {
1377 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
1378 return -EINVAL;
1379 }
1380
1381 if (nr_stripes < min_stripes) {
1382 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
1383 nr_stripes, min_stripes);
1384 nr_stripes = min_stripes;
1385 }
1386
1387 conf = mddev->private;
1388 if (!conf) {
1389 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
1390 return -EINVAL;
1391 }
1392
1393 /* Try setting number of stripes in raid456 stripe cache */
1394 if (conf->min_nr_stripes != nr_stripes) {
1395 r = raid5_set_cache_size(mddev, nr_stripes);
1396 if (r) {
1397 rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
1398 return r;
1399 }
1400
1401 DMINFO("%u stripe cache entries", nr_stripes);
1402 }
1403
1404 return 0;
1405}
1406
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001407/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1408static unsigned int mddev_data_stripes(struct raid_set *rs)
1409{
1410 return rs->md.raid_disks - rs->raid_type->parity_devs;
1411}
1412
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001413/* Return # of data stripes of @rs (i.e. as of ctr) */
1414static unsigned int rs_data_stripes(struct raid_set *rs)
1415{
1416 return rs->raid_disks - rs->raid_type->parity_devs;
1417}
1418
1419/* Calculate the sectors per device and per array used for @rs */
1420static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1421{
1422 int delta_disks;
1423 unsigned int data_stripes;
1424 struct mddev *mddev = &rs->md;
1425 struct md_rdev *rdev;
1426 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1427
1428 if (use_mddev) {
1429 delta_disks = mddev->delta_disks;
1430 data_stripes = mddev_data_stripes(rs);
1431 } else {
1432 delta_disks = rs->delta_disks;
1433 data_stripes = rs_data_stripes(rs);
1434 }
1435
1436 /* Special raid1 case w/o delta_disks support (yet) */
1437 if (rt_is_raid1(rs->raid_type))
1438 ;
1439 else if (rt_is_raid10(rs->raid_type)) {
1440 if (rs->raid10_copies < 2 ||
1441 delta_disks < 0) {
1442 rs->ti->error = "Bogus raid10 data copies or delta disks";
1443 return EINVAL;
1444 }
1445
1446 dev_sectors *= rs->raid10_copies;
1447 if (sector_div(dev_sectors, data_stripes))
1448 goto bad;
1449
1450 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1451 if (sector_div(array_sectors, rs->raid10_copies))
1452 goto bad;
1453
1454 } else if (sector_div(dev_sectors, data_stripes))
1455 goto bad;
1456
1457 else
1458 /* Striped layouts */
1459 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1460
1461 rdev_for_each(rdev, mddev)
1462 rdev->sectors = dev_sectors;
1463
1464 mddev->array_sectors = array_sectors;
1465 mddev->dev_sectors = dev_sectors;
1466
1467 return 0;
1468bad:
1469 rs->ti->error = "Target length not divisible by number of data devices";
1470 return EINVAL;
1471}
1472
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001473/* Setup recovery on @rs */
1474static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1475{
1476 /* raid0 does not recover */
1477 if (rs_is_raid0(rs))
1478 rs->md.recovery_cp = MaxSector;
1479 /*
1480 * A raid6 set has to be recovered either
1481 * completely or for the grown part to
1482 * ensure proper parity and Q-Syndrome
1483 */
1484 else if (rs_is_raid6(rs))
1485 rs->md.recovery_cp = dev_sectors;
1486 /*
1487 * Other raid set types may skip recovery
1488 * depending on the 'nosync' flag.
1489 */
1490 else
1491 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
1492 ? MaxSector : dev_sectors;
1493}
1494
1495/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
1496static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1497{
1498 if (!dev_sectors)
1499 /* New raid set or 'sync' flag provided */
1500 __rs_setup_recovery(rs, 0);
1501 else if (dev_sectors == MaxSector)
1502 /* Prevent recovery */
1503 __rs_setup_recovery(rs, MaxSector);
1504 else if (rs->dev[0].rdev.sectors < dev_sectors)
1505 /* Grown raid set */
1506 __rs_setup_recovery(rs, rs->dev[0].rdev.sectors);
1507 else
1508 __rs_setup_recovery(rs, MaxSector);
1509}
1510
NeilBrown9d09e662011-01-13 20:00:02 +00001511static void do_table_event(struct work_struct *ws)
1512{
1513 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1514
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001515 smp_rmb(); /* Make sure we access most actual mddev properties */
1516 if (!rs_is_reshaping(rs))
1517 rs_set_capacity(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001518 dm_table_event(rs->ti->table);
1519}
1520
1521static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1522{
1523 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1524
NeilBrown5c675f82014-12-15 12:56:56 +11001525 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001526}
1527
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001528/*
1529 * Make sure a valid takover (level switch) is being requested on @rs
1530 *
1531 * Conversions of raid sets from one MD personality to another
1532 * have to conform to restrictions which are enforced here.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001533 */
1534static int rs_check_takeover(struct raid_set *rs)
1535{
1536 struct mddev *mddev = &rs->md;
1537 unsigned int near_copies;
1538
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001539 if (rs->md.degraded) {
1540 rs->ti->error = "Can't takeover degraded raid set";
1541 return -EPERM;
1542 }
1543
1544 if (rs_is_reshaping(rs)) {
1545 rs->ti->error = "Can't takeover reshaping raid set";
1546 return -EPERM;
1547 }
1548
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001549 switch (mddev->level) {
1550 case 0:
1551 /* raid0 -> raid1/5 with one disk */
1552 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1553 mddev->raid_disks == 1)
1554 return 0;
1555
1556 /* raid0 -> raid10 */
1557 if (mddev->new_level == 10 &&
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001558 !(rs->raid_disks % mddev->raid_disks))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001559 return 0;
1560
1561 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001562 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001563 mddev->new_layout == ALGORITHM_PARITY_N &&
1564 mddev->raid_disks > 1)
1565 return 0;
1566
1567 break;
1568
1569 case 10:
1570 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001571 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001572 break;
1573
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001574 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001575
1576 /* raid10* -> raid0 */
1577 if (mddev->new_level == 0) {
1578 /* Can takeover raid10_near with raid disks divisable by data copies! */
1579 if (near_copies > 1 &&
1580 !(mddev->raid_disks % near_copies)) {
1581 mddev->raid_disks /= near_copies;
1582 mddev->delta_disks = mddev->raid_disks;
1583 return 0;
1584 }
1585
1586 /* Can takeover raid10_far */
1587 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001588 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001589 return 0;
1590
1591 break;
1592 }
1593
1594 /* raid10_{near,far} -> raid1 */
1595 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001596 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001597 return 0;
1598
1599 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001600 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001601 mddev->raid_disks == 2)
1602 return 0;
1603 break;
1604
1605 case 1:
1606 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001607 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001608 mddev->raid_disks == 2) {
1609 mddev->degraded = 1;
1610 return 0;
1611 }
1612
1613 /* raid1 -> raid0 */
1614 if (mddev->new_level == 0 &&
1615 mddev->raid_disks == 1)
1616 return 0;
1617
1618 /* raid1 -> raid10 */
1619 if (mddev->new_level == 10)
1620 return 0;
1621
1622 break;
1623
1624 case 4:
1625 /* raid4 -> raid0 */
1626 if (mddev->new_level == 0)
1627 return 0;
1628
1629 /* raid4 -> raid1/5 with 2 disks */
1630 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1631 mddev->raid_disks == 2)
1632 return 0;
1633
1634 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001635 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001636 mddev->layout == ALGORITHM_PARITY_N)
1637 return 0;
1638 break;
1639
1640 case 5:
1641 /* raid5 with parity N -> raid0 */
1642 if (mddev->new_level == 0 &&
1643 mddev->layout == ALGORITHM_PARITY_N)
1644 return 0;
1645
1646 /* raid5 with parity N -> raid4 */
1647 if (mddev->new_level == 4 &&
1648 mddev->layout == ALGORITHM_PARITY_N)
1649 return 0;
1650
1651 /* raid5 with 2 disks -> raid1/4/10 */
1652 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1653 mddev->raid_disks == 2)
1654 return 0;
1655
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001656 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001657 if (mddev->new_level == 6 &&
1658 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001659 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001660 return 0;
1661 break;
1662
1663 case 6:
1664 /* raid6 with parity N -> raid0 */
1665 if (mddev->new_level == 0 &&
1666 mddev->layout == ALGORITHM_PARITY_N)
1667 return 0;
1668
1669 /* raid6 with parity N -> raid4 */
1670 if (mddev->new_level == 4 &&
1671 mddev->layout == ALGORITHM_PARITY_N)
1672 return 0;
1673
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001674 /* raid6_*_n with Q-Syndrome N -> raid5_* */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001675 if (mddev->new_level == 5 &&
1676 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001677 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001678 return 0;
1679
1680 default:
1681 break;
1682 }
1683
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001684 rs->ti->error = "takeover not possible";
1685 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001686}
1687
1688/* True if @rs requested to be taken over */
1689static bool rs_takeover_requested(struct raid_set *rs)
1690{
1691 return rs->md.new_level != rs->md.level;
1692}
1693
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001694/* True if @rs is requested to reshape by ctr */
1695static bool rs_reshape_requested(struct raid_set *rs)
1696{
1697 struct mddev *mddev = &rs->md;
1698
1699 if (!mddev->level)
1700 return false;
1701
1702 return !__is_raid10_far(mddev->new_layout) &&
1703 mddev->new_level == mddev->level &&
1704 (mddev->new_layout != mddev->layout ||
1705 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1706 rs->raid_disks + rs->delta_disks != mddev->raid_disks);
1707}
1708
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001709/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001710#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001711
1712/* State flags for sb->flags */
1713#define SB_FLAG_RESHAPE_ACTIVE 0x1
1714#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1715
NeilBrown9d09e662011-01-13 20:00:02 +00001716/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001717 * This structure is never routinely used by userspace, unlike md superblocks.
1718 * Devices with this superblock should only ever be accessed via device-mapper.
1719 */
1720#define DM_RAID_MAGIC 0x64526D44
1721struct dm_raid_superblock {
1722 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001723 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001724
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001725 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1726 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001727
1728 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001729 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001730 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001731
1732 /*
1733 * This offset tracks the progress of the repair or replacement of
1734 * an individual drive.
1735 */
1736 __le64 disk_recovery_offset;
1737
1738 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001739 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001740 * synchronisation/parity calculation.
1741 */
1742 __le64 array_resync_offset;
1743
1744 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001745 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001746 */
1747 __le32 level;
1748 __le32 layout;
1749 __le32 stripe_sectors;
1750
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001751 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001752 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001753 *
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001754 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001755 */
1756
1757 __le32 flags; /* Flags defining array states for reshaping */
1758
1759 /*
1760 * This offset tracks the progress of a raid
1761 * set reshape in order to be able to restart it
1762 */
1763 __le64 reshape_position;
1764
1765 /*
1766 * These define the properties of the array in case of an interrupted reshape
1767 */
1768 __le32 new_level;
1769 __le32 new_layout;
1770 __le32 new_stripe_sectors;
1771 __le32 delta_disks;
1772
1773 __le64 array_sectors; /* Array size in sectors */
1774
1775 /*
1776 * Sector offsets to data on devices (reshaping).
1777 * Needed to support out of place reshaping, thus
1778 * not writing over any stripes whilst converting
1779 * them from old to new layout
1780 */
1781 __le64 data_offset;
1782 __le64 new_data_offset;
1783
1784 __le64 sectors; /* Used device size in sectors */
1785
1786 /*
1787 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001788 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001789 */
1790 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1791
1792 __le32 incompat_features; /* Used to indicate any incompatible features */
1793
1794 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001795} __packed;
1796
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001797/*
1798 * Check for reshape constraints on raid set @rs:
1799 *
1800 * - reshape function non-existent
1801 * - degraded set
1802 * - ongoing recovery
1803 * - ongoing reshape
1804 *
1805 * Returns 0 if none or -EPERM if given constraint
1806 * and error message reference in @errmsg
1807 */
1808static int rs_check_reshape(struct raid_set *rs)
1809{
1810 struct mddev *mddev = &rs->md;
1811
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001812 if (!mddev->pers || !mddev->pers->check_reshape)
1813 rs->ti->error = "Reshape not supported";
1814 else if (mddev->degraded)
1815 rs->ti->error = "Can't reshape degraded raid set";
1816 else if (rs_is_recovering(rs))
1817 rs->ti->error = "Convert request on recovering raid set prohibited";
1818 else if (mddev->reshape_position && rs_is_reshaping(rs))
1819 rs->ti->error = "raid set already reshaping!";
1820 else if (!(rs_is_raid10(rs) || rs_is_raid456(rs)))
1821 rs->ti->error = "Reshaping only supported for raid4/5/6/10";
1822 else
1823 return 0;
1824
1825 return -EPERM;
1826}
1827
NeilBrown3cb03002011-10-11 16:45:26 +11001828static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001829{
1830 BUG_ON(!rdev->sb_page);
1831
1832 if (rdev->sb_loaded)
1833 return 0;
1834
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02001835 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01001836 DMERR("Failed to read superblock of device at position %d",
1837 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10001838 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001839 return -EINVAL;
1840 }
1841
1842 rdev->sb_loaded = 1;
1843
1844 return 0;
1845}
1846
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001847static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1848{
1849 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1850 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1851
Mike Snitzer42863252016-06-02 12:27:46 -04001852 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001853 int i = ARRAY_SIZE(sb->extended_failed_devices);
1854
1855 while (i--)
1856 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1857 }
1858}
1859
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001860static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1861{
1862 int i = ARRAY_SIZE(sb->extended_failed_devices);
1863
1864 sb->failed_devices = cpu_to_le64(failed_devices[0]);
1865 while (i--)
1866 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1867}
1868
1869/*
1870 * Synchronize the superblock members with the raid set properties
1871 *
1872 * All superblock data is little endian.
1873 */
NeilBrownfd01b882011-10-11 16:47:53 +11001874static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001875{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001876 bool update_failed_devices = false;
1877 unsigned int i;
1878 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001879 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001880 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001881
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001882 /* No metadata device, no superblock */
1883 if (!rdev->meta_bdev)
1884 return;
1885
1886 BUG_ON(!rdev->sb_page);
1887
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001888 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001889
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001890 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001891
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001892 for (i = 0; i < rs->raid_disks; i++)
1893 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
1894 update_failed_devices = true;
1895 set_bit(i, (void *) failed_devices);
1896 }
1897
1898 if (update_failed_devices)
1899 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001900
1901 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001902 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001903
1904 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1905 sb->array_position = cpu_to_le32(rdev->raid_disk);
1906
1907 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001908
1909 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1910 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1911
1912 sb->level = cpu_to_le32(mddev->level);
1913 sb->layout = cpu_to_le32(mddev->layout);
1914 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001915
1916 sb->new_level = cpu_to_le32(mddev->new_level);
1917 sb->new_layout = cpu_to_le32(mddev->new_layout);
1918 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
1919
1920 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1921
1922 smp_rmb(); /* Make sure we access most recent reshape position */
1923 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1924 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
1925 /* Flag ongoing reshape */
1926 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
1927
1928 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1929 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04001930 } else {
1931 /* Clear reshape flags */
1932 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
1933 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001934
1935 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
1936 sb->data_offset = cpu_to_le64(rdev->data_offset);
1937 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
1938 sb->sectors = cpu_to_le64(rdev->sectors);
1939
1940 /* Zero out the rest of the payload after the size of the superblock */
1941 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001942}
1943
1944/*
1945 * super_load
1946 *
1947 * This function creates a superblock if one is not found on the device
1948 * and will decide which superblock to use if there's a choice.
1949 *
1950 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1951 */
NeilBrown3cb03002011-10-11 16:45:26 +11001952static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001953{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001954 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001955 struct dm_raid_superblock *sb;
1956 struct dm_raid_superblock *refsb;
1957 uint64_t events_sb, events_refsb;
1958
1959 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001960 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1961 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1962 DMERR("superblock size of a logical block is no longer valid");
1963 return -EINVAL;
1964 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001965
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001966 r = read_disk_sb(rdev, rdev->sb_size);
1967 if (r)
1968 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001969
1970 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001971
1972 /*
1973 * Two cases that we want to write new superblocks and rebuild:
1974 * 1) New device (no matching magic number)
1975 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1976 */
1977 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1978 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001979 super_sync(rdev->mddev, rdev);
1980
1981 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001982 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001983
1984 /* Force writing of superblocks to disk */
1985 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1986
1987 /* Any superblock is better than none, choose that if given */
1988 return refdev ? 0 : 1;
1989 }
1990
1991 if (!refdev)
1992 return 1;
1993
1994 events_sb = le64_to_cpu(sb->events);
1995
1996 refsb = page_address(refdev->sb_page);
1997 events_refsb = le64_to_cpu(refsb->events);
1998
1999 return (events_sb > events_refsb) ? 1 : 0;
2000}
2001
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002002static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002003{
2004 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002005 unsigned int d;
2006 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002007 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002008 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002009 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002010 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002011 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002012 struct dm_raid_superblock *sb2;
2013
2014 sb = page_address(rdev->sb_page);
2015 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002016
2017 /*
2018 * Initialise to 1 if this is a new superblock.
2019 */
2020 mddev->events = events_sb ? : 1;
2021
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002022 mddev->reshape_position = MaxSector;
2023
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002024 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002025 * Reshaping is supported, e.g. reshape_position is valid
2026 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002027 */
Mike Snitzer42863252016-06-02 12:27:46 -04002028 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002029 /* Superblock is authoritative wrt given raid set layout! */
2030 mddev->raid_disks = le32_to_cpu(sb->num_devices);
2031 mddev->level = le32_to_cpu(sb->level);
2032 mddev->layout = le32_to_cpu(sb->layout);
2033 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
2034 mddev->new_level = le32_to_cpu(sb->new_level);
2035 mddev->new_layout = le32_to_cpu(sb->new_layout);
2036 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
2037 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
2038 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
2039
2040 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04002041 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
2042 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002043 DMERR("Reshape requested but raid set is still reshaping");
2044 return -EINVAL;
2045 }
2046
2047 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04002048 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002049 mddev->reshape_backwards = 1;
2050 else
2051 mddev->reshape_backwards = 0;
2052
2053 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
2054 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
2055 }
2056
2057 } else {
2058 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002059 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002060 */
2061 if (le32_to_cpu(sb->level) != mddev->level) {
2062 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
2063 return -EINVAL;
2064 }
2065 if (le32_to_cpu(sb->layout) != mddev->layout) {
2066 DMERR("Reshaping raid sets not yet supported. (raid layout change)");
Mike Snitzer43157842016-05-30 13:03:37 -04002067 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
2068 DMERR(" Old layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002069 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
2070 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
Mike Snitzer43157842016-05-30 13:03:37 -04002071 DMERR(" New layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002072 raid10_md_layout_to_format(mddev->layout),
2073 raid10_md_layout_to_copies(mddev->layout));
2074 return -EINVAL;
2075 }
2076 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
2077 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
2078 return -EINVAL;
2079 }
2080
2081 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
2082 if (!rt_is_raid1(rs->raid_type) &&
2083 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
2084 DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
2085 sb->num_devices, mddev->raid_disks);
2086 return -EINVAL;
2087 }
2088
2089 /* Table line is checked vs. authoritative superblock */
2090 rs_set_new(rs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002091 }
2092
Mike Snitzer42863252016-06-02 12:27:46 -04002093 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002094 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
2095
2096 /*
2097 * During load, we set FirstUse if a new superblock was written.
2098 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002099 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04002100 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002101 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002102 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002103 * and the new device needs to be rebuilt - in which
2104 * case the In_sync bit will /not/ be set and
2105 * recovery_cp must be MaxSector.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002106 * 3) This is/are a new device(s) being added to an old
2107 * raid set during takeover to a higher raid level
2108 * to provide capacity for redundancy or during reshape
2109 * to add capacity to grow the raid set.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002110 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002111 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002112 rdev_for_each(r, mddev) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002113 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002114 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002115
2116 if (!test_bit(In_sync, &r->flags)) {
2117 DMINFO("Device %d specified for rebuild; clearing superblock",
2118 r->raid_disk);
2119 rebuilds++;
2120
2121 if (test_bit(FirstUse, &r->flags))
2122 rebuild_and_new++;
2123 }
2124
2125 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002126 }
2127
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002128 if (new_devs == rs->raid_disks || !rebuilds) {
2129 /* Replace a broken device */
2130 if (new_devs == 1 && !rs->delta_disks)
2131 ;
2132 if (new_devs == rs->raid_disks) {
2133 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002134 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002135 } else if (new_devs != rebuilds &&
2136 new_devs != rs->delta_disks) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002137 DMERR("New device injected into existing raid set without "
2138 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002139 return -EINVAL;
2140 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002141 } else if (new_devs && new_devs != rebuilds) {
2142 DMERR("%u 'rebuild' devices cannot be injected into"
2143 " a raid set with %u other first-time devices",
2144 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002145 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002146 } else if (rebuilds) {
2147 if (rebuild_and_new && rebuilds != rebuild_and_new) {
2148 DMERR("new device%s provided without 'rebuild'",
2149 new_devs > 1 ? "s" : "");
2150 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002151 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002152 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
2153 (unsigned long long) mddev->recovery_cp);
2154 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002155 } else if (rs_is_reshaping(rs)) {
2156 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
2157 (unsigned long long) mddev->reshape_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002158 return -EINVAL;
2159 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002160 }
2161
2162 /*
2163 * Now we set the Faulty bit for those devices that are
2164 * recorded in the superblock as failed.
2165 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002166 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11002167 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002168 if (!r->sb_page)
2169 continue;
2170 sb2 = page_address(r->sb_page);
2171 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002172 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002173
2174 /*
2175 * Check for any device re-ordering.
2176 */
2177 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2178 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002179 if (role < 0)
2180 continue;
2181
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002182 if (role != r->raid_disk) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002183 if (__is_raid10_near(mddev->layout)) {
2184 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002185 rs->raid_disks % rs->raid10_copies) {
2186 rs->ti->error =
2187 "Cannot change raid10 near set to odd # of devices!";
2188 return -EINVAL;
2189 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002190
2191 sb2->array_position = cpu_to_le32(r->raid_disk);
2192
2193 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002194 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2195 !rt_is_raid1(rs->raid_type)) {
2196 rs->ti->error = "Cannot change device positions in raid set";
2197 return -EINVAL;
2198 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002199
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002200 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002201 }
2202
2203 /*
2204 * Partial recovery is performed on
2205 * returning failed devices.
2206 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002207 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002208 set_bit(Faulty, &r->flags);
2209 }
2210 }
2211
2212 return 0;
2213}
2214
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002215static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002216{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002217 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002218 struct dm_raid_superblock *sb;
2219
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002220 if (rs_is_raid0(rs) || !rdev->sb_page)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002221 return 0;
2222
2223 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002224
2225 /*
2226 * If mddev->events is not set, we know we have not yet initialized
2227 * the array.
2228 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002229 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002230 return -EINVAL;
2231
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002232 if (le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
2233 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2234 return -EINVAL;
2235 }
2236
2237 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002238 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002239 return -EINVAL;
2240 }
2241
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002242 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002243 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002244 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
2245
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002246 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
2247 /* Retrieve device size stored in superblock to be prepared for shrink */
2248 rdev->sectors = le64_to_cpu(sb->sectors);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002249 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002250 if (rdev->recovery_offset == MaxSector)
2251 set_bit(In_sync, &rdev->flags);
2252 /*
2253 * If no reshape in progress -> we're recovering single
2254 * disk(s) and have to set the device(s) to out-of-sync
2255 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002256 else if (!rs_is_reshaping(rs))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002257 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002258 }
2259
2260 /*
2261 * If a device comes back, set it as not In_sync and no longer faulty.
2262 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002263 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2264 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002265 clear_bit(In_sync, &rdev->flags);
2266 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002267 }
2268
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002269 /* Reshape support -> restore repective data offsets */
2270 rdev->data_offset = le64_to_cpu(sb->data_offset);
2271 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002272
2273 return 0;
2274}
2275
2276/*
2277 * Analyse superblocks and select the freshest.
2278 */
2279static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2280{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002281 int r;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002282 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002283 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002284 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002285
2286 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002287 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11002288 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002289 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002290 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002291 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002292 * of the "sync" directive.
2293 *
2294 * When reshaping capability is added, we must ensure
2295 * that the "sync" directive is disallowed during the
2296 * reshape.
2297 */
Mike Snitzer42863252016-06-02 12:27:46 -04002298 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002299 continue;
2300
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002301 if (!rdev->meta_bdev)
2302 continue;
2303
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002304 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002305
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002306 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002307 case 1:
2308 freshest = rdev;
2309 break;
2310 case 0:
2311 break;
2312 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01002313 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002314 if (dev->meta_dev)
2315 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002316
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002317 dev->meta_dev = NULL;
2318 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002319
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002320 if (rdev->sb_page)
2321 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002322
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002323 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002324
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002325 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002326
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002327 /*
2328 * We might be able to salvage the data device
2329 * even though the meta device has failed. For
2330 * now, we behave as though '- -' had been
2331 * set for this device in the table.
2332 */
2333 if (dev->data_dev)
2334 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002335
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002336 dev->data_dev = NULL;
2337 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002338
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002339 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002340 }
2341 }
2342
2343 if (!freshest)
2344 return 0;
2345
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002346 if (validate_raid_redundancy(rs)) {
2347 rs->ti->error = "Insufficient redundancy to activate array";
2348 return -EINVAL;
2349 }
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002350
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002351 /*
2352 * Validation of the freshest device provides the source of
2353 * validation for the remaining devices.
2354 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002355 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2356 if (super_validate(rs, freshest))
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002357 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002358
NeilBrowndafb20f2012-03-19 12:46:39 +11002359 rdev_for_each(rdev, mddev)
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002360 if ((rdev != freshest) && super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002361 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002362 return 0;
2363}
2364
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002365/*
2366 * Adjust data_offset and new_data_offset on all disk members of @rs
2367 * for out of place reshaping if requested by contructor
2368 *
2369 * We need free space at the beginning of each raid disk for forward
2370 * and at the end for backward reshapes which userspace has to provide
2371 * via remapping/reordering of space.
2372 */
2373static int rs_adjust_data_offsets(struct raid_set *rs)
2374{
2375 sector_t data_offset = 0, new_data_offset = 0;
2376 struct md_rdev *rdev;
2377
2378 /* Constructor did not request data offset change */
2379 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2380 if (!rs_is_reshapable(rs))
2381 goto out;
2382
2383 return 0;
2384 }
2385
2386 /* HM FIXME: get InSync raid_dev? */
2387 rdev = &rs->dev[0].rdev;
2388
2389 if (rs->delta_disks < 0) {
2390 /*
2391 * Removing disks (reshaping backwards):
2392 *
2393 * - before reshape: data is at offset 0 and free space
2394 * is at end of each component LV
2395 *
2396 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2397 */
2398 data_offset = 0;
2399 new_data_offset = rs->data_offset;
2400
2401 } else if (rs->delta_disks > 0) {
2402 /*
2403 * Adding disks (reshaping forwards):
2404 *
2405 * - before reshape: data is at offset rs->data_offset != 0 and
2406 * free space is at begin of each component LV
2407 *
2408 * - after reshape: data is at offset 0 on each component LV
2409 */
2410 data_offset = rs->data_offset;
2411 new_data_offset = 0;
2412
2413 } else {
2414 /*
2415 * User space passes in 0 for data offset after having removed reshape space
2416 *
2417 * - or - (data offset != 0)
2418 *
2419 * Changing RAID layout or chunk size -> toggle offsets
2420 *
2421 * - before reshape: data is at offset rs->data_offset 0 and
2422 * free space is at end of each component LV
2423 * -or-
2424 * data is at offset rs->data_offset != 0 and
2425 * free space is at begin of each component LV
2426 *
2427 * - after reshape: data is at offset 0 if i was at offset != 0
2428 * of at offset != 0 if it was at offset 0
2429 * on each component LV
2430 *
2431 */
2432 data_offset = rs->data_offset ? rdev->data_offset : 0;
2433 new_data_offset = data_offset ? 0 : rs->data_offset;
2434 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2435 }
2436
2437 /*
2438 * Make sure we got a minimum amount of free sectors per device
2439 */
2440 if (rs->data_offset &&
2441 to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) {
2442 rs->ti->error = data_offset ? "No space for forward reshape" :
2443 "No space for backward reshape";
2444 return -ENOSPC;
2445 }
2446out:
2447 /* Adjust data offsets on all rdevs */
2448 rdev_for_each(rdev, &rs->md) {
2449 rdev->data_offset = data_offset;
2450 rdev->new_data_offset = new_data_offset;
2451 }
2452
2453 return 0;
2454}
2455
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002456/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002457static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002458{
2459 int i = 0;
2460 struct md_rdev *rdev;
2461
2462 rdev_for_each(rdev, &rs->md) {
2463 rdev->raid_disk = i++;
2464 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2465 }
2466}
2467
2468/*
2469 * Setup @rs for takeover by a different raid level
2470 */
2471static int rs_setup_takeover(struct raid_set *rs)
2472{
2473 struct mddev *mddev = &rs->md;
2474 struct md_rdev *rdev;
2475 unsigned int d = mddev->raid_disks = rs->raid_disks;
2476 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2477
2478 if (rt_is_raid10(rs->raid_type)) {
2479 if (mddev->level == 0) {
2480 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002481 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002482
2483 /* raid0 -> raid10_far layout */
2484 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2485 rs->raid10_copies);
2486 } else if (mddev->level == 1)
2487 /* raid1 -> raid10_near layout */
2488 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2489 rs->raid_disks);
2490 else
2491 return -EINVAL;
2492
2493 }
2494
2495 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2496 mddev->recovery_cp = MaxSector;
2497
2498 while (d--) {
2499 rdev = &rs->dev[d].rdev;
2500
2501 if (test_bit(d, (void *) rs->rebuild_disks)) {
2502 clear_bit(In_sync, &rdev->flags);
2503 clear_bit(Faulty, &rdev->flags);
2504 mddev->recovery_cp = rdev->recovery_offset = 0;
2505 /* Bitmap has to be created when we do an "up" takeover */
2506 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2507 }
2508
2509 rdev->new_data_offset = new_data_offset;
2510 }
2511
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002512 return 0;
2513}
2514
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002515/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002516 *
2517 * - change raid layout
2518 * - change chunk size
2519 * - add disks
2520 * - remove disks
2521 */
2522static int rs_setup_reshape(struct raid_set *rs)
2523{
2524 int r = 0;
2525 unsigned int cur_raid_devs, d;
2526 struct mddev *mddev = &rs->md;
2527 struct md_rdev *rdev;
2528
2529 mddev->delta_disks = rs->delta_disks;
2530 cur_raid_devs = mddev->raid_disks;
2531
2532 /* Ignore impossible layout change whilst adding/removing disks */
2533 if (mddev->delta_disks &&
2534 mddev->layout != mddev->new_layout) {
2535 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
2536 mddev->new_layout = mddev->layout;
2537 }
2538
2539 /*
2540 * Adjust array size:
2541 *
2542 * - in case of adding disks, array size has
2543 * to grow after the disk adding reshape,
2544 * which'll hapen in the event handler;
2545 * reshape will happen forward, so space has to
2546 * be available at the beginning of each disk
2547 *
2548 * - in case of removing disks, array size
2549 * has to shrink before starting the reshape,
2550 * which'll happen here;
2551 * reshape will happen backward, so space has to
2552 * be available at the end of each disk
2553 *
2554 * - data_offset and new_data_offset are
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002555 * adjusted for aforementioned out of place
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002556 * reshaping based on userspace passing in
2557 * the "data_offset <sectors>" key/value
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002558 * pair via the constructor
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002559 */
2560
2561 /* Add disk(s) */
2562 if (rs->delta_disks > 0) {
2563 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
2564 for (d = cur_raid_devs; d < rs->raid_disks; d++) {
2565 rdev = &rs->dev[d].rdev;
2566 clear_bit(In_sync, &rdev->flags);
2567
2568 /*
2569 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
2570 * by md, which'll store that erroneously in the superblock on reshape
2571 */
2572 rdev->saved_raid_disk = -1;
2573 rdev->raid_disk = d;
2574
2575 rdev->sectors = mddev->dev_sectors;
2576 rdev->recovery_offset = MaxSector;
2577 }
2578
2579 mddev->reshape_backwards = 0; /* adding disks -> forward reshape */
2580
2581 /* Remove disk(s) */
2582 } else if (rs->delta_disks < 0) {
2583 r = rs_set_dev_and_array_sectors(rs, true);
2584 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
2585
2586 /* Change layout and/or chunk size */
2587 } else {
2588 /*
2589 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
2590 *
2591 * keeping number of disks and do layout change ->
2592 *
2593 * toggle reshape_backward depending on data_offset:
2594 *
2595 * - free space upfront -> reshape forward
2596 *
2597 * - free space at the end -> reshape backward
2598 *
2599 *
2600 * This utilizes free reshape space avoiding the need
2601 * for userspace to move (parts of) LV segments in
2602 * case of layout/chunksize change (for disk
2603 * adding/removing reshape space has to be at
2604 * the proper address (see above with delta_disks):
2605 *
2606 * add disk(s) -> begin
2607 * remove disk(s)-> end
2608 */
2609 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
2610 }
2611
2612 return r;
2613}
2614
2615/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002616 * Enable/disable discard support on RAID set depending on
2617 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002618 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002619static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002620{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002621 int i;
2622 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002623 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002624
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002625 /* Assume discards not supported until after checks below. */
2626 ti->discards_supported = false;
2627
2628 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002629 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002630
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002631 for (i = 0; i < rs->md.raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002632 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002633
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002634 if (!rs->dev[i].rdev.bdev)
2635 continue;
2636
2637 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002638 if (!q || !blk_queue_discard(q))
2639 return;
2640
2641 if (raid456) {
2642 if (!q->limits.discard_zeroes_data)
2643 return;
2644 if (!devices_handle_discard_safely) {
2645 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2646 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2647 return;
2648 }
2649 }
2650 }
2651
2652 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002653 ti->discards_supported = true;
2654
2655 /*
2656 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002657 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002658 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002659 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002660 ti->num_discard_bios = 1;
2661}
2662
2663/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002664 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002665 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002666 * <raid_type> <#raid_params> <raid_params>{0,} \
2667 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002668 *
Mike Snitzer43157842016-05-30 13:03:37 -04002669 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002670 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002671 *
2672 * Userspace is free to initialize the metadata devices, hence the superblocks to
2673 * enforce recreation based on the passed in table parameters.
2674 *
NeilBrown9d09e662011-01-13 20:00:02 +00002675 */
2676static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
2677{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002678 int r;
NeilBrown9d09e662011-01-13 20:00:02 +00002679 struct raid_type *rt;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002680 unsigned num_raid_params, num_raid_devs;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002681 sector_t calculated_dev_sectors;
NeilBrown9d09e662011-01-13 20:00:02 +00002682 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002683 const char *arg;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002684 struct rs_layout rs_layout;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002685 struct dm_arg_set as = { argc, argv }, as_nrd;
2686 struct dm_arg _args[] = {
2687 { 0, as.argc, "Cannot understand number of raid parameters" },
2688 { 1, 254, "Cannot understand number of raid devices parameters" }
2689 };
NeilBrown9d09e662011-01-13 20:00:02 +00002690
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002691 /* Must have <raid_type> */
2692 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002693 if (!arg) {
2694 ti->error = "No arguments";
2695 return -EINVAL;
2696 }
NeilBrown9d09e662011-01-13 20:00:02 +00002697
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002698 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002699 if (!rt) {
2700 ti->error = "Unrecognised raid_type";
2701 return -EINVAL;
2702 }
NeilBrown9d09e662011-01-13 20:00:02 +00002703
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002704 /* Must have <#raid_params> */
2705 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002706 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00002707
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002708 /* number of raid device tupples <meta_dev data_dev> */
2709 as_nrd = as;
2710 dm_consume_args(&as_nrd, num_raid_params);
2711 _args[1].max = (as_nrd.argc - 1) / 2;
2712 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002713 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002714
Mike Snitzerbb91a632016-06-02 12:06:54 -04002715 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002716 ti->error = "Invalid number of supplied raid devices";
2717 return -EINVAL;
2718 }
NeilBrown9d09e662011-01-13 20:00:02 +00002719
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002720 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00002721 if (IS_ERR(rs))
2722 return PTR_ERR(rs);
2723
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002724 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002725 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002726 goto bad;
2727
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02002728 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002729 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002730 goto bad;
2731
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002732 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002733
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002734 r = rs_set_dev_and_array_sectors(rs, false);
2735 if (r)
2736 return r;
2737
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002738 calculated_dev_sectors = rs->dev[0].rdev.sectors;
2739
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002740 /*
2741 * Backup any new raid set level, layout, ...
2742 * requested to be able to compare to superblock
2743 * members for conversion decisions.
2744 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002745 rs_config_backup(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002746
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002747 r = analyse_superblocks(ti, rs);
2748 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002749 goto bad;
2750
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002751 rs_setup_recovery(rs, calculated_dev_sectors);
2752
NeilBrown9d09e662011-01-13 20:00:02 +00002753 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00002754 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002755 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00002756
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002757 /* Restore any requested new layout for conversion decision */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002758 rs_config_restore(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002759
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002760 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
2761 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2762 rs_set_new(rs);
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02002763 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
2764 if (rs_is_raid6(rs) &&
2765 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
2766 ti->error = "'nosync' not allowed for new raid6 set";
2767 return -EINVAL;
2768 }
2769 rs_setup_recovery(rs, 0);
2770 } else if (rs_is_reshaping(rs)) {
2771 /* Have to reject size change request during reshape */
2772 if (calculated_dev_sectors != rs->dev[0].rdev.sectors) {
2773 ti->error = "Can't resize a reshaping raid set";
2774 return -EPERM;
2775 }
2776 /* skip setup rs */
2777 } else if (rs_takeover_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002778 if (rs_is_reshaping(rs)) {
2779 ti->error = "Can't takeover a reshaping raid set";
2780 return -EPERM;
2781 }
2782
2783 /*
2784 * If a takeover is needed, just set the level to
2785 * the new requested one and allow the raid set to run.
2786 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002787 r = rs_check_takeover(rs);
2788 if (r)
2789 return r;
2790
2791 r = rs_setup_takeover(rs);
2792 if (r)
2793 return r;
2794
Mike Snitzer42863252016-06-02 12:27:46 -04002795 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04002796 set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002797 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002798 } else if (rs_reshape_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002799 if (rs_is_reshaping(rs)) {
2800 ti->error = "raid set already reshaping!";
2801 return -EPERM;
2802 }
2803
2804 if (rs_is_raid10(rs)) {
2805 if (rs->raid_disks != rs->md.raid_disks &&
2806 __is_raid10_near(rs->md.layout) &&
2807 rs->raid10_copies &&
2808 rs->raid10_copies != __raid10_near_copies(rs->md.layout)) {
2809 /*
2810 * raid disk have to be multiple of data copies to allow this conversion,
2811 *
2812 * This is actually not a reshape it is a
2813 * rebuild of any additional mirrors per group
2814 */
2815 if (rs->raid_disks % rs->raid10_copies) {
2816 ti->error = "Can't reshape raid10 mirror groups";
2817 return -EINVAL;
2818 }
2819
2820 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
2821 __reorder_raid_disk_indexes(rs);
2822 rs->md.layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2823 rs->raid10_copies);
2824 rs->md.new_layout = rs->md.layout;
2825
2826 } else
2827 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2828
2829 } else if (rs_is_raid456(rs))
2830 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2831
2832 /*
2833 * HM FIXME: process raid1 via delta_disks as well?
2834 * Would cause allocations in raid1->check_reshape
2835 * though, thus more issues with potential failures
2836 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04002837 else if (rs_is_raid1(rs)) {
2838 set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002839 rs->md.raid_disks = rs->raid_disks;
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04002840 }
2841
2842 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
2843 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2844 set_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags);
2845 }
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002846
2847 if (rs->md.raid_disks < rs->raid_disks)
2848 set_bit(MD_ARRAY_FIRST_USE, &rs->md.flags);
2849
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002850 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002851 rs_setup_recovery(rs, MaxSector);
2852 } else {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002853 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002854 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
2855 0 : calculated_dev_sectors);
2856 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002857
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002858 /* If constructor requested it, change data and new_data offsets */
2859 r = rs_adjust_data_offsets(rs);
2860 if (r)
2861 return r;
2862
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002863 /* Start raid set read-only and assumed clean to change in raid_resume() */
2864 rs->md.ro = 1;
2865 rs->md.in_sync = 1;
2866 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002867
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002868 /* Has to be held on running the array */
2869 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002870 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002871 rs->md.in_sync = 0; /* Assume already marked dirty */
NeilBrown9d09e662011-01-13 20:00:02 +00002872
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002873 if (r) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002874 ti->error = "Failed to run raid array";
2875 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002876 goto bad;
2877 }
2878
2879 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00002880 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
2881
Jonathan Brassow32737272011-08-02 12:32:07 +01002882 mddev_suspend(&rs->md);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002883
2884 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
2885 if (rs_is_raid456(rs)) {
2886 r = rs_set_raid456_stripe_cache(rs);
2887 if (r)
2888 goto bad_stripe_cache;
2889 }
2890
2891 /* Now do an early reshape check */
2892 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
2893 r = rs_check_reshape(rs);
2894 if (r)
2895 return r;
2896
2897 /* Restore new, ctr requested layout to perform check */
2898 rs_config_restore(rs, &rs_layout);
2899
2900 r = rs->md.pers->check_reshape(&rs->md);
2901 if (r) {
2902 ti->error = "Reshape check failed";
2903 goto bad_check_reshape;
2904 }
2905 }
2906
2907 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002908 return 0;
2909
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002910bad_stripe_cache:
2911bad_check_reshape:
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002912 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002913bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002914 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002915
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002916 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002917}
2918
2919static void raid_dtr(struct dm_target *ti)
2920{
2921 struct raid_set *rs = ti->private;
2922
2923 list_del_init(&rs->callbacks.list);
2924 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002925 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002926}
2927
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002928static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00002929{
2930 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11002931 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00002932
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002933 /*
2934 * If we're reshaping to add disk(s)), ti->len and
2935 * mddev->array_sectors will differ during the process
2936 * (ti->len > mddev->array_sectors), so we have to requeue
2937 * bios with addresses > mddev->array_sectors here or
2938 * or there will occur accesses past EOD of the component
2939 * data images thus erroring the raid set.
2940 */
2941 if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
2942 return DM_MAPIO_REQUEUE;
2943
NeilBrown9d09e662011-01-13 20:00:02 +00002944 mddev->pers->make_request(mddev, bio);
2945
2946 return DM_MAPIO_SUBMITTED;
2947}
2948
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002949/* Return string describing the current sync action of @mddev */
Jonathan Brassowbe836512013-04-24 11:42:43 +10002950static const char *decipher_sync_action(struct mddev *mddev)
2951{
2952 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
2953 return "frozen";
2954
2955 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2956 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2957 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2958 return "reshape";
2959
2960 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2961 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2962 return "resync";
2963 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2964 return "check";
2965 return "repair";
2966 }
2967
2968 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
2969 return "recover";
2970 }
2971
2972 return "idle";
2973}
2974
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002975/*
2976 * Return status string @rdev
2977 *
2978 * Status characters:
2979 *
2980 * 'D' = Dead/Failed device
2981 * 'a' = Alive but not in-sync
2982 * 'A' = Alive and in-sync
2983 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002984static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002985{
2986 if (test_bit(Faulty, &rdev->flags))
2987 return "D";
2988 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags))
2989 return "a";
2990 else
2991 return "A";
2992}
2993
2994/* Helper to return resync/reshape progress for @rs and @array_in_sync */
2995static sector_t rs_get_progress(struct raid_set *rs,
2996 sector_t resync_max_sectors, bool *array_in_sync)
2997{
2998 sector_t r, recovery_cp, curr_resync_completed;
2999 struct mddev *mddev = &rs->md;
3000
3001 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp;
3002 recovery_cp = mddev->recovery_cp;
3003 *array_in_sync = false;
3004
3005 if (rs_is_raid0(rs)) {
3006 r = resync_max_sectors;
3007 *array_in_sync = true;
3008
3009 } else {
3010 r = mddev->reshape_position;
3011
3012 /* Reshape is relative to the array size */
3013 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
3014 r != MaxSector) {
3015 if (r == MaxSector) {
3016 *array_in_sync = true;
3017 r = resync_max_sectors;
3018 } else {
3019 /* Got to reverse on backward reshape */
3020 if (mddev->reshape_backwards)
3021 r = mddev->array_sectors - r;
3022
3023 /* Devide by # of data stripes */
3024 sector_div(r, mddev_data_stripes(rs));
3025 }
3026
3027 /* Sync is relative to the component device size */
3028 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3029 r = curr_resync_completed;
3030 else
3031 r = recovery_cp;
3032
3033 if (r == MaxSector) {
3034 /*
3035 * Sync complete.
3036 */
3037 *array_in_sync = true;
3038 r = resync_max_sectors;
3039 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
3040 /*
3041 * If "check" or "repair" is occurring, the raid set has
3042 * undergone an initial sync and the health characters
3043 * should not be 'a' anymore.
3044 */
3045 *array_in_sync = true;
3046 } else {
3047 struct md_rdev *rdev;
3048
3049 /*
3050 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04003051 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003052 * devices are In_sync, then it is the raid set that is
3053 * being initialized.
3054 */
3055 rdev_for_each(rdev, mddev)
3056 if (!test_bit(In_sync, &rdev->flags))
3057 *array_in_sync = true;
3058#if 0
3059 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */
3060#endif
3061 }
3062 }
3063
3064 return r;
3065}
3066
3067/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003068static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003069{
3070 return dev ? dev->name : "-";
3071}
3072
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003073static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003074 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00003075{
3076 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003077 struct mddev *mddev = &rs->md;
3078 struct r5conf *conf = mddev->private;
3079 int max_nr_stripes = conf ? conf->max_nr_stripes : 0;
3080 bool array_in_sync;
3081 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
3082 unsigned int sz = 0;
3083 unsigned int write_mostly_params = 0;
3084 sector_t progress, resync_max_sectors, resync_mismatches;
3085 const char *sync_action;
3086 struct raid_type *rt;
3087 struct md_rdev *rdev;
NeilBrown9d09e662011-01-13 20:00:02 +00003088
3089 switch (type) {
3090 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003091 /* *Should* always succeed */
3092 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
3093 if (!rt)
3094 return;
NeilBrown9d09e662011-01-13 20:00:02 +00003095
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003096 DMEMIT("%s %d ", rt->name, mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00003097
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003098 /* Access most recent mddev properties for status output */
3099 smp_rmb();
3100 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04003101 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003102 mddev->resync_max_sectors : mddev->dev_sectors;
3103 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync);
3104 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003105 atomic64_read(&mddev->resync_mismatches) : 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003106 sync_action = decipher_sync_action(&rs->md);
3107
3108 /* HM FIXME: do we want another state char for raid0? It shows 'D' or 'A' now */
3109 rdev_for_each(rdev, mddev)
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003110 DMEMIT(__raid_dev_status(rdev, array_in_sync));
Jonathan Brassowbe836512013-04-24 11:42:43 +10003111
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003112 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003113 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003114 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003115 * - Initializing the raid set
3116 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003117 * The user can distinguish between the two by referring
3118 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003119 *
3120 * The reshape ratio shows the progress of
3121 * changing the raid layout or the number of
3122 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003123 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003124 DMEMIT(" %llu/%llu", (unsigned long long) progress,
3125 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003126
Jonathan Brassowbe836512013-04-24 11:42:43 +10003127 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003128 * v1.5.0+:
3129 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003130 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003131 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10003132 * information on each of these states.
3133 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003134 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10003135
3136 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003137 * v1.5.0+:
3138 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003139 * resync_mismatches/mismatch_cnt
3140 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003141 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10003142 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003143 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
3144
3145 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003146 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003147 *
3148 * data_offset (needed for out of space reshaping)
3149 * This field shows the data offset into the data
3150 * image LV where the first stripes data starts.
3151 *
3152 * We keep data_offset equal on all raid disks of the set,
3153 * so retrieving it from the first raid disk is sufficient.
3154 */
3155 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
NeilBrown9d09e662011-01-13 20:00:02 +00003156 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003157
NeilBrown9d09e662011-01-13 20:00:02 +00003158 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003159 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00003160
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003161 /* Calculate raid parameter count */
3162 rdev_for_each(rdev, mddev)
3163 if (test_bit(WriteMostly, &rdev->flags))
3164 write_mostly_params += 2;
3165 raid_param_cnt += memweight(rs->rebuild_disks,
3166 DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks)) * 2 +
3167 write_mostly_params +
3168 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
3169 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2;
3170 /* Emit table line */
3171 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04003172 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003173 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003174 raid10_md_layout_to_format(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04003175 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003176 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003177 raid10_md_layout_to_copies(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04003178 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003179 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04003180 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003181 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04003182 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003183 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003184 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
Mike Snitzer42863252016-06-02 12:27:46 -04003185 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003186 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003187 (unsigned long long) rs->data_offset);
Mike Snitzer42863252016-06-02 12:27:46 -04003188 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003189 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003190 mddev->bitmap_info.daemon_sleep);
Mike Snitzer42863252016-06-02 12:27:46 -04003191 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003192 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003193 mddev->delta_disks);
Mike Snitzer42863252016-06-02 12:27:46 -04003194 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003195 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003196 max_nr_stripes);
3197 rdev_for_each(rdev, mddev)
3198 if (test_bit(rdev->raid_disk, (void *) rs->rebuild_disks))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003199 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003200 rdev->raid_disk);
3201 rdev_for_each(rdev, mddev)
3202 if (test_bit(WriteMostly, &rdev->flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003203 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003204 rdev->raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04003205 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003206 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003207 mddev->bitmap_info.max_write_behind);
Mike Snitzer42863252016-06-02 12:27:46 -04003208 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003209 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003210 mddev->sync_speed_max);
Mike Snitzer42863252016-06-02 12:27:46 -04003211 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003212 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003213 mddev->sync_speed_min);
3214 DMEMIT(" %d", rs->raid_disks);
3215 rdev_for_each(rdev, mddev) {
3216 struct raid_dev *rd = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003217
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003218 DMEMIT(" %s %s", __get_dev_name(rd->meta_dev),
3219 __get_dev_name(rd->data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00003220 }
3221 }
NeilBrown9d09e662011-01-13 20:00:02 +00003222}
3223
Jonathan Brassowbe836512013-04-24 11:42:43 +10003224static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
3225{
3226 struct raid_set *rs = ti->private;
3227 struct mddev *mddev = &rs->md;
3228
Jonathan Brassowbe836512013-04-24 11:42:43 +10003229 if (!mddev->pers || !mddev->pers->sync_request)
3230 return -EINVAL;
3231
3232 if (!strcasecmp(argv[0], "frozen"))
3233 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3234 else
3235 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3236
3237 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
3238 if (mddev->sync_thread) {
3239 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3240 md_reap_sync_thread(mddev);
3241 }
3242 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3243 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3244 return -EBUSY;
3245 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003246 ; /* MD_RECOVERY_NEEDED set below */
3247 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003248 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003249 else {
Jonathan Brassowbe836512013-04-24 11:42:43 +10003250 if (!strcasecmp(argv[0], "check"))
3251 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3252 else if (!!strcasecmp(argv[0], "repair"))
3253 return -EINVAL;
3254 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3255 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3256 }
3257 if (mddev->ro == 2) {
3258 /* A write to sync_action is enough to justify
3259 * canceling read-auto mode
3260 */
3261 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003262 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003263 md_wakeup_thread(mddev->sync_thread);
3264 }
3265 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003266 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003267 md_wakeup_thread(mddev->thread);
3268
3269 return 0;
3270}
3271
3272static int raid_iterate_devices(struct dm_target *ti,
3273 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00003274{
3275 struct raid_set *rs = ti->private;
3276 unsigned i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003277 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00003278
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003279 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00003280 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003281 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00003282 rs->dev[i].data_dev,
3283 0, /* No offset on data devs */
3284 rs->md.dev_sectors,
3285 data);
3286
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003287 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003288}
3289
3290static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
3291{
3292 struct raid_set *rs = ti->private;
3293 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11003294 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00003295
3296 blk_limits_io_min(limits, chunk_size);
3297 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
3298}
3299
3300static void raid_presuspend(struct dm_target *ti)
3301{
3302 struct raid_set *rs = ti->private;
3303
3304 md_stop_writes(&rs->md);
3305}
3306
3307static void raid_postsuspend(struct dm_target *ti)
3308{
3309 struct raid_set *rs = ti->private;
3310
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003311 if (test_and_clear_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
3312 if (!rs->md.suspended)
3313 mddev_suspend(&rs->md);
3314 rs->md.ro = 1;
3315 }
NeilBrown9d09e662011-01-13 20:00:02 +00003316}
3317
Jonathan Brassowf381e712013-05-08 17:57:13 -05003318static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00003319{
Jonathan Brassow9092c022013-05-02 14:19:24 -05003320 int i;
3321 uint64_t failed_devices, cleared_failed_devices = 0;
3322 unsigned long flags;
3323 struct dm_raid_superblock *sb;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003324 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00003325
Jonathan Brassowf381e712013-05-08 17:57:13 -05003326 for (i = 0; i < rs->md.raid_disks; i++) {
3327 r = &rs->dev[i].rdev;
3328 if (test_bit(Faulty, &r->flags) && r->sb_page &&
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02003329 sync_page_io(r, 0, r->sb_size, r->sb_page,
3330 REQ_OP_READ, 0, true)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003331 DMINFO("Faulty %s device #%d has readable super block."
3332 " Attempting to revive it.",
3333 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003334
3335 /*
3336 * Faulty bit may be set, but sometimes the array can
3337 * be suspended before the personalities can respond
3338 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04003339 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003340 * the failed device, its 'raid_disk' number will be
3341 * '>= 0' - meaning we must call this function
3342 * ourselves.
3343 */
3344 if ((r->raid_disk >= 0) &&
3345 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
3346 /* Failed to revive this device, try next */
3347 continue;
3348
Jonathan Brassowf381e712013-05-08 17:57:13 -05003349 r->raid_disk = i;
3350 r->saved_raid_disk = i;
3351 flags = r->flags;
3352 clear_bit(Faulty, &r->flags);
3353 clear_bit(WriteErrorSeen, &r->flags);
3354 clear_bit(In_sync, &r->flags);
3355 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
3356 r->raid_disk = -1;
3357 r->saved_raid_disk = -1;
3358 r->flags = flags;
3359 } else {
3360 r->recovery_offset = 0;
3361 cleared_failed_devices |= 1 << i;
3362 }
3363 }
3364 }
3365 if (cleared_failed_devices) {
3366 rdev_for_each(r, &rs->md) {
3367 sb = page_address(r->sb_page);
3368 failed_devices = le64_to_cpu(sb->failed_devices);
3369 failed_devices &= ~cleared_failed_devices;
3370 sb->failed_devices = cpu_to_le64(failed_devices);
3371 }
3372 }
3373}
3374
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003375static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003376{
3377 int r = 0;
3378
3379 /* Try loading the bitmap unless "raid0", which does not have one */
3380 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04003381 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003382 r = bitmap_load(&rs->md);
3383 if (r)
3384 DMERR("Failed to load bitmap");
3385 }
3386
3387 return r;
3388}
3389
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003390/* Enforce updating all superblocks */
3391static void rs_update_sbs(struct raid_set *rs)
3392{
3393 struct mddev *mddev = &rs->md;
3394 int ro = mddev->ro;
3395
3396 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3397 mddev->ro = 0;
3398 md_update_sb(mddev, 1);
3399 mddev->ro = ro;
3400}
3401
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003402/*
3403 * Reshape changes raid algorithm of @rs to new one within personality
3404 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
3405 * disks from a raid set thus growing/shrinking it or resizes the set
3406 *
3407 * Call mddev_lock_nointr() before!
3408 */
3409static int rs_start_reshape(struct raid_set *rs)
3410{
3411 int r;
3412 struct mddev *mddev = &rs->md;
3413 struct md_personality *pers = mddev->pers;
3414
3415 r = rs_setup_reshape(rs);
3416 if (r)
3417 return r;
3418
3419 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
3420 if (mddev->suspended)
3421 mddev_resume(mddev);
3422
3423 /*
3424 * Check any reshape constraints enforced by the personalility
3425 *
3426 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
3427 */
3428 r = pers->check_reshape(mddev);
3429 if (r) {
3430 rs->ti->error = "pers->check_reshape() failed";
3431 return r;
3432 }
3433
3434 /*
3435 * Personality may not provide start reshape method in which
3436 * case check_reshape above has already covered everything
3437 */
3438 if (pers->start_reshape) {
3439 r = pers->start_reshape(mddev);
3440 if (r) {
3441 rs->ti->error = "pers->start_reshape() failed";
3442 return r;
3443 }
3444 }
3445
3446 /* Suspend because a resume will happen in raid_resume() */
3447 if (!mddev->suspended)
3448 mddev_suspend(mddev);
3449
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003450 /*
3451 * Now reshape got set up, update superblocks to
3452 * reflect the fact so that a table reload will
3453 * access proper superblock content in the ctr.
3454 */
3455 rs_update_sbs(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003456
3457 return 0;
3458}
3459
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003460static int raid_preresume(struct dm_target *ti)
3461{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003462 int r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003463 struct raid_set *rs = ti->private;
3464 struct mddev *mddev = &rs->md;
3465
3466 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04003467 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003468 return 0;
3469
3470 /*
3471 * The superblocks need to be updated on disk if the
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003472 * array is new or new devices got added (thus zeroed
3473 * out by userspace) or __load_dirty_region_bitmap
3474 * will overwrite them in core with old data or fail.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003475 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003476 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
3477 rs_update_sbs(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003478
3479 /*
3480 * Disable/enable discard support on raid set after any
3481 * conversion, because devices can have been added
3482 */
3483 configure_discard_support(rs);
3484
3485 /* Load the bitmap from disk unless raid0 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003486 r = __load_dirty_region_bitmap(rs);
3487 if (r)
3488 return r;
3489
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003490 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
3491 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) &&
3492 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
3493 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
3494 to_bytes(rs->requested_bitmap_chunk_sectors), 0);
3495 if (r)
3496 DMERR("Failed to resize bitmap");
3497 }
3498
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003499 /* Check for any resize/reshape on @rs and adjust/initiate */
3500 /* Be prepared for mddev_resume() in raid_resume() */
3501 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3502 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
3503 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3504 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3505 mddev->resync_min = mddev->recovery_cp;
3506 }
3507
3508 rs_set_capacity(rs);
3509
3510 /* Check for any reshape request and region size change unless new raid set */
3511 if (test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3512 /* Initiate a reshape. */
3513 mddev_lock_nointr(mddev);
3514 r = rs_start_reshape(rs);
3515 mddev_unlock(mddev);
3516 if (r)
3517 DMWARN("Failed to check/start reshape, continuing without change");
3518 r = 0;
3519 }
3520
3521 return r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003522}
3523
Jonathan Brassowf381e712013-05-08 17:57:13 -05003524static void raid_resume(struct dm_target *ti)
3525{
3526 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003527 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003528
Mike Snitzer42863252016-06-02 12:27:46 -04003529 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003530 /*
3531 * A secondary resume while the device is active.
3532 * Take this opportunity to check whether any failed
3533 * devices are reachable again.
3534 */
3535 attempt_restore_of_faulty_devices(rs);
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003536 } else {
3537 mddev->ro = 0;
3538 mddev->in_sync = 0;
3539
3540 /*
3541 * When passing in flags to the ctr, we expect userspace
3542 * to reset them because they made it to the superblocks
3543 * and reload the mapping anyway.
3544 *
3545 * -> only unfreeze recovery in case of a table reload or
3546 * we'll have a bogus recovery/reshape position
3547 * retrieved from the superblock by the ctr because
3548 * the ongoing recovery/reshape will change it after read.
3549 */
3550 if (!test_bit(RT_FLAG_KEEP_RS_FROZEN, &rs->runtime_flags))
3551 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3552
3553 if (mddev->suspended)
3554 mddev_resume(mddev);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003555 }
NeilBrown9d09e662011-01-13 20:00:02 +00003556}
3557
3558static struct target_type raid_target = {
3559 .name = "raid",
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003560 .version = {1, 9, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00003561 .module = THIS_MODULE,
3562 .ctr = raid_ctr,
3563 .dtr = raid_dtr,
3564 .map = raid_map,
3565 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10003566 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00003567 .iterate_devices = raid_iterate_devices,
3568 .io_hints = raid_io_hints,
3569 .presuspend = raid_presuspend,
3570 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003571 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00003572 .resume = raid_resume,
3573};
3574
3575static int __init dm_raid_init(void)
3576{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11003577 DMINFO("Loading target version %u.%u.%u",
3578 raid_target.version[0],
3579 raid_target.version[1],
3580 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00003581 return dm_register_target(&raid_target);
3582}
3583
3584static void __exit dm_raid_exit(void)
3585{
3586 dm_unregister_target(&raid_target);
3587}
3588
3589module_init(dm_raid_init);
3590module_exit(dm_raid_exit);
3591
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02003592module_param(devices_handle_discard_safely, bool, 0644);
3593MODULE_PARM_DESC(devices_handle_discard_safely,
3594 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
3595
Mike Snitzeref9b85a2016-06-02 12:02:19 -04003596MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
3597MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003598MODULE_ALIAS("dm-raid1");
3599MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00003600MODULE_ALIAS("dm-raid4");
3601MODULE_ALIAS("dm-raid5");
3602MODULE_ALIAS("dm-raid6");
3603MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003604MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00003605MODULE_LICENSE("GPL");