Linux 6.16-rc6
[linux-block.git] / drivers / md / md.h
CommitLineData
af1a8899 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4 2/*
7e841526 3 md.h : kernel internal structure of the Linux MD driver
1da177e4 4 Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman
f72ffdd6 5
1da177e4
LT
6*/
7
63fe0817
CH
8#ifndef _MD_MD_H
9#define _MD_MD_H
10
11#include <linux/blkdev.h>
66114cad 12#include <linux/backing-dev.h>
fc974ee2 13#include <linux/badblocks.h>
63fe0817
CH
14#include <linux/kobject.h>
15#include <linux/list.h>
16#include <linux/mm.h>
17#include <linux/mutex.h>
18#include <linux/timer.h>
19#include <linux/wait.h>
20#include <linux/workqueue.h>
d3beb7c9 21#include <linux/raid/md_u.h>
c396b90e 22#include <trace/events/block.h>
9361401e 23
1da177e4 24#define MaxSector (~(sector_t)0)
1da177e4 25
d3beb7c9
YK
26enum md_submodule_type {
27 MD_PERSONALITY = 0,
28 MD_CLUSTER,
29 MD_BITMAP, /* TODO */
30};
31
32enum md_submodule_id {
33 ID_LINEAR = LEVEL_LINEAR,
34 ID_RAID0 = 0,
35 ID_RAID1 = 1,
36 ID_RAID4 = 4,
37 ID_RAID5 = 5,
38 ID_RAID6 = 6,
39 ID_RAID10 = 10,
40 ID_CLUSTER,
41 ID_BITMAP, /* TODO */
42 ID_LLBITMAP, /* TODO */
43};
44
45struct md_submodule_head {
46 enum md_submodule_type type;
47 enum md_submodule_id id;
48 const char *name;
49 struct module *owner;
50};
51
46533ff7
N
52/*
53 * These flags should really be called "NO_RETRY" rather than
54 * "FAILFAST" because they don't make any promise about time lapse,
55 * only about the number of retries, which will be zero.
56 * REQ_FAILFAST_DRIVER is not included because
57 * Commit: 4a27446f3e39 ("[SCSI] modify scsi to handle new fail fast flags.")
58 * seems to suggest that the errors it avoids retrying should usually
59 * be retried.
60 */
61#define MD_FAILFAST (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT)
69b00b5b 62
a85aa09d
YK
63/* Status of sync thread. */
64enum sync_action {
65 /*
66 * Represent by MD_RECOVERY_SYNC, start when:
67 * 1) after assemble, sync data from first rdev to other copies, this
68 * must be done first before other sync actions and will only execute
69 * once;
70 * 2) resize the array(notice that this is not reshape), sync data for
71 * the new range;
72 */
73 ACTION_RESYNC,
74 /*
75 * Represent by MD_RECOVERY_RECOVER, start when:
76 * 1) for new replacement, sync data based on the replace rdev or
77 * available copies from other rdev;
78 * 2) for new member disk while the array is degraded, sync data from
79 * other rdev;
80 * 3) reassemble after power failure or re-add a hot removed rdev, sync
81 * data from first rdev to other copies based on bitmap;
82 */
83 ACTION_RECOVER,
84 /*
85 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED |
86 * MD_RECOVERY_CHECK, start when user echo "check" to sysfs api
87 * sync_action, used to check if data copies from differenct rdev are
88 * the same. The number of mismatch sectors will be exported to user
89 * by sysfs api mismatch_cnt;
90 */
91 ACTION_CHECK,
92 /*
93 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED, start when
94 * user echo "repair" to sysfs api sync_action, usually paired with
95 * ACTION_CHECK, used to force syncing data once user found that there
96 * are inconsistent data,
97 */
98 ACTION_REPAIR,
99 /*
100 * Represent by MD_RECOVERY_RESHAPE, start when new member disk is added
101 * to the conf, notice that this is different from spares or
102 * replacement;
103 */
104 ACTION_RESHAPE,
105 /*
106 * Represent by MD_RECOVERY_FROZEN, can be set by sysfs api sync_action
107 * or internal usage like setting the array read-only, will forbid above
108 * actions.
109 */
110 ACTION_FROZEN,
111 /*
112 * All above actions don't match.
113 */
114 ACTION_IDLE,
115 NR_SYNC_ACTIONS,
116};
117
69b00b5b
GJ
118/*
119 * The struct embedded in rdev is used to serialize IO.
120 */
121struct serial_in_rdev {
122 struct rb_root_cached serial_rb;
123 spinlock_t serial_lock;
124 wait_queue_head_t serial_io_wait;
125};
126
1da177e4
LT
127/*
128 * MD's 'extended' device
129 */
3cb03002 130struct md_rdev {
1da177e4
LT
131 struct list_head same_set; /* RAID devices within the same set */
132
dd8ac336 133 sector_t sectors; /* Device size (in 512bytes sectors) */
fd01b88c 134 struct mddev *mddev; /* RAID array if running */
e5797ae7 135 unsigned long last_events; /* IO event timestamp */
1da177e4 136
a6ff7e08
JB
137 /*
138 * If meta_bdev is non-NULL, it means that a separate device is
139 * being used to store the metadata (superblock/bitmap) which
140 * would otherwise be contained on the same device as the data (bdev).
141 */
142 struct block_device *meta_bdev;
1da177e4 143 struct block_device *bdev; /* block device handle */
a28d893e 144 struct file *bdev_file; /* Handle from open for bdev */
1da177e4 145
2699b672 146 struct page *sb_page, *bb_page;
1da177e4 147 int sb_loaded;
42543769 148 __u64 sb_events;
1da177e4 149 sector_t data_offset; /* start of data in array */
c6563a8c 150 sector_t new_data_offset;/* only relevant while reshaping */
f72ffdd6 151 sector_t sb_start; /* offset of the super block (in 512byte sectors) */
0002b271 152 int sb_size; /* bytes in the superblock */
1da177e4
LT
153 int preferred_minor; /* autorun support */
154
86e6ffdd
N
155 struct kobject kobj;
156
1da177e4
LT
157 /* A device can be in one of three states based on two flags:
158 * Not working: faulty==1 in_sync==0
159 * Fully working: faulty==0 in_sync==1
160 * Working, but not
161 * in sync with array
162 * faulty==0 in_sync==0
163 *
164 * It can never have faulty==1, in_sync==1
165 * This reduces the burden of testing multiple flags in many cases
166 */
1da177e4 167
2d78f8c4 168 unsigned long flags; /* bit set of 'enum flag_bits' bits. */
6bfe0b49 169 wait_queue_head_t blocked_wait;
8ddf9efe 170
1da177e4
LT
171 int desc_nr; /* descriptor index in the superblock */
172 int raid_disk; /* role of device in array */
e93f68a1
N
173 int new_raid_disk; /* role that the device will have in
174 * the array after a level-change completes.
175 */
41158c7e
N
176 int saved_raid_disk; /* role that device used to have in the
177 * array and could again if we did a partial
178 * resync from the bitmap
179 */
3069aa8d
SL
180 union {
181 sector_t recovery_offset;/* If this device has been partially
5fd6c1dc
N
182 * recovered, this is where we were
183 * up to.
184 */
3069aa8d
SL
185 sector_t journal_tail; /* If this device is a journal device,
186 * this is the journal tail (journal
187 * recovery start point)
188 */
189 };
1da177e4
LT
190
191 atomic_t nr_pending; /* number of pending requests.
192 * only maintained for arrays that
193 * support hot removal
194 */
ba22dcbf
N
195 atomic_t read_errors; /* number of consecutive read errors that
196 * we have tried to ignore.
197 */
0e3ef49e 198 time64_t last_read_error; /* monotonic time since our
1e50915f
RB
199 * last read error
200 */
4dbcdc75
N
201 atomic_t corrected_errors; /* number of corrected read errors,
202 * for reporting to userspace and storing
203 * in superblock.
204 */
3e148a32 205
69b00b5b 206 struct serial_in_rdev *serial; /* used for raid1 io serialization */
3e148a32 207
324a56e1 208 struct kernfs_node *sysfs_state; /* handle for 'state'
3c0ee63a 209 * sysfs entry */
e1a86dbb
JB
210 /* handle for 'unacknowledged_bad_blocks' sysfs dentry */
211 struct kernfs_node *sysfs_unack_badblocks;
212 /* handle for 'bad_blocks' sysfs dentry */
213 struct kernfs_node *sysfs_badblocks;
fc974ee2 214 struct badblocks badblocks;
ea0213e0
AP
215
216 struct {
217 short offset; /* Offset from superblock to start of PPL.
218 * Not used by external metadata. */
219 unsigned int size; /* Size in sectors of the PPL space */
220 sector_t sector; /* First sector of the PPL space */
221 } ppl;
1da177e4 222};
2d78f8c4
N
223enum flag_bits {
224 Faulty, /* device is known to have a fault */
225 In_sync, /* device is in_sync with rest of array */
8313b8e5 226 Bitmap_sync, /* ..actually, not quite In_sync. Need a
4aaf7694
GJ
227 * bitmap-based recovery to get fully in sync.
228 * The bit is only meaningful before device
229 * has been passed to pers->hot_add_disk.
8313b8e5 230 */
2d78f8c4
N
231 WriteMostly, /* Avoid reading if at all possible */
232 AutoDetected, /* added by auto-detect */
233 Blocked, /* An error occurred but has not yet
234 * been acknowledged by the metadata
235 * handler, so don't allow writes
236 * until it is cleared */
237 WriteErrorSeen, /* A write error has been seen on this
238 * device
239 */
240 FaultRecorded, /* Intermediate state for clearing
241 * Blocked. The Fault is/will-be
242 * recorded in the metadata, but that
243 * metadata hasn't been stored safely
244 * on disk yet.
245 */
246 BlockedBadBlocks, /* A writer is blocked because they
247 * found an unacknowledged bad-block.
248 * This can safely be cleared at any
249 * time, and the writer will re-check.
250 * It may be set at any time, and at
251 * worst the writer will timeout and
252 * re-check. So setting it as
253 * accurately as possible is good, but
254 * not absolutely critical.
255 */
256 WantReplacement, /* This device is a candidate to be
257 * hot-replaced, either because it has
258 * reported some faults, or because
259 * of explicit request.
260 */
261 Replacement, /* This device is a replacement for
262 * a want_replacement device with same
263 * raid_disk number.
264 */
1aee41f6
GR
265 Candidate, /* For clustered environments only:
266 * This device is seen locally but not
267 * by the whole cluster
268 */
bac624f3
SL
269 Journal, /* This device is used as journal for
270 * raid-5/6.
271 * Usually, this device should be faster
272 * than other devices in the array
273 */
659b254f 274 ClusterRemove,
35b785f7
TM
275 ExternalBbl, /* External metadata provides bad
276 * block management for a disk
277 */
688834e6
N
278 FailFast, /* Minimal retries should be attempted on
279 * this device, so use REQ_FAILFAST_DEV.
280 * Also don't try to repair failed reads.
281 * It is expects that no bad block log
282 * is present.
283 */
46533ff7
N
284 LastDev, /* Seems to be the last working dev as
285 * it didn't fail, so don't use FailFast
286 * any more for metadata
287 */
404659cf
GJ
288 CollisionCheck, /*
289 * check if there is collision between raid1
290 * serial bios.
3e148a32 291 */
2c27d09d 292 Nonrot, /* non-rotational device (SSD) */
2d78f8c4 293};
1da177e4 294
d301f164
ZQ
295static inline int is_badblock(struct md_rdev *rdev, sector_t s, sector_t sectors,
296 sector_t *first_bad, sector_t *bad_sectors)
2230dfe4
N
297{
298 if (unlikely(rdev->badblocks.count)) {
fc974ee2 299 int rv = badblocks_check(&rdev->badblocks, rdev->data_offset + s,
2230dfe4
N
300 sectors,
301 first_bad, bad_sectors);
302 if (rv)
303 *first_bad -= rdev->data_offset;
304 return rv;
305 }
306 return 0;
307}
3a0f007b
YK
308
309static inline int rdev_has_badblock(struct md_rdev *rdev, sector_t s,
310 int sectors)
311{
312 sector_t first_bad;
d301f164 313 sector_t bad_sectors;
3a0f007b
YK
314
315 return is_badblock(rdev, s, sectors, &first_bad, &bad_sectors);
316}
317
7e5102dd
ZQ
318extern bool rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
319 int is_new);
320extern void rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
321 int is_new);
c4ce867f 322struct md_cluster_info;
c594de04 323struct md_cluster_operations;
c4ce867f 324
9631abdb
MT
325/**
326 * enum mddev_flags - md device flags.
327 * @MD_ARRAY_FIRST_USE: First use of array, needs initialization.
328 * @MD_CLOSING: If set, we are closing the array, do not open it then.
329 * @MD_JOURNAL_CLEAN: A raid with journal is already clean.
330 * @MD_HAS_JOURNAL: The raid array has journal feature set.
331 * @MD_CLUSTER_RESYNC_LOCKED: cluster raid only, which means node, already took
332 * resync lock, need to release the lock.
333 * @MD_FAILFAST_SUPPORTED: Using MD_FAILFAST on metadata writes is supported as
334 * calls to md_error() will never cause the array to
335 * become failed.
336 * @MD_HAS_PPL: The raid array has PPL feature set.
337 * @MD_HAS_MULTIPLE_PPLS: The raid array has multiple PPLs feature set.
9631abdb
MT
338 * @MD_NOT_READY: do_md_run() is active, so 'array_state', ust not report that
339 * array is ready yet.
340 * @MD_BROKEN: This is used to stop writes and mark array as failed.
12a6caf2 341 * @MD_DELETED: This device is being deleted
9631abdb
MT
342 *
343 * change UNSUPPORTED_MDDEV_FLAGS for each array type if new flag is added
344 */
be306c29 345enum mddev_flags {
9631abdb
MT
346 MD_ARRAY_FIRST_USE,
347 MD_CLOSING,
348 MD_JOURNAL_CLEAN,
349 MD_HAS_JOURNAL,
350 MD_CLUSTER_RESYNC_LOCKED,
351 MD_FAILFAST_SUPPORTED,
352 MD_HAS_PPL,
353 MD_HAS_MULTIPLE_PPLS,
9631abdb
MT
354 MD_NOT_READY,
355 MD_BROKEN,
12a6caf2 356 MD_DELETED,
be306c29 357};
2953079c
SL
358
359enum mddev_sb_flags {
360 MD_SB_CHANGE_DEVS, /* Some device status has changed */
361 MD_SB_CHANGE_CLEAN, /* transition to or from 'clean' */
362 MD_SB_CHANGE_PENDING, /* switch from 'clean' to 'active' in progress */
363 MD_SB_NEED_REWRITE, /* metadata write needs to be repeated */
364};
365
404659cf
GJ
366#define NR_SERIAL_INFOS 8
367/* record current range of serialize IOs */
368struct serial_info {
69b00b5b
GJ
369 struct rb_node node;
370 sector_t start; /* start sector of rb node */
371 sector_t last; /* end sector of rb node */
372 sector_t _subtree_last; /* highest sector in subtree of rb node */
3e148a32
GJ
373};
374
eac58d08
LG
375/*
376 * mddev->curr_resync stores the current sector of the resync but
377 * also has some overloaded values.
378 */
379enum {
380 /* No resync in progress */
381 MD_RESYNC_NONE = 0,
382 /* Yielded to allow another conflicting resync to commence */
383 MD_RESYNC_YIELDED = 1,
384 /* Delayed to check that there is no conflict with another sync */
385 MD_RESYNC_DELAYED = 2,
386 /* Any value greater than or equal to this is in an active resync */
387 MD_RESYNC_ACTIVE = 3,
388};
389
fd01b88c 390struct mddev {
1da177e4 391 void *private;
84fc4b56 392 struct md_personality *pers;
1da177e4
LT
393 dev_t unit;
394 int md_minor;
7a0a5355 395 struct list_head disks;
850b2b42 396 unsigned long flags;
2953079c 397 unsigned long sb_flags;
850b2b42 398
409c57f3 399 int suspended;
714d2015 400 struct mutex suspend_mutex;
72adae23 401 struct percpu_ref active_io;
1da177e4 402 int ro;
bb4f1e9d
N
403 int sysfs_active; /* set when sysfs deletes
404 * are happening, so run/
405 * takeover/stop are not safe
406 */
7168be3c
YK
407 struct gendisk *gendisk; /* mdraid gendisk */
408 struct gendisk *dm_gendisk; /* dm-raid gendisk */
1da177e4 409
eae1701f 410 struct kobject kobj;
d3374825
N
411 int hold_active;
412#define UNTIL_IOCTL 1
efeb53c0 413#define UNTIL_STOP 2
eae1701f 414
1da177e4
LT
415 /* Superblock information */
416 int major_version,
417 minor_version,
418 patch_version;
419 int persistent;
f72ffdd6 420 int external; /* metadata is
e691063a
N
421 * managed externally */
422 char metadata_type[17]; /* externally set*/
77a68698 423 int chunk_sectors;
9ebc6ef1 424 time64_t ctime, utime;
1da177e4 425 int level, layout;
d9d166c2 426 char clevel[16];
1da177e4
LT
427 int raid_disks;
428 int max_disks;
f72ffdd6 429 sector_t dev_sectors; /* used size of
58c0fed4 430 * component devices */
f233ea5c 431 sector_t array_sectors; /* exported array size */
b522adcd
DW
432 int external_size; /* size managed
433 * externally */
1da177e4 434 __u64 events;
a8707c08
N
435 /* If the last 'event' was simply a clean->dirty transition, and
436 * we didn't write it to the spares, then it is safe and simple
437 * to just decrement the event count on a dirty->clean transition.
438 * So we record that possibility here.
439 */
440 int can_decrease_events;
1da177e4
LT
441
442 char uuid[16];
443
f6705578
N
444 /* If the array is being reshaped, we need to record the
445 * new shape and an indication of where we are up to.
446 * This is written to the superblock.
447 * If reshape_position is MaxSector, then no reshape is happening (yet).
448 */
449 sector_t reshape_position;
664e7c41 450 int delta_disks, new_level, new_layout;
77a68698 451 int new_chunk_sectors;
2c810cdd 452 int reshape_backwards;
f6705578 453
44693154
YK
454 struct md_thread __rcu *thread; /* management thread */
455 struct md_thread __rcu *sync_thread; /* doing resync or reconstruct */
c4a39551 456
d249e541
YK
457 /*
458 * Set when a sync operation is started. It holds this value even
c4a39551 459 * when the sync thread is "frozen" (interrupted) or "idle" (stopped
d249e541 460 * or finished). It is overwritten when a new sync operation is begun.
c4a39551 461 */
d249e541 462 enum sync_action last_sync_action;
ff4e8d9a 463 sector_t curr_resync; /* last block scheduled */
97e4f42d
N
464 /* As resync requests can complete out of order, we cannot easily track
465 * how much resync has been completed. So we occasionally pause until
466 * everything completes, then set curr_resync_completed to curr_resync.
467 * As such it may be well behind the real resync mark, but it is a value
468 * we are certain of.
469 */
470 sector_t curr_resync_completed;
1da177e4
LT
471 unsigned long resync_mark; /* a recent timestamp */
472 sector_t resync_mark_cnt;/* blocks written at resync_mark */
ff4e8d9a 473 sector_t curr_mark_cnt; /* blocks scheduled now */
1da177e4
LT
474
475 sector_t resync_max_sectors; /* may be set by personality */
9d88883e 476
7f7583d4 477 atomic64_t resync_mismatches; /* count of sectors where
9d88883e
N
478 * parity/replica mismatch found
479 */
e464eafd
N
480
481 /* allow user-space to request suspension of IO to regions of the array */
482 sector_t suspend_lo;
483 sector_t suspend_hi;
88202a0c
N
484 /* if zero, use the system-wide default */
485 int sync_speed_min;
486 int sync_speed_max;
03720d82 487 int sync_io_depth;
88202a0c 488
90b08710
BS
489 /* resync even though the same disks are shared among md-devices */
490 int parallel_resync;
491
6ff8d8ec 492 int ok_start_degraded;
5fd6c1dc 493
1da177e4 494 unsigned long recovery;
5389042f
N
495 /* If a RAID personality determines that recovery (of a particular
496 * device) will fail due to a read error on the source device, it
497 * takes a copy of this number and does not attempt recovery again
498 * until this number changes.
499 */
500 int recovery_disabled;
1da177e4
LT
501
502 int in_sync; /* know to not need resync */
c8c00a69
N
503 /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
504 * that we are never stopping an array while it is open.
505 * 'reconfig_mutex' protects all other reconfiguration.
506 * These locks are separate due to conflicting interactions
a8698707 507 * with disk->open_mutex.
c8c00a69 508 * Lock ordering is:
a8698707
CH
509 * reconfig_mutex -> disk->open_mutex
510 * disk->open_mutex -> open_mutex: e.g. __blkdev_get -> md_open
c8c00a69
N
511 */
512 struct mutex open_mutex;
df5b89b3 513 struct mutex reconfig_mutex;
f2ea68cf
N
514 atomic_t active; /* general refcount */
515 atomic_t openers; /* number of active opens */
1da177e4 516
f0b4f7e2
N
517 int changed; /* True if we might need to
518 * reread partition info */
1da177e4
LT
519 int degraded; /* whether md should consider
520 * adding a spare
521 */
522
e5797ae7 523 unsigned long normal_io_events; /* IO event timestamp */
1da177e4
LT
524 atomic_t recovery_active; /* blocks scheduled, but not written */
525 wait_queue_head_t recovery_wait;
526 sector_t recovery_cp;
5e96ee65
NB
527 sector_t resync_min; /* user requested sync
528 * starts here */
c6207277
N
529 sector_t resync_max; /* resync should pause
530 * when it gets here */
06d91a5f 531
324a56e1 532 struct kernfs_node *sysfs_state; /* handle for 'array_state'
b62b7590
N
533 * file in sysfs.
534 */
324a56e1 535 struct kernfs_node *sysfs_action; /* handle for 'sync_action' */
e1a86dbb
JB
536 struct kernfs_node *sysfs_completed; /*handle for 'sync_completed' */
537 struct kernfs_node *sysfs_degraded; /*handle for 'degraded' */
538 struct kernfs_node *sysfs_level; /*handle for 'level' */
b62b7590 539
ac619781
YK
540 /* used for delayed sysfs removal */
541 struct work_struct del_work;
542 /* used for register new sync thread */
543 struct work_struct sync_work;
d3374825 544
85572d7c
N
545 /* "lock" protects:
546 * flush_bio transition from NULL to !NULL
547 * rdev superblocks, events
548 * clearing MD_CHANGE_*
549 * in_sync - and related safemode and MD_CHANGE changes
36d091f4 550 * pers (also protected by reconfig_mutex and pending IO).
978a7a47 551 * clearing ->bitmap
4af1a041 552 * clearing ->bitmap_info.file
23da422b
N
553 * changing ->resync_{min,max}
554 * setting MD_RECOVERY_RUNNING (which interacts with resync_{min,max})
85572d7c
N
555 */
556 spinlock_t lock;
3d310eb7 557 wait_queue_head_t sb_wait; /* for waiting on superblock updates */
7bfa19f2 558 atomic_t pending_writes; /* number of active superblock writes */
06d91a5f 559
1da177e4
LT
560 unsigned int safemode; /* if set, update "clean" superblock
561 * when no writes pending.
f72ffdd6 562 */
1da177e4
LT
563 unsigned int safemode_delay;
564 struct timer_list safemode_timer;
4ad23a97
N
565 struct percpu_ref writes_pending;
566 int sync_checkers; /* # of threads checking writes_pending */
1da177e4 567
59fdd433 568 void *bitmap; /* the bitmap for the device */
7add9db6 569 struct bitmap_operations *bitmap_ops;
c3d9714e
N
570 struct {
571 struct file *file; /* the bitmap file */
f6af949c 572 loff_t offset; /* offset from superblock of
c3d9714e
N
573 * start of bitmap. May be
574 * negative, but not '0'
f6af949c 575 * For external metadata, offset
f72ffdd6 576 * from start of device.
c3d9714e 577 */
6409bb05 578 unsigned long space; /* space available at this offset */
f6af949c 579 loff_t default_offset; /* this is the offset to use when
c3d9714e
N
580 * hot-adding a bitmap. It should
581 * eventually be settable by sysfs.
582 */
6409bb05
N
583 unsigned long default_space; /* space available at
584 * default offset */
c3d9714e 585 struct mutex mutex;
42a04b50 586 unsigned long chunksize;
ac2f40be 587 unsigned long daemon_sleep; /* how many jiffies between updates? */
42a04b50 588 unsigned long max_write_behind; /* write-behind mode */
ece5cff0 589 int external;
c4ce867f 590 int nodes; /* Maximum number of nodes in the cluster */
cf921cc1 591 char cluster_name[64]; /* Name of the cluster */
c3d9714e 592 } bitmap_info;
32a7627c 593
f72ffdd6 594 atomic_t max_corr_read_errors; /* max read retries */
1da177e4 595 struct list_head all_mddevs;
a2826aa9 596
c32dc040 597 const struct attribute_group *to_remove;
252ac522 598
afeee514
KO
599 struct bio_set bio_set;
600 struct bio_set sync_set; /* for sync operations like
5a85071c
N
601 * metadata and bitmap writes
602 */
c687297b 603 struct bio_set io_clone_set;
a167f663 604
768a418d 605 struct work_struct event_work; /* used by dm to report failure event */
404659cf 606 mempool_t *serial_info_pool;
fd01b88c 607 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
c4ce867f 608 struct md_cluster_info *cluster_info;
87a86277 609 struct md_cluster_operations *cluster_ops;
15858fa5 610 unsigned int good_device_nr; /* good device num within cluster raid */
78f57ef9 611 unsigned int noio_flag; /* for memalloc scope API */
4b6c1060 612
3ce94ce5
YK
613 /*
614 * Temporarily store rdev that will be finally removed when
4934b640 615 * reconfig_mutex is unlocked, protected by reconfig_mutex.
3ce94ce5
YK
616 */
617 struct list_head deleting;
3ce94ce5 618
130443d6
YK
619 /* The sequence number for sync thread */
620 atomic_t sync_seq;
6f56f0c4 621
4b6c1060 622 bool has_superblocks:1;
9a567843 623 bool fail_last_dev:1;
3938f5fb 624 bool serialize_policy:1;
be306c29
N
625};
626
627enum recovery_flags {
0476d09c
YK
628 /* flags for sync thread running status */
629
630 /*
631 * set when one of sync action is set and new sync thread need to be
632 * registered, or just add/remove spares from conf.
633 */
634 MD_RECOVERY_NEEDED,
635 /* sync thread is running, or about to be started */
636 MD_RECOVERY_RUNNING,
637 /* sync thread needs to be aborted for some reason */
638 MD_RECOVERY_INTR,
639 /* sync thread is done and is waiting to be unregistered */
640 MD_RECOVERY_DONE,
641 /* running sync thread must abort immediately, and not restart */
642 MD_RECOVERY_FROZEN,
643 /* waiting for pers->start() to finish */
644 MD_RECOVERY_WAIT,
645 /* interrupted because io-error */
646 MD_RECOVERY_ERROR,
647
a85aa09d 648 /* flags determines sync action, see details in enum sync_action */
0476d09c
YK
649
650 /* if just this flag is set, action is resync. */
651 MD_RECOVERY_SYNC,
652 /*
653 * paired with MD_RECOVERY_SYNC, if MD_RECOVERY_CHECK is not set,
654 * action is repair, means user requested resync.
655 */
656 MD_RECOVERY_REQUESTED,
be306c29 657 /*
0476d09c
YK
658 * paired with MD_RECOVERY_SYNC and MD_RECOVERY_REQUESTED, action is
659 * check.
be306c29 660 */
0476d09c
YK
661 MD_RECOVERY_CHECK,
662 /* recovery, or need to try it */
663 MD_RECOVERY_RECOVER,
664 /* reshape */
665 MD_RECOVERY_RESHAPE,
666 /* remote node is running resync thread */
667 MD_RESYNCING_REMOTE,
1da177e4
LT
668};
669
314e9af0
YK
670enum md_ro_state {
671 MD_RDWR,
672 MD_RDONLY,
673 MD_AUTO_READ,
674 MD_MAX_STATE
675};
676
677static inline bool md_is_rdwr(struct mddev *mddev)
678{
679 return (mddev->ro == MD_RDWR);
680}
681
503f9d43
YK
682static inline bool reshape_interrupted(struct mddev *mddev)
683{
684 /* reshape never start */
685 if (mddev->reshape_position == MaxSector)
686 return false;
687
688 /* interrupted */
689 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
690 return true;
691
692 /* running reshape will be interrupted soon. */
693 if (test_bit(MD_RECOVERY_WAIT, &mddev->recovery) ||
694 test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
695 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
696 return true;
697
698 return false;
699}
700
5c47daf6
N
701static inline int __must_check mddev_lock(struct mddev *mddev)
702{
703 return mutex_lock_interruptible(&mddev->reconfig_mutex);
704}
705
706/* Sometimes we need to take the lock in a situation where
707 * failure due to interrupts is not acceptable.
708 */
709static inline void mddev_lock_nointr(struct mddev *mddev)
710{
711 mutex_lock(&mddev->reconfig_mutex);
712}
713
5c47daf6
N
714static inline int mddev_trylock(struct mddev *mddev)
715{
716 return mutex_trylock(&mddev->reconfig_mutex);
717}
718extern void mddev_unlock(struct mddev *mddev);
719
84fc4b56 720struct md_personality
1da177e4 721{
d3beb7c9 722 struct md_submodule_head head;
3d44e1d1 723
775d7831 724 bool __must_check (*make_request)(struct mddev *mddev, struct bio *bio);
d5d885fd
SL
725 /*
726 * start up works that do NOT require md_thread. tasks that
727 * requires md_thread should go into start()
728 */
fd01b88c 729 int (*run)(struct mddev *mddev);
d5d885fd
SL
730 /* start up works that require md threads */
731 int (*start)(struct mddev *mddev);
afa0f557 732 void (*free)(struct mddev *mddev, void *priv);
fd01b88c 733 void (*status)(struct seq_file *seq, struct mddev *mddev);
1da177e4 734 /* error_handler must set ->faulty and clear ->in_sync
f72ffdd6 735 * if appropriate, and should abort recovery if needed
1da177e4 736 */
fd01b88c
N
737 void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev);
738 int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev);
b8321b68 739 int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev);
fd01b88c 740 int (*spare_active) (struct mddev *mddev);
bc49694a
YK
741 sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr,
742 sector_t max_sector, int *skipped);
fd01b88c
N
743 int (*resize) (struct mddev *mddev, sector_t sectors);
744 sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks);
745 int (*check_reshape) (struct mddev *mddev);
746 int (*start_reshape) (struct mddev *mddev);
747 void (*finish_reshape) (struct mddev *mddev);
7564beda 748 void (*update_reshape_pos) (struct mddev *mddev);
5625ff8b 749 void (*prepare_suspend) (struct mddev *mddev);
b03e0ccb
N
750 /* quiesce suspends or resumes internal processing.
751 * 1 - stop new actions and wait for action io to complete
752 * 0 - return to normal behaviour
36fa3063 753 */
b03e0ccb 754 void (*quiesce) (struct mddev *mddev, int quiesce);
245f46c2
N
755 /* takeover is used to transition an array from one
756 * personality to another. The new personality must be able
757 * to handle the data in the current layout.
758 * e.g. 2drive raid1 -> 2drive raid5
759 * ndrive raid5 -> degraded n+1drive raid6 with special layout
760 * If the takeover succeeds, a new 'private' structure is returned.
761 * This needs to be installed and then ->run used to activate the
762 * array.
763 */
fd01b88c 764 void *(*takeover) (struct mddev *mddev);
ba903a3e
AP
765 /* Changes the consistency policy of an active array. */
766 int (*change_consistency_policy)(struct mddev *mddev, const char *buf);
0c984a28
YK
767 /* convert io ranges from array to bitmap */
768 void (*bitmap_sector)(struct mddev *mddev, sector_t *offset,
769 unsigned long *sectors);
1da177e4
LT
770};
771
007583c9
N
772struct md_sysfs_entry {
773 struct attribute attr;
fd01b88c
N
774 ssize_t (*show)(struct mddev *, char *);
775 ssize_t (*store)(struct mddev *, const char *, size_t);
007583c9 776};
c32dc040 777extern const struct attribute_group md_bitmap_group;
007583c9 778
324a56e1 779static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name)
00bcb4ac
N
780{
781 if (sd)
388975cc 782 return sysfs_get_dirent(sd, name);
00bcb4ac
N
783 return sd;
784}
324a56e1 785static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
00bcb4ac
N
786{
787 if (sd)
788 sysfs_notify_dirent(sd);
789}
790
fd01b88c 791static inline char * mdname (struct mddev * mddev)
1da177e4
LT
792{
793 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
794}
795
fd01b88c 796static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev)
36fad858
NK
797{
798 char nm[20];
9b15603d
SL
799 if (!test_bit(Replacement, &rdev->flags) &&
800 !test_bit(Journal, &rdev->flags) &&
801 mddev->kobj.sd) {
2d78f8c4
N
802 sprintf(nm, "rd%d", rdev->raid_disk);
803 return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
804 } else
805 return 0;
36fad858
NK
806}
807
fd01b88c 808static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev)
36fad858
NK
809{
810 char nm[20];
9b15603d
SL
811 if (!test_bit(Replacement, &rdev->flags) &&
812 !test_bit(Journal, &rdev->flags) &&
813 mddev->kobj.sd) {
2d78f8c4
N
814 sprintf(nm, "rd%d", rdev->raid_disk);
815 sysfs_remove_link(&mddev->kobj, nm);
816 }
36fad858
NK
817}
818
1da177e4
LT
819/*
820 * iterates through some rdev ringlist. It's safe to remove the
821 * current 'rdev'. Dont touch 'tmp' though.
822 */
159ec1fc
CR
823#define rdev_for_each_list(rdev, tmp, head) \
824 list_for_each_entry_safe(rdev, tmp, head, same_set)
825
1da177e4
LT
826/*
827 * iterates through the 'same array disks' ringlist
828 */
dafb20fa
N
829#define rdev_for_each(rdev, mddev) \
830 list_for_each_entry(rdev, &((mddev)->disks), same_set)
831
832#define rdev_for_each_safe(rdev, tmp, mddev) \
159ec1fc 833 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
1da177e4 834
4b80991c
N
835#define rdev_for_each_rcu(rdev, mddev) \
836 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
837
2b8bf345 838struct md_thread {
4ed8731d 839 void (*run) (struct md_thread *thread);
fd01b88c 840 struct mddev *mddev;
1da177e4 841 wait_queue_head_t wqueue;
f72ffdd6 842 unsigned long flags;
1da177e4 843 struct task_struct *tsk;
32a7627c 844 unsigned long timeout;
4ed8731d 845 void *private;
2b8bf345 846};
1da177e4 847
c687297b 848struct md_io_clone {
76fed014
XN
849 struct mddev *mddev;
850 struct bio *orig_bio;
851 unsigned long start_time;
cd5fc653
YK
852 sector_t offset;
853 unsigned long sectors;
76fed014 854 struct bio bio_clone;
10764815
GJ
855};
856
1da177e4
LT
857#define THREAD_WAKEUP 0
858
1345b1d8
N
859static inline void safe_put_page(struct page *p)
860{
861 if (p) put_page(p);
862}
863
d3beb7c9
YK
864int register_md_submodule(struct md_submodule_head *msh);
865void unregister_md_submodule(struct md_submodule_head *msh);
866
2b8bf345 867extern struct md_thread *md_register_thread(
4ed8731d 868 void (*run)(struct md_thread *thread),
2b8bf345
N
869 struct mddev *mddev,
870 const char *name);
7eb8ff02 871extern void md_unregister_thread(struct mddev *mddev, struct md_thread __rcu **threadp);
44693154 872extern void md_wakeup_thread(struct md_thread __rcu *thread);
fd01b88c 873extern void md_check_recovery(struct mddev *mddev);
d0a18034 874extern void md_reap_sync_thread(struct mddev *mddev);
e792a4c2
YK
875extern enum sync_action md_sync_action(struct mddev *mddev);
876extern enum sync_action md_sync_action_by_name(const char *page);
877extern const char *md_sync_action_name(enum sync_action action);
03e792ea 878extern void md_write_start(struct mddev *mddev, struct bio *bi);
49728050 879extern void md_write_inc(struct mddev *mddev, struct bio *bi);
fd01b88c
N
880extern void md_write_end(struct mddev *mddev);
881extern void md_done_sync(struct mddev *mddev, int blocks, int ok);
882extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
c6563a8c 883extern void md_finish_reshape(struct mddev *mddev);
cf78408f
XN
884void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
885 struct bio *bio, sector_t start, sector_t size);
10764815 886void md_account_bio(struct mddev *mddev, struct bio **bio);
41425f96 887void md_free_cloned_bio(struct bio *bio);
fd01b88c 888
775d7831 889extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio);
fd01b88c 890extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
92022950 891 sector_t sector, int size, struct page *page);
46533ff7 892extern int md_super_wait(struct mddev *mddev);
f72ffdd6 893extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
4ce4c73f 894 struct page *page, blk_opf_t opf, bool metadata_op);
4ed8731d 895extern void md_do_sync(struct md_thread *thread);
54679486 896extern void md_new_event(void);
2214c260 897extern void md_allow_write(struct mddev *mddev);
fd01b88c
N
898extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev);
899extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors);
900extern int md_check_no_bitmap(struct mddev *mddev);
901extern int md_integrity_register(struct mddev *mddev);
72e02075 902extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
63fe0817 903
d58eff83
YK
904extern int mddev_init(struct mddev *mddev);
905extern void mddev_destroy(struct mddev *mddev);
573d5abf 906void md_init_stacking_limits(struct queue_limits *lim);
34cb92c0
CH
907struct mddev *md_alloc(dev_t dev, char *name);
908void mddev_put(struct mddev *mddev);
fd01b88c 909extern int md_run(struct mddev *mddev);
d5d885fd 910extern int md_start(struct mddev *mddev);
fd01b88c
N
911extern void md_stop(struct mddev *mddev);
912extern void md_stop_writes(struct mddev *mddev);
3cb03002 913extern int md_rdev_init(struct md_rdev *rdev);
545c8795 914extern void md_rdev_clear(struct md_rdev *rdev);
63fe0817 915
41425f96 916extern bool md_handle_request(struct mddev *mddev, struct bio *bio);
2b16a525 917extern int mddev_suspend(struct mddev *mddev, bool interruptible);
fd01b88c 918extern void mddev_resume(struct mddev *mddev);
7a2347e2
YK
919extern void md_idle_sync_thread(struct mddev *mddev);
920extern void md_frozen_sync_thread(struct mddev *mddev);
921extern void md_unfrozen_sync_thread(struct mddev *mddev);
9cbb1750 922
1aee41f6 923extern void md_update_sb(struct mddev *mddev, int force);
b4128c00
YK
924extern void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev);
925extern void mddev_destroy_serial_pool(struct mddev *mddev,
926 struct md_rdev *rdev);
57d051dc 927struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
1532d9e8 928struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
dfe15ac1 929
c31fea2f 930static inline bool is_rdev_broken(struct md_rdev *rdev)
62f7b198 931{
c31fea2f 932 return !disk_live(rdev->bdev->bd_disk);
62f7b198
GP
933}
934
dfe15ac1
HR
935static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev)
936{
937 int faulty = test_bit(Faulty, &rdev->flags);
938 if (atomic_dec_and_test(&rdev->nr_pending) && faulty) {
939 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
940 md_wakeup_thread(mddev->thread);
941 }
942}
943
c4ce867f
GR
944static inline int mddev_is_clustered(struct mddev *mddev)
945{
946 return mddev->cluster_info && mddev->bitmap_info.nodes > 1;
947}
394ed8e4
SL
948
949/* clear unsupported mddev_flags */
950static inline void mddev_clear_unsupported_flags(struct mddev *mddev,
951 unsigned long unsupported_flags)
952{
953 mddev->flags &= ~unsupported_flags;
954}
26483819 955
3deff1a7
CH
956static inline void mddev_check_write_zeroes(struct mddev *mddev, struct bio *bio)
957{
958 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
309dca30 959 !bio->bi_bdev->bd_disk->queue->limits.max_write_zeroes_sectors)
396799eb 960 mddev->gendisk->queue->limits.max_write_zeroes_sectors = 0;
3deff1a7 961}
d82fa81c 962
f45461e2
YK
963static inline int mddev_suspend_and_lock(struct mddev *mddev)
964{
965 int ret;
966
2b16a525 967 ret = mddev_suspend(mddev, true);
f45461e2
YK
968 if (ret)
969 return ret;
970
971 ret = mddev_lock(mddev);
972 if (ret)
2b16a525 973 mddev_resume(mddev);
f45461e2
YK
974
975 return ret;
976}
977
978static inline void mddev_suspend_and_lock_nointr(struct mddev *mddev)
979{
2b16a525 980 mddev_suspend(mddev, false);
f45461e2
YK
981 mutex_lock(&mddev->reconfig_mutex);
982}
983
984static inline void mddev_unlock_and_resume(struct mddev *mddev)
985{
986 mddev_unlock(mddev);
2b16a525 987 mddev_resume(mddev);
f45461e2
YK
988}
989
7e0adbfc
CH
990struct mdu_array_info_s;
991struct mdu_disk_info_s;
992
1a6a0506 993extern int mdp_major;
a022325a 994extern struct workqueue_struct *md_bitmap_wq;
d82fa81c 995void md_autostart_arrays(int part);
7e0adbfc
CH
996int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info);
997int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info);
998int do_md_run(struct mddev *mddev);
c6e56cf6
CH
999#define MDDEV_STACK_INTEGRITY (1u << 0)
1000int mddev_stack_rdev_limits(struct mddev *mddev, struct queue_limits *lim,
1001 unsigned int flags);
e305fce1
CH
1002int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev);
1003void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes);
7e0adbfc
CH
1004
1005extern const struct block_device_operations md_fops;
d82fa81c 1006
176df894
CH
1007/*
1008 * MD devices can be used undeneath by DM, in which case ->gendisk is NULL.
1009 */
1010static inline bool mddev_is_dm(struct mddev *mddev)
1011{
1012 return !mddev->gendisk;
1013}
1014
c396b90e
CH
1015static inline void mddev_trace_remap(struct mddev *mddev, struct bio *bio,
1016 sector_t sector)
1017{
176df894 1018 if (!mddev_is_dm(mddev))
c396b90e
CH
1019 trace_block_bio_remap(bio, disk_devt(mddev->gendisk), sector);
1020}
1021
4abfce19
YK
1022static inline bool rdev_blocked(struct md_rdev *rdev)
1023{
1024 /*
1025 * Blocked will be set by error handler and cleared by daemon after
1026 * updating superblock, meanwhile write IO should be blocked to prevent
1027 * reading old data after power failure.
1028 */
1029 if (test_bit(Blocked, &rdev->flags))
1030 return true;
1031
1032 /*
1033 * Faulty device should not be accessed anymore, there is no need to
1034 * wait for bad block to be acknowledged.
1035 */
1036 if (test_bit(Faulty, &rdev->flags))
1037 return false;
1038
1039 /* rdev is blocked by badblocks. */
1040 if (test_bit(BlockedBadBlocks, &rdev->flags))
1041 return true;
1042
1043 return false;
1044}
1045
28be4fd3
CH
1046#define mddev_add_trace_msg(mddev, fmt, args...) \
1047do { \
176df894 1048 if (!mddev_is_dm(mddev)) \
396799eb 1049 blk_add_trace_msg((mddev)->gendisk->queue, fmt, ##args); \
28be4fd3
CH
1050} while (0)
1051
63fe0817 1052#endif /* _MD_MD_H */