ore.c 21.1 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 51 52
static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
				 struct ore_striping_info *si);

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

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

63
static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
64
{
65 66 67 68
	ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
		    ios->oc->first_dev, ios->oc->numdevs, index,
		    ios->oc->ods);

69
	return ore_comp_dev(ios->oc, index);
70 71
}

72 73 74
static int  _get_io_state(struct ore_layout *layout,
			  struct ore_components *oc, unsigned numdevs,
			  struct ore_io_state **pios)
75
{
76
	struct ore_io_state *ios;
77 78

	/*TODO: Maybe use kmem_cach per sbi of size
79
	 * exofs_io_state_size(layout->s_numdevs)
80
	 */
81
	ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
82
	if (unlikely(!ios)) {
83
		ORE_DBGMSG("Failed kzalloc bytes=%d\n",
84
			   ore_io_state_size(numdevs));
85 86 87 88
		*pios = NULL;
		return -ENOMEM;
	}

89
	ios->layout = layout;
90
	ios->oc = oc;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	*pios = ios;
	return 0;
}

/* Allocate an io_state for only a single group of devices
 *
 * If a user needs to call ore_read/write() this version must be used becase it
 * allocates extra stuff for striping and raid.
 * The ore might decide to only IO less then @length bytes do to alignmets
 * and constrains as follows:
 * - The IO cannot cross group boundary.
 * - In raid5/6 The end of the IO must align at end of a stripe eg.
 *   (@offset + @length) % strip_size == 0. Or the complete range is within a
 *   single stripe.
 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
 *   And check the returned ios->length for max_io_size.)
 *
 * The caller must check returned ios->length (and/or ios->nr_pages) and
 * re-issue these pages that fall outside of ios->length
 */
int  ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
		      bool is_reading, u64 offset, u64 length,
		      struct ore_io_state **pios)
{
	struct ore_io_state *ios;
	unsigned numdevs = layout->group_width * layout->mirrors_p1;
	int ret;

	ret = _get_io_state(layout, oc, numdevs, pios);
	if (unlikely(ret))
		return ret;

	ios = *pios;
124
	ios->reading = is_reading;
125 126 127
	ios->offset = offset;

	if (length) {
128 129 130
		ore_calc_stripe_info(layout, offset, &ios->si);
		ios->length = (length <= ios->si.group_length) ? length :
							ios->si.group_length;
131 132
		ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
	}
133

134
	return 0;
135
}
B
Boaz Harrosh 已提交
136
EXPORT_SYMBOL(ore_get_rw_state);
137

138 139 140 141 142 143 144
/* Allocate an io_state for all the devices in the comps array
 *
 * This version of io_state allocation is used mostly by create/remove
 * and trunc where we currently need all the devices. The only wastful
 * bit is the read/write_attributes with no IO. Those sites should
 * be converted to use ore_get_rw_state() with length=0
 */
145
int  ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
146
		      struct ore_io_state **pios)
147
{
148
	return _get_io_state(layout, oc, oc->numdevs, pios);
149
}
B
Boaz Harrosh 已提交
150
EXPORT_SYMBOL(ore_get_io_state);
151

152
void ore_put_io_state(struct ore_io_state *ios)
153
{
154 155
	if (ios) {
		unsigned i;
156

157
		for (i = 0; i < ios->numdevs; i++) {
158
			struct ore_per_dev_state *per_dev = &ios->per_dev[i];
159 160 161 162 163 164 165 166

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

		kfree(ios);
167
	}
168
}
B
Boaz Harrosh 已提交
169
EXPORT_SYMBOL(ore_put_io_state);
170

171
static void _sync_done(struct ore_io_state *ios, void *p)
172 173
{
	struct completion *waiting = p;
174

175 176 177 178 179
	complete(waiting);
}

static void _last_io(struct kref *kref)
{
180 181
	struct ore_io_state *ios = container_of(
					kref, struct ore_io_state, kref);
182 183 184 185 186 187

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

static void _done_io(struct osd_request *or, void *p)
{
188
	struct ore_io_state *ios = p;
189 190 191 192

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

193
static int ore_io_execute(struct ore_io_state *ios)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
	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;

209
		ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
210
		if (unlikely(ret)) {
211
			ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
				     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);
233
		ret = ore_check_io(ios, NULL);
234
	}
235 236 237
	return ret;
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
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);
	}
}

