ore_raid.c 20 KB
Newer Older
Boaz Harrosh's avatar
Boaz Harrosh committed
1
2
/*
 * Copyright (C) 2011
3
 * Boaz Harrosh <ooo@electrozaur.com>
Boaz Harrosh's avatar
Boaz Harrosh committed
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This file is part of the objects raid engine (ore).
 *
 * It is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with "ore". If not, write to the Free Software Foundation, Inc:
 *	"Free Software Foundation <info@fsf.org>"
 */

#include <linux/gfp.h>
Boaz Harrosh's avatar
Boaz Harrosh committed
17
#include <linux/async_tx.h>
Boaz Harrosh's avatar
Boaz Harrosh committed
18
19
20

#include "ore_raid.h"

Boaz Harrosh's avatar
Boaz Harrosh committed
21
22
23
#undef ORE_DBGMSG2
#define ORE_DBGMSG2 ORE_DBGMSG

24
static struct page *_raid_page_alloc(void)
Boaz Harrosh's avatar
Boaz Harrosh committed
25
26
27
28
{
	return alloc_page(GFP_KERNEL);
}

29
static void _raid_page_free(struct page *p)
Boaz Harrosh's avatar
Boaz Harrosh committed
30
31
32
33
{
	__free_page(p);
}

Boaz Harrosh's avatar
Boaz Harrosh committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* This struct is forward declare in ore_io_state, but is private to here.
 * It is put on ios->sp2d for RAID5/6 writes only. See _gen_xor_unit.
 *
 * __stripe_pages_2d is a 2d array of pages, and it is also a corner turn.
 * Ascending page index access is sp2d(p-minor, c-major). But storage is
 * sp2d[p-minor][c-major], so it can be properlly presented to the async-xor
 * API.
 */
struct __stripe_pages_2d {
	/* Cache some hot path repeated calculations */
	unsigned parity;
	unsigned data_devs;
	unsigned pages_in_unit;

	bool needed ;

	/* Array size is pages_in_unit (layout->stripe_unit / PAGE_SIZE) */
	struct __1_page_stripe {
		bool alloc;
		unsigned write_count;
		struct async_submit_ctl submit;
		struct dma_async_tx_descriptor *tx;

		/* The size of this array is data_devs + parity */
		struct page **pages;
		struct page **scribble;
		/* bool array, size of this array is data_devs */
		char *page_is_read;
	} _1p_stripes[];
};

/* This can get bigger then a page. So support multiple page allocations
 * _sp2d_free should be called even if _sp2d_alloc fails (by returning
 * none-zero).
 */
