1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/gfp.h>
17#include <linux/async_tx.h>
18
19#include "ore_raid.h"
20
21#undef ORE_DBGMSG2
22#define ORE_DBGMSG2 ORE_DBGMSG
23
24static struct page *_raid_page_alloc(void)
25{
26 return alloc_page(GFP_KERNEL);
27}
28
29static void _raid_page_free(struct page *p)
30{
31 __free_page(p);
32}
33
34
35
36
37
38
39
40
41
42struct __stripe_pages_2d {
43
44 unsigned parity;
45 unsigned data_devs;
46 unsigned pages_in_unit;
47
48 bool needed ;
49
50
51 struct __1_page_stripe {
52 bool alloc;
53 unsigned write_count;
54 struct async_submit_ctl submit;
55 struct dma_async_tx_descriptor *tx;
56
57
58 struct page **pages;
59 struct page **scribble;
60
61 char *page_is_read;
62 } _1p_stripes[];
63};
64
65
66
67
68
69static int _sp2d_alloc(unsigned pages_in_unit, unsigned group_width,
70 unsigned parity, struct __stripe_pages_2d **psp2d)
71{
72 struct __stripe_pages_2d *sp2d;
73 unsigned data_devs = group_width - parity;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 char *__a1pa;
97 char *__a1pa_end;
98
99 const size_t sizeof_stripe_pages_2d =
100 sizeof(struct __stripe_pages_2d) +
101 sizeof(struct __1_page_stripe) * pages_in_unit;
102 const size_t sizeof__a1pa =
103 ALIGN(sizeof(struct page *) * (2 * group_width) + data_devs,
104 sizeof(void *));
105 const size_t sizeof__a1pa_arrays = sizeof__a1pa * pages_in_unit;
106 const size_t alloc_total = sizeof_stripe_pages_2d +
107 sizeof__a1pa_arrays;
108
109 unsigned num_a1pa, alloc_size, i;
110
111
112 BUG_ON(sizeof_stripe_pages_2d > PAGE_SIZE);
113 BUG_ON(sizeof__a1pa > PAGE_SIZE);
114
115
116
117
118
119
120 if (alloc_total > PAGE_SIZE) {
121 num_a1pa = (PAGE_SIZE - sizeof_stripe_pages_2d) / sizeof__a1pa;
122 alloc_size = sizeof_stripe_pages_2d + sizeof__a1pa * num_a1pa;
123 } else {
124 num_a1pa = pages_in_unit;
125 alloc_size = alloc_total;
126 }
127
128 *psp2d = sp2d = kzalloc(alloc_size, GFP_KERNEL);
129 if (unlikely(!sp2d)) {
130 ORE_DBGMSG("!! Failed to alloc sp2d size=%d\n", alloc_size);
131 return -ENOMEM;
132 }
133
134
135
136 __a1pa = (char *)sp2d + sizeof_stripe_pages_2d;
137
138 __a1pa_end = __a1pa + alloc_size;
139
140
141 for (i = 0; i < pages_in_unit; ++i) {
142 struct __1_page_stripe *stripe = &sp2d->_1p_stripes[i];
143
144 if (unlikely(__a1pa >= __a1pa_end)) {
145 num_a1pa = min_t(unsigned, PAGE_SIZE / sizeof__a1pa,
146 pages_in_unit - i);
147 alloc_size = sizeof__a1pa * num_a1pa;
148
149 __a1pa = kzalloc(alloc_size, GFP_KERNEL);
150 if (unlikely(!__a1pa)) {
151 ORE_DBGMSG("!! Failed to _alloc_1p_arrays=%d\n",
152 num_a1pa);
153 return -ENOMEM;
154 }
155 __a1pa_end = __a1pa + alloc_size;
156
157 stripe->alloc = true;
158 }
159
160
161
162
163
164
165 stripe->pages = (void *)__a1pa;
166 stripe->scribble = stripe->pages + group_width;
167 stripe->page_is_read = (char *)stripe->scribble + group_width;
168 __a1pa += sizeof__a1pa;
169 }
170
171 sp2d->parity = parity;
172 sp2d->data_devs = data_devs;
173 sp2d->pages_in_unit = pages_in_unit;
174 return 0;
175}
176
177static void _sp2d_reset(struct __stripe_pages_2d *sp2d,
178 const struct _ore_r4w_op *r4w, void *priv)
179{
180 unsigned data_devs = sp2d->data_devs;
181 unsigned group_width = data_devs + sp2d->parity;
182 int p, c;
183
184 if (!sp2d->needed)
185 return;
186
187 for (c = data_devs - 1; c >= 0; --c)
188 for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
189 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
190
191 if (_1ps->page_is_read[c]) {
192 struct page *page = _1ps->pages[c];
193
194 r4w->put_page(priv, page);
195 _1ps->page_is_read[c] = false;
196 }
197 }
198
199 for (p = 0; p < sp2d->pages_in_unit; p++) {
200 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
201
202 memset(_1ps->pages, 0, group_width * sizeof(*_1ps->pages));
203 _1ps->write_count = 0;
204 _1ps->tx = NULL;
205 }
206
207 sp2d->needed = false;
208}
209
210static void _sp2d_free(struct __stripe_pages_2d *sp2d)
211{
212 unsigned i;
213
214 if (!sp2d)
215 return;
216
217 for (i = 0; i < sp2d->pages_in_unit; ++i) {
218 if (sp2d->_1p_stripes[i].alloc)
219 kfree(sp2d->_1p_stripes[i].pages);
220 }
221
222 kfree(sp2d);
223}
224
225static unsigned _sp2d_min_pg(struct __stripe_pages_2d *sp2d)
226{
227 unsigned p;
228
229 for (p = 0; p < sp2d->pages_in_unit; p++) {
230 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
231
232 if (_1ps->write_count)
233 return p;
234 }
235
236 return ~0;
237}
238
239static unsigned _sp2d_max_pg(struct __stripe_pages_2d *sp2d)
240{
241 int p;
242
243 for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
244 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
245
246 if (_1ps->write_count)
247 return p;
248 }
249
250 return ~0;
251}
252
253static void _gen_xor_unit(struct __stripe_pages_2d *sp2d)
254{
255 unsigned p;
256 unsigned tx_flags = ASYNC_TX_ACK;
257
258 if (sp2d->parity == 1)
259 tx_flags |= ASYNC_TX_XOR_ZERO_DST;
260
261 for (p = 0; p < sp2d->pages_in_unit; p++) {
262 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
263
264 if (!_1ps->write_count)
265 continue;
266
267 init_async_submit(&_1ps->submit, tx_flags,
268 NULL, NULL, NULL, (addr_conv_t *)_1ps->scribble);
269
270 if (sp2d->parity == 1)
271 _1ps->tx = async_xor(_1ps->pages[sp2d->data_devs],
272 _1ps->pages, 0, sp2d->data_devs,
273 PAGE_SIZE, &_1ps->submit);
274 else
275 _1ps->tx = async_gen_syndrome(_1ps->pages, 0,
276 sp2d->data_devs + sp2d->parity,
277 PAGE_SIZE, &_1ps->submit);
278 }
279
280 for (p = 0; p < sp2d->pages_in_unit; p++) {
281 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
282
283
284
285
286 async_tx_issue_pending(_1ps->tx);
287 }
288}
289
290void _ore_add_stripe_page(struct __stripe_pages_2d *sp2d,
291 struct ore_striping_info *si, struct page *page)
292{
293 struct __1_page_stripe *_1ps;
294
295 sp2d->needed = true;
296
297 _1ps = &sp2d->_1p_stripes[si->cur_pg];
298 _1ps->pages[si->cur_comp] = page;
299 ++_1ps->write_count;
300
301 si->cur_pg = (si->cur_pg + 1) % sp2d->pages_in_unit;
302
303}
304
305void _ore_add_sg_seg(struct ore_per_dev_state *per_dev, unsigned cur_len,
306 bool not_last)
307{
308 struct osd_sg_entry *sge;
309
310 ORE_DBGMSG("dev=%d cur_len=0x%x not_last=%d cur_sg=%d "
311 "offset=0x%llx length=0x%x last_sgs_total=0x%x\n",
312 per_dev->dev, cur_len, not_last, per_dev->cur_sg,
313 _LLU(per_dev->offset), per_dev->length,
314 per_dev->last_sgs_total);
315
316 if (!per_dev->cur_sg) {
317 sge = per_dev->sglist;
318
319
320 if (per_dev->length) {
321 ++per_dev->cur_sg;
322 sge->offset = per_dev->offset;
323 sge->len = per_dev->length;
324 } else {
325
326
327
328
329
330 per_dev->offset += cur_len;
331 return;
332 }
333 } else {
334
335 sge = &per_dev->sglist[per_dev->cur_sg - 1];
336 sge->len = per_dev->length - per_dev->last_sgs_total;
337 }
338
339 if (not_last) {
340
341 struct osd_sg_entry *next_sge = sge + 1;
342
343 ++per_dev->cur_sg;
344 next_sge->offset = sge->offset + sge->len + cur_len;
345
346 per_dev->last_sgs_total = per_dev->length;
347 next_sge->len = 0;
348 } else if (!sge->len) {
349
350 --per_dev->cur_sg;
351 }
352}
353
354static int _alloc_read_4_write(struct ore_io_state *ios)
355{
356 struct ore_layout *layout = ios->layout;
357 int ret;
358
359
360
361 unsigned sgs_per_dev = ios->sp2d->pages_in_unit + 2;
362
363 ret = _ore_get_io_state(layout, ios->oc,
364 layout->group_width * layout->mirrors_p1,
365 sgs_per_dev, 0, &ios->ios_read_4_write);
366 return ret;
367}
368
369
370
371
372static int _add_to_r4w(struct ore_io_state *ios, struct ore_striping_info *si,
373 struct page *page, unsigned pg_len)
374{
375 struct request_queue *q;
376 struct ore_per_dev_state *per_dev;
377 struct ore_io_state *read_ios;
378 unsigned first_dev = si->dev - (si->dev %
379 (ios->layout->group_width * ios->layout->mirrors_p1));
380 unsigned comp = si->dev - first_dev;
381 unsigned added_len;
382
383 if (!ios->ios_read_4_write) {
384 int ret = _alloc_read_4_write(ios);
385
386 if (unlikely(ret))
387 return ret;
388 }
389
390 read_ios = ios->ios_read_4_write;
391 read_ios->numdevs = ios->layout->group_width * ios->layout->mirrors_p1;
392
393 per_dev = &read_ios->per_dev[comp];
394 if (!per_dev->length) {
395 per_dev->bio = bio_kmalloc(GFP_KERNEL,
396 ios->sp2d->pages_in_unit);
397 if (unlikely(!per_dev->bio)) {
398 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
399 ios->sp2d->pages_in_unit);
400 return -ENOMEM;
401 }
402 per_dev->offset = si->obj_offset;
403 per_dev->dev = si->dev;
404 } else if (si->obj_offset != (per_dev->offset + per_dev->length)) {
405 u64 gap = si->obj_offset - (per_dev->offset + per_dev->length);
406
407 _ore_add_sg_seg(per_dev, gap, true);
408 }
409 q = osd_request_queue(ore_comp_dev(read_ios->oc, per_dev->dev));
410 added_len = bio_add_pc_page(q, per_dev->bio, page, pg_len,
411 si->obj_offset % PAGE_SIZE);
412 if (unlikely(added_len != pg_len)) {
413 ORE_DBGMSG("Failed to bio_add_pc_page bi_vcnt=%d\n",
414 per_dev->bio->bi_vcnt);
415 return -ENOMEM;
416 }
417
418 per_dev->length += pg_len;
419 return 0;
420}
421
422
423static int _add_to_r4w_first_page(struct ore_io_state *ios, struct page *page)
424{
425 struct ore_striping_info si;
426 unsigned pg_len;
427
428 ore_calc_stripe_info(ios->layout, ios->offset, 0, &si);
429
430 pg_len = si.obj_offset % PAGE_SIZE;
431 si.obj_offset -= pg_len;
432
433 ORE_DBGMSG("offset=0x%llx len=0x%x index=0x%lx dev=%x\n",
434 _LLU(si.obj_offset), pg_len, page->index, si.dev);
435
436 return _add_to_r4w(ios, &si, page, pg_len);
437}
438
439
440static int _add_to_r4w_last_page(struct ore_io_state *ios, u64 *offset)
441{
442 struct ore_striping_info si;
443 struct page *page;
444 unsigned pg_len, p, c;
445
446 ore_calc_stripe_info(ios->layout, *offset, 0, &si);
447
448 p = si.cur_pg;
449 c = si.cur_comp;
450 page = ios->sp2d->_1p_stripes[p].pages[c];
451
452 pg_len = PAGE_SIZE - (si.unit_off % PAGE_SIZE);
453 *offset += pg_len;
454
455 ORE_DBGMSG("p=%d, c=%d next-offset=0x%llx len=0x%x dev=%x par_dev=%d\n",
456 p, c, _LLU(*offset), pg_len, si.dev, si.par_dev);
457
458 BUG_ON(!page);
459
460 return _add_to_r4w(ios, &si, page, pg_len);
461}
462
463static void _mark_read4write_pages_uptodate(struct ore_io_state *ios, int ret)
464{
465 struct bio_vec *bv;
466 unsigned i, d;
467
468
469 for (d = 0; d < ios->numdevs; d++) {
470 struct bio *bio = ios->per_dev[d].bio;
471
472 if (!bio)
473 continue;
474
475 bio_for_each_segment_all(bv, bio, i) {
476 struct page *page = bv->bv_page;
477
478 SetPageUptodate(page);
479 if (PageError(page))
480 ClearPageError(page);
481 }
482 }
483}
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504static int _read_4_write_first_stripe(struct ore_io_state *ios)
505{
506 struct ore_striping_info read_si;
507 struct __stripe_pages_2d *sp2d = ios->sp2d;
508 u64 offset = ios->si.first_stripe_start;
509 unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;
510
511 if (offset == ios->offset)
512 goto read_last_stripe;
513
514 min_p = _sp2d_min_pg(sp2d);
515 max_p = _sp2d_max_pg(sp2d);
516
517 ORE_DBGMSG("stripe_start=0x%llx ios->offset=0x%llx min_p=%d max_p=%d\n",
518 offset, ios->offset, min_p, max_p);
519
520 for (c = 0; ; c++) {
521 ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
522 read_si.obj_offset += min_p * PAGE_SIZE;
523 offset += min_p * PAGE_SIZE;
524 for (p = min_p; p <= max_p; p++) {
525 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
526 struct page **pp = &_1ps->pages[c];
527 bool uptodate;
528
529 if (*pp) {
530 if (ios->offset % PAGE_SIZE)
531
532 _add_to_r4w_first_page(ios, *pp);
533
534 goto read_last_stripe;
535 }
536
537 *pp = ios->r4w->get_page(ios->private, offset,
538 &uptodate);
539 if (unlikely(!*pp))
540 return -ENOMEM;
541
542 if (!uptodate)
543 _add_to_r4w(ios, &read_si, *pp, PAGE_SIZE);
544
545
546 _1ps->page_is_read[c] = true;
547 read_si.obj_offset += PAGE_SIZE;
548 offset += PAGE_SIZE;
549 }
550 offset += (sp2d->pages_in_unit - p) * PAGE_SIZE;
551 }
552
553read_last_stripe:
554 return 0;
555}
556
557static int _read_4_write_last_stripe(struct ore_io_state *ios)
558{
559 struct ore_striping_info read_si;
560 struct __stripe_pages_2d *sp2d = ios->sp2d;
561 u64 offset;
562 u64 last_stripe_end;
563 unsigned bytes_in_stripe = ios->si.bytes_in_stripe;
564 unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;
565
566 offset = ios->offset + ios->length;
567 if (offset % PAGE_SIZE)
568 _add_to_r4w_last_page(ios, &offset);
569
570
571 last_stripe_end = div_u64(offset + bytes_in_stripe - 1, bytes_in_stripe)
572 * bytes_in_stripe;
573 if (offset == last_stripe_end)
574 goto read_it;
575
576 ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
577 p = read_si.cur_pg;
578 c = read_si.cur_comp;
579
580 if (min_p == sp2d->pages_in_unit) {
581
582 min_p = _sp2d_min_pg(sp2d);
583 max_p = _sp2d_max_pg(sp2d);
584 }
585
586 ORE_DBGMSG("offset=0x%llx stripe_end=0x%llx min_p=%d max_p=%d\n",
587 offset, last_stripe_end, min_p, max_p);
588
589 while (offset < last_stripe_end) {
590 struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
591
592 if ((min_p <= p) && (p <= max_p)) {
593 struct page *page;
594 bool uptodate;
595
596 BUG_ON(_1ps->pages[c]);
597 page = ios->r4w->get_page(ios->private, offset,
598 &uptodate);
599 if (unlikely(!page))
600 return -ENOMEM;
601
602 _1ps->pages[c] = page;
603
604 _1ps->page_is_read[c] = true;
605 if (!uptodate)
606 _add_to_r4w(ios, &read_si, page, PAGE_SIZE);
607 }
608
609 offset += PAGE_SIZE;
610 if (p == (sp2d->pages_in_unit - 1)) {
611 ++c;
612 p = 0;
613 ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
614 } else {
615 read_si.obj_offset += PAGE_SIZE;
616 ++p;
617 }
618 }
619
620read_it:
621 return 0;
622}
623
624static int _read_4_write_execute(struct ore_io_state *ios)
625{
626 struct ore_io_state *ios_read;
627 unsigned i;
628 int ret;
629
630 ios_read = ios->ios_read_4_write;
631 if (!ios_read)
632 return 0;
633
634
635
636
637 ios_read->pages = ios->pages;
638
639
640 for (i = 0; i < ios_read->numdevs; i += ios_read->layout->mirrors_p1) {
641 ret = _ore_read_mirror(ios_read, i);
642 if (unlikely(ret))
643 return ret;
644 }
645
646 ret = ore_io_execute(ios_read);
647 if (unlikely(ret)) {
648 ORE_DBGMSG("!! ore_io_execute => %d\n", ret);
649 return ret;
650 }
651
652 _mark_read4write_pages_uptodate(ios_read, ret);
653 ore_put_io_state(ios_read);
654 ios->ios_read_4_write = NULL;
655 return 0;
656}
657
658
659int _ore_add_parity_unit(struct ore_io_state *ios,
660 struct ore_striping_info *si,
661 struct ore_per_dev_state *per_dev,
662 unsigned cur_len, bool do_xor)
663{
664 if (ios->reading) {
665 if (per_dev->cur_sg >= ios->sgs_per_dev) {
666 ORE_DBGMSG("cur_sg(%d) >= sgs_per_dev(%d)\n" ,
667 per_dev->cur_sg, ios->sgs_per_dev);
668 return -ENOMEM;
669 }
670 _ore_add_sg_seg(per_dev, cur_len, true);
671 } else {
672 struct __stripe_pages_2d *sp2d = ios->sp2d;
673 struct page **pages = ios->parity_pages + ios->cur_par_page;
674 unsigned num_pages;
675 unsigned array_start = 0;
676 unsigned i;
677 int ret;
678
679 si->cur_pg = _sp2d_min_pg(sp2d);
680 num_pages = _sp2d_max_pg(sp2d) + 1 - si->cur_pg;
681
682 if (!per_dev->length) {
683 per_dev->offset += si->cur_pg * PAGE_SIZE;
684
685
686
687 if (do_xor)
688 _read_4_write_first_stripe(ios);
689 }
690 if (!cur_len && do_xor)
691
692 _read_4_write_last_stripe(ios);
693 _read_4_write_execute(ios);
694
695 for (i = 0; i < num_pages; i++) {
696 pages[i] = _raid_page_alloc();
697 if (unlikely(!pages[i]))
698 return -ENOMEM;
699
700 ++(ios->cur_par_page);
701 }
702
703 BUG_ON(si->cur_comp < sp2d->data_devs);
704 BUG_ON(si->cur_pg + num_pages > sp2d->pages_in_unit);
705
706 ret = _ore_add_stripe_unit(ios, &array_start, 0, pages,
707 per_dev, num_pages * PAGE_SIZE);
708 if (unlikely(ret))
709 return ret;
710
711 if (do_xor) {
712 _gen_xor_unit(sp2d);
713 _sp2d_reset(sp2d, ios->r4w, ios->private);
714 }
715 }
716 return 0;
717}
718
719int _ore_post_alloc_raid_stuff(struct ore_io_state *ios)
720{
721 if (ios->parity_pages) {
722 struct ore_layout *layout = ios->layout;
723 unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
724
725 if (_sp2d_alloc(pages_in_unit, layout->group_width,
726 layout->parity, &ios->sp2d)) {
727 return -ENOMEM;
728 }
729 }
730 return 0;
731}
732
733void _ore_free_raid_stuff(struct ore_io_state *ios)
734{
735 if (ios->sp2d) {
736 unsigned i;
737
738 for (i = 0; i < ios->cur_par_page; i++) {
739 struct page *page = ios->parity_pages[i];
740
741 if (page)
742 _raid_page_free(page);
743 }
744 if (ios->extra_part_alloc)
745 kfree(ios->parity_pages);
746
747 _sp2d_reset(ios->sp2d, ios->r4w, ios->private);
748 _sp2d_free(ios->sp2d);
749 } else {
750
751 if (ios->extra_part_alloc)
752 kfree(ios->per_dev[0].sglist);
753 }
754 if (ios->ios_read_4_write)
755 ore_put_io_state(ios->ios_read_4_write);
756}
757