ore.c 21.0 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
	return ore_comp_dev(ios->oc, index);
66 67
}

68 69 70
static int  _get_io_state(struct ore_layout *layout,
			  struct ore_components *oc, unsigned numdevs,
			  struct ore_io_state **pios)
71
{
72
	struct ore_io_state *ios;
73 74

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

85
	ios->layout = layout;
86
	ios->oc = oc;
87 88 89 90 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
	*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;
120
	ios->reading = is_reading;
121 122 123
	ios->offset = offset;

	if (length) {
124 125 126
		ore_calc_stripe_info(layout, offset, &ios->si);
		ios->length = (length <= ios->si.group_length) ? length :
							ios->si.group_length;
127 128
		ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
	}
129

130
	return 0;
131
}
B
Boaz Harrosh 已提交
132
EXPORT_SYMBOL(ore_get_rw_state);
133

134 135 136 137 138 139 140
/* 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
 */
141
int  ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
142
		      struct ore_io_state **pios)
143
{
144
	return _get_io_state(layout, oc, oc->numdevs, pios);
145
}
B
Boaz Harrosh 已提交
146
EXPORT_SYMBOL(ore_get_io_state);
147

148
void ore_put_io_state(struct ore_io_state *ios)
149
{
150 151
	if (ios) {
		unsigned i;
152

153
		for (i = 0; i < ios->numdevs; i++) {
154
			struct ore_per_dev_state *per_dev = &ios->per_dev[i];
155 156 157 158 159 160 161 162

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

		kfree(ios);
163
	}
164
}
B
Boaz Harrosh 已提交
165
EXPORT_SYMBOL(ore_put_io_state);
166

167
static void _sync_done(struct ore_io_state *ios, void *p)
168 169
{
	struct completion *waiting = p;
170

171 172 173 174 175
	complete(waiting);
}

static void _last_io(struct kref *kref)
{
176 177
	struct ore_io_state *ios = container_of(
					kref, struct ore_io_state, kref);
178 179 180 181 182 183

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

static void _done_io(struct osd_request *or, void *p)
{
184
	struct ore_io_state *ios = p;
185 186 187 188

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

189
static int ore_io_execute(struct ore_io_state *ios)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
	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;

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

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
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);
	}
}

249
int ore_check_io(struct ore_io_state *ios, u64 *resid)
250
{
251 252 253
	enum osd_err_priority acumulated_osd_err = 0;
	int acumulated_lin_err = 0;
	int i;
254

255 256
	for (i = 0; i < ios->numdevs; i++) {
		struct osd_sense_info osi;
257 258 259 260 261
		struct osd_request *or = ios->per_dev[i].or;
		int ret;

		if (unlikely(!or))
			continue;
262

263
		ret = osd_req_decode_sense(or, &osi);
264 265 266
		if (likely(!ret))
			continue;

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

			continue; /* we recovered */
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		}

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

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

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

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

B
Boaz Harrosh 已提交
366 367 368 369
	si->obj_offset = si->unit_off + (N * stripe_unit) +
				  (M * group_depth * stripe_unit);

	si->group_length = T - H;
370
	si->M = M;
B
Boaz Harrosh 已提交
371 372
}

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

	per_dev->length += cur_len;

	if (per_dev->bio == NULL) {
		unsigned pages_in_stripe = ios->layout->group_width *
					(ios->layout->stripe_unit / PAGE_SIZE);
386
		unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
B
Boaz Harrosh 已提交
387 388 389 390
						ios->layout->group_width;

		per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
		if (unlikely(!per_dev->bio)) {
391
			ORE_DBGMSG("Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
392 393 394 395 396 397
				     bio_size);
			return -ENOMEM;
		}
	}

	while (cur_len > 0) {
398 399
		unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
		unsigned added_len;
B
Boaz Harrosh 已提交
400

401 402
		BUG_ON(ios->nr_pages <= pg);
		cur_len -= pglen;
B
Boaz Harrosh 已提交
403

404 405 406
		added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
					    pglen, pgbase);
		if (unlikely(pglen != added_len))
B
Boaz Harrosh 已提交
407
			return -ENOMEM;