static int _sp2d_alloc(unsigned pages_in_unit, unsigned group_width,
		       unsigned parity, struct __stripe_pages_2d **psp2d)
{
	struct __stripe_pages_2d *sp2d;
	unsigned data_devs = group_width - parity;
Kees Cook's avatar
Kees Cook committed
74
75
76
77
78

	/*
	 * Desired allocation layout is, though when larger than PAGE_SIZE,
	 * each struct __alloc_1p_arrays is separately allocated:

Boaz Harrosh's avatar
Boaz Harrosh committed
79
80
81
82
83
84
85
86
87
88
89
	struct _alloc_all_bytes {
		struct __alloc_stripe_pages_2d {
			struct __stripe_pages_2d sp2d;
			struct __1_page_stripe _1p_stripes[pages_in_unit];
		} __asp2d;
		struct __alloc_1p_arrays {
			struct page *pages[group_width];
			struct page *scribble[group_width];
			char page_is_read[data_devs];
		} __a1pa[pages_in_unit];
	} *_aab;
Kees Cook's avatar
Kees Cook committed
90

Boaz Harrosh's avatar
Boaz Harrosh committed
91
92
	struct __alloc_1p_arrays *__a1pa;
	struct __alloc_1p_arrays *__a1pa_end;
Kees Cook's avatar
Kees Cook committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

	*/

	char *__a1pa;
	char *__a1pa_end;

	const size_t sizeof_stripe_pages_2d =
		sizeof(struct __stripe_pages_2d) +
		sizeof(struct __1_page_stripe) * pages_in_unit;
	const size_t sizeof__a1pa =
		ALIGN(sizeof(struct page *) * (2 * group_width) + data_devs,
		      sizeof(void *));
	const size_t sizeof__a1pa_arrays = sizeof__a1pa * pages_in_unit;
	const size_t alloc_total = sizeof_stripe_pages_2d +
				   sizeof__a1pa_arrays;

Boaz Harrosh's avatar
Boaz Harrosh committed
109
110
111
	unsigned num_a1pa, alloc_size, i;

	/* FIXME: check these numbers in ore_verify_layout */
Kees Cook's avatar
Kees Cook committed
112
	BUG_ON(sizeof_stripe_pages_2d > PAGE_SIZE);
Boaz Harrosh's avatar
Boaz Harrosh committed
113
114
	BUG_ON(sizeof__a1pa > PAGE_SIZE);

Kees Cook's avatar
Kees Cook committed
115
116
117
118
119
120
121
122
	/*
	 * If alloc_total would be larger than PAGE_SIZE, only allocate
	 * as many a1pa items as would fill the rest of the page, instead
	 * of the full pages_in_unit count.
	 */
	if (alloc_total > PAGE_SIZE) {
		num_a1pa = (PAGE_SIZE - sizeof_stripe_pages_2d) / sizeof__a1pa;
		alloc_size = sizeof_stripe_pages_2d + sizeof__a1pa * num_a1pa;
Boaz Harrosh's avatar
Boaz Harrosh committed
123
124
	} else {
		num_a1pa = pages_in_unit;
Kees Cook's avatar
Kees Cook committed
125
		alloc_size = alloc_total;
Boaz Harrosh's avatar
Boaz Harrosh committed
126
127
	}

Kees Cook's avatar
Kees Cook committed
128
129
	*psp2d = sp2d = kzalloc(alloc_size, GFP_KERNEL);
	if (unlikely(!sp2d)) {
Boaz Harrosh's avatar
Boaz Harrosh committed
130
131
132
		ORE_DBGMSG("!! Failed to alloc sp2d size=%d\n", alloc_size);
		return -ENOMEM;
	}
Kees Cook's avatar
Kees Cook committed
133
	/* From here Just call _sp2d_free */
Boaz Harrosh's avatar
Boaz Harrosh committed
134

Kees Cook's avatar
Kees Cook committed
135
136
137
138
	/* Find start of a1pa area. */
	__a1pa = (char *)sp2d + sizeof_stripe_pages_2d;
	/* Find end of the _allocated_ a1pa area. */
	__a1pa_end = __a1pa + alloc_size;
Boaz Harrosh's avatar
Boaz Harrosh committed
139

Kees Cook's avatar
Kees Cook committed
140
	/* Allocate additionally needed a1pa items in PAGE_SIZE chunks. */
Boaz Harrosh's avatar
Boaz Harrosh committed
141
	for (i = 0; i < pages_in_unit; ++i) {
Kees Cook's avatar
Kees Cook committed
142
143
		struct __1_page_stripe *stripe = &sp2d->_1p_stripes[i];

Boaz Harrosh's avatar
Boaz Harrosh committed
144
145
146
		if (unlikely(__a1pa >= __a1pa_end)) {
			num_a1pa = min_t(unsigned, PAGE_SIZE / sizeof__a1pa,
							pages_in_unit - i);
Kees Cook's avatar
Kees Cook committed
147
			alloc_size = sizeof__a1pa * num_a1pa;
Boaz Harrosh's avatar
Boaz Harrosh committed
148

Kees Cook's avatar
Kees Cook committed
149
			__a1pa = kzalloc(alloc_size, GFP_KERNEL);
Boaz Harrosh's avatar
Boaz Harrosh committed
150
151
152
153
154
			if (unlikely(!__a1pa)) {
				ORE_DBGMSG("!! Failed to _alloc_1p_arrays=%d\n",
					   num_a1pa);
				return -ENOMEM;
			}
Kees Cook's avatar
Kees Cook committed
155
			__a1pa_end = __a1pa + alloc_size;
Boaz Harrosh's avatar
Boaz Harrosh committed
156
			/* First *pages is marked for kfree of the buffer */
Kees Cook's avatar
Kees Cook committed
157
			stripe->alloc = true;
Boaz Harrosh's avatar
Boaz Harrosh committed
158
159
		}

Kees Cook's avatar
Kees Cook committed
160
161
162
163
164
165
166
167
168
		/*
		 * Attach all _lp_stripes pointers to the allocation for
		 * it which was either part of the original PAGE_SIZE
		 * allocation or the subsequent allocation in this loop.
		 */
		stripe->pages = (void *)__a1pa;
		stripe->scribble = stripe->pages + group_width;
		stripe->page_is_read = (char *)stripe->scribble + group_width;
		__a1pa += sizeof__a1pa;
Boaz Harrosh's avatar
Boaz Harrosh committed
169
170
171
172
173
174
175
176
177
178
179
180
181
	}

	sp2d->parity = parity;
	sp2d->data_devs = data_devs;
	sp2d->pages_in_unit = pages_in_unit;
	return 0;
}

