blob: 7d893228c40f50dd7d0017fca004b504fa27f567 [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"
NeilBrown9d09e662011-01-13 20:00:02 +000015#include "bitmap.h"
16
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010017#include <linux/device-mapper.h>
18
NeilBrown9d09e662011-01-13 20:00:02 +000019#define DM_MSG_PREFIX "raid"
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +020020#define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
NeilBrown9d09e662011-01-13 20:00:02 +000021
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +020022/*
23 * Minimum sectors of free reshape space per raid device
24 */
25#define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
26
Heinz 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 Mauelshagen48cf06b2014-09-24 17:47:19 +020032static bool devices_handle_discard_safely = false;
33
NeilBrown9d09e662011-01-13 20:00:02 +000034/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010035 * The following flags are used by dm-raid.c to set up the array state.
36 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000037 */
Mike Snitzer43157842016-05-30 13:03:37 -040038#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000039
40struct raid_dev {
41 /*
42 * Two DM devices, one to hold metadata and one to hold the
Mike Snitzer43157842016-05-30 13:03:37 -040043 * actual data/parity. The reason for this is to not confuse
NeilBrown9d09e662011-01-13 20:00:02 +000044 * ti->len and give more flexibility in altering size and
45 * characteristics.
46 *
47 * While it is possible for this device to be associated
48 * with a different physical device than the data_dev, it
49 * is intended for it to be the same.
50 * |--------- Physical Device ---------|
51 * |- meta_dev -|------ data_dev ------|
52 */
53 struct dm_dev *meta_dev;
54 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110055 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000056};
57
58/*
Mike Snitzer42863252016-06-02 12:27:46 -040059 * Bits for establishing rs->ctr_flags
Heinz Mauelshagen702108d2016-05-19 18:49:26 +020060 *
61 * 1 = no flag value
62 * 2 = flag with value
NeilBrown9d09e662011-01-13 20:00:02 +000063 */
Mike Snitzer42863252016-06-02 12:27:46 -040064#define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
65#define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
66#define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
67#define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
68#define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
69#define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
70#define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
71#define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
72#define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
73#define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
74#define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
75#define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
Mike Snitzer9b6e5422016-06-02 11:48:09 -040076/* New for v1.9.0 */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +020077#define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
Mike Snitzer42863252016-06-02 12:27:46 -040078#define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
79#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
80
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010081/* New for v1.10.0 */
Heinz Mauelshagen6e536362017-03-22 17:44:38 +010082#define __CTR_FLAG_JOURNAL_DEV 15 /* 2 */ /* Only with raid4/5/6 (journal device)! */
83
84/* New for v1.11.1 */
85#define __CTR_FLAG_JOURNAL_MODE 16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +010086
Mike Snitzer42863252016-06-02 12:27:46 -040087/*
88 * Flags for rs->ctr_flags field.
89 */
90#define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
91#define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
92#define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
93#define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
94#define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
95#define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
96#define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
97#define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
98#define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
99#define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
100#define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
101#define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
102#define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
103#define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
104#define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100105#define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV)
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100106#define CTR_FLAG_JOURNAL_MODE (1 << __CTR_FLAG_JOURNAL_MODE)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500107
Mike Snitzer2664f3c2017-02-28 15:31:44 -0500108#define RESUME_STAY_FROZEN_FLAGS (CTR_FLAG_DELTA_DISKS | CTR_FLAG_DATA_OFFSET)
109
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200110/*
111 * Definitions of various constructor flags to
112 * be used in checks of valid / invalid flags
113 * per raid level.
114 */
115/* Define all any sync flags */
116#define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
117
118/* Define flags for options without argument (e.g. 'nosync') */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200119#define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
120 CTR_FLAG_RAID10_USE_NEAR_SETS)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200121
122/* Define flags for options with one argument (e.g. 'delta_disks +2') */
123#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
124 CTR_FLAG_WRITE_MOSTLY | \
125 CTR_FLAG_DAEMON_SLEEP | \
126 CTR_FLAG_MIN_RECOVERY_RATE | \
127 CTR_FLAG_MAX_RECOVERY_RATE | \
128 CTR_FLAG_MAX_WRITE_BEHIND | \
129 CTR_FLAG_STRIPE_CACHE | \
130 CTR_FLAG_REGION_SIZE | \
131 CTR_FLAG_RAID10_COPIES | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200132 CTR_FLAG_RAID10_FORMAT | \
133 CTR_FLAG_DELTA_DISKS | \
134 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200135
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200136/* Valid options definitions per raid level... */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200137
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200138/* "raid0" does only accept data offset */
139#define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200140
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200141/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
142#define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
143 CTR_FLAG_REBUILD | \
144 CTR_FLAG_WRITE_MOSTLY | \
145 CTR_FLAG_DAEMON_SLEEP | \
146 CTR_FLAG_MIN_RECOVERY_RATE | \
147 CTR_FLAG_MAX_RECOVERY_RATE | \
148 CTR_FLAG_MAX_WRITE_BEHIND | \
149 CTR_FLAG_REGION_SIZE | \
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +0200150 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200151 CTR_FLAG_DATA_OFFSET)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200152
153/* "raid10" does not accept any raid1 or stripe cache options */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200154#define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
155 CTR_FLAG_REBUILD | \
156 CTR_FLAG_DAEMON_SLEEP | \
157 CTR_FLAG_MIN_RECOVERY_RATE | \
158 CTR_FLAG_MAX_RECOVERY_RATE | \
159 CTR_FLAG_REGION_SIZE | \
160 CTR_FLAG_RAID10_COPIES | \
161 CTR_FLAG_RAID10_FORMAT | \
162 CTR_FLAG_DELTA_DISKS | \
163 CTR_FLAG_DATA_OFFSET | \
164 CTR_FLAG_RAID10_USE_NEAR_SETS)
165
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200166/*
167 * "raid4/5/6" do not accept any raid1 or raid10 specific options
168 *
169 * "raid6" does not accept "nosync", because it is not guaranteed
170 * that both parity and q-syndrome are being written properly with
171 * any writes
172 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200173#define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
174 CTR_FLAG_REBUILD | \
175 CTR_FLAG_DAEMON_SLEEP | \
176 CTR_FLAG_MIN_RECOVERY_RATE | \
177 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200178 CTR_FLAG_STRIPE_CACHE | \
179 CTR_FLAG_REGION_SIZE | \
180 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100181 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100182 CTR_FLAG_JOURNAL_DEV | \
183 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200184
185#define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
186 CTR_FLAG_REBUILD | \
187 CTR_FLAG_DAEMON_SLEEP | \
188 CTR_FLAG_MIN_RECOVERY_RATE | \
189 CTR_FLAG_MAX_RECOVERY_RATE | \
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200190 CTR_FLAG_STRIPE_CACHE | \
191 CTR_FLAG_REGION_SIZE | \
192 CTR_FLAG_DELTA_DISKS | \
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100193 CTR_FLAG_DATA_OFFSET | \
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100194 CTR_FLAG_JOURNAL_DEV | \
195 CTR_FLAG_JOURNAL_MODE)
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200196/* ...valid options definitions per raid level */
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200197
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200198/*
199 * Flags for rs->runtime_flags field
200 * (RT_FLAG prefix meaning "runtime flag")
201 *
202 * These are all internal and used to define runtime state,
203 * e.g. to prevent another resume from preresume processing
204 * the raid set all over again.
205 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200206#define RT_FLAG_RS_PRERESUMED 0
207#define RT_FLAG_RS_RESUMED 1
208#define RT_FLAG_RS_BITMAP_LOADED 2
209#define RT_FLAG_UPDATE_SBS 3
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200210#define RT_FLAG_RESHAPE_RS 4
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200211
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200212/* Array elements of 64 bit needed for rebuild/failed disk bits */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200213#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
214
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200215/*
216 * raid set level, layout and chunk sectors backup/restore
217 */
218struct rs_layout {
219 int new_level;
220 int new_layout;
221 int new_chunk_sectors;
222};
223
NeilBrown9d09e662011-01-13 20:00:02 +0000224struct raid_set {
225 struct dm_target *ti;
226
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -0600227 uint32_t bitmap_loaded;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200228 uint32_t stripe_cache_entries;
Mike Snitzer42863252016-06-02 12:27:46 -0400229 unsigned long ctr_flags;
230 unsigned long runtime_flags;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200231
232 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown9d09e662011-01-13 20:00:02 +0000233
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200234 int raid_disks;
235 int delta_disks;
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200236 int data_offset;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200237 int raid10_copies;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +0200238 int requested_bitmap_chunk_sectors;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200239
NeilBrownfd01b882011-10-11 16:47:53 +1100240 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +0000241 struct raid_type *raid_type;
242 struct dm_target_callbacks callbacks;
243
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100244 /* Optional raid4/5/6 journal device */
245 struct journal_dev {
246 struct dm_dev *dev;
247 struct md_rdev rdev;
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100248 int mode;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100249 } journal_dev;
250
NeilBrown9d09e662011-01-13 20:00:02 +0000251 struct raid_dev dev[0];
252};
253
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200254static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200255{
256 struct mddev *mddev = &rs->md;
257
258 l->new_level = mddev->new_level;
259 l->new_layout = mddev->new_layout;
260 l->new_chunk_sectors = mddev->new_chunk_sectors;
261}
262
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200263static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +0200264{
265 struct mddev *mddev = &rs->md;
266
267 mddev->new_level = l->new_level;
268 mddev->new_layout = l->new_layout;
269 mddev->new_chunk_sectors = l->new_chunk_sectors;
270}
271
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200272/* raid10 algorithms (i.e. formats) */
273#define ALGORITHM_RAID10_DEFAULT 0
274#define ALGORITHM_RAID10_NEAR 1
275#define ALGORITHM_RAID10_OFFSET 2
276#define ALGORITHM_RAID10_FAR 3
277
NeilBrown9d09e662011-01-13 20:00:02 +0000278/* Supported raid types and properties. */
279static struct raid_type {
280 const char *name; /* RAID algorithm. */
281 const char *descr; /* Descriptor text for logging. */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200282 const unsigned int parity_devs; /* # of parity devices. */
283 const unsigned int minimal_devs;/* minimal # of devices in set. */
284 const unsigned int level; /* RAID level. */
285 const unsigned int algorithm; /* RAID algorithm. */
NeilBrown9d09e662011-01-13 20:00:02 +0000286} raid_types[] = {
Mike Snitzer43157842016-05-30 13:03:37 -0400287 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
288 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
289 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200290 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
Mike Snitzer43157842016-05-30 13:03:37 -0400291 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
292 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
Heinz Mauelshagenb052b072016-10-17 21:20:07 +0200293 {"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
Mike Snitzer43157842016-05-30 13:03:37 -0400294 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
295 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
296 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
297 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
298 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
299 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
300 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
301 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
302 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
303 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
304 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
305 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
306 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
NeilBrown9d09e662011-01-13 20:00:02 +0000307};
308
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200309/* True, if @v is in inclusive range [@min, @max] */
Mike Snitzerbb91a632016-06-02 12:06:54 -0400310static bool __within_range(long v, long min, long max)
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200311{
312 return v >= min && v <= max;
313}
314
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200315/* All table line arguments are defined here */
316static struct arg_name_flag {
Mike Snitzer42863252016-06-02 12:27:46 -0400317 const unsigned long flag;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200318 const char *name;
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400319} __arg_name_flags[] = {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200320 { CTR_FLAG_SYNC, "sync"},
321 { CTR_FLAG_NOSYNC, "nosync"},
322 { CTR_FLAG_REBUILD, "rebuild"},
323 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
324 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
325 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
326 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
Heinz Mauelshagen65359ee2016-06-24 21:32:25 +0200327 { CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200328 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
329 { CTR_FLAG_REGION_SIZE, "region_size"},
330 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
331 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
Heinz Mauelshagen4763e542016-05-19 18:49:31 +0200332 { CTR_FLAG_DATA_OFFSET, "data_offset"},
333 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
334 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100335 { CTR_FLAG_JOURNAL_DEV, "journal_dev" },
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100336 { CTR_FLAG_JOURNAL_MODE, "journal_mode" },
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200337};
338
339/* Return argument name string for given @flag */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -0400340static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200341{
342 if (hweight32(flag) == 1) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400343 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200344
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400345 while (anf-- > __arg_name_flags)
Mike Snitzer42863252016-06-02 12:27:46 -0400346 if (flag & anf->flag)
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200347 return anf->name;
348
349 } else
350 DMERR("%s called with more than one flag!", __func__);
351
352 return NULL;
353}
354
Heinz Mauelshagen6e536362017-03-22 17:44:38 +0100355/* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
356static struct {
357 const int mode;
358 const char *param;
359} _raid456_journal_mode[] = {
360 { R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
361 { R5C_JOURNAL_MODE_WRITE_BACK , "writeback" }
362};
363
364/* Return MD raid4/5/6 journal mode for dm @journal_mode one */
365static int dm_raid_journal_mode_to_md(const char *mode)
366{
367 int m = ARRAY_SIZE(_raid456_journal_mode);
368
369 while (m--)
370 if (!strcasecmp(mode, _raid456_journal_mode[m].param))
371 return _raid456_journal_mode[m].mode;
372
373 return -EINVAL;
374}
375
376/* Return dm-raid raid4/5/6 journal mode string for @mode */
377static const char *md_journal_mode_to_dm_raid(const int mode)
378{
379 int m = ARRAY_SIZE(_raid456_journal_mode);
380
381 while (m--)
382 if (mode == _raid456_journal_mode[m].mode)
383 return _raid456_journal_mode[m].param;
384
385 return "unknown";
386}
387
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200388/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200389 * Bool helpers to test for various raid levels of a raid set.
390 * It's level as reported by the superblock rather than
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200391 * the requested raid_type passed to the constructor.
392 */
393/* Return true, if raid set in @rs is raid0 */
394static bool rs_is_raid0(struct raid_set *rs)
395{
396 return !rs->md.level;
397}
398
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200399/* Return true, if raid set in @rs is raid1 */
400static bool rs_is_raid1(struct raid_set *rs)
401{
402 return rs->md.level == 1;
403}
404
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200405/* Return true, if raid set in @rs is raid10 */
406static bool rs_is_raid10(struct raid_set *rs)
407{
408 return rs->md.level == 10;
409}
410
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200411/* Return true, if raid set in @rs is level 6 */
412static bool rs_is_raid6(struct raid_set *rs)
413{
414 return rs->md.level == 6;
415}
416
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200417/* Return true, if raid set in @rs is level 4, 5 or 6 */
418static bool rs_is_raid456(struct raid_set *rs)
419{
420 return __within_range(rs->md.level, 4, 6);
421}
422
423/* Return true, if raid set in @rs is reshapable */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200424static bool __is_raid10_far(int layout);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +0200425static bool rs_is_reshapable(struct raid_set *rs)
426{
427 return rs_is_raid456(rs) ||
428 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
429}
430
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200431/* Return true, if raid set in @rs is recovering */
432static bool rs_is_recovering(struct raid_set *rs)
433{
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +0100434 return rs->md.recovery_cp < rs->md.dev_sectors;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200435}
436
437/* Return true, if raid set in @rs is reshaping */
438static bool rs_is_reshaping(struct raid_set *rs)
439{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200440 return rs->md.reshape_position != MaxSector;
441}
442
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200443/*
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200444 * bool helpers to test for various raid levels of a raid type @rt
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200445 */
446
447/* Return true, if raid type in @rt is raid0 */
448static bool rt_is_raid0(struct raid_type *rt)
449{
450 return !rt->level;
451}
452
453/* Return true, if raid type in @rt is raid1 */
454static bool rt_is_raid1(struct raid_type *rt)
455{
456 return rt->level == 1;
457}
458
459/* Return true, if raid type in @rt is raid10 */
460static bool rt_is_raid10(struct raid_type *rt)
461{
462 return rt->level == 10;
463}
464
465/* Return true, if raid type in @rt is raid4/5 */
466static bool rt_is_raid45(struct raid_type *rt)
467{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400468 return __within_range(rt->level, 4, 5);
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200469}
470
471/* Return true, if raid type in @rt is raid6 */
472static bool rt_is_raid6(struct raid_type *rt)
473{
474 return rt->level == 6;
475}
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200476
477/* Return true, if raid type in @rt is raid4/5/6 */
478static bool rt_is_raid456(struct raid_type *rt)
479{
Mike Snitzerbb91a632016-06-02 12:06:54 -0400480 return __within_range(rt->level, 4, 6);
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +0200481}
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200482/* END: raid level bools */
483
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200484/* Return valid ctr flags for the raid level of @rs */
485static unsigned long __valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200486{
487 if (rt_is_raid0(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200488 return RAID0_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200489 else if (rt_is_raid1(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200490 return RAID1_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200491 else if (rt_is_raid10(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200492 return RAID10_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200493 else if (rt_is_raid45(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200494 return RAID45_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200495 else if (rt_is_raid6(rs->raid_type))
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200496 return RAID6_VALID_FLAGS;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200497
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200498 return 0;
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200499}
500
501/*
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200502 * Check for valid flags set on @rs
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200503 *
504 * Has to be called after parsing of the ctr flags!
505 */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200506static int rs_check_for_valid_flags(struct raid_set *rs)
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200507{
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +0200508 if (rs->ctr_flags & ~__valid_flags(rs)) {
Mike Snitzer42863252016-06-02 12:27:46 -0400509 rs->ti->error = "Invalid flags combination";
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400510 return -EINVAL;
511 }
Heinz Mauelshagenf0902792016-05-19 18:49:27 +0200512
513 return 0;
514}
515
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200516/* MD raid10 bit definitions and helpers */
517#define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
518#define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
519#define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
520#define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
521
522/* Return md raid10 near copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400523static unsigned int __raid10_near_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200524{
525 return layout & 0xFF;
526}
527
528/* Return md raid10 far copies for @layout */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400529static unsigned int __raid10_far_copies(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200530{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400531 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200532}
533
534/* Return true if md raid10 offset for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200535static bool __is_raid10_offset(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200536{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200537 return !!(layout & RAID10_OFFSET);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200538}
539
540/* Return true if md raid10 near for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200541static bool __is_raid10_near(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200542{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400543 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200544}
545
546/* Return true if md raid10 far for @layout */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200547static bool __is_raid10_far(int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200548{
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400549 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200550}
551
552/* Return md raid10 layout string for @layout */
553static const char *raid10_md_layout_to_format(int layout)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100554{
555 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200556 * Bit 16 stands for "offset"
557 * (i.e. adjacent stripes hold copies)
558 *
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100559 * Refer to MD's raid10.c for details
560 */
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400561 if (__is_raid10_offset(layout))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100562 return "offset";
563
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400564 if (__raid10_near_copies(layout) > 1)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100565 return "near";
566
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400567 WARN_ON(__raid10_far_copies(layout) < 2);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200568
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100569 return "far";
570}
571
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200572/* Return md raid10 algorithm for @name */
Arnd Bergmann68c1c4d2016-06-16 11:03:25 +0200573static int raid10_name_to_format(const char *name)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500574{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200575 if (!strcasecmp(name, "near"))
576 return ALGORITHM_RAID10_NEAR;
577 else if (!strcasecmp(name, "offset"))
578 return ALGORITHM_RAID10_OFFSET;
579 else if (!strcasecmp(name, "far"))
580 return ALGORITHM_RAID10_FAR;
581
582 return -EINVAL;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500583}
584
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200585/* Return md raid10 copies for @layout */
586static unsigned int raid10_md_layout_to_copies(int layout)
587{
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200588 return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200589}
590
591/* Return md raid10 format id for @format string */
592static int raid10_format_to_md_layout(struct raid_set *rs,
593 unsigned int algorithm,
594 unsigned int copies)
595{
596 unsigned int n = 1, f = 1, r = 0;
597
598 /*
599 * MD resilienece flaw:
600 *
601 * enabling use_far_sets for far/offset formats causes copies
602 * to be colocated on the same devs together with their origins!
603 *
604 * -> disable it for now in the definition above
605 */
606 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
607 algorithm == ALGORITHM_RAID10_NEAR)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100608 n = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200609
610 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100611 f = copies;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200612 r = RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400613 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200614 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100615
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200616 } else if (algorithm == ALGORITHM_RAID10_FAR) {
617 f = copies;
618 r = !RAID10_OFFSET;
Mike Snitzer42863252016-06-02 12:27:46 -0400619 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200620 r |= RAID10_USE_FAR_SETS;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100621
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200622 } else
623 return -EINVAL;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100624
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200625 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
626}
627/* END: MD raid10 bit definitions and helpers */
628
629/* Check for any of the raid10 algorithms */
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200630static bool __got_raid10(struct raid_type *rtp, const int layout)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200631{
632 if (rtp->level == 10) {
633 switch (rtp->algorithm) {
634 case ALGORITHM_RAID10_DEFAULT:
635 case ALGORITHM_RAID10_NEAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400636 return __is_raid10_near(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200637 case ALGORITHM_RAID10_OFFSET:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400638 return __is_raid10_offset(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200639 case ALGORITHM_RAID10_FAR:
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400640 return __is_raid10_far(layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200641 default:
642 break;
643 }
644 }
645
Heinz Mauelshagend7ccc2e2016-07-06 18:29:22 +0200646 return false;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500647}
648
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200649/* Return raid_type for @name */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200650static struct raid_type *get_raid_type(const char *name)
NeilBrown9d09e662011-01-13 20:00:02 +0000651{
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200652 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown9d09e662011-01-13 20:00:02 +0000653
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200654 while (rtp-- > raid_types)
655 if (!strcasecmp(rtp->name, name))
656 return rtp;
NeilBrown9d09e662011-01-13 20:00:02 +0000657
658 return NULL;
659}
660
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200661/* Return raid_type for @name based derived from @level and @layout */
662static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
663{
664 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
665
666 while (rtp-- > raid_types) {
667 /* RAID10 special checks based on @layout flags/properties */
668 if (rtp->level == level &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -0400669 (__got_raid10(rtp, layout) || rtp->algorithm == layout))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200670 return rtp;
671 }
672
673 return NULL;
674}
675
676/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200677 * Conditionally change bdev capacity of @rs
678 * in case of a disk add/remove reshape
679 */
680static void rs_set_capacity(struct raid_set *rs)
681{
682 struct mddev *mddev = &rs->md;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200683 struct md_rdev *rdev;
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200684 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200685
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200686 /*
687 * raid10 sets rdev->sector to the device size, which
688 * is unintended in case of out-of-place reshaping
689 */
690 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100691 if (!test_bit(Journal, &rdev->flags))
692 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagenfbe63652016-06-24 00:36:08 +0200693
Heinz Mauelshagen0095dbc2016-06-24 00:10:12 +0200694 set_capacity(gendisk, mddev->array_sectors);
695 revalidate_disk(gendisk);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200696}
697
698/*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200699 * Set the mddev properties in @rs to the current
700 * ones retrieved from the freshest superblock
701 */
702static void rs_set_cur(struct raid_set *rs)
703{
704 struct mddev *mddev = &rs->md;
705
706 mddev->new_level = mddev->level;
707 mddev->new_layout = mddev->layout;
708 mddev->new_chunk_sectors = mddev->chunk_sectors;
709}
710
711/*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200712 * Set the mddev properties in @rs to the new
713 * ones requested by the ctr
714 */
715static void rs_set_new(struct raid_set *rs)
716{
717 struct mddev *mddev = &rs->md;
718
719 mddev->level = mddev->new_level;
720 mddev->layout = mddev->new_layout;
721 mddev->chunk_sectors = mddev->new_chunk_sectors;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200722 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200723 mddev->delta_disks = 0;
724}
725
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400726static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200727 unsigned int raid_devs)
NeilBrown9d09e662011-01-13 20:00:02 +0000728{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200729 unsigned int i;
NeilBrown9d09e662011-01-13 20:00:02 +0000730 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000731
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400732 if (raid_devs <= raid_type->parity_devs) {
733 ti->error = "Insufficient number of devices";
734 return ERR_PTR(-EINVAL);
735 }
NeilBrown9d09e662011-01-13 20:00:02 +0000736
NeilBrown9d09e662011-01-13 20:00:02 +0000737 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400738 if (!rs) {
739 ti->error = "Cannot allocate raid context";
740 return ERR_PTR(-ENOMEM);
741 }
NeilBrown9d09e662011-01-13 20:00:02 +0000742
743 mddev_init(&rs->md);
744
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +0200745 rs->raid_disks = raid_devs;
746 rs->delta_disks = 0;
747
NeilBrown9d09e662011-01-13 20:00:02 +0000748 rs->ti = ti;
749 rs->raid_type = raid_type;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +0200750 rs->stripe_cache_entries = 256;
NeilBrown9d09e662011-01-13 20:00:02 +0000751 rs->md.raid_disks = raid_devs;
752 rs->md.level = raid_type->level;
753 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000754 rs->md.layout = raid_type->algorithm;
755 rs->md.new_layout = rs->md.layout;
756 rs->md.delta_disks = 0;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +0200757 rs->md.recovery_cp = MaxSector;
NeilBrown9d09e662011-01-13 20:00:02 +0000758
759 for (i = 0; i < raid_devs; i++)
760 md_rdev_init(&rs->dev[i].rdev);
761
762 /*
763 * Remaining items to be initialized by further RAID params:
764 * rs->md.persistent
765 * rs->md.external
766 * rs->md.chunk_sectors
767 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100768 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000769 */
770
771 return rs;
772}
773
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400774static void raid_set_free(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +0000775{
776 int i;
777
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100778 if (rs->journal_dev.dev) {
779 md_rdev_clear(&rs->journal_dev.rdev);
780 dm_put_device(rs->ti, rs->journal_dev.dev);
781 }
782
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200783 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100784 if (rs->dev[i].meta_dev)
785 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000786 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000787 if (rs->dev[i].data_dev)
788 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100789 }
NeilBrown9d09e662011-01-13 20:00:02 +0000790
791 kfree(rs);
792}
793
794/*
795 * For every device we have two words
796 * <meta_dev>: meta device name or '-' if missing
797 * <data_dev>: data device name or '-' if missing
798 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100799 * The following are permitted:
800 * - -
801 * - <data_dev>
802 * <meta_dev> <data_dev>
803 *
804 * The following is not allowed:
805 * <meta_dev> -
806 *
807 * This code parses those words. If there is a failure,
Mike Snitzerbfcee0e2016-06-02 15:08:09 -0400808 * the caller must use raid_set_free() to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000809 */
Heinz Mauelshagen702108d2016-05-19 18:49:26 +0200810static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown9d09e662011-01-13 20:00:02 +0000811{
812 int i;
813 int rebuild = 0;
814 int metadata_available = 0;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +0200815 int r = 0;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200816 const char *arg;
NeilBrown9d09e662011-01-13 20:00:02 +0000817
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200818 /* Put off the number of raid devices argument to get to dev pairs */
819 arg = dm_shift_arg(as);
820 if (!arg)
821 return -EINVAL;
822
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +0200823 for (i = 0; i < rs->raid_disks; i++) {
NeilBrown9d09e662011-01-13 20:00:02 +0000824 rs->dev[i].rdev.raid_disk = i;
825
826 rs->dev[i].meta_dev = NULL;
827 rs->dev[i].data_dev = NULL;
828
829 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100830 * There are no offsets initially.
831 * Out of place reshape will set them accordingly.
NeilBrown9d09e662011-01-13 20:00:02 +0000832 */
833 rs->dev[i].rdev.data_offset = 0;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100834 rs->dev[i].rdev.new_data_offset = 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000835 rs->dev[i].rdev.mddev = &rs->md;
836
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200837 arg = dm_shift_arg(as);
838 if (!arg)
839 return -EINVAL;
840
841 if (strcmp(arg, "-")) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400842 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
843 &rs->dev[i].meta_dev);
844 if (r) {
845 rs->ti->error = "RAID metadata device lookup failure";
846 return r;
847 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100848
849 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400850 if (!rs->dev[i].rdev.sb_page) {
851 rs->ti->error = "Failed to allocate superblock page";
852 return -ENOMEM;
853 }
NeilBrown9d09e662011-01-13 20:00:02 +0000854 }
855
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +0200856 arg = dm_shift_arg(as);
857 if (!arg)
858 return -EINVAL;
859
860 if (!strcmp(arg, "-")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000861 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400862 (!rs->dev[i].rdev.recovery_offset)) {
863 rs->ti->error = "Drive designated for rebuild not specified";
864 return -EINVAL;
865 }
NeilBrown9d09e662011-01-13 20:00:02 +0000866
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400867 if (rs->dev[i].meta_dev) {
868 rs->ti->error = "No data device supplied with metadata device";
869 return -EINVAL;
870 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100871
NeilBrown9d09e662011-01-13 20:00:02 +0000872 continue;
873 }
874
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400875 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
876 &rs->dev[i].data_dev);
877 if (r) {
878 rs->ti->error = "RAID device lookup failure";
879 return r;
880 }
NeilBrown9d09e662011-01-13 20:00:02 +0000881
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100882 if (rs->dev[i].meta_dev) {
883 metadata_available = 1;
884 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
885 }
NeilBrown9d09e662011-01-13 20:00:02 +0000886 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +0200887 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown9d09e662011-01-13 20:00:02 +0000888 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
889 rebuild++;
890 }
891
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +0100892 if (rs->journal_dev.dev)
893 list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
894
NeilBrown9d09e662011-01-13 20:00:02 +0000895 if (metadata_available) {
896 rs->md.external = 0;
897 rs->md.persistent = 1;
898 rs->md.major_version = 2;
899 } else if (rebuild && !rs->md.recovery_cp) {
900 /*
901 * Without metadata, we will not be able to tell if the array
902 * is in-sync or not - we must assume it is not. Therefore,
903 * it is impossible to rebuild a drive.
904 *
905 * Even if there is metadata, the on-disk information may
906 * indicate that the array is not in-sync and it will then
907 * fail at that time.
908 *
909 * User could specify 'nosync' option if desperate.
910 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400911 rs->ti->error = "Unable to rebuild drive while array is not in-sync";
912 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000913 }
914
915 return 0;
916}
917
918/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100919 * validate_region_size
920 * @rs
921 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
922 *
923 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
924 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
925 *
926 * Returns: 0 on success, -EINVAL on failure.
927 */
928static int validate_region_size(struct raid_set *rs, unsigned long region_size)
929{
930 unsigned long min_region_size = rs->ti->len / (1 << 21);
931
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +0200932 if (rs_is_raid0(rs))
933 return 0;
934
Jonathan Brassowc1084562011-08-02 12:32:07 +0100935 if (!region_size) {
936 /*
Mike Snitzer43157842016-05-30 13:03:37 -0400937 * Choose a reasonable default. All figures in sectors.
Jonathan Brassowc1084562011-08-02 12:32:07 +0100938 */
939 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000940 /* If not a power of 2, make it the next power of 2 */
Mikulas Patocka042745e2015-10-02 11:17:37 -0400941 region_size = roundup_pow_of_two(min_region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100942 DMINFO("Choosing default region size of %lu sectors",
943 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100944 } else {
945 DMINFO("Choosing default region size of 4MiB");
946 region_size = 1 << 13; /* sectors */
947 }
948 } else {
949 /*
950 * Validate user-supplied value.
951 */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400952 if (region_size > rs->ti->len) {
953 rs->ti->error = "Supplied region size is too large";
954 return -EINVAL;
955 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100956
957 if (region_size < min_region_size) {
958 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
959 region_size, min_region_size);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400960 rs->ti->error = "Supplied region size is too small";
961 return -EINVAL;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100962 }
963
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400964 if (!is_power_of_2(region_size)) {
965 rs->ti->error = "Region size is not a power of 2";
966 return -EINVAL;
967 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100968
Mike Snitzerbd83a4c2016-05-31 14:26:52 -0400969 if (region_size < rs->md.chunk_sectors) {
970 rs->ti->error = "Region size is smaller than the chunk size";
971 return -EINVAL;
972 }
Jonathan Brassowc1084562011-08-02 12:32:07 +0100973 }
974
975 /*
976 * Convert sectors to bytes.
977 */
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +0200978 rs->md.bitmap_info.chunksize = to_bytes(region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100979
980 return 0;
981}
982
983/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600984 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100985 * @rs
986 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600987 * Determine if there are enough devices in the array that haven't
988 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100989 *
990 * Returns: 0 on success, -EINVAL on failure.
991 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600992static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100993{
Heinz Mauelshagen094f3942016-07-19 14:03:51 +0200994 unsigned int i, rebuild_cnt = 0;
995 unsigned int rebuilds_per_group = 0, copies;
996 unsigned int group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100997
Jonathan Brassoweb649122012-10-11 13:40:09 +1100998 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600999 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
1000 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +11001001 rebuild_cnt++;
1002
1003 switch (rs->raid_type->level) {
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02001004 case 0:
1005 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001006 case 1:
1007 if (rebuild_cnt >= rs->md.raid_disks)
1008 goto too_many;
1009 break;
1010 case 4:
1011 case 5:
1012 case 6:
1013 if (rebuild_cnt > rs->raid_type->parity_devs)
1014 goto too_many;
1015 break;
1016 case 10:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001017 copies = raid10_md_layout_to_copies(rs->md.new_layout);
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001018 if (rebuild_cnt < copies)
1019 break;
1020
1021 /*
1022 * It is possible to have a higher rebuild count for RAID10,
1023 * as long as the failed devices occur in different mirror
1024 * groups (i.e. different stripes).
1025 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001026 * When checking "near" format, make sure no adjacent devices
1027 * have failed beyond what can be handled. In addition to the
1028 * simple case where the number of devices is a multiple of the
1029 * number of copies, we must also handle cases where the number
1030 * of devices is not a multiple of the number of copies.
Mike Snitzer43157842016-05-30 13:03:37 -04001031 * E.g. dev1 dev2 dev3 dev4 dev5
1032 * A A B B C
1033 * C D D E E
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001034 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001035 if (__is_raid10_near(rs->md.new_layout)) {
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001036 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001037 if (!(i % copies))
1038 rebuilds_per_group = 0;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001039 if ((!rs->dev[i].rdev.sb_page ||
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001040 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001041 (++rebuilds_per_group >= copies))
1042 goto too_many;
1043 }
1044 break;
1045 }
1046
1047 /*
1048 * When checking "far" and "offset" formats, we need to ensure
1049 * that the device that holds its copy is not also dead or
1050 * being rebuilt. (Note that "far" and "offset" formats only
1051 * support two copies right now. These formats also only ever
1052 * use the 'use_far_sets' variant.)
1053 *
1054 * This check is somewhat complicated by the need to account
Mike Snitzer43157842016-05-30 13:03:37 -04001055 * for arrays that are not a multiple of (far) copies. This
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001056 * results in the need to treat the last (potentially larger)
1057 * set differently.
1058 */
1059 group_size = (rs->md.raid_disks / copies);
1060 last_group_start = (rs->md.raid_disks / group_size) - 1;
1061 last_group_start *= group_size;
1062 for (i = 0; i < rs->md.raid_disks; i++) {
1063 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001064 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001065 if ((!rs->dev[i].rdev.sb_page ||
1066 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001067 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001068 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +11001069 }
1070 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001071 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001072 if (rebuild_cnt)
1073 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +11001074 }
1075
1076 return 0;
1077
1078too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +11001079 return -EINVAL;
1080}
1081
1082/*
NeilBrown9d09e662011-01-13 20:00:02 +00001083 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +00001084 * <chunk_size> [optional_args]
1085 *
Jonathan Brassow32737272011-08-02 12:32:07 +01001086 * Argument definitions
1087 * <chunk_size> The number of sectors per disk that
Mike Snitzer43157842016-05-30 13:03:37 -04001088 * will form the "stripe"
Jonathan Brassow32737272011-08-02 12:32:07 +01001089 * [[no]sync] Force or prevent recovery of the
Mike Snitzer43157842016-05-30 13:03:37 -04001090 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +00001091 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +01001092 * [daemon_sleep <ms>] Time between bitmap daemon work to
Mike Snitzer43157842016-05-30 13:03:37 -04001093 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +00001094 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
1095 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001096 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +00001097 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
1098 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Mike Snitzer43157842016-05-30 13:03:37 -04001099 * [region_size <sectors>] Defines granularity of bitmap
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001100 * [journal_dev <dev>] raid4/5/6 journaling deviice
1101 * (i.e. write hole closing log)
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001102 *
1103 * RAID10-only options:
Mike Snitzer43157842016-05-30 13:03:37 -04001104 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001105 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +00001106 */
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001107static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001108 unsigned int num_raid_params)
NeilBrown9d09e662011-01-13 20:00:02 +00001109{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001110 int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001111 unsigned int raid10_copies = 2;
1112 unsigned int i, write_mostly = 0;
1113 unsigned int region_size = 0;
Mike Snitzer542f9032012-07-27 15:08:00 +01001114 sector_t max_io_len;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001115 const char *arg, *key;
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001116 struct raid_dev *rd;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001117 struct raid_type *rt = rs->raid_type;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001118
1119 arg = dm_shift_arg(as);
1120 num_raid_params--; /* Account for chunk_size argument */
1121
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001122 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001123 rs->ti->error = "Bad numerical argument given for chunk_size";
1124 return -EINVAL;
1125 }
NeilBrown9d09e662011-01-13 20:00:02 +00001126
1127 /*
1128 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +01001129 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +00001130 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001131 if (rt_is_raid1(rt)) {
Jonathan Brassow32737272011-08-02 12:32:07 +01001132 if (value)
1133 DMERR("Ignoring chunk size parameter for RAID 1");
1134 value = 0;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001135 } else if (!is_power_of_2(value)) {
1136 rs->ti->error = "Chunk size must be a power of 2";
1137 return -EINVAL;
1138 } else if (value < 8) {
1139 rs->ti->error = "Chunk size value is too small";
1140 return -EINVAL;
1141 }
NeilBrown9d09e662011-01-13 20:00:02 +00001142
1143 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001144
1145 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001146 * We set each individual device as In_sync with a completed
1147 * 'recovery_offset'. If there has been a device failure or
1148 * replacement then one of the following cases applies:
1149 *
1150 * 1) User specifies 'rebuild'.
Mike Snitzer43157842016-05-30 13:03:37 -04001151 * - Device is reset when param is read.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001152 * 2) A new device is supplied.
Mike Snitzer43157842016-05-30 13:03:37 -04001153 * - No matching superblock found, resets device.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001154 * 3) Device failure was transient and returns on reload.
Mike Snitzer43157842016-05-30 13:03:37 -04001155 * - Failure noticed, resets device for bitmap replay.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001156 * 4) Device hadn't completed recovery after previous failure.
Mike Snitzer43157842016-05-30 13:03:37 -04001157 * - Superblock is read and overrides recovery_offset.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001158 *
1159 * What is found in the superblocks of the devices is always
1160 * authoritative, unless 'rebuild' or '[no]sync' was specified.
1161 */
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02001162 for (i = 0; i < rs->raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001163 set_bit(In_sync, &rs->dev[i].rdev.flags);
1164 rs->dev[i].rdev.recovery_offset = MaxSector;
1165 }
1166
1167 /*
NeilBrown9d09e662011-01-13 20:00:02 +00001168 * Second, parse the unordered optional arguments
1169 */
NeilBrown9d09e662011-01-13 20:00:02 +00001170 for (i = 0; i < num_raid_params; i++) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001171 key = dm_shift_arg(as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001172 if (!key) {
1173 rs->ti->error = "Not enough raid parameters given";
1174 return -EINVAL;
1175 }
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001176
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001177 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001178 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001179 rs->ti->error = "Only one 'nosync' argument allowed";
1180 return -EINVAL;
1181 }
NeilBrown9d09e662011-01-13 20:00:02 +00001182 continue;
1183 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001184 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001185 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001186 rs->ti->error = "Only one 'sync' argument allowed";
1187 return -EINVAL;
1188 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001189 continue;
1190 }
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001191 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001192 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001193 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
1194 return -EINVAL;
1195 }
NeilBrown9d09e662011-01-13 20:00:02 +00001196 continue;
1197 }
1198
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02001199 arg = dm_shift_arg(as);
1200 i++; /* Account for the argument pairs */
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001201 if (!arg) {
1202 rs->ti->error = "Wrong number of raid parameters given";
1203 return -EINVAL;
1204 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001205
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001206 /*
1207 * Parameters that take a string value are checked here.
1208 */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001209 /* "raid10_format {near|offset|far} */
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001210 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001211 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001212 rs->ti->error = "Only one 'raid10_format' argument pair allowed";
1213 return -EINVAL;
1214 }
1215 if (!rt_is_raid10(rt)) {
1216 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
1217 return -EINVAL;
1218 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001219 raid10_format = raid10_name_to_format(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001220 if (raid10_format < 0) {
1221 rs->ti->error = "Invalid 'raid10_format' value given";
1222 return raid10_format;
1223 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001224 continue;
1225 }
1226
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001227 /* "journal_dev <dev>" */
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001228 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
1229 int r;
1230 struct md_rdev *jdev;
1231
1232 if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1233 rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
1234 return -EINVAL;
1235 }
1236 if (!rt_is_raid456(rt)) {
1237 rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
1238 return -EINVAL;
1239 }
1240 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
1241 &rs->journal_dev.dev);
1242 if (r) {
1243 rs->ti->error = "raid4/5/6 journal device lookup failure";
1244 return r;
1245 }
1246 jdev = &rs->journal_dev.rdev;
1247 md_rdev_init(jdev);
1248 jdev->mddev = &rs->md;
1249 jdev->bdev = rs->journal_dev.dev->bdev;
1250 jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
1251 if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
1252 rs->ti->error = "No space for raid4/5/6 journal";
1253 return -ENOSPC;
1254 }
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001255 rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001256 set_bit(Journal, &jdev->flags);
1257 continue;
1258 }
1259
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01001260 /* "journal_mode <mode>" ("journal_dev" mandatory!) */
1261 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
1262 int r;
1263
1264 if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
1265 rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
1266 return -EINVAL;
1267 }
1268 if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
1269 rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
1270 return -EINVAL;
1271 }
1272 r = dm_raid_journal_mode_to_md(arg);
1273 if (r < 0) {
1274 rs->ti->error = "Invalid 'journal_mode' argument";
1275 return r;
1276 }
1277 rs->journal_dev.mode = r;
1278 continue;
1279 }
1280
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001281 /*
1282 * Parameters with number values from here on.
1283 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001284 if (kstrtoint(arg, 10, &value) < 0) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001285 rs->ti->error = "Bad numerical argument given in raid params";
1286 return -EINVAL;
1287 }
NeilBrown9d09e662011-01-13 20:00:02 +00001288
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001289 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001290 /*
1291 * "rebuild" is being passed in by userspace to provide
1292 * indexes of replaced devices and to set up additional
1293 * devices on raid level takeover.
Mike Snitzer43157842016-05-30 13:03:37 -04001294 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001295 if (!__within_range(value, 0, rs->raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001296 rs->ti->error = "Invalid rebuild index given";
1297 return -EINVAL;
1298 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001299
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001300 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
1301 rs->ti->error = "rebuild for this index already given";
1302 return -EINVAL;
1303 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001304
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001305 rd = rs->dev + value;
1306 clear_bit(In_sync, &rd->rdev.flags);
1307 clear_bit(Faulty, &rd->rdev.flags);
1308 rd->rdev.recovery_offset = 0;
Mike Snitzer42863252016-06-02 12:27:46 -04001309 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001310 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001311 if (!rt_is_raid1(rt)) {
1312 rs->ti->error = "write_mostly option is only valid for RAID1";
1313 return -EINVAL;
1314 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001315
Mike Snitzerbb91a632016-06-02 12:06:54 -04001316 if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001317 rs->ti->error = "Invalid write_mostly index given";
1318 return -EINVAL;
1319 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001320
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001321 write_mostly++;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001322 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Mike Snitzer42863252016-06-02 12:27:46 -04001323 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001324 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001325 if (!rt_is_raid1(rt)) {
1326 rs->ti->error = "max_write_behind option is only valid for RAID1";
1327 return -EINVAL;
1328 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001329
Mike Snitzer42863252016-06-02 12:27:46 -04001330 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001331 rs->ti->error = "Only one max_write_behind argument pair allowed";
1332 return -EINVAL;
1333 }
NeilBrown9d09e662011-01-13 20:00:02 +00001334
1335 /*
1336 * In device-mapper, we specify things in sectors, but
1337 * MD records this value in kB
1338 */
1339 value /= 2;
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001340 if (value > COUNTER_MAX) {
1341 rs->ti->error = "Max write-behind limit out of range";
1342 return -EINVAL;
1343 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001344
NeilBrown9d09e662011-01-13 20:00:02 +00001345 rs->md.bitmap_info.max_write_behind = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001346 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001347 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001348 rs->ti->error = "Only one daemon_sleep argument pair allowed";
1349 return -EINVAL;
1350 }
1351 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
1352 rs->ti->error = "daemon sleep period out of range";
1353 return -EINVAL;
1354 }
NeilBrown9d09e662011-01-13 20:00:02 +00001355 rs->md.bitmap_info.daemon_sleep = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001356 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001357 /* Userspace passes new data_offset after having extended the the data image LV */
Mike Snitzer42863252016-06-02 12:27:46 -04001358 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001359 rs->ti->error = "Only one data_offset argument pair allowed";
1360 return -EINVAL;
1361 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001362 /* Ensure sensible data offset */
Heinz Mauelshagen75dd3b92016-06-15 22:27:08 +02001363 if (value < 0 ||
1364 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001365 rs->ti->error = "Bogus data_offset value";
1366 return -EINVAL;
1367 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001368 rs->data_offset = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001369 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001370 /* Define the +/-# of disks to add to/remove from the given raid set */
Mike Snitzer42863252016-06-02 12:27:46 -04001371 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001372 rs->ti->error = "Only one delta_disks argument pair allowed";
1373 return -EINVAL;
1374 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001375 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001376 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001377 rs->ti->error = "Too many delta_disk requested";
1378 return -EINVAL;
1379 }
Heinz Mauelshagen4763e542016-05-19 18:49:31 +02001380
1381 rs->delta_disks = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001382 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001383 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001384 rs->ti->error = "Only one stripe_cache argument pair allowed";
1385 return -EINVAL;
1386 }
1387
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001388 if (!rt_is_raid456(rt)) {
1389 rs->ti->error = "Inappropriate argument: stripe_cache";
1390 return -EINVAL;
1391 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001392
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001393 rs->stripe_cache_entries = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001394 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001395 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001396 rs->ti->error = "Only one min_recovery_rate argument pair allowed";
1397 return -EINVAL;
1398 }
1399 if (value > INT_MAX) {
1400 rs->ti->error = "min_recovery_rate out of range";
1401 return -EINVAL;
1402 }
NeilBrown9d09e662011-01-13 20:00:02 +00001403 rs->md.sync_speed_min = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001404 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
Heinz Mauelshagenf15f64d2016-07-27 23:34:01 +02001405 if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001406 rs->ti->error = "Only one max_recovery_rate argument pair allowed";
1407 return -EINVAL;
1408 }
1409 if (value > INT_MAX) {
1410 rs->ti->error = "max_recovery_rate out of range";
1411 return -EINVAL;
1412 }
NeilBrown9d09e662011-01-13 20:00:02 +00001413 rs->md.sync_speed_max = (int)value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001414 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001415 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001416 rs->ti->error = "Only one region_size argument pair allowed";
1417 return -EINVAL;
1418 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001419
Jonathan Brassowc1084562011-08-02 12:32:07 +01001420 region_size = value;
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02001421 rs->requested_bitmap_chunk_sectors = value;
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04001422 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
Mike Snitzer42863252016-06-02 12:27:46 -04001423 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001424 rs->ti->error = "Only one raid10_copies argument pair allowed";
1425 return -EINVAL;
1426 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001427
Mike Snitzerbb91a632016-06-02 12:06:54 -04001428 if (!__within_range(value, 2, rs->md.raid_disks)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001429 rs->ti->error = "Bad value for 'raid10_copies'";
1430 return -EINVAL;
1431 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001432
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001433 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +00001434 } else {
1435 DMERR("Unable to parse RAID parameter: %s", key);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001436 rs->ti->error = "Unable to parse RAID parameter";
1437 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00001438 }
1439 }
1440
Heinz Mauelshagen0d851d12016-06-15 18:43:55 +02001441 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
1442 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
1443 rs->ti->error = "sync and nosync are mutually exclusive";
1444 return -EINVAL;
1445 }
1446
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02001447 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
1448 (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
1449 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
1450 rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
1451 return -EINVAL;
1452 }
1453
Heinz Mauelshagen5fa146b2016-06-15 18:50:18 +02001454 if (write_mostly >= rs->md.raid_disks) {
1455 rs->ti->error = "Can't set all raid1 devices to write_mostly";
1456 return -EINVAL;
1457 }
1458
Jonathan Brassowc1084562011-08-02 12:32:07 +01001459 if (validate_region_size(rs, region_size))
1460 return -EINVAL;
1461
1462 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +01001463 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001464 else
Mike Snitzer542f9032012-07-27 15:08:00 +01001465 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +01001466
Mike Snitzer542f9032012-07-27 15:08:00 +01001467 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1468 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +01001469
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001470 if (rt_is_raid10(rt)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001471 if (raid10_copies > rs->md.raid_disks) {
1472 rs->ti->error = "Not enough devices to satisfy specification";
1473 return -EINVAL;
1474 }
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001475
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001476 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001477 if (rs->md.new_layout < 0) {
1478 rs->ti->error = "Error getting raid10 format";
1479 return rs->md.new_layout;
1480 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001481
1482 rt = get_raid_type_by_ll(10, rs->md.new_layout);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001483 if (!rt) {
1484 rs->ti->error = "Failed to recognize new raid10 layout";
1485 return -EINVAL;
1486 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001487
1488 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1489 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
Mike Snitzer42863252016-06-02 12:27:46 -04001490 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001491 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
1492 return -EINVAL;
1493 }
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001494 }
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02001495
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001496 rs->raid10_copies = raid10_copies;
Jonathan E Brassowc039c332012-07-27 15:08:04 +01001497
NeilBrown9d09e662011-01-13 20:00:02 +00001498 /* Assume there are no metadata devices until the drives are parsed */
1499 rs->md.persistent = 0;
1500 rs->md.external = 1;
1501
Heinz Mauelshagenf0902792016-05-19 18:49:27 +02001502 /* Check, if any invalid ctr arguments have been passed in for the raid level */
Heinz Mauelshagena30cbc02016-06-09 16:42:16 +02001503 return rs_check_for_valid_flags(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001504}
1505
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001506/* Set raid4/5/6 cache size */
1507static int rs_set_raid456_stripe_cache(struct raid_set *rs)
1508{
1509 int r;
1510 struct r5conf *conf;
1511 struct mddev *mddev = &rs->md;
1512 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
1513 uint32_t nr_stripes = rs->stripe_cache_entries;
1514
1515 if (!rt_is_raid456(rs->raid_type)) {
1516 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
1517 return -EINVAL;
1518 }
1519
1520 if (nr_stripes < min_stripes) {
1521 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
1522 nr_stripes, min_stripes);
1523 nr_stripes = min_stripes;
1524 }
1525
1526 conf = mddev->private;
1527 if (!conf) {
1528 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
1529 return -EINVAL;
1530 }
1531
1532 /* Try setting number of stripes in raid456 stripe cache */
1533 if (conf->min_nr_stripes != nr_stripes) {
1534 r = raid5_set_cache_size(mddev, nr_stripes);
1535 if (r) {
1536 rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
1537 return r;
1538 }
1539
1540 DMINFO("%u stripe cache entries", nr_stripes);
1541 }
1542
1543 return 0;
1544}
1545
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02001546/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
1547static unsigned int mddev_data_stripes(struct raid_set *rs)
1548{
1549 return rs->md.raid_disks - rs->raid_type->parity_devs;
1550}
1551
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001552/* Return # of data stripes of @rs (i.e. as of ctr) */
1553static unsigned int rs_data_stripes(struct raid_set *rs)
1554{
1555 return rs->raid_disks - rs->raid_type->parity_devs;
1556}
1557
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001558/*
1559 * Retrieve rdev->sectors from any valid raid device of @rs
1560 * to allow userpace to pass in arbitray "- -" device tupples.
1561 */
1562static sector_t __rdev_sectors(struct raid_set *rs)
1563{
1564 int i;
1565
1566 for (i = 0; i < rs->md.raid_disks; i++) {
1567 struct md_rdev *rdev = &rs->dev[i].rdev;
1568
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001569 if (!test_bit(Journal, &rdev->flags) &&
1570 rdev->bdev && rdev->sectors)
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001571 return rdev->sectors;
1572 }
1573
1574 BUG(); /* Constructor ensures we got some. */
1575}
1576
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001577/* Calculate the sectors per device and per array used for @rs */
1578static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
1579{
1580 int delta_disks;
1581 unsigned int data_stripes;
1582 struct mddev *mddev = &rs->md;
1583 struct md_rdev *rdev;
1584 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
1585
1586 if (use_mddev) {
1587 delta_disks = mddev->delta_disks;
1588 data_stripes = mddev_data_stripes(rs);
1589 } else {
1590 delta_disks = rs->delta_disks;
1591 data_stripes = rs_data_stripes(rs);
1592 }
1593
1594 /* Special raid1 case w/o delta_disks support (yet) */
1595 if (rt_is_raid1(rs->raid_type))
1596 ;
1597 else if (rt_is_raid10(rs->raid_type)) {
1598 if (rs->raid10_copies < 2 ||
1599 delta_disks < 0) {
1600 rs->ti->error = "Bogus raid10 data copies or delta disks";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001601 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001602 }
1603
1604 dev_sectors *= rs->raid10_copies;
1605 if (sector_div(dev_sectors, data_stripes))
1606 goto bad;
1607
1608 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1609 if (sector_div(array_sectors, rs->raid10_copies))
1610 goto bad;
1611
1612 } else if (sector_div(dev_sectors, data_stripes))
1613 goto bad;
1614
1615 else
1616 /* Striped layouts */
1617 array_sectors = (data_stripes + delta_disks) * dev_sectors;
1618
1619 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01001620 if (!test_bit(Journal, &rdev->flags))
1621 rdev->sectors = dev_sectors;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001622
1623 mddev->array_sectors = array_sectors;
1624 mddev->dev_sectors = dev_sectors;
1625
1626 return 0;
1627bad:
1628 rs->ti->error = "Target length not divisible by number of data devices";
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02001629 return -EINVAL;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001630}
1631
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001632/* Setup recovery on @rs */
1633static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1634{
1635 /* raid0 does not recover */
1636 if (rs_is_raid0(rs))
1637 rs->md.recovery_cp = MaxSector;
1638 /*
1639 * A raid6 set has to be recovered either
1640 * completely or for the grown part to
1641 * ensure proper parity and Q-Syndrome
1642 */
1643 else if (rs_is_raid6(rs))
1644 rs->md.recovery_cp = dev_sectors;
1645 /*
1646 * Other raid set types may skip recovery
1647 * depending on the 'nosync' flag.
1648 */
1649 else
1650 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
1651 ? MaxSector : dev_sectors;
1652}
1653
1654/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
1655static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
1656{
1657 if (!dev_sectors)
1658 /* New raid set or 'sync' flag provided */
1659 __rs_setup_recovery(rs, 0);
1660 else if (dev_sectors == MaxSector)
1661 /* Prevent recovery */
1662 __rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001663 else if (__rdev_sectors(rs) < dev_sectors)
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001664 /* Grown raid set */
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01001665 __rs_setup_recovery(rs, __rdev_sectors(rs));
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02001666 else
1667 __rs_setup_recovery(rs, MaxSector);
1668}
1669
NeilBrown9d09e662011-01-13 20:00:02 +00001670static void do_table_event(struct work_struct *ws)
1671{
1672 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1673
Heinz Mauelshagen9d9d9392016-06-16 03:15:49 +02001674 smp_rmb(); /* Make sure we access most actual mddev properties */
1675 if (!rs_is_reshaping(rs))
1676 rs_set_capacity(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00001677 dm_table_event(rs->ti->table);
1678}
1679
1680static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1681{
1682 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1683
NeilBrown5c675f82014-12-15 12:56:56 +11001684 return mddev_congested(&rs->md, bits);
NeilBrown9d09e662011-01-13 20:00:02 +00001685}
1686
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001687/*
1688 * Make sure a valid takover (level switch) is being requested on @rs
1689 *
1690 * Conversions of raid sets from one MD personality to another
1691 * have to conform to restrictions which are enforced here.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001692 */
1693static int rs_check_takeover(struct raid_set *rs)
1694{
1695 struct mddev *mddev = &rs->md;
1696 unsigned int near_copies;
1697
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001698 if (rs->md.degraded) {
1699 rs->ti->error = "Can't takeover degraded raid set";
1700 return -EPERM;
1701 }
1702
1703 if (rs_is_reshaping(rs)) {
1704 rs->ti->error = "Can't takeover reshaping raid set";
1705 return -EPERM;
1706 }
1707
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001708 switch (mddev->level) {
1709 case 0:
1710 /* raid0 -> raid1/5 with one disk */
1711 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1712 mddev->raid_disks == 1)
1713 return 0;
1714
1715 /* raid0 -> raid10 */
1716 if (mddev->new_level == 10 &&
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001717 !(rs->raid_disks % mddev->raid_disks))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001718 return 0;
1719
1720 /* raid0 with multiple disks -> raid4/5/6 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001721 if (__within_range(mddev->new_level, 4, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001722 mddev->new_layout == ALGORITHM_PARITY_N &&
1723 mddev->raid_disks > 1)
1724 return 0;
1725
1726 break;
1727
1728 case 10:
1729 /* Can't takeover raid10_offset! */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001730 if (__is_raid10_offset(mddev->layout))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001731 break;
1732
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001733 near_copies = __raid10_near_copies(mddev->layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001734
1735 /* raid10* -> raid0 */
1736 if (mddev->new_level == 0) {
1737 /* Can takeover raid10_near with raid disks divisable by data copies! */
1738 if (near_copies > 1 &&
1739 !(mddev->raid_disks % near_copies)) {
1740 mddev->raid_disks /= near_copies;
1741 mddev->delta_disks = mddev->raid_disks;
1742 return 0;
1743 }
1744
1745 /* Can takeover raid10_far */
1746 if (near_copies == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001747 __raid10_far_copies(mddev->layout) > 1)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001748 return 0;
1749
1750 break;
1751 }
1752
1753 /* raid10_{near,far} -> raid1 */
1754 if (mddev->new_level == 1 &&
Mike Snitzere6ca5e12016-06-02 15:27:22 -04001755 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001756 return 0;
1757
1758 /* raid10_{near,far} with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001759 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001760 mddev->raid_disks == 2)
1761 return 0;
1762 break;
1763
1764 case 1:
1765 /* raid1 with 2 disks -> raid4/5 */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001766 if (__within_range(mddev->new_level, 4, 5) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001767 mddev->raid_disks == 2) {
1768 mddev->degraded = 1;
1769 return 0;
1770 }
1771
1772 /* raid1 -> raid0 */
1773 if (mddev->new_level == 0 &&
1774 mddev->raid_disks == 1)
1775 return 0;
1776
1777 /* raid1 -> raid10 */
1778 if (mddev->new_level == 10)
1779 return 0;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001780 break;
1781
1782 case 4:
1783 /* raid4 -> raid0 */
1784 if (mddev->new_level == 0)
1785 return 0;
1786
1787 /* raid4 -> raid1/5 with 2 disks */
1788 if ((mddev->new_level == 1 || mddev->new_level == 5) &&
1789 mddev->raid_disks == 2)
1790 return 0;
1791
1792 /* raid4 -> raid5/6 with parity N */
Mike Snitzerbb91a632016-06-02 12:06:54 -04001793 if (__within_range(mddev->new_level, 5, 6) &&
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001794 mddev->layout == ALGORITHM_PARITY_N)
1795 return 0;
1796 break;
1797
1798 case 5:
1799 /* raid5 with parity N -> raid0 */
1800 if (mddev->new_level == 0 &&
1801 mddev->layout == ALGORITHM_PARITY_N)
1802 return 0;
1803
1804 /* raid5 with parity N -> raid4 */
1805 if (mddev->new_level == 4 &&
1806 mddev->layout == ALGORITHM_PARITY_N)
1807 return 0;
1808
1809 /* raid5 with 2 disks -> raid1/4/10 */
1810 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
1811 mddev->raid_disks == 2)
1812 return 0;
1813
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001814 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001815 if (mddev->new_level == 6 &&
1816 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001817 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001818 return 0;
1819 break;
1820
1821 case 6:
1822 /* raid6 with parity N -> raid0 */
1823 if (mddev->new_level == 0 &&
1824 mddev->layout == ALGORITHM_PARITY_N)
1825 return 0;
1826
1827 /* raid6 with parity N -> raid4 */
1828 if (mddev->new_level == 4 &&
1829 mddev->layout == ALGORITHM_PARITY_N)
1830 return 0;
1831
Heinz Mauelshagen6ee0bae2016-06-15 22:29:09 +02001832 /* raid6_*_n with Q-Syndrome N -> raid5_* */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001833 if (mddev->new_level == 5 &&
1834 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
Mike Snitzerbb91a632016-06-02 12:06:54 -04001835 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001836 return 0;
1837
1838 default:
1839 break;
1840 }
1841
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04001842 rs->ti->error = "takeover not possible";
1843 return -EINVAL;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02001844}
1845
1846/* True if @rs requested to be taken over */
1847static bool rs_takeover_requested(struct raid_set *rs)
1848{
1849 return rs->md.new_level != rs->md.level;
1850}
1851
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001852/* True if @rs is requested to reshape by ctr */
1853static bool rs_reshape_requested(struct raid_set *rs)
1854{
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001855 bool change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001856 struct mddev *mddev = &rs->md;
1857
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001858 if (rs_takeover_requested(rs))
1859 return false;
1860
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001861 if (!mddev->level)
1862 return false;
1863
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001864 change = mddev->new_layout != mddev->layout ||
1865 mddev->new_chunk_sectors != mddev->chunk_sectors ||
1866 rs->delta_disks;
1867
1868 /* Historical case to support raid1 reshape without delta disks */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001869 if (mddev->level == 1) {
1870 if (rs->delta_disks)
1871 return !!rs->delta_disks;
1872
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001873 return !change &&
1874 mddev->raid_disks != rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001875 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001876
1877 if (mddev->level == 10)
1878 return change &&
1879 !__is_raid10_far(mddev->new_layout) &&
1880 rs->delta_disks >= 0;
1881
1882 return change;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02001883}
1884
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001885/* Features */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001886#define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001887
1888/* State flags for sb->flags */
1889#define SB_FLAG_RESHAPE_ACTIVE 0x1
1890#define SB_FLAG_RESHAPE_BACKWARDS 0x2
1891
NeilBrown9d09e662011-01-13 20:00:02 +00001892/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001893 * This structure is never routinely used by userspace, unlike md superblocks.
1894 * Devices with this superblock should only ever be accessed via device-mapper.
1895 */
1896#define DM_RAID_MAGIC 0x64526D44
1897struct dm_raid_superblock {
1898 __le32 magic; /* "DmRd" */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001899 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001900
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001901 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1902 __le32 array_position; /* The position of this drive in the raid set */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001903
1904 __le64 events; /* Incremented by md when superblock updated */
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001905 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001906 /* indicate failures (see extension below) */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001907
1908 /*
1909 * This offset tracks the progress of the repair or replacement of
1910 * an individual drive.
1911 */
1912 __le64 disk_recovery_offset;
1913
1914 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001915 * This offset tracks the progress of the initial raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001916 * synchronisation/parity calculation.
1917 */
1918 __le64 array_resync_offset;
1919
1920 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001921 * raid characteristics
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001922 */
1923 __le32 level;
1924 __le32 layout;
1925 __le32 stripe_sectors;
1926
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001927 /********************************************************************
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001928 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001929 *
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001930 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001931 */
1932
1933 __le32 flags; /* Flags defining array states for reshaping */
1934
1935 /*
1936 * This offset tracks the progress of a raid
1937 * set reshape in order to be able to restart it
1938 */
1939 __le64 reshape_position;
1940
1941 /*
1942 * These define the properties of the array in case of an interrupted reshape
1943 */
1944 __le32 new_level;
1945 __le32 new_layout;
1946 __le32 new_stripe_sectors;
1947 __le32 delta_disks;
1948
1949 __le64 array_sectors; /* Array size in sectors */
1950
1951 /*
1952 * Sector offsets to data on devices (reshaping).
1953 * Needed to support out of place reshaping, thus
1954 * not writing over any stripes whilst converting
1955 * them from old to new layout
1956 */
1957 __le64 data_offset;
1958 __le64 new_data_offset;
1959
1960 __le64 sectors; /* Used device size in sectors */
1961
1962 /*
1963 * Additonal Bit field of devices indicating failures to support
Mike Snitzer9b6e5422016-06-02 11:48:09 -04001964 * up to 256 devices with the 1.9.0 on-disk metadata format
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02001965 */
1966 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1967
1968 __le32 incompat_features; /* Used to indicate any incompatible features */
1969
1970 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001971} __packed;
1972
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001973/*
1974 * Check for reshape constraints on raid set @rs:
1975 *
1976 * - reshape function non-existent
1977 * - degraded set
1978 * - ongoing recovery
1979 * - ongoing reshape
1980 *
1981 * Returns 0 if none or -EPERM if given constraint
1982 * and error message reference in @errmsg
1983 */
1984static int rs_check_reshape(struct raid_set *rs)
1985{
1986 struct mddev *mddev = &rs->md;
1987
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001988 if (!mddev->pers || !mddev->pers->check_reshape)
1989 rs->ti->error = "Reshape not supported";
1990 else if (mddev->degraded)
1991 rs->ti->error = "Can't reshape degraded raid set";
1992 else if (rs_is_recovering(rs))
1993 rs->ti->error = "Convert request on recovering raid set prohibited";
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02001994 else if (rs_is_reshaping(rs))
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001995 rs->ti->error = "raid set already reshaping!";
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02001996 else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
1997 rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02001998 else
1999 return 0;
2000
2001 return -EPERM;
2002}
2003
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002004static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002005{
2006 BUG_ON(!rdev->sb_page);
2007
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002008 if (rdev->sb_loaded && !force_reload)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002009 return 0;
2010
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002011 rdev->sb_loaded = 0;
2012
Heinz Mauelshagen0a7b8182016-06-15 18:45:56 +02002013 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +01002014 DMERR("Failed to read superblock of device at position %d",
2015 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +10002016 md_error(rdev->mddev, rdev);
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002017 set_bit(Faulty, &rdev->flags);
2018 return -EIO;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002019 }
2020
2021 rdev->sb_loaded = 1;
2022
2023 return 0;
2024}
2025
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002026static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2027{
2028 failed_devices[0] = le64_to_cpu(sb->failed_devices);
2029 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
2030
Mike Snitzer42863252016-06-02 12:27:46 -04002031 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002032 int i = ARRAY_SIZE(sb->extended_failed_devices);
2033
2034 while (i--)
2035 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
2036 }
2037}
2038
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002039static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
2040{
2041 int i = ARRAY_SIZE(sb->extended_failed_devices);
2042
2043 sb->failed_devices = cpu_to_le64(failed_devices[0]);
2044 while (i--)
2045 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
2046}
2047
2048/*
2049 * Synchronize the superblock members with the raid set properties
2050 *
2051 * All superblock data is little endian.
2052 */
NeilBrownfd01b882011-10-11 16:47:53 +11002053static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002054{
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002055 bool update_failed_devices = false;
2056 unsigned int i;
2057 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002058 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +10002059 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002060
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002061 /* No metadata device, no superblock */
2062 if (!rdev->meta_bdev)
2063 return;
2064
2065 BUG_ON(!rdev->sb_page);
2066
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002067 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002068
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002069 sb_retrieve_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002070
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002071 for (i = 0; i < rs->raid_disks; i++)
2072 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
2073 update_failed_devices = true;
2074 set_bit(i, (void *) failed_devices);
2075 }
2076
2077 if (update_failed_devices)
2078 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002079
2080 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002081 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002082
2083 sb->num_devices = cpu_to_le32(mddev->raid_disks);
2084 sb->array_position = cpu_to_le32(rdev->raid_disk);
2085
2086 sb->events = cpu_to_le64(mddev->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002087
2088 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
2089 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
2090
2091 sb->level = cpu_to_le32(mddev->level);
2092 sb->layout = cpu_to_le32(mddev->layout);
2093 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002094
2095 sb->new_level = cpu_to_le32(mddev->new_level);
2096 sb->new_layout = cpu_to_le32(mddev->new_layout);
2097 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
2098
2099 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2100
2101 smp_rmb(); /* Make sure we access most recent reshape position */
2102 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2103 if (le64_to_cpu(sb->reshape_position) != MaxSector) {
2104 /* Flag ongoing reshape */
2105 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
2106
2107 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
2108 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
Mike Snitzer42863252016-06-02 12:27:46 -04002109 } else {
2110 /* Clear reshape flags */
2111 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
2112 }
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002113
2114 sb->array_sectors = cpu_to_le64(mddev->array_sectors);
2115 sb->data_offset = cpu_to_le64(rdev->data_offset);
2116 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
2117 sb->sectors = cpu_to_le64(rdev->sectors);
Heinz Mauelshagenb2a48722016-08-03 17:47:04 +02002118 sb->incompat_features = cpu_to_le32(0);
Heinz Mauelshagen7b34df72016-05-19 18:49:32 +02002119
2120 /* Zero out the rest of the payload after the size of the superblock */
2121 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002122}
2123
2124/*
2125 * super_load
2126 *
2127 * This function creates a superblock if one is not found on the device
2128 * and will decide which superblock to use if there's a choice.
2129 *
2130 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
2131 */
NeilBrown3cb03002011-10-11 16:45:26 +11002132static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002133{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002134 int r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002135 struct dm_raid_superblock *sb;
2136 struct dm_raid_superblock *refsb;
2137 uint64_t events_sb, events_refsb;
2138
2139 rdev->sb_start = 0;
Heinz Mauelshagen40d43c42014-10-17 13:38:50 +02002140 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
2141 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
2142 DMERR("superblock size of a logical block is no longer valid");
2143 return -EINVAL;
2144 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002145
Heinz Mauelshagene2568462017-01-14 03:53:10 +01002146 r = read_disk_sb(rdev, rdev->sb_size, false);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002147 if (r)
2148 return r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002149
2150 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +00002151
2152 /*
2153 * Two cases that we want to write new superblocks and rebuild:
2154 * 1) New device (no matching magic number)
2155 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
2156 */
2157 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
2158 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002159 super_sync(rdev->mddev, rdev);
2160
2161 set_bit(FirstUse, &rdev->flags);
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002162 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002163
2164 /* Force writing of superblocks to disk */
Shaohua Li29530792016-12-08 15:48:19 -08002165 set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002166
2167 /* Any superblock is better than none, choose that if given */
2168 return refdev ? 0 : 1;
2169 }
2170
2171 if (!refdev)
2172 return 1;
2173
2174 events_sb = le64_to_cpu(sb->events);
2175
2176 refsb = page_address(refdev->sb_page);
2177 events_refsb = le64_to_cpu(refsb->events);
2178
2179 return (events_sb > events_refsb) ? 1 : 0;
2180}
2181
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002182static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002183{
2184 int role;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002185 unsigned int d;
2186 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002187 uint64_t events_sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002188 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002189 struct dm_raid_superblock *sb;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002190 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002191 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002192 struct dm_raid_superblock *sb2;
2193
2194 sb = page_address(rdev->sb_page);
2195 events_sb = le64_to_cpu(sb->events);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002196
2197 /*
2198 * Initialise to 1 if this is a new superblock.
2199 */
2200 mddev->events = events_sb ? : 1;
2201
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002202 mddev->reshape_position = MaxSector;
2203
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002204 mddev->raid_disks = le32_to_cpu(sb->num_devices);
2205 mddev->level = le32_to_cpu(sb->level);
2206 mddev->layout = le32_to_cpu(sb->layout);
2207 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
2208
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002209 /*
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002210 * Reshaping is supported, e.g. reshape_position is valid
2211 * in superblock and superblock content is authoritative.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002212 */
Mike Snitzer42863252016-06-02 12:27:46 -04002213 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002214 /* Superblock is authoritative wrt given raid set layout! */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002215 mddev->new_level = le32_to_cpu(sb->new_level);
2216 mddev->new_layout = le32_to_cpu(sb->new_layout);
2217 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
2218 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
2219 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
2220
2221 /* raid was reshaping and got interrupted */
Mike Snitzer42863252016-06-02 12:27:46 -04002222 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
2223 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002224 DMERR("Reshape requested but raid set is still reshaping");
2225 return -EINVAL;
2226 }
2227
2228 if (mddev->delta_disks < 0 ||
Mike Snitzer42863252016-06-02 12:27:46 -04002229 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002230 mddev->reshape_backwards = 1;
2231 else
2232 mddev->reshape_backwards = 0;
2233
2234 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
2235 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
2236 }
2237
2238 } else {
2239 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002240 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002241 */
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002242 struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
2243 struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002244
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002245 if (rs_takeover_requested(rs)) {
2246 if (rt_cur && rt_new)
2247 DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
2248 rt_cur->name, rt_new->name);
2249 else
2250 DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
2251 return -EINVAL;
2252 } else if (rs_reshape_requested(rs)) {
2253 DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
2254 if (mddev->layout != mddev->new_layout) {
2255 if (rt_cur && rt_new)
2256 DMERR(" current layout %s vs new layout %s",
2257 rt_cur->name, rt_new->name);
2258 else
2259 DMERR(" current layout 0x%X vs new layout 0x%X",
2260 le32_to_cpu(sb->layout), mddev->new_layout);
2261 }
2262 if (mddev->chunk_sectors != mddev->new_chunk_sectors)
2263 DMERR(" current stripe sectors %u vs new stripe sectors %u",
2264 mddev->chunk_sectors, mddev->new_chunk_sectors);
2265 if (rs->delta_disks)
2266 DMERR(" current %u disks vs new %u disks",
2267 mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
2268 if (rs_is_raid10(rs)) {
2269 DMERR(" Old layout: %s w/ %u copies",
2270 raid10_md_layout_to_format(mddev->layout),
2271 raid10_md_layout_to_copies(mddev->layout));
2272 DMERR(" New layout: %s w/ %u copies",
2273 raid10_md_layout_to_format(mddev->new_layout),
2274 raid10_md_layout_to_copies(mddev->new_layout));
2275 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002276 return -EINVAL;
2277 }
2278
Heinz Mauelshagenb052b072016-10-17 21:20:07 +02002279 DMINFO("Discovered old metadata format; upgrading to extended metadata format");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002280 }
2281
Mike Snitzer42863252016-06-02 12:27:46 -04002282 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002283 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
2284
2285 /*
2286 * During load, we set FirstUse if a new superblock was written.
2287 * There are two reasons we might not have a superblock:
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002288 * 1) The raid set is brand new - in which case, all of the
Mike Snitzer43157842016-05-30 13:03:37 -04002289 * devices must have their In_sync bit set. Also,
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002290 * recovery_cp must be 0, unless forced.
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002291 * 2) This is a new device being added to an old raid set
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002292 * and the new device needs to be rebuilt - in which
2293 * case the In_sync bit will /not/ be set and
2294 * recovery_cp must be MaxSector.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002295 * 3) This is/are a new device(s) being added to an old
2296 * raid set during takeover to a higher raid level
2297 * to provide capacity for redundancy or during reshape
2298 * to add capacity to grow the raid set.
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002299 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002300 d = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002301 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002302 if (test_bit(Journal, &rdev->flags))
2303 continue;
2304
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002305 if (test_bit(FirstUse, &r->flags))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002306 new_devs++;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002307
2308 if (!test_bit(In_sync, &r->flags)) {
2309 DMINFO("Device %d specified for rebuild; clearing superblock",
2310 r->raid_disk);
2311 rebuilds++;
2312
2313 if (test_bit(FirstUse, &r->flags))
2314 rebuild_and_new++;
2315 }
2316
2317 d++;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002318 }
2319
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002320 if (new_devs == rs->raid_disks || !rebuilds) {
2321 /* Replace a broken device */
2322 if (new_devs == 1 && !rs->delta_disks)
2323 ;
2324 if (new_devs == rs->raid_disks) {
2325 DMINFO("Superblocks created for new raid set");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002326 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002327 } else if (new_devs != rebuilds &&
2328 new_devs != rs->delta_disks) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002329 DMERR("New device injected into existing raid set without "
2330 "'delta_disks' or 'rebuild' parameter specified");
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002331 return -EINVAL;
2332 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002333 } else if (new_devs && new_devs != rebuilds) {
2334 DMERR("%u 'rebuild' devices cannot be injected into"
2335 " a raid set with %u other first-time devices",
2336 rebuilds, new_devs);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002337 return -EINVAL;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002338 } else if (rebuilds) {
2339 if (rebuild_and_new && rebuilds != rebuild_and_new) {
2340 DMERR("new device%s provided without 'rebuild'",
2341 new_devs > 1 ? "s" : "");
2342 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002343 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002344 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
2345 (unsigned long long) mddev->recovery_cp);
2346 return -EINVAL;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002347 } else if (rs_is_reshaping(rs)) {
2348 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
2349 (unsigned long long) mddev->reshape_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002350 return -EINVAL;
2351 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002352 }
2353
2354 /*
2355 * Now we set the Faulty bit for those devices that are
2356 * recorded in the superblock as failed.
2357 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002358 sb_retrieve_failed_devices(sb, failed_devices);
NeilBrowndafb20f2012-03-19 12:46:39 +11002359 rdev_for_each(r, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002360 if (test_bit(Journal, &rdev->flags) ||
2361 !r->sb_page)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002362 continue;
2363 sb2 = page_address(r->sb_page);
2364 sb2->failed_devices = 0;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002365 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002366
2367 /*
2368 * Check for any device re-ordering.
2369 */
2370 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
2371 role = le32_to_cpu(sb2->array_position);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002372 if (role < 0)
2373 continue;
2374
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002375 if (role != r->raid_disk) {
Heinz Mauelshagen453c2a82016-10-18 17:46:45 +02002376 if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002377 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002378 rs->raid_disks % rs->raid10_copies) {
2379 rs->ti->error =
2380 "Cannot change raid10 near set to odd # of devices!";
2381 return -EINVAL;
2382 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002383
2384 sb2->array_position = cpu_to_le32(r->raid_disk);
2385
2386 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002387 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
2388 !rt_is_raid1(rs->raid_type)) {
2389 rs->ti->error = "Cannot change device positions in raid set";
2390 return -EINVAL;
2391 }
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002392
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002393 DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002394 }
2395
2396 /*
2397 * Partial recovery is performed on
2398 * returning failed devices.
2399 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002400 if (test_bit(role, (void *) failed_devices))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002401 set_bit(Faulty, &r->flags);
2402 }
2403 }
2404
2405 return 0;
2406}
2407
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002408static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002409{
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002410 struct mddev *mddev = &rs->md;
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002411 struct dm_raid_superblock *sb;
2412
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002413 if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002414 return 0;
2415
2416 sb = page_address(rdev->sb_page);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002417
2418 /*
2419 * If mddev->events is not set, we know we have not yet initialized
2420 * the array.
2421 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002422 if (!mddev->events && super_init_validation(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002423 return -EINVAL;
2424
Andy Whitcroft5c336772016-10-11 15:16:57 +01002425 if (le32_to_cpu(sb->compat_features) &&
2426 le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
Mike Snitzer9b6e5422016-06-02 11:48:09 -04002427 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
2428 return -EINVAL;
2429 }
2430
2431 if (sb->incompat_features) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002432 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
Heinz Mauelshagen4c9971c2016-04-29 18:59:56 +02002433 return -EINVAL;
2434 }
2435
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002436 /* Enable bitmap creation for RAID levels != 0 */
Heinz Mauelshagen676fa5a2016-05-19 18:49:29 +02002437 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
Heinz Mauelshagen977f1a02017-01-14 03:53:11 +01002438 mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02002439
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002440 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
2441 /* Retrieve device size stored in superblock to be prepared for shrink */
2442 rdev->sectors = le64_to_cpu(sb->sectors);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002443 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002444 if (rdev->recovery_offset == MaxSector)
2445 set_bit(In_sync, &rdev->flags);
2446 /*
2447 * If no reshape in progress -> we're recovering single
2448 * disk(s) and have to set the device(s) to out-of-sync
2449 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002450 else if (!rs_is_reshaping(rs))
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002451 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002452 }
2453
2454 /*
2455 * If a device comes back, set it as not In_sync and no longer faulty.
2456 */
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002457 if (test_and_clear_bit(Faulty, &rdev->flags)) {
2458 rdev->recovery_offset = 0;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002459 clear_bit(In_sync, &rdev->flags);
2460 rdev->saved_raid_disk = rdev->raid_disk;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002461 }
2462
Heinz Mauelshagen33e53f02016-05-19 18:49:30 +02002463 /* Reshape support -> restore repective data offsets */
2464 rdev->data_offset = le64_to_cpu(sb->data_offset);
2465 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002466
2467 return 0;
2468}
2469
2470/*
2471 * Analyse superblocks and select the freshest.
2472 */
2473static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
2474{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002475 int r;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002476 struct md_rdev *rdev, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11002477 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002478
2479 freshest = NULL;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002480 rdev_for_each(rdev, mddev) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002481 if (test_bit(Journal, &rdev->flags))
2482 continue;
2483
Jonathan Brassow761becf2012-10-11 13:42:19 +11002484 /*
Heinz Mauelshagenc76d53f2015-04-29 14:03:00 +02002485 * Skipping super_load due to CTR_FLAG_SYNC will cause
Jonathan Brassow761becf2012-10-11 13:42:19 +11002486 * the array to undergo initialization again as
Mike Snitzer43157842016-05-30 13:03:37 -04002487 * though it were new. This is the intended effect
Jonathan Brassow761becf2012-10-11 13:42:19 +11002488 * of the "sync" directive.
2489 *
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002490 * With reshaping capability added, we must ensure that
2491 * that the "sync" directive is disallowed during the reshape.
Jonathan Brassow761becf2012-10-11 13:42:19 +11002492 */
Mike Snitzer42863252016-06-02 12:27:46 -04002493 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Jonathan Brassow761becf2012-10-11 13:42:19 +11002494 continue;
2495
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002496 if (!rdev->meta_bdev)
2497 continue;
2498
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002499 r = super_load(rdev, freshest);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002500
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002501 switch (r) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002502 case 1:
2503 freshest = rdev;
2504 break;
2505 case 0:
2506 break;
2507 default:
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002508 /* This is a failure to read the superblock from the metadata device. */
Heinz Mauelshagen9e7d9362016-08-17 15:36:44 +02002509 /*
2510 * We have to keep any raid0 data/metadata device pairs or
2511 * the MD raid0 personality will fail to start the array.
2512 */
2513 if (rs_is_raid0(rs))
2514 continue;
2515
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002516 /*
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002517 * We keep the dm_devs to be able to emit the device tuple
2518 * properly on the table line in raid_status() (rather than
2519 * mistakenly acting as if '- -' got passed into the constructor).
2520 *
2521 * The rdev has to stay on the same_set list to allow for
2522 * the attempt to restore faulty devices on second resume.
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002523 */
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01002524 rdev->raid_disk = rdev->saved_raid_disk = -1;
2525 break;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002526 }
2527 }
2528
2529 if (!freshest)
2530 return 0;
2531
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002532 if (validate_raid_redundancy(rs)) {
2533 rs->ti->error = "Insufficient redundancy to activate array";
2534 return -EINVAL;
2535 }
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06002536
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002537 /*
2538 * Validation of the freshest device provides the source of
2539 * validation for the remaining devices.
2540 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002541 rs->ti->error = "Unable to assemble array: Invalid superblocks";
2542 if (super_validate(rs, freshest))
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002543 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002544
NeilBrowndafb20f2012-03-19 12:46:39 +11002545 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002546 if (!test_bit(Journal, &rdev->flags) &&
2547 rdev != freshest &&
2548 super_validate(rs, rdev))
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002549 return -EINVAL;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002550 return 0;
2551}
2552
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002553/*
2554 * Adjust data_offset and new_data_offset on all disk members of @rs
2555 * for out of place reshaping if requested by contructor
2556 *
2557 * We need free space at the beginning of each raid disk for forward
2558 * and at the end for backward reshapes which userspace has to provide
2559 * via remapping/reordering of space.
2560 */
2561static int rs_adjust_data_offsets(struct raid_set *rs)
2562{
2563 sector_t data_offset = 0, new_data_offset = 0;
2564 struct md_rdev *rdev;
2565
2566 /* Constructor did not request data offset change */
2567 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
2568 if (!rs_is_reshapable(rs))
2569 goto out;
2570
2571 return 0;
2572 }
2573
2574 /* HM FIXME: get InSync raid_dev? */
2575 rdev = &rs->dev[0].rdev;
2576
2577 if (rs->delta_disks < 0) {
2578 /*
2579 * Removing disks (reshaping backwards):
2580 *
2581 * - before reshape: data is at offset 0 and free space
2582 * is at end of each component LV
2583 *
2584 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
2585 */
2586 data_offset = 0;
2587 new_data_offset = rs->data_offset;
2588
2589 } else if (rs->delta_disks > 0) {
2590 /*
2591 * Adding disks (reshaping forwards):
2592 *
2593 * - before reshape: data is at offset rs->data_offset != 0 and
2594 * free space is at begin of each component LV
2595 *
2596 * - after reshape: data is at offset 0 on each component LV
2597 */
2598 data_offset = rs->data_offset;
2599 new_data_offset = 0;
2600
2601 } else {
2602 /*
2603 * User space passes in 0 for data offset after having removed reshape space
2604 *
2605 * - or - (data offset != 0)
2606 *
2607 * Changing RAID layout or chunk size -> toggle offsets
2608 *
2609 * - before reshape: data is at offset rs->data_offset 0 and
2610 * free space is at end of each component LV
2611 * -or-
2612 * data is at offset rs->data_offset != 0 and
2613 * free space is at begin of each component LV
2614 *
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002615 * - after reshape: data is at offset 0 if it was at offset != 0
2616 * or at offset != 0 if it was at offset 0
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002617 * on each component LV
2618 *
2619 */
2620 data_offset = rs->data_offset ? rdev->data_offset : 0;
2621 new_data_offset = data_offset ? 0 : rs->data_offset;
2622 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2623 }
2624
2625 /*
2626 * Make sure we got a minimum amount of free sectors per device
2627 */
2628 if (rs->data_offset &&
2629 to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) {
2630 rs->ti->error = data_offset ? "No space for forward reshape" :
2631 "No space for backward reshape";
2632 return -ENOSPC;
2633 }
2634out:
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002635 /* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002636 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002637 if (!test_bit(Journal, &rdev->flags)) {
2638 rdev->data_offset = data_offset;
2639 rdev->new_data_offset = new_data_offset;
2640 }
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002641 }
2642
2643 return 0;
2644}
2645
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002646/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002647static void __reorder_raid_disk_indexes(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002648{
2649 int i = 0;
2650 struct md_rdev *rdev;
2651
2652 rdev_for_each(rdev, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01002653 if (!test_bit(Journal, &rdev->flags)) {
2654 rdev->raid_disk = i++;
2655 rdev->saved_raid_disk = rdev->new_raid_disk = -1;
2656 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002657 }
2658}
2659
2660/*
2661 * Setup @rs for takeover by a different raid level
2662 */
2663static int rs_setup_takeover(struct raid_set *rs)
2664{
2665 struct mddev *mddev = &rs->md;
2666 struct md_rdev *rdev;
2667 unsigned int d = mddev->raid_disks = rs->raid_disks;
2668 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
2669
2670 if (rt_is_raid10(rs->raid_type)) {
2671 if (mddev->level == 0) {
2672 /* Userpace reordered disks -> adjust raid_disk indexes */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04002673 __reorder_raid_disk_indexes(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002674
2675 /* raid0 -> raid10_far layout */
2676 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
2677 rs->raid10_copies);
2678 } else if (mddev->level == 1)
2679 /* raid1 -> raid10_near layout */
2680 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2681 rs->raid_disks);
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002682 else
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002683 return -EINVAL;
2684
2685 }
2686
2687 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2688 mddev->recovery_cp = MaxSector;
2689
2690 while (d--) {
2691 rdev = &rs->dev[d].rdev;
2692
2693 if (test_bit(d, (void *) rs->rebuild_disks)) {
2694 clear_bit(In_sync, &rdev->flags);
2695 clear_bit(Faulty, &rdev->flags);
2696 mddev->recovery_cp = rdev->recovery_offset = 0;
2697 /* Bitmap has to be created when we do an "up" takeover */
2698 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2699 }
2700
2701 rdev->new_data_offset = new_data_offset;
2702 }
2703
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002704 return 0;
2705}
2706
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002707/* Prepare @rs for reshape */
2708static int rs_prepare_reshape(struct raid_set *rs)
2709{
2710 bool reshape;
2711 struct mddev *mddev = &rs->md;
2712
2713 if (rs_is_raid10(rs)) {
2714 if (rs->raid_disks != mddev->raid_disks &&
2715 __is_raid10_near(mddev->layout) &&
2716 rs->raid10_copies &&
2717 rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
2718 /*
2719 * raid disk have to be multiple of data copies to allow this conversion,
2720 *
2721 * This is actually not a reshape it is a
2722 * rebuild of any additional mirrors per group
2723 */
2724 if (rs->raid_disks % rs->raid10_copies) {
2725 rs->ti->error = "Can't reshape raid10 mirror groups";
2726 return -EINVAL;
2727 }
2728
2729 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
2730 __reorder_raid_disk_indexes(rs);
2731 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
2732 rs->raid10_copies);
2733 mddev->new_layout = mddev->layout;
2734 reshape = false;
2735 } else
2736 reshape = true;
2737
2738 } else if (rs_is_raid456(rs))
2739 reshape = true;
2740
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002741 else if (rs_is_raid1(rs)) {
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002742 if (rs->delta_disks) {
2743 /* Process raid1 via delta_disks */
2744 mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
2745 reshape = true;
2746 } else {
2747 /* Process raid1 without delta_disks */
2748 mddev->raid_disks = rs->raid_disks;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002749 reshape = false;
2750 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002751 } else {
2752 rs->ti->error = "Called with bogus raid type";
2753 return -EINVAL;
2754 }
2755
2756 if (reshape) {
2757 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
2758 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002759 } else if (mddev->raid_disks < rs->raid_disks)
2760 /* Create new superblocks and bitmaps, if any new disks */
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002761 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002762
2763 return 0;
2764}
2765
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002766/*
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002767 *
2768 * - change raid layout
2769 * - change chunk size
2770 * - add disks
2771 * - remove disks
2772 */
2773static int rs_setup_reshape(struct raid_set *rs)
2774{
2775 int r = 0;
2776 unsigned int cur_raid_devs, d;
2777 struct mddev *mddev = &rs->md;
2778 struct md_rdev *rdev;
2779
2780 mddev->delta_disks = rs->delta_disks;
2781 cur_raid_devs = mddev->raid_disks;
2782
2783 /* Ignore impossible layout change whilst adding/removing disks */
2784 if (mddev->delta_disks &&
2785 mddev->layout != mddev->new_layout) {
2786 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
2787 mddev->new_layout = mddev->layout;
2788 }
2789
2790 /*
2791 * Adjust array size:
2792 *
2793 * - in case of adding disks, array size has
2794 * to grow after the disk adding reshape,
2795 * which'll hapen in the event handler;
2796 * reshape will happen forward, so space has to
2797 * be available at the beginning of each disk
2798 *
2799 * - in case of removing disks, array size
2800 * has to shrink before starting the reshape,
2801 * which'll happen here;
2802 * reshape will happen backward, so space has to
2803 * be available at the end of each disk
2804 *
2805 * - data_offset and new_data_offset are
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002806 * adjusted for aforementioned out of place
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002807 * reshaping based on userspace passing in
2808 * the "data_offset <sectors>" key/value
Heinz Mauelshagenae3c6cf2016-06-15 22:27:40 +02002809 * pair via the constructor
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002810 */
2811
2812 /* Add disk(s) */
2813 if (rs->delta_disks > 0) {
2814 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
2815 for (d = cur_raid_devs; d < rs->raid_disks; d++) {
2816 rdev = &rs->dev[d].rdev;
2817 clear_bit(In_sync, &rdev->flags);
2818
2819 /*
2820 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
2821 * by md, which'll store that erroneously in the superblock on reshape
2822 */
2823 rdev->saved_raid_disk = -1;
2824 rdev->raid_disk = d;
2825
2826 rdev->sectors = mddev->dev_sectors;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02002827 rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002828 }
2829
2830 mddev->reshape_backwards = 0; /* adding disks -> forward reshape */
2831
2832 /* Remove disk(s) */
2833 } else if (rs->delta_disks < 0) {
2834 r = rs_set_dev_and_array_sectors(rs, true);
2835 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
2836
2837 /* Change layout and/or chunk size */
2838 } else {
2839 /*
2840 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
2841 *
2842 * keeping number of disks and do layout change ->
2843 *
2844 * toggle reshape_backward depending on data_offset:
2845 *
2846 * - free space upfront -> reshape forward
2847 *
2848 * - free space at the end -> reshape backward
2849 *
2850 *
2851 * This utilizes free reshape space avoiding the need
2852 * for userspace to move (parts of) LV segments in
2853 * case of layout/chunksize change (for disk
2854 * adding/removing reshape space has to be at
2855 * the proper address (see above with delta_disks):
2856 *
2857 * add disk(s) -> begin
2858 * remove disk(s)-> end
2859 */
2860 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
2861 }
2862
2863 return r;
2864}
2865
2866/*
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002867 * Enable/disable discard support on RAID set depending on
2868 * RAID level and discard properties of underlying RAID members.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002869 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002870static void configure_discard_support(struct raid_set *rs)
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002871{
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002872 int i;
2873 bool raid456;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002874 struct dm_target *ti = rs->ti;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002875
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002876 /* Assume discards not supported until after checks below. */
2877 ti->discards_supported = false;
2878
Christoph Hellwig48920ff2017-04-05 19:21:23 +02002879 /*
2880 * XXX: RAID level 4,5,6 require zeroing for safety.
2881 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002882 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002883
Heinz Mauelshagenffeeac72016-06-30 14:37:50 +02002884 for (i = 0; i < rs->raid_disks; i++) {
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002885 struct request_queue *q;
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002886
Heinz Mauelshagend20c4b02014-10-29 19:02:27 +01002887 if (!rs->dev[i].rdev.bdev)
2888 continue;
2889
2890 q = bdev_get_queue(rs->dev[i].rdev.bdev);
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002891 if (!q || !blk_queue_discard(q))
2892 return;
2893
2894 if (raid456) {
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002895 if (!devices_handle_discard_safely) {
2896 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
2897 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
2898 return;
2899 }
2900 }
2901 }
2902
2903 /* All RAID members properly support discards */
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002904 ti->discards_supported = true;
2905
2906 /*
2907 * RAID1 and RAID10 personalities require bio splitting,
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002908 * RAID0/4/5/6 don't and process large discard bios properly.
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002909 */
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02002910 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02002911 ti->num_discard_bios = 1;
2912}
2913
2914/*
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002915 * Construct a RAID0/1/10/4/5/6 mapping:
NeilBrown9d09e662011-01-13 20:00:02 +00002916 * Args:
Mike Snitzer43157842016-05-30 13:03:37 -04002917 * <raid_type> <#raid_params> <raid_params>{0,} \
2918 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
NeilBrown9d09e662011-01-13 20:00:02 +00002919 *
Mike Snitzer43157842016-05-30 13:03:37 -04002920 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
NeilBrown9d09e662011-01-13 20:00:02 +00002921 * details on possible <raid_params>.
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002922 *
2923 * Userspace is free to initialize the metadata devices, hence the superblocks to
2924 * enforce recreation based on the passed in table parameters.
2925 *
NeilBrown9d09e662011-01-13 20:00:02 +00002926 */
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002927static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
NeilBrown9d09e662011-01-13 20:00:02 +00002928{
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002929 int r;
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02002930 bool resize;
NeilBrown9d09e662011-01-13 20:00:02 +00002931 struct raid_type *rt;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02002932 unsigned int num_raid_params, num_raid_devs;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002933 sector_t calculated_dev_sectors;
NeilBrown9d09e662011-01-13 20:00:02 +00002934 struct raid_set *rs = NULL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002935 const char *arg;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02002936 struct rs_layout rs_layout;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002937 struct dm_arg_set as = { argc, argv }, as_nrd;
2938 struct dm_arg _args[] = {
2939 { 0, as.argc, "Cannot understand number of raid parameters" },
2940 { 1, 254, "Cannot understand number of raid devices parameters" }
2941 };
NeilBrown9d09e662011-01-13 20:00:02 +00002942
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002943 /* Must have <raid_type> */
2944 arg = dm_shift_arg(&as);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002945 if (!arg) {
2946 ti->error = "No arguments";
2947 return -EINVAL;
2948 }
NeilBrown9d09e662011-01-13 20:00:02 +00002949
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002950 rt = get_raid_type(arg);
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002951 if (!rt) {
2952 ti->error = "Unrecognised raid_type";
2953 return -EINVAL;
2954 }
NeilBrown9d09e662011-01-13 20:00:02 +00002955
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002956 /* Must have <#raid_params> */
2957 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002958 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +00002959
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002960 /* number of raid device tupples <meta_dev data_dev> */
2961 as_nrd = as;
2962 dm_consume_args(&as_nrd, num_raid_params);
2963 _args[1].max = (as_nrd.argc - 1) / 2;
2964 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
Mike Snitzer43157842016-05-30 13:03:37 -04002965 return -EINVAL;
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002966
Mike Snitzerbb91a632016-06-02 12:06:54 -04002967 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
Mike Snitzerbd83a4c2016-05-31 14:26:52 -04002968 ti->error = "Invalid number of supplied raid devices";
2969 return -EINVAL;
2970 }
NeilBrown9d09e662011-01-13 20:00:02 +00002971
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04002972 rs = raid_set_alloc(ti, rt, num_raid_devs);
NeilBrown9d09e662011-01-13 20:00:02 +00002973 if (IS_ERR(rs))
2974 return PTR_ERR(rs);
2975
Heinz Mauelshagen92c83d72016-05-19 18:49:25 +02002976 r = parse_raid_params(rs, &as, num_raid_params);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002977 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002978 goto bad;
2979
Heinz Mauelshagen702108d2016-05-19 18:49:26 +02002980 r = parse_dev_params(rs, &as);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02002981 if (r)
NeilBrown9d09e662011-01-13 20:00:02 +00002982 goto bad;
2983
Jonathan Brassowb12d4372011-08-02 12:32:07 +01002984 rs->md.sync_super = super_sync;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002985
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02002986 /*
2987 * Calculate ctr requested array and device sizes to allow
2988 * for superblock analysis needing device sizes defined.
2989 *
2990 * Any existing superblock will overwrite the array and device sizes
2991 */
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002992 r = rs_set_dev_and_array_sectors(rs, false);
2993 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02002994 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02002995
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01002996 calculated_dev_sectors = rs->md.dev_sectors;
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02002997
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02002998 /*
2999 * Backup any new raid set level, layout, ...
3000 * requested to be able to compare to superblock
3001 * members for conversion decisions.
3002 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003003 rs_config_backup(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003004
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003005 r = analyse_superblocks(ti, rs);
3006 if (r)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01003007 goto bad;
3008
Heinz Mauelshagen50c4feb2017-01-14 03:53:08 +01003009 resize = calculated_dev_sectors != __rdev_sectors(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003010
NeilBrown9d09e662011-01-13 20:00:02 +00003011 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00003012 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003013 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00003014
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003015 /* Restore any requested new layout for conversion decision */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003016 rs_config_restore(rs, &rs_layout);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003017
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003018 /*
3019 * Now that we have any superblock metadata available,
3020 * check for new, recovering, reshaping, to be taken over,
3021 * to be reshaped or an existing, unchanged raid set to
3022 * run in sequence.
3023 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003024 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003025 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
3026 if (rs_is_raid6(rs) &&
3027 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
3028 ti->error = "'nosync' not allowed for new raid6 set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003029 r = -EINVAL;
3030 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003031 }
3032 rs_setup_recovery(rs, 0);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003033 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3034 rs_set_new(rs);
3035 } else if (rs_is_recovering(rs)) {
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003036 /* A recovering raid set may be resized */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003037 ; /* skip setup rs */
3038 } else if (rs_is_reshaping(rs)) {
3039 /* Have to reject size change request during reshape */
3040 if (resize) {
3041 ti->error = "Can't resize a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003042 r = -EPERM;
3043 goto bad;
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003044 }
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003045 /* skip setup rs */
Heinz Mauelshagen2d92a3c2016-06-24 00:32:58 +02003046 } else if (rs_takeover_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003047 if (rs_is_reshaping(rs)) {
3048 ti->error = "Can't takeover a reshaping raid set";
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003049 r = -EPERM;
3050 goto bad;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003051 }
3052
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003053 /* We can't takeover a journaled raid4/5/6 */
3054 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3055 ti->error = "Can't takeover a journaled raid4/5/6 set";
3056 r = -EPERM;
3057 goto bad;
3058 }
3059
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003060 /*
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003061 * If a takeover is needed, userspace sets any additional
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003062 * devices to rebuild and we can check for a valid request here.
3063 *
3064 * If acceptible, set the level to the new requested
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003065 * one, prohibit requesting recovery, allow the raid
3066 * set to run and store superblocks during resume.
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003067 */
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003068 r = rs_check_takeover(rs);
3069 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003070 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003071
3072 r = rs_setup_takeover(rs);
3073 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003074 goto bad;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003075
Mike Snitzer42863252016-06-02 12:27:46 -04003076 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003077 /* Takeover ain't recovery, so disable recovery */
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003078 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003079 rs_set_new(rs);
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003080 } else if (rs_reshape_requested(rs)) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003081 /*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003082 * No need to check for 'ongoing' takeover here, because takeover
3083 * is an instant operation as oposed to an ongoing reshape.
3084 */
3085
3086 /* We can't reshape a journaled raid4/5/6 */
3087 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
3088 ti->error = "Can't reshape a journaled raid4/5/6 set";
3089 r = -EPERM;
3090 goto bad;
3091 }
3092
3093 /*
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003094 * We can only prepare for a reshape here, because the
3095 * raid set needs to run to provide the repective reshape
3096 * check functions via its MD personality instance.
3097 *
3098 * So do the reshape check after md_run() succeeded.
3099 */
3100 r = rs_prepare_reshape(rs);
3101 if (r)
3102 return r;
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003103
Heinz Mauelshagen469b3042016-06-29 18:13:58 +02003104 /* Reshaping ain't recovery, so disable recovery */
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003105 rs_setup_recovery(rs, MaxSector);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003106 rs_set_cur(rs);
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003107 } else {
3108 /* May not set recovery when a device rebuild is requested */
Heinz Mauelshagen37f10be2016-06-24 23:21:37 +02003109 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
3110 rs_setup_recovery(rs, MaxSector);
3111 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
3112 } else
3113 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
Heinz Mauelshagen2a5556c2016-06-27 14:44:09 +02003114 0 : (resize ? calculated_dev_sectors : MaxSector));
3115 rs_set_cur(rs);
Heinz Mauelshagen4dff2f12016-06-24 00:21:09 +02003116 }
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003117
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003118 /* If constructor requested it, change data and new_data offsets */
3119 r = rs_adjust_data_offsets(rs);
3120 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003121 goto bad;
Heinz Mauelshagen40ba37e2016-06-13 17:55:13 +02003122
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003123 /* Start raid set read-only and assumed clean to change in raid_resume() */
3124 rs->md.ro = 1;
3125 rs->md.in_sync = 1;
3126 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003127
Heinz Mauelshagen0cf45032015-04-29 14:03:04 +02003128 /* Has to be held on running the array */
3129 mddev_lock_nointr(&rs->md);
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003130 r = md_run(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003131 rs->md.in_sync = 0; /* Assume already marked dirty */
NeilBrown9d09e662011-01-13 20:00:02 +00003132
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003133 if (r) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003134 ti->error = "Failed to run raid array";
3135 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003136 goto bad;
3137 }
3138
3139 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00003140 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
3141
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003142 /* If raid4/5/6 journal mode explictely requested (only possible with journal dev) -> set it */
3143 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
3144 r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
3145 if (r) {
3146 ti->error = "Failed to set raid4/5/6 journal mode";
3147 mddev_unlock(&rs->md);
3148 goto bad_journal_mode_set;
3149 }
3150 }
3151
Jonathan Brassow32737272011-08-02 12:32:07 +01003152 mddev_suspend(&rs->md);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003153
3154 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
3155 if (rs_is_raid456(rs)) {
3156 r = rs_set_raid456_stripe_cache(rs);
3157 if (r)
3158 goto bad_stripe_cache;
3159 }
3160
3161 /* Now do an early reshape check */
3162 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3163 r = rs_check_reshape(rs);
3164 if (r)
Heinz Mauelshagenb1956dc2016-06-24 21:49:26 +02003165 goto bad_check_reshape;
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003166
3167 /* Restore new, ctr requested layout to perform check */
3168 rs_config_restore(rs, &rs_layout);
3169
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003170 if (rs->md.pers->start_reshape) {
3171 r = rs->md.pers->check_reshape(&rs->md);
3172 if (r) {
3173 ti->error = "Reshape check failed";
3174 goto bad_check_reshape;
3175 }
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003176 }
3177 }
3178
Heinz Mauelshagen11e29682016-11-29 22:37:30 +01003179 /* Disable/enable discard support on raid set. */
3180 configure_discard_support(rs);
3181
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003182 mddev_unlock(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003183 return 0;
3184
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003185bad_journal_mode_set:
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003186bad_stripe_cache:
3187bad_check_reshape:
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003188 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00003189bad:
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003190 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003191
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003192 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003193}
3194
3195static void raid_dtr(struct dm_target *ti)
3196{
3197 struct raid_set *rs = ti->private;
3198
3199 list_del_init(&rs->callbacks.list);
3200 md_stop(&rs->md);
Mike Snitzerbfcee0e2016-06-02 15:08:09 -04003201 raid_set_free(rs);
NeilBrown9d09e662011-01-13 20:00:02 +00003202}
3203
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003204static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00003205{
3206 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11003207 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00003208
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003209 /*
3210 * If we're reshaping to add disk(s)), ti->len and
3211 * mddev->array_sectors will differ during the process
3212 * (ti->len > mddev->array_sectors), so we have to requeue
3213 * bios with addresses > mddev->array_sectors here or
Heinz Mauelshagen2527b562016-06-24 01:03:19 +02003214 * there will occur accesses past EOD of the component
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003215 * data images thus erroring the raid set.
3216 */
3217 if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
3218 return DM_MAPIO_REQUEUE;
3219
NeilBrown9d09e662011-01-13 20:00:02 +00003220 mddev->pers->make_request(mddev, bio);
3221
3222 return DM_MAPIO_SUBMITTED;
3223}
3224
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003225/* Return string describing the current sync action of @mddev */
Jonathan Brassowbe836512013-04-24 11:42:43 +10003226static const char *decipher_sync_action(struct mddev *mddev)
3227{
3228 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3229 return "frozen";
3230
3231 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3232 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3233 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3234 return "reshape";
3235
3236 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3237 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3238 return "resync";
3239 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3240 return "check";
3241 return "repair";
3242 }
3243
3244 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3245 return "recover";
3246 }
3247
3248 return "idle";
3249}
3250
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003251/*
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003252 * Return status string for @rdev
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003253 *
3254 * Status characters:
3255 *
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003256 * 'D' = Dead/Failed raid set component or raid4/5/6 journal device
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003257 * 'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
3258 * 'A' = Alive and in-sync raid set component _or_ alive raid4/5/6 'write_through' journal device
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003259 * '-' = Non-existing device (i.e. uspace passed '- -' into the ctr)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003260 */
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003261static const char *__raid_dev_status(struct raid_set *rs, struct md_rdev *rdev, bool array_in_sync)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003262{
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003263 if (!rdev->bdev)
3264 return "-";
3265 else if (test_bit(Faulty, &rdev->flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003266 return "D";
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003267 else if (test_bit(Journal, &rdev->flags))
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003268 return (rs->journal_dev.mode == R5C_JOURNAL_MODE_WRITE_THROUGH) ? "A" : "a";
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003269 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags))
3270 return "a";
3271 else
3272 return "A";
3273}
3274
3275/* Helper to return resync/reshape progress for @rs and @array_in_sync */
3276static sector_t rs_get_progress(struct raid_set *rs,
3277 sector_t resync_max_sectors, bool *array_in_sync)
3278{
3279 sector_t r, recovery_cp, curr_resync_completed;
3280 struct mddev *mddev = &rs->md;
3281
3282 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp;
3283 recovery_cp = mddev->recovery_cp;
3284 *array_in_sync = false;
3285
3286 if (rs_is_raid0(rs)) {
3287 r = resync_max_sectors;
3288 *array_in_sync = true;
3289
3290 } else {
3291 r = mddev->reshape_position;
3292
3293 /* Reshape is relative to the array size */
3294 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
3295 r != MaxSector) {
3296 if (r == MaxSector) {
3297 *array_in_sync = true;
3298 r = resync_max_sectors;
3299 } else {
3300 /* Got to reverse on backward reshape */
3301 if (mddev->reshape_backwards)
3302 r = mddev->array_sectors - r;
3303
3304 /* Devide by # of data stripes */
3305 sector_div(r, mddev_data_stripes(rs));
3306 }
3307
3308 /* Sync is relative to the component device size */
3309 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3310 r = curr_resync_completed;
3311 else
3312 r = recovery_cp;
3313
3314 if (r == MaxSector) {
3315 /*
3316 * Sync complete.
3317 */
3318 *array_in_sync = true;
3319 r = resync_max_sectors;
3320 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
3321 /*
3322 * If "check" or "repair" is occurring, the raid set has
3323 * undergone an initial sync and the health characters
3324 * should not be 'a' anymore.
3325 */
3326 *array_in_sync = true;
3327 } else {
3328 struct md_rdev *rdev;
3329
3330 /*
3331 * The raid set may be doing an initial sync, or it may
Mike Snitzer43157842016-05-30 13:03:37 -04003332 * be rebuilding individual components. If all the
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003333 * devices are In_sync, then it is the raid set that is
3334 * being initialized.
3335 */
3336 rdev_for_each(rdev, mddev)
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003337 if (!test_bit(Journal, &rdev->flags) &&
3338 !test_bit(In_sync, &rdev->flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003339 *array_in_sync = true;
3340#if 0
3341 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */
3342#endif
3343 }
3344 }
3345
3346 return r;
3347}
3348
3349/* Helper to return @dev name or "-" if !@dev */
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003350static const char *__get_dev_name(struct dm_dev *dev)
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003351{
3352 return dev ? dev->name : "-";
3353}
3354
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003355static void raid_status(struct dm_target *ti, status_type_t type,
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003356 unsigned int status_flags, char *result, unsigned int maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00003357{
3358 struct raid_set *rs = ti->private;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003359 struct mddev *mddev = &rs->md;
3360 struct r5conf *conf = mddev->private;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003361 int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003362 bool array_in_sync;
3363 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
3364 unsigned int sz = 0;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003365 unsigned int rebuild_disks;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003366 unsigned int write_mostly_params = 0;
3367 sector_t progress, resync_max_sectors, resync_mismatches;
3368 const char *sync_action;
3369 struct raid_type *rt;
NeilBrown9d09e662011-01-13 20:00:02 +00003370
3371 switch (type) {
3372 case STATUSTYPE_INFO:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003373 /* *Should* always succeed */
3374 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
3375 if (!rt)
3376 return;
NeilBrown9d09e662011-01-13 20:00:02 +00003377
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003378 DMEMIT("%s %d ", rt->name, mddev->raid_disks);
NeilBrown9d09e662011-01-13 20:00:02 +00003379
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003380 /* Access most recent mddev properties for status output */
3381 smp_rmb();
3382 /* Get sensible max sectors even if raid set not yet started */
Mike Snitzer42863252016-06-02 12:27:46 -04003383 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003384 mddev->resync_max_sectors : mddev->dev_sectors;
3385 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync);
3386 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003387 atomic64_read(&mddev->resync_mismatches) : 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003388 sync_action = decipher_sync_action(&rs->md);
3389
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003390 /* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
3391 for (i = 0; i < rs->raid_disks; i++)
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003392 DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev, array_in_sync));
Jonathan Brassowbe836512013-04-24 11:42:43 +10003393
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003394 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003395 * In-sync/Reshape ratio:
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003396 * The in-sync ratio shows the progress of:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003397 * - Initializing the raid set
3398 * - Rebuilding a subset of devices of the raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003399 * The user can distinguish between the two by referring
3400 * to the status characters.
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003401 *
3402 * The reshape ratio shows the progress of
3403 * changing the raid layout or the number of
3404 * disks of a raid set
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00003405 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003406 DMEMIT(" %llu/%llu", (unsigned long long) progress,
3407 (unsigned long long) resync_max_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003408
Jonathan Brassowbe836512013-04-24 11:42:43 +10003409 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003410 * v1.5.0+:
3411 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003412 * Sync action:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003413 * See Documentation/device-mapper/dm-raid.txt for
Jonathan Brassowbe836512013-04-24 11:42:43 +10003414 * information on each of these states.
3415 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003416 DMEMIT(" %s", sync_action);
Jonathan Brassowbe836512013-04-24 11:42:43 +10003417
3418 /*
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003419 * v1.5.0+:
3420 *
Jonathan Brassowbe836512013-04-24 11:42:43 +10003421 * resync_mismatches/mismatch_cnt
3422 * This field shows the number of discrepancies found when
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003423 * performing a "check" of the raid set.
Jonathan Brassowbe836512013-04-24 11:42:43 +10003424 */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003425 DMEMIT(" %llu", (unsigned long long) resync_mismatches);
3426
3427 /*
Mike Snitzer9b6e5422016-06-02 11:48:09 -04003428 * v1.9.0+:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003429 *
3430 * data_offset (needed for out of space reshaping)
3431 * This field shows the data offset into the data
3432 * image LV where the first stripes data starts.
3433 *
3434 * We keep data_offset equal on all raid disks of the set,
3435 * so retrieving it from the first raid disk is sufficient.
3436 */
3437 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003438
3439 /*
3440 * v1.10.0+:
3441 */
3442 DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003443 __raid_dev_status(rs, &rs->journal_dev.rdev, 0) : "-");
NeilBrown9d09e662011-01-13 20:00:02 +00003444 break;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003445
NeilBrown9d09e662011-01-13 20:00:02 +00003446 case STATUSTYPE_TABLE:
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003447 /* Report the table line string you would use to construct this raid set */
NeilBrown9d09e662011-01-13 20:00:02 +00003448
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003449 /* Calculate raid parameter count */
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003450 for (i = 0; i < rs->raid_disks; i++)
3451 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003452 write_mostly_params += 2;
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003453 rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
3454 raid_param_cnt += rebuild_disks * 2 +
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003455 write_mostly_params +
3456 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003457 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 +
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003458 (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0) +
3459 (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags) ? 2 : 0);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003460
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003461 /* Emit table line */
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003462 /* This has to be in the documented order for userspace! */
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003463 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
Mike Snitzer42863252016-06-02 12:27:46 -04003464 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003465 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003466 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
3467 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003468 if (rebuild_disks)
3469 for (i = 0; i < rs->raid_disks; i++)
3470 if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
3471 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
3472 rs->dev[i].rdev.raid_disk);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003473 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
3474 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
3475 mddev->bitmap_info.daemon_sleep);
3476 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
3477 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
3478 mddev->sync_speed_min);
3479 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
3480 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
3481 mddev->sync_speed_max);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003482 if (write_mostly_params)
3483 for (i = 0; i < rs->raid_disks; i++)
3484 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
3485 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
3486 rs->dev[i].rdev.raid_disk);
Mike Snitzer42863252016-06-02 12:27:46 -04003487 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
Mike Snitzer3fa6cf32016-06-02 11:58:51 -04003488 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003489 mddev->bitmap_info.max_write_behind);
Heinz Mauelshagen4464e362017-03-18 01:39:12 +01003490 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
3491 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
3492 max_nr_stripes);
3493 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
3494 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
3495 (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
3496 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
3497 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
3498 raid10_md_layout_to_copies(mddev->layout));
3499 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
3500 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
3501 raid10_md_layout_to_format(mddev->layout));
3502 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
3503 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
3504 max(rs->delta_disks, mddev->delta_disks));
3505 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
3506 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
3507 (unsigned long long) rs->data_offset);
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003508 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
3509 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
3510 __get_dev_name(rs->journal_dev.dev));
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003511 if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
3512 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
3513 md_journal_mode_to_dm_raid(rs->journal_dev.mode));
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003514 DMEMIT(" %d", rs->raid_disks);
Heinz Mauelshagen7a7c3302016-06-30 13:57:08 +02003515 for (i = 0; i < rs->raid_disks; i++)
3516 DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
3517 __get_dev_name(rs->dev[i].data_dev));
NeilBrown9d09e662011-01-13 20:00:02 +00003518 }
NeilBrown9d09e662011-01-13 20:00:02 +00003519}
3520
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003521static int raid_message(struct dm_target *ti, unsigned int argc, char **argv)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003522{
3523 struct raid_set *rs = ti->private;
3524 struct mddev *mddev = &rs->md;
3525
Jonathan Brassowbe836512013-04-24 11:42:43 +10003526 if (!mddev->pers || !mddev->pers->sync_request)
3527 return -EINVAL;
3528
3529 if (!strcasecmp(argv[0], "frozen"))
3530 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3531 else
3532 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3533
3534 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
3535 if (mddev->sync_thread) {
3536 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3537 md_reap_sync_thread(mddev);
3538 }
3539 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3540 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3541 return -EBUSY;
3542 else if (!strcasecmp(argv[0], "resync"))
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003543 ; /* MD_RECOVERY_NEEDED set below */
3544 else if (!strcasecmp(argv[0], "recover"))
Jonathan Brassowbe836512013-04-24 11:42:43 +10003545 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003546 else {
Mike Snitzerad470472017-02-28 11:55:16 -05003547 if (!strcasecmp(argv[0], "check")) {
Jonathan Brassowbe836512013-04-24 11:42:43 +10003548 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Mike Snitzerad470472017-02-28 11:55:16 -05003549 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3550 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3551 } else if (!strcasecmp(argv[0], "repair")) {
Mike Snitzer105db592017-01-06 15:38:08 -05003552 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3553 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3554 } else
Jonathan Brassowbe836512013-04-24 11:42:43 +10003555 return -EINVAL;
Jonathan Brassowbe836512013-04-24 11:42:43 +10003556 }
3557 if (mddev->ro == 2) {
3558 /* A write to sync_action is enough to justify
3559 * canceling read-auto mode
3560 */
3561 mddev->ro = 0;
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003562 if (!mddev->suspended && mddev->sync_thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003563 md_wakeup_thread(mddev->sync_thread);
3564 }
3565 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003566 if (!mddev->suspended && mddev->thread)
Jonathan Brassowbe836512013-04-24 11:42:43 +10003567 md_wakeup_thread(mddev->thread);
3568
3569 return 0;
3570}
3571
3572static int raid_iterate_devices(struct dm_target *ti,
3573 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00003574{
3575 struct raid_set *rs = ti->private;
Heinz Mauelshagen094f3942016-07-19 14:03:51 +02003576 unsigned int i;
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003577 int r = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00003578
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003579 for (i = 0; !r && i < rs->md.raid_disks; i++)
NeilBrown9d09e662011-01-13 20:00:02 +00003580 if (rs->dev[i].data_dev)
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003581 r = fn(ti,
NeilBrown9d09e662011-01-13 20:00:02 +00003582 rs->dev[i].data_dev,
3583 0, /* No offset on data devs */
3584 rs->md.dev_sectors,
3585 data);
3586
Heinz Mauelshagen73c6f232016-05-19 18:49:24 +02003587 return r;
NeilBrown9d09e662011-01-13 20:00:02 +00003588}
3589
3590static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
3591{
3592 struct raid_set *rs = ti->private;
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003593 unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
NeilBrown9d09e662011-01-13 20:00:02 +00003594
3595 blk_limits_io_min(limits, chunk_size);
Heinz Mauelshagen89d3d9a2016-07-19 13:16:24 +02003596 blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
NeilBrown9d09e662011-01-13 20:00:02 +00003597}
3598
3599static void raid_presuspend(struct dm_target *ti)
3600{
3601 struct raid_set *rs = ti->private;
3602
3603 md_stop_writes(&rs->md);
3604}
3605
3606static void raid_postsuspend(struct dm_target *ti)
3607{
3608 struct raid_set *rs = ti->private;
3609
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003610 if (!rs->md.suspended)
3611 mddev_suspend(&rs->md);
3612
3613 rs->md.ro = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00003614}
3615
Jonathan Brassowf381e712013-05-08 17:57:13 -05003616static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00003617{
Jonathan Brassow9092c022013-05-02 14:19:24 -05003618 int i;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003619 uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
Jonathan Brassow9092c022013-05-02 14:19:24 -05003620 unsigned long flags;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003621 bool cleared = false;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003622 struct dm_raid_superblock *sb;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003623 struct mddev *mddev = &rs->md;
Jonathan Brassow9092c022013-05-02 14:19:24 -05003624 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00003625
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003626 /* RAID personalities have to provide hot add/remove methods or we need to bail out. */
3627 if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
3628 return;
3629
3630 memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
3631
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003632 for (i = 0; i < mddev->raid_disks; i++) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003633 r = &rs->dev[i].rdev;
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003634 /* HM FIXME: enhance journal device recovery processing */
3635 if (test_bit(Journal, &r->flags))
3636 continue;
3637
Heinz Mauelshagene2568462017-01-14 03:53:10 +01003638 if (test_bit(Faulty, &r->flags) &&
3639 r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
Jonathan Brassowf381e712013-05-08 17:57:13 -05003640 DMINFO("Faulty %s device #%d has readable super block."
3641 " Attempting to revive it.",
3642 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003643
3644 /*
3645 * Faulty bit may be set, but sometimes the array can
3646 * be suspended before the personalities can respond
3647 * by removing the device from the array (i.e. calling
Mike Snitzer43157842016-05-30 13:03:37 -04003648 * 'hot_remove_disk'). If they haven't yet removed
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05003649 * the failed device, its 'raid_disk' number will be
3650 * '>= 0' - meaning we must call this function
3651 * ourselves.
3652 */
Jonathan Brassowf381e712013-05-08 17:57:13 -05003653 flags = r->flags;
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003654 clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
3655 if (r->raid_disk >= 0) {
3656 if (mddev->pers->hot_remove_disk(mddev, r)) {
3657 /* Failed to revive this device, try next */
3658 r->flags = flags;
3659 continue;
3660 }
3661 } else
3662 r->raid_disk = r->saved_raid_disk = i;
3663
Jonathan Brassowf381e712013-05-08 17:57:13 -05003664 clear_bit(Faulty, &r->flags);
3665 clear_bit(WriteErrorSeen, &r->flags);
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003666
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003667 if (mddev->pers->hot_add_disk(mddev, r)) {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003668 /* Failed to revive this device, try next */
3669 r->raid_disk = r->saved_raid_disk = -1;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003670 r->flags = flags;
3671 } else {
Heinz Mauelshagenc63ede32017-01-14 03:53:07 +01003672 clear_bit(In_sync, &r->flags);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003673 r->recovery_offset = 0;
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003674 set_bit(i, (void *) cleared_failed_devices);
3675 cleared = true;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003676 }
3677 }
3678 }
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003679
3680 /* If any failed devices could be cleared, update all sbs failed_devices bits */
3681 if (cleared) {
3682 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
3683
Jonathan Brassowf381e712013-05-08 17:57:13 -05003684 rdev_for_each(r, &rs->md) {
Heinz Mauelshagen63c32ed2016-11-30 22:31:05 +01003685 if (test_bit(Journal, &r->flags))
3686 continue;
3687
Jonathan Brassowf381e712013-05-08 17:57:13 -05003688 sb = page_address(r->sb_page);
Heinz Mauelshagena3c06a32016-08-09 14:55:35 +02003689 sb_retrieve_failed_devices(sb, failed_devices);
3690
3691 for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
3692 failed_devices[i] &= ~cleared_failed_devices[i];
3693
3694 sb_update_failed_devices(sb, failed_devices);
Jonathan Brassowf381e712013-05-08 17:57:13 -05003695 }
3696 }
3697}
3698
Mike Snitzere6ca5e12016-06-02 15:27:22 -04003699static int __load_dirty_region_bitmap(struct raid_set *rs)
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003700{
3701 int r = 0;
3702
3703 /* Try loading the bitmap unless "raid0", which does not have one */
3704 if (!rs_is_raid0(rs) &&
Mike Snitzer42863252016-06-02 12:27:46 -04003705 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003706 r = bitmap_load(&rs->md);
3707 if (r)
3708 DMERR("Failed to load bitmap");
3709 }
3710
3711 return r;
3712}
3713
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003714/* Enforce updating all superblocks */
3715static void rs_update_sbs(struct raid_set *rs)
3716{
3717 struct mddev *mddev = &rs->md;
3718 int ro = mddev->ro;
3719
Shaohua Li29530792016-12-08 15:48:19 -08003720 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003721 mddev->ro = 0;
3722 md_update_sb(mddev, 1);
3723 mddev->ro = ro;
3724}
3725
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003726/*
3727 * Reshape changes raid algorithm of @rs to new one within personality
3728 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
3729 * disks from a raid set thus growing/shrinking it or resizes the set
3730 *
3731 * Call mddev_lock_nointr() before!
3732 */
3733static int rs_start_reshape(struct raid_set *rs)
3734{
3735 int r;
3736 struct mddev *mddev = &rs->md;
3737 struct md_personality *pers = mddev->pers;
3738
3739 r = rs_setup_reshape(rs);
3740 if (r)
3741 return r;
3742
3743 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
3744 if (mddev->suspended)
3745 mddev_resume(mddev);
3746
3747 /*
3748 * Check any reshape constraints enforced by the personalility
3749 *
3750 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
3751 */
3752 r = pers->check_reshape(mddev);
3753 if (r) {
3754 rs->ti->error = "pers->check_reshape() failed";
3755 return r;
3756 }
3757
3758 /*
3759 * Personality may not provide start reshape method in which
3760 * case check_reshape above has already covered everything
3761 */
3762 if (pers->start_reshape) {
3763 r = pers->start_reshape(mddev);
3764 if (r) {
3765 rs->ti->error = "pers->start_reshape() failed";
3766 return r;
3767 }
3768 }
3769
3770 /* Suspend because a resume will happen in raid_resume() */
3771 if (!mddev->suspended)
3772 mddev_suspend(mddev);
3773
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003774 /*
3775 * Now reshape got set up, update superblocks to
3776 * reflect the fact so that a table reload will
3777 * access proper superblock content in the ctr.
3778 */
3779 rs_update_sbs(rs);
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003780
3781 return 0;
3782}
3783
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003784static int raid_preresume(struct dm_target *ti)
3785{
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003786 int r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003787 struct raid_set *rs = ti->private;
3788 struct mddev *mddev = &rs->md;
3789
3790 /* This is a resume after a suspend of the set -> it's already started */
Mike Snitzer42863252016-06-02 12:27:46 -04003791 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003792 return 0;
3793
3794 /*
3795 * The superblocks need to be updated on disk if the
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003796 * array is new or new devices got added (thus zeroed
3797 * out by userspace) or __load_dirty_region_bitmap
3798 * will overwrite them in core with old data or fail.
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003799 */
Heinz Mauelshagen6e209022016-06-14 15:23:13 -04003800 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
3801 rs_update_sbs(rs);
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003802
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003803 /* Load the bitmap from disk unless raid0 */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003804 r = __load_dirty_region_bitmap(rs);
3805 if (r)
3806 return r;
3807
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003808 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
Dmitry Bilunov7a0c5c52017-03-30 18:14:26 +03003809 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
Heinz Mauelshagen4257e082016-06-14 01:46:03 +02003810 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
3811 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
3812 to_bytes(rs->requested_bitmap_chunk_sectors), 0);
3813 if (r)
3814 DMERR("Failed to resize bitmap");
3815 }
3816
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003817 /* Check for any resize/reshape on @rs and adjust/initiate */
3818 /* Be prepared for mddev_resume() in raid_resume() */
3819 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3820 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003821 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3822 mddev->resync_min = mddev->recovery_cp;
3823 }
3824
3825 rs_set_capacity(rs);
3826
Heinz Mauelshagen345a6cd2016-06-25 02:42:54 +02003827 /* Check for any reshape request unless new raid set */
Heinz Mauelshagen9dbd1aa2016-06-13 17:55:14 +02003828 if (test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3829 /* Initiate a reshape. */
3830 mddev_lock_nointr(mddev);
3831 r = rs_start_reshape(rs);
3832 mddev_unlock(mddev);
3833 if (r)
3834 DMWARN("Failed to check/start reshape, continuing without change");
3835 r = 0;
3836 }
3837
3838 return r;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003839}
3840
Jonathan Brassowf381e712013-05-08 17:57:13 -05003841static void raid_resume(struct dm_target *ti)
3842{
3843 struct raid_set *rs = ti->private;
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003844 struct mddev *mddev = &rs->md;
Jonathan Brassowf381e712013-05-08 17:57:13 -05003845
Mike Snitzer42863252016-06-02 12:27:46 -04003846 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003847 /*
3848 * A secondary resume while the device is active.
3849 * Take this opportunity to check whether any failed
3850 * devices are reachable again.
3851 */
3852 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003853 }
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003854
3855 mddev->ro = 0;
3856 mddev->in_sync = 0;
3857
Heinz Mauelshagend36a1952017-02-28 19:17:49 +01003858 /*
3859 * Keep the RAID set frozen if reshape/rebuild flags are set.
3860 * The RAID set is unfrozen once the next table load/resume,
3861 * which clears the reshape/rebuild flags, occurs.
3862 * This ensures that the constructor for the inactive table
3863 * retrieves an up-to-date reshape_position.
3864 */
3865 if (!(rs->ctr_flags & RESUME_STAY_FROZEN_FLAGS))
3866 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Heinz Mauelshagen31e10a42016-08-10 02:45:59 +02003867
3868 if (mddev->suspended)
3869 mddev_resume(mddev);
NeilBrown9d09e662011-01-13 20:00:02 +00003870}
3871
3872static struct target_type raid_target = {
3873 .name = "raid",
Heinz Mauelshagen6e536362017-03-22 17:44:38 +01003874 .version = {1, 11, 1},
NeilBrown9d09e662011-01-13 20:00:02 +00003875 .module = THIS_MODULE,
3876 .ctr = raid_ctr,
3877 .dtr = raid_dtr,
3878 .map = raid_map,
3879 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10003880 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00003881 .iterate_devices = raid_iterate_devices,
3882 .io_hints = raid_io_hints,
3883 .presuspend = raid_presuspend,
3884 .postsuspend = raid_postsuspend,
Heinz Mauelshagenecbfb9f2016-05-19 18:49:33 +02003885 .preresume = raid_preresume,
NeilBrown9d09e662011-01-13 20:00:02 +00003886 .resume = raid_resume,
3887};
3888
3889static int __init dm_raid_init(void)
3890{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11003891 DMINFO("Loading target version %u.%u.%u",
3892 raid_target.version[0],
3893 raid_target.version[1],
3894 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00003895 return dm_register_target(&raid_target);
3896}
3897
3898static void __exit dm_raid_exit(void)
3899{
3900 dm_unregister_target(&raid_target);
3901}
3902
3903module_init(dm_raid_init);
3904module_exit(dm_raid_exit);
3905
Heinz Mauelshagen48cf06b2014-09-24 17:47:19 +02003906module_param(devices_handle_discard_safely, bool, 0644);
3907MODULE_PARM_DESC(devices_handle_discard_safely,
3908 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
3909
Mike Snitzeref9b85a2016-06-02 12:02:19 -04003910MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
3911MODULE_ALIAS("dm-raid0");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05003912MODULE_ALIAS("dm-raid1");
3913MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00003914MODULE_ALIAS("dm-raid4");
3915MODULE_ALIAS("dm-raid5");
3916MODULE_ALIAS("dm-raid6");
3917MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
Heinz Mauelshagen3a1c1ef2016-05-19 18:49:34 +02003918MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
NeilBrown9d09e662011-01-13 20:00:02 +00003919MODULE_LICENSE("GPL");