408 409
		pgbase = 0;
		++pg;
B
Boaz Harrosh 已提交
410 411 412
	}
	BUG_ON(cur_len);

413
	*cur_pg = pg;
B
Boaz Harrosh 已提交
414 415 416
	return 0;
}

417
static int _prepare_for_striping(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
418
{
419
	struct ore_striping_info *si = &ios->si;
B
Boaz Harrosh 已提交
420
	unsigned stripe_unit = ios->layout->stripe_unit;
B
Boaz Harrosh 已提交
421
	unsigned mirrors_p1 = ios->layout->mirrors_p1;
B
Boaz Harrosh 已提交
422
	unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
B
Boaz Harrosh 已提交
423
	unsigned dev = si->dev;
B
Boaz Harrosh 已提交
424 425
	unsigned first_dev = dev - (dev % devs_in_group);
	unsigned cur_pg = ios->pages_consumed;
426
	u64 length = ios->length;
427
	int ret = 0;
B
Boaz Harrosh 已提交
428

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	if (!ios->pages) {
		if (ios->kern_buff) {
			struct ore_per_dev_state *per_dev = &ios->per_dev[0];

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

	BUG_ON(length > si->group_length);

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

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

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

478 479
		dev += mirrors_p1;
		dev = (dev % devs_in_group) + first_dev;
B
Boaz Harrosh 已提交
480 481 482 483

		length -= cur_len;
	}
out:
484
	ios->numdevs = devs_in_group;
B
Boaz Harrosh 已提交
485
	ios->pages_consumed = cur_pg;
B
Boaz Harrosh 已提交
486 487 488
	return ret;
}

489
int ore_create(struct ore_io_state *ios)
490 491 492
{
	int i, ret;

493
	for (i = 0; i < ios->oc->numdevs; i++) {
494 495
		struct osd_request *or;

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

505
		osd_req_create_object(or, _ios_obj(ios, i));
506
	}
507
	ret = ore_io_execute(ios);
508 509 510 511

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

514
int ore_remove(struct ore_io_state *ios)
515 516 517
{
	int i, ret;

518
	for (i = 0; i < ios->oc->numdevs; i++) {
519 520
		struct osd_request *or;

521
		or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
522
		if (unlikely(!or)) {
523
			ORE_ERR("%s: osd_start_request failed\n", __func__);
524 525 526 527 528 529
			ret = -ENOMEM;
			goto out;
		}
		ios->per_dev[i].or = or;
		ios->numdevs++;

530
		osd_req_remove_object(or, _ios_obj(ios, i));
531
	}
532
	ret = ore_io_execute(ios);
533 534 535 536

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

539
static int _write_mirror(struct ore_io_state *ios, int cur_comp)
540
{
541
	struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
B
Boaz Harrosh 已提交
542 543 544
	unsigned dev = ios->per_dev[cur_comp].dev;
	unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
	int ret = 0;
545

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

B
Boaz Harrosh 已提交
549
	for (; cur_comp < last_comp; ++cur_comp, ++dev) {
550
		struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
551 552
		struct osd_request *or;

553
		or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
554
		if (unlikely(!or)) {
555
			ORE_ERR("%s: osd_start_request failed\n", __func__);
556 557 558
			ret = -ENOMEM;
			goto out;
		}
B
Boaz Harrosh 已提交
559 560
		per_dev->or = or;
		per_dev->offset = master_dev->offset;
561

562
		if (ios->pages) {
563 564
			struct bio *bio;

B
Boaz Harrosh 已提交
565
			if (per_dev != master_dev) {
B
Boaz Harrosh 已提交
566
				bio = bio_kmalloc(GFP_KERNEL,
B
Boaz Harrosh 已提交
567
						  master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
568
				if (unlikely(!bio)) {
569
					ORE_DBGMSG(
P
Paul Bolle 已提交
570
					      "Failed to allocate BIO size=%u\n",
B
Boaz Harrosh 已提交
571
					      master_dev->bio->bi_max_vecs);
B
Boaz Harrosh 已提交
572 573 574 575
					ret = -ENOMEM;
					goto out;
				}

B
Boaz Harrosh 已提交
576
				__bio_clone(bio, master_dev->bio);
B
Boaz Harrosh 已提交
577 578
				bio->bi_bdev = NULL;
				bio->bi_next = NULL;
B
Boaz Harrosh 已提交
579 580 581
				per_dev->length = master_dev->length;
				per_dev->bio =  bio;
				per_dev->dev = dev;
B
Boaz Harrosh 已提交
582
			} else {
B
Boaz Harrosh 已提交
583 584
				bio = master_dev->bio;
				/* FIXME: bio_set_dir() */
585
				bio->bi_rw |= REQ_WRITE;
B
Boaz Harrosh 已提交
586
			}
587

588 589
			osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
				      bio, per_dev->length);
590
			ORE_DBGMSG("write(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
591
				      "length=0x%llx dev=%d\n",
592 593
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
594
				     _LLU(per_dev->length), dev);
595
		} else if (ios->kern_buff) {
596 597 598
			ret = osd_req_write_kern(or, _ios_obj(ios, dev),
						 per_dev->offset,
						 ios->kern_buff, ios->length);
B
Boaz Harrosh 已提交
599 600
			if (unlikely(ret))
				goto out;
601
			ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
602
				      "length=0x%llx dev=%d\n",
603 604
				     _LLU(_ios_obj(ios, dev)->id),
				     _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
605
				     _LLU(ios->length), dev);
606
		} else {
607
			osd_req_set_attributes(or, _ios_obj(ios, dev));
608
			ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
609 610
				     _LLU(_ios_obj(ios, dev)->id),
				     ios->out_attr_len, dev);
611 612 613 614 615 616 617 618 619
		}

		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);
620
	}
621 622 623 624 625

out:
	return ret;
}

626
int ore_write(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
627 628 629 630 631 632 633 634 635
{
	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) {
636
		ret = _write_mirror(ios, i);
B
Boaz Harrosh 已提交
637 638 639 640
		if (unlikely(ret))
			return ret;
	}

641
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
642 643
	return ret;
}
B
Boaz Harrosh 已提交
644
EXPORT_SYMBOL(ore_write);
B
Boaz Harrosh 已提交
645

646
static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
647
{
B
Boaz Harrosh 已提交
648
	struct osd_request *or;
649
	struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
650 651
	struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
	unsigned first_dev = (unsigned)obj->id;
652

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

B
Boaz Harrosh 已提交
656
	first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
657
	or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
B
Boaz Harrosh 已提交
658
	if (unlikely(!or)) {
659
		ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
660 661 662 663
		return -ENOMEM;
	}
	per_dev->or = or;

664
	if (ios->pages) {
665
		osd_req_read(or, obj, per_dev->offset,
B
Boaz Harrosh 已提交
666
				per_dev->bio, per_dev->length);
667
		ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
668
			     " dev=%d\n", _LLU(obj->id),
B
Boaz Harrosh 已提交
669
			     _LLU(per_dev->offset), _LLU(per_dev->length),
B
Boaz Harrosh 已提交
670 671
			     first_dev);
	} else if (ios->kern_buff) {
672
		int ret = osd_req_read_kern(or, obj, per_dev->offset,
B
Boaz Harrosh 已提交
673
					    ios->kern_buff, ios->length);
674
		ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
B
Boaz Harrosh 已提交
675
			      "length=0x%llx dev=%d ret=>%d\n",
676
			      _LLU(obj->id), _LLU(per_dev->offset),
B
Boaz Harrosh 已提交
677 678 679 680
			      _LLU(ios->length), first_dev, ret);
		if (unlikely(ret))
			return ret;
	} else {
681
		osd_req_get_attributes(or, obj);
682
		ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
683 684
			      _LLU(obj->id),
			      ios->in_attr_len, first_dev);