253
int ore_check_io(struct ore_io_state *ios, u64 *resid)
254
{
255 256 257
	enum osd_err_priority acumulated_osd_err = 0;
	int acumulated_lin_err = 0;
	int i;
258

259 260
	for (i = 0; i < ios->numdevs; i++) {
		struct osd_sense_info osi;
261 262 263 264 265
		struct osd_request *or = ios->per_dev[i].or;
		int ret;

		if (unlikely(!or))
			continue;
266

267
		ret = osd_req_decode_sense(or, &osi);
268 269 270
		if (likely(!ret))
			continue;

271 272 273
		if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
			/* start read offset passed endof file */
			_clear_bio(ios->per_dev[i].bio);
274
			ORE_DBGMSG("start read offset passed end of file "
275
				"offset=0x%llx, length=0x%llx\n",
B
Boaz Harrosh 已提交
276 277
				_LLU(ios->per_dev[i].offset),
				_LLU(ios->per_dev[i].length));
278 279

			continue; /* we recovered */
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		}

		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 已提交
298
EXPORT_SYMBOL(ore_check_io);
299

B
Boaz Harrosh 已提交
300 301 302
/*
 * L - logical offset into the file
 *
B
Boaz Harrosh 已提交
303
 * U - The number of bytes in a stripe within a group
B
Boaz Harrosh 已提交
304 305 306
 *
 *	U = stripe_unit * group_width
 *
B
Boaz Harrosh 已提交
307 308
 * T - The number of bytes striped within a group of component objects
 *     (before advancing to the next group)
B
Boaz Harrosh 已提交
309
 *
B
Boaz Harrosh 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
 *	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 已提交
332 333 334
 *
 * C - The component index coresponding to L
 *
B
Boaz Harrosh 已提交
335 336
 *	C = (H - (N * U)) / stripe_unit + G * group_width
 *	[or (L % U) / stripe_unit + G * group_width]
B
Boaz Harrosh 已提交
337 338 339
 *
 * O - The component offset coresponding to L
 *
B
Boaz Harrosh 已提交
340
 *	O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
B
Boaz Harrosh 已提交
341
 */
342 343
static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
				 struct ore_striping_info *si)
B
Boaz Harrosh 已提交
344
{
345 346 347
	u32	stripe_unit = layout->stripe_unit;
	u32	group_width = layout->group_width;
	u64	group_depth = layout->group_depth;
B
Boaz Harrosh 已提交
348

B
Boaz Harrosh 已提交
349
	u32	U = stripe_unit * group_width;
B
Boaz Harrosh 已提交
350
	u64	T = U * group_depth;
351
	u64	S = T * layout->group_count;
B
Boaz Harrosh 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365
	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;
366
	si->dev *= layout->mirrors_p1;
B
Boaz Harrosh 已提交
367

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

B
Boaz Harrosh 已提交
370 371 372 373
	si->obj_offset = si->unit_off + (N * stripe_unit) +
				  (M * group_depth * stripe_unit);

	si->group_length = T - H;
374
	si->M = M;
B
Boaz Harrosh 已提交
375 376
}

377 378
static int _add_stripe_unit(struct ore_io_state *ios,  unsigned *cur_pg,
		unsigned pgbase, struct ore_per_dev_state *per_dev,
379
		int cur_len)
B
Boaz Harrosh 已提交
380
{
381
	unsigned pg = *cur_pg;
B
Boaz Harrosh 已提交
382
	struct request_queue *q =
383
			osd_request_queue(_ios_od(ios, per_dev->dev));
B
Boaz Harrosh 已提交
384 385
	unsigned len = cur_len;
	int ret;
B
Boaz Harrosh 已提交
386 387 388 389

	if (per_dev->bio == NULL) {
		unsigned pages_in_stripe = ios->layout->group_width *
					(ios->layout->stripe_unit / PAGE_SIZE);
390
		unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
B
Boaz Harrosh 已提交
391 392 393 394
						ios->layout->group_width;

		per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
		if (unlikely(!per_dev->bio)) {
395
			ORE_DBGMSG("Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
396
				     bio_size);
B
Boaz Harrosh 已提交
397 398
			ret = -ENOMEM;
			goto out;
B
Boaz Harrosh 已提交
399 400 401 402
		}
	}

	while (cur_len > 0) {
403 404
		unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
		unsigned added_len;
B
Boaz Harrosh 已提交
405

406 407
		BUG_ON(ios->nr_pages <= pg);
		cur_len -= pglen;
B
Boaz Harrosh 已提交
408

409 410
		added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
					    pglen, pgbase);
B
Boaz Harrosh 已提交
411 412 413 414
		if (unlikely(pglen != added_len)) {
			ret = -ENOMEM;
			goto out;
		}
415 416
		pgbase = 0;
		++pg;
B
Boaz Harrosh 已提交
417 418 419
	}
	BUG_ON(cur_len);

