Merge remote-tracking branch 'net/master'
[linux-2.6-microblaze.git] / drivers / md / raid5-ppl.c
1 /*
2  * Partial Parity Log for closing the RAID5 write hole
3  * Copyright (c) 2017, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/blkdev.h>
17 #include <linux/slab.h>
18 #include <linux/crc32c.h>
19 #include <linux/async_tx.h>
20 #include <linux/raid/md_p.h>
21 #include "md.h"
22 #include "raid5.h"
23
24 /*
25  * PPL consists of a 4KB header (struct ppl_header) and at least 128KB for
26  * partial parity data. The header contains an array of entries
27  * (struct ppl_header_entry) which describe the logged write requests.
28  * Partial parity for the entries comes after the header, written in the same
29  * sequence as the entries:
30  *
31  * Header
32  *   entry0
33  *   ...
34  *   entryN
35  * PP data
36  *   PP for entry0
37  *   ...
38  *   PP for entryN
39  *
40  * An entry describes one or more consecutive stripe_heads, up to a full
41  * stripe. The modifed raid data chunks form an m-by-n matrix, where m is the
42  * number of stripe_heads in the entry and n is the number of modified data
43  * disks. Every stripe_head in the entry must write to the same data disks.
44  * An example of a valid case described by a single entry (writes to the first
45  * stripe of a 4 disk array, 16k chunk size):
46  *
47  * sh->sector   dd0   dd1   dd2    ppl
48  *            +-----+-----+-----+
49  * 0          | --- | --- | --- | +----+
50  * 8          | -W- | -W- | --- | | pp |   data_sector = 8
51  * 16         | -W- | -W- | --- | | pp |   data_size = 3 * 2 * 4k
52  * 24         | -W- | -W- | --- | | pp |   pp_size = 3 * 4k
53  *            +-----+-----+-----+ +----+
54  *
55  * data_sector is the first raid sector of the modified data, data_size is the
56  * total size of modified data and pp_size is the size of partial parity for
57  * this entry. Entries for full stripe writes contain no partial parity
58  * (pp_size = 0), they only mark the stripes for which parity should be
59  * recalculated after an unclean shutdown. Every entry holds a checksum of its
60  * partial parity, the header also has a checksum of the header itself.
61  *
62  * A write request is always logged to the PPL instance stored on the parity
63  * disk of the corresponding stripe. For each member disk there is one ppl_log
64  * used to handle logging for this disk, independently from others. They are
65  * grouped in child_logs array in struct ppl_conf, which is assigned to
66  * r5conf->log_private.
67  *
68  * ppl_io_unit represents a full PPL write, header_page contains the ppl_header.
69  * PPL entries for logged stripes are added in ppl_log_stripe(). A stripe_head
70  * can be appended to the last entry if it meets the conditions for a valid
71  * entry described above, otherwise a new entry is added. Checksums of entries
72  * are calculated incrementally as stripes containing partial parity are being
73  * added. ppl_submit_iounit() calculates the checksum of the header and submits
74  * a bio containing the header page and partial parity pages (sh->ppl_page) for
75  * all stripes of the io_unit. When the PPL write completes, the stripes
76  * associated with the io_unit are released and raid5d starts writing their data
77  * and parity. When all stripes are written, the io_unit is freed and the next
78  * can be submitted.
79  *
80  * An io_unit is used to gather stripes until it is submitted or becomes full
81  * (if the maximum number of entries or size of PPL is reached). Another io_unit
82  * can't be submitted until the previous has completed (PPL and stripe
83  * data+parity is written). The log->io_list tracks all io_units of a log
84  * (for a single member disk). New io_units are added to the end of the list
85  * and the first io_unit is submitted, if it is not submitted already.
86  * The current io_unit accepting new stripes is always at the end of the list.
87  *
88  * If write-back cache is enabled for any of the disks in the array, its data
89  * must be flushed before next io_unit is submitted.
90  */
91
92 #define PPL_SPACE_SIZE (128 * 1024)
93
94 struct ppl_conf {
95         struct mddev *mddev;
96
97         /* array of child logs, one for each raid disk */
98         struct ppl_log *child_logs;
99         int count;
100
101         int block_size;         /* the logical block size used for data_sector
102                                  * in ppl_header_entry */
103         u32 signature;          /* raid array identifier */
104         atomic64_t seq;         /* current log write sequence number */
105
106         struct kmem_cache *io_kc;
107         mempool_t io_pool;
108         struct bio_set bs;
109         struct bio_set flush_bs;
110
111         /* used only for recovery */
112         int recovered_entries;
113         int mismatch_count;
114
115         /* stripes to retry if failed to allocate io_unit */
116         struct list_head no_mem_stripes;
117         spinlock_t no_mem_stripes_lock;
118 };
119
120 struct ppl_log {
121         struct ppl_conf *ppl_conf;      /* shared between all log instances */
122
123         struct md_rdev *rdev;           /* array member disk associated with
124                                          * this log instance */
125         struct mutex io_mutex;
126         struct ppl_io_unit *current_io; /* current io_unit accepting new data
127                                          * always at the end of io_list */
128         spinlock_t io_list_lock;
129         struct list_head io_list;       /* all io_units of this log */
130
131         sector_t next_io_sector;
132         unsigned int entry_space;
133         bool use_multippl;
134         bool wb_cache_on;
135         unsigned long disk_flush_bitmap;
136 };
137
138 #define PPL_IO_INLINE_BVECS 32
139
140 struct ppl_io_unit {
141         struct ppl_log *log;
142
143         struct page *header_page;       /* for ppl_header */
144
145         unsigned int entries_count;     /* number of entries in ppl_header */
146         unsigned int pp_size;           /* total size current of partial parity */
147
148         u64 seq;                        /* sequence number of this log write */
149         struct list_head log_sibling;   /* log->io_list */
150
151         struct list_head stripe_list;   /* stripes added to the io_unit */
152         atomic_t pending_stripes;       /* how many stripes not written to raid */
153         atomic_t pending_flushes;       /* how many disk flushes are in progress */
154
155         bool submitted;                 /* true if write to log started */
156
157         /* inline bio and its biovec for submitting the iounit */
158         struct bio bio;
159         struct bio_vec biovec[PPL_IO_INLINE_BVECS];
160 };
161
162 struct dma_async_tx_descriptor *
163 ops_run_partial_parity(struct stripe_head *sh, struct raid5_percpu *percpu,
164                        struct dma_async_tx_descriptor *tx)
165 {
166         int disks = sh->disks;
167         struct page **srcs = percpu->scribble;
168         int count = 0, pd_idx = sh->pd_idx, i;
169         struct async_submit_ctl submit;
170
171         pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
172
173         /*
174          * Partial parity is the XOR of stripe data chunks that are not changed
175          * during the write request. Depending on available data
176          * (read-modify-write vs. reconstruct-write case) we calculate it
177          * differently.
178          */
179         if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
180                 /*
181                  * rmw: xor old data and parity from updated disks
182                  * This is calculated earlier by ops_run_prexor5() so just copy
183                  * the parity dev page.
184                  */
185                 srcs[count++] = sh->dev[pd_idx].page;
186         } else if (sh->reconstruct_state == reconstruct_state_drain_run) {
187                 /* rcw: xor data from all not updated disks */
188                 for (i = disks; i--;) {
189                         struct r5dev *dev = &sh->dev[i];
190                         if (test_bit(R5_UPTODATE, &dev->flags))
191                                 srcs[count++] = dev->page;
192                 }
193         } else {
194                 return tx;
195         }
196
197         init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, tx,
198                           NULL, sh, (void *) (srcs + sh->disks + 2));
199
200         if (count == 1)
201                 tx = async_memcpy(sh->ppl_page, srcs[0], 0, 0, PAGE_SIZE,
202                                   &submit);
203         else
204                 tx = async_xor(sh->ppl_page, srcs, 0, count, PAGE_SIZE,
205                                &submit);
206
207         return tx;
208 }
209
210 static void *ppl_io_pool_alloc(gfp_t gfp_mask, void *pool_data)
211 {
212         struct kmem_cache *kc = pool_data;
213         struct ppl_io_unit *io;
214
215         io = kmem_cache_alloc(kc, gfp_mask);
216         if (!io)
217                 return NULL;
218
219         io->header_page = alloc_page(gfp_mask);
220         if (!io->header_page) {
221                 kmem_cache_free(kc, io);
222                 return NULL;
223         }
224
225         return io;
226 }
227
228 static void ppl_io_pool_free(void *element, void *pool_data)
229 {
230         struct kmem_cache *kc = pool_data;
231         struct ppl_io_unit *io = element;
232
233         __free_page(io->header_page);
234         kmem_cache_free(kc, io);
235 }
236
237 static struct ppl_io_unit *ppl_new_iounit(struct ppl_log *log,
238                                           struct stripe_head *sh)
239 {
240         struct ppl_conf *ppl_conf = log->ppl_conf;
241         struct ppl_io_unit *io;
242         struct ppl_header *pplhdr;
243         struct page *header_page;
244
245         io = mempool_alloc(&ppl_conf->io_pool, GFP_NOWAIT);
246         if (!io)
247                 return NULL;
248
249         header_page = io->header_page;
250         memset(io, 0, sizeof(*io));
251         io->header_page = header_page;
252
253         io->log = log;
254         INIT_LIST_HEAD(&io->log_sibling);
255         INIT_LIST_HEAD(&io->stripe_list);
256         atomic_set(&io->pending_stripes, 0);
257         atomic_set(&io->pending_flushes, 0);
258         bio_init(&io->bio, io->biovec, PPL_IO_INLINE_BVECS);
259
260         pplhdr = page_address(io->header_page);
261         clear_page(pplhdr);
262         memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
263         pplhdr->signature = cpu_to_le32(ppl_conf->signature);
264
265         io->seq = atomic64_add_return(1, &ppl_conf->seq);
266         pplhdr->generation = cpu_to_le64(io->seq);
267
268         return io;
269 }
270
271 static int ppl_log_stripe(struct ppl_log *log, struct stripe_head *sh)
272 {
273         struct ppl_io_unit *io = log->current_io;
274         struct ppl_header_entry *e = NULL;
275         struct ppl_header *pplhdr;
276         int i;
277         sector_t data_sector = 0;
278         int data_disks = 0;
279         struct r5conf *conf = sh->raid_conf;
280
281         pr_debug("%s: stripe: %llu\n", __func__, (unsigned long long)sh->sector);
282
283         /* check if current io_unit is full */
284         if (io && (io->pp_size == log->entry_space ||
285                    io->entries_count == PPL_HDR_MAX_ENTRIES)) {
286                 pr_debug("%s: add io_unit blocked by seq: %llu\n",
287                          __func__, io->seq);
288                 io = NULL;
289         }
290
291         /* add a new unit if there is none or the current is full */
292         if (!io) {
293                 io = ppl_new_iounit(log, sh);
294                 if (!io)
295                         return -ENOMEM;
296                 spin_lock_irq(&log->io_list_lock);
297                 list_add_tail(&io->log_sibling, &log->io_list);
298                 spin_unlock_irq(&log->io_list_lock);
299
300                 log->current_io = io;
301         }
302
303         for (i = 0; i < sh->disks; i++) {
304                 struct r5dev *dev = &sh->dev[i];
305
306                 if (i != sh->pd_idx && test_bit(R5_Wantwrite, &dev->flags)) {
307                         if (!data_disks || dev->sector < data_sector)
308                                 data_sector = dev->sector;
309                         data_disks++;
310                 }
311         }
312         BUG_ON(!data_disks);
313
314         pr_debug("%s: seq: %llu data_sector: %llu data_disks: %d\n", __func__,
315                  io->seq, (unsigned long long)data_sector, data_disks);
316
317         pplhdr = page_address(io->header_page);
318
319         if (io->entries_count > 0) {
320                 struct ppl_header_entry *last =
321                                 &pplhdr->entries[io->entries_count - 1];
322                 struct stripe_head *sh_last = list_last_entry(
323                                 &io->stripe_list, struct stripe_head, log_list);
324                 u64 data_sector_last = le64_to_cpu(last->data_sector);
325                 u32 data_size_last = le32_to_cpu(last->data_size);
326
327                 /*
328                  * Check if we can append the stripe to the last entry. It must
329                  * be just after the last logged stripe and write to the same
330                  * disks. Use bit shift and logarithm to avoid 64-bit division.
331                  */
332                 if ((sh->sector == sh_last->sector + STRIPE_SECTORS) &&
333                     (data_sector >> ilog2(conf->chunk_sectors) ==
334                      data_sector_last >> ilog2(conf->chunk_sectors)) &&
335                     ((data_sector - data_sector_last) * data_disks ==
336                      data_size_last >> 9))
337                         e = last;
338         }
339
340         if (!e) {
341                 e = &pplhdr->entries[io->entries_count++];
342                 e->data_sector = cpu_to_le64(data_sector);
343                 e->parity_disk = cpu_to_le32(sh->pd_idx);
344                 e->checksum = cpu_to_le32(~0);
345         }
346
347         le32_add_cpu(&e->data_size, data_disks << PAGE_SHIFT);
348
349         /* don't write any PP if full stripe write */
350         if (!test_bit(STRIPE_FULL_WRITE, &sh->state)) {
351                 le32_add_cpu(&e->pp_size, PAGE_SIZE);
352                 io->pp_size += PAGE_SIZE;
353                 e->checksum = cpu_to_le32(crc32c_le(le32_to_cpu(e->checksum),
354                                                     page_address(sh->ppl_page),
355                                                     PAGE_SIZE));
356         }
357
358         list_add_tail(&sh->log_list, &io->stripe_list);
359         atomic_inc(&io->pending_stripes);
360         sh->ppl_io = io;
361
362         return 0;
363 }
364
365 int ppl_write_stripe(struct r5conf *conf, struct stripe_head *sh)
366 {
367         struct ppl_conf *ppl_conf = conf->log_private;
368         struct ppl_io_unit *io = sh->ppl_io;
369         struct ppl_log *log;
370
371         if (io || test_bit(STRIPE_SYNCING, &sh->state) || !sh->ppl_page ||
372             !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
373             !test_bit(R5_Insync, &sh->dev[sh->pd_idx].flags)) {
374                 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
375                 return -EAGAIN;
376         }
377
378         log = &ppl_conf->child_logs[sh->pd_idx];
379
380         mutex_lock(&log->io_mutex);
381
382         if (!log->rdev || test_bit(Faulty, &log->rdev->flags)) {
383                 mutex_unlock(&log->io_mutex);
384                 return -EAGAIN;
385         }
386
387         set_bit(STRIPE_LOG_TRAPPED, &sh->state);
388         clear_bit(STRIPE_DELAYED, &sh->state);
389         atomic_inc(&sh->count);
390
391         if (ppl_log_stripe(log, sh)) {
392                 spin_lock_irq(&ppl_conf->no_mem_stripes_lock);
393                 list_add_tail(&sh->log_list, &ppl_conf->no_mem_stripes);
394                 spin_unlock_irq(&ppl_conf->no_mem_stripes_lock);
395         }
396
397         mutex_unlock(&log->io_mutex);
398
399         return 0;
400 }
401
402 static void ppl_log_endio(struct bio *bio)
403 {
404         struct ppl_io_unit *io = bio->bi_private;
405         struct ppl_log *log = io->log;
406         struct ppl_conf *ppl_conf = log->ppl_conf;
407         struct stripe_head *sh, *next;
408
409         pr_debug("%s: seq: %llu\n", __func__, io->seq);
410
411         if (bio->bi_status)
412                 md_error(ppl_conf->mddev, log->rdev);
413
414         list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
415                 list_del_init(&sh->log_list);
416
417                 set_bit(STRIPE_HANDLE, &sh->state);
418                 raid5_release_stripe(sh);
419         }
420 }
421
422 static void ppl_submit_iounit_bio(struct ppl_io_unit *io, struct bio *bio)
423 {
424         char b[BDEVNAME_SIZE];
425
426         pr_debug("%s: seq: %llu size: %u sector: %llu dev: %s\n",
427                  __func__, io->seq, bio->bi_iter.bi_size,
428                  (unsigned long long)bio->bi_iter.bi_sector,
429                  bio_devname(bio, b));
430
431         submit_bio(bio);
432 }
433
434 static void ppl_submit_iounit(struct ppl_io_unit *io)
435 {
436         struct ppl_log *log = io->log;
437         struct ppl_conf *ppl_conf = log->ppl_conf;
438         struct ppl_header *pplhdr = page_address(io->header_page);
439         struct bio *bio = &io->bio;
440         struct stripe_head *sh;
441         int i;
442
443         bio->bi_private = io;
444
445         if (!log->rdev || test_bit(Faulty, &log->rdev->flags)) {
446                 ppl_log_endio(bio);
447                 return;
448         }
449
450         for (i = 0; i < io->entries_count; i++) {
451                 struct ppl_header_entry *e = &pplhdr->entries[i];
452
453                 pr_debug("%s: seq: %llu entry: %d data_sector: %llu pp_size: %u data_size: %u\n",
454                          __func__, io->seq, i, le64_to_cpu(e->data_sector),
455                          le32_to_cpu(e->pp_size), le32_to_cpu(e->data_size));
456
457                 e->data_sector = cpu_to_le64(le64_to_cpu(e->data_sector) >>
458                                              ilog2(ppl_conf->block_size >> 9));
459                 e->checksum = cpu_to_le32(~le32_to_cpu(e->checksum));
460         }
461
462         pplhdr->entries_count = cpu_to_le32(io->entries_count);
463         pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PPL_HEADER_SIZE));
464
465         /* Rewind the buffer if current PPL is larger then remaining space */
466         if (log->use_multippl &&
467             log->rdev->ppl.sector + log->rdev->ppl.size - log->next_io_sector <
468             (PPL_HEADER_SIZE + io->pp_size) >> 9)
469                 log->next_io_sector = log->rdev->ppl.sector;
470
471
472         bio->bi_end_io = ppl_log_endio;
473         bio->bi_opf = REQ_OP_WRITE | REQ_FUA;
474         bio_set_dev(bio, log->rdev->bdev);
475         bio->bi_iter.bi_sector = log->next_io_sector;
476         bio_add_page(bio, io->header_page, PAGE_SIZE, 0);
477
478         pr_debug("%s: log->current_io_sector: %llu\n", __func__,
479             (unsigned long long)log->next_io_sector);
480
481         if (log->use_multippl)
482                 log->next_io_sector += (PPL_HEADER_SIZE + io->pp_size) >> 9;
483
484         WARN_ON(log->disk_flush_bitmap != 0);
485
486         list_for_each_entry(sh, &io->stripe_list, log_list) {
487                 for (i = 0; i < sh->disks; i++) {
488                         struct r5dev *dev = &sh->dev[i];
489
490                         if ((ppl_conf->child_logs[i].wb_cache_on) &&
491                             (test_bit(R5_Wantwrite, &dev->flags))) {
492                                 set_bit(i, &log->disk_flush_bitmap);
493                         }
494                 }
495
496                 /* entries for full stripe writes have no partial parity */
497                 if (test_bit(STRIPE_FULL_WRITE, &sh->state))
498                         continue;
499
500                 if (!bio_add_page(bio, sh->ppl_page, PAGE_SIZE, 0)) {
501                         struct bio *prev = bio;
502
503                         bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES,
504                                                &ppl_conf->bs);
505                         bio->bi_opf = prev->bi_opf;
506                         bio_copy_dev(bio, prev);
507                         bio->bi_iter.bi_sector = bio_end_sector(prev);
508                         bio_add_page(bio, sh->ppl_page, PAGE_SIZE, 0);
509
510                         bio_chain(bio, prev);
511                         ppl_submit_iounit_bio(io, prev);
512                 }
513         }
514
515         ppl_submit_iounit_bio(io, bio);
516 }
517
518 static void ppl_submit_current_io(struct ppl_log *log)
519 {
520         struct ppl_io_unit *io;
521
522         spin_lock_irq(&log->io_list_lock);
523
524         io = list_first_entry_or_null(&log->io_list, struct ppl_io_unit,
525                                       log_sibling);
526         if (io && io->submitted)
527                 io = NULL;
528
529         spin_unlock_irq(&log->io_list_lock);
530
531         if (io) {
532                 io->submitted = true;
533
534                 if (io == log->current_io)
535                         log->current_io = NULL;
536
537                 ppl_submit_iounit(io);
538         }
539 }
540
541 void ppl_write_stripe_run(struct r5conf *conf)
542 {
543         struct ppl_conf *ppl_conf = conf->log_private;
544         struct ppl_log *log;
545         int i;
546
547         for (i = 0; i < ppl_conf->count; i++) {
548                 log = &ppl_conf->child_logs[i];
549
550                 mutex_lock(&log->io_mutex);
551                 ppl_submit_current_io(log);
552                 mutex_unlock(&log->io_mutex);
553         }
554 }
555
556 static void ppl_io_unit_finished(struct ppl_io_unit *io)
557 {
558         struct ppl_log *log = io->log;
559         struct ppl_conf *ppl_conf = log->ppl_conf;
560         struct r5conf *conf = ppl_conf->mddev->private;
561         unsigned long flags;
562
563         pr_debug("%s: seq: %llu\n", __func__, io->seq);
564
565         local_irq_save(flags);
566
567         spin_lock(&log->io_list_lock);
568         list_del(&io->log_sibling);
569         spin_unlock(&log->io_list_lock);
570
571         mempool_free(io, &ppl_conf->io_pool);
572
573         spin_lock(&ppl_conf->no_mem_stripes_lock);
574         if (!list_empty(&ppl_conf->no_mem_stripes)) {
575                 struct stripe_head *sh;
576
577                 sh = list_first_entry(&ppl_conf->no_mem_stripes,
578                                       struct stripe_head, log_list);
579                 list_del_init(&sh->log_list);
580                 set_bit(STRIPE_HANDLE, &sh->state);
581                 raid5_release_stripe(sh);
582         }
583         spin_unlock(&ppl_conf->no_mem_stripes_lock);
584
585         local_irq_restore(flags);
586
587         wake_up(&conf->wait_for_quiescent);
588 }
589
590 static void ppl_flush_endio(struct bio *bio)
591 {
592         struct ppl_io_unit *io = bio->bi_private;
593         struct ppl_log *log = io->log;
594         struct ppl_conf *ppl_conf = log->ppl_conf;
595         struct r5conf *conf = ppl_conf->mddev->private;
596         char b[BDEVNAME_SIZE];
597
598         pr_debug("%s: dev: %s\n", __func__, bio_devname(bio, b));
599
600         if (bio->bi_status) {
601                 struct md_rdev *rdev;
602
603                 rcu_read_lock();
604                 rdev = md_find_rdev_rcu(conf->mddev, bio_dev(bio));
605                 if (rdev)
606                         md_error(rdev->mddev, rdev);
607                 rcu_read_unlock();
608         }
609
610         bio_put(bio);
611
612         if (atomic_dec_and_test(&io->pending_flushes)) {
613                 ppl_io_unit_finished(io);
614                 md_wakeup_thread(conf->mddev->thread);
615         }
616 }
617
618 static void ppl_do_flush(struct ppl_io_unit *io)
619 {
620         struct ppl_log *log = io->log;
621         struct ppl_conf *ppl_conf = log->ppl_conf;
622         struct r5conf *conf = ppl_conf->mddev->private;
623         int raid_disks = conf->raid_disks;
624         int flushed_disks = 0;
625         int i;
626
627         atomic_set(&io->pending_flushes, raid_disks);
628
629         for_each_set_bit(i, &log->disk_flush_bitmap, raid_disks) {
630                 struct md_rdev *rdev;
631                 struct block_device *bdev = NULL;
632
633                 rcu_read_lock();
634                 rdev = rcu_dereference(conf->disks[i].rdev);
635                 if (rdev && !test_bit(Faulty, &rdev->flags))
636                         bdev = rdev->bdev;
637                 rcu_read_unlock();
638
639                 if (bdev) {
640                         struct bio *bio;
641                         char b[BDEVNAME_SIZE];
642
643                         bio = bio_alloc_bioset(GFP_NOIO, 0, &ppl_conf->flush_bs);
644                         bio_set_dev(bio, bdev);
645                         bio->bi_private = io;
646                         bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
647                         bio->bi_end_io = ppl_flush_endio;
648
649                         pr_debug("%s: dev: %s\n", __func__,
650                                  bio_devname(bio, b));
651
652                         submit_bio(bio);
653                         flushed_disks++;
654                 }
655         }
656
657         log->disk_flush_bitmap = 0;
658
659         for (i = flushed_disks ; i < raid_disks; i++) {
660                 if (atomic_dec_and_test(&io->pending_flushes))
661                         ppl_io_unit_finished(io);
662         }
663 }
664
665 static inline bool ppl_no_io_unit_submitted(struct r5conf *conf,
666                                             struct ppl_log *log)
667 {
668         struct ppl_io_unit *io;
669
670         io = list_first_entry_or_null(&log->io_list, struct ppl_io_unit,
671                                       log_sibling);
672
673         return !io || !io->submitted;
674 }
675
676 void ppl_quiesce(struct r5conf *conf, int quiesce)
677 {
678         struct ppl_conf *ppl_conf = conf->log_private;
679         int i;
680
681         if (quiesce) {
682                 for (i = 0; i < ppl_conf->count; i++) {
683                         struct ppl_log *log = &ppl_conf->child_logs[i];
684
685                         spin_lock_irq(&log->io_list_lock);
686                         wait_event_lock_irq(conf->wait_for_quiescent,
687                                             ppl_no_io_unit_submitted(conf, log),
688                                             log->io_list_lock);
689                         spin_unlock_irq(&log->io_list_lock);
690                 }
691         }
692 }
693
694 int ppl_handle_flush_request(struct r5l_log *log, struct bio *bio)
695 {
696         if (bio->bi_iter.bi_size == 0) {
697                 bio_endio(bio);
698                 return 0;
699         }
700         bio->bi_opf &= ~REQ_PREFLUSH;
701         return -EAGAIN;
702 }
703
704 void ppl_stripe_write_finished(struct stripe_head *sh)
705 {
706         struct ppl_io_unit *io;
707
708         io = sh->ppl_io;
709         sh->ppl_io = NULL;
710
711         if (io && atomic_dec_and_test(&io->pending_stripes)) {
712                 if (io->log->disk_flush_bitmap)
713                         ppl_do_flush(io);
714                 else
715                         ppl_io_unit_finished(io);
716         }
717 }
718
719 static void ppl_xor(int size, struct page *page1, struct page *page2)
720 {
721         struct async_submit_ctl submit;
722         struct dma_async_tx_descriptor *tx;
723         struct page *xor_srcs[] = { page1, page2 };
724
725         init_async_submit(&submit, ASYNC_TX_ACK|ASYNC_TX_XOR_DROP_DST,
726                           NULL, NULL, NULL, NULL);
727         tx = async_xor(page1, xor_srcs, 0, 2, size, &submit);
728
729         async_tx_quiesce(&tx);
730 }
731
732 /*
733  * PPL recovery strategy: xor partial parity and data from all modified data
734  * disks within a stripe and write the result as the new stripe parity. If all
735  * stripe data disks are modified (full stripe write), no partial parity is
736  * available, so just xor the data disks.
737  *
738  * Recovery of a PPL entry shall occur only if all modified data disks are
739  * available and read from all of them succeeds.
740  *
741  * A PPL entry applies to a stripe, partial parity size for an entry is at most
742  * the size of the chunk. Examples of possible cases for a single entry:
743  *
744  * case 0: single data disk write:
745  *   data0    data1    data2     ppl        parity
746  * +--------+--------+--------+           +--------------------+
747  * | ------ | ------ | ------ | +----+    | (no change)        |
748  * | ------ | -data- | ------ | | pp | -> | data1 ^ pp         |
749  * | ------ | -data- | ------ | | pp | -> | data1 ^ pp         |
750  * | ------ | ------ | ------ | +----+    | (no change)        |
751  * +--------+--------+--------+           +--------------------+
752  * pp_size = data_size
753  *
754  * case 1: more than one data disk write:
755  *   data0    data1    data2     ppl        parity
756  * +--------+--------+--------+           +--------------------+
757  * | ------ | ------ | ------ | +----+    | (no change)        |
758  * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
759  * | -data- | -data- | ------ | | pp | -> | data0 ^ data1 ^ pp |
760  * | ------ | ------ | ------ | +----+    | (no change)        |
761  * +--------+--------+--------+           +--------------------+
762  * pp_size = data_size / modified_data_disks
763  *
764  * case 2: write to all data disks (also full stripe write):
765  *   data0    data1    data2                parity
766  * +--------+--------+--------+           +--------------------+
767  * | ------ | ------ | ------ |           | (no change)        |
768  * | -data- | -data- | -data- | --------> | xor all data       |
769  * | ------ | ------ | ------ | --------> | (no change)        |
770  * | ------ | ------ | ------ |           | (no change)        |
771  * +--------+--------+--------+           +--------------------+
772  * pp_size = 0
773  *
774  * The following cases are possible only in other implementations. The recovery
775  * code can handle them, but they are not generated at runtime because they can
776  * be reduced to cases 0, 1 and 2:
777  *
778  * case 3:
779  *   data0    data1    data2     ppl        parity
780  * +--------+--------+--------+ +----+    +--------------------+
781  * | ------ | -data- | -data- | | pp |    | data1 ^ data2 ^ pp |
782  * | ------ | -data- | -data- | | pp | -> | data1 ^ data2 ^ pp |
783  * | -data- | -data- | -data- | | -- | -> | xor all data       |
784  * | -data- | -data- | ------ | | pp |    | data0 ^ data1 ^ pp |
785  * +--------+--------+--------+ +----+    +--------------------+
786  * pp_size = chunk_size
787  *
788  * case 4:
789  *   data0    data1    data2     ppl        parity
790  * +--------+--------+--------+ +----+    +--------------------+
791  * | ------ | -data- | ------ | | pp |    | data1 ^ pp         |
792  * | ------ | ------ | ------ | | -- | -> | (no change)        |
793  * | ------ | ------ | ------ | | -- | -> | (no change)        |
794  * | -data- | ------ | ------ | | pp |    | data0 ^ pp         |
795  * +--------+--------+--------+ +----+    +--------------------+
796  * pp_size = chunk_size
797  */
798 static int ppl_recover_entry(struct ppl_log *log, struct ppl_header_entry *e,
799                              sector_t ppl_sector)
800 {
801         struct ppl_conf *ppl_conf = log->ppl_conf;
802         struct mddev *mddev = ppl_conf->mddev;
803         struct r5conf *conf = mddev->private;
804         int block_size = ppl_conf->block_size;
805         struct page *page1;
806         struct page *page2;
807         sector_t r_sector_first;
808         sector_t r_sector_last;
809         int strip_sectors;
810         int data_disks;
811         int i;
812         int ret = 0;
813         char b[BDEVNAME_SIZE];
814         unsigned int pp_size = le32_to_cpu(e->pp_size);
815         unsigned int data_size = le32_to_cpu(e->data_size);
816
817         page1 = alloc_page(GFP_KERNEL);
818         page2 = alloc_page(GFP_KERNEL);
819
820         if (!page1 || !page2) {
821                 ret = -ENOMEM;
822                 goto out;
823         }
824
825         r_sector_first = le64_to_cpu(e->data_sector) * (block_size >> 9);
826
827         if ((pp_size >> 9) < conf->chunk_sectors) {
828                 if (pp_size > 0) {
829                         data_disks = data_size / pp_size;
830                         strip_sectors = pp_size >> 9;
831                 } else {
832                         data_disks = conf->raid_disks - conf->max_degraded;
833                         strip_sectors = (data_size >> 9) / data_disks;
834                 }
835                 r_sector_last = r_sector_first +
836                                 (data_disks - 1) * conf->chunk_sectors +
837                                 strip_sectors;
838         } else {
839                 data_disks = conf->raid_disks - conf->max_degraded;
840                 strip_sectors = conf->chunk_sectors;
841                 r_sector_last = r_sector_first + (data_size >> 9);
842         }
843
844         pr_debug("%s: array sector first: %llu last: %llu\n", __func__,
845                  (unsigned long long)r_sector_first,
846                  (unsigned long long)r_sector_last);
847
848         /* if start and end is 4k aligned, use a 4k block */
849         if (block_size == 512 &&
850             (r_sector_first & (STRIPE_SECTORS - 1)) == 0 &&
851             (r_sector_last & (STRIPE_SECTORS - 1)) == 0)
852                 block_size = STRIPE_SIZE;
853
854         /* iterate through blocks in strip */
855         for (i = 0; i < strip_sectors; i += (block_size >> 9)) {
856                 bool update_parity = false;
857                 sector_t parity_sector;
858                 struct md_rdev *parity_rdev;
859                 struct stripe_head sh;
860                 int disk;
861                 int indent = 0;
862
863                 pr_debug("%s:%*s iter %d start\n", __func__, indent, "", i);
864                 indent += 2;
865
866                 memset(page_address(page1), 0, PAGE_SIZE);
867
868                 /* iterate through data member disks */
869                 for (disk = 0; disk < data_disks; disk++) {
870                         int dd_idx;
871                         struct md_rdev *rdev;
872                         sector_t sector;
873                         sector_t r_sector = r_sector_first + i +
874                                             (disk * conf->chunk_sectors);
875
876                         pr_debug("%s:%*s data member disk %d start\n",
877                                  __func__, indent, "", disk);
878                         indent += 2;
879
880                         if (r_sector >= r_sector_last) {
881                                 pr_debug("%s:%*s array sector %llu doesn't need parity update\n",
882                                          __func__, indent, "",
883                                          (unsigned long long)r_sector);
884                                 indent -= 2;
885                                 continue;
886                         }
887
888                         update_parity = true;
889
890                         /* map raid sector to member disk */
891                         sector = raid5_compute_sector(conf, r_sector, 0,
892                                                       &dd_idx, NULL);
893                         pr_debug("%s:%*s processing array sector %llu => data member disk %d, sector %llu\n",
894                                  __func__, indent, "",
895                                  (unsigned long long)r_sector, dd_idx,
896                                  (unsigned long long)sector);
897
898                         rdev = conf->disks[dd_idx].rdev;
899                         if (!rdev || (!test_bit(In_sync, &rdev->flags) &&
900                                       sector >= rdev->recovery_offset)) {
901                                 pr_debug("%s:%*s data member disk %d missing\n",
902                                          __func__, indent, "", dd_idx);
903                                 update_parity = false;
904                                 break;
905                         }
906
907                         pr_debug("%s:%*s reading data member disk %s sector %llu\n",
908                                  __func__, indent, "", bdevname(rdev->bdev, b),
909                                  (unsigned long long)sector);
910                         if (!sync_page_io(rdev, sector, block_size, page2,
911                                         REQ_OP_READ, 0, false)) {
912                                 md_error(mddev, rdev);
913                                 pr_debug("%s:%*s read failed!\n", __func__,
914                                          indent, "");
915                                 ret = -EIO;
916                                 goto out;
917                         }
918
919                         ppl_xor(block_size, page1, page2);
920
921                         indent -= 2;
922                 }
923
924                 if (!update_parity)
925                         continue;
926
927                 if (pp_size > 0) {
928                         pr_debug("%s:%*s reading pp disk sector %llu\n",
929                                  __func__, indent, "",
930                                  (unsigned long long)(ppl_sector + i));
931                         if (!sync_page_io(log->rdev,
932                                         ppl_sector - log->rdev->data_offset + i,
933                                         block_size, page2, REQ_OP_READ, 0,
934                                         false)) {
935                                 pr_debug("%s:%*s read failed!\n", __func__,
936                                          indent, "");
937                                 md_error(mddev, log->rdev);
938                                 ret = -EIO;
939                                 goto out;
940                         }
941
942                         ppl_xor(block_size, page1, page2);
943                 }
944
945                 /* map raid sector to parity disk */
946                 parity_sector = raid5_compute_sector(conf, r_sector_first + i,
947                                 0, &disk, &sh);
948                 BUG_ON(sh.pd_idx != le32_to_cpu(e->parity_disk));
949                 parity_rdev = conf->disks[sh.pd_idx].rdev;
950
951                 BUG_ON(parity_rdev->bdev->bd_dev != log->rdev->bdev->bd_dev);
952                 pr_debug("%s:%*s write parity at sector %llu, disk %s\n",
953                          __func__, indent, "",
954                          (unsigned long long)parity_sector,
955                          bdevname(parity_rdev->bdev, b));
956                 if (!sync_page_io(parity_rdev, parity_sector, block_size,
957                                 page1, REQ_OP_WRITE, 0, false)) {
958                         pr_debug("%s:%*s parity write error!\n", __func__,
959                                  indent, "");
960                         md_error(mddev, parity_rdev);
961                         ret = -EIO;
962                         goto out;
963                 }
964         }
965 out:
966         if (page1)
967                 __free_page(page1);
968         if (page2)
969                 __free_page(page2);
970         return ret;
971 }
972
973 static int ppl_recover(struct ppl_log *log, struct ppl_header *pplhdr,
974                        sector_t offset)
975 {
976         struct ppl_conf *ppl_conf = log->ppl_conf;
977         struct md_rdev *rdev = log->rdev;
978         struct mddev *mddev = rdev->mddev;
979         sector_t ppl_sector = rdev->ppl.sector + offset +
980                               (PPL_HEADER_SIZE >> 9);
981         struct page *page;
982         int i;
983         int ret = 0;
984
985         page = alloc_page(GFP_KERNEL);
986         if (!page)
987                 return -ENOMEM;
988
989         /* iterate through all PPL entries saved */
990         for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++) {
991                 struct ppl_header_entry *e = &pplhdr->entries[i];
992                 u32 pp_size = le32_to_cpu(e->pp_size);
993                 sector_t sector = ppl_sector;
994                 int ppl_entry_sectors = pp_size >> 9;
995                 u32 crc, crc_stored;
996
997                 pr_debug("%s: disk: %d entry: %d ppl_sector: %llu pp_size: %u\n",
998                          __func__, rdev->raid_disk, i,
999                          (unsigned long long)ppl_sector, pp_size);
1000
1001                 crc = ~0;
1002                 crc_stored = le32_to_cpu(e->checksum);
1003
1004                 /* read parial parity for this entry and calculate its checksum */
1005                 while (pp_size) {
1006                         int s = pp_size > PAGE_SIZE ? PAGE_SIZE : pp_size;
1007
1008                         if (!sync_page_io(rdev, sector - rdev->data_offset,
1009                                         s, page, REQ_OP_READ, 0, false)) {
1010                                 md_error(mddev, rdev);
1011                                 ret = -EIO;
1012                                 goto out;
1013                         }
1014
1015                         crc = crc32c_le(crc, page_address(page), s);
1016
1017                         pp_size -= s;
1018                         sector += s >> 9;
1019                 }
1020
1021                 crc = ~crc;
1022
1023                 if (crc != crc_stored) {
1024                         /*
1025                          * Don't recover this entry if the checksum does not
1026                          * match, but keep going and try to recover other
1027                          * entries.
1028                          */
1029                         pr_debug("%s: ppl entry crc does not match: stored: 0x%x calculated: 0x%x\n",
1030                                  __func__, crc_stored, crc);
1031                         ppl_conf->mismatch_count++;
1032                 } else {
1033                         ret = ppl_recover_entry(log, e, ppl_sector);
1034                         if (ret)
1035                                 goto out;
1036                         ppl_conf->recovered_entries++;
1037                 }
1038
1039                 ppl_sector += ppl_entry_sectors;
1040         }
1041
1042         /* flush the disk cache after recovery if necessary */
1043         ret = blkdev_issue_flush(rdev->bdev, GFP_KERNEL, NULL);
1044 out:
1045         __free_page(page);
1046         return ret;
1047 }
1048
1049 static int ppl_write_empty_header(struct ppl_log *log)
1050 {
1051         struct page *page;
1052         struct ppl_header *pplhdr;
1053         struct md_rdev *rdev = log->rdev;
1054         int ret = 0;
1055
1056         pr_debug("%s: disk: %d ppl_sector: %llu\n", __func__,
1057                  rdev->raid_disk, (unsigned long long)rdev->ppl.sector);
1058
1059         page = alloc_page(GFP_NOIO | __GFP_ZERO);
1060         if (!page)
1061                 return -ENOMEM;
1062
1063         pplhdr = page_address(page);
1064         /* zero out PPL space to avoid collision with old PPLs */
1065         blkdev_issue_zeroout(rdev->bdev, rdev->ppl.sector,
1066                             log->rdev->ppl.size, GFP_NOIO, 0);
1067         memset(pplhdr->reserved, 0xff, PPL_HDR_RESERVED);
1068         pplhdr->signature = cpu_to_le32(log->ppl_conf->signature);
1069         pplhdr->checksum = cpu_to_le32(~crc32c_le(~0, pplhdr, PAGE_SIZE));
1070
1071         if (!sync_page_io(rdev, rdev->ppl.sector - rdev->data_offset,
1072                           PPL_HEADER_SIZE, page, REQ_OP_WRITE | REQ_SYNC |
1073                           REQ_FUA, 0, false)) {
1074                 md_error(rdev->mddev, rdev);
1075                 ret = -EIO;
1076         }
1077
1078         __free_page(page);
1079         return ret;
1080 }
1081
1082 static int ppl_load_distributed(struct ppl_log *log)
1083 {
1084         struct ppl_conf *ppl_conf = log->ppl_conf;
1085         struct md_rdev *rdev = log->rdev;
1086         struct mddev *mddev = rdev->mddev;
1087         struct page *page, *page2, *tmp;
1088         struct ppl_header *pplhdr = NULL, *prev_pplhdr = NULL;
1089         u32 crc, crc_stored;
1090         u32 signature;
1091         int ret = 0, i;
1092         sector_t pplhdr_offset = 0, prev_pplhdr_offset = 0;
1093
1094         pr_debug("%s: disk: %d\n", __func__, rdev->raid_disk);
1095         /* read PPL headers, find the recent one */
1096         page = alloc_page(GFP_KERNEL);
1097         if (!page)
1098                 return -ENOMEM;
1099
1100         page2 = alloc_page(GFP_KERNEL);
1101         if (!page2) {
1102                 __free_page(page);
1103                 return -ENOMEM;
1104         }
1105
1106         /* searching ppl area for latest ppl */
1107         while (pplhdr_offset < rdev->ppl.size - (PPL_HEADER_SIZE >> 9)) {
1108                 if (!sync_page_io(rdev,
1109                                   rdev->ppl.sector - rdev->data_offset +
1110                                   pplhdr_offset, PAGE_SIZE, page, REQ_OP_READ,
1111                                   0, false)) {
1112                         md_error(mddev, rdev);
1113                         ret = -EIO;
1114                         /* if not able to read - don't recover any PPL */
1115                         pplhdr = NULL;
1116                         break;
1117                 }
1118                 pplhdr = page_address(page);
1119
1120                 /* check header validity */
1121                 crc_stored = le32_to_cpu(pplhdr->checksum);
1122                 pplhdr->checksum = 0;
1123                 crc = ~crc32c_le(~0, pplhdr, PAGE_SIZE);
1124
1125                 if (crc_stored != crc) {
1126                         pr_debug("%s: ppl header crc does not match: stored: 0x%x calculated: 0x%x (offset: %llu)\n",
1127                                  __func__, crc_stored, crc,
1128                                  (unsigned long long)pplhdr_offset);
1129                         pplhdr = prev_pplhdr;
1130                         pplhdr_offset = prev_pplhdr_offset;
1131                         break;
1132                 }
1133
1134                 signature = le32_to_cpu(pplhdr->signature);
1135
1136                 if (mddev->external) {
1137                         /*
1138                          * For external metadata the header signature is set and
1139                          * validated in userspace.
1140                          */
1141                         ppl_conf->signature = signature;
1142                 } else if (ppl_conf->signature != signature) {
1143                         pr_debug("%s: ppl header signature does not match: stored: 0x%x configured: 0x%x (offset: %llu)\n",
1144                                  __func__, signature, ppl_conf->signature,
1145                                  (unsigned long long)pplhdr_offset);
1146                         pplhdr = prev_pplhdr;
1147                         pplhdr_offset = prev_pplhdr_offset;
1148                         break;
1149                 }
1150
1151                 if (prev_pplhdr && le64_to_cpu(prev_pplhdr->generation) >
1152                     le64_to_cpu(pplhdr->generation)) {
1153                         /* previous was newest */
1154                         pplhdr = prev_pplhdr;
1155                         pplhdr_offset = prev_pplhdr_offset;
1156                         break;
1157                 }
1158
1159                 prev_pplhdr_offset = pplhdr_offset;
1160                 prev_pplhdr = pplhdr;
1161
1162                 tmp = page;
1163                 page = page2;
1164                 page2 = tmp;
1165
1166                 /* calculate next potential ppl offset */
1167                 for (i = 0; i < le32_to_cpu(pplhdr->entries_count); i++)
1168                         pplhdr_offset +=
1169                             le32_to_cpu(pplhdr->entries[i].pp_size) >> 9;
1170                 pplhdr_offset += PPL_HEADER_SIZE >> 9;
1171         }
1172
1173         /* no valid ppl found */
1174         if (!pplhdr)
1175                 ppl_conf->mismatch_count++;
1176         else
1177                 pr_debug("%s: latest PPL found at offset: %llu, with generation: %llu\n",
1178                     __func__, (unsigned long long)pplhdr_offset,
1179                     le64_to_cpu(pplhdr->generation));
1180
1181         /* attempt to recover from log if we are starting a dirty array */
1182         if (pplhdr && !mddev->pers && mddev->recovery_cp != MaxSector)
1183                 ret = ppl_recover(log, pplhdr, pplhdr_offset);
1184
1185         /* write empty header if we are starting the array */
1186         if (!ret && !mddev->pers)
1187                 ret = ppl_write_empty_header(log);
1188
1189         __free_page(page);
1190         __free_page(page2);
1191
1192         pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
1193                  __func__, ret, ppl_conf->mismatch_count,
1194                  ppl_conf->recovered_entries);
1195         return ret;
1196 }
1197
1198 static int ppl_load(struct ppl_conf *ppl_conf)
1199 {
1200         int ret = 0;
1201         u32 signature = 0;
1202         bool signature_set = false;
1203         int i;
1204
1205         for (i = 0; i < ppl_conf->count; i++) {
1206                 struct ppl_log *log = &ppl_conf->child_logs[i];
1207
1208                 /* skip missing drive */
1209                 if (!log->rdev)
1210                         continue;
1211
1212                 ret = ppl_load_distributed(log);
1213                 if (ret)
1214                         break;
1215
1216                 /*
1217                  * For external metadata we can't check if the signature is
1218                  * correct on a single drive, but we can check if it is the same
1219                  * on all drives.
1220                  */
1221                 if (ppl_conf->mddev->external) {
1222                         if (!signature_set) {
1223                                 signature = ppl_conf->signature;
1224                                 signature_set = true;
1225                         } else if (signature != ppl_conf->signature) {
1226                                 pr_warn("md/raid:%s: PPL header signature does not match on all member drives\n",
1227                                         mdname(ppl_conf->mddev));
1228                                 ret = -EINVAL;
1229                                 break;
1230                         }
1231                 }
1232         }
1233
1234         pr_debug("%s: return: %d mismatch_count: %d recovered_entries: %d\n",
1235                  __func__, ret, ppl_conf->mismatch_count,
1236                  ppl_conf->recovered_entries);
1237         return ret;
1238 }
1239
1240 static void __ppl_exit_log(struct ppl_conf *ppl_conf)
1241 {
1242         clear_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
1243         clear_bit(MD_HAS_MULTIPLE_PPLS, &ppl_conf->mddev->flags);
1244
1245         kfree(ppl_conf->child_logs);
1246
1247         bioset_exit(&ppl_conf->bs);
1248         bioset_exit(&ppl_conf->flush_bs);
1249         mempool_exit(&ppl_conf->io_pool);
1250         kmem_cache_destroy(ppl_conf->io_kc);
1251
1252         kfree(ppl_conf);
1253 }
1254
1255 void ppl_exit_log(struct r5conf *conf)
1256 {
1257         struct ppl_conf *ppl_conf = conf->log_private;
1258
1259         if (ppl_conf) {
1260                 __ppl_exit_log(ppl_conf);
1261                 conf->log_private = NULL;
1262         }
1263 }
1264
1265 static int ppl_validate_rdev(struct md_rdev *rdev)
1266 {
1267         char b[BDEVNAME_SIZE];
1268         int ppl_data_sectors;
1269         int ppl_size_new;
1270
1271         /*
1272          * The configured PPL size must be enough to store
1273          * the header and (at the very least) partial parity
1274          * for one stripe. Round it down to ensure the data
1275          * space is cleanly divisible by stripe size.
1276          */
1277         ppl_data_sectors = rdev->ppl.size - (PPL_HEADER_SIZE >> 9);
1278
1279         if (ppl_data_sectors > 0)
1280                 ppl_data_sectors = rounddown(ppl_data_sectors, STRIPE_SECTORS);
1281
1282         if (ppl_data_sectors <= 0) {
1283                 pr_warn("md/raid:%s: PPL space too small on %s\n",
1284                         mdname(rdev->mddev), bdevname(rdev->bdev, b));
1285                 return -ENOSPC;
1286         }
1287
1288         ppl_size_new = ppl_data_sectors + (PPL_HEADER_SIZE >> 9);
1289
1290         if ((rdev->ppl.sector < rdev->data_offset &&
1291              rdev->ppl.sector + ppl_size_new > rdev->data_offset) ||
1292             (rdev->ppl.sector >= rdev->data_offset &&
1293              rdev->data_offset + rdev->sectors > rdev->ppl.sector)) {
1294                 pr_warn("md/raid:%s: PPL space overlaps with data on %s\n",
1295                         mdname(rdev->mddev), bdevname(rdev->bdev, b));
1296                 return -EINVAL;
1297         }
1298
1299         if (!rdev->mddev->external &&
1300             ((rdev->ppl.offset > 0 && rdev->ppl.offset < (rdev->sb_size >> 9)) ||
1301              (rdev->ppl.offset <= 0 && rdev->ppl.offset + ppl_size_new > 0))) {
1302                 pr_warn("md/raid:%s: PPL space overlaps with superblock on %s\n",
1303                         mdname(rdev->mddev), bdevname(rdev->bdev, b));
1304                 return -EINVAL;
1305         }
1306
1307         rdev->ppl.size = ppl_size_new;
1308
1309         return 0;
1310 }
1311
1312 static void ppl_init_child_log(struct ppl_log *log, struct md_rdev *rdev)
1313 {
1314         struct request_queue *q;
1315
1316         if ((rdev->ppl.size << 9) >= (PPL_SPACE_SIZE +
1317                                       PPL_HEADER_SIZE) * 2) {
1318                 log->use_multippl = true;
1319                 set_bit(MD_HAS_MULTIPLE_PPLS,
1320                         &log->ppl_conf->mddev->flags);
1321                 log->entry_space = PPL_SPACE_SIZE;
1322         } else {
1323                 log->use_multippl = false;
1324                 log->entry_space = (log->rdev->ppl.size << 9) -
1325                                    PPL_HEADER_SIZE;
1326         }
1327         log->next_io_sector = rdev->ppl.sector;
1328
1329         q = bdev_get_queue(rdev->bdev);
1330         if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
1331                 log->wb_cache_on = true;
1332 }
1333
1334 int ppl_init_log(struct r5conf *conf)
1335 {
1336         struct ppl_conf *ppl_conf;
1337         struct mddev *mddev = conf->mddev;
1338         int ret = 0;
1339         int max_disks;
1340         int i;
1341
1342         pr_debug("md/raid:%s: enabling distributed Partial Parity Log\n",
1343                  mdname(conf->mddev));
1344
1345         if (PAGE_SIZE != 4096)
1346                 return -EINVAL;
1347
1348         if (mddev->level != 5) {
1349                 pr_warn("md/raid:%s PPL is not compatible with raid level %d\n",
1350                         mdname(mddev), mddev->level);
1351                 return -EINVAL;
1352         }
1353
1354         if (mddev->bitmap_info.file || mddev->bitmap_info.offset) {
1355                 pr_warn("md/raid:%s PPL is not compatible with bitmap\n",
1356                         mdname(mddev));
1357                 return -EINVAL;
1358         }
1359
1360         if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
1361                 pr_warn("md/raid:%s PPL is not compatible with journal\n",
1362                         mdname(mddev));
1363                 return -EINVAL;
1364         }
1365
1366         max_disks = FIELD_SIZEOF(struct ppl_log, disk_flush_bitmap) *
1367                 BITS_PER_BYTE;
1368         if (conf->raid_disks > max_disks) {
1369                 pr_warn("md/raid:%s PPL doesn't support over %d disks in the array\n",
1370                         mdname(mddev), max_disks);
1371                 return -EINVAL;
1372         }
1373
1374         ppl_conf = kzalloc(sizeof(struct ppl_conf), GFP_KERNEL);
1375         if (!ppl_conf)
1376                 return -ENOMEM;
1377
1378         ppl_conf->mddev = mddev;
1379
1380         ppl_conf->io_kc = KMEM_CACHE(ppl_io_unit, 0);
1381         if (!ppl_conf->io_kc) {
1382                 ret = -ENOMEM;
1383                 goto err;
1384         }
1385
1386         ret = mempool_init(&ppl_conf->io_pool, conf->raid_disks, ppl_io_pool_alloc,
1387                            ppl_io_pool_free, ppl_conf->io_kc);
1388         if (ret)
1389                 goto err;
1390
1391         ret = bioset_init(&ppl_conf->bs, conf->raid_disks, 0, BIOSET_NEED_BVECS);
1392         if (ret)
1393                 goto err;
1394
1395         ret = bioset_init(&ppl_conf->flush_bs, conf->raid_disks, 0, 0);
1396         if (ret)
1397                 goto err;
1398
1399         ppl_conf->count = conf->raid_disks;
1400         ppl_conf->child_logs = kcalloc(ppl_conf->count, sizeof(struct ppl_log),
1401                                        GFP_KERNEL);
1402         if (!ppl_conf->child_logs) {
1403                 ret = -ENOMEM;
1404                 goto err;
1405         }
1406
1407         atomic64_set(&ppl_conf->seq, 0);
1408         INIT_LIST_HEAD(&ppl_conf->no_mem_stripes);
1409         spin_lock_init(&ppl_conf->no_mem_stripes_lock);
1410
1411         if (!mddev->external) {
1412                 ppl_conf->signature = ~crc32c_le(~0, mddev->uuid, sizeof(mddev->uuid));
1413                 ppl_conf->block_size = 512;
1414         } else {
1415                 ppl_conf->block_size = queue_logical_block_size(mddev->queue);
1416         }
1417
1418         for (i = 0; i < ppl_conf->count; i++) {
1419                 struct ppl_log *log = &ppl_conf->child_logs[i];
1420                 struct md_rdev *rdev = conf->disks[i].rdev;
1421
1422                 mutex_init(&log->io_mutex);
1423                 spin_lock_init(&log->io_list_lock);
1424                 INIT_LIST_HEAD(&log->io_list);
1425
1426                 log->ppl_conf = ppl_conf;
1427                 log->rdev = rdev;
1428
1429                 if (rdev) {
1430                         ret = ppl_validate_rdev(rdev);
1431                         if (ret)
1432                                 goto err;
1433
1434                         ppl_init_child_log(log, rdev);
1435                 }
1436         }
1437
1438         /* load and possibly recover the logs from the member disks */
1439         ret = ppl_load(ppl_conf);
1440
1441         if (ret) {
1442                 goto err;
1443         } else if (!mddev->pers && mddev->recovery_cp == 0 &&
1444                    ppl_conf->recovered_entries > 0 &&
1445                    ppl_conf->mismatch_count == 0) {
1446                 /*
1447                  * If we are starting a dirty array and the recovery succeeds
1448                  * without any issues, set the array as clean.
1449                  */
1450                 mddev->recovery_cp = MaxSector;
1451                 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
1452         } else if (mddev->pers && ppl_conf->mismatch_count > 0) {
1453                 /* no mismatch allowed when enabling PPL for a running array */
1454                 ret = -EINVAL;
1455                 goto err;
1456         }
1457
1458         conf->log_private = ppl_conf;
1459         set_bit(MD_HAS_PPL, &ppl_conf->mddev->flags);
1460
1461         return 0;
1462 err:
1463         __ppl_exit_log(ppl_conf);
1464         return ret;
1465 }
1466
1467 int ppl_modify_log(struct r5conf *conf, struct md_rdev *rdev, bool add)
1468 {
1469         struct ppl_conf *ppl_conf = conf->log_private;
1470         struct ppl_log *log;
1471         int ret = 0;
1472         char b[BDEVNAME_SIZE];
1473
1474         if (!rdev)
1475                 return -EINVAL;
1476
1477         pr_debug("%s: disk: %d operation: %s dev: %s\n",
1478                  __func__, rdev->raid_disk, add ? "add" : "remove",
1479                  bdevname(rdev->bdev, b));
1480
1481         if (rdev->raid_disk < 0)
1482                 return 0;
1483
1484         if (rdev->raid_disk >= ppl_conf->count)
1485                 return -ENODEV;
1486
1487         log = &ppl_conf->child_logs[rdev->raid_disk];
1488
1489         mutex_lock(&log->io_mutex);
1490         if (add) {
1491                 ret = ppl_validate_rdev(rdev);
1492                 if (!ret) {
1493                         log->rdev = rdev;
1494                         ret = ppl_write_empty_header(log);
1495                         ppl_init_child_log(log, rdev);
1496                 }
1497         } else {
1498                 log->rdev = NULL;
1499         }
1500         mutex_unlock(&log->io_mutex);
1501
1502         return ret;
1503 }