static void _sp2d_reset(struct __stripe_pages_2d *sp2d,
			const struct _ore_r4w_op *r4w, void *priv)
{
	unsigned data_devs = sp2d->data_devs;
	unsigned group_width = data_devs + sp2d->parity;
182
	int p, c;
Boaz Harrosh's avatar
Boaz Harrosh committed
183
184
185
186

	if (!sp2d->needed)
		return;

187
188
189
	for (c = data_devs - 1; c >= 0; --c)
		for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
			struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
Boaz Harrosh's avatar
Boaz Harrosh committed
190

191
192
			if (_1ps->page_is_read[c]) {
				struct page *page = _1ps->pages[c];
Boaz Harrosh's avatar
Boaz Harrosh committed
193

194
195
196
				r4w->put_page(priv, page);
				_1ps->page_is_read[c] = false;
			}
Boaz Harrosh's avatar
Boaz Harrosh committed
197
198
		}

199
200
201
	for (p = 0; p < sp2d->pages_in_unit; p++) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];

Boaz Harrosh's avatar
Boaz Harrosh committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
		memset(_1ps->pages, 0, group_width * sizeof(*_1ps->pages));
		_1ps->write_count = 0;
		_1ps->tx = NULL;
	}

	sp2d->needed = false;
}

static void _sp2d_free(struct __stripe_pages_2d *sp2d)
{
	unsigned i;

	if (!sp2d)
		return;

	for (i = 0; i < sp2d->pages_in_unit; ++i) {
		if (sp2d->_1p_stripes[i].alloc)
			kfree(sp2d->_1p_stripes[i].pages);
	}

	kfree(sp2d);
}

static unsigned _sp2d_min_pg(struct __stripe_pages_2d *sp2d)
{
	unsigned p;

	for (p = 0; p < sp2d->pages_in_unit; p++) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];

		if (_1ps->write_count)
			return p;
	}

	return ~0;
}

static unsigned _sp2d_max_pg(struct __stripe_pages_2d *sp2d)
{
241
	int p;
Boaz Harrosh's avatar
Boaz Harrosh committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255

	for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];

		if (_1ps->write_count)
			return p;
	}

	return ~0;
}

static void _gen_xor_unit(struct __stripe_pages_2d *sp2d)
{
	unsigned p;
Boaz Harrosh's avatar
Boaz Harrosh committed
256
257
258
259
260
	unsigned tx_flags = ASYNC_TX_ACK;

	if (sp2d->parity == 1)
		tx_flags |= ASYNC_TX_XOR_ZERO_DST;

Boaz Harrosh's avatar
Boaz Harrosh committed
261
262
263
264
265
266
	for (p = 0; p < sp2d->pages_in_unit; p++) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];

		if (!_1ps->write_count)
			continue;

Boaz Harrosh's avatar
Boaz Harrosh committed
267
		init_async_submit(&_1ps->submit, tx_flags,
268
			NULL, NULL, NULL, (addr_conv_t *)_1ps->scribble);
Boaz Harrosh's avatar
Boaz Harrosh committed
269

Boaz Harrosh's avatar
Boaz Harrosh committed
270
271
272
273
274
275
276
277
		if (sp2d->parity == 1)
			_1ps->tx = async_xor(_1ps->pages[sp2d->data_devs],
						_1ps->pages, 0, sp2d->data_devs,
						PAGE_SIZE, &_1ps->submit);
		else /* parity == 2 */
			_1ps->tx = async_gen_syndrome(_1ps->pages, 0,
						sp2d->data_devs + sp2d->parity,
						PAGE_SIZE, &_1ps->submit);