B
Boaz Harrosh 已提交
420
	per_dev->length += len;
421
	*cur_pg = pg;
B
Boaz Harrosh 已提交
422 423 424 425 426 427 428
	ret = 0;
out:	/* we fail the complete unit on an error eg don't advance
	 * per_dev->length and cur_pg. This means that we might have a bigger
	 * bio than the CDB requested length (per_dev->length). That's fine
	 * only the oposite is fatal.
	 */
	return ret;
B
Boaz Harrosh 已提交
429 430
}

431
static int _prepare_for_striping(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
432
{
433
	struct ore_striping_info *si = &ios->si;
B
Boaz Harrosh 已提交
434
	unsigned stripe_unit = ios->layout->stripe_unit;
B
Boaz Harrosh 已提交
435
	unsigned mirrors_p1 = ios->layout->mirrors_p1;
B
Boaz Harrosh 已提交
436
	unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
B
Boaz Harrosh 已提交
437
	unsigned dev = si->dev;
B
Boaz Harrosh 已提交
438 439
	unsigned first_dev = dev - (dev % devs_in_group);
	unsigned cur_pg = ios->pages_consumed;
440
	u64 length = ios->length;
441
	int ret = 0;
B
Boaz Harrosh 已提交
442

443 444 445 446 447 448 449
	if (!ios->pages) {
		ios->numdevs = ios->layout->mirrors_p1;
		return 0;
	}

	BUG_ON(length > si->group_length);

B
Boaz Harrosh 已提交
450
	while (length) {
451 452
		unsigned comp = dev - first_dev;
		struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
B
Boaz Harrosh 已提交
453
		unsigned cur_len, page_off = 0;
B
Boaz Harrosh 已提交
454 455

		if (!per_dev->length) {
B
Boaz Harrosh 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469
			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 已提交
470
		} else {
B
Boaz Harrosh 已提交
471
			cur_len = stripe_unit;
B
Boaz Harrosh 已提交
472
		}
B
Boaz Harrosh 已提交
473 474
		if (cur_len >= length)
			cur_len = length;
B
Boaz Harrosh 已提交
475

476 477
		ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
				       cur_len);
B
Boaz Harrosh 已提交
478 479 480
		if (unlikely(ret))
			goto out;

481 482
		dev += mirrors_p1;
		dev = (dev % devs_in_group) + first_dev;
B
Boaz Harrosh 已提交
483 484 485 486

		length -= cur_len;
	}
out:
487
	ios->numdevs = devs_in_group;
B
Boaz Harrosh 已提交
488
	ios->pages_consumed = cur_pg;
B
Boaz Harrosh 已提交
489 490 491 492 493 494 495
	if (unlikely(ret)) {
		if (length == ios->length)
			return ret;
		else
			ios->length -= length;
	}
	return 0;
B
Boaz Harrosh 已提交
496 497
}

