ore.c 19.8 KB
Newer Older
1 2
/*
 * Copyright (C) 2005, 2006
B
Boaz Harrosh 已提交
3
 * Avishay Traeger (avishay@gmail.com)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 2008, 2009
 * Boaz Harrosh <bharrosh@panasas.com>
 *
 * This file is part of exofs.
 *
 * exofs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.  Since it is based on ext2, and the only
 * valid version of GPL for the Linux kernel is version 2, the only valid
 * version of GPL for exofs is version 2.
 *
 * exofs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with exofs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

25
#include <linux/slab.h>
B
Boaz Harrosh 已提交
26
#include <asm/div64.h>
27

28
#include <scsi/osd_ore.h>
29

30
#define ORE_ERR(fmt, a...) printk(KERN_ERR "ore: " fmt, ##a)
B
Boaz Harrosh 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45
#ifdef CONFIG_EXOFS_DEBUG
#define ORE_DBGMSG(fmt, a...) \
	printk(KERN_NOTICE "ore @%s:%d: " fmt, __func__, __LINE__, ##a)
#else
#define ORE_DBGMSG(fmt, a...) \
	do { if (0) printk(fmt, ##a); } while (0)
#endif

/* u64 has problems with printk this will cast it to unsigned long long */
#define _LLU(x) (unsigned long long)(x)

#define ORE_DBGMSG2(M...) do {} while (0)
/* #define ORE_DBGMSG2 ORE_DBGMSG */

B
Boaz Harrosh 已提交
46 47 48 49
MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
MODULE_LICENSE("GPL");

50
static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
51
{
52
	return ios->oc->comps[index & ios->oc->single_comp].cred;
53 54
}

55
static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
56
{
57
	return &ios->oc->comps[index & ios->oc->single_comp].obj;
58 59
}

60
static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
61
{
62
	return ore_comp_dev(ios->oc, index);
63 64
}

65
int  ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
66 67
		      bool is_reading, u64 offset, u64 length,
		      struct ore_io_state **pios)
68
{
69
	struct ore_io_state *ios;
70 71

	/*TODO: Maybe use kmem_cach per sbi of size
72
	 * exofs_io_state_size(layout->s_numdevs)
73
	 */
74
	ios = kzalloc(ore_io_state_size(oc->numdevs), GFP_KERNEL);
75
	if (unlikely(!ios)) {
76
		ORE_DBGMSG("Failed kzalloc bytes=%d\n",
77
			     ore_io_state_size(oc->numdevs));
78 79 80 81
		*pios = NULL;
		return -ENOMEM;
	}

82
	ios->layout = layout;
83
	ios->oc = oc;
84 85 86 87
	ios->offset = offset;
	ios->length = length;
	ios->reading = is_reading;

88 89
	*pios = ios;
	return 0;
90
}
B
Boaz Harrosh 已提交
91
EXPORT_SYMBOL(ore_get_rw_state);
92

93
int  ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
94
		      struct ore_io_state **ios)
95
{
96
	return ore_get_rw_state(layout, oc, true, 0, 0, ios);
97
}
B
Boaz Harrosh 已提交
98
EXPORT_SYMBOL(ore_get_io_state);
99

100
void ore_put_io_state(struct ore_io_state *ios)
101
{
102 103
	if (ios) {
		unsigned i;
104

105
		for (i = 0; i < ios->numdevs; i++) {
106
			struct ore_per_dev_state *per_dev = &ios->per_dev[i];
107 108 109 110 111 112 113 114

			if (per_dev->or)
				osd_end_request(per_dev->or);
			if (per_dev->bio)
				bio_put(per_dev->bio);
		}

		kfree(ios);
115
	}
116
}
B
Boaz Harrosh 已提交
117
EXPORT_SYMBOL(ore_put_io_state);
118

119
static void _sync_done(struct ore_io_state *ios, void *p)
120 121
{
	struct completion *waiting = p;
122

123 124 125 126 127
	complete(waiting);
}

static void _last_io(struct kref *kref)
{
128 129
	struct ore_io_state *ios = container_of(
					kref, struct ore_io_state, kref);
130 131 132 133 134 135

	ios->done(ios, ios->private);
}