Boaz Harrosh's avatar
Boaz Harrosh committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	}

	for (p = 0; p < sp2d->pages_in_unit; p++) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
		/* NOTE: We wait for HW synchronously (I don't have such HW
		 * to test with.) Is parallelism needed with today's multi
		 * cores?
		 */
		async_tx_issue_pending(_1ps->tx);
	}
}

void _ore_add_stripe_page(struct __stripe_pages_2d *sp2d,
		       struct ore_striping_info *si, struct page *page)
{
	struct __1_page_stripe *_1ps;

	sp2d->needed = true;

	_1ps = &sp2d->_1p_stripes[si->cur_pg];
	_1ps->pages[si->cur_comp] = page;
	++_1ps->write_count;

	si->cur_pg = (si->cur_pg + 1) % sp2d->pages_in_unit;
	/* si->cur_comp is advanced outside at main loop */
}

Boaz Harrosh's avatar
Boaz Harrosh committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
void _ore_add_sg_seg(struct ore_per_dev_state *per_dev, unsigned cur_len,
		     bool not_last)
{
	struct osd_sg_entry *sge;

	ORE_DBGMSG("dev=%d cur_len=0x%x not_last=%d cur_sg=%d "
		     "offset=0x%llx length=0x%x last_sgs_total=0x%x\n",
		     per_dev->dev, cur_len, not_last, per_dev->cur_sg,
		     _LLU(per_dev->offset), per_dev->length,
		     per_dev->last_sgs_total);

	if (!per_dev->cur_sg) {
		sge = per_dev->sglist;

		/* First time we prepare two entries */
		if (per_dev->length) {
			++per_dev->cur_sg;
			sge->offset = per_dev->offset;
			sge->len = per_dev->length;
		} else {
			/* Here the parity is the first unit of this object.
			 * This happens every time we reach a parity device on
			 * the same stripe as the per_dev->offset. We need to
			 * just skip this unit.
			 */
			per_dev->offset += cur_len;
			return;
		}
	} else {
		/* finalize the last one */
		sge = &per_dev->sglist[per_dev->cur_sg - 1];
		sge->len = per_dev->length - per_dev->last_sgs_total;
	}

	if (not_last) {
		/* Partly prepare the next one */
		struct osd_sg_entry *next_sge = sge + 1;

		++per_dev->cur_sg;
		next_sge->offset = sge->offset + sge->len + cur_len;
		/* Save cur len so we know how mutch was added next time */
		per_dev->last_sgs_total = per_dev->length;
		next_sge->len = 0;
	} else if (!sge->len) {
		/* Optimize for when the last unit is a parity */
		--per_dev->cur_sg;
	}
}

Boaz Harrosh's avatar
Boaz Harrosh committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
static int _alloc_read_4_write(struct ore_io_state *ios)
{
	struct ore_layout *layout = ios->layout;
	int ret;
	/* We want to only read those pages not in cache so worst case
	 * is a stripe populated with every other page
	 */
	unsigned sgs_per_dev = ios->sp2d->pages_in_unit + 2;

	ret = _ore_get_io_state(layout, ios->oc,
				layout->group_width * layout->mirrors_p1,
				sgs_per_dev, 0, &ios->ios_read_4_write);
	return ret;
}

/* @si contains info of the to-be-inserted page. Update of @si should be
 * maintained by caller. Specificaly si->dev, si->obj_offset, ...
 */
372
373
static int _add_to_r4w(struct ore_io_state *ios, struct ore_striping_info *si,
		       struct page *page, unsigned pg_len)