498
int ore_create(struct ore_io_state *ios)
499 500 501
{
	int i, ret;

502
	for (i = 0; i < ios->oc->numdevs; i++) {
503 504
		struct osd_request *or;

505
		or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
506
		if (unlikely(!or)) {
507
			ORE_ERR("%s: osd_start_request failed\n", __func__);
508 509 510 511 512 513
			ret = -ENOMEM;
			goto out;
		}
		ios->per_dev[i].or = or;
		ios->numdevs++;

514
		osd_req_create_object(or, _ios_obj(ios, i));
515
	}
516
	ret = ore_io_execute(ios);
517 518 519 520

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

523
int ore_remove(struct ore_io_state *ios)
524 525 526
{
	int i, ret;

527
	for (i = 0; i < ios->oc->numdevs; i++) {
528 529
		struct osd_request *or;

530
		or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
531
		if (unlikely(!or)) {
532
			ORE_ERR("%s: osd_start_request failed\n", __func__);
533 534 535 536 537 538
			ret = -ENOMEM;
			goto out;
		}
		ios->per_dev[i].or = or;
		ios->numdevs++;

539
		osd_req_remove_object(or, _ios_obj(ios, i));
540
	}
541
	ret = ore_io_execute(ios);
542 543 544 545

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

548
static int _write_mirror(struct ore_io_state *ios, int cur_comp)
549
{
550
	struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
B
Boaz Harrosh 已提交
551 552 553
	unsigned dev = ios->per_dev[cur_comp].dev;
	unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
	int ret = 0;
554

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

B
Boaz Harrosh 已提交
558
	for (; cur_comp < last_comp; ++cur_comp, ++dev) {
559
		struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
560 561
		struct osd_request *or;

562
		or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
563
		if (unlikely(!or)) {
564
			ORE_ERR("%s: osd_start_request failed\n", __func__);
565 566 567
			ret = -ENOMEM;
			goto out;
		}
B
Boaz Harrosh 已提交
568
		per_dev->or = or;
569

570
		if (ios->pages) {
571 572
			struct bio *bio;

B
Boaz Harrosh 已提交
573
			if (per_dev != master_dev) {
B
Boaz Harrosh 已提交
574
				bio = bio_kmalloc(GFP_KERNEL,
B
Boaz Harrosh 已提交
575
						  master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
576
				if (unlikely(!bio)) {
577
					ORE_DBGMSG(
P
Paul Bolle 已提交
578
					      "Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
579
					      master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
580 581 582 583
					ret = -ENOMEM;
					goto out;
				}

B
Boaz Harrosh 已提交
584
				__bio_clone(bio, master_dev->bio);
B
Boaz Harrosh 已提交
585 586
				bio->bi_bdev = NULL;
				bio->bi_next = NULL;
587
				per_dev->offset = master_dev->offset;
B
Boaz Harrosh 已提交
588 589 590
				per_dev->length = master_dev->length;
				per_dev->bio =  bio;
				per_dev->dev = dev;
B
Boaz Harrosh 已提交
591
			} else {
B
Boaz Harrosh 已提交
592 593
				bio = master_dev->bio;
				/* FIXME: bio_set_dir() */
594
				bio->bi_rw |= REQ_WRITE;
B
Boaz Harrosh 已提交
595
			}
596

597 598
			osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
				      bio, per_dev->length);
599
			ORE_DBGMSG("write(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
600
				      "length=0x%llx dev=%d\n",
601 602
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
603
				     _LLU(per_dev->length), dev);
604
		} else if (ios->kern_buff) {
605 606 607 608 609 610 611 612 613
			per_dev->offset = ios->si.obj_offset;
			per_dev->dev = ios->si.dev + dev;

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

			ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
614 615
						 per_dev->offset,
						 ios->kern_buff, ios->length);
B
Boaz Harrosh 已提交
616 617
			if (unlikely(ret))
				goto out;
618
			ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
619
				      "length=0x%llx dev=%d\n",
620 621
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
622
				     _LLU(ios->length), per_dev->dev);
623
		} else {
624
			osd_req_set_attributes(or, _ios_obj(ios, dev));
625
			ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
626 627
				     _LLU(_ios_obj(ios, dev)->id),
				     ios->out_attr_len, dev);
628 629 630 631 632 633 634 635 636
		}

		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);
637
	}
638 639 640 641 642

out:
	return ret;
}

643
int ore_write(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
644 645 646 647 648 649 650 651 652
{
	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) {
653
		ret = _write_mirror(ios, i);
B
Boaz Harrosh 已提交
654 655 656 657
		if (unlikely(ret))
			return ret;
	}

658
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
659 660
	return ret;
}
B
Boaz Harrosh 已提交
661
EXPORT_SYMBOL(ore_write);
B
Boaz Harrosh 已提交
662

663
static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
664
{
B
Boaz Harrosh 已提交
665
	struct osd_request *or;
666
	struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
667 668
	struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
	unsigned first_dev = (unsigned)obj->id;
669

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

B
Boaz Harrosh 已提交
673
	first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
674
	or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
B
Boaz Harrosh 已提交
675
	if (unlikely(!or)) {
676
		ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
677 678 679 680
		return -ENOMEM;
	}
	per_dev->or = or;

681
	if (ios->pages) {
682
		osd_req_read(or, obj, per_dev->offset,
B
Boaz Harrosh 已提交
683
				per_dev->bio, per_dev->length);
684
		ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
685
			     " dev=%d\n", _LLU(obj->id),
B
Boaz Harrosh 已提交
686
			     _LLU(per_dev->offset), _LLU(per_dev->length),
B
Boaz Harrosh 已提交
687 688
			     first_dev);
	} else {
689 690
		BUG_ON(ios->kern_buff);

691
		osd_req_get_attributes(or, obj);
692
		ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
693 694
			      _LLU(obj->id),
			      ios->in_attr_len, first_dev);