static void _done_io(struct osd_request *or, void *p)
{
136
	struct ore_io_state *ios = p;
137 138 139 140

	kref_put(&ios->kref, _last_io);
}

141
static int ore_io_execute(struct ore_io_state *ios)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
{
	DECLARE_COMPLETION_ONSTACK(wait);
	bool sync = (ios->done == NULL);
	int i, ret;

	if (sync) {
		ios->done = _sync_done;
		ios->private = &wait;
	}

	for (i = 0; i < ios->numdevs; i++) {
		struct osd_request *or = ios->per_dev[i].or;
		if (unlikely(!or))
			continue;

157
		ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
158
		if (unlikely(ret)) {
159
			ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
				     ret);
			return ret;
		}
	}

	kref_init(&ios->kref);

	for (i = 0; i < ios->numdevs; i++) {
		struct osd_request *or = ios->per_dev[i].or;
		if (unlikely(!or))
			continue;

		kref_get(&ios->kref);
		osd_execute_request_async(or, _done_io, ios);
	}

	kref_put(&ios->kref, _last_io);
	ret = 0;

	if (sync) {
		wait_for_completion(&wait);
181
		ret = ore_check_io(ios, NULL);
182
	}
183 184 185
	return ret;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static void _clear_bio(struct bio *bio)
{
	struct bio_vec *bv;
	unsigned i;

	__bio_for_each_segment(bv, bio, i, 0) {
		unsigned this_count = bv->bv_len;

		if (likely(PAGE_SIZE == this_count))
			clear_highpage(bv->bv_page);
		else
			zero_user(bv->bv_page, bv->bv_offset, this_count);
	}
}

201
int ore_check_io(struct ore_io_state *ios, u64 *resid)
202
{
203 204 205
	enum osd_err_priority acumulated_osd_err = 0;
	int acumulated_lin_err = 0;
	int i;
206

207 208
	for (i = 0; i < ios->numdevs; i++) {
		struct osd_sense_info osi;
209 210 211 212 213
		struct osd_request *or = ios->per_dev[i].or;
		int ret;

		if (unlikely(!or))
			continue;
214

215
		ret = osd_req_decode_sense(or, &osi);
216 217 218
		if (likely(!ret))
			continue;

219 220 221
		if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
			/* start read offset passed endof file */
			_clear_bio(ios->per_dev[i].bio);
222
			ORE_DBGMSG("start read offset passed end of file "
223
				"offset=0x%llx, length=0x%llx\n",
B
Boaz Harrosh 已提交
224 225
				_LLU(ios->per_dev[i].offset),
				_LLU(ios->per_dev[i].length));
226 227

			continue; /* we recovered */
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		}

		if (osi.osd_err_pri >= acumulated_osd_err) {
			acumulated_osd_err = osi.osd_err_pri;
			acumulated_lin_err = ret;
		}
	}

	/* TODO: raid specific residual calculations */
	if (resid) {
		if (likely(!acumulated_lin_err))
			*resid = 0;
		else
			*resid = ios->length;
	}

	return acumulated_lin_err;
}
B
Boaz Harrosh 已提交
246
EXPORT_SYMBOL(ore_check_io);
247

B
Boaz Harrosh 已提交
248 249 250
/*
 * L - logical offset into the file
 *
B
Boaz Harrosh 已提交
251
 * U - The number of bytes in a stripe within a group
B
Boaz Harrosh 已提交
252 253 254
 *
 *	U = stripe_unit * group_width
 *
B
Boaz Harrosh 已提交
255 256
 * T - The number of bytes striped within a group of component objects
 *     (before advancing to the next group)
B
Boaz Harrosh 已提交
257
 *
B
Boaz Harrosh 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
 *	T = stripe_unit * group_width * group_depth
 *
 * S - The number of bytes striped across all component objects
 *     before the pattern repeats
 *
 *	S = stripe_unit * group_width * group_depth * group_count
 *
 * M - The "major" (i.e., across all components) stripe number
 *
 *	M = L / S
 *
 * G - Counts the groups from the beginning of the major stripe
 *
 *	G = (L - (M * S)) / T	[or (L % S) / T]
 *
 * H - The byte offset within the group
 *
 *	H = (L - (M * S)) % T	[or (L % S) % T]
 *
 * N - The "minor" (i.e., across the group) stripe number
 *
 *	N = H / U
B
Boaz Harrosh 已提交
280 281 282
 *
 * C - The component index coresponding to L
 *
B
Boaz Harrosh 已提交
283 284
 *	C = (H - (N * U)) / stripe_unit + G * group_width
 *	[or (L % U) / stripe_unit + G * group_width]
B
Boaz Harrosh 已提交
285 286 287
 *
 * O - The component offset coresponding to L
 *
B
Boaz Harrosh 已提交
288
 *	O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
B
Boaz Harrosh 已提交
289
 */