Boaz Harrosh's avatar
Boaz Harrosh committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
	struct request_queue *q;
	struct ore_per_dev_state *per_dev;
	struct ore_io_state *read_ios;
	unsigned first_dev = si->dev - (si->dev %
			  (ios->layout->group_width * ios->layout->mirrors_p1));
	unsigned comp = si->dev - first_dev;
	unsigned added_len;

	if (!ios->ios_read_4_write) {
		int ret = _alloc_read_4_write(ios);

		if (unlikely(ret))
			return ret;
	}

	read_ios = ios->ios_read_4_write;
	read_ios->numdevs = ios->layout->group_width * ios->layout->mirrors_p1;

	per_dev = &read_ios->per_dev[comp];
	if (!per_dev->length) {
		per_dev->bio = bio_kmalloc(GFP_KERNEL,
					   ios->sp2d->pages_in_unit);
		if (unlikely(!per_dev->bio)) {
			ORE_DBGMSG("Failed to allocate BIO size=%u\n",
				     ios->sp2d->pages_in_unit);
			return -ENOMEM;
		}
		per_dev->offset = si->obj_offset;
		per_dev->dev = si->dev;
	} else if (si->obj_offset != (per_dev->offset + per_dev->length)) {
		u64 gap = si->obj_offset - (per_dev->offset + per_dev->length);

		_ore_add_sg_seg(per_dev, gap, true);
	}
	q = osd_request_queue(ore_comp_dev(read_ios->oc, per_dev->dev));
410
411
412
	added_len = bio_add_pc_page(q, per_dev->bio, page, pg_len,
				    si->obj_offset % PAGE_SIZE);
	if (unlikely(added_len != pg_len)) {
Boaz Harrosh's avatar
Boaz Harrosh committed
413
414
415
416
417
		ORE_DBGMSG("Failed to bio_add_pc_page bi_vcnt=%d\n",
			      per_dev->bio->bi_vcnt);
		return -ENOMEM;
	}

418
	per_dev->length += pg_len;
Boaz Harrosh's avatar
Boaz Harrosh committed
419
420
421
	return 0;
}

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* read the beginning of an unaligned first page */
static int _add_to_r4w_first_page(struct ore_io_state *ios, struct page *page)
{
	struct ore_striping_info si;
	unsigned pg_len;

	ore_calc_stripe_info(ios->layout, ios->offset, 0, &si);

	pg_len = si.obj_offset % PAGE_SIZE;
	si.obj_offset -= pg_len;

	ORE_DBGMSG("offset=0x%llx len=0x%x index=0x%lx dev=%x\n",
		   _LLU(si.obj_offset), pg_len, page->index, si.dev);

	return _add_to_r4w(ios, &si, page, pg_len);
}

/* read the end of an incomplete last page */
static int _add_to_r4w_last_page(struct ore_io_state *ios, u64 *offset)
{
	struct ore_striping_info si;
	struct page *page;
	unsigned pg_len, p, c;

	ore_calc_stripe_info(ios->layout, *offset, 0, &si);

448
449
	p = si.cur_pg;
	c = si.cur_comp;
450
451
452
453
454
455
456
457
458
459
460
461
462
	page = ios->sp2d->_1p_stripes[p].pages[c];

	pg_len = PAGE_SIZE - (si.unit_off % PAGE_SIZE);
	*offset += pg_len;

	ORE_DBGMSG("p=%d, c=%d next-offset=0x%llx len=0x%x dev=%x par_dev=%d\n",
		   p, c, _LLU(*offset), pg_len, si.dev, si.par_dev);

	BUG_ON(!page);

	return _add_to_r4w(ios, &si, page, pg_len);
}

Boaz Harrosh's avatar
Boaz Harrosh committed
463
464
465
466
467
468
469
470
471
472
473
474
static void _mark_read4write_pages_uptodate(struct ore_io_state *ios, int ret)
{
	struct bio_vec *bv;
	unsigned i, d;

	/* loop on all devices all pages */
	for (d = 0; d < ios->numdevs; d++) {
		struct bio *bio = ios->per_dev[d].bio;

		if (!bio)
			continue;

475
		bio_for_each_segment_all(bv, bio, i) {
Boaz Harrosh's avatar
Boaz Harrosh committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
			struct page *page = bv->bv_page;

			SetPageUptodate(page);
			if (PageError(page))
				ClearPageError(page);
		}
	}
}

