]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/md/raid1.c
md: raid1/raid10: initialize bvec table via bio_add_page()
[karo-tx-linux.git] / drivers / md / raid1.c
1 /*
2  * raid1.c : Multiple Devices driver for Linux
3  *
4  * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5  *
6  * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7  *
8  * RAID-1 management functions.
9  *
10  * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11  *
12  * Fixes to reconstruction by Jakob Ã˜stergaard" <jakob@ostenfeld.dk>
13  * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14  *
15  * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16  * bitmapped intelligence in resync:
17  *
18  *      - bitmap marked during normal i/o
19  *      - bitmap used to skip nondirty blocks during sync
20  *
21  * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22  * - persistent bitmap code
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2, or (at your option)
27  * any later version.
28  *
29  * You should have received a copy of the GNU General Public License
30  * (for example /usr/src/linux/COPYING); if not, write to the Free
31  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/module.h>
38 #include <linux/seq_file.h>
39 #include <linux/ratelimit.h>
40 #include <linux/sched/signal.h>
41
42 #include <trace/events/block.h>
43
44 #include "md.h"
45 #include "raid1.h"
46 #include "bitmap.h"
47
48 #define UNSUPPORTED_MDDEV_FLAGS         \
49         ((1L << MD_HAS_JOURNAL) |       \
50          (1L << MD_JOURNAL_CLEAN) |     \
51          (1L << MD_HAS_PPL))
52
53 /*
54  * Number of guaranteed r1bios in case of extreme VM load:
55  */
56 #define NR_RAID1_BIOS 256
57
58 /* when we get a read error on a read-only array, we redirect to another
59  * device without failing the first device, or trying to over-write to
60  * correct the read error.  To keep track of bad blocks on a per-bio
61  * level, we store IO_BLOCKED in the appropriate 'bios' pointer
62  */
63 #define IO_BLOCKED ((struct bio *)1)
64 /* When we successfully write to a known bad-block, we need to remove the
65  * bad-block marking which must be done from process context.  So we record
66  * the success by setting devs[n].bio to IO_MADE_GOOD
67  */
68 #define IO_MADE_GOOD ((struct bio *)2)
69
70 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
71
72 /* When there are this many requests queue to be written by
73  * the raid1 thread, we become 'congested' to provide back-pressure
74  * for writeback.
75  */
76 static int max_queued_requests = 1024;
77
78 static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
79 static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
80
81 #define raid1_log(md, fmt, args...)                             \
82         do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
83
84 #include "raid1-10.c"
85
86 /*
87  * 'strct resync_pages' stores actual pages used for doing the resync
88  *  IO, and it is per-bio, so make .bi_private points to it.
89  */
90 static inline struct resync_pages *get_resync_pages(struct bio *bio)
91 {
92         return bio->bi_private;
93 }
94
95 /*
96  * for resync bio, r1bio pointer can be retrieved from the per-bio
97  * 'struct resync_pages'.
98  */
99 static inline struct r1bio *get_resync_r1bio(struct bio *bio)
100 {
101         return get_resync_pages(bio)->raid_bio;
102 }
103
104 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
105 {
106         struct pool_info *pi = data;
107         int size = offsetof(struct r1bio, bios[pi->raid_disks]);
108
109         /* allocate a r1bio with room for raid_disks entries in the bios array */
110         return kzalloc(size, gfp_flags);
111 }
112
113 static void r1bio_pool_free(void *r1_bio, void *data)
114 {
115         kfree(r1_bio);
116 }
117
118 #define RESYNC_DEPTH 32
119 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
120 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
121 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
122 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
123 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
124
125 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
126 {
127         struct pool_info *pi = data;
128         struct r1bio *r1_bio;
129         struct bio *bio;
130         int need_pages;
131         int j;
132         struct resync_pages *rps;
133
134         r1_bio = r1bio_pool_alloc(gfp_flags, pi);
135         if (!r1_bio)
136                 return NULL;
137
138         rps = kmalloc(sizeof(struct resync_pages) * pi->raid_disks,
139                       gfp_flags);
140         if (!rps)
141                 goto out_free_r1bio;
142
143         /*
144          * Allocate bios : 1 for reading, n-1 for writing
145          */
146         for (j = pi->raid_disks ; j-- ; ) {
147                 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
148                 if (!bio)
149                         goto out_free_bio;
150                 r1_bio->bios[j] = bio;
151         }
152         /*
153          * Allocate RESYNC_PAGES data pages and attach them to
154          * the first bio.
155          * If this is a user-requested check/repair, allocate
156          * RESYNC_PAGES for each bio.
157          */
158         if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
159                 need_pages = pi->raid_disks;
160         else
161                 need_pages = 1;
162         for (j = 0; j < pi->raid_disks; j++) {
163                 struct resync_pages *rp = &rps[j];
164
165                 bio = r1_bio->bios[j];
166
167                 if (j < need_pages) {
168                         if (resync_alloc_pages(rp, gfp_flags))
169                                 goto out_free_pages;
170                 } else {
171                         memcpy(rp, &rps[0], sizeof(*rp));
172                         resync_get_all_pages(rp);
173                 }
174
175                 rp->raid_bio = r1_bio;
176                 bio->bi_private = rp;
177         }
178
179         r1_bio->master_bio = NULL;
180
181         return r1_bio;
182
183 out_free_pages:
184         while (--j >= 0)
185                 resync_free_pages(&rps[j]);
186
187 out_free_bio:
188         while (++j < pi->raid_disks)
189                 bio_put(r1_bio->bios[j]);
190         kfree(rps);
191
192 out_free_r1bio:
193         r1bio_pool_free(r1_bio, data);
194         return NULL;
195 }
196
197 static void r1buf_pool_free(void *__r1_bio, void *data)
198 {
199         struct pool_info *pi = data;
200         int i;
201         struct r1bio *r1bio = __r1_bio;
202         struct resync_pages *rp = NULL;
203
204         for (i = pi->raid_disks; i--; ) {
205                 rp = get_resync_pages(r1bio->bios[i]);
206                 resync_free_pages(rp);
207                 bio_put(r1bio->bios[i]);
208         }
209
210         /* resync pages array stored in the 1st bio's .bi_private */
211         kfree(rp);
212
213         r1bio_pool_free(r1bio, data);
214 }
215
216 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
217 {
218         int i;
219
220         for (i = 0; i < conf->raid_disks * 2; i++) {
221                 struct bio **bio = r1_bio->bios + i;
222                 if (!BIO_SPECIAL(*bio))
223                         bio_put(*bio);
224                 *bio = NULL;
225         }
226 }
227
228 static void free_r1bio(struct r1bio *r1_bio)
229 {
230         struct r1conf *conf = r1_bio->mddev->private;
231
232         put_all_bios(conf, r1_bio);
233         mempool_free(r1_bio, conf->r1bio_pool);
234 }
235
236 static void put_buf(struct r1bio *r1_bio)
237 {
238         struct r1conf *conf = r1_bio->mddev->private;
239         sector_t sect = r1_bio->sector;
240         int i;
241
242         for (i = 0; i < conf->raid_disks * 2; i++) {
243                 struct bio *bio = r1_bio->bios[i];
244                 if (bio->bi_end_io)
245                         rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
246         }
247
248         mempool_free(r1_bio, conf->r1buf_pool);
249
250         lower_barrier(conf, sect);
251 }
252
253 static void reschedule_retry(struct r1bio *r1_bio)
254 {
255         unsigned long flags;
256         struct mddev *mddev = r1_bio->mddev;
257         struct r1conf *conf = mddev->private;
258         int idx;
259
260         idx = sector_to_idx(r1_bio->sector);
261         spin_lock_irqsave(&conf->device_lock, flags);
262         list_add(&r1_bio->retry_list, &conf->retry_list);
263         atomic_inc(&conf->nr_queued[idx]);
264         spin_unlock_irqrestore(&conf->device_lock, flags);
265
266         wake_up(&conf->wait_barrier);
267         md_wakeup_thread(mddev->thread);
268 }
269
270 /*
271  * raid_end_bio_io() is called when we have finished servicing a mirrored
272  * operation and are ready to return a success/failure code to the buffer
273  * cache layer.
274  */
275 static void call_bio_endio(struct r1bio *r1_bio)
276 {
277         struct bio *bio = r1_bio->master_bio;
278         struct r1conf *conf = r1_bio->mddev->private;
279
280         if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
281                 bio->bi_status = BLK_STS_IOERR;
282
283         bio_endio(bio);
284         /*
285          * Wake up any possible resync thread that waits for the device
286          * to go idle.
287          */
288         allow_barrier(conf, r1_bio->sector);
289 }
290
291 static void raid_end_bio_io(struct r1bio *r1_bio)
292 {
293         struct bio *bio = r1_bio->master_bio;
294
295         /* if nobody has done the final endio yet, do it now */
296         if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
297                 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
298                          (bio_data_dir(bio) == WRITE) ? "write" : "read",
299                          (unsigned long long) bio->bi_iter.bi_sector,
300                          (unsigned long long) bio_end_sector(bio) - 1);
301
302                 call_bio_endio(r1_bio);
303         }
304         free_r1bio(r1_bio);
305 }
306
307 /*
308  * Update disk head position estimator based on IRQ completion info.
309  */
310 static inline void update_head_pos(int disk, struct r1bio *r1_bio)
311 {
312         struct r1conf *conf = r1_bio->mddev->private;
313
314         conf->mirrors[disk].head_position =
315                 r1_bio->sector + (r1_bio->sectors);
316 }
317
318 /*
319  * Find the disk number which triggered given bio
320  */
321 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
322 {
323         int mirror;
324         struct r1conf *conf = r1_bio->mddev->private;
325         int raid_disks = conf->raid_disks;
326
327         for (mirror = 0; mirror < raid_disks * 2; mirror++)
328                 if (r1_bio->bios[mirror] == bio)
329                         break;
330
331         BUG_ON(mirror == raid_disks * 2);
332         update_head_pos(mirror, r1_bio);
333
334         return mirror;
335 }
336
337 static void raid1_end_read_request(struct bio *bio)
338 {
339         int uptodate = !bio->bi_status;
340         struct r1bio *r1_bio = bio->bi_private;
341         struct r1conf *conf = r1_bio->mddev->private;
342         struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
343
344         /*
345          * this branch is our 'one mirror IO has finished' event handler:
346          */
347         update_head_pos(r1_bio->read_disk, r1_bio);
348
349         if (uptodate)
350                 set_bit(R1BIO_Uptodate, &r1_bio->state);
351         else if (test_bit(FailFast, &rdev->flags) &&
352                  test_bit(R1BIO_FailFast, &r1_bio->state))
353                 /* This was a fail-fast read so we definitely
354                  * want to retry */
355                 ;
356         else {
357                 /* If all other devices have failed, we want to return
358                  * the error upwards rather than fail the last device.
359                  * Here we redefine "uptodate" to mean "Don't want to retry"
360                  */
361                 unsigned long flags;
362                 spin_lock_irqsave(&conf->device_lock, flags);
363                 if (r1_bio->mddev->degraded == conf->raid_disks ||
364                     (r1_bio->mddev->degraded == conf->raid_disks-1 &&
365                      test_bit(In_sync, &rdev->flags)))
366                         uptodate = 1;
367                 spin_unlock_irqrestore(&conf->device_lock, flags);
368         }
369
370         if (uptodate) {
371                 raid_end_bio_io(r1_bio);
372                 rdev_dec_pending(rdev, conf->mddev);
373         } else {
374                 /*
375                  * oops, read error:
376                  */
377                 char b[BDEVNAME_SIZE];
378                 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
379                                    mdname(conf->mddev),
380                                    bdevname(rdev->bdev, b),
381                                    (unsigned long long)r1_bio->sector);
382                 set_bit(R1BIO_ReadError, &r1_bio->state);
383                 reschedule_retry(r1_bio);
384                 /* don't drop the reference on read_disk yet */
385         }
386 }
387
388 static void close_write(struct r1bio *r1_bio)
389 {
390         /* it really is the end of this request */
391         if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
392                 bio_free_pages(r1_bio->behind_master_bio);
393                 bio_put(r1_bio->behind_master_bio);
394                 r1_bio->behind_master_bio = NULL;
395         }
396         /* clear the bitmap if all writes complete successfully */
397         bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
398                         r1_bio->sectors,
399                         !test_bit(R1BIO_Degraded, &r1_bio->state),
400                         test_bit(R1BIO_BehindIO, &r1_bio->state));
401         md_write_end(r1_bio->mddev);
402 }
403
404 static void r1_bio_write_done(struct r1bio *r1_bio)
405 {
406         if (!atomic_dec_and_test(&r1_bio->remaining))
407                 return;
408
409         if (test_bit(R1BIO_WriteError, &r1_bio->state))
410                 reschedule_retry(r1_bio);
411         else {
412                 close_write(r1_bio);
413                 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
414                         reschedule_retry(r1_bio);
415                 else
416                         raid_end_bio_io(r1_bio);
417         }
418 }
419
420 static void raid1_end_write_request(struct bio *bio)
421 {
422         struct r1bio *r1_bio = bio->bi_private;
423         int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
424         struct r1conf *conf = r1_bio->mddev->private;
425         struct bio *to_put = NULL;
426         int mirror = find_bio_disk(r1_bio, bio);
427         struct md_rdev *rdev = conf->mirrors[mirror].rdev;
428         bool discard_error;
429
430         discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
431
432         /*
433          * 'one mirror IO has finished' event handler:
434          */
435         if (bio->bi_status && !discard_error) {
436                 set_bit(WriteErrorSeen, &rdev->flags);
437                 if (!test_and_set_bit(WantReplacement, &rdev->flags))
438                         set_bit(MD_RECOVERY_NEEDED, &
439                                 conf->mddev->recovery);
440
441                 if (test_bit(FailFast, &rdev->flags) &&
442                     (bio->bi_opf & MD_FAILFAST) &&
443                     /* We never try FailFast to WriteMostly devices */
444                     !test_bit(WriteMostly, &rdev->flags)) {
445                         md_error(r1_bio->mddev, rdev);
446                         if (!test_bit(Faulty, &rdev->flags))
447                                 /* This is the only remaining device,
448                                  * We need to retry the write without
449                                  * FailFast
450                                  */
451                                 set_bit(R1BIO_WriteError, &r1_bio->state);
452                         else {
453                                 /* Finished with this branch */
454                                 r1_bio->bios[mirror] = NULL;
455                                 to_put = bio;
456                         }
457                 } else
458                         set_bit(R1BIO_WriteError, &r1_bio->state);
459         } else {
460                 /*
461                  * Set R1BIO_Uptodate in our master bio, so that we
462                  * will return a good error code for to the higher
463                  * levels even if IO on some other mirrored buffer
464                  * fails.
465                  *
466                  * The 'master' represents the composite IO operation
467                  * to user-side. So if something waits for IO, then it
468                  * will wait for the 'master' bio.
469                  */
470                 sector_t first_bad;
471                 int bad_sectors;
472
473                 r1_bio->bios[mirror] = NULL;
474                 to_put = bio;
475                 /*
476                  * Do not set R1BIO_Uptodate if the current device is
477                  * rebuilding or Faulty. This is because we cannot use
478                  * such device for properly reading the data back (we could
479                  * potentially use it, if the current write would have felt
480                  * before rdev->recovery_offset, but for simplicity we don't
481                  * check this here.
482                  */
483                 if (test_bit(In_sync, &rdev->flags) &&
484                     !test_bit(Faulty, &rdev->flags))
485                         set_bit(R1BIO_Uptodate, &r1_bio->state);
486
487                 /* Maybe we can clear some bad blocks. */
488                 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
489                                 &first_bad, &bad_sectors) && !discard_error) {
490                         r1_bio->bios[mirror] = IO_MADE_GOOD;
491                         set_bit(R1BIO_MadeGood, &r1_bio->state);
492                 }
493         }
494
495         if (behind) {
496                 /* we release behind master bio when all write are done */
497                 if (r1_bio->behind_master_bio == bio)
498                         to_put = NULL;
499
500                 if (test_bit(WriteMostly, &rdev->flags))
501                         atomic_dec(&r1_bio->behind_remaining);
502
503                 /*
504                  * In behind mode, we ACK the master bio once the I/O
505                  * has safely reached all non-writemostly
506                  * disks. Setting the Returned bit ensures that this
507                  * gets done only once -- we don't ever want to return
508                  * -EIO here, instead we'll wait
509                  */
510                 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
511                     test_bit(R1BIO_Uptodate, &r1_bio->state)) {
512                         /* Maybe we can return now */
513                         if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
514                                 struct bio *mbio = r1_bio->master_bio;
515                                 pr_debug("raid1: behind end write sectors"
516                                          " %llu-%llu\n",
517                                          (unsigned long long) mbio->bi_iter.bi_sector,
518                                          (unsigned long long) bio_end_sector(mbio) - 1);
519                                 call_bio_endio(r1_bio);
520                         }
521                 }
522         }
523         if (r1_bio->bios[mirror] == NULL)
524                 rdev_dec_pending(rdev, conf->mddev);
525
526         /*
527          * Let's see if all mirrored write operations have finished
528          * already.
529          */
530         r1_bio_write_done(r1_bio);
531
532         if (to_put)
533                 bio_put(to_put);
534 }
535
536 static sector_t align_to_barrier_unit_end(sector_t start_sector,
537                                           sector_t sectors)
538 {
539         sector_t len;
540
541         WARN_ON(sectors == 0);
542         /*
543          * len is the number of sectors from start_sector to end of the
544          * barrier unit which start_sector belongs to.
545          */
546         len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
547               start_sector;
548
549         if (len > sectors)
550                 len = sectors;
551
552         return len;
553 }
554
555 /*
556  * This routine returns the disk from which the requested read should
557  * be done. There is a per-array 'next expected sequential IO' sector
558  * number - if this matches on the next IO then we use the last disk.
559  * There is also a per-disk 'last know head position' sector that is
560  * maintained from IRQ contexts, both the normal and the resync IO
561  * completion handlers update this position correctly. If there is no
562  * perfect sequential match then we pick the disk whose head is closest.
563  *
564  * If there are 2 mirrors in the same 2 devices, performance degrades
565  * because position is mirror, not device based.
566  *
567  * The rdev for the device selected will have nr_pending incremented.
568  */
569 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
570 {
571         const sector_t this_sector = r1_bio->sector;
572         int sectors;
573         int best_good_sectors;
574         int best_disk, best_dist_disk, best_pending_disk;
575         int has_nonrot_disk;
576         int disk;
577         sector_t best_dist;
578         unsigned int min_pending;
579         struct md_rdev *rdev;
580         int choose_first;
581         int choose_next_idle;
582
583         rcu_read_lock();
584         /*
585          * Check if we can balance. We can balance on the whole
586          * device if no resync is going on, or below the resync window.
587          * We take the first readable disk when above the resync window.
588          */
589  retry:
590         sectors = r1_bio->sectors;
591         best_disk = -1;
592         best_dist_disk = -1;
593         best_dist = MaxSector;
594         best_pending_disk = -1;
595         min_pending = UINT_MAX;
596         best_good_sectors = 0;
597         has_nonrot_disk = 0;
598         choose_next_idle = 0;
599         clear_bit(R1BIO_FailFast, &r1_bio->state);
600
601         if ((conf->mddev->recovery_cp < this_sector + sectors) ||
602             (mddev_is_clustered(conf->mddev) &&
603             md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
604                     this_sector + sectors)))
605                 choose_first = 1;
606         else
607                 choose_first = 0;
608
609         for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
610                 sector_t dist;
611                 sector_t first_bad;
612                 int bad_sectors;
613                 unsigned int pending;
614                 bool nonrot;
615
616                 rdev = rcu_dereference(conf->mirrors[disk].rdev);
617                 if (r1_bio->bios[disk] == IO_BLOCKED
618                     || rdev == NULL
619                     || test_bit(Faulty, &rdev->flags))
620                         continue;
621                 if (!test_bit(In_sync, &rdev->flags) &&
622                     rdev->recovery_offset < this_sector + sectors)
623                         continue;
624                 if (test_bit(WriteMostly, &rdev->flags)) {
625                         /* Don't balance among write-mostly, just
626                          * use the first as a last resort */
627                         if (best_dist_disk < 0) {
628                                 if (is_badblock(rdev, this_sector, sectors,
629                                                 &first_bad, &bad_sectors)) {
630                                         if (first_bad <= this_sector)
631                                                 /* Cannot use this */
632                                                 continue;
633                                         best_good_sectors = first_bad - this_sector;
634                                 } else
635                                         best_good_sectors = sectors;
636                                 best_dist_disk = disk;
637                                 best_pending_disk = disk;
638                         }
639                         continue;
640                 }
641                 /* This is a reasonable device to use.  It might
642                  * even be best.
643                  */
644                 if (is_badblock(rdev, this_sector, sectors,
645                                 &first_bad, &bad_sectors)) {
646                         if (best_dist < MaxSector)
647                                 /* already have a better device */
648                                 continue;
649                         if (first_bad <= this_sector) {
650                                 /* cannot read here. If this is the 'primary'
651                                  * device, then we must not read beyond
652                                  * bad_sectors from another device..
653                                  */
654                                 bad_sectors -= (this_sector - first_bad);
655                                 if (choose_first && sectors > bad_sectors)
656                                         sectors = bad_sectors;
657                                 if (best_good_sectors > sectors)
658                                         best_good_sectors = sectors;
659
660                         } else {
661                                 sector_t good_sectors = first_bad - this_sector;
662                                 if (good_sectors > best_good_sectors) {
663                                         best_good_sectors = good_sectors;
664                                         best_disk = disk;
665                                 }
666                                 if (choose_first)
667                                         break;
668                         }
669                         continue;
670                 } else {
671                         if ((sectors > best_good_sectors) && (best_disk >= 0))
672                                 best_disk = -1;
673                         best_good_sectors = sectors;
674                 }
675
676                 if (best_disk >= 0)
677                         /* At least two disks to choose from so failfast is OK */
678                         set_bit(R1BIO_FailFast, &r1_bio->state);
679
680                 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
681                 has_nonrot_disk |= nonrot;
682                 pending = atomic_read(&rdev->nr_pending);
683                 dist = abs(this_sector - conf->mirrors[disk].head_position);
684                 if (choose_first) {
685                         best_disk = disk;
686                         break;
687                 }
688                 /* Don't change to another disk for sequential reads */
689                 if (conf->mirrors[disk].next_seq_sect == this_sector
690                     || dist == 0) {
691                         int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
692                         struct raid1_info *mirror = &conf->mirrors[disk];
693
694                         best_disk = disk;
695                         /*
696                          * If buffered sequential IO size exceeds optimal
697                          * iosize, check if there is idle disk. If yes, choose
698                          * the idle disk. read_balance could already choose an
699                          * idle disk before noticing it's a sequential IO in
700                          * this disk. This doesn't matter because this disk
701                          * will idle, next time it will be utilized after the
702                          * first disk has IO size exceeds optimal iosize. In
703                          * this way, iosize of the first disk will be optimal
704                          * iosize at least. iosize of the second disk might be
705                          * small, but not a big deal since when the second disk
706                          * starts IO, the first disk is likely still busy.
707                          */
708                         if (nonrot && opt_iosize > 0 &&
709                             mirror->seq_start != MaxSector &&
710                             mirror->next_seq_sect > opt_iosize &&
711                             mirror->next_seq_sect - opt_iosize >=
712                             mirror->seq_start) {
713                                 choose_next_idle = 1;
714                                 continue;
715                         }
716                         break;
717                 }
718
719                 if (choose_next_idle)
720                         continue;
721
722                 if (min_pending > pending) {
723                         min_pending = pending;
724                         best_pending_disk = disk;
725                 }
726
727                 if (dist < best_dist) {
728                         best_dist = dist;
729                         best_dist_disk = disk;
730                 }
731         }
732
733         /*
734          * If all disks are rotational, choose the closest disk. If any disk is
735          * non-rotational, choose the disk with less pending request even the
736          * disk is rotational, which might/might not be optimal for raids with
737          * mixed ratation/non-rotational disks depending on workload.
738          */
739         if (best_disk == -1) {
740                 if (has_nonrot_disk || min_pending == 0)
741                         best_disk = best_pending_disk;
742                 else
743                         best_disk = best_dist_disk;
744         }
745
746         if (best_disk >= 0) {
747                 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
748                 if (!rdev)
749                         goto retry;
750                 atomic_inc(&rdev->nr_pending);
751                 sectors = best_good_sectors;
752
753                 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
754                         conf->mirrors[best_disk].seq_start = this_sector;
755
756                 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
757         }
758         rcu_read_unlock();
759         *max_sectors = sectors;
760
761         return best_disk;
762 }
763
764 static int raid1_congested(struct mddev *mddev, int bits)
765 {
766         struct r1conf *conf = mddev->private;
767         int i, ret = 0;
768
769         if ((bits & (1 << WB_async_congested)) &&
770             conf->pending_count >= max_queued_requests)
771                 return 1;
772
773         rcu_read_lock();
774         for (i = 0; i < conf->raid_disks * 2; i++) {
775                 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
776                 if (rdev && !test_bit(Faulty, &rdev->flags)) {
777                         struct request_queue *q = bdev_get_queue(rdev->bdev);
778
779                         BUG_ON(!q);
780
781                         /* Note the '|| 1' - when read_balance prefers
782                          * non-congested targets, it can be removed
783                          */
784                         if ((bits & (1 << WB_async_congested)) || 1)
785                                 ret |= bdi_congested(q->backing_dev_info, bits);
786                         else
787                                 ret &= bdi_congested(q->backing_dev_info, bits);
788                 }
789         }
790         rcu_read_unlock();
791         return ret;
792 }
793
794 static void flush_bio_list(struct r1conf *conf, struct bio *bio)
795 {
796         /* flush any pending bitmap writes to disk before proceeding w/ I/O */
797         bitmap_unplug(conf->mddev->bitmap);
798         wake_up(&conf->wait_barrier);
799
800         while (bio) { /* submit pending writes */
801                 struct bio *next = bio->bi_next;
802                 struct md_rdev *rdev = (void*)bio->bi_bdev;
803                 bio->bi_next = NULL;
804                 bio->bi_bdev = rdev->bdev;
805                 if (test_bit(Faulty, &rdev->flags)) {
806                         bio->bi_status = BLK_STS_IOERR;
807                         bio_endio(bio);
808                 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
809                                     !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
810                         /* Just ignore it */
811                         bio_endio(bio);
812                 else
813                         generic_make_request(bio);
814                 bio = next;
815         }
816 }
817
818 static void flush_pending_writes(struct r1conf *conf)
819 {
820         /* Any writes that have been queued but are awaiting
821          * bitmap updates get flushed here.
822          */
823         spin_lock_irq(&conf->device_lock);
824
825         if (conf->pending_bio_list.head) {
826                 struct bio *bio;
827                 bio = bio_list_get(&conf->pending_bio_list);
828                 conf->pending_count = 0;
829                 spin_unlock_irq(&conf->device_lock);
830                 flush_bio_list(conf, bio);
831         } else
832                 spin_unlock_irq(&conf->device_lock);
833 }
834
835 /* Barriers....
836  * Sometimes we need to suspend IO while we do something else,
837  * either some resync/recovery, or reconfigure the array.
838  * To do this we raise a 'barrier'.
839  * The 'barrier' is a counter that can be raised multiple times
840  * to count how many activities are happening which preclude
841  * normal IO.
842  * We can only raise the barrier if there is no pending IO.
843  * i.e. if nr_pending == 0.
844  * We choose only to raise the barrier if no-one is waiting for the
845  * barrier to go down.  This means that as soon as an IO request
846  * is ready, no other operations which require a barrier will start
847  * until the IO request has had a chance.
848  *
849  * So: regular IO calls 'wait_barrier'.  When that returns there
850  *    is no backgroup IO happening,  It must arrange to call
851  *    allow_barrier when it has finished its IO.
852  * backgroup IO calls must call raise_barrier.  Once that returns
853  *    there is no normal IO happeing.  It must arrange to call
854  *    lower_barrier when the particular background IO completes.
855  */
856 static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
857 {
858         int idx = sector_to_idx(sector_nr);
859
860         spin_lock_irq(&conf->resync_lock);
861
862         /* Wait until no block IO is waiting */
863         wait_event_lock_irq(conf->wait_barrier,
864                             !atomic_read(&conf->nr_waiting[idx]),
865                             conf->resync_lock);
866
867         /* block any new IO from starting */
868         atomic_inc(&conf->barrier[idx]);
869         /*
870          * In raise_barrier() we firstly increase conf->barrier[idx] then
871          * check conf->nr_pending[idx]. In _wait_barrier() we firstly
872          * increase conf->nr_pending[idx] then check conf->barrier[idx].
873          * A memory barrier here to make sure conf->nr_pending[idx] won't
874          * be fetched before conf->barrier[idx] is increased. Otherwise
875          * there will be a race between raise_barrier() and _wait_barrier().
876          */
877         smp_mb__after_atomic();
878
879         /* For these conditions we must wait:
880          * A: while the array is in frozen state
881          * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
882          *    existing in corresponding I/O barrier bucket.
883          * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
884          *    max resync count which allowed on current I/O barrier bucket.
885          */
886         wait_event_lock_irq(conf->wait_barrier,
887                             !conf->array_frozen &&
888                              !atomic_read(&conf->nr_pending[idx]) &&
889                              atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH,
890                             conf->resync_lock);
891
892         atomic_inc(&conf->nr_sync_pending);
893         spin_unlock_irq(&conf->resync_lock);
894 }
895
896 static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
897 {
898         int idx = sector_to_idx(sector_nr);
899
900         BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
901
902         atomic_dec(&conf->barrier[idx]);
903         atomic_dec(&conf->nr_sync_pending);
904         wake_up(&conf->wait_barrier);
905 }
906
907 static void _wait_barrier(struct r1conf *conf, int idx)
908 {
909         /*
910          * We need to increase conf->nr_pending[idx] very early here,
911          * then raise_barrier() can be blocked when it waits for
912          * conf->nr_pending[idx] to be 0. Then we can avoid holding
913          * conf->resync_lock when there is no barrier raised in same
914          * barrier unit bucket. Also if the array is frozen, I/O
915          * should be blocked until array is unfrozen.
916          */
917         atomic_inc(&conf->nr_pending[idx]);
918         /*
919          * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
920          * check conf->barrier[idx]. In raise_barrier() we firstly increase
921          * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
922          * barrier is necessary here to make sure conf->barrier[idx] won't be
923          * fetched before conf->nr_pending[idx] is increased. Otherwise there
924          * will be a race between _wait_barrier() and raise_barrier().
925          */
926         smp_mb__after_atomic();
927
928         /*
929          * Don't worry about checking two atomic_t variables at same time
930          * here. If during we check conf->barrier[idx], the array is
931          * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
932          * 0, it is safe to return and make the I/O continue. Because the
933          * array is frozen, all I/O returned here will eventually complete
934          * or be queued, no race will happen. See code comment in
935          * frozen_array().
936          */
937         if (!READ_ONCE(conf->array_frozen) &&
938             !atomic_read(&conf->barrier[idx]))
939                 return;
940
941         /*
942          * After holding conf->resync_lock, conf->nr_pending[idx]
943          * should be decreased before waiting for barrier to drop.
944          * Otherwise, we may encounter a race condition because
945          * raise_barrer() might be waiting for conf->nr_pending[idx]
946          * to be 0 at same time.
947          */
948         spin_lock_irq(&conf->resync_lock);
949         atomic_inc(&conf->nr_waiting[idx]);
950         atomic_dec(&conf->nr_pending[idx]);
951         /*
952          * In case freeze_array() is waiting for
953          * get_unqueued_pending() == extra
954          */
955         wake_up(&conf->wait_barrier);
956         /* Wait for the barrier in same barrier unit bucket to drop. */
957         wait_event_lock_irq(conf->wait_barrier,
958                             !conf->array_frozen &&
959                              !atomic_read(&conf->barrier[idx]),
960                             conf->resync_lock);
961         atomic_inc(&conf->nr_pending[idx]);
962         atomic_dec(&conf->nr_waiting[idx]);
963         spin_unlock_irq(&conf->resync_lock);
964 }
965
966 static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
967 {
968         int idx = sector_to_idx(sector_nr);
969
970         /*
971          * Very similar to _wait_barrier(). The difference is, for read
972          * I/O we don't need wait for sync I/O, but if the whole array
973          * is frozen, the read I/O still has to wait until the array is
974          * unfrozen. Since there is no ordering requirement with
975          * conf->barrier[idx] here, memory barrier is unnecessary as well.
976          */
977         atomic_inc(&conf->nr_pending[idx]);
978
979         if (!READ_ONCE(conf->array_frozen))
980                 return;
981
982         spin_lock_irq(&conf->resync_lock);
983         atomic_inc(&conf->nr_waiting[idx]);
984         atomic_dec(&conf->nr_pending[idx]);
985         /*
986          * In case freeze_array() is waiting for
987          * get_unqueued_pending() == extra
988          */
989         wake_up(&conf->wait_barrier);
990         /* Wait for array to be unfrozen */
991         wait_event_lock_irq(conf->wait_barrier,
992                             !conf->array_frozen,
993                             conf->resync_lock);
994         atomic_inc(&conf->nr_pending[idx]);
995         atomic_dec(&conf->nr_waiting[idx]);
996         spin_unlock_irq(&conf->resync_lock);
997 }
998
999 static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
1000 {
1001         int idx = sector_to_idx(sector_nr);
1002
1003         _wait_barrier(conf, idx);
1004 }
1005
1006 static void wait_all_barriers(struct r1conf *conf)
1007 {
1008         int idx;
1009
1010         for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1011                 _wait_barrier(conf, idx);
1012 }
1013
1014 static void _allow_barrier(struct r1conf *conf, int idx)
1015 {
1016         atomic_dec(&conf->nr_pending[idx]);
1017         wake_up(&conf->wait_barrier);
1018 }
1019
1020 static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
1021 {
1022         int idx = sector_to_idx(sector_nr);
1023
1024         _allow_barrier(conf, idx);
1025 }
1026
1027 static void allow_all_barriers(struct r1conf *conf)
1028 {
1029         int idx;
1030
1031         for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1032                 _allow_barrier(conf, idx);
1033 }
1034
1035 /* conf->resync_lock should be held */
1036 static int get_unqueued_pending(struct r1conf *conf)
1037 {
1038         int idx, ret;
1039
1040         ret = atomic_read(&conf->nr_sync_pending);
1041         for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1042                 ret += atomic_read(&conf->nr_pending[idx]) -
1043                         atomic_read(&conf->nr_queued[idx]);
1044
1045         return ret;
1046 }
1047
1048 static void freeze_array(struct r1conf *conf, int extra)
1049 {
1050         /* Stop sync I/O and normal I/O and wait for everything to
1051          * go quiet.
1052          * This is called in two situations:
1053          * 1) management command handlers (reshape, remove disk, quiesce).
1054          * 2) one normal I/O request failed.
1055
1056          * After array_frozen is set to 1, new sync IO will be blocked at
1057          * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1058          * or wait_read_barrier(). The flying I/Os will either complete or be
1059          * queued. When everything goes quite, there are only queued I/Os left.
1060
1061          * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1062          * barrier bucket index which this I/O request hits. When all sync and
1063          * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1064          * of all conf->nr_queued[]. But normal I/O failure is an exception,
1065          * in handle_read_error(), we may call freeze_array() before trying to
1066          * fix the read error. In this case, the error read I/O is not queued,
1067          * so get_unqueued_pending() == 1.
1068          *
1069          * Therefore before this function returns, we need to wait until
1070          * get_unqueued_pendings(conf) gets equal to extra. For
1071          * normal I/O context, extra is 1, in rested situations extra is 0.
1072          */
1073         spin_lock_irq(&conf->resync_lock);
1074         conf->array_frozen = 1;
1075         raid1_log(conf->mddev, "wait freeze");
1076         wait_event_lock_irq_cmd(
1077                 conf->wait_barrier,
1078                 get_unqueued_pending(conf) == extra,
1079                 conf->resync_lock,
1080                 flush_pending_writes(conf));
1081         spin_unlock_irq(&conf->resync_lock);
1082 }
1083 static void unfreeze_array(struct r1conf *conf)
1084 {
1085         /* reverse the effect of the freeze */
1086         spin_lock_irq(&conf->resync_lock);
1087         conf->array_frozen = 0;
1088         spin_unlock_irq(&conf->resync_lock);
1089         wake_up(&conf->wait_barrier);
1090 }
1091
1092 static struct bio *alloc_behind_master_bio(struct r1bio *r1_bio,
1093                                            struct bio *bio)
1094 {
1095         int size = bio->bi_iter.bi_size;
1096         unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1097         int i = 0;
1098         struct bio *behind_bio = NULL;
1099
1100         behind_bio = bio_alloc_mddev(GFP_NOIO, vcnt, r1_bio->mddev);
1101         if (!behind_bio)
1102                 goto fail;
1103
1104         /* discard op, we don't support writezero/writesame yet */
1105         if (!bio_has_data(bio))
1106                 goto skip_copy;
1107
1108         while (i < vcnt && size) {
1109                 struct page *page;
1110                 int len = min_t(int, PAGE_SIZE, size);
1111
1112                 page = alloc_page(GFP_NOIO);
1113                 if (unlikely(!page))
1114                         goto free_pages;
1115
1116                 bio_add_page(behind_bio, page, len, 0);
1117
1118                 size -= len;
1119                 i++;
1120         }
1121
1122         bio_copy_data(behind_bio, bio);
1123 skip_copy:
1124         r1_bio->behind_master_bio = behind_bio;;
1125         set_bit(R1BIO_BehindIO, &r1_bio->state);
1126
1127         return behind_bio;
1128
1129 free_pages:
1130         pr_debug("%dB behind alloc failed, doing sync I/O\n",
1131                  bio->bi_iter.bi_size);
1132         bio_free_pages(behind_bio);
1133 fail:
1134         return behind_bio;
1135 }
1136
1137 struct raid1_plug_cb {
1138         struct blk_plug_cb      cb;
1139         struct bio_list         pending;
1140         int                     pending_cnt;
1141 };
1142
1143 static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1144 {
1145         struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1146                                                   cb);
1147         struct mddev *mddev = plug->cb.data;
1148         struct r1conf *conf = mddev->private;
1149         struct bio *bio;
1150
1151         if (from_schedule || current->bio_list) {
1152                 spin_lock_irq(&conf->device_lock);
1153                 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1154                 conf->pending_count += plug->pending_cnt;
1155                 spin_unlock_irq(&conf->device_lock);
1156                 wake_up(&conf->wait_barrier);
1157                 md_wakeup_thread(mddev->thread);
1158                 kfree(plug);
1159                 return;
1160         }
1161
1162         /* we aren't scheduling, so we can do the write-out directly. */
1163         bio = bio_list_get(&plug->pending);
1164         flush_bio_list(conf, bio);
1165         kfree(plug);
1166 }
1167
1168 static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio)
1169 {
1170         r1_bio->master_bio = bio;
1171         r1_bio->sectors = bio_sectors(bio);
1172         r1_bio->state = 0;
1173         r1_bio->mddev = mddev;
1174         r1_bio->sector = bio->bi_iter.bi_sector;
1175 }
1176
1177 static inline struct r1bio *
1178 alloc_r1bio(struct mddev *mddev, struct bio *bio)
1179 {
1180         struct r1conf *conf = mddev->private;
1181         struct r1bio *r1_bio;
1182
1183         r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1184         /* Ensure no bio records IO_BLOCKED */
1185         memset(r1_bio->bios, 0, conf->raid_disks * sizeof(r1_bio->bios[0]));
1186         init_r1bio(r1_bio, mddev, bio);
1187         return r1_bio;
1188 }
1189
1190 static void raid1_read_request(struct mddev *mddev, struct bio *bio,
1191                                int max_read_sectors, struct r1bio *r1_bio)
1192 {
1193         struct r1conf *conf = mddev->private;
1194         struct raid1_info *mirror;
1195         struct bio *read_bio;
1196         struct bitmap *bitmap = mddev->bitmap;
1197         const int op = bio_op(bio);
1198         const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1199         int max_sectors;
1200         int rdisk;
1201         bool print_msg = !!r1_bio;
1202         char b[BDEVNAME_SIZE];
1203
1204         /*
1205          * If r1_bio is set, we are blocking the raid1d thread
1206          * so there is a tiny risk of deadlock.  So ask for
1207          * emergency memory if needed.
1208          */
1209         gfp_t gfp = r1_bio ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO;
1210
1211         if (print_msg) {
1212                 /* Need to get the block device name carefully */
1213                 struct md_rdev *rdev;
1214                 rcu_read_lock();
1215                 rdev = rcu_dereference(conf->mirrors[r1_bio->read_disk].rdev);
1216                 if (rdev)
1217                         bdevname(rdev->bdev, b);
1218                 else
1219                         strcpy(b, "???");
1220                 rcu_read_unlock();
1221         }
1222
1223         /*
1224          * Still need barrier for READ in case that whole
1225          * array is frozen.
1226          */
1227         wait_read_barrier(conf, bio->bi_iter.bi_sector);
1228
1229         if (!r1_bio)
1230                 r1_bio = alloc_r1bio(mddev, bio);
1231         else
1232                 init_r1bio(r1_bio, mddev, bio);
1233         r1_bio->sectors = max_read_sectors;
1234
1235         /*
1236          * make_request() can abort the operation when read-ahead is being
1237          * used and no empty request is available.
1238          */
1239         rdisk = read_balance(conf, r1_bio, &max_sectors);
1240
1241         if (rdisk < 0) {
1242                 /* couldn't find anywhere to read from */
1243                 if (print_msg) {
1244                         pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1245                                             mdname(mddev),
1246                                             b,
1247                                             (unsigned long long)r1_bio->sector);
1248                 }
1249                 raid_end_bio_io(r1_bio);
1250                 return;
1251         }
1252         mirror = conf->mirrors + rdisk;
1253
1254         if (print_msg)
1255                 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
1256                                     mdname(mddev),
1257                                     (unsigned long long)r1_bio->sector,
1258                                     bdevname(mirror->rdev->bdev, b));
1259
1260         if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1261             bitmap) {
1262                 /*
1263                  * Reading from a write-mostly device must take care not to
1264                  * over-take any writes that are 'behind'
1265                  */
1266                 raid1_log(mddev, "wait behind writes");
1267                 wait_event(bitmap->behind_wait,
1268                            atomic_read(&bitmap->behind_writes) == 0);
1269         }
1270
1271         if (max_sectors < bio_sectors(bio)) {
1272                 struct bio *split = bio_split(bio, max_sectors,
1273                                               gfp, conf->bio_split);
1274                 bio_chain(split, bio);
1275                 generic_make_request(bio);
1276                 bio = split;
1277                 r1_bio->master_bio = bio;
1278                 r1_bio->sectors = max_sectors;
1279         }
1280
1281         r1_bio->read_disk = rdisk;
1282
1283         read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
1284
1285         r1_bio->bios[rdisk] = read_bio;
1286
1287         read_bio->bi_iter.bi_sector = r1_bio->sector +
1288                 mirror->rdev->data_offset;
1289         read_bio->bi_bdev = mirror->rdev->bdev;
1290         read_bio->bi_end_io = raid1_end_read_request;
1291         bio_set_op_attrs(read_bio, op, do_sync);
1292         if (test_bit(FailFast, &mirror->rdev->flags) &&
1293             test_bit(R1BIO_FailFast, &r1_bio->state))
1294                 read_bio->bi_opf |= MD_FAILFAST;
1295         read_bio->bi_private = r1_bio;
1296
1297         if (mddev->gendisk)
1298                 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1299                                       read_bio, disk_devt(mddev->gendisk),
1300                                       r1_bio->sector);
1301
1302         generic_make_request(read_bio);
1303 }
1304
1305 static void raid1_write_request(struct mddev *mddev, struct bio *bio,
1306                                 int max_write_sectors)
1307 {
1308         struct r1conf *conf = mddev->private;
1309         struct r1bio *r1_bio;
1310         int i, disks;
1311         struct bitmap *bitmap = mddev->bitmap;
1312         unsigned long flags;
1313         struct md_rdev *blocked_rdev;
1314         struct blk_plug_cb *cb;
1315         struct raid1_plug_cb *plug = NULL;
1316         int first_clone;
1317         int max_sectors;
1318
1319         /*
1320          * Register the new request and wait if the reconstruction
1321          * thread has put up a bar for new requests.
1322          * Continue immediately if no resync is active currently.
1323          */
1324
1325
1326         if ((bio_end_sector(bio) > mddev->suspend_lo &&
1327             bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1328             (mddev_is_clustered(mddev) &&
1329              md_cluster_ops->area_resyncing(mddev, WRITE,
1330                      bio->bi_iter.bi_sector, bio_end_sector(bio)))) {
1331
1332                 /*
1333                  * As the suspend_* range is controlled by userspace, we want
1334                  * an interruptible wait.
1335                  */
1336                 DEFINE_WAIT(w);
1337                 for (;;) {
1338                         sigset_t full, old;
1339                         prepare_to_wait(&conf->wait_barrier,
1340                                         &w, TASK_INTERRUPTIBLE);
1341                         if (bio_end_sector(bio) <= mddev->suspend_lo ||
1342                             bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1343                             (mddev_is_clustered(mddev) &&
1344                              !md_cluster_ops->area_resyncing(mddev, WRITE,
1345                                      bio->bi_iter.bi_sector,
1346                                      bio_end_sector(bio))))
1347                                 break;
1348                         sigfillset(&full);
1349                         sigprocmask(SIG_BLOCK, &full, &old);
1350                         schedule();
1351                         sigprocmask(SIG_SETMASK, &old, NULL);
1352                 }
1353                 finish_wait(&conf->wait_barrier, &w);
1354         }
1355         wait_barrier(conf, bio->bi_iter.bi_sector);
1356
1357         r1_bio = alloc_r1bio(mddev, bio);
1358         r1_bio->sectors = max_write_sectors;
1359
1360         if (conf->pending_count >= max_queued_requests) {
1361                 md_wakeup_thread(mddev->thread);
1362                 raid1_log(mddev, "wait queued");
1363                 wait_event(conf->wait_barrier,
1364                            conf->pending_count < max_queued_requests);
1365         }
1366         /* first select target devices under rcu_lock and
1367          * inc refcount on their rdev.  Record them by setting
1368          * bios[x] to bio
1369          * If there are known/acknowledged bad blocks on any device on
1370          * which we have seen a write error, we want to avoid writing those
1371          * blocks.
1372          * This potentially requires several writes to write around
1373          * the bad blocks.  Each set of writes gets it's own r1bio
1374          * with a set of bios attached.
1375          */
1376
1377         disks = conf->raid_disks * 2;
1378  retry_write:
1379         blocked_rdev = NULL;
1380         rcu_read_lock();
1381         max_sectors = r1_bio->sectors;
1382         for (i = 0;  i < disks; i++) {
1383                 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1384                 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1385                         atomic_inc(&rdev->nr_pending);
1386                         blocked_rdev = rdev;
1387                         break;
1388                 }
1389                 r1_bio->bios[i] = NULL;
1390                 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1391                         if (i < conf->raid_disks)
1392                                 set_bit(R1BIO_Degraded, &r1_bio->state);
1393                         continue;
1394                 }
1395
1396                 atomic_inc(&rdev->nr_pending);
1397                 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1398                         sector_t first_bad;
1399                         int bad_sectors;
1400                         int is_bad;
1401
1402                         is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
1403                                              &first_bad, &bad_sectors);
1404                         if (is_bad < 0) {
1405                                 /* mustn't write here until the bad block is
1406                                  * acknowledged*/
1407                                 set_bit(BlockedBadBlocks, &rdev->flags);
1408                                 blocked_rdev = rdev;
1409                                 break;
1410                         }
1411                         if (is_bad && first_bad <= r1_bio->sector) {
1412                                 /* Cannot write here at all */
1413                                 bad_sectors -= (r1_bio->sector - first_bad);
1414                                 if (bad_sectors < max_sectors)
1415                                         /* mustn't write more than bad_sectors
1416                                          * to other devices yet
1417                                          */
1418                                         max_sectors = bad_sectors;
1419                                 rdev_dec_pending(rdev, mddev);
1420                                 /* We don't set R1BIO_Degraded as that
1421                                  * only applies if the disk is
1422                                  * missing, so it might be re-added,
1423                                  * and we want to know to recover this
1424                                  * chunk.
1425                                  * In this case the device is here,
1426                                  * and the fact that this chunk is not
1427                                  * in-sync is recorded in the bad
1428                                  * block log
1429                                  */
1430                                 continue;
1431                         }
1432                         if (is_bad) {
1433                                 int good_sectors = first_bad - r1_bio->sector;
1434                                 if (good_sectors < max_sectors)
1435                                         max_sectors = good_sectors;
1436                         }
1437                 }
1438                 r1_bio->bios[i] = bio;
1439         }
1440         rcu_read_unlock();
1441
1442         if (unlikely(blocked_rdev)) {
1443                 /* Wait for this device to become unblocked */
1444                 int j;
1445
1446                 for (j = 0; j < i; j++)
1447                         if (r1_bio->bios[j])
1448                                 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1449                 r1_bio->state = 0;
1450                 allow_barrier(conf, bio->bi_iter.bi_sector);
1451                 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1452                 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1453                 wait_barrier(conf, bio->bi_iter.bi_sector);
1454                 goto retry_write;
1455         }
1456
1457         if (max_sectors < bio_sectors(bio)) {
1458                 struct bio *split = bio_split(bio, max_sectors,
1459                                               GFP_NOIO, conf->bio_split);
1460                 bio_chain(split, bio);
1461                 generic_make_request(bio);
1462                 bio = split;
1463                 r1_bio->master_bio = bio;
1464                 r1_bio->sectors = max_sectors;
1465         }
1466
1467         atomic_set(&r1_bio->remaining, 1);
1468         atomic_set(&r1_bio->behind_remaining, 0);
1469
1470         first_clone = 1;
1471
1472         for (i = 0; i < disks; i++) {
1473                 struct bio *mbio = NULL;
1474                 if (!r1_bio->bios[i])
1475                         continue;
1476
1477
1478                 if (first_clone) {
1479                         /* do behind I/O ?
1480                          * Not if there are too many, or cannot
1481                          * allocate memory, or a reader on WriteMostly
1482                          * is waiting for behind writes to flush */
1483                         if (bitmap &&
1484                             (atomic_read(&bitmap->behind_writes)
1485                              < mddev->bitmap_info.max_write_behind) &&
1486                             !waitqueue_active(&bitmap->behind_wait)) {
1487                                 mbio = alloc_behind_master_bio(r1_bio, bio);
1488                         }
1489
1490                         bitmap_startwrite(bitmap, r1_bio->sector,
1491                                           r1_bio->sectors,
1492                                           test_bit(R1BIO_BehindIO,
1493                                                    &r1_bio->state));
1494                         first_clone = 0;
1495                 }
1496
1497                 if (!mbio) {
1498                         if (r1_bio->behind_master_bio)
1499                                 mbio = bio_clone_fast(r1_bio->behind_master_bio,
1500                                                       GFP_NOIO,
1501                                                       mddev->bio_set);
1502                         else
1503                                 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1504                 }
1505
1506                 if (r1_bio->behind_master_bio) {
1507                         if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1508                                 atomic_inc(&r1_bio->behind_remaining);
1509                 }
1510
1511                 r1_bio->bios[i] = mbio;
1512
1513                 mbio->bi_iter.bi_sector = (r1_bio->sector +
1514                                    conf->mirrors[i].rdev->data_offset);
1515                 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1516                 mbio->bi_end_io = raid1_end_write_request;
1517                 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
1518                 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1519                     !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1520                     conf->raid_disks - mddev->degraded > 1)
1521                         mbio->bi_opf |= MD_FAILFAST;
1522                 mbio->bi_private = r1_bio;
1523
1524                 atomic_inc(&r1_bio->remaining);
1525
1526                 if (mddev->gendisk)
1527                         trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1528                                               mbio, disk_devt(mddev->gendisk),
1529                                               r1_bio->sector);
1530                 /* flush_pending_writes() needs access to the rdev so...*/
1531                 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1532
1533                 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1534                 if (cb)
1535                         plug = container_of(cb, struct raid1_plug_cb, cb);
1536                 else
1537                         plug = NULL;
1538                 if (plug) {
1539                         bio_list_add(&plug->pending, mbio);
1540                         plug->pending_cnt++;
1541                 } else {
1542                         spin_lock_irqsave(&conf->device_lock, flags);
1543                         bio_list_add(&conf->pending_bio_list, mbio);
1544                         conf->pending_count++;
1545                         spin_unlock_irqrestore(&conf->device_lock, flags);
1546                         md_wakeup_thread(mddev->thread);
1547                 }
1548         }
1549
1550         r1_bio_write_done(r1_bio);
1551
1552         /* In case raid1d snuck in to freeze_array */
1553         wake_up(&conf->wait_barrier);
1554 }
1555
1556 static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
1557 {
1558         sector_t sectors;
1559
1560         if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1561                 md_flush_request(mddev, bio);
1562                 return true;
1563         }
1564
1565         /*
1566          * There is a limit to the maximum size, but
1567          * the read/write handler might find a lower limit
1568          * due to bad blocks.  To avoid multiple splits,
1569          * we pass the maximum number of sectors down
1570          * and let the lower level perform the split.
1571          */
1572         sectors = align_to_barrier_unit_end(
1573                 bio->bi_iter.bi_sector, bio_sectors(bio));
1574
1575         if (bio_data_dir(bio) == READ)
1576                 raid1_read_request(mddev, bio, sectors, NULL);
1577         else {
1578                 if (!md_write_start(mddev,bio))
1579                         return false;
1580                 raid1_write_request(mddev, bio, sectors);
1581         }
1582         return true;
1583 }
1584
1585 static void raid1_status(struct seq_file *seq, struct mddev *mddev)
1586 {
1587         struct r1conf *conf = mddev->private;
1588         int i;
1589
1590         seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1591                    conf->raid_disks - mddev->degraded);
1592         rcu_read_lock();
1593         for (i = 0; i < conf->raid_disks; i++) {
1594                 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1595                 seq_printf(seq, "%s",
1596                            rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1597         }
1598         rcu_read_unlock();
1599         seq_printf(seq, "]");
1600 }
1601
1602 static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
1603 {
1604         char b[BDEVNAME_SIZE];
1605         struct r1conf *conf = mddev->private;
1606         unsigned long flags;
1607
1608         /*
1609          * If it is not operational, then we have already marked it as dead
1610          * else if it is the last working disks, ignore the error, let the
1611          * next level up know.
1612          * else mark the drive as failed
1613          */
1614         spin_lock_irqsave(&conf->device_lock, flags);
1615         if (test_bit(In_sync, &rdev->flags)
1616             && (conf->raid_disks - mddev->degraded) == 1) {
1617                 /*
1618                  * Don't fail the drive, act as though we were just a
1619                  * normal single drive.
1620                  * However don't try a recovery from this drive as
1621                  * it is very likely to fail.
1622                  */
1623                 conf->recovery_disabled = mddev->recovery_disabled;
1624                 spin_unlock_irqrestore(&conf->device_lock, flags);
1625                 return;
1626         }
1627         set_bit(Blocked, &rdev->flags);
1628         if (test_and_clear_bit(In_sync, &rdev->flags)) {
1629                 mddev->degraded++;
1630                 set_bit(Faulty, &rdev->flags);
1631         } else
1632                 set_bit(Faulty, &rdev->flags);
1633         spin_unlock_irqrestore(&conf->device_lock, flags);
1634         /*
1635          * if recovery is running, make sure it aborts.
1636          */
1637         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1638         set_mask_bits(&mddev->sb_flags, 0,
1639                       BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1640         pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1641                 "md/raid1:%s: Operation continuing on %d devices.\n",
1642                 mdname(mddev), bdevname(rdev->bdev, b),
1643                 mdname(mddev), conf->raid_disks - mddev->degraded);
1644 }
1645
1646 static void print_conf(struct r1conf *conf)
1647 {
1648         int i;
1649
1650         pr_debug("RAID1 conf printout:\n");
1651         if (!conf) {
1652                 pr_debug("(!conf)\n");
1653                 return;
1654         }
1655         pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1656                  conf->raid_disks);
1657
1658         rcu_read_lock();
1659         for (i = 0; i < conf->raid_disks; i++) {
1660                 char b[BDEVNAME_SIZE];
1661                 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1662                 if (rdev)
1663                         pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1664                                  i, !test_bit(In_sync, &rdev->flags),
1665                                  !test_bit(Faulty, &rdev->flags),
1666                                  bdevname(rdev->bdev,b));
1667         }
1668         rcu_read_unlock();
1669 }
1670
1671 static void close_sync(struct r1conf *conf)
1672 {
1673         wait_all_barriers(conf);
1674         allow_all_barriers(conf);
1675
1676         mempool_destroy(conf->r1buf_pool);
1677         conf->r1buf_pool = NULL;
1678 }
1679
1680 static int raid1_spare_active(struct mddev *mddev)
1681 {
1682         int i;
1683         struct r1conf *conf = mddev->private;
1684         int count = 0;
1685         unsigned long flags;
1686
1687         /*
1688          * Find all failed disks within the RAID1 configuration
1689          * and mark them readable.
1690          * Called under mddev lock, so rcu protection not needed.
1691          * device_lock used to avoid races with raid1_end_read_request
1692          * which expects 'In_sync' flags and ->degraded to be consistent.
1693          */
1694         spin_lock_irqsave(&conf->device_lock, flags);
1695         for (i = 0; i < conf->raid_disks; i++) {
1696                 struct md_rdev *rdev = conf->mirrors[i].rdev;
1697                 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1698                 if (repl
1699                     && !test_bit(Candidate, &repl->flags)
1700                     && repl->recovery_offset == MaxSector
1701                     && !test_bit(Faulty, &repl->flags)
1702                     && !test_and_set_bit(In_sync, &repl->flags)) {
1703                         /* replacement has just become active */
1704                         if (!rdev ||
1705                             !test_and_clear_bit(In_sync, &rdev->flags))
1706                                 count++;
1707                         if (rdev) {
1708                                 /* Replaced device not technically
1709                                  * faulty, but we need to be sure
1710                                  * it gets removed and never re-added
1711                                  */
1712                                 set_bit(Faulty, &rdev->flags);
1713                                 sysfs_notify_dirent_safe(
1714                                         rdev->sysfs_state);
1715                         }
1716                 }
1717                 if (rdev
1718                     && rdev->recovery_offset == MaxSector
1719                     && !test_bit(Faulty, &rdev->flags)
1720                     && !test_and_set_bit(In_sync, &rdev->flags)) {
1721                         count++;
1722                         sysfs_notify_dirent_safe(rdev->sysfs_state);
1723                 }
1724         }
1725         mddev->degraded -= count;
1726         spin_unlock_irqrestore(&conf->device_lock, flags);
1727
1728         print_conf(conf);
1729         return count;
1730 }
1731
1732 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1733 {
1734         struct r1conf *conf = mddev->private;
1735         int err = -EEXIST;
1736         int mirror = 0;
1737         struct raid1_info *p;
1738         int first = 0;
1739         int last = conf->raid_disks - 1;
1740
1741         if (mddev->recovery_disabled == conf->recovery_disabled)
1742                 return -EBUSY;
1743
1744         if (md_integrity_add_rdev(rdev, mddev))
1745                 return -ENXIO;
1746
1747         if (rdev->raid_disk >= 0)
1748                 first = last = rdev->raid_disk;
1749
1750         /*
1751          * find the disk ... but prefer rdev->saved_raid_disk
1752          * if possible.
1753          */
1754         if (rdev->saved_raid_disk >= 0 &&
1755             rdev->saved_raid_disk >= first &&
1756             conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1757                 first = last = rdev->saved_raid_disk;
1758
1759         for (mirror = first; mirror <= last; mirror++) {
1760                 p = conf->mirrors+mirror;
1761                 if (!p->rdev) {
1762
1763                         if (mddev->gendisk)
1764                                 disk_stack_limits(mddev->gendisk, rdev->bdev,
1765                                                   rdev->data_offset << 9);
1766
1767                         p->head_position = 0;
1768                         rdev->raid_disk = mirror;
1769                         err = 0;
1770                         /* As all devices are equivalent, we don't need a full recovery
1771                          * if this was recently any drive of the array
1772                          */
1773                         if (rdev->saved_raid_disk < 0)
1774                                 conf->fullsync = 1;
1775                         rcu_assign_pointer(p->rdev, rdev);
1776                         break;
1777                 }
1778                 if (test_bit(WantReplacement, &p->rdev->flags) &&
1779                     p[conf->raid_disks].rdev == NULL) {
1780                         /* Add this device as a replacement */
1781                         clear_bit(In_sync, &rdev->flags);
1782                         set_bit(Replacement, &rdev->flags);
1783                         rdev->raid_disk = mirror;
1784                         err = 0;
1785                         conf->fullsync = 1;
1786                         rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1787                         break;
1788                 }
1789         }
1790         if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1791                 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1792         print_conf(conf);
1793         return err;
1794 }
1795
1796 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1797 {
1798         struct r1conf *conf = mddev->private;
1799         int err = 0;
1800         int number = rdev->raid_disk;
1801         struct raid1_info *p = conf->mirrors + number;
1802
1803         if (rdev != p->rdev)
1804                 p = conf->mirrors + conf->raid_disks + number;
1805
1806         print_conf(conf);
1807         if (rdev == p->rdev) {
1808                 if (test_bit(In_sync, &rdev->flags) ||
1809                     atomic_read(&rdev->nr_pending)) {
1810                         err = -EBUSY;
1811                         goto abort;
1812                 }
1813                 /* Only remove non-faulty devices if recovery
1814                  * is not possible.
1815                  */
1816                 if (!test_bit(Faulty, &rdev->flags) &&
1817                     mddev->recovery_disabled != conf->recovery_disabled &&
1818                     mddev->degraded < conf->raid_disks) {
1819                         err = -EBUSY;
1820                         goto abort;
1821                 }
1822                 p->rdev = NULL;
1823                 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1824                         synchronize_rcu();
1825                         if (atomic_read(&rdev->nr_pending)) {
1826                                 /* lost the race, try later */
1827                                 err = -EBUSY;
1828                                 p->rdev = rdev;
1829                                 goto abort;
1830                         }
1831                 }
1832                 if (conf->mirrors[conf->raid_disks + number].rdev) {
1833                         /* We just removed a device that is being replaced.
1834                          * Move down the replacement.  We drain all IO before
1835                          * doing this to avoid confusion.
1836                          */
1837                         struct md_rdev *repl =
1838                                 conf->mirrors[conf->raid_disks + number].rdev;
1839                         freeze_array(conf, 0);
1840                         clear_bit(Replacement, &repl->flags);
1841                         p->rdev = repl;
1842                         conf->mirrors[conf->raid_disks + number].rdev = NULL;
1843                         unfreeze_array(conf);
1844                 }
1845
1846                 clear_bit(WantReplacement, &rdev->flags);
1847                 err = md_integrity_register(mddev);
1848         }
1849 abort:
1850
1851         print_conf(conf);
1852         return err;
1853 }
1854
1855 static void end_sync_read(struct bio *bio)
1856 {
1857         struct r1bio *r1_bio = get_resync_r1bio(bio);
1858
1859         update_head_pos(r1_bio->read_disk, r1_bio);
1860
1861         /*
1862          * we have read a block, now it needs to be re-written,
1863          * or re-read if the read failed.
1864          * We don't do much here, just schedule handling by raid1d
1865          */
1866         if (!bio->bi_status)
1867                 set_bit(R1BIO_Uptodate, &r1_bio->state);
1868
1869         if (atomic_dec_and_test(&r1_bio->remaining))
1870                 reschedule_retry(r1_bio);
1871 }
1872
1873 static void end_sync_write(struct bio *bio)
1874 {
1875         int uptodate = !bio->bi_status;
1876         struct r1bio *r1_bio = get_resync_r1bio(bio);
1877         struct mddev *mddev = r1_bio->mddev;
1878         struct r1conf *conf = mddev->private;
1879         sector_t first_bad;
1880         int bad_sectors;
1881         struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
1882
1883         if (!uptodate) {
1884                 sector_t sync_blocks = 0;
1885                 sector_t s = r1_bio->sector;
1886                 long sectors_to_go = r1_bio->sectors;
1887                 /* make sure these bits doesn't get cleared. */
1888                 do {
1889                         bitmap_end_sync(mddev->bitmap, s,
1890                                         &sync_blocks, 1);
1891                         s += sync_blocks;
1892                         sectors_to_go -= sync_blocks;
1893                 } while (sectors_to_go > 0);
1894                 set_bit(WriteErrorSeen, &rdev->flags);
1895                 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1896                         set_bit(MD_RECOVERY_NEEDED, &
1897                                 mddev->recovery);
1898                 set_bit(R1BIO_WriteError, &r1_bio->state);
1899         } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
1900                                &first_bad, &bad_sectors) &&
1901                    !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1902                                 r1_bio->sector,
1903                                 r1_bio->sectors,
1904                                 &first_bad, &bad_sectors)
1905                 )
1906                 set_bit(R1BIO_MadeGood, &r1_bio->state);
1907
1908         if (atomic_dec_and_test(&r1_bio->remaining)) {
1909                 int s = r1_bio->sectors;
1910                 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1911                     test_bit(R1BIO_WriteError, &r1_bio->state))
1912                         reschedule_retry(r1_bio);
1913                 else {
1914                         put_buf(r1_bio);
1915                         md_done_sync(mddev, s, uptodate);
1916                 }
1917         }
1918 }
1919
1920 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1921                             int sectors, struct page *page, int rw)
1922 {
1923         if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
1924                 /* success */
1925                 return 1;
1926         if (rw == WRITE) {
1927                 set_bit(WriteErrorSeen, &rdev->flags);
1928                 if (!test_and_set_bit(WantReplacement,
1929                                       &rdev->flags))
1930                         set_bit(MD_RECOVERY_NEEDED, &
1931                                 rdev->mddev->recovery);
1932         }
1933         /* need to record an error - either for the block or the device */
1934         if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1935                 md_error(rdev->mddev, rdev);
1936         return 0;
1937 }
1938
1939 static int fix_sync_read_error(struct r1bio *r1_bio)
1940 {
1941         /* Try some synchronous reads of other devices to get
1942          * good data, much like with normal read errors.  Only
1943          * read into the pages we already have so we don't
1944          * need to re-issue the read request.
1945          * We don't need to freeze the array, because being in an
1946          * active sync request, there is no normal IO, and
1947          * no overlapping syncs.
1948          * We don't need to check is_badblock() again as we
1949          * made sure that anything with a bad block in range
1950          * will have bi_end_io clear.
1951          */
1952         struct mddev *mddev = r1_bio->mddev;
1953         struct r1conf *conf = mddev->private;
1954         struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1955         struct page **pages = get_resync_pages(bio)->pages;
1956         sector_t sect = r1_bio->sector;
1957         int sectors = r1_bio->sectors;
1958         int idx = 0;
1959         struct md_rdev *rdev;
1960
1961         rdev = conf->mirrors[r1_bio->read_disk].rdev;
1962         if (test_bit(FailFast, &rdev->flags)) {
1963                 /* Don't try recovering from here - just fail it
1964                  * ... unless it is the last working device of course */
1965                 md_error(mddev, rdev);
1966                 if (test_bit(Faulty, &rdev->flags))
1967                         /* Don't try to read from here, but make sure
1968                          * put_buf does it's thing
1969                          */
1970                         bio->bi_end_io = end_sync_write;
1971         }
1972
1973         while(sectors) {
1974                 int s = sectors;
1975                 int d = r1_bio->read_disk;
1976                 int success = 0;
1977                 int start;
1978
1979                 if (s > (PAGE_SIZE>>9))
1980                         s = PAGE_SIZE >> 9;
1981                 do {
1982                         if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1983                                 /* No rcu protection needed here devices
1984                                  * can only be removed when no resync is
1985                                  * active, and resync is currently active
1986                                  */
1987                                 rdev = conf->mirrors[d].rdev;
1988                                 if (sync_page_io(rdev, sect, s<<9,
1989                                                  pages[idx],
1990                                                  REQ_OP_READ, 0, false)) {
1991                                         success = 1;
1992                                         break;
1993                                 }
1994                         }
1995                         d++;
1996                         if (d == conf->raid_disks * 2)
1997                                 d = 0;
1998                 } while (!success && d != r1_bio->read_disk);
1999
2000                 if (!success) {
2001                         char b[BDEVNAME_SIZE];
2002                         int abort = 0;
2003                         /* Cannot read from anywhere, this block is lost.
2004                          * Record a bad block on each device.  If that doesn't
2005                          * work just disable and interrupt the recovery.
2006                          * Don't fail devices as that won't really help.
2007                          */
2008                         pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2009                                             mdname(mddev),
2010                                             bdevname(bio->bi_bdev, b),
2011                                             (unsigned long long)r1_bio->sector);
2012                         for (d = 0; d < conf->raid_disks * 2; d++) {
2013                                 rdev = conf->mirrors[d].rdev;
2014                                 if (!rdev || test_bit(Faulty, &rdev->flags))
2015                                         continue;
2016                                 if (!rdev_set_badblocks(rdev, sect, s, 0))
2017                                         abort = 1;
2018                         }
2019                         if (abort) {
2020                                 conf->recovery_disabled =
2021                                         mddev->recovery_disabled;
2022                                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2023                                 md_done_sync(mddev, r1_bio->sectors, 0);
2024                                 put_buf(r1_bio);
2025                                 return 0;
2026                         }
2027                         /* Try next page */
2028                         sectors -= s;
2029                         sect += s;
2030                         idx++;
2031                         continue;
2032                 }
2033
2034                 start = d;
2035                 /* write it back and re-read */
2036                 while (d != r1_bio->read_disk) {
2037                         if (d == 0)
2038                                 d = conf->raid_disks * 2;
2039                         d--;
2040                         if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2041                                 continue;
2042                         rdev = conf->mirrors[d].rdev;
2043                         if (r1_sync_page_io(rdev, sect, s,
2044                                             pages[idx],
2045                                             WRITE) == 0) {
2046                                 r1_bio->bios[d]->bi_end_io = NULL;
2047                                 rdev_dec_pending(rdev, mddev);
2048                         }
2049                 }
2050                 d = start;
2051                 while (d != r1_bio->read_disk) {
2052                         if (d == 0)
2053                                 d = conf->raid_disks * 2;
2054                         d--;
2055                         if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2056                                 continue;
2057                         rdev = conf->mirrors[d].rdev;
2058                         if (r1_sync_page_io(rdev, sect, s,
2059                                             pages[idx],
2060                                             READ) != 0)
2061                                 atomic_add(s, &rdev->corrected_errors);
2062                 }
2063                 sectors -= s;
2064                 sect += s;
2065                 idx ++;
2066         }
2067         set_bit(R1BIO_Uptodate, &r1_bio->state);
2068         bio->bi_status = 0;
2069         return 1;
2070 }
2071
2072 static void process_checks(struct r1bio *r1_bio)
2073 {
2074         /* We have read all readable devices.  If we haven't
2075          * got the block, then there is no hope left.
2076          * If we have, then we want to do a comparison
2077          * and skip the write if everything is the same.
2078          * If any blocks failed to read, then we need to
2079          * attempt an over-write
2080          */
2081         struct mddev *mddev = r1_bio->mddev;
2082         struct r1conf *conf = mddev->private;
2083         int primary;
2084         int i;
2085         int vcnt;
2086
2087         /* Fix variable parts of all bios */
2088         vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2089         for (i = 0; i < conf->raid_disks * 2; i++) {
2090                 blk_status_t status;
2091                 struct bio *b = r1_bio->bios[i];
2092                 struct resync_pages *rp = get_resync_pages(b);
2093                 if (b->bi_end_io != end_sync_read)
2094                         continue;
2095                 /* fixup the bio for reuse, but preserve errno */
2096                 status = b->bi_status;
2097                 bio_reset(b);
2098                 b->bi_status = status;
2099                 b->bi_iter.bi_sector = r1_bio->sector +
2100                         conf->mirrors[i].rdev->data_offset;
2101                 b->bi_bdev = conf->mirrors[i].rdev->bdev;
2102                 b->bi_end_io = end_sync_read;
2103                 rp->raid_bio = r1_bio;
2104                 b->bi_private = rp;
2105
2106                 /* initialize bvec table again */
2107                 md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9);
2108         }
2109         for (primary = 0; primary < conf->raid_disks * 2; primary++)
2110                 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
2111                     !r1_bio->bios[primary]->bi_status) {
2112                         r1_bio->bios[primary]->bi_end_io = NULL;
2113                         rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2114                         break;
2115                 }
2116         r1_bio->read_disk = primary;
2117         for (i = 0; i < conf->raid_disks * 2; i++) {
2118                 int j;
2119                 struct bio *pbio = r1_bio->bios[primary];
2120                 struct bio *sbio = r1_bio->bios[i];
2121                 blk_status_t status = sbio->bi_status;
2122                 struct page **ppages = get_resync_pages(pbio)->pages;
2123                 struct page **spages = get_resync_pages(sbio)->pages;
2124                 struct bio_vec *bi;
2125                 int page_len[RESYNC_PAGES] = { 0 };
2126
2127                 if (sbio->bi_end_io != end_sync_read)
2128                         continue;
2129                 /* Now we can 'fixup' the error value */
2130                 sbio->bi_status = 0;
2131
2132                 bio_for_each_segment_all(bi, sbio, j)
2133                         page_len[j] = bi->bv_len;
2134
2135                 if (!status) {
2136                         for (j = vcnt; j-- ; ) {
2137                                 if (memcmp(page_address(ppages[j]),
2138                                            page_address(spages[j]),
2139                                            page_len[j]))
2140                                         break;
2141                         }
2142                 } else
2143                         j = 0;
2144                 if (j >= 0)
2145                         atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
2146                 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
2147                               && !status)) {
2148                         /* No need to write to this device. */
2149                         sbio->bi_end_io = NULL;
2150                         rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2151                         continue;
2152                 }
2153
2154                 bio_copy_data(sbio, pbio);
2155         }
2156 }
2157
2158 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
2159 {
2160         struct r1conf *conf = mddev->private;
2161         int i;
2162         int disks = conf->raid_disks * 2;
2163         struct bio *wbio;
2164
2165         if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2166                 /* ouch - failed to read all of that. */
2167                 if (!fix_sync_read_error(r1_bio))
2168                         return;
2169
2170         if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2171                 process_checks(r1_bio);
2172
2173         /*
2174          * schedule writes
2175          */
2176         atomic_set(&r1_bio->remaining, 1);
2177         for (i = 0; i < disks ; i++) {
2178                 wbio = r1_bio->bios[i];
2179                 if (wbio->bi_end_io == NULL ||
2180                     (wbio->bi_end_io == end_sync_read &&
2181                      (i == r1_bio->read_disk ||
2182                       !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
2183                         continue;
2184                 if (test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2185                         continue;
2186
2187                 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2188                 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2189                         wbio->bi_opf |= MD_FAILFAST;
2190
2191                 wbio->bi_end_io = end_sync_write;
2192                 atomic_inc(&r1_bio->remaining);
2193                 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
2194
2195                 generic_make_request(wbio);
2196         }
2197
2198         if (atomic_dec_and_test(&r1_bio->remaining)) {
2199                 /* if we're here, all write(s) have completed, so clean up */
2200                 int s = r1_bio->sectors;
2201                 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2202                     test_bit(R1BIO_WriteError, &r1_bio->state))
2203                         reschedule_retry(r1_bio);
2204                 else {
2205                         put_buf(r1_bio);
2206                         md_done_sync(mddev, s, 1);
2207                 }
2208         }
2209 }
2210
2211 /*
2212  * This is a kernel thread which:
2213  *
2214  *      1.      Retries failed read operations on working mirrors.
2215  *      2.      Updates the raid superblock when problems encounter.
2216  *      3.      Performs writes following reads for array synchronising.
2217  */
2218
2219 static void fix_read_error(struct r1conf *conf, int read_disk,
2220                            sector_t sect, int sectors)
2221 {
2222         struct mddev *mddev = conf->mddev;
2223         while(sectors) {
2224                 int s = sectors;
2225                 int d = read_disk;
2226                 int success = 0;
2227                 int start;
2228                 struct md_rdev *rdev;
2229
2230                 if (s > (PAGE_SIZE>>9))
2231                         s = PAGE_SIZE >> 9;
2232
2233                 do {
2234                         sector_t first_bad;
2235                         int bad_sectors;
2236
2237                         rcu_read_lock();
2238                         rdev = rcu_dereference(conf->mirrors[d].rdev);
2239                         if (rdev &&
2240                             (test_bit(In_sync, &rdev->flags) ||
2241                              (!test_bit(Faulty, &rdev->flags) &&
2242                               rdev->recovery_offset >= sect + s)) &&
2243                             is_badblock(rdev, sect, s,
2244                                         &first_bad, &bad_sectors) == 0) {
2245                                 atomic_inc(&rdev->nr_pending);
2246                                 rcu_read_unlock();
2247                                 if (sync_page_io(rdev, sect, s<<9,
2248                                          conf->tmppage, REQ_OP_READ, 0, false))
2249                                         success = 1;
2250                                 rdev_dec_pending(rdev, mddev);
2251                                 if (success)
2252                                         break;
2253                         } else
2254                                 rcu_read_unlock();
2255                         d++;
2256                         if (d == conf->raid_disks * 2)
2257                                 d = 0;
2258                 } while (!success && d != read_disk);
2259
2260                 if (!success) {
2261                         /* Cannot read from anywhere - mark it bad */
2262                         struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
2263                         if (!rdev_set_badblocks(rdev, sect, s, 0))
2264                                 md_error(mddev, rdev);
2265                         break;
2266                 }
2267                 /* write it back and re-read */
2268                 start = d;
2269                 while (d != read_disk) {
2270                         if (d==0)
2271                                 d = conf->raid_disks * 2;
2272                         d--;
2273                         rcu_read_lock();
2274                         rdev = rcu_dereference(conf->mirrors[d].rdev);
2275                         if (rdev &&
2276                             !test_bit(Faulty, &rdev->flags)) {
2277                                 atomic_inc(&rdev->nr_pending);
2278                                 rcu_read_unlock();
2279                                 r1_sync_page_io(rdev, sect, s,
2280                                                 conf->tmppage, WRITE);
2281                                 rdev_dec_pending(rdev, mddev);
2282                         } else
2283                                 rcu_read_unlock();
2284                 }
2285                 d = start;
2286                 while (d != read_disk) {
2287                         char b[BDEVNAME_SIZE];
2288                         if (d==0)
2289                                 d = conf->raid_disks * 2;
2290                         d--;
2291                         rcu_read_lock();
2292                         rdev = rcu_dereference(conf->mirrors[d].rdev);
2293                         if (rdev &&
2294                             !test_bit(Faulty, &rdev->flags)) {
2295                                 atomic_inc(&rdev->nr_pending);
2296                                 rcu_read_unlock();
2297                                 if (r1_sync_page_io(rdev, sect, s,
2298                                                     conf->tmppage, READ)) {
2299                                         atomic_add(s, &rdev->corrected_errors);
2300                                         pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2301                                                 mdname(mddev), s,
2302                                                 (unsigned long long)(sect +
2303                                                                      rdev->data_offset),
2304                                                 bdevname(rdev->bdev, b));
2305                                 }
2306                                 rdev_dec_pending(rdev, mddev);
2307                         } else
2308                                 rcu_read_unlock();
2309                 }
2310                 sectors -= s;
2311                 sect += s;
2312         }
2313 }
2314
2315 static int narrow_write_error(struct r1bio *r1_bio, int i)
2316 {
2317         struct mddev *mddev = r1_bio->mddev;
2318         struct r1conf *conf = mddev->private;
2319         struct md_rdev *rdev = conf->mirrors[i].rdev;
2320
2321         /* bio has the data to be written to device 'i' where
2322          * we just recently had a write error.
2323          * We repeatedly clone the bio and trim down to one block,
2324          * then try the write.  Where the write fails we record
2325          * a bad block.
2326          * It is conceivable that the bio doesn't exactly align with
2327          * blocks.  We must handle this somehow.
2328          *
2329          * We currently own a reference on the rdev.
2330          */
2331
2332         int block_sectors;
2333         sector_t sector;
2334         int sectors;
2335         int sect_to_write = r1_bio->sectors;
2336         int ok = 1;
2337
2338         if (rdev->badblocks.shift < 0)
2339                 return 0;
2340
2341         block_sectors = roundup(1 << rdev->badblocks.shift,
2342                                 bdev_logical_block_size(rdev->bdev) >> 9);
2343         sector = r1_bio->sector;
2344         sectors = ((sector + block_sectors)
2345                    & ~(sector_t)(block_sectors - 1))
2346                 - sector;
2347
2348         while (sect_to_write) {
2349                 struct bio *wbio;
2350                 if (sectors > sect_to_write)
2351                         sectors = sect_to_write;
2352                 /* Write at 'sector' for 'sectors'*/
2353
2354                 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2355                         wbio = bio_clone_fast(r1_bio->behind_master_bio,
2356                                               GFP_NOIO,
2357                                               mddev->bio_set);
2358                         /* We really need a _all clone */
2359                         wbio->bi_iter = (struct bvec_iter){ 0 };
2360                 } else {
2361                         wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2362                                               mddev->bio_set);
2363                 }
2364
2365                 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2366                 wbio->bi_iter.bi_sector = r1_bio->sector;
2367                 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
2368
2369                 bio_trim(wbio, sector - r1_bio->sector, sectors);
2370                 wbio->bi_iter.bi_sector += rdev->data_offset;
2371                 wbio->bi_bdev = rdev->bdev;
2372
2373                 if (submit_bio_wait(wbio) < 0)
2374                         /* failure! */
2375                         ok = rdev_set_badblocks(rdev, sector,
2376                                                 sectors, 0)
2377                                 && ok;
2378
2379                 bio_put(wbio);
2380                 sect_to_write -= sectors;
2381                 sector += sectors;
2382                 sectors = block_sectors;
2383         }
2384         return ok;
2385 }
2386
2387 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2388 {
2389         int m;
2390         int s = r1_bio->sectors;
2391         for (m = 0; m < conf->raid_disks * 2 ; m++) {
2392                 struct md_rdev *rdev = conf->mirrors[m].rdev;
2393                 struct bio *bio = r1_bio->bios[m];
2394                 if (bio->bi_end_io == NULL)
2395                         continue;
2396                 if (!bio->bi_status &&
2397                     test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2398                         rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
2399                 }
2400                 if (bio->bi_status &&
2401                     test_bit(R1BIO_WriteError, &r1_bio->state)) {
2402                         if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2403                                 md_error(conf->mddev, rdev);
2404                 }
2405         }
2406         put_buf(r1_bio);
2407         md_done_sync(conf->mddev, s, 1);
2408 }
2409
2410 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2411 {
2412         int m, idx;
2413         bool fail = false;
2414
2415         for (m = 0; m < conf->raid_disks * 2 ; m++)
2416                 if (r1_bio->bios[m] == IO_MADE_GOOD) {
2417                         struct md_rdev *rdev = conf->mirrors[m].rdev;
2418                         rdev_clear_badblocks(rdev,
2419                                              r1_bio->sector,
2420                                              r1_bio->sectors, 0);
2421                         rdev_dec_pending(rdev, conf->mddev);
2422                 } else if (r1_bio->bios[m] != NULL) {
2423                         /* This drive got a write error.  We need to
2424                          * narrow down and record precise write
2425                          * errors.
2426                          */
2427                         fail = true;
2428                         if (!narrow_write_error(r1_bio, m)) {
2429                                 md_error(conf->mddev,
2430                                          conf->mirrors[m].rdev);
2431                                 /* an I/O failed, we can't clear the bitmap */
2432                                 set_bit(R1BIO_Degraded, &r1_bio->state);
2433                         }
2434                         rdev_dec_pending(conf->mirrors[m].rdev,
2435                                          conf->mddev);
2436                 }
2437         if (fail) {
2438                 spin_lock_irq(&conf->device_lock);
2439                 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
2440                 idx = sector_to_idx(r1_bio->sector);
2441                 atomic_inc(&conf->nr_queued[idx]);
2442                 spin_unlock_irq(&conf->device_lock);
2443                 /*
2444                  * In case freeze_array() is waiting for condition
2445                  * get_unqueued_pending() == extra to be true.
2446                  */
2447                 wake_up(&conf->wait_barrier);
2448                 md_wakeup_thread(conf->mddev->thread);
2449         } else {
2450                 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2451                         close_write(r1_bio);
2452                 raid_end_bio_io(r1_bio);
2453         }
2454 }
2455
2456 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
2457 {
2458         struct mddev *mddev = conf->mddev;
2459         struct bio *bio;
2460         struct md_rdev *rdev;
2461         dev_t bio_dev;
2462         sector_t bio_sector;
2463
2464         clear_bit(R1BIO_ReadError, &r1_bio->state);
2465         /* we got a read error. Maybe the drive is bad.  Maybe just
2466          * the block and we can fix it.
2467          * We freeze all other IO, and try reading the block from
2468          * other devices.  When we find one, we re-write
2469          * and check it that fixes the read error.
2470          * This is all done synchronously while the array is
2471          * frozen
2472          */
2473
2474         bio = r1_bio->bios[r1_bio->read_disk];
2475         bio_dev = bio->bi_bdev->bd_dev;
2476         bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
2477         bio_put(bio);
2478         r1_bio->bios[r1_bio->read_disk] = NULL;
2479
2480         rdev = conf->mirrors[r1_bio->read_disk].rdev;
2481         if (mddev->ro == 0
2482             && !test_bit(FailFast, &rdev->flags)) {
2483                 freeze_array(conf, 1);
2484                 fix_read_error(conf, r1_bio->read_disk,
2485                                r1_bio->sector, r1_bio->sectors);
2486                 unfreeze_array(conf);
2487         } else {
2488                 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2489         }
2490
2491         rdev_dec_pending(rdev, conf->mddev);
2492         allow_barrier(conf, r1_bio->sector);
2493         bio = r1_bio->master_bio;
2494
2495         /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */
2496         r1_bio->state = 0;
2497         raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio);
2498 }
2499
2500 static void raid1d(struct md_thread *thread)
2501 {
2502         struct mddev *mddev = thread->mddev;
2503         struct r1bio *r1_bio;
2504         unsigned long flags;
2505         struct r1conf *conf = mddev->private;
2506         struct list_head *head = &conf->retry_list;
2507         struct blk_plug plug;
2508         int idx;
2509
2510         md_check_recovery(mddev);
2511
2512         if (!list_empty_careful(&conf->bio_end_io_list) &&
2513             !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2514                 LIST_HEAD(tmp);
2515                 spin_lock_irqsave(&conf->device_lock, flags);
2516                 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2517                         list_splice_init(&conf->bio_end_io_list, &tmp);
2518                 spin_unlock_irqrestore(&conf->device_lock, flags);
2519                 while (!list_empty(&tmp)) {
2520                         r1_bio = list_first_entry(&tmp, struct r1bio,
2521                                                   retry_list);
2522                         list_del(&r1_bio->retry_list);
2523                         idx = sector_to_idx(r1_bio->sector);
2524                         atomic_dec(&conf->nr_queued[idx]);
2525                         if (mddev->degraded)
2526                                 set_bit(R1BIO_Degraded, &r1_bio->state);
2527                         if (test_bit(R1BIO_WriteError, &r1_bio->state))
2528                                 close_write(r1_bio);
2529                         raid_end_bio_io(r1_bio);
2530                 }
2531         }
2532
2533         blk_start_plug(&plug);
2534         for (;;) {
2535
2536                 flush_pending_writes(conf);
2537
2538                 spin_lock_irqsave(&conf->device_lock, flags);
2539                 if (list_empty(head)) {
2540                         spin_unlock_irqrestore(&conf->device_lock, flags);
2541                         break;
2542                 }
2543                 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
2544                 list_del(head->prev);
2545                 idx = sector_to_idx(r1_bio->sector);
2546                 atomic_dec(&conf->nr_queued[idx]);
2547                 spin_unlock_irqrestore(&conf->device_lock, flags);
2548
2549                 mddev = r1_bio->mddev;
2550                 conf = mddev->private;
2551                 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2552                         if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2553                             test_bit(R1BIO_WriteError, &r1_bio->state))
2554                                 handle_sync_write_finished(conf, r1_bio);
2555                         else
2556                                 sync_request_write(mddev, r1_bio);
2557                 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2558                            test_bit(R1BIO_WriteError, &r1_bio->state))
2559                         handle_write_finished(conf, r1_bio);
2560                 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2561                         handle_read_error(conf, r1_bio);
2562                 else
2563                         WARN_ON_ONCE(1);
2564
2565                 cond_resched();
2566                 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2567                         md_check_recovery(mddev);
2568         }
2569         blk_finish_plug(&plug);
2570 }
2571
2572 static int init_resync(struct r1conf *conf)
2573 {
2574         int buffs;
2575
2576         buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2577         BUG_ON(conf->r1buf_pool);
2578         conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2579                                           conf->poolinfo);
2580         if (!conf->r1buf_pool)
2581                 return -ENOMEM;
2582         return 0;
2583 }
2584
2585 /*
2586  * perform a "sync" on one "block"
2587  *
2588  * We need to make sure that no normal I/O request - particularly write
2589  * requests - conflict with active sync requests.
2590  *
2591  * This is achieved by tracking pending requests and a 'barrier' concept
2592  * that can be installed to exclude normal IO requests.
2593  */
2594
2595 static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2596                                    int *skipped)
2597 {
2598         struct r1conf *conf = mddev->private;
2599         struct r1bio *r1_bio;
2600         struct bio *bio;
2601         sector_t max_sector, nr_sectors;
2602         int disk = -1;
2603         int i;
2604         int wonly = -1;
2605         int write_targets = 0, read_targets = 0;
2606         sector_t sync_blocks;
2607         int still_degraded = 0;
2608         int good_sectors = RESYNC_SECTORS;
2609         int min_bad = 0; /* number of sectors that are bad in all devices */
2610         int idx = sector_to_idx(sector_nr);
2611         int page_idx = 0;
2612
2613         if (!conf->r1buf_pool)
2614                 if (init_resync(conf))
2615                         return 0;
2616
2617         max_sector = mddev->dev_sectors;
2618         if (sector_nr >= max_sector) {
2619                 /* If we aborted, we need to abort the
2620                  * sync on the 'current' bitmap chunk (there will
2621                  * only be one in raid1 resync.
2622                  * We can find the current addess in mddev->curr_resync
2623                  */
2624                 if (mddev->curr_resync < max_sector) /* aborted */
2625                         bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2626                                                 &sync_blocks, 1);
2627                 else /* completed sync */
2628                         conf->fullsync = 0;
2629
2630                 bitmap_close_sync(mddev->bitmap);
2631                 close_sync(conf);
2632
2633                 if (mddev_is_clustered(mddev)) {
2634                         conf->cluster_sync_low = 0;
2635                         conf->cluster_sync_high = 0;
2636                 }
2637                 return 0;
2638         }
2639
2640         if (mddev->bitmap == NULL &&
2641             mddev->recovery_cp == MaxSector &&
2642             !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2643             conf->fullsync == 0) {
2644                 *skipped = 1;
2645                 return max_sector - sector_nr;
2646         }
2647         /* before building a request, check if we can skip these blocks..
2648          * This call the bitmap_start_sync doesn't actually record anything
2649          */
2650         if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2651             !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2652                 /* We can skip this block, and probably several more */
2653                 *skipped = 1;
2654                 return sync_blocks;
2655         }
2656
2657         /*
2658          * If there is non-resync activity waiting for a turn, then let it
2659          * though before starting on this new sync request.
2660          */
2661         if (atomic_read(&conf->nr_waiting[idx]))
2662                 schedule_timeout_uninterruptible(1);
2663
2664         /* we are incrementing sector_nr below. To be safe, we check against
2665          * sector_nr + two times RESYNC_SECTORS
2666          */
2667
2668         bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2669                 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
2670         r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2671
2672         raise_barrier(conf, sector_nr);
2673
2674         rcu_read_lock();
2675         /*
2676          * If we get a correctably read error during resync or recovery,
2677          * we might want to read from a different device.  So we
2678          * flag all drives that could conceivably be read from for READ,
2679          * and any others (which will be non-In_sync devices) for WRITE.
2680          * If a read fails, we try reading from something else for which READ
2681          * is OK.
2682          */
2683
2684         r1_bio->mddev = mddev;
2685         r1_bio->sector = sector_nr;
2686         r1_bio->state = 0;
2687         set_bit(R1BIO_IsSync, &r1_bio->state);
2688         /* make sure good_sectors won't go across barrier unit boundary */
2689         good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
2690
2691         for (i = 0; i < conf->raid_disks * 2; i++) {
2692                 struct md_rdev *rdev;
2693                 bio = r1_bio->bios[i];
2694
2695                 rdev = rcu_dereference(conf->mirrors[i].rdev);
2696                 if (rdev == NULL ||
2697                     test_bit(Faulty, &rdev->flags)) {
2698                         if (i < conf->raid_disks)
2699                                 still_degraded = 1;
2700                 } else if (!test_bit(In_sync, &rdev->flags)) {
2701                         bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2702                         bio->bi_end_io = end_sync_write;
2703                         write_targets ++;
2704                 } else {
2705                         /* may need to read from here */
2706                         sector_t first_bad = MaxSector;
2707                         int bad_sectors;
2708
2709                         if (is_badblock(rdev, sector_nr, good_sectors,
2710                                         &first_bad, &bad_sectors)) {
2711                                 if (first_bad > sector_nr)
2712                                         good_sectors = first_bad - sector_nr;
2713                                 else {
2714                                         bad_sectors -= (sector_nr - first_bad);
2715                                         if (min_bad == 0 ||
2716                                             min_bad > bad_sectors)
2717                                                 min_bad = bad_sectors;
2718                                 }
2719                         }
2720                         if (sector_nr < first_bad) {
2721                                 if (test_bit(WriteMostly, &rdev->flags)) {
2722                                         if (wonly < 0)
2723                                                 wonly = i;
2724                                 } else {
2725                                         if (disk < 0)
2726                                                 disk = i;
2727                                 }
2728                                 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2729                                 bio->bi_end_io = end_sync_read;
2730                                 read_targets++;
2731                         } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2732                                 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2733                                 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2734                                 /*
2735                                  * The device is suitable for reading (InSync),
2736                                  * but has bad block(s) here. Let's try to correct them,
2737                                  * if we are doing resync or repair. Otherwise, leave
2738                                  * this device alone for this sync request.
2739                                  */
2740                                 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
2741                                 bio->bi_end_io = end_sync_write;
2742                                 write_targets++;
2743                         }
2744                 }
2745                 if (bio->bi_end_io) {
2746                         atomic_inc(&rdev->nr_pending);
2747                         bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
2748                         bio->bi_bdev = rdev->bdev;
2749                         if (test_bit(FailFast, &rdev->flags))
2750                                 bio->bi_opf |= MD_FAILFAST;
2751                 }
2752         }
2753         rcu_read_unlock();
2754         if (disk < 0)
2755                 disk = wonly;
2756         r1_bio->read_disk = disk;
2757
2758         if (read_targets == 0 && min_bad > 0) {
2759                 /* These sectors are bad on all InSync devices, so we
2760                  * need to mark them bad on all write targets
2761                  */
2762                 int ok = 1;
2763                 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
2764                         if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2765                                 struct md_rdev *rdev = conf->mirrors[i].rdev;
2766                                 ok = rdev_set_badblocks(rdev, sector_nr,
2767                                                         min_bad, 0
2768                                         ) && ok;
2769                         }
2770                 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2771                 *skipped = 1;
2772                 put_buf(r1_bio);
2773
2774                 if (!ok) {
2775                         /* Cannot record the badblocks, so need to
2776                          * abort the resync.
2777                          * If there are multiple read targets, could just
2778                          * fail the really bad ones ???
2779                          */
2780                         conf->recovery_disabled = mddev->recovery_disabled;
2781                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2782                         return 0;
2783                 } else
2784                         return min_bad;
2785
2786         }
2787         if (min_bad > 0 && min_bad < good_sectors) {
2788                 /* only resync enough to reach the next bad->good
2789                  * transition */
2790                 good_sectors = min_bad;
2791         }
2792
2793         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2794                 /* extra read targets are also write targets */
2795                 write_targets += read_targets-1;
2796
2797         if (write_targets == 0 || read_targets == 0) {
2798                 /* There is nowhere to write, so all non-sync
2799                  * drives must be failed - so we are finished
2800                  */
2801                 sector_t rv;
2802                 if (min_bad > 0)
2803                         max_sector = sector_nr + min_bad;
2804                 rv = max_sector - sector_nr;
2805                 *skipped = 1;
2806                 put_buf(r1_bio);
2807                 return rv;
2808         }
2809
2810         if (max_sector > mddev->resync_max)
2811                 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2812         if (max_sector > sector_nr + good_sectors)
2813                 max_sector = sector_nr + good_sectors;
2814         nr_sectors = 0;
2815         sync_blocks = 0;
2816         do {
2817                 struct page *page;
2818                 int len = PAGE_SIZE;
2819                 if (sector_nr + (len>>9) > max_sector)
2820                         len = (max_sector - sector_nr) << 9;
2821                 if (len == 0)
2822                         break;
2823                 if (sync_blocks == 0) {
2824                         if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2825                                                &sync_blocks, still_degraded) &&
2826                             !conf->fullsync &&
2827                             !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2828                                 break;
2829                         if ((len >> 9) > sync_blocks)
2830                                 len = sync_blocks<<9;
2831                 }
2832
2833                 for (i = 0 ; i < conf->raid_disks * 2; i++) {
2834                         struct resync_pages *rp;
2835
2836                         bio = r1_bio->bios[i];
2837                         rp = get_resync_pages(bio);
2838                         if (bio->bi_end_io) {
2839                                 page = resync_fetch_page(rp, page_idx);
2840
2841                                 /*
2842                                  * won't fail because the vec table is big
2843                                  * enough to hold all these pages
2844                                  */
2845                                 bio_add_page(bio, page, len, 0);
2846                         }
2847                 }
2848                 nr_sectors += len>>9;
2849                 sector_nr += len>>9;
2850                 sync_blocks -= (len>>9);
2851         } while (++page_idx < RESYNC_PAGES);
2852
2853         r1_bio->sectors = nr_sectors;
2854
2855         if (mddev_is_clustered(mddev) &&
2856                         conf->cluster_sync_high < sector_nr + nr_sectors) {
2857                 conf->cluster_sync_low = mddev->curr_resync_completed;
2858                 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2859                 /* Send resync message */
2860                 md_cluster_ops->resync_info_update(mddev,
2861                                 conf->cluster_sync_low,
2862                                 conf->cluster_sync_high);
2863         }
2864
2865         /* For a user-requested sync, we read all readable devices and do a
2866          * compare
2867          */
2868         if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2869                 atomic_set(&r1_bio->remaining, read_targets);
2870                 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2871                         bio = r1_bio->bios[i];
2872                         if (bio->bi_end_io == end_sync_read) {
2873                                 read_targets--;
2874                                 md_sync_acct(bio->bi_bdev, nr_sectors);
2875                                 if (read_targets == 1)
2876                                         bio->bi_opf &= ~MD_FAILFAST;
2877                                 generic_make_request(bio);
2878                         }
2879                 }
2880         } else {
2881                 atomic_set(&r1_bio->remaining, 1);
2882                 bio = r1_bio->bios[r1_bio->read_disk];
2883                 md_sync_acct(bio->bi_bdev, nr_sectors);
2884                 if (read_targets == 1)
2885                         bio->bi_opf &= ~MD_FAILFAST;
2886                 generic_make_request(bio);
2887
2888         }
2889         return nr_sectors;
2890 }
2891
2892 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2893 {
2894         if (sectors)
2895                 return sectors;
2896
2897         return mddev->dev_sectors;
2898 }
2899
2900 static struct r1conf *setup_conf(struct mddev *mddev)
2901 {
2902         struct r1conf *conf;
2903         int i;
2904         struct raid1_info *disk;
2905         struct md_rdev *rdev;
2906         int err = -ENOMEM;
2907
2908         conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
2909         if (!conf)
2910                 goto abort;
2911
2912         conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
2913                                    sizeof(atomic_t), GFP_KERNEL);
2914         if (!conf->nr_pending)
2915                 goto abort;
2916
2917         conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
2918                                    sizeof(atomic_t), GFP_KERNEL);
2919         if (!conf->nr_waiting)
2920                 goto abort;
2921
2922         conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
2923                                   sizeof(atomic_t), GFP_KERNEL);
2924         if (!conf->nr_queued)
2925                 goto abort;
2926
2927         conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
2928                                 sizeof(atomic_t), GFP_KERNEL);
2929         if (!conf->barrier)
2930                 goto abort;
2931
2932         conf->mirrors = kzalloc(sizeof(struct raid1_info)
2933                                 * mddev->raid_disks * 2,
2934                                  GFP_KERNEL);
2935         if (!conf->mirrors)
2936                 goto abort;
2937
2938         conf->tmppage = alloc_page(GFP_KERNEL);
2939         if (!conf->tmppage)
2940                 goto abort;
2941
2942         conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2943         if (!conf->poolinfo)
2944                 goto abort;
2945         conf->poolinfo->raid_disks = mddev->raid_disks * 2;
2946         conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2947                                           r1bio_pool_free,
2948                                           conf->poolinfo);
2949         if (!conf->r1bio_pool)
2950                 goto abort;
2951
2952         conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
2953         if (!conf->bio_split)
2954                 goto abort;
2955
2956         conf->poolinfo->mddev = mddev;
2957
2958         err = -EINVAL;
2959         spin_lock_init(&conf->device_lock);
2960         rdev_for_each(rdev, mddev) {
2961                 int disk_idx = rdev->raid_disk;
2962                 if (disk_idx >= mddev->raid_disks
2963                     || disk_idx < 0)
2964                         continue;
2965                 if (test_bit(Replacement, &rdev->flags))
2966                         disk = conf->mirrors + mddev->raid_disks + disk_idx;
2967                 else
2968                         disk = conf->mirrors + disk_idx;
2969
2970                 if (disk->rdev)
2971                         goto abort;
2972                 disk->rdev = rdev;
2973                 disk->head_position = 0;
2974                 disk->seq_start = MaxSector;
2975         }
2976         conf->raid_disks = mddev->raid_disks;
2977         conf->mddev = mddev;
2978         INIT_LIST_HEAD(&conf->retry_list);
2979         INIT_LIST_HEAD(&conf->bio_end_io_list);
2980
2981         spin_lock_init(&conf->resync_lock);
2982         init_waitqueue_head(&conf->wait_barrier);
2983
2984         bio_list_init(&conf->pending_bio_list);
2985         conf->pending_count = 0;
2986         conf->recovery_disabled = mddev->recovery_disabled - 1;
2987
2988         err = -EIO;
2989         for (i = 0; i < conf->raid_disks * 2; i++) {
2990
2991                 disk = conf->mirrors + i;
2992
2993                 if (i < conf->raid_disks &&
2994                     disk[conf->raid_disks].rdev) {
2995                         /* This slot has a replacement. */
2996                         if (!disk->rdev) {
2997                                 /* No original, just make the replacement
2998                                  * a recovering spare
2999                                  */
3000                                 disk->rdev =
3001                                         disk[conf->raid_disks].rdev;
3002                                 disk[conf->raid_disks].rdev = NULL;
3003                         } else if (!test_bit(In_sync, &disk->rdev->flags))
3004                                 /* Original is not in_sync - bad */
3005                                 goto abort;
3006                 }
3007
3008                 if (!disk->rdev ||
3009                     !test_bit(In_sync, &disk->rdev->flags)) {
3010                         disk->head_position = 0;
3011                         if (disk->rdev &&
3012                             (disk->rdev->saved_raid_disk < 0))
3013                                 conf->fullsync = 1;
3014                 }
3015         }
3016
3017         err = -ENOMEM;
3018         conf->thread = md_register_thread(raid1d, mddev, "raid1");
3019         if (!conf->thread)
3020                 goto abort;
3021
3022         return conf;
3023
3024  abort:
3025         if (conf) {
3026                 mempool_destroy(conf->r1bio_pool);
3027                 kfree(conf->mirrors);
3028                 safe_put_page(conf->tmppage);
3029                 kfree(conf->poolinfo);
3030                 kfree(conf->nr_pending);
3031                 kfree(conf->nr_waiting);
3032                 kfree(conf->nr_queued);
3033                 kfree(conf->barrier);
3034                 if (conf->bio_split)
3035                         bioset_free(conf->bio_split);
3036                 kfree(conf);
3037         }
3038         return ERR_PTR(err);
3039 }
3040
3041 static void raid1_free(struct mddev *mddev, void *priv);
3042 static int raid1_run(struct mddev *mddev)
3043 {
3044         struct r1conf *conf;
3045         int i;
3046         struct md_rdev *rdev;
3047         int ret;
3048         bool discard_supported = false;
3049
3050         if (mddev->level != 1) {
3051                 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3052                         mdname(mddev), mddev->level);
3053                 return -EIO;
3054         }
3055         if (mddev->reshape_position != MaxSector) {
3056                 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3057                         mdname(mddev));
3058                 return -EIO;
3059         }
3060         if (mddev_init_writes_pending(mddev) < 0)
3061                 return -ENOMEM;
3062         /*
3063          * copy the already verified devices into our private RAID1
3064          * bookkeeping area. [whatever we allocate in run(),
3065          * should be freed in raid1_free()]
3066          */
3067         if (mddev->private == NULL)
3068                 conf = setup_conf(mddev);
3069         else
3070                 conf = mddev->private;
3071
3072         if (IS_ERR(conf))
3073                 return PTR_ERR(conf);
3074
3075         if (mddev->queue) {
3076                 blk_queue_max_write_same_sectors(mddev->queue, 0);
3077                 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3078         }
3079
3080         rdev_for_each(rdev, mddev) {
3081                 if (!mddev->gendisk)
3082                         continue;
3083                 disk_stack_limits(mddev->gendisk, rdev->bdev,
3084                                   rdev->data_offset << 9);
3085                 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3086                         discard_supported = true;
3087         }
3088
3089         mddev->degraded = 0;
3090         for (i=0; i < conf->raid_disks; i++)
3091                 if (conf->mirrors[i].rdev == NULL ||
3092                     !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3093                     test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3094                         mddev->degraded++;
3095
3096         if (conf->raid_disks - mddev->degraded == 1)
3097                 mddev->recovery_cp = MaxSector;
3098
3099         if (mddev->recovery_cp != MaxSector)
3100                 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3101                         mdname(mddev));
3102         pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
3103                 mdname(mddev), mddev->raid_disks - mddev->degraded,
3104                 mddev->raid_disks);
3105
3106         /*
3107          * Ok, everything is just fine now
3108          */
3109         mddev->thread = conf->thread;
3110         conf->thread = NULL;
3111         mddev->private = conf;
3112         set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3113
3114         md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
3115
3116         if (mddev->queue) {
3117                 if (discard_supported)
3118                         queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3119                                                 mddev->queue);
3120                 else
3121                         queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3122                                                   mddev->queue);
3123         }
3124
3125         ret =  md_integrity_register(mddev);
3126         if (ret) {
3127                 md_unregister_thread(&mddev->thread);
3128                 raid1_free(mddev, conf);
3129         }
3130         return ret;
3131 }
3132
3133 static void raid1_free(struct mddev *mddev, void *priv)
3134 {
3135         struct r1conf *conf = priv;
3136
3137         mempool_destroy(conf->r1bio_pool);
3138         kfree(conf->mirrors);
3139         safe_put_page(conf->tmppage);
3140         kfree(conf->poolinfo);
3141         kfree(conf->nr_pending);
3142         kfree(conf->nr_waiting);
3143         kfree(conf->nr_queued);
3144         kfree(conf->barrier);
3145         if (conf->bio_split)
3146                 bioset_free(conf->bio_split);
3147         kfree(conf);
3148 }
3149
3150 static int raid1_resize(struct mddev *mddev, sector_t sectors)
3151 {
3152         /* no resync is happening, and there is enough space
3153          * on all devices, so we can resize.
3154          * We need to make sure resync covers any new space.
3155          * If the array is shrinking we should possibly wait until
3156          * any io in the removed space completes, but it hardly seems
3157          * worth it.
3158          */
3159         sector_t newsize = raid1_size(mddev, sectors, 0);
3160         if (mddev->external_size &&
3161             mddev->array_sectors > newsize)
3162                 return -EINVAL;
3163         if (mddev->bitmap) {
3164                 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3165                 if (ret)
3166                         return ret;
3167         }
3168         md_set_array_sectors(mddev, newsize);
3169         if (sectors > mddev->dev_sectors &&
3170             mddev->recovery_cp > mddev->dev_sectors) {
3171                 mddev->recovery_cp = mddev->dev_sectors;
3172                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3173         }
3174         mddev->dev_sectors = sectors;
3175         mddev->resync_max_sectors = sectors;
3176         return 0;
3177 }
3178
3179 static int raid1_reshape(struct mddev *mddev)
3180 {
3181         /* We need to:
3182          * 1/ resize the r1bio_pool
3183          * 2/ resize conf->mirrors
3184          *
3185          * We allocate a new r1bio_pool if we can.
3186          * Then raise a device barrier and wait until all IO stops.
3187          * Then resize conf->mirrors and swap in the new r1bio pool.
3188          *
3189          * At the same time, we "pack" the devices so that all the missing
3190          * devices have the higher raid_disk numbers.
3191          */
3192         mempool_t *newpool, *oldpool;
3193         struct pool_info *newpoolinfo;
3194         struct raid1_info *newmirrors;
3195         struct r1conf *conf = mddev->private;
3196         int cnt, raid_disks;
3197         unsigned long flags;
3198         int d, d2;
3199
3200         /* Cannot change chunk_size, layout, or level */
3201         if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
3202             mddev->layout != mddev->new_layout ||
3203             mddev->level != mddev->new_level) {
3204                 mddev->new_chunk_sectors = mddev->chunk_sectors;
3205                 mddev->new_layout = mddev->layout;
3206                 mddev->new_level = mddev->level;
3207                 return -EINVAL;
3208         }
3209
3210         if (!mddev_is_clustered(mddev))
3211                 md_allow_write(mddev);
3212
3213         raid_disks = mddev->raid_disks + mddev->delta_disks;
3214
3215         if (raid_disks < conf->raid_disks) {
3216                 cnt=0;
3217                 for (d= 0; d < conf->raid_disks; d++)
3218                         if (conf->mirrors[d].rdev)
3219                                 cnt++;
3220                 if (cnt > raid_disks)
3221                         return -EBUSY;
3222         }
3223
3224         newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3225         if (!newpoolinfo)
3226                 return -ENOMEM;
3227         newpoolinfo->mddev = mddev;
3228         newpoolinfo->raid_disks = raid_disks * 2;
3229
3230         newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3231                                  r1bio_pool_free, newpoolinfo);
3232         if (!newpool) {
3233                 kfree(newpoolinfo);
3234                 return -ENOMEM;
3235         }
3236         newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
3237                              GFP_KERNEL);
3238         if (!newmirrors) {
3239                 kfree(newpoolinfo);
3240                 mempool_destroy(newpool);
3241                 return -ENOMEM;
3242         }
3243
3244         freeze_array(conf, 0);
3245
3246         /* ok, everything is stopped */
3247         oldpool = conf->r1bio_pool;
3248         conf->r1bio_pool = newpool;
3249
3250         for (d = d2 = 0; d < conf->raid_disks; d++) {
3251                 struct md_rdev *rdev = conf->mirrors[d].rdev;
3252                 if (rdev && rdev->raid_disk != d2) {
3253                         sysfs_unlink_rdev(mddev, rdev);
3254                         rdev->raid_disk = d2;
3255                         sysfs_unlink_rdev(mddev, rdev);
3256                         if (sysfs_link_rdev(mddev, rdev))
3257                                 pr_warn("md/raid1:%s: cannot register rd%d\n",
3258                                         mdname(mddev), rdev->raid_disk);
3259                 }
3260                 if (rdev)
3261                         newmirrors[d2++].rdev = rdev;
3262         }
3263         kfree(conf->mirrors);
3264         conf->mirrors = newmirrors;
3265         kfree(conf->poolinfo);
3266         conf->poolinfo = newpoolinfo;
3267
3268         spin_lock_irqsave(&conf->device_lock, flags);
3269         mddev->degraded += (raid_disks - conf->raid_disks);
3270         spin_unlock_irqrestore(&conf->device_lock, flags);
3271         conf->raid_disks = mddev->raid_disks = raid_disks;
3272         mddev->delta_disks = 0;
3273
3274         unfreeze_array(conf);
3275
3276         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3277         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3278         md_wakeup_thread(mddev->thread);
3279
3280         mempool_destroy(oldpool);
3281         return 0;
3282 }
3283
3284 static void raid1_quiesce(struct mddev *mddev, int state)
3285 {
3286         struct r1conf *conf = mddev->private;
3287
3288         switch(state) {
3289         case 2: /* wake for suspend */
3290                 wake_up(&conf->wait_barrier);
3291                 break;
3292         case 1:
3293                 freeze_array(conf, 0);
3294                 break;
3295         case 0:
3296                 unfreeze_array(conf);
3297                 break;
3298         }
3299 }
3300
3301 static void *raid1_takeover(struct mddev *mddev)
3302 {
3303         /* raid1 can take over:
3304          *  raid5 with 2 devices, any layout or chunk size
3305          */
3306         if (mddev->level == 5 && mddev->raid_disks == 2) {
3307                 struct r1conf *conf;
3308                 mddev->new_level = 1;
3309                 mddev->new_layout = 0;
3310                 mddev->new_chunk_sectors = 0;
3311                 conf = setup_conf(mddev);
3312                 if (!IS_ERR(conf)) {
3313                         /* Array must appear to be quiesced */
3314                         conf->array_frozen = 1;
3315                         mddev_clear_unsupported_flags(mddev,
3316                                 UNSUPPORTED_MDDEV_FLAGS);
3317                 }
3318                 return conf;
3319         }
3320         return ERR_PTR(-EINVAL);
3321 }
3322
3323 static struct md_personality raid1_personality =
3324 {
3325         .name           = "raid1",
3326         .level          = 1,
3327         .owner          = THIS_MODULE,
3328         .make_request   = raid1_make_request,
3329         .run            = raid1_run,
3330         .free           = raid1_free,
3331         .status         = raid1_status,
3332         .error_handler  = raid1_error,
3333         .hot_add_disk   = raid1_add_disk,
3334         .hot_remove_disk= raid1_remove_disk,
3335         .spare_active   = raid1_spare_active,
3336         .sync_request   = raid1_sync_request,
3337         .resize         = raid1_resize,
3338         .size           = raid1_size,
3339         .check_reshape  = raid1_reshape,
3340         .quiesce        = raid1_quiesce,
3341         .takeover       = raid1_takeover,
3342         .congested      = raid1_congested,
3343 };
3344
3345 static int __init raid_init(void)
3346 {
3347         return register_md_personality(&raid1_personality);
3348 }
3349
3350 static void raid_exit(void)
3351 {
3352         unregister_md_personality(&raid1_personality);
3353 }
3354
3355 module_init(raid_init);
3356 module_exit(raid_exit);
3357 MODULE_LICENSE("GPL");
3358 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3359 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3360 MODULE_ALIAS("md-raid1");
3361 MODULE_ALIAS("md-level-1");
3362
3363 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);