290 291
static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
				 struct ore_striping_info *si)
B
Boaz Harrosh 已提交
292
{
293 294 295
	u32	stripe_unit = layout->stripe_unit;
	u32	group_width = layout->group_width;
	u64	group_depth = layout->group_depth;
B
Boaz Harrosh 已提交
296

B
Boaz Harrosh 已提交
297
	u32	U = stripe_unit * group_width;
B
Boaz Harrosh 已提交
298
	u64	T = U * group_depth;
299
	u64	S = T * layout->group_count;
B
Boaz Harrosh 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
	u64	M = div64_u64(file_offset, S);

	/*
	G = (L - (M * S)) / T
	H = (L - (M * S)) % T
	*/
	u64	LmodS = file_offset - M * S;
	u32	G = div64_u64(LmodS, T);
	u64	H = LmodS - G * T;

	u32	N = div_u64(H, U);

	/* "H - (N * U)" is just "H % U" so it's bound to u32 */
	si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
314
	si->dev *= layout->mirrors_p1;
B
Boaz Harrosh 已提交
315

B
Boaz Harrosh 已提交
316
	div_u64_rem(file_offset, stripe_unit, &si->unit_off);
B
Boaz Harrosh 已提交
317

B
Boaz Harrosh 已提交
318 319 320 321
	si->obj_offset = si->unit_off + (N * stripe_unit) +
				  (M * group_depth * stripe_unit);

	si->group_length = T - H;
322
	si->M = M;
B
Boaz Harrosh 已提交
323 324
}

325 326
static int _add_stripe_unit(struct ore_io_state *ios,  unsigned *cur_pg,
		unsigned pgbase, struct ore_per_dev_state *per_dev,
327
		int cur_len)
B
Boaz Harrosh 已提交
328
{
329
	unsigned pg = *cur_pg;
B
Boaz Harrosh 已提交
330
	struct request_queue *q =
331
			osd_request_queue(_ios_od(ios, per_dev->dev));
B
Boaz Harrosh 已提交
332 333 334 335 336 337

	per_dev->length += cur_len;

	if (per_dev->bio == NULL) {
		unsigned pages_in_stripe = ios->layout->group_width *
					(ios->layout->stripe_unit / PAGE_SIZE);
338
		unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
B
Boaz Harrosh 已提交
339 340 341 342
						ios->layout->group_width;

		per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
		if (unlikely(!per_dev->bio)) {
343
			ORE_DBGMSG("Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
344 345 346 347 348 349
				     bio_size);
			return -ENOMEM;
		}
	}

	while (cur_len > 0) {
350 351
		unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
		unsigned added_len;
B
Boaz Harrosh 已提交
352

353 354
		BUG_ON(ios->nr_pages <= pg);
		cur_len -= pglen;
B
Boaz Harrosh 已提交
355

356 357 358
		added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
					    pglen, pgbase);
		if (unlikely(pglen != added_len))
B
Boaz Harrosh 已提交
359
			return -ENOMEM;
360 361
		pgbase = 0;
		++pg;
B
Boaz Harrosh 已提交
362 363 364
	}
	BUG_ON(cur_len);

365
	*cur_pg = pg;
B
Boaz Harrosh 已提交
366 367 368
	return 0;
}

369
static int _prepare_one_group(struct ore_io_state *ios, u64 length,
370
			      struct ore_striping_info *si)