B
Boaz Harrosh 已提交
695 696 697
	}
	if (ios->out_attr)
		osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
698

B
Boaz Harrosh 已提交
699 700
	if (ios->in_attr)
		osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
701

B
Boaz Harrosh 已提交
702 703 704
	return 0;
}

705
int ore_read(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
706 707 708 709 710 711 712 713 714
{
	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) {
715
		ret = _read_mirror(ios, i);
B
Boaz Harrosh 已提交
716 717 718 719
		if (unlikely(ret))
			return ret;
	}

720
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
721
	return ret;
722
}
B
Boaz Harrosh 已提交
723
EXPORT_SYMBOL(ore_read);
724

725
int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
726 727 728 729 730 731 732
{
	struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
	void *iter = NULL;
	int nelem;

	do {
		nelem = 1;
733 734
		osd_req_decode_get_attr_list(ios->per_dev[0].or,
					     &cur_attr, &nelem, &iter);
735 736 737 738 739 740 741 742 743 744
		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 已提交
745
EXPORT_SYMBOL(extract_attr_from_ios);
746

747
static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
B
Boaz Harrosh 已提交
748 749 750 751 752
			     struct osd_attr *attr)
{
	int last_comp = cur_comp + ios->layout->mirrors_p1;

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

756
		or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
B
Boaz Harrosh 已提交
757
		if (unlikely(!or)) {
758
			ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
759 760 761 762
			return -ENOMEM;
		}
		per_dev->or = or;

763
		osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
B
Boaz Harrosh 已提交
764 765 766 767 768 769
		osd_req_add_set_attr_list(or, attr, 1);
	}

	return 0;
}

770
struct _trunc_info {
771
	struct ore_striping_info si;
772 773 774 775 776 777 778
	u64 prev_group_obj_off;
	u64 next_group_obj_off;

	unsigned first_group_dev;
	unsigned nex_group_dev;
};

779 780
static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
			     struct _trunc_info *ti)
781 782 783
{
	unsigned stripe_unit = layout->stripe_unit;

784
	ore_calc_stripe_info(layout, file_offset, &ti->si);
785 786 787 788 789 790 791 792

	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;
}

793
int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
794
		   u64 size)
795
{
796
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
797 798 799 800
	struct exofs_trunc_attr {
		struct osd_attr attr;
		__be64 newsize;
	} *size_attrs;
801
	struct _trunc_info ti;
802 803
	int i, ret;

804
	ret = ore_get_io_state(layout, oc, &ios);
B
Boaz Harrosh 已提交
805 806 807
	if (unlikely(ret))
		return ret;

808 809
	_calc_trunk_info(ios->layout, size, &ti);

810
	size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
B
Boaz Harrosh 已提交
811 812 813 814 815
			     GFP_KERNEL);
	if (unlikely(!size_attrs)) {
		ret = -ENOMEM;
		goto out;
	}
816

817
	ios->numdevs = ios->oc->numdevs;
818

819
	for (i = 0; i < ios->numdevs; ++i) {
B
Boaz Harrosh 已提交
820 821
		struct exofs_trunc_attr *size_attr = &size_attrs[i];
		u64 obj_size;
822

823 824 825 826 827 828 829 830 831 832 833
		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;
834

B
Boaz Harrosh 已提交
835 836 837 838
		size_attr->newsize = cpu_to_be64(obj_size);
		size_attr->attr = g_attr_logical_length;
		size_attr->attr.val_ptr = &size_attr->newsize;

839
		ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
840
			     _LLU(oc->comps->obj.id), _LLU(obj_size), i);
B
Boaz Harrosh 已提交
841 842 843 844
		ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
					&size_attr->attr);
		if (unlikely(ret))
			goto out;
845
	}
846
	ret = ore_io_execute(ios);
847 848

out:
B
Boaz Harrosh 已提交
849
	kfree(size_attrs);
850
	ore_put_io_state(ios);
851 852
	return ret;
}
B
Boaz Harrosh 已提交
853
EXPORT_SYMBOL(ore_truncate);
854 855 856

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