xref: /linux/drivers/md/md.h (revision 26bb0d3f38a764b743a3ad5c8b6e5b5044d7ceb4)
1af1a8899SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
243b2e5d8SNeilBrown /*
37e841526SWang Sheng-Hui    md.h : kernel internal structure of the Linux MD driver
443b2e5d8SNeilBrown           Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman
543b2e5d8SNeilBrown 
643b2e5d8SNeilBrown */
743b2e5d8SNeilBrown 
863fe0817SChristoph Hellwig #ifndef _MD_MD_H
963fe0817SChristoph Hellwig #define _MD_MD_H
1043b2e5d8SNeilBrown 
1163fe0817SChristoph Hellwig #include <linux/blkdev.h>
1266114cadSTejun Heo #include <linux/backing-dev.h>
13fc974ee2SVishal Verma #include <linux/badblocks.h>
1463fe0817SChristoph Hellwig #include <linux/kobject.h>
1563fe0817SChristoph Hellwig #include <linux/list.h>
1663fe0817SChristoph Hellwig #include <linux/mm.h>
1763fe0817SChristoph Hellwig #include <linux/mutex.h>
1863fe0817SChristoph Hellwig #include <linux/timer.h>
1963fe0817SChristoph Hellwig #include <linux/wait.h>
2063fe0817SChristoph Hellwig #include <linux/workqueue.h>
21c396b90eSChristoph Hellwig #include <trace/events/block.h>
22edb39c9dSGoldwyn Rodrigues #include "md-cluster.h"
2343b2e5d8SNeilBrown 
2443b2e5d8SNeilBrown #define MaxSector (~(sector_t)0)
2543b2e5d8SNeilBrown 
2643b2e5d8SNeilBrown /*
2746533ff7SNeilBrown  * These flags should really be called "NO_RETRY" rather than
2846533ff7SNeilBrown  * "FAILFAST" because they don't make any promise about time lapse,
2946533ff7SNeilBrown  * only about the number of retries, which will be zero.
3046533ff7SNeilBrown  * REQ_FAILFAST_DRIVER is not included because
3146533ff7SNeilBrown  * Commit: 4a27446f3e39 ("[SCSI] modify scsi to handle new fail fast flags.")
3246533ff7SNeilBrown  * seems to suggest that the errors it avoids retrying should usually
3346533ff7SNeilBrown  * be retried.
3446533ff7SNeilBrown  */
3546533ff7SNeilBrown #define	MD_FAILFAST	(REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT)
3669b00b5bSGuoqing Jiang 
37a85aa09dSYu Kuai /* Status of sync thread. */
38a85aa09dSYu Kuai enum sync_action {
39a85aa09dSYu Kuai 	/*
40a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_SYNC, start when:
41a85aa09dSYu Kuai 	 * 1) after assemble, sync data from first rdev to other copies, this
42a85aa09dSYu Kuai 	 * must be done first before other sync actions and will only execute
43a85aa09dSYu Kuai 	 * once;
44a85aa09dSYu Kuai 	 * 2) resize the array(notice that this is not reshape), sync data for
45a85aa09dSYu Kuai 	 * the new range;
46a85aa09dSYu Kuai 	 */
47a85aa09dSYu Kuai 	ACTION_RESYNC,
48a85aa09dSYu Kuai 	/*
49a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_RECOVER, start when:
50a85aa09dSYu Kuai 	 * 1) for new replacement, sync data based on the replace rdev or
51a85aa09dSYu Kuai 	 * available copies from other rdev;
52a85aa09dSYu Kuai 	 * 2) for new member disk while the array is degraded, sync data from
53a85aa09dSYu Kuai 	 * other rdev;
54a85aa09dSYu Kuai 	 * 3) reassemble after power failure or re-add a hot removed rdev, sync
55a85aa09dSYu Kuai 	 * data from first rdev to other copies based on bitmap;
56a85aa09dSYu Kuai 	 */
57a85aa09dSYu Kuai 	ACTION_RECOVER,
58a85aa09dSYu Kuai 	/*
59a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED |
60a85aa09dSYu Kuai 	 * MD_RECOVERY_CHECK, start when user echo "check" to sysfs api
61a85aa09dSYu Kuai 	 * sync_action, used to check if data copies from differenct rdev are
62a85aa09dSYu Kuai 	 * the same. The number of mismatch sectors will be exported to user
63a85aa09dSYu Kuai 	 * by sysfs api mismatch_cnt;
64a85aa09dSYu Kuai 	 */
65a85aa09dSYu Kuai 	ACTION_CHECK,
66a85aa09dSYu Kuai 	/*
67a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_SYNC | MD_RECOVERY_REQUESTED, start when
68a85aa09dSYu Kuai 	 * user echo "repair" to sysfs api sync_action, usually paired with
69a85aa09dSYu Kuai 	 * ACTION_CHECK, used to force syncing data once user found that there
70a85aa09dSYu Kuai 	 * are inconsistent data,
71a85aa09dSYu Kuai 	 */
72a85aa09dSYu Kuai 	ACTION_REPAIR,
73a85aa09dSYu Kuai 	/*
74a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_RESHAPE, start when new member disk is added
75a85aa09dSYu Kuai 	 * to the conf, notice that this is different from spares or
76a85aa09dSYu Kuai 	 * replacement;
77a85aa09dSYu Kuai 	 */
78a85aa09dSYu Kuai 	ACTION_RESHAPE,
79a85aa09dSYu Kuai 	/*
80a85aa09dSYu Kuai 	 * Represent by MD_RECOVERY_FROZEN, can be set by sysfs api sync_action
81a85aa09dSYu Kuai 	 * or internal usage like setting the array read-only, will forbid above
82a85aa09dSYu Kuai 	 * actions.
83a85aa09dSYu Kuai 	 */
84a85aa09dSYu Kuai 	ACTION_FROZEN,
85a85aa09dSYu Kuai 	/*
86a85aa09dSYu Kuai 	 * All above actions don't match.
87a85aa09dSYu Kuai 	 */
88a85aa09dSYu Kuai 	ACTION_IDLE,
89a85aa09dSYu Kuai 	NR_SYNC_ACTIONS,
90a85aa09dSYu Kuai };
91a85aa09dSYu Kuai 
9269b00b5bSGuoqing Jiang /*
9369b00b5bSGuoqing Jiang  * The struct embedded in rdev is used to serialize IO.
9469b00b5bSGuoqing Jiang  */
9569b00b5bSGuoqing Jiang struct serial_in_rdev {
9669b00b5bSGuoqing Jiang 	struct rb_root_cached serial_rb;
9769b00b5bSGuoqing Jiang 	spinlock_t serial_lock;
9869b00b5bSGuoqing Jiang 	wait_queue_head_t serial_io_wait;
9969b00b5bSGuoqing Jiang };
10069b00b5bSGuoqing Jiang 
10146533ff7SNeilBrown /*
10243b2e5d8SNeilBrown  * MD's 'extended' device
10343b2e5d8SNeilBrown  */
1043cb03002SNeilBrown struct md_rdev {
10543b2e5d8SNeilBrown 	struct list_head same_set;	/* RAID devices within the same set */
10643b2e5d8SNeilBrown 
107dd8ac336SAndre Noll 	sector_t sectors;		/* Device size (in 512bytes sectors) */
108fd01b88cSNeilBrown 	struct mddev *mddev;		/* RAID array if running */
109504fbcffSLi Nan 	int last_events;		/* IO event timestamp */
11043b2e5d8SNeilBrown 
111a6ff7e08SJonathan Brassow 	/*
112a6ff7e08SJonathan Brassow 	 * If meta_bdev is non-NULL, it means that a separate device is
113a6ff7e08SJonathan Brassow 	 * being used to store the metadata (superblock/bitmap) which
114a6ff7e08SJonathan Brassow 	 * would otherwise be contained on the same device as the data (bdev).
115a6ff7e08SJonathan Brassow 	 */
116a6ff7e08SJonathan Brassow 	struct block_device *meta_bdev;
11743b2e5d8SNeilBrown 	struct block_device *bdev;	/* block device handle */
118a28d893eSChristian Brauner 	struct file *bdev_file;		/* Handle from open for bdev */
11943b2e5d8SNeilBrown 
1202699b672SNeilBrown 	struct page	*sb_page, *bb_page;
12143b2e5d8SNeilBrown 	int		sb_loaded;
12243b2e5d8SNeilBrown 	__u64		sb_events;
12343b2e5d8SNeilBrown 	sector_t	data_offset;	/* start of data in array */
124c6563a8cSNeilBrown 	sector_t	new_data_offset;/* only relevant while reshaping */
12543b2e5d8SNeilBrown 	sector_t	sb_start;	/* offset of the super block (in 512byte sectors) */
12643b2e5d8SNeilBrown 	int		sb_size;	/* bytes in the superblock */
12743b2e5d8SNeilBrown 	int		preferred_minor;	/* autorun support */
12843b2e5d8SNeilBrown 
12943b2e5d8SNeilBrown 	struct kobject	kobj;
13043b2e5d8SNeilBrown 
13143b2e5d8SNeilBrown 	/* A device can be in one of three states based on two flags:
13243b2e5d8SNeilBrown 	 * Not working:   faulty==1 in_sync==0
13343b2e5d8SNeilBrown 	 * Fully working: faulty==0 in_sync==1
13443b2e5d8SNeilBrown 	 * Working, but not
13543b2e5d8SNeilBrown 	 * in sync with array
13643b2e5d8SNeilBrown 	 *                faulty==0 in_sync==0
13743b2e5d8SNeilBrown 	 *
13843b2e5d8SNeilBrown 	 * It can never have faulty==1, in_sync==1
13943b2e5d8SNeilBrown 	 * This reduces the burden of testing multiple flags in many cases
14043b2e5d8SNeilBrown 	 */
14143b2e5d8SNeilBrown 
1422d78f8c4SNeilBrown 	unsigned long	flags;	/* bit set of 'enum flag_bits' bits. */
14343b2e5d8SNeilBrown 	wait_queue_head_t blocked_wait;
14443b2e5d8SNeilBrown 
14543b2e5d8SNeilBrown 	int desc_nr;			/* descriptor index in the superblock */
14643b2e5d8SNeilBrown 	int raid_disk;			/* role of device in array */
147e93f68a1SNeilBrown 	int new_raid_disk;		/* role that the device will have in
148e93f68a1SNeilBrown 					 * the array after a level-change completes.
149e93f68a1SNeilBrown 					 */
15043b2e5d8SNeilBrown 	int saved_raid_disk;		/* role that device used to have in the
15143b2e5d8SNeilBrown 					 * array and could again if we did a partial
15243b2e5d8SNeilBrown 					 * resync from the bitmap
15343b2e5d8SNeilBrown 					 */
1543069aa8dSShaohua Li 	union {
15543b2e5d8SNeilBrown 		sector_t recovery_offset;/* If this device has been partially
15643b2e5d8SNeilBrown 					 * recovered, this is where we were
15743b2e5d8SNeilBrown 					 * up to.
15843b2e5d8SNeilBrown 					 */
1593069aa8dSShaohua Li 		sector_t journal_tail;	/* If this device is a journal device,
1603069aa8dSShaohua Li 					 * this is the journal tail (journal
1613069aa8dSShaohua Li 					 * recovery start point)
1623069aa8dSShaohua Li 					 */
1633069aa8dSShaohua Li 	};
16443b2e5d8SNeilBrown 
16543b2e5d8SNeilBrown 	atomic_t	nr_pending;	/* number of pending requests.
16643b2e5d8SNeilBrown 					 * only maintained for arrays that
16743b2e5d8SNeilBrown 					 * support hot removal
16843b2e5d8SNeilBrown 					 */
16943b2e5d8SNeilBrown 	atomic_t	read_errors;	/* number of consecutive read errors that
17043b2e5d8SNeilBrown 					 * we have tried to ignore.
17143b2e5d8SNeilBrown 					 */
1720e3ef49eSArnd Bergmann 	time64_t	last_read_error;	/* monotonic time since our
1731e50915fSRobert Becker 						 * last read error
1741e50915fSRobert Becker 						 */
17543b2e5d8SNeilBrown 	atomic_t	corrected_errors; /* number of corrected read errors,
17643b2e5d8SNeilBrown 					   * for reporting to userspace and storing
17743b2e5d8SNeilBrown 					   * in superblock.
17843b2e5d8SNeilBrown 					   */
1793e148a32SGuoqing Jiang 
18069b00b5bSGuoqing Jiang 	struct serial_in_rdev *serial;  /* used for raid1 io serialization */
1813e148a32SGuoqing Jiang 
182324a56e1STejun Heo 	struct kernfs_node *sysfs_state; /* handle for 'state'
18343b2e5d8SNeilBrown 					   * sysfs entry */
184e1a86dbbSJunxiao Bi 	/* handle for 'unacknowledged_bad_blocks' sysfs dentry */
185e1a86dbbSJunxiao Bi 	struct kernfs_node *sysfs_unack_badblocks;
186e1a86dbbSJunxiao Bi 	/* handle for 'bad_blocks' sysfs dentry */
187e1a86dbbSJunxiao Bi 	struct kernfs_node *sysfs_badblocks;
188fc974ee2SVishal Verma 	struct badblocks badblocks;
189ea0213e0SArtur Paszkiewicz 
190ea0213e0SArtur Paszkiewicz 	struct {
191ea0213e0SArtur Paszkiewicz 		short offset;	/* Offset from superblock to start of PPL.
192ea0213e0SArtur Paszkiewicz 				 * Not used by external metadata. */
193ea0213e0SArtur Paszkiewicz 		unsigned int size;	/* Size in sectors of the PPL space */
194ea0213e0SArtur Paszkiewicz 		sector_t sector;	/* First sector of the PPL space */
195ea0213e0SArtur Paszkiewicz 	} ppl;
19643b2e5d8SNeilBrown };
1972d78f8c4SNeilBrown enum flag_bits {
1982d78f8c4SNeilBrown 	Faulty,			/* device is known to have a fault */
1992d78f8c4SNeilBrown 	In_sync,		/* device is in_sync with rest of array */
2008313b8e5SNeilBrown 	Bitmap_sync,		/* ..actually, not quite In_sync.  Need a
2014aaf7694SGuoqing Jiang 				 * bitmap-based recovery to get fully in sync.
2024aaf7694SGuoqing Jiang 				 * The bit is only meaningful before device
2034aaf7694SGuoqing Jiang 				 * has been passed to pers->hot_add_disk.
2048313b8e5SNeilBrown 				 */
2052d78f8c4SNeilBrown 	WriteMostly,		/* Avoid reading if at all possible */
2062d78f8c4SNeilBrown 	AutoDetected,		/* added by auto-detect */
2072d78f8c4SNeilBrown 	Blocked,		/* An error occurred but has not yet
2082d78f8c4SNeilBrown 				 * been acknowledged by the metadata
2092d78f8c4SNeilBrown 				 * handler, so don't allow writes
2102d78f8c4SNeilBrown 				 * until it is cleared */
2112d78f8c4SNeilBrown 	WriteErrorSeen,		/* A write error has been seen on this
2122d78f8c4SNeilBrown 				 * device
2132d78f8c4SNeilBrown 				 */
2142d78f8c4SNeilBrown 	FaultRecorded,		/* Intermediate state for clearing
2152d78f8c4SNeilBrown 				 * Blocked.  The Fault is/will-be
2162d78f8c4SNeilBrown 				 * recorded in the metadata, but that
2172d78f8c4SNeilBrown 				 * metadata hasn't been stored safely
2182d78f8c4SNeilBrown 				 * on disk yet.
2192d78f8c4SNeilBrown 				 */
2202d78f8c4SNeilBrown 	BlockedBadBlocks,	/* A writer is blocked because they
2212d78f8c4SNeilBrown 				 * found an unacknowledged bad-block.
2222d78f8c4SNeilBrown 				 * This can safely be cleared at any
2232d78f8c4SNeilBrown 				 * time, and the writer will re-check.
2242d78f8c4SNeilBrown 				 * It may be set at any time, and at
2252d78f8c4SNeilBrown 				 * worst the writer will timeout and
2262d78f8c4SNeilBrown 				 * re-check.  So setting it as
2272d78f8c4SNeilBrown 				 * accurately as possible is good, but
2282d78f8c4SNeilBrown 				 * not absolutely critical.
2292d78f8c4SNeilBrown 				 */
2302d78f8c4SNeilBrown 	WantReplacement,	/* This device is a candidate to be
2312d78f8c4SNeilBrown 				 * hot-replaced, either because it has
2322d78f8c4SNeilBrown 				 * reported some faults, or because
2332d78f8c4SNeilBrown 				 * of explicit request.
2342d78f8c4SNeilBrown 				 */
2352d78f8c4SNeilBrown 	Replacement,		/* This device is a replacement for
2362d78f8c4SNeilBrown 				 * a want_replacement device with same
2372d78f8c4SNeilBrown 				 * raid_disk number.
2382d78f8c4SNeilBrown 				 */
2391aee41f6SGoldwyn Rodrigues 	Candidate,		/* For clustered environments only:
2401aee41f6SGoldwyn Rodrigues 				 * This device is seen locally but not
2411aee41f6SGoldwyn Rodrigues 				 * by the whole cluster
2421aee41f6SGoldwyn Rodrigues 				 */
243bac624f3SSong Liu 	Journal,		/* This device is used as journal for
244bac624f3SSong Liu 				 * raid-5/6.
245bac624f3SSong Liu 				 * Usually, this device should be faster
246bac624f3SSong Liu 				 * than other devices in the array
247bac624f3SSong Liu 				 */
248659b254fSGuoqing Jiang 	ClusterRemove,
24935b785f7STomasz Majchrzak 	ExternalBbl,            /* External metadata provides bad
25035b785f7STomasz Majchrzak 				 * block management for a disk
25135b785f7STomasz Majchrzak 				 */
252688834e6SNeilBrown 	FailFast,		/* Minimal retries should be attempted on
253688834e6SNeilBrown 				 * this device, so use REQ_FAILFAST_DEV.
254688834e6SNeilBrown 				 * Also don't try to repair failed reads.
255688834e6SNeilBrown 				 * It is expects that no bad block log
256688834e6SNeilBrown 				 * is present.
257688834e6SNeilBrown 				 */
25846533ff7SNeilBrown 	LastDev,		/* Seems to be the last working dev as
25946533ff7SNeilBrown 				 * it didn't fail, so don't use FailFast
26046533ff7SNeilBrown 				 * any more for metadata
26146533ff7SNeilBrown 				 */
262404659cfSGuoqing Jiang 	CollisionCheck,		/*
263404659cfSGuoqing Jiang 				 * check if there is collision between raid1
264404659cfSGuoqing Jiang 				 * serial bios.
2653e148a32SGuoqing Jiang 				 */
2662c27d09dSYu Kuai 	Nonrot,			/* non-rotational device (SSD) */
2672d78f8c4SNeilBrown };
26843b2e5d8SNeilBrown 
is_badblock(struct md_rdev * rdev,sector_t s,int sectors,sector_t * first_bad,int * bad_sectors)2693cb03002SNeilBrown static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors,
2702230dfe4SNeilBrown 			      sector_t *first_bad, int *bad_sectors)
2712230dfe4SNeilBrown {
2722230dfe4SNeilBrown 	if (unlikely(rdev->badblocks.count)) {
273fc974ee2SVishal Verma 		int rv = badblocks_check(&rdev->badblocks, rdev->data_offset + s,
2742230dfe4SNeilBrown 					sectors,
2752230dfe4SNeilBrown 					first_bad, bad_sectors);
2762230dfe4SNeilBrown 		if (rv)
2772230dfe4SNeilBrown 			*first_bad -= rdev->data_offset;
2782230dfe4SNeilBrown 		return rv;
2792230dfe4SNeilBrown 	}
2802230dfe4SNeilBrown 	return 0;
2812230dfe4SNeilBrown }
2823a0f007bSYu Kuai 
rdev_has_badblock(struct md_rdev * rdev,sector_t s,int sectors)2833a0f007bSYu Kuai static inline int rdev_has_badblock(struct md_rdev *rdev, sector_t s,
2843a0f007bSYu Kuai 				    int sectors)
2853a0f007bSYu Kuai {
2863a0f007bSYu Kuai 	sector_t first_bad;
2873a0f007bSYu Kuai 	int bad_sectors;
2883a0f007bSYu Kuai 
2893a0f007bSYu Kuai 	return is_badblock(rdev, s, sectors, &first_bad, &bad_sectors);
2903a0f007bSYu Kuai }
2913a0f007bSYu Kuai 
2923cb03002SNeilBrown extern int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
293c6563a8cSNeilBrown 			      int is_new);
294c6563a8cSNeilBrown extern int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
295c6563a8cSNeilBrown 				int is_new);
296c4ce867fSGoldwyn Rodrigues struct md_cluster_info;
297c4ce867fSGoldwyn Rodrigues 
2989631abdbSMariusz Tkaczyk /**
2999631abdbSMariusz Tkaczyk  * enum mddev_flags - md device flags.
3009631abdbSMariusz Tkaczyk  * @MD_ARRAY_FIRST_USE: First use of array, needs initialization.
3019631abdbSMariusz Tkaczyk  * @MD_CLOSING: If set, we are closing the array, do not open it then.
3029631abdbSMariusz Tkaczyk  * @MD_JOURNAL_CLEAN: A raid with journal is already clean.
3039631abdbSMariusz Tkaczyk  * @MD_HAS_JOURNAL: The raid array has journal feature set.
3049631abdbSMariusz Tkaczyk  * @MD_CLUSTER_RESYNC_LOCKED: cluster raid only, which means node, already took
3059631abdbSMariusz Tkaczyk  *			       resync lock, need to release the lock.
3069631abdbSMariusz Tkaczyk  * @MD_FAILFAST_SUPPORTED: Using MD_FAILFAST on metadata writes is supported as
3079631abdbSMariusz Tkaczyk  *			    calls to md_error() will never cause the array to
3089631abdbSMariusz Tkaczyk  *			    become failed.
3099631abdbSMariusz Tkaczyk  * @MD_HAS_PPL:  The raid array has PPL feature set.
3109631abdbSMariusz Tkaczyk  * @MD_HAS_MULTIPLE_PPLS: The raid array has multiple PPLs feature set.
3119631abdbSMariusz Tkaczyk  * @MD_NOT_READY: do_md_run() is active, so 'array_state', ust not report that
3129631abdbSMariusz Tkaczyk  *		   array is ready yet.
3139631abdbSMariusz Tkaczyk  * @MD_BROKEN: This is used to stop writes and mark array as failed.
31412a6caf2SChristoph Hellwig  * @MD_DELETED: This device is being deleted
3159631abdbSMariusz Tkaczyk  *
3169631abdbSMariusz Tkaczyk  * change UNSUPPORTED_MDDEV_FLAGS for each array type if new flag is added
3179631abdbSMariusz Tkaczyk  */
318be306c29SNeilBrown enum mddev_flags {
3199631abdbSMariusz Tkaczyk 	MD_ARRAY_FIRST_USE,
3209631abdbSMariusz Tkaczyk 	MD_CLOSING,
3219631abdbSMariusz Tkaczyk 	MD_JOURNAL_CLEAN,
3229631abdbSMariusz Tkaczyk 	MD_HAS_JOURNAL,
3239631abdbSMariusz Tkaczyk 	MD_CLUSTER_RESYNC_LOCKED,
3249631abdbSMariusz Tkaczyk 	MD_FAILFAST_SUPPORTED,
3259631abdbSMariusz Tkaczyk 	MD_HAS_PPL,
3269631abdbSMariusz Tkaczyk 	MD_HAS_MULTIPLE_PPLS,
3279631abdbSMariusz Tkaczyk 	MD_NOT_READY,
3289631abdbSMariusz Tkaczyk 	MD_BROKEN,
32912a6caf2SChristoph Hellwig 	MD_DELETED,
330be306c29SNeilBrown };
3312953079cSShaohua Li 
3322953079cSShaohua Li enum mddev_sb_flags {
3332953079cSShaohua Li 	MD_SB_CHANGE_DEVS,		/* Some device status has changed */
3342953079cSShaohua Li 	MD_SB_CHANGE_CLEAN,	/* transition to or from 'clean' */
3352953079cSShaohua Li 	MD_SB_CHANGE_PENDING,	/* switch from 'clean' to 'active' in progress */
3362953079cSShaohua Li 	MD_SB_NEED_REWRITE,	/* metadata write needs to be repeated */
3372953079cSShaohua Li };
3382953079cSShaohua Li 
339404659cfSGuoqing Jiang #define NR_SERIAL_INFOS		8
340404659cfSGuoqing Jiang /* record current range of serialize IOs */
341404659cfSGuoqing Jiang struct serial_info {
34269b00b5bSGuoqing Jiang 	struct rb_node node;
34369b00b5bSGuoqing Jiang 	sector_t start;		/* start sector of rb node */
34469b00b5bSGuoqing Jiang 	sector_t last;		/* end sector of rb node */
34569b00b5bSGuoqing Jiang 	sector_t _subtree_last; /* highest sector in subtree of rb node */
3463e148a32SGuoqing Jiang };
3473e148a32SGuoqing Jiang 
348eac58d08SLogan Gunthorpe /*
349eac58d08SLogan Gunthorpe  * mddev->curr_resync stores the current sector of the resync but
350eac58d08SLogan Gunthorpe  * also has some overloaded values.
351eac58d08SLogan Gunthorpe  */
352eac58d08SLogan Gunthorpe enum {
353eac58d08SLogan Gunthorpe 	/* No resync in progress */
354eac58d08SLogan Gunthorpe 	MD_RESYNC_NONE = 0,
355eac58d08SLogan Gunthorpe 	/* Yielded to allow another conflicting resync to commence */
356eac58d08SLogan Gunthorpe 	MD_RESYNC_YIELDED = 1,
357eac58d08SLogan Gunthorpe 	/* Delayed to check that there is no conflict with another sync */
358eac58d08SLogan Gunthorpe 	MD_RESYNC_DELAYED = 2,
359eac58d08SLogan Gunthorpe 	/* Any value greater than or equal to this is in an active resync */
360eac58d08SLogan Gunthorpe 	MD_RESYNC_ACTIVE = 3,
361eac58d08SLogan Gunthorpe };
362eac58d08SLogan Gunthorpe 
363fd01b88cSNeilBrown struct mddev {
36443b2e5d8SNeilBrown 	void				*private;
36584fc4b56SNeilBrown 	struct md_personality		*pers;
36643b2e5d8SNeilBrown 	dev_t				unit;
36743b2e5d8SNeilBrown 	int				md_minor;
36843b2e5d8SNeilBrown 	struct list_head		disks;
36943b2e5d8SNeilBrown 	unsigned long			flags;
3702953079cSShaohua Li 	unsigned long			sb_flags;
37143b2e5d8SNeilBrown 
372409c57f3SNeilBrown 	int				suspended;
373714d2015SYu Kuai 	struct mutex			suspend_mutex;
37472adae23SXiao Ni 	struct percpu_ref		active_io;
37543b2e5d8SNeilBrown 	int				ro;
376bb4f1e9dSNeilBrown 	int				sysfs_active; /* set when sysfs deletes
377bb4f1e9dSNeilBrown 						       * are happening, so run/
378bb4f1e9dSNeilBrown 						       * takeover/stop are not safe
379bb4f1e9dSNeilBrown 						       */
38043b2e5d8SNeilBrown 	struct gendisk			*gendisk;
38143b2e5d8SNeilBrown 
38243b2e5d8SNeilBrown 	struct kobject			kobj;
38343b2e5d8SNeilBrown 	int				hold_active;
38443b2e5d8SNeilBrown #define	UNTIL_IOCTL	1
38543b2e5d8SNeilBrown #define	UNTIL_STOP	2
38643b2e5d8SNeilBrown 
38743b2e5d8SNeilBrown 	/* Superblock information */
38843b2e5d8SNeilBrown 	int				major_version,
38943b2e5d8SNeilBrown 					minor_version,
39043b2e5d8SNeilBrown 					patch_version;
39143b2e5d8SNeilBrown 	int				persistent;
39243b2e5d8SNeilBrown 	int				external;	/* metadata is
39343b2e5d8SNeilBrown 							 * managed externally */
39443b2e5d8SNeilBrown 	char				metadata_type[17]; /* externally set*/
39577a68698SMike Snitzer 	int				chunk_sectors;
3969ebc6ef1SDeepa Dinamani 	time64_t			ctime, utime;
39743b2e5d8SNeilBrown 	int				level, layout;
39843b2e5d8SNeilBrown 	char				clevel[16];
39943b2e5d8SNeilBrown 	int				raid_disks;
40043b2e5d8SNeilBrown 	int				max_disks;
40158c0fed4SAndre Noll 	sector_t			dev_sectors;	/* used size of
40258c0fed4SAndre Noll 							 * component devices */
40343b2e5d8SNeilBrown 	sector_t			array_sectors; /* exported array size */
404b522adcdSDan Williams 	int				external_size; /* size managed
405b522adcdSDan Williams 							* externally */
40643b2e5d8SNeilBrown 	__u64				events;
407a8707c08SNeilBrown 	/* If the last 'event' was simply a clean->dirty transition, and
408a8707c08SNeilBrown 	 * we didn't write it to the spares, then it is safe and simple
409a8707c08SNeilBrown 	 * to just decrement the event count on a dirty->clean transition.
410a8707c08SNeilBrown 	 * So we record that possibility here.
411a8707c08SNeilBrown 	 */
412a8707c08SNeilBrown 	int				can_decrease_events;
41343b2e5d8SNeilBrown 
41443b2e5d8SNeilBrown 	char				uuid[16];
41543b2e5d8SNeilBrown 
41643b2e5d8SNeilBrown 	/* If the array is being reshaped, we need to record the
41743b2e5d8SNeilBrown 	 * new shape and an indication of where we are up to.
41843b2e5d8SNeilBrown 	 * This is written to the superblock.
41943b2e5d8SNeilBrown 	 * If reshape_position is MaxSector, then no reshape is happening (yet).
42043b2e5d8SNeilBrown 	 */
42143b2e5d8SNeilBrown 	sector_t			reshape_position;
422664e7c41SAndre Noll 	int				delta_disks, new_level, new_layout;
42377a68698SMike Snitzer 	int				new_chunk_sectors;
4242c810cddSNeilBrown 	int				reshape_backwards;
42543b2e5d8SNeilBrown 
42644693154SYu Kuai 	struct md_thread __rcu		*thread;	/* management thread */
42744693154SYu Kuai 	struct md_thread __rcu		*sync_thread;	/* doing resync or reconstruct */
428c4a39551SJonathan Brassow 
429d249e541SYu Kuai 	/*
430d249e541SYu Kuai 	 * Set when a sync operation is started. It holds this value even
431c4a39551SJonathan Brassow 	 * when the sync thread is "frozen" (interrupted) or "idle" (stopped
432c4a39551SJonathan Brassow 	 * or finished). It is overwritten when a new sync operation is begun.
433c4a39551SJonathan Brassow 	 */
434d249e541SYu Kuai 	enum sync_action		last_sync_action;
43543b2e5d8SNeilBrown 	sector_t			curr_resync;	/* last block scheduled */
43697e4f42dSNeilBrown 	/* As resync requests can complete out of order, we cannot easily track
43797e4f42dSNeilBrown 	 * how much resync has been completed.  So we occasionally pause until
43897e4f42dSNeilBrown 	 * everything completes, then set curr_resync_completed to curr_resync.
43997e4f42dSNeilBrown 	 * As such it may be well behind the real resync mark, but it is a value
44097e4f42dSNeilBrown 	 * we are certain of.
44197e4f42dSNeilBrown 	 */
44297e4f42dSNeilBrown 	sector_t			curr_resync_completed;
44343b2e5d8SNeilBrown 	unsigned long			resync_mark;	/* a recent timestamp */
44443b2e5d8SNeilBrown 	sector_t			resync_mark_cnt;/* blocks written at resync_mark */
44543b2e5d8SNeilBrown 	sector_t			curr_mark_cnt; /* blocks scheduled now */
44643b2e5d8SNeilBrown 
44743b2e5d8SNeilBrown 	sector_t			resync_max_sectors; /* may be set by personality */
44843b2e5d8SNeilBrown 
4497f7583d4SJianpeng Ma 	atomic64_t			resync_mismatches; /* count of sectors where
45043b2e5d8SNeilBrown 							    * parity/replica mismatch found
45143b2e5d8SNeilBrown 							    */
45243b2e5d8SNeilBrown 
45343b2e5d8SNeilBrown 	/* allow user-space to request suspension of IO to regions of the array */
45443b2e5d8SNeilBrown 	sector_t			suspend_lo;
45543b2e5d8SNeilBrown 	sector_t			suspend_hi;
45643b2e5d8SNeilBrown 	/* if zero, use the system-wide default */
45743b2e5d8SNeilBrown 	int				sync_speed_min;
45843b2e5d8SNeilBrown 	int				sync_speed_max;
45943b2e5d8SNeilBrown 
46043b2e5d8SNeilBrown 	/* resync even though the same disks are shared among md-devices */
46143b2e5d8SNeilBrown 	int				parallel_resync;
46243b2e5d8SNeilBrown 
46343b2e5d8SNeilBrown 	int				ok_start_degraded;
46443b2e5d8SNeilBrown 
46543b2e5d8SNeilBrown 	unsigned long			recovery;
4665389042fSNeilBrown 	/* If a RAID personality determines that recovery (of a particular
4675389042fSNeilBrown 	 * device) will fail due to a read error on the source device, it
4685389042fSNeilBrown 	 * takes a copy of this number and does not attempt recovery again
4695389042fSNeilBrown 	 * until this number changes.
4705389042fSNeilBrown 	 */
4715389042fSNeilBrown 	int				recovery_disabled;
47243b2e5d8SNeilBrown 
47343b2e5d8SNeilBrown 	int				in_sync;	/* know to not need resync */
474c8c00a69SNeilBrown 	/* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
475c8c00a69SNeilBrown 	 * that we are never stopping an array while it is open.
476c8c00a69SNeilBrown 	 * 'reconfig_mutex' protects all other reconfiguration.
477c8c00a69SNeilBrown 	 * These locks are separate due to conflicting interactions
478a8698707SChristoph Hellwig 	 * with disk->open_mutex.
479c8c00a69SNeilBrown 	 * Lock ordering is:
480a8698707SChristoph Hellwig 	 *  reconfig_mutex -> disk->open_mutex
481a8698707SChristoph Hellwig 	 *  disk->open_mutex -> open_mutex:  e.g. __blkdev_get -> md_open
482c8c00a69SNeilBrown 	 */
483c8c00a69SNeilBrown 	struct mutex			open_mutex;
48443b2e5d8SNeilBrown 	struct mutex			reconfig_mutex;
48543b2e5d8SNeilBrown 	atomic_t			active;		/* general refcount */
48643b2e5d8SNeilBrown 	atomic_t			openers;	/* number of active opens */
48743b2e5d8SNeilBrown 
488f0b4f7e2SNeilBrown 	int				changed;	/* True if we might need to
489f0b4f7e2SNeilBrown 							 * reread partition info */
49043b2e5d8SNeilBrown 	int				degraded;	/* whether md should consider
49143b2e5d8SNeilBrown 							 * adding a spare
49243b2e5d8SNeilBrown 							 */
49343b2e5d8SNeilBrown 
49443b2e5d8SNeilBrown 	atomic_t			recovery_active; /* blocks scheduled, but not written */
49543b2e5d8SNeilBrown 	wait_queue_head_t		recovery_wait;
49643b2e5d8SNeilBrown 	sector_t			recovery_cp;
49743b2e5d8SNeilBrown 	sector_t			resync_min;	/* user requested sync
49843b2e5d8SNeilBrown 							 * starts here */
49943b2e5d8SNeilBrown 	sector_t			resync_max;	/* resync should pause
50043b2e5d8SNeilBrown 							 * when it gets here */
50143b2e5d8SNeilBrown 
502324a56e1STejun Heo 	struct kernfs_node		*sysfs_state;	/* handle for 'array_state'
50343b2e5d8SNeilBrown 							 * file in sysfs.
50443b2e5d8SNeilBrown 							 */
505324a56e1STejun Heo 	struct kernfs_node		*sysfs_action;  /* handle for 'sync_action' */
506e1a86dbbSJunxiao Bi 	struct kernfs_node		*sysfs_completed;	/*handle for 'sync_completed' */
507e1a86dbbSJunxiao Bi 	struct kernfs_node		*sysfs_degraded;	/*handle for 'degraded' */
508e1a86dbbSJunxiao Bi 	struct kernfs_node		*sysfs_level;		/*handle for 'level' */
50943b2e5d8SNeilBrown 
510ac619781SYu Kuai 	/* used for delayed sysfs removal */
511ac619781SYu Kuai 	struct work_struct del_work;
512ac619781SYu Kuai 	/* used for register new sync thread */
513ac619781SYu Kuai 	struct work_struct sync_work;
51443b2e5d8SNeilBrown 
51585572d7cSNeilBrown 	/* "lock" protects:
51685572d7cSNeilBrown 	 *   flush_bio transition from NULL to !NULL
51785572d7cSNeilBrown 	 *   rdev superblocks, events
51885572d7cSNeilBrown 	 *   clearing MD_CHANGE_*
51985572d7cSNeilBrown 	 *   in_sync - and related safemode and MD_CHANGE changes
52036d091f4SNeilBrown 	 *   pers (also protected by reconfig_mutex and pending IO).
521978a7a47SNeilBrown 	 *   clearing ->bitmap
5224af1a041SNeilBrown 	 *   clearing ->bitmap_info.file
52323da422bSNeilBrown 	 *   changing ->resync_{min,max}
52423da422bSNeilBrown 	 *   setting MD_RECOVERY_RUNNING (which interacts with resync_{min,max})
52585572d7cSNeilBrown 	 */
52685572d7cSNeilBrown 	spinlock_t			lock;
52743b2e5d8SNeilBrown 	wait_queue_head_t		sb_wait;	/* for waiting on superblock updates */
52843b2e5d8SNeilBrown 	atomic_t			pending_writes;	/* number of active superblock writes */
52943b2e5d8SNeilBrown 
53043b2e5d8SNeilBrown 	unsigned int			safemode;	/* if set, update "clean" superblock
53143b2e5d8SNeilBrown 							 * when no writes pending.
53243b2e5d8SNeilBrown 							 */
53343b2e5d8SNeilBrown 	unsigned int			safemode_delay;
53443b2e5d8SNeilBrown 	struct timer_list		safemode_timer;
5354ad23a97SNeilBrown 	struct percpu_ref		writes_pending;
5364ad23a97SNeilBrown 	int				sync_checkers;	/* # of threads checking writes_pending */
53743b2e5d8SNeilBrown 
538*59fdd433SYu Kuai 	void				*bitmap; /* the bitmap for the device */
5397add9db6SYu Kuai 	struct bitmap_operations	*bitmap_ops;
540c3d9714eSNeilBrown 	struct {
541c3d9714eSNeilBrown 		struct file		*file; /* the bitmap file */
542f6af949cSNeilBrown 		loff_t			offset; /* offset from superblock of
54343b2e5d8SNeilBrown 						 * start of bitmap. May be
54443b2e5d8SNeilBrown 						 * negative, but not '0'
545f6af949cSNeilBrown 						 * For external metadata, offset
546f6af949cSNeilBrown 						 * from start of device.
54743b2e5d8SNeilBrown 						 */
5486409bb05SNeilBrown 		unsigned long		space; /* space available at this offset */
549f6af949cSNeilBrown 		loff_t			default_offset; /* this is the offset to use when
55043b2e5d8SNeilBrown 							 * hot-adding a bitmap.  It should
55143b2e5d8SNeilBrown 							 * eventually be settable by sysfs.
55243b2e5d8SNeilBrown 							 */
5536409bb05SNeilBrown 		unsigned long		default_space; /* space available at
5546409bb05SNeilBrown 							* default offset */
555c3d9714eSNeilBrown 		struct mutex		mutex;
55642a04b50SNeilBrown 		unsigned long		chunksize;
557ac2f40beSNeilBrown 		unsigned long		daemon_sleep; /* how many jiffies between updates? */
55842a04b50SNeilBrown 		unsigned long		max_write_behind; /* write-behind mode */
559ece5cff0SNeilBrown 		int			external;
560c4ce867fSGoldwyn Rodrigues 		int			nodes; /* Maximum number of nodes in the cluster */
561cf921cc1SGoldwyn Rodrigues 		char                    cluster_name[64]; /* Name of the cluster */
562c3d9714eSNeilBrown 	} bitmap_info;
56343b2e5d8SNeilBrown 
5641e50915fSRobert Becker 	atomic_t			max_corr_read_errors; /* max read retries */
56543b2e5d8SNeilBrown 	struct list_head		all_mddevs;
566a2826aa9SNeilBrown 
567c32dc040SRikard Falkeborn 	const struct attribute_group	*to_remove;
568252ac522SNeilBrown 
569afeee514SKent Overstreet 	struct bio_set			bio_set;
570afeee514SKent Overstreet 	struct bio_set			sync_set; /* for sync operations like
5715a85071cSNeilBrown 						   * metadata and bitmap writes
5725a85071cSNeilBrown 						   */
573c687297bSYu Kuai 	struct bio_set			io_clone_set;
574a167f663SNeilBrown 
575768a418dSNeilBrown 	struct work_struct event_work;	/* used by dm to report failure event */
576404659cfSGuoqing Jiang 	mempool_t *serial_info_pool;
577fd01b88cSNeilBrown 	void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
578c4ce867fSGoldwyn Rodrigues 	struct md_cluster_info		*cluster_info;
57915858fa5SGuoqing Jiang 	unsigned int			good_device_nr;	/* good device num within cluster raid */
58078f57ef9SColy Li 	unsigned int			noio_flag; /* for memalloc scope API */
5814b6c1060SHeinz Mauelshagen 
5823ce94ce5SYu Kuai 	/*
5833ce94ce5SYu Kuai 	 * Temporarily store rdev that will be finally removed when
5844934b640SYu Kuai 	 * reconfig_mutex is unlocked, protected by reconfig_mutex.
5853ce94ce5SYu Kuai 	 */
5863ce94ce5SYu Kuai 	struct list_head		deleting;
5873ce94ce5SYu Kuai 
588130443d6SYu Kuai 	/* The sequence number for sync thread */
589130443d6SYu Kuai 	atomic_t sync_seq;
5906f56f0c4SYu Kuai 
5914b6c1060SHeinz Mauelshagen 	bool	has_superblocks:1;
5929a567843SGuoqing Jiang 	bool	fail_last_dev:1;
5933938f5fbSGuoqing Jiang 	bool	serialize_policy:1;
59443b2e5d8SNeilBrown };
59543b2e5d8SNeilBrown 
596be306c29SNeilBrown enum recovery_flags {
5970476d09cSYu Kuai 	/* flags for sync thread running status */
5980476d09cSYu Kuai 
599be306c29SNeilBrown 	/*
6000476d09cSYu Kuai 	 * set when one of sync action is set and new sync thread need to be
6010476d09cSYu Kuai 	 * registered, or just add/remove spares from conf.
602be306c29SNeilBrown 	 */
6030476d09cSYu Kuai 	MD_RECOVERY_NEEDED,
6040476d09cSYu Kuai 	/* sync thread is running, or about to be started */
6050476d09cSYu Kuai 	MD_RECOVERY_RUNNING,
6060476d09cSYu Kuai 	/* sync thread needs to be aborted for some reason */
6070476d09cSYu Kuai 	MD_RECOVERY_INTR,
6080476d09cSYu Kuai 	/* sync thread is done and is waiting to be unregistered */
6090476d09cSYu Kuai 	MD_RECOVERY_DONE,
6100476d09cSYu Kuai 	/* running sync thread must abort immediately, and not restart */
6110476d09cSYu Kuai 	MD_RECOVERY_FROZEN,
6120476d09cSYu Kuai 	/* waiting for pers->start() to finish */
6130476d09cSYu Kuai 	MD_RECOVERY_WAIT,
6140476d09cSYu Kuai 	/* interrupted because io-error */
6150476d09cSYu Kuai 	MD_RECOVERY_ERROR,
6160476d09cSYu Kuai 
617a85aa09dSYu Kuai 	/* flags determines sync action, see details in enum sync_action */
6180476d09cSYu Kuai 
6190476d09cSYu Kuai 	/* if just this flag is set, action is resync. */
6200476d09cSYu Kuai 	MD_RECOVERY_SYNC,
6210476d09cSYu Kuai 	/*
6220476d09cSYu Kuai 	 * paired with MD_RECOVERY_SYNC, if MD_RECOVERY_CHECK is not set,
6230476d09cSYu Kuai 	 * action is repair, means user requested resync.
6240476d09cSYu Kuai 	 */
6250476d09cSYu Kuai 	MD_RECOVERY_REQUESTED,
6260476d09cSYu Kuai 	/*
6270476d09cSYu Kuai 	 * paired with MD_RECOVERY_SYNC and MD_RECOVERY_REQUESTED, action is
6280476d09cSYu Kuai 	 * check.
6290476d09cSYu Kuai 	 */
6300476d09cSYu Kuai 	MD_RECOVERY_CHECK,
6310476d09cSYu Kuai 	/* recovery, or need to try it */
6320476d09cSYu Kuai 	MD_RECOVERY_RECOVER,
6330476d09cSYu Kuai 	/* reshape */
6340476d09cSYu Kuai 	MD_RECOVERY_RESHAPE,
6350476d09cSYu Kuai 	/* remote node is running resync thread */
6360476d09cSYu Kuai 	MD_RESYNCING_REMOTE,
637be306c29SNeilBrown };
638be306c29SNeilBrown 
639314e9af0SYu Kuai enum md_ro_state {
640314e9af0SYu Kuai 	MD_RDWR,
641314e9af0SYu Kuai 	MD_RDONLY,
642314e9af0SYu Kuai 	MD_AUTO_READ,
643314e9af0SYu Kuai 	MD_MAX_STATE
644314e9af0SYu Kuai };
645314e9af0SYu Kuai 
md_is_rdwr(struct mddev * mddev)646314e9af0SYu Kuai static inline bool md_is_rdwr(struct mddev *mddev)
647314e9af0SYu Kuai {
648314e9af0SYu Kuai 	return (mddev->ro == MD_RDWR);
649314e9af0SYu Kuai }
650314e9af0SYu Kuai 
reshape_interrupted(struct mddev * mddev)651503f9d43SYu Kuai static inline bool reshape_interrupted(struct mddev *mddev)
652503f9d43SYu Kuai {
653503f9d43SYu Kuai 	/* reshape never start */
654503f9d43SYu Kuai 	if (mddev->reshape_position == MaxSector)
655503f9d43SYu Kuai 		return false;
656503f9d43SYu Kuai 
657503f9d43SYu Kuai 	/* interrupted */
658503f9d43SYu Kuai 	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
659503f9d43SYu Kuai 		return true;
660503f9d43SYu Kuai 
661503f9d43SYu Kuai 	/* running reshape will be interrupted soon. */
662503f9d43SYu Kuai 	if (test_bit(MD_RECOVERY_WAIT, &mddev->recovery) ||
663503f9d43SYu Kuai 	    test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
664503f9d43SYu Kuai 	    test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
665503f9d43SYu Kuai 		return true;
666503f9d43SYu Kuai 
667503f9d43SYu Kuai 	return false;
668503f9d43SYu Kuai }
669503f9d43SYu Kuai 
mddev_lock(struct mddev * mddev)6705c47daf6SNeilBrown static inline int __must_check mddev_lock(struct mddev *mddev)
6715c47daf6SNeilBrown {
6725c47daf6SNeilBrown 	return mutex_lock_interruptible(&mddev->reconfig_mutex);
6735c47daf6SNeilBrown }
6745c47daf6SNeilBrown 
6755c47daf6SNeilBrown /* Sometimes we need to take the lock in a situation where
6765c47daf6SNeilBrown  * failure due to interrupts is not acceptable.
6775c47daf6SNeilBrown  */
mddev_lock_nointr(struct mddev * mddev)6785c47daf6SNeilBrown static inline void mddev_lock_nointr(struct mddev *mddev)
6795c47daf6SNeilBrown {
6805c47daf6SNeilBrown 	mutex_lock(&mddev->reconfig_mutex);
6815c47daf6SNeilBrown }
6825c47daf6SNeilBrown 
mddev_trylock(struct mddev * mddev)6835c47daf6SNeilBrown static inline int mddev_trylock(struct mddev *mddev)
6845c47daf6SNeilBrown {
6855c47daf6SNeilBrown 	return mutex_trylock(&mddev->reconfig_mutex);
6865c47daf6SNeilBrown }
6875c47daf6SNeilBrown extern void mddev_unlock(struct mddev *mddev);
6885c47daf6SNeilBrown 
md_sync_acct(struct block_device * bdev,unsigned long nr_sectors)68943b2e5d8SNeilBrown static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
69043b2e5d8SNeilBrown {
6919d1110f9SLi Nan 	if (blk_queue_io_stat(bdev->bd_disk->queue))
692504fbcffSLi Nan 		atomic_add(nr_sectors, &bdev->bd_disk->sync_io);
69343b2e5d8SNeilBrown }
69443b2e5d8SNeilBrown 
md_sync_acct_bio(struct bio * bio,unsigned long nr_sectors)69574d46992SChristoph Hellwig static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
69674d46992SChristoph Hellwig {
697309dca30SChristoph Hellwig 	md_sync_acct(bio->bi_bdev, nr_sectors);
69874d46992SChristoph Hellwig }
69974d46992SChristoph Hellwig 
70084fc4b56SNeilBrown struct md_personality
70143b2e5d8SNeilBrown {
70243b2e5d8SNeilBrown 	char *name;
70343b2e5d8SNeilBrown 	int level;
70443b2e5d8SNeilBrown 	struct list_head list;
70543b2e5d8SNeilBrown 	struct module *owner;
706775d7831SDavid Jeffery 	bool __must_check (*make_request)(struct mddev *mddev, struct bio *bio);
707d5d885fdSSong Liu 	/*
708d5d885fdSSong Liu 	 * start up works that do NOT require md_thread. tasks that
709d5d885fdSSong Liu 	 * requires md_thread should go into start()
710d5d885fdSSong Liu 	 */
711fd01b88cSNeilBrown 	int (*run)(struct mddev *mddev);
712d5d885fdSSong Liu 	/* start up works that require md threads */
713d5d885fdSSong Liu 	int (*start)(struct mddev *mddev);
714afa0f557SNeilBrown 	void (*free)(struct mddev *mddev, void *priv);
715fd01b88cSNeilBrown 	void (*status)(struct seq_file *seq, struct mddev *mddev);
71643b2e5d8SNeilBrown 	/* error_handler must set ->faulty and clear ->in_sync
71743b2e5d8SNeilBrown 	 * if appropriate, and should abort recovery if needed
71843b2e5d8SNeilBrown 	 */
719fd01b88cSNeilBrown 	void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev);
720fd01b88cSNeilBrown 	int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev);
721b8321b68SNeilBrown 	int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev);
722fd01b88cSNeilBrown 	int (*spare_active) (struct mddev *mddev);
723bc49694aSYu Kuai 	sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr,
724bc49694aSYu Kuai 				 sector_t max_sector, int *skipped);
725fd01b88cSNeilBrown 	int (*resize) (struct mddev *mddev, sector_t sectors);
726fd01b88cSNeilBrown 	sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks);
727fd01b88cSNeilBrown 	int (*check_reshape) (struct mddev *mddev);
728fd01b88cSNeilBrown 	int (*start_reshape) (struct mddev *mddev);
729fd01b88cSNeilBrown 	void (*finish_reshape) (struct mddev *mddev);
7307564bedaSGuoqing Jiang 	void (*update_reshape_pos) (struct mddev *mddev);
7315625ff8bSYu Kuai 	void (*prepare_suspend) (struct mddev *mddev);
732b03e0ccbSNeilBrown 	/* quiesce suspends or resumes internal processing.
733b03e0ccbSNeilBrown 	 * 1 - stop new actions and wait for action io to complete
734b03e0ccbSNeilBrown 	 * 0 - return to normal behaviour
73543b2e5d8SNeilBrown 	 */
736b03e0ccbSNeilBrown 	void (*quiesce) (struct mddev *mddev, int quiesce);
737245f46c2SNeilBrown 	/* takeover is used to transition an array from one
738245f46c2SNeilBrown 	 * personality to another.  The new personality must be able
739245f46c2SNeilBrown 	 * to handle the data in the current layout.
740245f46c2SNeilBrown 	 * e.g. 2drive raid1 -> 2drive raid5
741245f46c2SNeilBrown 	 *      ndrive raid5 -> degraded n+1drive raid6 with special layout
742245f46c2SNeilBrown 	 * If the takeover succeeds, a new 'private' structure is returned.
743245f46c2SNeilBrown 	 * This needs to be installed and then ->run used to activate the
744245f46c2SNeilBrown 	 * array.
745245f46c2SNeilBrown 	 */
746fd01b88cSNeilBrown 	void *(*takeover) (struct mddev *mddev);
747ba903a3eSArtur Paszkiewicz 	/* Changes the consistency policy of an active array. */
748ba903a3eSArtur Paszkiewicz 	int (*change_consistency_policy)(struct mddev *mddev, const char *buf);
74943b2e5d8SNeilBrown };
75043b2e5d8SNeilBrown 
75143b2e5d8SNeilBrown struct md_sysfs_entry {
75243b2e5d8SNeilBrown 	struct attribute attr;
753fd01b88cSNeilBrown 	ssize_t (*show)(struct mddev *, char *);
754fd01b88cSNeilBrown 	ssize_t (*store)(struct mddev *, const char *, size_t);
75543b2e5d8SNeilBrown };
756c32dc040SRikard Falkeborn extern const struct attribute_group md_bitmap_group;
75743b2e5d8SNeilBrown 
sysfs_get_dirent_safe(struct kernfs_node * sd,char * name)758324a56e1STejun Heo static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name)
75900bcb4acSNeilBrown {
76000bcb4acSNeilBrown 	if (sd)
761388975ccSTejun Heo 		return sysfs_get_dirent(sd, name);
76200bcb4acSNeilBrown 	return sd;
76300bcb4acSNeilBrown }
sysfs_notify_dirent_safe(struct kernfs_node * sd)764324a56e1STejun Heo static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
76500bcb4acSNeilBrown {
76600bcb4acSNeilBrown 	if (sd)
76700bcb4acSNeilBrown 		sysfs_notify_dirent(sd);
76800bcb4acSNeilBrown }
76900bcb4acSNeilBrown 
mdname(struct mddev * mddev)770fd01b88cSNeilBrown static inline char * mdname (struct mddev * mddev)
77143b2e5d8SNeilBrown {
77243b2e5d8SNeilBrown 	return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
77343b2e5d8SNeilBrown }
77443b2e5d8SNeilBrown 
sysfs_link_rdev(struct mddev * mddev,struct md_rdev * rdev)775fd01b88cSNeilBrown static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev)
77636fad858SNamhyung Kim {
77736fad858SNamhyung Kim 	char nm[20];
7789b15603dSShaohua Li 	if (!test_bit(Replacement, &rdev->flags) &&
7799b15603dSShaohua Li 	    !test_bit(Journal, &rdev->flags) &&
7809b15603dSShaohua Li 	    mddev->kobj.sd) {
78136fad858SNamhyung Kim 		sprintf(nm, "rd%d", rdev->raid_disk);
78236fad858SNamhyung Kim 		return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
7832d78f8c4SNeilBrown 	} else
7842d78f8c4SNeilBrown 		return 0;
78536fad858SNamhyung Kim }
78636fad858SNamhyung Kim 
sysfs_unlink_rdev(struct mddev * mddev,struct md_rdev * rdev)787fd01b88cSNeilBrown static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev)
78836fad858SNamhyung Kim {
78936fad858SNamhyung Kim 	char nm[20];
7909b15603dSShaohua Li 	if (!test_bit(Replacement, &rdev->flags) &&
7919b15603dSShaohua Li 	    !test_bit(Journal, &rdev->flags) &&
7929b15603dSShaohua Li 	    mddev->kobj.sd) {
79336fad858SNamhyung Kim 		sprintf(nm, "rd%d", rdev->raid_disk);
79436fad858SNamhyung Kim 		sysfs_remove_link(&mddev->kobj, nm);
79536fad858SNamhyung Kim 	}
7962d78f8c4SNeilBrown }
79736fad858SNamhyung Kim 
79843b2e5d8SNeilBrown /*
79943b2e5d8SNeilBrown  * iterates through some rdev ringlist. It's safe to remove the
80043b2e5d8SNeilBrown  * current 'rdev'. Dont touch 'tmp' though.
80143b2e5d8SNeilBrown  */
80243b2e5d8SNeilBrown #define rdev_for_each_list(rdev, tmp, head)				\
80343b2e5d8SNeilBrown 	list_for_each_entry_safe(rdev, tmp, head, same_set)
80443b2e5d8SNeilBrown 
80543b2e5d8SNeilBrown /*
80643b2e5d8SNeilBrown  * iterates through the 'same array disks' ringlist
80743b2e5d8SNeilBrown  */
808dafb20faSNeilBrown #define rdev_for_each(rdev, mddev)				\
809dafb20faSNeilBrown 	list_for_each_entry(rdev, &((mddev)->disks), same_set)
810dafb20faSNeilBrown 
811dafb20faSNeilBrown #define rdev_for_each_safe(rdev, tmp, mddev)				\
81243b2e5d8SNeilBrown 	list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
81343b2e5d8SNeilBrown 
81443b2e5d8SNeilBrown #define rdev_for_each_rcu(rdev, mddev)				\
81543b2e5d8SNeilBrown 	list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
81643b2e5d8SNeilBrown 
8172b8bf345SNeilBrown struct md_thread {
8184ed8731dSShaohua Li 	void			(*run) (struct md_thread *thread);
819fd01b88cSNeilBrown 	struct mddev		*mddev;
82043b2e5d8SNeilBrown 	wait_queue_head_t	wqueue;
82143b2e5d8SNeilBrown 	unsigned long		flags;
82243b2e5d8SNeilBrown 	struct task_struct	*tsk;
82343b2e5d8SNeilBrown 	unsigned long		timeout;
8244ed8731dSShaohua Li 	void			*private;
8252b8bf345SNeilBrown };
82643b2e5d8SNeilBrown 
827c687297bSYu Kuai struct md_io_clone {
82876fed014SXiao Ni 	struct mddev	*mddev;
82910764815SGuoqing Jiang 	struct bio	*orig_bio;
83010764815SGuoqing Jiang 	unsigned long	start_time;
83110764815SGuoqing Jiang 	struct bio	bio_clone;
83210764815SGuoqing Jiang };
83310764815SGuoqing Jiang 
83443b2e5d8SNeilBrown #define THREAD_WAKEUP  0
83543b2e5d8SNeilBrown 
safe_put_page(struct page * p)83643b2e5d8SNeilBrown static inline void safe_put_page(struct page *p)
83743b2e5d8SNeilBrown {
83843b2e5d8SNeilBrown 	if (p) put_page(p);
83943b2e5d8SNeilBrown }
84043b2e5d8SNeilBrown 
84184fc4b56SNeilBrown extern int register_md_personality(struct md_personality *p);
84284fc4b56SNeilBrown extern int unregister_md_personality(struct md_personality *p);
8431f4a72ffSChristophe JAILLET extern int register_md_cluster_operations(const struct md_cluster_operations *ops,
844edb39c9dSGoldwyn Rodrigues 		struct module *module);
845edb39c9dSGoldwyn Rodrigues extern int unregister_md_cluster_operations(void);
846edb39c9dSGoldwyn Rodrigues extern int md_setup_cluster(struct mddev *mddev, int nodes);
847edb39c9dSGoldwyn Rodrigues extern void md_cluster_stop(struct mddev *mddev);
8482b8bf345SNeilBrown extern struct md_thread *md_register_thread(
8494ed8731dSShaohua Li 	void (*run)(struct md_thread *thread),
8502b8bf345SNeilBrown 	struct mddev *mddev,
8512b8bf345SNeilBrown 	const char *name);
8527eb8ff02SLi Lingfeng extern void md_unregister_thread(struct mddev *mddev, struct md_thread __rcu **threadp);
85344693154SYu Kuai extern void md_wakeup_thread(struct md_thread __rcu *thread);
854fd01b88cSNeilBrown extern void md_check_recovery(struct mddev *mddev);
855d0a18034SGuoqing Jiang extern void md_reap_sync_thread(struct mddev *mddev);
856e792a4c2SYu Kuai extern enum sync_action md_sync_action(struct mddev *mddev);
857e792a4c2SYu Kuai extern enum sync_action md_sync_action_by_name(const char *page);
858e792a4c2SYu Kuai extern const char *md_sync_action_name(enum sync_action action);
85903e792eaSLi Nan extern void md_write_start(struct mddev *mddev, struct bio *bi);
86049728050SNeilBrown extern void md_write_inc(struct mddev *mddev, struct bio *bi);
861fd01b88cSNeilBrown extern void md_write_end(struct mddev *mddev);
862fd01b88cSNeilBrown extern void md_done_sync(struct mddev *mddev, int blocks, int ok);
863fd01b88cSNeilBrown extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
864c6563a8cSNeilBrown extern void md_finish_reshape(struct mddev *mddev);
865cf78408fSXiao Ni void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
866cf78408fSXiao Ni 			struct bio *bio, sector_t start, sector_t size);
86710764815SGuoqing Jiang void md_account_bio(struct mddev *mddev, struct bio **bio);
86841425f96SYu Kuai void md_free_cloned_bio(struct bio *bio);
86943b2e5d8SNeilBrown 
870775d7831SDavid Jeffery extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio);
871fd01b88cSNeilBrown extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
87243b2e5d8SNeilBrown 			   sector_t sector, int size, struct page *page);
87346533ff7SNeilBrown extern int md_super_wait(struct mddev *mddev);
8743cb03002SNeilBrown extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
8754ce4c73fSBart Van Assche 		struct page *page, blk_opf_t opf, bool metadata_op);
8764ed8731dSShaohua Li extern void md_do_sync(struct md_thread *thread);
87754679486SGuoqing Jiang extern void md_new_event(void);
8782214c260SArtur Paszkiewicz extern void md_allow_write(struct mddev *mddev);
879fd01b88cSNeilBrown extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev);
880fd01b88cSNeilBrown extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors);
881fd01b88cSNeilBrown extern int md_check_no_bitmap(struct mddev *mddev);
882fd01b88cSNeilBrown extern int md_integrity_register(struct mddev *mddev);
88372e02075SNeilBrown extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
88463fe0817SChristoph Hellwig 
885d58eff83SYu Kuai extern int mddev_init(struct mddev *mddev);
886d58eff83SYu Kuai extern void mddev_destroy(struct mddev *mddev);
887573d5abfSChristoph Hellwig void md_init_stacking_limits(struct queue_limits *lim);
88834cb92c0SChristoph Hellwig struct mddev *md_alloc(dev_t dev, char *name);
88934cb92c0SChristoph Hellwig void mddev_put(struct mddev *mddev);
890fd01b88cSNeilBrown extern int md_run(struct mddev *mddev);
891d5d885fdSSong Liu extern int md_start(struct mddev *mddev);
892fd01b88cSNeilBrown extern void md_stop(struct mddev *mddev);
893fd01b88cSNeilBrown extern void md_stop_writes(struct mddev *mddev);
8943cb03002SNeilBrown extern int md_rdev_init(struct md_rdev *rdev);
895545c8795SNeilBrown extern void md_rdev_clear(struct md_rdev *rdev);
896390ee602SNeilBrown 
89741425f96SYu Kuai extern bool md_handle_request(struct mddev *mddev, struct bio *bio);
8982b16a525SYu Kuai extern int mddev_suspend(struct mddev *mddev, bool interruptible);
899fd01b88cSNeilBrown extern void mddev_resume(struct mddev *mddev);
9007a2347e2SYu Kuai extern void md_idle_sync_thread(struct mddev *mddev);
9017a2347e2SYu Kuai extern void md_frozen_sync_thread(struct mddev *mddev);
9027a2347e2SYu Kuai extern void md_unfrozen_sync_thread(struct mddev *mddev);
9039cbb1750SNeilBrown 
90470bcecdbSGoldwyn Rodrigues extern void md_reload_sb(struct mddev *mddev, int raid_disk);
9051aee41f6SGoldwyn Rodrigues extern void md_update_sb(struct mddev *mddev, int force);
906b4128c00SYu Kuai extern void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev);
907b4128c00SYu Kuai extern void mddev_destroy_serial_pool(struct mddev *mddev,
908b4128c00SYu Kuai 				      struct md_rdev *rdev);
90957d051dcSGoldwyn Rodrigues struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
9101532d9e8STomasz Majchrzak struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
911dfe15ac1SHannes Reinecke 
is_rdev_broken(struct md_rdev * rdev)912c31fea2fSMariusz Tkaczyk static inline bool is_rdev_broken(struct md_rdev *rdev)
91362f7b198SGuilherme G. Piccoli {
914c31fea2fSMariusz Tkaczyk 	return !disk_live(rdev->bdev->bd_disk);
91562f7b198SGuilherme G. Piccoli }
91662f7b198SGuilherme G. Piccoli 
rdev_dec_pending(struct md_rdev * rdev,struct mddev * mddev)917dfe15ac1SHannes Reinecke static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev)
918dfe15ac1SHannes Reinecke {
919dfe15ac1SHannes Reinecke 	int faulty = test_bit(Faulty, &rdev->flags);
920dfe15ac1SHannes Reinecke 	if (atomic_dec_and_test(&rdev->nr_pending) && faulty) {
921dfe15ac1SHannes Reinecke 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
922dfe15ac1SHannes Reinecke 		md_wakeup_thread(mddev->thread);
923dfe15ac1SHannes Reinecke 	}
924dfe15ac1SHannes Reinecke }
925dfe15ac1SHannes Reinecke 
9261f4a72ffSChristophe JAILLET extern const struct md_cluster_operations *md_cluster_ops;
mddev_is_clustered(struct mddev * mddev)927c4ce867fSGoldwyn Rodrigues static inline int mddev_is_clustered(struct mddev *mddev)
928c4ce867fSGoldwyn Rodrigues {
929c4ce867fSGoldwyn Rodrigues 	return mddev->cluster_info && mddev->bitmap_info.nodes > 1;
930c4ce867fSGoldwyn Rodrigues }
931394ed8e4SShaohua Li 
932394ed8e4SShaohua Li /* clear unsupported mddev_flags */
mddev_clear_unsupported_flags(struct mddev * mddev,unsigned long unsupported_flags)933394ed8e4SShaohua Li static inline void mddev_clear_unsupported_flags(struct mddev *mddev,
934394ed8e4SShaohua Li 	unsigned long unsupported_flags)
935394ed8e4SShaohua Li {
936394ed8e4SShaohua Li 	mddev->flags &= ~unsupported_flags;
937394ed8e4SShaohua Li }
93826483819SShaohua Li 
mddev_check_write_zeroes(struct mddev * mddev,struct bio * bio)9393deff1a7SChristoph Hellwig static inline void mddev_check_write_zeroes(struct mddev *mddev, struct bio *bio)
9403deff1a7SChristoph Hellwig {
9413deff1a7SChristoph Hellwig 	if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
942309dca30SChristoph Hellwig 	    !bio->bi_bdev->bd_disk->queue->limits.max_write_zeroes_sectors)
943396799ebSChristoph Hellwig 		mddev->gendisk->queue->limits.max_write_zeroes_sectors = 0;
9443deff1a7SChristoph Hellwig }
945d82fa81cSChristoph Hellwig 
mddev_suspend_and_lock(struct mddev * mddev)946f45461e2SYu Kuai static inline int mddev_suspend_and_lock(struct mddev *mddev)
947f45461e2SYu Kuai {
948f45461e2SYu Kuai 	int ret;
949f45461e2SYu Kuai 
9502b16a525SYu Kuai 	ret = mddev_suspend(mddev, true);
951f45461e2SYu Kuai 	if (ret)
952f45461e2SYu Kuai 		return ret;
953f45461e2SYu Kuai 
954f45461e2SYu Kuai 	ret = mddev_lock(mddev);
955f45461e2SYu Kuai 	if (ret)
9562b16a525SYu Kuai 		mddev_resume(mddev);
957f45461e2SYu Kuai 
958f45461e2SYu Kuai 	return ret;
959f45461e2SYu Kuai }
960f45461e2SYu Kuai 
mddev_suspend_and_lock_nointr(struct mddev * mddev)961f45461e2SYu Kuai static inline void mddev_suspend_and_lock_nointr(struct mddev *mddev)
962f45461e2SYu Kuai {
9632b16a525SYu Kuai 	mddev_suspend(mddev, false);
964f45461e2SYu Kuai 	mutex_lock(&mddev->reconfig_mutex);
965f45461e2SYu Kuai }
966f45461e2SYu Kuai 
mddev_unlock_and_resume(struct mddev * mddev)967f45461e2SYu Kuai static inline void mddev_unlock_and_resume(struct mddev *mddev)
968f45461e2SYu Kuai {
969f45461e2SYu Kuai 	mddev_unlock(mddev);
9702b16a525SYu Kuai 	mddev_resume(mddev);
971f45461e2SYu Kuai }
972f45461e2SYu Kuai 
9737e0adbfcSChristoph Hellwig struct mdu_array_info_s;
9747e0adbfcSChristoph Hellwig struct mdu_disk_info_s;
9757e0adbfcSChristoph Hellwig 
9761a6a0506SChristoph Hellwig extern int mdp_major;
977a022325aSYu Kuai extern struct workqueue_struct *md_bitmap_wq;
978d82fa81cSChristoph Hellwig void md_autostart_arrays(int part);
9797e0adbfcSChristoph Hellwig int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info);
9807e0adbfcSChristoph Hellwig int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info);
9817e0adbfcSChristoph Hellwig int do_md_run(struct mddev *mddev);
982c6e56cf6SChristoph Hellwig #define MDDEV_STACK_INTEGRITY	(1u << 0)
983c6e56cf6SChristoph Hellwig int mddev_stack_rdev_limits(struct mddev *mddev, struct queue_limits *lim,
984c6e56cf6SChristoph Hellwig 		unsigned int flags);
985e305fce1SChristoph Hellwig int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev);
986e305fce1SChristoph Hellwig void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes);
9877e0adbfcSChristoph Hellwig 
9887e0adbfcSChristoph Hellwig extern const struct block_device_operations md_fops;
989d82fa81cSChristoph Hellwig 
990176df894SChristoph Hellwig /*
991176df894SChristoph Hellwig  * MD devices can be used undeneath by DM, in which case ->gendisk is NULL.
992176df894SChristoph Hellwig  */
mddev_is_dm(struct mddev * mddev)993176df894SChristoph Hellwig static inline bool mddev_is_dm(struct mddev *mddev)
994176df894SChristoph Hellwig {
995176df894SChristoph Hellwig 	return !mddev->gendisk;
996176df894SChristoph Hellwig }
997176df894SChristoph Hellwig 
mddev_trace_remap(struct mddev * mddev,struct bio * bio,sector_t sector)998c396b90eSChristoph Hellwig static inline void mddev_trace_remap(struct mddev *mddev, struct bio *bio,
999c396b90eSChristoph Hellwig 		sector_t sector)
1000c396b90eSChristoph Hellwig {
1001176df894SChristoph Hellwig 	if (!mddev_is_dm(mddev))
1002c396b90eSChristoph Hellwig 		trace_block_bio_remap(bio, disk_devt(mddev->gendisk), sector);
1003c396b90eSChristoph Hellwig }
1004c396b90eSChristoph Hellwig 
100528be4fd3SChristoph Hellwig #define mddev_add_trace_msg(mddev, fmt, args...)			\
100628be4fd3SChristoph Hellwig do {									\
1007176df894SChristoph Hellwig 	if (!mddev_is_dm(mddev))					\
1008396799ebSChristoph Hellwig 		blk_add_trace_msg((mddev)->gendisk->queue, fmt, ##args); \
100928be4fd3SChristoph Hellwig } while (0)
101028be4fd3SChristoph Hellwig 
101163fe0817SChristoph Hellwig #endif /* _MD_MD_H */
1012