B
Boaz Harrosh 已提交
371 372
{
	unsigned stripe_unit = ios->layout->stripe_unit;
B
Boaz Harrosh 已提交
373
	unsigned mirrors_p1 = ios->layout->mirrors_p1;
B
Boaz Harrosh 已提交
374
	unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
B
Boaz Harrosh 已提交
375
	unsigned dev = si->dev;
B
Boaz Harrosh 已提交
376 377 378
	unsigned first_dev = dev - (dev % devs_in_group);
	unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
	unsigned cur_pg = ios->pages_consumed;
379
	int ret = 0;
B
Boaz Harrosh 已提交
380 381

	while (length) {
382
		struct ore_per_dev_state *per_dev = &ios->per_dev[dev];
B
Boaz Harrosh 已提交
383
		unsigned cur_len, page_off = 0;
B
Boaz Harrosh 已提交
384 385

		if (!per_dev->length) {
B
Boaz Harrosh 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399
			per_dev->dev = dev;
			if (dev < si->dev) {
				per_dev->offset = si->obj_offset + stripe_unit -
								   si->unit_off;
				cur_len = stripe_unit;
			} else if (dev == si->dev) {
				per_dev->offset = si->obj_offset;
				cur_len = stripe_unit - si->unit_off;
				page_off = si->unit_off & ~PAGE_MASK;
				BUG_ON(page_off && (page_off != ios->pgbase));
			} else { /* dev > si->dev */
				per_dev->offset = si->obj_offset - si->unit_off;
				cur_len = stripe_unit;
			}
B
Boaz Harrosh 已提交
400

401 402
			if (max_comp < dev)
				max_comp = dev;
B
Boaz Harrosh 已提交
403
		} else {
B
Boaz Harrosh 已提交
404
			cur_len = stripe_unit;
B
Boaz Harrosh 已提交
405
		}
B
Boaz Harrosh 已提交
406 407
		if (cur_len >= length)
			cur_len = length;
B
Boaz Harrosh 已提交
408

409 410
		ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
				       cur_len);
B
Boaz Harrosh 已提交
411 412 413
		if (unlikely(ret))
			goto out;

414 415
		dev += mirrors_p1;
		dev = (dev % devs_in_group) + first_dev;
B
Boaz Harrosh 已提交
416 417 418 419

		length -= cur_len;
	}
out:
B
Boaz Harrosh 已提交
420 421
	ios->numdevs = max_comp + mirrors_p1;
	ios->pages_consumed = cur_pg;
B
Boaz Harrosh 已提交
422 423 424
	return ret;
}