/* read_4_write is hacked to read the start of the first stripe and/or
 * the end of the last stripe. If needed, with an sg-gap at each device/page.
 * It is assumed to be called after the to_be_written pages of the first stripe
 * are populating ios->sp2d[][]
 *
 * NOTE: We call ios->r4w->lock_fn for all pages needed for parity calculations
 * These pages are held at sp2d[p].pages[c] but with
 * sp2d[p].page_is_read[c] = true. At _sp2d_reset these pages are
 * ios->r4w->lock_fn(). The ios->r4w->lock_fn might signal that the page is
 * @uptodate=true, so we don't need to read it, only unlock, after IO.
 *
 * TODO: The read_4_write should calc a need_to_read_pages_count, if bigger then
 * to-be-written count, we should consider the xor-in-place mode.
 * need_to_read_pages_count is the actual number of pages not present in cache.
 * maybe "devs_in_group - ios->sp2d[p].write_count" is a good enough
 * approximation? In this mode the read pages are put in the empty places of
 * ios->sp2d[p][*], xor is calculated the same way. These pages are
 * allocated/freed and don't go through cache
 */
504
static int _read_4_write_first_stripe(struct ore_io_state *ios)
Boaz Harrosh's avatar
Boaz Harrosh committed
505
506
507
508
{
	struct ore_striping_info read_si;
	struct __stripe_pages_2d *sp2d = ios->sp2d;
	u64 offset = ios->si.first_stripe_start;
509
	unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;
Boaz Harrosh's avatar
Boaz Harrosh committed
510
511
512
513
514
515
516

	if (offset == ios->offset) /* Go to start collect $200 */
		goto read_last_stripe;

	min_p = _sp2d_min_pg(sp2d);
	max_p = _sp2d_max_pg(sp2d);

517
518
519
	ORE_DBGMSG("stripe_start=0x%llx ios->offset=0x%llx min_p=%d max_p=%d\n",
		   offset, ios->offset, min_p, max_p);

Boaz Harrosh's avatar
Boaz Harrosh committed
520
521
522
523
524
525
526
527
528
	for (c = 0; ; c++) {
		ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
		read_si.obj_offset += min_p * PAGE_SIZE;
		offset += min_p * PAGE_SIZE;
		for (p = min_p; p <= max_p; p++) {
			struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
			struct page **pp = &_1ps->pages[c];
			bool uptodate;

529
530
531
532
			if (*pp) {
				if (ios->offset % PAGE_SIZE)
					/* Read the remainder of the page */
					_add_to_r4w_first_page(ios, *pp);
Boaz Harrosh's avatar
Boaz Harrosh committed
533
534
				/* to-be-written pages start here */
				goto read_last_stripe;
535
			}
Boaz Harrosh's avatar
Boaz Harrosh committed
536
537
538
539
540
541
542

			*pp = ios->r4w->get_page(ios->private, offset,
						 &uptodate);
			if (unlikely(!*pp))
				return -ENOMEM;

			if (!uptodate)
543
				_add_to_r4w(ios, &read_si, *pp, PAGE_SIZE);
Boaz Harrosh's avatar
Boaz Harrosh committed
544
545
546
547
548
549
550
551
552
553

			/* Mark read-pages to be cache_released */
			_1ps->page_is_read[c] = true;
			read_si.obj_offset += PAGE_SIZE;
			offset += PAGE_SIZE;
		}
		offset += (sp2d->pages_in_unit - p) * PAGE_SIZE;
	}

read_last_stripe:
554
555
556
557
558
559
560
561
562
563
564
565
	return 0;
}

