blob: a32cddbb5f4fa1d7e8ce71fee2b31c5c3c6414f4 [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 Mauelshagen48cf06b2014-09-24 17:47:19 +020022static bool devices_handle_discard_safely = false;
23
NeilBrown9d09e662011-01-13 20:00:02 +000024/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010025 * The following flags are used by dm-raid.c to set up the array state.
26 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000027 */
Mike Snitzer43157842016-05-30 13:03:37 -040028#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000029
30struct raid_dev {
31 /*
32 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040033 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000034 * ti->len and give more flexibility in altering size and
35 * characteristics.
36 *
37 * While it is possible for this device to be associated
38 * with a different physical device than the data_dev, it
39 * is intended for it to be the same.
40 * |--------- Physical Device ---------|
41 * |- meta_dev -|------ data_dev ------|
42 */
43 struct dm_dev *meta_dev;
44 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110045 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000046};
47
48/*
Mike Snitzer42863252016-06-02 12:27:46 -040049 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020050 *
51 * 1 = no flag value
52 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000053 */
Mike Snitzer42863252016-06-02 12:27:46 -040054#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
55#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
56#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
57#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
58#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
59#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
60#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
61#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
62#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
63#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
64#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
65#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
Mike Snitzer9b6e5422016-06-02 11:48:09 -040066/* New for v1.9.0 */
Mike Snitzer42863252016-06-02 12:27:46 -040067#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid4/5/6/10! */
68#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
69#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
70
71/*
72 * Flags for rs->ctr_flags field.
73 */
74#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
75#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
76#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
77#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
78#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
79#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
80#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
81#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
82#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
83#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
84#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
85#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
86#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
87#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
88#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050089
Heinz Mauelshagenf0902792016-05-19 18:49:27 +020090/*
91 * Definitions of various constructor flags to
92 * be used in checks of valid / invalid flags
93 * per raid level.
94 */
95/* Define all any sync flags */
96#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
97
98/* Define flags for options without argument (e.g. 'nosync') */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +020099#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
100 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200101
102/* Define flags for options with one argument (e.g. 'delta_disks +2') */
103#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
104 CTR_FLAG_WRITE_MOSTLY | \
105 CTR_FLAG_DAEMON_SLEEP | \
106 CTR_FLAG_MIN_RECOVERY_RATE | \
107 CTR_FLAG_MAX_RECOVERY_RATE | \
108 CTR_FLAG_MAX_WRITE_BEHIND | \
109 CTR_FLAG_STRIPE_CACHE | \
110 CTR_FLAG_REGION_SIZE | \
111 CTR_FLAG_RAID10_COPIES | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200112 CTR_FLAG_RAID10_FORMAT | \
113 CTR_FLAG_DELTA_DISKS | \
114 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200115
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200116/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200117
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200118/* "raid0" does only accept data offset */
119#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200120
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200121/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
122#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
123 CTR_FLAG_REBUILD | \
124 CTR_FLAG_WRITE_MOSTLY | \
125 CTR_FLAG_DAEMON_SLEEP | \
126 CTR_FLAG_MIN_RECOVERY_RATE | \
127 CTR_FLAG_MAX_RECOVERY_RATE | \
128 CTR_FLAG_MAX_WRITE_BEHIND | \
129 CTR_FLAG_REGION_SIZE | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200130 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200131
132/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200133#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
134 CTR_FLAG_REBUILD | \
135 CTR_FLAG_DAEMON_SLEEP | \
136 CTR_FLAG_MIN_RECOVERY_RATE | \
137 CTR_FLAG_MAX_RECOVERY_RATE | \
138 CTR_FLAG_REGION_SIZE | \
139 CTR_FLAG_RAID10_COPIES | \
140 CTR_FLAG_RAID10_FORMAT | \
141 CTR_FLAG_DELTA_DISKS | \
142 CTR_FLAG_DATA_OFFSET | \
143 CTR_FLAG_RAID10_USE_NEAR_SETS)
144
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200145/*
146 * "raid4/5/6" do not accept any raid1 or raid10 specific options
147 *
148 * "raid6" does not accept "nosync", because it is not guaranteed
149 * that both parity and q-syndrome are being written properly with
150 * any writes
151 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200152#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
153 CTR_FLAG_REBUILD | \
154 CTR_FLAG_DAEMON_SLEEP | \
155 CTR_FLAG_MIN_RECOVERY_RATE | \
156 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200157 CTR_FLAG_MAX_WRITE_BEHIND | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200158 CTR_FLAG_STRIPE_CACHE | \
159 CTR_FLAG_REGION_SIZE | \
160 CTR_FLAG_DELTA_DISKS | \
161 CTR_FLAG_DATA_OFFSET)
162
163#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
164 CTR_FLAG_REBUILD | \
165 CTR_FLAG_DAEMON_SLEEP | \
166 CTR_FLAG_MIN_RECOVERY_RATE | \
167 CTR_FLAG_MAX_RECOVERY_RATE | \
168 CTR_FLAG_MAX_WRITE_BEHIND | \
169 CTR_FLAG_STRIPE_CACHE | \
170 CTR_FLAG_REGION_SIZE | \
171 CTR_FLAG_DELTA_DISKS | \
172 CTR_FLAG_DATA_OFFSET)
173/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200174
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200175/*
176 * Flags for rs->runtime_flags field
177 * (RT_FLAG prefix meaning "runtime flag")
178 *
179 * These are all internal and used to define runtime state,
180 * e.g. to prevent another resume from preresume processing
181 * the raid set all over again.
182 */
183#define RT_FLAG_RS_PRERESUMED 0x1
184#define RT_FLAG_RS_RESUMED 0x2
185#define RT_FLAG_RS_BITMAP_LOADED 0x4
186#define RT_FLAG_UPDATE_SBS 0x8
187
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200188/* Array elements of 64 bit needed for rebuild/write_mostly bits */
189#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
190
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200191/*
192 * raid set level, layout and chunk sectors backup/restore
193 */
194struct rs_layout {
195 int new_level;
196 int new_layout;
197 int new_chunk_sectors;
198};
199
NeilBrown9d09e662011-01-13 20:00:02 +0000200struct raid_set {
201 struct dm_target *ti;
202
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -0600203 uint32_t bitmap_loaded;
Mike Snitzer42863252016-06-02 12:27:46 -0400204 unsigned long ctr_flags;
205 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200206
207 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000208
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200209 int raid_disks;
210 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200211 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200212 int raid10_copies;
213
NeilBrownfd01b882011-10-11 16:47:53 +1100214 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000215 struct raid_type *raid_type;
216 struct dm_target_callbacks callbacks;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200217 struct rs_layout rs_layout;
NeilBrown9d09e662011-01-13 20:00:02 +0000218
219 struct raid_dev dev[0];
220};
221
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400222static void rs_config_backup(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200223{
224 struct mddev *mddev = &rs->md;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400225 struct rs_layout *l = &rs->rs_layout;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200226
227 l->new_level = mddev->new_level;
228 l->new_layout = mddev->new_layout;
229 l->new_chunk_sectors = mddev->new_chunk_sectors;
230}
231
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400232static void rs_config_restore(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200233{
234 struct mddev *mddev = &rs->md;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400235 struct rs_layout *l = &rs->rs_layout;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200236
237 mddev->new_level = l->new_level;
238 mddev->new_layout = l->new_layout;
239 mddev->new_chunk_sectors = l->new_chunk_sectors;
240}
241
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200242/* raid10 algorithms (i.e. formats) */
243#define ALGORITHM_RAID10_DEFAULT 0
244#define ALGORITHM_RAID10_NEAR 1
245#define ALGORITHM_RAID10_OFFSET 2
246#define ALGORITHM_RAID10_FAR 3
247
NeilBrown9d09e662011-01-13 20:00:02 +0000248/* Supported raid types and properties. */
249static struct raid_type {
250 const char *name; /* RAID algorithm. */
251 const char *descr; /* Descriptor text for logging. */
252 const unsigned parity_devs; /* # of parity devices. */
253 const unsigned minimal_devs; /* minimal # of devices in set. */
254 const unsigned level; /* RAID level. */
255 const unsigned algorithm; /* RAID algorithm. */
256} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400257 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
258 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
259 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200260 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400261 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
262 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
263 {"raid4", "raid4 (dedicated last parity disk)", 1, 2, 4, ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */
264 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
265 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
266 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
267 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
268 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
269 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
270 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
271 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
272 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
273 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
274 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
275 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
276 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000277};
278
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200279/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400280static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200281{
282 return v >= min && v <= max;
283}
284
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200285/* All table line arguments are defined here */
286static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400287 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200288 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400289} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200290 { CTR_FLAG_SYNC, "sync"},
291 { CTR_FLAG_NOSYNC, "nosync"},
292 { CTR_FLAG_REBUILD, "rebuild"},
293 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
294 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
295 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
296 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
297 { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
298 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
299 { CTR_FLAG_REGION_SIZE, "region_size"},
300 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
301 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200302 { CTR_FLAG_DATA_OFFSET, "data_offset"},
303 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
304 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200305};
306
307/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400308static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200309{
310 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400311 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200312
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400313 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400314 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200315 return anf->name;
316
317 } else
318 DMERR("%s called with more than one flag!", __func__);
319
320 return NULL;
321}
322
323/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200324 * bool helpers to test for various raid levels of a raid set,
325 * is. it's level as reported by the superblock rather than
326 * the requested raid_type passed to the constructor.
327 */
328/* Return true, if raid set in @rs is raid0 */
329static bool rs_is_raid0(struct raid_set *rs)
330{
331 return !rs->md.level;
332}
333
334/* Return true, if raid set in @rs is raid10 */
335static bool rs_is_raid10(struct raid_set *rs)
336{
337 return rs->md.level == 10;
338}
339
340/*
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200341 * bool helpers to test for various raid levels of a raid type
342 */
343
344/* Return true, if raid type in @rt is raid0 */
345static bool rt_is_raid0(struct raid_type *rt)
346{
347 return !rt->level;
348}
349
350/* Return true, if raid type in @rt is raid1 */
351static bool rt_is_raid1(struct raid_type *rt)
352{
353 return rt->level == 1;
354}
355
356/* Return true, if raid type in @rt is raid10 */
357static bool rt_is_raid10(struct raid_type *rt)
358{
359 return rt->level == 10;
360}
361
362/* Return true, if raid type in @rt is raid4/5 */
363static bool rt_is_raid45(struct raid_type *rt)
364{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400365 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200366}
367
368/* Return true, if raid type in @rt is raid6 */
369static bool rt_is_raid6(struct raid_type *rt)
370{
371 return rt->level == 6;
372}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200373
374/* Return true, if raid type in @rt is raid4/5/6 */
375static bool rt_is_raid456(struct raid_type *rt)
376{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400377 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200378}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200379/* END: raid level bools */
380
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200381/* Return valid ctr flags for the raid level of @rs */
382static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200383{
384 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200385 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200386 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200387 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200388 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200389 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200390 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200391 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200392 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200393 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200394
395 return ~0;
396}
397
398/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200399 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200400 *
401 * Has to be called after parsing of the ctr flags!
402 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200403static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200404{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200405 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400406 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400407 return -EINVAL;
408 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200409
410 return 0;
411}
412
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200413/* MD raid10 bit definitions and helpers */
414#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
415#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
416#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
417#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
418
419/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400420static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200421{
422 return layout & 0xFF;
423}
424
425/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400426static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200427{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400428 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200429}
430
431/* Return true if md raid10 offset for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400432static unsigned int __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200433{
434 return layout & RAID10_OFFSET;
435}
436
437/* Return true if md raid10 near for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400438static unsigned int __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200439{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400440 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200441}
442
443/* Return true if md raid10 far for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400444static unsigned int __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200445{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400446 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200447}
448
449/* Return md raid10 layout string for @layout */
450static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100451{
452 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200453 * Bit 16 stands for "offset"
454 * (i.e. adjacent stripes hold copies)
455 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100456 * Refer to MD's raid10.c for details
457 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400458 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100459 return "offset";
460
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400461 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100462 return "near";
463
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400464 WARN_ON(__raid10_far_copies(layout) < 2);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200465
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100466 return "far";
467}
468
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200469/* Return md raid10 algorithm for @name */
470static const int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500471{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200472 if (!strcasecmp(name, "near"))
473 return ALGORITHM_RAID10_NEAR;
474 else if (!strcasecmp(name, "offset"))
475 return ALGORITHM_RAID10_OFFSET;
476 else if (!strcasecmp(name, "far"))
477 return ALGORITHM_RAID10_FAR;
478
479 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500480}
481
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200482/* Return md raid10 copies for @layout */
483static unsigned int raid10_md_layout_to_copies(int layout)
484{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400485 return __raid10_near_copies(layout) > 1 ?
486 __raid10_near_copies(layout) : __raid10_far_copies(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200487}
488
489/* Return md raid10 format id for @format string */
490static int raid10_format_to_md_layout(struct raid_set *rs,
491 unsigned int algorithm,
492 unsigned int copies)
493{
494 unsigned int n = 1, f = 1, r = 0;
495
496 /*
497 * MD resilienece flaw:
498 *
499 * enabling use_far_sets for far/offset formats causes copies
500 * to be colocated on the same devs together with their origins!
501 *
502 * -> disable it for now in the definition above
503 */
504 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
505 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100506 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200507
508 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100509 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200510 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400511 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200512 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100513
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200514 } else if (algorithm == ALGORITHM_RAID10_FAR) {
515 f = copies;
516 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400517 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200518 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100519
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200520 } else
521 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100522
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200523 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
524}
525/* END: MD raid10 bit definitions and helpers */
526
527/* Check for any of the raid10 algorithms */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400528static int __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200529{
530 if (rtp->level == 10) {
531 switch (rtp->algorithm) {
532 case ALGORITHM_RAID10_DEFAULT:
533 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400534 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200535 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400536 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200537 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400538 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200539 default:
540 break;
541 }
542 }
543
544 return 0;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500545}
546
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200547/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200548static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000549{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200550 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000551
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200552 while (rtp-- > raid_types)
553 if (!strcasecmp(rtp->name, name))
554 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000555
556 return NULL;
557}
558
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200559/* Return raid_type for @name based derived from @level and @layout */
560static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
561{
562 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
563
564 while (rtp-- > raid_types) {
565 /* RAID10 special checks based on @layout flags/properties */
566 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400567 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200568 return rtp;
569 }
570
571 return NULL;
572}
573
574/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200575 * Set the mddev properties in @rs to the current
576 * ones retrieved from the freshest superblock
577 */
578static void rs_set_cur(struct raid_set *rs)
579{
580 struct mddev *mddev = &rs->md;
581
582 mddev->new_level = mddev->level;
583 mddev->new_layout = mddev->layout;
584 mddev->new_chunk_sectors = mddev->chunk_sectors;
585}
586
587/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200588 * Set the mddev properties in @rs to the new
589 * ones requested by the ctr
590 */
591static void rs_set_new(struct raid_set *rs)
592{
593 struct mddev *mddev = &rs->md;
594
595 mddev->level = mddev->new_level;
596 mddev->layout = mddev->new_layout;
597 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200598 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200599 mddev->delta_disks = 0;
600}
601
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400602static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
603 unsigned raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000604{
605 unsigned i;
606 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000607
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400608 if (raid_devs <= raid_type->parity_devs) {
609 ti->error = "Insufficient number of devices";
610 return ERR_PTR(-EINVAL);
611 }
NeilBrown9d09e662011-01-13 20:00:02 +0000612
NeilBrown9d09e662011-01-13 20:00:02 +0000613 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400614 if (!rs) {
615 ti->error = "Cannot allocate raid context";
616 return ERR_PTR(-ENOMEM);
617 }
NeilBrown9d09e662011-01-13 20:00:02 +0000618
619 mddev_init(&rs->md);
620
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200621 rs->raid_disks = raid_devs;
622 rs->delta_disks = 0;
623
NeilBrown9d09e662011-01-13 20:00:02 +0000624 rs->ti = ti;
625 rs->raid_type = raid_type;
626 rs->md.raid_disks = raid_devs;
627 rs->md.level = raid_type->level;
628 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000629 rs->md.layout = raid_type->algorithm;
630 rs->md.new_layout = rs->md.layout;
631 rs->md.delta_disks = 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200632 rs->md.recovery_cp = rs_is_raid0(rs) ? MaxSector : 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000633
634 for (i = 0; i < raid_devs; i++)
635 md_rdev_init(&rs->dev[i].rdev);
636
637 /*
638 * Remaining items to be initialized by further RAID params:
639 * rs->md.persistent
640 * rs->md.external
641 * rs->md.chunk_sectors
642 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100643 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000644 */
645
646 return rs;
647}
648
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400649static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000650{
651 int i;
652
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100653 for (i = 0; i < rs->md.raid_disks; i++) {
654 if (rs->dev[i].meta_dev)
655 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000656 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000657 if (rs->dev[i].data_dev)
658 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100659 }
NeilBrown9d09e662011-01-13 20:00:02 +0000660
661 kfree(rs);
662}
663
664/*
665 * For every device we have two words
666 * <meta_dev>: meta device name or '-' if missing
667 * <data_dev>: data device name or '-' if missing
668 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100669 * The following are permitted:
670 * - -
671 * - <data_dev>
672 * <meta_dev> <data_dev>
673 *
674 * The following is not allowed:
675 * <meta_dev> -
676 *
677 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400678 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000679 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200680static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000681{
682 int i;
683 int rebuild = 0;
684 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200685 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200686 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000687
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200688 /* Put off the number of raid devices argument to get to dev pairs */
689 arg = dm_shift_arg(as);
690 if (!arg)
691 return -EINVAL;
692
693 for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000694 rs->dev[i].rdev.raid_disk = i;
695
696 rs->dev[i].meta_dev = NULL;
697 rs->dev[i].data_dev = NULL;
698
699 /*
700 * There are no offsets, since there is a separate device
701 * for data and metadata.
702 */
703 rs->dev[i].rdev.data_offset = 0;
704 rs->dev[i].rdev.mddev = &rs->md;
705
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200706 arg = dm_shift_arg(as);
707 if (!arg)
708 return -EINVAL;
709
710 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400711 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
712 &rs->dev[i].meta_dev);
713 if (r) {
714 rs->ti->error = "RAID metadata device lookup failure";
715 return r;
716 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100717
718 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400719 if (!rs->dev[i].rdev.sb_page) {
720 rs->ti->error = "Failed to allocate superblock page";
721 return -ENOMEM;
722 }
NeilBrown9d09e662011-01-13 20:00:02 +0000723 }
724
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200725 arg = dm_shift_arg(as);
726 if (!arg)
727 return -EINVAL;
728
729 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000730 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400731 (!rs->dev[i].rdev.recovery_offset)) {
732 rs->ti->error = "Drive designated for rebuild not specified";
733 return -EINVAL;
734 }
NeilBrown9d09e662011-01-13 20:00:02 +0000735
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400736 if (rs->dev[i].meta_dev) {
737 rs->ti->error = "No data device supplied with metadata device";
738 return -EINVAL;
739 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100740
NeilBrown9d09e662011-01-13 20:00:02 +0000741 continue;
742 }
743
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400744 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
745 &rs->dev[i].data_dev);
746 if (r) {
747 rs->ti->error = "RAID device lookup failure";
748 return r;
749 }
NeilBrown9d09e662011-01-13 20:00:02 +0000750
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100751 if (rs->dev[i].meta_dev) {
752 metadata_available = 1;
753 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
754 }
NeilBrown9d09e662011-01-13 20:00:02 +0000755 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200756 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000757 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
758 rebuild++;
759 }
760
761 if (metadata_available) {
762 rs->md.external = 0;
763 rs->md.persistent = 1;
764 rs->md.major_version = 2;
765 } else if (rebuild && !rs->md.recovery_cp) {
766 /*
767 * Without metadata, we will not be able to tell if the array
768 * is in-sync or not - we must assume it is not. Therefore,
769 * it is impossible to rebuild a drive.
770 *
771 * Even if there is metadata, the on-disk information may
772 * indicate that the array is not in-sync and it will then
773 * fail at that time.
774 *
775 * User could specify 'nosync' option if desperate.
776 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400777 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
778 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000779 }
780
781 return 0;
782}
783
784/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100785 * validate_region_size
786 * @rs
787 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
788 *
789 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
790 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
791 *
792 * Returns: 0 on success, -EINVAL on failure.
793 */
794static int validate_region_size(struct raid_set *rs, unsigned long region_size)
795{
796 unsigned long min_region_size = rs->ti->len / (1 << 21);
797
798 if (!region_size) {
799 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400800 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100801 */
802 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000803 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400804 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100805 DMINFO("Choosing default region size of %lu sectors",
806 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100807 } else {
808 DMINFO("Choosing default region size of 4MiB");
809 region_size = 1 << 13; /* sectors */
810 }
811 } else {
812 /*
813 * Validate user-supplied value.
814 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400815 if (region_size > rs->ti->len) {
816 rs->ti->error = "Supplied region size is too large";
817 return -EINVAL;
818 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100819
820 if (region_size < min_region_size) {
821 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
822 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400823 rs->ti->error = "Supplied region size is too small";
824 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100825 }
826
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400827 if (!is_power_of_2(region_size)) {
828 rs->ti->error = "Region size is not a power of 2";
829 return -EINVAL;
830 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100831
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400832 if (region_size < rs->md.chunk_sectors) {
833 rs->ti->error = "Region size is smaller than the chunk size";
834 return -EINVAL;
835 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100836 }
837
838 /*
839 * Convert sectors to bytes.
840 */
841 rs->md.bitmap_info.chunksize = (region_size << 9);
842
843 return 0;
844}
845
846/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600847 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100848 * @rs
849 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600850 * Determine if there are enough devices in the array that haven't
851 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100852 *
853 * Returns: 0 on success, -EINVAL on failure.
854 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600855static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100856{
857 unsigned i, rebuild_cnt = 0;
NeilBrown3f6bbd32013-05-09 10:27:49 +1000858 unsigned rebuilds_per_group = 0, copies, d;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100859 unsigned group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100860
Jonathan Brassoweb649122012-10-11 13:40:09 +1100861 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600862 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
863 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100864 rebuild_cnt++;
865
866 switch (rs->raid_type->level) {
867 case 1:
868 if (rebuild_cnt >= rs->md.raid_disks)
869 goto too_many;
870 break;
871 case 4:
872 case 5:
873 case 6:
874 if (rebuild_cnt > rs->raid_type->parity_devs)
875 goto too_many;
876 break;
877 case 10:
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100878 copies = raid10_md_layout_to_copies(rs->md.layout);
879 if (rebuild_cnt < copies)
880 break;
881
882 /*
883 * It is possible to have a higher rebuild count for RAID10,
884 * as long as the failed devices occur in different mirror
885 * groups (i.e. different stripes).
886 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100887 * When checking "near" format, make sure no adjacent devices
888 * have failed beyond what can be handled. In addition to the
889 * simple case where the number of devices is a multiple of the
890 * number of copies, we must also handle cases where the number
891 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -0400892 * E.g. dev1 dev2 dev3 dev4 dev5
893 * A A B B C
894 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100895 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100896 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
897 for (i = 0; i < rs->md.raid_disks * copies; i++) {
898 if (!(i % copies))
899 rebuilds_per_group = 0;
900 d = i % rs->md.raid_disks;
901 if ((!rs->dev[d].rdev.sb_page ||
902 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
903 (++rebuilds_per_group >= copies))
904 goto too_many;
905 }
906 break;
907 }
908
909 /*
910 * When checking "far" and "offset" formats, we need to ensure
911 * that the device that holds its copy is not also dead or
912 * being rebuilt. (Note that "far" and "offset" formats only
913 * support two copies right now. These formats also only ever
914 * use the 'use_far_sets' variant.)
915 *
916 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -0400917 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100918 * results in the need to treat the last (potentially larger)
919 * set differently.
920 */
921 group_size = (rs->md.raid_disks / copies);
922 last_group_start = (rs->md.raid_disks / group_size) - 1;
923 last_group_start *= group_size;
924 for (i = 0; i < rs->md.raid_disks; i++) {
925 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600926 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100927 if ((!rs->dev[i].rdev.sb_page ||
928 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100929 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100930 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100931 }
932 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100933 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600934 if (rebuild_cnt)
935 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100936 }
937
938 return 0;
939
940too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +1100941 return -EINVAL;
942}
943
944/*
NeilBrown9d09e662011-01-13 20:00:02 +0000945 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +0000946 * <chunk_size> [optional_args]
947 *
Jonathan Brassow32737272011-08-02 12:32:07 +0100948 * Argument definitions
949 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -0400950 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +0100951 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -0400952 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +0000953 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +0100954 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -0400955 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +0000956 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
957 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100958 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000959 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
960 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -0400961 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500962 *
963 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -0400964 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100965 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +0000966 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200967static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown9d09e662011-01-13 20:00:02 +0000968 unsigned num_raid_params)
969{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200970 int raid10_format = ALGORITHM_RAID10_DEFAULT;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500971 unsigned raid10_copies = 2;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100972 unsigned i;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200973 unsigned value, region_size = 0;
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100974 sector_t sectors_per_dev = rs->ti->len;
Mike Snitzer542f9032012-07-27 15:08:00 +0100975 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200976 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200977 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200978 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200979
980 arg = dm_shift_arg(as);
981 num_raid_params--; /* Account for chunk_size argument */
982
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400983 if (kstrtouint(arg, 10, &value) < 0) {
984 rs->ti->error = "Bad numerical argument given for chunk_size";
985 return -EINVAL;
986 }
NeilBrown9d09e662011-01-13 20:00:02 +0000987
988 /*
989 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +0100990 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +0000991 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200992 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +0100993 if (value)
994 DMERR("Ignoring chunk size parameter for RAID 1");
995 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400996 } else if (!is_power_of_2(value)) {
997 rs->ti->error = "Chunk size must be a power of 2";
998 return -EINVAL;
999 } else if (value < 8) {
1000 rs->ti->error = "Chunk size value is too small";
1001 return -EINVAL;
1002 }
NeilBrown9d09e662011-01-13 20:00:02 +00001003
1004 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001005
1006 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001007 * We set each individual device as In_sync with a completed
1008 * 'recovery_offset'. If there has been a device failure or
1009 * replacement then one of the following cases applies:
1010 *
1011 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001012 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001013 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001014 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001015 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001016 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001017 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001018 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001019 *
1020 * What is found in the superblocks of the devices is always
1021 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1022 */
1023 for (i = 0; i < rs->md.raid_disks; i++) {
1024 set_bit(In_sync, &rs->dev[i].rdev.flags);
1025 rs->dev[i].rdev.recovery_offset = MaxSector;
1026 }
1027
1028 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001029 * Second, parse the unordered optional arguments
1030 */
NeilBrown9d09e662011-01-13 20:00:02 +00001031 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001032 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001033 if (!key) {
1034 rs->ti->error = "Not enough raid parameters given";
1035 return -EINVAL;
1036 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001037
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001038 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001039 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001040 rs->ti->error = "Only one 'nosync' argument allowed";
1041 return -EINVAL;
1042 }
NeilBrown9d09e662011-01-13 20:00:02 +00001043 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +00001044 continue;
1045 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001046 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001047 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001048 rs->ti->error = "Only one 'sync' argument allowed";
1049 return -EINVAL;
1050 }
NeilBrown9d09e662011-01-13 20:00:02 +00001051 rs->md.recovery_cp = 0;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001052 continue;
1053 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001054 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001055 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001056 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1057 return -EINVAL;
1058 }
NeilBrown9d09e662011-01-13 20:00:02 +00001059 continue;
1060 }
1061
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001062 arg = dm_shift_arg(as);
1063 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001064 if (!arg) {
1065 rs->ti->error = "Wrong number of raid parameters given";
1066 return -EINVAL;
1067 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001068
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001069 /*
1070 * Parameters that take a string value are checked here.
1071 */
1072
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001073 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001074 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001075 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1076 return -EINVAL;
1077 }
1078 if (!rt_is_raid10(rt)) {
1079 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1080 return -EINVAL;
1081 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001082 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001083 if (raid10_format < 0) {
1084 rs->ti->error = "Invalid 'raid10_format' value given";
1085 return raid10_format;
1086 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001087 continue;
1088 }
1089
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001090 if (kstrtouint(arg, 10, &value) < 0) {
1091 rs->ti->error = "Bad numerical argument given in raid params";
1092 return -EINVAL;
1093 }
NeilBrown9d09e662011-01-13 20:00:02 +00001094
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001095 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001096 /*
1097 * "rebuild" is being passed in by userspace to provide
1098 * indexes of replaced devices and to set up additional
1099 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001100 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001101 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001102 rs->ti->error = "Invalid rebuild index given";
1103 return -EINVAL;
1104 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001105
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001106 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1107 rs->ti->error = "rebuild for this index already given";
1108 return -EINVAL;
1109 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001110
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001111 rd = rs->dev + value;
1112 clear_bit(In_sync, &rd->rdev.flags);
1113 clear_bit(Faulty, &rd->rdev.flags);
1114 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001115 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001116 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001117 if (!rt_is_raid1(rt)) {
1118 rs->ti->error = "write_mostly option is only valid for RAID1";
1119 return -EINVAL;
1120 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001121
Mike Snitzerbb91a632016-06-02 12:06:54 -04001122 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001123 rs->ti->error = "Invalid write_mostly index given";
1124 return -EINVAL;
1125 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001126
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001127 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001128 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001129 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001130 if (!rt_is_raid1(rt)) {
1131 rs->ti->error = "max_write_behind option is only valid for RAID1";
1132 return -EINVAL;
1133 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001134
Mike Snitzer42863252016-06-02 12:27:46 -04001135 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001136 rs->ti->error = "Only one max_write_behind argument pair allowed";
1137 return -EINVAL;
1138 }
NeilBrown9d09e662011-01-13 20:00:02 +00001139
1140 /*
1141 * In device-mapper, we specify things in sectors, but
1142 * MD records this value in kB
1143 */
1144 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001145 if (value > COUNTER_MAX) {
1146 rs->ti->error = "Max write-behind limit out of range";
1147 return -EINVAL;
1148 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001149
NeilBrown9d09e662011-01-13 20:00:02 +00001150 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001151 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001152 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001153 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1154 return -EINVAL;
1155 }
1156 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1157 rs->ti->error = "daemon sleep period out of range";
1158 return -EINVAL;
1159 }
NeilBrown9d09e662011-01-13 20:00:02 +00001160 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001161 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001162 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001163 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001164 rs->ti->error = "Only one data_offset argument pair allowed";
1165 return -EINVAL;
1166 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001167 /* Ensure sensible data offset */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001168 if (value < 0) {
1169 rs->ti->error = "Bogus data_offset value";
1170 return -EINVAL;
1171 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001172 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001173 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001174 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001175 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001176 rs->ti->error = "Only one delta_disks argument pair allowed";
1177 return -EINVAL;
1178 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001179 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001180 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001181 rs->ti->error = "Too many delta_disk requested";
1182 return -EINVAL;
1183 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001184
1185 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001186 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001187 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001188 rs->ti->error = "Only one stripe_cache argument pair allowed";
1189 return -EINVAL;
1190 }
1191
NeilBrown9d09e662011-01-13 20:00:02 +00001192 /*
1193 * In device-mapper, we specify things in sectors, but
1194 * MD records this value in kB
1195 */
1196 value /= 2;
1197
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001198 if (!rt_is_raid456(rt)) {
1199 rs->ti->error = "Inappropriate argument: stripe_cache";
1200 return -EINVAL;
1201 }
1202 if (raid5_set_cache_size(&rs->md, (int)value)) {
1203 rs->ti->error = "Bad stripe_cache size";
1204 return -EINVAL;
1205 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001206
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001207 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001208 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001209 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1210 return -EINVAL;
1211 }
1212 if (value > INT_MAX) {
1213 rs->ti->error = "min_recovery_rate out of range";
1214 return -EINVAL;
1215 }
NeilBrown9d09e662011-01-13 20:00:02 +00001216 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001217 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001218 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001219 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1220 return -EINVAL;
1221 }
1222 if (value > INT_MAX) {
1223 rs->ti->error = "max_recovery_rate out of range";
1224 return -EINVAL;
1225 }
NeilBrown9d09e662011-01-13 20:00:02 +00001226 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001227 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001228 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001229 rs->ti->error = "Only one region_size argument pair allowed";
1230 return -EINVAL;
1231 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001232
Jonathan Brassowc1084562011-08-02 12:32:07 +01001233 region_size = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001234 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001235 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001236 rs->ti->error = "Only one raid10_copies argument pair allowed";
1237 return -EINVAL;
1238 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001239
Mike Snitzerbb91a632016-06-02 12:06:54 -04001240 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001241 rs->ti->error = "Bad value for 'raid10_copies'";
1242 return -EINVAL;
1243 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001244
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001245 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001246 } else {
1247 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001248 rs->ti->error = "Unable to parse RAID parameter";
1249 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001250 }
1251 }
1252
Jonathan Brassowc1084562011-08-02 12:32:07 +01001253 if (validate_region_size(rs, region_size))
1254 return -EINVAL;
1255
1256 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001257 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001258 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001259 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001260
Mike Snitzer542f9032012-07-27 15:08:00 +01001261 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1262 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001263
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001264 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001265 if (raid10_copies > rs->md.raid_disks) {
1266 rs->ti->error = "Not enough devices to satisfy specification";
1267 return -EINVAL;
1268 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001269
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001270 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001271 if (rs->md.new_layout < 0) {
1272 rs->ti->error = "Error getting raid10 format";
1273 return rs->md.new_layout;
1274 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001275
1276 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001277 if (!rt) {
1278 rs->ti->error = "Failed to recognize new raid10 layout";
1279 return -EINVAL;
1280 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001281
1282 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1283 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001284 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001285 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1286 return -EINVAL;
1287 }
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001288
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001289 /* (Len * #mirrors) / #devices */
1290 sectors_per_dev = rs->ti->len * raid10_copies;
1291 sector_div(sectors_per_dev, rs->md.raid_disks);
1292
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001293 rs->md.layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001294 rs->md.new_layout = rs->md.layout;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001295 } else if (!rt_is_raid1(rt) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001296 sector_div(sectors_per_dev, (rs->md.raid_disks - rt->parity_devs))) {
1297 rs->ti->error = "Target length not divisible by number of data devices";
1298 return -EINVAL;
1299 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001300
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001301 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001302 rs->md.dev_sectors = sectors_per_dev;
1303
NeilBrown9d09e662011-01-13 20:00:02 +00001304 /* Assume there are no metadata devices until the drives are parsed */
1305 rs->md.persistent = 0;
1306 rs->md.external = 1;
1307
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001308 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001309 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001310}
1311
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001312/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1313static unsigned int mddev_data_stripes(struct raid_set *rs)
1314{
1315 return rs->md.raid_disks - rs->raid_type->parity_devs;
1316}
1317
NeilBrown9d09e662011-01-13 20:00:02 +00001318static void do_table_event(struct work_struct *ws)
1319{
1320 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1321
1322 dm_table_event(rs->ti->table);
1323}
1324
1325static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1326{
1327 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1328
NeilBrown5c675f82014-12-15 12:56:56 +11001329 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001330}
1331
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001332/*
1333 * Make sure a valid takover (level switch) is being requested on @rs
1334 *
1335 * Conversions of raid sets from one MD personality to another
1336 * have to conform to restrictions which are enforced here.
1337 *
1338 * Degration is already checked for in rs_check_conversion() below.
1339 */
1340static int rs_check_takeover(struct raid_set *rs)
1341{
1342 struct mddev *mddev = &rs->md;
1343 unsigned int near_copies;
1344
1345 switch (mddev->level) {
1346 case 0:
1347 /* raid0 -> raid1/5 with one disk */
1348 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1349 mddev->raid_disks == 1)
1350 return 0;
1351
1352 /* raid0 -> raid10 */
1353 if (mddev->new_level == 10 &&
1354 !(rs->raid_disks % 2))
1355 return 0;
1356
1357 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001358 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001359 mddev->new_layout == ALGORITHM_PARITY_N &&
1360 mddev->raid_disks > 1)
1361 return 0;
1362
1363 break;
1364
1365 case 10:
1366 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001367 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001368 break;
1369
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001370 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001371
1372 /* raid10* -> raid0 */
1373 if (mddev->new_level == 0) {
1374 /* Can takeover raid10_near with raid disks divisable by data copies! */
1375 if (near_copies > 1 &&
1376 !(mddev->raid_disks % near_copies)) {
1377 mddev->raid_disks /= near_copies;
1378 mddev->delta_disks = mddev->raid_disks;
1379 return 0;
1380 }
1381
1382 /* Can takeover raid10_far */
1383 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001384 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001385 return 0;
1386
1387 break;
1388 }
1389
1390 /* raid10_{near,far} -> raid1 */
1391 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001392 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001393 return 0;
1394
1395 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001396 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001397 mddev->raid_disks == 2)
1398 return 0;
1399 break;
1400
1401 case 1:
1402 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001403 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001404 mddev->raid_disks == 2) {
1405 mddev->degraded = 1;
1406 return 0;
1407 }
1408
1409 /* raid1 -> raid0 */
1410 if (mddev->new_level == 0 &&
1411 mddev->raid_disks == 1)
1412 return 0;
1413
1414 /* raid1 -> raid10 */
1415 if (mddev->new_level == 10)
1416 return 0;
1417
1418 break;
1419
1420 case 4:
1421 /* raid4 -> raid0 */
1422 if (mddev->new_level == 0)
1423 return 0;
1424
1425 /* raid4 -> raid1/5 with 2 disks */
1426 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1427 mddev->raid_disks == 2)
1428 return 0;
1429
1430 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001431 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001432 mddev->layout == ALGORITHM_PARITY_N)
1433 return 0;
1434 break;
1435
1436 case 5:
1437 /* raid5 with parity N -> raid0 */
1438 if (mddev->new_level == 0 &&
1439 mddev->layout == ALGORITHM_PARITY_N)
1440 return 0;
1441
1442 /* raid5 with parity N -> raid4 */
1443 if (mddev->new_level == 4 &&
1444 mddev->layout == ALGORITHM_PARITY_N)
1445 return 0;
1446
1447 /* raid5 with 2 disks -> raid1/4/10 */
1448 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1449 mddev->raid_disks == 2)
1450 return 0;
1451
1452 /* raid5 with parity N -> raid6 with parity N */
1453 if (mddev->new_level == 6 &&
1454 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001455 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001456 return 0;
1457 break;
1458
1459 case 6:
1460 /* raid6 with parity N -> raid0 */
1461 if (mddev->new_level == 0 &&
1462 mddev->layout == ALGORITHM_PARITY_N)
1463 return 0;
1464
1465 /* raid6 with parity N -> raid4 */
1466 if (mddev->new_level == 4 &&
1467 mddev->layout == ALGORITHM_PARITY_N)
1468 return 0;
1469
1470 /* raid6_*_n with parity N -> raid5_* */
1471 if (mddev->new_level == 5 &&
1472 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001473 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001474 return 0;
1475
1476 default:
1477 break;
1478 }
1479
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001480 rs->ti->error = "takeover not possible";
1481 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001482}
1483
1484/* True if @rs requested to be taken over */
1485static bool rs_takeover_requested(struct raid_set *rs)
1486{
1487 return rs->md.new_level != rs->md.level;
1488}
1489
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001490/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001491#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001492
1493/* State flags for sb->flags */
1494#define SB_FLAG_RESHAPE_ACTIVE 0x1
1495#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1496
NeilBrown9d09e662011-01-13 20:00:02 +00001497/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001498 * This structure is never routinely used by userspace, unlike md superblocks.
1499 * Devices with this superblock should only ever be accessed via device-mapper.
1500 */
1501#define DM_RAID_MAGIC 0x64526D44
1502struct dm_raid_superblock {
1503 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001504 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001505
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001506 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1507 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001508
1509 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001510 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001511 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001512
1513 /*
1514 * This offset tracks the progress of the repair or replacement of
1515 * an individual drive.
1516 */
1517 __le64 disk_recovery_offset;
1518
1519 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001520 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001521 * synchronisation/parity calculation.
1522 */
1523 __le64 array_resync_offset;
1524
1525 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001526 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001527 */
1528 __le32 level;
1529 __le32 layout;
1530 __le32 stripe_sectors;
1531
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001532 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001533 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001534 *
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001535 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001536 */
1537
1538 __le32 flags; /* Flags defining array states for reshaping */
1539
1540 /*
1541 * This offset tracks the progress of a raid
1542 * set reshape in order to be able to restart it
1543 */
1544 __le64 reshape_position;
1545
1546 /*
1547 * These define the properties of the array in case of an interrupted reshape
1548 */
1549 __le32 new_level;
1550 __le32 new_layout;
1551 __le32 new_stripe_sectors;
1552 __le32 delta_disks;
1553
1554 __le64 array_sectors; /* Array size in sectors */
1555
1556 /*
1557 * Sector offsets to data on devices (reshaping).
1558 * Needed to support out of place reshaping, thus
1559 * not writing over any stripes whilst converting
1560 * them from old to new layout
1561 */
1562 __le64 data_offset;
1563 __le64 new_data_offset;
1564
1565 __le64 sectors; /* Used device size in sectors */
1566
1567 /*
1568 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001569 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001570 */
1571 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1572
1573 __le32 incompat_features; /* Used to indicate any incompatible features */
1574
1575 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001576} __packed;
1577
NeilBrown3cb03002011-10-11 16:45:26 +11001578static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001579{
1580 BUG_ON(!rdev->sb_page);
1581
1582 if (rdev->sb_loaded)
1583 return 0;
1584
Mike Christie796a5cf2016-06-05 14:32:07 -05001585 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01001586 DMERR("Failed to read superblock of device at position %d",
1587 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10001588 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001589 return -EINVAL;
1590 }
1591
1592 rdev->sb_loaded = 1;
1593
1594 return 0;
1595}
1596
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001597static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1598{
1599 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1600 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1601
Mike Snitzer42863252016-06-02 12:27:46 -04001602 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001603 int i = ARRAY_SIZE(sb->extended_failed_devices);
1604
1605 while (i--)
1606 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1607 }
1608}
1609
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001610static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1611{
1612 int i = ARRAY_SIZE(sb->extended_failed_devices);
1613
1614 sb->failed_devices = cpu_to_le64(failed_devices[0]);
1615 while (i--)
1616 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1617}
1618
1619/*
1620 * Synchronize the superblock members with the raid set properties
1621 *
1622 * All superblock data is little endian.
1623 */
NeilBrownfd01b882011-10-11 16:47:53 +11001624static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001625{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001626 bool update_failed_devices = false;
1627 unsigned int i;
1628 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001629 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001630 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001631
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001632 /* No metadata device, no superblock */
1633 if (!rdev->meta_bdev)
1634 return;
1635
1636 BUG_ON(!rdev->sb_page);
1637
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001638 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001639
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001640 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001641
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001642 for (i = 0; i < rs->raid_disks; i++)
1643 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
1644 update_failed_devices = true;
1645 set_bit(i, (void *) failed_devices);
1646 }
1647
1648 if (update_failed_devices)
1649 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001650
1651 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001652 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001653
1654 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1655 sb->array_position = cpu_to_le32(rdev->raid_disk);
1656
1657 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001658
1659 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1660 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1661
1662 sb->level = cpu_to_le32(mddev->level);
1663 sb->layout = cpu_to_le32(mddev->layout);
1664 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001665
1666 sb->new_level = cpu_to_le32(mddev->new_level);
1667 sb->new_layout = cpu_to_le32(mddev->new_layout);
1668 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
1669
1670 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1671
1672 smp_rmb(); /* Make sure we access most recent reshape position */
1673 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1674 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
1675 /* Flag ongoing reshape */
1676 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
1677
1678 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1679 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04001680 } else {
1681 /* Clear reshape flags */
1682 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
1683 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02001684
1685 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
1686 sb->data_offset = cpu_to_le64(rdev->data_offset);
1687 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
1688 sb->sectors = cpu_to_le64(rdev->sectors);
1689
1690 /* Zero out the rest of the payload after the size of the superblock */
1691 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001692}
1693
1694/*
1695 * super_load
1696 *
1697 * This function creates a superblock if one is not found on the device
1698 * and will decide which superblock to use if there's a choice.
1699 *
1700 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1701 */
NeilBrown3cb03002011-10-11 16:45:26 +11001702static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001703{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001704 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001705 struct dm_raid_superblock *sb;
1706 struct dm_raid_superblock *refsb;
1707 uint64_t events_sb, events_refsb;
1708
1709 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02001710 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1711 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1712 DMERR("superblock size of a logical block is no longer valid");
1713 return -EINVAL;
1714 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001715
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02001716 r = read_disk_sb(rdev, rdev->sb_size);
1717 if (r)
1718 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001719
1720 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00001721
1722 /*
1723 * Two cases that we want to write new superblocks and rebuild:
1724 * 1) New device (no matching magic number)
1725 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1726 */
1727 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1728 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001729 super_sync(rdev->mddev, rdev);
1730
1731 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001732 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001733
1734 /* Force writing of superblocks to disk */
1735 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1736
1737 /* Any superblock is better than none, choose that if given */
1738 return refdev ? 0 : 1;
1739 }
1740
1741 if (!refdev)
1742 return 1;
1743
1744 events_sb = le64_to_cpu(sb->events);
1745
1746 refsb = page_address(refdev->sb_page);
1747 events_refsb = le64_to_cpu(refsb->events);
1748
1749 return (events_sb > events_refsb) ? 1 : 0;
1750}
1751
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001752static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001753{
1754 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001755 unsigned int d;
1756 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001757 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001758 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001759 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001760 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001761 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001762 struct dm_raid_superblock *sb2;
1763
1764 sb = page_address(rdev->sb_page);
1765 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001766
1767 /*
1768 * Initialise to 1 if this is a new superblock.
1769 */
1770 mddev->events = events_sb ? : 1;
1771
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001772 mddev->reshape_position = MaxSector;
1773
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001774 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001775 * Reshaping is supported, e.g. reshape_position is valid
1776 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001777 */
Mike Snitzer42863252016-06-02 12:27:46 -04001778 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001779 /* Superblock is authoritative wrt given raid set layout! */
1780 mddev->raid_disks = le32_to_cpu(sb->num_devices);
1781 mddev->level = le32_to_cpu(sb->level);
1782 mddev->layout = le32_to_cpu(sb->layout);
1783 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
1784 mddev->new_level = le32_to_cpu(sb->new_level);
1785 mddev->new_layout = le32_to_cpu(sb->new_layout);
1786 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
1787 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1788 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
1789
1790 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04001791 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
1792 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001793 DMERR("Reshape requested but raid set is still reshaping");
1794 return -EINVAL;
1795 }
1796
1797 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04001798 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001799 mddev->reshape_backwards = 1;
1800 else
1801 mddev->reshape_backwards = 0;
1802
1803 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1804 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
1805 }
1806
1807 } else {
1808 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001809 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001810 */
1811 if (le32_to_cpu(sb->level) != mddev->level) {
1812 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
1813 return -EINVAL;
1814 }
1815 if (le32_to_cpu(sb->layout) != mddev->layout) {
1816 DMERR("Reshaping raid sets not yet supported. (raid layout change)");
Mike Snitzer43157842016-05-30 13:03:37 -04001817 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
1818 DMERR(" Old layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001819 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
1820 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
Mike Snitzer43157842016-05-30 13:03:37 -04001821 DMERR(" New layout: %s w/ %d copies",
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001822 raid10_md_layout_to_format(mddev->layout),
1823 raid10_md_layout_to_copies(mddev->layout));
1824 return -EINVAL;
1825 }
1826 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1827 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
1828 return -EINVAL;
1829 }
1830
1831 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
1832 if (!rt_is_raid1(rs->raid_type) &&
1833 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
1834 DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
1835 sb->num_devices, mddev->raid_disks);
1836 return -EINVAL;
1837 }
1838
1839 /* Table line is checked vs. authoritative superblock */
1840 rs_set_new(rs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001841 }
1842
Mike Snitzer42863252016-06-02 12:27:46 -04001843 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001844 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
1845
1846 /*
1847 * During load, we set FirstUse if a new superblock was written.
1848 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001849 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04001850 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001851 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001852 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001853 * and the new device needs to be rebuilt - in which
1854 * case the In_sync bit will /not/ be set and
1855 * recovery_cp must be MaxSector.
1856 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001857 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001858 rdev_for_each(r, mddev) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001859 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001860 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001861
1862 if (!test_bit(In_sync, &r->flags)) {
1863 DMINFO("Device %d specified for rebuild; clearing superblock",
1864 r->raid_disk);
1865 rebuilds++;
1866
1867 if (test_bit(FirstUse, &r->flags))
1868 rebuild_and_new++;
1869 }
1870
1871 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001872 }
1873
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001874 if (new_devs == rs->raid_disks || !rebuilds) {
1875 /* Replace a broken device */
1876 if (new_devs == 1 && !rs->delta_disks)
1877 ;
1878 if (new_devs == rs->raid_disks) {
1879 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001880 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001881 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001882 mddev->recovery_cp = 0;
1883 } else if (new_devs && new_devs != rs->raid_disks && !rebuilds) {
1884 DMERR("New device injected into existing raid set without "
1885 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001886 return -EINVAL;
1887 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001888 } else if (new_devs && new_devs != rebuilds) {
1889 DMERR("%u 'rebuild' devices cannot be injected into"
1890 " a raid set with %u other first-time devices",
1891 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001892 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001893 } else if (rebuilds) {
1894 if (rebuild_and_new && rebuilds != rebuild_and_new) {
1895 DMERR("new device%s provided without 'rebuild'",
1896 new_devs > 1 ? "s" : "");
1897 return -EINVAL;
1898 } else if (mddev->recovery_cp != MaxSector) {
1899 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
1900 (unsigned long long) mddev->recovery_cp);
1901 return -EINVAL;
1902 } else if (mddev->reshape_position != MaxSector) {
1903 DMERR("'rebuild' specified while raid set is being reshaped");
1904 return -EINVAL;
1905 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001906 }
1907
1908 /*
1909 * Now we set the Faulty bit for those devices that are
1910 * recorded in the superblock as failed.
1911 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001912 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11001913 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001914 if (!r->sb_page)
1915 continue;
1916 sb2 = page_address(r->sb_page);
1917 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001918 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001919
1920 /*
1921 * Check for any device re-ordering.
1922 */
1923 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1924 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001925 if (role < 0)
1926 continue;
1927
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001928 if (role != r->raid_disk) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001929 if (__is_raid10_near(mddev->layout)) {
1930 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001931 rs->raid_disks % rs->raid10_copies) {
1932 rs->ti->error =
1933 "Cannot change raid10 near set to odd # of devices!";
1934 return -EINVAL;
1935 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001936
1937 sb2->array_position = cpu_to_le32(r->raid_disk);
1938
1939 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001940 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
1941 !rt_is_raid1(rs->raid_type)) {
1942 rs->ti->error = "Cannot change device positions in raid set";
1943 return -EINVAL;
1944 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001945
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001946 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001947 }
1948
1949 /*
1950 * Partial recovery is performed on
1951 * returning failed devices.
1952 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001953 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001954 set_bit(Faulty, &r->flags);
1955 }
1956 }
1957
1958 return 0;
1959}
1960
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001961static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001962{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001963 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001964 struct dm_raid_superblock *sb;
1965
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001966 if (rs_is_raid0(rs) || !rdev->sb_page)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001967 return 0;
1968
1969 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001970
1971 /*
1972 * If mddev->events is not set, we know we have not yet initialized
1973 * the array.
1974 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001975 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001976 return -EINVAL;
1977
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001978 if (le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
1979 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
1980 return -EINVAL;
1981 }
1982
1983 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001984 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02001985 return -EINVAL;
1986 }
1987
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001988 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02001989 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02001990 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
1991
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001992 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
1993 /* Retrieve device size stored in superblock to be prepared for shrink */
1994 rdev->sectors = le64_to_cpu(sb->sectors);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001995 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001996 if (rdev->recovery_offset == MaxSector)
1997 set_bit(In_sync, &rdev->flags);
1998 /*
1999 * If no reshape in progress -> we're recovering single
2000 * disk(s) and have to set the device(s) to out-of-sync
2001 */
2002 else if (rs->md.reshape_position == MaxSector)
2003 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002004 }
2005
2006 /*
2007 * If a device comes back, set it as not In_sync and no longer faulty.
2008 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002009 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2010 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002011 clear_bit(In_sync, &rdev->flags);
2012 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002013 }
2014
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002015 /* Reshape support -> restore repective data offsets */
2016 rdev->data_offset = le64_to_cpu(sb->data_offset);
2017 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002018
2019 return 0;
2020}
2021
2022/*
2023 * Analyse superblocks and select the freshest.
2024 */
2025static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2026{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002027 int r;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002028 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002029 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002030 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002031
2032 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10002033 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11002034 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002035 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002036 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002037 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002038 * of the "sync" directive.
2039 *
2040 * When reshaping capability is added, we must ensure
2041 * that the "sync" directive is disallowed during the
2042 * reshape.
2043 */
Mike Snitzer42863252016-06-02 12:27:46 -04002044 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002045 continue;
2046
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002047 if (!rdev->meta_bdev)
2048 continue;
2049
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002050 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002051
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002052 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002053 case 1:
2054 freshest = rdev;
2055 break;
2056 case 0:
2057 break;
2058 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01002059 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002060 if (dev->meta_dev)
2061 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002062
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002063 dev->meta_dev = NULL;
2064 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002065
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002066 if (rdev->sb_page)
2067 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002068
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002069 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002070
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002071 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002072
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002073 /*
2074 * We might be able to salvage the data device
2075 * even though the meta device has failed. For
2076 * now, we behave as though '- -' had been
2077 * set for this device in the table.
2078 */
2079 if (dev->data_dev)
2080 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01002081
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002082 dev->data_dev = NULL;
2083 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01002084
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002085 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002086 }
2087 }
2088
2089 if (!freshest)
2090 return 0;
2091
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002092 if (validate_raid_redundancy(rs)) {
2093 rs->ti->error = "Insufficient redundancy to activate array";
2094 return -EINVAL;
2095 }
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002096
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002097 /*
2098 * Validation of the freshest device provides the source of
2099 * validation for the remaining devices.
2100 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002101 if (super_validate(rs, freshest)) {
2102 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2103 return -EINVAL;
2104 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002105
NeilBrowndafb20f2012-03-19 12:46:39 +11002106 rdev_for_each(rdev, mddev)
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002107 if ((rdev != freshest) && super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002108 return -EINVAL;
2109
2110 return 0;
2111}
2112
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002113/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002114static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002115{
2116 int i = 0;
2117 struct md_rdev *rdev;
2118
2119 rdev_for_each(rdev, &rs->md) {
2120 rdev->raid_disk = i++;
2121 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2122 }
2123}
2124
2125/*
2126 * Setup @rs for takeover by a different raid level
2127 */
2128static int rs_setup_takeover(struct raid_set *rs)
2129{
2130 struct mddev *mddev = &rs->md;
2131 struct md_rdev *rdev;
2132 unsigned int d = mddev->raid_disks = rs->raid_disks;
2133 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2134
2135 if (rt_is_raid10(rs->raid_type)) {
2136 if (mddev->level == 0) {
2137 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002138 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002139
2140 /* raid0 -> raid10_far layout */
2141 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2142 rs->raid10_copies);
2143 } else if (mddev->level == 1)
2144 /* raid1 -> raid10_near layout */
2145 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2146 rs->raid_disks);
2147 else
2148 return -EINVAL;
2149
2150 }
2151
2152 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2153 mddev->recovery_cp = MaxSector;
2154
2155 while (d--) {
2156 rdev = &rs->dev[d].rdev;
2157
2158 if (test_bit(d, (void *) rs->rebuild_disks)) {
2159 clear_bit(In_sync, &rdev->flags);
2160 clear_bit(Faulty, &rdev->flags);
2161 mddev->recovery_cp = rdev->recovery_offset = 0;
2162 /* Bitmap has to be created when we do an "up" takeover */
2163 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2164 }
2165
2166 rdev->new_data_offset = new_data_offset;
2167 }
2168
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002169 return 0;
2170}
2171
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002172/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002173 * Enable/disable discard support on RAID set depending on
2174 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002175 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002176static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002177{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002178 int i;
2179 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002180 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002181
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002182 /* Assume discards not supported until after checks below. */
2183 ti->discards_supported = false;
2184
2185 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002186 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002187
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002188 for (i = 0; i < rs->md.raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002189 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002190
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002191 if (!rs->dev[i].rdev.bdev)
2192 continue;
2193
2194 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002195 if (!q || !blk_queue_discard(q))
2196 return;
2197
2198 if (raid456) {
2199 if (!q->limits.discard_zeroes_data)
2200 return;
2201 if (!devices_handle_discard_safely) {
2202 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2203 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2204 return;
2205 }
2206 }
2207 }
2208
2209 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002210 ti->discards_supported = true;
2211
2212 /*
2213 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002214 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002215 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002216 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002217 ti->num_discard_bios = 1;
2218}
2219
2220/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002221 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002222 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002223 * <raid_type> <#raid_params> <raid_params>{0,} \
2224 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002225 *
Mike Snitzer43157842016-05-30 13:03:37 -04002226 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002227 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002228 *
2229 * Userspace is free to initialize the metadata devices, hence the superblocks to
2230 * enforce recreation based on the passed in table parameters.
2231 *
NeilBrown9d09e662011-01-13 20:00:02 +00002232 */
2233static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
2234{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002235 int r;
NeilBrown9d09e662011-01-13 20:00:02 +00002236 struct raid_type *rt;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002237 unsigned num_raid_params, num_raid_devs;
NeilBrown9d09e662011-01-13 20:00:02 +00002238 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002239 const char *arg;
2240 struct dm_arg_set as = { argc, argv }, as_nrd;
2241 struct dm_arg _args[] = {
2242 { 0, as.argc, "Cannot understand number of raid parameters" },
2243 { 1, 254, "Cannot understand number of raid devices parameters" }
2244 };
NeilBrown9d09e662011-01-13 20:00:02 +00002245
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002246 /* Must have <raid_type> */
2247 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002248 if (!arg) {
2249 ti->error = "No arguments";
2250 return -EINVAL;
2251 }
NeilBrown9d09e662011-01-13 20:00:02 +00002252
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002253 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002254 if (!rt) {
2255 ti->error = "Unrecognised raid_type";
2256 return -EINVAL;
2257 }
NeilBrown9d09e662011-01-13 20:00:02 +00002258
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002259 /* Must have <#raid_params> */
2260 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002261 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00002262
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002263 /* number of raid device tupples <meta_dev data_dev> */
2264 as_nrd = as;
2265 dm_consume_args(&as_nrd, num_raid_params);
2266 _args[1].max = (as_nrd.argc - 1) / 2;
2267 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002268 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002269
Mike Snitzerbb91a632016-06-02 12:06:54 -04002270 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002271 ti->error = "Invalid number of supplied raid devices";
2272 return -EINVAL;
2273 }
NeilBrown9d09e662011-01-13 20:00:02 +00002274
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002275 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00002276 if (IS_ERR(rs))
2277 return PTR_ERR(rs);
2278
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002279 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002280 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002281 goto bad;
2282
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02002283 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002284 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002285 goto bad;
2286
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002287 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002288
2289 /*
2290 * Backup any new raid set level, layout, ...
2291 * requested to be able to compare to superblock
2292 * members for conversion decisions.
2293 */
2294 rs_config_backup(rs);
2295
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002296 r = analyse_superblocks(ti, rs);
2297 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002298 goto bad;
2299
NeilBrown9d09e662011-01-13 20:00:02 +00002300 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00002301 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002302 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00002303
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002304 /* Restore any requested new layout for conversion decision */
2305 rs_config_restore(rs);
2306
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002307 /*
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002308 * If a takeover is needed, just set the level to
2309 * the new requested one and allow the raid set to run.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002310 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002311 if (rs_takeover_requested(rs)) {
2312 r = rs_check_takeover(rs);
2313 if (r)
2314 return r;
2315
2316 r = rs_setup_takeover(rs);
2317 if (r)
2318 return r;
2319
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002320 /* Tell preresume to update superblocks with new layout */
Mike Snitzer42863252016-06-02 12:27:46 -04002321 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002322 rs_set_new(rs);
2323 } else
2324 rs_set_cur(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002325
2326 /* Start raid set read-only and assumed clean to change in raid_resume() */
2327 rs->md.ro = 1;
2328 rs->md.in_sync = 1;
2329 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002330
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002331 /* Has to be held on running the array */
2332 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002333 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002334 rs->md.in_sync = 0; /* Assume already marked dirty */
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002335 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002336
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002337 if (r) {
NeilBrown9d09e662011-01-13 20:00:02 +00002338 ti->error = "Fail to run raid array";
2339 goto bad;
2340 }
2341
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002342 if (ti->len != rs->md.array_sectors) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002343 ti->error = "Array size does not match requested target length";
2344 r = -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002345 goto size_mismatch;
2346 }
NeilBrown9d09e662011-01-13 20:00:02 +00002347 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00002348 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
2349
Jonathan Brassow32737272011-08-02 12:32:07 +01002350 mddev_suspend(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002351 return 0;
2352
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002353size_mismatch:
2354 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00002355bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002356 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002357
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002358 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002359}
2360
2361static void raid_dtr(struct dm_target *ti)
2362{
2363 struct raid_set *rs = ti->private;
2364
2365 list_del_init(&rs->callbacks.list);
2366 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002367 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00002368}
2369
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002370static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00002371{
2372 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11002373 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00002374
2375 mddev->pers->make_request(mddev, bio);
2376
2377 return DM_MAPIO_SUBMITTED;
2378}
2379
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002380/* Return string describing the current sync action of @mddev */
Jonathan Brassowbe836512013-04-24 11:42:43 +10002381static const char *decipher_sync_action(struct mddev *mddev)
2382{
2383 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
2384 return "frozen";
2385
2386 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2387 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2388 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2389 return "reshape";
2390
2391 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2392 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2393 return "resync";
2394 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2395 return "check";
2396 return "repair";
2397 }
2398
2399 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
2400 return "recover";
2401 }
2402
2403 return "idle";
2404}
2405
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002406/*
2407 * Return status string @rdev
2408 *
2409 * Status characters:
2410 *
2411 * 'D' = Dead/Failed device
2412 * 'a' = Alive but not in-sync
2413 * 'A' = Alive and in-sync
2414 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002415static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002416{
2417 if (test_bit(Faulty, &rdev->flags))
2418 return "D";
2419 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags))
2420 return "a";
2421 else
2422 return "A";
2423}
2424
2425/* Helper to return resync/reshape progress for @rs and @array_in_sync */
2426static sector_t rs_get_progress(struct raid_set *rs,
2427 sector_t resync_max_sectors, bool *array_in_sync)
2428{
2429 sector_t r, recovery_cp, curr_resync_completed;
2430 struct mddev *mddev = &rs->md;
2431
2432 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp;
2433 recovery_cp = mddev->recovery_cp;
2434 *array_in_sync = false;
2435
2436 if (rs_is_raid0(rs)) {
2437 r = resync_max_sectors;
2438 *array_in_sync = true;
2439
2440 } else {
2441 r = mddev->reshape_position;
2442
2443 /* Reshape is relative to the array size */
2444 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
2445 r != MaxSector) {
2446 if (r == MaxSector) {
2447 *array_in_sync = true;
2448 r = resync_max_sectors;
2449 } else {
2450 /* Got to reverse on backward reshape */
2451 if (mddev->reshape_backwards)
2452 r = mddev->array_sectors - r;
2453
2454 /* Devide by # of data stripes */
2455 sector_div(r, mddev_data_stripes(rs));
2456 }
2457
2458 /* Sync is relative to the component device size */
2459 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2460 r = curr_resync_completed;
2461 else
2462 r = recovery_cp;
2463
2464 if (r == MaxSector) {
2465 /*
2466 * Sync complete.
2467 */
2468 *array_in_sync = true;
2469 r = resync_max_sectors;
2470 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2471 /*
2472 * If "check" or "repair" is occurring, the raid set has
2473 * undergone an initial sync and the health characters
2474 * should not be 'a' anymore.
2475 */
2476 *array_in_sync = true;
2477 } else {
2478 struct md_rdev *rdev;
2479
2480 /*
2481 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04002482 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002483 * devices are In_sync, then it is the raid set that is
2484 * being initialized.
2485 */
2486 rdev_for_each(rdev, mddev)
2487 if (!test_bit(In_sync, &rdev->flags))
2488 *array_in_sync = true;
2489#if 0
2490 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */
2491#endif
2492 }
2493 }
2494
2495 return r;
2496}
2497
2498/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002499static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002500{
2501 return dev ? dev->name : "-";
2502}
2503
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002504static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002505 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00002506{
2507 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002508 struct mddev *mddev = &rs->md;
2509 struct r5conf *conf = mddev->private;
2510 int max_nr_stripes = conf ? conf->max_nr_stripes : 0;
2511 bool array_in_sync;
2512 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
2513 unsigned int sz = 0;
2514 unsigned int write_mostly_params = 0;
2515 sector_t progress, resync_max_sectors, resync_mismatches;
2516 const char *sync_action;
2517 struct raid_type *rt;
2518 struct md_rdev *rdev;
NeilBrown9d09e662011-01-13 20:00:02 +00002519
2520 switch (type) {
2521 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002522 /* *Should* always succeed */
2523 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
2524 if (!rt)
2525 return;
NeilBrown9d09e662011-01-13 20:00:02 +00002526
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002527 DMEMIT("%s %d ", rt ? rt->name : "unknown", mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00002528
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002529 /* Access most recent mddev properties for status output */
2530 smp_rmb();
2531 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04002532 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002533 mddev->resync_max_sectors : mddev->dev_sectors;
2534 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync);
2535 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
2536 (unsigned int) atomic64_read(&mddev->resync_mismatches) : 0;
2537 sync_action = decipher_sync_action(&rs->md);
2538
2539 /* HM FIXME: do we want another state char for raid0? It shows 'D' or 'A' now */
2540 rdev_for_each(rdev, mddev)
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002541 DMEMIT(__raid_dev_status(rdev, array_in_sync));
Jonathan Brassowbe836512013-04-24 11:42:43 +10002542
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002543 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002544 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002545 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002546 * - Initializing the raid set
2547 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002548 * The user can distinguish between the two by referring
2549 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002550 *
2551 * The reshape ratio shows the progress of
2552 * changing the raid layout or the number of
2553 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00002554 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002555 DMEMIT(" %llu/%llu", (unsigned long long) progress,
2556 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00002557
Jonathan Brassowbe836512013-04-24 11:42:43 +10002558 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002559 * v1.5.0+:
2560 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10002561 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002562 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10002563 * information on each of these states.
2564 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002565 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10002566
2567 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002568 * v1.5.0+:
2569 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10002570 * resync_mismatches/mismatch_cnt
2571 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002572 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10002573 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002574 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
2575
2576 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002577 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002578 *
2579 * data_offset (needed for out of space reshaping)
2580 * This field shows the data offset into the data
2581 * image LV where the first stripes data starts.
2582 *
2583 * We keep data_offset equal on all raid disks of the set,
2584 * so retrieving it from the first raid disk is sufficient.
2585 */
2586 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
NeilBrown9d09e662011-01-13 20:00:02 +00002587 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002588
NeilBrown9d09e662011-01-13 20:00:02 +00002589 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002590 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00002591
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002592 /* Calculate raid parameter count */
2593 rdev_for_each(rdev, mddev)
2594 if (test_bit(WriteMostly, &rdev->flags))
2595 write_mostly_params += 2;
2596 raid_param_cnt += memweight(rs->rebuild_disks,
2597 DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks)) * 2 +
2598 write_mostly_params +
2599 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
2600 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2;
2601 /* Emit table line */
2602 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04002603 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002604 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002605 raid10_md_layout_to_format(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04002606 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002607 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002608 raid10_md_layout_to_copies(mddev->layout));
Mike Snitzer42863252016-06-02 12:27:46 -04002609 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002610 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04002611 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002612 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Mike Snitzer42863252016-06-02 12:27:46 -04002613 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002614 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002615 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
Mike Snitzer42863252016-06-02 12:27:46 -04002616 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002617 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002618 (unsigned long long) rs->data_offset);
Mike Snitzer42863252016-06-02 12:27:46 -04002619 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002620 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002621 mddev->bitmap_info.daemon_sleep);
Mike Snitzer42863252016-06-02 12:27:46 -04002622 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002623 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002624 mddev->delta_disks);
Mike Snitzer42863252016-06-02 12:27:46 -04002625 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002626 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002627 max_nr_stripes);
2628 rdev_for_each(rdev, mddev)
2629 if (test_bit(rdev->raid_disk, (void *) rs->rebuild_disks))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002630 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002631 rdev->raid_disk);
2632 rdev_for_each(rdev, mddev)
2633 if (test_bit(WriteMostly, &rdev->flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002634 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002635 rdev->raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04002636 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002637 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002638 mddev->bitmap_info.max_write_behind);
Mike Snitzer42863252016-06-02 12:27:46 -04002639 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002640 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002641 mddev->sync_speed_max);
Mike Snitzer42863252016-06-02 12:27:46 -04002642 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04002643 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002644 mddev->sync_speed_min);
2645 DMEMIT(" %d", rs->raid_disks);
2646 rdev_for_each(rdev, mddev) {
2647 struct raid_dev *rd = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002648
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002649 DMEMIT(" %s %s", __get_dev_name(rd->meta_dev),
2650 __get_dev_name(rd->data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00002651 }
2652 }
NeilBrown9d09e662011-01-13 20:00:02 +00002653}
2654
Jonathan Brassowbe836512013-04-24 11:42:43 +10002655static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
2656{
2657 struct raid_set *rs = ti->private;
2658 struct mddev *mddev = &rs->md;
2659
2660 if (!strcasecmp(argv[0], "reshape")) {
2661 DMERR("Reshape not supported.");
2662 return -EINVAL;
2663 }
2664
2665 if (!mddev->pers || !mddev->pers->sync_request)
2666 return -EINVAL;
2667
2668 if (!strcasecmp(argv[0], "frozen"))
2669 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2670 else
2671 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2672
2673 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
2674 if (mddev->sync_thread) {
2675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2676 md_reap_sync_thread(mddev);
2677 }
2678 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2679 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2680 return -EBUSY;
2681 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002682 ; /* MD_RECOVERY_NEEDED set below */
2683 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10002684 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002685 else {
Jonathan Brassowbe836512013-04-24 11:42:43 +10002686 if (!strcasecmp(argv[0], "check"))
2687 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2688 else if (!!strcasecmp(argv[0], "repair"))
2689 return -EINVAL;
2690 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2691 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2692 }
2693 if (mddev->ro == 2) {
2694 /* A write to sync_action is enough to justify
2695 * canceling read-auto mode
2696 */
2697 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002698 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10002699 md_wakeup_thread(mddev->sync_thread);
2700 }
2701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002702 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10002703 md_wakeup_thread(mddev->thread);
2704
2705 return 0;
2706}
2707
2708static int raid_iterate_devices(struct dm_target *ti,
2709 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00002710{
2711 struct raid_set *rs = ti->private;
2712 unsigned i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002713 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00002714
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002715 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00002716 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002717 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00002718 rs->dev[i].data_dev,
2719 0, /* No offset on data devs */
2720 rs->md.dev_sectors,
2721 data);
2722
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002723 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00002724}
2725
2726static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
2727{
2728 struct raid_set *rs = ti->private;
2729 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11002730 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00002731
2732 blk_limits_io_min(limits, chunk_size);
2733 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
2734}
2735
2736static void raid_presuspend(struct dm_target *ti)
2737{
2738 struct raid_set *rs = ti->private;
2739
2740 md_stop_writes(&rs->md);
2741}
2742
2743static void raid_postsuspend(struct dm_target *ti)
2744{
2745 struct raid_set *rs = ti->private;
2746
2747 mddev_suspend(&rs->md);
2748}
2749
Jonathan Brassowf381e712013-05-08 17:57:13 -05002750static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00002751{
Jonathan Brassow9092c022013-05-02 14:19:24 -05002752 int i;
2753 uint64_t failed_devices, cleared_failed_devices = 0;
2754 unsigned long flags;
2755 struct dm_raid_superblock *sb;
Jonathan Brassow9092c022013-05-02 14:19:24 -05002756 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00002757
Jonathan Brassowf381e712013-05-08 17:57:13 -05002758 for (i = 0; i < rs->md.raid_disks; i++) {
2759 r = &rs->dev[i].rdev;
2760 if (test_bit(Faulty, &r->flags) && r->sb_page &&
Mike Christie796a5cf2016-06-05 14:32:07 -05002761 sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
2762 1)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05002763 DMINFO("Faulty %s device #%d has readable super block."
2764 " Attempting to revive it.",
2765 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05002766
2767 /*
2768 * Faulty bit may be set, but sometimes the array can
2769 * be suspended before the personalities can respond
2770 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04002771 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05002772 * the failed device, its 'raid_disk' number will be
2773 * '>= 0' - meaning we must call this function
2774 * ourselves.
2775 */
2776 if ((r->raid_disk >= 0) &&
2777 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
2778 /* Failed to revive this device, try next */
2779 continue;
2780
Jonathan Brassowf381e712013-05-08 17:57:13 -05002781 r->raid_disk = i;
2782 r->saved_raid_disk = i;
2783 flags = r->flags;
2784 clear_bit(Faulty, &r->flags);
2785 clear_bit(WriteErrorSeen, &r->flags);
2786 clear_bit(In_sync, &r->flags);
2787 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
2788 r->raid_disk = -1;
2789 r->saved_raid_disk = -1;
2790 r->flags = flags;
2791 } else {
2792 r->recovery_offset = 0;
2793 cleared_failed_devices |= 1 << i;
2794 }
2795 }
2796 }
2797 if (cleared_failed_devices) {
2798 rdev_for_each(r, &rs->md) {
2799 sb = page_address(r->sb_page);
2800 failed_devices = le64_to_cpu(sb->failed_devices);
2801 failed_devices &= ~cleared_failed_devices;
2802 sb->failed_devices = cpu_to_le64(failed_devices);
2803 }
2804 }
2805}
2806
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002807static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002808{
2809 int r = 0;
2810
2811 /* Try loading the bitmap unless "raid0", which does not have one */
2812 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04002813 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002814 r = bitmap_load(&rs->md);
2815 if (r)
2816 DMERR("Failed to load bitmap");
2817 }
2818
2819 return r;
2820}
2821
2822static int raid_preresume(struct dm_target *ti)
2823{
2824 struct raid_set *rs = ti->private;
2825 struct mddev *mddev = &rs->md;
2826
2827 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04002828 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002829 return 0;
2830
2831 /*
2832 * The superblocks need to be updated on disk if the
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002833 * array is new or __load_dirty_region_bitmap will overwrite them
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002834 * in core with old data.
2835 *
2836 * In case the array got modified (takeover/reshape/resize)
2837 * or the data offsets on the component devices changed, they
2838 * have to be updated as well.
2839 *
2840 * Have to switch to readwrite and back in order to
2841 * allow for the superblock updates.
2842 */
Mike Snitzer42863252016-06-02 12:27:46 -04002843 if (test_and_clear_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002844 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2845 mddev->ro = 0;
2846 md_update_sb(mddev, 1);
2847 mddev->ro = 1;
2848 }
2849
2850 /*
2851 * Disable/enable discard support on raid set after any
2852 * conversion, because devices can have been added
2853 */
2854 configure_discard_support(rs);
2855
2856 /* Load the bitmap from disk unless raid0 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002857 return __load_dirty_region_bitmap(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002858}
2859
Jonathan Brassowf381e712013-05-08 17:57:13 -05002860static void raid_resume(struct dm_target *ti)
2861{
2862 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002863 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05002864
Mike Snitzer42863252016-06-02 12:27:46 -04002865 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002866 /*
2867 * A secondary resume while the device is active.
2868 * Take this opportunity to check whether any failed
2869 * devices are reachable again.
2870 */
2871 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10002872 }
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06002873
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002874 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002875 mddev->in_sync = 0;
2876 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2877
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002878 if (mddev->suspended)
2879 mddev_resume(mddev);
NeilBrown9d09e662011-01-13 20:00:02 +00002880}
2881
2882static struct target_type raid_target = {
2883 .name = "raid",
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002884 .version = {1, 9, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00002885 .module = THIS_MODULE,
2886 .ctr = raid_ctr,
2887 .dtr = raid_dtr,
2888 .map = raid_map,
2889 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10002890 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00002891 .iterate_devices = raid_iterate_devices,
2892 .io_hints = raid_io_hints,
2893 .presuspend = raid_presuspend,
2894 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002895 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00002896 .resume = raid_resume,
2897};
2898
2899static int __init dm_raid_init(void)
2900{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11002901 DMINFO("Loading target version %u.%u.%u",
2902 raid_target.version[0],
2903 raid_target.version[1],
2904 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00002905 return dm_register_target(&raid_target);
2906}
2907
2908static void __exit dm_raid_exit(void)
2909{
2910 dm_unregister_target(&raid_target);
2911}
2912
2913module_init(dm_raid_init);
2914module_exit(dm_raid_exit);
2915
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002916module_param(devices_handle_discard_safely, bool, 0644);
2917MODULE_PARM_DESC(devices_handle_discard_safely,
2918 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
2919
Mike Snitzeref9b85a2016-06-02 12:02:19 -04002920MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
2921MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05002922MODULE_ALIAS("dm-raid1");
2923MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00002924MODULE_ALIAS("dm-raid4");
2925MODULE_ALIAS("dm-raid5");
2926MODULE_ALIAS("dm-raid6");
2927MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02002928MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00002929MODULE_LICENSE("GPL");