425
static int _prepare_for_striping(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
426
{
B
Boaz Harrosh 已提交
427
	u64 length = ios->length;
428
	u64 offset = ios->offset;
429
	struct ore_striping_info si;
B
Boaz Harrosh 已提交
430
	int ret = 0;
B
Boaz Harrosh 已提交
431 432 433

	if (!ios->pages) {
		if (ios->kern_buff) {
434
			struct ore_per_dev_state *per_dev = &ios->per_dev[0];
B
Boaz Harrosh 已提交
435

436
			ore_calc_stripe_info(ios->layout, ios->offset, &si);
B
Boaz Harrosh 已提交
437 438 439 440 441 442 443 444 445 446 447 448
			per_dev->offset = si.obj_offset;
			per_dev->dev = si.dev;

			/* no cross device without page array */
			BUG_ON((ios->layout->group_width > 1) &&
			       (si.unit_off + ios->length >
				ios->layout->stripe_unit));
		}
		ios->numdevs = ios->layout->mirrors_p1;
		return 0;
	}

B
Boaz Harrosh 已提交
449
	while (length) {
450
		ore_calc_stripe_info(ios->layout, offset, &si);
451

B
Boaz Harrosh 已提交
452 453 454
		if (length < si.group_length)
			si.group_length = length;

455
		ret = _prepare_one_group(ios, si.group_length, &si);
B
Boaz Harrosh 已提交
456 457 458
		if (unlikely(ret))
			goto out;

459
		offset += si.group_length;
B
Boaz Harrosh 已提交
460 461 462 463 464
		length -= si.group_length;
	}

out:
	return ret;
B
Boaz Harrosh 已提交
465 466
}

467
int ore_create(struct ore_io_state *ios)
468 469 470
{
	int i, ret;

471
	for (i = 0; i < ios->oc->numdevs; i++) {
472 473
		struct osd_request *or;

474
		or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
475
		if (unlikely(!or)) {
476
			ORE_ERR("%s: osd_start_request failed\n", __func__);
477 478 479 480 481 482
			ret = -ENOMEM;
			goto out;
		}
		ios->per_dev[i].or = or;
		ios->numdevs++;

483
		osd_req_create_object(or, _ios_obj(ios, i));
484
	}
485
	ret = ore_io_execute(ios);
486 487 488 489

out:
	return ret;
}
B
Boaz Harrosh 已提交
490
EXPORT_SYMBOL(ore_create);
491

492
int ore_remove(struct ore_io_state *ios)
493 494 495
{
	int i, ret;

496
	for (i = 0; i < ios->oc->numdevs; i++) {
497 498
		struct osd_request *or;

499
		or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
500
		if (unlikely(!or)) {
501
			ORE_ERR("%s: osd_start_request failed\n", __func__);
502 503 504 505 506 507
			ret = -ENOMEM;
			goto out;
		}
		ios->per_dev[i].or = or;
		ios->numdevs++;

508
		osd_req_remove_object(or, _ios_obj(ios, i));
509
	}
510
	ret = ore_io_execute(ios);
511 512 513 514

out:
	return ret;
}
B
Boaz Harrosh 已提交
515
EXPORT_SYMBOL(ore_remove);
516

517
static int _write_mirror(struct ore_io_state *ios, int cur_comp)
518
{
519
	struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
B
Boaz Harrosh 已提交
520 521 522
	unsigned dev = ios->per_dev[cur_comp].dev;
	unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
	int ret = 0;
523

B
Boaz Harrosh 已提交
524 525 526
	if (ios->pages && !master_dev->length)
		return 0; /* Just an empty slot */

B
Boaz Harrosh 已提交
527
	for (; cur_comp < last_comp; ++cur_comp, ++dev) {
528
		struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
529 530
		struct osd_request *or;

531
		or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
532
		if (unlikely(!or)) {
533
			ORE_ERR("%s: osd_start_request failed\n", __func__);
534 535 536
			ret = -ENOMEM;
			goto out;
		}
B
Boaz Harrosh 已提交
537 538
		per_dev->or = or;
		per_dev->offset = master_dev->offset;
539

540
		if (ios->pages) {
541 542
			struct bio *bio;

B
Boaz Harrosh 已提交
543
			if (per_dev != master_dev) {
B
Boaz Harrosh 已提交
544
				bio = bio_kmalloc(GFP_KERNEL,
B
Boaz Harrosh 已提交
545
						  master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
546
				if (unlikely(!bio)) {
547
					ORE_DBGMSG(
P
Paul Bolle 已提交
548
					      "Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
549
					      master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
550 551 552 553
					ret = -ENOMEM;
					goto out;
				}

B
Boaz Harrosh 已提交
554
				__bio_clone(bio, master_dev->bio);
B
Boaz Harrosh 已提交
555 556
				bio->bi_bdev = NULL;
				bio->bi_next = NULL;
B
Boaz Harrosh 已提交
557 558 559
				per_dev->length = master_dev->length;
				per_dev->bio =  bio;
				per_dev->dev = dev;
B
Boaz Harrosh 已提交
560
			} else {
B
Boaz Harrosh 已提交
561 562
				bio = master_dev->bio;
				/* FIXME: bio_set_dir() */
563
				bio->bi_rw |= REQ_WRITE;
B
Boaz Harrosh 已提交
564
			}
565

566 567
			osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
				      bio, per_dev->length);
568
			ORE_DBGMSG("write(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
569
				      "length=0x%llx dev=%d\n",
570 571
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
572
				     _LLU(per_dev->length), dev);
573
		} else if (ios->kern_buff) {
574 575 576
			ret = osd_req_write_kern(or, _ios_obj(ios, dev),
						 per_dev->offset,
						 ios->kern_buff, ios->length);
B
Boaz Harrosh 已提交
577 578
			if (unlikely(ret))
				goto out;
579
			ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
580
				      "length=0x%llx dev=%d\n",
581 582
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
583
				     _LLU(ios->length), dev);
584
		} else {
585
			osd_req_set_attributes(or, _ios_obj(ios, dev));
586
			ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
587 588
				     _LLU(_ios_obj(ios, dev)->id),
				     ios->out_attr_len, dev);
589 590 591 592 593 594 595 596 597
		}

		if (ios->out_attr)
			osd_req_add_set_attr_list(or, ios->out_attr,
						  ios->out_attr_len);

		if (ios->in_attr)
			osd_req_add_get_attr_list(or, ios->in_attr,
						  ios->in_attr_len);
598
	}
599 600 601 602 603

out:
	return ret;
}

