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