1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef _MD_MD_H
16#define _MD_MD_H
17
18#include <linux/blkdev.h>
19#include <linux/backing-dev.h>
20#include <linux/badblocks.h>
21#include <linux/kobject.h>
22#include <linux/list.h>
23#include <linux/mm.h>
24#include <linux/mutex.h>
25#include <linux/timer.h>
26#include <linux/wait.h>
27#include <linux/workqueue.h>
28#include "md-cluster.h"
29
30#define MaxSector (~(sector_t)0)
31
32
33
34
35
36
37
38
39
40
41#define MD_FAILFAST (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT)
42
43
44
45struct md_rdev {
46 struct list_head same_set;
47
48 sector_t sectors;
49 struct mddev *mddev;
50 int last_events;
51
52
53
54
55
56
57 struct block_device *meta_bdev;
58 struct block_device *bdev;
59
60 struct page *sb_page, *bb_page;
61 int sb_loaded;
62 __u64 sb_events;
63 sector_t data_offset;
64 sector_t new_data_offset;
65 sector_t sb_start;
66 int sb_size;
67 int preferred_minor;
68
69 struct kobject kobj;
70
71
72
73
74
75
76
77
78
79
80
81
82 unsigned long flags;
83 wait_queue_head_t blocked_wait;
84
85 int desc_nr;
86 int raid_disk;
87 int new_raid_disk;
88
89
90 int saved_raid_disk;
91
92
93
94 union {
95 sector_t recovery_offset;
96
97
98
99 sector_t journal_tail;
100
101
102
103 };
104
105 atomic_t nr_pending;
106
107
108
109 atomic_t read_errors;
110
111
112 time64_t last_read_error;
113
114
115 atomic_t corrected_errors;
116
117
118
119 struct work_struct del_work;
120
121 struct kernfs_node *sysfs_state;
122
123
124 struct badblocks badblocks;
125
126 struct {
127 short offset;
128
129 unsigned int size;
130 sector_t sector;
131 } ppl;
132};
133enum flag_bits {
134 Faulty,
135 In_sync,
136 Bitmap_sync,
137
138
139
140
141 WriteMostly,
142 AutoDetected,
143 Blocked,
144
145
146
147 WriteErrorSeen,
148
149
150 FaultRecorded,
151
152
153
154
155
156 BlockedBadBlocks,
157
158
159
160
161
162
163
164
165
166 WantReplacement,
167
168
169
170
171 Replacement,
172
173
174
175 Candidate,
176
177
178
179 Journal,
180
181
182
183
184 ClusterRemove,
185 RemoveSynchronized,
186
187
188
189
190 ExternalBbl,
191
192
193 FailFast,
194
195
196
197
198
199 LastDev,
200
201
202
203};
204
205static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors,
206 sector_t *first_bad, int *bad_sectors)
207{
208 if (unlikely(rdev->badblocks.count)) {
209 int rv = badblocks_check(&rdev->badblocks, rdev->data_offset + s,
210 sectors,
211 first_bad, bad_sectors);
212 if (rv)
213 *first_bad -= rdev->data_offset;
214 return rv;
215 }
216 return 0;
217}
218extern int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
219 int is_new);
220extern int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
221 int is_new);
222struct md_cluster_info;
223
224
225enum mddev_flags {
226 MD_ARRAY_FIRST_USE,
227 MD_CLOSING,
228
229 MD_JOURNAL_CLEAN,
230 MD_HAS_JOURNAL,
231 MD_CLUSTER_RESYNC_LOCKED,
232
233
234 MD_FAILFAST_SUPPORTED,
235
236
237
238 MD_HAS_PPL,
239 MD_HAS_MULTIPLE_PPLS,
240 MD_ALLOW_SB_UPDATE,
241
242
243 MD_UPDATING_SB,
244
245
246};
247
248enum mddev_sb_flags {
249 MD_SB_CHANGE_DEVS,
250 MD_SB_CHANGE_CLEAN,
251 MD_SB_CHANGE_PENDING,
252 MD_SB_NEED_REWRITE,
253};
254
255#define NR_FLUSH_INFOS 8
256#define NR_FLUSH_BIOS 64
257struct flush_info {
258 struct bio *bio;
259 struct mddev *mddev;
260 struct work_struct flush_work;
261 atomic_t flush_pending;
262};
263struct flush_bio {
264 struct flush_info *fi;
265 struct md_rdev *rdev;
266};
267
268struct mddev {
269 void *private;
270 struct md_personality *pers;
271 dev_t unit;
272 int md_minor;
273 struct list_head disks;
274 unsigned long flags;
275 unsigned long sb_flags;
276
277 int suspended;
278 atomic_t active_io;
279 int ro;
280 int sysfs_active;
281
282
283
284 struct gendisk *gendisk;
285
286 struct kobject kobj;
287 int hold_active;
288#define UNTIL_IOCTL 1
289#define UNTIL_STOP 2
290
291
292 int major_version,
293 minor_version,
294 patch_version;
295 int persistent;
296 int external;
297
298 char metadata_type[17];
299 int chunk_sectors;
300 time64_t ctime, utime;
301 int level, layout;
302 char clevel[16];
303 int raid_disks;
304 int max_disks;
305 sector_t dev_sectors;
306
307 sector_t array_sectors;
308 int external_size;
309
310 __u64 events;
311
312
313
314
315
316 int can_decrease_events;
317
318 char uuid[16];
319
320
321
322
323
324
325 sector_t reshape_position;
326 int delta_disks, new_level, new_layout;
327 int new_chunk_sectors;
328 int reshape_backwards;
329
330 struct md_thread *thread;
331 struct md_thread *sync_thread;
332
333
334
335
336
337
338
339 char *last_sync_action;
340 sector_t curr_resync;
341
342
343
344
345
346
347 sector_t curr_resync_completed;
348 unsigned long resync_mark;
349 sector_t resync_mark_cnt;
350 sector_t curr_mark_cnt;
351
352 sector_t resync_max_sectors;
353
354 atomic64_t resync_mismatches;
355
356
357
358
359 sector_t suspend_lo;
360 sector_t suspend_hi;
361
362 int sync_speed_min;
363 int sync_speed_max;
364
365
366 int parallel_resync;
367
368 int ok_start_degraded;
369
370 unsigned long recovery;
371
372
373
374
375
376 int recovery_disabled;
377
378 int in_sync;
379
380
381
382
383
384
385
386
387
388 struct mutex open_mutex;
389 struct mutex reconfig_mutex;
390 atomic_t active;
391 atomic_t openers;
392
393 int changed;
394
395 int degraded;
396
397
398
399 atomic_t recovery_active;
400 wait_queue_head_t recovery_wait;
401 sector_t recovery_cp;
402 sector_t resync_min;
403
404 sector_t resync_max;
405
406
407 struct kernfs_node *sysfs_state;
408
409
410 struct kernfs_node *sysfs_action;
411
412 struct work_struct del_work;
413
414
415
416
417
418
419
420
421
422
423
424
425 spinlock_t lock;
426 wait_queue_head_t sb_wait;
427 atomic_t pending_writes;
428
429 unsigned int safemode;
430
431
432 unsigned int safemode_delay;
433 struct timer_list safemode_timer;
434 struct percpu_ref writes_pending;
435 int sync_checkers;
436 struct request_queue *queue;
437
438 struct bitmap *bitmap;
439 struct {
440 struct file *file;
441 loff_t offset;
442
443
444
445
446
447 unsigned long space;
448 loff_t default_offset;
449
450
451
452 unsigned long default_space;
453
454 struct mutex mutex;
455 unsigned long chunksize;
456 unsigned long daemon_sleep;
457 unsigned long max_write_behind;
458 int external;
459 int nodes;
460 char cluster_name[64];
461 } bitmap_info;
462
463 atomic_t max_corr_read_errors;
464 struct list_head all_mddevs;
465
466 struct attribute_group *to_remove;
467
468 struct bio_set bio_set;
469 struct bio_set sync_set;
470
471
472
473 mempool_t *flush_pool;
474 mempool_t *flush_bio_pool;
475 struct work_struct event_work;
476 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
477 struct md_cluster_info *cluster_info;
478 unsigned int good_device_nr;
479
480 bool has_superblocks:1;
481};
482
483enum recovery_flags {
484
485
486
487 MD_RECOVERY_RUNNING,
488 MD_RECOVERY_SYNC,
489 MD_RECOVERY_RECOVER,
490 MD_RECOVERY_INTR,
491 MD_RECOVERY_DONE,
492 MD_RECOVERY_NEEDED,
493 MD_RECOVERY_REQUESTED,
494 MD_RECOVERY_CHECK,
495 MD_RECOVERY_RESHAPE,
496 MD_RECOVERY_FROZEN,
497 MD_RECOVERY_ERROR,
498 MD_RECOVERY_WAIT,
499 MD_RESYNCING_REMOTE,
500};
501
502static inline int __must_check mddev_lock(struct mddev *mddev)
503{
504 return mutex_lock_interruptible(&mddev->reconfig_mutex);
505}
506
507
508
509
510static inline void mddev_lock_nointr(struct mddev *mddev)
511{
512 mutex_lock(&mddev->reconfig_mutex);
513}
514
515static inline int mddev_trylock(struct mddev *mddev)
516{
517 return mutex_trylock(&mddev->reconfig_mutex);
518}
519extern void mddev_unlock(struct mddev *mddev);
520
521static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
522{
523 atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io);
524}
525
526static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
527{
528 atomic_add(nr_sectors, &bio->bi_disk->sync_io);
529}
530
531struct md_personality
532{
533 char *name;
534 int level;
535 struct list_head list;
536 struct module *owner;
537 bool (*make_request)(struct mddev *mddev, struct bio *bio);
538
539
540
541
542 int (*run)(struct mddev *mddev);
543
544 int (*start)(struct mddev *mddev);
545 void (*free)(struct mddev *mddev, void *priv);
546 void (*status)(struct seq_file *seq, struct mddev *mddev);
547
548
549
550 void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev);
551 int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev);
552 int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev);
553 int (*spare_active) (struct mddev *mddev);
554 sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr, int *skipped);
555 int (*resize) (struct mddev *mddev, sector_t sectors);
556 sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks);
557 int (*check_reshape) (struct mddev *mddev);
558 int (*start_reshape) (struct mddev *mddev);
559 void (*finish_reshape) (struct mddev *mddev);
560 void (*update_reshape_pos) (struct mddev *mddev);
561
562
563
564
565 void (*quiesce) (struct mddev *mddev, int quiesce);
566
567
568
569
570
571
572
573
574
575 void *(*takeover) (struct mddev *mddev);
576
577
578 int (*congested)(struct mddev *mddev, int bits);
579
580 int (*change_consistency_policy)(struct mddev *mddev, const char *buf);
581};
582
583struct md_sysfs_entry {
584 struct attribute attr;
585 ssize_t (*show)(struct mddev *, char *);
586 ssize_t (*store)(struct mddev *, const char *, size_t);
587};
588extern struct attribute_group md_bitmap_group;
589
590static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name)
591{
592 if (sd)
593 return sysfs_get_dirent(sd, name);
594 return sd;
595}
596static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
597{
598 if (sd)
599 sysfs_notify_dirent(sd);
600}
601
602static inline char * mdname (struct mddev * mddev)
603{
604 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
605}
606
607static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev)
608{
609 char nm[20];
610 if (!test_bit(Replacement, &rdev->flags) &&
611 !test_bit(Journal, &rdev->flags) &&
612 mddev->kobj.sd) {
613 sprintf(nm, "rd%d", rdev->raid_disk);
614 return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
615 } else
616 return 0;
617}
618
619static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev)
620{
621 char nm[20];
622 if (!test_bit(Replacement, &rdev->flags) &&
623 !test_bit(Journal, &rdev->flags) &&
624 mddev->kobj.sd) {
625 sprintf(nm, "rd%d", rdev->raid_disk);
626 sysfs_remove_link(&mddev->kobj, nm);
627 }
628}
629
630
631
632
633
634#define rdev_for_each_list(rdev, tmp, head) \
635 list_for_each_entry_safe(rdev, tmp, head, same_set)
636
637
638
639
640#define rdev_for_each(rdev, mddev) \
641 list_for_each_entry(rdev, &((mddev)->disks), same_set)
642
643#define rdev_for_each_safe(rdev, tmp, mddev) \
644 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
645
646#define rdev_for_each_rcu(rdev, mddev) \
647 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
648
649struct md_thread {
650 void (*run) (struct md_thread *thread);
651 struct mddev *mddev;
652 wait_queue_head_t wqueue;
653 unsigned long flags;
654 struct task_struct *tsk;
655 unsigned long timeout;
656 void *private;
657};
658
659#define THREAD_WAKEUP 0
660
661static inline void safe_put_page(struct page *p)
662{
663 if (p) put_page(p);
664}
665
666extern int register_md_personality(struct md_personality *p);
667extern int unregister_md_personality(struct md_personality *p);
668extern int register_md_cluster_operations(struct md_cluster_operations *ops,
669 struct module *module);
670extern int unregister_md_cluster_operations(void);
671extern int md_setup_cluster(struct mddev *mddev, int nodes);
672extern void md_cluster_stop(struct mddev *mddev);
673extern struct md_thread *md_register_thread(
674 void (*run)(struct md_thread *thread),
675 struct mddev *mddev,
676 const char *name);
677extern void md_unregister_thread(struct md_thread **threadp);
678extern void md_wakeup_thread(struct md_thread *thread);
679extern void md_check_recovery(struct mddev *mddev);
680extern void md_reap_sync_thread(struct mddev *mddev);
681extern int mddev_init_writes_pending(struct mddev *mddev);
682extern bool md_write_start(struct mddev *mddev, struct bio *bi);
683extern void md_write_inc(struct mddev *mddev, struct bio *bi);
684extern void md_write_end(struct mddev *mddev);
685extern void md_done_sync(struct mddev *mddev, int blocks, int ok);
686extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
687extern void md_finish_reshape(struct mddev *mddev);
688
689extern int mddev_congested(struct mddev *mddev, int bits);
690extern void md_flush_request(struct mddev *mddev, struct bio *bio);
691extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
692 sector_t sector, int size, struct page *page);
693extern int md_super_wait(struct mddev *mddev);
694extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
695 struct page *page, int op, int op_flags,
696 bool metadata_op);
697extern void md_do_sync(struct md_thread *thread);
698extern void md_new_event(struct mddev *mddev);
699extern void md_allow_write(struct mddev *mddev);
700extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev);
701extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors);
702extern int md_check_no_bitmap(struct mddev *mddev);
703extern int md_integrity_register(struct mddev *mddev);
704extern int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev);
705extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
706
707extern void mddev_init(struct mddev *mddev);
708extern int md_run(struct mddev *mddev);
709extern int md_start(struct mddev *mddev);
710extern void md_stop(struct mddev *mddev);
711extern void md_stop_writes(struct mddev *mddev);
712extern int md_rdev_init(struct md_rdev *rdev);
713extern void md_rdev_clear(struct md_rdev *rdev);
714
715extern void md_handle_request(struct mddev *mddev, struct bio *bio);
716extern void mddev_suspend(struct mddev *mddev);
717extern void mddev_resume(struct mddev *mddev);
718extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
719 struct mddev *mddev);
720
721extern void md_reload_sb(struct mddev *mddev, int raid_disk);
722extern void md_update_sb(struct mddev *mddev, int force);
723extern void md_kick_rdev_from_array(struct md_rdev * rdev);
724struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
725struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
726
727static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev)
728{
729 int faulty = test_bit(Faulty, &rdev->flags);
730 if (atomic_dec_and_test(&rdev->nr_pending) && faulty) {
731 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
732 md_wakeup_thread(mddev->thread);
733 }
734}
735
736extern struct md_cluster_operations *md_cluster_ops;
737static inline int mddev_is_clustered(struct mddev *mddev)
738{
739 return mddev->cluster_info && mddev->bitmap_info.nodes > 1;
740}
741
742
743static inline void mddev_clear_unsupported_flags(struct mddev *mddev,
744 unsigned long unsupported_flags)
745{
746 mddev->flags &= ~unsupported_flags;
747}
748
749static inline void mddev_check_writesame(struct mddev *mddev, struct bio *bio)
750{
751 if (bio_op(bio) == REQ_OP_WRITE_SAME &&
752 !bio->bi_disk->queue->limits.max_write_same_sectors)
753 mddev->queue->limits.max_write_same_sectors = 0;
754}
755
756static inline void mddev_check_write_zeroes(struct mddev *mddev, struct bio *bio)
757{
758 if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
759 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
760 mddev->queue->limits.max_write_zeroes_sectors = 0;
761}
762#endif
763