604
int ore_write(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
605 606 607 608 609 610 611 612 613
{
	int i;
	int ret;

	ret = _prepare_for_striping(ios);
	if (unlikely(ret))
		return ret;

	for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
614
		ret = _write_mirror(ios, i);
B
Boaz Harrosh 已提交
615 616 617 618
		if (unlikely(ret))
			return ret;
	}

619
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
620 621
	return ret;
}
B
Boaz Harrosh 已提交
622
EXPORT_SYMBOL(ore_write);
B
Boaz Harrosh 已提交
623

624
static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
625
{
B
Boaz Harrosh 已提交
626
	struct osd_request *or;
627
	struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
628 629
	struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
	unsigned first_dev = (unsigned)obj->id;
630

B
Boaz Harrosh 已提交
631 632 633
	if (ios->pages && !per_dev->length)
		return 0; /* Just an empty slot */

B
Boaz Harrosh 已提交
634
	first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
635
	or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
B
Boaz Harrosh 已提交
636
	if (unlikely(!or)) {
637
		ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
638 639 640 641
		return -ENOMEM;
	}
	per_dev->or = or;

642
	if (ios->pages) {
643
		osd_req_read(or, obj, per_dev->offset,
B
Boaz Harrosh 已提交
644
				per_dev->bio, per_dev->length);
645
		ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
646
			     " dev=%d\n", _LLU(obj->id),
B
Boaz Harrosh 已提交
647
			     _LLU(per_dev->offset), _LLU(per_dev->length),
B
Boaz Harrosh 已提交
648 649
			     first_dev);
	} else if (ios->kern_buff) {
650
		int ret = osd_req_read_kern(or, obj, per_dev->offset,
B
Boaz Harrosh 已提交
651
					    ios->kern_buff, ios->length);
652
		ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
653
			      "length=0x%llx dev=%d ret=>%d\n",
654
			      _LLU(obj->id), _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
655 656 657 658
			      _LLU(ios->length), first_dev, ret);
		if (unlikely(ret))
			return ret;
	} else {
659
		osd_req_get_attributes(or, obj);
660
		ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
661 662
			      _LLU(obj->id),
			      ios->in_attr_len, first_dev);
B
Boaz Harrosh 已提交
663 664 665
	}
	if (ios->out_attr)
		osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
666

B
Boaz Harrosh 已提交
667 668
	if (ios->in_attr)
		osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
669

B
Boaz Harrosh 已提交
670 671 672
	return 0;
}

673
int ore_read(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
674 675 676 677 678 679 680 681 682
{
	int i;
	int ret;

	ret = _prepare_for_striping(ios);
	if (unlikely(ret))
		return ret;

	for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
683
		ret = _read_mirror(ios, i);
B
Boaz Harrosh 已提交
684 685 686 687
		if (unlikely(ret))
			return ret;
	}

688
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
689
	return ret;
690
}
B
Boaz Harrosh 已提交
691
EXPORT_SYMBOL(ore_read);
692

693
int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
694 695 696 697 698 699 700
{
	struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
	void *iter = NULL;
	int nelem;

	do {
		nelem = 1;
701 702
		osd_req_decode_get_attr_list(ios->per_dev[0].or,
					     &cur_attr, &nelem, &iter);
703 704 705 706 707 708 709 710 711 712
		if ((cur_attr.attr_page == attr->attr_page) &&
		    (cur_attr.attr_id == attr->attr_id)) {
			attr->len = cur_attr.len;
			attr->val_ptr = cur_attr.val_ptr;
			return 0;
		}
	} while (iter);

	return -EIO;
}
B
Boaz Harrosh 已提交
713
EXPORT_SYMBOL(extract_attr_from_ios);
714