B
Boaz Harrosh 已提交
685 686 687
	}
	if (ios->out_attr)
		osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
688

B
Boaz Harrosh 已提交
689 690
	if (ios->in_attr)
		osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
691

B
Boaz Harrosh 已提交
692 693 694
	return 0;
}

695
int ore_read(struct ore_io_state *ios)
B
Boaz Harrosh 已提交
696 697 698 699 700 701 702 703 704
{
	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) {
705
		ret = _read_mirror(ios, i);
B
Boaz Harrosh 已提交
706 707 708 709
		if (unlikely(ret))
			return ret;
	}

710
	ret = ore_io_execute(ios);
B
Boaz Harrosh 已提交
711
	return ret;
712
}
B
Boaz Harrosh 已提交
713
EXPORT_SYMBOL(ore_read);
714

715
int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
716 717 718 719 720 721 722
{
	struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
	void *iter = NULL;
	int nelem;

	do {
		nelem = 1;
723 724
		osd_req_decode_get_attr_list(ios->per_dev[0].or,
					     &cur_attr, &nelem, &iter);
725 726 727 728 729 730 731 732 733 734
		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 已提交
735
EXPORT_SYMBOL(extract_attr_from_ios);
736

737
static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
B
Boaz Harrosh 已提交
738 739 740 741 742
			     struct osd_attr *attr)
{
	int last_comp = cur_comp + ios->layout->mirrors_p1;

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

746
		or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
B
Boaz Harrosh 已提交
747
		if (unlikely(!or)) {
748
			ORE_ERR("%s: osd_start_request failed\n", __func__);
B
Boaz Harrosh 已提交
749 750 751 752
			return -ENOMEM;
		}
		per_dev->or = or;

753
		osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
B
Boaz Harrosh 已提交
754 755 756 757 758 759
		osd_req_add_set_attr_list(or, attr, 1);
	}

	return 0;
}