static int _read_4_write_last_stripe(struct ore_io_state *ios)
{
	struct ore_striping_info read_si;
	struct __stripe_pages_2d *sp2d = ios->sp2d;
	u64 offset;
	u64 last_stripe_end;
	unsigned bytes_in_stripe = ios->si.bytes_in_stripe;
	unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;

566
567
568
569
570
	offset = ios->offset + ios->length;
	if (offset % PAGE_SIZE)
		_add_to_r4w_last_page(ios, &offset);
		/* offset will be aligned to next page */

Boaz Harrosh's avatar
Boaz Harrosh committed
571
572
573
574
575
576
	last_stripe_end = div_u64(offset + bytes_in_stripe - 1, bytes_in_stripe)
				 * bytes_in_stripe;
	if (offset == last_stripe_end) /* Optimize for the aligned case */
		goto read_it;

	ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
577
578
	p = read_si.cur_pg;
	c = read_si.cur_comp;
Boaz Harrosh's avatar
Boaz Harrosh committed
579
580
581
582
583
584
585

	if (min_p == sp2d->pages_in_unit) {
		/* Didn't do it yet */
		min_p = _sp2d_min_pg(sp2d);
		max_p = _sp2d_max_pg(sp2d);
	}

586
587
588
	ORE_DBGMSG("offset=0x%llx stripe_end=0x%llx min_p=%d max_p=%d\n",
		   offset, last_stripe_end, min_p, max_p);

Boaz Harrosh's avatar
Boaz Harrosh committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
	while (offset < last_stripe_end) {
		struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];

		if ((min_p <= p) && (p <= max_p)) {
			struct page *page;
			bool uptodate;

			BUG_ON(_1ps->pages[c]);
			page = ios->r4w->get_page(ios->private, offset,
						  &uptodate);
			if (unlikely(!page))
				return -ENOMEM;

			_1ps->pages[c] = page;
			/* Mark read-pages to be cache_released */
			_1ps->page_is_read[c] = true;
			if (!uptodate)
606
				_add_to_r4w(ios, &read_si, page, PAGE_SIZE);
Boaz Harrosh's avatar
Boaz Harrosh committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
		}

		offset += PAGE_SIZE;
		if (p == (sp2d->pages_in_unit - 1)) {
			++c;
			p = 0;
			ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
		} else {
			read_si.obj_offset += PAGE_SIZE;
			++p;
		}
	}

read_it:
621
622
623
624
625
626
627
628
629
	return 0;
}

static int _read_4_write_execute(struct ore_io_state *ios)
{
	struct ore_io_state *ios_read;
	unsigned i;
	int ret;

Boaz Harrosh's avatar
Boaz Harrosh committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
	ios_read = ios->ios_read_4_write;
	if (!ios_read)
		return 0;

	/* FIXME: Ugly to signal _sbi_read_mirror that we have bio(s). Change
	 * to check for per_dev->bio
	 */
	ios_read->pages = ios->pages;

	/* Now read these devices */
	for (i = 0; i < ios_read->numdevs; i += ios_read->layout->mirrors_p1) {
		ret = _ore_read_mirror(ios_read, i);
		if (unlikely(ret))
			return ret;
	}

	ret = ore_io_execute(ios_read); /* Synchronus execution */
	if (unlikely(ret)) {
		ORE_DBGMSG("!! ore_io_execute => %d\n", ret);
		return ret;
	}

	_mark_read4write_pages_uptodate(ios_read, ret);
653
654
	ore_put_io_state(ios_read);
	ios->ios_read_4_write = NULL; /* Might need a reuse at last stripe */
Boaz Harrosh's avatar
Boaz Harrosh committed
655
656
657
	return 0;
}

Boaz Harrosh's avatar
Boaz Harrosh committed
658
659
660
661
/* In writes @cur_len means length left. .i.e cur_len==0 is the last parity U */
int _ore_add_parity_unit(struct ore_io_state *ios,
			    struct ore_striping_info *si,
			    struct ore_per_dev_state *per_dev,
Boaz Harrosh's avatar
Boaz Harrosh committed
662
			    unsigned cur_len, bool do_xor)