715
static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
B
Boaz Harrosh 已提交
716 717 718 719 720
			     struct osd_attr *attr)
{
	int last_comp = cur_comp + ios->layout->mirrors_p1;

	for (; cur_comp < last_comp; ++cur_comp) {
721
		struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
B
Boaz Harrosh 已提交
722 723
		struct osd_request *or;

724
		or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
B
Boaz Harrosh 已提交
725
		if (unlikely(!or)) {
726
			ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
727 728 729 730
			return -ENOMEM;
		}
		per_dev->or = or;

731
		osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
B
Boaz Harrosh 已提交
732 733 734 735 736 737
		osd_req_add_set_attr_list(or, attr, 1);
	}

	return 0;
}

738
struct _trunc_info {
739
	struct ore_striping_info si;
740 741 742 743 744 745 746 747
	u64 prev_group_obj_off;
	u64 next_group_obj_off;

	unsigned first_group_dev;
	unsigned nex_group_dev;
	unsigned max_devs;
};

748 749
static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
			     struct _trunc_info *ti)
750 751 752
{
	unsigned stripe_unit = layout->stripe_unit;

753
	ore_calc_stripe_info(layout, file_offset, &ti->si);
754 755 756 757 758 759 760 761 762

	ti->prev_group_obj_off = ti->si.M * stripe_unit;
	ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;

	ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
	ti->nex_group_dev = ti->first_group_dev + layout->group_width;
	ti->max_devs = layout->group_width * layout->group_count;
}

763
int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
764
		   u64 size)
765
{
766
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
767 768 769 770
	struct exofs_trunc_attr {
		struct osd_attr attr;
		__be64 newsize;
	} *size_attrs;
771
	struct _trunc_info ti;
772 773
	int i, ret;

774
	ret = ore_get_io_state(layout, oc, &ios);
B
Boaz Harrosh 已提交
775 776 777
	if (unlikely(ret))
		return ret;

778 779 780
	_calc_trunk_info(ios->layout, size, &ti);

	size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
B
Boaz Harrosh 已提交
781 782 783 784 785
			     GFP_KERNEL);
	if (unlikely(!size_attrs)) {
		ret = -ENOMEM;
		goto out;
	}
786

787
	ios->numdevs = ios->oc->numdevs;
788

789
	for (i = 0; i < ti.max_devs; ++i) {
B
Boaz Harrosh 已提交
790 791
		struct exofs_trunc_attr *size_attr = &size_attrs[i];
		u64 obj_size;
792

793 794 795 796 797 798 799 800 801 802 803
		if (i < ti.first_group_dev)
			obj_size = ti.prev_group_obj_off;
		else if (i >= ti.nex_group_dev)
			obj_size = ti.next_group_obj_off;
		else if (i < ti.si.dev) /* dev within this group */
			obj_size = ti.si.obj_offset +
				      ios->layout->stripe_unit - ti.si.unit_off;
		else if (i == ti.si.dev)
			obj_size = ti.si.obj_offset;
		else /* i > ti.dev */
			obj_size = ti.si.obj_offset - ti.si.unit_off;
804

B
Boaz Harrosh 已提交
805 806 807 808
		size_attr->newsize = cpu_to_be64(obj_size);
		size_attr->attr = g_attr_logical_length;
		size_attr->attr.val_ptr = &size_attr->newsize;

809
		ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
810
			     _LLU(oc->comps->obj.id), _LLU(obj_size), i);
B
Boaz Harrosh 已提交
811 812 813 814
		ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
					&size_attr->attr);
		if (unlikely(ret))
			goto out;
815
	}
816
	ret = ore_io_execute(ios);
817 818

out:
B
Boaz Harrosh 已提交
819
	kfree(size_attrs);
820
	ore_put_io_state(ios);
821 822
	return ret;
}
B
Boaz Harrosh 已提交
823
EXPORT_SYMBOL(ore_truncate);
824 825 826

const struct osd_attr g_attr_logical_length = ATTR_DEF(
	OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
B
Boaz Harrosh 已提交
827
EXPORT_SYMBOL(g_attr_logical_length);