760
struct _trunc_info {
761
	struct ore_striping_info si;
762 763 764 765 766 767 768
	u64 prev_group_obj_off;
	u64 next_group_obj_off;

	unsigned first_group_dev;
	unsigned nex_group_dev;
};

769 770
static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
			     struct _trunc_info *ti)
771 772 773
{
	unsigned stripe_unit = layout->stripe_unit;

774
	ore_calc_stripe_info(layout, file_offset, &ti->si);
775 776 777 778 779 780 781 782

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

783
int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
784
		   u64 size)
785
{
786
	struct ore_io_state *ios;
B
Boaz Harrosh 已提交
787 788 789 790
	struct exofs_trunc_attr {
		struct osd_attr attr;
		__be64 newsize;
	} *size_attrs;
791
	struct _trunc_info ti;
792 793
	int i, ret;

794
	ret = ore_get_io_state(layout, oc, &ios);
B
Boaz Harrosh 已提交
795 796 797
	if (unlikely(ret))
		return ret;

798 799
	_calc_trunk_info(ios->layout, size, &ti);

800
	size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
B
Boaz Harrosh 已提交
801 802 803 804 805
			     GFP_KERNEL);
	if (unlikely(!size_attrs)) {
		ret = -ENOMEM;
		goto out;
	}
806

807
	ios->numdevs = ios->oc->numdevs;
808

809
	for (i = 0; i < ios->numdevs; ++i) {
B
Boaz Harrosh 已提交
810 811
		struct exofs_trunc_attr *size_attr = &size_attrs[i];
		u64 obj_size;
812

813 814 815 816 817 818 819 820 821 822 823
		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;
824

B
Boaz Harrosh 已提交
825 826 827 828
		size_attr->newsize = cpu_to_be64(obj_size);
		size_attr->attr = g_attr_logical_length;
		size_attr->attr.val_ptr = &size_attr->newsize;

829
		ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
830
			     _LLU(oc->comps->obj.id), _LLU(obj_size), i);
B
Boaz Harrosh 已提交
831 832 833 834
		ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
					&size_attr->attr);
		if (unlikely(ret))
			goto out;
835
	}
836
	ret = ore_io_execute(ios);
837 838

out:
B
Boaz Harrosh 已提交
839
	kfree(size_attrs);
840
	ore_put_io_state(ios);
841 842
	return ret;
}
B
Boaz Harrosh 已提交
843
EXPORT_SYMBOL(ore_truncate);
844 845 846

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