Boaz Harrosh's avatar
Boaz Harrosh committed
663
664
{
	if (ios->reading) {
665
666
667
668
669
		if (per_dev->cur_sg >= ios->sgs_per_dev) {
			ORE_DBGMSG("cur_sg(%d) >= sgs_per_dev(%d)\n" ,
				per_dev->cur_sg, ios->sgs_per_dev);
			return -ENOMEM;
		}
Boaz Harrosh's avatar
Boaz Harrosh committed
670
671
		_ore_add_sg_seg(per_dev, cur_len, true);
	} else {
Boaz Harrosh's avatar
Boaz Harrosh committed
672
		struct __stripe_pages_2d *sp2d = ios->sp2d;
Boaz Harrosh's avatar
Boaz Harrosh committed
673
		struct page **pages = ios->parity_pages + ios->cur_par_page;
Boaz Harrosh's avatar
Boaz Harrosh committed
674
		unsigned num_pages;
Boaz Harrosh's avatar
Boaz Harrosh committed
675
676
677
678
		unsigned array_start = 0;
		unsigned i;
		int ret;

Boaz Harrosh's avatar
Boaz Harrosh committed
679
680
681
682
683
684
685
686
		si->cur_pg = _sp2d_min_pg(sp2d);
		num_pages  = _sp2d_max_pg(sp2d) + 1 - si->cur_pg;

		if (!per_dev->length) {
			per_dev->offset += si->cur_pg * PAGE_SIZE;
			/* If first stripe, Read in all read4write pages
			 * (if needed) before we calculate the first parity.
			 */
Boaz Harrosh's avatar
Boaz Harrosh committed
687
688
			if (do_xor)
				_read_4_write_first_stripe(ios);
Boaz Harrosh's avatar
Boaz Harrosh committed
689
		}
Boaz Harrosh's avatar
Boaz Harrosh committed
690
691
		if (!cur_len && do_xor)
			/* If last stripe r4w pages of last stripe */
692
693
			_read_4_write_last_stripe(ios);
		_read_4_write_execute(ios);
Boaz Harrosh's avatar
Boaz Harrosh committed
694

Boaz Harrosh's avatar
Boaz Harrosh committed
695
696
697
698
699
700
701
702
		for (i = 0; i < num_pages; i++) {
			pages[i] = _raid_page_alloc();
			if (unlikely(!pages[i]))
				return -ENOMEM;

			++(ios->cur_par_page);
		}

Boaz Harrosh's avatar
Boaz Harrosh committed
703
		BUG_ON(si->cur_comp < sp2d->data_devs);
Boaz Harrosh's avatar
Boaz Harrosh committed
704
		BUG_ON(si->cur_pg + num_pages > sp2d->pages_in_unit);
Boaz Harrosh's avatar
Boaz Harrosh committed
705
706
707
708
709

		ret = _ore_add_stripe_unit(ios,  &array_start, 0, pages,
					   per_dev, num_pages * PAGE_SIZE);
		if (unlikely(ret))
			return ret;
Boaz Harrosh's avatar
Boaz Harrosh committed
710

Boaz Harrosh's avatar
Boaz Harrosh committed
711
712
713
714
		if (do_xor) {
			_gen_xor_unit(sp2d);
			_sp2d_reset(sp2d, ios->r4w, ios->private);
		}
Boaz Harrosh's avatar
Boaz Harrosh committed
715
716
717
718
719
720
	}
	return 0;
}

int _ore_post_alloc_raid_stuff(struct ore_io_state *ios)
{
Boaz Harrosh's avatar
Boaz Harrosh committed
721
	if (ios->parity_pages) {
722
		struct ore_layout *layout = ios->layout;
Boaz Harrosh's avatar
Boaz Harrosh committed
723
724
725
726
727
728
729
		unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;

		if (_sp2d_alloc(pages_in_unit, layout->group_width,
				layout->parity, &ios->sp2d)) {
			return -ENOMEM;
		}
	}
Boaz Harrosh's avatar
Boaz Harrosh committed
730
731
732
733
734
	return 0;
}

void _ore_free_raid_stuff(struct ore_io_state *ios)
{
Boaz Harrosh's avatar
Boaz Harrosh committed
735
	if (ios->sp2d) { /* writing and raid */
Boaz Harrosh's avatar
Boaz Harrosh committed
736
737
738
739
740
741
742
743
744
745
		unsigned i;

		for (i = 0; i < ios->cur_par_page; i++) {
			struct page *page = ios->parity_pages[i];

			if (page)
				_raid_page_free(page);
		}
		if (ios->extra_part_alloc)
			kfree(ios->parity_pages);
Boaz Harrosh's avatar
Boaz Harrosh committed
746
747
748
		/* If IO returned an error pages might need unlocking */
		_sp2d_reset(ios->sp2d, ios->r4w, ios->private);
		_sp2d_free(ios->sp2d);
Boaz Harrosh's avatar
Boaz Harrosh committed
749
750
751
752
753
	} else {
		/* Will only be set if raid reading && sglist is big */
		if (ios->extra_part_alloc)
			kfree(ios->per_dev[0].sglist);
	}
Boaz Harrosh's avatar
Boaz Harrosh committed
754
755
	if (ios->ios_read_4_write)
		ore_put_io_state(ios->ios_read_4_write);
Boaz Harrosh's avatar
Boaz Harrosh committed
756
}