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 enum md_submodule_type {
27 MD_PERSONALITY = 0,
28 MD_CLUSTER,
29 MD_BITMAP, /* TODO */
30 };
31
32 enum 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
45 struct 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
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)
62
63 /* Status of sync thread. */
64 enum 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
118 /*
119 * The struct embedded in rdev is used to serialize IO.
120 */
121 struct 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
127 /*
128 * MD's 'extended' device
129 */
130 struct md_rdev {
131 struct list_head same_set; /* RAID devices within the same set */
132
133 sector_t sectors; /* Device size (in 512bytes sectors) */
134 struct mddev *mddev; /* RAID array if running */
135 int last_events; /* IO event timestamp */
136
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;
143 struct block_device *bdev; /* block device handle */
144 struct file *bdev_file; /* Handle from open for bdev */
145
146 struct page *sb_page, *bb_page;
147 int sb_loaded;
148 __u64 sb_events;
149 sector_t data_offset; /* start of data in array */
150 sector_t new_data_offset;/* only relevant while reshaping */
151 sector_t sb_start; /* offset of the super block (in 512byte sectors) */
152 int sb_size; /* bytes in the superblock */
153 int preferred_minor; /* autorun support */
154
155 struct kobject kobj;
156
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 */
167
168 unsigned long flags; /* bit set of 'enum flag_bits' bits. */
169 wait_queue_head_t blocked_wait;
170
171 int desc_nr; /* descriptor index in the superblock */
172 int raid_disk; /* role of device in array */
173 int new_raid_disk; /* role that the device will have in
174 * the array after a level-change completes.
175 */
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 */
180 union {
181 sector_t recovery_offset;/* If this device has been partially
182 * recovered, this is where we were
183 * up to.
184 */
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 };
190
191 atomic_t nr_pending; /* number of pending requests.
192 * only maintained for arrays that
193 * support hot removal
194 */
195 atomic_t read_errors; /* number of consecutive read errors that
196 * we have tried to ignore.
197 */
198 time64_t last_read_error; /* monotonic time since our
199 * last read error
200 */
201 atomic_t corrected_errors; /* number of corrected read errors,
202 * for reporting to userspace and storing
203 * in superblock.
204 */
205
206 struct serial_in_rdev *serial; /* used for raid1 io serialization */
207
208 struct kernfs_node *sysfs_state; /* handle for 'state'
209 * sysfs entry */
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;
214 struct badblocks badblocks;
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;
222 };
223 enum flag_bits {
224 Faulty, /* device is known to have a fault */
225 In_sync, /* device is in_sync with rest of array */
226 Bitmap_sync, /* ..actually, not quite In_sync. Need a
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.
230 */
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 */
265 Candidate, /* For clustered environments only:
266 * This device is seen locally but not
267 * by the whole cluster
268 */
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 */
274 ClusterRemove,
275 ExternalBbl, /* External metadata provides bad
276 * block management for a disk
277 */
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 */
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 */
288 CollisionCheck, /*
289 * check if there is collision between raid1
290 * serial bios.
291 */
292 Nonrot, /* non-rotational device (SSD) */
293 };
294
is_badblock(struct md_rdev * rdev,sector_t s,sector_t sectors,sector_t * first_bad,sector_t * bad_sectors)295 static inline int is_badblock(struct md_rdev *rdev, sector_t s, sector_t sectors,
296 sector_t *first_bad, sector_t *bad_sectors)
297 {
298 if (unlikely(rdev->badblocks.count)) {
299 int rv = badblocks_check(&rdev->badblocks, rdev->data_offset + s,
300 sectors,
301 first_bad, bad_sectors);
302 if (rv)
303 *first_bad -= rdev->data_offset;
304 return rv;
305 }
306 return 0;
307 }
308
rdev_has_badblock(struct md_rdev * rdev,sector_t s,int sectors)309 static inline int rdev_has_badblock(struct md_rdev *rdev, sector_t s,
310 int sectors)
311 {
312 sector_t first_bad;
313 sector_t bad_sectors;
314
315 return is_badblock(rdev, s, sectors, &first_bad, &bad_sectors);
316 }
317
318 extern bool rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
319 int is_new);
320 extern void rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
321 int is_new);
322 struct md_cluster_info;
323 struct md_cluster_operations;
324
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.
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.
341 * @MD_DELETED: This device is being deleted
342 *
343 * change UNSUPPORTED_MDDEV_FLAGS for each array type if new flag is added
344 */
345 enum mddev_flags {
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,
354 MD_NOT_READY,
355 MD_BROKEN,
356 MD_DELETED,
357 };
358
359 enum 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
366 #define NR_SERIAL_INFOS 8
367 /* record current range of serialize IOs */
368 struct serial_info {
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 */
373 };
374
375 /*
376 * mddev->curr_resync stores the current sector of the resync but
377 * also has some overloaded values.
378 */
379 enum {
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
390 struct mddev {
391 void *private;
392 struct md_personality *pers;
393 dev_t unit;
394 int md_minor;
395 struct list_head disks;
396 unsigned long flags;
397 unsigned long sb_flags;
398
399 int suspended;
400 struct mutex suspend_mutex;
401 struct percpu_ref active_io;
402 int ro;
403 int sysfs_active; /* set when sysfs deletes
404 * are happening, so run/
405 * takeover/stop are not safe
406 */
407 struct gendisk *gendisk;
408
409 struct kobject kobj;
410 int hold_active;
411 #define UNTIL_IOCTL 1
412 #define UNTIL_STOP 2
413
414 /* Superblock information */
415 int major_version,
416 minor_version,
417 patch_version;
418 int persistent;
419 int external; /* metadata is
420 * managed externally */
421 char metadata_type[17]; /* externally set*/
422 int chunk_sectors;
423 time64_t ctime, utime;
424 int level, layout;
425 char clevel[16];
426 int raid_disks;
427 int max_disks;
428 sector_t dev_sectors; /* used size of
429 * component devices */
430 sector_t array_sectors; /* exported array size */
431 int external_size; /* size managed
432 * externally */
433 __u64 events;
434 /* If the last 'event' was simply a clean->dirty transition, and
435 * we didn't write it to the spares, then it is safe and simple
436 * to just decrement the event count on a dirty->clean transition.
437 * So we record that possibility here.
438 */
439 int can_decrease_events;
440
441 char uuid[16];
442
443 /* If the array is being reshaped, we need to record the
444 * new shape and an indication of where we are up to.
445 * This is written to the superblock.
446 * If reshape_position is MaxSector, then no reshape is happening (yet).
447 */
448 sector_t reshape_position;
449 int delta_disks, new_level, new_layout;
450 int new_chunk_sectors;
451 int reshape_backwards;
452
453 struct md_thread __rcu *thread; /* management thread */
454 struct md_thread __rcu *sync_thread; /* doing resync or reconstruct */
455
456 /*
457 * Set when a sync operation is started. It holds this value even
458 * when the sync thread is "frozen" (interrupted) or "idle" (stopped
459 * or finished). It is overwritten when a new sync operation is begun.
460 */
461 enum sync_action last_sync_action;
462 sector_t curr_resync; /* last block scheduled */
463 /* As resync requests can complete out of order, we cannot easily track
464 * how much resync has been completed. So we occasionally pause until
465 * everything completes, then set curr_resync_completed to curr_resync.
466 * As such it may be well behind the real resync mark, but it is a value
467 * we are certain of.
468 */
469 sector_t curr_resync_completed;
470 unsigned long resync_mark; /* a recent timestamp */
471 sector_t resync_mark_cnt;/* blocks written at resync_mark */
472 sector_t curr_mark_cnt; /* blocks scheduled now */
473
474 sector_t resync_max_sectors; /* may be set by personality */
475
476 atomic64_t resync_mismatches; /* count of sectors where
477 * parity/replica mismatch found
478 */
479
480 /* allow user-space to request suspension of IO to regions of the array */
481 sector_t suspend_lo;
482 sector_t suspend_hi;
483 /* if zero, use the system-wide default */
484 int sync_speed_min;
485 int sync_speed_max;
486
487 /* resync even though the same disks are shared among md-devices */
488 int parallel_resync;
489
490 int ok_start_degraded;
491
492 unsigned long recovery;
493 /* If a RAID personality determines that recovery (of a particular
494 * device) will fail due to a read error on the source device, it
495 * takes a copy of this number and does not attempt recovery again
496 * until this number changes.
497 */
498 int recovery_disabled;
499
500 int in_sync; /* know to not need resync */
501 /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
502 * that we are never stopping an array while it is open.
503 * 'reconfig_mutex' protects all other reconfiguration.
504 * These locks are separate due to conflicting interactions
505 * with disk->open_mutex.
506 * Lock ordering is:
507 * reconfig_mutex -> disk->open_mutex
508 * disk->open_mutex -> open_mutex: e.g. __blkdev_get -> md_open
509 */
510 struct mutex open_mutex;
511 struct mutex reconfig_mutex;
512 atomic_t active; /* general refcount */
513 atomic_t openers; /* number of active opens */
514
515 int changed; /* True if we might need to
516 * reread partition info */
517 int degraded; /* whether md should consider
518 * adding a spare
519 */
520
521 atomic_t recovery_active; /* blocks scheduled, but not written */
522 wait_queue_head_t recovery_wait;
523 sector_t recovery_cp;
524 sector_t resync_min; /* user requested sync
525 * starts here */
526 sector_t resync_max; /* resync should pause
527 * when it gets here */
528
529 struct kernfs_node *sysfs_state; /* handle for 'array_state'
530 * file in sysfs.
531 */
532 struct kernfs_node *sysfs_action; /* handle for 'sync_action' */
533 struct kernfs_node *sysfs_completed; /*handle for 'sync_completed' */
534 struct kernfs_node *sysfs_degraded; /*handle for 'degraded' */
535 struct kernfs_node *sysfs_level; /*handle for 'level' */
536
537 /* used for delayed sysfs removal */
538 struct work_struct del_work;
539 /* used for register new sync thread */
540 struct work_struct sync_work;
541
542 /* "lock" protects:
543 * flush_bio transition from NULL to !NULL
544 * rdev superblocks, events
545 * clearing MD_CHANGE_*
546 * in_sync - and related safemode and MD_CHANGE changes
547 * pers (also protected by reconfig_mutex and pending IO).
548 * clearing ->bitmap
549 * clearing ->bitmap_info.file
550 * changing ->resync_{min,max}
551 * setting MD_RECOVERY_RUNNING (which interacts with resync_{min,max})
552 */
553 spinlock_t lock;
554 wait_queue_head_t sb_wait; /* for waiting on superblock updates */
555 atomic_t pending_writes; /* number of active superblock writes */
556
557 unsigned int safemode; /* if set, update "clean" superblock
558 * when no writes pending.
559 */
560 unsigned int safemode_delay;
561 struct timer_list safemode_timer;
562 struct percpu_ref writes_pending;
563 int sync_checkers; /* # of threads checking writes_pending */
564
565 void *bitmap; /* the bitmap for the device */
566 struct bitmap_operations *bitmap_ops;
567 struct {
568 struct file *file; /* the bitmap file */
569 loff_t offset; /* offset from superblock of
570 * start of bitmap. May be
571 * negative, but not '0'
572 * For external metadata, offset
573 * from start of device.
574 */
575 unsigned long space; /* space available at this offset */
576 loff_t default_offset; /* this is the offset to use when
577 * hot-adding a bitmap. It should
578 * eventually be settable by sysfs.
579 */
580 unsigned long default_space; /* space available at
581 * default offset */
582 struct mutex mutex;
583 unsigned long chunksize;
584 unsigned long daemon_sleep; /* how many jiffies between updates? */
585 unsigned long max_write_behind; /* write-behind mode */
586 int external;
587 int nodes; /* Maximum number of nodes in the cluster */
588 char cluster_name[64]; /* Name of the cluster */
589 } bitmap_info;
590
591 atomic_t max_corr_read_errors; /* max read retries */
592 struct list_head all_mddevs;
593
594 const struct attribute_group *to_remove;
595
596 struct bio_set bio_set;
597 struct bio_set sync_set; /* for sync operations like
598 * metadata and bitmap writes
599 */
600 struct bio_set io_clone_set;
601
602 struct work_struct event_work; /* used by dm to report failure event */
603 mempool_t *serial_info_pool;
604 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
605 struct md_cluster_info *cluster_info;
606 struct md_cluster_operations *cluster_ops;
607 unsigned int good_device_nr; /* good device num within cluster raid */
608 unsigned int noio_flag; /* for memalloc scope API */
609
610 /*
611 * Temporarily store rdev that will be finally removed when
612 * reconfig_mutex is unlocked, protected by reconfig_mutex.
613 */
614 struct list_head deleting;
615
616 /* The sequence number for sync thread */
617 atomic_t sync_seq;
618
619 bool has_superblocks:1;
620 bool fail_last_dev:1;
621 bool serialize_policy:1;
622 };
623
624 enum recovery_flags {
625 /* flags for sync thread running status */
626
627 /*
628 * set when one of sync action is set and new sync thread need to be
629 * registered, or just add/remove spares from conf.
630 */
631 MD_RECOVERY_NEEDED,
632 /* sync thread is running, or about to be started */
633 MD_RECOVERY_RUNNING,
634 /* sync thread needs to be aborted for some reason */
635 MD_RECOVERY_INTR,
636 /* sync thread is done and is waiting to be unregistered */
637 MD_RECOVERY_DONE,
638 /* running sync thread must abort immediately, and not restart */
639 MD_RECOVERY_FROZEN,
640 /* waiting for pers->start() to finish */
641 MD_RECOVERY_WAIT,
642 /* interrupted because io-error */
643 MD_RECOVERY_ERROR,
644
645 /* flags determines sync action, see details in enum sync_action */
646
647 /* if just this flag is set, action is resync. */
648 MD_RECOVERY_SYNC,
649 /*
650 * paired with MD_RECOVERY_SYNC, if MD_RECOVERY_CHECK is not set,
651 * action is repair, means user requested resync.
652 */
653 MD_RECOVERY_REQUESTED,
654 /*
655 * paired with MD_RECOVERY_SYNC and MD_RECOVERY_REQUESTED, action is
656 * check.
657 */
658 MD_RECOVERY_CHECK,
659 /* recovery, or need to try it */
660 MD_RECOVERY_RECOVER,
661 /* reshape */
662 MD_RECOVERY_RESHAPE,
663 /* remote node is running resync thread */
664 MD_RESYNCING_REMOTE,
665 };
666
667 enum md_ro_state {
668 MD_RDWR,
669 MD_RDONLY,
670 MD_AUTO_READ,
671 MD_MAX_STATE
672 };
673
md_is_rdwr(struct mddev * mddev)674 static inline bool md_is_rdwr(struct mddev *mddev)
675 {
676 return (mddev->ro == MD_RDWR);
677 }
678
reshape_interrupted(struct mddev * mddev)679 static inline bool reshape_interrupted(struct mddev *mddev)
680 {
681 /* reshape never start */
682 if (mddev->reshape_position == MaxSector)
683 return false;
684
685 /* interrupted */
686 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
687 return true;
688
689 /* running reshape will be interrupted soon. */
690 if (test_bit(MD_RECOVERY_WAIT, &mddev->recovery) ||
691 test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
692 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
693 return true;
694
695 return false;
696 }
697
mddev_lock(struct mddev * mddev)698 static inline int __must_check mddev_lock(struct mddev *mddev)
699 {
700 return mutex_lock_interruptible(&mddev->reconfig_mutex);
701 }
702
703 /* Sometimes we need to take the lock in a situation where
704 * failure due to interrupts is not acceptable.
705 */
mddev_lock_nointr(struct mddev * mddev)706 static inline void mddev_lock_nointr(struct mddev *mddev)
707 {
708 mutex_lock(&mddev->reconfig_mutex);
709 }
710
mddev_trylock(struct mddev * mddev)711 static inline int mddev_trylock(struct mddev *mddev)
712 {
713 return mutex_trylock(&mddev->reconfig_mutex);
714 }
715 extern void mddev_unlock(struct mddev *mddev);
716
md_sync_acct(struct block_device * bdev,unsigned long nr_sectors)717 static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
718 {
719 if (blk_queue_io_stat(bdev->bd_disk->queue))
720 atomic_add(nr_sectors, &bdev->bd_disk->sync_io);
721 }
722
md_sync_acct_bio(struct bio * bio,unsigned long nr_sectors)723 static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
724 {
725 md_sync_acct(bio->bi_bdev, nr_sectors);
726 }
727
728 struct md_personality
729 {
730 struct md_submodule_head head;
731
732 bool __must_check (*make_request)(struct mddev *mddev, struct bio *bio);
733 /*
734 * start up works that do NOT require md_thread. tasks that
735 * requires md_thread should go into start()
736 */
737 int (*run)(struct mddev *mddev);
738 /* start up works that require md threads */
739 int (*start)(struct mddev *mddev);
740 void (*free)(struct mddev *mddev, void *priv);
741 void (*status)(struct seq_file *seq, struct mddev *mddev);
742 /* error_handler must set ->faulty and clear ->in_sync
743 * if appropriate, and should abort recovery if needed
744 */
745 void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev);
746 int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev);
747 int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev);
748 int (*spare_active) (struct mddev *mddev);
749 sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr,
750 sector_t max_sector, int *skipped);
751 int (*resize) (struct mddev *mddev, sector_t sectors);
752 sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks);
753 int (*check_reshape) (struct mddev *mddev);
754 int (*start_reshape) (struct mddev *mddev);
755 void (*finish_reshape) (struct mddev *mddev);
756 void (*update_reshape_pos) (struct mddev *mddev);
757 void (*prepare_suspend) (struct mddev *mddev);
758 /* quiesce suspends or resumes internal processing.
759 * 1 - stop new actions and wait for action io to complete
760 * 0 - return to normal behaviour
761 */
762 void (*quiesce) (struct mddev *mddev, int quiesce);
763 /* takeover is used to transition an array from one
764 * personality to another. The new personality must be able
765 * to handle the data in the current layout.
766 * e.g. 2drive raid1 -> 2drive raid5
767 * ndrive raid5 -> degraded n+1drive raid6 with special layout
768 * If the takeover succeeds, a new 'private' structure is returned.
769 * This needs to be installed and then ->run used to activate the
770 * array.
771 */
772 void *(*takeover) (struct mddev *mddev);
773 /* Changes the consistency policy of an active array. */
774 int (*change_consistency_policy)(struct mddev *mddev, const char *buf);
775 /* convert io ranges from array to bitmap */
776 void (*bitmap_sector)(struct mddev *mddev, sector_t *offset,
777 unsigned long *sectors);
778 };
779
780 struct md_sysfs_entry {
781 struct attribute attr;
782 ssize_t (*show)(struct mddev *, char *);
783 ssize_t (*store)(struct mddev *, const char *, size_t);
784 };
785 extern const struct attribute_group md_bitmap_group;
786
sysfs_get_dirent_safe(struct kernfs_node * sd,char * name)787 static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name)
788 {
789 if (sd)
790 return sysfs_get_dirent(sd, name);
791 return sd;
792 }
sysfs_notify_dirent_safe(struct kernfs_node * sd)793 static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
794 {
795 if (sd)
796 sysfs_notify_dirent(sd);
797 }
798
mdname(struct mddev * mddev)799 static inline char * mdname (struct mddev * mddev)
800 {
801 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
802 }
803
sysfs_link_rdev(struct mddev * mddev,struct md_rdev * rdev)804 static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev)
805 {
806 char nm[20];
807 if (!test_bit(Replacement, &rdev->flags) &&
808 !test_bit(Journal, &rdev->flags) &&
809 mddev->kobj.sd) {
810 sprintf(nm, "rd%d", rdev->raid_disk);
811 return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
812 } else
813 return 0;
814 }
815
sysfs_unlink_rdev(struct mddev * mddev,struct md_rdev * rdev)816 static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev)
817 {
818 char nm[20];
819 if (!test_bit(Replacement, &rdev->flags) &&
820 !test_bit(Journal, &rdev->flags) &&
821 mddev->kobj.sd) {
822 sprintf(nm, "rd%d", rdev->raid_disk);
823 sysfs_remove_link(&mddev->kobj, nm);
824 }
825 }
826
827 /*
828 * iterates through some rdev ringlist. It's safe to remove the
829 * current 'rdev'. Dont touch 'tmp' though.
830 */
831 #define rdev_for_each_list(rdev, tmp, head) \
832 list_for_each_entry_safe(rdev, tmp, head, same_set)
833
834 /*
835 * iterates through the 'same array disks' ringlist
836 */
837 #define rdev_for_each(rdev, mddev) \
838 list_for_each_entry(rdev, &((mddev)->disks), same_set)
839
840 #define rdev_for_each_safe(rdev, tmp, mddev) \
841 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
842
843 #define rdev_for_each_rcu(rdev, mddev) \
844 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
845
846 struct md_thread {
847 void (*run) (struct md_thread *thread);
848 struct mddev *mddev;
849 wait_queue_head_t wqueue;
850 unsigned long flags;
851 struct task_struct *tsk;
852 unsigned long timeout;
853 void *private;
854 };
855
856 struct md_io_clone {
857 struct mddev *mddev;
858 struct bio *orig_bio;
859 unsigned long start_time;
860 sector_t offset;
861 unsigned long sectors;
862 struct bio bio_clone;
863 };
864
865 #define THREAD_WAKEUP 0
866
safe_put_page(struct page * p)867 static inline void safe_put_page(struct page *p)
868 {
869 if (p) put_page(p);
870 }
871
872 int register_md_submodule(struct md_submodule_head *msh);
873 void unregister_md_submodule(struct md_submodule_head *msh);
874
875 extern struct md_thread *md_register_thread(
876 void (*run)(struct md_thread *thread),
877 struct mddev *mddev,
878 const char *name);
879 extern void md_unregister_thread(struct mddev *mddev, struct md_thread __rcu **threadp);
880 extern void md_wakeup_thread(struct md_thread __rcu *thread);
881 extern void md_check_recovery(struct mddev *mddev);
882 extern void md_reap_sync_thread(struct mddev *mddev);
883 extern enum sync_action md_sync_action(struct mddev *mddev);
884 extern enum sync_action md_sync_action_by_name(const char *page);
885 extern const char *md_sync_action_name(enum sync_action action);
886 extern void md_write_start(struct mddev *mddev, struct bio *bi);
887 extern void md_write_inc(struct mddev *mddev, struct bio *bi);
888 extern void md_write_end(struct mddev *mddev);
889 extern void md_done_sync(struct mddev *mddev, int blocks, int ok);
890 extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
891 extern void md_finish_reshape(struct mddev *mddev);
892 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
893 struct bio *bio, sector_t start, sector_t size);
894 void md_account_bio(struct mddev *mddev, struct bio **bio);
895 void md_free_cloned_bio(struct bio *bio);
896
897 extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio);
898 extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
899 sector_t sector, int size, struct page *page);
900 extern int md_super_wait(struct mddev *mddev);
901 extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
902 struct page *page, blk_opf_t opf, bool metadata_op);
903 extern void md_do_sync(struct md_thread *thread);
904 extern void md_new_event(void);
905 extern void md_allow_write(struct mddev *mddev);
906 extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev);
907 extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors);
908 extern int md_check_no_bitmap(struct mddev *mddev);
909 extern int md_integrity_register(struct mddev *mddev);
910 extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
911
912 extern int mddev_init(struct mddev *mddev);
913 extern void mddev_destroy(struct mddev *mddev);
914 void md_init_stacking_limits(struct queue_limits *lim);
915 struct mddev *md_alloc(dev_t dev, char *name);
916 void mddev_put(struct mddev *mddev);
917 extern int md_run(struct mddev *mddev);
918 extern int md_start(struct mddev *mddev);
919 extern void md_stop(struct mddev *mddev);
920 extern void md_stop_writes(struct mddev *mddev);
921 extern int md_rdev_init(struct md_rdev *rdev);
922 extern void md_rdev_clear(struct md_rdev *rdev);
923
924 extern bool md_handle_request(struct mddev *mddev, struct bio *bio);
925 extern int mddev_suspend(struct mddev *mddev, bool interruptible);
926 extern void mddev_resume(struct mddev *mddev);
927 extern void md_idle_sync_thread(struct mddev *mddev);
928 extern void md_frozen_sync_thread(struct mddev *mddev);
929 extern void md_unfrozen_sync_thread(struct mddev *mddev);
930
931 extern void md_update_sb(struct mddev *mddev, int force);
932 extern void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev);
933 extern void mddev_destroy_serial_pool(struct mddev *mddev,
934 struct md_rdev *rdev);
935 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
936 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
937
is_rdev_broken(struct md_rdev * rdev)938 static inline bool is_rdev_broken(struct md_rdev *rdev)
939 {
940 return !disk_live(rdev->bdev->bd_disk);
941 }
942
rdev_dec_pending(struct md_rdev * rdev,struct mddev * mddev)943 static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev)
944 {
945 int faulty = test_bit(Faulty, &rdev->flags);
946 if (atomic_dec_and_test(&rdev->nr_pending) && faulty) {
947 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
948 md_wakeup_thread(mddev->thread);
949 }
950 }
951
mddev_is_clustered(struct mddev * mddev)952 static inline int mddev_is_clustered(struct mddev *mddev)
953 {
954 return mddev->cluster_info && mddev->bitmap_info.nodes > 1;
955 }
956
957 /* clear unsupported mddev_flags */
mddev_clear_unsupported_flags(struct mddev * mddev,unsigned long unsupported_flags)958 static inline void mddev_clear_unsupported_flags(struct mddev *mddev,
959 unsigned long unsupported_flags)
960 {
961 mddev->flags &= ~unsupported_flags;
962 }
963
mddev_check_write_zeroes(struct mddev * mddev,struct bio * bio)964 static inline void mddev_check_write_zeroes(struct mddev *mddev, struct bio *bio)
965 {
966 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
967 !bio->bi_bdev->bd_disk->queue->limits.max_write_zeroes_sectors)
968 mddev->gendisk->queue->limits.max_write_zeroes_sectors = 0;
969 }
970
mddev_suspend_and_lock(struct mddev * mddev)971 static inline int mddev_suspend_and_lock(struct mddev *mddev)
972 {
973 int ret;
974
975 ret = mddev_suspend(mddev, true);
976 if (ret)
977 return ret;
978
979 ret = mddev_lock(mddev);
980 if (ret)
981 mddev_resume(mddev);
982
983 return ret;
984 }
985
mddev_suspend_and_lock_nointr(struct mddev * mddev)986 static inline void mddev_suspend_and_lock_nointr(struct mddev *mddev)
987 {
988 mddev_suspend(mddev, false);
989 mutex_lock(&mddev->reconfig_mutex);
990 }
991
mddev_unlock_and_resume(struct mddev * mddev)992 static inline void mddev_unlock_and_resume(struct mddev *mddev)
993 {
994 mddev_unlock(mddev);
995 mddev_resume(mddev);
996 }
997
998 struct mdu_array_info_s;
999 struct mdu_disk_info_s;
1000
1001 extern int mdp_major;
1002 extern struct workqueue_struct *md_bitmap_wq;
1003 void md_autostart_arrays(int part);
1004 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info);
1005 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info);
1006 int do_md_run(struct mddev *mddev);
1007 #define MDDEV_STACK_INTEGRITY (1u << 0)
1008 int mddev_stack_rdev_limits(struct mddev *mddev, struct queue_limits *lim,
1009 unsigned int flags);
1010 int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev);
1011 void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes);
1012
1013 extern const struct block_device_operations md_fops;
1014
1015 /*
1016 * MD devices can be used undeneath by DM, in which case ->gendisk is NULL.
1017 */
mddev_is_dm(struct mddev * mddev)1018 static inline bool mddev_is_dm(struct mddev *mddev)
1019 {
1020 return !mddev->gendisk;
1021 }
1022
mddev_trace_remap(struct mddev * mddev,struct bio * bio,sector_t sector)1023 static inline void mddev_trace_remap(struct mddev *mddev, struct bio *bio,
1024 sector_t sector)
1025 {
1026 if (!mddev_is_dm(mddev))
1027 trace_block_bio_remap(bio, disk_devt(mddev->gendisk), sector);
1028 }
1029
rdev_blocked(struct md_rdev * rdev)1030 static inline bool rdev_blocked(struct md_rdev *rdev)
1031 {
1032 /*
1033 * Blocked will be set by error handler and cleared by daemon after
1034 * updating superblock, meanwhile write IO should be blocked to prevent
1035 * reading old data after power failure.
1036 */
1037 if (test_bit(Blocked, &rdev->flags))
1038 return true;
1039
1040 /*
1041 * Faulty device should not be accessed anymore, there is no need to
1042 * wait for bad block to be acknowledged.
1043 */
1044 if (test_bit(Faulty, &rdev->flags))
1045 return false;
1046
1047 /* rdev is blocked by badblocks. */
1048 if (test_bit(BlockedBadBlocks, &rdev->flags))
1049 return true;
1050
1051 return false;
1052 }
1053
1054 #define mddev_add_trace_msg(mddev, fmt, args...) \
1055 do { \
1056 if (!mddev_is_dm(mddev)) \
1057 blk_add_trace_msg((mddev)->gendisk->queue, fmt, ##args); \
1058 } while (0)
1059
1060 #endif /* _MD_MD_H */
1061