blob: 7ef469e902c620126b95f69d899e528ae114b3bc [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003 * Copyright (C) 2010-2017 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"
Mike Snitzer935fe092017-10-10 17:02:41 -040015#include "md-bitmap.h"
NeilBrown9d09e662011-01-13 20:00:02 +000016
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010017#include <linux/device-mapper.h>
18
NeilBrown9d09e662011-01-13 20:00:02 +000019#define DM_MSG_PREFIX "raid"
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +020020#define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
NeilBrown9d09e662011-01-13 20:00:02 +000021
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +020022/*
23 * Minimum sectors of free reshape space per raid device
24 */
25#define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
26
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010027/*
28 * Minimum journal space 4 MiB in sectors.
29 */
30#define MIN_RAID456_JOURNAL_SPACE (4*2048)
31
Heinz Mauelshagen11e47232017-12-13 17:13:18 +010032/* Global list of all raid sets */
Wei Yongjun67ac9012018-01-02 06:18:10 +000033static LIST_HEAD(raid_sets);
Heinz Mauelshagen11e47232017-12-13 17:13:18 +010034
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +020035static bool devices_handle_discard_safely = false;
36
NeilBrown9d09e662011-01-13 20:00:02 +000037/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010038 * The following flags are used by dm-raid.c to set up the array state.
39 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000040 */
Mike Snitzer43157842016-05-30 13:03:37 -040041#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000042
43struct raid_dev {
44 /*
45 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040046 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000047 * ti->len and give more flexibility in altering size and
48 * characteristics.
49 *
50 * While it is possible for this device to be associated
51 * with a different physical device than the data_dev, it
52 * is intended for it to be the same.
53 * |--------- Physical Device ---------|
54 * |- meta_dev -|------ data_dev ------|
55 */
56 struct dm_dev *meta_dev;
57 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110058 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000059};
60
61/*
Mike Snitzer42863252016-06-02 12:27:46 -040062 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020063 *
64 * 1 = no flag value
65 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000066 */
Mike Snitzer42863252016-06-02 12:27:46 -040067#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
68#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
69#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
70#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
71#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
72#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
73#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
74#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
75#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
76#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
77#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
78#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
Mike Snitzer9b6e5422016-06-02 11:48:09 -040079/* New for v1.9.0 */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +020080#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
Mike Snitzer42863252016-06-02 12:27:46 -040081#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
82#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
83
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010084/* New for v1.10.0 */
Heinz Mauelshagen6e536362017-03-22 17:44:38 +010085#define __CTR_FLAG_JOURNAL_DEV 15 /* 2 */ /* Only with raid4/5/6 (journal device)! */
86
87/* New for v1.11.1 */
88#define __CTR_FLAG_JOURNAL_MODE 16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010089
Mike Snitzer42863252016-06-02 12:27:46 -040090/*
91 * Flags for rs->ctr_flags field.
92 */
93#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
94#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
95#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
96#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
97#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
98#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
99#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
100#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
101#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
102#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
103#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
104#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
105#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
106#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
107#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100108#define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV)
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100109#define CTR_FLAG_JOURNAL_MODE (1 << __CTR_FLAG_JOURNAL_MODE)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500110
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200111/*
112 * Definitions of various constructor flags to
113 * be used in checks of valid / invalid flags
114 * per raid level.
115 */
116/* Define all any sync flags */
117#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
118
119/* Define flags for options without argument (e.g. 'nosync') */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200120#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
121 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200122
123/* Define flags for options with one argument (e.g. 'delta_disks +2') */
124#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
125 CTR_FLAG_WRITE_MOSTLY | \
126 CTR_FLAG_DAEMON_SLEEP | \
127 CTR_FLAG_MIN_RECOVERY_RATE | \
128 CTR_FLAG_MAX_RECOVERY_RATE | \
129 CTR_FLAG_MAX_WRITE_BEHIND | \
130 CTR_FLAG_STRIPE_CACHE | \
131 CTR_FLAG_REGION_SIZE | \
132 CTR_FLAG_RAID10_COPIES | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200133 CTR_FLAG_RAID10_FORMAT | \
134 CTR_FLAG_DELTA_DISKS | \
135 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200136
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200137/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200138
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200139/* "raid0" does only accept data offset */
140#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200141
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200142/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
143#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
144 CTR_FLAG_REBUILD | \
145 CTR_FLAG_WRITE_MOSTLY | \
146 CTR_FLAG_DAEMON_SLEEP | \
147 CTR_FLAG_MIN_RECOVERY_RATE | \
148 CTR_FLAG_MAX_RECOVERY_RATE | \
149 CTR_FLAG_MAX_WRITE_BEHIND | \
150 CTR_FLAG_REGION_SIZE | \
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +0200151 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200152 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200153
154/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200155#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
156 CTR_FLAG_REBUILD | \
157 CTR_FLAG_DAEMON_SLEEP | \
158 CTR_FLAG_MIN_RECOVERY_RATE | \
159 CTR_FLAG_MAX_RECOVERY_RATE | \
160 CTR_FLAG_REGION_SIZE | \
161 CTR_FLAG_RAID10_COPIES | \
162 CTR_FLAG_RAID10_FORMAT | \
163 CTR_FLAG_DELTA_DISKS | \
164 CTR_FLAG_DATA_OFFSET | \
165 CTR_FLAG_RAID10_USE_NEAR_SETS)
166
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200167/*
168 * "raid4/5/6" do not accept any raid1 or raid10 specific options
169 *
170 * "raid6" does not accept "nosync", because it is not guaranteed
171 * that both parity and q-syndrome are being written properly with
172 * any writes
173 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200174#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
175 CTR_FLAG_REBUILD | \
176 CTR_FLAG_DAEMON_SLEEP | \
177 CTR_FLAG_MIN_RECOVERY_RATE | \
178 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200179 CTR_FLAG_STRIPE_CACHE | \
180 CTR_FLAG_REGION_SIZE | \
181 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100182 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100183 CTR_FLAG_JOURNAL_DEV | \
184 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200185
186#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
187 CTR_FLAG_REBUILD | \
188 CTR_FLAG_DAEMON_SLEEP | \
189 CTR_FLAG_MIN_RECOVERY_RATE | \
190 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200191 CTR_FLAG_STRIPE_CACHE | \
192 CTR_FLAG_REGION_SIZE | \
193 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100194 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100195 CTR_FLAG_JOURNAL_DEV | \
196 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200197/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200198
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200199/*
200 * Flags for rs->runtime_flags field
201 * (RT_FLAG prefix meaning "runtime flag")
202 *
203 * These are all internal and used to define runtime state,
204 * e.g. to prevent another resume from preresume processing
205 * the raid set all over again.
206 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200207#define RT_FLAG_RS_PRERESUMED 0
208#define RT_FLAG_RS_RESUMED 1
209#define RT_FLAG_RS_BITMAP_LOADED 2
210#define RT_FLAG_UPDATE_SBS 3
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200211#define RT_FLAG_RESHAPE_RS 4
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +0200212#define RT_FLAG_RS_SUSPENDED 5
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +0100213#define RT_FLAG_RS_IN_SYNC 6
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +0100214#define RT_FLAG_RS_RESYNCING 7
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200215
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200216/* Array elements of 64 bit needed for rebuild/failed disk bits */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200217#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
218
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200219/*
220 * raid set level, layout and chunk sectors backup/restore
221 */
222struct rs_layout {
223 int new_level;
224 int new_layout;
225 int new_chunk_sectors;
226};
227
NeilBrown9d09e662011-01-13 20:00:02 +0000228struct raid_set {
229 struct dm_target *ti;
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100230 struct list_head list;
NeilBrown9d09e662011-01-13 20:00:02 +0000231
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200232 uint32_t stripe_cache_entries;
Mike Snitzer42863252016-06-02 12:27:46 -0400233 unsigned long ctr_flags;
234 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200235
236 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000237
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200238 int raid_disks;
239 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200240 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200241 int raid10_copies;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +0200242 int requested_bitmap_chunk_sectors;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200243
NeilBrownfd01b882011-10-11 16:47:53 +1100244 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000245 struct raid_type *raid_type;
246 struct dm_target_callbacks callbacks;
247
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100248 /* Optional raid4/5/6 journal device */
249 struct journal_dev {
250 struct dm_dev *dev;
251 struct md_rdev rdev;
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100252 int mode;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100253 } journal_dev;
254
NeilBrown9d09e662011-01-13 20:00:02 +0000255 struct raid_dev dev[0];
256};
257
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200258static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200259{
260 struct mddev *mddev = &rs->md;
261
262 l->new_level = mddev->new_level;
263 l->new_layout = mddev->new_layout;
264 l->new_chunk_sectors = mddev->new_chunk_sectors;
265}
266
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200267static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200268{
269 struct mddev *mddev = &rs->md;
270
271 mddev->new_level = l->new_level;
272 mddev->new_layout = l->new_layout;
273 mddev->new_chunk_sectors = l->new_chunk_sectors;
274}
275
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100276/* Find any raid_set in active slot for @rs on global list */
277static struct raid_set *rs_find_active(struct raid_set *rs)
278{
279 struct raid_set *r;
280 struct mapped_device *md = dm_table_get_md(rs->ti->table);
281
282 list_for_each_entry(r, &raid_sets, list)
283 if (r != rs && dm_table_get_md(r->ti->table) == md)
284 return r;
285
286 return NULL;
287}
288
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200289/* raid10 algorithms (i.e. formats) */
290#define ALGORITHM_RAID10_DEFAULT 0
291#define ALGORITHM_RAID10_NEAR 1
292#define ALGORITHM_RAID10_OFFSET 2
293#define ALGORITHM_RAID10_FAR 3
294
NeilBrown9d09e662011-01-13 20:00:02 +0000295/* Supported raid types and properties. */
296static struct raid_type {
297 const char *name; /* RAID algorithm. */
298 const char *descr; /* Descriptor text for logging. */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200299 const unsigned int parity_devs; /* # of parity devices. */
300 const unsigned int minimal_devs;/* minimal # of devices in set. */
301 const unsigned int level; /* RAID level. */
302 const unsigned int algorithm; /* RAID algorithm. */
NeilBrown9d09e662011-01-13 20:00:02 +0000303} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400304 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
305 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
306 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200307 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400308 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
309 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
Heinz Mauelshagenb052b072016-10-17 21:20:07 +0200310 {"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
Mike Snitzer43157842016-05-30 13:03:37 -0400311 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
312 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
313 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
314 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
315 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
316 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
317 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
318 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
319 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
320 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
321 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
322 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
323 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000324};
325
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200326/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400327static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200328{
329 return v >= min && v <= max;
330}
331
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200332/* All table line arguments are defined here */
333static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400334 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200335 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400336} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200337 { CTR_FLAG_SYNC, "sync"},
338 { CTR_FLAG_NOSYNC, "nosync"},
339 { CTR_FLAG_REBUILD, "rebuild"},
340 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
341 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
342 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
343 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
Heinz Mauelshagen65359ee2016-06-24 21:32:25 +0200344 { CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200345 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
346 { CTR_FLAG_REGION_SIZE, "region_size"},
347 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
348 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200349 { CTR_FLAG_DATA_OFFSET, "data_offset"},
350 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
351 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100352 { CTR_FLAG_JOURNAL_DEV, "journal_dev" },
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100353 { CTR_FLAG_JOURNAL_MODE, "journal_mode" },
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200354};
355
356/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400357static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200358{
359 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400360 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200361
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400362 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400363 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200364 return anf->name;
365
366 } else
367 DMERR("%s called with more than one flag!", __func__);
368
369 return NULL;
370}
371
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100372/* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
373static struct {
374 const int mode;
375 const char *param;
376} _raid456_journal_mode[] = {
377 { R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
378 { R5C_JOURNAL_MODE_WRITE_BACK , "writeback" }
379};
380
381/* Return MD raid4/5/6 journal mode for dm @journal_mode one */
382static int dm_raid_journal_mode_to_md(const char *mode)
383{
384 int m = ARRAY_SIZE(_raid456_journal_mode);
385
386 while (m--)
387 if (!strcasecmp(mode, _raid456_journal_mode[m].param))
388 return _raid456_journal_mode[m].mode;
389
390 return -EINVAL;
391}
392
393/* Return dm-raid raid4/5/6 journal mode string for @mode */
394static const char *md_journal_mode_to_dm_raid(const int mode)
395{
396 int m = ARRAY_SIZE(_raid456_journal_mode);
397
398 while (m--)
399 if (mode == _raid456_journal_mode[m].mode)
400 return _raid456_journal_mode[m].param;
401
402 return "unknown";
403}
404
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200405/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200406 * Bool helpers to test for various raid levels of a raid set.
407 * It's level as reported by the superblock rather than
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200408 * the requested raid_type passed to the constructor.
409 */
410/* Return true, if raid set in @rs is raid0 */
411static bool rs_is_raid0(struct raid_set *rs)
412{
413 return !rs->md.level;
414}
415
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200416/* Return true, if raid set in @rs is raid1 */
417static bool rs_is_raid1(struct raid_set *rs)
418{
419 return rs->md.level == 1;
420}
421
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200422/* Return true, if raid set in @rs is raid10 */
423static bool rs_is_raid10(struct raid_set *rs)
424{
425 return rs->md.level == 10;
426}
427
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200428/* Return true, if raid set in @rs is level 6 */
429static bool rs_is_raid6(struct raid_set *rs)
430{
431 return rs->md.level == 6;
432}
433
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200434/* Return true, if raid set in @rs is level 4, 5 or 6 */
435static bool rs_is_raid456(struct raid_set *rs)
436{
437 return __within_range(rs->md.level, 4, 6);
438}
439
440/* Return true, if raid set in @rs is reshapable */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200441static bool __is_raid10_far(int layout);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200442static bool rs_is_reshapable(struct raid_set *rs)
443{
444 return rs_is_raid456(rs) ||
445 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
446}
447
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200448/* Return true, if raid set in @rs is recovering */
449static bool rs_is_recovering(struct raid_set *rs)
450{
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +0100451 return rs->md.recovery_cp < rs->md.dev_sectors;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200452}
453
454/* Return true, if raid set in @rs is reshaping */
455static bool rs_is_reshaping(struct raid_set *rs)
456{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200457 return rs->md.reshape_position != MaxSector;
458}
459
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200460/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200461 * bool helpers to test for various raid levels of a raid type @rt
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200462 */
463
464/* Return true, if raid type in @rt is raid0 */
465static bool rt_is_raid0(struct raid_type *rt)
466{
467 return !rt->level;
468}
469
470/* Return true, if raid type in @rt is raid1 */
471static bool rt_is_raid1(struct raid_type *rt)
472{
473 return rt->level == 1;
474}
475
476/* Return true, if raid type in @rt is raid10 */
477static bool rt_is_raid10(struct raid_type *rt)
478{
479 return rt->level == 10;
480}
481
482/* Return true, if raid type in @rt is raid4/5 */
483static bool rt_is_raid45(struct raid_type *rt)
484{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400485 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200486}
487
488/* Return true, if raid type in @rt is raid6 */
489static bool rt_is_raid6(struct raid_type *rt)
490{
491 return rt->level == 6;
492}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200493
494/* Return true, if raid type in @rt is raid4/5/6 */
495static bool rt_is_raid456(struct raid_type *rt)
496{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400497 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200498}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200499/* END: raid level bools */
500
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200501/* Return valid ctr flags for the raid level of @rs */
502static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200503{
504 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200505 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200506 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200507 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200508 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200509 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200510 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200511 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200512 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200513 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200514
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200515 return 0;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200516}
517
518/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200519 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200520 *
521 * Has to be called after parsing of the ctr flags!
522 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200523static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200524{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200525 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400526 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400527 return -EINVAL;
528 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200529
530 return 0;
531}
532
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200533/* MD raid10 bit definitions and helpers */
534#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
535#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
536#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
537#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
538
539/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400540static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200541{
542 return layout & 0xFF;
543}
544
545/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400546static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200547{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400548 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200549}
550
551/* Return true if md raid10 offset for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200552static bool __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200553{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200554 return !!(layout & RAID10_OFFSET);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200555}
556
557/* Return true if md raid10 near for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200558static bool __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200559{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400560 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200561}
562
563/* Return true if md raid10 far for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200564static bool __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200565{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400566 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200567}
568
569/* Return md raid10 layout string for @layout */
570static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100571{
572 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200573 * Bit 16 stands for "offset"
574 * (i.e. adjacent stripes hold copies)
575 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100576 * Refer to MD's raid10.c for details
577 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400578 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100579 return "offset";
580
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400581 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100582 return "near";
583
Heinz Mauelshagenbbac1e02017-07-13 17:33:22 +0200584 if (__raid10_far_copies(layout) > 1)
585 return "far";
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200586
Heinz Mauelshagenbbac1e02017-07-13 17:33:22 +0200587 return "unknown";
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100588}
589
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200590/* Return md raid10 algorithm for @name */
Heinz Mauelshagen552aa672017-12-13 17:13:21 +0100591static const int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500592{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200593 if (!strcasecmp(name, "near"))
594 return ALGORITHM_RAID10_NEAR;
595 else if (!strcasecmp(name, "offset"))
596 return ALGORITHM_RAID10_OFFSET;
597 else if (!strcasecmp(name, "far"))
598 return ALGORITHM_RAID10_FAR;
599
600 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500601}
602
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200603/* Return md raid10 copies for @layout */
604static unsigned int raid10_md_layout_to_copies(int layout)
605{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200606 return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200607}
608
609/* Return md raid10 format id for @format string */
610static int raid10_format_to_md_layout(struct raid_set *rs,
611 unsigned int algorithm,
612 unsigned int copies)
613{
614 unsigned int n = 1, f = 1, r = 0;
615
616 /*
617 * MD resilienece flaw:
618 *
619 * enabling use_far_sets for far/offset formats causes copies
620 * to be colocated on the same devs together with their origins!
621 *
622 * -> disable it for now in the definition above
623 */
624 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
625 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100626 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200627
628 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100629 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200630 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400631 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200632 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100633
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200634 } else if (algorithm == ALGORITHM_RAID10_FAR) {
635 f = copies;
636 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400637 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200638 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100639
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200640 } else
641 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100642
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200643 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
644}
645/* END: MD raid10 bit definitions and helpers */
646
647/* Check for any of the raid10 algorithms */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200648static bool __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200649{
650 if (rtp->level == 10) {
651 switch (rtp->algorithm) {
652 case ALGORITHM_RAID10_DEFAULT:
653 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400654 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200655 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400656 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200657 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400658 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200659 default:
660 break;
661 }
662 }
663
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200664 return false;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500665}
666
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200667/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200668static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000669{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200670 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000671
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200672 while (rtp-- > raid_types)
673 if (!strcasecmp(rtp->name, name))
674 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000675
676 return NULL;
677}
678
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200679/* Return raid_type for @name based derived from @level and @layout */
680static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
681{
682 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
683
684 while (rtp-- > raid_types) {
685 /* RAID10 special checks based on @layout flags/properties */
686 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400687 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200688 return rtp;
689 }
690
691 return NULL;
692}
693
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100694/* Adjust rdev sectors */
695static void rs_set_rdev_sectors(struct raid_set *rs)
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200696{
697 struct mddev *mddev = &rs->md;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200698 struct md_rdev *rdev;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200699
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200700 /*
701 * raid10 sets rdev->sector to the device size, which
702 * is unintended in case of out-of-place reshaping
703 */
704 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100705 if (!test_bit(Journal, &rdev->flags))
706 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100707}
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200708
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +0100709/*
710 * Change bdev capacity of @rs in case of a disk add/remove reshape
711 */
712static void rs_set_capacity(struct raid_set *rs)
713{
714 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
715
716 set_capacity(gendisk, rs->md.array_sectors);
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200717 revalidate_disk(gendisk);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200718}
719
720/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200721 * Set the mddev properties in @rs to the current
722 * ones retrieved from the freshest superblock
723 */
724static void rs_set_cur(struct raid_set *rs)
725{
726 struct mddev *mddev = &rs->md;
727
728 mddev->new_level = mddev->level;
729 mddev->new_layout = mddev->layout;
730 mddev->new_chunk_sectors = mddev->chunk_sectors;
731}
732
733/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200734 * Set the mddev properties in @rs to the new
735 * ones requested by the ctr
736 */
737static void rs_set_new(struct raid_set *rs)
738{
739 struct mddev *mddev = &rs->md;
740
741 mddev->level = mddev->new_level;
742 mddev->layout = mddev->new_layout;
743 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200744 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200745 mddev->delta_disks = 0;
746}
747
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400748static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200749 unsigned int raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000750{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200751 unsigned int i;
NeilBrown9d09e662011-01-13 20:00:02 +0000752 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000753
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400754 if (raid_devs <= raid_type->parity_devs) {
755 ti->error = "Insufficient number of devices";
756 return ERR_PTR(-EINVAL);
757 }
NeilBrown9d09e662011-01-13 20:00:02 +0000758
NeilBrown9d09e662011-01-13 20:00:02 +0000759 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400760 if (!rs) {
761 ti->error = "Cannot allocate raid context";
762 return ERR_PTR(-ENOMEM);
763 }
NeilBrown9d09e662011-01-13 20:00:02 +0000764
765 mddev_init(&rs->md);
766
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100767 INIT_LIST_HEAD(&rs->list);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200768 rs->raid_disks = raid_devs;
769 rs->delta_disks = 0;
770
NeilBrown9d09e662011-01-13 20:00:02 +0000771 rs->ti = ti;
772 rs->raid_type = raid_type;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200773 rs->stripe_cache_entries = 256;
NeilBrown9d09e662011-01-13 20:00:02 +0000774 rs->md.raid_disks = raid_devs;
775 rs->md.level = raid_type->level;
776 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000777 rs->md.layout = raid_type->algorithm;
778 rs->md.new_layout = rs->md.layout;
779 rs->md.delta_disks = 0;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200780 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +0000781
782 for (i = 0; i < raid_devs; i++)
783 md_rdev_init(&rs->dev[i].rdev);
784
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100785 /* Add @rs to global list. */
786 list_add(&rs->list, &raid_sets);
787
NeilBrown9d09e662011-01-13 20:00:02 +0000788 /*
789 * Remaining items to be initialized by further RAID params:
790 * rs->md.persistent
791 * rs->md.external
792 * rs->md.chunk_sectors
793 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100794 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000795 */
796
797 return rs;
798}
799
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100800/* Free all @rs allocations and remove it from global list. */
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400801static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000802{
803 int i;
804
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100805 if (rs->journal_dev.dev) {
806 md_rdev_clear(&rs->journal_dev.rdev);
807 dm_put_device(rs->ti, rs->journal_dev.dev);
808 }
809
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200810 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100811 if (rs->dev[i].meta_dev)
812 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000813 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000814 if (rs->dev[i].data_dev)
815 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100816 }
NeilBrown9d09e662011-01-13 20:00:02 +0000817
Heinz Mauelshagen11e47232017-12-13 17:13:18 +0100818 list_del(&rs->list);
819
NeilBrown9d09e662011-01-13 20:00:02 +0000820 kfree(rs);
821}
822
823/*
824 * For every device we have two words
825 * <meta_dev>: meta device name or '-' if missing
826 * <data_dev>: data device name or '-' if missing
827 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100828 * The following are permitted:
829 * - -
830 * - <data_dev>
831 * <meta_dev> <data_dev>
832 *
833 * The following is not allowed:
834 * <meta_dev> -
835 *
836 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400837 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000838 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200839static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000840{
841 int i;
842 int rebuild = 0;
843 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200844 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200845 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000846
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200847 /* Put off the number of raid devices argument to get to dev pairs */
848 arg = dm_shift_arg(as);
849 if (!arg)
850 return -EINVAL;
851
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200852 for (i = 0; i < rs->raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000853 rs->dev[i].rdev.raid_disk = i;
854
855 rs->dev[i].meta_dev = NULL;
856 rs->dev[i].data_dev = NULL;
857
858 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100859 * There are no offsets initially.
860 * Out of place reshape will set them accordingly.
NeilBrown9d09e662011-01-13 20:00:02 +0000861 */
862 rs->dev[i].rdev.data_offset = 0;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100863 rs->dev[i].rdev.new_data_offset = 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000864 rs->dev[i].rdev.mddev = &rs->md;
865
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200866 arg = dm_shift_arg(as);
867 if (!arg)
868 return -EINVAL;
869
870 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400871 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
872 &rs->dev[i].meta_dev);
873 if (r) {
874 rs->ti->error = "RAID metadata device lookup failure";
875 return r;
876 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100877
878 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400879 if (!rs->dev[i].rdev.sb_page) {
880 rs->ti->error = "Failed to allocate superblock page";
881 return -ENOMEM;
882 }
NeilBrown9d09e662011-01-13 20:00:02 +0000883 }
884
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200885 arg = dm_shift_arg(as);
886 if (!arg)
887 return -EINVAL;
888
889 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000890 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400891 (!rs->dev[i].rdev.recovery_offset)) {
892 rs->ti->error = "Drive designated for rebuild not specified";
893 return -EINVAL;
894 }
NeilBrown9d09e662011-01-13 20:00:02 +0000895
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400896 if (rs->dev[i].meta_dev) {
897 rs->ti->error = "No data device supplied with metadata device";
898 return -EINVAL;
899 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100900
NeilBrown9d09e662011-01-13 20:00:02 +0000901 continue;
902 }
903
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400904 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
905 &rs->dev[i].data_dev);
906 if (r) {
907 rs->ti->error = "RAID device lookup failure";
908 return r;
909 }
NeilBrown9d09e662011-01-13 20:00:02 +0000910
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100911 if (rs->dev[i].meta_dev) {
912 metadata_available = 1;
913 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
914 }
NeilBrown9d09e662011-01-13 20:00:02 +0000915 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200916 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000917 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
918 rebuild++;
919 }
920
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100921 if (rs->journal_dev.dev)
922 list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
923
NeilBrown9d09e662011-01-13 20:00:02 +0000924 if (metadata_available) {
925 rs->md.external = 0;
926 rs->md.persistent = 1;
927 rs->md.major_version = 2;
928 } else if (rebuild && !rs->md.recovery_cp) {
929 /*
930 * Without metadata, we will not be able to tell if the array
931 * is in-sync or not - we must assume it is not. Therefore,
932 * it is impossible to rebuild a drive.
933 *
934 * Even if there is metadata, the on-disk information may
935 * indicate that the array is not in-sync and it will then
936 * fail at that time.
937 *
938 * User could specify 'nosync' option if desperate.
939 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400940 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
941 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000942 }
943
944 return 0;
945}
946
947/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100948 * validate_region_size
949 * @rs
950 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
951 *
952 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
953 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
954 *
955 * Returns: 0 on success, -EINVAL on failure.
956 */
957static int validate_region_size(struct raid_set *rs, unsigned long region_size)
958{
959 unsigned long min_region_size = rs->ti->len / (1 << 21);
960
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +0200961 if (rs_is_raid0(rs))
962 return 0;
963
Jonathan Brassowc1084562011-08-02 12:32:07 +0100964 if (!region_size) {
965 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400966 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100967 */
968 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000969 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400970 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100971 DMINFO("Choosing default region size of %lu sectors",
972 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100973 } else {
974 DMINFO("Choosing default region size of 4MiB");
975 region_size = 1 << 13; /* sectors */
976 }
977 } else {
978 /*
979 * Validate user-supplied value.
980 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400981 if (region_size > rs->ti->len) {
982 rs->ti->error = "Supplied region size is too large";
983 return -EINVAL;
984 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100985
986 if (region_size < min_region_size) {
987 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
988 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400989 rs->ti->error = "Supplied region size is too small";
990 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100991 }
992
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400993 if (!is_power_of_2(region_size)) {
994 rs->ti->error = "Region size is not a power of 2";
995 return -EINVAL;
996 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100997
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400998 if (region_size < rs->md.chunk_sectors) {
999 rs->ti->error = "Region size is smaller than the chunk size";
1000 return -EINVAL;
1001 }
Jonathan Brassowc1084562011-08-02 12:32:07 +01001002 }
1003
1004 /*
1005 * Convert sectors to bytes.
1006 */
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02001007 rs->md.bitmap_info.chunksize = to_bytes(region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +01001008
1009 return 0;
1010}
1011
1012/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001013 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +11001014 * @rs
1015 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001016 * Determine if there are enough devices in the array that haven't
1017 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +11001018 *
1019 * Returns: 0 on success, -EINVAL on failure.
1020 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001021static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +11001022{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001023 unsigned int i, rebuild_cnt = 0;
1024 unsigned int rebuilds_per_group = 0, copies;
1025 unsigned int group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001026
Jonathan Brassoweb649122012-10-11 13:40:09 +11001027 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001028 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
1029 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +11001030 rebuild_cnt++;
1031
Heinz Mauelshagen53bf5382017-12-13 17:13:17 +01001032 switch (rs->md.level) {
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02001033 case 0:
1034 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001035 case 1:
1036 if (rebuild_cnt >= rs->md.raid_disks)
1037 goto too_many;
1038 break;
1039 case 4:
1040 case 5:
1041 case 6:
1042 if (rebuild_cnt > rs->raid_type->parity_devs)
1043 goto too_many;
1044 break;
1045 case 10:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001046 copies = raid10_md_layout_to_copies(rs->md.new_layout);
Heinz Mauelshagen53bf5382017-12-13 17:13:17 +01001047 if (copies < 2) {
1048 DMERR("Bogus raid10 data copies < 2!");
1049 return -EINVAL;
1050 }
1051
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001052 if (rebuild_cnt < copies)
1053 break;
1054
1055 /*
1056 * It is possible to have a higher rebuild count for RAID10,
1057 * as long as the failed devices occur in different mirror
1058 * groups (i.e. different stripes).
1059 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001060 * When checking "near" format, make sure no adjacent devices
1061 * have failed beyond what can be handled. In addition to the
1062 * simple case where the number of devices is a multiple of the
1063 * number of copies, we must also handle cases where the number
1064 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -04001065 * E.g. dev1 dev2 dev3 dev4 dev5
1066 * A A B B C
1067 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001068 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001069 if (__is_raid10_near(rs->md.new_layout)) {
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001070 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001071 if (!(i % copies))
1072 rebuilds_per_group = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001073 if ((!rs->dev[i].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001074 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001075 (++rebuilds_per_group >= copies))
1076 goto too_many;
1077 }
1078 break;
1079 }
1080
1081 /*
1082 * When checking "far" and "offset" formats, we need to ensure
1083 * that the device that holds its copy is not also dead or
1084 * being rebuilt. (Note that "far" and "offset" formats only
1085 * support two copies right now. These formats also only ever
1086 * use the 'use_far_sets' variant.)
1087 *
1088 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -04001089 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001090 * results in the need to treat the last (potentially larger)
1091 * set differently.
1092 */
1093 group_size = (rs->md.raid_disks / copies);
1094 last_group_start = (rs->md.raid_disks / group_size) - 1;
1095 last_group_start *= group_size;
1096 for (i = 0; i < rs->md.raid_disks; i++) {
1097 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001098 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001099 if ((!rs->dev[i].rdev.sb_page ||
1100 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001101 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001102 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001103 }
1104 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001105 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001106 if (rebuild_cnt)
1107 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001108 }
1109
1110 return 0;
1111
1112too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +11001113 return -EINVAL;
1114}
1115
1116/*
NeilBrown9d09e662011-01-13 20:00:02 +00001117 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +00001118 * <chunk_size> [optional_args]
1119 *
Jonathan Brassow32737272011-08-02 12:32:07 +01001120 * Argument definitions
1121 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -04001122 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +01001123 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -04001124 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +00001125 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +01001126 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -04001127 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +00001128 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
1129 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001130 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +00001131 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
1132 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -04001133 * [region_size <sectors>] Defines granularity of bitmap
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001134 * [journal_dev <dev>] raid4/5/6 journaling deviice
1135 * (i.e. write hole closing log)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001136 *
1137 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -04001138 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001139 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +00001140 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001141static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001142 unsigned int num_raid_params)
NeilBrown9d09e662011-01-13 20:00:02 +00001143{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001144 int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001145 unsigned int raid10_copies = 2;
1146 unsigned int i, write_mostly = 0;
1147 unsigned int region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +01001148 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001149 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001150 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001151 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001152
1153 arg = dm_shift_arg(as);
1154 num_raid_params--; /* Account for chunk_size argument */
1155
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001156 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001157 rs->ti->error = "Bad numerical argument given for chunk_size";
1158 return -EINVAL;
1159 }
NeilBrown9d09e662011-01-13 20:00:02 +00001160
1161 /*
1162 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001163 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001164 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001165 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001166 if (value)
1167 DMERR("Ignoring chunk size parameter for RAID 1");
1168 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001169 } else if (!is_power_of_2(value)) {
1170 rs->ti->error = "Chunk size must be a power of 2";
1171 return -EINVAL;
1172 } else if (value < 8) {
1173 rs->ti->error = "Chunk size value is too small";
1174 return -EINVAL;
1175 }
NeilBrown9d09e662011-01-13 20:00:02 +00001176
1177 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001178
1179 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001180 * We set each individual device as In_sync with a completed
1181 * 'recovery_offset'. If there has been a device failure or
1182 * replacement then one of the following cases applies:
1183 *
1184 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001185 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001186 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001187 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001188 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001189 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001190 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001191 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001192 *
1193 * What is found in the superblocks of the devices is always
1194 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1195 */
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001196 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001197 set_bit(In_sync, &rs->dev[i].rdev.flags);
1198 rs->dev[i].rdev.recovery_offset = MaxSector;
1199 }
1200
1201 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001202 * Second, parse the unordered optional arguments
1203 */
NeilBrown9d09e662011-01-13 20:00:02 +00001204 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001205 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001206 if (!key) {
1207 rs->ti->error = "Not enough raid parameters given";
1208 return -EINVAL;
1209 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001210
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001211 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001212 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001213 rs->ti->error = "Only one 'nosync' argument allowed";
1214 return -EINVAL;
1215 }
NeilBrown9d09e662011-01-13 20:00:02 +00001216 continue;
1217 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001218 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001219 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001220 rs->ti->error = "Only one 'sync' argument allowed";
1221 return -EINVAL;
1222 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001223 continue;
1224 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001225 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001226 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001227 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1228 return -EINVAL;
1229 }
NeilBrown9d09e662011-01-13 20:00:02 +00001230 continue;
1231 }
1232
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001233 arg = dm_shift_arg(as);
1234 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001235 if (!arg) {
1236 rs->ti->error = "Wrong number of raid parameters given";
1237 return -EINVAL;
1238 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001239
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001240 /*
1241 * Parameters that take a string value are checked here.
1242 */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001243 /* "raid10_format {near|offset|far} */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001244 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001245 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001246 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1247 return -EINVAL;
1248 }
1249 if (!rt_is_raid10(rt)) {
1250 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1251 return -EINVAL;
1252 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001253 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001254 if (raid10_format < 0) {
1255 rs->ti->error = "Invalid 'raid10_format' value given";
1256 return raid10_format;
1257 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001258 continue;
1259 }
1260
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001261 /* "journal_dev <dev>" */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001262 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
1263 int r;
1264 struct md_rdev *jdev;
1265
1266 if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1267 rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
1268 return -EINVAL;
1269 }
1270 if (!rt_is_raid456(rt)) {
1271 rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
1272 return -EINVAL;
1273 }
1274 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
1275 &rs->journal_dev.dev);
1276 if (r) {
1277 rs->ti->error = "raid4/5/6 journal device lookup failure";
1278 return r;
1279 }
1280 jdev = &rs->journal_dev.rdev;
1281 md_rdev_init(jdev);
1282 jdev->mddev = &rs->md;
1283 jdev->bdev = rs->journal_dev.dev->bdev;
1284 jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
1285 if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
1286 rs->ti->error = "No space for raid4/5/6 journal";
1287 return -ENOSPC;
1288 }
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001289 rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001290 set_bit(Journal, &jdev->flags);
1291 continue;
1292 }
1293
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001294 /* "journal_mode <mode>" ("journal_dev" mandatory!) */
1295 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
1296 int r;
1297
1298 if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1299 rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
1300 return -EINVAL;
1301 }
1302 if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
1303 rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
1304 return -EINVAL;
1305 }
1306 r = dm_raid_journal_mode_to_md(arg);
1307 if (r < 0) {
1308 rs->ti->error = "Invalid 'journal_mode' argument";
1309 return r;
1310 }
1311 rs->journal_dev.mode = r;
1312 continue;
1313 }
1314
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001315 /*
1316 * Parameters with number values from here on.
1317 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001318 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001319 rs->ti->error = "Bad numerical argument given in raid params";
1320 return -EINVAL;
1321 }
NeilBrown9d09e662011-01-13 20:00:02 +00001322
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001323 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001324 /*
1325 * "rebuild" is being passed in by userspace to provide
1326 * indexes of replaced devices and to set up additional
1327 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001328 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001329 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001330 rs->ti->error = "Invalid rebuild index given";
1331 return -EINVAL;
1332 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001333
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001334 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1335 rs->ti->error = "rebuild for this index already given";
1336 return -EINVAL;
1337 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001338
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001339 rd = rs->dev + value;
1340 clear_bit(In_sync, &rd->rdev.flags);
1341 clear_bit(Faulty, &rd->rdev.flags);
1342 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001343 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001344 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001345 if (!rt_is_raid1(rt)) {
1346 rs->ti->error = "write_mostly option is only valid for RAID1";
1347 return -EINVAL;
1348 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001349
Mike Snitzerbb91a632016-06-02 12:06:54 -04001350 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001351 rs->ti->error = "Invalid write_mostly index given";
1352 return -EINVAL;
1353 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001354
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001355 write_mostly++;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001356 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001357 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001358 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001359 if (!rt_is_raid1(rt)) {
1360 rs->ti->error = "max_write_behind option is only valid for RAID1";
1361 return -EINVAL;
1362 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001363
Mike Snitzer42863252016-06-02 12:27:46 -04001364 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001365 rs->ti->error = "Only one max_write_behind argument pair allowed";
1366 return -EINVAL;
1367 }
NeilBrown9d09e662011-01-13 20:00:02 +00001368
1369 /*
1370 * In device-mapper, we specify things in sectors, but
1371 * MD records this value in kB
1372 */
1373 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001374 if (value > COUNTER_MAX) {
1375 rs->ti->error = "Max write-behind limit out of range";
1376 return -EINVAL;
1377 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001378
NeilBrown9d09e662011-01-13 20:00:02 +00001379 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001380 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001381 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001382 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1383 return -EINVAL;
1384 }
1385 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1386 rs->ti->error = "daemon sleep period out of range";
1387 return -EINVAL;
1388 }
NeilBrown9d09e662011-01-13 20:00:02 +00001389 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001390 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001391 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001392 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001393 rs->ti->error = "Only one data_offset argument pair allowed";
1394 return -EINVAL;
1395 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001396 /* Ensure sensible data offset */
Heinz Mauelshagen75dd3b92016-06-15 22:27:08 +02001397 if (value < 0 ||
1398 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001399 rs->ti->error = "Bogus data_offset value";
1400 return -EINVAL;
1401 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001402 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001403 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001404 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001405 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001406 rs->ti->error = "Only one delta_disks argument pair allowed";
1407 return -EINVAL;
1408 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001409 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001410 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001411 rs->ti->error = "Too many delta_disk requested";
1412 return -EINVAL;
1413 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001414
1415 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001416 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001417 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001418 rs->ti->error = "Only one stripe_cache argument pair allowed";
1419 return -EINVAL;
1420 }
1421
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001422 if (!rt_is_raid456(rt)) {
1423 rs->ti->error = "Inappropriate argument: stripe_cache";
1424 return -EINVAL;
1425 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001426
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001427 rs->stripe_cache_entries = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001428 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001429 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001430 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1431 return -EINVAL;
1432 }
1433 if (value > INT_MAX) {
1434 rs->ti->error = "min_recovery_rate out of range";
1435 return -EINVAL;
1436 }
NeilBrown9d09e662011-01-13 20:00:02 +00001437 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001438 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Heinz Mauelshagenf15f64d2016-07-27 23:34:01 +02001439 if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001440 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1441 return -EINVAL;
1442 }
1443 if (value > INT_MAX) {
1444 rs->ti->error = "max_recovery_rate out of range";
1445 return -EINVAL;
1446 }
NeilBrown9d09e662011-01-13 20:00:02 +00001447 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001448 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001449 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001450 rs->ti->error = "Only one region_size argument pair allowed";
1451 return -EINVAL;
1452 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001453
Jonathan Brassowc1084562011-08-02 12:32:07 +01001454 region_size = value;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02001455 rs->requested_bitmap_chunk_sectors = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001456 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001457 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001458 rs->ti->error = "Only one raid10_copies argument pair allowed";
1459 return -EINVAL;
1460 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001461
Mike Snitzerbb91a632016-06-02 12:06:54 -04001462 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001463 rs->ti->error = "Bad value for 'raid10_copies'";
1464 return -EINVAL;
1465 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001466
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001467 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001468 } else {
1469 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001470 rs->ti->error = "Unable to parse RAID parameter";
1471 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001472 }
1473 }
1474
Heinz Mauelshagen0d851d12016-06-15 18:43:55 +02001475 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
1476 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
1477 rs->ti->error = "sync and nosync are mutually exclusive";
1478 return -EINVAL;
1479 }
1480
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02001481 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
1482 (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
1483 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
1484 rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
1485 return -EINVAL;
1486 }
1487
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001488 if (write_mostly >= rs->md.raid_disks) {
1489 rs->ti->error = "Can't set all raid1 devices to write_mostly";
1490 return -EINVAL;
1491 }
1492
Jonathan Brassowc1084562011-08-02 12:32:07 +01001493 if (validate_region_size(rs, region_size))
1494 return -EINVAL;
1495
1496 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001497 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001498 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001499 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001500
Mike Snitzer542f9032012-07-27 15:08:00 +01001501 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1502 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001503
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001504 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001505 if (raid10_copies > rs->md.raid_disks) {
1506 rs->ti->error = "Not enough devices to satisfy specification";
1507 return -EINVAL;
1508 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001509
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001510 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001511 if (rs->md.new_layout < 0) {
1512 rs->ti->error = "Error getting raid10 format";
1513 return rs->md.new_layout;
1514 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001515
1516 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001517 if (!rt) {
1518 rs->ti->error = "Failed to recognize new raid10 layout";
1519 return -EINVAL;
1520 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001521
1522 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1523 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001524 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001525 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1526 return -EINVAL;
1527 }
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001528 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001529
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001530 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001531
NeilBrown9d09e662011-01-13 20:00:02 +00001532 /* Assume there are no metadata devices until the drives are parsed */
1533 rs->md.persistent = 0;
1534 rs->md.external = 1;
1535
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001536 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001537 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001538}
1539
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001540/* Set raid4/5/6 cache size */
1541static int rs_set_raid456_stripe_cache(struct raid_set *rs)
1542{
1543 int r;
1544 struct r5conf *conf;
1545 struct mddev *mddev = &rs->md;
1546 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
1547 uint32_t nr_stripes = rs->stripe_cache_entries;
1548
1549 if (!rt_is_raid456(rs->raid_type)) {
1550 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
1551 return -EINVAL;
1552 }
1553
1554 if (nr_stripes < min_stripes) {
1555 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
1556 nr_stripes, min_stripes);
1557 nr_stripes = min_stripes;
1558 }
1559
1560 conf = mddev->private;
1561 if (!conf) {
1562 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
1563 return -EINVAL;
1564 }
1565
1566 /* Try setting number of stripes in raid456 stripe cache */
1567 if (conf->min_nr_stripes != nr_stripes) {
1568 r = raid5_set_cache_size(mddev, nr_stripes);
1569 if (r) {
1570 rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
1571 return r;
1572 }
1573
1574 DMINFO("%u stripe cache entries", nr_stripes);
1575 }
1576
1577 return 0;
1578}
1579
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001580/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1581static unsigned int mddev_data_stripes(struct raid_set *rs)
1582{
1583 return rs->md.raid_disks - rs->raid_type->parity_devs;
1584}
1585
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001586/* Return # of data stripes of @rs (i.e. as of ctr) */
1587static unsigned int rs_data_stripes(struct raid_set *rs)
1588{
1589 return rs->raid_disks - rs->raid_type->parity_devs;
1590}
1591
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001592/*
1593 * Retrieve rdev->sectors from any valid raid device of @rs
1594 * to allow userpace to pass in arbitray "- -" device tupples.
1595 */
1596static sector_t __rdev_sectors(struct raid_set *rs)
1597{
1598 int i;
1599
1600 for (i = 0; i < rs->md.raid_disks; i++) {
1601 struct md_rdev *rdev = &rs->dev[i].rdev;
1602
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001603 if (!test_bit(Journal, &rdev->flags) &&
1604 rdev->bdev && rdev->sectors)
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001605 return rdev->sectors;
1606 }
1607
Heinz Mauelshagen4d49f1b2017-06-30 15:45:58 +02001608 return 0;
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001609}
1610
Heinz Mauelshagen188a2122017-12-02 01:03:59 +01001611/* Check that calculated dev_sectors fits all component devices. */
1612static int _check_data_dev_sectors(struct raid_set *rs)
1613{
1614 sector_t ds = ~0;
1615 struct md_rdev *rdev;
1616
1617 rdev_for_each(rdev, &rs->md)
1618 if (!test_bit(Journal, &rdev->flags) && rdev->bdev) {
1619 ds = min(ds, to_sector(i_size_read(rdev->bdev->bd_inode)));
1620 if (ds < rs->md.dev_sectors) {
1621 rs->ti->error = "Component device(s) too small";
1622 return -EINVAL;
1623 }
1624 }
1625
1626 return 0;
1627}
1628
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001629/* Calculate the sectors per device and per array used for @rs */
1630static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1631{
1632 int delta_disks;
1633 unsigned int data_stripes;
1634 struct mddev *mddev = &rs->md;
1635 struct md_rdev *rdev;
1636 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1637
1638 if (use_mddev) {
1639 delta_disks = mddev->delta_disks;
1640 data_stripes = mddev_data_stripes(rs);
1641 } else {
1642 delta_disks = rs->delta_disks;
1643 data_stripes = rs_data_stripes(rs);
1644 }
1645
1646 /* Special raid1 case w/o delta_disks support (yet) */
1647 if (rt_is_raid1(rs->raid_type))
1648 ;
1649 else if (rt_is_raid10(rs->raid_type)) {
1650 if (rs->raid10_copies < 2 ||
1651 delta_disks < 0) {
1652 rs->ti->error = "Bogus raid10 data copies or delta disks";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001653 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001654 }
1655
1656 dev_sectors *= rs->raid10_copies;
1657 if (sector_div(dev_sectors, data_stripes))
1658 goto bad;
1659
1660 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1661 if (sector_div(array_sectors, rs->raid10_copies))
1662 goto bad;
1663
1664 } else if (sector_div(dev_sectors, data_stripes))
1665 goto bad;
1666
1667 else
1668 /* Striped layouts */
1669 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1670
1671 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001672 if (!test_bit(Journal, &rdev->flags))
1673 rdev->sectors = dev_sectors;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001674
1675 mddev->array_sectors = array_sectors;
1676 mddev->dev_sectors = dev_sectors;
1677
Heinz Mauelshagen188a2122017-12-02 01:03:59 +01001678 return _check_data_dev_sectors(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001679bad:
1680 rs->ti->error = "Target length not divisible by number of data devices";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001681 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001682}
1683
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001684/* Setup recovery on @rs */
1685static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1686{
1687 /* raid0 does not recover */
1688 if (rs_is_raid0(rs))
1689 rs->md.recovery_cp = MaxSector;
1690 /*
1691 * A raid6 set has to be recovered either
1692 * completely or for the grown part to
1693 * ensure proper parity and Q-Syndrome
1694 */
1695 else if (rs_is_raid6(rs))
1696 rs->md.recovery_cp = dev_sectors;
1697 /*
1698 * Other raid set types may skip recovery
1699 * depending on the 'nosync' flag.
1700 */
1701 else
1702 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
1703 ? MaxSector : dev_sectors;
1704}
1705
1706/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
1707static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1708{
1709 if (!dev_sectors)
1710 /* New raid set or 'sync' flag provided */
1711 __rs_setup_recovery(rs, 0);
1712 else if (dev_sectors == MaxSector)
1713 /* Prevent recovery */
1714 __rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001715 else if (__rdev_sectors(rs) < dev_sectors)
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001716 /* Grown raid set */
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001717 __rs_setup_recovery(rs, __rdev_sectors(rs));
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001718 else
1719 __rs_setup_recovery(rs, MaxSector);
1720}
1721
NeilBrown9d09e662011-01-13 20:00:02 +00001722static void do_table_event(struct work_struct *ws)
1723{
1724 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1725
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001726 smp_rmb(); /* Make sure we access most actual mddev properties */
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01001727 if (!rs_is_reshaping(rs)) {
1728 if (rs_is_raid10(rs))
1729 rs_set_rdev_sectors(rs);
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001730 rs_set_capacity(rs);
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01001731 }
NeilBrown9d09e662011-01-13 20:00:02 +00001732 dm_table_event(rs->ti->table);
1733}
1734
1735static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1736{
1737 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1738
NeilBrown5c675f82014-12-15 12:56:56 +11001739 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001740}
1741
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001742/*
1743 * Make sure a valid takover (level switch) is being requested on @rs
1744 *
1745 * Conversions of raid sets from one MD personality to another
1746 * have to conform to restrictions which are enforced here.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001747 */
1748static int rs_check_takeover(struct raid_set *rs)
1749{
1750 struct mddev *mddev = &rs->md;
1751 unsigned int near_copies;
1752
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001753 if (rs->md.degraded) {
1754 rs->ti->error = "Can't takeover degraded raid set";
1755 return -EPERM;
1756 }
1757
1758 if (rs_is_reshaping(rs)) {
1759 rs->ti->error = "Can't takeover reshaping raid set";
1760 return -EPERM;
1761 }
1762
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001763 switch (mddev->level) {
1764 case 0:
1765 /* raid0 -> raid1/5 with one disk */
1766 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1767 mddev->raid_disks == 1)
1768 return 0;
1769
1770 /* raid0 -> raid10 */
1771 if (mddev->new_level == 10 &&
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001772 !(rs->raid_disks % mddev->raid_disks))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001773 return 0;
1774
1775 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001776 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001777 mddev->new_layout == ALGORITHM_PARITY_N &&
1778 mddev->raid_disks > 1)
1779 return 0;
1780
1781 break;
1782
1783 case 10:
1784 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001785 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001786 break;
1787
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001788 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001789
1790 /* raid10* -> raid0 */
1791 if (mddev->new_level == 0) {
1792 /* Can takeover raid10_near with raid disks divisable by data copies! */
1793 if (near_copies > 1 &&
1794 !(mddev->raid_disks % near_copies)) {
1795 mddev->raid_disks /= near_copies;
1796 mddev->delta_disks = mddev->raid_disks;
1797 return 0;
1798 }
1799
1800 /* Can takeover raid10_far */
1801 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001802 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001803 return 0;
1804
1805 break;
1806 }
1807
1808 /* raid10_{near,far} -> raid1 */
1809 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001810 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001811 return 0;
1812
1813 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001814 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001815 mddev->raid_disks == 2)
1816 return 0;
1817 break;
1818
1819 case 1:
1820 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001821 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001822 mddev->raid_disks == 2) {
1823 mddev->degraded = 1;
1824 return 0;
1825 }
1826
1827 /* raid1 -> raid0 */
1828 if (mddev->new_level == 0 &&
1829 mddev->raid_disks == 1)
1830 return 0;
1831
1832 /* raid1 -> raid10 */
1833 if (mddev->new_level == 10)
1834 return 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001835 break;
1836
1837 case 4:
1838 /* raid4 -> raid0 */
1839 if (mddev->new_level == 0)
1840 return 0;
1841
1842 /* raid4 -> raid1/5 with 2 disks */
1843 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1844 mddev->raid_disks == 2)
1845 return 0;
1846
1847 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001848 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001849 mddev->layout == ALGORITHM_PARITY_N)
1850 return 0;
1851 break;
1852
1853 case 5:
1854 /* raid5 with parity N -> raid0 */
1855 if (mddev->new_level == 0 &&
1856 mddev->layout == ALGORITHM_PARITY_N)
1857 return 0;
1858
1859 /* raid5 with parity N -> raid4 */
1860 if (mddev->new_level == 4 &&
1861 mddev->layout == ALGORITHM_PARITY_N)
1862 return 0;
1863
1864 /* raid5 with 2 disks -> raid1/4/10 */
1865 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1866 mddev->raid_disks == 2)
1867 return 0;
1868
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001869 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001870 if (mddev->new_level == 6 &&
1871 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001872 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001873 return 0;
1874 break;
1875
1876 case 6:
1877 /* raid6 with parity N -> raid0 */
1878 if (mddev->new_level == 0 &&
1879 mddev->layout == ALGORITHM_PARITY_N)
1880 return 0;
1881
1882 /* raid6 with parity N -> raid4 */
1883 if (mddev->new_level == 4 &&
1884 mddev->layout == ALGORITHM_PARITY_N)
1885 return 0;
1886
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001887 /* raid6_*_n with Q-Syndrome N -> raid5_* */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001888 if (mddev->new_level == 5 &&
1889 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001890 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001891 return 0;
1892
1893 default:
1894 break;
1895 }
1896
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001897 rs->ti->error = "takeover not possible";
1898 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001899}
1900
1901/* True if @rs requested to be taken over */
1902static bool rs_takeover_requested(struct raid_set *rs)
1903{
1904 return rs->md.new_level != rs->md.level;
1905}
1906
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001907/* True if @rs is requested to reshape by ctr */
1908static bool rs_reshape_requested(struct raid_set *rs)
1909{
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001910 bool change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001911 struct mddev *mddev = &rs->md;
1912
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001913 if (rs_takeover_requested(rs))
1914 return false;
1915
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01001916 if (rs_is_raid0(rs))
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001917 return false;
1918
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001919 change = mddev->new_layout != mddev->layout ||
1920 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1921 rs->delta_disks;
1922
1923 /* Historical case to support raid1 reshape without delta disks */
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01001924 if (rs_is_raid1(rs)) {
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001925 if (rs->delta_disks)
1926 return !!rs->delta_disks;
1927
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001928 return !change &&
1929 mddev->raid_disks != rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001930 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001931
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01001932 if (rs_is_raid10(rs))
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001933 return change &&
1934 !__is_raid10_far(mddev->new_layout) &&
1935 rs->delta_disks >= 0;
1936
1937 return change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001938}
1939
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001940/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001941#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001942
1943/* State flags for sb->flags */
1944#define SB_FLAG_RESHAPE_ACTIVE 0x1
1945#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1946
NeilBrown9d09e662011-01-13 20:00:02 +00001947/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001948 * This structure is never routinely used by userspace, unlike md superblocks.
1949 * Devices with this superblock should only ever be accessed via device-mapper.
1950 */
1951#define DM_RAID_MAGIC 0x64526D44
1952struct dm_raid_superblock {
1953 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001954 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001955
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001956 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1957 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001958
1959 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001960 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001961 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001962
1963 /*
1964 * This offset tracks the progress of the repair or replacement of
1965 * an individual drive.
1966 */
1967 __le64 disk_recovery_offset;
1968
1969 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001970 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001971 * synchronisation/parity calculation.
1972 */
1973 __le64 array_resync_offset;
1974
1975 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001976 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001977 */
1978 __le32 level;
1979 __le32 layout;
1980 __le32 stripe_sectors;
1981
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001982 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001983 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001984 *
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02001985 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001986 */
1987
1988 __le32 flags; /* Flags defining array states for reshaping */
1989
1990 /*
1991 * This offset tracks the progress of a raid
1992 * set reshape in order to be able to restart it
1993 */
1994 __le64 reshape_position;
1995
1996 /*
1997 * These define the properties of the array in case of an interrupted reshape
1998 */
1999 __le32 new_level;
2000 __le32 new_layout;
2001 __le32 new_stripe_sectors;
2002 __le32 delta_disks;
2003
2004 __le64 array_sectors; /* Array size in sectors */
2005
2006 /*
2007 * Sector offsets to data on devices (reshaping).
2008 * Needed to support out of place reshaping, thus
2009 * not writing over any stripes whilst converting
2010 * them from old to new layout
2011 */
2012 __le64 data_offset;
2013 __le64 new_data_offset;
2014
2015 __le64 sectors; /* Used device size in sectors */
2016
2017 /*
2018 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002019 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002020 */
2021 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
2022
2023 __le32 incompat_features; /* Used to indicate any incompatible features */
2024
2025 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002026} __packed;
2027
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002028/*
2029 * Check for reshape constraints on raid set @rs:
2030 *
2031 * - reshape function non-existent
2032 * - degraded set
2033 * - ongoing recovery
2034 * - ongoing reshape
2035 *
2036 * Returns 0 if none or -EPERM if given constraint
2037 * and error message reference in @errmsg
2038 */
2039static int rs_check_reshape(struct raid_set *rs)
2040{
2041 struct mddev *mddev = &rs->md;
2042
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002043 if (!mddev->pers || !mddev->pers->check_reshape)
2044 rs->ti->error = "Reshape not supported";
2045 else if (mddev->degraded)
2046 rs->ti->error = "Can't reshape degraded raid set";
2047 else if (rs_is_recovering(rs))
2048 rs->ti->error = "Convert request on recovering raid set prohibited";
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002049 else if (rs_is_reshaping(rs))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002050 rs->ti->error = "raid set already reshaping!";
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002051 else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
2052 rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002053 else
2054 return 0;
2055
2056 return -EPERM;
2057}
2058
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002059static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002060{
2061 BUG_ON(!rdev->sb_page);
2062
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002063 if (rdev->sb_loaded && !force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002064 return 0;
2065
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002066 rdev->sb_loaded = 0;
2067
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02002068 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01002069 DMERR("Failed to read superblock of device at position %d",
2070 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10002071 md_error(rdev->mddev, rdev);
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002072 set_bit(Faulty, &rdev->flags);
2073 return -EIO;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002074 }
2075
2076 rdev->sb_loaded = 1;
2077
2078 return 0;
2079}
2080
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002081static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2082{
2083 failed_devices[0] = le64_to_cpu(sb->failed_devices);
2084 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
2085
Mike Snitzer42863252016-06-02 12:27:46 -04002086 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002087 int i = ARRAY_SIZE(sb->extended_failed_devices);
2088
2089 while (i--)
2090 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
2091 }
2092}
2093
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002094static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2095{
2096 int i = ARRAY_SIZE(sb->extended_failed_devices);
2097
2098 sb->failed_devices = cpu_to_le64(failed_devices[0]);
2099 while (i--)
2100 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
2101}
2102
2103/*
2104 * Synchronize the superblock members with the raid set properties
2105 *
2106 * All superblock data is little endian.
2107 */
NeilBrownfd01b882011-10-11 16:47:53 +11002108static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002109{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002110 bool update_failed_devices = false;
2111 unsigned int i;
2112 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002113 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10002114 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002115
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002116 /* No metadata device, no superblock */
2117 if (!rdev->meta_bdev)
2118 return;
2119
2120 BUG_ON(!rdev->sb_page);
2121
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002122 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002123
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002124 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002125
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002126 for (i = 0; i < rs->raid_disks; i++)
2127 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
2128 update_failed_devices = true;
2129 set_bit(i, (void *) failed_devices);
2130 }
2131
2132 if (update_failed_devices)
2133 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002134
2135 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002136 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002137
2138 sb->num_devices = cpu_to_le32(mddev->raid_disks);
2139 sb->array_position = cpu_to_le32(rdev->raid_disk);
2140
2141 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002142
2143 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
2144 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
2145
2146 sb->level = cpu_to_le32(mddev->level);
2147 sb->layout = cpu_to_le32(mddev->layout);
2148 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002149
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02002150 /********************************************************************
2151 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
2152 *
2153 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
2154 */
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002155 sb->new_level = cpu_to_le32(mddev->new_level);
2156 sb->new_layout = cpu_to_le32(mddev->new_layout);
2157 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
2158
2159 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2160
2161 smp_rmb(); /* Make sure we access most recent reshape position */
2162 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2163 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
2164 /* Flag ongoing reshape */
2165 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
2166
2167 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
2168 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04002169 } else {
2170 /* Clear reshape flags */
2171 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
2172 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002173
2174 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
2175 sb->data_offset = cpu_to_le64(rdev->data_offset);
2176 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
2177 sb->sectors = cpu_to_le64(rdev->sectors);
Heinz Mauelshagenb2a48722016-08-03 17:47:04 +02002178 sb->incompat_features = cpu_to_le32(0);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002179
2180 /* Zero out the rest of the payload after the size of the superblock */
2181 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002182}
2183
2184/*
2185 * super_load
2186 *
2187 * This function creates a superblock if one is not found on the device
2188 * and will decide which superblock to use if there's a choice.
2189 *
2190 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
2191 */
NeilBrown3cb03002011-10-11 16:45:26 +11002192static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002193{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002194 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002195 struct dm_raid_superblock *sb;
2196 struct dm_raid_superblock *refsb;
2197 uint64_t events_sb, events_refsb;
2198
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002199 r = read_disk_sb(rdev, rdev->sb_size, false);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002200 if (r)
2201 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002202
2203 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00002204
2205 /*
2206 * Two cases that we want to write new superblocks and rebuild:
2207 * 1) New device (no matching magic number)
2208 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
2209 */
2210 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
2211 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002212 super_sync(rdev->mddev, rdev);
2213
2214 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002215 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002216
2217 /* Force writing of superblocks to disk */
Shaohua Li29530792016-12-08 15:48:19 -08002218 set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002219
2220 /* Any superblock is better than none, choose that if given */
2221 return refdev ? 0 : 1;
2222 }
2223
2224 if (!refdev)
2225 return 1;
2226
2227 events_sb = le64_to_cpu(sb->events);
2228
2229 refsb = page_address(refdev->sb_page);
2230 events_refsb = le64_to_cpu(refsb->events);
2231
2232 return (events_sb > events_refsb) ? 1 : 0;
2233}
2234
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002235static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002236{
2237 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002238 unsigned int d;
2239 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002240 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002241 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002242 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002243 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002244 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002245 struct dm_raid_superblock *sb2;
2246
2247 sb = page_address(rdev->sb_page);
2248 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002249
2250 /*
2251 * Initialise to 1 if this is a new superblock.
2252 */
2253 mddev->events = events_sb ? : 1;
2254
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002255 mddev->reshape_position = MaxSector;
2256
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002257 mddev->raid_disks = le32_to_cpu(sb->num_devices);
2258 mddev->level = le32_to_cpu(sb->level);
2259 mddev->layout = le32_to_cpu(sb->layout);
2260 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
2261
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002262 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002263 * Reshaping is supported, e.g. reshape_position is valid
2264 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002265 */
Mike Snitzer42863252016-06-02 12:27:46 -04002266 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002267 /* Superblock is authoritative wrt given raid set layout! */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002268 mddev->new_level = le32_to_cpu(sb->new_level);
2269 mddev->new_layout = le32_to_cpu(sb->new_layout);
2270 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
2271 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
2272 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
2273
2274 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04002275 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
2276 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002277 DMERR("Reshape requested but raid set is still reshaping");
2278 return -EINVAL;
2279 }
2280
2281 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04002282 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002283 mddev->reshape_backwards = 1;
2284 else
2285 mddev->reshape_backwards = 0;
2286
2287 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
2288 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
2289 }
2290
2291 } else {
2292 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002293 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002294 */
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002295 struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
2296 struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002297
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002298 if (rs_takeover_requested(rs)) {
2299 if (rt_cur && rt_new)
2300 DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
2301 rt_cur->name, rt_new->name);
2302 else
2303 DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
2304 return -EINVAL;
2305 } else if (rs_reshape_requested(rs)) {
2306 DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
2307 if (mddev->layout != mddev->new_layout) {
2308 if (rt_cur && rt_new)
2309 DMERR(" current layout %s vs new layout %s",
2310 rt_cur->name, rt_new->name);
2311 else
2312 DMERR(" current layout 0x%X vs new layout 0x%X",
2313 le32_to_cpu(sb->layout), mddev->new_layout);
2314 }
2315 if (mddev->chunk_sectors != mddev->new_chunk_sectors)
2316 DMERR(" current stripe sectors %u vs new stripe sectors %u",
2317 mddev->chunk_sectors, mddev->new_chunk_sectors);
2318 if (rs->delta_disks)
2319 DMERR(" current %u disks vs new %u disks",
2320 mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
2321 if (rs_is_raid10(rs)) {
2322 DMERR(" Old layout: %s w/ %u copies",
2323 raid10_md_layout_to_format(mddev->layout),
2324 raid10_md_layout_to_copies(mddev->layout));
2325 DMERR(" New layout: %s w/ %u copies",
2326 raid10_md_layout_to_format(mddev->new_layout),
2327 raid10_md_layout_to_copies(mddev->new_layout));
2328 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002329 return -EINVAL;
2330 }
2331
Heinz Mauelshagenb052b072016-10-17 21:20:07 +02002332 DMINFO("Discovered old metadata format; upgrading to extended metadata format");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002333 }
2334
Mike Snitzer42863252016-06-02 12:27:46 -04002335 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002336 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
2337
2338 /*
2339 * During load, we set FirstUse if a new superblock was written.
2340 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002341 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04002342 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002343 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002344 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002345 * and the new device needs to be rebuilt - in which
2346 * case the In_sync bit will /not/ be set and
2347 * recovery_cp must be MaxSector.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002348 * 3) This is/are a new device(s) being added to an old
2349 * raid set during takeover to a higher raid level
2350 * to provide capacity for redundancy or during reshape
2351 * to add capacity to grow the raid set.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002352 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002353 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002354 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002355 if (test_bit(Journal, &rdev->flags))
2356 continue;
2357
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002358 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002359 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002360
2361 if (!test_bit(In_sync, &r->flags)) {
2362 DMINFO("Device %d specified for rebuild; clearing superblock",
2363 r->raid_disk);
2364 rebuilds++;
2365
2366 if (test_bit(FirstUse, &r->flags))
2367 rebuild_and_new++;
2368 }
2369
2370 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002371 }
2372
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002373 if (new_devs == rs->raid_disks || !rebuilds) {
2374 /* Replace a broken device */
2375 if (new_devs == 1 && !rs->delta_disks)
2376 ;
2377 if (new_devs == rs->raid_disks) {
2378 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002379 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002380 } else if (new_devs != rebuilds &&
2381 new_devs != rs->delta_disks) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002382 DMERR("New device injected into existing raid set without "
2383 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002384 return -EINVAL;
2385 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002386 } else if (new_devs && new_devs != rebuilds) {
2387 DMERR("%u 'rebuild' devices cannot be injected into"
2388 " a raid set with %u other first-time devices",
2389 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002390 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002391 } else if (rebuilds) {
2392 if (rebuild_and_new && rebuilds != rebuild_and_new) {
2393 DMERR("new device%s provided without 'rebuild'",
2394 new_devs > 1 ? "s" : "");
2395 return -EINVAL;
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01002396 } else if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) && rs_is_recovering(rs)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002397 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
2398 (unsigned long long) mddev->recovery_cp);
2399 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002400 } else if (rs_is_reshaping(rs)) {
2401 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
2402 (unsigned long long) mddev->reshape_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002403 return -EINVAL;
2404 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002405 }
2406
2407 /*
2408 * Now we set the Faulty bit for those devices that are
2409 * recorded in the superblock as failed.
2410 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002411 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11002412 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002413 if (test_bit(Journal, &rdev->flags) ||
2414 !r->sb_page)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002415 continue;
2416 sb2 = page_address(r->sb_page);
2417 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002418 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002419
2420 /*
2421 * Check for any device re-ordering.
2422 */
2423 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2424 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002425 if (role < 0)
2426 continue;
2427
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002428 if (role != r->raid_disk) {
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002429 if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002430 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002431 rs->raid_disks % rs->raid10_copies) {
2432 rs->ti->error =
2433 "Cannot change raid10 near set to odd # of devices!";
2434 return -EINVAL;
2435 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002436
2437 sb2->array_position = cpu_to_le32(r->raid_disk);
2438
2439 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002440 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2441 !rt_is_raid1(rs->raid_type)) {
2442 rs->ti->error = "Cannot change device positions in raid set";
2443 return -EINVAL;
2444 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002445
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002446 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002447 }
2448
2449 /*
2450 * Partial recovery is performed on
2451 * returning failed devices.
2452 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002453 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002454 set_bit(Faulty, &r->flags);
2455 }
2456 }
2457
2458 return 0;
2459}
2460
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002461static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002462{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002463 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002464 struct dm_raid_superblock *sb;
2465
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002466 if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002467 return 0;
2468
2469 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002470
2471 /*
2472 * If mddev->events is not set, we know we have not yet initialized
2473 * the array.
2474 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002475 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002476 return -EINVAL;
2477
Andy Whitcroft5c336772016-10-11 15:16:57 +01002478 if (le32_to_cpu(sb->compat_features) &&
2479 le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002480 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2481 return -EINVAL;
2482 }
2483
2484 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002485 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002486 return -EINVAL;
2487 }
2488
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002489 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002490 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen977f1a02017-01-14 03:53:11 +01002491 mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002492
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002493 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
Heinz Mauelshagenc4d097d2017-06-23 17:27:01 +02002494 /*
2495 * Retrieve rdev size stored in superblock to be prepared for shrink.
2496 * Check extended superblock members are present otherwise the size
2497 * will not be set!
2498 */
2499 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190)
2500 rdev->sectors = le64_to_cpu(sb->sectors);
2501
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002502 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002503 if (rdev->recovery_offset == MaxSector)
2504 set_bit(In_sync, &rdev->flags);
2505 /*
2506 * If no reshape in progress -> we're recovering single
2507 * disk(s) and have to set the device(s) to out-of-sync
2508 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002509 else if (!rs_is_reshaping(rs))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002510 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002511 }
2512
2513 /*
2514 * If a device comes back, set it as not In_sync and no longer faulty.
2515 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002516 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2517 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002518 clear_bit(In_sync, &rdev->flags);
2519 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002520 }
2521
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002522 /* Reshape support -> restore repective data offsets */
2523 rdev->data_offset = le64_to_cpu(sb->data_offset);
2524 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002525
2526 return 0;
2527}
2528
2529/*
2530 * Analyse superblocks and select the freshest.
2531 */
2532static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2533{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002534 int r;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002535 struct md_rdev *rdev, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002536 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002537
2538 freshest = NULL;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002539 rdev_for_each(rdev, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002540 if (test_bit(Journal, &rdev->flags))
2541 continue;
2542
Heinz Mauelshagen23397842017-11-02 19:58:28 +01002543 if (!rdev->meta_bdev)
2544 continue;
2545
2546 /* Set superblock offset/size for metadata device. */
2547 rdev->sb_start = 0;
2548 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
2549 if (rdev->sb_size < sizeof(struct dm_raid_superblock) || rdev->sb_size > PAGE_SIZE) {
2550 DMERR("superblock size of a logical block is no longer valid");
2551 return -EINVAL;
2552 }
2553
Jonathan Brassow761becf2012-10-11 13:42:19 +11002554 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002555 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002556 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002557 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002558 * of the "sync" directive.
2559 *
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002560 * With reshaping capability added, we must ensure that
2561 * that the "sync" directive is disallowed during the reshape.
Jonathan Brassow761becf2012-10-11 13:42:19 +11002562 */
Mike Snitzer42863252016-06-02 12:27:46 -04002563 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002564 continue;
2565
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002566 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002567
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002568 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002569 case 1:
2570 freshest = rdev;
2571 break;
2572 case 0:
2573 break;
2574 default:
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002575 /* This is a failure to read the superblock from the metadata device. */
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02002576 /*
2577 * We have to keep any raid0 data/metadata device pairs or
2578 * the MD raid0 personality will fail to start the array.
2579 */
2580 if (rs_is_raid0(rs))
2581 continue;
2582
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002583 /*
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002584 * We keep the dm_devs to be able to emit the device tuple
2585 * properly on the table line in raid_status() (rather than
2586 * mistakenly acting as if '- -' got passed into the constructor).
2587 *
2588 * The rdev has to stay on the same_set list to allow for
2589 * the attempt to restore faulty devices on second resume.
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002590 */
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002591 rdev->raid_disk = rdev->saved_raid_disk = -1;
2592 break;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002593 }
2594 }
2595
2596 if (!freshest)
2597 return 0;
2598
2599 /*
2600 * Validation of the freshest device provides the source of
2601 * validation for the remaining devices.
2602 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002603 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2604 if (super_validate(rs, freshest))
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002605 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002606
Heinz Mauelshagenf4af3f82017-07-13 17:36:12 +02002607 if (validate_raid_redundancy(rs)) {
2608 rs->ti->error = "Insufficient redundancy to activate array";
2609 return -EINVAL;
2610 }
2611
NeilBrowndafb20f2012-03-19 12:46:39 +11002612 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002613 if (!test_bit(Journal, &rdev->flags) &&
2614 rdev != freshest &&
2615 super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002616 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002617 return 0;
2618}
2619
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002620/*
2621 * Adjust data_offset and new_data_offset on all disk members of @rs
2622 * for out of place reshaping if requested by contructor
2623 *
2624 * We need free space at the beginning of each raid disk for forward
2625 * and at the end for backward reshapes which userspace has to provide
2626 * via remapping/reordering of space.
2627 */
2628static int rs_adjust_data_offsets(struct raid_set *rs)
2629{
2630 sector_t data_offset = 0, new_data_offset = 0;
2631 struct md_rdev *rdev;
2632
2633 /* Constructor did not request data offset change */
2634 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2635 if (!rs_is_reshapable(rs))
2636 goto out;
2637
2638 return 0;
2639 }
2640
2641 /* HM FIXME: get InSync raid_dev? */
2642 rdev = &rs->dev[0].rdev;
2643
2644 if (rs->delta_disks < 0) {
2645 /*
2646 * Removing disks (reshaping backwards):
2647 *
2648 * - before reshape: data is at offset 0 and free space
2649 * is at end of each component LV
2650 *
2651 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2652 */
2653 data_offset = 0;
2654 new_data_offset = rs->data_offset;
2655
2656 } else if (rs->delta_disks > 0) {
2657 /*
2658 * Adding disks (reshaping forwards):
2659 *
2660 * - before reshape: data is at offset rs->data_offset != 0 and
2661 * free space is at begin of each component LV
2662 *
2663 * - after reshape: data is at offset 0 on each component LV
2664 */
2665 data_offset = rs->data_offset;
2666 new_data_offset = 0;
2667
2668 } else {
2669 /*
2670 * User space passes in 0 for data offset after having removed reshape space
2671 *
2672 * - or - (data offset != 0)
2673 *
2674 * Changing RAID layout or chunk size -> toggle offsets
2675 *
2676 * - before reshape: data is at offset rs->data_offset 0 and
2677 * free space is at end of each component LV
2678 * -or-
2679 * data is at offset rs->data_offset != 0 and
2680 * free space is at begin of each component LV
2681 *
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002682 * - after reshape: data is at offset 0 if it was at offset != 0
2683 * or at offset != 0 if it was at offset 0
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002684 * on each component LV
2685 *
2686 */
2687 data_offset = rs->data_offset ? rdev->data_offset : 0;
2688 new_data_offset = data_offset ? 0 : rs->data_offset;
2689 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2690 }
2691
2692 /*
2693 * Make sure we got a minimum amount of free sectors per device
2694 */
2695 if (rs->data_offset &&
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002696 to_sector(i_size_read(rdev->bdev->bd_inode)) - rs->md.dev_sectors < MIN_FREE_RESHAPE_SPACE) {
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002697 rs->ti->error = data_offset ? "No space for forward reshape" :
2698 "No space for backward reshape";
2699 return -ENOSPC;
2700 }
2701out:
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002702 /*
2703 * Raise recovery_cp in case data_offset != 0 to
2704 * avoid false recovery positives in the constructor.
2705 */
2706 if (rs->md.recovery_cp < rs->md.dev_sectors)
2707 rs->md.recovery_cp += rs->dev[0].rdev.data_offset;
2708
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002709 /* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002710 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002711 if (!test_bit(Journal, &rdev->flags)) {
2712 rdev->data_offset = data_offset;
2713 rdev->new_data_offset = new_data_offset;
2714 }
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002715 }
2716
2717 return 0;
2718}
2719
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002720/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002721static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002722{
2723 int i = 0;
2724 struct md_rdev *rdev;
2725
2726 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002727 if (!test_bit(Journal, &rdev->flags)) {
2728 rdev->raid_disk = i++;
2729 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2730 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002731 }
2732}
2733
2734/*
2735 * Setup @rs for takeover by a different raid level
2736 */
2737static int rs_setup_takeover(struct raid_set *rs)
2738{
2739 struct mddev *mddev = &rs->md;
2740 struct md_rdev *rdev;
2741 unsigned int d = mddev->raid_disks = rs->raid_disks;
2742 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2743
2744 if (rt_is_raid10(rs->raid_type)) {
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01002745 if (rs_is_raid0(rs)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002746 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002747 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002748
2749 /* raid0 -> raid10_far layout */
2750 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2751 rs->raid10_copies);
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01002752 } else if (rs_is_raid1(rs))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002753 /* raid1 -> raid10_near layout */
2754 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2755 rs->raid_disks);
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002756 else
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002757 return -EINVAL;
2758
2759 }
2760
2761 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2762 mddev->recovery_cp = MaxSector;
2763
2764 while (d--) {
2765 rdev = &rs->dev[d].rdev;
2766
2767 if (test_bit(d, (void *) rs->rebuild_disks)) {
2768 clear_bit(In_sync, &rdev->flags);
2769 clear_bit(Faulty, &rdev->flags);
2770 mddev->recovery_cp = rdev->recovery_offset = 0;
2771 /* Bitmap has to be created when we do an "up" takeover */
2772 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2773 }
2774
2775 rdev->new_data_offset = new_data_offset;
2776 }
2777
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002778 return 0;
2779}
2780
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002781/* Prepare @rs for reshape */
2782static int rs_prepare_reshape(struct raid_set *rs)
2783{
2784 bool reshape;
2785 struct mddev *mddev = &rs->md;
2786
2787 if (rs_is_raid10(rs)) {
2788 if (rs->raid_disks != mddev->raid_disks &&
2789 __is_raid10_near(mddev->layout) &&
2790 rs->raid10_copies &&
2791 rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
2792 /*
2793 * raid disk have to be multiple of data copies to allow this conversion,
2794 *
2795 * This is actually not a reshape it is a
2796 * rebuild of any additional mirrors per group
2797 */
2798 if (rs->raid_disks % rs->raid10_copies) {
2799 rs->ti->error = "Can't reshape raid10 mirror groups";
2800 return -EINVAL;
2801 }
2802
2803 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
2804 __reorder_raid_disk_indexes(rs);
2805 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2806 rs->raid10_copies);
2807 mddev->new_layout = mddev->layout;
2808 reshape = false;
2809 } else
2810 reshape = true;
2811
2812 } else if (rs_is_raid456(rs))
2813 reshape = true;
2814
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002815 else if (rs_is_raid1(rs)) {
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002816 if (rs->delta_disks) {
2817 /* Process raid1 via delta_disks */
2818 mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
2819 reshape = true;
2820 } else {
2821 /* Process raid1 without delta_disks */
2822 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002823 reshape = false;
2824 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002825 } else {
2826 rs->ti->error = "Called with bogus raid type";
2827 return -EINVAL;
2828 }
2829
2830 if (reshape) {
2831 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2832 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002833 } else if (mddev->raid_disks < rs->raid_disks)
2834 /* Create new superblocks and bitmaps, if any new disks */
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002835 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002836
2837 return 0;
2838}
2839
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002840/* Get reshape sectors from data_offsets or raid set */
2841static sector_t _get_reshape_sectors(struct raid_set *rs)
2842{
2843 struct md_rdev *rdev;
2844 sector_t reshape_sectors = 0;
2845
2846 rdev_for_each(rdev, &rs->md)
2847 if (!test_bit(Journal, &rdev->flags)) {
2848 reshape_sectors = (rdev->data_offset > rdev->new_data_offset) ?
2849 rdev->data_offset - rdev->new_data_offset :
2850 rdev->new_data_offset - rdev->data_offset;
2851 break;
2852 }
2853
2854 return max(reshape_sectors, (sector_t) rs->data_offset);
2855}
2856
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002857/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002858 *
2859 * - change raid layout
2860 * - change chunk size
2861 * - add disks
2862 * - remove disks
2863 */
2864static int rs_setup_reshape(struct raid_set *rs)
2865{
2866 int r = 0;
2867 unsigned int cur_raid_devs, d;
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002868 sector_t reshape_sectors = _get_reshape_sectors(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002869 struct mddev *mddev = &rs->md;
2870 struct md_rdev *rdev;
2871
2872 mddev->delta_disks = rs->delta_disks;
2873 cur_raid_devs = mddev->raid_disks;
2874
2875 /* Ignore impossible layout change whilst adding/removing disks */
2876 if (mddev->delta_disks &&
2877 mddev->layout != mddev->new_layout) {
2878 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
2879 mddev->new_layout = mddev->layout;
2880 }
2881
2882 /*
2883 * Adjust array size:
2884 *
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002885 * - in case of adding disk(s), array size has
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002886 * to grow after the disk adding reshape,
2887 * which'll hapen in the event handler;
2888 * reshape will happen forward, so space has to
2889 * be available at the beginning of each disk
2890 *
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002891 * - in case of removing disk(s), array size
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002892 * has to shrink before starting the reshape,
2893 * which'll happen here;
2894 * reshape will happen backward, so space has to
2895 * be available at the end of each disk
2896 *
2897 * - data_offset and new_data_offset are
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002898 * adjusted for aforementioned out of place
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002899 * reshaping based on userspace passing in
2900 * the "data_offset <sectors>" key/value
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002901 * pair via the constructor
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002902 */
2903
2904 /* Add disk(s) */
2905 if (rs->delta_disks > 0) {
2906 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
2907 for (d = cur_raid_devs; d < rs->raid_disks; d++) {
2908 rdev = &rs->dev[d].rdev;
2909 clear_bit(In_sync, &rdev->flags);
2910
2911 /*
2912 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
2913 * by md, which'll store that erroneously in the superblock on reshape
2914 */
2915 rdev->saved_raid_disk = -1;
2916 rdev->raid_disk = d;
2917
2918 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002919 rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002920 }
2921
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002922 mddev->reshape_backwards = 0; /* adding disk(s) -> forward reshape */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002923
2924 /* Remove disk(s) */
2925 } else if (rs->delta_disks < 0) {
2926 r = rs_set_dev_and_array_sectors(rs, true);
2927 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
2928
2929 /* Change layout and/or chunk size */
2930 } else {
2931 /*
2932 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
2933 *
2934 * keeping number of disks and do layout change ->
2935 *
2936 * toggle reshape_backward depending on data_offset:
2937 *
2938 * - free space upfront -> reshape forward
2939 *
2940 * - free space at the end -> reshape backward
2941 *
2942 *
2943 * This utilizes free reshape space avoiding the need
2944 * for userspace to move (parts of) LV segments in
2945 * case of layout/chunksize change (for disk
2946 * adding/removing reshape space has to be at
2947 * the proper address (see above with delta_disks):
2948 *
2949 * add disk(s) -> begin
2950 * remove disk(s)-> end
2951 */
2952 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
2953 }
2954
Heinz Mauelshagen052b2b12017-12-02 01:03:49 +01002955 /*
2956 * Adjust device size for forward reshape
2957 * because md_finish_reshape() reduces it.
2958 */
2959 if (!mddev->reshape_backwards)
2960 rdev_for_each(rdev, &rs->md)
2961 if (!test_bit(Journal, &rdev->flags))
2962 rdev->sectors += reshape_sectors;
2963
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002964 return r;
2965}
2966
2967/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002968 * Enable/disable discard support on RAID set depending on
2969 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002970 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002971static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002972{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002973 int i;
2974 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002975 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002976
Christoph Hellwig48920ff2017-04-05 19:21:23 +02002977 /*
2978 * XXX: RAID level 4,5,6 require zeroing for safety.
2979 */
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01002980 raid456 = rs_is_raid456(rs);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002981
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02002982 for (i = 0; i < rs->raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002983 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002984
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002985 if (!rs->dev[i].rdev.bdev)
2986 continue;
2987
2988 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002989 if (!q || !blk_queue_discard(q))
2990 return;
2991
2992 if (raid456) {
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002993 if (!devices_handle_discard_safely) {
2994 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2995 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2996 return;
2997 }
2998 }
2999 }
3000
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003001 /*
3002 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02003003 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003004 */
Heinz Mauelshagen552aa672017-12-13 17:13:21 +01003005 ti->split_discard_bios = !!(rs_is_raid1(rs) || rs_is_raid10(rs));
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003006 ti->num_discard_bios = 1;
3007}
3008
3009/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003010 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00003011 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04003012 * <raid_type> <#raid_params> <raid_params>{0,} \
3013 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00003014 *
Mike Snitzer43157842016-05-30 13:03:37 -04003015 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00003016 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003017 *
3018 * Userspace is free to initialize the metadata devices, hence the superblocks to
3019 * enforce recreation based on the passed in table parameters.
3020 *
NeilBrown9d09e662011-01-13 20:00:02 +00003021 */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003022static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
NeilBrown9d09e662011-01-13 20:00:02 +00003023{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003024 int r;
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003025 bool resize = false;
NeilBrown9d09e662011-01-13 20:00:02 +00003026 struct raid_type *rt;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003027 unsigned int num_raid_params, num_raid_devs;
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003028 sector_t calculated_dev_sectors, rdev_sectors, reshape_sectors;
NeilBrown9d09e662011-01-13 20:00:02 +00003029 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003030 const char *arg;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003031 struct rs_layout rs_layout;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003032 struct dm_arg_set as = { argc, argv }, as_nrd;
3033 struct dm_arg _args[] = {
3034 { 0, as.argc, "Cannot understand number of raid parameters" },
3035 { 1, 254, "Cannot understand number of raid devices parameters" }
3036 };
NeilBrown9d09e662011-01-13 20:00:02 +00003037
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003038 /* Must have <raid_type> */
3039 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003040 if (!arg) {
3041 ti->error = "No arguments";
3042 return -EINVAL;
3043 }
NeilBrown9d09e662011-01-13 20:00:02 +00003044
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003045 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003046 if (!rt) {
3047 ti->error = "Unrecognised raid_type";
3048 return -EINVAL;
3049 }
NeilBrown9d09e662011-01-13 20:00:02 +00003050
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003051 /* Must have <#raid_params> */
3052 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04003053 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00003054
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003055 /* number of raid device tupples <meta_dev data_dev> */
3056 as_nrd = as;
3057 dm_consume_args(&as_nrd, num_raid_params);
3058 _args[1].max = (as_nrd.argc - 1) / 2;
3059 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04003060 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003061
Mike Snitzerbb91a632016-06-02 12:06:54 -04003062 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04003063 ti->error = "Invalid number of supplied raid devices";
3064 return -EINVAL;
3065 }
NeilBrown9d09e662011-01-13 20:00:02 +00003066
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003067 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00003068 if (IS_ERR(rs))
3069 return PTR_ERR(rs);
3070
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02003071 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003072 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00003073 goto bad;
3074
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02003075 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003076 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00003077 goto bad;
3078
Jonathan Brassowb12d4372011-08-02 12:32:07 +01003079 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003080
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003081 /*
3082 * Calculate ctr requested array and device sizes to allow
3083 * for superblock analysis needing device sizes defined.
3084 *
3085 * Any existing superblock will overwrite the array and device sizes
3086 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003087 r = rs_set_dev_and_array_sectors(rs, false);
3088 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003089 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003090
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01003091 calculated_dev_sectors = rs->md.dev_sectors;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003092
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003093 /*
3094 * Backup any new raid set level, layout, ...
3095 * requested to be able to compare to superblock
3096 * members for conversion decisions.
3097 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003098 rs_config_backup(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003099
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003100 r = analyse_superblocks(ti, rs);
3101 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01003102 goto bad;
3103
Heinz Mauelshagen4d49f1b2017-06-30 15:45:58 +02003104 rdev_sectors = __rdev_sectors(rs);
3105 if (!rdev_sectors) {
3106 ti->error = "Invalid rdev size";
3107 r = -EINVAL;
3108 goto bad;
3109 }
3110
Heinz Mauelshagen75015372017-12-02 01:03:50 +01003111
3112 reshape_sectors = _get_reshape_sectors(rs);
3113 if (calculated_dev_sectors != rdev_sectors)
3114 resize = calculated_dev_sectors != (reshape_sectors ? rdev_sectors - reshape_sectors : rdev_sectors);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003115
NeilBrown9d09e662011-01-13 20:00:02 +00003116 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00003117 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003118 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00003119
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003120 /* Restore any requested new layout for conversion decision */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003121 rs_config_restore(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003122
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003123 /*
3124 * Now that we have any superblock metadata available,
3125 * check for new, recovering, reshaping, to be taken over,
3126 * to be reshaped or an existing, unchanged raid set to
3127 * run in sequence.
3128 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003129 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003130 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
3131 if (rs_is_raid6(rs) &&
3132 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
3133 ti->error = "'nosync' not allowed for new raid6 set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003134 r = -EINVAL;
3135 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003136 }
3137 rs_setup_recovery(rs, 0);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003138 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3139 rs_set_new(rs);
3140 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003141 /* A recovering raid set may be resized */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003142 ; /* skip setup rs */
3143 } else if (rs_is_reshaping(rs)) {
3144 /* Have to reject size change request during reshape */
3145 if (resize) {
3146 ti->error = "Can't resize a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003147 r = -EPERM;
3148 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003149 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003150 /* skip setup rs */
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003151 } else if (rs_takeover_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003152 if (rs_is_reshaping(rs)) {
3153 ti->error = "Can't takeover a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003154 r = -EPERM;
3155 goto bad;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003156 }
3157
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003158 /* We can't takeover a journaled raid4/5/6 */
3159 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3160 ti->error = "Can't takeover a journaled raid4/5/6 set";
3161 r = -EPERM;
3162 goto bad;
3163 }
3164
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003165 /*
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003166 * If a takeover is needed, userspace sets any additional
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003167 * devices to rebuild and we can check for a valid request here.
3168 *
3169 * If acceptible, set the level to the new requested
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003170 * one, prohibit requesting recovery, allow the raid
3171 * set to run and store superblocks during resume.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003172 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003173 r = rs_check_takeover(rs);
3174 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003175 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003176
3177 r = rs_setup_takeover(rs);
3178 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003179 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003180
Mike Snitzer42863252016-06-02 12:27:46 -04003181 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003182 /* Takeover ain't recovery, so disable recovery */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003183 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003184 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003185 } else if (rs_reshape_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003186 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003187 * No need to check for 'ongoing' takeover here, because takeover
3188 * is an instant operation as oposed to an ongoing reshape.
3189 */
3190
3191 /* We can't reshape a journaled raid4/5/6 */
3192 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3193 ti->error = "Can't reshape a journaled raid4/5/6 set";
3194 r = -EPERM;
3195 goto bad;
3196 }
3197
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01003198 /* Out-of-place space has to be available to allow for a reshape unless raid1! */
3199 if (reshape_sectors || rs_is_raid1(rs)) {
3200 /*
3201 * We can only prepare for a reshape here, because the
3202 * raid set needs to run to provide the repective reshape
3203 * check functions via its MD personality instance.
3204 *
3205 * So do the reshape check after md_run() succeeded.
3206 */
3207 r = rs_prepare_reshape(rs);
3208 if (r)
3209 return r;
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003210
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01003211 /* Reshaping ain't recovery, so disable recovery */
3212 rs_setup_recovery(rs, MaxSector);
3213 }
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003214 rs_set_cur(rs);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003215 } else {
3216 /* May not set recovery when a device rebuild is requested */
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02003217 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
3218 rs_setup_recovery(rs, MaxSector);
3219 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3220 } else
3221 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003222 0 : (resize ? calculated_dev_sectors : MaxSector));
3223 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003224 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003225
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003226 /* If constructor requested it, change data and new_data offsets */
3227 r = rs_adjust_data_offsets(rs);
3228 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003229 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003230
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003231 /* Start raid set read-only and assumed clean to change in raid_resume() */
3232 rs->md.ro = 1;
3233 rs->md.in_sync = 1;
3234 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003235
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02003236 /* Has to be held on running the array */
3237 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003238 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003239 rs->md.in_sync = 0; /* Assume already marked dirty */
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003240 if (r) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003241 ti->error = "Failed to run raid array";
3242 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003243 goto bad;
3244 }
3245
Song Liud5d885f2017-11-19 22:17:01 -08003246 r = md_start(&rs->md);
3247
3248 if (r) {
3249 ti->error = "Failed to start raid array";
3250 mddev_unlock(&rs->md);
3251 goto bad_md_start;
3252 }
3253
NeilBrown9d09e662011-01-13 20:00:02 +00003254 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00003255 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
3256
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003257 /* If raid4/5/6 journal mode explictely requested (only possible with journal dev) -> set it */
3258 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
3259 r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
3260 if (r) {
3261 ti->error = "Failed to set raid4/5/6 journal mode";
3262 mddev_unlock(&rs->md);
3263 goto bad_journal_mode_set;
3264 }
3265 }
3266
Jonathan Brassow32737272011-08-02 12:32:07 +01003267 mddev_suspend(&rs->md);
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003268 set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003269
3270 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
3271 if (rs_is_raid456(rs)) {
3272 r = rs_set_raid456_stripe_cache(rs);
3273 if (r)
3274 goto bad_stripe_cache;
3275 }
3276
3277 /* Now do an early reshape check */
3278 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3279 r = rs_check_reshape(rs);
3280 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003281 goto bad_check_reshape;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003282
3283 /* Restore new, ctr requested layout to perform check */
3284 rs_config_restore(rs, &rs_layout);
3285
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003286 if (rs->md.pers->start_reshape) {
3287 r = rs->md.pers->check_reshape(&rs->md);
3288 if (r) {
3289 ti->error = "Reshape check failed";
3290 goto bad_check_reshape;
3291 }
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003292 }
3293 }
3294
Heinz Mauelshagen11e29682016-11-29 22:37:30 +01003295 /* Disable/enable discard support on raid set. */
3296 configure_discard_support(rs);
3297
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003298 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003299 return 0;
3300
Song Liud5d885f2017-11-19 22:17:01 -08003301bad_md_start:
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003302bad_journal_mode_set:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003303bad_stripe_cache:
3304bad_check_reshape:
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003305 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003306bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003307 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003308
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003309 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003310}
3311
3312static void raid_dtr(struct dm_target *ti)
3313{
3314 struct raid_set *rs = ti->private;
3315
3316 list_del_init(&rs->callbacks.list);
3317 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003318 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003319}
3320
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003321static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00003322{
3323 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11003324 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00003325
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003326 /*
3327 * If we're reshaping to add disk(s)), ti->len and
3328 * mddev->array_sectors will differ during the process
3329 * (ti->len > mddev->array_sectors), so we have to requeue
3330 * bios with addresses > mddev->array_sectors here or
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003331 * there will occur accesses past EOD of the component
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003332 * data images thus erroring the raid set.
3333 */
3334 if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
3335 return DM_MAPIO_REQUEUE;
3336
Shaohua Lic4d6a1b2017-09-21 10:29:22 -07003337 md_handle_request(mddev, bio);
NeilBrown9d09e662011-01-13 20:00:02 +00003338
3339 return DM_MAPIO_SUBMITTED;
3340}
3341
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003342/* Return string describing the current sync action of @mddev */
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003343static const char *decipher_sync_action(struct mddev *mddev, unsigned long recovery)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003344{
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003345 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003346 return "frozen";
3347
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003348 /* The MD sync thread can be done with io but still be running */
3349 if (!test_bit(MD_RECOVERY_DONE, &recovery) &&
3350 (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
3351 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery)))) {
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003352 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003353 return "reshape";
3354
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003355 if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
3356 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003357 return "resync";
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003358 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003359 return "check";
3360 return "repair";
3361 }
3362
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003363 if (test_bit(MD_RECOVERY_RECOVER, &recovery))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003364 return "recover";
3365 }
3366
3367 return "idle";
3368}
3369
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003370/*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003371 * Return status string for @rdev
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003372 *
3373 * Status characters:
3374 *
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003375 * 'D' = Dead/Failed raid set component or raid4/5/6 journal device
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003376 * 'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
3377 * 'A' = Alive and in-sync raid set component _or_ alive raid4/5/6 'write_through' journal device
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003378 * '-' = Non-existing device (i.e. uspace passed '- -' into the ctr)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003379 */
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003380static const char *__raid_dev_status(struct raid_set *rs, struct md_rdev *rdev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003381{
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003382 if (!rdev->bdev)
3383 return "-";
3384 else if (test_bit(Faulty, &rdev->flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003385 return "D";
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003386 else if (test_bit(Journal, &rdev->flags))
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003387 return (rs->journal_dev.mode == R5C_JOURNAL_MODE_WRITE_THROUGH) ? "A" : "a";
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003388 else if (test_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags) ||
3389 (!test_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags) &&
3390 !test_bit(In_sync, &rdev->flags)))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003391 return "a";
3392 else
3393 return "A";
3394}
3395
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003396/* Helper to return resync/reshape progress for @rs and runtime flags for raid set in sync / resynching */
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003397static sector_t rs_get_progress(struct raid_set *rs, unsigned long recovery,
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003398 sector_t resync_max_sectors)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003399{
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003400 sector_t r;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003401 struct mddev *mddev = &rs->md;
3402
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003403 clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003404 clear_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003405
3406 if (rs_is_raid0(rs)) {
3407 r = resync_max_sectors;
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003408 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003409
3410 } else {
Heinz Mauelshagen7c297442017-12-13 17:13:20 +01003411 if (test_bit(MD_RECOVERY_NEEDED, &recovery) ||
3412 test_bit(MD_RECOVERY_RESHAPE, &recovery) ||
3413 test_bit(MD_RECOVERY_RUNNING, &recovery))
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003414 r = mddev->curr_resync_completed;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003415 else
Jonathan Brassow41dcf192017-10-02 17:17:35 -05003416 r = mddev->recovery_cp;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003417
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003418 if (r >= resync_max_sectors &&
3419 (!test_bit(MD_RECOVERY_REQUESTED, &recovery) ||
3420 (!test_bit(MD_RECOVERY_FROZEN, &recovery) &&
3421 !test_bit(MD_RECOVERY_NEEDED, &recovery) &&
3422 !test_bit(MD_RECOVERY_RUNNING, &recovery)))) {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003423 /*
3424 * Sync complete.
3425 */
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003426 /* In case we have finished recovering, the array is in sync. */
3427 if (test_bit(MD_RECOVERY_RECOVER, &recovery))
3428 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3429
3430 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) {
3431 /*
3432 * In case we are recovering, the array is not in sync
3433 * and health chars should show the recovering legs.
3434 */
3435 ;
3436
3437 } else if (test_bit(MD_RECOVERY_SYNC, &recovery) &&
3438 !test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
3439 /*
3440 * If "resync" is occurring, the raid set
3441 * is or may be out of sync hence the health
3442 * characters shall be 'a'.
3443 */
3444 set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3445
Heinz Mauelshagendc15b942017-12-13 17:13:19 +01003446 } else if (test_bit(MD_RECOVERY_RESHAPE, &recovery) &&
3447 !test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
3448 /*
3449 * If "reshape" is occurring, the raid set
3450 * is or may be out of sync hence the health
3451 * characters shall be 'a'.
3452 */
3453 set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3454
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003455 } else if (test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003456 /*
3457 * If "check" or "repair" is occurring, the raid set has
3458 * undergone an initial sync and the health characters
3459 * should not be 'a' anymore.
3460 */
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003461 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003462
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003463 } else {
3464 struct md_rdev *rdev;
3465
3466 /*
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003467 * We are idle and recovery is needed, prevent 'A' chars race
3468 * caused by components still set to in-sync by constrcuctor.
3469 */
3470 if (test_bit(MD_RECOVERY_NEEDED, &recovery))
3471 set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3472
3473 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003474 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04003475 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003476 * devices are In_sync, then it is the raid set that is
3477 * being initialized.
3478 */
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003479 set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003480 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003481 if (!test_bit(Journal, &rdev->flags) &&
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003482 !test_bit(In_sync, &rdev->flags)) {
3483 clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3484 break;
3485 }
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003486 }
3487 }
3488
Heinz Mauelshagen4102d9d2017-12-02 01:03:55 +01003489 return min(r, resync_max_sectors);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003490}
3491
3492/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003493static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003494{
3495 return dev ? dev->name : "-";
3496}
3497
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003498static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003499 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00003500{
3501 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003502 struct mddev *mddev = &rs->md;
3503 struct r5conf *conf = mddev->private;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003504 int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003505 unsigned long recovery;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003506 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
3507 unsigned int sz = 0;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003508 unsigned int rebuild_disks;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003509 unsigned int write_mostly_params = 0;
3510 sector_t progress, resync_max_sectors, resync_mismatches;
3511 const char *sync_action;
3512 struct raid_type *rt;
NeilBrown9d09e662011-01-13 20:00:02 +00003513
3514 switch (type) {
3515 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003516 /* *Should* always succeed */
3517 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
3518 if (!rt)
3519 return;
NeilBrown9d09e662011-01-13 20:00:02 +00003520
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003521 DMEMIT("%s %d ", rt->name, mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00003522
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003523 /* Access most recent mddev properties for status output */
3524 smp_rmb();
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003525 recovery = rs->md.recovery;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003526 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04003527 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003528 mddev->resync_max_sectors : mddev->dev_sectors;
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003529 progress = rs_get_progress(rs, recovery, resync_max_sectors);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003530 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003531 atomic64_read(&mddev->resync_mismatches) : 0;
Heinz Mauelshagen67143512017-12-02 01:03:53 +01003532 sync_action = decipher_sync_action(&rs->md, recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003533
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003534 /* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
3535 for (i = 0; i < rs->raid_disks; i++)
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003536 DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev));
Jonathan Brassowbe836512013-04-24 11:42:43 +10003537
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003538 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003539 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003540 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003541 * - Initializing the raid set
3542 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003543 * The user can distinguish between the two by referring
3544 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003545 *
3546 * The reshape ratio shows the progress of
3547 * changing the raid layout or the number of
3548 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003549 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003550 DMEMIT(" %llu/%llu", (unsigned long long) progress,
3551 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003552
Jonathan Brassowbe836512013-04-24 11:42:43 +10003553 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003554 * v1.5.0+:
3555 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003556 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003557 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10003558 * information on each of these states.
3559 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003560 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10003561
3562 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003563 * v1.5.0+:
3564 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003565 * resync_mismatches/mismatch_cnt
3566 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003567 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10003568 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003569 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
3570
3571 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003572 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003573 *
3574 * data_offset (needed for out of space reshaping)
3575 * This field shows the data offset into the data
3576 * image LV where the first stripes data starts.
3577 *
3578 * We keep data_offset equal on all raid disks of the set,
3579 * so retrieving it from the first raid disk is sufficient.
3580 */
3581 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003582
3583 /*
3584 * v1.10.0+:
3585 */
3586 DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
Heinz Mauelshagen242ea5a2017-12-02 01:03:54 +01003587 __raid_dev_status(rs, &rs->journal_dev.rdev) : "-");
NeilBrown9d09e662011-01-13 20:00:02 +00003588 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003589
NeilBrown9d09e662011-01-13 20:00:02 +00003590 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003591 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00003592
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003593 /* Calculate raid parameter count */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003594 for (i = 0; i < rs->raid_disks; i++)
3595 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003596 write_mostly_params += 2;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003597 rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
3598 raid_param_cnt += rebuild_disks * 2 +
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003599 write_mostly_params +
3600 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003601 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 +
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003602 (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0) +
3603 (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags) ? 2 : 0);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003604
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003605 /* Emit table line */
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003606 /* This has to be in the documented order for userspace! */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003607 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04003608 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003609 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003610 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
3611 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003612 if (rebuild_disks)
3613 for (i = 0; i < rs->raid_disks; i++)
3614 if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
3615 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
3616 rs->dev[i].rdev.raid_disk);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003617 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
3618 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
3619 mddev->bitmap_info.daemon_sleep);
3620 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
3621 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
3622 mddev->sync_speed_min);
3623 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
3624 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
3625 mddev->sync_speed_max);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003626 if (write_mostly_params)
3627 for (i = 0; i < rs->raid_disks; i++)
3628 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
3629 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
3630 rs->dev[i].rdev.raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04003631 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003632 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003633 mddev->bitmap_info.max_write_behind);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003634 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
3635 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
3636 max_nr_stripes);
3637 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
3638 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
3639 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
3640 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
3641 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
3642 raid10_md_layout_to_copies(mddev->layout));
3643 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
3644 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
3645 raid10_md_layout_to_format(mddev->layout));
3646 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
3647 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
3648 max(rs->delta_disks, mddev->delta_disks));
3649 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
3650 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
3651 (unsigned long long) rs->data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003652 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
3653 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
3654 __get_dev_name(rs->journal_dev.dev));
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003655 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
3656 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
3657 md_journal_mode_to_dm_raid(rs->journal_dev.mode));
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003658 DMEMIT(" %d", rs->raid_disks);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003659 for (i = 0; i < rs->raid_disks; i++)
3660 DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
3661 __get_dev_name(rs->dev[i].data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00003662 }
NeilBrown9d09e662011-01-13 20:00:02 +00003663}
3664
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003665static int raid_message(struct dm_target *ti, unsigned int argc, char **argv)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003666{
3667 struct raid_set *rs = ti->private;
3668 struct mddev *mddev = &rs->md;
3669
Jonathan Brassowbe836512013-04-24 11:42:43 +10003670 if (!mddev->pers || !mddev->pers->sync_request)
3671 return -EINVAL;
3672
3673 if (!strcasecmp(argv[0], "frozen"))
3674 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3675 else
3676 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3677
3678 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
3679 if (mddev->sync_thread) {
3680 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3681 md_reap_sync_thread(mddev);
3682 }
3683 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3684 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3685 return -EBUSY;
3686 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003687 ; /* MD_RECOVERY_NEEDED set below */
3688 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003689 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003690 else {
Mike Snitzerad470472017-02-28 11:55:16 -05003691 if (!strcasecmp(argv[0], "check")) {
Jonathan Brassowbe836512013-04-24 11:42:43 +10003692 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Mike Snitzerad470472017-02-28 11:55:16 -05003693 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3694 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3695 } else if (!strcasecmp(argv[0], "repair")) {
Mike Snitzer105db592017-01-06 15:38:08 -05003696 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3697 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3698 } else
Jonathan Brassowbe836512013-04-24 11:42:43 +10003699 return -EINVAL;
Jonathan Brassowbe836512013-04-24 11:42:43 +10003700 }
3701 if (mddev->ro == 2) {
3702 /* A write to sync_action is enough to justify
3703 * canceling read-auto mode
3704 */
3705 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003706 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003707 md_wakeup_thread(mddev->sync_thread);
3708 }
3709 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003710 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003711 md_wakeup_thread(mddev->thread);
3712
3713 return 0;
3714}
3715
3716static int raid_iterate_devices(struct dm_target *ti,
3717 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00003718{
3719 struct raid_set *rs = ti->private;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003720 unsigned int i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003721 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00003722
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003723 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00003724 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003725 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00003726 rs->dev[i].data_dev,
3727 0, /* No offset on data devs */
3728 rs->md.dev_sectors,
3729 data);
3730
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003731 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003732}
3733
3734static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
3735{
3736 struct raid_set *rs = ti->private;
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003737 unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003738
3739 blk_limits_io_min(limits, chunk_size);
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003740 blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
NeilBrown9d09e662011-01-13 20:00:02 +00003741}
3742
NeilBrown9d09e662011-01-13 20:00:02 +00003743static void raid_postsuspend(struct dm_target *ti)
3744{
3745 struct raid_set *rs = ti->private;
3746
NeilBrown4d5324f2017-10-19 12:17:16 +11003747 if (!test_and_set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
Heinz Mauelshagen1af20482017-12-02 01:03:48 +01003748 /* Writes have to be stopped before suspending to avoid deadlocks. */
3749 if (!test_bit(MD_RECOVERY_FROZEN, &rs->md.recovery))
3750 md_stop_writes(&rs->md);
3751
NeilBrown4d5324f2017-10-19 12:17:16 +11003752 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003753 mddev_suspend(&rs->md);
NeilBrown4d5324f2017-10-19 12:17:16 +11003754 mddev_unlock(&rs->md);
3755 }
NeilBrown9d09e662011-01-13 20:00:02 +00003756}
3757
Jonathan Brassowf381e712013-05-08 17:57:13 -05003758static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00003759{
Jonathan Brassow9092c022013-05-02 14:19:24 -05003760 int i;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003761 uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassow9092c022013-05-02 14:19:24 -05003762 unsigned long flags;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003763 bool cleared = false;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003764 struct dm_raid_superblock *sb;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003765 struct mddev *mddev = &rs->md;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003766 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00003767
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003768 /* RAID personalities have to provide hot add/remove methods or we need to bail out. */
3769 if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
3770 return;
3771
3772 memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
3773
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003774 for (i = 0; i < mddev->raid_disks; i++) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003775 r = &rs->dev[i].rdev;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003776 /* HM FIXME: enhance journal device recovery processing */
3777 if (test_bit(Journal, &r->flags))
3778 continue;
3779
Heinz Mauelshagene2568462017-01-14 03:53:10 +01003780 if (test_bit(Faulty, &r->flags) &&
3781 r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003782 DMINFO("Faulty %s device #%d has readable super block."
3783 " Attempting to revive it.",
3784 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003785
3786 /*
3787 * Faulty bit may be set, but sometimes the array can
3788 * be suspended before the personalities can respond
3789 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04003790 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003791 * the failed device, its 'raid_disk' number will be
3792 * '>= 0' - meaning we must call this function
3793 * ourselves.
3794 */
Jonathan Brassowf381e712013-05-08 17:57:13 -05003795 flags = r->flags;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003796 clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
3797 if (r->raid_disk >= 0) {
3798 if (mddev->pers->hot_remove_disk(mddev, r)) {
3799 /* Failed to revive this device, try next */
3800 r->flags = flags;
3801 continue;
3802 }
3803 } else
3804 r->raid_disk = r->saved_raid_disk = i;
3805
Jonathan Brassowf381e712013-05-08 17:57:13 -05003806 clear_bit(Faulty, &r->flags);
3807 clear_bit(WriteErrorSeen, &r->flags);
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003808
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003809 if (mddev->pers->hot_add_disk(mddev, r)) {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003810 /* Failed to revive this device, try next */
3811 r->raid_disk = r->saved_raid_disk = -1;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003812 r->flags = flags;
3813 } else {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003814 clear_bit(In_sync, &r->flags);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003815 r->recovery_offset = 0;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003816 set_bit(i, (void *) cleared_failed_devices);
3817 cleared = true;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003818 }
3819 }
3820 }
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003821
3822 /* If any failed devices could be cleared, update all sbs failed_devices bits */
3823 if (cleared) {
3824 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
3825
Jonathan Brassowf381e712013-05-08 17:57:13 -05003826 rdev_for_each(r, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003827 if (test_bit(Journal, &r->flags))
3828 continue;
3829
Jonathan Brassowf381e712013-05-08 17:57:13 -05003830 sb = page_address(r->sb_page);
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003831 sb_retrieve_failed_devices(sb, failed_devices);
3832
3833 for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
3834 failed_devices[i] &= ~cleared_failed_devices[i];
3835
3836 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003837 }
3838 }
3839}
3840
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003841static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003842{
3843 int r = 0;
3844
3845 /* Try loading the bitmap unless "raid0", which does not have one */
3846 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04003847 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003848 r = bitmap_load(&rs->md);
3849 if (r)
3850 DMERR("Failed to load bitmap");
3851 }
3852
3853 return r;
3854}
3855
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003856/* Enforce updating all superblocks */
3857static void rs_update_sbs(struct raid_set *rs)
3858{
3859 struct mddev *mddev = &rs->md;
3860 int ro = mddev->ro;
3861
Shaohua Li29530792016-12-08 15:48:19 -08003862 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003863 mddev->ro = 0;
3864 md_update_sb(mddev, 1);
3865 mddev->ro = ro;
3866}
3867
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003868/*
3869 * Reshape changes raid algorithm of @rs to new one within personality
3870 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
3871 * disks from a raid set thus growing/shrinking it or resizes the set
3872 *
3873 * Call mddev_lock_nointr() before!
3874 */
3875static int rs_start_reshape(struct raid_set *rs)
3876{
3877 int r;
3878 struct mddev *mddev = &rs->md;
3879 struct md_personality *pers = mddev->pers;
3880
3881 r = rs_setup_reshape(rs);
3882 if (r)
3883 return r;
3884
3885 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003886 if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003887 mddev_resume(mddev);
3888
3889 /*
3890 * Check any reshape constraints enforced by the personalility
3891 *
3892 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
3893 */
3894 r = pers->check_reshape(mddev);
3895 if (r) {
3896 rs->ti->error = "pers->check_reshape() failed";
3897 return r;
3898 }
3899
3900 /*
3901 * Personality may not provide start reshape method in which
3902 * case check_reshape above has already covered everything
3903 */
3904 if (pers->start_reshape) {
3905 r = pers->start_reshape(mddev);
3906 if (r) {
3907 rs->ti->error = "pers->start_reshape() failed";
3908 return r;
3909 }
3910 }
3911
3912 /* Suspend because a resume will happen in raid_resume() */
Heinz Mauelshagen0cf352e2017-07-13 17:34:24 +02003913 set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
3914 mddev_suspend(mddev);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003915
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003916 /*
3917 * Now reshape got set up, update superblocks to
3918 * reflect the fact so that a table reload will
3919 * access proper superblock content in the ctr.
3920 */
3921 rs_update_sbs(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003922
3923 return 0;
3924}
3925
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003926static int raid_preresume(struct dm_target *ti)
3927{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003928 int r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003929 struct raid_set *rs = ti->private;
3930 struct mddev *mddev = &rs->md;
3931
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01003932 /* This is a resume after a suspend of the set -> it's already started. */
Mike Snitzer42863252016-06-02 12:27:46 -04003933 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003934 return 0;
3935
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01003936 if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
3937 struct raid_set *rs_active = rs_find_active(rs);
3938
3939 if (rs_active) {
3940 /*
3941 * In case no rebuilds have been requested
3942 * and an active table slot exists, copy
3943 * current resynchonization completed and
3944 * reshape position pointers across from
3945 * suspended raid set in the active slot.
3946 *
3947 * This resumes the new mapping at current
3948 * offsets to continue recover/reshape without
3949 * necessarily redoing a raid set partially or
3950 * causing data corruption in case of a reshape.
3951 */
3952 if (rs_active->md.curr_resync_completed != MaxSector)
3953 mddev->curr_resync_completed = rs_active->md.curr_resync_completed;
3954 if (rs_active->md.reshape_position != MaxSector)
3955 mddev->reshape_position = rs_active->md.reshape_position;
3956 }
3957 }
3958
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003959 /*
3960 * The superblocks need to be updated on disk if the
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003961 * array is new or new devices got added (thus zeroed
3962 * out by userspace) or __load_dirty_region_bitmap
3963 * will overwrite them in core with old data or fail.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003964 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003965 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
3966 rs_update_sbs(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003967
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003968 /* Load the bitmap from disk unless raid0 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003969 r = __load_dirty_region_bitmap(rs);
3970 if (r)
3971 return r;
3972
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003973 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
Dmitry Bilunov7a0c5c52017-03-30 18:14:26 +03003974 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003975 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
3976 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
3977 to_bytes(rs->requested_bitmap_chunk_sectors), 0);
3978 if (r)
3979 DMERR("Failed to resize bitmap");
3980 }
3981
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003982 /* Check for any resize/reshape on @rs and adjust/initiate */
3983 /* Be prepared for mddev_resume() in raid_resume() */
3984 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3985 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003986 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3987 mddev->resync_min = mddev->recovery_cp;
3988 }
3989
Heinz Mauelshagen345a6cd2016-06-25 02:42:54 +02003990 /* Check for any reshape request unless new raid set */
Heinz Mauelshagend39f0012017-12-02 01:03:52 +01003991 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003992 /* Initiate a reshape. */
Heinz Mauelshagen61e06e22017-12-02 01:03:51 +01003993 rs_set_rdev_sectors(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003994 mddev_lock_nointr(mddev);
3995 r = rs_start_reshape(rs);
3996 mddev_unlock(mddev);
3997 if (r)
3998 DMWARN("Failed to check/start reshape, continuing without change");
3999 r = 0;
4000 }
4001
4002 return r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02004003}
4004
Jonathan Brassowf381e712013-05-08 17:57:13 -05004005static void raid_resume(struct dm_target *ti)
4006{
4007 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02004008 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05004009
Mike Snitzer42863252016-06-02 12:27:46 -04004010 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02004011 /*
4012 * A secondary resume while the device is active.
4013 * Take this opportunity to check whether any failed
4014 * devices are reachable again.
4015 */
4016 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004017 }
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02004018
NeilBrown4d5324f2017-10-19 12:17:16 +11004019 if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01004020 /* Only reduce raid set size before running a disk removing reshape. */
4021 if (mddev->delta_disks < 0)
4022 rs_set_capacity(rs);
4023
NeilBrown4d5324f2017-10-19 12:17:16 +11004024 mddev_lock_nointr(mddev);
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01004025 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Heinz Mauelshagen78a75d12017-12-02 01:03:56 +01004026 mddev->ro = 0;
4027 mddev->in_sync = 0;
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02004028 mddev_resume(mddev);
NeilBrown4d5324f2017-10-19 12:17:16 +11004029 mddev_unlock(mddev);
4030 }
NeilBrown9d09e662011-01-13 20:00:02 +00004031}
4032
4033static struct target_type raid_target = {
4034 .name = "raid",
Heinz Mauelshagen11e47232017-12-13 17:13:18 +01004035 .version = {1, 13, 2},
NeilBrown9d09e662011-01-13 20:00:02 +00004036 .module = THIS_MODULE,
4037 .ctr = raid_ctr,
4038 .dtr = raid_dtr,
4039 .map = raid_map,
4040 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10004041 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00004042 .iterate_devices = raid_iterate_devices,
4043 .io_hints = raid_io_hints,
NeilBrown9d09e662011-01-13 20:00:02 +00004044 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02004045 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00004046 .resume = raid_resume,
4047};
4048
4049static int __init dm_raid_init(void)
4050{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11004051 DMINFO("Loading target version %u.%u.%u",
4052 raid_target.version[0],
4053 raid_target.version[1],
4054 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00004055 return dm_register_target(&raid_target);
4056}
4057
4058static void __exit dm_raid_exit(void)
4059{
4060 dm_unregister_target(&raid_target);
4061}
4062
4063module_init(dm_raid_init);
4064module_exit(dm_raid_exit);
4065
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02004066module_param(devices_handle_discard_safely, bool, 0644);
4067MODULE_PARM_DESC(devices_handle_discard_safely,
4068 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
4069
Mike Snitzeref9b85a2016-06-02 12:02:19 -04004070MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
4071MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05004072MODULE_ALIAS("dm-raid1");
4073MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00004074MODULE_ALIAS("dm-raid4");
4075MODULE_ALIAS("dm-raid5");
4076MODULE_ALIAS("dm-raid6");
4077MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02004078MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00004079MODULE_LICENSE("GPL");